1 // 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 <memory>
11 #include <numeric>
12 #include <random>
13 #include <vector>
14
15 #include <cpuinfo.h>
16 #include <pthreadpool.h>
17
18 #include <benchmark/benchmark.h>
19 #include <fp16/fp16.h>
20
21 #include "bench/utils.h"
22 #include <xnnpack/aligned-allocator.h>
23 #include <xnnpack/common.h>
24 #include <xnnpack/math.h>
25 #include <xnnpack/math-stubs.h>
26
27
28 struct ComputeErrorContext {
29 const float* input;
30 const float* output;
31 float* error;
32 };
33
ComputeError(struct ComputeErrorContext * context,size_t start,size_t range)34 static void ComputeError(
35 struct ComputeErrorContext* context,
36 size_t start,
37 size_t range)
38 {
39 const float* input = context->input;
40 const float* output = context->output;
41 float* error = context->error;
42 for (size_t i = start; i < start + range; i++) {
43 const double output_ref = std::exp(double(input[i]));
44 const double abs_error = std::abs(output_ref - double(output[i]));
45 const float output_abs = std::abs(output_ref);
46 const float output_ulp = uint32_as_float(float_as_uint32(output_abs) + 1) - output_abs;
47 error[i] = float(abs_error / output_ulp);
48 }
49 }
50
ExpError(benchmark::State & state,xnn_f32_unary_math_function exp,benchmark::utils::IsaCheckFunction isa_check=nullptr)51 static void ExpError(
52 benchmark::State& state,
53 xnn_f32_unary_math_function exp,
54 benchmark::utils::IsaCheckFunction isa_check = nullptr)
55 {
56 if (!cpuinfo_initialize()) {
57 state.SkipWithError("failed cpuinfo init");
58 return;
59 }
60 if (isa_check && !isa_check(state)) {
61 return;
62 }
63
64 // The smallest x for which expf(x) is non-zero (-0x1.9FE368p+6f).
65 const uint32_t min_input = 0xC2CFF1B4;
66 // The largest x for which expf(x) is finite (0x1.62E42Ep6f).
67 const uint32_t max_input = 0x42B17217;
68 // Number of elements in one block of inputs/outputs.
69 // Combining multiple elements in a block reduce function call overhead.
70 const size_t block_size = 16384;
71 // Number of elements in one parallelization tile. Worker threads process this many elements in each task.
72 const size_t tile_size = 64;
73
74 uint32_t num_threads = cpuinfo_get_cores_count();
75 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
76 // Use all cores except for the least performant cluster
77 if (cpuinfo_get_clusters_count() > 1) {
78 num_threads -= cpuinfo_get_cluster(cpuinfo_get_clusters_count() - 1)->core_count;
79 }
80 #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
81
82 std::unique_ptr<pthreadpool, decltype(&pthreadpool_destroy)> threadpool(
83 pthreadpool_create(num_threads), pthreadpool_destroy);
84
85 std::vector<float, AlignedAllocator<float, 64>> x(block_size);
86 std::vector<float, AlignedAllocator<float, 64>> y(block_size);
87 std::vector<float> ulp_error(block_size);
88 float max_ulp_error = 0.0f;
89
90 ComputeErrorContext context;
91 context.input = x.data();
92 context.output = y.data();
93 context.error = ulp_error.data();
94 for (auto _ : state) {
95 for (uint32_t n = min_input; int32_t(n) < 0; n -= block_size) {
96 for (uint32_t i = 0; i < block_size; i++) {
97 x[i] = uint32_as_float(std::max<uint32_t>(n - i, 0x80000000));
98 }
99 std::fill(y.begin(), y.end(), std::nanf(""));
100
101 exp(block_size * sizeof(float), x.data(), y.data());
102
103 pthreadpool_parallelize_1d_tile_1d(
104 threadpool.get(),
105 reinterpret_cast<pthreadpool_task_1d_tile_1d_t>(ComputeError),
106 static_cast<void*>(&context),
107 block_size, tile_size, 0 /* flags */);
108
109 max_ulp_error = std::accumulate(ulp_error.cbegin(), ulp_error.cend(), max_ulp_error,
110 static_cast<const float& (*)(const float&, const float&)>(std::max<float>));
111 }
112 for (uint32_t n = 0; n < max_input; n += block_size) {
113 for (uint32_t i = 0; i < block_size; i++) {
114 x[i] = uint32_as_float(std::min<uint32_t>(n + i, max_input));
115 }
116 std::fill(y.begin(), y.end(), std::nanf(""));
117
118 exp(block_size * sizeof(float), x.data(), y.data());
119
120 pthreadpool_parallelize_1d_tile_1d(
121 threadpool.get(),
122 reinterpret_cast<pthreadpool_task_1d_tile_1d_t>(ComputeError),
123 static_cast<void*>(&context),
124 block_size, tile_size, 0 /* flags */);
125
126 max_ulp_error = std::accumulate(ulp_error.cbegin(), ulp_error.cend(), max_ulp_error,
127 static_cast<const float& (*)(const float&, const float&)>(std::max<float>));
128 }
129 }
130
131 state.counters["ULPERROR"] = benchmark::Counter(max_ulp_error);
132 }
133
134 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
135 BENCHMARK_CAPTURE(ExpError, neonfma_rr2_lut64_p2,
136 xnn_math_f32_exp__neonfma_rr2_lut64_p2,
137 benchmark::utils::CheckNEONFMA)
138 ->Unit(benchmark::kMillisecond)
139 ->Iterations(1);
140 BENCHMARK_CAPTURE(ExpError, neonfma_rr2_p5,
141 xnn_math_f32_exp__neonfma_rr2_p5,
142 benchmark::utils::CheckNEONFMA)
143 ->Unit(benchmark::kMillisecond)
144 ->Iterations(1);
145 #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
146
147 #if XNN_ARCH_X86 || XNN_ARCH_X86_64
148 BENCHMARK_CAPTURE(ExpError, avx512f_rr2_lut16_p3_perm,
149 xnn_math_f32_exp__avx512f_rr2_lut16_p3_perm,
150 benchmark::utils::CheckAVX512F)
151 ->Unit(benchmark::kMillisecond)
152 ->Iterations(1);
153 BENCHMARK_CAPTURE(ExpError, avx512f_rr2_lut16_p3_perm_scalef,
154 xnn_math_f32_exp__avx512f_rr2_lut16_p3_perm_scalef,
155 benchmark::utils::CheckAVX512F)
156 ->Unit(benchmark::kMillisecond)
157 ->Iterations(1);
158 BENCHMARK_CAPTURE(ExpError, avx512f_rr2_lut32_p2_perm2,
159 xnn_math_f32_exp__avx512f_rr2_lut32_p2_perm2,
160 benchmark::utils::CheckAVX512F)
161 ->Unit(benchmark::kMillisecond)
162 ->Iterations(1);
163 BENCHMARK_CAPTURE(ExpError, avx512f_rr2_lut32_p2_perm2_scalef,
164 xnn_math_f32_exp__avx512f_rr2_lut32_p2_perm2_scalef,
165 benchmark::utils::CheckAVX512F)
166 ->Unit(benchmark::kMillisecond)
167 ->Iterations(1);
168 BENCHMARK_CAPTURE(ExpError, avx512f_rr2_p5,
169 xnn_math_f32_exp__avx512f_rr2_p5,
170 benchmark::utils::CheckAVX512F)
171 ->Unit(benchmark::kMillisecond)
172 ->Iterations(1);
173 BENCHMARK_CAPTURE(ExpError, avx512f_rr2_p5_scalef,
174 xnn_math_f32_exp__avx512f_rr2_p5_scalef,
175 benchmark::utils::CheckAVX512F)
176 ->Unit(benchmark::kMillisecond)
177 ->Iterations(1);
178
179 BENCHMARK_CAPTURE(ExpError, avx2_rr2_lut8_p3_perm,
180 xnn_math_f32_exp__avx2_rr2_lut8_p3_perm,
181 benchmark::utils::CheckAVX2)
182 ->Unit(benchmark::kMillisecond)
183 ->Iterations(1);
184 BENCHMARK_CAPTURE(ExpError, avx2_rr2_lut8_p4_perm,
185 xnn_math_f32_exp__avx2_rr2_lut8_p4_perm,
186 benchmark::utils::CheckAVX2)
187 ->Unit(benchmark::kMillisecond)
188 ->Iterations(1);
189 BENCHMARK_CAPTURE(ExpError, avx2_rr2_p5,
190 xnn_math_f32_exp__avx2_rr2_p5,
191 benchmark::utils::CheckAVX2)
192 ->Unit(benchmark::kMillisecond)
193 ->Iterations(1);
194
195 BENCHMARK_CAPTURE(ExpError, avx_rr2_p5,
196 xnn_math_f32_exp__avx_rr2_p5,
197 benchmark::utils::CheckAVX)
198 ->Unit(benchmark::kMillisecond)
199 ->Iterations(1);
200
201 BENCHMARK_CAPTURE(ExpError, sse2_rr2_lut64_p2,
202 xnn_math_f32_exp__sse2_rr2_lut64_p2)
203 ->Unit(benchmark::kMillisecond)
204 ->Iterations(1);
205 BENCHMARK_CAPTURE(ExpError, sse2_rr2_p5,
206 xnn_math_f32_exp__sse2_rr2_p5)
207 ->Unit(benchmark::kMillisecond)
208 ->Iterations(1);
209 #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
210
211 #ifndef XNNPACK_BENCHMARK_NO_MAIN
212 BENCHMARK_MAIN();
213 #endif
214