Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1 | /* |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [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/CL/functions/CLGEMMLowpMatrixMultiplyCore.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/gemm/native/CLGEMMNativeKernelConfiguration.h" |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/gemm/reshaped_only_rhs/CLGEMMReshapedOnlyRHSKernelConfiguration.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Error.h" |
| 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Validate.h" |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 34 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 35 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 36 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 37 | |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 40 | using namespace arm_compute::misc::shape_calculator; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 41 | using namespace arm_compute::cl_gemm; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 42 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 43 | namespace |
| 44 | { |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 45 | inline bool is_gemm_reshaped(bool reshape_b_only_on_first_run, GPUTarget gpu_target) |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 46 | { |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 47 | return (get_arch_from_target(gpu_target) != GPUTarget::MIDGARD) && (reshape_b_only_on_first_run); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 48 | } |
| 49 | } // namespace |
| 50 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 51 | CLGEMMLowpMatrixMultiplyCore::CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 52 | : _memory_group(std::move(memory_manager)), |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 53 | _weights_to_qasymm8(), |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 54 | _mm_midgard_kernel(), |
| 55 | _mm_native_kernel(), |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 56 | _mm_reshaped_only_rhs_kernel(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 57 | _mtx_b_reshape_kernel(), |
| 58 | _mtx_a_reduction_kernel(), |
| 59 | _mtx_b_reduction_kernel(), |
| 60 | _offset_contribution_kernel(), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 61 | _offset_contribution_output_stage_kernel(), |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 62 | _qasymm8_weights(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 63 | _vector_sum_col(), |
| 64 | _vector_sum_row(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 65 | _tmp_b(), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 66 | _mm_result_s32(), |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 67 | _gemm_output_stage_multipliers(), |
| 68 | _gemm_output_stage_shifts(), |
| 69 | _matrix_a(nullptr), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 70 | _original_b(nullptr), |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 71 | _output(nullptr), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 72 | _a_offset(0), |
| 73 | _b_offset(0), |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 74 | _is_gemm_reshaped(true), |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 75 | _is_midgard(false), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 76 | _reshape_b_only_on_first_run(false), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 77 | _is_prepared(false), |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 78 | _run_output_stage(false), |
| 79 | _convert_to_qasymm8(false), |
| 80 | _run_offset_contribution(false) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 81 | { |
| 82 | } |
| 83 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 84 | void CLGEMMLowpMatrixMultiplyCore::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 85 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 86 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 87 | ARM_COMPUTE_ERROR_THROW_ON(CLGEMMLowpMatrixMultiplyCore::validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), gemm_info)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 88 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 89 | _is_prepared = false; |
| 90 | _original_b = b; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 91 | _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 92 | _a_offset = a->info()->quantization_info().uniform().offset; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 93 | _matrix_a = a; |
| 94 | _output = output; |
| 95 | |
| 96 | _convert_to_qasymm8 = is_data_type_quantized_per_channel(b->info()->data_type()) && is_data_type_quantized_symmetric(b->info()->data_type()) |
| 97 | && is_data_type_quantized_asymmetric(a->info()->data_type()); |
| 98 | _b_offset = _convert_to_qasymm8 ? -128 : b->info()->quantization_info().uniform().offset; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 99 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 100 | // Get the GPU target |
| 101 | const GPUTarget gpu_target = CLScheduler::get().target(); |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 102 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 103 | // Set the target for the kernels |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 104 | _mm_midgard_kernel.set_target(gpu_target); |
| 105 | _mm_native_kernel.set_target(gpu_target); |
| 106 | _mm_reshaped_only_rhs_kernel.set_target(gpu_target); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 107 | |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 108 | GEMMRHSMatrixInfo rhs_info; |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 109 | GEMMLHSMatrixInfo lhs_info; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 110 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 111 | // Arguments used by GEMMReshapeInfo |
| 112 | // If we pass the matrix A and matrix B reshaped to CLGEMMMatrixMultiplyKernel, we need to pass m, n, k, mult_transpose1xW_width and mult_interleave4x4_height to CLGEMMReshapeInfo |
| 113 | // in order to know how the matrices have been reshaped |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 114 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 115 | const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1); |
| 116 | const unsigned int n = b->info()->dimension(0); |
| 117 | const unsigned int k = a->info()->dimension(0); |
| 118 | const unsigned int batch_size = reinterpret_input_as_3d ? a->info()->dimension(3) : a->info()->dimension(2); |
| 119 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 120 | |
| 121 | // Check if we need to reshape the matrix A and matrix B |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 122 | _is_gemm_reshaped = is_gemm_reshaped(_reshape_b_only_on_first_run, gpu_target); |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 123 | _is_midgard = gpu_target == GPUTarget::MIDGARD; |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 124 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 125 | if(_convert_to_qasymm8) |
| 126 | { |
| 127 | // Set data type for converted weights |
| 128 | TensorInfo weights_info(*b->info()); |
| 129 | weights_info.set_data_type(DataType::QASYMM8); |
| 130 | _qasymm8_weights.allocator()->init(weights_info); |
| 131 | _weights_to_qasymm8.configure(b, &_qasymm8_weights, ConvertPolicy::WRAP, 0); |
| 132 | } |
| 133 | |
| 134 | const ICLTensor *matrix_b = _convert_to_qasymm8 ? &_qasymm8_weights : b; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 135 | if(_is_gemm_reshaped) |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 136 | { |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 137 | matrix_b = &_tmp_b; |
| 138 | |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 139 | if(!_reshape_b_only_on_first_run) |
| 140 | { |
| 141 | _memory_group.manage(&_tmp_b); |
| 142 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 143 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 144 | // Pick up the GEMM configuration |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 145 | // Datatype is DataType::QASYMM8 or DataType::QASYMM8_SIGNED doesn't matter, since it only affect the shape configuration |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 146 | std::tie(lhs_info, rhs_info) = CLGEMMReshapedOnlyRHSKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 147 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 148 | // Configure reshape RHS kernel |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 149 | _mtx_b_reshape_kernel.configure(_convert_to_qasymm8 ? &_qasymm8_weights : b, &_tmp_b, rhs_info); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 150 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 151 | |
| 152 | // Initialize matrix B reduction kernel only if _a_offset is not equal to 0 |
| 153 | if(_a_offset != 0) |
| 154 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 155 | TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 156 | _vector_sum_col.allocator()->init(info_vector_sum_col); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 157 | if(!_reshape_b_only_on_first_run) |
| 158 | { |
| 159 | _memory_group.manage(&_vector_sum_col); |
| 160 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 161 | |
| 162 | // Configure Matrix B reduction kernel |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 163 | _mtx_b_reduction_kernel.configure(_convert_to_qasymm8 ? &_qasymm8_weights : b, &_vector_sum_col); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0 |
| 167 | if(_b_offset != 0) |
| 168 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 169 | TensorInfo info_vector_sum_row(compute_reductionB_shape(*a->info()), 1, DataType::S32); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 170 | _vector_sum_row.allocator()->init(info_vector_sum_row); |
| 171 | _memory_group.manage(&_vector_sum_row); |
| 172 | |
| 173 | // Configure matrix A reduction kernel |
| 174 | _mtx_a_reduction_kernel.configure(a, &_vector_sum_row); |
| 175 | } |
| 176 | |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 177 | GEMMKernelInfo gemm_kernel_info; |
| 178 | gemm_kernel_info.m = m; |
| 179 | gemm_kernel_info.n = n; |
| 180 | gemm_kernel_info.k = k; |
| 181 | gemm_kernel_info.depth_output_gemm3d = depth_output_gemm3d; |
| 182 | gemm_kernel_info.reinterpret_input_as_3d = reinterpret_input_as_3d; |
| 183 | gemm_kernel_info.lhs_info = lhs_info; |
| 184 | gemm_kernel_info.rhs_info = rhs_info; |
| 185 | gemm_kernel_info.a_offset = _a_offset; |
| 186 | gemm_kernel_info.b_offset = _b_offset; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 187 | // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage |
| 188 | if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE) |
| 189 | { |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 190 | // Configure offset contribution kernel |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 191 | const size_t num_filters = (gemm_info.gemmlowp_output_stage().is_quantized_per_channel) ? gemm_info.gemmlowp_output_stage().gemmlowp_multipliers.size() : 1; |
| 192 | |
| 193 | _gemm_output_stage_multipliers.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32)); |
| 194 | _gemm_output_stage_shifts.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32)); |
| 195 | |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 196 | GEMMLowpOutputStageInfo gemmlowp_output_stage = gemm_info.gemmlowp_output_stage(); |
| 197 | gemmlowp_output_stage.output_data_type = _matrix_a->info()->data_type(); |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 198 | |
| 199 | gemm_kernel_info.output_stage = gemmlowp_output_stage; |
| 200 | |
| 201 | if(_is_gemm_reshaped && gemmlowp_output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT) |
| 202 | { |
| 203 | // Configure and tune matrix multiply kernel with fused output stage |
| 204 | _mm_reshaped_only_rhs_kernel.configure(_matrix_a, matrix_b, output, gemm_kernel_info, _a_offset == 0 ? nullptr : &_vector_sum_col, |
| 205 | _b_offset == 0 ? nullptr : &_vector_sum_row, c, &_gemm_output_stage_multipliers, &_gemm_output_stage_shifts); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | _run_output_stage = true; |
| 210 | |
| 211 | _memory_group.manage(&_mm_result_s32); |
| 212 | |
| 213 | if(_is_gemm_reshaped) |
| 214 | { |
| 215 | _mm_reshaped_only_rhs_kernel.configure(_matrix_a, matrix_b, &_mm_result_s32, gemm_kernel_info); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | if(_is_midgard) |
| 220 | { |
| 221 | // Configure matrix multiply kernel |
| 222 | _mm_midgard_kernel.configure(_matrix_a, matrix_b, &_mm_result_s32, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d)); |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | // Pick up the GEMM configuration |
| 227 | std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8); |
| 228 | |
| 229 | // Configure matrix multiply kernel |
| 230 | _mm_native_kernel.configure(_matrix_a, matrix_b, &_mm_result_s32, lhs_info, rhs_info, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d)); |
| 231 | } |
| 232 | _offset_contribution_output_stage_kernel.configure(&_mm_result_s32, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, c, output, a->info()->dimension(0), |
| 233 | _a_offset, _b_offset, gemmlowp_output_stage, &_gemm_output_stage_multipliers, &_gemm_output_stage_shifts); |
| 234 | |
| 235 | _mm_result_s32.allocator()->allocate(); |
| 236 | } |
| 237 | } |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 238 | |
| 239 | _gemm_output_stage_multipliers.allocator()->allocate(); |
| 240 | _gemm_output_stage_shifts.allocator()->allocate(); |
| 241 | // Compute GEMM output multipliers and shifts for output stage |
| 242 | _gemm_output_stage_multipliers.map(); |
| 243 | _gemm_output_stage_shifts.map(); |
| 244 | std::memcpy(_gemm_output_stage_multipliers.ptr_to_element(Coordinates(0)), gemm_info.gemmlowp_output_stage().gemmlowp_multipliers.data(), num_filters * sizeof(int32_t)); |
| 245 | std::memcpy(_gemm_output_stage_shifts.ptr_to_element(Coordinates(0)), gemm_info.gemmlowp_output_stage().gemmlowp_shifts.data(), num_filters * sizeof(int32_t)); |
| 246 | _gemm_output_stage_multipliers.unmap(); |
| 247 | _gemm_output_stage_shifts.unmap(); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 248 | } |
| 249 | else |
| 250 | { |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 251 | _run_offset_contribution = true; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 252 | if(_is_gemm_reshaped) |
| 253 | { |
| 254 | // Configure and tune matrix multiply kernel |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 255 | _mm_reshaped_only_rhs_kernel.configure(_matrix_a, matrix_b, output, gemm_kernel_info); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 256 | } |
| 257 | else |
| 258 | { |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 259 | if(_is_midgard) |
| 260 | { |
| 261 | // Configure matrix multiply kernel |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 262 | _mm_midgard_kernel.configure(_matrix_a, matrix_b, output, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d)); |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 263 | } |
| 264 | else |
| 265 | { |
| 266 | // Pick up the GEMM configuration |
| 267 | std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8); |
| 268 | |
| 269 | // Configure matrix multiply kernel |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 270 | _mm_native_kernel.configure(_matrix_a, matrix_b, output, lhs_info, rhs_info, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d)); |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 271 | } |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 272 | } |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 273 | |
| 274 | // Configure offset contribution kernel |
| 275 | _offset_contribution_kernel.configure(output, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, c, a->info()->dimension(0), _a_offset, _b_offset); |
| 276 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 277 | |
| 278 | // Allocate tensors |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 279 | if(_is_gemm_reshaped) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 280 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 281 | if(!_reshape_b_only_on_first_run) |
| 282 | { |
| 283 | _tmp_b.allocator()->allocate(); |
| 284 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 287 | if(_a_offset != 0 && !_reshape_b_only_on_first_run) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 288 | { |
| 289 | _vector_sum_col.allocator()->allocate(); |
| 290 | } |
| 291 | |
| 292 | if(_b_offset != 0) |
| 293 | { |
| 294 | _vector_sum_row.allocator()->allocate(); |
| 295 | } |
| 296 | } |
| 297 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 298 | Status CLGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 299 | { |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 300 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
| 301 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED); |
| 302 | //DataType::QSYMM8_PER_CHANNEL supported only for weights |
| 303 | if(b->data_type() != DataType::QSYMM8_PER_CHANNEL) |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 304 | { |
| 305 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b); |
| 306 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 307 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported"); |
| 308 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported"); |
| 309 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 310 | int32_t a_offset = a->quantization_info().uniform().offset; |
| 311 | int32_t b_offset = b->quantization_info().uniform().offset; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 312 | |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 313 | const ITensorInfo *matrix_a_info = a; |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 314 | |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 315 | TensorInfo tmp_b_info{}; |
| 316 | GEMMRHSMatrixInfo rhs_info; |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 317 | GEMMLHSMatrixInfo lhs_info; |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 318 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 319 | // Get the GPU target |
| 320 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 321 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 322 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 323 | const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1); |
| 324 | const unsigned int n = b->dimension(0); |
| 325 | const unsigned int k = a->dimension(0); |
| 326 | const unsigned int batch_size = reinterpret_input_as_3d ? a->dimension(3) : a->dimension(2); |
| 327 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 328 | const bool is_midgard = gpu_target == GPUTarget::MIDGARD; |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 329 | |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 330 | bool reshape_matrix_b = is_gemm_reshaped(gemm_info.reshape_b_only_on_first_run(), CLScheduler::get().target()); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 331 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 332 | const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 333 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 334 | bool convert_to_qasymm8 = is_data_type_quantized_per_channel(b->data_type()) && is_data_type_quantized_symmetric(b->data_type()) |
| 335 | && is_data_type_quantized_asymmetric(a->data_type()); |
| 336 | TensorInfo weights_info(*b); |
| 337 | if(convert_to_qasymm8) |
| 338 | { |
| 339 | b_offset = -128; |
| 340 | weights_info.set_data_type(DataType::QASYMM8); |
| 341 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthConvertLayerKernel::validate(b, &weights_info, ConvertPolicy::WRAP, 0)); |
| 342 | } |
| 343 | const ITensorInfo *matrix_b_info = &weights_info; |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 344 | if(reshape_matrix_b) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 345 | { |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 346 | matrix_b_info = &tmp_b_info; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 347 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 348 | // Pick up the GEMM configuration |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 349 | std::tie(lhs_info, rhs_info) = CLGEMMReshapedOnlyRHSKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8); |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 350 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 351 | // Validate reshape RHS kernel |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 352 | auto_init_if_empty(tmp_b_info, weights_info.clone()->set_tensor_shape(compute_rhs_reshaped_shape(weights_info, rhs_info))); |
| 353 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(&weights_info, &tmp_b_info, rhs_info)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 354 | } |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 355 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 356 | TensorInfo info_vector_sum_col{}; |
| 357 | TensorInfo info_vector_sum_row{}; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 358 | |
| 359 | // Validate matrix B reduction kernel only if _a_offset is not equal to 0 |
| 360 | if(a_offset != 0) |
| 361 | { |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 362 | info_vector_sum_col = TensorInfo(compute_reductionA_shape(weights_info), 1, DataType::S32); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 363 | |
| 364 | // Configure Matrix B reduction kernel |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 365 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixBReductionKernel::validate(&weights_info, &info_vector_sum_col)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // Validate Matrix A reduction kernel only if _b_offset is not equal to 0 |
| 369 | if(b_offset != 0) |
| 370 | { |
| 371 | info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32); |
| 372 | |
| 373 | // Configure matrix A reduction kernel |
| 374 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row)); |
| 375 | } |
| 376 | |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 377 | GEMMKernelInfo gemm_kernel_info; |
| 378 | gemm_kernel_info.m = m; |
| 379 | gemm_kernel_info.n = n; |
| 380 | gemm_kernel_info.k = k; |
| 381 | gemm_kernel_info.depth_output_gemm3d = depth_output_gemm3d; |
| 382 | gemm_kernel_info.reinterpret_input_as_3d = reinterpret_input_as_3d; |
| 383 | gemm_kernel_info.lhs_info = lhs_info; |
| 384 | gemm_kernel_info.rhs_info = rhs_info; |
| 385 | gemm_kernel_info.a_offset = a_offset; |
| 386 | gemm_kernel_info.b_offset = b_offset; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 387 | if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE) |
| 388 | { |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 389 | const size_t num_filters = (gemm_info.gemmlowp_output_stage().is_quantized_per_channel) ? gemm_info.gemmlowp_output_stage().gemmlowp_multipliers.size() : 1; |
| 390 | |
| 391 | const TensorInfo gemm_output_stage_multipliers_shifts_info(TensorInfo(TensorShape(num_filters), 1, DataType::S32)); |
| 392 | |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 393 | GEMMLowpOutputStageInfo gemmlowp_output_stage = gemm_info.gemmlowp_output_stage(); |
| 394 | gemmlowp_output_stage.output_data_type = a->data_type(); |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 395 | |
| 396 | gemm_kernel_info.output_stage = gemmlowp_output_stage; |
| 397 | if(reshape_matrix_b && gemm_info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT) |
| 398 | { |
| 399 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::validate(matrix_a_info, matrix_b_info, output, gemm_kernel_info, |
| 400 | a_offset == 0 ? nullptr : &info_vector_sum_col, |
| 401 | b_offset == 0 ? nullptr : &info_vector_sum_row, |
| 402 | c, |
| 403 | &gemm_output_stage_multipliers_shifts_info, |
| 404 | &gemm_output_stage_multipliers_shifts_info)); |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | TensorInfo mm_result_s32_info{}; |
| 409 | |
| 410 | if(reshape_matrix_b) |
| 411 | { |
| 412 | // Output tensor auto inizialitation if not yet initialized |
| 413 | auto_init_if_empty(mm_result_s32_info, a->clone()->set_tensor_shape(compute_mm_shape(*matrix_a_info, *matrix_b_info, reshape_info)).set_data_type(DataType::S32)); |
| 414 | |
| 415 | // Validate matrix multiply |
| 416 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info, gemm_kernel_info)); |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | // Output tensor auto inizialitation if not yet initialized |
| 421 | auto_init_if_empty(mm_result_s32_info, a->clone()->set_tensor_shape(compute_mm_shape(*matrix_a_info, *matrix_b_info, false, reshape_info)).set_data_type(DataType::S32)); |
| 422 | |
| 423 | if(is_midgard) |
| 424 | { |
| 425 | // Validate matrix multiply |
| 426 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info, reshape_info)); |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | // Pick up the GEMM configuration |
| 431 | std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8); |
| 432 | |
| 433 | // Validate matrix multiply |
| 434 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyNativeKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info, lhs_info, rhs_info, reshape_info)); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // Validate offset contribution kernel |
| 439 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info, |
| 440 | a_offset == 0 ? nullptr : &info_vector_sum_col, |
| 441 | b_offset == 0 ? nullptr : &info_vector_sum_row, |
| 442 | c, |
| 443 | output, |
| 444 | a_offset, b_offset, |
| 445 | gemmlowp_output_stage, |
| 446 | &gemm_output_stage_multipliers_shifts_info, |
| 447 | &gemm_output_stage_multipliers_shifts_info)); |
| 448 | } |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 449 | } |
| 450 | else |
| 451 | { |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 452 | if(reshape_matrix_b) |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 453 | { |
| 454 | // Validate matrix multiply |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 455 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::validate(matrix_a_info, matrix_b_info, output, gemm_kernel_info)); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 456 | } |
| 457 | else |
| 458 | { |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 459 | if(is_midgard) |
| 460 | { |
| 461 | // Validate matrix multiply |
| 462 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output, reshape_info)); |
| 463 | } |
| 464 | else |
| 465 | { |
| 466 | // Pick up the GEMM configuration |
| 467 | std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8); |
| 468 | |
| 469 | // Validate matrix multiply |
| 470 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyNativeKernel::validate(matrix_a_info, matrix_b_info, output, lhs_info, rhs_info, reshape_info)); |
| 471 | } |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 472 | } |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 473 | |
giuros01 | 2f7c149 | 2019-03-18 12:30:02 +0000 | [diff] [blame] | 474 | if(output->total_size() != 0) |
| 475 | { |
| 476 | // Validate offset contribution kernel |
| 477 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionKernel::validate(output, |
| 478 | a_offset == 0 ? nullptr : &info_vector_sum_col, |
| 479 | b_offset == 0 ? nullptr : &info_vector_sum_row, |
| 480 | c, |
| 481 | a_offset, b_offset)); |
| 482 | } |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 483 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 484 | |
| 485 | return Status{}; |
| 486 | } |
| 487 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 488 | void CLGEMMLowpMatrixMultiplyCore::run() |
| 489 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 490 | prepare(); |
| 491 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 492 | MemoryGroupResourceScope scope_mg(_memory_group); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 493 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 494 | if(_is_gemm_reshaped) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 495 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 496 | if(!_reshape_b_only_on_first_run) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 497 | { |
| 498 | // Run reshape matrix B |
| 499 | CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false); |
| 500 | } |
| 501 | } |
| 502 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 503 | // Run matrix B reduction kernel only if _a_offset is not equal to 0 |
| 504 | if(_a_offset != 0 && !_reshape_b_only_on_first_run) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 505 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 506 | CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 509 | // Run matrix A reduction kernel only if _b_offset is not equal to 0 |
| 510 | if(_b_offset != 0) |
| 511 | { |
| 512 | CLScheduler::get().enqueue(_mtx_a_reduction_kernel, false); |
| 513 | } |
| 514 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 515 | // Run matrix multiply |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 516 | if(_is_gemm_reshaped) |
| 517 | { |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 518 | CLScheduler::get().enqueue(_mm_reshaped_only_rhs_kernel, false); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 519 | } |
| 520 | else |
| 521 | { |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 522 | if(_is_midgard) |
| 523 | { |
| 524 | CLScheduler::get().enqueue(_mm_midgard_kernel, false); |
| 525 | } |
| 526 | else |
| 527 | { |
| 528 | CLScheduler::get().enqueue(_mm_native_kernel, false); |
| 529 | } |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 530 | } |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 531 | if(_run_output_stage) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 532 | { |
| 533 | // Run offset contribution/output stage kernel |
| 534 | CLScheduler::get().enqueue(_offset_contribution_output_stage_kernel, true); |
| 535 | } |
Michele Di Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 536 | if(_run_offset_contribution) |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 537 | { |
| 538 | // Run offset contribution kernel |
| 539 | CLScheduler::get().enqueue(_offset_contribution_kernel, true); |
| 540 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 541 | } |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 542 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 543 | void CLGEMMLowpMatrixMultiplyCore::prepare() |
| 544 | { |
| 545 | if(!_is_prepared) |
| 546 | { |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 547 | if(_convert_to_qasymm8) |
| 548 | { |
| 549 | _qasymm8_weights.allocator()->allocate(); |
| 550 | CLScheduler::get().enqueue(_weights_to_qasymm8, false); |
| 551 | } |
| 552 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 553 | if(_is_gemm_reshaped && _reshape_b_only_on_first_run) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 554 | { |
| 555 | ARM_COMPUTE_ERROR_ON(!_original_b->is_used()); |
| 556 | |
| 557 | // Run reshape kernel and mark original weights tensor as unused |
| 558 | _tmp_b.allocator()->allocate(); |
| 559 | CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false); |
| 560 | _original_b->mark_as_unused(); |
| 561 | } |
| 562 | |
| 563 | // Run matrix B reduction kernel only if _a_offset is not equal to 0 |
| 564 | if(_a_offset != 0 && _reshape_b_only_on_first_run) |
| 565 | { |
| 566 | _vector_sum_col.allocator()->allocate(); |
| 567 | CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false); |
| 568 | } |
| 569 | |
| 570 | CLScheduler::get().queue().finish(); |
| 571 | _is_prepared = true; |
| 572 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 573 | } |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 574 | } // namespace arm_compute |