blob: 43ec9eea31753d03665b7ff289c052893bca2664 [file] [log] [blame]
Marat Dukhan84000762020-06-29 18:38:43 -07001// 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>
Marat Dukhanced880d2020-12-13 17:52:27 -080010#include <memory>
Marat Dukhan3a305212020-12-06 19:24:27 -080011#include <numeric>
Marat Dukhan84000762020-06-29 18:38:43 -070012#include <random>
13#include <vector>
14
Marat Dukhan3a305212020-12-06 19:24:27 -080015#include <cpuinfo.h>
16#include <pthreadpool.h>
17
Marat Dukhan84000762020-06-29 18:38:43 -070018#include <benchmark/benchmark.h>
19#include <fp16/fp16.h>
20
21#include "bench/utils.h"
22#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::sqrt(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
Marat Dukhan84000762020-06-29 18:38:43 -070050static void SqrtError(benchmark::State& state,
51 xnn_f32_unary_math_function sqrt,
Marat Dukhan84000762020-06-29 18:38:43 -070052 benchmark::utils::IsaCheckFunction isa_check = nullptr)
53{
Marat Dukhan3a305212020-12-06 19:24:27 -080054 if (!cpuinfo_initialize()) {
55 state.SkipWithError("failed cpuinfo init");
56 return;
57 }
Marat Dukhan84000762020-06-29 18:38:43 -070058 if (isa_check && !isa_check(state)) {
59 return;
60 }
61
62 const uint32_t min_input = 0x3F800000;
63 const uint32_t max_input = 0x41800000;
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 Dukhan84000762020-06-29 18:38:43 -070069
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 Dukhan84000762020-06-29 18:38:43 -070090 for (auto _ : state) {
Marat Dukhan3a305212020-12-06 19:24:27 -080091 for (uint32_t n = min_input; n < max_input; n += block_size) {
92 for (uint32_t i = 0; i < block_size; i++) {
Marat Dukhan84000762020-06-29 18:38:43 -070093 x[i] = fp32_from_bits(std::min<uint32_t>(n + i, max_input));
94 }
95 std::fill(y.begin(), y.end(), std::nanf(""));
96
Marat Dukhan3a305212020-12-06 19:24:27 -080097 sqrt(block_size * sizeof(float), x.data(), y.data());
Marat Dukhan84000762020-06-29 18:38:43 -070098
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 Dukhan84000762020-06-29 18:38:43 -0700107 }
108 }
109
110 state.counters["ULPERROR"] = benchmark::Counter(max_ulp_error);
111}
112
Marat Dukhan84000762020-06-29 18:38:43 -0700113#if XNN_ARCH_ARM || XNN_ARCH_ARM64
Marat Dukhan3a305212020-12-06 19:24:27 -0800114 BENCHMARK_CAPTURE(SqrtError, neonfma_nr1fma,
115 xnn_math_f32_sqrt__neonfma_nr1fma,
116 benchmark::utils::CheckNEONFMA)
117 ->Unit(benchmark::kMillisecond)
118 ->Iterations(1);
119 BENCHMARK_CAPTURE(SqrtError, neonfma_nr2fma,
120 xnn_math_f32_sqrt__neonfma_nr2fma,
121 benchmark::utils::CheckNEONFMA)
122 ->Unit(benchmark::kMillisecond)
123 ->Iterations(1);
124 BENCHMARK_CAPTURE(SqrtError, neonfma_nr3fma,
125 xnn_math_f32_sqrt__neonfma_nr3fma,
126 benchmark::utils::CheckNEONFMA)
127 ->Unit(benchmark::kMillisecond)
128 ->Iterations(1);
129 BENCHMARK_CAPTURE(SqrtError, neonfma_nr2fma1adj,
130 xnn_math_f32_sqrt__neonfma_nr2fma1adj,
131 benchmark::utils::CheckNEONFMA)
132 ->Unit(benchmark::kMillisecond)
133 ->Iterations(1);
134 BENCHMARK_CAPTURE(SqrtError, neonfma_nr1rsqrts1fma1adj,
135 xnn_math_f32_sqrt__neonfma_nr1rsqrts1fma1adj,
136 benchmark::utils::CheckNEONFMA)
137 ->Unit(benchmark::kMillisecond)
138 ->Iterations(1);
139
140 BENCHMARK_CAPTURE(SqrtError, neon_nr1rsqrts,
141 xnn_math_f32_sqrt__neon_nr1rsqrts,
142 benchmark::utils::CheckNEON)
143 ->Unit(benchmark::kMillisecond)
144 ->Iterations(1);
145 BENCHMARK_CAPTURE(SqrtError, neon_nr2rsqrts,
146 xnn_math_f32_sqrt__neon_nr2rsqrts,
147 benchmark::utils::CheckNEON)
148 ->Unit(benchmark::kMillisecond)
149 ->Iterations(1);
150 BENCHMARK_CAPTURE(SqrtError, neon_nr3rsqrts,
151 xnn_math_f32_sqrt__neon_nr3rsqrts,
152 benchmark::utils::CheckNEON)
153 ->Unit(benchmark::kMillisecond)
154 ->Iterations(1);
Marat Dukhan84000762020-06-29 18:38:43 -0700155#endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
156
Marat Dukhan3a305212020-12-06 19:24:27 -0800157#if XNN_ARCH_X86 || XNN_ARCH_X86_64
158 BENCHMARK_CAPTURE(SqrtError, avx512f_nr1fma,
159 xnn_math_f32_sqrt__avx512f_nr1fma,
160 benchmark::utils::CheckAVX512F)
161 ->Unit(benchmark::kMillisecond)
162 ->Iterations(1);
163 BENCHMARK_CAPTURE(SqrtError, avx512f_nr2fma,
164 xnn_math_f32_sqrt__avx512f_nr2fma,
165 benchmark::utils::CheckAVX512F)
166 ->Unit(benchmark::kMillisecond)
167 ->Iterations(1);
168 BENCHMARK_CAPTURE(SqrtError, avx512f_nr1fma1adj,
169 xnn_math_f32_sqrt__avx512f_nr1fma1adj,
170 benchmark::utils::CheckAVX512F)
171 ->Unit(benchmark::kMillisecond)
172 ->Iterations(1);
173
174 BENCHMARK_CAPTURE(SqrtError, fma3_nr1fma,
175 xnn_math_f32_sqrt__fma3_nr1fma,
176 benchmark::utils::CheckFMA3)
177 ->Unit(benchmark::kMillisecond)
178 ->Iterations(1);
179 BENCHMARK_CAPTURE(SqrtError, fma3_nr2fma,
180 xnn_math_f32_sqrt__fma3_nr2fma,
181 benchmark::utils::CheckFMA3)
182 ->Unit(benchmark::kMillisecond)
183 ->Iterations(1);
184 BENCHMARK_CAPTURE(SqrtError, fma3_nr1fma1adj,
185 xnn_math_f32_sqrt__fma3_nr1fma1adj,
186 benchmark::utils::CheckFMA3)
187 ->Unit(benchmark::kMillisecond)
188 ->Iterations(1);
189
190 BENCHMARK_CAPTURE(SqrtError, sse_nr1mac,
191 xnn_math_f32_sqrt__sse_nr1mac)
192 ->Unit(benchmark::kMillisecond)
193 ->Iterations(1);
194 BENCHMARK_CAPTURE(SqrtError, sse_nr2mac,
195 xnn_math_f32_sqrt__sse_nr2mac)
196 ->Unit(benchmark::kMillisecond)
197 ->Iterations(1);
198 BENCHMARK_CAPTURE(SqrtError, sse_hh1mac,
199 xnn_math_f32_sqrt__sse_hh1mac)
200 ->Unit(benchmark::kMillisecond)
201 ->Iterations(1);
202#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
203
Marat Dukhan84000762020-06-29 18:38:43 -0700204#ifndef XNNPACK_BENCHMARK_NO_MAIN
205BENCHMARK_MAIN();
206#endif