Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/runtime/CL/functions/CLGEMM.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/GPUTarget.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Types.h" |
Gian Marco Iodice | bb36a8e | 2018-04-19 12:05:08 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/Validate.h" |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 36 | #include "arm_compute/runtime/ITensorAllocator.h" |
| 37 | |
| 38 | using namespace arm_compute; |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 39 | using namespace arm_compute::misc::shape_calculator; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 41 | namespace |
| 42 | { |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 43 | inline bool is_interleaved_transposed(unsigned int m, unsigned int n, unsigned int k, DataType data_type, bool reshape_b_only_on_first_run, GPUTarget gpu_target) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 44 | { |
| 45 | bool flag = true; |
| 46 | |
Georgios Pinitas | a34286e | 2018-09-04 12:18:50 +0100 | [diff] [blame] | 47 | if(gpu_target_is_in(gpu_target, GPUTarget::G52, GPUTarget::G52LIT, GPUTarget::G71, GPUTarget::G72, GPUTarget::G76)) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 48 | { |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 49 | if((m > 1) && n < 16) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 50 | { |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 51 | flag = true; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 52 | } |
| 53 | else |
| 54 | { |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 55 | // COMPMID-852 |
| 56 | if(k > 256 && m > 4 && is_data_type_float(data_type) && reshape_b_only_on_first_run) |
| 57 | { |
| 58 | constexpr float alpha = 3.2f; |
| 59 | constexpr float fact0 = 1.51f; |
| 60 | constexpr float fact1 = 1.66f; |
| 61 | constexpr float ops = 12.0f; |
| 62 | const float scale = k > 1024 ? 1.07f : 1.0f; |
| 63 | flag = alpha + ((n * fact0) / ops) < ((fact1 * n * scale) / ops); |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | flag = false; |
| 68 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
Gian Marco Iodice | cda0c38 | 2018-04-23 16:16:22 +0100 | [diff] [blame] | 71 | else |
| 72 | { |
| 73 | // We reshape the matrices only if we do not have the vector-by-matrix case and we reshape the matrix B only once |
| 74 | flag = m != 1 && reshape_b_only_on_first_run; |
| 75 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 76 | |
| 77 | return flag; |
| 78 | } |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 79 | |
| 80 | inline void select_gemm_configuration(unsigned int m, unsigned int n, GEMMLHSMatrixInfo &lhs_info, GEMMRHSMatrixInfo &rhs_info) |
| 81 | { |
| 82 | // Heuristic selection for GEMM |
| 83 | if(n <= 4) |
| 84 | { |
| 85 | // Configure GEMMLHSMatrixInfo |
| 86 | lhs_info.m0 = 4; |
| 87 | lhs_info.k0 = 8; |
| 88 | lhs_info.v0 = lhs_info.m0 * 16 < m ? 2 : 16; |
| 89 | lhs_info.interleave = true; |
| 90 | lhs_info.transpose = false; |
| 91 | |
| 92 | // Configure GEMMRHSMatrixInfo |
| 93 | rhs_info.n0 = 2; |
| 94 | rhs_info.k0 = lhs_info.k0; |
| 95 | rhs_info.h0 = rhs_info.n0 * 16 < n ? 2 : 16; |
| 96 | rhs_info.interleave = false; |
| 97 | rhs_info.transpose = true; |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | // Configure GEMMLHSMatrixInfo |
| 102 | lhs_info.m0 = (m * n) / 24 > 2048 ? 6 : 5; |
| 103 | lhs_info.k0 = 4; |
| 104 | lhs_info.v0 = 32; |
| 105 | lhs_info.interleave = false; |
| 106 | lhs_info.transpose = false; |
| 107 | |
| 108 | // Configure GEMMRHSMatrixInfo |
| 109 | rhs_info.n0 = 4; |
| 110 | rhs_info.k0 = lhs_info.k0; |
| 111 | rhs_info.h0 = 32; |
| 112 | rhs_info.interleave = true; |
| 113 | rhs_info.transpose = true; |
| 114 | } |
| 115 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 116 | } // namespace |
| 117 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 118 | CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 119 | : _memory_group(std::move(memory_manager)), |
| 120 | _interleave_kernel(), |
| 121 | _transpose_kernel(), |
| 122 | _mm_kernel(), |
| 123 | _ma_kernel(), |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 124 | _reshape_lhs_kernel(), |
| 125 | _reshape_rhs_kernel(), |
| 126 | _mm_reshaped_kernel(), |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 127 | _tmp_a(), |
| 128 | _tmp_b(), |
| 129 | _original_b(nullptr), |
| 130 | _is_interleaved_transposed(false), |
| 131 | _run_addition(false), |
| 132 | _reshape_b_only_on_first_run(false), |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 133 | _is_prepared(false), |
| 134 | _is_G76_path(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | { |
| 136 | } |
| 137 | |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 138 | void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 140 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 142 | // Perform validation step |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 143 | ARM_COMPUTE_ERROR_THROW_ON(validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 145 | // Check if we need to reshape the matrix B only on the first run |
| 146 | _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
Michele Di Giorgio | ba1ffe9 | 2018-08-22 14:28:30 +0100 | [diff] [blame] | 147 | _is_prepared = gemm_info.retain_internal_weights(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 148 | _original_b = b; |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 149 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 150 | const ICLTensor *matrix_a = a; |
| 151 | const ICLTensor *matrix_b = b; |
| 152 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 153 | // Get the GPU target |
| 154 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 155 | |
| 156 | // Set the target for the kernels |
| 157 | _interleave_kernel.set_target(gpu_target); |
| 158 | _mm_kernel.set_target(gpu_target); |
| 159 | |
| 160 | // Arguments used by GEMMReshapeInfo |
| 161 | // 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 |
| 162 | // in order to know how the matrices have been reshaped |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 163 | DataType data_type = a->info()->data_type(); |
| 164 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 165 | const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1); |
| 166 | const unsigned int n = b->info()->dimension(0); |
| 167 | const unsigned int k = a->info()->dimension(0); |
| 168 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
| 169 | int mult_transpose1xW_width = 1; |
| 170 | int mult_interleave4x4_height = 1; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 171 | |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 172 | if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 173 | { |
| 174 | mult_transpose1xW_width = 4; |
| 175 | mult_interleave4x4_height = 2; |
| 176 | } |
| 177 | |
| 178 | // Check if we need to reshape the matrix A and matrix B |
| 179 | _is_interleaved_transposed = is_interleaved_transposed(m, n, k, a->info()->data_type(), _reshape_b_only_on_first_run, gpu_target); |
Gian Marco | b5311a6 | 2017-12-13 12:48:03 +0000 | [diff] [blame] | 180 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 181 | // Check if we can run the new reshaped GEMM |
| 182 | _is_G76_path = (gpu_target == GPUTarget::G76) && _is_interleaved_transposed && (data_type == DataType::F32); |
| 183 | ; |
| 184 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 185 | // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D |
| 186 | if(_is_interleaved_transposed) |
| 187 | { |
| 188 | reinterpret_input_as_3d = false; |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 189 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 190 | matrix_a = &_tmp_a; |
| 191 | matrix_b = &_tmp_b; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 192 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 193 | // Manage intermediate buffers |
| 194 | _memory_group.manage(&_tmp_a); |
Georgios Pinitas | ae4ce7b | 2018-03-19 17:50:45 +0000 | [diff] [blame] | 195 | if(!_reshape_b_only_on_first_run) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 196 | { |
| 197 | _memory_group.manage(&_tmp_b); |
| 198 | } |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 199 | // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 200 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 201 | if(_is_G76_path) |
| 202 | { |
| 203 | GEMMLHSMatrixInfo lhs_info; |
| 204 | GEMMRHSMatrixInfo rhs_info; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 206 | // Pick up the GEMM configuration based on M,N and K |
| 207 | select_gemm_configuration(m, n, lhs_info, rhs_info); |
| 208 | |
| 209 | _reshape_lhs_kernel.configure(a, &_tmp_a, lhs_info, gemm_info.reinterpret_input_as_3d()); |
| 210 | _reshape_rhs_kernel.configure(b, &_tmp_b, rhs_info); |
| 211 | |
| 212 | // Configure and tune matrix multiply kernel |
| 213 | _mm_reshaped_kernel.configure(matrix_a, matrix_b, output, alpha, lhs_info, rhs_info, GEMMReshapeInfo(m, n, k, 1, 1, |
| 214 | depth_output_gemm3d, reinterpret_input_as_3d)); |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | // Configure interleave kernel |
| 219 | _interleave_kernel.configure(a, &_tmp_a, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d()); |
| 220 | |
| 221 | // Configure transpose kernel |
| 222 | _transpose_kernel.configure(b, &_tmp_b, mult_transpose1xW_width); |
| 223 | } |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 224 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 225 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 226 | if(!_is_G76_path) |
| 227 | { |
| 228 | // Configure and tune matrix multiply kernel |
| 229 | _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, |
| 230 | mult_transpose1xW_width, mult_interleave4x4_height, |
| 231 | depth_output_gemm3d, reinterpret_input_as_3d), |
| 232 | gemm_info.fp_mixed_precision()); |
| 233 | CLScheduler::get().tune_kernel_static(_mm_kernel); |
| 234 | } |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 235 | |
| 236 | if(_is_interleaved_transposed) |
| 237 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 238 | // Allocate intermediate tensors |
| 239 | _tmp_a.allocator()->allocate(); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 240 | if(!_reshape_b_only_on_first_run) |
| 241 | { |
| 242 | _tmp_b.allocator()->allocate(); |
| 243 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 244 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 245 | |
| 246 | // Configure matrix addition kernel |
| 247 | if(beta != 0 && c != nullptr) |
| 248 | { |
| 249 | _ma_kernel.configure(c, output, beta); |
| 250 | _run_addition = true; |
| 251 | } |
| 252 | } |
| 253 | |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 254 | Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 255 | { |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 256 | ARM_COMPUTE_UNUSED(alpha); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 257 | ARM_COMPUTE_UNUSED(output); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 258 | |
| 259 | // Check if we need to reshape the matrix B only on the first run |
| 260 | const bool reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
| 261 | |
| 262 | const ITensorInfo *matrix_a_info = a; |
| 263 | const ITensorInfo *matrix_b_info = b; |
| 264 | |
| 265 | TensorInfo tmp_a_info{}; |
| 266 | TensorInfo tmp_b_info{}; |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 267 | |
| 268 | // Get the GPU target |
| 269 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 270 | |
| 271 | // Arguments used by GEMMReshapeInfo |
| 272 | // 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 |
| 273 | // in order to know how the matrices have been reshaped |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 274 | DataType data_type = a->data_type(); |
| 275 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 276 | const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1); |
| 277 | const unsigned int n = b->dimension(0); |
| 278 | const unsigned int k = a->dimension(0); |
| 279 | int mult_transpose1xW_width = 1; |
| 280 | int mult_interleave4x4_height = 1; |
| 281 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 282 | |
| 283 | if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST) |
| 284 | { |
| 285 | mult_transpose1xW_width = 4; |
| 286 | mult_interleave4x4_height = 2; |
| 287 | } |
| 288 | |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 289 | // Check if we need to reshape the matrix A and matrix B |
| 290 | const bool run_interleave_transpose = is_interleaved_transposed(m, n, k, a->data_type(), reshape_b_only_on_first_run, gpu_target); |
| 291 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 292 | // Check if we can run the new reshaped GEMM |
| 293 | const bool is_G76_path = (gpu_target == GPUTarget::G76) && run_interleave_transpose && (data_type == DataType::F32); |
| 294 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 295 | // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D |
| 296 | if(run_interleave_transpose) |
| 297 | { |
| 298 | reinterpret_input_as_3d = false; |
| 299 | } |
| 300 | |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 301 | const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d, reinterpret_input_as_3d); |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 302 | |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 303 | if(run_interleave_transpose) |
| 304 | { |
| 305 | matrix_a_info = &tmp_a_info; |
| 306 | matrix_b_info = &tmp_b_info; |
| 307 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 308 | if(is_G76_path) |
| 309 | { |
| 310 | GEMMLHSMatrixInfo lhs_info; |
| 311 | GEMMRHSMatrixInfo rhs_info; |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 312 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 313 | // Pick up the GEMM configuration based on M,N and K |
| 314 | select_gemm_configuration(m, n, lhs_info, rhs_info); |
| 315 | |
| 316 | auto_init_if_empty(tmp_a_info, a->clone()->set_tensor_shape(compute_lhs_reshaped_shape(*a, lhs_info, gemm_info.reinterpret_input_as_3d()))); |
| 317 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeLHSMatrixKernel::validate(a, &tmp_a_info, lhs_info, gemm_info.reinterpret_input_as_3d())); |
| 318 | |
| 319 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info))); |
| 320 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info)); |
| 321 | |
| 322 | // Validate matrix multiply |
| 323 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyReshapedKernel::validate(matrix_a_info, matrix_b_info, output, alpha, lhs_info, rhs_info, GEMMReshapeInfo(m, n, k, 1, 1, |
| 324 | depth_output_gemm3d, reinterpret_input_as_3d))); |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | // Validate interleave kernel |
| 329 | 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()))); |
| 330 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMInterleave4x4Kernel::validate(a, &tmp_a_info, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d())); |
| 331 | |
| 332 | // Validate transpose kernel |
| 333 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width))); |
| 334 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMTranspose1xWKernel::validate(b, &tmp_b_info, mult_transpose1xW_width)); |
| 335 | } |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 338 | if(!is_G76_path) |
| 339 | { |
| 340 | // Validate matrix multiply |
| 341 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output, alpha, run_interleave_transpose, reshape_info, gpu_target, gemm_info.fp_mixed_precision())); |
| 342 | } |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 343 | |
| 344 | if(beta != 0 && c != nullptr) |
| 345 | { |
| 346 | // Validate matrix addition kernel |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 347 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta)); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 350 | return Status{}; |
| 351 | } |
| 352 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 353 | void CLGEMM::run() |
| 354 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 355 | prepare(); |
| 356 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 357 | _memory_group.acquire(); |
| 358 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 359 | if(_is_interleaved_transposed) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 360 | { |
| 361 | // Run interleave kernel |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 362 | if(_is_G76_path) |
| 363 | { |
| 364 | CLScheduler::get().enqueue(_reshape_lhs_kernel, false); |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | CLScheduler::get().enqueue(_interleave_kernel, false); |
| 369 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 370 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 371 | if(!_reshape_b_only_on_first_run) |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 372 | { |
| 373 | // Run transpose kernel |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 374 | if(_is_G76_path) |
| 375 | { |
| 376 | CLScheduler::get().enqueue(_reshape_rhs_kernel, false); |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | CLScheduler::get().enqueue(_transpose_kernel, false); |
| 381 | } |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 382 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | // Run matrix multiply kernel |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 386 | if(_is_G76_path) |
| 387 | { |
| 388 | CLScheduler::get().enqueue(_mm_reshaped_kernel, !_run_addition); |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | CLScheduler::get().enqueue(_mm_kernel, !_run_addition); |
| 393 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 394 | |
| 395 | // Run matrix addition kernel |
| 396 | if(_run_addition) |
| 397 | { |
| 398 | CLScheduler::get().enqueue(_ma_kernel); |
| 399 | } |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 400 | |
| 401 | _memory_group.release(); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 402 | } |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame] | 403 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 404 | void CLGEMM::prepare() |
| 405 | { |
| 406 | if(!_is_prepared) |
| 407 | { |
| 408 | if(_is_interleaved_transposed && _reshape_b_only_on_first_run) |
| 409 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 410 | // Run transpose kernel and mark original weights tensor as unused |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 411 | _tmp_b.allocator()->allocate(); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame^] | 412 | if(_is_G76_path) |
| 413 | { |
| 414 | CLScheduler::get().enqueue(_reshape_rhs_kernel, false); |
| 415 | } |
| 416 | else |
| 417 | { |
| 418 | CLScheduler::get().enqueue(_transpose_kernel, false); |
| 419 | } |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 420 | _original_b->mark_as_unused(); |
| 421 | } |
| 422 | CLScheduler::get().queue().finish(); |
| 423 | _is_prepared = true; |
| 424 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 425 | } |