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/conv.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> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 20 | #include <xnnpack/gemm.h> |
| 21 | #include <xnnpack/im2col.h> |
| 22 | #include <xnnpack/pack.h> |
Marat Dukhan | eeaa7bd | 2019-10-25 17:31:25 -0700 | [diff] [blame] | 23 | #include <xnnpack/params-init.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 24 | #include <xnnpack/params.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | static void Im2ColGEMMBenchmark(benchmark::State& state, |
Marat Dukhan | de06f49 | 2020-04-09 00:19:31 -0700 | [diff] [blame] | 28 | xnn_f32_gemm_minmax_ukernel_function f32_gemm, |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 50 | 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] | 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 | |
Marat Dukhan | 4232323 | 2019-10-23 02:09:02 -0700 | [diff] [blame] | 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); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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 + |
Marat Dukhan | 4232323 | 2019-10-23 02:09:02 -0700 | [diff] [blame] | 73 | benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(), |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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, |
Marat Dukhan | b42f866 | 2020-07-06 20:46:13 -0700 | [diff] [blame] | 79 | nr, kr, sr, k.data(), b.data(), w.data(), nullptr); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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 | |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 89 | xnn_f32_minmax_params params = |
Marat Dukhan | eb09a6b | 2020-04-08 17:34:32 -0700 | [diff] [blame] | 90 | 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] | 91 | |
| 92 | size_t buffer_index = 0; |
| 93 | for (auto _ : state) { |
| 94 | state.PauseTiming(); |
Marat Dukhan | 4232323 | 2019-10-23 02:09:02 -0700 | [diff] [blame] | 95 | benchmark::utils::PrefetchToL1(a.data(), a.size() * sizeof(float)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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); |
Frank Barchard | 95bebc9 | 2019-11-15 18:18:28 -0800 | [diff] [blame] | 117 | f32_gemm( |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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), |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 122 | ¶ms); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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 | |
Frank Barchard | 7e95597 | 2019-10-11 10:34:25 -0700 | [diff] [blame] | 137 | #if XNN_ARCH_ARM64 && XNN_ENABLE_ASSEMBLY |
Frank Barchard | 95bebc9 | 2019-11-15 18:18:28 -0800 | [diff] [blame] | 138 | static void f32_gemm_4x8__aarch64_neonfma_cortex_a75(benchmark::State& state, const char* net) { |
Marat Dukhan | de06f49 | 2020-04-09 00:19:31 -0700 | [diff] [blame] | 139 | Im2ColGEMMBenchmark(state, xnn_f32_gemm_minmax_ukernel_4x8__aarch64_neonfma_cortex_a75, 4, 8, 1, 1); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Frank Barchard | 95bebc9 | 2019-11-15 18:18:28 -0800 | [diff] [blame] | 142 | BENCHMARK_CONV(f32_gemm_4x8__aarch64_neonfma_cortex_a75) |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 143 | #endif // XNN_ARCH_ARM64 |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 144 | |
Frank Barchard | 95bebc9 | 2019-11-15 18:18:28 -0800 | [diff] [blame] | 145 | static void f32_gemm_2x4__scalar(benchmark::State& state, const char* net) { |
Marat Dukhan | de06f49 | 2020-04-09 00:19:31 -0700 | [diff] [blame] | 146 | Im2ColGEMMBenchmark(state, xnn_f32_gemm_minmax_ukernel_2x4__scalar, 2, 4, 1, 1); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Frank Barchard | 95bebc9 | 2019-11-15 18:18:28 -0800 | [diff] [blame] | 149 | static void f32_gemm_4x4__scalar(benchmark::State& state, const char* net) { |
Marat Dukhan | de06f49 | 2020-04-09 00:19:31 -0700 | [diff] [blame] | 150 | Im2ColGEMMBenchmark(state, xnn_f32_gemm_minmax_ukernel_4x4__scalar, 4, 4, 1, 1); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Frank Barchard | 95bebc9 | 2019-11-15 18:18:28 -0800 | [diff] [blame] | 153 | BENCHMARK_CONV(f32_gemm_2x4__scalar) |
| 154 | BENCHMARK_CONV(f32_gemm_4x4__scalar) |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 155 | |
| 156 | |
| 157 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 158 | BENCHMARK_MAIN(); |
| 159 | #endif |