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