Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1 | /* |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 33 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 34 | |
| 35 | using namespace arm_compute; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 36 | using namespace arm_compute::misc::shape_calculator; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 37 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 38 | namespace |
| 39 | { |
| 40 | inline bool is_interleaved_transposed(int m, int n, int k, bool reshape_b_only_on_first_run, GPUTarget gpu_target) |
| 41 | { |
| 42 | bool flag = true; |
| 43 | |
Georgios Pinitas | a34286e | 2018-09-04 12:18:50 +0100 | [diff] [blame] | 44 | if(gpu_target_is_in(gpu_target, |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 45 | GPUTarget::G71, GPUTarget::G72, |
Gian Marco Iodice | e059f4f | 2018-11-15 15:21:17 +0000 | [diff] [blame] | 46 | GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT)) |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 47 | { |
| 48 | // COMPMID-852 |
| 49 | if(k > 256 && m > 4 && reshape_b_only_on_first_run) |
| 50 | { |
| 51 | flag = ((0.72f + n * 0.10766f) < (n * 0.1284f)); |
| 52 | } |
| 53 | else |
| 54 | { |
| 55 | flag = false; |
| 56 | } |
| 57 | } |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 58 | else |
| 59 | { |
| 60 | flag = m > 1; |
| 61 | } |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 62 | |
| 63 | return flag; |
| 64 | } |
| 65 | } // namespace |
| 66 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 67 | CLGEMMLowpMatrixMultiplyCore::CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 68 | : _memory_group(std::move(memory_manager)), |
| 69 | _mm_kernel(), |
| 70 | _mtx_a_reshape_kernel(), |
| 71 | _mtx_b_reshape_kernel(), |
| 72 | _mtx_a_reduction_kernel(), |
| 73 | _mtx_b_reduction_kernel(), |
| 74 | _offset_contribution_kernel(), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 75 | _offset_contribution_output_stage_kernel(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 76 | _vector_sum_col(), |
| 77 | _vector_sum_row(), |
| 78 | _tmp_a(), |
| 79 | _tmp_b(), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 80 | _mm_result_s32(), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 81 | _original_b(nullptr), |
| 82 | _a_offset(0), |
| 83 | _b_offset(0), |
| 84 | _is_interleaved_transposed(true), |
| 85 | _reshape_b_only_on_first_run(false), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 86 | _is_prepared(false), |
| 87 | _fuse_output_stage(false) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 88 | { |
| 89 | } |
| 90 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 91 | 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] | 92 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 93 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 94 | 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] | 95 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 96 | _is_prepared = false; |
| 97 | _original_b = b; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 98 | _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
| 99 | _a_offset = a->info()->quantization_info().offset; |
| 100 | _b_offset = b->info()->quantization_info().offset; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 101 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 102 | // Get the GPU target |
| 103 | const GPUTarget gpu_target = CLScheduler::get().target(); |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 104 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 105 | // Set the target for the kernels |
| 106 | _mtx_a_reshape_kernel.set_target(gpu_target); |
| 107 | _mm_kernel.set_target(gpu_target); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 108 | |
| 109 | const ICLTensor *matrix_a = a; |
| 110 | const ICLTensor *matrix_b = b; |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame^] | 111 | GEMMRHSMatrixInfo rhs_info; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 112 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 113 | // Arguments used by GEMMReshapeInfo |
| 114 | // 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 |
| 115 | // in order to know how the matrices have been reshaped |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 116 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 117 | const bool unroll_block = dot8_supported(CLKernelLibrary::get().get_device()); |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 118 | const int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 119 | const int n = b->info()->dimension(0); |
| 120 | const int k = a->info()->dimension(0); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 121 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 122 | constexpr int mult_transpose1xW_width = 1; |
| 123 | constexpr int mult_interleave4x4_height = 1; |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame^] | 124 | rhs_info.n0 = 16 / b->info()->element_size(); |
| 125 | rhs_info.k0 = 1; |
| 126 | rhs_info.h0 = mult_transpose1xW_width; |
| 127 | rhs_info.interleave = false; |
| 128 | rhs_info.transpose = false; |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 129 | |
| 130 | // Check if we need to reshape the matrix A and matrix B |
| 131 | _is_interleaved_transposed = is_interleaved_transposed(m, n, k, _reshape_b_only_on_first_run, gpu_target); |
| 132 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 133 | if(_is_interleaved_transposed) |
| 134 | { |
Isabella Gottardi | f02e527 | 2018-10-01 12:26:28 +0100 | [diff] [blame] | 135 | // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 136 | reinterpret_input_as_3d = false; |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 137 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 138 | matrix_a = &_tmp_a; |
| 139 | matrix_b = &_tmp_b; |
| 140 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 141 | _memory_group.manage(&_tmp_a); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 142 | if(!_reshape_b_only_on_first_run) |
| 143 | { |
| 144 | _memory_group.manage(&_tmp_b); |
| 145 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 146 | |
| 147 | // Configure interleave kernel |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 148 | _mtx_a_reshape_kernel.configure(a, &_tmp_a, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d(), unroll_block); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 149 | |
| 150 | // Configure transpose kernel |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame^] | 151 | _mtx_b_reshape_kernel.configure(b, &_tmp_b, rhs_info); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 152 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 153 | |
| 154 | // Initialize matrix B reduction kernel only if _a_offset is not equal to 0 |
| 155 | if(_a_offset != 0) |
| 156 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 157 | 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] | 158 | _vector_sum_col.allocator()->init(info_vector_sum_col); |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 159 | if(!_reshape_b_only_on_first_run) |
| 160 | { |
| 161 | _memory_group.manage(&_vector_sum_col); |
| 162 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 163 | |
| 164 | // Configure Matrix B reduction kernel |
| 165 | _mtx_b_reduction_kernel.configure(b, &_vector_sum_col); |
| 166 | } |
| 167 | |
| 168 | // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0 |
| 169 | if(_b_offset != 0) |
| 170 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 171 | 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] | 172 | _vector_sum_row.allocator()->init(info_vector_sum_row); |
| 173 | _memory_group.manage(&_vector_sum_row); |
| 174 | |
| 175 | // Configure matrix A reduction kernel |
| 176 | _mtx_a_reduction_kernel.configure(a, &_vector_sum_row); |
| 177 | } |
| 178 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 179 | // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage |
| 180 | if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE) |
| 181 | { |
| 182 | _fuse_output_stage = true; |
| 183 | |
| 184 | _memory_group.manage(&_mm_result_s32); |
| 185 | |
| 186 | // Configure matrix multiply kernel |
| 187 | _mm_kernel.configure(matrix_a, matrix_b, &_mm_result_s32, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, |
| 188 | mult_transpose1xW_width, mult_interleave4x4_height, |
| 189 | depth_output_gemm3d, reinterpret_input_as_3d)); |
| 190 | |
| 191 | // Configure offset contribution kernel |
| 192 | _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), |
| 193 | _a_offset, _b_offset, gemm_info.gemmlowp_output_stage()); |
| 194 | |
| 195 | _mm_result_s32.allocator()->allocate(); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | // Configure matrix multiply kernel |
| 200 | _mm_kernel.configure(matrix_a, matrix_b, output, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, |
| 201 | mult_transpose1xW_width, mult_interleave4x4_height, |
| 202 | depth_output_gemm3d, reinterpret_input_as_3d)); |
| 203 | |
| 204 | // Configure offset contribution kernel |
| 205 | _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); |
| 206 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 207 | |
| 208 | // Allocate tensors |
| 209 | if(_is_interleaved_transposed) |
| 210 | { |
| 211 | _tmp_a.allocator()->allocate(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 212 | if(!_reshape_b_only_on_first_run) |
| 213 | { |
| 214 | _tmp_b.allocator()->allocate(); |
| 215 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 218 | if(_a_offset != 0 && !_reshape_b_only_on_first_run) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 219 | { |
| 220 | _vector_sum_col.allocator()->allocate(); |
| 221 | } |
| 222 | |
| 223 | if(_b_offset != 0) |
| 224 | { |
| 225 | _vector_sum_row.allocator()->allocate(); |
| 226 | } |
| 227 | } |
| 228 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 229 | 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] | 230 | { |
| 231 | 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] | 232 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 233 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported"); |
| 234 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported"); |
| 235 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 236 | int32_t a_offset = a->quantization_info().offset; |
| 237 | int32_t b_offset = b->quantization_info().offset; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 238 | |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 239 | const ITensorInfo *matrix_a_info = a; |
| 240 | const ITensorInfo *matrix_b_info = b; |
| 241 | |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame^] | 242 | TensorInfo tmp_a_info{}; |
| 243 | TensorInfo tmp_b_info{}; |
| 244 | GEMMRHSMatrixInfo rhs_info; |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 245 | |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 246 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 247 | const int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 248 | const int n = b->dimension(0); |
| 249 | const int k = a->dimension(0); |
| 250 | constexpr int mult_transpose1xW_width = 1; |
| 251 | constexpr int mult_interleave4x4_height = 1; |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 252 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame^] | 253 | rhs_info.n0 = 16 / b->element_size(); |
| 254 | rhs_info.k0 = 1; |
| 255 | rhs_info.h0 = mult_transpose1xW_width; |
| 256 | rhs_info.interleave = false; |
| 257 | rhs_info.transpose = false; |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 258 | |
| 259 | bool reshape_matrices = is_interleaved_transposed(m, n, k, gemm_info.reshape_b_only_on_first_run(), CLScheduler::get().target()); |
| 260 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 261 | // if reshape_matrices is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D |
| 262 | if(reshape_matrices) |
| 263 | { |
| 264 | reinterpret_input_as_3d = false; |
| 265 | } |
| 266 | |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 267 | const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d, reinterpret_input_as_3d); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 268 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 269 | if(reshape_matrices) |
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_a_info = &tmp_a_info; |
| 272 | matrix_b_info = &tmp_b_info; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 273 | |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 274 | // Validate interleave kernel |
| 275 | auto_init_if_empty(tmp_a_info, a->clone()->set_tensor_shape(compute_interleaved_shape(*a, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d()))); |
| 276 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMInterleave4x4Kernel::validate(a, &tmp_a_info, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d())); |
| 277 | |
| 278 | // Validate transpose kernel |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame^] | 279 | |
| 280 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info))); |
| 281 | 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] | 282 | } |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 283 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 284 | TensorInfo info_vector_sum_col, info_vector_sum_row; |
| 285 | |
| 286 | // Validate matrix B reduction kernel only if _a_offset is not equal to 0 |
| 287 | if(a_offset != 0) |
| 288 | { |
| 289 | info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32); |
| 290 | |
| 291 | // Configure Matrix B reduction kernel |
| 292 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col)); |
| 293 | } |
| 294 | |
| 295 | // Validate Matrix A reduction kernel only if _b_offset is not equal to 0 |
| 296 | if(b_offset != 0) |
| 297 | { |
| 298 | info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32); |
| 299 | |
| 300 | // Configure matrix A reduction kernel |
| 301 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row)); |
| 302 | } |
| 303 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 304 | if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE) |
| 305 | { |
| 306 | TensorInfo mm_result_s32_info{}; |
| 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_matrices, reshape_info)).set_data_type(DataType::S32)); |
| 310 | |
| 311 | // Validate matrix multiply |
| 312 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info, reshape_matrices, reshape_info)); |
| 313 | |
| 314 | // Validate offset contribution kernel |
| 315 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info, |
| 316 | a_offset == 0 ? nullptr : &info_vector_sum_col, |
| 317 | b_offset == 0 ? nullptr : &info_vector_sum_row, |
| 318 | c, |
| 319 | output, |
| 320 | a_offset, b_offset, |
| 321 | gemm_info.gemmlowp_output_stage())); |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | // Validate matrix multiply |
| 326 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output, reshape_matrices, reshape_info)); |
| 327 | |
| 328 | // Validate offset contribution kernel |
| 329 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionKernel::validate(output, |
| 330 | a_offset == 0 ? nullptr : &info_vector_sum_col, |
| 331 | b_offset == 0 ? nullptr : &info_vector_sum_row, |
| 332 | c, |
| 333 | a_offset, b_offset)); |
| 334 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 335 | |
| 336 | return Status{}; |
| 337 | } |
| 338 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 339 | void CLGEMMLowpMatrixMultiplyCore::run() |
| 340 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 341 | prepare(); |
| 342 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 343 | _memory_group.acquire(); |
| 344 | |
| 345 | if(_is_interleaved_transposed) |
| 346 | { |
| 347 | // Run reshape matrix A |
| 348 | CLScheduler::get().enqueue(_mtx_a_reshape_kernel, false); |
| 349 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 350 | if(!_reshape_b_only_on_first_run) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 351 | { |
| 352 | // Run reshape matrix B |
| 353 | CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false); |
| 354 | } |
| 355 | } |
| 356 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 357 | // Run matrix B reduction kernel only if _a_offset is not equal to 0 |
| 358 | if(_a_offset != 0 && !_reshape_b_only_on_first_run) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 359 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 360 | CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Run matrix multiply |
| 364 | CLScheduler::get().enqueue(_mm_kernel, false); |
| 365 | |
| 366 | // Run matrix A reduction kernel only if _b_offset is not equal to 0 |
| 367 | if(_b_offset != 0) |
| 368 | { |
| 369 | CLScheduler::get().enqueue(_mtx_a_reduction_kernel, false); |
| 370 | } |
| 371 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 372 | if(_fuse_output_stage) |
| 373 | { |
| 374 | // Run offset contribution/output stage kernel |
| 375 | CLScheduler::get().enqueue(_offset_contribution_output_stage_kernel, true); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | // Run offset contribution kernel |
| 380 | CLScheduler::get().enqueue(_offset_contribution_kernel, true); |
| 381 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 382 | |
| 383 | _memory_group.release(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 384 | } |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 385 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 386 | void CLGEMMLowpMatrixMultiplyCore::prepare() |
| 387 | { |
| 388 | if(!_is_prepared) |
| 389 | { |
| 390 | if(_is_interleaved_transposed && _reshape_b_only_on_first_run) |
| 391 | { |
| 392 | ARM_COMPUTE_ERROR_ON(!_original_b->is_used()); |
| 393 | |
| 394 | // Run reshape kernel and mark original weights tensor as unused |
| 395 | _tmp_b.allocator()->allocate(); |
| 396 | CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false); |
| 397 | _original_b->mark_as_unused(); |
| 398 | } |
| 399 | |
| 400 | // Run matrix B reduction kernel only if _a_offset is not equal to 0 |
| 401 | if(_a_offset != 0 && _reshape_b_only_on_first_run) |
| 402 | { |
| 403 | _vector_sum_col.allocator()->allocate(); |
| 404 | CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false); |
| 405 | } |
| 406 | |
| 407 | CLScheduler::get().queue().finish(); |
| 408 | _is_prepared = true; |
| 409 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 410 | } |