blob: 6897e64d4b2b206145457441f1caf53eee1b567a [file] [log] [blame]
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001/*
Joseph Dobson6f8b17d2020-02-11 19:32:11 +00002 * Copyright (c) 2017-2020 ARM Limited.
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01003 *
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 Pinitascfa2bba2019-06-27 17:00:52 +010031#include "utils.hpp"
32
Vincent ABRIOU267e65d2020-05-27 16:26:46 +020033#include "arm_compute/core/NEON/kernels/arm_gemm/ndrange.hpp"
34
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010035#include "mergeresults.hpp"
36#include "transform.hpp"
37
38#ifdef CYCLE_PROFILING
39#include "profiler.hpp"
40#endif
41
42namespace arm_gemm {
43
44// Implementation of the GemmCommon abstract class.
45template<typename strategy, typename To, typename Tr>
46class GemmHybridQuantized : public GemmCommon<To, Tr> {
47 typedef typename strategy::operand_type Toi;
48 typedef typename strategy::result_type Tri;
49
50 /* const properties set by constructor */
51 const CPUInfo * const _ci;
52
53 const unsigned int _Msize;
54 const unsigned int _Nsize;
55 const unsigned int _Ksize;
56
57 const unsigned int _nbatches;
58 const unsigned int _nmulti;
59
60 const bool _trB;
61
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010062 /* Blocking info */
63 const unsigned int _k_block;
64 const unsigned int _n_block;
65 const unsigned int _Mround;
66
67 /* Pretransposed buffer. */
68 const Toi *_B_transposed=nullptr;
69
70 const NDRange<4> _window_range;
71
Michalis Spyrou71ac9032019-11-14 14:31:44 +000072 Requantize32 _qp;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010073 int32_t *row_bias = nullptr;
74 int32_t *col_bias = nullptr;
75
76 void *working_space = nullptr;
77
78 unsigned int _nthreads;
79
80 unsigned int get_col_sum_size() const {
81 return _Nsize * _nmulti * sizeof(int32_t);
82 }
83
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010084 static unsigned int compute_k_block(const GemmArgs &args) {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010085 // We don't support K blocks as we only temporarily store 32 bit results.
86 return args._Ksize;
87
88 if (args._cfg && args._cfg->inner_block_size) {
89 return args._cfg->inner_block_size;
90 }
91
92 const unsigned int L1_size = args._ci->get_L1_cache_size();
93
94 // k_block: Find out how much of the larger array can be loaded into half the cache.
95 // This should account for associative caches.
96 unsigned int k_block = (L1_size / 2) / (sizeof(Toi) * (std::max(strategy::out_width(), strategy::out_height())));
97
98 // Needs to be (at least a single) multiple of the K unroll level.
99 k_block /= strategy::k_unroll();
100 k_block = std::max(k_block, 1U) * strategy::k_unroll();
101
102 // Now tune to presented problem size; this is how many blocks we need.
103 unsigned int numk_blocks = iceildiv(args._Ksize, k_block);
104
105 // So divide the space equally into that many blocks.
106 k_block = iceildiv(args._Ksize, numk_blocks);
107
108 // And round UP to the K unroll level required.
109 k_block = roundup(k_block, strategy::k_unroll());
110
111 return k_block;
112 }
113
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100114 static unsigned int compute_n_block(const GemmArgs &args) {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100115 if (args._cfg && args._cfg->outer_block_size) {
116 return args._cfg->outer_block_size;
117 }
118
119 const unsigned int k_block = compute_k_block(args);
120 const unsigned int L2_size = args._ci->get_L2_cache_size();
121
122 // n_block: Work out how many rows (of length k_block) will fit in the L2
123 // Don't allocate more than 90% of the L2 to allow for overheads, and subtract off the L1 contents.
124 unsigned int n_block = (((L2_size * 9) / 10) - (k_block * sizeof(Toi) * (strategy::out_width() + strategy::out_height()))) /
125 (sizeof(Toi) * k_block);
126
127 // Needs to be (at least a single) multiple of the kernel output width.
128 n_block /= strategy::out_width();
129 n_block = std::max(n_block, 1U) * strategy::out_width();
130
131 // And tune to the presented problem size.
132 unsigned int numblocks = iceildiv(args._Nsize, n_block);
133 n_block = iceildiv(args._Nsize, numblocks);
134 n_block = roundup(n_block, strategy::out_width());
135
136 return n_block;
137 }
138
139public:
140 GemmHybridQuantized(GemmHybridQuantized &) = delete;
141 GemmHybridQuantized & operator= (GemmHybridQuantized &) = delete;
142
143 /* Constructor */
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000144 GemmHybridQuantized(const GemmArgs &args, const Requantize32 &qp)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100145 : _ci(args._ci), _Msize(args._Msize), _Nsize(args._Nsize), _Ksize(args._Ksize),
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100146 _nbatches(args._nbatches), _nmulti(args._nmulti), _trB(args._trB),
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100147 _k_block(compute_k_block(args)), _n_block(compute_n_block(args)),
148 _Mround(roundup(args._Msize, strategy::out_height())),
149 _window_range(iceildiv(args._Msize, strategy::out_height()), _nbatches, iceildiv(_Nsize, _n_block), _nmulti),
150 _qp (qp), _nthreads(args._maxthreads) { }
151
152 // Interface implementation - Compulsory functions
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000153 ndrange_t get_window_size() const override {
154 return { _window_range.total_size(), 1u, 1u, 1u, 1u, 1u };
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100155 }
156
157 // This kernel can always be dynamically scheduled.
158 bool supports_dynamic_scheduling() const override {
159 return true;
160 }
161
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000162 void execute_1d(unsigned int start, unsigned int end, int threadid) {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100163#ifdef CYCLE_PROFILING
164 profiler prof;
165#endif
166 strategy strat(_ci);
167
168 uintptr_t working_int = reinterpret_cast<uintptr_t>(working_space);
169
170 Tri *result_buffer = reinterpret_cast<Tri *>(working_int + (threadid * strategy::out_height() * _Nsize * sizeof(Tri)));
171
172 /* Make sure we've been set up correctly. */
173 assert(_B_transposed);
174 static_assert(std::is_same<To, Toi>::value, "gemm_native: Operand types must be the same.");
175
176 /* For now, each work item implies all the K for a given output
177 * pixel (so we don't need to synchronize access to the output
178 * array). So separate the loop over K blocks here. */
179 for (unsigned int k0=0; k0<_Ksize; k0+=_k_block) {
180 unsigned int kmax = std::min(k0 + _k_block, _Ksize);
181 unsigned int kern_k = roundup(kmax-k0, strategy::k_unroll());
182
183 auto p = _window_range.iterator(start, end);
184
185 if (p.done()) {
186 return;
187 }
188
189 do {
190 const unsigned int m_start = p.dim(0) * strategy::out_height();
191 const unsigned int m_end = std::min((p.dim(0) + 1) * strategy::out_height(), _Msize);
192 const unsigned int batch = p.dim(1);
193 const unsigned int n0 = p.dim(2) * _n_block;
194 const unsigned int nmax = std::min(n0 + _n_block, _Nsize);
195 const unsigned int multi = p.dim(3);
196
197 int32_t local_row_sums[strategy::out_height()];
198
199 const Toi *b_panel = _B_transposed +
200 (multi * roundup(_Nsize, strategy::out_width()) * roundup(_Ksize, strategy::k_unroll())) +
201 (k0 * roundup(_Nsize, strategy::out_width())) +
202 (n0 * kern_k);
203
204 {
205#ifdef CYCLE_PROFILING
206 auto p = prof.ScopedProfiler(PROFILE_KERNEL, (m_end - m_start) * kern_k * roundup(nmax-n0, strategy::out_width()));
207#endif
208 strat.kernel(this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (m_start * this->_lda) + k0, this->_lda,
209 b_panel,
Michalis Spyrou3e183d92019-08-23 15:31:08 +0100210 result_buffer, (nmax-n0),
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100211 (m_end - m_start), (nmax - n0), kern_k,
212 nullptr, Activation(), false);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100213 }
214
215 {
216#ifdef CYCLE_PROFILING
217 auto p = prof.ScopedProfiler(PROFILE_ROWSUMS, (m_end - m_start) * _Ksize);
218#endif
219 compute_row_sums(_qp, _Ksize, (m_end - m_start),
220 this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (m_start * this->_lda), this->_lda,
221 local_row_sums);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100222 }
223
224 {
225#ifdef CYCLE_PROFILING
226 auto p = prof.ScopedProfiler(PROFILE_QUANTIZE, (m_end - m_start) * _Nsize);
227#endif
228
229 requantize_block_32(_qp, (nmax - n0), (m_end - m_start), result_buffer, (nmax - n0),
230 this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (m_start * this->_ldc) + n0, this->_ldc,
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100231 local_row_sums, col_bias + (multi * _Nsize) + n0);
232 }
233 } while (p.next_dim0());
234 }
235 }
236
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000237 // Execute
238 void execute(const ndcoord_t& work_range, const ndcoord_t& thread_locator, int threadid) override {
239 UNUSED(thread_locator);
240
241 const auto start = work_range.get_position(0);
242 const auto size = work_range.get_size(0);
243 const auto stop = start + size;
244
245 execute_1d(start, stop, threadid);
246 }
247
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100248 // Working space needed for intermediate result buffers.
249 size_t get_working_size() const override {
250 return (_nthreads * strategy::out_height() * _Nsize * sizeof(Tri));
251 }
252
253 void set_working_space(void *buffer) override {
254 working_space = buffer;
255 }
256
257 // Interface implementation - pretransposed
258 bool B_is_pretransposed() const override {
259 return true;
260 }
261
262 bool B_pretranspose_required() const override {
263 return (_B_transposed==nullptr);
264 }
265
266 size_t get_B_pretransposed_array_size() const override {
267 return get_col_sum_size() + (roundup(_Nsize, strategy::out_width()) * roundup(_Ksize, strategy::k_unroll()) * _nmulti * sizeof(Toi));
268 }
269
270 void pretranspose_B_array(void *in_buffer, const To *B, const int ldb, const int B_multi_stride) override {
271 col_bias = reinterpret_cast<int32_t *>(in_buffer);
272
273 for (unsigned int i=0; i<_nmulti; i++) {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100274 compute_col_sums(_qp, _Nsize, _Ksize, B + (i * B_multi_stride), ldb, col_bias + (i * _Nsize), _Ksize, i, 0);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100275 }
276
277 uintptr_t buffer_int = reinterpret_cast<uintptr_t>(in_buffer);
278 Toi *buffer = reinterpret_cast<Toi *>(buffer_int + get_col_sum_size());
279 _B_transposed = buffer;
280 strategy strat(_ci);
281
282 for (unsigned int multi=0; multi<_nmulti; multi++) {
283 for (unsigned int k0=0; k0<_Ksize; k0+=_k_block) {
284 const unsigned int kmax = std::min(k0 + _k_block, _Ksize);
285 const unsigned int k_size = roundup(kmax-k0, strategy::k_unroll());
286
287 for (unsigned int x0=0; x0<_Nsize; x0+=_n_block) {
288 const unsigned int xmax = std::min(x0+_n_block, _Nsize);
289
290 const unsigned int size = roundup(xmax-x0, strategy::out_width()) * k_size;
291
292 strat.transforms.PrepareB( buffer, B + (multi * B_multi_stride), ldb,
293 x0, xmax, k0, kmax, _trB);
294
295 buffer += size;
296 }
297 }
298 }
299 }
300
301 void set_pretransposed_B_data(void *in_buffer) override {
302 uintptr_t buffer_int = reinterpret_cast<uintptr_t>(in_buffer);
303 _B_transposed = reinterpret_cast<Toi *>(buffer_int + get_col_sum_size());
304 col_bias = reinterpret_cast<int32_t *>(in_buffer);
305 }
306
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100307 void set_quantized_bias(const int32_t *bias, size_t bias_multi_stride) override {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100308 _qp.bias = bias;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100309 _qp.bias_multi_stride = bias_multi_stride;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100310 }
311};
312
313} // namespace arm_gemm