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