XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 1 | // Copyright (c) Facebook, Inc. and its affiliates. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Copyright 2019 Google LLC |
| 5 | // |
| 6 | // This source code is licensed under the BSD-style license found in the |
| 7 | // LICENSE file in the root directory of this source tree. |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <cfloat> |
| 11 | #include <cmath> |
| 12 | #include <functional> |
| 13 | #include <random> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include <cpuinfo.h> |
| 17 | |
Frank Barchard | bb4c18b | 2019-09-30 11:05:52 -0700 | [diff] [blame] | 18 | #include <benchmark/benchmark.h> |
Marat Dukhan | 629a33e | 2019-10-01 10:39:14 -0700 | [diff] [blame] | 19 | #include <fp16/fp16.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 20 | #include "bench/gemm.h" |
Frank Barchard | bb4c18b | 2019-09-30 11:05:52 -0700 | [diff] [blame] | 21 | #include "bench/utils.h" |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 22 | #include <xnnpack/AlignedAllocator.h> |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 23 | #include <xnnpack/common.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 24 | #include <xnnpack/gemm.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 25 | #include <xnnpack/pack.h> |
Marat Dukhan | eeaa7bd | 2019-10-25 17:31:25 -0700 | [diff] [blame] | 26 | #include <xnnpack/params-init.h> |
Frank Barchard | e0601b5 | 2019-10-25 17:43:34 -0700 | [diff] [blame] | 27 | #include <xnnpack/params.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 28 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 29 | |
| 30 | static void GEMMBenchmark(benchmark::State& state, |
Frank Barchard | e92f859 | 2020-05-04 10:08:44 -0700 | [diff] [blame] | 31 | xnn_f16_gemm_minmax_ukernel_function gemm, |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 32 | size_t mr, size_t nr, size_t kr, size_t sr) |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 33 | { |
| 34 | if (!cpuinfo_initialize()) { |
| 35 | state.SkipWithError("cpuinfo initialization failed"); |
| 36 | return; |
| 37 | } |
Frank Barchard | 40f50e1 | 2020-05-29 22:21:56 -0700 | [diff] [blame] | 38 | if (!benchmark::utils::CheckNEONFP16ARITH(state)) { |
| 39 | return; |
| 40 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 41 | |
| 42 | const size_t mc = state.range(0); |
| 43 | const size_t nc = state.range(1); |
| 44 | const size_t kc = state.range(2); |
| 45 | |
Marat Dukhan | 4232323 | 2019-10-23 02:09:02 -0700 | [diff] [blame] | 46 | const size_t nc_stride = benchmark::utils::RoundUp(nc, nr); |
| 47 | const size_t kc_stride = benchmark::utils::RoundUp(kc, kr); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 48 | |
| 49 | std::random_device random_device; |
| 50 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 51 | auto f32rng = std::bind(std::uniform_real_distribution<float>(), std::ref(rng)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 52 | auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng); |
| 53 | |
| 54 | std::vector<uint16_t> a(mc * kc); |
| 55 | std::generate(a.begin(), a.end(), std::ref(f16rng)); |
| 56 | std::vector<uint16_t> k(nc * kc); |
| 57 | std::generate(k.begin(), k.end(), std::ref(f16rng)); |
| 58 | std::vector<uint16_t> b(nc); |
| 59 | std::generate(b.begin(), b.end(), std::ref(f16rng)); |
| 60 | |
| 61 | const size_t w_elements = nc_stride * kc_stride + nc_stride; |
| 62 | const size_t c_elements = mc * nc; |
| 63 | const size_t num_buffers = 1 + |
Marat Dukhan | 4232323 | 2019-10-23 02:09:02 -0700 | [diff] [blame] | 64 | benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(), |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 65 | sizeof(uint16_t) * (w_elements + c_elements)); |
| 66 | |
| 67 | std::vector<uint16_t, AlignedAllocator<uint16_t, 32>> w(w_elements * num_buffers); |
| 68 | std::fill(w.begin(), w.end(), 0); |
Marat Dukhan | b42f866 | 2020-07-06 20:46:13 -0700 | [diff] [blame] | 69 | xnn_pack_f16_gemm_goi_w(1 /* groups */, nc, kc, nr, kr, sr, k.data(), b.data(), w.data(), nullptr); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 70 | std::vector<uint16_t> c(c_elements * num_buffers); |
| 71 | std::fill(c.begin(), c.end(), UINT16_C(0x7E00) /* NaN */); |
| 72 | |
Frank Barchard | 40d20fe | 2020-05-05 00:37:45 -0700 | [diff] [blame] | 73 | // Prepare minmax parameters. |
| 74 | xnn_f16_scaleminmax_params params; |
| 75 | params = xnn_init_f16_scaleminmax_params( |
Marat Dukhan | ef4ce31 | 2020-09-10 12:29:08 -0700 | [diff] [blame] | 76 | UINT16_C(0x3C00) /* 1.0 */, UINT16_C(0x7C00) /* inf */, UINT16_C(0xFC00) /* -inf */); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 77 | |
| 78 | size_t buffer_index = 0; |
| 79 | for (auto _ : state) { |
| 80 | // Use circular buffers (exceeding cache size) and prefetch to control cache state: |
| 81 | // - A is always in L1 cache (if fits, otherwise L2, L3, etc) |
| 82 | // - W is not in cache (for any cache level) |
| 83 | // - C is not in cache (for any cache level) |
| 84 | state.PauseTiming(); |
Marat Dukhan | 4232323 | 2019-10-23 02:09:02 -0700 | [diff] [blame] | 85 | benchmark::utils::PrefetchToL1(a.data(), a.size() * sizeof(uint16_t)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 86 | buffer_index = (buffer_index + 1) % num_buffers; |
| 87 | state.ResumeTiming(); |
| 88 | |
| 89 | for (uint32_t m = 0; m < mc; m += mr) { |
| 90 | const uint32_t mb = min(mc - m, mr); |
| 91 | for (uint32_t n = 0; n < nc; n += nr) { |
| 92 | const uint32_t nb = min(nc - n, nr); |
Frank Barchard | e92f859 | 2020-05-04 10:08:44 -0700 | [diff] [blame] | 93 | gemm( |
Marat Dukhan | b186463 | 2019-11-25 16:34:17 -0800 | [diff] [blame] | 94 | mb, nb, kc * sizeof(uint16_t), |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 95 | a.data() + m * kc, kc * sizeof(uint16_t), |
| 96 | w.data() + (nc_stride * buffer_index + n) * (kc_stride + 1), |
| 97 | c.data() + (mc * buffer_index + m) * nc + n, nc * sizeof(uint16_t), nr * sizeof(uint16_t), |
Frank Barchard | 77acbf2 | 2020-05-01 10:08:26 -0700 | [diff] [blame] | 98 | ¶ms); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 103 | const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 104 | if (cpu_frequency != 0) { |
| 105 | state.counters["cpufreq"] = cpu_frequency; |
| 106 | } |
| 107 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 108 | state.counters["FLOPS"] = benchmark::Counter( |
| 109 | uint64_t(state.iterations()) * 2 * mc * nc * kc, benchmark::Counter::kIsRate); |
| 110 | } |
| 111 | |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 112 | #if XNN_ARCH_ARM64 |
Frank Barchard | 1f4e461 | 2020-04-13 18:24:54 -0700 | [diff] [blame] | 113 | static void f16_gemm_1x8__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 114 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_1x8__neonfp16arith_ld64, 1, 8, 1, 1); |
Frank Barchard | 1f4e461 | 2020-04-13 18:24:54 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Frank Barchard | 54a9d9d | 2019-11-25 16:46:38 -0800 | [diff] [blame] | 117 | static void f16_gemm_4x8__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 118 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_4x8__neonfp16arith_ld64, 4, 8, 1, 1); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Frank Barchard | 54a9d9d | 2019-11-25 16:46:38 -0800 | [diff] [blame] | 121 | static void f16_gemm_6x8__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 122 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_6x8__neonfp16arith_ld64, 6, 8, 1, 1); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Frank Barchard | 54a9d9d | 2019-11-25 16:46:38 -0800 | [diff] [blame] | 125 | static void f16_gemm_8x8__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 126 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_8x8__neonfp16arith_ld64, 8, 8, 1, 1); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Frank Barchard | 3f9f99f | 2020-05-06 01:12:04 -0700 | [diff] [blame] | 129 | static void f16_gemm_1x16__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 130 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_1x16__neonfp16arith_ld64, 1, 16, 1, 1); |
| 131 | } |
| 132 | |
| 133 | static void f16_gemm_4x16__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 134 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_4x16__neonfp16arith_ld64, 4, 16, 1, 1); |
| 135 | } |
| 136 | |
| 137 | static void f16_gemm_6x16__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 138 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_6x16__neonfp16arith_ld64, 6, 16, 1, 1); |
| 139 | } |
| 140 | |
| 141 | static void f16_gemm_8x16__neonfp16arith_ld64(benchmark::State& state, const char* net) { |
| 142 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_8x16__neonfp16arith_ld64, 8, 16, 1, 1); |
| 143 | } |
| 144 | |
Frank Barchard | 1f4e461 | 2020-04-13 18:24:54 -0700 | [diff] [blame] | 145 | BENCHMARK_GEMM(f16_gemm_1x8__neonfp16arith_ld64) |
Frank Barchard | 54a9d9d | 2019-11-25 16:46:38 -0800 | [diff] [blame] | 146 | BENCHMARK_GEMM(f16_gemm_4x8__neonfp16arith_ld64) |
| 147 | BENCHMARK_GEMM(f16_gemm_6x8__neonfp16arith_ld64) |
| 148 | BENCHMARK_GEMM(f16_gemm_8x8__neonfp16arith_ld64) |
Frank Barchard | 3f9f99f | 2020-05-06 01:12:04 -0700 | [diff] [blame] | 149 | BENCHMARK_GEMM(f16_gemm_1x16__neonfp16arith_ld64) |
| 150 | BENCHMARK_GEMM(f16_gemm_4x16__neonfp16arith_ld64) |
| 151 | BENCHMARK_GEMM(f16_gemm_6x16__neonfp16arith_ld64) |
| 152 | BENCHMARK_GEMM(f16_gemm_8x16__neonfp16arith_ld64) |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 153 | #endif |
| 154 | |
Frank Barchard | 683f559 | 2020-04-10 00:48:26 -0700 | [diff] [blame] | 155 | #if XNN_ARCH_ARM64 && XNN_ENABLE_ASSEMBLY |
Frank Barchard | 36b76b6 | 2020-04-10 12:39:17 -0700 | [diff] [blame] | 156 | static void f16_gemm_1x16__aarch64_neonfp16arith_ld32(benchmark::State& state, const char* net) { |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 157 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_1x16__aarch64_neonfp16arith_ld32, 1, 16, 1, 1); |
Frank Barchard | 36b76b6 | 2020-04-10 12:39:17 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Frank Barchard | 683f559 | 2020-04-10 00:48:26 -0700 | [diff] [blame] | 160 | static void f16_gemm_4x16__aarch64_neonfp16arith_ld32(benchmark::State& state, const char* net) { |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 161 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_4x16__aarch64_neonfp16arith_ld32, 4, 16, 1, 1); |
Frank Barchard | 683f559 | 2020-04-10 00:48:26 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static void f16_gemm_6x16__aarch64_neonfp16arith_ld32(benchmark::State& state, const char* net) { |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 165 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_6x16__aarch64_neonfp16arith_ld32, 6, 16, 1, 1); |
Frank Barchard | 683f559 | 2020-04-10 00:48:26 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Frank Barchard | bddfbcd | 2020-04-15 12:32:41 -0700 | [diff] [blame] | 168 | static void f16_gemm_1x8__aarch64_neonfp16arith_ld64(benchmark::State& state, const char* net) { |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 169 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_1x8__aarch64_neonfp16arith_ld64, 1, 8, 1, 1); |
Frank Barchard | bddfbcd | 2020-04-15 12:32:41 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void f16_gemm_4x8__aarch64_neonfp16arith_ld64(benchmark::State& state, const char* net) { |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 173 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_4x8__aarch64_neonfp16arith_ld64, 4, 8, 1, 1); |
Frank Barchard | bddfbcd | 2020-04-15 12:32:41 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static void f16_gemm_6x8__aarch64_neonfp16arith_ld64(benchmark::State& state, const char* net) { |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 177 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_6x8__aarch64_neonfp16arith_ld64, 6, 8, 1, 1); |
Frank Barchard | bddfbcd | 2020-04-15 12:32:41 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Frank Barchard | 3b8e566 | 2020-04-20 12:12:53 -0700 | [diff] [blame] | 180 | static void f16_gemm_8x8__aarch64_neonfp16arith_ld64(benchmark::State& state, const char* net) { |
Frank Barchard | 142268b | 2020-04-29 16:37:18 -0700 | [diff] [blame] | 181 | GEMMBenchmark(state, xnn_f16_gemm_minmax_ukernel_8x8__aarch64_neonfp16arith_ld64, 8, 8, 1, 1); |
Frank Barchard | 3b8e566 | 2020-04-20 12:12:53 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Frank Barchard | 36b76b6 | 2020-04-10 12:39:17 -0700 | [diff] [blame] | 184 | BENCHMARK_GEMM(f16_gemm_1x16__aarch64_neonfp16arith_ld32) |
Frank Barchard | 683f559 | 2020-04-10 00:48:26 -0700 | [diff] [blame] | 185 | BENCHMARK_GEMM(f16_gemm_4x16__aarch64_neonfp16arith_ld32) |
| 186 | BENCHMARK_GEMM(f16_gemm_6x16__aarch64_neonfp16arith_ld32) |
Frank Barchard | bddfbcd | 2020-04-15 12:32:41 -0700 | [diff] [blame] | 187 | BENCHMARK_GEMM(f16_gemm_1x8__aarch64_neonfp16arith_ld64) |
| 188 | BENCHMARK_GEMM(f16_gemm_4x8__aarch64_neonfp16arith_ld64) |
| 189 | BENCHMARK_GEMM(f16_gemm_6x8__aarch64_neonfp16arith_ld64) |
Frank Barchard | 3b8e566 | 2020-04-20 12:12:53 -0700 | [diff] [blame] | 190 | BENCHMARK_GEMM(f16_gemm_8x8__aarch64_neonfp16arith_ld64) |
Frank Barchard | 683f559 | 2020-04-10 00:48:26 -0700 | [diff] [blame] | 191 | #endif // XNN_ARCH_ARM64 |
Frank Barchard | bddfbcd | 2020-04-15 12:32:41 -0700 | [diff] [blame] | 192 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 193 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 194 | BENCHMARK_MAIN(); |
| 195 | #endif |