Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1 | /* |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 35 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 36 | |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 39 | using namespace arm_compute::misc::shape_calculator; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 40 | using namespace arm_compute::cl_gemm; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 41 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 42 | namespace |
| 43 | { |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 44 | 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] | 45 | { |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 46 | 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] | 47 | } |
| 48 | } // namespace |
| 49 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 50 | CLGEMMLowpMatrixMultiplyCore::CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 51 | : _memory_group(std::move(memory_manager)), |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame^] | 52 | _mm_midgard_kernel(), |
| 53 | _mm_native_kernel(), |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 54 | _mm_reshaped_only_rhs_kernel(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 55 | _mtx_b_reshape_kernel(), |
| 56 | _mtx_a_reduction_kernel(), |
| 57 | _mtx_b_reduction_kernel(), |
| 58 | _offset_contribution_kernel(), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 59 | _offset_contribution_output_stage_kernel(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 60 | _vector_sum_col(), |
| 61 | _vector_sum_row(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 62 | _tmp_b(), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 63 | _mm_result_s32(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 64 | _original_b(nullptr), |
| 65 | _a_offset(0), |
| 66 | _b_offset(0), |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 67 | _is_gemm_reshaped(true), |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame^] | 68 | _is_midgard(false), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 69 | _reshape_b_only_on_first_run(false), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 70 | _is_prepared(false), |
| 71 | _fuse_output_stage(false) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 72 | { |
| 73 | } |
| 74 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 75 | 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] | 76 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 78 | 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] | 79 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 80 | _is_prepared = false; |
| 81 | _original_b = b; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 82 | _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] | 83 | _a_offset = a->info()->quantization_info().uniform().offset; |
| 84 | _b_offset = b->info()->quantization_info().uniform().offset; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 85 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 86 | // Get the GPU target |
| 87 | const GPUTarget gpu_target = CLScheduler::get().target(); |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 88 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 89 | // Set the target for the kernels |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame^] | 90 | _mm_midgard_kernel.set_target(gpu_target); |
| 91 | _mm_native_kernel.set_target(gpu_target); |
| 92 | _mm_reshaped_only_rhs_kernel.set_target(gpu_target); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 93 | |
| 94 | const ICLTensor *matrix_a = a; |
| 95 | const ICLTensor *matrix_b = b; |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 96 | GEMMRHSMatrixInfo rhs_info; |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 97 | GEMMLHSMatrixInfo lhs_info; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 98 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 99 | // Arguments used by GEMMReshapeInfo |
| 100 | // 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 |
| 101 | // in order to know how the matrices have been reshaped |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 102 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 103 | const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1); |
| 104 | const unsigned int n = b->info()->dimension(0); |
| 105 | const unsigned int k = a->info()->dimension(0); |
| 106 | const unsigned int batch_size = reinterpret_input_as_3d ? a->info()->dimension(3) : a->info()->dimension(2); |
| 107 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 108 | |
| 109 | // 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] | 110 | _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^] | 111 | _is_midgard = gpu_target == GPUTarget::MIDGARD; |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 112 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 113 | if(_is_gemm_reshaped) |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 114 | { |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 115 | matrix_b = &_tmp_b; |
| 116 | |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 117 | if(!_reshape_b_only_on_first_run) |
| 118 | { |
| 119 | _memory_group.manage(&_tmp_b); |
| 120 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 121 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 122 | // Pick up the GEMM configuration |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 123 | 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] | 124 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 125 | // Configure reshape RHS kernel |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 126 | _mtx_b_reshape_kernel.configure(b, &_tmp_b, rhs_info); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 127 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 128 | |
| 129 | // Initialize matrix B reduction kernel only if _a_offset is not equal to 0 |
| 130 | if(_a_offset != 0) |
| 131 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 132 | 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] | 133 | _vector_sum_col.allocator()->init(info_vector_sum_col); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 134 | if(!_reshape_b_only_on_first_run) |
| 135 | { |
| 136 | _memory_group.manage(&_vector_sum_col); |
| 137 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 138 | |
| 139 | // Configure Matrix B reduction kernel |
| 140 | _mtx_b_reduction_kernel.configure(b, &_vector_sum_col); |
| 141 | } |
| 142 | |
| 143 | // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0 |
| 144 | if(_b_offset != 0) |
| 145 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 146 | 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] | 147 | _vector_sum_row.allocator()->init(info_vector_sum_row); |
| 148 | _memory_group.manage(&_vector_sum_row); |
| 149 | |
| 150 | // Configure matrix A reduction kernel |
| 151 | _mtx_a_reduction_kernel.configure(a, &_vector_sum_row); |
| 152 | } |
| 153 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 154 | // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage |
| 155 | if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE) |
| 156 | { |
| 157 | _fuse_output_stage = true; |
| 158 | |
| 159 | _memory_group.manage(&_mm_result_s32); |
| 160 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 161 | if(_is_gemm_reshaped) |
| 162 | { |
| 163 | // Configure and tune matrix multiply kernel |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 164 | _mm_reshaped_only_rhs_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)); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 165 | } |
| 166 | else |
| 167 | { |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame^] | 168 | if(_is_midgard) |
| 169 | { |
| 170 | // Configure matrix multiply kernel |
| 171 | _mm_midgard_kernel.configure(matrix_a, matrix_b, &_mm_result_s32, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d)); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | // Pick up the GEMM configuration |
| 176 | std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8); |
| 177 | |
| 178 | // Configure matrix multiply kernel |
| 179 | _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)); |
| 180 | } |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 181 | } |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 182 | |
| 183 | // Configure offset contribution kernel |
| 184 | _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), |
| 185 | _a_offset, _b_offset, gemm_info.gemmlowp_output_stage()); |
| 186 | |
| 187 | _mm_result_s32.allocator()->allocate(); |
| 188 | } |
| 189 | else |
| 190 | { |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 191 | if(_is_gemm_reshaped) |
| 192 | { |
| 193 | // Configure and tune matrix multiply kernel |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 194 | _mm_reshaped_only_rhs_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 | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 195 | } |
| 196 | else |
| 197 | { |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame^] | 198 | if(_is_midgard) |
| 199 | { |
| 200 | // Configure matrix multiply kernel |
| 201 | _mm_midgard_kernel.configure(matrix_a, matrix_b, output, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d)); |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | // Pick up the GEMM configuration |
| 206 | std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8); |
| 207 | |
| 208 | // Configure matrix multiply kernel |
| 209 | _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)); |
| 210 | } |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 211 | } |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 212 | |
| 213 | // Configure offset contribution kernel |
| 214 | _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); |
| 215 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 216 | |
| 217 | // Allocate tensors |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 218 | if(_is_gemm_reshaped) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 219 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 220 | if(!_reshape_b_only_on_first_run) |
| 221 | { |
| 222 | _tmp_b.allocator()->allocate(); |
| 223 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 226 | if(_a_offset != 0 && !_reshape_b_only_on_first_run) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 227 | { |
| 228 | _vector_sum_col.allocator()->allocate(); |
| 229 | } |
| 230 | |
| 231 | if(_b_offset != 0) |
| 232 | { |
| 233 | _vector_sum_row.allocator()->allocate(); |
| 234 | } |
| 235 | } |
| 236 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 237 | 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] | 238 | { |
| 239 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 240 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 241 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported"); |
| 242 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported"); |
| 243 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 244 | int32_t a_offset = a->quantization_info().uniform().offset; |
| 245 | int32_t b_offset = b->quantization_info().uniform().offset; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 246 | |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 247 | const ITensorInfo *matrix_a_info = a; |
| 248 | const ITensorInfo *matrix_b_info = b; |
| 249 | |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 250 | TensorInfo tmp_b_info{}; |
| 251 | GEMMRHSMatrixInfo rhs_info; |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 252 | GEMMLHSMatrixInfo lhs_info; |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 253 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 254 | // Get the GPU target |
| 255 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 256 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 257 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 258 | const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1); |
| 259 | const unsigned int n = b->dimension(0); |
| 260 | const unsigned int k = a->dimension(0); |
| 261 | const unsigned int batch_size = reinterpret_input_as_3d ? a->dimension(3) : a->dimension(2); |
| 262 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame^] | 263 | const bool is_midgard = gpu_target == GPUTarget::MIDGARD; |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 264 | |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 265 | 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] | 266 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 267 | 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] | 268 | |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 269 | if(reshape_matrix_b) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 270 | { |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 271 | matrix_b_info = &tmp_b_info; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 272 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 273 | // Pick up the GEMM configuration |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 274 | 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] | 275 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 276 | // Validate reshape RHS kernel |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 277 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info))); |
| 278 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 279 | } |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 280 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 281 | TensorInfo info_vector_sum_col{}; |
| 282 | TensorInfo info_vector_sum_row{}; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 283 | |
| 284 | // Validate matrix B reduction kernel only if _a_offset is not equal to 0 |
| 285 | if(a_offset != 0) |
| 286 | { |
| 287 | info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32); |
| 288 | |
| 289 | // Configure Matrix B reduction kernel |
| 290 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col)); |
| 291 | } |
| 292 | |
| 293 | // Validate Matrix A reduction kernel only if _b_offset is not equal to 0 |
| 294 | if(b_offset != 0) |
| 295 | { |
| 296 | info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32); |
| 297 | |
| 298 | // Configure matrix A reduction kernel |
| 299 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row)); |
| 300 | } |
| 301 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 302 | if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE) |
| 303 | { |
| 304 | TensorInfo mm_result_s32_info{}; |
| 305 | |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 306 | if(reshape_matrix_b) |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 307 | { |
| 308 | // Output tensor auto inizialitation if not yet initialized |
| 309 | 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)); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 310 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 311 | // Validate matrix multiply |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 312 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info, lhs_info, rhs_info, reshape_info)); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 313 | } |
| 314 | else |
| 315 | { |
| 316 | // Output tensor auto inizialitation if not yet initialized |
| 317 | 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)); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 318 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame^] | 319 | if(is_midgard) |
| 320 | { |
| 321 | // Validate matrix multiply |
| 322 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info, reshape_info)); |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | // Pick up the GEMM configuration |
| 327 | std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8); |
| 328 | |
| 329 | // Validate matrix multiply |
| 330 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyNativeKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info, lhs_info, rhs_info, reshape_info)); |
| 331 | } |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 332 | } |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame^] | 333 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 334 | // Validate offset contribution kernel |
| 335 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info, |
| 336 | a_offset == 0 ? nullptr : &info_vector_sum_col, |
| 337 | b_offset == 0 ? nullptr : &info_vector_sum_row, |
| 338 | c, |
| 339 | output, |
| 340 | a_offset, b_offset, |
| 341 | gemm_info.gemmlowp_output_stage())); |
| 342 | } |
| 343 | else |
| 344 | { |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 345 | if(reshape_matrix_b) |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 346 | { |
| 347 | // Validate matrix multiply |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 348 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::validate(matrix_a_info, matrix_b_info, output, lhs_info, rhs_info, reshape_info)); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 349 | } |
| 350 | else |
| 351 | { |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame^] | 352 | if(is_midgard) |
| 353 | { |
| 354 | // Validate matrix multiply |
| 355 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output, reshape_info)); |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | // Pick up the GEMM configuration |
| 360 | std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8); |
| 361 | |
| 362 | // Validate matrix multiply |
| 363 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyNativeKernel::validate(matrix_a_info, matrix_b_info, output, lhs_info, rhs_info, reshape_info)); |
| 364 | } |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 365 | } |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame^] | 366 | |
giuros01 | 2f7c149 | 2019-03-18 12:30:02 +0000 | [diff] [blame] | 367 | if(output->total_size() != 0) |
| 368 | { |
| 369 | // Validate offset contribution kernel |
| 370 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionKernel::validate(output, |
| 371 | a_offset == 0 ? nullptr : &info_vector_sum_col, |
| 372 | b_offset == 0 ? nullptr : &info_vector_sum_row, |
| 373 | c, |
| 374 | a_offset, b_offset)); |
| 375 | } |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 376 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 377 | |
| 378 | return Status{}; |
| 379 | } |
| 380 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 381 | void CLGEMMLowpMatrixMultiplyCore::run() |
| 382 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 383 | prepare(); |
| 384 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 385 | MemoryGroupResourceScope scope_mg(_memory_group); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 386 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 387 | if(_is_gemm_reshaped) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 388 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 389 | if(!_reshape_b_only_on_first_run) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 390 | { |
| 391 | // Run reshape matrix B |
| 392 | CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false); |
| 393 | } |
| 394 | } |
| 395 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 396 | // Run matrix B reduction kernel only if _a_offset is not equal to 0 |
| 397 | if(_a_offset != 0 && !_reshape_b_only_on_first_run) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 398 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 399 | CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // Run matrix multiply |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 403 | if(_is_gemm_reshaped) |
| 404 | { |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 405 | CLScheduler::get().enqueue(_mm_reshaped_only_rhs_kernel, false); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 406 | } |
| 407 | else |
| 408 | { |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame^] | 409 | if(_is_midgard) |
| 410 | { |
| 411 | CLScheduler::get().enqueue(_mm_midgard_kernel, false); |
| 412 | } |
| 413 | else |
| 414 | { |
| 415 | CLScheduler::get().enqueue(_mm_native_kernel, false); |
| 416 | } |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 417 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 418 | |
| 419 | // Run matrix A reduction kernel only if _b_offset is not equal to 0 |
| 420 | if(_b_offset != 0) |
| 421 | { |
| 422 | CLScheduler::get().enqueue(_mtx_a_reduction_kernel, false); |
| 423 | } |
| 424 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 425 | if(_fuse_output_stage) |
| 426 | { |
| 427 | // Run offset contribution/output stage kernel |
| 428 | CLScheduler::get().enqueue(_offset_contribution_output_stage_kernel, true); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | // Run offset contribution kernel |
| 433 | CLScheduler::get().enqueue(_offset_contribution_kernel, true); |
| 434 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 435 | } |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 436 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 437 | void CLGEMMLowpMatrixMultiplyCore::prepare() |
| 438 | { |
| 439 | if(!_is_prepared) |
| 440 | { |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 441 | if(_is_gemm_reshaped && _reshape_b_only_on_first_run) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 442 | { |
| 443 | ARM_COMPUTE_ERROR_ON(!_original_b->is_used()); |
| 444 | |
| 445 | // Run reshape kernel and mark original weights tensor as unused |
| 446 | _tmp_b.allocator()->allocate(); |
| 447 | CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false); |
| 448 | _original_b->mark_as_unused(); |
| 449 | } |
| 450 | |
| 451 | // Run matrix B reduction kernel only if _a_offset is not equal to 0 |
| 452 | if(_a_offset != 0 && _reshape_b_only_on_first_run) |
| 453 | { |
| 454 | _vector_sum_col.allocator()->allocate(); |
| 455 | CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false); |
| 456 | } |
| 457 | |
| 458 | CLScheduler::get().queue().finish(); |
| 459 | _is_prepared = true; |
| 460 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 461 | } |
Gian Marco Iodice | 2ec6c1e | 2019-04-09 12:03:05 +0100 | [diff] [blame] | 462 | } // namespace arm_compute |