Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 71ac903 | 2019-11-14 14:31:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +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 | */ |
| 24 | #include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/KernelDescriptors.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h" |
| 32 | #include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/Types.h" |
| 35 | #include "arm_compute/core/Validate.h" |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 36 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 37 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 38 | #include "arm_compute/runtime/TensorAllocator.h" |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 39 | #include "support/MemorySupport.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 40 | |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 41 | namespace arm_compute |
| 42 | { |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 43 | using namespace arm_compute::misc::shape_calculator; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 44 | |
| 45 | NEGEMMLowpMatrixMultiplyCore::NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager) |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 46 | : _memory_group(memory_manager), _asm_glue(memory_manager), _mm_kernel(nullptr), _mtx_a_reshape_kernel(nullptr), _mtx_b_reshape_kernel(nullptr), _mtx_a_reduction_kernel(), _mtx_b_reduction_kernel(), |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 47 | _offset_contribution_kernel(), _offset_contribution_output_stage_kernel(), _activation_func(), _convert_to_signed_asymm(), _convert_from_signed_asymm(), _vector_sum_col(), _vector_sum_row(), _tmp_a(), |
| 48 | _tmp_b(), _mm_result_s32(), _signed_a(), _signed_output(), _original_b(nullptr), _a_offset(0), _b_offset(0), _run_vector_matrix_multiplication(false), _assembly_path(false), |
| 49 | _fused_assembly_path(false), _reshape_b_only_on_first_run(false), _is_prepared(false), _fuse_output_stage(false), _run_activation(false), _flip_signedness(false) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 50 | { |
| 51 | } |
| 52 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 53 | void NEGEMMLowpMatrixMultiplyCore::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *output, const GEMMInfo &gemm_info) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 54 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 56 | ARM_COMPUTE_UNUSED(c); |
| 57 | ARM_COMPUTE_ERROR_THROW_ON(NEGEMMLowpMatrixMultiplyCore::validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), gemm_info)); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 58 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 59 | const ITensor *matrix_a = a; |
| 60 | const ITensor *matrix_b = b; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 61 | GEMMInfo info = gemm_info; |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 62 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 63 | // Clear state |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 64 | _mtx_a_reshape_kernel = nullptr; |
| 65 | _mtx_b_reshape_kernel = nullptr; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 66 | |
| 67 | // Set internal variables |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 68 | _a_offset = a->info()->quantization_info().uniform().offset; |
| 69 | _b_offset = b->info()->quantization_info().uniform().offset; |
Gian Marco | c7f9b89 | 2017-11-30 14:31:13 +0000 | [diff] [blame] | 70 | _run_vector_matrix_multiplication = a->info()->dimension(1) < 2; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 71 | _reshape_b_only_on_first_run = info.reshape_b_only_on_first_run(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 72 | _is_prepared = false; |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 73 | _fused_assembly_path = false; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 74 | _flip_signedness = is_data_type_quantized_per_channel(b->info()->data_type()) && (a->info()->data_type() == DataType::QASYMM8) && _reshape_b_only_on_first_run; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 75 | _original_b = b; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 76 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 77 | const ITensor *a_to_use = a; |
| 78 | |
| 79 | // Convert to QASYMM8 -> QASYMM8_SIGNED and back |
| 80 | if(_flip_signedness) |
| 81 | { |
| 82 | const int32_t offset_correction = 128; |
| 83 | const DataType dt = DataType::QASYMM8_SIGNED; |
| 84 | const UniformQuantizationInfo iqinfo = a_to_use->info()->quantization_info().uniform(); |
| 85 | |
| 86 | _signed_a.allocator()->init(a_to_use->info()->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction))); |
| 87 | _memory_group.manage(&_signed_a); |
| 88 | _convert_to_signed_asymm.configure(a_to_use, &_signed_a); |
| 89 | a_to_use = &_signed_a; |
| 90 | _a_offset = _signed_a.info()->quantization_info().uniform().offset; |
| 91 | |
| 92 | const UniformQuantizationInfo oqinfo = output->info()->quantization_info().uniform(); |
| 93 | _memory_group.manage(&_signed_output); |
| 94 | _signed_output.allocator()->init(output->info()->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction))); |
| 95 | |
| 96 | // Output stage correction |
| 97 | GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage(); |
| 98 | output_stage_corr.gemmlowp_offset = _signed_output.info()->quantization_info().uniform().offset; |
| 99 | output_stage_corr.gemmlowp_min_bound -= offset_correction; |
| 100 | output_stage_corr.gemmlowp_max_bound -= offset_correction; |
| 101 | info.set_gemmlowp_output_stage(output_stage_corr); |
Georgios Pinitas | 63d4dbd | 2019-11-08 11:51:56 +0000 | [diff] [blame] | 102 | |
| 103 | // Update matrix a |
| 104 | matrix_a = &_signed_a; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 105 | } |
| 106 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 107 | // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 108 | if(info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE) |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 109 | { |
| 110 | _fuse_output_stage = true; |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 111 | _memory_group.manage(&_mm_result_s32); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 112 | TensorInfo info_mm_result_s32(output->info()->tensor_shape(), 1, DataType::S32); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 113 | _mm_result_s32.allocator()->init(info_mm_result_s32); |
| 114 | } |
| 115 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 116 | #ifdef __aarch64__ |
| 117 | switch(a->info()->data_type()) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 118 | { |
Pablo Tello | 66c656a | 2018-03-15 10:34:58 +0000 | [diff] [blame] | 119 | case DataType::QASYMM8: |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 120 | case DataType::QASYMM8_SIGNED: |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 121 | case DataType::U8: |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 122 | case DataType::S8: |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 123 | { |
Michalis Spyrou | 71ac903 | 2019-11-14 14:31:44 +0000 | [diff] [blame] | 124 | if(is_data_type_quantized_asymmetric(a_to_use->info()->data_type()) && info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT) |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 125 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 126 | _asm_glue.configure(a_to_use, b, c, output, gemm_info); |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 127 | _fused_assembly_path = _asm_glue.is_configured(); |
| 128 | } |
| 129 | else |
| 130 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 131 | _asm_glue.configure(a_to_use, b, nullptr, _fuse_output_stage ? &_mm_result_s32 : output, gemm_info); |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 132 | } |
| 133 | _assembly_path = _asm_glue.is_configured(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | default: |
| 137 | { |
| 138 | ARM_COMPUTE_ERROR("Datatype not supported"); |
| 139 | break; |
| 140 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 141 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 142 | #endif /* __aarch64__ */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 143 | if(!(_assembly_path || _run_vector_matrix_multiplication)) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 144 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 145 | matrix_a = &_tmp_a; |
| 146 | matrix_b = &_tmp_b; |
| 147 | |
| 148 | // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ] |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 149 | TensorInfo a_info(compute_interleaved_shape(*a_to_use->info()), 1, a_to_use->info()->data_type(), a_to_use->info()->quantization_info()); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 150 | // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ] |
Georgios Pinitas | 02acf01 | 2019-03-19 10:49:03 +0000 | [diff] [blame] | 151 | TensorInfo b_info(compute_transpose1xW_shape(*b->info()), 1, b->info()->data_type(), b->info()->quantization_info()); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 152 | _tmp_a.allocator()->init(a_info); |
| 153 | _tmp_b.allocator()->init(b_info); |
| 154 | _memory_group.manage(&_tmp_a); |
| 155 | if(!_reshape_b_only_on_first_run) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 156 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 157 | _memory_group.manage(&_tmp_b); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 158 | } |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 159 | |
| 160 | // Configure interleave kernel |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 161 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 162 | auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>(); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 163 | k->configure(a_to_use, &_tmp_a); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 164 | _mtx_a_reshape_kernel = std::move(k); |
| 165 | } |
Gian Marco | c7f9b89 | 2017-11-30 14:31:13 +0000 | [diff] [blame] | 166 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 167 | // Configure transpose kernel |
| 168 | { |
| 169 | auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>(); |
| 170 | k->configure(b, &_tmp_b); |
| 171 | _mtx_b_reshape_kernel = std::move(k); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 172 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 173 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 174 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 175 | if(!_fused_assembly_path) |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 176 | { |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 177 | // Build reduction info |
| 178 | const GEMMLowpReductionKernelInfo reduction_info(a_to_use->info()->dimension(0), false, 0, false); |
| 179 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 180 | // Initialize matrix B reduction kernel only if _a_offset is not equal to 0 |
| 181 | if(_a_offset != 0) |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 182 | { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 183 | TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32); |
| 184 | |
| 185 | _vector_sum_col.allocator()->init(info_vector_sum_col); |
| 186 | if(!_reshape_b_only_on_first_run) |
| 187 | { |
| 188 | _memory_group.manage(&_vector_sum_col); |
| 189 | } |
| 190 | |
| 191 | // Configure Matrix B reduction kernel |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 192 | _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, reduction_info); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 193 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 194 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 195 | // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0 |
| 196 | if(_b_offset != 0) |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 197 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 198 | TensorInfo info_vector_sum_row(compute_reductionB_shape(*a_to_use->info()), 1, DataType::S32); |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 199 | |
| 200 | _vector_sum_row.allocator()->init(info_vector_sum_row); |
| 201 | _memory_group.manage(&_vector_sum_row); |
| 202 | |
| 203 | // Configure matrix A reduction kernel |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 204 | _mtx_a_reduction_kernel.configure(a_to_use, &_vector_sum_row, reduction_info); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 207 | if(_fuse_output_stage) |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 208 | { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 209 | // Configure matrix multiply kernel |
| 210 | if(!_assembly_path) |
| 211 | { |
| 212 | auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>(); |
| 213 | k->configure(matrix_a, matrix_b, &_mm_result_s32); |
| 214 | _mm_kernel = std::move(k); |
| 215 | } |
| 216 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 217 | _offset_contribution_output_stage_kernel.configure(&_mm_result_s32, |
| 218 | _a_offset == 0 ? nullptr : &_vector_sum_col, |
| 219 | _b_offset == 0 ? nullptr : &_vector_sum_row, c, |
| 220 | _flip_signedness ? &_signed_output : output, |
| 221 | a->info()->dimension(0), |
| 222 | _a_offset, _b_offset, info.gemmlowp_output_stage()); |
| 223 | |
| 224 | if(_flip_signedness) |
| 225 | { |
| 226 | _convert_from_signed_asymm.configure(&_signed_output, output); |
| 227 | } |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 228 | } |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 229 | else |
| 230 | { |
| 231 | // Configure matrix multiply kernel |
| 232 | if(!_assembly_path) |
| 233 | { |
| 234 | auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>(); |
| 235 | k->configure(matrix_a, matrix_b, output); |
| 236 | _mm_kernel = std::move(k); |
| 237 | } |
| 238 | // Configure offset contribution kernel |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 239 | _offset_contribution_kernel.configure(output, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, a_to_use->info()->dimension(0), _a_offset, _b_offset); |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 240 | } |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 241 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 242 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 243 | // Configure activation |
| 244 | const ActivationLayerInfo &activation = gemm_info.activation_info(); |
| 245 | _run_activation = activation.enabled() && (!_assembly_path || (_assembly_path && !NEGEMMAssemblyDispatch::is_activation_supported(activation))); |
| 246 | if(_run_activation) |
| 247 | { |
| 248 | _activation_func.configure(output, nullptr, activation); |
| 249 | } |
| 250 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 251 | // Allocate tensors |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 252 | if(!_assembly_path && !_run_vector_matrix_multiplication) |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 253 | { |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 254 | _tmp_a.allocator()->allocate(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 255 | if(!_reshape_b_only_on_first_run) |
| 256 | { |
| 257 | _tmp_b.allocator()->allocate(); |
| 258 | } |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 259 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 260 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 261 | if(!_fused_assembly_path) |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 262 | { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 263 | if(_a_offset != 0 && !_reshape_b_only_on_first_run) |
| 264 | { |
| 265 | _vector_sum_col.allocator()->allocate(); |
| 266 | } |
| 267 | |
| 268 | if(_b_offset != 0) |
| 269 | { |
| 270 | _vector_sum_row.allocator()->allocate(); |
| 271 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 274 | if(_fuse_output_stage) |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 275 | { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 276 | _mm_result_s32.allocator()->allocate(); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 277 | } |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 278 | |
| 279 | if(_flip_signedness) |
| 280 | { |
| 281 | _signed_a.allocator()->allocate(); |
| 282 | _signed_output.allocator()->allocate(); |
| 283 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 286 | Status NEGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 287 | { |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 288 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED); |
| 289 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(b, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL); |
| 290 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32, DataType::QASYMM8, DataType::QASYMM8_SIGNED); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 291 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(c != nullptr && gemm_info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::NONE, "Bias addition not supported in NEGEMMLowpMatrixMultiplyCore for output S32"); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 292 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1), |
| 293 | "The product AB is defined only if the number of columns in A is equal to the number of rows in B"); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 294 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported"); |
| 295 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported"); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 296 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 297 | GEMMInfo info = gemm_info; |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 298 | const ITensorInfo *matrix_a_info = a; |
| 299 | const ITensorInfo *matrix_b_info = b; |
| 300 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 301 | const ITensorInfo *a_to_use = a; |
| 302 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 303 | TensorInfo tmp_a_info{}; |
| 304 | TensorInfo tmp_b_info{}; |
| 305 | TensorInfo mm_result_s32_info{}; |
| 306 | |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 307 | int32_t a_offset = a->quantization_info().uniform().offset; |
| 308 | int32_t b_offset = b->quantization_info().uniform().offset; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 309 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 310 | bool fuse_output_stage = info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE; |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 311 | if(fuse_output_stage) |
| 312 | { |
| 313 | auto_init_if_empty(mm_result_s32_info, a->clone()->set_tensor_shape(output->tensor_shape()).set_data_type(DataType::S32)); |
| 314 | } |
| 315 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 316 | // Convert QASYMM8->QASYMM8_SIGNED |
| 317 | TensorInfo signed_a{}; |
| 318 | TensorInfo signed_output{}; |
| 319 | bool flip_signedness = is_data_type_quantized_per_channel(b->data_type()) && (a->data_type() == DataType::QASYMM8) && info.reshape_b_only_on_first_run(); |
| 320 | if(flip_signedness) |
| 321 | { |
| 322 | const int32_t offset_correction = 128; |
| 323 | const DataType dt = DataType::QASYMM8_SIGNED; |
| 324 | const UniformQuantizationInfo iqinfo = a_to_use->quantization_info().uniform(); |
| 325 | |
| 326 | signed_a = a_to_use->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(iqinfo.scale, iqinfo.offset + offset_correction)); |
| 327 | ARM_COMPUTE_RETURN_ON_ERROR(NEConvertQuantizedSignednessKernel::validate(a_to_use, &signed_a)); |
| 328 | a_to_use = &signed_a; |
| 329 | a_offset = signed_a.quantization_info().uniform().offset; |
| 330 | |
| 331 | const UniformQuantizationInfo oqinfo = output->quantization_info().uniform(); |
| 332 | signed_output = output->clone()->set_data_type(dt).set_quantization_info(QuantizationInfo(oqinfo.scale, oqinfo.offset - offset_correction)); |
| 333 | |
| 334 | // Output stage correction |
| 335 | GEMMLowpOutputStageInfo output_stage_corr = info.gemmlowp_output_stage(); |
| 336 | output_stage_corr.gemmlowp_offset = signed_output.quantization_info().uniform().offset; |
| 337 | output_stage_corr.gemmlowp_min_bound -= offset_correction; |
| 338 | output_stage_corr.gemmlowp_max_bound -= offset_correction; |
| 339 | info.set_gemmlowp_output_stage(output_stage_corr); |
Georgios Pinitas | 63d4dbd | 2019-11-08 11:51:56 +0000 | [diff] [blame] | 340 | |
| 341 | // Update matrix a |
| 342 | matrix_a_info = &signed_a; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 345 | // Check if we need to run the optimized assembly kernel |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 346 | bool run_optimised = false; |
| 347 | bool run_optimised_requantized = false; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 348 | if(a_to_use->data_type() == DataType::QASYMM8 && info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT) |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 349 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 350 | run_optimised = bool(NEGEMMAssemblyDispatch::validate(a_to_use, b, c, output, gemm_info)); |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 351 | run_optimised_requantized = run_optimised; |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 352 | |
| 353 | const UniformQuantizationInfo a_qinfo = a_to_use->quantization_info().uniform(); |
| 354 | const QuantizationInfo b_qinfo = b->quantization_info(); |
| 355 | const UniformQuantizationInfo output_qinfo = output->quantization_info().uniform(); |
| 356 | for(auto const s : b_qinfo.scale()) |
| 357 | { |
| 358 | const float fmultipler = a_qinfo.scale * s / output_qinfo.scale; |
| 359 | if(fmultipler > 1.f) |
| 360 | { |
| 361 | run_optimised_requantized = false; |
| 362 | break; |
| 363 | } |
| 364 | } |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 365 | } |
| 366 | else |
| 367 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 368 | run_optimised = bool(NEGEMMAssemblyDispatch::validate(a_to_use, b, nullptr, fuse_output_stage ? &mm_result_s32_info : output, gemm_info)); |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 369 | } |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 370 | |
| 371 | if(run_optimised) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 372 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 373 | ARM_COMPUTE_RETURN_ERROR_ON(b->dimension(0) != output->dimension(0)); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 374 | if(info.depth_output_gemm3d() != 0) |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 375 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 376 | if(info.reinterpret_input_as_3d()) |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 377 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 378 | ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1)); |
| 379 | ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(2) != output->dimension(2)); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 380 | } |
| 381 | else |
| 382 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 383 | ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1) * output->dimension(2)); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 386 | else |
| 387 | { |
| 388 | ARM_COMPUTE_RETURN_ERROR_ON(a->dimension(1) != output->dimension(1)); |
| 389 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 390 | } |
| 391 | else |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 392 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 393 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.reinterpret_input_as_3d(), "NEGEMM cannot reinterpret the input tensor as 3D"); |
| 394 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.depth_output_gemm3d() != 0, "NEGEMM cannot reinterpret the output tensor as 3D"); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 395 | |
| 396 | const bool run_vector_matrix_multiplication = a->dimension(1) < 2; |
| 397 | if(!run_vector_matrix_multiplication) |
| 398 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 399 | matrix_a_info = &tmp_a_info; |
| 400 | matrix_b_info = &tmp_b_info; |
| 401 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 402 | // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ] |
| 403 | TensorShape shape_tmp_a = a->tensor_shape(); |
| 404 | shape_tmp_a.set(0, a->dimension(0) * 4); |
| 405 | shape_tmp_a.set(1, std::ceil(a->dimension(1) / 4.f)); |
| 406 | |
| 407 | // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ] |
| 408 | TensorShape shape_tmp_b = b->tensor_shape(); |
| 409 | shape_tmp_b.set(0, b->dimension(1) * 16); |
| 410 | shape_tmp_b.set(1, std::ceil(b->dimension(0) / 16.f)); |
| 411 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 412 | // Validate interleave kernel |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 413 | auto_init_if_empty(tmp_a_info, a_to_use->clone()->set_tensor_shape(shape_tmp_a)); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 414 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(shape_tmp_b)); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 415 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 416 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(a_to_use, &tmp_a_info)); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 417 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(b, &tmp_b_info)); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 418 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 421 | if(!run_optimised_requantized) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 422 | { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 423 | TensorInfo info_vector_sum_col{}; |
| 424 | TensorInfo info_vector_sum_row{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 425 | |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 426 | const GEMMLowpReductionKernelInfo reduction_info(a_to_use->dimension(0), false, 0, false); |
| 427 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 428 | // Validate matrix B reduction kernel only if _a_offset is not equal to 0 |
| 429 | if(a_offset != 0) |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 430 | { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 431 | info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32); |
| 432 | |
| 433 | // Configure Matrix B reduction kernel |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 434 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col, reduction_info)); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 435 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 436 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 437 | // Validate Matrix A reduction kernel only if _b_offset is not equal to 0 |
| 438 | if(b_offset != 0) |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 439 | { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 440 | info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32); |
| 441 | |
| 442 | // Configure matrix A reduction kernel |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 443 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(a_to_use, &info_vector_sum_row, reduction_info)); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 444 | } |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 445 | |
| 446 | if(fuse_output_stage) |
| 447 | { |
| 448 | if(!run_optimised) |
| 449 | { |
| 450 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info)); |
| 451 | } |
| 452 | |
| 453 | // Validate offset contribution kernel |
| 454 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info, |
| 455 | a_offset == 0 ? nullptr : &info_vector_sum_col, |
| 456 | b_offset == 0 ? nullptr : &info_vector_sum_row, |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 457 | c, |
| 458 | flip_signedness ? &signed_output : output, |
| 459 | a_offset, b_offset, |
| 460 | info.gemmlowp_output_stage())); |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 461 | } |
| 462 | else |
| 463 | { |
| 464 | if(!run_optimised) |
| 465 | { |
| 466 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output)); |
| 467 | } |
| 468 | // Validate offset contribution kernel |
| 469 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOffsetContributionKernel::validate(output, |
| 470 | a_offset == 0 ? nullptr : &info_vector_sum_col, |
| 471 | b_offset == 0 ? nullptr : &info_vector_sum_row, |
| 472 | a_offset, b_offset)); |
| 473 | } |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 474 | } |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 475 | |
| 476 | // Validate activation |
| 477 | const ActivationLayerInfo &activation = gemm_info.activation_info(); |
| 478 | if(activation.enabled()) |
| 479 | { |
| 480 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, activation)); |
| 481 | } |
| 482 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 483 | return Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 486 | void NEGEMMLowpMatrixMultiplyCore::run() |
| 487 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 488 | prepare(); |
| 489 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 490 | MemoryGroupResourceScope scope_mg(_memory_group); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 491 | |
Georgios Pinitas | 63d4dbd | 2019-11-08 11:51:56 +0000 | [diff] [blame] | 492 | // Convert QASYMM8->QASYMM8_SIGNED |
| 493 | if(_flip_signedness) |
| 494 | { |
| 495 | NEScheduler::get().schedule(&_convert_to_signed_asymm, Window::DimY); |
| 496 | } |
| 497 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 498 | // Reshape inputs |
| 499 | if(_mtx_a_reshape_kernel) |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 500 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 501 | NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY); |
| 502 | } |
| 503 | if(_mtx_b_reshape_kernel && !_reshape_b_only_on_first_run) |
| 504 | { |
| 505 | NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY); |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 506 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 507 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 508 | // Run GEMM |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 509 | if(_asm_glue.is_configured()) |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 510 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 511 | _asm_glue.run(); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 512 | } |
| 513 | else |
| 514 | { |
| 515 | NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY); |
| 516 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 517 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 518 | if(!_fused_assembly_path) |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 519 | { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 520 | // Run matrix A reduction kernel only if _b_offset is not equal to 0 |
| 521 | if(_b_offset != 0) |
| 522 | { |
| 523 | NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX); |
| 524 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 525 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 526 | // Run matrix B reduction kernel only if _a_offset is not equal to 0 |
| 527 | if(_a_offset != 0 && !_reshape_b_only_on_first_run) |
| 528 | { |
| 529 | NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX); |
| 530 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 531 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 532 | if(_fuse_output_stage) |
| 533 | { |
| 534 | // Run offset contribution kernel |
| 535 | NEScheduler::get().schedule(&_offset_contribution_output_stage_kernel, Window::DimY); |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | // Run offset contribution kernel |
| 540 | NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY); |
| 541 | } |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 542 | } |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 543 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 544 | // Convert QASYMM8_SIGNED->QASYMM8 |
| 545 | if(_flip_signedness) |
| 546 | { |
| 547 | NEScheduler::get().schedule(&_convert_from_signed_asymm, Window::DimY); |
| 548 | } |
| 549 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 550 | // Run fused activation |
| 551 | if(_run_activation) |
| 552 | { |
| 553 | _activation_func.run(); |
| 554 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 555 | } |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 556 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 557 | void NEGEMMLowpMatrixMultiplyCore::prepare() |
| 558 | { |
| 559 | if(!_is_prepared) |
| 560 | { |
| 561 | // Run assembly reshape |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 562 | if(_asm_glue.is_configured() && _reshape_b_only_on_first_run) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 563 | { |
| 564 | ARM_COMPUTE_ERROR_ON(!_original_b->is_used()); |
| 565 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 566 | _asm_glue.prepare(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 567 | _original_b->mark_as_unused(); |
| 568 | } |
| 569 | // Run non-assembly reshape |
| 570 | else if(_mtx_b_reshape_kernel && _reshape_b_only_on_first_run) |
| 571 | { |
| 572 | ARM_COMPUTE_ERROR_ON(!_original_b->is_used()); |
| 573 | |
| 574 | // Run reshape kernel and mark original weights tensor as unused |
| 575 | _tmp_b.allocator()->allocate(); |
| 576 | NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY); |
| 577 | _original_b->mark_as_unused(); |
| 578 | } |
| 579 | |
| 580 | // Run matrix B reduction kernel only if _a_offset is not equal to 0 |
| 581 | if(_a_offset != 0 && _reshape_b_only_on_first_run) |
| 582 | { |
| 583 | _vector_sum_col.allocator()->allocate(); |
| 584 | NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX); |
| 585 | } |
| 586 | |
| 587 | _is_prepared = true; |
| 588 | } |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 589 | } |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 590 | } // namespace arm_compute |