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::sqrt(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
SqrtError(benchmark::State & state,xnn_f32_unary_math_function sqrt,benchmark::utils::IsaCheckFunction isa_check=nullptr)51 static void SqrtError(benchmark::State& state,
52 xnn_f32_unary_math_function sqrt,
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 const uint32_t min_input = 0x3F800000;
64 const uint32_t max_input = 0x41800000;
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; n < max_input; n += block_size) {
93 for (uint32_t i = 0; i < block_size; i++) {
94 x[i] = uint32_as_float(std::min<uint32_t>(n + i, max_input));
95 }
96 std::fill(y.begin(), y.end(), std::nanf(""));
97
98 sqrt(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(SqrtError, neonfma_nr1fma,
116 xnn_math_f32_sqrt__neonfma_nr1fma,
117 benchmark::utils::CheckNEONFMA)
118 ->Unit(benchmark::kMillisecond)
119 ->Iterations(1);
120 BENCHMARK_CAPTURE(SqrtError, neonfma_nr2fma,
121 xnn_math_f32_sqrt__neonfma_nr2fma,
122 benchmark::utils::CheckNEONFMA)
123 ->Unit(benchmark::kMillisecond)
124 ->Iterations(1);
125 BENCHMARK_CAPTURE(SqrtError, neonfma_nr3fma,
126 xnn_math_f32_sqrt__neonfma_nr3fma,
127 benchmark::utils::CheckNEONFMA)
128 ->Unit(benchmark::kMillisecond)
129 ->Iterations(1);
130 BENCHMARK_CAPTURE(SqrtError, neonfma_nr2fma1adj,
131 xnn_math_f32_sqrt__neonfma_nr2fma1adj,
132 benchmark::utils::CheckNEONFMA)
133 ->Unit(benchmark::kMillisecond)
134 ->Iterations(1);
135 BENCHMARK_CAPTURE(SqrtError, neonfma_nr1rsqrts1fma1adj,
136 xnn_math_f32_sqrt__neonfma_nr1rsqrts1fma1adj,
137 benchmark::utils::CheckNEONFMA)
138 ->Unit(benchmark::kMillisecond)
139 ->Iterations(1);
140
141 BENCHMARK_CAPTURE(SqrtError, neon_nr1rsqrts,
142 xnn_math_f32_sqrt__neon_nr1rsqrts,
143 benchmark::utils::CheckNEON)
144 ->Unit(benchmark::kMillisecond)
145 ->Iterations(1);
146 BENCHMARK_CAPTURE(SqrtError, neon_nr2rsqrts,
147 xnn_math_f32_sqrt__neon_nr2rsqrts,
148 benchmark::utils::CheckNEON)
149 ->Unit(benchmark::kMillisecond)
150 ->Iterations(1);
151 BENCHMARK_CAPTURE(SqrtError, neon_nr3rsqrts,
152 xnn_math_f32_sqrt__neon_nr3rsqrts,
153 benchmark::utils::CheckNEON)
154 ->Unit(benchmark::kMillisecond)
155 ->Iterations(1);
156 #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
157
158 #if XNN_ARCH_X86 || XNN_ARCH_X86_64
159 BENCHMARK_CAPTURE(SqrtError, avx512f_nr1fma,
160 xnn_math_f32_sqrt__avx512f_nr1fma,
161 benchmark::utils::CheckAVX512F)
162 ->Unit(benchmark::kMillisecond)
163 ->Iterations(1);
164 BENCHMARK_CAPTURE(SqrtError, avx512f_nr2fma,
165 xnn_math_f32_sqrt__avx512f_nr2fma,
166 benchmark::utils::CheckAVX512F)
167 ->Unit(benchmark::kMillisecond)
168 ->Iterations(1);
169 BENCHMARK_CAPTURE(SqrtError, avx512f_nr1fma1adj,
170 xnn_math_f32_sqrt__avx512f_nr1fma1adj,
171 benchmark::utils::CheckAVX512F)
172 ->Unit(benchmark::kMillisecond)
173 ->Iterations(1);
174
175 BENCHMARK_CAPTURE(SqrtError, fma3_nr1fma,
176 xnn_math_f32_sqrt__fma3_nr1fma,
177 benchmark::utils::CheckFMA3)
178 ->Unit(benchmark::kMillisecond)
179 ->Iterations(1);
180 BENCHMARK_CAPTURE(SqrtError, fma3_nr2fma,
181 xnn_math_f32_sqrt__fma3_nr2fma,
182 benchmark::utils::CheckFMA3)
183 ->Unit(benchmark::kMillisecond)
184 ->Iterations(1);
185 BENCHMARK_CAPTURE(SqrtError, fma3_nr1fma1adj,
186 xnn_math_f32_sqrt__fma3_nr1fma1adj,
187 benchmark::utils::CheckFMA3)
188 ->Unit(benchmark::kMillisecond)
189 ->Iterations(1);
190
191 BENCHMARK_CAPTURE(SqrtError, sse_nr1mac,
192 xnn_math_f32_sqrt__sse_nr1mac)
193 ->Unit(benchmark::kMillisecond)
194 ->Iterations(1);
195 BENCHMARK_CAPTURE(SqrtError, sse_nr2mac,
196 xnn_math_f32_sqrt__sse_nr2mac)
197 ->Unit(benchmark::kMillisecond)
198 ->Iterations(1);
199 BENCHMARK_CAPTURE(SqrtError, sse_hh1mac,
200 xnn_math_f32_sqrt__sse_hh1mac)
201 ->Unit(benchmark::kMillisecond)
202 ->Iterations(1);
203 #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
204
205 #ifndef XNNPACK_BENCHMARK_NO_MAIN
206 BENCHMARK_MAIN();
207 #endif
208