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