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 <array>
8 #include <cmath>
9 #include <functional>
10 #include <random>
11 #include <vector>
12
13 #include <benchmark/benchmark.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/lut.h>
20 #include <xnnpack/microfnptr.h>
21
22
x8_lut(benchmark::State & state,xnn_x8_lut_ukernel_function lut,benchmark::utils::IsaCheckFunction isa_check=nullptr)23 static void x8_lut(
24 benchmark::State& state,
25 xnn_x8_lut_ukernel_function lut,
26 benchmark::utils::IsaCheckFunction isa_check = nullptr)
27 {
28 if (isa_check && !isa_check(state)) {
29 return;
30 }
31
32 const size_t num_elements = state.range(0);
33 std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> input(num_elements);
34 std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> output(num_elements);
35 std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> table(256);
36
37 std::random_device random_device;
38 auto rng = std::mt19937(random_device());
39 auto u8rng = std::bind(
40 std::uniform_int_distribution<uint32_t>(0, std::numeric_limits<uint8_t>::max()), std::ref(rng));
41 std::generate(input.begin(), input.end(), std::ref(u8rng));
42 std::generate(table.begin(), table.end(), std::ref(u8rng));
43 std::fill(output.begin(), output.end(), UINT8_C(0xAA));
44
45 for (auto _ : state) {
46 lut(num_elements * sizeof(uint8_t), input.data(), output.data(), table.data());
47 }
48
49 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
50 if (cpu_frequency != 0) {
51 state.counters["cpufreq"] = cpu_frequency;
52 }
53
54 const size_t elements_per_iteration = num_elements;
55 state.counters["elements"] =
56 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
57
58 const size_t bytes_per_iteration = 2 * num_elements * sizeof(uint8_t);
59 state.counters["bytes"] =
60 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
61 }
62
63 #if XNN_ARCH_ARM64
64 BENCHMARK_CAPTURE(x8_lut, neon_tbx128x4_x16,
65 xnn_x8_lut_ukernel__neon_tbx128x4_x16)
66 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
67 ->UseRealTime();
68 BENCHMARK_CAPTURE(x8_lut, neon_tbx128x4_x32,
69 xnn_x8_lut_ukernel__neon_tbx128x4_x32)
70 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
71 ->UseRealTime();
72 BENCHMARK_CAPTURE(x8_lut, neon_tbx128x4_x48,
73 xnn_x8_lut_ukernel__neon_tbx128x4_x48)
74 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
75 ->UseRealTime();
76 BENCHMARK_CAPTURE(x8_lut, neon_tbx128x4_x64,
77 xnn_x8_lut_ukernel__neon_tbx128x4_x64)
78 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
79 ->UseRealTime();
80 #endif // XNN_ARCH_ARM64
81
82 #if XNN_ARCH_X86 || XNN_ARCH_X86_64
83 BENCHMARK_CAPTURE(x8_lut, avx512skx_vpshufb_x64,
84 xnn_x8_lut_ukernel__avx512skx_vpshufb_x64,
85 benchmark::utils::CheckAVX512SKX)
86 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
87 ->UseRealTime();
88 BENCHMARK_CAPTURE(x8_lut, avx512skx_vpshufb_x128,
89 xnn_x8_lut_ukernel__avx512skx_vpshufb_x128,
90 benchmark::utils::CheckAVX512SKX)
91 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
92 ->UseRealTime();
93 BENCHMARK_CAPTURE(x8_lut, avx512skx_vpshufb_x192,
94 xnn_x8_lut_ukernel__avx512skx_vpshufb_x192,
95 benchmark::utils::CheckAVX512SKX)
96 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
97 ->UseRealTime();
98 BENCHMARK_CAPTURE(x8_lut, avx512skx_vpshufb_x256,
99 xnn_x8_lut_ukernel__avx512skx_vpshufb_x256,
100 benchmark::utils::CheckAVX512SKX)
101 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
102 ->UseRealTime();
103
104 BENCHMARK_CAPTURE(x8_lut, avx2_x32,
105 xnn_x8_lut_ukernel__avx2_x32,
106 benchmark::utils::CheckAVX2)
107 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
108 ->UseRealTime();
109 BENCHMARK_CAPTURE(x8_lut, avx2_x64,
110 xnn_x8_lut_ukernel__avx2_x64,
111 benchmark::utils::CheckAVX2)
112 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
113 ->UseRealTime();
114 BENCHMARK_CAPTURE(x8_lut, avx2_x96,
115 xnn_x8_lut_ukernel__avx2_x96,
116 benchmark::utils::CheckAVX2)
117 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
118 ->UseRealTime();
119 BENCHMARK_CAPTURE(x8_lut, avx2_x128,
120 xnn_x8_lut_ukernel__avx2_x128,
121 benchmark::utils::CheckAVX2)
122 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
123 ->UseRealTime();
124
125 BENCHMARK_CAPTURE(x8_lut, avx_x16,
126 xnn_x8_lut_ukernel__avx_x16,
127 benchmark::utils::CheckAVX)
128 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
129 ->UseRealTime();
130 BENCHMARK_CAPTURE(x8_lut, avx_x32,
131 xnn_x8_lut_ukernel__avx_x32,
132 benchmark::utils::CheckAVX)
133 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
134 ->UseRealTime();
135 BENCHMARK_CAPTURE(x8_lut, avx_x48,
136 xnn_x8_lut_ukernel__avx_x48,
137 benchmark::utils::CheckAVX)
138 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
139 ->UseRealTime();
140 BENCHMARK_CAPTURE(x8_lut, avx_x64,
141 xnn_x8_lut_ukernel__avx_x64,
142 benchmark::utils::CheckAVX)
143 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
144 ->UseRealTime();
145
146 BENCHMARK_CAPTURE(x8_lut, ssse3_x16,
147 xnn_x8_lut_ukernel__ssse3_x16,
148 benchmark::utils::CheckSSSE3)
149 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
150 ->UseRealTime();
151 BENCHMARK_CAPTURE(x8_lut, ssse3_x32,
152 xnn_x8_lut_ukernel__ssse3_x32,
153 benchmark::utils::CheckSSSE3)
154 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
155 ->UseRealTime();
156 #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
157
158 #if XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
159 BENCHMARK_CAPTURE(x8_lut, wasmsimd_x16,
160 xnn_x8_lut_ukernel__wasmsimd_x16)
161 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
162 ->UseRealTime();
163 BENCHMARK_CAPTURE(x8_lut, wasmsimd_x32,
164 xnn_x8_lut_ukernel__wasmsimd_x32)
165 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
166 ->UseRealTime();
167 BENCHMARK_CAPTURE(x8_lut, wasmsimd_x48,
168 xnn_x8_lut_ukernel__wasmsimd_x48)
169 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
170 ->UseRealTime();
171 BENCHMARK_CAPTURE(x8_lut, wasmsimd_x64,
172 xnn_x8_lut_ukernel__wasmsimd_x64)
173 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
174 ->UseRealTime();
175 #endif // XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
176
177 BENCHMARK_CAPTURE(x8_lut, scalar_x1,
178 xnn_x8_lut_ukernel__scalar_x1)
179 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
180 ->UseRealTime();
181 BENCHMARK_CAPTURE(x8_lut, scalar_x2,
182 xnn_x8_lut_ukernel__scalar_x2)
183 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
184 ->UseRealTime();
185 BENCHMARK_CAPTURE(x8_lut, scalar_x4,
186 xnn_x8_lut_ukernel__scalar_x4)
187 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
188 ->UseRealTime();
189 BENCHMARK_CAPTURE(x8_lut, scalar_x8,
190 xnn_x8_lut_ukernel__scalar_x8)
191 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
192 ->UseRealTime();
193 BENCHMARK_CAPTURE(x8_lut, scalar_x16,
194 xnn_x8_lut_ukernel__scalar_x16)
195 ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>)
196 ->UseRealTime();
197
198 #ifndef XNNPACK_BENCHMARK_NO_MAIN
199 BENCHMARK_MAIN();
200 #endif
201