blob: 53f8e6c9389feafdb21446b0a8e32d4ab787d52f [file] [log] [blame]
Joseph Dobson6f8b17d2020-02-11 19:32:11 +00001/*
2 * Copyright (c) 2020 ARM Limited.
3 *
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
26#include "arm_gemm.hpp"
27#include "utils.hpp"
28
29#include "mergeresults.hpp"
30#include "transform.hpp"
31
32#ifdef CYCLE_PROFILING
33#include "profiler.hpp"
34#endif
35
36#include <algorithm>
37#include <cassert>
38
39// Some macros used to decide how much working space to allocate.
40// Round allocations up to the next cache line.
41#define ALLOC_ROUND 64
42#define ROUND_UP(x) ((((x) + ALLOC_ROUND-1) / ALLOC_ROUND) * ALLOC_ROUND)
43
44// Implementation of the GemmCommon abstract class.
45//
46// This implementation interleaves the source matrices in blocks - good for
47// larger matrices.
48namespace arm_gemm {
49
50template<typename strategy, typename To, typename Tr>
51class GemmInterleaved2d : public GemmCommon<To, Tr> {
52 typedef typename strategy::operand_type Toi;
53 typedef typename strategy::result_type Tri;
54
55 /* const properties set by constructor */
56 const CPUInfo * const _ci;
57
58 const unsigned int _Msize;
59 const unsigned int _Nsize;
60 const unsigned int _Ksize;
61
62 const unsigned int _nbatches;
63 const unsigned int _nmulti;
64
65 const bool _trA;
66 const bool _trB;
67
68 const Activation _act;
69
70 const int _maxthreads;
71 int _nthreads;
72
73 /* Blocking info */
74 unsigned int _k_block=0;
75 unsigned int _x_block=0;
76
77 unsigned int _Mround_div=0;
78 unsigned int _Mround=0;
79 unsigned int _Nround_div=0;
80 unsigned int _Nround=0;
81
82 /* Working space, pretransposed buffer */
83 void *_working_space=nullptr;
84
85 /* We will need to walk through the blocks of B in a few contexts, so
86 * factor that out. */
87 class blockwalker {
88 private:
89 /* Size loops, etc. based on our parent's configuration */
90 const GemmInterleaved2d<strategy, To, Tr> &_parent;
91
92 /* K, X and multi parameters for current iteration. */
93 unsigned int _k0=0, _x0=0, _xmin=0, _xmax=0, _multi=0;
94
95 unsigned int _index=0;
96 bool _done=false;
97 bool _newkblock=true;
98 bool _newmulti=true;
99
100 public:
101 blockwalker(const GemmInterleaved2d<strategy, To, Tr> &parent)
102 : _parent(parent)
103 , _xmax { parent._Nsize }
104 { }
105
106 blockwalker(const GemmInterleaved2d<strategy, To, Tr> &parent, unsigned int x0, unsigned int xmax)
107 : _parent(parent)
108 , _x0 { x0 }
109 , _xmin { x0 }
110 , _xmax { xmax }
111 {
112 assert(_x0 <= _xmax);
113 }
114
115 unsigned int xmax() {
116 return std::min(_x0 + _parent._x_block, _xmax);
117 }
118
119 unsigned int kmax() {
120 return std::min(_k0 + _parent._k_block, _parent._Ksize);
121 }
122
123 /* Advance to the next block, return false at the end. */
124 bool advance(void) {
125 if (_done) {
126 return false;
127 }
128
129 _newkblock=false;
130 _x0 += _parent._x_block;
131 if (_x0 >= _xmax) {
132 _x0=_xmin;
133 _k0 += _parent._k_block;
134 if (_k0 >= _parent._Ksize) {
135 _k0=0;
136 _multi++;
137 if (_multi >= _parent._nmulti) {
138 _done=true;
139 return false;
140 }
141 _newmulti=true;
142 }
143 _newkblock=true;
144 }
145 _index++;
146
147 return true;
148 }
149
150 unsigned int k0(void) { return _k0; }
151 unsigned int x0(void) { return _x0; }
152 unsigned int multi(void) { return _multi; }
153 unsigned int index(void) { return _index; }
154 bool done(void) { return _done; }
155 bool newkblock(void) { return _newkblock; }
156 };
157
158 // A working size: One of these needed, regardless of thread count. Divided according to window.
159 size_t get_a_working_size() const {
160 return ROUND_UP(sizeof(Toi) * _k_block * _Mround * _nbatches) * 2;
161 }
162
163 // B working size: 0, 1 or 3 of these needed depending on pretransposed and threading settings.
164 size_t get_b_working_size() const {
165 return ROUND_UP(sizeof(Toi) * _x_block * _k_block);
166 }
167
168 // C working size: One needed per thread.
169 size_t get_c_working_size() const {
170 return ROUND_UP(sizeof(Tri) * _x_block * strategy::out_height());
171 }
172
173 void execute_transpose(unsigned int m_start, unsigned int m_end, unsigned int n_start, unsigned int n_end, int threadid, int mthreadid, int nthreadid) {
174 UNUSED(mthreadid);
175
176 strategy strat(_ci);
177
178 /* Translate 'start' and 'end' into a position within the batches and rows. */
179 const unsigned int window_per_batch = _Mround / strategy::out_height();
180 unsigned int batch_0 = m_start / window_per_batch;
181 unsigned int batch_end = m_end / window_per_batch;
182
183 /* Compute the M values to operate on */
184 unsigned int m_0 = (m_start - (batch_0 * window_per_batch)) * strategy::out_height();
185 unsigned int m_max = (m_end - (batch_end * window_per_batch)) * strategy::out_height();
186
187 unsigned int n_0 = std::min(this->_Nsize, strategy::out_width() * n_start);
188 unsigned int n_max = std::min(this->_Nsize, strategy::out_width() * n_end);
189
190 blockwalker current(*this, n_0, n_max);
191
192 /* get workspace as int8_t */
193 assert(_working_space);
194 int8_t *working_space_bytes = reinterpret_cast<int8_t *>(_working_space);
195
196 auto c_panel_start = working_space_bytes;
197 auto a_panel_start = c_panel_start + get_c_working_size() * _maxthreads;
198 auto b_panel_start = a_panel_start + get_a_working_size() * _maxthreads;
199
200 auto c_panel = reinterpret_cast<Tri *>(c_panel_start + get_c_working_size() * threadid);
201 auto a_panel = reinterpret_cast<Toi *>(a_panel_start + get_a_working_size() * nthreadid);
202 auto b_panel = reinterpret_cast<Toi *>(b_panel_start + get_b_working_size() * threadid);
203
204
205 // newkblock() is always true on the first iteration, so this will be set properly on the first loop.
206
207 int kern_k = 0;
208 for (;!current.done();current.advance()) {
209 const int bblocks = iceildiv(current.xmax() - current.x0(), strategy::out_width());
210 /*
211 * The entirity of A^kblock is transpose upfront and computed against individual
212 * blocks of B (xblock)
213 *
214 * Therefore, we only need to retranspose when k_block progresses
215 */
216 if (current.newkblock()) {
217 for (unsigned int batch = batch_0; batch <= batch_end; batch++) {
218 unsigned int first_m = (batch == batch_0) ? m_0 : 0;
219 unsigned int last_m = (batch == batch_end) ? m_max : _Msize;
220
221 if (first_m >= last_m)
222 continue;
223
224 auto a_thread_panel_in = this->_Aptr
225 + (batch * this->_A_batch_stride)
226 + (current.multi() * this->_A_multi_stride);
227
228 auto a_thread_panel_out = a_panel + ((batch * _Mround + first_m) * _k_block);
229
230 strat.transforms.PrepareA(
231 a_thread_panel_out,
232 a_thread_panel_in,
233 this->_lda,
234 first_m,
235 last_m,
236 current.k0(),
237 current.kmax(),
238 _trA);
239 }
240
241 kern_k = iceildiv(current.kmax() - current.k0(), strategy::k_unroll());
242 kern_k *= strat.k_unroll();
243 }
244
245 auto *b_panel_in = this->_Bptr + (current.multi() * this->_B_multi_stride);
246
247 strat.transforms.PrepareB(
248 b_panel, //dst
249 b_panel_in, //src
250 this->_ldb,
251 current.x0(), //idx from
252 current.xmax(), //idx to
253 current.k0(),
254 current.kmax(),
255 _trB);
256
257 //Iterate over the batches
258 for (unsigned int batch = batch_0; batch <= batch_end; batch++) {
259 unsigned int first_m = (batch == batch_0) ? m_0 : 0;
260 unsigned int last_m = (batch == batch_end) ? m_max : _Msize;
261
262 if (first_m >= last_m)
263 continue;
264
265 const Toi *a_ptr = a_panel + (batch * _Mround + first_m) * _k_block;
266
267
268 //Iterate over the inerleaved rows of the packed A matrix
269 for (unsigned int y=first_m; y<last_m; y+=strategy::out_height()) {
270 unsigned int ymax = std::min(_Msize, y + strategy::out_height());
271
272 strat.kernel(a_ptr, b_panel, c_panel, 1, bblocks, kern_k);
273 a_ptr += (strategy::out_height() * kern_k);
274
275 const bool first_pass = current.k0()==0;
276 const bool last_pass = current.kmax()==_Ksize;
277
278 auto c_panel_out = this->_Cptr
279 + this->_C_batch_stride * batch
280 + this->_C_multi_stride * current.multi();
281
282 auto bias = (first_pass && this->_bias)
283 ? this->_bias + (current.multi() * this->_bias_multi_stride)
284 : nullptr;
285
286 auto act = last_pass ? _act : Activation();
287
288 strat.transforms.Merge(
289 c_panel_out,
290 c_panel,
291 this->_ldc,
292 y,
293 ymax,
294 current.x0(),
295 current.xmax(),
296 bias,
297 act,
298 !first_pass); //Append
299 }
300 }
301 }
302 }
303public:
304 GemmInterleaved2d(GemmInterleaved2d &) = delete;
305 GemmInterleaved2d & operator= (GemmInterleaved2d &) = delete;
306
307 /* Constructor */
308 /* Constructor */
309 GemmInterleaved2d(const GemmArgs &args)
310 : _ci(args._ci)
311 , _Msize(args._Msize)
312 , _Nsize(args._Nsize)
313 , _Ksize(args._Ksize)
314 , _nbatches(args._nbatches)
315 , _nmulti(args._nmulti)
316 , _trA(args._trA)
317 , _trB(args._trB)
318 , _act(args._act)
319 , _maxthreads(args._maxthreads)
320 , _nthreads(args._maxthreads)
321
322 // Work out the rounded size of M - needed for some buffers.
323 , _Mround_div ( iceildiv(_Msize, strategy::out_height()) )
324 , _Mround ( _Mround_div * strategy::out_height() )
325
326 , _Nround_div ( iceildiv(_Nsize, strategy::out_width()) )
327 , _Nround ( _Nround_div * strategy::out_width() )
328 {
329 const unsigned int L1_size = _ci->get_L1_cache_size();
330 const unsigned int L2_size = _ci->get_L2_cache_size();
331
332 assert(_maxthreads > 0);
333
334 // Work out blocking parameters, or override from provided GemmConfig
335 if (args._cfg && args._cfg->inner_block_size) {
336 _k_block = args._cfg->inner_block_size;
337 } else {
338 // k_block: Find out how much of the larger array can be loaded into half the cache.
339 // This should account for associative caches.
340 _k_block = (L1_size / 2) / (sizeof(Toi) * (std::max(strategy::out_width(), strategy::out_height())));
341
342 // Needs to be (at least a single) multiple of the K unroll level.
343 _k_block /= strategy::k_unroll();
344 _k_block = std::max(_k_block, 1U) * strategy::k_unroll();
345
346 // Now tune to presented problem size; this is how many blocks we need.
347 unsigned int num_k_blocks = iceildiv(_Ksize, _k_block);
348
349 // So divide the space equally into that many blocks.
350 _k_block = iceildiv(_Ksize, num_k_blocks);
351
352 // And round UP to the K unroll level required.
353 _k_block = iceildiv(_k_block, strategy::k_unroll());
354 _k_block *= strategy::k_unroll();
355 }
356
357 if (args._cfg && args._cfg->outer_block_size) {
358 _x_block = args._cfg->outer_block_size;
359 } else {
360 // x_block: Work out how many rows (of length k_block) will fit in the L2
361 // Don't allocate more than 90% of the L2 to allow for overheads, and subtract off the L1 contents.
362 _x_block = (((L2_size * 9) / 10) - (_k_block * sizeof(Toi) * (strategy::out_width() + strategy::out_height()))) /
363 (sizeof(Toi) * _k_block);
364
365 // Needs to be (at least a single) multiple of the kernel output width.
366 _x_block /= strategy::out_width();
367 _x_block = std::max(_x_block, 1U) * strategy::out_width();
368
369 // And tune to the presented problem size.
370 unsigned int num_x_blocks = iceildiv(_Nsize, _x_block);
371 _x_block = iceildiv(_Nsize, num_x_blocks);
372
373 _x_block = iceildiv(_x_block, strategy::out_width());
374 _x_block *= strategy::out_width();
375 }
376
377 // Work out the rounded size of M - needed for some buffers.
378 }
379
380 // Interface implementation - Compulsory functions
381 ndrange_t get_window_size() const override {
382 unsigned m = (_Mround / strategy::out_height()) * _nbatches;
383 unsigned n = _Nround_div;
384
385 return { m, n, 1u, 1u, 1u, 1u };
386 }
387
388 // set_nthreads: pass on to buffer manager to avoid it waiting for non-existant threads.
389 void set_nthreads(int nthreads) override {
390 _nthreads = std::min(nthreads, _maxthreads);
391 }
392
393 void execute(const ndcoord_t& work_range, const ndcoord_t& thread_locator, int threadid) override {
394 /*
395 * This particular GEMM implementation can only be broken up over the M & N
396 * dimensions, we inform the frame work of this limitation via the get_window_size function
397 */
398 assert(ndrange_popcount(work_range) <= 2);
399
400 const auto m_start = work_range.get_position(0);
401 const auto n_start = work_range.get_position(1);
402 const auto m_size = work_range.get_size(0);
403 const auto n_size = work_range.get_size(1);
404 const auto m_end = m_start + m_size;
405 const auto n_end = n_start + n_size;
406
407 const auto m_threadid = thread_locator.get_position(0);
408 const auto n_threadid = thread_locator.get_position(1);
409
410 execute_transpose(m_start, m_end, n_start, n_end, threadid, m_threadid, n_threadid);
411 }
412
413 std::size_t get_working_size()const override {
414 /*
415 * Because we do not know how schedular will break up
416 * the task, we need to ensure that alloc enough
417 * space to be able to handle the case where every thread
418 * is parallelised across B AND also every thrread is parallelised across A
419 *
420 * If we parallelise across A, then we only need one buffer of A and 64 buffers of B
421 * If we parallelise across B, then we only need 64 buffer of B and
422 */
423 return get_c_working_size() * _maxthreads
424 + get_a_working_size() * _maxthreads
425 + get_b_working_size() * _maxthreads
426 + 64; //to account for cacheline alignment
427 }
428
429
430 void set_working_space(void *working_space) override {
431 // Make sure everything ends up cache line aligned
432 int8_t *working_space_bytes = reinterpret_cast<int8_t *>(working_space);
433 intptr_t working_space_int = reinterpret_cast<intptr_t>(working_space);
434
435 size_t diff=0;
436
437 if (working_space_int & 0x3F) {
438 diff = 0x40 - (working_space_int & 0x3F);
439 }
440
441 working_space_bytes += diff;
442
443 _working_space = reinterpret_cast<void *>(working_space_bytes);
444 }
445
446 ~GemmInterleaved2d() override { }
447};
448
449} // namespace arm_gemm