Frank Barchard | 40d20fe | 2020-05-05 00:37:45 -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> |
Frank Barchard | 9c1a735 | 2020-06-04 20:15:01 -0700 | [diff] [blame] | 16 | #include <fp16/fp16.h> |
Frank Barchard | 40d20fe | 2020-05-05 00:37:45 -0700 | [diff] [blame] | 17 | #include "bench/conv.h" |
| 18 | #include "bench/utils.h" |
| 19 | #include <xnnpack/AlignedAllocator.h> |
| 20 | #include <xnnpack/common.h> |
| 21 | #include <xnnpack/igemm.h> |
| 22 | #include <xnnpack/indirection.h> |
| 23 | #include <xnnpack/operator.h> |
| 24 | #include <xnnpack/pack.h> |
| 25 | #include <xnnpack/params-init.h> |
| 26 | #include <xnnpack/params.h> |
| 27 | |
| 28 | |
| 29 | static void IGEMMBenchmark(benchmark::State& state, |
| 30 | xnn_f16_igemm_minmax_ukernel_function f16_igemm, |
Frank Barchard | 40f50e1 | 2020-05-29 22:21:56 -0700 | [diff] [blame] | 31 | uint32_t mr, uint32_t nr, uint32_t kr, uint32_t sr) |
Frank Barchard | 40d20fe | 2020-05-05 00:37:45 -0700 | [diff] [blame] | 32 | { |
| 33 | if (!cpuinfo_initialize()) { |
| 34 | state.SkipWithError("cpuinfo initialization failed"); |
| 35 | } |
Frank Barchard | 40f50e1 | 2020-05-29 22:21:56 -0700 | [diff] [blame] | 36 | if (!benchmark::utils::CheckNEONFP16ARITH(state)) { |
Frank Barchard | 40d20fe | 2020-05-05 00:37:45 -0700 | [diff] [blame] | 37 | return; |
| 38 | } |
| 39 | |
| 40 | const size_t input_height = state.range(0); |
| 41 | const size_t input_width = state.range(1); |
| 42 | const size_t kernel_height = state.range(2); |
| 43 | const size_t kernel_width = state.range(3); |
| 44 | const size_t kernel_size = kernel_height * kernel_width; |
| 45 | const size_t padding_height = state.range(4); |
| 46 | const size_t padding_width = state.range(5); |
| 47 | const size_t subsampling = state.range(6); |
| 48 | const size_t dilation = state.range(7); |
| 49 | const size_t group_input_channels = state.range(8); |
| 50 | const size_t group_output_channels = state.range(9); |
| 51 | |
| 52 | std::random_device random_device; |
| 53 | auto rng = std::mt19937(random_device()); |
| 54 | auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng); |
| 55 | auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng); |
| 56 | |
| 57 | const size_t output_pixel_stride = group_output_channels; |
| 58 | const size_t input_pixel_stride = group_input_channels; |
| 59 | const size_t effective_kernel_height = (kernel_height - 1) * dilation + 1; |
| 60 | const size_t effective_kernel_width = (kernel_width - 1) * dilation + 1; |
| 61 | const size_t padding_left = padding_width / 2; |
| 62 | const size_t padding_top = padding_height / 2; |
| 63 | const size_t output_height = (input_height + padding_height - effective_kernel_height) / subsampling + 1; |
| 64 | const size_t output_width = (input_width + padding_width - effective_kernel_width) / subsampling + 1; |
| 65 | const size_t output_size = output_height * output_width; |
| 66 | |
| 67 | const size_t mc_stride = benchmark::utils::RoundUp<size_t>(output_size, mr); |
| 68 | const size_t nc_stride = benchmark::utils::RoundUp<size_t>(group_output_channels, nr); |
| 69 | const size_t kc_stride = benchmark::utils::RoundUp<size_t>(group_input_channels, kr); |
| 70 | |
| 71 | std::vector<uint16_t> a(input_height * input_width * input_pixel_stride); |
| 72 | std::generate(a.begin(), a.end(), std::ref(f16rng)); |
| 73 | std::vector<uint16_t> k(group_output_channels * kernel_height * kernel_width * group_input_channels); |
| 74 | std::generate(k.begin(), k.end(), std::ref(f16rng)); |
| 75 | std::vector<uint16_t> b(group_output_channels); |
| 76 | std::generate(b.begin(), b.end(), std::ref(f16rng)); |
| 77 | |
| 78 | std::vector<uint16_t> z(group_input_channels); |
| 79 | |
| 80 | const size_t w_elements = (kernel_size * kc_stride + 1) * nc_stride; |
| 81 | const size_t i_elements = mc_stride * kernel_size; |
| 82 | const size_t c_elements = output_height * output_width * output_pixel_stride; |
| 83 | const size_t num_buffers = 1 + |
| 84 | benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(), |
| 85 | sizeof(uint16_t) * (w_elements + c_elements) + sizeof(void*) * i_elements); |
| 86 | |
| 87 | std::vector<uint16_t, AlignedAllocator<uint16_t, 32>> w(w_elements * num_buffers); |
| 88 | std::fill(w.begin(), w.end(), 0); |
| 89 | xnn_pack_f16_conv_goki_w( |
| 90 | 1 /* groups */, group_output_channels, kernel_size, group_input_channels, |
Marat Dukhan | b42f866 | 2020-07-06 20:46:13 -0700 | [diff] [blame] | 91 | nr, kr, sr, k.data(), b.data(), w.data(), nullptr); |
Frank Barchard | 40d20fe | 2020-05-05 00:37:45 -0700 | [diff] [blame] | 92 | for (size_t n = 1; n < num_buffers; n++) { |
| 93 | std::copy(w.cbegin(), w.cbegin() + w_elements, w.begin() + n * w_elements); |
| 94 | } |
| 95 | |
| 96 | std::vector<const uint16_t*> i(i_elements * num_buffers); |
| 97 | xnn_operator convolution_op = { }; |
| 98 | convolution_op.indirection_buffer = reinterpret_cast<const void**>(i.data()); |
| 99 | convolution_op.input = a.data(); |
| 100 | convolution_op.input_pixel_stride = input_pixel_stride; |
| 101 | convolution_op.zero_buffer = z.data(); |
| 102 | convolution_op.groups = 1; |
| 103 | convolution_op.group_input_channels = group_input_channels; |
| 104 | convolution_op.batch_size = 1; |
| 105 | convolution_op.input_height = input_height; |
| 106 | convolution_op.input_width = input_width; |
| 107 | convolution_op.output_height = output_height; |
| 108 | convolution_op.output_width = output_width; |
| 109 | convolution_op.kernel_height = kernel_height; |
| 110 | convolution_op.kernel_width = kernel_width; |
| 111 | convolution_op.stride_height = subsampling; |
| 112 | convolution_op.stride_width = subsampling; |
| 113 | convolution_op.dilation_height = dilation; |
| 114 | convolution_op.dilation_width = dilation; |
| 115 | convolution_op.padding_top = padding_top; |
| 116 | convolution_op.padding_left = padding_left; |
Frank Barchard | d960714 | 2020-06-03 10:02:34 -0700 | [diff] [blame] | 117 | xnn_indirection_init_conv2d(&convolution_op, mr, 1 /* log2(sizeof(uint16_t)) */); |
Frank Barchard | 40d20fe | 2020-05-05 00:37:45 -0700 | [diff] [blame] | 118 | for (size_t n = 1; n < num_buffers; n++) { |
| 119 | std::copy(i.cbegin(), i.cbegin() + i_elements, i.begin() + n * i_elements); |
| 120 | } |
| 121 | |
| 122 | std::vector<uint16_t> c(c_elements * num_buffers); |
| 123 | std::fill(c.begin(), c.end(), std::nanf("")); |
| 124 | |
| 125 | // Prepare minmax parameters. |
| 126 | xnn_f16_scaleminmax_params params; |
| 127 | params = xnn_init_f16_scaleminmax_params( |
| 128 | UINT16_C(0x3C00), /* 1.0 */ |
| 129 | UINT16_C(0x7C00), /* inf */ |
| 130 | UINT16_C(0xFC00)); /* -inf */ |
| 131 | |
| 132 | size_t buffer_index = 0; |
| 133 | for (auto _ : state) { |
| 134 | state.PauseTiming(); |
| 135 | benchmark::utils::PrefetchToL1(a.data(), a.size() * sizeof(uint16_t)); |
| 136 | buffer_index = (buffer_index + 1) % num_buffers; |
| 137 | state.ResumeTiming(); |
| 138 | |
| 139 | for (uint32_t m = 0; m < output_size; m += mr) { |
| 140 | const uint32_t mb = min(output_size - m, mr); |
| 141 | for (uint32_t n = 0; n < group_output_channels; n += nr) { |
| 142 | const uint32_t nb = min(group_output_channels - n, nr); |
| 143 | f16_igemm( |
| 144 | mb, nb, group_input_channels * sizeof(uint16_t), kernel_size * mr * sizeof(void*), |
| 145 | reinterpret_cast<const void**>(i.data()) + buffer_index * i_elements + m, |
| 146 | w.data() + buffer_index * w_elements + n * (kc_stride * kernel_size + 1), |
| 147 | c.data() + buffer_index * c_elements + m * group_output_channels + n, group_output_channels * sizeof(uint16_t), nr * sizeof(uint16_t), |
| 148 | 0, z.data(), ¶ms); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency(); |
| 154 | state.counters["FLOPS"] = benchmark::Counter( |
| 155 | uint64_t(state.iterations()) * 2 * |
| 156 | output_height * output_width * |
| 157 | group_input_channels * group_output_channels * |
| 158 | kernel_height * kernel_width, |
| 159 | benchmark::Counter::kIsRate); |
| 160 | } |
| 161 | |
| 162 | #if XNN_ARCH_ARM64 |
| 163 | static void f16_igemm_1x8__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 164 | IGEMMBenchmark(state, xnn_f16_igemm_minmax_ukernel_1x8__neonfp16arith_ld64, 1, 8, 1, 1); |
| 165 | } |
| 166 | |
| 167 | static void f16_igemm_4x8__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 168 | IGEMMBenchmark(state, xnn_f16_igemm_minmax_ukernel_4x8__neonfp16arith_ld64, 4, 8, 1, 1); |
| 169 | } |
| 170 | |
| 171 | static void f16_igemm_6x8__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 172 | IGEMMBenchmark(state, xnn_f16_igemm_minmax_ukernel_6x8__neonfp16arith_ld64, 6, 8, 1, 1); |
| 173 | } |
| 174 | |
| 175 | static void f16_igemm_8x8__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 176 | IGEMMBenchmark(state, xnn_f16_igemm_minmax_ukernel_8x8__neonfp16arith_ld64, 8, 8, 1, 1); |
| 177 | } |
| 178 | |
Frank Barchard | 3f9f99f | 2020-05-06 01:12:04 -0700 | [diff] [blame] | 179 | static void f16_igemm_1x16__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 180 | IGEMMBenchmark(state, xnn_f16_igemm_minmax_ukernel_1x16__neonfp16arith_ld64, 1, 16, 1, 1); |
| 181 | } |
| 182 | |
| 183 | static void f16_igemm_4x16__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 184 | IGEMMBenchmark(state, xnn_f16_igemm_minmax_ukernel_4x16__neonfp16arith_ld64, 4, 16, 1, 1); |
| 185 | } |
| 186 | |
| 187 | static void f16_igemm_6x16__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 188 | IGEMMBenchmark(state, xnn_f16_igemm_minmax_ukernel_6x16__neonfp16arith_ld64, 6, 16, 1, 1); |
| 189 | } |
| 190 | |
| 191 | static void f16_igemm_8x16__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 192 | IGEMMBenchmark(state, xnn_f16_igemm_minmax_ukernel_8x16__neonfp16arith_ld64, 8, 16, 1, 1); |
| 193 | } |
| 194 | |
Frank Barchard | 40d20fe | 2020-05-05 00:37:45 -0700 | [diff] [blame] | 195 | BENCHMARK_CONV(f16_igemm_1x8__neonfp16arith_ld64) |
| 196 | BENCHMARK_CONV(f16_igemm_4x8__neonfp16arith_ld64) |
| 197 | BENCHMARK_CONV(f16_igemm_6x8__neonfp16arith_ld64) |
| 198 | BENCHMARK_CONV(f16_igemm_8x8__neonfp16arith_ld64) |
Frank Barchard | 3f9f99f | 2020-05-06 01:12:04 -0700 | [diff] [blame] | 199 | |
| 200 | BENCHMARK_CONV(f16_igemm_1x16__neonfp16arith_ld64) |
| 201 | BENCHMARK_CONV(f16_igemm_4x16__neonfp16arith_ld64) |
| 202 | BENCHMARK_CONV(f16_igemm_6x16__neonfp16arith_ld64) |
| 203 | BENCHMARK_CONV(f16_igemm_8x16__neonfp16arith_ld64) |
Frank Barchard | 40d20fe | 2020-05-05 00:37:45 -0700 | [diff] [blame] | 204 | #endif /* XNN_ARCH_ARM64 */ |
| 205 | |
| 206 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 207 | BENCHMARK_MAIN(); |
| 208 | #endif |