1 // Copyright 2020 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::expm1(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
ExpM1Error(benchmark::State & state,xnn_f32_unary_math_function expm1,benchmark::utils::IsaCheckFunction isa_check=nullptr)51 static void ExpM1Error(benchmark::State& state,
52 xnn_f32_unary_math_function expm1,
53 benchmark::utils::IsaCheckFunction isa_check = nullptr)
54 {
55 if (!cpuinfo_initialize()) {
56 state.SkipWithError("failed cpuinfo init");
57 return;
58 }
59 if (isa_check && !isa_check(state)) {
60 return;
61 }
62
63 // The smallest x for which expm1f(x) is not saturated at -1 (-0x1.154244p+4f).
64 const uint32_t min_input = UINT32_C(0xC18AA122);
65 // Number of elements in one block of inputs/outputs.
66 // Combining multiple elements in a block reduce function call overhead.
67 const size_t block_size = 16384;
68 // Number of elements in one parallelization tile. Worker threads process this many elements in each task.
69 const size_t tile_size = 64;
70
71 uint32_t num_threads = cpuinfo_get_cores_count();
72 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
73 // Use all cores except for the least performant cluster
74 if (cpuinfo_get_clusters_count() > 1) {
75 num_threads -= cpuinfo_get_cluster(cpuinfo_get_clusters_count() - 1)->core_count;
76 }
77 #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
78
79 std::unique_ptr<pthreadpool, decltype(&pthreadpool_destroy)> threadpool(
80 pthreadpool_create(num_threads), pthreadpool_destroy);
81
82 std::vector<float, AlignedAllocator<float, 64>> x(block_size);
83 std::vector<float, AlignedAllocator<float, 64>> y(block_size);
84 std::vector<float> ulp_error(block_size);
85 float max_ulp_error = 0.0f;
86
87 ComputeErrorContext context;
88 context.input = x.data();
89 context.output = y.data();
90 context.error = ulp_error.data();
91 for (auto _ : state) {
92 for (uint32_t n = min_input; int32_t(n) < 0; n -= block_size) {
93 for (uint32_t i = 0; i < block_size; i++) {
94 x[i] = uint32_as_float(std::max<uint32_t>(n - i, 0x80000000));
95 }
96 std::fill(y.begin(), y.end(), std::nanf(""));
97
98 expm1(block_size * sizeof(float), x.data(), y.data());
99
100 pthreadpool_parallelize_1d_tile_1d(
101 threadpool.get(),
102 reinterpret_cast<pthreadpool_task_1d_tile_1d_t>(ComputeError),
103 static_cast<void*>(&context),
104 block_size, tile_size, 0 /* flags */);
105
106 max_ulp_error = std::accumulate(ulp_error.cbegin(), ulp_error.cend(), max_ulp_error,
107 static_cast<const float& (*)(const float&, const float&)>(std::max<float>));
108 }
109 }
110
111 state.counters["ULPERROR"] = benchmark::Counter(max_ulp_error);
112 }
113
114 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
115 BENCHMARK_CAPTURE(ExpM1Error, neon_rr2_lut16_p3,
116 xnn_math_f32_expm1minus__neon_rr2_lut16_p3,
117 benchmark::utils::CheckNEON)
118 ->Unit(benchmark::kMillisecond)
119 ->Iterations(1);
120 BENCHMARK_CAPTURE(ExpM1Error, neon_rr2_p6,
121 xnn_math_f32_expm1minus__neon_rr2_p6,
122 benchmark::utils::CheckNEON)
123 ->Unit(benchmark::kMillisecond)
124 ->Iterations(1);
125
126 BENCHMARK_CAPTURE(ExpM1Error, neonfma_rr1_lut16_p3,
127 xnn_math_f32_expm1minus__neonfma_rr1_lut16_p3,
128 benchmark::utils::CheckNEONFMA)
129 ->Unit(benchmark::kMillisecond)
130 ->Iterations(1);
131 BENCHMARK_CAPTURE(ExpM1Error, neonfma_rr1_p6,
132 xnn_math_f32_expm1minus__neonfma_rr1_p6,
133 benchmark::utils::CheckNEONFMA)
134 ->Unit(benchmark::kMillisecond)
135 ->Iterations(1);
136 #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
137
138 #if XNN_ARCH_X86 || XNN_ARCH_X86_64
139 BENCHMARK_CAPTURE(ExpM1Error, avx512f_rr1_lut16_p3_perm,
140 xnn_math_f32_expm1minus__avx512f_rr1_lut16_p3_perm,
141 benchmark::utils::CheckAVX512F)
142 ->Unit(benchmark::kMillisecond)
143 ->Iterations(1);
144 BENCHMARK_CAPTURE(ExpM1Error, avx512f_rr1_p6,
145 xnn_math_f32_expm1minus__avx512f_rr1_p6,
146 benchmark::utils::CheckAVX512F)
147 ->Unit(benchmark::kMillisecond)
148 ->Iterations(1);
149
150 BENCHMARK_CAPTURE(ExpM1Error, avx2_rr1_lut4_p4_perm,
151 xnn_math_f32_expm1minus__avx2_rr1_lut4_p4_perm,
152 benchmark::utils::CheckAVX2)
153 ->Unit(benchmark::kMillisecond)
154 ->Iterations(1);
155 BENCHMARK_CAPTURE(ExpM1Error, avx2_rr1_lut8_p4_perm,
156 xnn_math_f32_expm1minus__avx2_rr1_lut8_p4_perm,
157 benchmark::utils::CheckAVX2)
158 ->Unit(benchmark::kMillisecond)
159 ->Iterations(1);
160 BENCHMARK_CAPTURE(ExpM1Error, avx2_rr1_lut16_p3_gather,
161 xnn_math_f32_expm1minus__avx2_rr1_lut16_p3_gather,
162 benchmark::utils::CheckAVX2)
163 ->Unit(benchmark::kMillisecond)
164 ->Iterations(1);
165 BENCHMARK_CAPTURE(ExpM1Error, avx2_rr1_p6,
166 xnn_math_f32_expm1minus__avx2_rr1_p6,
167 benchmark::utils::CheckAVX2)
168 ->Unit(benchmark::kMillisecond)
169 ->Iterations(1);
170
171 BENCHMARK_CAPTURE(ExpM1Error, avx_rr2_lut4_p4_perm,
172 xnn_math_f32_expm1minus__avx_rr2_lut4_p4_perm,
173 benchmark::utils::CheckAVX)
174 ->Unit(benchmark::kMillisecond)
175 ->Iterations(1);
176 BENCHMARK_CAPTURE(ExpM1Error, avx_rr2_lut16_p3,
177 xnn_math_f32_expm1minus__avx_rr2_lut16_p3,
178 benchmark::utils::CheckAVX)
179 ->Unit(benchmark::kMillisecond)
180 ->Iterations(1);
181 BENCHMARK_CAPTURE(ExpM1Error, avx_rr2_p6,
182 xnn_math_f32_expm1minus__avx_rr2_p6,
183 benchmark::utils::CheckAVX)
184 ->Unit(benchmark::kMillisecond)
185 ->Iterations(1);
186
187 BENCHMARK_CAPTURE(ExpM1Error, sse2_rr2_lut16_p3,
188 xnn_math_f32_expm1minus__sse2_rr2_lut16_p3)
189 ->Unit(benchmark::kMillisecond)
190 ->Iterations(1);
191 BENCHMARK_CAPTURE(ExpM1Error, sse2_rr2_p6,
192 xnn_math_f32_expm1minus__sse2_rr2_p6)
193 ->Unit(benchmark::kMillisecond)
194 ->Iterations(1);
195 #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
196
197 #if XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
198 BENCHMARK_CAPTURE(ExpM1Error, wasmsimd_rr2_lut16_p3_andnot,
199 xnn_math_f32_expm1minus__wasmsimd_rr2_lut16_p3_andnot)
200 ->Unit(benchmark::kMillisecond)
201 ->Iterations(1);
202 BENCHMARK_CAPTURE(ExpM1Error, wasmsimd_rr2_lut16_p3_max,
203 xnn_math_f32_expm1minus__wasmsimd_rr2_lut16_p3_max)
204 ->Unit(benchmark::kMillisecond)
205 ->Iterations(1);
206 BENCHMARK_CAPTURE(ExpM1Error, wasmsimd_rr2_p6_andnot,
207 xnn_math_f32_expm1minus__wasmsimd_rr2_p6_andnot)
208 ->Unit(benchmark::kMillisecond)
209 ->Iterations(1);
210 BENCHMARK_CAPTURE(ExpM1Error, wasmsimd_rr2_p6_max,
211 xnn_math_f32_expm1minus__wasmsimd_rr2_p6_max)
212 ->Unit(benchmark::kMillisecond)
213 ->Iterations(1);
214 #endif // XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
215
216 BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_lut4_p4,
217 xnn_math_f32_expm1minus__scalar_rr2_lut4_p4)
218 ->Unit(benchmark::kMillisecond)
219 ->Iterations(1);
220 BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_lut8_p3,
221 xnn_math_f32_expm1minus__scalar_rr2_lut8_p3)
222 ->Unit(benchmark::kMillisecond)
223 ->Iterations(1);
224 BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_lut8_p4,
225 xnn_math_f32_expm1minus__scalar_rr2_lut8_p4)
226 ->Unit(benchmark::kMillisecond)
227 ->Iterations(1);
228 BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_lut16_p3,
229 xnn_math_f32_expm1minus__scalar_rr2_lut16_p3)
230 ->Unit(benchmark::kMillisecond)
231 ->Iterations(1);
232 BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_lut16_p4,
233 xnn_math_f32_expm1minus__scalar_rr2_lut16_p4)
234 ->Unit(benchmark::kMillisecond)
235 ->Iterations(1);
236 BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_p5,
237 xnn_math_f32_expm1minus__scalar_rr2_p5)
238 ->Unit(benchmark::kMillisecond)
239 ->Iterations(1);
240 BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_p6,
241 xnn_math_f32_expm1minus__scalar_rr2_p6)
242 ->Unit(benchmark::kMillisecond)
243 ->Iterations(1);
244
245 #ifndef XNNPACK_BENCHMARK_NO_MAIN
246 BENCHMARK_MAIN();
247 #endif
248