Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | 9b0a6b4 | 2023-04-03 16:27:25 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2023 Arm Limited. |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 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 Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/operators/CpuGemmLowpMatrixMultiplyCore.h" |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 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" |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 32 | #include "arm_compute/core/Validate.h" |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 34 | #include "arm_compute/runtime/TensorAllocator.h" |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 35 | |
ramelg01 | 3ae3d88 | 2021-09-12 23:07:47 +0100 | [diff] [blame] | 36 | #include "src/common/utils/Log.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 37 | #include "src/core/helpers/AutoConfiguration.h" |
| 38 | #include "src/core/helpers/MemoryHelpers.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 39 | #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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 49 | |
| 50 | using namespace arm_compute::misc::shape_calculator; |
| 51 | using namespace arm_compute::experimental; |
| 52 | |
| 53 | namespace arm_compute |
| 54 | { |
| 55 | namespace cpu |
| 56 | { |
| 57 | namespace |
| 58 | { |
| 59 | cpu::AsmGemmInfo init_assembly_metadata(const GEMMInfo &info) |
| 60 | { |
| 61 | cpu::AsmGemmInfo asm_info; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 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(); |
| 67 | asm_info.fast_mode = info.fast_math(); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 68 | |
| 69 | return asm_info; |
| 70 | } |
| 71 | } // namespace |
| 72 | |
| 73 | CpuGemmLowpMatrixMultiplyCore::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 | } |
| 106 | CpuGemmLowpMatrixMultiplyCore::~CpuGemmLowpMatrixMultiplyCore() = default; |
| 107 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 108 | void CpuGemmLowpMatrixMultiplyCore::configure( |
| 109 | const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *dst, const GEMMInfo &gemm_info) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 110 | { |
| 111 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, dst); |
| 112 | ARM_COMPUTE_ERROR_THROW_ON(CpuGemmLowpMatrixMultiplyCore::validate(a, b, c, dst, gemm_info)); |
ramelg01 | 3ae3d88 | 2021-09-12 23:07:47 +0100 | [diff] [blame] | 113 | ARM_COMPUTE_LOG_PARAMS(a, b, c, dst, gemm_info); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 114 | |
| 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 Do | 9b0a6b4 | 2023-04-03 16:27:25 +0100 | [diff] [blame] | 123 | _reshape_b_only_on_first_run = b->are_values_constant(); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 124 | _is_prepared = false; |
| 125 | _fused_assembly_path = false; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 126 | _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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 129 | |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 135 | if (_flip_signedness) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 136 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 141 | _signed_a = a_to_use->clone()->set_data_type(dt).set_quantization_info( |
| 142 | QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction)); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 143 | _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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 149 | _signed_output = dst->clone()->set_data_type(dt).set_quantization_info( |
| 150 | QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction)); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 151 | |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 164 | if (info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 165 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 173 | if (!(!b->are_values_constant() && |
| 174 | b->tensor_shape().z() > 1)) // Disable batch matmul as optimized GeMM handles batching differently. |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 175 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 176 | switch (a->data_type()) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 177 | { |
Viet-Hoa Do | 9b0a6b4 | 2023-04-03 16:27:25 +0100 | [diff] [blame] | 178 | case DataType::QASYMM8: |
| 179 | case DataType::QASYMM8_SIGNED: |
| 180 | case DataType::U8: |
| 181 | case DataType::S8: |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 182 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 183 | if (is_data_type_quantized_asymmetric(a_to_use->data_type()) && |
| 184 | info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT) |
Viet-Hoa Do | 9b0a6b4 | 2023-04-03 16:27:25 +0100 | [diff] [blame] | 185 | { |
| 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 197 | } |
Viet-Hoa Do | 9b0a6b4 | 2023-04-03 16:27:25 +0100 | [diff] [blame] | 198 | default: |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 199 | { |
Viet-Hoa Do | 9b0a6b4 | 2023-04-03 16:27:25 +0100 | [diff] [blame] | 200 | ARM_COMPUTE_ERROR("Datatype not supported"); |
| 201 | break; |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 202 | } |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | #endif /* __aarch64__ */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 206 | if (!(_assembly_path || _run_vector_matrix_multiplication)) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 207 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 212 | _tmp_a = |
| 213 | TensorInfo(compute_interleaved_shape(*a_to_use), 1, a_to_use->data_type(), a_to_use->quantization_info()); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 214 | // 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 226 | if (!_fused_assembly_path) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 227 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 232 | if (_a_offset != 0) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 233 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 242 | if (_b_offset != 0) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 243 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 251 | if (_fuse_output_stage) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 252 | { |
| 253 | // Configure matrix multiply kernel |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 254 | if (!_assembly_path) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 255 | { |
| 256 | _mm_kernel = std::make_unique<kernels::CpuGemmLowpMatrixMultiplyKernel>(); |
| 257 | _mm_kernel->configure(matrix_a, matrix_b, &_mm_result_s32); |
| 258 | } |
| 259 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 260 | _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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 266 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 267 | if (_flip_signedness) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 268 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 276 | if (!_assembly_path) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 277 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 283 | _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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 285 | _a_offset, _b_offset); |
| 286 | } |
| 287 | } |
| 288 | // Configure activation |
| 289 | const ActivationLayerInfo &activation = gemm_info.activation_info(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 290 | _run_activation = |
| 291 | activation.enabled() && (!_assembly_path || !cpu::CpuGemmAssemblyDispatch::is_activation_supported(activation)); |
| 292 | if (_run_activation) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 293 | { |
| 294 | _activation_func = std::make_unique<CpuActivation>(); |
| 295 | _activation_func->configure(dst, nullptr, activation); |
| 296 | } |
| 297 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 298 | if (_assembly_path) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 299 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 306 | _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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 324 | Status CpuGemmLowpMatrixMultiplyCore::validate(const ITensorInfo *a, |
| 325 | const ITensorInfo *b, |
| 326 | const ITensorInfo *c, |
| 327 | const ITensorInfo *output, |
| 328 | const GEMMInfo &gemm_info) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 329 | { |
| 330 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 331 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 341 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 358 | if (fuse_output_stage) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 359 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 360 | auto_init_if_empty(mm_result_s32_info, |
| 361 | a->clone()->set_tensor_shape(output->tensor_shape()).set_data_type(DataType::S32)); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // Convert QASYMM8->QASYMM8_SIGNED |
| 365 | TensorInfo signed_a{}; |
| 366 | TensorInfo signed_output{}; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 367 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 370 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 375 | signed_a = a_to_use->clone()->set_data_type(dt).set_quantization_info( |
| 376 | QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction)); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 377 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 382 | signed_output = output->clone()->set_data_type(dt).set_quantization_info( |
| 383 | QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction)); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 384 | |
| 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 Do | 9b0a6b4 | 2023-04-03 16:27:25 +0100 | [diff] [blame] | 402 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 403 | if (!(!b->are_values_constant() && |
| 404 | b->tensor_shape().z() > 1)) // Disable batch matmul as optimized GeMM handles batching differently. |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 405 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 406 | if (is_data_type_quantized_asymmetric(a_to_use->data_type()) && |
| 407 | info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT) |
Viet-Hoa Do | 9b0a6b4 | 2023-04-03 16:27:25 +0100 | [diff] [blame] | 408 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 414 | run_optimised = bool(CpuGemmAssemblyDispatch::validate( |
| 415 | a_to_use, b, nullptr, fuse_output_stage ? &mm_result_s32_info : output, asm_info)); |
Viet-Hoa Do | 9b0a6b4 | 2023-04-03 16:27:25 +0100 | [diff] [blame] | 416 | } |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 417 | } |
| 418 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 419 | if (run_optimised) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 420 | { |
| 421 | ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 422 | if (info.depth_output_gemm3d() != 0) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 423 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 424 | if (info.reinterpret_input_as_3d()) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 425 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 441 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 445 | |
| 446 | const bool run_vector_matrix_multiplication = a->dimension(1) < 2; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 447 | if (!run_vector_matrix_multiplication) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 448 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 471 | if (!run_optimised_requantized) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 472 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 479 | if (a_offset != 0) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 480 | { |
| 481 | info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32); |
| 482 | |
| 483 | // Configure Matrix B reduction kernel |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 484 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 485 | kernels::CpuGemmLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, reduction_info)); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // Validate Matrix A reduction kernel only if _b_offset is not equal to 0 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 489 | if (b_offset != 0) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 490 | { |
| 491 | info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32); |
| 492 | |
| 493 | // Configure matrix A reduction kernel |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 494 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 495 | kernels::CpuGemmLowpMatrixAReductionKernel::validate(a_to_use, &info_vector_sum_row, reduction_info)); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 498 | if (fuse_output_stage) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 499 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 500 | if (!run_optimised) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 501 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 502 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 508 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 509 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuGemmLowpMatrixMultiplyKernel::validate( |
| 510 | matrix_a_info, matrix_b_info, &mm_result_s32_info)); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | // Validate offset contribution kernel |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 514 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 518 | } |
| 519 | else |
| 520 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 521 | if (!run_optimised) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 522 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 523 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 529 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 530 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 531 | kernels::CpuGemmLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output)); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 532 | } |
| 533 | // Validate offset contribution kernel |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 534 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | // Validate activation |
| 541 | const ActivationLayerInfo &activation = gemm_info.activation_info(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 542 | if (activation.enabled()) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 543 | { |
| 544 | ARM_COMPUTE_RETURN_ON_ERROR(CpuActivation::validate(output, nullptr, activation)); |
| 545 | } |
| 546 | |
| 547 | return Status{}; |
| 548 | } |
| 549 | |
| 550 | void CpuGemmLowpMatrixMultiplyCore::run(ITensorPack &tensors) |
| 551 | { |
| 552 | prepare(tensors); |
Georgios Pinitas | 22f5ed5 | 2021-07-23 18:58:43 +0100 | [diff] [blame] | 553 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 554 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 571 | if (_flip_signedness) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 572 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 573 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 576 | a_to_use = signed_a.get(); |
Georgios Pinitas | d4a5bc5 | 2021-08-12 07:42:51 +0100 | [diff] [blame] | 577 | matrix_a = signed_a.get(); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // Run GEMM |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 581 | if (_asm_glue->is_configured()) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 582 | { |
| 583 | ITensorPack asm_glue_tensors = tensors; |
| 584 | auto output_to_use = (_fuse_output_stage ? mm_result_s32.get() : dst); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 585 | if (is_data_type_quantized_asymmetric(a_to_use->info()->data_type()) && |
| 586 | _gemm_info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 587 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 603 | if (!_run_vector_matrix_multiplication) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 604 | { |
| 605 | matrix_a = tmp_a.get(); |
| 606 | matrix_b = tmp_b.get(); |
| 607 | // Run interleave kernel |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 608 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 611 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 612 | if (!_reshape_b_only_on_first_run) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 613 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 614 | ITensorPack pack_b = {{TensorType::ACL_SRC, b}, {TensorType::ACL_DST, tmp_b.get()}}; |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 615 | // Run transpose kernel |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 616 | NEScheduler::get().schedule_op(_mtx_b_reshape_kernel.get(), Window::DimY, |
| 617 | _mtx_b_reshape_kernel->window(), pack_b); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 618 | } |
| 619 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 620 | ITensorPack pack_mm = {{TensorType::ACL_SRC_0, matrix_a}, {TensorType::ACL_SRC_1, matrix_b}}; |
| 621 | if (_fuse_output_stage) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 622 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 632 | if (!_fused_assembly_path) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 633 | { |
| 634 | // Run matrix A reduction kernel only if _b_offset is not equal to 0 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 635 | if (_b_offset != 0) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 636 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 637 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | // Run matrix B reduction kernel only if _a_offset is not equal to 0 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 643 | if (_a_offset != 0 && !_reshape_b_only_on_first_run) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 644 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 645 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 648 | } |
| 649 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 650 | if (_fuse_output_stage) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 651 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 660 | NEScheduler::get().schedule_op(_offset_contribution_output_stage_kernel.get(), Window::DimY, |
| 661 | _offset_contribution_output_stage_kernel->window(), pack); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 662 | } |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 671 | NEScheduler::get().schedule_op(_offset_contribution_kernel.get(), Window::DimY, |
| 672 | _offset_contribution_kernel->window(), pack); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | // Convert QASYMM8_SIGNED->QASYMM8 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 677 | if (!_fused_assembly_path && _fuse_output_stage && _flip_signedness) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 678 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 679 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | // Run fused activation unless already run in the fused assembly |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 685 | if (_run_activation) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 686 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 687 | ITensorPack pack = {{TensorType::ACL_SRC, dst}, {TensorType::ACL_DST, dst}}; |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 688 | _activation_func->run(pack); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | void CpuGemmLowpMatrixMultiplyCore::prepare(ITensorPack &tensors) |
| 693 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 694 | if (!_is_prepared) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 695 | { |
| 696 | auto original_b = tensors.get_const_tensor(TensorType::ACL_SRC_1); |
| 697 | // Run assembly reshape |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 698 | if (_asm_glue->is_configured()) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 699 | { |
| 700 | _asm_glue->prepare(tensors); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 701 | } |
| 702 | // Run non-assembly reshape |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 703 | else if (_reshape_b_only_on_first_run && !_run_vector_matrix_multiplication && !_asm_glue->is_configured()) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 704 | { |
| 705 | // Run reshape kernel and mark original weights tensor as unused |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 706 | ITensor *tmp_b_p = utils::cast::polymorphic_downcast<ITensor *>(tensors.get_tensor(offset_int_vec(TmpB))); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 707 | CpuAuxTensorHandler tmp_b(_tmp_b, *tmp_b_p); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 708 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | // Run matrix B reduction kernel only if _a_offset is not equal to 0 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 714 | if (!_fused_assembly_path && _a_offset != 0 && _reshape_b_only_on_first_run) |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 715 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 716 | ITensor *vector_sum_col_p = |
| 717 | utils::cast::polymorphic_downcast<ITensor *>(tensors.get_tensor(offset_int_vec(VectorSumCol))); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 718 | CpuAuxTensorHandler vector_sum_col(_vector_sum_col, *vector_sum_col_p); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 719 | 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 Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 722 | } |
| 723 | _is_prepared = true; |
| 724 | } |
| 725 | } |
| 726 | experimental::MemoryRequirements CpuGemmLowpMatrixMultiplyCore::workspace() const |
| 727 | { |
| 728 | return _aux_mem; |
| 729 | } |
| 730 | } // namespace cpu |
| 731 | } // namespace arm_compute |