blob: 8c84437f5cd225e491676ed59be733216e1d9ca4 [file] [log] [blame]
Marat Dukhana438aca2020-11-20 15:45:01 -08001// 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>
Marat Dukhanced880d2020-12-13 17:52:27 -080010#include <memory>
Marat Dukhan3a305212020-12-06 19:24:27 -080011#include <numeric>
Marat Dukhana438aca2020-11-20 15:45:01 -080012#include <random>
13#include <vector>
14
Marat Dukhan3a305212020-12-06 19:24:27 -080015#include <cpuinfo.h>
16#include <pthreadpool.h>
17
Marat Dukhana438aca2020-11-20 15:45:01 -080018#include <benchmark/benchmark.h>
19#include <fp16/fp16.h>
20
Marat Dukhande390d42020-11-29 19:32:18 -080021#include "bench/utils.h"
Marat Dukhana438aca2020-11-20 15:45:01 -080022#include <xnnpack/AlignedAllocator.h>
23#include <xnnpack/common.h>
24#include <xnnpack/math-stubs.h>
25
26
Marat Dukhan3a305212020-12-06 19:24:27 -080027struct ComputeErrorContext {
28 const float* input;
29 const float* output;
30 float* error;
31};
32
33static void ComputeError(
34 struct ComputeErrorContext* context,
35 size_t start,
36 size_t range)
37{
38 const float* input = context->input;
39 const float* output = context->output;
40 float* error = context->error;
41 for (size_t i = start; i < start + range; i++) {
42 const double output_ref = std::expm1(double(input[i]));
43 const double abs_error = std::abs(output_ref - double(output[i]));
44 const float output_abs = std::abs(output_ref);
45 const float output_ulp = fp32_from_bits(fp32_to_bits(output_abs) + 1) - output_abs;
46 error[i] = float(abs_error / output_ulp);
47 }
48}
49
50static void ExpM1Error(benchmark::State& state,
Marat Dukhana438aca2020-11-20 15:45:01 -080051 xnn_f32_unary_math_function expm1,
Marat Dukhande390d42020-11-29 19:32:18 -080052 benchmark::utils::IsaCheckFunction isa_check = nullptr)
Marat Dukhana438aca2020-11-20 15:45:01 -080053{
Marat Dukhan3a305212020-12-06 19:24:27 -080054 if (!cpuinfo_initialize()) {
55 state.SkipWithError("failed cpuinfo init");
56 return;
57 }
Marat Dukhande390d42020-11-29 19:32:18 -080058 if (isa_check && !isa_check(state)) {
59 return;
60 }
61
Marat Dukhana438aca2020-11-20 15:45:01 -080062 // The smallest x for which expm1f(x) is not saturated at -1 (-0x1.154244p+4f).
Marat Dukhand28a5a22020-12-14 15:27:22 -080063 const uint32_t min_input = UINT32_C(0xC18AA122);
Marat Dukhan3a305212020-12-06 19:24:27 -080064 // Number of elements in one block of inputs/outputs.
65 // Combining multiple elements in a block reduce function call overhead.
66 const size_t block_size = 16384;
67 // Number of elements in one parallelization tile. Worker threads process this many elements in each task.
68 const size_t tile_size = 64;
Marat Dukhana438aca2020-11-20 15:45:01 -080069
Marat Dukhan3a305212020-12-06 19:24:27 -080070 uint32_t num_threads = cpuinfo_get_cores_count();
71 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
72 // Use all cores except for the least performant cluster
73 if (cpuinfo_get_clusters_count() > 1) {
74 num_threads -= cpuinfo_get_cluster(cpuinfo_get_clusters_count() - 1)->core_count;
75 }
76 #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
77
78 std::unique_ptr<pthreadpool, decltype(&pthreadpool_destroy)> threadpool(
79 pthreadpool_create(num_threads), pthreadpool_destroy);
80
81 std::vector<float, AlignedAllocator<float, 64>> x(block_size);
82 std::vector<float, AlignedAllocator<float, 64>> y(block_size);
83 std::vector<float> ulp_error(block_size);
84 float max_ulp_error = 0.0f;
85
86 ComputeErrorContext context;
87 context.input = x.data();
88 context.output = y.data();
89 context.error = ulp_error.data();
Marat Dukhana438aca2020-11-20 15:45:01 -080090 for (auto _ : state) {
Marat Dukhan3a305212020-12-06 19:24:27 -080091 for (uint32_t n = min_input; int32_t(n) < 0; n -= block_size) {
92 for (uint32_t i = 0; i < block_size; i++) {
Marat Dukhana438aca2020-11-20 15:45:01 -080093 x[i] = fp32_from_bits(std::max<uint32_t>(n - i, 0x80000000));
94 }
95 std::fill(y.begin(), y.end(), std::nanf(""));
96
Marat Dukhan3a305212020-12-06 19:24:27 -080097 expm1(block_size * sizeof(float), x.data(), y.data());
Marat Dukhana438aca2020-11-20 15:45:01 -080098
Marat Dukhan3a305212020-12-06 19:24:27 -080099 pthreadpool_parallelize_1d_tile_1d(
100 threadpool.get(),
101 reinterpret_cast<pthreadpool_task_1d_tile_1d_t>(ComputeError),
102 static_cast<void*>(&context),
103 block_size, tile_size, 0 /* flags */);
104
105 max_ulp_error = std::accumulate(ulp_error.cbegin(), ulp_error.cend(), max_ulp_error,
106 static_cast<const float& (*)(const float&, const float&)>(std::max<float>));
Marat Dukhana438aca2020-11-20 15:45:01 -0800107 }
108 }
109
110 state.counters["ULPERROR"] = benchmark::Counter(max_ulp_error);
111}
112
Marat Dukhande390d42020-11-29 19:32:18 -0800113#if XNN_ARCH_ARM || XNN_ARCH_ARM64
Marat Dukhan3a305212020-12-06 19:24:27 -0800114 BENCHMARK_CAPTURE(ExpM1Error, neon_rr2_lut16_p3,
115 xnn_math_f32_expm1minus__neon_rr2_lut16_p3,
116 benchmark::utils::CheckNEON)
117 ->Unit(benchmark::kMillisecond)
118 ->Iterations(1);
119 BENCHMARK_CAPTURE(ExpM1Error, neon_rr2_p6,
120 xnn_math_f32_expm1minus__neon_rr2_p6,
121 benchmark::utils::CheckNEON)
122 ->Unit(benchmark::kMillisecond)
123 ->Iterations(1);
Marat Dukhande390d42020-11-29 19:32:18 -0800124
Marat Dukhan3a305212020-12-06 19:24:27 -0800125 BENCHMARK_CAPTURE(ExpM1Error, neonfma_rr1_lut16_p3,
126 xnn_math_f32_expm1minus__neonfma_rr1_lut16_p3,
127 benchmark::utils::CheckNEONFMA)
128 ->Unit(benchmark::kMillisecond)
129 ->Iterations(1);
130 BENCHMARK_CAPTURE(ExpM1Error, neonfma_rr1_p6,
131 xnn_math_f32_expm1minus__neonfma_rr1_p6,
132 benchmark::utils::CheckNEONFMA)
133 ->Unit(benchmark::kMillisecond)
134 ->Iterations(1);
Marat Dukhande390d42020-11-29 19:32:18 -0800135#endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
136
Marat Dukhana438aca2020-11-20 15:45:01 -0800137#if XNN_ARCH_X86 || XNN_ARCH_X86_64
Marat Dukhan3a305212020-12-06 19:24:27 -0800138 BENCHMARK_CAPTURE(ExpM1Error, avx512f_rr1_lut16_p3_perm,
139 xnn_math_f32_expm1minus__avx512f_rr1_lut16_p3_perm,
140 benchmark::utils::CheckAVX512F)
141 ->Unit(benchmark::kMillisecond)
142 ->Iterations(1);
143 BENCHMARK_CAPTURE(ExpM1Error, avx512f_rr1_p6,
144 xnn_math_f32_expm1minus__avx512f_rr1_p6,
145 benchmark::utils::CheckAVX512F)
146 ->Unit(benchmark::kMillisecond)
147 ->Iterations(1);
Marat Dukhande390d42020-11-29 19:32:18 -0800148
Marat Dukhan3a305212020-12-06 19:24:27 -0800149 BENCHMARK_CAPTURE(ExpM1Error, avx2_rr1_lut4_p4_perm,
150 xnn_math_f32_expm1minus__avx2_rr1_lut4_p4_perm,
151 benchmark::utils::CheckAVX2)
152 ->Unit(benchmark::kMillisecond)
153 ->Iterations(1);
154 BENCHMARK_CAPTURE(ExpM1Error, avx2_rr1_lut8_p4_perm,
155 xnn_math_f32_expm1minus__avx2_rr1_lut8_p4_perm,
156 benchmark::utils::CheckAVX2)
157 ->Unit(benchmark::kMillisecond)
158 ->Iterations(1);
159 BENCHMARK_CAPTURE(ExpM1Error, avx2_rr1_lut16_p3_gather,
160 xnn_math_f32_expm1minus__avx2_rr1_lut16_p3_gather,
161 benchmark::utils::CheckAVX2)
162 ->Unit(benchmark::kMillisecond)
163 ->Iterations(1);
164 BENCHMARK_CAPTURE(ExpM1Error, avx2_rr1_p6,
165 xnn_math_f32_expm1minus__avx2_rr1_p6,
166 benchmark::utils::CheckAVX2)
167 ->Unit(benchmark::kMillisecond)
168 ->Iterations(1);
Marat Dukhande390d42020-11-29 19:32:18 -0800169
Marat Dukhan3a305212020-12-06 19:24:27 -0800170 BENCHMARK_CAPTURE(ExpM1Error, avx_rr2_lut4_p4_perm,
171 xnn_math_f32_expm1minus__avx_rr2_lut4_p4_perm,
172 benchmark::utils::CheckAVX)
173 ->Unit(benchmark::kMillisecond)
174 ->Iterations(1);
175 BENCHMARK_CAPTURE(ExpM1Error, avx_rr2_lut16_p3,
176 xnn_math_f32_expm1minus__avx_rr2_lut16_p3,
177 benchmark::utils::CheckAVX)
178 ->Unit(benchmark::kMillisecond)
179 ->Iterations(1);
180 BENCHMARK_CAPTURE(ExpM1Error, avx_rr2_p6,
181 xnn_math_f32_expm1minus__avx_rr2_p6,
182 benchmark::utils::CheckAVX)
183 ->Unit(benchmark::kMillisecond)
184 ->Iterations(1);
Marat Dukhande390d42020-11-29 19:32:18 -0800185
Marat Dukhan3a305212020-12-06 19:24:27 -0800186 BENCHMARK_CAPTURE(ExpM1Error, sse2_rr2_lut16_p3,
187 xnn_math_f32_expm1minus__sse2_rr2_lut16_p3)
188 ->Unit(benchmark::kMillisecond)
189 ->Iterations(1);
190 BENCHMARK_CAPTURE(ExpM1Error, sse2_rr2_p6,
191 xnn_math_f32_expm1minus__sse2_rr2_p6)
192 ->Unit(benchmark::kMillisecond)
193 ->Iterations(1);
Marat Dukhana438aca2020-11-20 15:45:01 -0800194#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
195
Marat Dukhande390d42020-11-29 19:32:18 -0800196#if XNN_ARCH_WASMSIMD
Marat Dukhan3a305212020-12-06 19:24:27 -0800197 BENCHMARK_CAPTURE(ExpM1Error, wasmsimd_rr2_lut16_p3_andnot,
198 xnn_math_f32_expm1minus__wasmsimd_rr2_lut16_p3_andnot)
199 ->Unit(benchmark::kMillisecond)
200 ->Iterations(1);
201 BENCHMARK_CAPTURE(ExpM1Error, wasmsimd_rr2_lut16_p3_max,
202 xnn_math_f32_expm1minus__wasmsimd_rr2_lut16_p3_max)
203 ->Unit(benchmark::kMillisecond)
204 ->Iterations(1);
205 BENCHMARK_CAPTURE(ExpM1Error, wasmsimd_rr2_p6_andnot,
206 xnn_math_f32_expm1minus__wasmsimd_rr2_p6_andnot)
207 ->Unit(benchmark::kMillisecond)
208 ->Iterations(1);
209 BENCHMARK_CAPTURE(ExpM1Error, wasmsimd_rr2_p6_max,
210 xnn_math_f32_expm1minus__wasmsimd_rr2_p6_max)
211 ->Unit(benchmark::kMillisecond)
212 ->Iterations(1);
Marat Dukhande390d42020-11-29 19:32:18 -0800213#endif // XNN_ARCH_WASMSIMD
214
Marat Dukhan3a305212020-12-06 19:24:27 -0800215BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_lut4_p4,
216 xnn_math_f32_expm1minus__scalar_rr2_lut4_p4)
217 ->Unit(benchmark::kMillisecond)
218 ->Iterations(1);
219BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_lut8_p3,
220 xnn_math_f32_expm1minus__scalar_rr2_lut8_p3)
221 ->Unit(benchmark::kMillisecond)
222 ->Iterations(1);
223BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_lut8_p4,
224 xnn_math_f32_expm1minus__scalar_rr2_lut8_p4)
225 ->Unit(benchmark::kMillisecond)
226 ->Iterations(1);
227BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_lut16_p3,
228 xnn_math_f32_expm1minus__scalar_rr2_lut16_p3)
229 ->Unit(benchmark::kMillisecond)
230 ->Iterations(1);
231BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_lut16_p4,
232 xnn_math_f32_expm1minus__scalar_rr2_lut16_p4)
233 ->Unit(benchmark::kMillisecond)
234 ->Iterations(1);
235BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_p5,
236 xnn_math_f32_expm1minus__scalar_rr2_p5)
237 ->Unit(benchmark::kMillisecond)
238 ->Iterations(1);
239BENCHMARK_CAPTURE(ExpM1Error, scalar_rr2_p6,
240 xnn_math_f32_expm1minus__scalar_rr2_p6)
241 ->Unit(benchmark::kMillisecond)
242 ->Iterations(1);
Marat Dukhanc60742b2020-11-23 12:33:27 -0800243
Marat Dukhana438aca2020-11-20 15:45:01 -0800244#ifndef XNNPACK_BENCHMARK_NO_MAIN
245BENCHMARK_MAIN();
246#endif