blob: 3417c7273540dd1427da37dbeb8f9f97305e384f [file] [log] [blame]
Gian Marco Iodiceab182122017-10-09 15:05:40 +01001/*
Michalis Spyrou71ac9032019-11-14 14:31:44 +00002 * Copyright (c) 2017-2020 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"
Michele Di Giorgioa602f032020-03-12 19:34:33 +000029#include "arm_compute/core/KernelDescriptors.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010030#include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010031#include "arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h"
32#include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010033#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Validate.h"
Isabella Gottardie6630e42018-01-18 15:50:39 +000036#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010037#include "arm_compute/runtime/NEON/NEScheduler.h"
38#include "arm_compute/runtime/TensorAllocator.h"
Matthew Bentham92046462020-03-07 22:15:55 +000039#include "support/MemorySupport.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010040
Michele Di Giorgioa602f032020-03-12 19:34:33 +000041namespace arm_compute
42{
Isabella Gottardie6630e42018-01-18 15:50:39 +000043using namespace arm_compute::misc::shape_calculator;
Gian Marco Iodiceab182122017-10-09 15:05:40 +010044
45NEGEMMLowpMatrixMultiplyCore::NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +010046 : _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(),
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010047 _offset_contribution_kernel(), _offset_contribution_output_stage_kernel(), _activation_func(), _convert_to_signed_asymm(), _convert_from_signed_asymm(), _vector_sum_col(), _vector_sum_row(), _tmp_a(),
48 _tmp_b(), _mm_result_s32(), _signed_a(), _signed_output(), _original_b(nullptr), _a_offset(0), _b_offset(0), _run_vector_matrix_multiplication(false), _assembly_path(false),
49 _fused_assembly_path(false), _reshape_b_only_on_first_run(false), _is_prepared(false), _fuse_output_stage(false), _run_activation(false), _flip_signedness(false)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010050{
51}
52
Gian Marco Iodice4b908652018-10-18 10:21:02 +010053void NEGEMMLowpMatrixMultiplyCore::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *output, const GEMMInfo &gemm_info)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010054{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000055 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Gian Marco Iodice4b908652018-10-18 10:21:02 +010056 ARM_COMPUTE_UNUSED(c);
57 ARM_COMPUTE_ERROR_THROW_ON(NEGEMMLowpMatrixMultiplyCore::validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), gemm_info));
Gian Marco Iodiceab182122017-10-09 15:05:40 +010058
George Wort2d7e6832019-02-22 16:37:41 +000059 const ITensor *matrix_a = a;
60 const ITensor *matrix_b = b;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010061 GEMMInfo info = gemm_info;
George Wort2d7e6832019-02-22 16:37:41 +000062
Georgios Pinitas72219332018-06-05 14:56:06 +010063 // Clear state
Anthony Barbier71d9b572018-07-06 17:05:59 +010064 _mtx_a_reshape_kernel = nullptr;
65 _mtx_b_reshape_kernel = nullptr;
Georgios Pinitas72219332018-06-05 14:56:06 +010066
67 // Set internal variables
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010068 _a_offset = a->info()->quantization_info().uniform().offset;
69 _b_offset = b->info()->quantization_info().uniform().offset;
Gian Marcoc7f9b892017-11-30 14:31:13 +000070 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010071 _reshape_b_only_on_first_run = info.reshape_b_only_on_first_run();
Georgios Pinitas72219332018-06-05 14:56:06 +010072 _is_prepared = false;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010073 _fused_assembly_path = false;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010074 _flip_signedness = is_data_type_quantized_per_channel(b->info()->data_type()) && (a->info()->data_type() == DataType::QASYMM8) && _reshape_b_only_on_first_run;
Georgios Pinitas72219332018-06-05 14:56:06 +010075 _original_b = b;
Gian Marcoe75a02b2017-11-08 12:24:09 +000076
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010077 const ITensor *a_to_use = a;
78
79 // Convert to QASYMM8 -> QASYMM8_SIGNED and back
80 if(_flip_signedness)
81 {
82 const int32_t offset_correction = 128;
83 const DataType dt = DataType::QASYMM8_SIGNED;
84 const UniformQuantizationInfo iqinfo = a_to_use->info()->quantization_info().uniform();
85
86 _signed_a.allocator()->init(a_to_use->info()->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction)));
87 _memory_group.manage(&_signed_a);
88 _convert_to_signed_asymm.configure(a_to_use, &_signed_a);
89 a_to_use = &_signed_a;
90 _a_offset = _signed_a.info()->quantization_info().uniform().offset;
91
92 const UniformQuantizationInfo oqinfo = output->info()->quantization_info().uniform();
93 _memory_group.manage(&_signed_output);
94 _signed_output.allocator()->init(output->info()->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction)));
95
96 // Output stage correction
97 GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage();
98 output_stage_corr.gemmlowp_offset = _signed_output.info()->quantization_info().uniform().offset;
99 output_stage_corr.gemmlowp_min_bound -= offset_correction;
100 output_stage_corr.gemmlowp_max_bound -= offset_correction;
101 info.set_gemmlowp_output_stage(output_stage_corr);
Georgios Pinitas63d4dbd2019-11-08 11:51:56 +0000102
103 // Update matrix a
104 matrix_a = &_signed_a;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100105 }
106
George Wort2d7e6832019-02-22 16:37:41 +0000107 // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100108 if(info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE)
George Wort2d7e6832019-02-22 16:37:41 +0000109 {
110 _fuse_output_stage = true;
George Wort2d7e6832019-02-22 16:37:41 +0000111 _memory_group.manage(&_mm_result_s32);
George Wort2d7e6832019-02-22 16:37:41 +0000112 TensorInfo info_mm_result_s32(output->info()->tensor_shape(), 1, DataType::S32);
George Wort2d7e6832019-02-22 16:37:41 +0000113 _mm_result_s32.allocator()->init(info_mm_result_s32);
114 }
115
Pablo Telloeb82fd22018-02-23 13:43:50 +0000116#ifdef __aarch64__
117 switch(a->info()->data_type())
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100118 {
Pablo Tello66c656a2018-03-15 10:34:58 +0000119 case DataType::QASYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100120 case DataType::QASYMM8_SIGNED:
Pablo Telloeb82fd22018-02-23 13:43:50 +0000121 case DataType::U8:
Anthony Barbiereaefd002018-07-20 17:49:35 +0100122 case DataType::S8:
Pablo Telloeb82fd22018-02-23 13:43:50 +0000123 {
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000124 if(is_data_type_quantized_asymmetric(a_to_use->info()->data_type()) && info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100125 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100126 _asm_glue.configure(a_to_use, b, c, output, gemm_info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100127 _fused_assembly_path = _asm_glue.is_configured();
128 }
129 else
130 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100131 _asm_glue.configure(a_to_use, b, nullptr, _fuse_output_stage ? &_mm_result_s32 : output, gemm_info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100132 }
133 _assembly_path = _asm_glue.is_configured();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000134 break;
135 }
136 default:
137 {
138 ARM_COMPUTE_ERROR("Datatype not supported");
139 break;
140 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100141 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000142#endif /* __aarch64__ */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100143 if(!(_assembly_path || _run_vector_matrix_multiplication))
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100144 {
George Wort2d7e6832019-02-22 16:37:41 +0000145 matrix_a = &_tmp_a;
146 matrix_b = &_tmp_b;
147
148 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100149 TensorInfo a_info(compute_interleaved_shape(*a_to_use->info()), 1, a_to_use->info()->data_type(), a_to_use->info()->quantization_info());
George Wort2d7e6832019-02-22 16:37:41 +0000150 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
Georgios Pinitas02acf012019-03-19 10:49:03 +0000151 TensorInfo b_info(compute_transpose1xW_shape(*b->info()), 1, b->info()->data_type(), b->info()->quantization_info());
George Wort2d7e6832019-02-22 16:37:41 +0000152 _tmp_a.allocator()->init(a_info);
153 _tmp_b.allocator()->init(b_info);
154 _memory_group.manage(&_tmp_a);
155 if(!_reshape_b_only_on_first_run)
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100156 {
George Wort2d7e6832019-02-22 16:37:41 +0000157 _memory_group.manage(&_tmp_b);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100158 }
George Wort2d7e6832019-02-22 16:37:41 +0000159
160 // Configure interleave kernel
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100161 {
George Wort2d7e6832019-02-22 16:37:41 +0000162 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100163 k->configure(a_to_use, &_tmp_a);
George Wort2d7e6832019-02-22 16:37:41 +0000164 _mtx_a_reshape_kernel = std::move(k);
165 }
Gian Marcoc7f9b892017-11-30 14:31:13 +0000166
George Wort2d7e6832019-02-22 16:37:41 +0000167 // Configure transpose kernel
168 {
169 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
170 k->configure(b, &_tmp_b);
171 _mtx_b_reshape_kernel = std::move(k);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100172 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000173 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100174
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100175 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000176 {
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000177 // Build reduction info
178 const GEMMLowpReductionKernelInfo reduction_info(a_to_use->info()->dimension(0), false, 0, false);
179
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100180 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
181 if(_a_offset != 0)
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100182 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100183 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
184
185 _vector_sum_col.allocator()->init(info_vector_sum_col);
186 if(!_reshape_b_only_on_first_run)
187 {
188 _memory_group.manage(&_vector_sum_col);
189 }
190
191 // Configure Matrix B reduction kernel
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000192 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, reduction_info);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100193 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000194
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100195 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
196 if(_b_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000197 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100198 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a_to_use->info()), 1, DataType::S32);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100199
200 _vector_sum_row.allocator()->init(info_vector_sum_row);
201 _memory_group.manage(&_vector_sum_row);
202
203 // Configure matrix A reduction kernel
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000204 _mtx_a_reduction_kernel.configure(a_to_use, &_vector_sum_row, reduction_info);
George Wort2d7e6832019-02-22 16:37:41 +0000205 }
206
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100207 if(_fuse_output_stage)
George Wort2d7e6832019-02-22 16:37:41 +0000208 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100209 // Configure matrix multiply kernel
210 if(!_assembly_path)
211 {
212 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
213 k->configure(matrix_a, matrix_b, &_mm_result_s32);
214 _mm_kernel = std::move(k);
215 }
216
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100217 _offset_contribution_output_stage_kernel.configure(&_mm_result_s32,
218 _a_offset == 0 ? nullptr : &_vector_sum_col,
219 _b_offset == 0 ? nullptr : &_vector_sum_row, c,
220 _flip_signedness ? &_signed_output : output,
221 a->info()->dimension(0),
222 _a_offset, _b_offset, info.gemmlowp_output_stage());
223
224 if(_flip_signedness)
225 {
226 _convert_from_signed_asymm.configure(&_signed_output, output);
227 }
George Wort2d7e6832019-02-22 16:37:41 +0000228 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100229 else
230 {
231 // Configure matrix multiply kernel
232 if(!_assembly_path)
233 {
234 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
235 k->configure(matrix_a, matrix_b, output);
236 _mm_kernel = std::move(k);
237 }
238 // Configure offset contribution kernel
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100239 _offset_contribution_kernel.configure(output, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, a_to_use->info()->dimension(0), _a_offset, _b_offset);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100240 }
George Wort2d7e6832019-02-22 16:37:41 +0000241 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000242
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100243 // Configure activation
244 const ActivationLayerInfo &activation = gemm_info.activation_info();
245 _run_activation = activation.enabled() && (!_assembly_path || (_assembly_path && !NEGEMMAssemblyDispatch::is_activation_supported(activation)));
246 if(_run_activation)
247 {
248 _activation_func.configure(output, nullptr, activation);
249 }
250
Gian Marcoe75a02b2017-11-08 12:24:09 +0000251 // Allocate tensors
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100252 if(!_assembly_path && !_run_vector_matrix_multiplication)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000253 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000254 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100255 if(!_reshape_b_only_on_first_run)
256 {
257 _tmp_b.allocator()->allocate();
258 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000259 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000260
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100261 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000262 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100263 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
264 {
265 _vector_sum_col.allocator()->allocate();
266 }
267
268 if(_b_offset != 0)
269 {
270 _vector_sum_row.allocator()->allocate();
271 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000272 }
273
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100274 if(_fuse_output_stage)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000275 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100276 _mm_result_s32.allocator()->allocate();
Gian Marcoe75a02b2017-11-08 12:24:09 +0000277 }
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100278
279 if(_flip_signedness)
280 {
281 _signed_a.allocator()->allocate();
282 _signed_output.allocator()->allocate();
283 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100284}
285
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100286Status NEGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000287{
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000288 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
289 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(b, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL);
290 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
George Wort2d7e6832019-02-22 16:37:41 +0000291 ARM_COMPUTE_RETURN_ERROR_ON_MSG(c != nullptr && gemm_info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::NONE, "Bias addition not supported in NEGEMMLowpMatrixMultiplyCore for output S32");
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000292 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
293 "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
Chunosov5124be52017-11-22 20:42:13 +0700294 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
295 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 +0000296
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100297 GEMMInfo info = gemm_info;
George Wort2d7e6832019-02-22 16:37:41 +0000298 const ITensorInfo *matrix_a_info = a;
299 const ITensorInfo *matrix_b_info = b;
300
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100301 const ITensorInfo *a_to_use = a;
302
George Wort2d7e6832019-02-22 16:37:41 +0000303 TensorInfo tmp_a_info{};
304 TensorInfo tmp_b_info{};
305 TensorInfo mm_result_s32_info{};
306
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100307 int32_t a_offset = a->quantization_info().uniform().offset;
308 int32_t b_offset = b->quantization_info().uniform().offset;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000309
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100310 bool fuse_output_stage = info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE;
George Wort2d7e6832019-02-22 16:37:41 +0000311 if(fuse_output_stage)
312 {
313 auto_init_if_empty(mm_result_s32_info, a->clone()->set_tensor_shape(output->tensor_shape()).set_data_type(DataType::S32));
314 }
315
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100316 // Convert QASYMM8->QASYMM8_SIGNED
317 TensorInfo signed_a{};
318 TensorInfo signed_output{};
319 bool flip_signedness = is_data_type_quantized_per_channel(b->data_type()) && (a->data_type() == DataType::QASYMM8) && info.reshape_b_only_on_first_run();
320 if(flip_signedness)
321 {
322 const int32_t offset_correction = 128;
323 const DataType dt = DataType::QASYMM8_SIGNED;
324 const UniformQuantizationInfo iqinfo = a_to_use->quantization_info().uniform();
325
326 signed_a = a_to_use->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction));
327 ARM_COMPUTE_RETURN_ON_ERROR(NEConvertQuantizedSignednessKernel::validate(a_to_use, &signed_a));
328 a_to_use = &signed_a;
329 a_offset = signed_a.quantization_info().uniform().offset;
330
331 const UniformQuantizationInfo oqinfo = output->quantization_info().uniform();
332 signed_output = output->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction));
333
334 // Output stage correction
335 GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage();
336 output_stage_corr.gemmlowp_offset = signed_output.quantization_info().uniform().offset;
337 output_stage_corr.gemmlowp_min_bound -= offset_correction;
338 output_stage_corr.gemmlowp_max_bound -= offset_correction;
339 info.set_gemmlowp_output_stage(output_stage_corr);
Georgios Pinitas63d4dbd2019-11-08 11:51:56 +0000340
341 // Update matrix a
342 matrix_a_info = &signed_a;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100343 }
344
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000345 // Check if we need to run the optimized assembly kernel
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100346 bool run_optimised = false;
347 bool run_optimised_requantized = false;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100348 if(a_to_use->data_type() == DataType::QASYMM8 && info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100349 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100350 run_optimised = bool(NEGEMMAssemblyDispatch::validate(a_to_use, b, c, output, gemm_info));
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100351 run_optimised_requantized = run_optimised;
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000352
353 const UniformQuantizationInfo a_qinfo = a_to_use->quantization_info().uniform();
354 const QuantizationInfo b_qinfo = b->quantization_info();
355 const UniformQuantizationInfo output_qinfo = output->quantization_info().uniform();
356 for(auto const s : b_qinfo.scale())
357 {
358 const float fmultipler = a_qinfo.scale * s / output_qinfo.scale;
359 if(fmultipler > 1.f)
360 {
361 run_optimised_requantized = false;
362 break;
363 }
364 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100365 }
366 else
367 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100368 run_optimised = bool(NEGEMMAssemblyDispatch::validate(a_to_use, b, nullptr, fuse_output_stage ? &mm_result_s32_info : output, gemm_info));
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100369 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000370
371 if(run_optimised)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000372 {
George Wort2d7e6832019-02-22 16:37:41 +0000373 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100374 if(info.depth_output_gemm3d() != 0)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000375 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100376 if(info.reinterpret_input_as_3d())
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000377 {
George Wort2d7e6832019-02-22 16:37:41 +0000378 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
379 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000380 }
381 else
382 {
George Wort2d7e6832019-02-22 16:37:41 +0000383 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000384 }
385 }
George Wort2d7e6832019-02-22 16:37:41 +0000386 else
387 {
388 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
389 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000390 }
391 else
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000392 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100393 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.reinterpret_input_as_3d(), "NEGEMM cannot reinterpret the input tensor as 3D");
394 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.depth_output_gemm3d() != 0, "NEGEMM cannot reinterpret the output tensor as 3D");
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000395
396 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
397 if(!run_vector_matrix_multiplication)
398 {
George Wort2d7e6832019-02-22 16:37:41 +0000399 matrix_a_info = &tmp_a_info;
400 matrix_b_info = &tmp_b_info;
401
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000402 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
403 TensorShape shape_tmp_a = a->tensor_shape();
404 shape_tmp_a.set(0, a->dimension(0) * 4);
405 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
406
407 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
408 TensorShape shape_tmp_b = b->tensor_shape();
409 shape_tmp_b.set(0, b->dimension(1) * 16);
410 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
411
George Wort2d7e6832019-02-22 16:37:41 +0000412 // Validate interleave kernel
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100413 auto_init_if_empty(tmp_a_info, a_to_use->clone()->set_tensor_shape(shape_tmp_a));
George Wort2d7e6832019-02-22 16:37:41 +0000414 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(shape_tmp_b));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000415
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100416 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a_to_use, &tmp_a_info));
George Wort2d7e6832019-02-22 16:37:41 +0000417 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &tmp_b_info));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000418 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000419 }
420
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100421 if(!run_optimised_requantized)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000422 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100423 TensorInfo info_vector_sum_col{};
424 TensorInfo info_vector_sum_row{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000425
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000426 const GEMMLowpReductionKernelInfo reduction_info(a_to_use->dimension(0), false, 0, false);
427
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100428 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
429 if(a_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000430 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100431 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
432
433 // Configure Matrix B reduction kernel
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000434 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, reduction_info));
George Wort2d7e6832019-02-22 16:37:41 +0000435 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000436
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100437 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
438 if(b_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000439 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100440 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
441
442 // Configure matrix A reduction kernel
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000443 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(a_to_use, &info_vector_sum_row, reduction_info));
George Wort2d7e6832019-02-22 16:37:41 +0000444 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100445
446 if(fuse_output_stage)
447 {
448 if(!run_optimised)
449 {
450 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info));
451 }
452
453 // Validate offset contribution kernel
454 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info,
455 a_offset == 0 ? nullptr : &info_vector_sum_col,
456 b_offset == 0 ? nullptr : &info_vector_sum_row,
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100457 c,
458 flip_signedness ? &signed_output : output,
459 a_offset, b_offset,
460 info.gemmlowp_output_stage()));
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100461 }
462 else
463 {
464 if(!run_optimised)
465 {
466 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output));
467 }
468 // Validate offset contribution kernel
469 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionKernel::validate(output,
470 a_offset == 0 ? nullptr : &info_vector_sum_col,
471 b_offset == 0 ? nullptr : &info_vector_sum_row,
472 a_offset, b_offset));
473 }
George Wort2d7e6832019-02-22 16:37:41 +0000474 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100475
476 // Validate activation
477 const ActivationLayerInfo &activation = gemm_info.activation_info();
478 if(activation.enabled())
479 {
480 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, activation));
481 }
482
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000483 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000484}
485
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100486void NEGEMMLowpMatrixMultiplyCore::run()
487{
Georgios Pinitas72219332018-06-05 14:56:06 +0100488 prepare();
489
Georgios Pinitasda953f22019-04-02 17:27:03 +0100490 MemoryGroupResourceScope scope_mg(_memory_group);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100491
Georgios Pinitas63d4dbd2019-11-08 11:51:56 +0000492 // Convert QASYMM8->QASYMM8_SIGNED
493 if(_flip_signedness)
494 {
495 NEScheduler::get().schedule(&_convert_to_signed_asymm, Window::DimY);
496 }
497
Georgios Pinitas72219332018-06-05 14:56:06 +0100498 // Reshape inputs
499 if(_mtx_a_reshape_kernel)
Pablo Tello6ff12a02017-11-02 16:09:35 +0000500 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100501 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
502 }
503 if(_mtx_b_reshape_kernel && !_reshape_b_only_on_first_run)
504 {
505 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
Pablo Tello6ff12a02017-11-02 16:09:35 +0000506 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100507
Georgios Pinitas72219332018-06-05 14:56:06 +0100508 // Run GEMM
Anthony Barbiereaefd002018-07-20 17:49:35 +0100509 if(_asm_glue.is_configured())
Pablo Telloeb82fd22018-02-23 13:43:50 +0000510 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100511 _asm_glue.run();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000512 }
513 else
514 {
515 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
516 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100517
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100518 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000519 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100520 // Run matrix A reduction kernel only if _b_offset is not equal to 0
521 if(_b_offset != 0)
522 {
523 NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX);
524 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000525
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100526 // Run matrix B reduction kernel only if _a_offset is not equal to 0
527 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
528 {
529 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
530 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000531
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100532 if(_fuse_output_stage)
533 {
534 // Run offset contribution kernel
535 NEScheduler::get().schedule(&_offset_contribution_output_stage_kernel, Window::DimY);
536 }
537 else
538 {
539 // Run offset contribution kernel
540 NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY);
541 }
George Wort2d7e6832019-02-22 16:37:41 +0000542 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100543
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100544 // Convert QASYMM8_SIGNED->QASYMM8
545 if(_flip_signedness)
546 {
547 NEScheduler::get().schedule(&_convert_from_signed_asymm, Window::DimY);
548 }
549
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100550 // Run fused activation
551 if(_run_activation)
552 {
553 _activation_func.run();
554 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100555}
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100556
Georgios Pinitas72219332018-06-05 14:56:06 +0100557void NEGEMMLowpMatrixMultiplyCore::prepare()
558{
559 if(!_is_prepared)
560 {
561 // Run assembly reshape
Anthony Barbiereaefd002018-07-20 17:49:35 +0100562 if(_asm_glue.is_configured() && _reshape_b_only_on_first_run)
Georgios Pinitas72219332018-06-05 14:56:06 +0100563 {
564 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
565
Anthony Barbiereaefd002018-07-20 17:49:35 +0100566 _asm_glue.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100567 _original_b->mark_as_unused();
568 }
569 // Run non-assembly reshape
570 else if(_mtx_b_reshape_kernel && _reshape_b_only_on_first_run)
571 {
572 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
573
574 // Run reshape kernel and mark original weights tensor as unused
575 _tmp_b.allocator()->allocate();
576 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
577 _original_b->mark_as_unused();
578 }
579
580 // Run matrix B reduction kernel only if _a_offset is not equal to 0
581 if(_a_offset != 0 && _reshape_b_only_on_first_run)
582 {
583 _vector_sum_col.allocator()->allocate();
584 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
585 }
586
587 _is_prepared = true;
588 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000589}
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000590} // namespace arm_compute