Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 1 | /* |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +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/CLGEMM.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/GPUTarget.h" |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Types.h" |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/Utils.h" |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/Validate.h" |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 35 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 36 | #include "arm_compute/runtime/CL/gemm_reshaped/CLGEMMReshapedConfiguration.h" |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 37 | #include "arm_compute/runtime/ITensorAllocator.h" |
| 38 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 39 | namespace arm_compute |
| 40 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 41 | using namespace arm_compute::misc::shape_calculator; |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 42 | using namespace arm_compute::cl_gemm; |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 43 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 44 | namespace |
| 45 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 46 | 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) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 47 | { |
| 48 | bool flag = true; |
| 49 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 50 | if(gpu_target_is_in(gpu_target, GPUTarget::G52, GPUTarget::G52LIT, GPUTarget::G71, GPUTarget::G72, GPUTarget::G76)) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 51 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 52 | if((m > 1) && n < 16) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 53 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 54 | flag = true; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 55 | } |
| 56 | else |
| 57 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 58 | // COMPMID-852 |
| 59 | if(k > 256 && m > 4 && is_data_type_float(data_type) && reshape_b_only_on_first_run) |
| 60 | { |
| 61 | constexpr float alpha = 3.2f; |
| 62 | constexpr float fact0 = 1.51f; |
| 63 | constexpr float fact1 = 1.66f; |
| 64 | constexpr float ops = 12.0f; |
| 65 | const float scale = k > 1024 ? 1.07f : 1.0f; |
| 66 | flag = alpha + ((n * fact0) / ops) < ((fact1 * n * scale) / ops); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | flag = false; |
| 71 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 74 | else |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 75 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 76 | // We reshape the matrices only if we do not have the vector-by-matrix case and we reshape the matrix B only once |
| 77 | flag = m != 1 && reshape_b_only_on_first_run; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 80 | return flag; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 81 | } |
| 82 | } // namespace |
| 83 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 84 | CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager) |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 85 | : _memory_group(std::move(memory_manager)), |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 86 | _mm_kernel(), |
| 87 | _ma_kernel(), |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 88 | _reshape_lhs_kernel(), |
| 89 | _reshape_rhs_kernel(), |
| 90 | _mm_reshaped_kernel(), |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 91 | _tmp_a(), |
| 92 | _tmp_b(), |
| 93 | _original_b(nullptr), |
| 94 | _is_interleaved_transposed(false), |
| 95 | _run_addition(false), |
| 96 | _reshape_b_only_on_first_run(false), |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 97 | _is_prepared(false), |
| 98 | _is_new_gemm_reshaped(false) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 99 | { |
| 100 | } |
| 101 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 102 | void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 103 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 104 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 105 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 106 | // Perform validation step |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 107 | ARM_COMPUTE_ERROR_THROW_ON(validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info)); |
| 108 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 109 | // Check if we need to reshape the matrix B only on the first run |
| 110 | _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 111 | _is_prepared = gemm_info.retain_internal_weights(); |
| 112 | _original_b = b; |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 113 | |
| 114 | const ICLTensor *matrix_a = a; |
| 115 | const ICLTensor *matrix_b = b; |
| 116 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 117 | // Get the GPU target |
| 118 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 119 | |
| 120 | // Set the target for the kernels |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 121 | _reshape_lhs_kernel.set_target(gpu_target); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 122 | _mm_kernel.set_target(gpu_target); |
| 123 | |
| 124 | // Arguments used by GEMMReshapeInfo |
| 125 | // 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 |
| 126 | // in order to know how the matrices have been reshaped |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 127 | DataType data_type = a->info()->data_type(); |
| 128 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 129 | const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1); |
| 130 | const unsigned int n = b->info()->dimension(0); |
| 131 | const unsigned int k = a->info()->dimension(0); |
| 132 | const unsigned int batch_size = reinterpret_input_as_3d ? a->info()->dimension(3) : a->info()->dimension(2); |
| 133 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
| 134 | int mult_transpose1xW_width = 1; |
| 135 | int mult_interleave4x4_height = 1; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 136 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 137 | if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 138 | { |
| 139 | mult_transpose1xW_width = 4; |
| 140 | mult_interleave4x4_height = 2; |
| 141 | } |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 142 | GEMMRHSMatrixInfo rhs_info; |
| 143 | rhs_info.n0 = 16 / b->info()->element_size(); |
| 144 | rhs_info.k0 = 1; |
| 145 | rhs_info.h0 = mult_transpose1xW_width; |
| 146 | rhs_info.interleave = false; |
| 147 | rhs_info.transpose = false; |
| 148 | |
| 149 | GEMMLHSMatrixInfo lhs_info; |
| 150 | lhs_info.m0 = 4; |
| 151 | lhs_info.k0 = 4; |
| 152 | lhs_info.v0 = mult_interleave4x4_height; |
| 153 | lhs_info.interleave = true; |
| 154 | lhs_info.transpose = true; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 155 | |
| 156 | // Check if we need to reshape the matrix A and matrix B |
| 157 | _is_interleaved_transposed = is_interleaved_transposed(m, n, k, a->info()->data_type(), _reshape_b_only_on_first_run, gpu_target); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 158 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 159 | // Check if we can run the new reshaped GEMM |
| 160 | const auto workload = static_cast<float>((m * n) / 20.0f); |
| 161 | _is_new_gemm_reshaped = (workload > 1600.0f) && (get_arch_from_target(gpu_target) == GPUTarget::BIFROST) && _is_interleaved_transposed && (data_type == DataType::F32); |
| 162 | |
| 163 | const bool add_matrix_c = (beta != 0.f && c != nullptr); |
| 164 | const bool is_beta_one = std::abs(1.0f - beta) < 0.00001f; |
| 165 | const bool use_fused_add = is_beta_one && (c != nullptr && c->info()->num_dimensions() == 1) && !_is_new_gemm_reshaped; |
| 166 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 167 | // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D |
| 168 | if(_is_interleaved_transposed) |
| 169 | { |
| 170 | reinterpret_input_as_3d = false; |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 171 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 172 | matrix_a = &_tmp_a; |
| 173 | matrix_b = &_tmp_b; |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 174 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 175 | // Manage intermediate buffers |
| 176 | _memory_group.manage(&_tmp_a); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 177 | if(!_reshape_b_only_on_first_run) |
| 178 | { |
| 179 | _memory_group.manage(&_tmp_b); |
| 180 | } |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 181 | // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel |
| 182 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 183 | if(_is_new_gemm_reshaped) |
| 184 | { |
| 185 | GEMMLHSMatrixInfo lhs_info; |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 186 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 187 | // Pick up the GEMM configuration |
| 188 | std::tie(lhs_info, rhs_info) = CLGEMMReshapedConfigurationFactory::create()->configure(m, n, k, batch_size, data_type); |
| 189 | |
| 190 | _reshape_lhs_kernel.configure(a, &_tmp_a, lhs_info, gemm_info.reinterpret_input_as_3d()); |
| 191 | _reshape_rhs_kernel.configure(b, &_tmp_b, rhs_info); |
| 192 | |
| 193 | // Configure and tune matrix multiply kernel |
| 194 | _mm_reshaped_kernel.configure(matrix_a, matrix_b, output, alpha, lhs_info, rhs_info, GEMMReshapeInfo(m, n, k, 1, 1, |
| 195 | depth_output_gemm3d, reinterpret_input_as_3d)); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | // Configure interleave kernel |
| 200 | _reshape_lhs_kernel.configure(a, &_tmp_a, lhs_info, gemm_info.reinterpret_input_as_3d()); |
| 201 | // Configure transpose kernel |
| 202 | _reshape_rhs_kernel.configure(b, &_tmp_b, rhs_info); |
| 203 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 204 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 205 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 206 | if(!_is_new_gemm_reshaped) |
| 207 | { |
| 208 | // Configure and tune matrix multiply kernel |
| 209 | _mm_kernel.configure(matrix_a, matrix_b, (add_matrix_c && !use_fused_add) ? nullptr : c, output, alpha, beta, _is_interleaved_transposed, |
| 210 | GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d, reinterpret_input_as_3d), |
| 211 | gemm_info.fp_mixed_precision()); |
| 212 | CLScheduler::get().tune_kernel_static(_mm_kernel); |
| 213 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 214 | |
| 215 | if(_is_interleaved_transposed) |
| 216 | { |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 217 | // Allocate intermediate tensors |
| 218 | _tmp_a.allocator()->allocate(); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 219 | if(!_reshape_b_only_on_first_run) |
| 220 | { |
| 221 | _tmp_b.allocator()->allocate(); |
| 222 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 223 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 224 | |
| 225 | // Configure matrix addition kernel |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 226 | if(add_matrix_c && !use_fused_add) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 227 | { |
| 228 | _ma_kernel.configure(c, output, beta); |
| 229 | _run_addition = true; |
| 230 | } |
| 231 | } |
| 232 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 233 | Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 234 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 235 | ARM_COMPUTE_UNUSED(alpha); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 236 | ARM_COMPUTE_UNUSED(output); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 237 | |
| 238 | // Check if we need to reshape the matrix B only on the first run |
| 239 | const bool reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
| 240 | |
| 241 | const ITensorInfo *matrix_a_info = a; |
| 242 | const ITensorInfo *matrix_b_info = b; |
| 243 | |
| 244 | TensorInfo tmp_a_info{}; |
| 245 | TensorInfo tmp_b_info{}; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 246 | |
| 247 | // Get the GPU target |
| 248 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 249 | |
| 250 | // Arguments used by GEMMReshapeInfo |
| 251 | // 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 |
| 252 | // in order to know how the matrices have been reshaped |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 253 | DataType data_type = a->data_type(); |
| 254 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 255 | const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1); |
| 256 | const unsigned int n = b->dimension(0); |
| 257 | const unsigned int k = a->dimension(0); |
| 258 | const unsigned int batch_size = reinterpret_input_as_3d ? a->dimension(3) : a->dimension(2); |
| 259 | int mult_transpose1xW_width = 1; |
| 260 | int mult_interleave4x4_height = 1; |
| 261 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 262 | |
| 263 | if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST) |
| 264 | { |
| 265 | mult_transpose1xW_width = 4; |
| 266 | mult_interleave4x4_height = 2; |
| 267 | } |
| 268 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 269 | GEMMRHSMatrixInfo rhs_info; |
| 270 | rhs_info.n0 = 16 / b->element_size(); |
| 271 | rhs_info.k0 = 1; |
| 272 | rhs_info.h0 = mult_transpose1xW_width; |
| 273 | rhs_info.interleave = false; |
| 274 | rhs_info.transpose = false; |
| 275 | |
| 276 | GEMMLHSMatrixInfo lhs_info; |
| 277 | lhs_info.m0 = 4; |
| 278 | lhs_info.k0 = 4; |
| 279 | lhs_info.v0 = mult_interleave4x4_height; |
| 280 | lhs_info.interleave = true; |
| 281 | lhs_info.transpose = true; |
| 282 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 283 | // Check if we need to reshape the matrix A and matrix B |
| 284 | const bool run_interleave_transpose = is_interleaved_transposed(m, n, k, a->data_type(), reshape_b_only_on_first_run, gpu_target); |
| 285 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 286 | // Check if we can run the new reshaped GEMM |
| 287 | const auto workload = static_cast<float>((m * n) / 20.0f); |
| 288 | const bool is_new_gemm_reshaped = (workload > 1600.f) && (get_arch_from_target(gpu_target) == GPUTarget::BIFROST) && run_interleave_transpose && (data_type == DataType::F32); |
| 289 | |
| 290 | const bool add_matrix_c = (beta != 0.f && c != nullptr); |
| 291 | const bool is_beta_one = std::abs(1.0f - beta) < 0.00001f; |
| 292 | const bool use_fused_add = is_beta_one && (c != nullptr && c->num_dimensions() == 1) && !is_new_gemm_reshaped; |
| 293 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 294 | // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D |
| 295 | if(run_interleave_transpose) |
| 296 | { |
| 297 | reinterpret_input_as_3d = false; |
| 298 | } |
| 299 | |
| 300 | const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d, reinterpret_input_as_3d); |
| 301 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 302 | if(run_interleave_transpose) |
| 303 | { |
| 304 | matrix_a_info = &tmp_a_info; |
| 305 | matrix_b_info = &tmp_b_info; |
| 306 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 307 | if(is_new_gemm_reshaped) |
| 308 | { |
| 309 | GEMMLHSMatrixInfo lhs_info; |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 310 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 311 | // Pick up the GEMM configuration |
| 312 | std::tie(lhs_info, rhs_info) = CLGEMMReshapedConfigurationFactory::create()->configure(m, n, k, batch_size, data_type); |
| 313 | |
| 314 | 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()))); |
| 315 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeLHSMatrixKernel::validate(a, &tmp_a_info, lhs_info, gemm_info.reinterpret_input_as_3d())); |
| 316 | |
| 317 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info))); |
| 318 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info)); |
| 319 | |
| 320 | // Validate matrix multiply |
| 321 | 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, |
| 322 | depth_output_gemm3d, reinterpret_input_as_3d))); |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | // Validate interleave kernel |
| 327 | 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()))); |
| 328 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeLHSMatrixKernel::validate(a, &tmp_a_info, lhs_info, gemm_info.reinterpret_input_as_3d())); |
| 329 | // Validate transpose kernel |
| 330 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info))); |
| 331 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info)); |
| 332 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 335 | if(!is_new_gemm_reshaped) |
| 336 | { |
| 337 | // Validate matrix multiply |
| 338 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, (add_matrix_c && !use_fused_add) ? nullptr : c, output, alpha, beta, |
| 339 | run_interleave_transpose, reshape_info, gpu_target, gemm_info.fp_mixed_precision())); |
| 340 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 341 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 342 | if(add_matrix_c && !use_fused_add) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 343 | { |
| 344 | // Validate matrix addition kernel |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 345 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta)); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 346 | } |
| 347 | |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 348 | return Status{}; |
| 349 | } |
| 350 | |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 351 | void CLGEMM::run() |
| 352 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 353 | prepare(); |
| 354 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 355 | _memory_group.acquire(); |
| 356 | |
| 357 | if(_is_interleaved_transposed) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 358 | { |
| 359 | // Run interleave kernel |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 360 | CLScheduler::get().enqueue(_reshape_lhs_kernel, false); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 361 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 362 | if(!_reshape_b_only_on_first_run) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 363 | { |
| 364 | // Run transpose kernel |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 365 | CLScheduler::get().enqueue(_reshape_rhs_kernel, false); |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 366 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // Run matrix multiply kernel |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 370 | if(_is_new_gemm_reshaped) |
| 371 | { |
| 372 | CLScheduler::get().enqueue(_mm_reshaped_kernel, !_run_addition); |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | CLScheduler::get().enqueue(_mm_kernel, !_run_addition); |
| 377 | } |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 378 | |
| 379 | // Run matrix addition kernel |
| 380 | if(_run_addition) |
| 381 | { |
| 382 | CLScheduler::get().enqueue(_ma_kernel); |
| 383 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 384 | |
| 385 | _memory_group.release(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 386 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 387 | |
| 388 | void CLGEMM::prepare() |
| 389 | { |
| 390 | if(!_is_prepared) |
| 391 | { |
| 392 | if(_is_interleaved_transposed && _reshape_b_only_on_first_run) |
| 393 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 394 | // Run transpose kernel and mark original weights tensor as unused |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 395 | _tmp_b.allocator()->allocate(); |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 396 | CLScheduler::get().enqueue(_reshape_rhs_kernel, false); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 397 | _original_b->mark_as_unused(); |
| 398 | } |
| 399 | CLScheduler::get().queue().finish(); |
| 400 | _is_prepared = true; |
| 401 | } |
| 402 | } |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 403 | } // namespace arm_compute |