Erich Elsen | 563df5f | 2019-10-23 08:02:21 -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> |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -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> |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -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> |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 23 | #include <xnnpack/params.h> |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 24 | |
| 25 | |
Marat Dukhan | 1f29b80 | 2020-05-15 23:46:39 -0700 | [diff] [blame] | 26 | static void DConvHWC2CHW3X3S2P1Benchmark(benchmark::State& state, |
| 27 | xnn_f32_conv_hwc2chw_ukernel_function conv, |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 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)); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -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) * |
| 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 */, |
Marat Dukhan | b42f866 | 2020-07-06 20:46:13 -0700 | [diff] [blame] | 71 | kernel.data(), bias.data(), packed_weights.data(), NULL); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -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()); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 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), |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 100 | ¶ms); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -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 | |
| 112 | #if XNN_ARCH_ARM64 |
Marat Dukhan | 1f29b80 | 2020-05-15 23:46:39 -0700 | [diff] [blame] | 113 | static void f32_conv_hwc2chw_3x3s2p1c3x4__neonfma_2x2(benchmark::State& state, const char* net) { |
| 114 | DConvHWC2CHW3X3S2P1Benchmark(state, xnn_f32_conv_hwc2chw_ukernel_3x3s2p1c3x4__neonfma_2x2, 4); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Marat Dukhan | 1f29b80 | 2020-05-15 23:46:39 -0700 | [diff] [blame] | 117 | BENCHMARK_DCONV(f32_conv_hwc2chw_3x3s2p1c3x4__neonfma_2x2); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 118 | #endif |
Erich Elsen | 0a1970e | 2020-06-10 09:24:59 -0700 | [diff] [blame] | 119 | |
Erich Elsen | b123340 | 2020-06-08 15:53:15 -0700 | [diff] [blame] | 120 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 121 | static void f32_conv_hwc2chw_3x3s2p1c3x4__sse_1x1(benchmark::State& state, const char* net) { |
| 122 | DConvHWC2CHW3X3S2P1Benchmark(state, xnn_f32_conv_hwc2chw_ukernel_3x3s2p1c3x4__sse_1x1, 4); |
| 123 | } |
| 124 | |
| 125 | BENCHMARK_DCONV(f32_conv_hwc2chw_3x3s2p1c3x4__sse_1x1); |
| 126 | |
| 127 | static void f32_conv_hwc2chw_3x3s2p1c3x4__sse_2x2(benchmark::State& state, const char* net) { |
| 128 | DConvHWC2CHW3X3S2P1Benchmark(state, xnn_f32_conv_hwc2chw_ukernel_3x3s2p1c3x4__sse_2x2, 4); |
| 129 | } |
| 130 | |
| 131 | BENCHMARK_DCONV(f32_conv_hwc2chw_3x3s2p1c3x4__sse_2x2); |
| 132 | #endif |
Erich Elsen | 0a1970e | 2020-06-10 09:24:59 -0700 | [diff] [blame] | 133 | |
Marat Dukhan | a199d49 | 2020-07-24 15:01:25 -0700 | [diff] [blame] | 134 | #if !XNN_ARCH_WASM && !XNN_COMPILER_MSVC && !XNN_COMPILER_ICC |
Erich Elsen | 0a1970e | 2020-06-10 09:24:59 -0700 | [diff] [blame] | 135 | static void f32_conv_hwc2chw_3x3s2p1c3x4__psimd_2x2(benchmark::State& state, const char* net) { |
| 136 | DConvHWC2CHW3X3S2P1Benchmark(state, xnn_f32_conv_hwc2chw_ukernel_3x3s2p1c3x4__psimd_2x2, 4); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Erich Elsen | 0a1970e | 2020-06-10 09:24:59 -0700 | [diff] [blame] | 139 | BENCHMARK_DCONV(f32_conv_hwc2chw_3x3s2p1c3x4__psimd_2x2); |
Marat Dukhan | a199d49 | 2020-07-24 15:01:25 -0700 | [diff] [blame] | 140 | #endif // !XNN_ARCH_WASM && !XNN_COMPILER_MSVC && !XNN_COMPILER_ICC |
Erich Elsen | 0a1970e | 2020-06-10 09:24:59 -0700 | [diff] [blame] | 141 | |
| 142 | static void f32_conv_hwc2chw_3x3s2p1c3x4__scalar_1x1(benchmark::State& state, const char* net) { |
| 143 | DConvHWC2CHW3X3S2P1Benchmark(state, xnn_f32_conv_hwc2chw_ukernel_3x3s2p1c3x4__scalar_1x1, 4); |
| 144 | } |
| 145 | |
| 146 | BENCHMARK_DCONV(f32_conv_hwc2chw_3x3s2p1c3x4__scalar_1x1); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 147 | |
| 148 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 149 | BENCHMARK_MAIN(); |
| 150 | #endif |