blob: fd3e8f9b26ebafb4477bcfa8a3f457fea642e2e0 [file] [log] [blame]
Marat Dukhan346a9e52019-11-15 09:06:30 -08001// Copyright 2019 Google LLC
2//
3// This source code is licensed under the BSD-style license found in the
4// LICENSE file in the root directory of this source tree.
5
6#include <algorithm>
7#include <cfloat>
8#include <cmath>
9#include <functional>
10#include <random>
11#include <vector>
12
13#include <benchmark/benchmark.h>
14#include <fp16/fp16.h>
15
16#include <xnnpack/AlignedAllocator.h>
17#include <xnnpack/common.h>
18#include <xnnpack/math-stubs.h>
19
20
21static void SigmoidError(benchmark::State& state,
22 xnn_f32_unary_math_function sigmoid,
23 size_t tile_size)
24{
25 // The smallest x for which sigmoidf(x) is normalized (-0x1.5D589Ep+6f).
26 const uint32_t min_input = 0xC2AEAC4F;
27 // The largest x for which sigmoidf(x) is not 1.0f (0x1.154244p+4f).
28 const uint32_t max_input = 0x418AA122;
29 // Number of tiles in one block of inputs/outputs. Combining multiple tiles in a block reduce function call overhead.
30 const size_t num_tiles = 100;
31
32 double max_ulp_error = 0.0;
33 std::vector<float, AlignedAllocator<float, 64>> x(tile_size * num_tiles);
34 std::vector<float, AlignedAllocator<float, 64>> y(tile_size * num_tiles);
35 for (auto _ : state) {
36 for (uint32_t n = min_input; int32_t(n) < 0; n -= tile_size * num_tiles) {
37 for (uint32_t i = 0; i < tile_size * num_tiles; i++) {
38 x[i] = fp32_from_bits(std::max<uint32_t>(n - i, 0x80000000));
39 }
40 std::fill(y.begin(), y.end(), std::nanf(""));
41
42 sigmoid(tile_size * num_tiles * sizeof(float), x.data(), y.data());
43
44 for (uint32_t i = 0; i < tile_size * num_tiles; i++) {
45 const double e_ref = std::exp(double(x[i]));
46 const double y_ref = e_ref / (e_ref + 1.0);
47 const double abs_error = std::abs(y_ref - double(y[i]));
48 const float y_abs = std::abs(y_ref);
49 const float y_ulp = fp32_from_bits(fp32_to_bits(y_abs) + 1) - y_abs;
50 max_ulp_error = std::max<double>(max_ulp_error, abs_error / y_ulp);
51 }
52 }
53 for (uint32_t n = 0; n < max_input; n += tile_size * num_tiles) {
54 for (uint32_t i = 0; i < tile_size * num_tiles; i++) {
55 x[i] = fp32_from_bits(std::min<uint32_t>(n + i, max_input));
56 }
57 std::fill(y.begin(), y.end(), std::nanf(""));
58
59 sigmoid(tile_size * num_tiles * sizeof(float), x.data(), y.data());
60
61 for (uint32_t i = 0; i < tile_size * num_tiles; i++) {
62 const double y_ref = 1.0 / (1.0 + std::exp(-double(x[i])));
63 const double abs_error = std::abs(y_ref - double(y[i]));
64 const float y_abs = std::abs(y_ref);
65 const float y_ulp = fp32_from_bits(fp32_to_bits(y_abs) + 1) - y_abs;
66 max_ulp_error = std::max<double>(max_ulp_error, abs_error / y_ulp);
67 }
68 }
69 }
70
71 state.counters["ULPERROR"] = benchmark::Counter(max_ulp_error);
72}
73
74#if XNN_ARCH_ARM || XNN_ARCH_ARM64
75 static void f32_sigmoid__neonfma_p5(benchmark::State& state) {
76 SigmoidError(state, xnn_math_f32_sigmoid__neonfma_p5, 4);
77 }
78
79 BENCHMARK(f32_sigmoid__neonfma_p5)->Unit(benchmark::kMillisecond)->Iterations(1);
80#endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
81
82#ifndef XNNPACK_BENCHMARK_NO_MAIN
83BENCHMARK_MAIN();
84#endif