blob: 12529b881173f20359fa4c8f29eed68b4a47de93 [file] [log] [blame]
Marat Dukhanbdb56f52020-02-05 21:42:49 -08001// 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 <fp16/fp16.h>
17#include "bench/gemm.h"
18#include "bench/utils.h"
19#include <xnnpack/AlignedAllocator.h>
20#include <xnnpack/common.h>
21#include <xnnpack/params-init.h>
22#include <xnnpack/params.h>
23#include <xnnpack/spmm.h>
24
25
26static void SpMMBenchmark(benchmark::State& state,
27 xnn_f16_spmm_ukernel_function spmm, uint32_t mr, uint32_t nr, float sparsity)
28{
29 if (!cpuinfo_initialize()) {
30 state.SkipWithError("cpuinfo initialization failed");
31 return;
32 }
33
34 const size_t mc = state.range(0);
35 const size_t nc = state.range(1);
36 const size_t kc = state.range(2);
37
38 std::random_device random_device;
39 auto rng = std::mt19937(random_device());
40 auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng);
41 auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
42
43 // if using blocks, generate the reduced matrix first and then extrude along
44 // the block dimension (n), to get the full matrix
45 size_t ncols = nc / nr + nc % nr;
46 std::vector<uint16_t> b(ncols * kc);
47 std::vector<uint16_t> bias(nc);
48 std::vector<uint16_t> w;
49 std::vector<uint32_t> nmap;
50 std::vector<int32_t> dmap;
51 const size_t sparse_end = std::min(size_t(float(b.size()) * sparsity), b.size());
52 const size_t num_nonzeroes = nr * (b.size() - sparse_end);
53
54 const size_t w_elements = num_nonzeroes + nc;
55 const size_t c_elements = mc * nc;
56 const size_t dmap_elements = num_nonzeroes / nr;
57 const size_t nmap_elements = nc;
58 const size_t num_buffers = 1 +
59 benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(),
60 sizeof(uint16_t) * (w_elements + c_elements) + sizeof(uint32_t) * (dmap_elements + nmap_elements));
61
62 // Micro-kernel can access one element beyond w and dmap for software pipelining.
63 w.reserve(num_buffers * w_elements + 1);
64 dmap.reserve(num_buffers * dmap_elements + 1);
65 nmap.resize(num_buffers * nmap_elements);
66
67 std::vector<size_t> a_offsets(num_buffers);
68
69 for (size_t buffer_index = 0; buffer_index < num_buffers; buffer_index++) {
70 // Re-generate weights. Note: each re-generation produces the number of non-zeroes.
71 std::fill(b.begin(), b.begin() + sparse_end, 0);
72 std::generate(b.begin() + sparse_end, b.end(), std::ref(f16rng));
73 std::shuffle(b.begin(), b.end(), rng);
74 std::generate(bias.begin(), bias.end(), std::ref(f16rng));
75
76 uint32_t first_j = 0, last_j = 0;
77 bool is_first_nonzero = true;
78 for (uint32_t i = 0; i < nc / nr; i++) {
79 for (uint32_t n = 0; n < nr; n++)
80 w.push_back(bias[nr * i + n]);
81 for (uint32_t j = 0; j < kc; j++) {
82 if ((b[i * kc + j] & 0x7FFF) != 0) {
83 for (size_t l = 0; l < nr; l++)
84 w.push_back(fp16_ieee_from_fp32_value(fp16_ieee_to_fp32_value(b[i * kc + j]) + static_cast<float>(i)));
85 if (is_first_nonzero) {
86 first_j = j;
87 } else {
88 const ptrdiff_t increment = int32_t(j - last_j) * int32_t(mc) * int32_t(sizeof(uint16_t));
89 dmap.push_back(increment);
90 }
91 last_j = j;
92 is_first_nonzero = false;
93 nmap[buffer_index * nmap_elements + i] += 1;
94 }
95 }
96 }
97 for (uint32_t i = nc / nr; i < ncols; i++) {
98 w.push_back(bias[i]);
99 for (uint32_t j = 0; j < kc; j++) {
100 if ((b[i * kc + j] & 0x7FFF) != 0) {
101 w.push_back(b[i * kc + j]);
102 if (is_first_nonzero) {
103 first_j = j;
104 } else {
105 const ptrdiff_t increment = int32_t(j - last_j) * int32_t(mc) * int32_t(sizeof(uint16_t));
106 dmap.push_back(increment);
107 }
108 last_j = j;
109 is_first_nonzero = false;
110 nmap[buffer_index * nmap_elements + i] += 1;
111 }
112 }
113 }
114 {
115 const ptrdiff_t increment = int32_t(first_j - last_j) * int32_t(mc) * int32_t(sizeof(uint16_t));
116 dmap.push_back(increment);
117 }
118
119 a_offsets[buffer_index] = first_j * mc;
120 }
121
122 // Micro-kernel can access one element beyond w and dmap for software pipelining.
123 w.resize(w.size() + 1);
124 dmap.resize(dmap.size() + 1);
125
126 std::vector<float, AlignedAllocator<float, 64>> a(kc * mc);
127 std::vector<float, AlignedAllocator<float, 64>> c(num_buffers * c_elements);
128
129 std::generate(a.begin(), a.end(), std::ref(f32rng));
130 std::fill(c.begin(), c.end(), nanf(""));
131
132 xnn_f16_output_params output_params{
133 0x3C00 /* 1.0 */, 0x7C00 /* inf */, 0xFC00 /* -inf */};
134
135 size_t buffer_index = 0;
136 for (auto _ : state) {
137 // Use circular buffers (exceeding cache size) and prefetch to control cache state:
138 // - A is always in L1 cache (if fits, otherwise L2, L3, etc)
139 // - W, Kmap, and Nmap is not in cache (for any cache level)
140 // - C is not in cache (for any cache level)
141 state.PauseTiming();
142 benchmark::utils::PrefetchToL1(a.data(), a.size() * sizeof(uint16_t));
143 buffer_index = (buffer_index + 1) % num_buffers;
144 state.ResumeTiming();
145
146 spmm(mc, nc,
147 a.data() + a_offsets[buffer_index],
148 w.data() + buffer_index * w_elements,
149 dmap.data() + buffer_index * dmap_elements,
150 nmap.data() + buffer_index * nmap_elements,
151 c.data() + buffer_index * c_elements,
152 &output_params);
153 }
154
155 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
156 state.counters["FLOPS"] = benchmark::Counter(
157 uint64_t(state.iterations()) * 2 * mc * num_nonzeroes, benchmark::Counter::kIsRate);
158
159 state.counters["EffFLOPS"] = benchmark::Counter(
160 uint64_t(state.iterations()) * 2 * mc * nc * kc, benchmark::Counter::kIsRate);
161}
162
163
164#if XNN_ARCH_ARM64
165 static void spmm80_8x1__neonfp16arith(benchmark::State& state, const char* net) {
166 SpMMBenchmark(state, xnn_f16_spmm_ukernel_8x1__neonfp16arith, 8, 1, 0.8f);
167 }
168 static void spmm80_8x1__neonfp16arith_unroll2(benchmark::State& state, const char* net) {
169 SpMMBenchmark(state, xnn_f16_spmm_ukernel_8x1__neonfp16arith_unroll2, 8, 1, 0.8f);
170 }
171 static void spmm80_16x1__neonfp16arith(benchmark::State& state, const char* net) {
172 SpMMBenchmark(state, xnn_f16_spmm_ukernel_16x1__neonfp16arith, 16, 1, 0.8f);
173 }
174 static void spmm80_16x1__neonfp16arith_unroll2(benchmark::State& state, const char* net) {
175 SpMMBenchmark(state, xnn_f16_spmm_ukernel_16x1__neonfp16arith_unroll2, 16, 1, 0.8f);
176 }
177 static void spmm80_24x1__neonfp16arith(benchmark::State& state, const char* net) {
178 SpMMBenchmark(state, xnn_f16_spmm_ukernel_24x1__neonfp16arith, 24, 1, 0.8f);
179 }
180 static void spmm80_24x1__neonfp16arith_unroll2(benchmark::State& state, const char* net) {
181 SpMMBenchmark(state, xnn_f16_spmm_ukernel_24x1__neonfp16arith_unroll2, 24, 1, 0.8f);
182 }
183 static void spmm80_32x1__neonfp16arith(benchmark::State& state, const char* net) {
184 SpMMBenchmark(state, xnn_f16_spmm_ukernel_32x1__neonfp16arith, 32, 1, 0.8f);
185 }
186 static void spmm80_32x1__neonfp16arith_unroll2(benchmark::State& state, const char* net) {
187 SpMMBenchmark(state, xnn_f16_spmm_ukernel_32x1__neonfp16arith_unroll2, 32, 1, 0.8f);
188 }
189
190 BENCHMARK_GEMM(spmm80_8x1__neonfp16arith)
191 BENCHMARK_GEMM(spmm80_8x1__neonfp16arith_unroll2)
192 BENCHMARK_GEMM(spmm80_16x1__neonfp16arith)
193 BENCHMARK_GEMM(spmm80_16x1__neonfp16arith_unroll2)
194 BENCHMARK_GEMM(spmm80_24x1__neonfp16arith)
195 BENCHMARK_GEMM(spmm80_24x1__neonfp16arith_unroll2)
196 BENCHMARK_GEMM(spmm80_32x1__neonfp16arith)
197 BENCHMARK_GEMM(spmm80_32x1__neonfp16arith_unroll2)
198#endif // XNN_ARCH_ARM64
199
200#ifndef XNNPACK_BENCHMARK_NO_MAIN
201BENCHMARK_MAIN();
202#endif