blob: 6dc5dd2a6578738fa4dd018f6375fea450d93b7e [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(),
George Wort2d7e6832019-02-22 16:37:41 +000045 _offset_contribution_kernel(), _offset_contribution_output_stage_kernel(), _vector_sum_col(), _vector_sum_row(), _tmp_a(), _tmp_b(), _mm_result_s32(), _original_b(nullptr), _a_offset(0), _b_offset(0),
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010046 _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)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010047{
48}
49
Gian Marco Iodice4b908652018-10-18 10:21:02 +010050void 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 +010051{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000052 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Gian Marco Iodice4b908652018-10-18 10:21:02 +010053 ARM_COMPUTE_UNUSED(c);
54 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 +010055
George Wort2d7e6832019-02-22 16:37:41 +000056 const ITensor *matrix_a = a;
57 const ITensor *matrix_b = b;
58
Georgios Pinitas72219332018-06-05 14:56:06 +010059 // Clear state
Anthony Barbier71d9b572018-07-06 17:05:59 +010060 _mtx_a_reshape_kernel = nullptr;
61 _mtx_b_reshape_kernel = nullptr;
Georgios Pinitas72219332018-06-05 14:56:06 +010062
63 // Set internal variables
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010064 _a_offset = a->info()->quantization_info().uniform().offset;
65 _b_offset = b->info()->quantization_info().uniform().offset;
Gian Marcoc7f9b892017-11-30 14:31:13 +000066 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Giorgio Arenabb54e4e2018-04-05 17:20:34 +010067 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Georgios Pinitas72219332018-06-05 14:56:06 +010068 _is_prepared = false;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010069 _fused_assembly_path = false;
Georgios Pinitas72219332018-06-05 14:56:06 +010070 _original_b = b;
Gian Marcoe75a02b2017-11-08 12:24:09 +000071
George Wort2d7e6832019-02-22 16:37:41 +000072 // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage
73 if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE)
74 {
75 _fuse_output_stage = true;
George Wort2d7e6832019-02-22 16:37:41 +000076 _memory_group.manage(&_mm_result_s32);
George Wort2d7e6832019-02-22 16:37:41 +000077 TensorInfo info_mm_result_s32(output->info()->tensor_shape(), 1, DataType::S32);
George Wort2d7e6832019-02-22 16:37:41 +000078 _mm_result_s32.allocator()->init(info_mm_result_s32);
79 }
80
Pablo Telloeb82fd22018-02-23 13:43:50 +000081#ifdef __aarch64__
82 switch(a->info()->data_type())
Gian Marco Iodiceab182122017-10-09 15:05:40 +010083 {
Pablo Tello66c656a2018-03-15 10:34:58 +000084 case DataType::QASYMM8:
Pablo Telloeb82fd22018-02-23 13:43:50 +000085 case DataType::U8:
Anthony Barbiereaefd002018-07-20 17:49:35 +010086 case DataType::S8:
Pablo Telloeb82fd22018-02-23 13:43:50 +000087 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010088 if(a->info()->data_type() == DataType::QASYMM8 && gemm_info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
89 {
90 _asm_glue.configure(a, b, c, output, 1.f, 0.f, gemm_info);
91 _fused_assembly_path = _asm_glue.is_configured();
92 }
93 else
94 {
95 _asm_glue.configure(a, b, nullptr, _fuse_output_stage ? &_mm_result_s32 : output, 1.f, 0.f, gemm_info);
96 }
97 _assembly_path = _asm_glue.is_configured();
Pablo Telloeb82fd22018-02-23 13:43:50 +000098 break;
99 }
100 default:
101 {
102 ARM_COMPUTE_ERROR("Datatype not supported");
103 break;
104 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100105 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000106#endif /* __aarch64__ */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100107 if(!(_assembly_path || _run_vector_matrix_multiplication))
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100108 {
George Wort2d7e6832019-02-22 16:37:41 +0000109 matrix_a = &_tmp_a;
110 matrix_b = &_tmp_b;
111
112 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
Georgios Pinitas02acf012019-03-19 10:49:03 +0000113 TensorInfo a_info(compute_interleaved_shape(*a->info()), 1, a->info()->data_type(), a->info()->quantization_info());
George Wort2d7e6832019-02-22 16:37:41 +0000114 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
Georgios Pinitas02acf012019-03-19 10:49:03 +0000115 TensorInfo b_info(compute_transpose1xW_shape(*b->info()), 1, b->info()->data_type(), b->info()->quantization_info());
George Wort2d7e6832019-02-22 16:37:41 +0000116 _tmp_a.allocator()->init(a_info);
117 _tmp_b.allocator()->init(b_info);
118 _memory_group.manage(&_tmp_a);
119 if(!_reshape_b_only_on_first_run)
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100120 {
George Wort2d7e6832019-02-22 16:37:41 +0000121 _memory_group.manage(&_tmp_b);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100122 }
George Wort2d7e6832019-02-22 16:37:41 +0000123
124 // Configure interleave kernel
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100125 {
George Wort2d7e6832019-02-22 16:37:41 +0000126 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
127 k->configure(a, &_tmp_a);
128 _mtx_a_reshape_kernel = std::move(k);
129 }
Gian Marcoc7f9b892017-11-30 14:31:13 +0000130
George Wort2d7e6832019-02-22 16:37:41 +0000131 // Configure transpose kernel
132 {
133 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
134 k->configure(b, &_tmp_b);
135 _mtx_b_reshape_kernel = std::move(k);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100136 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000137 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100138
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100139 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000140 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100141 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
142 if(_a_offset != 0)
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100143 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100144 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
145
146 _vector_sum_col.allocator()->init(info_vector_sum_col);
147 if(!_reshape_b_only_on_first_run)
148 {
149 _memory_group.manage(&_vector_sum_col);
150 }
151
152 // Configure Matrix B reduction kernel
153 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, a->info()->dimension(0), false);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100154 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000155
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100156 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
157 if(_b_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000158 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100159 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a->info()), 1, DataType::S32);
160
161 _vector_sum_row.allocator()->init(info_vector_sum_row);
162 _memory_group.manage(&_vector_sum_row);
163
164 // Configure matrix A reduction kernel
165 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row, a->info()->dimension(0), false);
George Wort2d7e6832019-02-22 16:37:41 +0000166 }
167
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100168 if(_fuse_output_stage)
George Wort2d7e6832019-02-22 16:37:41 +0000169 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100170 // Configure matrix multiply kernel
171 if(!_assembly_path)
172 {
173 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
174 k->configure(matrix_a, matrix_b, &_mm_result_s32);
175 _mm_kernel = std::move(k);
176 }
177
178 _offset_contribution_output_stage_kernel.configure(&_mm_result_s32, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, c, output, a->info()->dimension(0),
179 _a_offset, _b_offset, gemm_info.gemmlowp_output_stage());
George Wort2d7e6832019-02-22 16:37:41 +0000180 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100181 else
182 {
183 // Configure matrix multiply kernel
184 if(!_assembly_path)
185 {
186 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
187 k->configure(matrix_a, matrix_b, output);
188 _mm_kernel = std::move(k);
189 }
190 // Configure offset contribution kernel
191 _offset_contribution_kernel.configure(output, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, a->info()->dimension(0), _a_offset, _b_offset);
192 }
George Wort2d7e6832019-02-22 16:37:41 +0000193 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000194
195 // Allocate tensors
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100196 if(!_assembly_path && !_run_vector_matrix_multiplication)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000197 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000198 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100199 if(!_reshape_b_only_on_first_run)
200 {
201 _tmp_b.allocator()->allocate();
202 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000203 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000204
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100205 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000206 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100207 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
208 {
209 _vector_sum_col.allocator()->allocate();
210 }
211
212 if(_b_offset != 0)
213 {
214 _vector_sum_row.allocator()->allocate();
215 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000216 }
217
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100218 if(_fuse_output_stage)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000219 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100220 _mm_result_s32.allocator()->allocate();
Gian Marcoe75a02b2017-11-08 12:24:09 +0000221 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100222}
223
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100224Status 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 +0000225{
226 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
George Wort2d7e6832019-02-22 16:37:41 +0000227 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32, DataType::QASYMM8);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000228 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
George Wort2d7e6832019-02-22 16:37:41 +0000229 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 +0000230 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
231 "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 +0700232 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
233 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 +0000234
George Wort2d7e6832019-02-22 16:37:41 +0000235 const ITensorInfo *matrix_a_info = a;
236 const ITensorInfo *matrix_b_info = b;
237
238 TensorInfo tmp_a_info{};
239 TensorInfo tmp_b_info{};
240 TensorInfo mm_result_s32_info{};
241
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100242 int32_t a_offset = a->quantization_info().uniform().offset;
243 int32_t b_offset = b->quantization_info().uniform().offset;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000244
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100245 bool fuse_output_stage = gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE && a->data_type() != DataType::QASYMM8;
George Wort2d7e6832019-02-22 16:37:41 +0000246 if(fuse_output_stage)
247 {
248 auto_init_if_empty(mm_result_s32_info, a->clone()->set_tensor_shape(output->tensor_shape()).set_data_type(DataType::S32));
249 }
250
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000251 // Check if we need to run the optimized assembly kernel
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100252 bool run_optimised = false;
253 bool run_optimised_requantized = false;
254 if(is_data_type_quantized_asymmetric(a->data_type()))
255 {
256 run_optimised = bool(NEGEMMAssemblyDispatch::validate(a, b, c, output, 1.f, 0.f, gemm_info));
257 run_optimised_requantized = run_optimised;
258 }
259 else
260 {
261 run_optimised = bool(NEGEMMAssemblyDispatch::validate(a, b, nullptr, fuse_output_stage ? &mm_result_s32_info : output, 1.f, 0.f, gemm_info));
262 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000263
264 if(run_optimised)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000265 {
George Wort2d7e6832019-02-22 16:37:41 +0000266 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
267 if(gemm_info.depth_output_gemm3d() != 0)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000268 {
George Wort2d7e6832019-02-22 16:37:41 +0000269 if(gemm_info.reinterpret_input_as_3d())
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000270 {
George Wort2d7e6832019-02-22 16:37:41 +0000271 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
272 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000273 }
274 else
275 {
George Wort2d7e6832019-02-22 16:37:41 +0000276 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000277 }
278 }
George Wort2d7e6832019-02-22 16:37:41 +0000279 else
280 {
281 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
282 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000283 }
284 else
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000285 {
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000286 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.reinterpret_input_as_3d(), "NEGEMM cannot reinterpret the input tensor as 3D");
287 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.depth_output_gemm3d() != 0, "NEGEMM cannot reinterpret the output tensor as 3D");
288
289 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
290 if(!run_vector_matrix_multiplication)
291 {
George Wort2d7e6832019-02-22 16:37:41 +0000292 matrix_a_info = &tmp_a_info;
293 matrix_b_info = &tmp_b_info;
294
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000295 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
296 TensorShape shape_tmp_a = a->tensor_shape();
297 shape_tmp_a.set(0, a->dimension(0) * 4);
298 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
299
300 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
301 TensorShape shape_tmp_b = b->tensor_shape();
302 shape_tmp_b.set(0, b->dimension(1) * 16);
303 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
304
George Wort2d7e6832019-02-22 16:37:41 +0000305 // Validate interleave kernel
306 auto_init_if_empty(tmp_a_info, a->clone()->set_tensor_shape(shape_tmp_a));
307 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(shape_tmp_b));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000308
George Wort2d7e6832019-02-22 16:37:41 +0000309 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &tmp_a_info));
310 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &tmp_b_info));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000311 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000312 }
313
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100314 if(!run_optimised_requantized)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000315 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100316 TensorInfo info_vector_sum_col{};
317 TensorInfo info_vector_sum_row{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000318
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100319 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
320 if(a_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000321 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100322 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
323
324 // Configure Matrix B reduction kernel
325 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, a->dimension(0), false));
George Wort2d7e6832019-02-22 16:37:41 +0000326 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000327
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100328 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
329 if(b_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000330 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100331 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
332
333 // Configure matrix A reduction kernel
334 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row, a->dimension(0), false));
George Wort2d7e6832019-02-22 16:37:41 +0000335 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100336
337 if(fuse_output_stage)
338 {
339 if(!run_optimised)
340 {
341 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info));
342 }
343
344 // Validate offset contribution kernel
345 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info,
346 a_offset == 0 ? nullptr : &info_vector_sum_col,
347 b_offset == 0 ? nullptr : &info_vector_sum_row,
348 c, output, a_offset, b_offset,
349 gemm_info.gemmlowp_output_stage()));
350 }
351 else
352 {
353 if(!run_optimised)
354 {
355 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output));
356 }
357 // Validate offset contribution kernel
358 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionKernel::validate(output,
359 a_offset == 0 ? nullptr : &info_vector_sum_col,
360 b_offset == 0 ? nullptr : &info_vector_sum_row,
361 a_offset, b_offset));
362 }
George Wort2d7e6832019-02-22 16:37:41 +0000363 }
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000364 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000365}
366
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100367void NEGEMMLowpMatrixMultiplyCore::run()
368{
Georgios Pinitas72219332018-06-05 14:56:06 +0100369 prepare();
370
Georgios Pinitasda953f22019-04-02 17:27:03 +0100371 MemoryGroupResourceScope scope_mg(_memory_group);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100372
Georgios Pinitas72219332018-06-05 14:56:06 +0100373 // Reshape inputs
374 if(_mtx_a_reshape_kernel)
Pablo Tello6ff12a02017-11-02 16:09:35 +0000375 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100376 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
377 }
378 if(_mtx_b_reshape_kernel && !_reshape_b_only_on_first_run)
379 {
380 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
Pablo Tello6ff12a02017-11-02 16:09:35 +0000381 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100382
Georgios Pinitas72219332018-06-05 14:56:06 +0100383 // Run GEMM
Anthony Barbiereaefd002018-07-20 17:49:35 +0100384 if(_asm_glue.is_configured())
Pablo Telloeb82fd22018-02-23 13:43:50 +0000385 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100386 _asm_glue.run();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000387 }
388 else
389 {
390 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
391 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100392
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100393 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000394 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100395 // Run matrix A reduction kernel only if _b_offset is not equal to 0
396 if(_b_offset != 0)
397 {
398 NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX);
399 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000400
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100401 // Run matrix B reduction kernel only if _a_offset is not equal to 0
402 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
403 {
404 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
405 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000406
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100407 if(_fuse_output_stage)
408 {
409 // Run offset contribution kernel
410 NEScheduler::get().schedule(&_offset_contribution_output_stage_kernel, Window::DimY);
411 }
412 else
413 {
414 // Run offset contribution kernel
415 NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY);
416 }
George Wort2d7e6832019-02-22 16:37:41 +0000417 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100418}
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100419
Georgios Pinitas72219332018-06-05 14:56:06 +0100420void NEGEMMLowpMatrixMultiplyCore::prepare()
421{
422 if(!_is_prepared)
423 {
424 // Run assembly reshape
Anthony Barbiereaefd002018-07-20 17:49:35 +0100425 if(_asm_glue.is_configured() && _reshape_b_only_on_first_run)
Georgios Pinitas72219332018-06-05 14:56:06 +0100426 {
427 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
428
Anthony Barbiereaefd002018-07-20 17:49:35 +0100429 _asm_glue.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100430 _original_b->mark_as_unused();
431 }
432 // Run non-assembly reshape
433 else if(_mtx_b_reshape_kernel && _reshape_b_only_on_first_run)
434 {
435 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
436
437 // Run reshape kernel and mark original weights tensor as unused
438 _tmp_b.allocator()->allocate();
439 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
440 _original_b->mark_as_unused();
441 }
442
443 // Run matrix B reduction kernel only if _a_offset is not equal to 0
444 if(_a_offset != 0 && _reshape_b_only_on_first_run)
445 {
446 _vector_sum_col.allocator()->allocate();
447 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
448 }
449
450 _is_prepared = true;
451 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000452}