blob: 01a99f7aca0d81779d2d72714554ce97256c745d [file] [log] [blame]
Gian Marco Iodiceab182122017-10-09 15:05:40 +01001/*
Georgios Pinitase46a7be2019-02-18 15:16:14 +00002 * Copyright (c) 2017-2019 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(),
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010045 _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(),
46 _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),
47 _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 +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 // Clear state
Anthony Barbier71d9b572018-07-06 17:05:59 +010062 _mtx_a_reshape_kernel = nullptr;
63 _mtx_b_reshape_kernel = nullptr;
Georgios Pinitas72219332018-06-05 14:56:06 +010064
65 // Set internal variables
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010066 _a_offset = a->info()->quantization_info().uniform().offset;
67 _b_offset = b->info()->quantization_info().uniform().offset;
Gian Marcoc7f9b892017-11-30 14:31:13 +000068 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010069 _reshape_b_only_on_first_run = info.reshape_b_only_on_first_run();
Georgios Pinitas72219332018-06-05 14:56:06 +010070 _is_prepared = false;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010071 _fused_assembly_path = false;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010072 _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 +010073 _original_b = b;
Gian Marcoe75a02b2017-11-08 12:24:09 +000074
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010075 const ITensor *a_to_use = a;
76
77 // Convert to QASYMM8 -> QASYMM8_SIGNED and back
78 if(_flip_signedness)
79 {
80 const int32_t offset_correction = 128;
81 const DataType dt = DataType::QASYMM8_SIGNED;
82 const UniformQuantizationInfo iqinfo = a_to_use->info()->quantization_info().uniform();
83
84 _signed_a.allocator()->init(a_to_use->info()->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction)));
85 _memory_group.manage(&_signed_a);
86 _convert_to_signed_asymm.configure(a_to_use, &_signed_a);
87 a_to_use = &_signed_a;
88 _a_offset = _signed_a.info()->quantization_info().uniform().offset;
89
90 const UniformQuantizationInfo oqinfo = output->info()->quantization_info().uniform();
91 _memory_group.manage(&_signed_output);
92 _signed_output.allocator()->init(output->info()->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction)));
93
94 // Output stage correction
95 GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage();
96 output_stage_corr.gemmlowp_offset = _signed_output.info()->quantization_info().uniform().offset;
97 output_stage_corr.gemmlowp_min_bound -= offset_correction;
98 output_stage_corr.gemmlowp_max_bound -= offset_correction;
99 info.set_gemmlowp_output_stage(output_stage_corr);
100 }
101
George Wort2d7e6832019-02-22 16:37:41 +0000102 // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100103 if(info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE)
George Wort2d7e6832019-02-22 16:37:41 +0000104 {
105 _fuse_output_stage = true;
George Wort2d7e6832019-02-22 16:37:41 +0000106 _memory_group.manage(&_mm_result_s32);
George Wort2d7e6832019-02-22 16:37:41 +0000107 TensorInfo info_mm_result_s32(output->info()->tensor_shape(), 1, DataType::S32);
George Wort2d7e6832019-02-22 16:37:41 +0000108 _mm_result_s32.allocator()->init(info_mm_result_s32);
109 }
110
Pablo Telloeb82fd22018-02-23 13:43:50 +0000111#ifdef __aarch64__
112 switch(a->info()->data_type())
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100113 {
Pablo Tello66c656a2018-03-15 10:34:58 +0000114 case DataType::QASYMM8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100115 case DataType::QASYMM8_SIGNED:
Pablo Telloeb82fd22018-02-23 13:43:50 +0000116 case DataType::U8:
Anthony Barbiereaefd002018-07-20 17:49:35 +0100117 case DataType::S8:
Pablo Telloeb82fd22018-02-23 13:43:50 +0000118 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100119 if(a_to_use->info()->data_type() == DataType::QASYMM8 && info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100120 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100121 _asm_glue.configure(a_to_use, b, c, output, gemm_info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100122 _fused_assembly_path = _asm_glue.is_configured();
123 }
124 else
125 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100126 _asm_glue.configure(a_to_use, b, nullptr, _fuse_output_stage ? &_mm_result_s32 : output, gemm_info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100127 }
128 _assembly_path = _asm_glue.is_configured();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000129 break;
130 }
131 default:
132 {
133 ARM_COMPUTE_ERROR("Datatype not supported");
134 break;
135 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100136 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000137#endif /* __aarch64__ */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100138 if(!(_assembly_path || _run_vector_matrix_multiplication))
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100139 {
George Wort2d7e6832019-02-22 16:37:41 +0000140 matrix_a = &_tmp_a;
141 matrix_b = &_tmp_b;
142
143 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100144 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 +0000145 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
Georgios Pinitas02acf012019-03-19 10:49:03 +0000146 TensorInfo b_info(compute_transpose1xW_shape(*b->info()), 1, b->info()->data_type(), b->info()->quantization_info());
George Wort2d7e6832019-02-22 16:37:41 +0000147 _tmp_a.allocator()->init(a_info);
148 _tmp_b.allocator()->init(b_info);
149 _memory_group.manage(&_tmp_a);
150 if(!_reshape_b_only_on_first_run)
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100151 {
George Wort2d7e6832019-02-22 16:37:41 +0000152 _memory_group.manage(&_tmp_b);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100153 }
George Wort2d7e6832019-02-22 16:37:41 +0000154
155 // Configure interleave kernel
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100156 {
George Wort2d7e6832019-02-22 16:37:41 +0000157 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100158 k->configure(a_to_use, &_tmp_a);
George Wort2d7e6832019-02-22 16:37:41 +0000159 _mtx_a_reshape_kernel = std::move(k);
160 }
Gian Marcoc7f9b892017-11-30 14:31:13 +0000161
George Wort2d7e6832019-02-22 16:37:41 +0000162 // Configure transpose kernel
163 {
164 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
165 k->configure(b, &_tmp_b);
166 _mtx_b_reshape_kernel = std::move(k);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100167 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000168 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100169
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100170 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000171 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100172 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
173 if(_a_offset != 0)
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100174 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100175 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
176
177 _vector_sum_col.allocator()->init(info_vector_sum_col);
178 if(!_reshape_b_only_on_first_run)
179 {
180 _memory_group.manage(&_vector_sum_col);
181 }
182
183 // Configure Matrix B reduction kernel
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100184 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, a_to_use->info()->dimension(0), false);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100185 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000186
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100187 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
188 if(_b_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000189 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100190 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a_to_use->info()), 1, DataType::S32);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100191
192 _vector_sum_row.allocator()->init(info_vector_sum_row);
193 _memory_group.manage(&_vector_sum_row);
194
195 // Configure matrix A reduction kernel
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100196 _mtx_a_reduction_kernel.configure(a_to_use, &_vector_sum_row, a_to_use->info()->dimension(0), false);
George Wort2d7e6832019-02-22 16:37:41 +0000197 }
198
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100199 if(_fuse_output_stage)
George Wort2d7e6832019-02-22 16:37:41 +0000200 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100201 // Configure matrix multiply kernel
202 if(!_assembly_path)
203 {
204 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
205 k->configure(matrix_a, matrix_b, &_mm_result_s32);
206 _mm_kernel = std::move(k);
207 }
208
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100209 _offset_contribution_output_stage_kernel.configure(&_mm_result_s32,
210 _a_offset == 0 ? nullptr : &_vector_sum_col,
211 _b_offset == 0 ? nullptr : &_vector_sum_row, c,
212 _flip_signedness ? &_signed_output : output,
213 a->info()->dimension(0),
214 _a_offset, _b_offset, info.gemmlowp_output_stage());
215
216 if(_flip_signedness)
217 {
218 _convert_from_signed_asymm.configure(&_signed_output, output);
219 }
George Wort2d7e6832019-02-22 16:37:41 +0000220 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100221 else
222 {
223 // Configure matrix multiply kernel
224 if(!_assembly_path)
225 {
226 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
227 k->configure(matrix_a, matrix_b, output);
228 _mm_kernel = std::move(k);
229 }
230 // Configure offset contribution kernel
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100231 _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 +0100232 }
George Wort2d7e6832019-02-22 16:37:41 +0000233 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000234
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100235 // Configure activation
236 const ActivationLayerInfo &activation = gemm_info.activation_info();
237 _run_activation = activation.enabled() && (!_assembly_path || (_assembly_path && !NEGEMMAssemblyDispatch::is_activation_supported(activation)));
238 if(_run_activation)
239 {
240 _activation_func.configure(output, nullptr, activation);
241 }
242
Gian Marcoe75a02b2017-11-08 12:24:09 +0000243 // Allocate tensors
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100244 if(!_assembly_path && !_run_vector_matrix_multiplication)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000245 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000246 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100247 if(!_reshape_b_only_on_first_run)
248 {
249 _tmp_b.allocator()->allocate();
250 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000251 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000252
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100253 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000254 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100255 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
256 {
257 _vector_sum_col.allocator()->allocate();
258 }
259
260 if(_b_offset != 0)
261 {
262 _vector_sum_row.allocator()->allocate();
263 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000264 }
265
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100266 if(_fuse_output_stage)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000267 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100268 _mm_result_s32.allocator()->allocate();
Gian Marcoe75a02b2017-11-08 12:24:09 +0000269 }
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100270
271 if(_flip_signedness)
272 {
273 _signed_a.allocator()->allocate();
274 _signed_output.allocator()->allocate();
275 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100276}
277
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100278Status 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 +0000279{
280 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100281 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(b, 1, DataType::QASYMM8, DataType::QSYMM8_PER_CHANNEL);
George Wort2d7e6832019-02-22 16:37:41 +0000282 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32, DataType::QASYMM8);
George Wort2d7e6832019-02-22 16:37:41 +0000283 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 +0000284 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
285 "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 +0700286 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
287 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 +0000288
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100289 GEMMInfo info = gemm_info;
George Wort2d7e6832019-02-22 16:37:41 +0000290 const ITensorInfo *matrix_a_info = a;
291 const ITensorInfo *matrix_b_info = b;
292
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100293 const ITensorInfo *a_to_use = a;
294
George Wort2d7e6832019-02-22 16:37:41 +0000295 TensorInfo tmp_a_info{};
296 TensorInfo tmp_b_info{};
297 TensorInfo mm_result_s32_info{};
298
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100299 int32_t a_offset = a->quantization_info().uniform().offset;
300 int32_t b_offset = b->quantization_info().uniform().offset;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000301
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100302 bool fuse_output_stage = info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE;
George Wort2d7e6832019-02-22 16:37:41 +0000303 if(fuse_output_stage)
304 {
305 auto_init_if_empty(mm_result_s32_info, a->clone()->set_tensor_shape(output->tensor_shape()).set_data_type(DataType::S32));
306 }
307
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100308 // Convert QASYMM8->QASYMM8_SIGNED
309 TensorInfo signed_a{};
310 TensorInfo signed_output{};
311 bool flip_signedness = is_data_type_quantized_per_channel(b->data_type()) && (a->data_type() == DataType::QASYMM8) && info.reshape_b_only_on_first_run();
312 if(flip_signedness)
313 {
314 const int32_t offset_correction = 128;
315 const DataType dt = DataType::QASYMM8_SIGNED;
316 const UniformQuantizationInfo iqinfo = a_to_use->quantization_info().uniform();
317
318 signed_a = a_to_use->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction));
319 ARM_COMPUTE_RETURN_ON_ERROR(NEConvertQuantizedSignednessKernel::validate(a_to_use, &signed_a));
320 a_to_use = &signed_a;
321 a_offset = signed_a.quantization_info().uniform().offset;
322
323 const UniformQuantizationInfo oqinfo = output->quantization_info().uniform();
324 signed_output = output->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction));
325
326 // Output stage correction
327 GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage();
328 output_stage_corr.gemmlowp_offset = signed_output.quantization_info().uniform().offset;
329 output_stage_corr.gemmlowp_min_bound -= offset_correction;
330 output_stage_corr.gemmlowp_max_bound -= offset_correction;
331 info.set_gemmlowp_output_stage(output_stage_corr);
332 }
333
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000334 // Check if we need to run the optimized assembly kernel
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100335 bool run_optimised = false;
336 bool run_optimised_requantized = false;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100337 if(a_to_use->data_type() == DataType::QASYMM8 && info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100338 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100339 run_optimised = bool(NEGEMMAssemblyDispatch::validate(a_to_use, b, c, output, gemm_info));
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100340 run_optimised_requantized = run_optimised;
341 }
342 else
343 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100344 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 +0100345 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000346
347 if(run_optimised)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000348 {
George Wort2d7e6832019-02-22 16:37:41 +0000349 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100350 if(info.depth_output_gemm3d() != 0)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000351 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100352 if(info.reinterpret_input_as_3d())
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000353 {
George Wort2d7e6832019-02-22 16:37:41 +0000354 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
355 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000356 }
357 else
358 {
George Wort2d7e6832019-02-22 16:37:41 +0000359 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000360 }
361 }
George Wort2d7e6832019-02-22 16:37:41 +0000362 else
363 {
364 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
365 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000366 }
367 else
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000368 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100369 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.reinterpret_input_as_3d(), "NEGEMM cannot reinterpret the input tensor as 3D");
370 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 +0000371
372 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
373 if(!run_vector_matrix_multiplication)
374 {
George Wort2d7e6832019-02-22 16:37:41 +0000375 matrix_a_info = &tmp_a_info;
376 matrix_b_info = &tmp_b_info;
377
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000378 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
379 TensorShape shape_tmp_a = a->tensor_shape();
380 shape_tmp_a.set(0, a->dimension(0) * 4);
381 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
382
383 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
384 TensorShape shape_tmp_b = b->tensor_shape();
385 shape_tmp_b.set(0, b->dimension(1) * 16);
386 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
387
George Wort2d7e6832019-02-22 16:37:41 +0000388 // Validate interleave kernel
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100389 auto_init_if_empty(tmp_a_info, a_to_use->clone()->set_tensor_shape(shape_tmp_a));
George Wort2d7e6832019-02-22 16:37:41 +0000390 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(shape_tmp_b));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000391
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100392 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a_to_use, &tmp_a_info));
George Wort2d7e6832019-02-22 16:37:41 +0000393 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &tmp_b_info));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000394 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000395 }
396
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100397 if(!run_optimised_requantized)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000398 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100399 TensorInfo info_vector_sum_col{};
400 TensorInfo info_vector_sum_row{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000401
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100402 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
403 if(a_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000404 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100405 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
406
407 // Configure Matrix B reduction kernel
408 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, a->dimension(0), false));
George Wort2d7e6832019-02-22 16:37:41 +0000409 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000410
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100411 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
412 if(b_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000413 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100414 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
415
416 // Configure matrix A reduction kernel
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100417 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(a_to_use, &info_vector_sum_row, a->dimension(0), false));
George Wort2d7e6832019-02-22 16:37:41 +0000418 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100419
420 if(fuse_output_stage)
421 {
422 if(!run_optimised)
423 {
424 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info));
425 }
426
427 // Validate offset contribution kernel
428 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info,
429 a_offset == 0 ? nullptr : &info_vector_sum_col,
430 b_offset == 0 ? nullptr : &info_vector_sum_row,
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100431 c,
432 flip_signedness ? &signed_output : output,
433 a_offset, b_offset,
434 info.gemmlowp_output_stage()));
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100435 }
436 else
437 {
438 if(!run_optimised)
439 {
440 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output));
441 }
442 // Validate offset contribution kernel
443 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionKernel::validate(output,
444 a_offset == 0 ? nullptr : &info_vector_sum_col,
445 b_offset == 0 ? nullptr : &info_vector_sum_row,
446 a_offset, b_offset));
447 }
George Wort2d7e6832019-02-22 16:37:41 +0000448 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100449
450 // Validate activation
451 const ActivationLayerInfo &activation = gemm_info.activation_info();
452 if(activation.enabled())
453 {
454 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, activation));
455 }
456
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000457 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000458}
459
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100460void NEGEMMLowpMatrixMultiplyCore::run()
461{
Georgios Pinitas72219332018-06-05 14:56:06 +0100462 prepare();
463
Georgios Pinitasda953f22019-04-02 17:27:03 +0100464 MemoryGroupResourceScope scope_mg(_memory_group);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100465
Georgios Pinitas72219332018-06-05 14:56:06 +0100466 // Reshape inputs
467 if(_mtx_a_reshape_kernel)
Pablo Tello6ff12a02017-11-02 16:09:35 +0000468 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100469 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
470 }
471 if(_mtx_b_reshape_kernel && !_reshape_b_only_on_first_run)
472 {
473 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
Pablo Tello6ff12a02017-11-02 16:09:35 +0000474 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100475
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100476 // Convert QASYMM8->QASYMM8_SIGNED
477 if(_flip_signedness)
478 {
479 NEScheduler::get().schedule(&_convert_to_signed_asymm, Window::DimY);
480 }
481
Georgios Pinitas72219332018-06-05 14:56:06 +0100482 // Run GEMM
Anthony Barbiereaefd002018-07-20 17:49:35 +0100483 if(_asm_glue.is_configured())
Pablo Telloeb82fd22018-02-23 13:43:50 +0000484 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100485 _asm_glue.run();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000486 }
487 else
488 {
489 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
490 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100491
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100492 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000493 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100494 // Run matrix A reduction kernel only if _b_offset is not equal to 0
495 if(_b_offset != 0)
496 {
497 NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX);
498 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000499
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100500 // Run matrix B reduction kernel only if _a_offset is not equal to 0
501 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
502 {
503 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
504 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000505
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100506 if(_fuse_output_stage)
507 {
508 // Run offset contribution kernel
509 NEScheduler::get().schedule(&_offset_contribution_output_stage_kernel, Window::DimY);
510 }
511 else
512 {
513 // Run offset contribution kernel
514 NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY);
515 }
George Wort2d7e6832019-02-22 16:37:41 +0000516 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100517
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100518 // Convert QASYMM8_SIGNED->QASYMM8
519 if(_flip_signedness)
520 {
521 NEScheduler::get().schedule(&_convert_from_signed_asymm, Window::DimY);
522 }
523
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100524 // Run fused activation
525 if(_run_activation)
526 {
527 _activation_func.run();
528 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100529}
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100530
Georgios Pinitas72219332018-06-05 14:56:06 +0100531void NEGEMMLowpMatrixMultiplyCore::prepare()
532{
533 if(!_is_prepared)
534 {
535 // Run assembly reshape
Anthony Barbiereaefd002018-07-20 17:49:35 +0100536 if(_asm_glue.is_configured() && _reshape_b_only_on_first_run)
Georgios Pinitas72219332018-06-05 14:56:06 +0100537 {
538 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
539
Anthony Barbiereaefd002018-07-20 17:49:35 +0100540 _asm_glue.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100541 _original_b->mark_as_unused();
542 }
543 // Run non-assembly reshape
544 else if(_mtx_b_reshape_kernel && _reshape_b_only_on_first_run)
545 {
546 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
547
548 // Run reshape kernel and mark original weights tensor as unused
549 _tmp_b.allocator()->allocate();
550 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
551 _original_b->mark_as_unused();
552 }
553
554 // Run matrix B reduction kernel only if _a_offset is not equal to 0
555 if(_a_offset != 0 && _reshape_b_only_on_first_run)
556 {
557 _vector_sum_col.allocator()->allocate();
558 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
559 }
560
561 _is_prepared = true;
562 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000563}