blob: a92ffa7c7bdfbce8666ce90bb1577c71e340f145 [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)
Pablo Telloeb82fd22018-02-23 13:43:50 +000044 : _memory_group(std::move(memory_manager)), _asm_glue_unsigned(), _asm_glue_signed(), _mm_kernel(nullptr), _mtx_a_reshape_kernel(nullptr), _mtx_b_reshape_kernel(nullptr), _mtx_a_reduction_kernel(),
Georgios Pinitas72219332018-06-05 14:56:06 +010045 _mtx_b_reduction_kernel(), _offset_contribution_kernel(), _vector_sum_col(), _vector_sum_row(), _tmp_a(), _tmp_b(), _workspace(), _B_pretranspose(), _original_b(nullptr), _a_offset(0), _b_offset(0),
46 _run_vector_matrix_multiplication(false), _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
56 _mtx_a_reshape_kernel = nullptr;
57 _mtx_b_reshape_kernel = nullptr;
58 _asm_glue_signed._optimised_kernel = nullptr;
59 _asm_glue_unsigned._optimised_kernel = nullptr;
60
61 // Set internal variables
Gian Marcoc7f9b892017-11-30 14:31:13 +000062 _a_offset = a->info()->quantization_info().offset;
63 _b_offset = b->info()->quantization_info().offset;
64 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Giorgio Arenabb54e4e2018-04-05 17:20:34 +010065 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Georgios Pinitas72219332018-06-05 14:56:06 +010066 _is_prepared = false;
67 _original_b = b;
Gian Marcoe75a02b2017-11-08 12:24:09 +000068
Pablo Telloeb82fd22018-02-23 13:43:50 +000069#ifdef __aarch64__
70 switch(a->info()->data_type())
Gian Marco Iodiceab182122017-10-09 15:05:40 +010071 {
Pablo Telloeb82fd22018-02-23 13:43:50 +000072 case DataType::S8:
73 {
Georgios Pinitas72219332018-06-05 14:56:06 +010074 _dot_product_path = setup_assembly_kernel(a, b, output, 1.f, 0.f, _reshape_b_only_on_first_run, _workspace, _B_pretranspose, _memory_group, _asm_glue_signed);
Pablo Telloeb82fd22018-02-23 13:43:50 +000075 break;
76 }
Pablo Tello66c656a2018-03-15 10:34:58 +000077 case DataType::QASYMM8:
Pablo Telloeb82fd22018-02-23 13:43:50 +000078 case DataType::U8:
79 {
Georgios Pinitas72219332018-06-05 14:56:06 +010080 _dot_product_path = setup_assembly_kernel(a, b, output, 1.f, 0.f, _reshape_b_only_on_first_run, _workspace, _B_pretranspose, _memory_group, _asm_glue_unsigned);
Pablo Telloeb82fd22018-02-23 13:43:50 +000081 break;
82 }
83 default:
84 {
85 ARM_COMPUTE_ERROR("Datatype not supported");
86 break;
87 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +010088 }
Pablo Telloeb82fd22018-02-23 13:43:50 +000089#endif /* __aarch64__ */
90 if(!_dot_product_path)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010091 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000092 if(_run_vector_matrix_multiplication)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010093 {
Gian Marcoc7f9b892017-11-30 14:31:13 +000094 // Configure matrix multiply kernel
95 {
96 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
97 k->configure(a, b, output);
98 _mm_kernel = std::move(k);
99 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100100 }
Gian Marcoc7f9b892017-11-30 14:31:13 +0000101 else
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100102 {
Gian Marcoc7f9b892017-11-30 14:31:13 +0000103 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
Isabella Gottardie6630e42018-01-18 15:50:39 +0000104 TensorInfo info_a(compute_interleaved_shape(*a->info()), 1, a->info()->data_type());
Gian Marcoc7f9b892017-11-30 14:31:13 +0000105 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
Isabella Gottardie6630e42018-01-18 15:50:39 +0000106 TensorInfo info_b(compute_transpose1xW_shape(*b->info()), 1, b->info()->data_type());
Gian Marcoc7f9b892017-11-30 14:31:13 +0000107 _tmp_a.allocator()->init(info_a);
108 _tmp_b.allocator()->init(info_b);
109 _memory_group.manage(&_tmp_a);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100110 if(!_reshape_b_only_on_first_run)
111 {
112 _memory_group.manage(&_tmp_b);
113 }
Gian Marcoc7f9b892017-11-30 14:31:13 +0000114
115 // Configure interleave kernel
116 {
117 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
118 k->configure(a, &_tmp_a);
119 _mtx_a_reshape_kernel = std::move(k);
120 }
121
122 // Configure transpose kernel
123 {
124 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
125 k->configure(b, &_tmp_b);
126 _mtx_b_reshape_kernel = std::move(k);
127 }
128
129 // Configure matrix multiply kernel
130 {
131 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
132 k->configure(&_tmp_a, &_tmp_b, output);
133 _mm_kernel = std::move(k);
134 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100135 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000136 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100137
Gian Marcoe75a02b2017-11-08 12:24:09 +0000138 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
139 if(_a_offset != 0)
140 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000141 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
142
Gian Marcoe75a02b2017-11-08 12:24:09 +0000143 _vector_sum_col.allocator()->init(info_vector_sum_col);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100144 if(!_reshape_b_only_on_first_run)
145 {
146 _memory_group.manage(&_vector_sum_col);
147 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000148
149 // Configure Matrix B reduction kernel
150 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, a->info()->dimension(0), false);
151 }
152
153 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
154 if(_b_offset != 0)
155 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000156 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a->info()), 1, DataType::S32);
157
Gian Marcoe75a02b2017-11-08 12:24:09 +0000158 _vector_sum_row.allocator()->init(info_vector_sum_row);
159 _memory_group.manage(&_vector_sum_row);
160
161 // Configure matrix A reduction kernel
162 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row, a->info()->dimension(0), false);
163 }
164
165 // Configure offset contribution kernel
166 _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);
167
168 // Allocate tensors
Gian Marcoc7f9b892017-11-30 14:31:13 +0000169 if(!_dot_product_path && !_run_vector_matrix_multiplication)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000170 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000171 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100172 if(!_reshape_b_only_on_first_run)
173 {
174 _tmp_b.allocator()->allocate();
175 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000176 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000177
Georgios Pinitas72219332018-06-05 14:56:06 +0100178 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000179 {
180 _vector_sum_col.allocator()->allocate();
181 }
182
183 if(_b_offset != 0)
184 {
185 _vector_sum_row.allocator()->allocate();
186 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100187}
188
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000189Status NEGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *output, const GEMMInfo &gemm_info)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000190{
191 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
192 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
193 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
194 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
195 "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
196 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(1) != (output)->dimension(1),
197 "The output matrix must have the same number of rows as the matrix A");
198 ARM_COMPUTE_RETURN_ERROR_ON_MSG((b)->dimension(0) != (output)->dimension(0),
199 "The output matrix must have the same number of columns as the matrix B");
Chunosov5124be52017-11-22 20:42:13 +0700200 ARM_COMPUTE_UNUSED(gemm_info);
201 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
202 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000203
Gian Marcoc7f9b892017-11-30 14:31:13 +0000204 int32_t a_offset = a->quantization_info().offset;
205 int32_t b_offset = b->quantization_info().offset;
206 bool run_vector_matrix_multiplication = a->dimension(1) < 2;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000207
Pablo Telloeb82fd22018-02-23 13:43:50 +0000208 if(!run_vector_matrix_multiplication)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000209 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000210 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
211 TensorShape shape_tmp_a = a->tensor_shape();
212 shape_tmp_a.set(0, a->dimension(0) * 4);
213 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
214
215 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
216 TensorShape shape_tmp_b = b->tensor_shape();
217 shape_tmp_b.set(0, b->dimension(1) * 16);
218 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
219
220 TensorInfo info_a(shape_tmp_a, 1, a->data_type());
221 TensorInfo info_b(shape_tmp_b, 1, b->data_type());
222
223 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &info_a));
224 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &info_b));
225 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(&info_a, &info_b, output));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000226 }
227 else
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000228 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000229 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(a, b, output));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000230 }
231
232 TensorInfo info_vector_sum_col, info_vector_sum_row;
233
234 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
235 if(a_offset != 0)
236 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000237 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000238
239 // Configure Matrix B reduction kernel
240 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, a->dimension(0), false));
241 }
242
243 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
244 if(b_offset != 0)
245 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000246 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000247
248 // Configure matrix A reduction kernel
249 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row, a->dimension(0), false));
250 }
251
252 // Validate offset contribution kernel
253 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionKernel::validate(output,
254 a_offset == 0 ? nullptr : &info_vector_sum_col,
255 b_offset == 0 ? nullptr : &info_vector_sum_row,
256 a_offset, b_offset));
257
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000258 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000259}
260
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100261void NEGEMMLowpMatrixMultiplyCore::run()
262{
Georgios Pinitas72219332018-06-05 14:56:06 +0100263 prepare();
264
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100265 _memory_group.acquire();
266
Georgios Pinitas72219332018-06-05 14:56:06 +0100267 // Reshape inputs
268 if(_mtx_a_reshape_kernel)
Pablo Tello6ff12a02017-11-02 16:09:35 +0000269 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100270 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
271 }
272 if(_mtx_b_reshape_kernel && !_reshape_b_only_on_first_run)
273 {
274 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
Pablo Tello6ff12a02017-11-02 16:09:35 +0000275 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100276
Georgios Pinitas72219332018-06-05 14:56:06 +0100277 // Run GEMM
Pablo Telloeb82fd22018-02-23 13:43:50 +0000278 if(_asm_glue_unsigned._optimised_kernel != nullptr)
279 {
280 _asm_glue_unsigned.run();
281 }
282 else if(_asm_glue_signed._optimised_kernel != nullptr)
283 {
284 _asm_glue_signed.run();
285 }
286 else
287 {
288 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
289 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100290
Gian Marcoe75a02b2017-11-08 12:24:09 +0000291 // Run matrix A reduction kernel only if _b_offset is not equal to 0
292 if(_b_offset != 0)
293 {
294 NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX);
295 }
296
297 // Run matrix B reduction kernel only if _a_offset is not equal to 0
Georgios Pinitas72219332018-06-05 14:56:06 +0100298 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000299 {
300 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
301 }
302
303 // Run offset contribution kernel
304 NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY);
305
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100306 _memory_group.release();
Georgios Pinitas72219332018-06-05 14:56:06 +0100307}
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100308
Georgios Pinitas72219332018-06-05 14:56:06 +0100309void NEGEMMLowpMatrixMultiplyCore::prepare()
310{
311 if(!_is_prepared)
312 {
313 // Run assembly reshape
314 if((_asm_glue_signed._optimised_kernel || _asm_glue_signed._optimised_kernel) && _reshape_b_only_on_first_run)
315 {
316 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
317
318 if(_asm_glue_unsigned._optimised_kernel != nullptr)
319 {
320 _asm_glue_unsigned.prepare();
321 }
322 else if(_asm_glue_signed._optimised_kernel != nullptr)
323 {
324 _asm_glue_signed.prepare();
325 }
326 _original_b->mark_as_unused();
327 }
328 // Run non-assembly reshape
329 else if(_mtx_b_reshape_kernel && _reshape_b_only_on_first_run)
330 {
331 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
332
333 // Run reshape kernel and mark original weights tensor as unused
334 _tmp_b.allocator()->allocate();
335 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
336 _original_b->mark_as_unused();
337 }
338
339 // Run matrix B reduction kernel only if _a_offset is not equal to 0
340 if(_a_offset != 0 && _reshape_b_only_on_first_run)
341 {
342 _vector_sum_col.allocator()->allocate();
343 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
344 }
345
346 _is_prepared = true;
347 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000348}