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/dconv.h"
17 #include "bench/utils.h"
18 #include <xnnpack/AlignedAllocator.h>
19 #include <xnnpack/common.h>
20 #include <xnnpack/conv.h>
21 #include <xnnpack/pack.h>
22 #include <xnnpack/params-init.h>
23 #include <xnnpack/params.h>
24
25
DConv3X3S2P1Benchmark(benchmark::State & state,xnn_f32_conv_hwc_ukernel_function conv,uint32_t output_channels_tile)26 static void DConv3X3S2P1Benchmark(benchmark::State& state,
27 xnn_f32_conv_hwc_ukernel_function conv,
28 uint32_t output_channels_tile)
29 {
30 if (!cpuinfo_initialize()) {
31 state.SkipWithError("cpuinfo initialization failed");
32 }
33
34 const size_t input_height = state.range(0);
35 const size_t input_width = state.range(1);
36 const size_t output_channels = state.range(2);
37
38 std::random_device random_device;
39 auto rng = std::mt19937(random_device());
40 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng);
41
42 const size_t input_channels = 3;
43 const size_t kernel_size = 3;
44 const size_t padding = 1;
45 const size_t subsampling = 2;
46
47 const size_t output_height = (input_height + 2 * padding - kernel_size) / subsampling + 1;
48 const size_t output_width = (input_width + 2 * padding - kernel_size) / subsampling + 1;
49
50 std::vector<float> input(input_height * input_width * input_channels + XNN_EXTRA_BYTES / sizeof(float));
51 std::generate(input.begin(), input.end(), std::ref(f32rng));
52 std::vector<float> kernel(output_channels * kernel_size * kernel_size * input_channels);
53 std::generate(kernel.begin(), kernel.end(), std::ref(f32rng));
54 std::vector<float> bias(output_channels);
55 std::generate(bias.begin(), bias.end(), std::ref(f32rng));
56
57 std::vector<float, AlignedAllocator<float, 32>> zero(input_channels * input_width + XNN_EXTRA_BYTES / sizeof(float));
58
59 const size_t weights_elements = (kernel_size * kernel_size * input_channels + 1) *
60 benchmark::utils::RoundUp<size_t>(output_channels, output_channels_tile);
61 const size_t output_elements = output_height * output_width * output_channels;
62 const size_t num_buffers = 1 +
63 benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(),
64 sizeof(float) * (weights_elements + output_elements));
65
66 std::vector<float, AlignedAllocator<float, 32>> packed_weights(weights_elements * num_buffers);
67 std::fill(packed_weights.begin(), packed_weights.end(), 0.0f);
68 xnn_pack_f32_dconv_oki_w(
69 output_channels, input_channels, output_channels_tile,
70 kernel_size /* kernel height */, kernel_size /* kernel width */,
71 kernel.data(), bias.data(), packed_weights.data());
72 for (size_t n = 1; n < num_buffers; n++) {
73 std::copy(packed_weights.cbegin(),
74 packed_weights.cbegin() + weights_elements,
75 packed_weights.begin() + n * weights_elements);
76 }
77
78 std::vector<float> output(output_elements * num_buffers);
79 std::fill(output.begin(), output.end(), std::nanf(""));
80
81 xnn_f32_output_params output_params =
82 xnn_init_f32_output_params(-std::numeric_limits<float>::infinity(), +std::numeric_limits<float>::infinity());
83
84 size_t buffer_index = 0;
85 for (auto _ : state) {
86 state.PauseTiming();
87 benchmark::utils::PrefetchToL1(input.data(), input.size() * sizeof(float));
88 buffer_index = (buffer_index + 1) % num_buffers;
89 state.ResumeTiming();
90
91 conv(
92 input_height, input_width,
93 0 /* output_y_start */, output_height /* output_y_end */,
94 input.data(), zero.data(),
95 packed_weights.data() + buffer_index * weights_elements,
96 output.data() + buffer_index * output_elements,
97 padding, output_channels,
98 output_channels * output_width * sizeof(float),
99 output_channels * sizeof(float),
100 &output_params);
101 }
102
103 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
104 state.counters["FLOPS"] = benchmark::Counter(
105 uint64_t(state.iterations()) * 2 *
106 output_height * output_width *
107 input_channels * output_channels *
108 kernel_size * kernel_size,
109 benchmark::Counter::kIsRate);
110 }
111
112 #if XNN_ARCH_ARM64
f32_conv_hwc_3x3s2p1c3x8__neonfma_2x2(benchmark::State & state,const char * net)113 static void f32_conv_hwc_3x3s2p1c3x8__neonfma_2x2(benchmark::State& state, const char* net) {
114 DConv3X3S2P1Benchmark(state, xnn_f32_conv_hwc_ukernel_3x3s2p1c3x8__neonfma_2x2, 8);
115 }
116
f32_conv_hwc_3x3s2p1c3x4__neonfma_2x2(benchmark::State & state,const char * net)117 static void f32_conv_hwc_3x3s2p1c3x4__neonfma_2x2(benchmark::State& state, const char* net) {
118 DConv3X3S2P1Benchmark(state, xnn_f32_conv_hwc_ukernel_3x3s2p1c3x4__neonfma_2x2, 4);
119 }
120
121 BENCHMARK_DCONV(f32_conv_hwc_3x3s2p1c3x8__neonfma_2x2);
122 BENCHMARK_DCONV(f32_conv_hwc_3x3s2p1c3x4__neonfma_2x2);
123 #endif
124
125 #ifndef XNNPACK_BENCHMARK_NO_MAIN
126 BENCHMARK_MAIN();
127 #endif
128