• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/vcvt.h>
22 
23 
qs8_vcvt(benchmark::State & state,xnn_qs8_vcvt_ukernel_function cvt,xnn_init_qs8_cvt_params_fn init_params,benchmark::utils::IsaCheckFunction isa_check=nullptr)24 static void qs8_vcvt(
25   benchmark::State& state,
26   xnn_qs8_vcvt_ukernel_function cvt,
27   xnn_init_qs8_cvt_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_cvt_params params;
48   init_params(&params, 1.25f /* scale */, -1 /* input zero point */, 1 /* output zero point */);
49   for (auto _ : state) {
50     cvt(num_elements * sizeof(int8_t), x.data(), y.data(), &params);
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_vcvt, neon_x8,
69                     xnn_qs8_vcvt_ukernel__neon_x8,
70                     xnn_init_qs8_cvt_neon_params,
71                     benchmark::utils::CheckNEON)
72     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
73     ->UseRealTime();
74   BENCHMARK_CAPTURE(qs8_vcvt, neon_x16,
75                     xnn_qs8_vcvt_ukernel__neon_x16,
76                     xnn_init_qs8_cvt_neon_params,
77                     benchmark::utils::CheckNEON)
78     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
79     ->UseRealTime();
80   BENCHMARK_CAPTURE(qs8_vcvt, neon_x32,
81                     xnn_qs8_vcvt_ukernel__neon_x32,
82                     xnn_init_qs8_cvt_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_vcvt, armsimd32_x4,
90                     xnn_qs8_vcvt_ukernel__armsimd32_x4,
91                     xnn_init_qs8_cvt_armsimd32_params,
92                     benchmark::utils::CheckARMV6)
93     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
94     ->UseRealTime();
95   BENCHMARK_CAPTURE(qs8_vcvt, armsimd32_x8,
96                     xnn_qs8_vcvt_ukernel__armsimd32_x8,
97                     xnn_init_qs8_cvt_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_vcvt, avx2_x16,
105                     xnn_qs8_vcvt_ukernel__avx2_x16,
106                     xnn_init_qs8_cvt_avx2_params,
107                     benchmark::utils::CheckAVX2)
108     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
109     ->UseRealTime();
110   BENCHMARK_CAPTURE(qs8_vcvt, avx2_x32,
111                     xnn_qs8_vcvt_ukernel__avx2_x32,
112                     xnn_init_qs8_cvt_avx2_params,
113                     benchmark::utils::CheckAVX2)
114     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
115     ->UseRealTime();
116   BENCHMARK_CAPTURE(qs8_vcvt, avx2_x64,
117                     xnn_qs8_vcvt_ukernel__avx2_x64,
118                     xnn_init_qs8_cvt_avx2_params,
119                     benchmark::utils::CheckAVX2)
120     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
121     ->UseRealTime();
122 
123   BENCHMARK_CAPTURE(qs8_vcvt, avx_x8,
124                     xnn_qs8_vcvt_ukernel__avx_x8,
125                     xnn_init_qs8_cvt_ssse3_params,
126                     benchmark::utils::CheckAVX)
127     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
128     ->UseRealTime();
129   BENCHMARK_CAPTURE(qs8_vcvt, avx_x16,
130                     xnn_qs8_vcvt_ukernel__avx_x16,
131                     xnn_init_qs8_cvt_ssse3_params,
132                     benchmark::utils::CheckAVX)
133     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
134     ->UseRealTime();
135   BENCHMARK_CAPTURE(qs8_vcvt, avx_x32,
136                     xnn_qs8_vcvt_ukernel__avx_x32,
137                     xnn_init_qs8_cvt_ssse3_params,
138                     benchmark::utils::CheckAVX)
139     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
140     ->UseRealTime();
141 
142   BENCHMARK_CAPTURE(qs8_vcvt, sse41_x8,
143                     xnn_qs8_vcvt_ukernel__sse41_x8,
144                     xnn_init_qs8_cvt_ssse3_params,
145                     benchmark::utils::CheckSSE41)
146     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
147     ->UseRealTime();
148   BENCHMARK_CAPTURE(qs8_vcvt, sse41_x16,
149                     xnn_qs8_vcvt_ukernel__sse41_x16,
150                     xnn_init_qs8_cvt_ssse3_params,
151                     benchmark::utils::CheckSSE41)
152     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
153     ->UseRealTime();
154   BENCHMARK_CAPTURE(qs8_vcvt, sse41_x32,
155                     xnn_qs8_vcvt_ukernel__sse41_x32,
156                     xnn_init_qs8_cvt_ssse3_params,
157                     benchmark::utils::CheckSSE41)
158     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
159     ->UseRealTime();
160 
161   BENCHMARK_CAPTURE(qs8_vcvt, ssse3_x16,
162                     xnn_qs8_vcvt_ukernel__ssse3_x16,
163                     xnn_init_qs8_cvt_ssse3_params,
164                     benchmark::utils::CheckSSSE3)
165     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
166     ->UseRealTime();
167   BENCHMARK_CAPTURE(qs8_vcvt, ssse3_x32,
168                     xnn_qs8_vcvt_ukernel__ssse3_x32,
169                     xnn_init_qs8_cvt_ssse3_params,
170                     benchmark::utils::CheckSSSE3)
171     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
172     ->UseRealTime();
173 
174   BENCHMARK_CAPTURE(qs8_vcvt, sse2_x16,
175                     xnn_qs8_vcvt_ukernel__sse2_x16,
176                     xnn_init_qs8_cvt_sse2_params)
177     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
178     ->UseRealTime();
179   BENCHMARK_CAPTURE(qs8_vcvt, sse2_x32,
180                     xnn_qs8_vcvt_ukernel__sse2_x32,
181                     xnn_init_qs8_cvt_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_vcvt, wasmrelaxedsimd_x8,
188                     xnn_qs8_vcvt_ukernel__wasmrelaxedsimd_x8,
189                     xnn_init_qs8_cvt_wasmsimd_params)
190     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
191     ->UseRealTime();
192   BENCHMARK_CAPTURE(qs8_vcvt, wasmrelaxedsimd_x16,
193                     xnn_qs8_vcvt_ukernel__wasmrelaxedsimd_x16,
194                     xnn_init_qs8_cvt_wasmsimd_params)
195     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
196     ->UseRealTime();
197   BENCHMARK_CAPTURE(qs8_vcvt, wasmrelaxedsimd_x32,
198                     xnn_qs8_vcvt_ukernel__wasmrelaxedsimd_x32,
199                     xnn_init_qs8_cvt_wasmsimd_params)
200     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
201     ->UseRealTime();
202 #endif  // XNN_ARCH_WASMRELAXEDSIMD
203 
204 #if XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
205   BENCHMARK_CAPTURE(qs8_vcvt, wasmsimd_x8,
206                     xnn_qs8_vcvt_ukernel__wasmsimd_x8,
207                     xnn_init_qs8_cvt_wasmsimd_params)
208     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
209     ->UseRealTime();
210   BENCHMARK_CAPTURE(qs8_vcvt, wasmsimd_x16,
211                     xnn_qs8_vcvt_ukernel__wasmsimd_x16,
212                     xnn_init_qs8_cvt_wasmsimd_params)
213     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
214     ->UseRealTime();
215   BENCHMARK_CAPTURE(qs8_vcvt, wasmsimd_x32,
216                     xnn_qs8_vcvt_ukernel__wasmsimd_x32,
217                     xnn_init_qs8_cvt_wasmsimd_params)
218     ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
219     ->UseRealTime();
220 #endif  // XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
221 
222 BENCHMARK_CAPTURE(qs8_vcvt, scalar_x1,
223                   xnn_qs8_vcvt_ukernel__scalar_x1,
224                   xnn_init_qs8_cvt_scalar_params)
225   ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
226   ->UseRealTime();
227 BENCHMARK_CAPTURE(qs8_vcvt, scalar_x2,
228                   xnn_qs8_vcvt_ukernel__scalar_x2,
229                   xnn_init_qs8_cvt_scalar_params)
230   ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
231   ->UseRealTime();
232 BENCHMARK_CAPTURE(qs8_vcvt, scalar_x4,
233                   xnn_qs8_vcvt_ukernel__scalar_x4,
234                   xnn_init_qs8_cvt_scalar_params)
235   ->Apply(benchmark::utils::UnaryElementwiseParameters<int8_t, int8_t>)
236   ->UseRealTime();
237 
238 #ifndef XNNPACK_BENCHMARK_NO_MAIN
239 BENCHMARK_MAIN();
240 #endif
241