blob: 828011d019446ff299534f4f7d099cef56e19c0c [file] [log] [blame]
Gian Marco Iodiceab182122017-10-09 15:05:40 +01001/*
Isabella Gottardie6630e42018-01-18 15:50:39 +00002 * Copyright (c) 2017-2018 ARM Limited.
Gian Marco Iodiceab182122017-10-09 15:05:40 +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#include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010030#include "arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h"
31#include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010032#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Validate.h"
Isabella Gottardie6630e42018-01-18 15:50:39 +000035#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010036#include "arm_compute/runtime/NEON/NEScheduler.h"
37#include "arm_compute/runtime/TensorAllocator.h"
38#include "support/ToolchainSupport.h"
39
40using namespace arm_compute;
Isabella Gottardie6630e42018-01-18 15:50:39 +000041using namespace arm_compute::misc::shape_calculator;
Gian Marco Iodiceab182122017-10-09 15:05:40 +010042
43NEGEMMLowpMatrixMultiplyCore::NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +010044 : _memory_group(memory_manager), _asm_glue(memory_manager), _mm_kernel(nullptr), _mtx_a_reshape_kernel(nullptr), _mtx_b_reshape_kernel(nullptr), _mtx_a_reduction_kernel(), _mtx_b_reduction_kernel(),
45 _offset_contribution_kernel(), _vector_sum_col(), _vector_sum_row(), _tmp_a(), _tmp_b(), _original_b(nullptr), _a_offset(0), _b_offset(0), _run_vector_matrix_multiplication(false),
46 _dot_product_path(false), _reshape_b_only_on_first_run(false), _is_prepared(false)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010047{
48}
49
Chunosov5124be52017-11-22 20:42:13 +070050void NEGEMMLowpMatrixMultiplyCore::configure(const ITensor *a, const ITensor *b, ITensor *output, const GEMMInfo &gemm_info)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010051{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000052 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Chunosov5124be52017-11-22 20:42:13 +070053 ARM_COMPUTE_ERROR_THROW_ON(NEGEMMLowpMatrixMultiplyCore::validate(a->info(), b->info(), output->info(), gemm_info));
Gian Marco Iodiceab182122017-10-09 15:05:40 +010054
Georgios Pinitas72219332018-06-05 14:56:06 +010055 // Clear state
Anthony Barbier71d9b572018-07-06 17:05:59 +010056 _mtx_a_reshape_kernel = nullptr;
57 _mtx_b_reshape_kernel = nullptr;
Georgios Pinitas72219332018-06-05 14:56:06 +010058
59 // Set internal variables
Gian Marcoc7f9b892017-11-30 14:31:13 +000060 _a_offset = a->info()->quantization_info().offset;
61 _b_offset = b->info()->quantization_info().offset;
62 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Giorgio Arenabb54e4e2018-04-05 17:20:34 +010063 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Georgios Pinitas72219332018-06-05 14:56:06 +010064 _is_prepared = false;
65 _original_b = b;
Gian Marcoe75a02b2017-11-08 12:24:09 +000066
Pablo Telloeb82fd22018-02-23 13:43:50 +000067#ifdef __aarch64__
68 switch(a->info()->data_type())
Gian Marco Iodiceab182122017-10-09 15:05:40 +010069 {
Pablo Tello66c656a2018-03-15 10:34:58 +000070 case DataType::QASYMM8:
Pablo Telloeb82fd22018-02-23 13:43:50 +000071 case DataType::U8:
Anthony Barbiereaefd002018-07-20 17:49:35 +010072 case DataType::S8:
Pablo Telloeb82fd22018-02-23 13:43:50 +000073 {
Anthony Barbiereaefd002018-07-20 17:49:35 +010074 _asm_glue.configure(a, b, output, 1.f, 0.f, _reshape_b_only_on_first_run);
75 _dot_product_path = _asm_glue.is_configured();
Pablo Telloeb82fd22018-02-23 13:43:50 +000076 break;
77 }
78 default:
79 {
80 ARM_COMPUTE_ERROR("Datatype not supported");
81 break;
82 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +010083 }
Pablo Telloeb82fd22018-02-23 13:43:50 +000084#endif /* __aarch64__ */
85 if(!_dot_product_path)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010086 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000087 if(_run_vector_matrix_multiplication)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010088 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000089 // Configure matrix multiply kernel
90 {
91 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
92 k->configure(a, b, output);
93 _mm_kernel = std::move(k);
94 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +010095 }
Gian Marcoc7f9b892017-11-30 14:31:13 +000096 else
Gian Marco Iodiceab182122017-10-09 15:05:40 +010097 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000098 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
Isabella Gottardie6630e42018-01-18 15:50:39 +000099 TensorInfo info_a(compute_interleaved_shape(*a->info()), 1, a->info()->data_type());
Gian Marcoc7f9b892017-11-30 14:31:13 +0000100 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
Isabella Gottardie6630e42018-01-18 15:50:39 +0000101 TensorInfo info_b(compute_transpose1xW_shape(*b->info()), 1, b->info()->data_type());
Gian Marcoc7f9b892017-11-30 14:31:13 +0000102 _tmp_a.allocator()->init(info_a);
103 _tmp_b.allocator()->init(info_b);
104 _memory_group.manage(&_tmp_a);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100105 if(!_reshape_b_only_on_first_run)
106 {
107 _memory_group.manage(&_tmp_b);
108 }
Gian Marcoc7f9b892017-11-30 14:31:13 +0000109
110 // Configure interleave kernel
111 {
112 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
113 k->configure(a, &_tmp_a);
114 _mtx_a_reshape_kernel = std::move(k);
115 }
116
117 // Configure transpose kernel
118 {
119 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
120 k->configure(b, &_tmp_b);
121 _mtx_b_reshape_kernel = std::move(k);
122 }
123
124 // Configure matrix multiply kernel
125 {
126 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
127 k->configure(&_tmp_a, &_tmp_b, output);
128 _mm_kernel = std::move(k);
129 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100130 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000131 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100132
Gian Marcoe75a02b2017-11-08 12:24:09 +0000133 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
134 if(_a_offset != 0)
135 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000136 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
137
Gian Marcoe75a02b2017-11-08 12:24:09 +0000138 _vector_sum_col.allocator()->init(info_vector_sum_col);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100139 if(!_reshape_b_only_on_first_run)
140 {
141 _memory_group.manage(&_vector_sum_col);
142 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000143
144 // Configure Matrix B reduction kernel
145 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, a->info()->dimension(0), false);
146 }
147
148 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
149 if(_b_offset != 0)
150 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000151 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a->info()), 1, DataType::S32);
152
Gian Marcoe75a02b2017-11-08 12:24:09 +0000153 _vector_sum_row.allocator()->init(info_vector_sum_row);
154 _memory_group.manage(&_vector_sum_row);
155
156 // Configure matrix A reduction kernel
157 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row, a->info()->dimension(0), false);
158 }
159
160 // Configure offset contribution kernel
161 _offset_contribution_kernel.configure(output, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, a->info()->dimension(0), _a_offset, _b_offset);
162
163 // Allocate tensors
Gian Marcoc7f9b892017-11-30 14:31:13 +0000164 if(!_dot_product_path && !_run_vector_matrix_multiplication)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000165 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000166 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100167 if(!_reshape_b_only_on_first_run)
168 {
169 _tmp_b.allocator()->allocate();
170 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000171 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000172
Georgios Pinitas72219332018-06-05 14:56:06 +0100173 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000174 {
175 _vector_sum_col.allocator()->allocate();
176 }
177
178 if(_b_offset != 0)
179 {
180 _vector_sum_row.allocator()->allocate();
181 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100182}
183
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000184Status NEGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *output, const GEMMInfo &gemm_info)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000185{
186 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
187 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
188 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
189 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
190 "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
191 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(1) != (output)->dimension(1),
192 "The output matrix must have the same number of rows as the matrix A");
193 ARM_COMPUTE_RETURN_ERROR_ON_MSG((b)->dimension(0) != (output)->dimension(0),
194 "The output matrix must have the same number of columns as the matrix B");
Chunosov5124be52017-11-22 20:42:13 +0700195 ARM_COMPUTE_UNUSED(gemm_info);
196 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
197 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100198 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.reinterpret_input_as_3d(), "NEGEMMLowpMatrixMultiplyCore cannot reinterpret the input tensor as 3D");
199 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.depth_output_gemm3d() != 1, "NEGEMMLowpMatrixMultiplyCore cannot reinterpret the output tensor as 3D");
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000200
Gian Marcoc7f9b892017-11-30 14:31:13 +0000201 int32_t a_offset = a->quantization_info().offset;
202 int32_t b_offset = b->quantization_info().offset;
203 bool run_vector_matrix_multiplication = a->dimension(1) < 2;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000204
Pablo Telloeb82fd22018-02-23 13:43:50 +0000205 if(!run_vector_matrix_multiplication)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000206 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000207 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
208 TensorShape shape_tmp_a = a->tensor_shape();
209 shape_tmp_a.set(0, a->dimension(0) * 4);
210 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
211
212 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
213 TensorShape shape_tmp_b = b->tensor_shape();
214 shape_tmp_b.set(0, b->dimension(1) * 16);
215 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
216
217 TensorInfo info_a(shape_tmp_a, 1, a->data_type());
218 TensorInfo info_b(shape_tmp_b, 1, b->data_type());
219
220 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &info_a));
221 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &info_b));
222 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(&info_a, &info_b, output));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000223 }
224 else
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000225 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000226 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(a, b, output));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000227 }
228
229 TensorInfo info_vector_sum_col, info_vector_sum_row;
230
231 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
232 if(a_offset != 0)
233 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000234 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000235
236 // Configure Matrix B reduction kernel
237 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, a->dimension(0), false));
238 }
239
240 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
241 if(b_offset != 0)
242 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000243 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000244
245 // Configure matrix A reduction kernel
246 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row, a->dimension(0), false));
247 }
248
249 // Validate offset contribution kernel
250 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionKernel::validate(output,
251 a_offset == 0 ? nullptr : &info_vector_sum_col,
252 b_offset == 0 ? nullptr : &info_vector_sum_row,
253 a_offset, b_offset));
254
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000255 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000256}
257
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100258void NEGEMMLowpMatrixMultiplyCore::run()
259{
Georgios Pinitas72219332018-06-05 14:56:06 +0100260 prepare();
261
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100262 _memory_group.acquire();
263
Georgios Pinitas72219332018-06-05 14:56:06 +0100264 // Reshape inputs
265 if(_mtx_a_reshape_kernel)
Pablo Tello6ff12a02017-11-02 16:09:35 +0000266 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100267 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
268 }
269 if(_mtx_b_reshape_kernel && !_reshape_b_only_on_first_run)
270 {
271 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
Pablo Tello6ff12a02017-11-02 16:09:35 +0000272 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100273
Georgios Pinitas72219332018-06-05 14:56:06 +0100274 // Run GEMM
Anthony Barbiereaefd002018-07-20 17:49:35 +0100275 if(_asm_glue.is_configured())
Pablo Telloeb82fd22018-02-23 13:43:50 +0000276 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100277 _asm_glue.run();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000278 }
279 else
280 {
281 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
282 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100283
Gian Marcoe75a02b2017-11-08 12:24:09 +0000284 // Run matrix A reduction kernel only if _b_offset is not equal to 0
285 if(_b_offset != 0)
286 {
287 NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX);
288 }
289
290 // Run matrix B reduction kernel only if _a_offset is not equal to 0
Georgios Pinitas72219332018-06-05 14:56:06 +0100291 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000292 {
293 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
294 }
295
296 // Run offset contribution kernel
297 NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY);
298
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100299 _memory_group.release();
Georgios Pinitas72219332018-06-05 14:56:06 +0100300}
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100301
Georgios Pinitas72219332018-06-05 14:56:06 +0100302void NEGEMMLowpMatrixMultiplyCore::prepare()
303{
304 if(!_is_prepared)
305 {
306 // Run assembly reshape
Anthony Barbiereaefd002018-07-20 17:49:35 +0100307 if(_asm_glue.is_configured() && _reshape_b_only_on_first_run)
Georgios Pinitas72219332018-06-05 14:56:06 +0100308 {
309 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
310
Anthony Barbiereaefd002018-07-20 17:49:35 +0100311 _asm_glue.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100312 _original_b->mark_as_unused();
313 }
314 // Run non-assembly reshape
315 else if(_mtx_b_reshape_kernel && _reshape_b_only_on_first_run)
316 {
317 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
318
319 // Run reshape kernel and mark original weights tensor as unused
320 _tmp_b.allocator()->allocate();
321 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
322 _original_b->mark_as_unused();
323 }
324
325 // Run matrix B reduction kernel only if _a_offset is not equal to 0
326 if(_a_offset != 0 && _reshape_b_only_on_first_run)
327 {
328 _vector_sum_col.allocator()->allocate();
329 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
330 }
331
332 _is_prepared = true;
333 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000334}