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