1 // Copyright 2022 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 <fp16/fp16.h>
14 #include "bench/utils.h"
15
16 #include <xnnpack.h>
17 #include <xnnpack/aligned-allocator.h>
18 #include <xnnpack/common.h>
19 #include <xnnpack/microfnptr.h>
20 #include <xnnpack/microparams-init.h>
21 #include <xnnpack/vlrelu.h>
22
23
qs8_vlrelu(benchmark::State & state,xnn_qs8_vlrelu_ukernel_function lrelu,xnn_init_qs8_lrelu_params_fn init_params,benchmark::utils::IsaCheckFunction isa_check=nullptr)24 static void qs8_vlrelu(
25 benchmark::State& state,
26 xnn_qs8_vlrelu_ukernel_function lrelu,
27 xnn_init_qs8_lrelu_params_fn init_params,
28 benchmark::utils::IsaCheckFunction isa_check = nullptr)
29 {
30 if (isa_check && !isa_check(state)) {
31 return;
32 }
33
34 const size_t num_elements = state.range(0);
35
36 std::random_device random_device;
37 auto rng = std::mt19937(random_device());
38 auto i8rng = std::bind(
39 std::uniform_int_distribution<int32_t>(std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()),
40 std::ref(rng));
41
42 std::vector<int8_t, AlignedAllocator<int8_t, 64>> x(num_elements + XNN_EXTRA_BYTES / sizeof(int8_t));
43 std::vector<int8_t, AlignedAllocator<int8_t, 64>> y(num_elements);
44 std::generate(x.begin(), x.end(), std::ref(i8rng));
45 std::fill(y.begin(), y.end(), INT8_C(0xAA));
46
47 xnn_qs8_lrelu_params params;
48 init_params(¶ms, 0.75f /* positive scale */, 1.25f /* negative scale */, -1 /* input zero point */, 1 /* output zero point */);
49 for (auto _ : state) {
50 lrelu(num_elements * sizeof(int8_t), x.data(), y.data(), ¶ms);
51 }
52
53 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
54 if (cpu_frequency != 0) {
55 state.counters["cpufreq"] = cpu_frequency;
56 }
57
58 const size_t elements_per_iteration = num_elements;
59 state.counters["elements"] =
60 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
61
62 const size_t bytes_per_iteration = num_elements * (sizeof(int8_t) + sizeof(int8_t));
63 state.counters["bytes"] =
64 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
65 }
66
67 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
68 BENCHMARK_CAPTURE(qs8_vlrelu, neon_x8,
69 xnn_qs8_vlrelu_ukernel__neon_x8,
70 xnn_init_qs8_lrelu_neon_params,
71 benchmark::utils::CheckNEON)
72 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
73 ->UseRealTime();
74 BENCHMARK_CAPTURE(qs8_vlrelu, neon_x16,
75 xnn_qs8_vlrelu_ukernel__neon_x16,
76 xnn_init_qs8_lrelu_neon_params,
77 benchmark::utils::CheckNEON)
78 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
79 ->UseRealTime();
80 BENCHMARK_CAPTURE(qs8_vlrelu, neon_x32,
81 xnn_qs8_vlrelu_ukernel__neon_x32,
82 xnn_init_qs8_lrelu_neon_params,
83 benchmark::utils::CheckNEON)
84 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
85 ->UseRealTime();
86 #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
87
88 #if XNN_ARCH_ARM
89 BENCHMARK_CAPTURE(qs8_vlrelu, armsimd32_x4,
90 xnn_qs8_vlrelu_ukernel__armsimd32_x4,
91 xnn_init_qs8_lrelu_armsimd32_params,
92 benchmark::utils::CheckARMV6)
93 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
94 ->UseRealTime();
95 BENCHMARK_CAPTURE(qs8_vlrelu, armsimd32_x8,
96 xnn_qs8_vlrelu_ukernel__armsimd32_x8,
97 xnn_init_qs8_lrelu_armsimd32_params,
98 benchmark::utils::CheckARMV6)
99 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
100 ->UseRealTime();
101 #endif // XNN_ARCH_ARM
102
103 #if XNN_ARCH_X86 || XNN_ARCH_X86_64
104 BENCHMARK_CAPTURE(qs8_vlrelu, avx2_x16,
105 xnn_qs8_vlrelu_ukernel__avx2_x16,
106 xnn_init_qs8_lrelu_avx2_params,
107 benchmark::utils::CheckAVX2)
108 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
109 ->UseRealTime();
110 BENCHMARK_CAPTURE(qs8_vlrelu, avx2_x32,
111 xnn_qs8_vlrelu_ukernel__avx2_x32,
112 xnn_init_qs8_lrelu_avx2_params,
113 benchmark::utils::CheckAVX2)
114 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
115 ->UseRealTime();
116 BENCHMARK_CAPTURE(qs8_vlrelu, avx2_x64,
117 xnn_qs8_vlrelu_ukernel__avx2_x64,
118 xnn_init_qs8_lrelu_avx2_params,
119 benchmark::utils::CheckAVX2)
120 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
121 ->UseRealTime();
122
123 BENCHMARK_CAPTURE(qs8_vlrelu, avx_x8,
124 xnn_qs8_vlrelu_ukernel__avx_x8,
125 xnn_init_qs8_lrelu_avx_params,
126 benchmark::utils::CheckAVX)
127 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
128 ->UseRealTime();
129 BENCHMARK_CAPTURE(qs8_vlrelu, avx_x16,
130 xnn_qs8_vlrelu_ukernel__avx_x16,
131 xnn_init_qs8_lrelu_avx_params,
132 benchmark::utils::CheckAVX)
133 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
134 ->UseRealTime();
135 BENCHMARK_CAPTURE(qs8_vlrelu, avx_x32,
136 xnn_qs8_vlrelu_ukernel__avx_x32,
137 xnn_init_qs8_lrelu_avx_params,
138 benchmark::utils::CheckAVX)
139 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
140 ->UseRealTime();
141
142 BENCHMARK_CAPTURE(qs8_vlrelu, sse41_x8,
143 xnn_qs8_vlrelu_ukernel__sse41_x8,
144 xnn_init_qs8_lrelu_sse2_params,
145 benchmark::utils::CheckSSE41)
146 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
147 ->UseRealTime();
148 BENCHMARK_CAPTURE(qs8_vlrelu, sse41_x16,
149 xnn_qs8_vlrelu_ukernel__sse41_x16,
150 xnn_init_qs8_lrelu_sse2_params,
151 benchmark::utils::CheckSSE41)
152 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
153 ->UseRealTime();
154 BENCHMARK_CAPTURE(qs8_vlrelu, sse41_x32,
155 xnn_qs8_vlrelu_ukernel__sse41_x32,
156 xnn_init_qs8_lrelu_sse2_params,
157 benchmark::utils::CheckSSE41)
158 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
159 ->UseRealTime();
160
161 BENCHMARK_CAPTURE(qs8_vlrelu, ssse3_x16,
162 xnn_qs8_vlrelu_ukernel__ssse3_x16,
163 xnn_init_qs8_lrelu_sse2_params,
164 benchmark::utils::CheckSSSE3)
165 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
166 ->UseRealTime();
167 BENCHMARK_CAPTURE(qs8_vlrelu, ssse3_x32,
168 xnn_qs8_vlrelu_ukernel__ssse3_x32,
169 xnn_init_qs8_lrelu_sse2_params,
170 benchmark::utils::CheckSSSE3)
171 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
172 ->UseRealTime();
173
174 BENCHMARK_CAPTURE(qs8_vlrelu, sse2_x16,
175 xnn_qs8_vlrelu_ukernel__sse2_x16,
176 xnn_init_qs8_lrelu_sse2_params)
177 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
178 ->UseRealTime();
179 BENCHMARK_CAPTURE(qs8_vlrelu, sse2_x32,
180 xnn_qs8_vlrelu_ukernel__sse2_x32,
181 xnn_init_qs8_lrelu_sse2_params)
182 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
183 ->UseRealTime();
184 #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
185
186 #if XNN_ARCH_WASMRELAXEDSIMD
187 BENCHMARK_CAPTURE(qs8_vlrelu, wasmrelaxedsimd_arm_x16,
188 xnn_qs8_vlrelu_ukernel__wasmrelaxedsimd_arm_x16,
189 xnn_init_qs8_lrelu_wasmsimd_arm_params)
190 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
191 ->UseRealTime();
192 BENCHMARK_CAPTURE(qs8_vlrelu, wasmrelaxedsimd_arm_x32,
193 xnn_qs8_vlrelu_ukernel__wasmrelaxedsimd_arm_x32,
194 xnn_init_qs8_lrelu_wasmsimd_arm_params)
195 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
196 ->UseRealTime();
197
198 BENCHMARK_CAPTURE(qs8_vlrelu, wasmrelaxedsimd_x86_x8,
199 xnn_qs8_vlrelu_ukernel__wasmrelaxedsimd_x86_x8,
200 xnn_init_qs8_lrelu_wasmsimd_x86_params)
201 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
202 ->UseRealTime();
203 BENCHMARK_CAPTURE(qs8_vlrelu, wasmrelaxedsimd_x86_x16,
204 xnn_qs8_vlrelu_ukernel__wasmrelaxedsimd_x86_x16,
205 xnn_init_qs8_lrelu_wasmsimd_x86_params)
206 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
207 ->UseRealTime();
208 BENCHMARK_CAPTURE(qs8_vlrelu, wasmrelaxedsimd_x86_x32,
209 xnn_qs8_vlrelu_ukernel__wasmrelaxedsimd_x86_x32,
210 xnn_init_qs8_lrelu_wasmsimd_x86_params)
211 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
212 ->UseRealTime();
213 #endif // XNN_ARCH_WASMRELAXEDSIMD
214
215 #if XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
216 BENCHMARK_CAPTURE(qs8_vlrelu, wasmsimd_arm_x16,
217 xnn_qs8_vlrelu_ukernel__wasmsimd_arm_x16,
218 xnn_init_qs8_lrelu_wasmsimd_arm_params)
219 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
220 ->UseRealTime();
221 BENCHMARK_CAPTURE(qs8_vlrelu, wasmsimd_arm_x32,
222 xnn_qs8_vlrelu_ukernel__wasmsimd_arm_x32,
223 xnn_init_qs8_lrelu_wasmsimd_arm_params)
224 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
225 ->UseRealTime();
226
227 BENCHMARK_CAPTURE(qs8_vlrelu, wasmsimd_x86_x8,
228 xnn_qs8_vlrelu_ukernel__wasmsimd_x86_x8,
229 xnn_init_qs8_lrelu_wasmsimd_x86_params)
230 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
231 ->UseRealTime();
232 BENCHMARK_CAPTURE(qs8_vlrelu, wasmsimd_x86_x16,
233 xnn_qs8_vlrelu_ukernel__wasmsimd_x86_x16,
234 xnn_init_qs8_lrelu_wasmsimd_x86_params)
235 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
236 ->UseRealTime();
237 BENCHMARK_CAPTURE(qs8_vlrelu, wasmsimd_x86_x32,
238 xnn_qs8_vlrelu_ukernel__wasmsimd_x86_x32,
239 xnn_init_qs8_lrelu_wasmsimd_x86_params)
240 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
241 ->UseRealTime();
242 #endif // XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
243
244 BENCHMARK_CAPTURE(qs8_vlrelu, scalar_andxor_x1,
245 xnn_qs8_vlrelu_ukernel__scalar_andxor_x1,
246 xnn_init_qs8_lrelu_scalar_andxor_params)
247 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
248 ->UseRealTime();
249 BENCHMARK_CAPTURE(qs8_vlrelu, scalar_andxor_x2,
250 xnn_qs8_vlrelu_ukernel__scalar_andxor_x2,
251 xnn_init_qs8_lrelu_scalar_andxor_params)
252 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
253 ->UseRealTime();
254 BENCHMARK_CAPTURE(qs8_vlrelu, scalar_andxor_x4,
255 xnn_qs8_vlrelu_ukernel__scalar_andxor_x4,
256 xnn_init_qs8_lrelu_scalar_andxor_params)
257 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
258 ->UseRealTime();
259
260 BENCHMARK_CAPTURE(qs8_vlrelu, scalar_select_x1,
261 xnn_qs8_vlrelu_ukernel__scalar_select_x1,
262 xnn_init_qs8_lrelu_scalar_select_params)
263 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
264 ->UseRealTime();
265 BENCHMARK_CAPTURE(qs8_vlrelu, scalar_select_x2,
266 xnn_qs8_vlrelu_ukernel__scalar_select_x2,
267 xnn_init_qs8_lrelu_scalar_select_params)
268 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
269 ->UseRealTime();
270 BENCHMARK_CAPTURE(qs8_vlrelu, scalar_select_x4,
271 xnn_qs8_vlrelu_ukernel__scalar_select_x4,
272 xnn_init_qs8_lrelu_scalar_select_params)
273 ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
274 ->UseRealTime();
275
276 #ifndef XNNPACK_BENCHMARK_NO_MAIN
277 BENCHMARK_MAIN();
278 #endif
279