• 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 "bench/conv.h"
17 #include "bench/utils.h"
18 #include <xnnpack/AlignedAllocator.h>
19 #include <xnnpack/common.h>
20 #include <xnnpack/gemm.h>
21 #include <xnnpack/im2col.h>
22 #include <xnnpack/pack.h>
23 #include <xnnpack/params-init.h>
24 #include <xnnpack/params.h>
25 
26 
Im2ColGEMMBenchmark(benchmark::State & state,xnn_f32_gemm_ukernel_function f32_gemm,uint32_t mr,uint32_t nr,uint32_t kr,uint32_t sr)27 static void Im2ColGEMMBenchmark(benchmark::State& state,
28   xnn_f32_gemm_ukernel_function f32_gemm,
29   uint32_t mr, uint32_t nr, uint32_t kr, uint32_t sr)
30 {
31   if (!cpuinfo_initialize()) {
32     state.SkipWithError("cpuinfo initialization failed");
33     return;
34   }
35 
36   const size_t input_height = state.range(0);
37   const size_t input_width = state.range(1);
38   const size_t kernel_height = state.range(2);
39   const size_t kernel_width = state.range(3);
40   const size_t kernel_size = kernel_height * kernel_width;
41   const size_t padding_height = state.range(4);
42   const size_t padding_width = state.range(5);
43   const size_t subsampling = state.range(6);
44   const size_t dilation = state.range(7);
45   const size_t group_input_channels = state.range(8);
46   const size_t group_output_channels = state.range(9);
47 
48   std::random_device random_device;
49   auto rng = std::mt19937(random_device());
50   auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng);
51 
52   const size_t effective_kernel_height = (kernel_height - 1) * dilation + 1;
53   const size_t effective_kernel_width = (kernel_width - 1) * dilation + 1;
54   const size_t padding_left = padding_width / 2;
55   const size_t padding_top = padding_height / 2;
56   const size_t output_height = (input_height + padding_height - effective_kernel_height) / subsampling + 1;
57   const size_t output_width = (input_width + padding_width - effective_kernel_width) / subsampling + 1;
58   const size_t output_size = output_height * output_width;
59 
60   const size_t nc_stride = benchmark::utils::RoundUp<size_t>(group_output_channels, nr);
61   const size_t kc_stride = benchmark::utils::RoundUp<size_t>(group_input_channels, kr);
62 
63   std::vector<float> a(input_height * input_width * group_input_channels);
64   std::generate(a.begin(), a.end(), std::ref(f32rng));
65   std::vector<float> k(group_output_channels * kernel_height * kernel_width * group_input_channels);
66   std::generate(k.begin(), k.end(), std::ref(f32rng));
67   std::vector<float> b(group_output_channels);
68   std::generate(b.begin(), b.end(), std::ref(f32rng));
69 
70   const size_t w_elements = (kernel_size * kc_stride + 1) * nc_stride;
71   const size_t c_elements = output_size * group_output_channels;
72   const size_t num_buffers = 1 +
73     benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(),
74       sizeof(float) * (w_elements + c_elements));
75 
76   std::vector<float, AlignedAllocator<float, 32>> w(w_elements * num_buffers);
77   std::fill(w.begin(), w.end(), 0.0f);
78   xnn_pack_f32_gemm_goi_w(1 /* groups */, group_output_channels, group_input_channels * kernel_size,
79     nr, kr, sr, k.data(), b.data(), w.data());
80   for (size_t n = 1; n < num_buffers; n++) {
81     std::copy(w.cbegin(), w.cbegin() + w_elements, w.begin() + n * w_elements);
82   }
83 
84   std::vector<float> im2col_buffer(output_size * group_input_channels * kernel_size * group_output_channels);
85 
86   std::vector<float> c(c_elements * num_buffers);
87   std::fill(c.begin(), c.end(), std::nanf(""));
88 
89   xnn_f32_output_params output_params =
90     xnn_init_f32_output_params(-std::numeric_limits<float>::infinity(), +std::numeric_limits<float>::infinity());
91 
92   size_t buffer_index = 0;
93   for (auto _ : state) {
94     state.PauseTiming();
95     benchmark::utils::PrefetchToL1(a.data(), a.size() * sizeof(float));
96     buffer_index = (buffer_index + 1) % num_buffers;
97     state.ResumeTiming();
98 
99     const float* inputData = a.data();
100     if (kernel_size != 1 || subsampling != 1) {
101       xnn_im2col_conv2d(
102         output_height, output_width,
103         kernel_height, kernel_width,
104         subsampling, subsampling,
105         dilation, dilation,
106         input_width, padding_top, padding_left,
107         group_input_channels * sizeof(float) /* input channels */,
108         group_input_channels * sizeof(float) /* input stride */,
109         a.data(), im2col_buffer.data());
110       inputData = im2col_buffer.data();
111     }
112 
113     for (uint32_t m = 0; m < output_size; m += mr) {
114       const uint32_t mb = min(output_size - m, mr);
115       for (uint32_t n = 0; n < group_output_channels; n += nr) {
116         const uint32_t nb = min(group_output_channels - n, nr);
117         f32_gemm(
118           mb, nb, kernel_size * group_input_channels * sizeof(float),
119           inputData + m * kernel_size * group_input_channels, kernel_size * group_input_channels * sizeof(float),
120           w.data() + (buffer_index * nc_stride + n) * (kernel_size * kc_stride + 1),
121           c.data() + (buffer_index * output_size + m) * group_output_channels + n, group_output_channels * sizeof(float), nr * sizeof(float),
122           &output_params);
123       }
124     }
125   }
126 
127   state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
128   state.counters["FLOPS"] = benchmark::Counter(
129     uint64_t(state.iterations()) * 2 *
130       output_height * output_width *
131       group_input_channels * group_output_channels *
132       kernel_height * kernel_width,
133     benchmark::Counter::kIsRate);
134 }
135 
136 
137 #if XNN_ARCH_ARM64 && XNN_ENABLE_ASSEMBLY
f32_gemm_4x8__aarch64_neonfma_cortex_a75(benchmark::State & state,const char * net)138   static void f32_gemm_4x8__aarch64_neonfma_cortex_a75(benchmark::State& state, const char* net) {
139     Im2ColGEMMBenchmark(state, xnn_f32_gemm_ukernel_4x8__aarch64_neonfma_cortex_a75, 4, 8, 1, 1);
140   }
141 
BENCHMARK_CONV(f32_gemm_4x8__aarch64_neonfma_cortex_a75)142   BENCHMARK_CONV(f32_gemm_4x8__aarch64_neonfma_cortex_a75)
143 #endif  // XNN_ARCH_ARM64
144 
145 #if !XNN_ARCH_WASM && !XNN_ARCH_ASMJS
146   static void f32_gemm_6x8__psimd_loadsplat(benchmark::State& state, const char* net) {
147     Im2ColGEMMBenchmark(state, xnn_f32_gemm_ukernel_6x8__psimd_loadsplat, 6, 8, 1, 1);
148   }
149 
BENCHMARK_CONV(f32_gemm_6x8__psimd_loadsplat)150   BENCHMARK_CONV(f32_gemm_6x8__psimd_loadsplat)
151 #endif  // !XNN_ARCH_WASM && !XNN_ARCH_ASMJS
152 
153 static void f32_gemm_2x4__scalar(benchmark::State& state, const char* net) {
154   Im2ColGEMMBenchmark(state, xnn_f32_gemm_ukernel_2x4__scalar, 2, 4, 1, 1);
155 }
156 
f32_gemm_4x4__scalar(benchmark::State & state,const char * net)157 static void f32_gemm_4x4__scalar(benchmark::State& state, const char* net) {
158   Im2ColGEMMBenchmark(state, xnn_f32_gemm_ukernel_4x4__scalar, 4, 4, 1, 1);
159 }
160 
161 BENCHMARK_CONV(f32_gemm_2x4__scalar)
162 BENCHMARK_CONV(f32_gemm_4x4__scalar)
163 
164 
165 #ifndef XNNPACK_BENCHMARK_NO_MAIN
166 BENCHMARK_MAIN();
167 #endif
168