blob: 7a983ed6ac6e95d1f5afc391bb549955fe2d5eb0 [file] [log] [blame]
Georgios Pinitas7cd26d42019-01-09 18:35:17 +00001/*
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +01002 * Copyright (c) 2017-2020 Arm Limited.
Georgios Pinitas7cd26d42019-01-09 18:35:17 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#pragma once
25
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000026#include <algorithm>
David Mansell318c9f42020-07-08 13:28:45 +010027#include <cassert>
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000028
29#include "arm_gemm.hpp"
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010030#include "bias_adder.hpp"
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +010031#include "ndrange.hpp"
David Mansell318c9f42020-07-08 13:28:45 +010032#include "performance_parameters.hpp"
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000033#include "transform.hpp"
David Mansell318c9f42020-07-08 13:28:45 +010034#include "utils.hpp"
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000035
36#ifdef CYCLE_PROFILING
37#include "profiler.hpp"
38#endif
39
40namespace arm_gemm {
41
42// Implementation of the GemmCommon abstract class.
43template<typename strategy, typename To, typename Tr>
44class GemmHybrid : public GemmCommon<To, Tr> {
45 typedef typename strategy::operand_type Toi;
46 typedef typename strategy::result_type Tri;
47
48 /* const properties set by constructor */
49 const CPUInfo * const _ci;
50
51 const unsigned int _Msize;
52 const unsigned int _Nsize;
53 const unsigned int _Ksize;
54
55 const unsigned int _nbatches;
56 const unsigned int _nmulti;
57
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010058 const Activation _act;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000059
60 /* Blocking info */
Georgios Pinitas1d480652019-01-23 11:24:50 +000061 const unsigned int _k_block;
62 const unsigned int _n_block;
63 const unsigned int _Mround;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000064
65 /* Pretransposed buffer. */
66 const Toi *_B_transposed=nullptr;
67
Georgios Pinitas1d480652019-01-23 11:24:50 +000068 const NDRange<4> _window_range;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000069
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010070 static unsigned int compute_k_block(const GemmArgs &args) {
Georgios Pinitas0cc50ed2020-07-06 19:10:38 +010071 // Some kernels don't support accumulate mode - these can't do K blocking at all.
72 if (!strategy::supports_accumulate()) {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010073 return args._Ksize;
74 }
75
Georgios Pinitas1d480652019-01-23 11:24:50 +000076 if (args._cfg && args._cfg->inner_block_size) {
77 return args._cfg->inner_block_size;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000078 }
79
Georgios Pinitas1d480652019-01-23 11:24:50 +000080 const unsigned int L1_size = args._ci->get_L1_cache_size();
81
82 // k_block: Find out how much of the larger array can be loaded into half the cache.
83 // This should account for associative caches.
84 unsigned int k_block = (L1_size / 2) / (sizeof(Toi) * (std::max(strategy::out_width(), strategy::out_height())));
85
86 // Needs to be (at least a single) multiple of the K unroll level.
87 k_block /= strategy::k_unroll();
88 k_block = std::max(k_block, 1U) * strategy::k_unroll();
89
90 // Now tune to presented problem size; this is how many blocks we need.
91 unsigned int numk_blocks = iceildiv(args._Ksize, k_block);
92
93 // So divide the space equally into that many blocks.
94 k_block = iceildiv(args._Ksize, numk_blocks);
95
96 // And round UP to the K unroll level required.
97 k_block = roundup(k_block, strategy::k_unroll());
98
99 return k_block;
100 }
101
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100102 static unsigned int compute_n_block(const GemmArgs &args) {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000103 if (args._cfg && args._cfg->outer_block_size) {
104 return args._cfg->outer_block_size;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000105 }
106
Georgios Pinitas1d480652019-01-23 11:24:50 +0000107 const unsigned int k_block = compute_k_block(args);
108 const unsigned int L2_size = args._ci->get_L2_cache_size();
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000109
Georgios Pinitas1d480652019-01-23 11:24:50 +0000110 // n_block: Work out how many rows (of length k_block) will fit in the L2
111 // Don't allocate more than 90% of the L2 to allow for overheads, and subtract off the L1 contents.
112 unsigned int n_block = (((L2_size * 9) / 10) - (k_block * sizeof(Toi) * (strategy::out_width() + strategy::out_height()))) /
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100113 (sizeof(Toi) * k_block);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000114
Georgios Pinitas1d480652019-01-23 11:24:50 +0000115 // Needs to be (at least a single) multiple of the kernel output width.
116 n_block /= strategy::out_width();
117 n_block = std::max(n_block, 1U) * strategy::out_width();
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000118
Georgios Pinitas1d480652019-01-23 11:24:50 +0000119 // And tune to the presented problem size.
120 unsigned int numblocks = iceildiv(args._Nsize, n_block);
121 n_block = iceildiv(args._Nsize, numblocks);
122 n_block = roundup(n_block, strategy::out_width());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000123
Georgios Pinitas1d480652019-01-23 11:24:50 +0000124 return n_block;
125 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000126
127public:
128 GemmHybrid(GemmHybrid &) = delete;
129 GemmHybrid & operator= (GemmHybrid &) = delete;
130
131 /* Constructor */
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100132 GemmHybrid(const GemmArgs &args)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100133 : _ci(args._ci), _Msize(args._Msize), _Nsize(args._Nsize), _Ksize(args._Ksize),
Georgios Pinitas0cc50ed2020-07-06 19:10:38 +0100134 _nbatches(args._nbatches), _nmulti(args._nmulti),
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100135 _act(args._act),
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100136 _k_block(compute_k_block(args)), _n_block(compute_n_block(args)),
137 _Mround(roundup(args._Msize, strategy::out_height())),
138 _window_range(iceildiv(args._Msize, strategy::out_height()), _nbatches, iceildiv(_Nsize, _n_block), _nmulti) { }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000139
140 // Interface implementation - Compulsory functions
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000141 ndrange_t get_window_size() const override {
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100142 return { _window_range.total_size() };
Georgios Pinitas1d480652019-01-23 11:24:50 +0000143 }
144
145 // This kernel can always be dynamically scheduled.
146 bool supports_dynamic_scheduling() const override {
147 return true;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000148 }
149
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100150 // Execute
151 void execute(const ndcoord_t &work_range, const ndcoord_t &, int) override {
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000152#ifdef CYCLE_PROFILING
153 profiler prof;
154#endif
155 strategy strat(_ci);
156
157 /* Make sure we've been set up correctly. */
158 assert(_B_transposed);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000159 static_assert(std::is_same<To, Toi>::value, "gemm_native: Operand types must be the same.");
160 static_assert(std::is_same<Tr, Tri>::value, "gemm_native: Result types must be the same.");
161
Georgios Pinitas1d480652019-01-23 11:24:50 +0000162 /* For now, each work item implies all the K for a given output
163 * pixel (so we don't need to synchronize access to the output
164 * array). So separate the loop over K blocks here. */
165 for (unsigned int k0=0; k0<_Ksize; k0+=_k_block) {
166 unsigned int kmax = std::min(k0 + _k_block, _Ksize);
167 unsigned int kern_k = roundup(kmax-k0, strategy::k_unroll());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000168
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100169 const bool first_pass = (k0 == 0);
170 const bool last_pass = (kmax == _Ksize);
171
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100172 auto p = _window_range.iterator(work_range.get_position(0), work_range.get_position_end(0));
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000173
Georgios Pinitas1d480652019-01-23 11:24:50 +0000174 if (p.done()) {
175 return;
176 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000177
Georgios Pinitas1d480652019-01-23 11:24:50 +0000178 do {
179 const unsigned int m_start = p.dim(0) * strategy::out_height();
180 const unsigned int m_end = std::min(p.dim0_max() * strategy::out_height(), _Msize);
181 const unsigned int batch = p.dim(1);
182 const unsigned int n0 = p.dim(2) * _n_block;
183 const unsigned int nmax = std::min(n0 + _n_block, _Nsize);
184 const unsigned int multi = p.dim(3);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000185
Georgios Pinitas1d480652019-01-23 11:24:50 +0000186 const Toi *b_panel = _B_transposed +
187 (multi * roundup(_Nsize, strategy::out_width()) * roundup(_Ksize, strategy::k_unroll())) +
188 (k0 * roundup(_Nsize, strategy::out_width())) +
189 (n0 * kern_k);
190
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000191#ifdef CYCLE_PROFILING
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100192 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (unsigned long)(m_end - m_start) * kern_k * roundup(nmax-n0, strategy::out_width()));
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000193#endif
194
Georgios Pinitas1d480652019-01-23 11:24:50 +0000195 strat.kernel(this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (m_start * this->_lda) + k0, this->_lda,
196 b_panel,
197 this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (m_start * this->_ldc) + n0, this->_ldc,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100198 (m_end - m_start), (nmax - n0), kmax-k0,
199 (strategy::supports_bias() && first_pass && this->_bias) ? this->_bias + (multi * this->_bias_multi_stride) + n0 : nullptr,
200 last_pass ? _act : Activation(), !first_pass);
201
202 // Add bias externally if needed
203 if (!strategy::supports_bias() && this->_bias && first_pass) {
204 bias_adder(this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (m_start * this->_ldc) + n0, this->_ldc,
205 this->_bias + (multi * this->_bias_multi_stride) + n0,
206 (m_end - m_start), (nmax - n0));
207 }
208
Georgios Pinitas1d480652019-01-23 11:24:50 +0000209 } while (p.next_dim1());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000210 }
211 }
212
213 // Interface implementation - pretransposed
214 bool B_is_pretransposed() const override {
215 return true;
216 }
217
218 bool B_pretranspose_required() const override {
219 return (_B_transposed==nullptr);
220 }
221
222 size_t get_B_pretransposed_array_size() const override {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000223 return roundup(_Nsize, strategy::out_width()) * roundup(_Ksize, strategy::k_unroll()) * _nmulti * sizeof(Toi);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000224 }
225
226 void pretranspose_B_array(void *in_buffer, const To *B, const int ldb, const int B_multi_stride) override {
227 Toi *buffer = reinterpret_cast<Toi *>(in_buffer);
228 _B_transposed = buffer;
229 strategy strat(_ci);
230
Georgios Pinitas1d480652019-01-23 11:24:50 +0000231 for (unsigned int multi=0; multi<_nmulti; multi++) {
232 for (unsigned int k0=0; k0<_Ksize; k0+=_k_block) {
233 const unsigned int kmax = std::min(k0 + _k_block, _Ksize);
234 const unsigned int k_size = roundup(kmax-k0, strategy::k_unroll());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000235
Georgios Pinitas1d480652019-01-23 11:24:50 +0000236 for (unsigned int x0=0; x0<_Nsize; x0+=_n_block) {
237 const unsigned int xmax = std::min(x0+_n_block, _Nsize);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000238
Georgios Pinitas1d480652019-01-23 11:24:50 +0000239 const unsigned int size = roundup(xmax-x0, strategy::out_width()) * k_size;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000240
Georgios Pinitas1d480652019-01-23 11:24:50 +0000241 strat.transforms.PrepareB( buffer, B + (multi * B_multi_stride), ldb,
Georgios Pinitas0cc50ed2020-07-06 19:10:38 +0100242 x0, xmax, k0, kmax);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000243
Georgios Pinitas1d480652019-01-23 11:24:50 +0000244 buffer += size;
245 }
246 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000247 }
248 }
249
250 void set_pretransposed_B_data(void *in_buffer) override {
251 _B_transposed = reinterpret_cast<Toi *>(in_buffer);
252 }
David Mansell318c9f42020-07-08 13:28:45 +0100253
254 // Estimate cycles for given problem given provided parameters
255 static uint64_t estimate_cycles(const GemmArgs &args, const PerformanceParameters &params) {
256 // Note: Current hybrid kernels don't actually round up height (they
257 // have paths for each possible height). Might need to make this
258 // configurable in future.
259 uint64_t total_macs = static_cast<uint64_t>(args._nbatches) * args._nmulti * args._Msize * roundup(args._Nsize, strategy::out_width()) * roundup(args._Ksize, strategy::k_unroll());
260
261 float mac_cycles = static_cast<float>(total_macs) / params.kernel_macs_cycle;
262
263 // TODO: A bit of a kludge here: current hybrid kernels incur extra
264 // overhead where the width is not a multiple of kernel width. It's
265 // most noticable where the overall width is quite low, so add 15%
266 // penalty for such widths.
267 if ((args._Nsize < strategy::out_width()) || (args._Nsize > strategy::out_width() && args._Nsize < 2*strategy::out_width())) {
268 mac_cycles *= 1.15f;
269 }
270
271 uint64_t total_cycles = mac_cycles;
272
273 return total_cycles;
274 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000275};
276
277} // namespace arm_gemm