XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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> |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 19 | #include <xnnpack/common.h> |
Marat Dukhan | eeaa7bd | 2019-10-25 17:31:25 -0700 | [diff] [blame] | 20 | #include <xnnpack/conv.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 21 | #include <xnnpack/pack.h> |
Marat Dukhan | eeaa7bd | 2019-10-25 17:31:25 -0700 | [diff] [blame] | 22 | #include <xnnpack/params-init.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 23 | #include <xnnpack/params.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 24 | |
| 25 | |
| 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()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 40 | auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), std::ref(rng)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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) * |
Marat Dukhan | 4232323 | 2019-10-23 02:09:02 -0700 | [diff] [blame] | 60 | benchmark::utils::RoundUp<size_t>(output_channels, output_channels_tile); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 61 | const size_t output_elements = output_height * output_width * output_channels; |
| 62 | const size_t num_buffers = 1 + |
Marat Dukhan | 4232323 | 2019-10-23 02:09:02 -0700 | [diff] [blame] | 63 | benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(), |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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 */, |
Marat Dukhan | b42f866 | 2020-07-06 20:46:13 -0700 | [diff] [blame] | 71 | kernel.data(), bias.data(), packed_weights.data(), nullptr); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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 | |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 81 | xnn_f32_minmax_params params = |
Marat Dukhan | eb09a6b | 2020-04-08 17:34:32 -0700 | [diff] [blame] | 82 | xnn_init_f32_minmax_params(-std::numeric_limits<float>::infinity(), +std::numeric_limits<float>::infinity()); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 83 | |
| 84 | size_t buffer_index = 0; |
| 85 | for (auto _ : state) { |
| 86 | state.PauseTiming(); |
Marat Dukhan | 4232323 | 2019-10-23 02:09:02 -0700 | [diff] [blame] | 87 | benchmark::utils::PrefetchToL1(input.data(), input.size() * sizeof(float)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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), |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 100 | ¶ms); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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 | |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 112 | #if XNN_ARCH_ARM64 |
Marat Dukhan | 56b10cd | 2020-05-18 09:35:49 -0700 | [diff] [blame] | 113 | static void f32_conv_hwc_3x3s2p1c3x8__neonfma_2x1(benchmark::State& state, const char* net) { |
| 114 | DConv3X3S2P1Benchmark(state, xnn_f32_conv_hwc_ukernel_3x3s2p1c3x8__neonfma_2x1, 8); |
| 115 | } |
| 116 | static void f32_conv_hwc_3x3s2p1c3x4__neonfma_2x1(benchmark::State& state, const char* net) { |
| 117 | DConv3X3S2P1Benchmark(state, xnn_f32_conv_hwc_ukernel_3x3s2p1c3x4__neonfma_2x1, 4); |
| 118 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 119 | static void f32_conv_hwc_3x3s2p1c3x8__neonfma_2x2(benchmark::State& state, const char* net) { |
| 120 | DConv3X3S2P1Benchmark(state, xnn_f32_conv_hwc_ukernel_3x3s2p1c3x8__neonfma_2x2, 8); |
| 121 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 122 | static void f32_conv_hwc_3x3s2p1c3x4__neonfma_2x2(benchmark::State& state, const char* net) { |
| 123 | DConv3X3S2P1Benchmark(state, xnn_f32_conv_hwc_ukernel_3x3s2p1c3x4__neonfma_2x2, 4); |
| 124 | } |
| 125 | |
Marat Dukhan | 56b10cd | 2020-05-18 09:35:49 -0700 | [diff] [blame] | 126 | BENCHMARK_DCONV(f32_conv_hwc_3x3s2p1c3x8__neonfma_2x1); |
| 127 | BENCHMARK_DCONV(f32_conv_hwc_3x3s2p1c3x4__neonfma_2x1); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 128 | BENCHMARK_DCONV(f32_conv_hwc_3x3s2p1c3x8__neonfma_2x2); |
| 129 | BENCHMARK_DCONV(f32_conv_hwc_3x3s2p1c3x4__neonfma_2x2); |
Marat Dukhan | 56b10cd | 2020-05-18 09:35:49 -0700 | [diff] [blame] | 130 | #endif // XNN_ARCH_ARM64 |
| 131 | |
| 132 | #if XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 133 | static void f32_conv_hwc_3x3s2p1c3x8__neon_2x1(benchmark::State& state, const char* net) { |
| 134 | DConv3X3S2P1Benchmark(state, xnn_f32_conv_hwc_ukernel_3x3s2p1c3x8__neon_2x1, 8); |
| 135 | } |
| 136 | static void f32_conv_hwc_3x3s2p1c3x4__neon_2x1(benchmark::State& state, const char* net) { |
| 137 | DConv3X3S2P1Benchmark(state, xnn_f32_conv_hwc_ukernel_3x3s2p1c3x4__neon_2x1, 4); |
| 138 | } |
| 139 | static void f32_conv_hwc_3x3s2p1c3x8__neon_2x2(benchmark::State& state, const char* net) { |
| 140 | DConv3X3S2P1Benchmark(state, xnn_f32_conv_hwc_ukernel_3x3s2p1c3x8__neon_2x2, 8); |
| 141 | } |
| 142 | static void f32_conv_hwc_3x3s2p1c3x4__neon_2x2(benchmark::State& state, const char* net) { |
| 143 | DConv3X3S2P1Benchmark(state, xnn_f32_conv_hwc_ukernel_3x3s2p1c3x4__neon_2x2, 4); |
| 144 | } |
| 145 | |
| 146 | BENCHMARK_DCONV(f32_conv_hwc_3x3s2p1c3x8__neon_2x1); |
| 147 | BENCHMARK_DCONV(f32_conv_hwc_3x3s2p1c3x4__neon_2x1); |
| 148 | BENCHMARK_DCONV(f32_conv_hwc_3x3s2p1c3x8__neon_2x2); |
| 149 | BENCHMARK_DCONV(f32_conv_hwc_3x3s2p1c3x4__neon_2x2); |
| 150 | #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64 |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 151 | |
| 152 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 153 | BENCHMARK_MAIN(); |
| 154 | #endif |