blob: bc971bce197a9b970d62bccbd149d831c52b1b30 [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// 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
18#include "third_party/FP16/include/fp16/fp16.h"
19#include "bench/utils.h"
20#include "bench/gemm.h"
21#include <xnnpack/AlignedAllocator.h>
22#include <xnnpack/gemm.h>
23#include <xnnpack/params.h>
24#include <xnnpack/pack.h>
25#include <xnnpack/requantization.h>
26
27#include <benchmark/benchmark.h>
28
29
30static void GEMMBenchmark(benchmark::State& state,
31 xnn_f16_gemm_ukernel_function hgemm,
32 size_t mr, size_t nr, size_t kr)
33{
34 if (!cpuinfo_initialize()) {
35 state.SkipWithError("cpuinfo initialization failed");
36 return;
37 }
38
39 const size_t mc = state.range(0);
40 const size_t nc = state.range(1);
41 const size_t kc = state.range(2);
42
43 const size_t nc_stride = benchmark::utils::roundUp(nc, nr);
44 const size_t kc_stride = benchmark::utils::roundUp(kc, kr);
45
46 std::random_device random_device;
47 auto rng = std::mt19937(random_device());
48 auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng);
49 auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
50
51 std::vector<uint16_t> a(mc * kc);
52 std::generate(a.begin(), a.end(), std::ref(f16rng));
53 std::vector<uint16_t> k(nc * kc);
54 std::generate(k.begin(), k.end(), std::ref(f16rng));
55 std::vector<uint16_t> b(nc);
56 std::generate(b.begin(), b.end(), std::ref(f16rng));
57
58 const size_t w_elements = nc_stride * kc_stride + nc_stride;
59 const size_t c_elements = mc * nc;
60 const size_t num_buffers = 1 +
61 benchmark::utils::divideRoundUp<size_t>(cpuinfo_get_max_cache_size(),
62 sizeof(uint16_t) * (w_elements + c_elements));
63
64 std::vector<uint16_t, AlignedAllocator<uint16_t, 32>> w(w_elements * num_buffers);
65 std::fill(w.begin(), w.end(), 0);
66 xnn_pack_f16_gemm_goi_w(1 /* groups */, nc, kc, nr, kr, k.data(), b.data(), w.data());
67 std::vector<uint16_t> c(c_elements * num_buffers);
68 std::fill(c.begin(), c.end(), UINT16_C(0x7E00) /* NaN */);
69
70 xnn_f16_output_params output_params{
71 0x3C00 /* 1.0 */, 0x7C00 /* inf */, 0xFC00 /* -inf */};
72
73 size_t buffer_index = 0;
74 for (auto _ : state) {
75 // Use circular buffers (exceeding cache size) and prefetch to control cache state:
76 // - A is always in L1 cache (if fits, otherwise L2, L3, etc)
77 // - W is not in cache (for any cache level)
78 // - C is not in cache (for any cache level)
79 state.PauseTiming();
80 benchmark::utils::prefetchToL1(a.data(), a.size() * sizeof(uint16_t));
81 buffer_index = (buffer_index + 1) % num_buffers;
82 state.ResumeTiming();
83
84 for (uint32_t m = 0; m < mc; m += mr) {
85 const uint32_t mb = min(mc - m, mr);
86 for (uint32_t n = 0; n < nc; n += nr) {
87 const uint32_t nb = min(nc - n, nr);
88 hgemm(
89 mb, nb, kc,
90 a.data() + m * kc, kc * sizeof(uint16_t),
91 w.data() + (nc_stride * buffer_index + n) * (kc_stride + 1),
92 c.data() + (mc * buffer_index + m) * nc + n, nc * sizeof(uint16_t), nr * sizeof(uint16_t),
93 &output_params);
94 }
95 }
96 }
97
98 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
99 state.counters["FLOPS"] = benchmark::Counter(
100 uint64_t(state.iterations()) * 2 * mc * nc * kc, benchmark::Counter::kIsRate);
101}
102
103#if CPUINFO_ARCH_ARM64
104 static void hgemm_4x8__neonfp16arith_ld64(benchmark::State& state, const char* net) {
105 GEMMBenchmark(state, xnn_f16_gemm_ukernel_4x8__neonfp16arith_ld64, 4, 8, 1);
106 }
107
108 static void hgemm_6x8__neonfp16arith_ld64(benchmark::State& state, const char* net) {
109 GEMMBenchmark(state, xnn_f16_gemm_ukernel_6x8__neonfp16arith_ld64, 6, 8, 1);
110 }
111
112 static void hgemm_8x8__neonfp16arith_ld64(benchmark::State& state, const char* net) {
113 GEMMBenchmark(state, xnn_f16_gemm_ukernel_8x8__neonfp16arith_ld64, 8, 8, 1);
114 }
115
116 BENCHMARK_GEMM(hgemm_4x8__neonfp16arith_ld64)
117 BENCHMARK_GEMM(hgemm_6x8__neonfp16arith_ld64)
118 BENCHMARK_GEMM(hgemm_8x8__neonfp16arith_ld64)
119#endif
120
121#ifndef XNNPACK_BENCHMARK_NO_MAIN
122BENCHMARK_MAIN();
123#endif