blob: 993907c29aba595c4309ac8baaf5c0d6a04a31dc [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/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Validate.h"
Isabella Gottardie6630e42018-01-18 15:50:39 +000033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010034#include "arm_compute/runtime/NEON/NEScheduler.h"
35#include "arm_compute/runtime/TensorAllocator.h"
Matthew Bentham92046462020-03-07 22:15:55 +000036#include "support/MemorySupport.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010037
Michele Di Giorgioa602f032020-03-12 19:34:33 +000038namespace arm_compute
39{
Isabella Gottardie6630e42018-01-18 15:50:39 +000040using namespace arm_compute::misc::shape_calculator;
Gian Marco Iodiceab182122017-10-09 15:05:40 +010041
SiCongLi2e5fd632020-03-02 15:39:15 +000042NEGEMMLowpMatrixMultiplyCore::NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
43 : _memory_group(memory_manager), _weights_manager(weights_manager), _asm_glue(memory_manager, weights_manager), _mm_kernel(), _mtx_a_reshape_kernel(), _mtx_b_reshape_kernel(),
44 _mtx_a_reduction_kernel(), _mtx_b_reduction_kernel(), _offset_contribution_kernel(), _offset_contribution_output_stage_kernel(), _activation_func(), _convert_to_signed_asymm(),
45 _convert_from_signed_asymm(), _vector_sum_col(), _vector_sum_row(), _tmp_a(), _tmp_b(), _mm_result_s32(), _signed_a(), _signed_output(), _original_b(nullptr), _a_offset(0), _b_offset(0),
46 _run_vector_matrix_multiplication(false), _assembly_path(false), _fused_assembly_path(false), _reshape_b_only_on_first_run(false), _is_prepared(false), _fuse_output_stage(false),
47 _run_activation(false), _flip_signedness(false)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010048{
49}
50
Gian Marco Iodice4b908652018-10-18 10:21:02 +010051void 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 +010052{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000053 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Gian Marco Iodice4b908652018-10-18 10:21:02 +010054 ARM_COMPUTE_UNUSED(c);
55 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 +010056
George Wort2d7e6832019-02-22 16:37:41 +000057 const ITensor *matrix_a = a;
58 const ITensor *matrix_b = b;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010059 GEMMInfo info = gemm_info;
George Wort2d7e6832019-02-22 16:37:41 +000060
Georgios Pinitas72219332018-06-05 14:56:06 +010061 // Set internal variables
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010062 _a_offset = a->info()->quantization_info().uniform().offset;
63 _b_offset = b->info()->quantization_info().uniform().offset;
Gian Marcoc7f9b892017-11-30 14:31:13 +000064 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010065 _reshape_b_only_on_first_run = info.reshape_b_only_on_first_run();
Georgios Pinitas72219332018-06-05 14:56:06 +010066 _is_prepared = false;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010067 _fused_assembly_path = false;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010068 _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 +010069 _original_b = b;
Gian Marcoe75a02b2017-11-08 12:24:09 +000070
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010071 const ITensor *a_to_use = a;
72
73 // Convert to QASYMM8 -> QASYMM8_SIGNED and back
74 if(_flip_signedness)
75 {
76 const int32_t offset_correction = 128;
77 const DataType dt = DataType::QASYMM8_SIGNED;
78 const UniformQuantizationInfo iqinfo = a_to_use->info()->quantization_info().uniform();
79
80 _signed_a.allocator()->init(a_to_use->info()->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction)));
81 _memory_group.manage(&_signed_a);
82 _convert_to_signed_asymm.configure(a_to_use, &_signed_a);
83 a_to_use = &_signed_a;
84 _a_offset = _signed_a.info()->quantization_info().uniform().offset;
85
86 const UniformQuantizationInfo oqinfo = output->info()->quantization_info().uniform();
87 _memory_group.manage(&_signed_output);
88 _signed_output.allocator()->init(output->info()->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction)));
89
90 // Output stage correction
91 GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage();
92 output_stage_corr.gemmlowp_offset = _signed_output.info()->quantization_info().uniform().offset;
93 output_stage_corr.gemmlowp_min_bound -= offset_correction;
94 output_stage_corr.gemmlowp_max_bound -= offset_correction;
95 info.set_gemmlowp_output_stage(output_stage_corr);
Georgios Pinitas63d4dbd2019-11-08 11:51:56 +000096
97 // Update matrix a
98 matrix_a = &_signed_a;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010099 }
100
George Wort2d7e6832019-02-22 16:37:41 +0000101 // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100102 if(info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE)
George Wort2d7e6832019-02-22 16:37:41 +0000103 {
104 _fuse_output_stage = true;
George Wort2d7e6832019-02-22 16:37:41 +0000105 _memory_group.manage(&_mm_result_s32);
George Wort2d7e6832019-02-22 16:37:41 +0000106 TensorInfo info_mm_result_s32(output->info()->tensor_shape(), 1, DataType::S32);
George Wort2d7e6832019-02-22 16:37:41 +0000107 _mm_result_s32.allocator()->init(info_mm_result_s32);
108 }
109
Pablo Telloeb82fd22018-02-23 13:43:50 +0000110#ifdef __aarch64__
111 switch(a->info()->data_type())
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100112 {
Pablo Tello66c656a2018-03-15 10:34:58 +0000113 case DataType::QASYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100114 case DataType::QASYMM8_SIGNED:
Pablo Telloeb82fd22018-02-23 13:43:50 +0000115 case DataType::U8:
Anthony Barbiereaefd002018-07-20 17:49:35 +0100116 case DataType::S8:
Pablo Telloeb82fd22018-02-23 13:43:50 +0000117 {
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000118 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 +0100119 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100120 _asm_glue.configure(a_to_use, b, c, output, gemm_info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100121 _fused_assembly_path = _asm_glue.is_configured();
122 }
123 else
124 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100125 _asm_glue.configure(a_to_use, b, nullptr, _fuse_output_stage ? &_mm_result_s32 : output, gemm_info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100126 }
127 _assembly_path = _asm_glue.is_configured();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000128 break;
129 }
130 default:
131 {
132 ARM_COMPUTE_ERROR("Datatype not supported");
133 break;
134 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100135 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000136#endif /* __aarch64__ */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100137 if(!(_assembly_path || _run_vector_matrix_multiplication))
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100138 {
George Wort2d7e6832019-02-22 16:37:41 +0000139 matrix_a = &_tmp_a;
140 matrix_b = &_tmp_b;
141
142 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100143 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 +0000144 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
Georgios Pinitas02acf012019-03-19 10:49:03 +0000145 TensorInfo b_info(compute_transpose1xW_shape(*b->info()), 1, b->info()->data_type(), b->info()->quantization_info());
George Wort2d7e6832019-02-22 16:37:41 +0000146 _tmp_a.allocator()->init(a_info);
147 _tmp_b.allocator()->init(b_info);
148 _memory_group.manage(&_tmp_a);
149 if(!_reshape_b_only_on_first_run)
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100150 {
George Wort2d7e6832019-02-22 16:37:41 +0000151 _memory_group.manage(&_tmp_b);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100152 }
George Wort2d7e6832019-02-22 16:37:41 +0000153
154 // Configure interleave kernel
SiCongLi2e5fd632020-03-02 15:39:15 +0000155 _mtx_a_reshape_kernel.configure(a_to_use, &_tmp_a);
Gian Marcoc7f9b892017-11-30 14:31:13 +0000156
George Wort2d7e6832019-02-22 16:37:41 +0000157 // Configure transpose kernel
SiCongLi2e5fd632020-03-02 15:39:15 +0000158 _mtx_b_reshape_kernel.configure(b, &_tmp_b);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000159 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100160
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100161 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000162 {
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000163 // Build reduction info
164 const GEMMLowpReductionKernelInfo reduction_info(a_to_use->info()->dimension(0), false, 0, false);
165
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100166 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
167 if(_a_offset != 0)
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100168 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100169 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
170
171 _vector_sum_col.allocator()->init(info_vector_sum_col);
172 if(!_reshape_b_only_on_first_run)
173 {
174 _memory_group.manage(&_vector_sum_col);
175 }
176
177 // Configure Matrix B reduction kernel
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000178 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, reduction_info);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100179 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000180
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100181 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
182 if(_b_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000183 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100184 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a_to_use->info()), 1, DataType::S32);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100185
186 _vector_sum_row.allocator()->init(info_vector_sum_row);
187 _memory_group.manage(&_vector_sum_row);
188
189 // Configure matrix A reduction kernel
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000190 _mtx_a_reduction_kernel.configure(a_to_use, &_vector_sum_row, reduction_info);
George Wort2d7e6832019-02-22 16:37:41 +0000191 }
192
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100193 if(_fuse_output_stage)
George Wort2d7e6832019-02-22 16:37:41 +0000194 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100195 // Configure matrix multiply kernel
196 if(!_assembly_path)
197 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000198 _mm_kernel.configure(matrix_a, matrix_b, &_mm_result_s32);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100199 }
200
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100201 _offset_contribution_output_stage_kernel.configure(&_mm_result_s32,
202 _a_offset == 0 ? nullptr : &_vector_sum_col,
203 _b_offset == 0 ? nullptr : &_vector_sum_row, c,
204 _flip_signedness ? &_signed_output : output,
205 a->info()->dimension(0),
206 _a_offset, _b_offset, info.gemmlowp_output_stage());
207
208 if(_flip_signedness)
209 {
210 _convert_from_signed_asymm.configure(&_signed_output, output);
211 }
George Wort2d7e6832019-02-22 16:37:41 +0000212 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100213 else
214 {
215 // Configure matrix multiply kernel
216 if(!_assembly_path)
217 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000218 _mm_kernel.configure(matrix_a, matrix_b, output);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100219 }
220 // Configure offset contribution kernel
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100221 _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 +0100222 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000223
SiCongLi2e5fd632020-03-02 15:39:15 +0000224 // Configure activation
225 const ActivationLayerInfo &activation = gemm_info.activation_info();
226 _run_activation = activation.enabled() && (!_assembly_path || (_assembly_path && !NEGEMMAssemblyDispatch::is_activation_supported(activation)));
227 if(_run_activation)
228 {
229 _activation_func.configure(output, nullptr, activation);
230 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100231 }
232
Gian Marcoe75a02b2017-11-08 12:24:09 +0000233 // Allocate tensors
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100234 if(!_assembly_path && !_run_vector_matrix_multiplication)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000235 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000236 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100237 if(!_reshape_b_only_on_first_run)
238 {
239 _tmp_b.allocator()->allocate();
240 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000241 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000242
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100243 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000244 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100245 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
246 {
247 _vector_sum_col.allocator()->allocate();
248 }
249
250 if(_b_offset != 0)
251 {
252 _vector_sum_row.allocator()->allocate();
253 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000254 }
255
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100256 if(_fuse_output_stage)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000257 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100258 _mm_result_s32.allocator()->allocate();
Gian Marcoe75a02b2017-11-08 12:24:09 +0000259 }
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100260
261 if(_flip_signedness)
262 {
263 _signed_a.allocator()->allocate();
264 _signed_output.allocator()->allocate();
265 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100266}
267
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100268Status 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 +0000269{
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000270 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000271 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(b, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8, DataType::QSYMM8_PER_CHANNEL);
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000272 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 +0000273 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 +0000274 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
275 "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 +0700276 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
277 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 +0000278
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100279 GEMMInfo info = gemm_info;
George Wort2d7e6832019-02-22 16:37:41 +0000280 const ITensorInfo *matrix_a_info = a;
281 const ITensorInfo *matrix_b_info = b;
282
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100283 const ITensorInfo *a_to_use = a;
284
George Wort2d7e6832019-02-22 16:37:41 +0000285 TensorInfo tmp_a_info{};
286 TensorInfo tmp_b_info{};
287 TensorInfo mm_result_s32_info{};
288
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100289 int32_t a_offset = a->quantization_info().uniform().offset;
290 int32_t b_offset = b->quantization_info().uniform().offset;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000291
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100292 bool fuse_output_stage = info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE;
George Wort2d7e6832019-02-22 16:37:41 +0000293 if(fuse_output_stage)
294 {
295 auto_init_if_empty(mm_result_s32_info, a->clone()->set_tensor_shape(output->tensor_shape()).set_data_type(DataType::S32));
296 }
297
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100298 // Convert QASYMM8->QASYMM8_SIGNED
299 TensorInfo signed_a{};
300 TensorInfo signed_output{};
301 bool flip_signedness = is_data_type_quantized_per_channel(b->data_type()) && (a->data_type() == DataType::QASYMM8) && info.reshape_b_only_on_first_run();
302 if(flip_signedness)
303 {
304 const int32_t offset_correction = 128;
305 const DataType dt = DataType::QASYMM8_SIGNED;
306 const UniformQuantizationInfo iqinfo = a_to_use->quantization_info().uniform();
307
308 signed_a = a_to_use->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction));
309 ARM_COMPUTE_RETURN_ON_ERROR(NEConvertQuantizedSignednessKernel::validate(a_to_use, &signed_a));
310 a_to_use = &signed_a;
311 a_offset = signed_a.quantization_info().uniform().offset;
312
313 const UniformQuantizationInfo oqinfo = output->quantization_info().uniform();
314 signed_output = output->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction));
315
316 // Output stage correction
317 GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage();
318 output_stage_corr.gemmlowp_offset = signed_output.quantization_info().uniform().offset;
319 output_stage_corr.gemmlowp_min_bound -= offset_correction;
320 output_stage_corr.gemmlowp_max_bound -= offset_correction;
321 info.set_gemmlowp_output_stage(output_stage_corr);
Georgios Pinitas63d4dbd2019-11-08 11:51:56 +0000322
323 // Update matrix a
324 matrix_a_info = &signed_a;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100325 }
326
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000327 // Check if we need to run the optimized assembly kernel
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100328 bool run_optimised = false;
329 bool run_optimised_requantized = false;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100330 if(a_to_use->data_type() == DataType::QASYMM8 && info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100331 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100332 run_optimised = bool(NEGEMMAssemblyDispatch::validate(a_to_use, b, c, output, gemm_info));
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100333 run_optimised_requantized = run_optimised;
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000334
335 const UniformQuantizationInfo a_qinfo = a_to_use->quantization_info().uniform();
336 const QuantizationInfo b_qinfo = b->quantization_info();
337 const UniformQuantizationInfo output_qinfo = output->quantization_info().uniform();
338 for(auto const s : b_qinfo.scale())
339 {
340 const float fmultipler = a_qinfo.scale * s / output_qinfo.scale;
341 if(fmultipler > 1.f)
342 {
343 run_optimised_requantized = false;
344 break;
345 }
346 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100347 }
348 else
349 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100350 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 +0100351 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000352
353 if(run_optimised)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000354 {
George Wort2d7e6832019-02-22 16:37:41 +0000355 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100356 if(info.depth_output_gemm3d() != 0)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000357 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100358 if(info.reinterpret_input_as_3d())
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000359 {
George Wort2d7e6832019-02-22 16:37:41 +0000360 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
361 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000362 }
363 else
364 {
George Wort2d7e6832019-02-22 16:37:41 +0000365 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000366 }
367 }
George Wort2d7e6832019-02-22 16:37:41 +0000368 else
369 {
370 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
371 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000372 }
373 else
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000374 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100375 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.reinterpret_input_as_3d(), "NEGEMM cannot reinterpret the input tensor as 3D");
376 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 +0000377
378 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
379 if(!run_vector_matrix_multiplication)
380 {
George Wort2d7e6832019-02-22 16:37:41 +0000381 matrix_a_info = &tmp_a_info;
382 matrix_b_info = &tmp_b_info;
383
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000384 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
385 TensorShape shape_tmp_a = a->tensor_shape();
386 shape_tmp_a.set(0, a->dimension(0) * 4);
387 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
388
389 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
390 TensorShape shape_tmp_b = b->tensor_shape();
391 shape_tmp_b.set(0, b->dimension(1) * 16);
392 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
393
George Wort2d7e6832019-02-22 16:37:41 +0000394 // Validate interleave kernel
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100395 auto_init_if_empty(tmp_a_info, a_to_use->clone()->set_tensor_shape(shape_tmp_a));
George Wort2d7e6832019-02-22 16:37:41 +0000396 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(shape_tmp_b));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000397
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100398 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a_to_use, &tmp_a_info));
George Wort2d7e6832019-02-22 16:37:41 +0000399 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &tmp_b_info));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000400 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000401 }
402
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100403 if(!run_optimised_requantized)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000404 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100405 TensorInfo info_vector_sum_col{};
406 TensorInfo info_vector_sum_row{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000407
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000408 const GEMMLowpReductionKernelInfo reduction_info(a_to_use->dimension(0), false, 0, false);
409
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100410 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
411 if(a_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000412 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100413 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
414
415 // Configure Matrix B reduction kernel
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000416 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, reduction_info));
George Wort2d7e6832019-02-22 16:37:41 +0000417 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000418
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100419 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
420 if(b_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000421 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100422 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
423
424 // Configure matrix A reduction kernel
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000425 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(a_to_use, &info_vector_sum_row, reduction_info));
George Wort2d7e6832019-02-22 16:37:41 +0000426 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100427
428 if(fuse_output_stage)
429 {
430 if(!run_optimised)
431 {
432 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info));
433 }
434
435 // Validate offset contribution kernel
436 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info,
437 a_offset == 0 ? nullptr : &info_vector_sum_col,
438 b_offset == 0 ? nullptr : &info_vector_sum_row,
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100439 c,
440 flip_signedness ? &signed_output : output,
441 a_offset, b_offset,
442 info.gemmlowp_output_stage()));
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100443 }
444 else
445 {
446 if(!run_optimised)
447 {
448 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output));
449 }
450 // Validate offset contribution kernel
451 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionKernel::validate(output,
452 a_offset == 0 ? nullptr : &info_vector_sum_col,
453 b_offset == 0 ? nullptr : &info_vector_sum_row,
454 a_offset, b_offset));
455 }
George Wort2d7e6832019-02-22 16:37:41 +0000456 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100457
458 // Validate activation
459 const ActivationLayerInfo &activation = gemm_info.activation_info();
460 if(activation.enabled())
461 {
462 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, activation));
463 }
464
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000465 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000466}
467
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100468void NEGEMMLowpMatrixMultiplyCore::run()
469{
Georgios Pinitas72219332018-06-05 14:56:06 +0100470 prepare();
471
Georgios Pinitasda953f22019-04-02 17:27:03 +0100472 MemoryGroupResourceScope scope_mg(_memory_group);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100473
Georgios Pinitas63d4dbd2019-11-08 11:51:56 +0000474 // Convert QASYMM8->QASYMM8_SIGNED
475 if(_flip_signedness)
476 {
477 NEScheduler::get().schedule(&_convert_to_signed_asymm, Window::DimY);
478 }
479
Georgios Pinitas72219332018-06-05 14:56:06 +0100480 // Run GEMM
Anthony Barbiereaefd002018-07-20 17:49:35 +0100481 if(_asm_glue.is_configured())
Pablo Telloeb82fd22018-02-23 13:43:50 +0000482 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100483 _asm_glue.run();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000484 }
485 else
486 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000487 if(!_run_vector_matrix_multiplication)
488 {
489 // Run interleave kernel
490 NEScheduler::get().schedule(&_mtx_a_reshape_kernel, Window::DimY);
491
492 if(!_reshape_b_only_on_first_run)
493 {
494 // Run transpose kernel
495 NEScheduler::get().schedule(&_mtx_b_reshape_kernel, Window::DimY);
496 }
497 }
498 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000499 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100500
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100501 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000502 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100503 // Run matrix A reduction kernel only if _b_offset is not equal to 0
504 if(_b_offset != 0)
505 {
506 NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX);
507 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000508
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100509 // Run matrix B reduction kernel only if _a_offset is not equal to 0
510 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
511 {
512 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
513 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000514
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100515 if(_fuse_output_stage)
516 {
517 // Run offset contribution kernel
518 NEScheduler::get().schedule(&_offset_contribution_output_stage_kernel, Window::DimY);
519 }
520 else
521 {
522 // Run offset contribution kernel
523 NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY);
524 }
George Wort2d7e6832019-02-22 16:37:41 +0000525 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100526
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100527 // Convert QASYMM8_SIGNED->QASYMM8
528 if(_flip_signedness)
529 {
530 NEScheduler::get().schedule(&_convert_from_signed_asymm, Window::DimY);
531 }
532
SiCongLi2e5fd632020-03-02 15:39:15 +0000533 // Run fused activation unless already run in the fused assembly
534 if(_run_activation && !_fused_assembly_path)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100535 {
536 _activation_func.run();
537 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100538}
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100539
Georgios Pinitas72219332018-06-05 14:56:06 +0100540void NEGEMMLowpMatrixMultiplyCore::prepare()
541{
542 if(!_is_prepared)
543 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000544 const bool original_b_managed_by_weights_manager = _weights_manager && _weights_manager->are_weights_managed(_original_b);
Georgios Pinitas72219332018-06-05 14:56:06 +0100545 // Run assembly reshape
SiCongLi2e5fd632020-03-02 15:39:15 +0000546 if(_asm_glue.is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100547 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000548 if(!original_b_managed_by_weights_manager)
549 {
550 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
551 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100552
Anthony Barbiereaefd002018-07-20 17:49:35 +0100553 _asm_glue.prepare();
SiCongLi2e5fd632020-03-02 15:39:15 +0000554 if(!original_b_managed_by_weights_manager)
555 {
556 _original_b->mark_as_unused();
557 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100558 }
559 // Run non-assembly reshape
SiCongLi2e5fd632020-03-02 15:39:15 +0000560 else if(_reshape_b_only_on_first_run && !_run_vector_matrix_multiplication && !_asm_glue.is_configured())
Georgios Pinitas72219332018-06-05 14:56:06 +0100561 {
SiCongLi2e5fd632020-03-02 15:39:15 +0000562 if(!original_b_managed_by_weights_manager)
563 {
564 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
565 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100566
567 // Run reshape kernel and mark original weights tensor as unused
568 _tmp_b.allocator()->allocate();
SiCongLi2e5fd632020-03-02 15:39:15 +0000569 NEScheduler::get().schedule(&_mtx_b_reshape_kernel, Window::DimY);
570 if(!original_b_managed_by_weights_manager)
571 {
572 _original_b->mark_as_unused();
573 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100574 }
575
576 // Run matrix B reduction kernel only if _a_offset is not equal to 0
Michalis Spyrouc8bdeaa2020-05-26 09:59:18 +0100577 if(!_fused_assembly_path && _a_offset != 0 && _reshape_b_only_on_first_run)
Georgios Pinitas72219332018-06-05 14:56:06 +0100578 {
579 _vector_sum_col.allocator()->allocate();
580 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
581 }
582
583 _is_prepared = true;
584 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000585}
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000586} // namespace arm_compute