blob: aeeed26702452e18989404e93a64f865283b8189 [file] [log] [blame]
Georgios Pinitas7cd26d42019-01-09 18:35:17 +00001/*
Joseph Dobson6f8b17d2020-02-11 19:32:11 +00002 * 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
26#include <assert.h>
27
28#include <algorithm>
29
30#include "arm_gemm.hpp"
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010031#include "bias_adder.hpp"
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000032#include "utils.hpp"
33
Vincent ABRIOU04c8e632020-05-27 16:26:46 +020034#include "arm_compute/core/NEON/kernels/arm_gemm/ndrange.hpp"
35
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000036#include "mergeresults.hpp"
37#include "transform.hpp"
38
39#ifdef CYCLE_PROFILING
40#include "profiler.hpp"
41#endif
42
43namespace arm_gemm {
44
45// Implementation of the GemmCommon abstract class.
46template<typename strategy, typename To, typename Tr>
47class GemmHybrid : public GemmCommon<To, Tr> {
48 typedef typename strategy::operand_type Toi;
49 typedef typename strategy::result_type Tri;
50
51 /* const properties set by constructor */
52 const CPUInfo * const _ci;
53
54 const unsigned int _Msize;
55 const unsigned int _Nsize;
56 const unsigned int _Ksize;
57
58 const unsigned int _nbatches;
59 const unsigned int _nmulti;
60
61 const bool _trB;
62
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010063 const Activation _act;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000064
65 /* Blocking info */
Georgios Pinitas1d480652019-01-23 11:24:50 +000066 const unsigned int _k_block;
67 const unsigned int _n_block;
68 const unsigned int _Mround;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000069
70 /* Pretransposed buffer. */
71 const Toi *_B_transposed=nullptr;
72
Georgios Pinitas1d480652019-01-23 11:24:50 +000073 const NDRange<4> _window_range;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000074
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010075 static unsigned int compute_k_block(const GemmArgs &args) {
76 // Some kernels don't support append mode - these can't do K blocking at all.
77 if (!strategy::supports_append()) {
78 return args._Ksize;
79 }
80
Georgios Pinitas1d480652019-01-23 11:24:50 +000081 if (args._cfg && args._cfg->inner_block_size) {
82 return args._cfg->inner_block_size;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000083 }
84
Georgios Pinitas1d480652019-01-23 11:24:50 +000085 const unsigned int L1_size = args._ci->get_L1_cache_size();
86
87 // k_block: Find out how much of the larger array can be loaded into half the cache.
88 // This should account for associative caches.
89 unsigned int k_block = (L1_size / 2) / (sizeof(Toi) * (std::max(strategy::out_width(), strategy::out_height())));
90
91 // Needs to be (at least a single) multiple of the K unroll level.
92 k_block /= strategy::k_unroll();
93 k_block = std::max(k_block, 1U) * strategy::k_unroll();
94
95 // Now tune to presented problem size; this is how many blocks we need.
96 unsigned int numk_blocks = iceildiv(args._Ksize, k_block);
97
98 // So divide the space equally into that many blocks.
99 k_block = iceildiv(args._Ksize, numk_blocks);
100
101 // And round UP to the K unroll level required.
102 k_block = roundup(k_block, strategy::k_unroll());
103
104 return k_block;
105 }
106
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100107 static unsigned int compute_n_block(const GemmArgs &args) {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000108 if (args._cfg && args._cfg->outer_block_size) {
109 return args._cfg->outer_block_size;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000110 }
111
Georgios Pinitas1d480652019-01-23 11:24:50 +0000112 const unsigned int k_block = compute_k_block(args);
113 const unsigned int L2_size = args._ci->get_L2_cache_size();
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000114
Georgios Pinitas1d480652019-01-23 11:24:50 +0000115 // n_block: Work out how many rows (of length k_block) will fit in the L2
116 // Don't allocate more than 90% of the L2 to allow for overheads, and subtract off the L1 contents.
117 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 +0100118 (sizeof(Toi) * k_block);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000119
Georgios Pinitas1d480652019-01-23 11:24:50 +0000120 // Needs to be (at least a single) multiple of the kernel output width.
121 n_block /= strategy::out_width();
122 n_block = std::max(n_block, 1U) * strategy::out_width();
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000123
Georgios Pinitas1d480652019-01-23 11:24:50 +0000124 // And tune to the presented problem size.
125 unsigned int numblocks = iceildiv(args._Nsize, n_block);
126 n_block = iceildiv(args._Nsize, numblocks);
127 n_block = roundup(n_block, strategy::out_width());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000128
Georgios Pinitas1d480652019-01-23 11:24:50 +0000129 return n_block;
130 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000131
132public:
133 GemmHybrid(GemmHybrid &) = delete;
134 GemmHybrid & operator= (GemmHybrid &) = delete;
135
136 /* Constructor */
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100137 GemmHybrid(const GemmArgs &args)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100138 : _ci(args._ci), _Msize(args._Msize), _Nsize(args._Nsize), _Ksize(args._Ksize),
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100139 _nbatches(args._nbatches), _nmulti(args._nmulti), _trB(args._trB),
140 _act(args._act),
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100141 _k_block(compute_k_block(args)), _n_block(compute_n_block(args)),
142 _Mround(roundup(args._Msize, strategy::out_height())),
143 _window_range(iceildiv(args._Msize, strategy::out_height()), _nbatches, iceildiv(_Nsize, _n_block), _nmulti) { }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000144
145 // Interface implementation - Compulsory functions
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000146 ndrange_t get_window_size() const override {
147 return { _window_range.total_size(), 1u, 1u, 1u, 1u, 1u };
Georgios Pinitas1d480652019-01-23 11:24:50 +0000148 }
149
150 // This kernel can always be dynamically scheduled.
151 bool supports_dynamic_scheduling() const override {
152 return true;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000153 }
154
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000155 void execute_1d(unsigned int start, unsigned int end, int threadid) {
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100156 UNUSED(threadid);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000157#ifdef CYCLE_PROFILING
158 profiler prof;
159#endif
160 strategy strat(_ci);
161
162 /* Make sure we've been set up correctly. */
163 assert(_B_transposed);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000164 static_assert(std::is_same<To, Toi>::value, "gemm_native: Operand types must be the same.");
165 static_assert(std::is_same<Tr, Tri>::value, "gemm_native: Result types must be the same.");
166
Georgios Pinitas1d480652019-01-23 11:24:50 +0000167 /* For now, each work item implies all the K for a given output
168 * pixel (so we don't need to synchronize access to the output
169 * array). So separate the loop over K blocks here. */
170 for (unsigned int k0=0; k0<_Ksize; k0+=_k_block) {
171 unsigned int kmax = std::min(k0 + _k_block, _Ksize);
172 unsigned int kern_k = roundup(kmax-k0, strategy::k_unroll());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000173
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100174 const bool first_pass = (k0 == 0);
175 const bool last_pass = (kmax == _Ksize);
176
Georgios Pinitas1d480652019-01-23 11:24:50 +0000177 auto p = _window_range.iterator(start, end);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000178
Georgios Pinitas1d480652019-01-23 11:24:50 +0000179 if (p.done()) {
180 return;
181 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000182
Georgios Pinitas1d480652019-01-23 11:24:50 +0000183 do {
184 const unsigned int m_start = p.dim(0) * strategy::out_height();
185 const unsigned int m_end = std::min(p.dim0_max() * strategy::out_height(), _Msize);
186 const unsigned int batch = p.dim(1);
187 const unsigned int n0 = p.dim(2) * _n_block;
188 const unsigned int nmax = std::min(n0 + _n_block, _Nsize);
189 const unsigned int multi = p.dim(3);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000190
Georgios Pinitas1d480652019-01-23 11:24:50 +0000191 const Toi *b_panel = _B_transposed +
192 (multi * roundup(_Nsize, strategy::out_width()) * roundup(_Ksize, strategy::k_unroll())) +
193 (k0 * roundup(_Nsize, strategy::out_width())) +
194 (n0 * kern_k);
195
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000196#ifdef CYCLE_PROFILING
Georgios Pinitas1d480652019-01-23 11:24:50 +0000197 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (m_end - m_start) * kern_k * roundup(nmax-n0, strategy::out_width()));
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000198#endif
199
Georgios Pinitas1d480652019-01-23 11:24:50 +0000200 strat.kernel(this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (m_start * this->_lda) + k0, this->_lda,
201 b_panel,
202 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 +0100203 (m_end - m_start), (nmax - n0), kmax-k0,
204 (strategy::supports_bias() && first_pass && this->_bias) ? this->_bias + (multi * this->_bias_multi_stride) + n0 : nullptr,
205 last_pass ? _act : Activation(), !first_pass);
206
207 // Add bias externally if needed
208 if (!strategy::supports_bias() && this->_bias && first_pass) {
209 bias_adder(this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (m_start * this->_ldc) + n0, this->_ldc,
210 this->_bias + (multi * this->_bias_multi_stride) + n0,
211 (m_end - m_start), (nmax - n0));
212 }
213
Georgios Pinitas1d480652019-01-23 11:24:50 +0000214 } while (p.next_dim1());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000215 }
216 }
217
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000218 // Execute
219 void execute(const ndcoord_t& work_range, const ndcoord_t& thread_locator, int threadid) override {
220 UNUSED(thread_locator);
221
222 const auto start = work_range.get_position(0);
223 const auto size = work_range.get_size(0);
224 const auto stop = start + size;
225
226 execute_1d(start, stop, threadid);
227 }
228
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000229 // Interface implementation - pretransposed
230 bool B_is_pretransposed() const override {
231 return true;
232 }
233
234 bool B_pretranspose_required() const override {
235 return (_B_transposed==nullptr);
236 }
237
238 size_t get_B_pretransposed_array_size() const override {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000239 return roundup(_Nsize, strategy::out_width()) * roundup(_Ksize, strategy::k_unroll()) * _nmulti * sizeof(Toi);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000240 }
241
242 void pretranspose_B_array(void *in_buffer, const To *B, const int ldb, const int B_multi_stride) override {
243 Toi *buffer = reinterpret_cast<Toi *>(in_buffer);
244 _B_transposed = buffer;
245 strategy strat(_ci);
246
Georgios Pinitas1d480652019-01-23 11:24:50 +0000247 for (unsigned int multi=0; multi<_nmulti; multi++) {
248 for (unsigned int k0=0; k0<_Ksize; k0+=_k_block) {
249 const unsigned int kmax = std::min(k0 + _k_block, _Ksize);
250 const unsigned int k_size = roundup(kmax-k0, strategy::k_unroll());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000251
Georgios Pinitas1d480652019-01-23 11:24:50 +0000252 for (unsigned int x0=0; x0<_Nsize; x0+=_n_block) {
253 const unsigned int xmax = std::min(x0+_n_block, _Nsize);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000254
Georgios Pinitas1d480652019-01-23 11:24:50 +0000255 const unsigned int size = roundup(xmax-x0, strategy::out_width()) * k_size;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000256
Georgios Pinitas1d480652019-01-23 11:24:50 +0000257 strat.transforms.PrepareB( buffer, B + (multi * B_multi_stride), ldb,
258 x0, xmax, k0, kmax, _trB);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000259
Georgios Pinitas1d480652019-01-23 11:24:50 +0000260 buffer += size;
261 }
262 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000263 }
264 }
265
266 void set_pretransposed_B_data(void *in_buffer) override {
267 _B_transposed = reinterpret_cast<Toi *>(in_buffer);
268 }
269};
270
271} // namespace arm_gemm