blob: dec01928ac1801fd45ed1b95e56e96c8761a7001 [file] [log] [blame]
Marat Dukhan595e1702020-07-31 10:12:52 -07001// Copyright 2020 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 <chrono>
9#include <cmath>
10#include <functional>
11#include <limits>
12#include <mutex>
13#include <random>
14#include <vector>
15
16#include <cpuinfo.h>
17
18#include <benchmark/benchmark.h>
19#include "bench/gemm.h"
20#include "bench/utils.h"
21#include <xnnpack/AlignedAllocator.h>
22#include <xnnpack/common.h>
23#include <xnnpack/gemm.h>
24#include <xnnpack/pack.h>
25#include <xnnpack/params-init.h>
26#include <xnnpack/params.h>
27
28
29static void GEMMBenchmark(benchmark::State& state,
30 xnn_qs8_gemm_ukernel_function gemm,
Marat Dukhan1566fee2020-08-02 21:55:41 -070031 size_t mr, size_t nr, size_t kr, size_t sr,
32 benchmark::utils::IsaCheckFunction isa_check = nullptr)
Marat Dukhan595e1702020-07-31 10:12:52 -070033{
34 if (!cpuinfo_initialize()) {
35 state.SkipWithError("cpuinfo initialization failed");
36 return;
37 }
Marat Dukhan1566fee2020-08-02 21:55:41 -070038 if (isa_check && !isa_check(state)) {
39 return;
40 }
Marat Dukhan595e1702020-07-31 10:12:52 -070041
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
46 const size_t nc_stride = benchmark::utils::RoundUp(nc, nr);
47 const size_t kc_stride = benchmark::utils::RoundUp(kc, kr);
48
49 std::random_device random_device;
50 auto rng = std::mt19937(random_device());
Marat Dukhan44f0ca72020-08-02 21:46:58 -070051 auto s32rng = std::bind(std::uniform_int_distribution<int32_t>(-10000, 10000), std::ref(rng));
Marat Dukhan595e1702020-07-31 10:12:52 -070052 auto s8rng = std::bind(
Marat Dukhan44f0ca72020-08-02 21:46:58 -070053 std::uniform_int_distribution<uint32_t>(std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()), std::ref(rng));
Marat Dukhan595e1702020-07-31 10:12:52 -070054
55 std::vector<int8_t> a(mc * kc);
56 std::generate(a.begin(), a.end(), std::ref(s8rng));
57 std::vector<int8_t> k(nc * kc);
58 std::generate(k.begin(), k.end(), std::ref(s8rng));
59 std::vector<int32_t> b(nc);
60 std::generate(b.begin(), b.end(), std::ref(s32rng));
61
62 const size_t w_elements = kc_stride * nc_stride + nc_stride * sizeof(int32_t) / sizeof(int8_t);
63 const size_t c_elements = mc * nc;
64 const size_t num_buffers = 1 +
65 benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(),
66 sizeof(int8_t) * (w_elements + c_elements));
67
68 std::vector<int8_t, AlignedAllocator<int8_t, 32>> w(w_elements * num_buffers);
69 std::fill(w.begin(), w.end(), 0);
70 const xnn_qs8_packing_params packing_params = { 127 };
71 xnn_pack_qs8_gemm_goi_w(1 /* groups */, nc, kc, nr, kr, sr, k.data(), b.data(), w.data(), &packing_params);
72 std::vector<int8_t> c(c_elements * num_buffers);
73 std::fill(c.begin(), c.end(), 0xA5);
74
75 union xnn_qs8_gemm_params quantization_params = xnn_init_qs8_gemm_params(0.75f, 127, -127, 126);
76
77 size_t buffer_index = 0;
78 for (auto _ : state) {
79 // Use circular buffers (exceeding cache size) and prefetch to control cache state:
80 // - A is always in L1 cache (if fits, otherwise L2, L3, etc)
81 // - W is not in cache (for any cache level)
82 // - C is not in cache (for any cache level)
83 state.PauseTiming();
84 benchmark::utils::PrefetchToL1(a.data(), a.size() * sizeof(int8_t));
85 buffer_index = (buffer_index + 1) % num_buffers;
86 state.ResumeTiming();
87
88 for (uint32_t m = 0; m < mc; m += mr) {
89 const uint32_t mb = min(mc - m, mr);
90 for (uint32_t n = 0; n < nc; n += nr) {
91 const uint32_t nb = min(nc - n, nr);
92 gemm(
93 mb, nb, kc * sizeof(int8_t),
94 a.data() + m * kc, kc * sizeof(int8_t),
95 w.data() + (w_elements * buffer_index + n * (kc_stride + sizeof(int32_t))) / sizeof(int8_t),
96 c.data() + (mc * buffer_index + m) * nc + n, nc * sizeof(int8_t), nr * sizeof(int8_t),
97 &quantization_params);
98 }
99 }
100 }
101
102 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
103 state.counters["OPS"] = benchmark::Counter(
104 uint64_t(state.iterations()) * 2 * mc * nc * kc, benchmark::Counter::kIsRate);
105}
106
107
108#if XNN_ARCH_X86 || XNN_ARCH_X86_64
Marat Dukhan14d3ce82020-07-31 16:19:15 -0700109 static void qs8_gemm_4x4c2__sse2_ld64(benchmark::State& state, const char* net) {
110 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_4x4c2__sse2_ld64, 4, 4, 2, 1);
Marat Dukhan595e1702020-07-31 10:12:52 -0700111 }
Marat Dukhan14d3ce82020-07-31 16:19:15 -0700112 static void qs8_gemm_4x4c2__ssse3_ld64(benchmark::State& state, const char* net) {
Marat Dukhan1566fee2020-08-02 21:55:41 -0700113 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_4x4c2__ssse3_ld64, 4, 4, 2, 1, benchmark::utils::CheckSSSE3);
Marat Dukhan595e1702020-07-31 10:12:52 -0700114 }
Marat Dukhan14d3ce82020-07-31 16:19:15 -0700115 static void qs8_gemm_4x4c2__sse41_ld64(benchmark::State& state, const char* net) {
Marat Dukhan1566fee2020-08-02 21:55:41 -0700116 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_4x4c2__sse41_ld64, 4, 4, 2, 1, benchmark::utils::CheckSSE41);
117 }
118 static void qs8_gemm_4x4c2__xop_ld64(benchmark::State& state, const char* net) {
119 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_4x4c2__xop_ld64, 4, 4, 2, 1, benchmark::utils::CheckXOP);
Marat Dukhan595e1702020-07-31 10:12:52 -0700120 }
121
Marat Dukhandee732b2020-07-31 17:32:04 -0700122 static void qs8_gemm_4x4c2__sse2_ld128(benchmark::State& state, const char* net) {
123 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_4x4c2__sse2_ld128, 4, 4, 2, 1);
124 }
125 static void qs8_gemm_4x4c2__ssse3_ld128(benchmark::State& state, const char* net) {
Marat Dukhan1566fee2020-08-02 21:55:41 -0700126 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_4x4c2__ssse3_ld128, 4, 4, 2, 1, benchmark::utils::CheckSSSE3);
Marat Dukhandee732b2020-07-31 17:32:04 -0700127 }
128 static void qs8_gemm_4x4c2__sse41_ld128(benchmark::State& state, const char* net) {
Marat Dukhan1566fee2020-08-02 21:55:41 -0700129 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_4x4c2__sse41_ld128, 4, 4, 2, 1, benchmark::utils::CheckSSE41);
130 }
131 static void qs8_gemm_4x4c2__xop_ld128(benchmark::State& state, const char* net) {
132 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_4x4c2__xop_ld128, 4, 4, 2, 1, benchmark::utils::CheckXOP);
Marat Dukhandee732b2020-07-31 17:32:04 -0700133 }
134
Marat Dukhan14d3ce82020-07-31 16:19:15 -0700135 static void qs8_gemm_2x4c8__sse2_ld64(benchmark::State& state, const char* net) {
136 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_2x4c8__sse2_ld64, 2, 4, 8, 1);
Marat Dukhan733d0be2020-07-31 15:55:36 -0700137 }
Marat Dukhan14d3ce82020-07-31 16:19:15 -0700138 static void qs8_gemm_2x4c8__ssse3_ld64(benchmark::State& state, const char* net) {
Marat Dukhan1566fee2020-08-02 21:55:41 -0700139 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_2x4c8__ssse3_ld64, 2, 4, 8, 1, benchmark::utils::CheckSSSE3);
Marat Dukhan733d0be2020-07-31 15:55:36 -0700140 }
Marat Dukhan14d3ce82020-07-31 16:19:15 -0700141 static void qs8_gemm_2x4c8__sse41_ld64(benchmark::State& state, const char* net) {
Marat Dukhan1566fee2020-08-02 21:55:41 -0700142 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_2x4c8__sse41_ld64, 2, 4, 8, 1, benchmark::utils::CheckSSE41);
143 }
144 static void qs8_gemm_2x4c8__xop_ld64(benchmark::State& state, const char* net) {
145 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_2x4c8__xop_ld64, 2, 4, 8, 1, benchmark::utils::CheckXOP);
Marat Dukhan733d0be2020-07-31 15:55:36 -0700146 }
147
Marat Dukhandee732b2020-07-31 17:32:04 -0700148 static void qs8_gemm_2x4c8__sse2_ld128(benchmark::State& state, const char* net) {
149 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_2x4c8__sse2_ld128, 2, 4, 8, 1);
150 }
151 static void qs8_gemm_2x4c8__ssse3_ld128(benchmark::State& state, const char* net) {
Marat Dukhan1566fee2020-08-02 21:55:41 -0700152 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_2x4c8__ssse3_ld128, 2, 4, 8, 1, benchmark::utils::CheckSSSE3);
Marat Dukhandee732b2020-07-31 17:32:04 -0700153 }
154 static void qs8_gemm_2x4c8__sse41_ld128(benchmark::State& state, const char* net) {
Marat Dukhan1566fee2020-08-02 21:55:41 -0700155 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_2x4c8__sse41_ld128, 2, 4, 8, 1, benchmark::utils::CheckSSE41);
156 }
157 static void qs8_gemm_2x4c8__xop_ld128(benchmark::State& state, const char* net) {
158 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_2x4c8__xop_ld128, 2, 4, 8, 1, benchmark::utils::CheckXOP);
Marat Dukhandee732b2020-07-31 17:32:04 -0700159 }
160
Marat Dukhane7edc802020-08-03 16:43:57 -0700161 static void qs8_gemm_3x4c8__sse2_ld64(benchmark::State& state, const char* net) {
162 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_3x4c8__sse2_ld64, 3, 4, 8, 1);
163 }
164 static void qs8_gemm_3x4c8__ssse3_ld64(benchmark::State& state, const char* net) {
165 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_3x4c8__ssse3_ld64, 3, 4, 8, 1, benchmark::utils::CheckSSSE3);
166 }
167 static void qs8_gemm_3x4c8__sse41_ld64(benchmark::State& state, const char* net) {
168 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_3x4c8__sse41_ld64, 3, 4, 8, 1, benchmark::utils::CheckSSE41);
169 }
170 static void qs8_gemm_3x4c8__xop_ld64(benchmark::State& state, const char* net) {
171 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_3x4c8__xop_ld64, 3, 4, 8, 1, benchmark::utils::CheckXOP);
172 }
173
174 static void qs8_gemm_3x4c8__sse2_ld128(benchmark::State& state, const char* net) {
175 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_3x4c8__sse2_ld128, 3, 4, 8, 1);
176 }
177 static void qs8_gemm_3x4c8__ssse3_ld128(benchmark::State& state, const char* net) {
178 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_3x4c8__ssse3_ld128, 3, 4, 8, 1, benchmark::utils::CheckSSSE3);
179 }
180 static void qs8_gemm_3x4c8__sse41_ld128(benchmark::State& state, const char* net) {
181 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_3x4c8__sse41_ld128, 3, 4, 8, 1, benchmark::utils::CheckSSE41);
182 }
183 static void qs8_gemm_3x4c8__xop_ld128(benchmark::State& state, const char* net) {
184 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_3x4c8__xop_ld128, 3, 4, 8, 1, benchmark::utils::CheckXOP);
185 }
186
Marat Dukhan12809522020-08-02 22:23:51 -0700187 static void qs8_gemm_2x8c8__avx2(benchmark::State& state, const char* net) {
188 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_2x8c8__avx2, 2, 8, 8, 1, benchmark::utils::CheckAVX2);
189 }
190 static void qs8_gemm_3x8c8__avx2(benchmark::State& state, const char* net) {
191 GEMMBenchmark(state, xnn_qs8_gemm_minmax_ukernel_3x8c8__avx2, 3, 8, 8, 1, benchmark::utils::CheckAVX2);
192 }
193
Marat Dukhan14d3ce82020-07-31 16:19:15 -0700194 BENCHMARK_GEMM(qs8_gemm_4x4c2__sse2_ld64)
195 BENCHMARK_GEMM(qs8_gemm_4x4c2__ssse3_ld64)
196 BENCHMARK_GEMM(qs8_gemm_4x4c2__sse41_ld64)
Marat Dukhan1566fee2020-08-02 21:55:41 -0700197 BENCHMARK_GEMM(qs8_gemm_4x4c2__xop_ld64)
Marat Dukhan733d0be2020-07-31 15:55:36 -0700198
Marat Dukhandee732b2020-07-31 17:32:04 -0700199 BENCHMARK_GEMM(qs8_gemm_4x4c2__sse2_ld128)
200 BENCHMARK_GEMM(qs8_gemm_4x4c2__ssse3_ld128)
201 BENCHMARK_GEMM(qs8_gemm_4x4c2__sse41_ld128)
Marat Dukhan1566fee2020-08-02 21:55:41 -0700202 BENCHMARK_GEMM(qs8_gemm_4x4c2__xop_ld128)
Marat Dukhandee732b2020-07-31 17:32:04 -0700203
Marat Dukhan14d3ce82020-07-31 16:19:15 -0700204 BENCHMARK_GEMM(qs8_gemm_2x4c8__sse2_ld64)
Marat Dukhane7edc802020-08-03 16:43:57 -0700205 BENCHMARK_GEMM(qs8_gemm_3x4c8__sse2_ld64)
Marat Dukhan14d3ce82020-07-31 16:19:15 -0700206 BENCHMARK_GEMM(qs8_gemm_2x4c8__ssse3_ld64)
Marat Dukhane7edc802020-08-03 16:43:57 -0700207 BENCHMARK_GEMM(qs8_gemm_3x4c8__ssse3_ld64)
Marat Dukhan14d3ce82020-07-31 16:19:15 -0700208 BENCHMARK_GEMM(qs8_gemm_2x4c8__sse41_ld64)
Marat Dukhane7edc802020-08-03 16:43:57 -0700209 BENCHMARK_GEMM(qs8_gemm_3x4c8__sse41_ld64)
Marat Dukhan1566fee2020-08-02 21:55:41 -0700210 BENCHMARK_GEMM(qs8_gemm_2x4c8__xop_ld64)
Marat Dukhane7edc802020-08-03 16:43:57 -0700211 BENCHMARK_GEMM(qs8_gemm_3x4c8__xop_ld64)
Marat Dukhandee732b2020-07-31 17:32:04 -0700212
213 BENCHMARK_GEMM(qs8_gemm_2x4c8__sse2_ld128)
Marat Dukhane7edc802020-08-03 16:43:57 -0700214 BENCHMARK_GEMM(qs8_gemm_3x4c8__sse2_ld128)
Marat Dukhandee732b2020-07-31 17:32:04 -0700215 BENCHMARK_GEMM(qs8_gemm_2x4c8__ssse3_ld128)
Marat Dukhane7edc802020-08-03 16:43:57 -0700216 BENCHMARK_GEMM(qs8_gemm_3x4c8__ssse3_ld128)
Marat Dukhandee732b2020-07-31 17:32:04 -0700217 BENCHMARK_GEMM(qs8_gemm_2x4c8__sse41_ld128)
Marat Dukhane7edc802020-08-03 16:43:57 -0700218 BENCHMARK_GEMM(qs8_gemm_3x4c8__sse41_ld128)
Marat Dukhan1566fee2020-08-02 21:55:41 -0700219 BENCHMARK_GEMM(qs8_gemm_2x4c8__xop_ld128)
Marat Dukhane7edc802020-08-03 16:43:57 -0700220 BENCHMARK_GEMM(qs8_gemm_3x4c8__xop_ld128)
Marat Dukhan12809522020-08-02 22:23:51 -0700221
222 BENCHMARK_GEMM(qs8_gemm_2x8c8__avx2)
223 BENCHMARK_GEMM(qs8_gemm_3x8c8__avx2)
Marat Dukhan595e1702020-07-31 10:12:52 -0700224#endif
225
226#ifndef XNNPACK_BENCHMARK_NO_MAIN
227BENCHMARK_MAIN();
228#endif