1 // Copyright 2021 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.h>
16 #include <xnnpack/aligned-allocator.h>
17 #include <xnnpack/common.h>
18 #include <xnnpack/microfnptr.h>
19 #include <xnnpack/microparams-init.h>
20 #include <xnnpack/vadd.h>
21
22
qu8_vadd(benchmark::State & state,xnn_qu8_vadd_minmax_ukernel_function vadd,xnn_init_qu8_add_minmax_params_fn init_params,benchmark::utils::IsaCheckFunction isa_check=nullptr)23 static void qu8_vadd(
24 benchmark::State& state,
25 xnn_qu8_vadd_minmax_ukernel_function vadd,
26 xnn_init_qu8_add_minmax_params_fn init_params,
27 benchmark::utils::IsaCheckFunction isa_check = nullptr)
28 {
29 if (isa_check && !isa_check(state)) {
30 return;
31 }
32
33 const size_t num_elements = state.range(0);
34
35 std::random_device random_device;
36 auto rng = std::mt19937(random_device());
37 auto u8rng = std::bind(
38 std::uniform_int_distribution<uint32_t>(std::numeric_limits<uint8_t>::min(), std::numeric_limits<uint8_t>::max()),
39 std::ref(rng));
40
41 std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> a(num_elements);
42 std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> b(num_elements);
43 std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> sum(num_elements);
44 std::generate(a.begin(), a.end(), std::ref(u8rng));
45 std::generate(b.begin(), b.end(), std::ref(u8rng));
46
47 union xnn_qu8_add_minmax_params params;
48 init_params(¶ms,
49 127 /* a zero point */, 127 /* b zero point */, 127 /* output zero point */,
50 0.5f /* a-output scale */, 0.75f /* b-output scale */,
51 std::numeric_limits<uint8_t>::min() + 1, std::numeric_limits<uint8_t>::max() - 1);
52 for (auto _ : state) {
53 vadd(num_elements * sizeof(uint8_t), a.data(), b.data(), sum.data(), ¶ms);
54 }
55
56 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
57 if (cpu_frequency != 0) {
58 state.counters["cpufreq"] = cpu_frequency;
59 }
60
61 const size_t num_elements_per_iteration = num_elements;
62 state.counters["num_elements"] =
63 benchmark::Counter(uint64_t(state.iterations()) * num_elements_per_iteration, benchmark::Counter::kIsRate);
64
65 const size_t bytes_per_iteration = 3 * num_elements * sizeof(int8_t);
66 state.counters["bytes"] =
67 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
68 }
69
70 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
71 BENCHMARK_CAPTURE(qu8_vadd, neon_ld64_x8,
72 xnn_qu8_vadd_minmax_ukernel__neon_ld64_x8,
73 xnn_init_qu8_add_minmax_neon_params,
74 benchmark::utils::CheckNEON)
75 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
76 ->UseRealTime();
77 BENCHMARK_CAPTURE(qu8_vadd, neon_ld64_x16,
78 xnn_qu8_vadd_minmax_ukernel__neon_ld64_x16,
79 xnn_init_qu8_add_minmax_neon_params,
80 benchmark::utils::CheckNEON)
81 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
82 ->UseRealTime();
83 BENCHMARK_CAPTURE(qu8_vadd, neon_ld64_x32,
84 xnn_qu8_vadd_minmax_ukernel__neon_ld64_x32,
85 xnn_init_qu8_add_minmax_neon_params,
86 benchmark::utils::CheckNEON)
87 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
88 ->UseRealTime();
89
90 BENCHMARK_CAPTURE(qu8_vadd, neon_ld128_x16,
91 xnn_qu8_vadd_minmax_ukernel__neon_ld128_x16,
92 xnn_init_qu8_add_minmax_neon_params,
93 benchmark::utils::CheckNEON)
94 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
95 ->UseRealTime();
96 #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
97
98 #if XNN_ARCH_X86 || XNN_ARCH_X86_64
99 BENCHMARK_CAPTURE(qu8_vadd, avx512skx_mul32_ld128_x16,
100 xnn_qu8_vadd_minmax_ukernel__avx512skx_mul32_ld128_x16,
101 xnn_init_qu8_add_minmax_avx512_params,
102 benchmark::utils::CheckAVX512SKX)
103 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
104 ->UseRealTime();
105 BENCHMARK_CAPTURE(qu8_vadd, avx512skx_mul32_ld128_x32,
106 xnn_qu8_vadd_minmax_ukernel__avx512skx_mul32_ld128_x32,
107 xnn_init_qu8_add_minmax_avx512_params,
108 benchmark::utils::CheckAVX512SKX)
109 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
110 ->UseRealTime();
111
112 BENCHMARK_CAPTURE(qu8_vadd, avx2_mul32_ld64_x8,
113 xnn_qu8_vadd_minmax_ukernel__avx2_mul32_ld64_x8,
114 xnn_init_qu8_add_minmax_avx2_params,
115 benchmark::utils::CheckAVX2)
116 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
117 ->UseRealTime();
118 BENCHMARK_CAPTURE(qu8_vadd, avx2_mul32_ld64_x16,
119 xnn_qu8_vadd_minmax_ukernel__avx2_mul32_ld64_x16,
120 xnn_init_qu8_add_minmax_avx2_params,
121 benchmark::utils::CheckAVX2)
122 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
123 ->UseRealTime();
124
125 BENCHMARK_CAPTURE(qu8_vadd, xop_mul32_ld32_x8,
126 xnn_qu8_vadd_minmax_ukernel__xop_mul32_ld32_x8,
127 xnn_init_qu8_add_minmax_sse4_params,
128 benchmark::utils::CheckXOP)
129 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
130 ->UseRealTime();
131 BENCHMARK_CAPTURE(qu8_vadd, xop_mul32_ld32_x16,
132 xnn_qu8_vadd_minmax_ukernel__xop_mul32_ld32_x16,
133 xnn_init_qu8_add_minmax_sse4_params,
134 benchmark::utils::CheckXOP)
135 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
136 ->UseRealTime();
137
138 BENCHMARK_CAPTURE(qu8_vadd, avx_mul16_ld64_x8,
139 xnn_qu8_vadd_minmax_ukernel__avx_mul16_ld64_x8,
140 xnn_init_qu8_add_minmax_sse2_params,
141 benchmark::utils::CheckAVX)
142 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
143 ->UseRealTime();
144 BENCHMARK_CAPTURE(qu8_vadd, avx_mul16_ld64_x16,
145 xnn_qu8_vadd_minmax_ukernel__avx_mul16_ld64_x16,
146 xnn_init_qu8_add_minmax_sse2_params,
147 benchmark::utils::CheckAVX)
148 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
149 ->UseRealTime();
150
151 BENCHMARK_CAPTURE(qu8_vadd, avx_mul32_ld32_x8,
152 xnn_qu8_vadd_minmax_ukernel__avx_mul32_ld32_x8,
153 xnn_init_qu8_add_minmax_sse4_params,
154 benchmark::utils::CheckAVX)
155 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
156 ->UseRealTime();
157 BENCHMARK_CAPTURE(qu8_vadd, avx_mul32_ld32_x16,
158 xnn_qu8_vadd_minmax_ukernel__avx_mul32_ld32_x16,
159 xnn_init_qu8_add_minmax_sse4_params,
160 benchmark::utils::CheckAVX)
161 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
162 ->UseRealTime();
163
164 BENCHMARK_CAPTURE(qu8_vadd, sse41_mul16_ld64_x8,
165 xnn_qu8_vadd_minmax_ukernel__sse41_mul16_ld64_x8,
166 xnn_init_qu8_add_minmax_sse2_params,
167 benchmark::utils::CheckSSE41)
168 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
169 ->UseRealTime();
170 BENCHMARK_CAPTURE(qu8_vadd, sse41_mul16_ld64_x16,
171 xnn_qu8_vadd_minmax_ukernel__sse41_mul16_ld64_x16,
172 xnn_init_qu8_add_minmax_sse2_params,
173 benchmark::utils::CheckSSE41)
174 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
175 ->UseRealTime();
176
177 BENCHMARK_CAPTURE(qu8_vadd, sse41_mul32_ld32_x8,
178 xnn_qu8_vadd_minmax_ukernel__sse41_mul32_ld32_x8,
179 xnn_init_qu8_add_minmax_sse4_params,
180 benchmark::utils::CheckSSE41)
181 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
182 ->UseRealTime();
183 BENCHMARK_CAPTURE(qu8_vadd, sse41_mul32_ld32_x16,
184 xnn_qu8_vadd_minmax_ukernel__sse41_mul32_ld32_x16,
185 xnn_init_qu8_add_minmax_sse4_params,
186 benchmark::utils::CheckSSE41)
187 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
188 ->UseRealTime();
189
190 BENCHMARK_CAPTURE(qu8_vadd, sse2_mul16_ld64_x8,
191 xnn_qu8_vadd_minmax_ukernel__sse2_mul16_ld64_x8,
192 xnn_init_qu8_add_minmax_sse2_params)
193 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
194 ->UseRealTime();
195 BENCHMARK_CAPTURE(qu8_vadd, sse2_mul16_ld64_x16,
196 xnn_qu8_vadd_minmax_ukernel__sse2_mul16_ld64_x16,
197 xnn_init_qu8_add_minmax_sse2_params)
198 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
199 ->UseRealTime();
200 #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
201
202 #if XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
203 BENCHMARK_CAPTURE(qu8_vadd, wasmsimd_x8,
204 xnn_qu8_vadd_minmax_ukernel__wasmsimd_x8,
205 xnn_init_qu8_add_minmax_wasmsimd_params)
206 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
207 ->UseRealTime();
208 BENCHMARK_CAPTURE(qu8_vadd, wasmsimd_x16,
209 xnn_qu8_vadd_minmax_ukernel__wasmsimd_x16,
210 xnn_init_qu8_add_minmax_wasmsimd_params)
211 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
212 ->UseRealTime();
213 #endif // XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
214
215 BENCHMARK_CAPTURE(qu8_vadd, scalar_x1,
216 xnn_qu8_vadd_minmax_ukernel__scalar_x1,
217 xnn_init_qu8_add_minmax_scalar_params)
218 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
219 ->UseRealTime();
220 BENCHMARK_CAPTURE(qu8_vadd, scalar_x2,
221 xnn_qu8_vadd_minmax_ukernel__scalar_x2,
222 xnn_init_qu8_add_minmax_scalar_params)
223 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
224 ->UseRealTime();
225 BENCHMARK_CAPTURE(qu8_vadd, scalar_x4,
226 xnn_qu8_vadd_minmax_ukernel__scalar_x4,
227 xnn_init_qu8_add_minmax_scalar_params)
228 ->Apply(benchmark::utils::BinaryElementwiseParameters<uint8_t, uint8_t>)
229 ->UseRealTime();
230
231 #ifndef XNNPACK_BENCHMARK_NO_MAIN
232 BENCHMARK_MAIN();
233 #endif
234