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 <cmath>
8 #include <functional>
9 #include <random>
10 #include <vector>
11
12 #include <benchmark/benchmark.h>
13 #include "bench/utils.h"
14
15 #include <xnnpack/aligned-allocator.h>
16 #include <xnnpack/common.h>
17 #include <xnnpack/microfnptr.h>
18 #include <xnnpack/vunary.h>
19
20
f32_vrelu(benchmark::State & state,xnn_f32_vrelu_ukernel_function f32_vrelu,benchmark::utils::IsaCheckFunction isa_check=nullptr)21 static void f32_vrelu(
22 benchmark::State& state,
23 xnn_f32_vrelu_ukernel_function f32_vrelu,
24 benchmark::utils::IsaCheckFunction isa_check = nullptr)
25 {
26 if (isa_check && !isa_check(state)) {
27 return;
28 }
29
30 const size_t num_elements = state.range(0);
31
32 std::random_device random_device;
33 auto rng = std::mt19937(random_device());
34 auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), std::ref(rng));
35
36 std::vector<float, AlignedAllocator<float, 64>> x(num_elements);
37 std::generate(x.begin(), x.end(), std::ref(f32rng));
38 std::vector<float, AlignedAllocator<float, 64>> y(num_elements);
39 std::generate(x.begin(), x.end(), std::ref(f32rng));
40
41 for (auto _ : state) {
42 f32_vrelu(num_elements * sizeof(float), x.data(), y.data(), NULL);
43 }
44
45 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
46 if (cpu_frequency != 0) {
47 state.counters["cpufreq"] = cpu_frequency;
48 }
49
50 const size_t elements_per_iteration = num_elements;
51 state.counters["elements"] =
52 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
53
54 const size_t bytes_per_iteration = 2 * num_elements * sizeof(float);
55 state.counters["bytes"] =
56 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
57 }
58
59 #if XNN_ARCH_X86 || XNN_ARCH_X86_64
60 BENCHMARK_CAPTURE(f32_vrelu, sse_x4,
61 xnn_f32_vrelu_ukernel__sse_x4)
62 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
63 ->UseRealTime();
64
65 BENCHMARK_CAPTURE(f32_vrelu, sse_x8,
66 xnn_f32_vrelu_ukernel__sse_x8)
67 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
68 ->UseRealTime();
69
70 BENCHMARK_CAPTURE(f32_vrelu, avx_x8,
71 xnn_f32_vrelu_ukernel__avx_x8,
72 benchmark::utils::CheckAVX)
73 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
74 ->UseRealTime();
75
76 BENCHMARK_CAPTURE(f32_vrelu, avx_x16,
77 xnn_f32_vrelu_ukernel__avx_x16,
78 benchmark::utils::CheckAVX)
79 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
80 ->UseRealTime();
81
82 BENCHMARK_CAPTURE(f32_vrelu, avx512f_x16,
83 xnn_f32_vrelu_ukernel__avx512f_x16,
84 benchmark::utils::CheckAVX512F)
85 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
86 ->UseRealTime();
87
88 BENCHMARK_CAPTURE(f32_vrelu, avx512f_x32,
89 xnn_f32_vrelu_ukernel__avx512f_x32,
90 benchmark::utils::CheckAVX512F)
91 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
92 ->UseRealTime();
93 #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
94
95 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
96 BENCHMARK_CAPTURE(f32_vrelu, neon_x4,
97 xnn_f32_vrelu_ukernel__neon_x4,
98 benchmark::utils::CheckNEON)
99 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
100 ->UseRealTime();
101 BENCHMARK_CAPTURE(f32_vrelu, neon_x8,
102 xnn_f32_vrelu_ukernel__neon_x8,
103 benchmark::utils::CheckNEON)
104 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
105 ->UseRealTime();
106 #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
107
108
109 #if XNN_ARCH_WASM || XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
110 BENCHMARK_CAPTURE(f32_vrelu, wasm_x1,
111 xnn_f32_vrelu_ukernel__wasm_x1)
112 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
113 ->UseRealTime();
114
115 BENCHMARK_CAPTURE(f32_vrelu, wasm_x2,
116 xnn_f32_vrelu_ukernel__wasm_x2)
117 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
118 ->UseRealTime();
119
120 BENCHMARK_CAPTURE(f32_vrelu, wasm_x4,
121 xnn_f32_vrelu_ukernel__wasm_x4)
122 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
123 ->UseRealTime();
124
125 BENCHMARK_CAPTURE(f32_vrelu, wasm_x8,
126 xnn_f32_vrelu_ukernel__wasm_x8)
127 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
128 ->UseRealTime();
129
130 BENCHMARK_CAPTURE(f32_vrelu, wasm32_shr_x1,
131 xnn_f32_vrelu_ukernel__wasm32_shr_x1)
132 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
133 ->UseRealTime();
134
135 BENCHMARK_CAPTURE(f32_vrelu, wasm32_shr_x2,
136 xnn_f32_vrelu_ukernel__wasm32_shr_x2)
137 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
138 ->UseRealTime();
139
140 BENCHMARK_CAPTURE(f32_vrelu, wasm32_shr_x4,
141 xnn_f32_vrelu_ukernel__wasm32_shr_x4)
142 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
143 ->UseRealTime();
144 #endif // XNN_ARCH_WASM || XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
145
146 #if XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
147 BENCHMARK_CAPTURE(f32_vrelu, wasmsimd_x4,
148 xnn_f32_vrelu_ukernel__wasmsimd_x4)
149 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
150 ->UseRealTime();
151
152 BENCHMARK_CAPTURE(f32_vrelu, wasmsimd_x8,
153 xnn_f32_vrelu_ukernel__wasmsimd_x8)
154 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
155 ->UseRealTime();
156
157 BENCHMARK_CAPTURE(f32_vrelu, wasmsimd_x16,
158 xnn_f32_vrelu_ukernel__wasmsimd_x16)
159 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
160 ->UseRealTime();
161 #endif // XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
162
163 BENCHMARK_CAPTURE(f32_vrelu, scalar_x1,
164 xnn_f32_vrelu_ukernel__scalar_x1)
165 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
166 ->UseRealTime();
167
168 BENCHMARK_CAPTURE(f32_vrelu, scalar_x2,
169 xnn_f32_vrelu_ukernel__scalar_x2)
170 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
171 ->UseRealTime();
172
173 BENCHMARK_CAPTURE(f32_vrelu, scalar_x4,
174 xnn_f32_vrelu_ukernel__scalar_x4)
175 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
176 ->UseRealTime();
177
178 BENCHMARK_CAPTURE(f32_vrelu, scalar_x8,
179 xnn_f32_vrelu_ukernel__scalar_x8)
180 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
181 ->UseRealTime();
182
183 #ifndef XNNPACK_BENCHMARK_NO_MAIN
184 BENCHMARK_MAIN();
185 #endif
186