Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CL/ICLGEMMKernelConfiguration.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/ICLTensor.h" |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/gemm/reshaped/CLGEMMReshapedKernelConfiguration.h" |
| 29 | #include "arm_compute/core/CL/gemm/reshaped_only_rhs/CLGEMMReshapedOnlyRHSKernelConfiguration.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Error.h" |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/GPUTarget.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/Types.h" |
Gian Marco Iodice | bb36a8e | 2018-04-19 12:05:08 +0100 | [diff] [blame] | 35 | #include "arm_compute/core/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/Validate.h" |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 37 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 39 | #include "arm_compute/runtime/ITensorAllocator.h" |
| 40 | |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 41 | namespace arm_compute |
| 42 | { |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 43 | using namespace arm_compute::misc::shape_calculator; |
Gian Marco Iodice | 90313eb | 2019-01-16 15:40:25 +0000 | [diff] [blame] | 44 | using namespace arm_compute::cl_gemm; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 46 | CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 47 | : _memory_group(std::move(memory_manager)), |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 48 | _mm_kernel(), |
| 49 | _ma_kernel(), |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 50 | _reshape_lhs_kernel(), |
| 51 | _reshape_rhs_kernel(), |
| 52 | _mm_reshaped_kernel(), |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 53 | _mm_reshaped_only_rhs_kernel(), |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 54 | _tmp_a(), |
| 55 | _tmp_b(), |
| 56 | _original_b(nullptr), |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 57 | _run_addition(false), |
| 58 | _reshape_b_only_on_first_run(false), |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 59 | _is_prepared(false), |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 60 | _gemm_type(GEMMType::NATIVE) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | { |
| 62 | } |
| 63 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 64 | CLGEMM::GEMMType CLGEMM::select_gemm_type(unsigned int m, unsigned int n, unsigned int k, DataType data_type, bool reshape_b_only_on_first_run, GPUTarget gpu_target) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | { |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 66 | GEMMType gemm_type = GEMMType::RESHAPED_V1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 68 | if(gpu_target_is_in(gpu_target, GPUTarget::G52, GPUTarget::G52LIT, GPUTarget::G71, GPUTarget::G72, GPUTarget::G76)) |
| 69 | { |
| 70 | if((m > 1) && (n < 16)) |
| 71 | { |
| 72 | gemm_type = GEMMType::RESHAPED_V1; |
| 73 | } |
| 74 | else if((m == 1) && (data_type == DataType::F32)) |
| 75 | { |
| 76 | gemm_type = GEMMType::RESHAPED_ONLY_RHS; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | // COMPMID-852 |
| 81 | if((k > 256) && (m > 4) && is_data_type_float(data_type) && reshape_b_only_on_first_run) |
| 82 | { |
| 83 | constexpr float alpha = 3.2f; |
| 84 | constexpr float fact0 = 1.51f; |
| 85 | constexpr float fact1 = 1.66f; |
| 86 | constexpr float ops = 12.0f; |
| 87 | const float scale = k > 1024 ? 1.07f : 1.0f; |
| 88 | gemm_type = (alpha + ((n * fact0) / ops) < ((fact1 * n * scale) / ops)) ? GEMMType::RESHAPED_V1 : GEMMType::NATIVE; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | gemm_type = GEMMType::NATIVE; |
| 93 | } |
| 94 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 96 | const auto workload = static_cast<float>((m * n) / 20.0f); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 97 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 98 | gemm_type = ((workload > 1600.0f) && (gemm_type == GEMMType::RESHAPED_V1) && (data_type == DataType::F32)) ? GEMMType::RESHAPED_V2 : gemm_type; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | // We reshape the matrices only if we do not have the vector-by-matrix case and we reshape the matrix B only once |
| 103 | gemm_type = ((m != 1) && reshape_b_only_on_first_run) ? GEMMType::RESHAPED_V1 : GEMMType::NATIVE; |
| 104 | } |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 105 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 106 | return gemm_type; |
| 107 | } |
| 108 | |
| 109 | void CLGEMM::configure_native(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info) |
| 110 | { |
| 111 | const unsigned int m = gemm_info.reinterpret_input_as_3d() ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1); |
| 112 | const unsigned int n = b->info()->dimension(0); |
| 113 | const unsigned int k = a->info()->dimension(0); |
| 114 | const GPUTarget gpu_target = CLScheduler::get().target(); |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 115 | |
| 116 | // Set the target for the kernels |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 117 | _mm_kernel.set_target(gpu_target); |
| 118 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 119 | GEMMReshapeInfo reshape_info(m, n, k, 1, 1, gemm_info.depth_output_gemm3d(), gemm_info.reinterpret_input_as_3d()); |
| 120 | |
| 121 | // Configure and tune matrix multiply kernel |
| 122 | _mm_kernel.configure(a, b, c, output, alpha, beta, false, reshape_info, gemm_info.fp_mixed_precision()); |
| 123 | |
| 124 | // Tune kernel statically |
| 125 | CLScheduler::get().tune_kernel_static(_mm_kernel); |
| 126 | } |
| 127 | |
| 128 | void CLGEMM::configure_reshaped_v1(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info) |
| 129 | { |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 130 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 131 | const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1); |
| 132 | const unsigned int n = b->info()->dimension(0); |
| 133 | const unsigned int k = a->info()->dimension(0); |
| 134 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 135 | const GPUTarget gpu_target = CLScheduler::get().target(); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 136 | int mult_transpose1xW_width = 1; |
| 137 | int mult_interleave4x4_height = 1; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 138 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 139 | // Set the target for the kernels |
| 140 | _reshape_lhs_kernel.set_target(gpu_target); |
| 141 | _mm_kernel.set_target(gpu_target); |
| 142 | |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 143 | if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 144 | { |
| 145 | mult_transpose1xW_width = 4; |
| 146 | mult_interleave4x4_height = 2; |
| 147 | } |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 148 | |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 149 | GEMMRHSMatrixInfo rhs_info; |
| 150 | rhs_info.n0 = 16 / b->info()->element_size(); |
| 151 | rhs_info.k0 = 1; |
| 152 | rhs_info.h0 = mult_transpose1xW_width; |
| 153 | rhs_info.interleave = false; |
| 154 | rhs_info.transpose = false; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 155 | |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 156 | GEMMLHSMatrixInfo lhs_info; |
| 157 | lhs_info.m0 = 4; |
| 158 | lhs_info.k0 = 4; |
| 159 | lhs_info.v0 = mult_interleave4x4_height; |
| 160 | lhs_info.interleave = true; |
| 161 | lhs_info.transpose = true; |
| 162 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 163 | GEMMReshapeInfo reshape_info(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d, false); |
Gian Marco | b5311a6 | 2017-12-13 12:48:03 +0000 | [diff] [blame] | 164 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 165 | _memory_group.manage(&_tmp_a); |
| 166 | if(!_reshape_b_only_on_first_run) |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 167 | { |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 168 | _memory_group.manage(&_tmp_b); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 169 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 171 | // Configure interleave kernel |
| 172 | _reshape_lhs_kernel.configure(a, &_tmp_a, lhs_info, reinterpret_input_as_3d); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 173 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 174 | // Configure transpose kernel |
| 175 | _reshape_rhs_kernel.configure(b, &_tmp_b, rhs_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 177 | // Configure and tune matrix multiply kernel |
| 178 | _mm_kernel.configure(&_tmp_a, &_tmp_b, c, output, alpha, beta, true, reshape_info, gemm_info.fp_mixed_precision()); |
| 179 | |
| 180 | CLScheduler::get().tune_kernel_static(_mm_kernel); |
| 181 | |
| 182 | // Allocate intermediate tensors |
| 183 | _tmp_a.allocator()->allocate(); |
| 184 | if(!_reshape_b_only_on_first_run) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | { |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 186 | _tmp_b.allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 190 | void CLGEMM::configure_reshaped_v2(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info) |
| 191 | { |
| 192 | ARM_COMPUTE_ERROR_ON(c != nullptr); |
| 193 | ARM_COMPUTE_UNUSED(beta); |
| 194 | ARM_COMPUTE_UNUSED(c); |
| 195 | |
| 196 | DataType data_type = a->info()->data_type(); |
| 197 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 198 | const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1); |
| 199 | const unsigned int n = b->info()->dimension(0); |
| 200 | const unsigned int k = a->info()->dimension(0); |
| 201 | const unsigned int batch_size = reinterpret_input_as_3d ? a->info()->dimension(3) : a->info()->dimension(2); |
| 202 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
| 203 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 204 | |
| 205 | // Set the target for the kernels |
| 206 | _reshape_lhs_kernel.set_target(gpu_target); |
| 207 | _mm_kernel.set_target(gpu_target); |
| 208 | |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 209 | GEMMReshapeInfo reshape_info(m, n, k, 1, 1, depth_output_gemm3d, false); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 210 | |
| 211 | // Manage intermediate buffers |
| 212 | _memory_group.manage(&_tmp_a); |
| 213 | if(!_reshape_b_only_on_first_run) |
| 214 | { |
| 215 | _memory_group.manage(&_tmp_b); |
| 216 | } |
| 217 | // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel |
| 218 | |
| 219 | GEMMLHSMatrixInfo lhs_info{}; |
| 220 | GEMMRHSMatrixInfo rhs_info{}; |
| 221 | |
| 222 | // Pick up the GEMM configuration |
| 223 | std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMReshapedKernelConfigurationFactory::create(gpu_target); |
| 224 | ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get()); |
| 225 | |
| 226 | // Configure lhs_info and rhs_info |
| 227 | std::tie(lhs_info, rhs_info) = gemm_config->configure(m, n, k, batch_size, data_type); |
| 228 | |
| 229 | _reshape_lhs_kernel.configure(a, &_tmp_a, lhs_info, gemm_info.reinterpret_input_as_3d()); |
| 230 | _reshape_rhs_kernel.configure(b, &_tmp_b, rhs_info); |
| 231 | |
| 232 | // Configure and tune matrix multiply kernel |
| 233 | _mm_reshaped_kernel.configure(&_tmp_a, &_tmp_b, output, alpha, lhs_info, rhs_info, reshape_info); |
| 234 | |
| 235 | // Allocate intermediate tensors |
| 236 | _tmp_a.allocator()->allocate(); |
| 237 | if(!_reshape_b_only_on_first_run) |
| 238 | { |
| 239 | _tmp_b.allocator()->allocate(); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void CLGEMM::configure_reshaped_only_rhs(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info) |
| 244 | { |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 245 | DataType data_type = a->info()->data_type(); |
| 246 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 247 | const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1); |
| 248 | const unsigned int n = b->info()->dimension(0); |
| 249 | const unsigned int k = a->info()->dimension(0); |
| 250 | const unsigned int batch_size = reinterpret_input_as_3d ? a->info()->dimension(3) : a->info()->dimension(2); |
| 251 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
| 252 | const GPUTarget gpu_target = CLScheduler::get().target(); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame^] | 253 | bool broadcast_bias = gemm_info.broadcast_bias(); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 254 | |
| 255 | // Set the target for the kernels |
| 256 | _mm_kernel.set_target(gpu_target); |
| 257 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame^] | 258 | GEMMReshapeInfo reshape_info(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d, broadcast_bias); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 259 | |
| 260 | // Manage intermediate buffers |
| 261 | if(!_reshape_b_only_on_first_run) |
| 262 | { |
| 263 | _memory_group.manage(&_tmp_b); |
| 264 | } |
| 265 | |
| 266 | GEMMLHSMatrixInfo lhs_info{}; |
| 267 | GEMMRHSMatrixInfo rhs_info{}; |
| 268 | |
| 269 | // Pick up the GEMM configuration |
| 270 | std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMReshapedOnlyRHSKernelConfigurationFactory::create(gpu_target); |
| 271 | ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get()); |
| 272 | |
| 273 | // Configure lhs_info and rhs_info |
| 274 | std::tie(lhs_info, rhs_info) = gemm_config->configure(m, n, k, batch_size, data_type); |
| 275 | |
| 276 | _reshape_rhs_kernel.configure(b, &_tmp_b, rhs_info); |
| 277 | |
| 278 | // Configure and tune matrix multiply kernel |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame^] | 279 | _mm_reshaped_only_rhs_kernel.configure(a, &_tmp_b, c, output, alpha, beta, lhs_info, rhs_info, reshape_info); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 280 | |
| 281 | if(!_reshape_b_only_on_first_run) |
| 282 | { |
| 283 | _tmp_b.allocator()->allocate(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | Status CLGEMM::validate_native(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] | 288 | { |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 289 | ARM_COMPUTE_UNUSED(alpha); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 290 | ARM_COMPUTE_UNUSED(output); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 291 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 292 | // Get the GPU target |
| 293 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 294 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 295 | const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1); |
| 296 | const unsigned int n = b->dimension(0); |
| 297 | const unsigned int k = a->dimension(0); |
| 298 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
| 299 | const bool add_c = (beta != 0.f && c != nullptr); |
| 300 | const bool is_beta_one = std::abs(1.0f - beta) < 0.00001f; |
| 301 | const bool fuse_add = is_beta_one && (c != nullptr && c->num_dimensions() == 1); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 302 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 303 | const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d); |
| 304 | |
| 305 | // Validate matrix multiply |
| 306 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyKernel::validate(a, b, (add_c && fuse_add) ? c : nullptr, output, alpha, beta, |
| 307 | false, reshape_info, gpu_target, gemm_info.fp_mixed_precision())); |
| 308 | |
| 309 | if(add_c && !fuse_add) |
| 310 | { |
| 311 | // Validate matrix addition kernel |
| 312 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta)); |
| 313 | } |
| 314 | |
| 315 | return Status{}; |
| 316 | } |
| 317 | |
| 318 | Status CLGEMM::validate_reshaped_v1(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info) |
| 319 | { |
| 320 | ARM_COMPUTE_UNUSED(alpha); |
| 321 | ARM_COMPUTE_UNUSED(output); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 322 | |
| 323 | TensorInfo tmp_a_info{}; |
| 324 | TensorInfo tmp_b_info{}; |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 325 | |
| 326 | // Get the GPU target |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 327 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 328 | const unsigned int m = gemm_info.reinterpret_input_as_3d() ? (a->dimension(1) * a->dimension(2)) : a->dimension(1); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 329 | const unsigned int n = b->dimension(0); |
| 330 | const unsigned int k = a->dimension(0); |
| 331 | int mult_transpose1xW_width = 1; |
| 332 | int mult_interleave4x4_height = 1; |
| 333 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 334 | const bool add_c = (beta != 0.f && c != nullptr); |
| 335 | const bool is_beta_one = std::abs(1.0f - beta) < 0.00001f; |
| 336 | const bool fuse_add = is_beta_one && (c != nullptr && c->num_dimensions() == 1); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 337 | |
| 338 | if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST) |
| 339 | { |
| 340 | mult_transpose1xW_width = 4; |
| 341 | mult_interleave4x4_height = 2; |
| 342 | } |
| 343 | |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 344 | GEMMRHSMatrixInfo rhs_info; |
| 345 | rhs_info.n0 = 16 / b->element_size(); |
| 346 | rhs_info.k0 = 1; |
| 347 | rhs_info.h0 = mult_transpose1xW_width; |
| 348 | rhs_info.interleave = false; |
| 349 | rhs_info.transpose = false; |
| 350 | |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 351 | GEMMLHSMatrixInfo lhs_info; |
| 352 | lhs_info.m0 = 4; |
| 353 | lhs_info.k0 = 4; |
| 354 | lhs_info.v0 = mult_interleave4x4_height; |
| 355 | lhs_info.interleave = true; |
| 356 | lhs_info.transpose = true; |
| 357 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 358 | const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d, false); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 359 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 360 | // Validate interleave kernel |
| 361 | 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()))); |
| 362 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeLHSMatrixKernel::validate(a, &tmp_a_info, lhs_info, gemm_info.reinterpret_input_as_3d())); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 363 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 364 | // Validate transpose kernel |
| 365 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info))); |
| 366 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info)); |
Michele Di Giorgio | ebc3a90 | 2018-11-16 16:04:25 +0000 | [diff] [blame] | 367 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 368 | // Validate matrix multiply |
| 369 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyKernel::validate(&tmp_a_info, &tmp_b_info, (add_c && fuse_add) ? c : nullptr, output, alpha, beta, |
| 370 | true, reshape_info, gpu_target, gemm_info.fp_mixed_precision())); |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 371 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 372 | if(add_c && !fuse_add) |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 373 | { |
| 374 | // Validate matrix addition kernel |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 375 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta)); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 376 | } |
| 377 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 378 | return Status{}; |
| 379 | } |
| 380 | |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 381 | Status CLGEMM::validate_reshaped_v2(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info) |
| 382 | { |
| 383 | ARM_COMPUTE_UNUSED(alpha); |
| 384 | ARM_COMPUTE_UNUSED(output); |
| 385 | |
| 386 | TensorInfo tmp_a_info{}; |
| 387 | TensorInfo tmp_b_info{}; |
| 388 | |
| 389 | // Get the GPU target |
| 390 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 391 | DataType data_type = a->data_type(); |
| 392 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 393 | const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1); |
| 394 | const unsigned int n = b->dimension(0); |
| 395 | const unsigned int k = a->dimension(0); |
| 396 | const unsigned int batch_size = reinterpret_input_as_3d ? a->dimension(3) : a->dimension(2); |
| 397 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
| 398 | const bool add_c = (beta != 0.f && c != nullptr); |
| 399 | |
| 400 | const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, false); |
| 401 | |
| 402 | GEMMLHSMatrixInfo lhs_info; |
| 403 | GEMMRHSMatrixInfo rhs_info; |
| 404 | |
| 405 | // Pick up the GEMM configuration |
| 406 | std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMReshapedKernelConfigurationFactory::create(gpu_target); |
| 407 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(gemm_config.get()); |
| 408 | |
| 409 | // Configure lhs_info and rhs_info |
| 410 | std::tie(lhs_info, rhs_info) = gemm_config->configure(m, n, k, batch_size, data_type); |
| 411 | |
| 412 | 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()))); |
| 413 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeLHSMatrixKernel::validate(a, &tmp_a_info, lhs_info, gemm_info.reinterpret_input_as_3d())); |
| 414 | |
| 415 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info))); |
| 416 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info)); |
| 417 | |
| 418 | // Validate matrix multiply |
| 419 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyReshapedKernel::validate(&tmp_a_info, &tmp_b_info, output, alpha, lhs_info, rhs_info, reshape_info)); |
| 420 | |
| 421 | if(add_c) |
| 422 | { |
| 423 | // Validate matrix addition kernel |
| 424 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta)); |
| 425 | } |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 426 | return Status{}; |
| 427 | } |
| 428 | |
| 429 | Status CLGEMM::validate_reshaped_only_rhs(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info) |
| 430 | { |
| 431 | ARM_COMPUTE_UNUSED(alpha); |
| 432 | ARM_COMPUTE_UNUSED(output); |
| 433 | |
| 434 | TensorInfo tmp_b_info{}; |
| 435 | |
| 436 | // Get the GPU target |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame^] | 437 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 438 | const DataType data_type = a->data_type(); |
| 439 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 440 | const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1); |
| 441 | const unsigned int n = b->dimension(0); |
| 442 | const unsigned int k = a->dimension(0); |
| 443 | const unsigned int batch_size = reinterpret_input_as_3d ? a->dimension(3) : a->dimension(2); |
| 444 | const int depth_output_gemm3d = gemm_info.depth_output_gemm3d(); |
| 445 | const bool broadcast_bias = gemm_info.broadcast_bias(); |
| 446 | const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d, broadcast_bias); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 447 | |
| 448 | GEMMLHSMatrixInfo lhs_info; |
| 449 | GEMMRHSMatrixInfo rhs_info; |
| 450 | |
| 451 | // Pick up the GEMM configuration |
| 452 | std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMReshapedOnlyRHSKernelConfigurationFactory::create(gpu_target); |
| 453 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(gemm_config.get()); |
| 454 | |
| 455 | // Configure lhs_info and rhs_info |
| 456 | std::tie(lhs_info, rhs_info) = gemm_config->configure(m, n, k, batch_size, data_type); |
| 457 | |
| 458 | auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info))); |
| 459 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info)); |
| 460 | |
| 461 | // Validate matrix multiply |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame^] | 462 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyReshapedOnlyRHSKernel::validate(a, &tmp_b_info, c, output, alpha, beta, lhs_info, rhs_info, reshape_info)); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 463 | |
| 464 | return Status{}; |
| 465 | } |
| 466 | |
| 467 | void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info) |
| 468 | { |
| 469 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
| 470 | |
| 471 | // Perform validation step |
| 472 | ARM_COMPUTE_ERROR_THROW_ON(validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info)); |
| 473 | |
| 474 | // Check if we need to reshape the matrix B only on the first run |
| 475 | _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run(); |
| 476 | _is_prepared = gemm_info.retain_internal_weights(); |
| 477 | _original_b = b; |
| 478 | |
| 479 | // Get the GPU target |
| 480 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 481 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 482 | const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1); |
| 483 | const unsigned int n = b->info()->dimension(0); |
| 484 | const unsigned int k = a->info()->dimension(0); |
| 485 | |
| 486 | // Select GEMMType |
| 487 | _gemm_type = select_gemm_type(m, n, k, a->info()->data_type(), _reshape_b_only_on_first_run, gpu_target); |
| 488 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame^] | 489 | const bool is_gemm_reshaped_only_rhs = _gemm_type == GEMMType::RESHAPED_ONLY_RHS; |
| 490 | const bool add_c = (beta != 0.f && c != nullptr); |
| 491 | const bool is_beta_one = std::abs(1.0f - beta) < 0.00001f; |
| 492 | const bool fuse_add = (is_beta_one && (c != nullptr && c->info()->num_dimensions() == 1)) || is_gemm_reshaped_only_rhs; |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 493 | |
| 494 | switch(_gemm_type) |
| 495 | { |
| 496 | case GEMMType::NATIVE: |
| 497 | { |
| 498 | configure_native(a, b, (add_c && fuse_add) ? c : nullptr, output, alpha, beta, gemm_info); |
| 499 | break; |
| 500 | } |
| 501 | case GEMMType::RESHAPED_V1: |
| 502 | { |
| 503 | configure_reshaped_v1(a, b, (add_c && fuse_add) ? c : nullptr, output, alpha, beta, gemm_info); |
| 504 | break; |
| 505 | } |
| 506 | case GEMMType::RESHAPED_V2: |
| 507 | { |
| 508 | configure_reshaped_v2(a, b, (add_c && fuse_add) ? c : nullptr, output, alpha, beta, gemm_info); |
| 509 | break; |
| 510 | } |
| 511 | case GEMMType::RESHAPED_ONLY_RHS: |
| 512 | { |
| 513 | configure_reshaped_only_rhs(a, b, (add_c && fuse_add) ? c : nullptr, output, alpha, beta, gemm_info); |
| 514 | break; |
| 515 | } |
| 516 | default: |
| 517 | { |
| 518 | ARM_COMPUTE_ERROR("GEMMType not supported"); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // Configure matrix addition kernel |
| 523 | if(add_c && !fuse_add) |
| 524 | { |
| 525 | _ma_kernel.configure(c, output, beta); |
| 526 | _run_addition = true; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info) |
| 531 | { |
| 532 | // Get the GPU target |
| 533 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 534 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 535 | const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1); |
| 536 | const unsigned int n = b->dimension(0); |
| 537 | const unsigned int k = a->dimension(0); |
| 538 | |
| 539 | // Select GEMMType |
| 540 | GEMMType gemm_type = select_gemm_type(m, n, k, a->data_type(), gemm_info.reshape_b_only_on_first_run(), gpu_target); |
| 541 | |
| 542 | switch(gemm_type) |
| 543 | { |
| 544 | case GEMMType::NATIVE: |
| 545 | { |
| 546 | ARM_COMPUTE_RETURN_ON_ERROR(validate_native(a, b, c, output, alpha, beta, gemm_info)); |
| 547 | break; |
| 548 | } |
| 549 | case GEMMType::RESHAPED_V1: |
| 550 | { |
| 551 | ARM_COMPUTE_RETURN_ON_ERROR(validate_reshaped_v1(a, b, c, output, alpha, beta, gemm_info)); |
| 552 | break; |
| 553 | } |
| 554 | case GEMMType::RESHAPED_V2: |
| 555 | { |
| 556 | ARM_COMPUTE_RETURN_ON_ERROR(validate_reshaped_v2(a, b, c, output, alpha, beta, gemm_info)); |
| 557 | break; |
| 558 | } |
| 559 | case GEMMType::RESHAPED_ONLY_RHS: |
| 560 | { |
| 561 | ARM_COMPUTE_RETURN_ON_ERROR(validate_reshaped_only_rhs(a, b, c, output, alpha, beta, gemm_info)); |
| 562 | break; |
| 563 | } |
| 564 | default: |
| 565 | { |
| 566 | ARM_COMPUTE_RETURN_ERROR_MSG("GEMMType not supported"); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return Status{}; |
| 571 | } |
| 572 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 573 | void CLGEMM::run() |
| 574 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 575 | prepare(); |
| 576 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 577 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 578 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 579 | // Run matrix multiply kernel |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 580 | switch(_gemm_type) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 581 | { |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 582 | case GEMMType::NATIVE: |
| 583 | { |
| 584 | CLScheduler::get().enqueue(_mm_kernel, !_run_addition); |
| 585 | break; |
| 586 | } |
| 587 | case GEMMType::RESHAPED_V1: |
| 588 | { |
| 589 | // Run interleave kernel |
| 590 | CLScheduler::get().enqueue(_reshape_lhs_kernel, false); |
| 591 | |
| 592 | if(!_reshape_b_only_on_first_run) |
| 593 | { |
| 594 | // Run transpose kernel |
| 595 | CLScheduler::get().enqueue(_reshape_rhs_kernel, false); |
| 596 | } |
| 597 | |
| 598 | CLScheduler::get().enqueue(_mm_kernel, !_run_addition); |
| 599 | break; |
| 600 | } |
| 601 | case GEMMType::RESHAPED_V2: |
| 602 | { |
| 603 | // Run interleave kernel |
| 604 | CLScheduler::get().enqueue(_reshape_lhs_kernel, false); |
| 605 | |
| 606 | if(!_reshape_b_only_on_first_run) |
| 607 | { |
| 608 | // Run transpose kernel |
| 609 | CLScheduler::get().enqueue(_reshape_rhs_kernel, false); |
| 610 | } |
| 611 | |
| 612 | CLScheduler::get().enqueue(_mm_reshaped_kernel, !_run_addition); |
| 613 | break; |
| 614 | } |
| 615 | case GEMMType::RESHAPED_ONLY_RHS: |
| 616 | { |
| 617 | if(!_reshape_b_only_on_first_run) |
| 618 | { |
| 619 | // Run transpose kernel |
| 620 | CLScheduler::get().enqueue(_reshape_rhs_kernel, false); |
| 621 | } |
| 622 | |
| 623 | CLScheduler::get().enqueue(_mm_reshaped_only_rhs_kernel, !_run_addition); |
| 624 | break; |
| 625 | } |
| 626 | default: |
| 627 | { |
| 628 | ARM_COMPUTE_ERROR("GEMMType not supported"); |
| 629 | } |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 630 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 631 | |
| 632 | // Run matrix addition kernel |
| 633 | if(_run_addition) |
| 634 | { |
| 635 | CLScheduler::get().enqueue(_ma_kernel); |
| 636 | } |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 637 | } |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame] | 638 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 639 | void CLGEMM::prepare() |
| 640 | { |
| 641 | if(!_is_prepared) |
| 642 | { |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 643 | if(_gemm_type != GEMMType::NATIVE && _reshape_b_only_on_first_run) |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 644 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 645 | // Run transpose kernel and mark original weights tensor as unused |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 646 | _tmp_b.allocator()->allocate(); |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 647 | CLScheduler::get().enqueue(_reshape_rhs_kernel, false); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 648 | _original_b->mark_as_unused(); |
| 649 | } |
| 650 | CLScheduler::get().queue().finish(); |
| 651 | _is_prepared = true; |
| 652 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 653 | } |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 654 | } // namespace arm_compute |