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