blob: 2ee879b67b7d1c26578005f6a46acf76b5bebcbc [file] [log] [blame]
Manuel Bottinicfac51c2021-06-18 15:47:28 +01001/*
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +01002 * Copyright (c) 2021-2023 Arm Limited.
Manuel Bottinicfac51c2021-06-18 15:47:28 +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 */
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"
Manuel Bottinicfac51c2021-06-18 15:47:28 +010031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032#include "arm_compute/core/Validate.h"
Manuel Bottinicfac51c2021-06-18 15:47:28 +010033#include "arm_compute/runtime/NEON/NEScheduler.h"
34#include "arm_compute/runtime/TensorAllocator.h"
Manuel Bottinicfac51c2021-06-18 15:47:28 +010035
ramelg013ae3d882021-09-12 23:07:47 +010036#include "src/common/utils/Log.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010037#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/MemoryHelpers.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;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 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();
67 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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100108void CpuGemmLowpMatrixMultiplyCore::configure(
109 const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *dst, const GEMMInfo &gemm_info)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100110{
111 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, dst);
112 ARM_COMPUTE_ERROR_THROW_ON(CpuGemmLowpMatrixMultiplyCore::validate(a, b, c, dst, gemm_info));
ramelg013ae3d882021-09-12 23:07:47 +0100113 ARM_COMPUTE_LOG_PARAMS(a, b, c, dst, gemm_info);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100114
115 const ITensorInfo *matrix_a = a;
116 const ITensorInfo *matrix_b = b;
117 GEMMInfo info = gemm_info;
118
119 // Set internal variables
120 _a_offset = a->quantization_info().uniform().offset;
121 _b_offset = b->quantization_info().uniform().offset;
122 _run_vector_matrix_multiplication = a->dimension(1) < 2;
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100123 _reshape_b_only_on_first_run = b->are_values_constant();
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100124 _is_prepared = false;
125 _fused_assembly_path = false;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 _flip_signedness = is_data_type_quantized_per_channel(b->data_type()) && (a->data_type() == DataType::QASYMM8) &&
127 _reshape_b_only_on_first_run;
128 _gemm_info = gemm_info;
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100129
130 _asm_glue = std::make_unique<cpu::CpuGemmAssemblyDispatch>();
131
132 const ITensorInfo *a_to_use = a;
133
134 // Convert to QASYMM8 -> QASYMM8_SIGNED and back
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135 if (_flip_signedness)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100136 {
137 const int32_t offset_correction = 128;
138 const DataType dt = DataType::QASYMM8_SIGNED;
139 const UniformQuantizationInfo iqinfo = a_to_use->quantization_info().uniform();
140
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141 _signed_a = a_to_use->clone()->set_data_type(dt).set_quantization_info(
142 QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100143 _convert_to_signed_asymm = std::make_unique<kernels::CpuConvertQuantizedSignednessKernel>();
144 _convert_to_signed_asymm->configure(a_to_use, &_signed_a);
145 a_to_use = &_signed_a;
146 _a_offset = _signed_a.quantization_info().uniform().offset;
147
148 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100149 _signed_output = dst->clone()->set_data_type(dt).set_quantization_info(
150 QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100151
152 // Output stage correction
153 GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage();
154 output_stage_corr.gemmlowp_offset = _signed_output.quantization_info().uniform().offset;
155 output_stage_corr.gemmlowp_min_bound -= offset_correction;
156 output_stage_corr.gemmlowp_max_bound -= offset_correction;
157 info.set_gemmlowp_output_stage(output_stage_corr);
158
159 // Update matrix a
160 matrix_a = &_signed_a;
161 }
162
163 // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100164 if (info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100165 {
166 _fuse_output_stage = true;
167 _mm_result_s32 = TensorInfo(dst->tensor_shape(), 1, DataType::S32);
168 }
169
170 // Initialize assembly kernel meta-data
171 const cpu::AsmGemmInfo asm_info = init_assembly_metadata(gemm_info);
172#ifdef __aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173 if (!(!b->are_values_constant() &&
174 b->tensor_shape().z() > 1)) // Disable batch matmul as optimized GeMM handles batching differently.
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100175 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100176 switch (a->data_type())
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100177 {
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100178 case DataType::QASYMM8:
179 case DataType::QASYMM8_SIGNED:
180 case DataType::U8:
181 case DataType::S8:
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100182 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100183 if (is_data_type_quantized_asymmetric(a_to_use->data_type()) &&
184 info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100185 {
186 auto c_info_to_use = c == nullptr ? nullptr : c;
187 _asm_glue->configure(a_to_use, b, c_info_to_use, dst, asm_info);
188 _fused_assembly_path = _asm_glue->is_configured();
189 }
190 else
191 {
192 auto output_to_use = (_fuse_output_stage ? &_mm_result_s32 : dst);
193 _asm_glue->configure(a_to_use, b, nullptr, output_to_use, asm_info);
194 }
195 _assembly_path = _asm_glue->is_configured();
196 break;
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100197 }
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100198 default:
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100199 {
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100200 ARM_COMPUTE_ERROR("Datatype not supported");
201 break;
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100202 }
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100203 }
204 }
205#endif /* __aarch64__ */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100206 if (!(_assembly_path || _run_vector_matrix_multiplication))
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100207 {
208 matrix_a = &_tmp_a;
209 matrix_b = &_tmp_b;
210
211 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100212 _tmp_a =
213 TensorInfo(compute_interleaved_shape(*a_to_use), 1, a_to_use->data_type(), a_to_use->quantization_info());
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100214 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
215 _tmp_b = TensorInfo(compute_transpose1xW_shape(*b), 1, b->data_type(), b->quantization_info());
216
217 // Configure interleave kernel
218 _mtx_a_reshape_kernel = std::make_unique<kernels::CpuGemmInterleave4x4Kernel>();
219 _mtx_a_reshape_kernel->configure(a_to_use, &_tmp_a);
220
221 // Configure transpose kernel
222 _mtx_b_reshape_kernel = std::make_unique<kernels::CpuGemmTranspose1xWKernel>();
223 _mtx_b_reshape_kernel->configure(b, &_tmp_b);
224 }
225
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100226 if (!_fused_assembly_path)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100227 {
228 // Build reduction info
229 const GEMMLowpReductionKernelInfo reduction_info(a_to_use->dimension(0), false, 0, false);
230
231 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100232 if (_a_offset != 0)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100233 {
234 _vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
235
236 // Configure Matrix B reduction kernel
237 _mtx_b_reduction_kernel = std::make_unique<kernels::CpuGemmLowpMatrixBReductionKernel>();
238 _mtx_b_reduction_kernel->configure(b, &_vector_sum_col, reduction_info);
239 }
240
241 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100242 if (_b_offset != 0)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100243 {
244 _vector_sum_row = TensorInfo(compute_reductionB_shape(*a_to_use), 1, DataType::S32);
245
246 // Configure matrix A reduction kernel
247 _mtx_a_reduction_kernel = std::make_unique<kernels::CpuGemmLowpMatrixAReductionKernel>();
248 _mtx_a_reduction_kernel->configure(a_to_use, &_vector_sum_row, reduction_info);
249 }
250
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100251 if (_fuse_output_stage)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100252 {
253 // Configure matrix multiply kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100254 if (!_assembly_path)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100255 {
256 _mm_kernel = std::make_unique<kernels::CpuGemmLowpMatrixMultiplyKernel>();
257 _mm_kernel->configure(matrix_a, matrix_b, &_mm_result_s32);
258 }
259
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100260 _offset_contribution_output_stage_kernel =
261 std::make_unique<kernels::CpuGemmLowpOffsetContributionOutputStageKernel>();
262 _offset_contribution_output_stage_kernel->configure(
263 &_mm_result_s32, _a_offset == 0 ? nullptr : &_vector_sum_col,
264 _b_offset == 0 ? nullptr : &_vector_sum_row, c, _flip_signedness ? &_signed_output : dst,
265 a->dimension(0), _a_offset, _b_offset, info.gemmlowp_output_stage());
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100266
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100267 if (_flip_signedness)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100268 {
269 _convert_from_signed_asymm = std::make_unique<kernels::CpuConvertQuantizedSignednessKernel>();
270 _convert_from_signed_asymm->configure(&_signed_output, dst);
271 }
272 }
273 else
274 {
275 // Configure matrix multiply kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100276 if (!_assembly_path)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100277 {
278 _mm_kernel = std::make_unique<kernels::CpuGemmLowpMatrixMultiplyKernel>();
279 _mm_kernel->configure(matrix_a, matrix_b, dst);
280 }
281 // Configure offset contribution kernel
282 _offset_contribution_kernel = std::make_unique<kernels::CpuGemmLowpOffsetContributionKernel>();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100283 _offset_contribution_kernel->configure(dst, _a_offset == 0 ? nullptr : &_vector_sum_col,
284 _b_offset == 0 ? nullptr : &_vector_sum_row, a_to_use->dimension(0),
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100285 _a_offset, _b_offset);
286 }
287 }
288 // Configure activation
289 const ActivationLayerInfo &activation = gemm_info.activation_info();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100290 _run_activation =
291 activation.enabled() && (!_assembly_path || !cpu::CpuGemmAssemblyDispatch::is_activation_supported(activation));
292 if (_run_activation)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100293 {
294 _activation_func = std::make_unique<CpuActivation>();
295 _activation_func->configure(dst, nullptr, activation);
296 }
297
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100298 if (_assembly_path)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100299 {
300 auto asm_mem_req = _asm_glue->workspace();
301 _aux_mem[AsmGemmWorkspace] = asm_mem_req[AsmGemmWorkspace];
302 _aux_mem[Pretranspose] = asm_mem_req[Pretranspose];
303 }
304
305 // Request memory for LHS and RHS reshape matrix
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100306 _aux_mem[VectorSumCol] =
307 MemoryInfo(offset_int_vec(VectorSumCol),
308 !_fused_assembly_path && _a_offset != 0 && _reshape_b_only_on_first_run ? MemoryLifetime::Persistent
309 : MemoryLifetime::Temporary,
310 _vector_sum_col.total_size());
311 _aux_mem[VectorSumRow] =
312 MemoryInfo(offset_int_vec(VectorSumRow), MemoryLifetime::Temporary, _vector_sum_row.total_size());
313 _aux_mem[TmpA] = MemoryInfo(offset_int_vec(TmpA), MemoryLifetime::Temporary, _tmp_a.total_size());
314 _aux_mem[TmpB] = MemoryInfo(offset_int_vec(TmpB),
315 _reshape_b_only_on_first_run ? MemoryLifetime::Persistent : MemoryLifetime::Temporary,
316 _tmp_b.total_size());
317 _aux_mem[MMResultS32] =
318 MemoryInfo(offset_int_vec(MMResultS32), MemoryLifetime::Temporary, _mm_result_s32.total_size());
319 _aux_mem[SignedA] = MemoryInfo(offset_int_vec(SignedA), MemoryLifetime::Temporary, _signed_a.total_size());
320 _aux_mem[SignedOutput] =
321 MemoryInfo(offset_int_vec(SignedOutput), MemoryLifetime::Temporary, _signed_output.total_size());
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100322}
323
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100324Status CpuGemmLowpMatrixMultiplyCore::validate(const ITensorInfo *a,
325 const ITensorInfo *b,
326 const ITensorInfo *c,
327 const ITensorInfo *output,
328 const GEMMInfo &gemm_info)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100329{
330 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100331 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(b, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
332 DataType::QSYMM8, DataType::QSYMM8_PER_CHANNEL);
333 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32, DataType::QASYMM8,
334 DataType::QASYMM8_SIGNED);
335 ARM_COMPUTE_RETURN_ERROR_ON_MSG(c != nullptr &&
336 gemm_info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::NONE,
337 "Bias addition not supported in NEGEMMLowpMatrixMultiplyCore for output S32");
338 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
339 (a)->dimension(0) != (b)->dimension(1),
340 "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100341 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
342 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
343
344 GEMMInfo info = gemm_info;
345 const ITensorInfo *matrix_a_info = a;
346 const ITensorInfo *matrix_b_info = b;
347
348 const ITensorInfo *a_to_use = a;
349
350 TensorInfo tmp_a_info{};
351 TensorInfo tmp_b_info{};
352 TensorInfo mm_result_s32_info{};
353
354 int32_t a_offset = a->quantization_info().uniform().offset;
355 int32_t b_offset = b->quantization_info().uniform().offset;
356
357 bool fuse_output_stage = info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100358 if (fuse_output_stage)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100359 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100360 auto_init_if_empty(mm_result_s32_info,
361 a->clone()->set_tensor_shape(output->tensor_shape()).set_data_type(DataType::S32));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100362 }
363
364 // Convert QASYMM8->QASYMM8_SIGNED
365 TensorInfo signed_a{};
366 TensorInfo signed_output{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100367 bool flip_signedness = is_data_type_quantized_per_channel(b->data_type()) &&
368 (a->data_type() == DataType::QASYMM8) && info.reshape_b_only_on_first_run();
369 if (flip_signedness)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100370 {
371 const int32_t offset_correction = 128;
372 const DataType dt = DataType::QASYMM8_SIGNED;
373 const UniformQuantizationInfo iqinfo = a_to_use->quantization_info().uniform();
374
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100375 signed_a = a_to_use->clone()->set_data_type(dt).set_quantization_info(
376 QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100377 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuConvertQuantizedSignednessKernel::validate(a_to_use, &signed_a));
378 a_to_use = &signed_a;
379 a_offset = signed_a.quantization_info().uniform().offset;
380
381 const UniformQuantizationInfo oqinfo = output->quantization_info().uniform();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100382 signed_output = output->clone()->set_data_type(dt).set_quantization_info(
383 QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100384
385 // Output stage correction
386 GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage();
387 output_stage_corr.gemmlowp_offset = signed_output.quantization_info().uniform().offset;
388 output_stage_corr.gemmlowp_min_bound -= offset_correction;
389 output_stage_corr.gemmlowp_max_bound -= offset_correction;
390 info.set_gemmlowp_output_stage(output_stage_corr);
391
392 // Update matrix a
393 matrix_a_info = &signed_a;
394 }
395
396 // Initialize assembly kernel meta-data
397 const AsmGemmInfo asm_info = init_assembly_metadata(info);
398
399 // Check if we need to run the optimized assembly kernel
400 bool run_optimised = false;
401 bool run_optimised_requantized = false;
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100402
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100403 if (!(!b->are_values_constant() &&
404 b->tensor_shape().z() > 1)) // Disable batch matmul as optimized GeMM handles batching differently.
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100405 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100406 if (is_data_type_quantized_asymmetric(a_to_use->data_type()) &&
407 info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100408 {
409 run_optimised = bool(CpuGemmAssemblyDispatch::validate(a_to_use, b, c, output, asm_info));
410 run_optimised_requantized = run_optimised;
411 }
412 else
413 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100414 run_optimised = bool(CpuGemmAssemblyDispatch::validate(
415 a_to_use, b, nullptr, fuse_output_stage ? &mm_result_s32_info : output, asm_info));
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100416 }
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100417 }
418
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100419 if (run_optimised)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100420 {
421 ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100422 if (info.depth_output_gemm3d() != 0)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100423 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100424 if (info.reinterpret_input_as_3d())
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100425 {
426 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
427 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2));
428 }
429 else
430 {
431 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2));
432 }
433 }
434 else
435 {
436 ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1));
437 }
438 }
439 else
440 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100441 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.reinterpret_input_as_3d(),
442 "NEGEMM cannot reinterpret the input tensor as 3D");
443 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.depth_output_gemm3d() != 0,
444 "NEGEMM cannot reinterpret the output tensor as 3D");
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100445
446 const bool run_vector_matrix_multiplication = a->dimension(1) < 2;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100447 if (!run_vector_matrix_multiplication)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100448 {
449 matrix_a_info = &tmp_a_info;
450 matrix_b_info = &tmp_b_info;
451
452 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
453 TensorShape shape_tmp_a = a->tensor_shape();
454 shape_tmp_a.set(0, a->dimension(0) * 4);
455 shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f));
456
457 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
458 TensorShape shape_tmp_b = b->tensor_shape();
459 shape_tmp_b.set(0, b->dimension(1) * 16);
460 shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f));
461
462 // Validate interleave kernel
463 auto_init_if_empty(tmp_a_info, a_to_use->clone()->set_tensor_shape(shape_tmp_a));
464 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(shape_tmp_b));
465
466 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmInterleave4x4Kernel::validate(a_to_use, &tmp_a_info));
467 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmTranspose1xWKernel::validate(b, &tmp_b_info));
468 }
469 }
470
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100471 if (!run_optimised_requantized)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100472 {
473 TensorInfo info_vector_sum_col{};
474 TensorInfo info_vector_sum_row{};
475
476 const GEMMLowpReductionKernelInfo reduction_info(a_to_use->dimension(0), false, 0, false);
477
478 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100479 if (a_offset != 0)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100480 {
481 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
482
483 // Configure Matrix B reduction kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100484 ARM_COMPUTE_RETURN_ON_ERROR(
485 kernels::CpuGemmLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, reduction_info));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100486 }
487
488 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100489 if (b_offset != 0)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100490 {
491 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
492
493 // Configure matrix A reduction kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100494 ARM_COMPUTE_RETURN_ON_ERROR(
495 kernels::CpuGemmLowpMatrixAReductionKernel::validate(a_to_use, &info_vector_sum_row, reduction_info));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100496 }
497
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100498 if (fuse_output_stage)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100499 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100500 if (!run_optimised)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100501 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100502 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
503 info.reinterpret_input_as_3d(),
504 "CpuGemmLowpMatrixMultiplyKernel cannot reinterpret the input tensor as 3D");
505 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
506 info.depth_output_gemm3d() != 0,
507 "CpuGemmLowpMatrixMultiplyKernel cannot reinterpret the output tensor as 3D");
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100508
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100509 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmLowpMatrixMultiplyKernel::validate(
510 matrix_a_info, matrix_b_info, &mm_result_s32_info));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100511 }
512
513 // Validate offset contribution kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100514 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmLowpOffsetContributionOutputStageKernel::validate(
515 &mm_result_s32_info, a_offset == 0 ? nullptr : &info_vector_sum_col,
516 b_offset == 0 ? nullptr : &info_vector_sum_row, c, flip_signedness ? &signed_output : output, a_offset,
517 b_offset, info.gemmlowp_output_stage()));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100518 }
519 else
520 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100521 if (!run_optimised)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100522 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100523 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
524 info.reinterpret_input_as_3d(),
525 "CpuGemmLowpMatrixMultiplyKernel cannot reinterpret the input tensor as 3D");
526 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
527 info.depth_output_gemm3d() != 0,
528 "CpuGemmLowpMatrixMultiplyKernel cannot reinterpret the output tensor as 3D");
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100529
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100530 ARM_COMPUTE_RETURN_ON_ERROR(
531 kernels::CpuGemmLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100532 }
533 // Validate offset contribution kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100534 ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmLowpOffsetContributionKernel::validate(
535 output, a_offset == 0 ? nullptr : &info_vector_sum_col, b_offset == 0 ? nullptr : &info_vector_sum_row,
536 a_offset, b_offset));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100537 }
538 }
539
540 // Validate activation
541 const ActivationLayerInfo &activation = gemm_info.activation_info();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100542 if (activation.enabled())
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100543 {
544 ARM_COMPUTE_RETURN_ON_ERROR(CpuActivation::validate(output, nullptr, activation));
545 }
546
547 return Status{};
548}
549
550void CpuGemmLowpMatrixMultiplyCore::run(ITensorPack &tensors)
551{
552 prepare(tensors);
Georgios Pinitas22f5ed52021-07-23 18:58:43 +0100553
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100554 auto a = tensors.get_const_tensor(TensorType::ACL_SRC_0);
555 auto b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
556 auto c = tensors.get_const_tensor(TensorType::ACL_SRC_2);
557 auto dst = tensors.get_tensor(TensorType::ACL_DST);
558 auto a_to_use = a;
559 auto matrix_a = a;
560 auto matrix_b = b;
561
562 CpuAuxTensorHandler vector_sum_col(offset_int_vec(VectorSumCol), _vector_sum_col, tensors, false);
563 CpuAuxTensorHandler vector_sum_row(offset_int_vec(VectorSumRow), _vector_sum_row, tensors, false);
564 CpuAuxTensorHandler tmp_a(offset_int_vec(TmpA), _tmp_a, tensors, false);
565 CpuAuxTensorHandler tmp_b(offset_int_vec(TmpB), _tmp_b, tensors, true);
566 CpuAuxTensorHandler mm_result_s32(offset_int_vec(MMResultS32), _mm_result_s32, tensors, false);
567 CpuAuxTensorHandler signed_a(offset_int_vec(SignedA), _signed_a, tensors, false);
568 CpuAuxTensorHandler signed_output(offset_int_vec(SignedOutput), _signed_output, tensors, false);
569
570 // Convert QASYMM8->QASYMM8_SIGNED
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100571 if (_flip_signedness)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100572 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100573 ITensorPack pack = {{TensorType::ACL_SRC, a}, {TensorType::ACL_DST, signed_a.get()}};
574 NEScheduler::get().schedule_op(_convert_to_signed_asymm.get(), Window::DimY, _convert_to_signed_asymm->window(),
575 pack);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100576 a_to_use = signed_a.get();
Georgios Pinitasd4a5bc52021-08-12 07:42:51 +0100577 matrix_a = signed_a.get();
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100578 }
579
580 // Run GEMM
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100581 if (_asm_glue->is_configured())
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100582 {
583 ITensorPack asm_glue_tensors = tensors;
584 auto output_to_use = (_fuse_output_stage ? mm_result_s32.get() : dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100585 if (is_data_type_quantized_asymmetric(a_to_use->info()->data_type()) &&
586 _gemm_info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100587 {
588 asm_glue_tensors.add_const_tensor(TensorType::ACL_SRC_0, a_to_use);
589 asm_glue_tensors.add_const_tensor(TensorType::ACL_SRC_1, b);
590 asm_glue_tensors.add_const_tensor(TensorType::ACL_SRC_2, c);
591 asm_glue_tensors.add_tensor(TensorType::ACL_DST, dst);
592 }
593 else
594 {
595 asm_glue_tensors.add_const_tensor(TensorType::ACL_SRC_0, a_to_use);
596 asm_glue_tensors.add_const_tensor(TensorType::ACL_SRC_1, b);
597 asm_glue_tensors.add_tensor(TensorType::ACL_DST, output_to_use);
598 }
599 _asm_glue->run(asm_glue_tensors);
600 }
601 else
602 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100603 if (!_run_vector_matrix_multiplication)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100604 {
605 matrix_a = tmp_a.get();
606 matrix_b = tmp_b.get();
607 // Run interleave kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100608 ITensorPack pack_a = {{TensorType::ACL_SRC, a_to_use}, {TensorType::ACL_DST, tmp_a.get()}};
609 NEScheduler::get().schedule_op(_mtx_a_reshape_kernel.get(), Window::DimY, _mtx_a_reshape_kernel->window(),
610 pack_a);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100611
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100612 if (!_reshape_b_only_on_first_run)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100613 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100614 ITensorPack pack_b = {{TensorType::ACL_SRC, b}, {TensorType::ACL_DST, tmp_b.get()}};
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100615 // Run transpose kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100616 NEScheduler::get().schedule_op(_mtx_b_reshape_kernel.get(), Window::DimY,
617 _mtx_b_reshape_kernel->window(), pack_b);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100618 }
619 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100620 ITensorPack pack_mm = {{TensorType::ACL_SRC_0, matrix_a}, {TensorType::ACL_SRC_1, matrix_b}};
621 if (_fuse_output_stage)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100622 {
623 pack_mm.add_tensor(TensorType::ACL_DST, mm_result_s32.get());
624 }
625 else
626 {
627 pack_mm.add_tensor(TensorType::ACL_DST, dst);
628 }
629 NEScheduler::get().schedule_op(_mm_kernel.get(), Window::DimY, _mm_kernel->window(), pack_mm);
630 }
631
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100632 if (!_fused_assembly_path)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100633 {
634 // Run matrix A reduction kernel only if _b_offset is not equal to 0
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100635 if (_b_offset != 0)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100636 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100637 ITensorPack pack = {{TensorType::ACL_SRC, a_to_use}, {TensorType::ACL_DST, vector_sum_row.get()}};
638 NEScheduler::get().schedule_op(_mtx_a_reduction_kernel.get(), Window::DimX,
639 _mtx_a_reduction_kernel->window(), pack);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100640 }
641
642 // Run matrix B reduction kernel only if _a_offset is not equal to 0
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100643 if (_a_offset != 0 && !_reshape_b_only_on_first_run)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100644 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100645 ITensorPack pack = {{TensorType::ACL_SRC, b}, {TensorType::ACL_DST, vector_sum_col.get()}};
646 NEScheduler::get().schedule_op(_mtx_b_reduction_kernel.get(), Window::DimX,
647 _mtx_b_reduction_kernel->window(), pack);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100648 }
649
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100650 if (_fuse_output_stage)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100651 {
652 ITensorPack pack;
653 pack.add_tensor(TensorType::ACL_SRC_0, mm_result_s32.get());
654 pack.add_tensor(TensorType::ACL_SRC_1, _a_offset == 0 ? nullptr : vector_sum_col.get());
655 pack.add_tensor(TensorType::ACL_SRC_2, _b_offset == 0 ? nullptr : vector_sum_row.get());
656 pack.add_tensor(TensorType::ACL_SRC_3, c);
657 pack.add_tensor(TensorType::ACL_DST, _flip_signedness ? signed_output.get() : dst);
658
659 // Run offset contribution kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100660 NEScheduler::get().schedule_op(_offset_contribution_output_stage_kernel.get(), Window::DimY,
661 _offset_contribution_output_stage_kernel->window(), pack);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100662 }
663 else
664 {
665 ITensorPack pack;
666 pack.add_tensor(TensorType::ACL_SRC_0, _a_offset == 0 ? nullptr : vector_sum_col.get());
667 pack.add_tensor(TensorType::ACL_SRC_1, _b_offset == 0 ? nullptr : vector_sum_row.get());
668 pack.add_tensor(TensorType::ACL_DST, dst);
669
670 // Run offset contribution kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100671 NEScheduler::get().schedule_op(_offset_contribution_kernel.get(), Window::DimY,
672 _offset_contribution_kernel->window(), pack);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100673 }
674 }
675
676 // Convert QASYMM8_SIGNED->QASYMM8
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100677 if (!_fused_assembly_path && _fuse_output_stage && _flip_signedness)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100678 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100679 ITensorPack pack = {{TensorType::ACL_SRC, signed_output.get()}, {TensorType::ACL_DST, dst}};
680 NEScheduler::get().schedule_op(_convert_from_signed_asymm.get(), Window::DimY,
681 _convert_from_signed_asymm->window(), pack);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100682 }
683
684 // Run fused activation unless already run in the fused assembly
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100685 if (_run_activation)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100686 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100687 ITensorPack pack = {{TensorType::ACL_SRC, dst}, {TensorType::ACL_DST, dst}};
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100688 _activation_func->run(pack);
689 }
690}
691
692void CpuGemmLowpMatrixMultiplyCore::prepare(ITensorPack &tensors)
693{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100694 if (!_is_prepared)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100695 {
696 auto original_b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
697 // Run assembly reshape
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100698 if (_asm_glue->is_configured())
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100699 {
700 _asm_glue->prepare(tensors);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100701 }
702 // Run non-assembly reshape
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100703 else if (_reshape_b_only_on_first_run && !_run_vector_matrix_multiplication && !_asm_glue->is_configured())
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100704 {
705 // Run reshape kernel and mark original weights tensor as unused
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100706 ITensor *tmp_b_p = utils::cast::polymorphic_downcast<ITensor *>(tensors.get_tensor(offset_int_vec(TmpB)));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100707 CpuAuxTensorHandler tmp_b(_tmp_b, *tmp_b_p);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100708 ITensorPack pack = {{TensorType::ACL_SRC, original_b}, {TensorType::ACL_DST, tmp_b.get()}};
709 NEScheduler::get().schedule_op(_mtx_b_reshape_kernel.get(), Window::DimY, _mtx_b_reshape_kernel->window(),
710 pack);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100711 }
712
713 // Run matrix B reduction kernel only if _a_offset is not equal to 0
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100714 if (!_fused_assembly_path && _a_offset != 0 && _reshape_b_only_on_first_run)
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100715 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100716 ITensor *vector_sum_col_p =
717 utils::cast::polymorphic_downcast<ITensor *>(tensors.get_tensor(offset_int_vec(VectorSumCol)));
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100718 CpuAuxTensorHandler vector_sum_col(_vector_sum_col, *vector_sum_col_p);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100719 ITensorPack pack = {{TensorType::ACL_SRC, original_b}, {TensorType::ACL_DST, vector_sum_col.get()}};
720 NEScheduler::get().schedule_op(_mtx_b_reduction_kernel.get(), Window::DimX,
721 _mtx_b_reduction_kernel->window(), pack);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100722 }
723 _is_prepared = true;
724 }
725}
726experimental::MemoryRequirements CpuGemmLowpMatrixMultiplyCore::workspace() const
727{
728 return _aux_mem;
729}
730} // namespace cpu
731} // namespace arm_compute