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, |
Marat Dukhan | 4e89587 | 2020-12-04 15:27:45 -0800 | [diff] [blame] | 28 | uint32_t output_channels_tile, |
| 29 | benchmark::utils::IsaCheckFunction isa_check = nullptr) |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 30 | { |
Marat Dukhan | 4e89587 | 2020-12-04 15:27:45 -0800 | [diff] [blame] | 31 | if (isa_check && !isa_check(state)) { |
| 32 | return; |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | const size_t input_height = state.range(0); |
| 36 | const size_t input_width = state.range(1); |
| 37 | const size_t output_channels = state.range(2); |
| 38 | |
| 39 | std::random_device random_device; |
| 40 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 41 | 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] | 42 | |
| 43 | const size_t input_channels = 3; |
| 44 | const size_t kernel_size = 3; |
| 45 | const size_t padding = 1; |
| 46 | const size_t subsampling = 2; |
| 47 | |
| 48 | const size_t output_height = (input_height + 2 * padding - kernel_size) / subsampling + 1; |
| 49 | const size_t output_width = (input_width + 2 * padding - kernel_size) / subsampling + 1; |
| 50 | |
| 51 | std::vector<float> input(input_height * input_width * input_channels + XNN_EXTRA_BYTES / sizeof(float)); |
| 52 | std::generate(input.begin(), input.end(), std::ref(f32rng)); |
| 53 | std::vector<float> kernel(output_channels * kernel_size * kernel_size * input_channels); |
| 54 | std::generate(kernel.begin(), kernel.end(), std::ref(f32rng)); |
| 55 | std::vector<float> bias(output_channels); |
| 56 | std::generate(bias.begin(), bias.end(), std::ref(f32rng)); |
| 57 | |
| 58 | std::vector<float, AlignedAllocator<float, 32>> zero(input_channels * input_width + XNN_EXTRA_BYTES / sizeof(float)); |
| 59 | |
| 60 | const size_t weights_elements = (kernel_size * kernel_size * input_channels + 1) * |
| 61 | benchmark::utils::RoundUp<size_t>(output_channels, output_channels_tile); |
| 62 | const size_t output_elements = output_height * output_width * output_channels; |
| 63 | const size_t num_buffers = 1 + |
| 64 | benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(), |
| 65 | sizeof(float) * (weights_elements + output_elements)); |
| 66 | |
| 67 | std::vector<float, AlignedAllocator<float, 32>> packed_weights(weights_elements * num_buffers); |
| 68 | std::fill(packed_weights.begin(), packed_weights.end(), 0.0f); |
| 69 | xnn_pack_f32_dconv_oki_w( |
| 70 | output_channels, input_channels, output_channels_tile, |
| 71 | kernel_size /* kernel height */, kernel_size /* kernel width */, |
Marat Dukhan | b42f866 | 2020-07-06 20:46:13 -0700 | [diff] [blame] | 72 | kernel.data(), bias.data(), packed_weights.data(), NULL); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 73 | for (size_t n = 1; n < num_buffers; n++) { |
| 74 | std::copy(packed_weights.cbegin(), |
| 75 | packed_weights.cbegin() + weights_elements, |
| 76 | packed_weights.begin() + n * weights_elements); |
| 77 | } |
| 78 | |
| 79 | std::vector<float> output(output_elements * num_buffers); |
| 80 | std::fill(output.begin(), output.end(), std::nanf("")); |
| 81 | |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 82 | xnn_f32_minmax_params params = |
Marat Dukhan | eb09a6b | 2020-04-08 17:34:32 -0700 | [diff] [blame] | 83 | 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] | 84 | |
| 85 | size_t buffer_index = 0; |
| 86 | for (auto _ : state) { |
| 87 | state.PauseTiming(); |
| 88 | benchmark::utils::PrefetchToL1(input.data(), input.size() * sizeof(float)); |
| 89 | buffer_index = (buffer_index + 1) % num_buffers; |
| 90 | state.ResumeTiming(); |
| 91 | |
| 92 | conv( |
| 93 | input_height, input_width, |
| 94 | 0 /* output_y_start */, output_height /* output_y_end */, |
| 95 | input.data(), zero.data(), |
| 96 | packed_weights.data() + buffer_index * weights_elements, |
| 97 | output.data() + buffer_index * output_elements, |
| 98 | padding, output_channels, |
| 99 | output_channels * output_width * sizeof(float), |
| 100 | output_channels * sizeof(float), |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 101 | ¶ms); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 104 | const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 105 | if (cpu_frequency != 0) { |
| 106 | state.counters["cpufreq"] = cpu_frequency; |
| 107 | } |
| 108 | |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 109 | state.counters["FLOPS"] = benchmark::Counter( |
| 110 | uint64_t(state.iterations()) * 2 * |
| 111 | output_height * output_width * |
| 112 | input_channels * output_channels * |
| 113 | kernel_size * kernel_size, |
| 114 | benchmark::Counter::kIsRate); |
| 115 | } |
| 116 | |
| 117 | #if XNN_ARCH_ARM64 |
Marat Dukhan | 1f29b80 | 2020-05-15 23:46:39 -0700 | [diff] [blame] | 118 | static void f32_conv_hwc2chw_3x3s2p1c3x4__neonfma_2x2(benchmark::State& state, const char* net) { |
Marat Dukhan | 4e89587 | 2020-12-04 15:27:45 -0800 | [diff] [blame] | 119 | DConvHWC2CHW3X3S2P1Benchmark(state, xnn_f32_conv_hwc2chw_ukernel_3x3s2p1c3x4__neonfma_2x2, 4, benchmark::utils::CheckNEONFMA); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Marat Dukhan | 1f29b80 | 2020-05-15 23:46:39 -0700 | [diff] [blame] | 122 | BENCHMARK_DCONV(f32_conv_hwc2chw_3x3s2p1c3x4__neonfma_2x2); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 123 | #endif |
Erich Elsen | 0a1970e | 2020-06-10 09:24:59 -0700 | [diff] [blame] | 124 | |
Erich Elsen | b123340 | 2020-06-08 15:53:15 -0700 | [diff] [blame] | 125 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 126 | static void f32_conv_hwc2chw_3x3s2p1c3x4__sse_1x1(benchmark::State& state, const char* net) { |
| 127 | DConvHWC2CHW3X3S2P1Benchmark(state, xnn_f32_conv_hwc2chw_ukernel_3x3s2p1c3x4__sse_1x1, 4); |
| 128 | } |
| 129 | |
| 130 | BENCHMARK_DCONV(f32_conv_hwc2chw_3x3s2p1c3x4__sse_1x1); |
| 131 | |
| 132 | static void f32_conv_hwc2chw_3x3s2p1c3x4__sse_2x2(benchmark::State& state, const char* net) { |
| 133 | DConvHWC2CHW3X3S2P1Benchmark(state, xnn_f32_conv_hwc2chw_ukernel_3x3s2p1c3x4__sse_2x2, 4); |
| 134 | } |
| 135 | |
| 136 | BENCHMARK_DCONV(f32_conv_hwc2chw_3x3s2p1c3x4__sse_2x2); |
| 137 | #endif |
Erich Elsen | 0a1970e | 2020-06-10 09:24:59 -0700 | [diff] [blame] | 138 | |
Frank Barchard | 2213606 | 2020-11-24 18:44:46 -0800 | [diff] [blame] | 139 | #if XNN_ARCH_WASMSIMD |
| 140 | static void f32_conv_hwc2chw_3x3s2p1c3x4__wasmsimd_2x2(benchmark::State& state, const char* net) { |
| 141 | DConvHWC2CHW3X3S2P1Benchmark(state, xnn_f32_conv_hwc2chw_ukernel_3x3s2p1c3x4__wasmsimd_2x2, 4); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Frank Barchard | 2213606 | 2020-11-24 18:44:46 -0800 | [diff] [blame] | 144 | BENCHMARK_DCONV(f32_conv_hwc2chw_3x3s2p1c3x4__wasmsimd_2x2); |
| 145 | #endif // XNN_ARCH_WASMSIMD |
Erich Elsen | 0a1970e | 2020-06-10 09:24:59 -0700 | [diff] [blame] | 146 | |
| 147 | static void f32_conv_hwc2chw_3x3s2p1c3x4__scalar_1x1(benchmark::State& state, const char* net) { |
| 148 | DConvHWC2CHW3X3S2P1Benchmark(state, xnn_f32_conv_hwc2chw_ukernel_3x3s2p1c3x4__scalar_1x1, 4); |
| 149 | } |
| 150 | |
| 151 | BENCHMARK_DCONV(f32_conv_hwc2chw_3x3s2p1c3x4__scalar_1x1); |
Erich Elsen | 563df5f | 2019-10-23 08:02:21 -0700 | [diff] [blame] | 152 | |
| 153 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 154 | BENCHMARK_MAIN(); |
| 155 | #endif |