blob: 617d66cf24456c910540454629f28f284541fad8 [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 Pinitas48b3ef82019-10-14 19:03:09 +010045 _offset_contribution_kernel(), _offset_contribution_output_stage_kernel(), _activation_func(), _vector_sum_col(), _vector_sum_row(), _tmp_a(), _tmp_b(), _mm_result_s32(), _original_b(nullptr),
46 _a_offset(0), _b_offset(0), _run_vector_matrix_multiplication(false), _assembly_path(false), _fused_assembly_path(false), _reshape_b_only_on_first_run(false), _is_prepared(false),
47 _fuse_output_stage(false), _run_activation(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;
59
Georgios Pinitas72219332018-06-05 14:56:06 +010060 // Clear state
Anthony Barbier71d9b572018-07-06 17:05:59 +010061 _mtx_a_reshape_kernel = nullptr;
62 _mtx_b_reshape_kernel = nullptr;
Georgios Pinitas72219332018-06-05 14:56:06 +010063
64 // Set internal variables
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010065 _a_offset = a->info()->quantization_info().uniform().offset;
66 _b_offset = b->info()->quantization_info().uniform().offset;
Gian Marcoc7f9b892017-11-30 14:31:13 +000067 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Giorgio Arenabb54e4e2018-04-05 17:20:34 +010068 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Georgios Pinitas72219332018-06-05 14:56:06 +010069 _is_prepared = false;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010070 _fused_assembly_path = false;
Georgios Pinitas72219332018-06-05 14:56:06 +010071 _original_b = b;
Gian Marcoe75a02b2017-11-08 12:24:09 +000072
George Wort2d7e6832019-02-22 16:37:41 +000073 // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage
74 if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE)
75 {
76 _fuse_output_stage = true;
George Wort2d7e6832019-02-22 16:37:41 +000077 _memory_group.manage(&_mm_result_s32);
George Wort2d7e6832019-02-22 16:37:41 +000078 TensorInfo info_mm_result_s32(output->info()->tensor_shape(), 1, DataType::S32);
George Wort2d7e6832019-02-22 16:37:41 +000079 _mm_result_s32.allocator()->init(info_mm_result_s32);
80 }
81
Pablo Telloeb82fd22018-02-23 13:43:50 +000082#ifdef __aarch64__
83 switch(a->info()->data_type())
Gian Marco Iodiceab182122017-10-09 15:05:40 +010084 {
Pablo Tello66c656a2018-03-15 10:34:58 +000085 case DataType::QASYMM8:
Pablo Telloeb82fd22018-02-23 13:43:50 +000086 case DataType::U8:
Anthony Barbiereaefd002018-07-20 17:49:35 +010087 case DataType::S8:
Pablo Telloeb82fd22018-02-23 13:43:50 +000088 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010089 if(a->info()->data_type() == DataType::QASYMM8 && gemm_info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
90 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010091 _asm_glue.configure(a, b, c, output, gemm_info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010092 _fused_assembly_path = _asm_glue.is_configured();
93 }
94 else
95 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010096 _asm_glue.configure(a, b, nullptr, _fuse_output_stage ? &_mm_result_s32 : output, gemm_info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010097 }
98 _assembly_path = _asm_glue.is_configured();
Pablo Telloeb82fd22018-02-23 13:43:50 +000099 break;
100 }
101 default:
102 {
103 ARM_COMPUTE_ERROR("Datatype not supported");
104 break;
105 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100106 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000107#endif /* __aarch64__ */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100108 if(!(_assembly_path || _run_vector_matrix_multiplication))
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100109 {
George Wort2d7e6832019-02-22 16:37:41 +0000110 matrix_a = &_tmp_a;
111 matrix_b = &_tmp_b;
112
113 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
Georgios Pinitas02acf012019-03-19 10:49:03 +0000114 TensorInfo a_info(compute_interleaved_shape(*a->info()), 1, a->info()->data_type(), a->info()->quantization_info());
George Wort2d7e6832019-02-22 16:37:41 +0000115 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
Georgios Pinitas02acf012019-03-19 10:49:03 +0000116 TensorInfo b_info(compute_transpose1xW_shape(*b->info()), 1, b->info()->data_type(), b->info()->quantization_info());
George Wort2d7e6832019-02-22 16:37:41 +0000117 _tmp_a.allocator()->init(a_info);
118 _tmp_b.allocator()->init(b_info);
119 _memory_group.manage(&_tmp_a);
120 if(!_reshape_b_only_on_first_run)
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100121 {
George Wort2d7e6832019-02-22 16:37:41 +0000122 _memory_group.manage(&_tmp_b);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100123 }
George Wort2d7e6832019-02-22 16:37:41 +0000124
125 // Configure interleave kernel
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100126 {
George Wort2d7e6832019-02-22 16:37:41 +0000127 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
128 k->configure(a, &_tmp_a);
129 _mtx_a_reshape_kernel = std::move(k);
130 }
Gian Marcoc7f9b892017-11-30 14:31:13 +0000131
George Wort2d7e6832019-02-22 16:37:41 +0000132 // Configure transpose kernel
133 {
134 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
135 k->configure(b, &_tmp_b);
136 _mtx_b_reshape_kernel = std::move(k);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100137 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000138 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100139
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100140 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000141 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100142 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
143 if(_a_offset != 0)
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100144 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100145 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
146
147 _vector_sum_col.allocator()->init(info_vector_sum_col);
148 if(!_reshape_b_only_on_first_run)
149 {
150 _memory_group.manage(&_vector_sum_col);
151 }
152
153 // Configure Matrix B reduction kernel
154 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, a->info()->dimension(0), false);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100155 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000156
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100157 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
158 if(_b_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000159 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100160 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a->info()), 1, DataType::S32);
161
162 _vector_sum_row.allocator()->init(info_vector_sum_row);
163 _memory_group.manage(&_vector_sum_row);
164
165 // Configure matrix A reduction kernel
166 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row, a->info()->dimension(0), false);
George Wort2d7e6832019-02-22 16:37:41 +0000167 }
168
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100169 if(_fuse_output_stage)
George Wort2d7e6832019-02-22 16:37:41 +0000170 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100171 // Configure matrix multiply kernel
172 if(!_assembly_path)
173 {
174 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
175 k->configure(matrix_a, matrix_b, &_mm_result_s32);
176 _mm_kernel = std::move(k);
177 }
178
179 _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),
180 _a_offset, _b_offset, gemm_info.gemmlowp_output_stage());
George Wort2d7e6832019-02-22 16:37:41 +0000181 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100182 else
183 {
184 // Configure matrix multiply kernel
185 if(!_assembly_path)
186 {
187 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
188 k->configure(matrix_a, matrix_b, output);
189 _mm_kernel = std::move(k);
190 }
191 // Configure offset contribution kernel
192 _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);
193 }
George Wort2d7e6832019-02-22 16:37:41 +0000194 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000195
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100196 // Configure activation
197 const ActivationLayerInfo &activation = gemm_info.activation_info();
198 _run_activation = activation.enabled() && (!_assembly_path || (_assembly_path && !NEGEMMAssemblyDispatch::is_activation_supported(activation)));
199 if(_run_activation)
200 {
201 _activation_func.configure(output, nullptr, activation);
202 }
203
Gian Marcoe75a02b2017-11-08 12:24:09 +0000204 // Allocate tensors
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100205 if(!_assembly_path && !_run_vector_matrix_multiplication)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000206 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000207 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100208 if(!_reshape_b_only_on_first_run)
209 {
210 _tmp_b.allocator()->allocate();
211 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000212 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000213
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100214 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000215 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100216 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
217 {
218 _vector_sum_col.allocator()->allocate();
219 }
220
221 if(_b_offset != 0)
222 {
223 _vector_sum_row.allocator()->allocate();
224 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000225 }
226
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100227 if(_fuse_output_stage)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000228 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100229 _mm_result_s32.allocator()->allocate();
Gian Marcoe75a02b2017-11-08 12:24:09 +0000230 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100231}
232
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100233Status 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 +0000234{
235 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
George Wort2d7e6832019-02-22 16:37:41 +0000236 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32, DataType::QASYMM8);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000237 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
George Wort2d7e6832019-02-22 16:37:41 +0000238 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 +0000239 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
240 "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 +0700241 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
242 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 +0000243
George Wort2d7e6832019-02-22 16:37:41 +0000244 const ITensorInfo *matrix_a_info = a;
245 const ITensorInfo *matrix_b_info = b;
246
247 TensorInfo tmp_a_info{};
248 TensorInfo tmp_b_info{};
249 TensorInfo mm_result_s32_info{};
250
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100251 int32_t a_offset = a->quantization_info().uniform().offset;
252 int32_t b_offset = b->quantization_info().uniform().offset;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000253
Georgios Pinitasa231ea62019-07-30 12:21:01 +0100254 bool fuse_output_stage = gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE;
George Wort2d7e6832019-02-22 16:37:41 +0000255 if(fuse_output_stage)
256 {
257 auto_init_if_empty(mm_result_s32_info, a->clone()->set_tensor_shape(output->tensor_shape()).set_data_type(DataType::S32));
258 }
259
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000260 // Check if we need to run the optimized assembly kernel
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100261 bool run_optimised = false;
262 bool run_optimised_requantized = false;
263 if(is_data_type_quantized_asymmetric(a->data_type()))
264 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100265 run_optimised = bool(NEGEMMAssemblyDispatch::validate(a, b, c, output, gemm_info));
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100266 run_optimised_requantized = run_optimised;
267 }
268 else
269 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100270 run_optimised = bool(NEGEMMAssemblyDispatch::validate(a, b, nullptr, fuse_output_stage ? &mm_result_s32_info : output, gemm_info));
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100271 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000272
273 if(run_optimised)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000274 {
George Wort2d7e6832019-02-22 16:37:41 +0000275 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
276 if(gemm_info.depth_output_gemm3d() != 0)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000277 {
George Wort2d7e6832019-02-22 16:37:41 +0000278 if(gemm_info.reinterpret_input_as_3d())
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000279 {
George Wort2d7e6832019-02-22 16:37:41 +0000280 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
281 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000282 }
283 else
284 {
George Wort2d7e6832019-02-22 16:37:41 +0000285 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000286 }
287 }
George Wort2d7e6832019-02-22 16:37:41 +0000288 else
289 {
290 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
291 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000292 }
293 else
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000294 {
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000295 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.reinterpret_input_as_3d(), "NEGEMM cannot reinterpret the input tensor as 3D");
296 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.depth_output_gemm3d() != 0, "NEGEMM cannot reinterpret the output tensor as 3D");
297
298 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
299 if(!run_vector_matrix_multiplication)
300 {
George Wort2d7e6832019-02-22 16:37:41 +0000301 matrix_a_info = &tmp_a_info;
302 matrix_b_info = &tmp_b_info;
303
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000304 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
305 TensorShape shape_tmp_a = a->tensor_shape();
306 shape_tmp_a.set(0, a->dimension(0) * 4);
307 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
308
309 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
310 TensorShape shape_tmp_b = b->tensor_shape();
311 shape_tmp_b.set(0, b->dimension(1) * 16);
312 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
313
George Wort2d7e6832019-02-22 16:37:41 +0000314 // Validate interleave kernel
315 auto_init_if_empty(tmp_a_info, a->clone()->set_tensor_shape(shape_tmp_a));
316 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(shape_tmp_b));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000317
George Wort2d7e6832019-02-22 16:37:41 +0000318 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a, &tmp_a_info));
319 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &tmp_b_info));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000320 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000321 }
322
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100323 if(!run_optimised_requantized)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000324 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100325 TensorInfo info_vector_sum_col{};
326 TensorInfo info_vector_sum_row{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000327
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100328 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
329 if(a_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000330 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100331 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
332
333 // Configure Matrix B reduction kernel
334 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, a->dimension(0), false));
George Wort2d7e6832019-02-22 16:37:41 +0000335 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000336
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100337 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
338 if(b_offset != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000339 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100340 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
341
342 // Configure matrix A reduction kernel
343 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row, a->dimension(0), false));
George Wort2d7e6832019-02-22 16:37:41 +0000344 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100345
346 if(fuse_output_stage)
347 {
348 if(!run_optimised)
349 {
350 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info));
351 }
352
353 // Validate offset contribution kernel
354 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info,
355 a_offset == 0 ? nullptr : &info_vector_sum_col,
356 b_offset == 0 ? nullptr : &info_vector_sum_row,
357 c, output, a_offset, b_offset,
358 gemm_info.gemmlowp_output_stage()));
359 }
360 else
361 {
362 if(!run_optimised)
363 {
364 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output));
365 }
366 // Validate offset contribution kernel
367 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionKernel::validate(output,
368 a_offset == 0 ? nullptr : &info_vector_sum_col,
369 b_offset == 0 ? nullptr : &info_vector_sum_row,
370 a_offset, b_offset));
371 }
George Wort2d7e6832019-02-22 16:37:41 +0000372 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100373
374 // Validate activation
375 const ActivationLayerInfo &activation = gemm_info.activation_info();
376 if(activation.enabled())
377 {
378 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, activation));
379 }
380
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000381 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000382}
383
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100384void NEGEMMLowpMatrixMultiplyCore::run()
385{
Georgios Pinitas72219332018-06-05 14:56:06 +0100386 prepare();
387
Georgios Pinitasda953f22019-04-02 17:27:03 +0100388 MemoryGroupResourceScope scope_mg(_memory_group);
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100389
Georgios Pinitas72219332018-06-05 14:56:06 +0100390 // Reshape inputs
391 if(_mtx_a_reshape_kernel)
Pablo Tello6ff12a02017-11-02 16:09:35 +0000392 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100393 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
394 }
395 if(_mtx_b_reshape_kernel && !_reshape_b_only_on_first_run)
396 {
397 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
Pablo Tello6ff12a02017-11-02 16:09:35 +0000398 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100399
Georgios Pinitas72219332018-06-05 14:56:06 +0100400 // Run GEMM
Anthony Barbiereaefd002018-07-20 17:49:35 +0100401 if(_asm_glue.is_configured())
Pablo Telloeb82fd22018-02-23 13:43:50 +0000402 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100403 _asm_glue.run();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000404 }
405 else
406 {
407 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
408 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100409
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100410 if(!_fused_assembly_path)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000411 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100412 // Run matrix A reduction kernel only if _b_offset is not equal to 0
413 if(_b_offset != 0)
414 {
415 NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX);
416 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000417
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100418 // Run matrix B reduction kernel only if _a_offset is not equal to 0
419 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
420 {
421 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
422 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000423
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100424 if(_fuse_output_stage)
425 {
426 // Run offset contribution kernel
427 NEScheduler::get().schedule(&_offset_contribution_output_stage_kernel, Window::DimY);
428 }
429 else
430 {
431 // Run offset contribution kernel
432 NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY);
433 }
George Wort2d7e6832019-02-22 16:37:41 +0000434 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100435
436 // Run fused activation
437 if(_run_activation)
438 {
439 _activation_func.run();
440 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100441}
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100442
Georgios Pinitas72219332018-06-05 14:56:06 +0100443void NEGEMMLowpMatrixMultiplyCore::prepare()
444{
445 if(!_is_prepared)
446 {
447 // Run assembly reshape
Anthony Barbiereaefd002018-07-20 17:49:35 +0100448 if(_asm_glue.is_configured() && _reshape_b_only_on_first_run)
Georgios Pinitas72219332018-06-05 14:56:06 +0100449 {
450 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
451
Anthony Barbiereaefd002018-07-20 17:49:35 +0100452 _asm_glue.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100453 _original_b->mark_as_unused();
454 }
455 // Run non-assembly reshape
456 else if(_mtx_b_reshape_kernel && _reshape_b_only_on_first_run)
457 {
458 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
459
460 // Run reshape kernel and mark original weights tensor as unused
461 _tmp_b.allocator()->allocate();
462 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
463 _original_b->mark_as_unused();
464 }
465
466 // Run matrix B reduction kernel only if _a_offset is not equal to 0
467 if(_a_offset != 0 && _reshape_b_only_on_first_run)
468 {
469 _vector_sum_col.allocator()->allocate();
470 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
471 }
472
473 _is_prepared = true;
474 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000475}