• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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 <cfloat>
8 #include <cmath>
9 #include <functional>
10 #include <random>
11 #include <vector>
12 
13 #include <cpuinfo.h>
14 
15 #include <benchmark/benchmark.h>
16 #include <fp16/fp16.h>
17 #include "bench/dwconv.h"
18 #include "bench/utils.h"
19 #include <xnnpack/AlignedAllocator.h>
20 #include <xnnpack/common.h>
21 #include <xnnpack/dwconv.h>
22 #include <xnnpack/indirection.h>
23 #include <xnnpack/operator.h>
24 #include <xnnpack/pack.h>
25 #include <xnnpack/params-init.h>
26 #include <xnnpack/params.h>
27 
28 
DWConvBenchmark(benchmark::State & state,xnn_f16_dwconv_minmax_unipass_ukernel_function dwconv,xnn_init_f16_minmax_params_fn init_params,uint32_t cr,uint32_t kr,benchmark::utils::IsaCheckFunction isa_check=nullptr)29 static void DWConvBenchmark(benchmark::State& state,
30   xnn_f16_dwconv_minmax_unipass_ukernel_function dwconv,
31   xnn_init_f16_minmax_params_fn init_params,
32   uint32_t cr, uint32_t kr,
33   benchmark::utils::IsaCheckFunction isa_check = nullptr)
34 {
35   if (!cpuinfo_initialize()) {
36     state.SkipWithError("cpuinfo initialization failed");
37     return;
38   }
39   if (isa_check && !isa_check(state)) {
40     return;
41   }
42 
43   const size_t input_height = state.range(0);
44   const size_t input_width = state.range(1);
45   const size_t kernel_height = state.range(2);
46   const size_t kernel_width = state.range(3);
47   const size_t padding_height = state.range(4);
48   const size_t padding_width = state.range(5);
49   const size_t subsampling = state.range(6);
50   const size_t dilation = state.range(7);
51   const size_t channels = state.range(8);
52 
53   const size_t kernel_size = kernel_height * kernel_width;
54   if (kernel_size != kr) {
55     state.SkipWithError("kernel size mismatch");
56     return;
57   }
58 
59   std::random_device random_device;
60   auto rng = std::mt19937(random_device());
61   auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), std::ref(rng));
62   auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
63 
64   const size_t effective_kernel_height = (kernel_height - 1) * dilation + 1;
65   const size_t effective_kernel_width = (kernel_width - 1) * dilation + 1;
66   const size_t padding_left = padding_width / 2;
67   const size_t padding_top = padding_height / 2;
68   const size_t output_height = (input_height + padding_height - effective_kernel_height) / subsampling + 1;
69   const size_t output_width = (input_width + padding_width - effective_kernel_width) / subsampling + 1;
70   const size_t output_size = output_height * output_width;
71   const size_t step_width = dilation == 1 ? subsampling : kernel_width;
72   const size_t step_height = kernel_size + (output_width - 1) * step_width * kernel_height;
73 
74   const size_t c_stride = benchmark::utils::RoundUp<size_t>(channels, cr);
75 
76   std::vector<uint16_t> a(channels * input_height * input_width + XNN_EXTRA_BYTES / sizeof(uint16_t));
77   std::generate(a.begin(), a.end(), std::ref(f16rng));
78   std::vector<uint16_t> k(channels * kernel_height * kernel_width);
79   std::generate(k.begin(), k.end(), std::ref(f16rng));
80   std::vector<uint16_t> b(channels);
81   std::generate(b.begin(), b.end(), std::ref(f16rng));
82 
83   std::vector<uint16_t> z(channels + XNN_EXTRA_BYTES / sizeof(uint16_t));
84 
85   const size_t w_elements = (kernel_size + 1) * c_stride;
86   const size_t i_elements = output_height * step_height;
87   const size_t c_elements = output_size * channels;
88   const size_t num_buffers = 1 +
89     benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(),
90       sizeof(uint16_t) * (w_elements + c_elements) + sizeof(void*) * i_elements);
91 
92   std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> w(w_elements * num_buffers);
93   std::fill(w.begin(), w.end(), 0.0f);
94   xnn_pack_f16_dwconv_ghw_w(kernel_height, kernel_width, channels, cr,
95       k.data(), b.data(), w.data(), 0 /* extra bytes */, nullptr);
96   for (size_t n = 1; n < num_buffers; n++) {
97     std::copy(w.cbegin(), w.cbegin() + w_elements, w.begin() + n * w_elements);
98   }
99 
100   std::vector<const uint16_t*> i(i_elements * num_buffers);
101   xnn_operator convolution_op = { };
102   convolution_op.indirection_buffer = reinterpret_cast<const void**>(i.data());
103   convolution_op.input              = a.data();
104   convolution_op.input_pixel_stride = channels;
105   convolution_op.zero_buffer        = z.data();
106   convolution_op.input_height       = input_height;
107   convolution_op.input_width        = input_width;
108   convolution_op.output_height      = output_height;
109   convolution_op.output_width       = output_width;
110   convolution_op.kernel_height      = kernel_height;
111   convolution_op.kernel_width       = kernel_width;
112   convolution_op.stride_height      = subsampling;
113   convolution_op.stride_width       = subsampling;
114   convolution_op.dilation_height    = dilation;
115   convolution_op.dilation_width     = dilation;
116   convolution_op.padding_top        = padding_top;
117   convolution_op.padding_left       = padding_left;
118 
119   xnn_indirection_init_dwconv2d(&convolution_op, step_height, step_width, 1 /* log2(sizeof(uint16_t)) */);
120   for (size_t n = 1; n < num_buffers; n++) {
121     std::copy(i.cbegin(), i.cbegin() + i_elements, i.begin() + n * i_elements);
122   }
123 
124   std::vector<uint16_t> c(c_elements * num_buffers);
125   std::fill(c.begin(), c.end(), std::nanf(""));
126 
127   xnn_f16_minmax_params params;
128   init_params(&params, UINT16_C(0xFC00) /* -inf */, UINT16_C(0x7C00) /* inf */);
129 
130   size_t buffer_index = 0;
131   for (auto _ : state) {
132     state.PauseTiming();
133     benchmark::utils::PrefetchToL1(a.data(), a.size() * sizeof(uint16_t));
134     buffer_index = (buffer_index + 1) % num_buffers;
135     state.ResumeTiming();
136 
137     for (size_t y = 0; y < output_height; y++) {
138       dwconv(channels, output_width,
139         reinterpret_cast<const void**>(i.data() + buffer_index * i_elements + step_height * y),
140         w.data() + buffer_index * w_elements,
141         c.data() + buffer_index * c_elements + y * output_width * channels,
142         kernel_height * step_width * sizeof(void*), 0,
143         0, z.data(), &params);
144     }
145   }
146 
147   const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
148   if (cpu_frequency != 0) {
149     state.counters["cpufreq"] = cpu_frequency;
150   }
151 
152   state.counters["FLOPS"] = benchmark::Counter(
153     uint64_t(state.iterations()) * 2 * output_size * channels * kernel_size, benchmark::Counter::kIsRate);
154 
155   state.counters["bytes"] = benchmark::Counter(
156     uint64_t(state.iterations()) * (output_size + input_height * input_width + kernel_size + 1 /* bias */) * channels * sizeof(uint16_t),
157     benchmark::Counter::kIsRate);
158 }
159 
160 #if XNN_ARCH_ARM64
f16_dwconv_8x4__neonfp16arith_acc2(benchmark::State & state,const char * net)161   static void f16_dwconv_8x4__neonfp16arith_acc2(benchmark::State& state, const char* net) {
162     DWConvBenchmark(state,
163       xnn_f16_dwconv_minmax_ukernel_up8x4__neonfp16arith_acc2,
164       xnn_init_f16_minmax_neon_params,
165       8, 4, benchmark::utils::CheckNEONFP16ARITH);
166   }
167 
f16_dwconv_8x4__neonfp16arith(benchmark::State & state,const char * net)168   static void f16_dwconv_8x4__neonfp16arith(benchmark::State& state, const char* net) {
169     DWConvBenchmark(state,
170       xnn_f16_dwconv_minmax_ukernel_up8x4__neonfp16arith,
171       xnn_init_f16_minmax_neon_params,
172       8, 4, benchmark::utils::CheckNEONFP16ARITH);
173   }
174 
f16_dwconv_8x9__neonfp16arith_acc2(benchmark::State & state,const char * net)175   static void f16_dwconv_8x9__neonfp16arith_acc2(benchmark::State& state, const char* net) {
176     DWConvBenchmark(state,
177       xnn_f16_dwconv_minmax_ukernel_up8x9__neonfp16arith_acc2,
178       xnn_init_f16_minmax_neon_params,
179       8, 9, benchmark::utils::CheckNEONFP16ARITH);
180   }
181 
f16_dwconv_8x9__neonfp16arith(benchmark::State & state,const char * net)182   static void f16_dwconv_8x9__neonfp16arith(benchmark::State& state, const char* net) {
183     DWConvBenchmark(state,
184       xnn_f16_dwconv_minmax_ukernel_up8x9__neonfp16arith,
185       xnn_init_f16_minmax_neon_params,
186       8, 9, benchmark::utils::CheckNEONFP16ARITH);
187   }
188 
f16_dwconv_8x25__neonfp16arith_acc2(benchmark::State & state,const char * net)189   static void f16_dwconv_8x25__neonfp16arith_acc2(benchmark::State& state, const char* net) {
190     DWConvBenchmark(state,
191       xnn_f16_dwconv_minmax_ukernel_up8x25__neonfp16arith_acc2,
192       xnn_init_f16_minmax_neon_params,
193       8, 25, benchmark::utils::CheckNEONFP16ARITH);
194   }
195 
f16_dwconv_8x25__neonfp16arith(benchmark::State & state,const char * net)196   static void f16_dwconv_8x25__neonfp16arith(benchmark::State& state, const char* net) {
197     DWConvBenchmark(state,
198       xnn_f16_dwconv_minmax_ukernel_up8x25__neonfp16arith,
199       xnn_init_f16_minmax_neon_params,
200       8, 25, benchmark::utils::CheckNEONFP16ARITH);
201   }
202 
f16_dwconv_16x4__neonfp16arith_acc2(benchmark::State & state,const char * net)203   static void f16_dwconv_16x4__neonfp16arith_acc2(benchmark::State& state, const char* net) {
204     DWConvBenchmark(state,
205       xnn_f16_dwconv_minmax_ukernel_up16x4__neonfp16arith_acc2,
206       xnn_init_f16_minmax_neon_params,
207       16, 4, benchmark::utils::CheckNEONFP16ARITH);
208   }
209 
f16_dwconv_16x4__neonfp16arith(benchmark::State & state,const char * net)210   static void f16_dwconv_16x4__neonfp16arith(benchmark::State& state, const char* net) {
211     DWConvBenchmark(state,
212       xnn_f16_dwconv_minmax_ukernel_up16x4__neonfp16arith,
213       xnn_init_f16_minmax_neon_params,
214       16, 4, benchmark::utils::CheckNEONFP16ARITH);
215   }
216 
f16_dwconv_16x9__neonfp16arith_acc2(benchmark::State & state,const char * net)217   static void f16_dwconv_16x9__neonfp16arith_acc2(benchmark::State& state, const char* net) {
218     DWConvBenchmark(state,
219       xnn_f16_dwconv_minmax_ukernel_up16x9__neonfp16arith_acc2,
220       xnn_init_f16_minmax_neon_params,
221       16, 9, benchmark::utils::CheckNEONFP16ARITH);
222   }
223 
f16_dwconv_16x9__neonfp16arith(benchmark::State & state,const char * net)224   static void f16_dwconv_16x9__neonfp16arith(benchmark::State& state, const char* net) {
225     DWConvBenchmark(state,
226       xnn_f16_dwconv_minmax_ukernel_up16x9__neonfp16arith,
227       xnn_init_f16_minmax_neon_params,
228       16, 9, benchmark::utils::CheckNEONFP16ARITH);
229   }
230 
f16_dwconv_16x25__neonfp16arith_acc2(benchmark::State & state,const char * net)231   static void f16_dwconv_16x25__neonfp16arith_acc2(benchmark::State& state, const char* net) {
232     DWConvBenchmark(state,
233       xnn_f16_dwconv_minmax_ukernel_up16x25__neonfp16arith_acc2,
234       xnn_init_f16_minmax_neon_params,
235       16, 25, benchmark::utils::CheckNEONFP16ARITH);
236   }
237 
f16_dwconv_16x25__neonfp16arith(benchmark::State & state,const char * net)238   static void f16_dwconv_16x25__neonfp16arith(benchmark::State& state, const char* net) {
239     DWConvBenchmark(state,
240       xnn_f16_dwconv_minmax_ukernel_up16x25__neonfp16arith,
241       xnn_init_f16_minmax_neon_params,
242       16, 25, benchmark::utils::CheckNEONFP16ARITH);
243   }
244 
f16_dwconv_32x4__neonfp16arith_acc2(benchmark::State & state,const char * net)245   static void f16_dwconv_32x4__neonfp16arith_acc2(benchmark::State& state, const char* net) {
246     DWConvBenchmark(state,
247       xnn_f16_dwconv_minmax_ukernel_up32x4__neonfp16arith_acc2,
248       xnn_init_f16_minmax_neon_params,
249       32, 4, benchmark::utils::CheckNEONFP16ARITH);
250   }
251 
f16_dwconv_32x4__neonfp16arith(benchmark::State & state,const char * net)252   static void f16_dwconv_32x4__neonfp16arith(benchmark::State& state, const char* net) {
253     DWConvBenchmark(state,
254       xnn_f16_dwconv_minmax_ukernel_up32x4__neonfp16arith,
255       xnn_init_f16_minmax_neon_params,
256       32, 4, benchmark::utils::CheckNEONFP16ARITH);
257   }
258 
f16_dwconv_32x9__neonfp16arith_acc2(benchmark::State & state,const char * net)259   static void f16_dwconv_32x9__neonfp16arith_acc2(benchmark::State& state, const char* net) {
260     DWConvBenchmark(state,
261       xnn_f16_dwconv_minmax_ukernel_up32x9__neonfp16arith_acc2,
262       xnn_init_f16_minmax_neon_params,
263       32, 9, benchmark::utils::CheckNEONFP16ARITH);
264   }
265 
f16_dwconv_32x9__neonfp16arith(benchmark::State & state,const char * net)266   static void f16_dwconv_32x9__neonfp16arith(benchmark::State& state, const char* net) {
267     DWConvBenchmark(state,
268       xnn_f16_dwconv_minmax_ukernel_up32x9__neonfp16arith,
269       xnn_init_f16_minmax_neon_params,
270       32, 9, benchmark::utils::CheckNEONFP16ARITH);
271   }
272 
f16_dwconv_32x25__neonfp16arith_acc2(benchmark::State & state,const char * net)273   static void f16_dwconv_32x25__neonfp16arith_acc2(benchmark::State& state, const char* net) {
274     DWConvBenchmark(state,
275       xnn_f16_dwconv_minmax_ukernel_up32x25__neonfp16arith_acc2,
276       xnn_init_f16_minmax_neon_params,
277       32, 25, benchmark::utils::CheckNEONFP16ARITH);
278   }
279 
f16_dwconv_32x25__neonfp16arith(benchmark::State & state,const char * net)280   static void f16_dwconv_32x25__neonfp16arith(benchmark::State& state, const char* net) {
281     DWConvBenchmark(state,
282       xnn_f16_dwconv_minmax_ukernel_up32x25__neonfp16arith,
283       xnn_init_f16_minmax_neon_params,
284       32, 25, benchmark::utils::CheckNEONFP16ARITH);
285   }
286 
287   BENCHMARK_DWCONV(f16_dwconv_8x4__neonfp16arith_acc2)
288   BENCHMARK_DWCONV(f16_dwconv_8x4__neonfp16arith)
289   BENCHMARK_DWCONV(f16_dwconv_8x9__neonfp16arith_acc2)
290   BENCHMARK_DWCONV(f16_dwconv_8x9__neonfp16arith)
291   BENCHMARK_DWCONV(f16_dwconv_8x25__neonfp16arith_acc2)
292   BENCHMARK_DWCONV(f16_dwconv_8x25__neonfp16arith)
293   BENCHMARK_DWCONV(f16_dwconv_16x4__neonfp16arith_acc2)
294   BENCHMARK_DWCONV(f16_dwconv_16x4__neonfp16arith)
295   BENCHMARK_DWCONV(f16_dwconv_16x9__neonfp16arith_acc2)
296   BENCHMARK_DWCONV(f16_dwconv_16x9__neonfp16arith)
297   BENCHMARK_DWCONV(f16_dwconv_16x25__neonfp16arith_acc2)
298   BENCHMARK_DWCONV(f16_dwconv_16x25__neonfp16arith)
299   BENCHMARK_DWCONV(f16_dwconv_32x4__neonfp16arith_acc2)
300   BENCHMARK_DWCONV(f16_dwconv_32x4__neonfp16arith)
301   BENCHMARK_DWCONV(f16_dwconv_32x9__neonfp16arith_acc2)
302   BENCHMARK_DWCONV(f16_dwconv_32x9__neonfp16arith)
303   BENCHMARK_DWCONV(f16_dwconv_32x25__neonfp16arith_acc2)
304   BENCHMARK_DWCONV(f16_dwconv_32x25__neonfp16arith)
305 #endif  // XNN_ARCH_ARM64
306 
307 #ifndef XNNPACK_BENCHMARK_NO_MAIN
308 BENCHMARK_MAIN();
309 #endif
310