blob: 8faa3c217a72871df474c8ac5a196d4f8d27c16a [file] [log] [blame]
Manuel Bottinicfac51c2021-06-18 15:47:28 +01001/*
2 * Copyright (c) 2021 Arm Limited.
3 *
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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/operators/CpuGemmLowpMatrixMultiplyCore.h"
Manuel Bottinicfac51c2021-06-18 15:47:28 +010025
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/KernelDescriptors.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
33#include "arm_compute/runtime/NEON/NEScheduler.h"
34#include "arm_compute/runtime/TensorAllocator.h"
35#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/MemoryHelpers.h"
37
ramelg013ae3d882021-09-12 23:07:47 +010038#include "src/common/utils/Log.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010039#include "src/cpu/kernels/CpuConvertQuantizedSignednessKernel.h"
40#include "src/cpu/kernels/CpuGemmInterleave4x4Kernel.h"
41#include "src/cpu/kernels/CpuGemmLowpMatrixMultiplyKernel.h"
42#include "src/cpu/kernels/CpuGemmLowpMatrixReductionKernel.h"
43#include "src/cpu/kernels/CpuGemmLowpOffsetContributionKernel.h"
44#include "src/cpu/kernels/CpuGemmLowpOffsetContributionOutputStageKernel.h"
45#include "src/cpu/kernels/CpuGemmTranspose1xWKernel.h"
46#include "src/cpu/operators/CpuActivation.h"
47#include "src/cpu/operators/internal/CpuGemmAssemblyDispatch.h"
48#include "src/cpu/utils/CpuAuxTensorHandler.h"
Manuel Bottinicfac51c2021-06-18 15:47:28 +010049
50using namespace arm_compute::misc::shape_calculator;
51using namespace arm_compute::experimental;
52
53namespace arm_compute
54{
55namespace cpu
56{
57namespace
58{
59cpu::AsmGemmInfo init_assembly_metadata(const GEMMInfo &info)
60{
61 cpu::AsmGemmInfo asm_info;
62 asm_info.method = cpu::AsmConvMethod::Im2Col;
63 asm_info.reinterpret_input_as_3d = info.reinterpret_input_as_3d();
64 asm_info.depth_output_gemm3d = info.depth_output_gemm3d();
65 asm_info.activation_info = info.activation_info();
66 asm_info.output_stage = info.gemmlowp_output_stage();
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010067 asm_info.fast_mode = info.fast_math();
Manuel Bottinicfac51c2021-06-18 15:47:28 +010068
69 return asm_info;
70}
71} // namespace
72
73CpuGemmLowpMatrixMultiplyCore::CpuGemmLowpMatrixMultiplyCore()
74 : _asm_glue(std::make_unique<CpuGemmAssemblyDispatch>()),
75 _mm_kernel(),
76 _mtx_a_reshape_kernel(),
77 _mtx_b_reshape_kernel(),
78 _mtx_a_reduction_kernel(),
79 _mtx_b_reduction_kernel(),
80 _offset_contribution_kernel(),
81 _offset_contribution_output_stage_kernel(),
82 _activation_func(),
83 _convert_to_signed_asymm(),
84 _convert_from_signed_asymm(),
85 _vector_sum_col(),
86 _vector_sum_row(),
87 _tmp_a(),
88 _tmp_b(),
89 _mm_result_s32(),
90 _signed_a(),
91 _signed_output(),
92 _a_offset(0),
93 _b_offset(0),
94 _run_vector_matrix_multiplication(false),
95 _assembly_path(false),
96 _fused_assembly_path(false),
97 _reshape_b_only_on_first_run(false),
98 _is_prepared(false),
99 _fuse_output_stage(false),
100 _run_activation(false),
101 _flip_signedness(false),
102 _gemm_info(),
103 _aux_mem(Count)
104{
105}
106CpuGemmLowpMatrixMultiplyCore::~CpuGemmLowpMatrixMultiplyCore() = default;
107
108void CpuGemmLowpMatrixMultiplyCore::configure(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *dst, const GEMMInfo &gemm_info)
109{
110 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, dst);
111 ARM_COMPUTE_ERROR_THROW_ON(CpuGemmLowpMatrixMultiplyCore::validate(a, b, c, dst, gemm_info));
ramelg013ae3d882021-09-12 23:07:47 +0100112 ARM_COMPUTE_LOG_PARAMS(a, b, c, dst, gemm_info);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100113
114 const ITensorInfo *matrix_a = a;
115 const ITensorInfo *matrix_b = b;
116 GEMMInfo info = gemm_info;
117
118 // Set internal variables
119 _a_offset = a->quantization_info().uniform().offset;
120 _b_offset = b->quantization_info().uniform().offset;
121 _run_vector_matrix_multiplication = a->dimension(1) < 2;
122 _reshape_b_only_on_first_run = info.reshape_b_only_on_first_run();
123 _is_prepared = false;
124 _fused_assembly_path = false;
125 _flip_signedness = is_data_type_quantized_per_channel(b->data_type()) && (a->data_type() == DataType::QASYMM8) && _reshape_b_only_on_first_run;
126 _gemm_info = gemm_info;
127
128 _asm_glue = std::make_unique<cpu::CpuGemmAssemblyDispatch>();
129
130 const ITensorInfo *a_to_use = a;
131
132 // Convert to QASYMM8 -> QASYMM8_SIGNED and back
133 if(_flip_signedness)
134 {
135 const int32_t offset_correction = 128;
136 const DataType dt = DataType::QASYMM8_SIGNED;
137 const UniformQuantizationInfo iqinfo = a_to_use->quantization_info().uniform();
138
139 _signed_a = a_to_use->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction));
140 _convert_to_signed_asymm = std::make_unique<kernels::CpuConvertQuantizedSignednessKernel>();
141 _convert_to_signed_asymm->configure(a_to_use, &_signed_a);
142 a_to_use = &_signed_a;
143 _a_offset = _signed_a.quantization_info().uniform().offset;
144
145 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
146 _signed_output = dst->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction));
147
148 // Output stage correction
149 GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage();
150 output_stage_corr.gemmlowp_offset = _signed_output.quantization_info().uniform().offset;
151 output_stage_corr.gemmlowp_min_bound -= offset_correction;
152 output_stage_corr.gemmlowp_max_bound -= offset_correction;
153 info.set_gemmlowp_output_stage(output_stage_corr);
154
155 // Update matrix a
156 matrix_a = &_signed_a;
157 }
158
159 // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage
160 if(info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE)
161 {
162 _fuse_output_stage = true;
163 _mm_result_s32 = TensorInfo(dst->tensor_shape(), 1, DataType::S32);
164 }
165
166 // Initialize assembly kernel meta-data
167 const cpu::AsmGemmInfo asm_info = init_assembly_metadata(gemm_info);
168#ifdef __aarch64__
169 switch(a->data_type())
170 {
171 case DataType::QASYMM8:
172 case DataType::QASYMM8_SIGNED:
173 case DataType::U8:
174 case DataType::S8:
175 {
176 if(is_data_type_quantized_asymmetric(a_to_use->data_type()) && info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
177 {
178 auto c_info_to_use = c == nullptr ? nullptr : c;
179 _asm_glue->configure(a_to_use, b, c_info_to_use, dst, asm_info);
180 _fused_assembly_path = _asm_glue->is_configured();
181 }
182 else
183 {
184 auto output_to_use = (_fuse_output_stage ? &_mm_result_s32 : dst);
185 _asm_glue->configure(a_to_use, b, nullptr, output_to_use, asm_info);
186 }
187 _assembly_path = _asm_glue->is_configured();
188 break;
189 }
190 default:
191 {
192 ARM_COMPUTE_ERROR("Datatype not supported");
193 break;
194 }
195 }
196#endif /* __aarch64__ */
197 if(!(_assembly_path || _run_vector_matrix_multiplication))
198 {
199 matrix_a = &_tmp_a;
200 matrix_b = &_tmp_b;
201
202 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
203 _tmp_a = TensorInfo(compute_interleaved_shape(*a_to_use), 1, a_to_use->data_type(), a_to_use->quantization_info());
204 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
205 _tmp_b = TensorInfo(compute_transpose1xW_shape(*b), 1, b->data_type(), b->quantization_info());
206
207 // Configure interleave kernel
208 _mtx_a_reshape_kernel = std::make_unique<kernels::CpuGemmInterleave4x4Kernel>();
209 _mtx_a_reshape_kernel->configure(a_to_use, &_tmp_a);
210
211 // Configure transpose kernel
212 _mtx_b_reshape_kernel = std::make_unique<kernels::CpuGemmTranspose1xWKernel>();
213 _mtx_b_reshape_kernel->configure(b, &_tmp_b);
214 }
215
216 if(!_fused_assembly_path)
217 {
218 // Build reduction info
219 const GEMMLowpReductionKernelInfo reduction_info(a_to_use->dimension(0), false, 0, false);
220
221 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
222 if(_a_offset != 0)
223 {
224 _vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
225
226 // Configure Matrix B reduction kernel
227 _mtx_b_reduction_kernel = std::make_unique<kernels::CpuGemmLowpMatrixBReductionKernel>();
228 _mtx_b_reduction_kernel->configure(b, &_vector_sum_col, reduction_info);
229 }
230
231 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
232 if(_b_offset != 0)
233 {
234 _vector_sum_row = TensorInfo(compute_reductionB_shape(*a_to_use), 1, DataType::S32);
235
236 // Configure matrix A reduction kernel
237 _mtx_a_reduction_kernel = std::make_unique<kernels::CpuGemmLowpMatrixAReductionKernel>();
238 _mtx_a_reduction_kernel->configure(a_to_use, &_vector_sum_row, reduction_info);
239 }
240
241 if(_fuse_output_stage)
242 {
243 // Configure matrix multiply kernel
244 if(!_assembly_path)
245 {
246 _mm_kernel = std::make_unique<kernels::CpuGemmLowpMatrixMultiplyKernel>();
247 _mm_kernel->configure(matrix_a, matrix_b, &_mm_result_s32);
248 }
249
250 _offset_contribution_output_stage_kernel = std::make_unique<kernels::CpuGemmLowpOffsetContributionOutputStageKernel>();
251 _offset_contribution_output_stage_kernel->configure(&_mm_result_s32,
252 _a_offset == 0 ? nullptr : &_vector_sum_col,
253 _b_offset == 0 ? nullptr : &_vector_sum_row, c,
254 _flip_signedness ? &_signed_output : dst,
255 a->dimension(0),
256 _a_offset, _b_offset, info.gemmlowp_output_stage());
257
258 if(_flip_signedness)
259 {
260 _convert_from_signed_asymm = std::make_unique<kernels::CpuConvertQuantizedSignednessKernel>();
261 _convert_from_signed_asymm->configure(&_signed_output, dst);
262 }
263 }
264 else
265 {
266 // Configure matrix multiply kernel
267 if(!_assembly_path)
268 {
269 _mm_kernel = std::make_unique<kernels::CpuGemmLowpMatrixMultiplyKernel>();
270 _mm_kernel->configure(matrix_a, matrix_b, dst);
271 }
272 // Configure offset contribution kernel
273 _offset_contribution_kernel = std::make_unique<kernels::CpuGemmLowpOffsetContributionKernel>();
274 _offset_contribution_kernel->configure(dst, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, a_to_use->dimension(0),
275 _a_offset, _b_offset);
276 }
277 }
278 // Configure activation
279 const ActivationLayerInfo &activation = gemm_info.activation_info();
280 _run_activation = activation.enabled() && (!_assembly_path || !cpu::CpuGemmAssemblyDispatch::is_activation_supported(activation));
281 if(_run_activation)
282 {
283 _activation_func = std::make_unique<CpuActivation>();
284 _activation_func->configure(dst, nullptr, activation);
285 }
286
287 if(_assembly_path)
288 {
289 auto asm_mem_req = _asm_glue->workspace();
290 _aux_mem[AsmGemmWorkspace] = asm_mem_req[AsmGemmWorkspace];
291 _aux_mem[Pretranspose] = asm_mem_req[Pretranspose];
292 }
293
294 // Request memory for LHS and RHS reshape matrix
295 _aux_mem[VectorSumCol] = MemoryInfo(offset_int_vec(VectorSumCol), !_fused_assembly_path && _a_offset != 0
296 && _reshape_b_only_on_first_run ?
297 MemoryLifetime::Persistent :
298 MemoryLifetime::Temporary,
299 _vector_sum_col.total_size());
300 _aux_mem[VectorSumRow] = MemoryInfo(offset_int_vec(VectorSumRow), MemoryLifetime::Temporary, _vector_sum_row.total_size());
301 _aux_mem[TmpA] = MemoryInfo(offset_int_vec(TmpA), MemoryLifetime::Temporary, _tmp_a.total_size());
302 _aux_mem[TmpB] = MemoryInfo(offset_int_vec(TmpB), _reshape_b_only_on_first_run ? MemoryLifetime::Persistent : MemoryLifetime::Temporary, _tmp_b.total_size());
303 _aux_mem[MMResultS32] = MemoryInfo(offset_int_vec(MMResultS32), MemoryLifetime::Temporary, _mm_result_s32.total_size());
304 _aux_mem[SignedA] = MemoryInfo(offset_int_vec(SignedA), MemoryLifetime::Temporary, _signed_a.total_size());
305 _aux_mem[SignedOutput] = MemoryInfo(offset_int_vec(SignedOutput), MemoryLifetime::Temporary, _signed_output.total_size());
306}
307
308Status CpuGemmLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info)
309{
310 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
311 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(b, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8, DataType::QSYMM8_PER_CHANNEL);
312 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
313 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");
314 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
315 "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
316 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
317 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
318
319 GEMMInfo info = gemm_info;
320 const ITensorInfo *matrix_a_info = a;
321 const ITensorInfo *matrix_b_info = b;
322
323 const ITensorInfo *a_to_use = a;
324
325 TensorInfo tmp_a_info{};
326 TensorInfo tmp_b_info{};
327 TensorInfo mm_result_s32_info{};
328
329 int32_t a_offset = a->quantization_info().uniform().offset;
330 int32_t b_offset = b->quantization_info().uniform().offset;
331
332 bool fuse_output_stage = info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE;
333 if(fuse_output_stage)
334 {
335 auto_init_if_empty(mm_result_s32_info, a->clone()->set_tensor_shape(output->tensor_shape()).set_data_type(DataType::S32));
336 }
337
338 // Convert QASYMM8->QASYMM8_SIGNED
339 TensorInfo signed_a{};
340 TensorInfo signed_output{};
341 bool flip_signedness = is_data_type_quantized_per_channel(b->data_type()) && (a->data_type() == DataType::QASYMM8) && info.reshape_b_only_on_first_run();
342 if(flip_signedness)
343 {
344 const int32_t offset_correction = 128;
345 const DataType dt = DataType::QASYMM8_SIGNED;
346 const UniformQuantizationInfo iqinfo = a_to_use->quantization_info().uniform();
347
348 signed_a = a_to_use->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction));
349 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuConvertQuantizedSignednessKernel::validate(a_to_use, &signed_a));
350 a_to_use = &signed_a;
351 a_offset = signed_a.quantization_info().uniform().offset;
352
353 const UniformQuantizationInfo oqinfo = output->quantization_info().uniform();
354 signed_output = output->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction));
355
356 // Output stage correction
357 GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage();
358 output_stage_corr.gemmlowp_offset = signed_output.quantization_info().uniform().offset;
359 output_stage_corr.gemmlowp_min_bound -= offset_correction;
360 output_stage_corr.gemmlowp_max_bound -= offset_correction;
361 info.set_gemmlowp_output_stage(output_stage_corr);
362
363 // Update matrix a
364 matrix_a_info = &signed_a;
365 }
366
367 // Initialize assembly kernel meta-data
368 const AsmGemmInfo asm_info = init_assembly_metadata(info);
369
370 // Check if we need to run the optimized assembly kernel
371 bool run_optimised = false;
372 bool run_optimised_requantized = false;
373 if(is_data_type_quantized_asymmetric(a_to_use->data_type()) && info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
374 {
375 run_optimised = bool(CpuGemmAssemblyDispatch::validate(a_to_use, b, c, output, asm_info));
376 run_optimised_requantized = run_optimised;
377 }
378 else
379 {
380 run_optimised = bool(CpuGemmAssemblyDispatch::validate(a_to_use, b, nullptr, fuse_output_stage ? &mm_result_s32_info : output, asm_info));
381 }
382
383 if(run_optimised)
384 {
385 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
386 if(info.depth_output_gemm3d() != 0)
387 {
388 if(info.reinterpret_input_as_3d())
389 {
390 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
391 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
392 }
393 else
394 {
395 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
396 }
397 }
398 else
399 {
400 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
401 }
402 }
403 else
404 {
405 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.reinterpret_input_as_3d(), "NEGEMM cannot reinterpret the input tensor as 3D");
406 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.depth_output_gemm3d() != 0, "NEGEMM cannot reinterpret the output tensor as 3D");
407
408 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
409 if(!run_vector_matrix_multiplication)
410 {
411 matrix_a_info = &tmp_a_info;
412 matrix_b_info = &tmp_b_info;
413
414 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
415 TensorShape shape_tmp_a = a->tensor_shape();
416 shape_tmp_a.set(0, a->dimension(0) * 4);
417 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
418
419 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
420 TensorShape shape_tmp_b = b->tensor_shape();
421 shape_tmp_b.set(0, b->dimension(1) * 16);
422 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
423
424 // Validate interleave kernel
425 auto_init_if_empty(tmp_a_info, a_to_use->clone()->set_tensor_shape(shape_tmp_a));
426 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(shape_tmp_b));
427
428 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmInterleave4x4Kernel::validate(a_to_use, &tmp_a_info));
429 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmTranspose1xWKernel::validate(b, &tmp_b_info));
430 }
431 }
432
433 if(!run_optimised_requantized)
434 {
435 TensorInfo info_vector_sum_col{};
436 TensorInfo info_vector_sum_row{};
437
438 const GEMMLowpReductionKernelInfo reduction_info(a_to_use->dimension(0), false, 0, false);
439
440 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
441 if(a_offset != 0)
442 {
443 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
444
445 // Configure Matrix B reduction kernel
446 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, reduction_info));
447 }
448
449 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
450 if(b_offset != 0)
451 {
452 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
453
454 // Configure matrix A reduction kernel
455 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmLowpMatrixAReductionKernel::validate(a_to_use, &info_vector_sum_row, reduction_info));
456 }
457
458 if(fuse_output_stage)
459 {
460 if(!run_optimised)
461 {
462 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.reinterpret_input_as_3d(), "CpuGemmLowpMatrixMultiplyKernel cannot reinterpret the input tensor as 3D");
463 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.depth_output_gemm3d() != 0, "CpuGemmLowpMatrixMultiplyKernel cannot reinterpret the output tensor as 3D");
464
465 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info));
466 }
467
468 // Validate offset contribution kernel
469 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info,
470 a_offset == 0 ? nullptr : &info_vector_sum_col,
471 b_offset == 0 ? nullptr : &info_vector_sum_row,
472 c,
473 flip_signedness ? &signed_output : output,
474 a_offset, b_offset,
475 info.gemmlowp_output_stage()));
476 }
477 else
478 {
479 if(!run_optimised)
480 {
481 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.reinterpret_input_as_3d(), "CpuGemmLowpMatrixMultiplyKernel cannot reinterpret the input tensor as 3D");
482 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.depth_output_gemm3d() != 0, "CpuGemmLowpMatrixMultiplyKernel cannot reinterpret the output tensor as 3D");
483
484 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output));
485 }
486 // Validate offset contribution kernel
487 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmLowpOffsetContributionKernel::validate(output,
488 a_offset == 0 ? nullptr : &info_vector_sum_col,
489 b_offset == 0 ? nullptr : &info_vector_sum_row,
490 a_offset, b_offset));
491 }
492 }
493
494 // Validate activation
495 const ActivationLayerInfo &activation = gemm_info.activation_info();
496 if(activation.enabled())
497 {
498 ARM_COMPUTE_RETURN_ON_ERROR(CpuActivation::validate(output, nullptr, activation));
499 }
500
501 return Status{};
502}
503
504void CpuGemmLowpMatrixMultiplyCore::run(ITensorPack &tensors)
505{
506 prepare(tensors);
Georgios Pinitas22f5ed52021-07-23 18:58:43 +0100507
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100508 auto a = tensors.get_const_tensor(TensorType::ACL_SRC_0);
509 auto b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
510 auto c = tensors.get_const_tensor(TensorType::ACL_SRC_2);
511 auto dst = tensors.get_tensor(TensorType::ACL_DST);
512 auto a_to_use = a;
513 auto matrix_a = a;
514 auto matrix_b = b;
515
516 CpuAuxTensorHandler vector_sum_col(offset_int_vec(VectorSumCol), _vector_sum_col, tensors, false);
517 CpuAuxTensorHandler vector_sum_row(offset_int_vec(VectorSumRow), _vector_sum_row, tensors, false);
518 CpuAuxTensorHandler tmp_a(offset_int_vec(TmpA), _tmp_a, tensors, false);
519 CpuAuxTensorHandler tmp_b(offset_int_vec(TmpB), _tmp_b, tensors, true);
520 CpuAuxTensorHandler mm_result_s32(offset_int_vec(MMResultS32), _mm_result_s32, tensors, false);
521 CpuAuxTensorHandler signed_a(offset_int_vec(SignedA), _signed_a, tensors, false);
522 CpuAuxTensorHandler signed_output(offset_int_vec(SignedOutput), _signed_output, tensors, false);
523
524 // Convert QASYMM8->QASYMM8_SIGNED
525 if(_flip_signedness)
526 {
527 ITensorPack pack =
528 {
529 { TensorType::ACL_SRC, a },
530 { TensorType::ACL_DST, signed_a.get() }
531 };
532 NEScheduler::get().schedule_op(_convert_to_signed_asymm.get(), Window::DimY, _convert_to_signed_asymm->window(), pack);
533 a_to_use = signed_a.get();
Georgios Pinitasd4a5bc52021-08-12 07:42:51 +0100534 matrix_a = signed_a.get();
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100535 }
536
537 // Run GEMM
538 if(_asm_glue->is_configured())
539 {
540 ITensorPack asm_glue_tensors = tensors;
541 auto output_to_use = (_fuse_output_stage ? mm_result_s32.get() : dst);
542 if(is_data_type_quantized_asymmetric(a_to_use->info()->data_type()) && _gemm_info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
543 {
544 asm_glue_tensors.add_const_tensor(TensorType::ACL_SRC_0, a_to_use);
545 asm_glue_tensors.add_const_tensor(TensorType::ACL_SRC_1, b);
546 asm_glue_tensors.add_const_tensor(TensorType::ACL_SRC_2, c);
547 asm_glue_tensors.add_tensor(TensorType::ACL_DST, dst);
548 }
549 else
550 {
551 asm_glue_tensors.add_const_tensor(TensorType::ACL_SRC_0, a_to_use);
552 asm_glue_tensors.add_const_tensor(TensorType::ACL_SRC_1, b);
553 asm_glue_tensors.add_tensor(TensorType::ACL_DST, output_to_use);
554 }
555 _asm_glue->run(asm_glue_tensors);
556 }
557 else
558 {
559 if(!_run_vector_matrix_multiplication)
560 {
561 matrix_a = tmp_a.get();
562 matrix_b = tmp_b.get();
563 // Run interleave kernel
564 ITensorPack pack_a =
565 {
566 { TensorType::ACL_SRC, a_to_use },
567 { TensorType::ACL_DST, tmp_a.get() }
568 };
569 NEScheduler::get().schedule_op(_mtx_a_reshape_kernel.get(), Window::DimY, _mtx_a_reshape_kernel->window(), pack_a);
570
571 if(!_reshape_b_only_on_first_run)
572 {
573 ITensorPack pack_b =
574 {
575 { TensorType::ACL_SRC, b },
576 { TensorType::ACL_DST, tmp_b.get() }
577 };
578 // Run transpose kernel
579 NEScheduler::get().schedule_op(_mtx_b_reshape_kernel.get(), Window::DimY, _mtx_b_reshape_kernel->window(), pack_b);
580 }
581 }
582 ITensorPack pack_mm =
583 {
584 { TensorType::ACL_SRC_0, matrix_a },
585 { TensorType::ACL_SRC_1, matrix_b }
586 };
587 if(_fuse_output_stage)
588 {
589 pack_mm.add_tensor(TensorType::ACL_DST, mm_result_s32.get());
590 }
591 else
592 {
593 pack_mm.add_tensor(TensorType::ACL_DST, dst);
594 }
595 NEScheduler::get().schedule_op(_mm_kernel.get(), Window::DimY, _mm_kernel->window(), pack_mm);
596 }
597
598 if(!_fused_assembly_path)
599 {
600 // Run matrix A reduction kernel only if _b_offset is not equal to 0
601 if(_b_offset != 0)
602 {
603 ITensorPack pack =
604 {
605 { TensorType::ACL_SRC, a_to_use },
606 { TensorType::ACL_DST, vector_sum_row.get() }
607 };
608 NEScheduler::get().schedule_op(_mtx_a_reduction_kernel.get(), Window::DimX, _mtx_a_reduction_kernel->window(), pack);
609 }
610
611 // Run matrix B reduction kernel only if _a_offset is not equal to 0
612 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
613 {
614 ITensorPack pack =
615 {
616 { TensorType::ACL_SRC, b },
617 { TensorType::ACL_DST, vector_sum_col.get() }
618 };
619 NEScheduler::get().schedule_op(_mtx_b_reduction_kernel.get(), Window::DimX, _mtx_b_reduction_kernel->window(), pack);
620 }
621
622 if(_fuse_output_stage)
623 {
624 ITensorPack pack;
625 pack.add_tensor(TensorType::ACL_SRC_0, mm_result_s32.get());
626 pack.add_tensor(TensorType::ACL_SRC_1, _a_offset == 0 ? nullptr : vector_sum_col.get());
627 pack.add_tensor(TensorType::ACL_SRC_2, _b_offset == 0 ? nullptr : vector_sum_row.get());
628 pack.add_tensor(TensorType::ACL_SRC_3, c);
629 pack.add_tensor(TensorType::ACL_DST, _flip_signedness ? signed_output.get() : dst);
630
631 // Run offset contribution kernel
632 NEScheduler::get().schedule_op(_offset_contribution_output_stage_kernel.get(), Window::DimY, _offset_contribution_output_stage_kernel->window(), pack);
633 }
634 else
635 {
636 ITensorPack pack;
637 pack.add_tensor(TensorType::ACL_SRC_0, _a_offset == 0 ? nullptr : vector_sum_col.get());
638 pack.add_tensor(TensorType::ACL_SRC_1, _b_offset == 0 ? nullptr : vector_sum_row.get());
639 pack.add_tensor(TensorType::ACL_DST, dst);
640
641 // Run offset contribution kernel
642 NEScheduler::get().schedule_op(_offset_contribution_kernel.get(), Window::DimY, _offset_contribution_kernel->window(), pack);
643 }
644 }
645
646 // Convert QASYMM8_SIGNED->QASYMM8
647 if(!_fused_assembly_path && _fuse_output_stage && _flip_signedness)
648 {
649 ITensorPack pack =
650 {
651 { TensorType::ACL_SRC, signed_output.get() },
652 { TensorType::ACL_DST, dst }
653 };
654 NEScheduler::get().schedule_op(_convert_from_signed_asymm.get(), Window::DimY, _convert_from_signed_asymm->window(), pack);
655 }
656
657 // Run fused activation unless already run in the fused assembly
658 if(_run_activation)
659 {
660 ITensorPack pack =
661 {
662 { TensorType::ACL_SRC, dst },
663 { TensorType::ACL_DST, dst }
664 };
665 _activation_func->run(pack);
666 }
667}
668
669void CpuGemmLowpMatrixMultiplyCore::prepare(ITensorPack &tensors)
670{
671 if(!_is_prepared)
672 {
673 auto original_b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
674 // Run assembly reshape
675 if(_asm_glue->is_configured())
676 {
677 _asm_glue->prepare(tensors);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100678 }
679 // Run non-assembly reshape
680 else if(_reshape_b_only_on_first_run && !_run_vector_matrix_multiplication && !_asm_glue->is_configured())
681 {
682 // Run reshape kernel and mark original weights tensor as unused
683 ITensor *tmp_b_p = utils::cast::polymorphic_downcast<ITensor *>(tensors.get_tensor(offset_int_vec(TmpB)));
684 CpuAuxTensorHandler tmp_b(_tmp_b, *tmp_b_p);
685 ITensorPack pack =
686 {
687 { TensorType::ACL_SRC, original_b },
688 { TensorType::ACL_DST, tmp_b.get() }
689 };
690 NEScheduler::get().schedule_op(_mtx_b_reshape_kernel.get(), Window::DimY, _mtx_b_reshape_kernel->window(), pack);
691 }
692
693 // Run matrix B reduction kernel only if _a_offset is not equal to 0
694 if(!_fused_assembly_path && _a_offset != 0 && _reshape_b_only_on_first_run)
695 {
696 ITensor *vector_sum_col_p = utils::cast::polymorphic_downcast<ITensor *>(tensors.get_tensor(offset_int_vec(VectorSumCol)));
697 CpuAuxTensorHandler vector_sum_col(_vector_sum_col, *vector_sum_col_p);
698 ITensorPack pack =
699 {
700 { TensorType::ACL_SRC, original_b },
701 { TensorType::ACL_DST, vector_sum_col.get() }
702 };
703 NEScheduler::get().schedule_op(_mtx_b_reduction_kernel.get(), Window::DimX, _mtx_b_reduction_kernel->window(), pack);
704 }
705 _is_prepared = true;
706 }
707}
708experimental::MemoryRequirements CpuGemmLowpMatrixMultiplyCore::workspace() const
709{
710 return _aux_mem;
711}
712} // namespace cpu
713} // namespace arm_compute