blob: c2bd0bb88234819400ce44e5d0bf4541925df18f [file] [log] [blame]
Georgios Pinitas7cd26d42019-01-09 18:35:17 +00001/*
2 * Copyright (c) 2019 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 <assert.h>
27
28#include <algorithm>
29
30#include "arm_gemm.hpp"
Georgios Pinitas1d480652019-01-23 11:24:50 +000031#include "ndrange.hpp"
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000032#include "utils.hpp"
33
34#include "mergeresults.hpp"
35#include "transform.hpp"
36
37#ifdef CYCLE_PROFILING
38#include "profiler.hpp"
39#endif
40
41namespace arm_gemm {
42
43// Implementation of the GemmCommon abstract class.
44template<typename strategy, typename To, typename Tr>
45class GemmHybrid : public GemmCommon<To, Tr> {
46 typedef typename strategy::operand_type Toi;
47 typedef typename strategy::result_type Tri;
48
49 /* const properties set by constructor */
50 const CPUInfo * const _ci;
51
52 const unsigned int _Msize;
53 const unsigned int _Nsize;
54 const unsigned int _Ksize;
55
56 const unsigned int _nbatches;
57 const unsigned int _nmulti;
58
59 const bool _trB;
60
61 const Tr _beta;
62
63 /* Blocking info */
Georgios Pinitas1d480652019-01-23 11:24:50 +000064 const unsigned int _k_block;
65 const unsigned int _n_block;
66 const unsigned int _Mround;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000067
68 /* Pretransposed buffer. */
69 const Toi *_B_transposed=nullptr;
70
Georgios Pinitas1d480652019-01-23 11:24:50 +000071 const NDRange<4> _window_range;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000072
Georgios Pinitas1d480652019-01-23 11:24:50 +000073 static unsigned int compute_k_block(const GemmArgs<Tr> &args) {
74 if (args._cfg && args._cfg->inner_block_size) {
75 return args._cfg->inner_block_size;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000076 }
77
Georgios Pinitas1d480652019-01-23 11:24:50 +000078 const unsigned int L1_size = args._ci->get_L1_cache_size();
79
80 // k_block: Find out how much of the larger array can be loaded into half the cache.
81 // This should account for associative caches.
82 unsigned int k_block = (L1_size / 2) / (sizeof(Toi) * (std::max(strategy::out_width(), strategy::out_height())));
83
84 // Needs to be (at least a single) multiple of the K unroll level.
85 k_block /= strategy::k_unroll();
86 k_block = std::max(k_block, 1U) * strategy::k_unroll();
87
88 // Now tune to presented problem size; this is how many blocks we need.
89 unsigned int numk_blocks = iceildiv(args._Ksize, k_block);
90
91 // So divide the space equally into that many blocks.
92 k_block = iceildiv(args._Ksize, numk_blocks);
93
94 // And round UP to the K unroll level required.
95 k_block = roundup(k_block, strategy::k_unroll());
96
97 return k_block;
98 }
99
100 static unsigned int compute_n_block(const GemmArgs<Tr> &args) {
101 if (args._cfg && args._cfg->outer_block_size) {
102 return args._cfg->outer_block_size;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000103 }
104
Georgios Pinitas1d480652019-01-23 11:24:50 +0000105 const unsigned int k_block = compute_k_block(args);
106 const unsigned int L2_size = args._ci->get_L2_cache_size();
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000107
Georgios Pinitas1d480652019-01-23 11:24:50 +0000108 // n_block: Work out how many rows (of length k_block) will fit in the L2
109 // Don't allocate more than 90% of the L2 to allow for overheads, and subtract off the L1 contents.
110 unsigned int n_block = (((L2_size * 9) / 10) - (k_block * sizeof(Toi) * (strategy::out_width() + strategy::out_height()))) /
111 (sizeof(Toi) * k_block);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000112
Georgios Pinitas1d480652019-01-23 11:24:50 +0000113 // Needs to be (at least a single) multiple of the kernel output width.
114 n_block /= strategy::out_width();
115 n_block = std::max(n_block, 1U) * strategy::out_width();
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000116
Georgios Pinitas1d480652019-01-23 11:24:50 +0000117 // And tune to the presented problem size.
118 unsigned int numblocks = iceildiv(args._Nsize, n_block);
119 n_block = iceildiv(args._Nsize, numblocks);
120 n_block = roundup(n_block, strategy::out_width());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000121
Georgios Pinitas1d480652019-01-23 11:24:50 +0000122 return n_block;
123 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000124
125public:
126 GemmHybrid(GemmHybrid &) = delete;
127 GemmHybrid & operator= (GemmHybrid &) = delete;
128
129 /* Constructor */
130 GemmHybrid(const GemmArgs<Tr> &args)
Georgios Pinitas1d480652019-01-23 11:24:50 +0000131 : _ci(args._ci), _Msize(args._Msize), _Nsize(args._Nsize), _Ksize(args._Ksize),
132 _nbatches(args._nbatches), _nmulti(args._nmulti), _trB(args._trB), _beta(args._beta),
133 _k_block(compute_k_block(args)), _n_block(compute_n_block(args)),
134 _Mround(roundup(args._Msize, strategy::out_height())),
135 _window_range(iceildiv(args._Msize, strategy::out_height()), _nbatches, iceildiv(_Nsize, _n_block), _nmulti) { }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000136
137 // Interface implementation - Compulsory functions
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000138 unsigned int get_window_size() const override {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000139 return _window_range.total_size();
140 }
141
142 // This kernel can always be dynamically scheduled.
143 bool supports_dynamic_scheduling() const override {
144 return true;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000145 }
146
147 // Execute
148 void execute(unsigned int start, unsigned int end, int threadid) override {
149#ifdef CYCLE_PROFILING
150 profiler prof;
151#endif
152 strategy strat(_ci);
153
154 /* Make sure we've been set up correctly. */
155 assert(_B_transposed);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000156 static_assert(std::is_same<To, Toi>::value, "gemm_native: Operand types must be the same.");
157 static_assert(std::is_same<Tr, Tri>::value, "gemm_native: Result types must be the same.");
158
Georgios Pinitas1d480652019-01-23 11:24:50 +0000159 /* For now, each work item implies all the K for a given output
160 * pixel (so we don't need to synchronize access to the output
161 * array). So separate the loop over K blocks here. */
162 for (unsigned int k0=0; k0<_Ksize; k0+=_k_block) {
163 unsigned int kmax = std::min(k0 + _k_block, _Ksize);
164 unsigned int kern_k = roundup(kmax-k0, strategy::k_unroll());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000165
Georgios Pinitas1d480652019-01-23 11:24:50 +0000166 auto p = _window_range.iterator(start, end);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000167
Georgios Pinitas1d480652019-01-23 11:24:50 +0000168 if (p.done()) {
169 return;
170 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000171
Georgios Pinitas1d480652019-01-23 11:24:50 +0000172 do {
173 const unsigned int m_start = p.dim(0) * strategy::out_height();
174 const unsigned int m_end = std::min(p.dim0_max() * strategy::out_height(), _Msize);
175 const unsigned int batch = p.dim(1);
176 const unsigned int n0 = p.dim(2) * _n_block;
177 const unsigned int nmax = std::min(n0 + _n_block, _Nsize);
178 const unsigned int multi = p.dim(3);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000179
Georgios Pinitas1d480652019-01-23 11:24:50 +0000180 const Toi *b_panel = _B_transposed +
181 (multi * roundup(_Nsize, strategy::out_width()) * roundup(_Ksize, strategy::k_unroll())) +
182 (k0 * roundup(_Nsize, strategy::out_width())) +
183 (n0 * kern_k);
184
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000185#ifdef CYCLE_PROFILING
Georgios Pinitas1d480652019-01-23 11:24:50 +0000186 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 +0000187#endif
188
Georgios Pinitas1d480652019-01-23 11:24:50 +0000189 strat.kernel(this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (m_start * this->_lda) + k0, this->_lda,
190 b_panel,
191 this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (m_start * this->_ldc) + n0, this->_ldc,
192 (k0 == 0) ? _beta : static_cast<Tr>(1),
193 (m_end - m_start), (nmax - n0), kern_k);
194 } while (p.next_dim1());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000195 }
196 }
197
198 // Interface implementation - pretransposed
199 bool B_is_pretransposed() const override {
200 return true;
201 }
202
203 bool B_pretranspose_required() const override {
204 return (_B_transposed==nullptr);
205 }
206
207 size_t get_B_pretransposed_array_size() const override {
Georgios Pinitas1d480652019-01-23 11:24:50 +0000208 return roundup(_Nsize, strategy::out_width()) * roundup(_Ksize, strategy::k_unroll()) * _nmulti * sizeof(Toi);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000209 }
210
Georgios Pinitas1d480652019-01-23 11:24:50 +0000211 using GemmCommon<To, Tr>::pretranspose_B_array;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000212 void pretranspose_B_array(void *in_buffer, const To *B, const int ldb, const int B_multi_stride) override {
213 Toi *buffer = reinterpret_cast<Toi *>(in_buffer);
214 _B_transposed = buffer;
215 strategy strat(_ci);
216
Georgios Pinitas1d480652019-01-23 11:24:50 +0000217 for (unsigned int multi=0; multi<_nmulti; multi++) {
218 for (unsigned int k0=0; k0<_Ksize; k0+=_k_block) {
219 const unsigned int kmax = std::min(k0 + _k_block, _Ksize);
220 const unsigned int k_size = roundup(kmax-k0, strategy::k_unroll());
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000221
Georgios Pinitas1d480652019-01-23 11:24:50 +0000222 for (unsigned int x0=0; x0<_Nsize; x0+=_n_block) {
223 const unsigned int xmax = std::min(x0+_n_block, _Nsize);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000224
Georgios Pinitas1d480652019-01-23 11:24:50 +0000225 const unsigned int size = roundup(xmax-x0, strategy::out_width()) * k_size;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000226
Georgios Pinitas1d480652019-01-23 11:24:50 +0000227 strat.transforms.PrepareB( buffer, B + (multi * B_multi_stride), ldb,
228 x0, xmax, k0, kmax, _trB);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000229
Georgios Pinitas1d480652019-01-23 11:24:50 +0000230 buffer += size;
231 }
232 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000233 }
234 }
235
236 void set_pretransposed_B_data(void *in_buffer) override {
237 _B_transposed = reinterpret_cast<Toi *>(in_buffer);
238 }
239};
240
241} // namespace arm_gemm