blob: 492709f0d08dccc6cb2df667ba3c1ae7b0fa13d1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
giuros011c9efeb2019-01-11 14:04:43 +00002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Iodice926afe12019-03-19 11:44:13 +000026#include "arm_compute/core/CL/ICLGEMMKernelConfiguration.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/ICLTensor.h"
Gian Marco Iodice926afe12019-03-19 11:44:13 +000028#include "arm_compute/core/CL/gemm/reshaped/CLGEMMReshapedKernelConfiguration.h"
29#include "arm_compute/core/CL/gemm/reshaped_only_rhs/CLGEMMReshapedOnlyRHSKernelConfiguration.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Error.h"
Gian Marco Iodice750641d2018-05-08 12:01:57 +010031#include "arm_compute/core/GPUTarget.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Types.h"
Gian Marco Iodicebb36a8e2018-04-19 12:05:08 +010035#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include "arm_compute/core/Validate.h"
Gian Marco Iodice750641d2018-05-08 12:01:57 +010037#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038#include "arm_compute/runtime/CL/CLScheduler.h"
39#include "arm_compute/runtime/ITensorAllocator.h"
40
giuros011c9efeb2019-01-11 14:04:43 +000041namespace arm_compute
42{
Gian Marco Iodice750641d2018-05-08 12:01:57 +010043using namespace arm_compute::misc::shape_calculator;
Gian Marco Iodice90313eb2019-01-16 15:40:25 +000044using namespace arm_compute::cl_gemm;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010046CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +010047 : _memory_group(std::move(memory_manager)),
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +010048 _mm_kernel(),
49 _ma_kernel(),
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000050 _reshape_lhs_kernel(),
51 _reshape_rhs_kernel(),
52 _mm_reshaped_kernel(),
Gian Marco Iodice926afe12019-03-19 11:44:13 +000053 _mm_reshaped_only_rhs_kernel(),
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +010054 _tmp_a(),
55 _tmp_b(),
56 _original_b(nullptr),
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +010057 _run_addition(false),
58 _reshape_b_only_on_first_run(false),
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000059 _is_prepared(false),
Gian Marco Iodice926afe12019-03-19 11:44:13 +000060 _gemm_type(GEMMType::NATIVE)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061{
62}
63
Gian Marco Iodice926afe12019-03-19 11:44:13 +000064CLGEMM::GEMMType CLGEMM::select_gemm_type(unsigned int m, unsigned int n, unsigned int k, DataType data_type, bool reshape_b_only_on_first_run, GPUTarget gpu_target)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065{
Gian Marco Iodice926afe12019-03-19 11:44:13 +000066 GEMMType gemm_type = GEMMType::RESHAPED_V1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067
Gian Marco Iodice926afe12019-03-19 11:44:13 +000068 if(gpu_target_is_in(gpu_target, GPUTarget::G52, GPUTarget::G52LIT, GPUTarget::G71, GPUTarget::G72, GPUTarget::G76))
69 {
70 if((m > 1) && (n < 16))
71 {
72 gemm_type = GEMMType::RESHAPED_V1;
73 }
74 else if((m == 1) && (data_type == DataType::F32))
75 {
76 gemm_type = GEMMType::RESHAPED_ONLY_RHS;
77 }
78 else
79 {
80 // COMPMID-852
81 if((k > 256) && (m > 4) && is_data_type_float(data_type) && reshape_b_only_on_first_run)
82 {
83 constexpr float alpha = 3.2f;
84 constexpr float fact0 = 1.51f;
85 constexpr float fact1 = 1.66f;
86 constexpr float ops = 12.0f;
87 const float scale = k > 1024 ? 1.07f : 1.0f;
88 gemm_type = (alpha + ((n * fact0) / ops) < ((fact1 * n * scale) / ops)) ? GEMMType::RESHAPED_V1 : GEMMType::NATIVE;
89 }
90 else
91 {
92 gemm_type = GEMMType::NATIVE;
93 }
94 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
Gian Marco Iodice926afe12019-03-19 11:44:13 +000096 const auto workload = static_cast<float>((m * n) / 20.0f);
Gian Marco Iodice1246b632017-08-16 18:38:32 +010097
Gian Marco Iodice926afe12019-03-19 11:44:13 +000098 gemm_type = ((workload > 1600.0f) && (gemm_type == GEMMType::RESHAPED_V1) && (data_type == DataType::F32)) ? GEMMType::RESHAPED_V2 : gemm_type;
99 }
100 else
101 {
102 // We reshape the matrices only if we do not have the vector-by-matrix case and we reshape the matrix B only once
103 gemm_type = ((m != 1) && reshape_b_only_on_first_run) ? GEMMType::RESHAPED_V1 : GEMMType::NATIVE;
104 }
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100105
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000106 return gemm_type;
107}
108
109void CLGEMM::configure_native(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
110{
111 const unsigned int m = gemm_info.reinterpret_input_as_3d() ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1);
112 const unsigned int n = b->info()->dimension(0);
113 const unsigned int k = a->info()->dimension(0);
114 const GPUTarget gpu_target = CLScheduler::get().target();
Gian Marco36a0a462018-01-12 10:21:40 +0000115
116 // Set the target for the kernels
Gian Marco36a0a462018-01-12 10:21:40 +0000117 _mm_kernel.set_target(gpu_target);
118
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000119 GEMMReshapeInfo reshape_info(m, n, k, 1, 1, gemm_info.depth_output_gemm3d(), gemm_info.reinterpret_input_as_3d());
120
121 // Configure and tune matrix multiply kernel
122 _mm_kernel.configure(a, b, c, output, alpha, beta, false, reshape_info, gemm_info.fp_mixed_precision());
123
124 // Tune kernel statically
125 CLScheduler::get().tune_kernel_static(_mm_kernel);
126}
127
128void CLGEMM::configure_reshaped_v1(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
129{
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000130 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
131 const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1);
132 const unsigned int n = b->info()->dimension(0);
133 const unsigned int k = a->info()->dimension(0);
134 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000135 const GPUTarget gpu_target = CLScheduler::get().target();
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000136 int mult_transpose1xW_width = 1;
137 int mult_interleave4x4_height = 1;
Gian Marco36a0a462018-01-12 10:21:40 +0000138
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000139 // Set the target for the kernels
140 _reshape_lhs_kernel.set_target(gpu_target);
141 _mm_kernel.set_target(gpu_target);
142
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100143 if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST)
Gian Marco36a0a462018-01-12 10:21:40 +0000144 {
145 mult_transpose1xW_width = 4;
146 mult_interleave4x4_height = 2;
147 }
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000148
giuros018b6b4a92018-12-18 19:01:33 +0000149 GEMMRHSMatrixInfo rhs_info;
150 rhs_info.n0 = 16 / b->info()->element_size();
151 rhs_info.k0 = 1;
152 rhs_info.h0 = mult_transpose1xW_width;
153 rhs_info.interleave = false;
154 rhs_info.transpose = false;
Gian Marco36a0a462018-01-12 10:21:40 +0000155
giuros011c9efeb2019-01-11 14:04:43 +0000156 GEMMLHSMatrixInfo lhs_info;
157 lhs_info.m0 = 4;
158 lhs_info.k0 = 4;
159 lhs_info.v0 = mult_interleave4x4_height;
160 lhs_info.interleave = true;
161 lhs_info.transpose = true;
162
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000163 GEMMReshapeInfo reshape_info(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d, false);
Gian Marcob5311a62017-12-13 12:48:03 +0000164
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000165 _memory_group.manage(&_tmp_a);
166 if(!_reshape_b_only_on_first_run)
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100167 {
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000168 _memory_group.manage(&_tmp_b);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100169 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000171 // Configure interleave kernel
172 _reshape_lhs_kernel.configure(a, &_tmp_a, lhs_info, reinterpret_input_as_3d);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100173
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000174 // Configure transpose kernel
175 _reshape_rhs_kernel.configure(b, &_tmp_b, rhs_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000177 // Configure and tune matrix multiply kernel
178 _mm_kernel.configure(&_tmp_a, &_tmp_b, c, output, alpha, beta, true, reshape_info, gemm_info.fp_mixed_precision());
179
180 CLScheduler::get().tune_kernel_static(_mm_kernel);
181
182 // Allocate intermediate tensors
183 _tmp_a.allocator()->allocate();
184 if(!_reshape_b_only_on_first_run)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 {
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000186 _tmp_b.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 }
188}
189
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000190void CLGEMM::configure_reshaped_v2(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
191{
192 ARM_COMPUTE_ERROR_ON(c != nullptr);
193 ARM_COMPUTE_UNUSED(beta);
194 ARM_COMPUTE_UNUSED(c);
195
196 DataType data_type = a->info()->data_type();
197 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
198 const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1);
199 const unsigned int n = b->info()->dimension(0);
200 const unsigned int k = a->info()->dimension(0);
201 const unsigned int batch_size = reinterpret_input_as_3d ? a->info()->dimension(3) : a->info()->dimension(2);
202 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
203 const GPUTarget gpu_target = CLScheduler::get().target();
204
205 // Set the target for the kernels
206 _reshape_lhs_kernel.set_target(gpu_target);
207 _mm_kernel.set_target(gpu_target);
208
giuros0146a49a02019-04-01 13:50:22 +0100209 GEMMReshapeInfo reshape_info(m, n, k, 1, 1, depth_output_gemm3d, false);
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000210
211 // Manage intermediate buffers
212 _memory_group.manage(&_tmp_a);
213 if(!_reshape_b_only_on_first_run)
214 {
215 _memory_group.manage(&_tmp_b);
216 }
217 // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel
218
219 GEMMLHSMatrixInfo lhs_info{};
220 GEMMRHSMatrixInfo rhs_info{};
221
222 // Pick up the GEMM configuration
223 std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMReshapedKernelConfigurationFactory::create(gpu_target);
224 ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
225
226 // Configure lhs_info and rhs_info
227 std::tie(lhs_info, rhs_info) = gemm_config->configure(m, n, k, batch_size, data_type);
228
229 _reshape_lhs_kernel.configure(a, &_tmp_a, lhs_info, gemm_info.reinterpret_input_as_3d());
230 _reshape_rhs_kernel.configure(b, &_tmp_b, rhs_info);
231
232 // Configure and tune matrix multiply kernel
233 _mm_reshaped_kernel.configure(&_tmp_a, &_tmp_b, output, alpha, lhs_info, rhs_info, reshape_info);
234
235 // Allocate intermediate tensors
236 _tmp_a.allocator()->allocate();
237 if(!_reshape_b_only_on_first_run)
238 {
239 _tmp_b.allocator()->allocate();
240 }
241}
242
243void CLGEMM::configure_reshaped_only_rhs(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
244{
245 ARM_COMPUTE_ERROR_ON(c != nullptr);
246 ARM_COMPUTE_UNUSED(beta);
247 ARM_COMPUTE_UNUSED(c);
248
249 DataType data_type = a->info()->data_type();
250 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
251 const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1);
252 const unsigned int n = b->info()->dimension(0);
253 const unsigned int k = a->info()->dimension(0);
254 const unsigned int batch_size = reinterpret_input_as_3d ? a->info()->dimension(3) : a->info()->dimension(2);
255 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
256 const GPUTarget gpu_target = CLScheduler::get().target();
257
258 // Set the target for the kernels
259 _mm_kernel.set_target(gpu_target);
260
261 GEMMReshapeInfo reshape_info(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d);
262
263 // Manage intermediate buffers
264 if(!_reshape_b_only_on_first_run)
265 {
266 _memory_group.manage(&_tmp_b);
267 }
268
269 GEMMLHSMatrixInfo lhs_info{};
270 GEMMRHSMatrixInfo rhs_info{};
271
272 // Pick up the GEMM configuration
273 std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMReshapedOnlyRHSKernelConfigurationFactory::create(gpu_target);
274 ARM_COMPUTE_ERROR_ON_NULLPTR(gemm_config.get());
275
276 // Configure lhs_info and rhs_info
277 std::tie(lhs_info, rhs_info) = gemm_config->configure(m, n, k, batch_size, data_type);
278
279 _reshape_rhs_kernel.configure(b, &_tmp_b, rhs_info);
280
281 // Configure and tune matrix multiply kernel
282 _mm_reshaped_only_rhs_kernel.configure(a, &_tmp_b, output, alpha, lhs_info, rhs_info, reshape_info);
283
284 if(!_reshape_b_only_on_first_run)
285 {
286 _tmp_b.allocator()->allocate();
287 }
288}
289
290Status CLGEMM::validate_native(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000291{
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100292 ARM_COMPUTE_UNUSED(alpha);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100293 ARM_COMPUTE_UNUSED(output);
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100294
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000295 // Get the GPU target
296 const GPUTarget gpu_target = CLScheduler::get().target();
297 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
298 const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1);
299 const unsigned int n = b->dimension(0);
300 const unsigned int k = a->dimension(0);
301 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
302 const bool add_c = (beta != 0.f && c != nullptr);
303 const bool is_beta_one = std::abs(1.0f - beta) < 0.00001f;
304 const bool fuse_add = is_beta_one && (c != nullptr && c->num_dimensions() == 1);
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100305
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000306 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d);
307
308 // Validate matrix multiply
309 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyKernel::validate(a, b, (add_c && fuse_add) ? c : nullptr, output, alpha, beta,
310 false, reshape_info, gpu_target, gemm_info.fp_mixed_precision()));
311
312 if(add_c && !fuse_add)
313 {
314 // Validate matrix addition kernel
315 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta));
316 }
317
318 return Status{};
319}
320
321Status CLGEMM::validate_reshaped_v1(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
322{
323 ARM_COMPUTE_UNUSED(alpha);
324 ARM_COMPUTE_UNUSED(output);
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100325
326 TensorInfo tmp_a_info{};
327 TensorInfo tmp_b_info{};
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100328
329 // Get the GPU target
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000330 const GPUTarget gpu_target = CLScheduler::get().target();
331 const unsigned int m = gemm_info.reinterpret_input_as_3d() ? (a->dimension(1) * a->dimension(2)) : a->dimension(1);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000332 const unsigned int n = b->dimension(0);
333 const unsigned int k = a->dimension(0);
334 int mult_transpose1xW_width = 1;
335 int mult_interleave4x4_height = 1;
336 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000337 const bool add_c = (beta != 0.f && c != nullptr);
338 const bool is_beta_one = std::abs(1.0f - beta) < 0.00001f;
339 const bool fuse_add = is_beta_one && (c != nullptr && c->num_dimensions() == 1);
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100340
341 if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST)
342 {
343 mult_transpose1xW_width = 4;
344 mult_interleave4x4_height = 2;
345 }
346
giuros018b6b4a92018-12-18 19:01:33 +0000347 GEMMRHSMatrixInfo rhs_info;
348 rhs_info.n0 = 16 / b->element_size();
349 rhs_info.k0 = 1;
350 rhs_info.h0 = mult_transpose1xW_width;
351 rhs_info.interleave = false;
352 rhs_info.transpose = false;
353
giuros011c9efeb2019-01-11 14:04:43 +0000354 GEMMLHSMatrixInfo lhs_info;
355 lhs_info.m0 = 4;
356 lhs_info.k0 = 4;
357 lhs_info.v0 = mult_interleave4x4_height;
358 lhs_info.interleave = true;
359 lhs_info.transpose = true;
360
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000361 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d, false);
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100362
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000363 // Validate interleave kernel
364 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())));
365 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeLHSMatrixKernel::validate(a, &tmp_a_info, lhs_info, gemm_info.reinterpret_input_as_3d()));
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000366
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000367 // Validate transpose kernel
368 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info)));
369 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info));
Michele Di Giorgioebc3a902018-11-16 16:04:25 +0000370
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000371 // Validate matrix multiply
372 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyKernel::validate(&tmp_a_info, &tmp_b_info, (add_c && fuse_add) ? c : nullptr, output, alpha, beta,
373 true, reshape_info, gpu_target, gemm_info.fp_mixed_precision()));
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100374
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000375 if(add_c && !fuse_add)
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100376 {
377 // Validate matrix addition kernel
Giorgio Arena0f170392018-07-18 16:13:12 +0100378 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta));
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100379 }
380
Georgios Pinitas78c00902018-01-09 17:33:11 +0000381 return Status{};
382}
383
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000384Status CLGEMM::validate_reshaped_v2(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
385{
386 ARM_COMPUTE_UNUSED(alpha);
387 ARM_COMPUTE_UNUSED(output);
388
389 TensorInfo tmp_a_info{};
390 TensorInfo tmp_b_info{};
391
392 // Get the GPU target
393 const GPUTarget gpu_target = CLScheduler::get().target();
394 DataType data_type = a->data_type();
395 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
396 const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1);
397 const unsigned int n = b->dimension(0);
398 const unsigned int k = a->dimension(0);
399 const unsigned int batch_size = reinterpret_input_as_3d ? a->dimension(3) : a->dimension(2);
400 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
401 const bool add_c = (beta != 0.f && c != nullptr);
402
403 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, false);
404
405 GEMMLHSMatrixInfo lhs_info;
406 GEMMRHSMatrixInfo rhs_info;
407
408 // Pick up the GEMM configuration
409 std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMReshapedKernelConfigurationFactory::create(gpu_target);
410 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(gemm_config.get());
411
412 // Configure lhs_info and rhs_info
413 std::tie(lhs_info, rhs_info) = gemm_config->configure(m, n, k, batch_size, data_type);
414
415 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())));
416 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeLHSMatrixKernel::validate(a, &tmp_a_info, lhs_info, gemm_info.reinterpret_input_as_3d()));
417
418 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info)));
419 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info));
420
421 // Validate matrix multiply
422 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyReshapedKernel::validate(&tmp_a_info, &tmp_b_info, output, alpha, lhs_info, rhs_info, reshape_info));
423
424 if(add_c)
425 {
426 // Validate matrix addition kernel
427 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta));
428 }
429
430 return Status{};
431}
432
433Status 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)
434{
435 ARM_COMPUTE_UNUSED(alpha);
436 ARM_COMPUTE_UNUSED(output);
437
438 TensorInfo tmp_b_info{};
439
440 // Get the GPU target
441 const GPUTarget gpu_target = CLScheduler::get().target();
442 const DataType data_type = a->data_type();
443 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
444 const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1);
445 const unsigned int n = b->dimension(0);
446 const unsigned int k = a->dimension(0);
447 const unsigned int batch_size = reinterpret_input_as_3d ? a->dimension(3) : a->dimension(2);
448 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
449 const bool add_c = (beta != 0.f && c != nullptr);
450
451 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d);
452
453 GEMMLHSMatrixInfo lhs_info;
454 GEMMRHSMatrixInfo rhs_info;
455
456 // Pick up the GEMM configuration
457 std::unique_ptr<ICLGEMMKernelConfiguration> gemm_config = CLGEMMReshapedOnlyRHSKernelConfigurationFactory::create(gpu_target);
458 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(gemm_config.get());
459
460 // Configure lhs_info and rhs_info
461 std::tie(lhs_info, rhs_info) = gemm_config->configure(m, n, k, batch_size, data_type);
462
463 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info)));
464 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info));
465
466 // Validate matrix multiply
467 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyReshapedOnlyRHSKernel::validate(a, &tmp_b_info, output, alpha, lhs_info, rhs_info, reshape_info));
468
469 if(add_c)
470 {
471 // Validate matrix addition kernel
472 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta));
473 }
474
475 return Status{};
476}
477
478void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
479{
480 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
481
482 // Perform validation step
483 ARM_COMPUTE_ERROR_THROW_ON(validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info));
484
485 // Check if we need to reshape the matrix B only on the first run
486 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
487 _is_prepared = gemm_info.retain_internal_weights();
488 _original_b = b;
489
490 // Get the GPU target
491 const GPUTarget gpu_target = CLScheduler::get().target();
492 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
493 const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1);
494 const unsigned int n = b->info()->dimension(0);
495 const unsigned int k = a->info()->dimension(0);
496
497 // Select GEMMType
498 _gemm_type = select_gemm_type(m, n, k, a->info()->data_type(), _reshape_b_only_on_first_run, gpu_target);
499
500 const bool is_gemm_v2 = (_gemm_type == GEMMType::RESHAPED_V2) || (_gemm_type == GEMMType::RESHAPED_ONLY_RHS);
501 const bool add_c = (beta != 0.f && c != nullptr);
502 const bool is_beta_one = std::abs(1.0f - beta) < 0.00001f;
503 const bool fuse_add = is_beta_one && (c != nullptr && c->info()->num_dimensions() == 1) && !is_gemm_v2;
504
505 switch(_gemm_type)
506 {
507 case GEMMType::NATIVE:
508 {
509 configure_native(a, b, (add_c && fuse_add) ? c : nullptr, output, alpha, beta, gemm_info);
510 break;
511 }
512 case GEMMType::RESHAPED_V1:
513 {
514 configure_reshaped_v1(a, b, (add_c && fuse_add) ? c : nullptr, output, alpha, beta, gemm_info);
515 break;
516 }
517 case GEMMType::RESHAPED_V2:
518 {
519 configure_reshaped_v2(a, b, (add_c && fuse_add) ? c : nullptr, output, alpha, beta, gemm_info);
520 break;
521 }
522 case GEMMType::RESHAPED_ONLY_RHS:
523 {
524 configure_reshaped_only_rhs(a, b, (add_c && fuse_add) ? c : nullptr, output, alpha, beta, gemm_info);
525 break;
526 }
527 default:
528 {
529 ARM_COMPUTE_ERROR("GEMMType not supported");
530 }
531 }
532
533 // Configure matrix addition kernel
534 if(add_c && !fuse_add)
535 {
536 _ma_kernel.configure(c, output, beta);
537 _run_addition = true;
538 }
539}
540
541Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
542{
543 // Get the GPU target
544 const GPUTarget gpu_target = CLScheduler::get().target();
545 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
546 const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1);
547 const unsigned int n = b->dimension(0);
548 const unsigned int k = a->dimension(0);
549
550 // Select GEMMType
551 GEMMType gemm_type = select_gemm_type(m, n, k, a->data_type(), gemm_info.reshape_b_only_on_first_run(), gpu_target);
552
553 switch(gemm_type)
554 {
555 case GEMMType::NATIVE:
556 {
557 ARM_COMPUTE_RETURN_ON_ERROR(validate_native(a, b, c, output, alpha, beta, gemm_info));
558 break;
559 }
560 case GEMMType::RESHAPED_V1:
561 {
562 ARM_COMPUTE_RETURN_ON_ERROR(validate_reshaped_v1(a, b, c, output, alpha, beta, gemm_info));
563 break;
564 }
565 case GEMMType::RESHAPED_V2:
566 {
567 ARM_COMPUTE_RETURN_ON_ERROR(validate_reshaped_v2(a, b, c, output, alpha, beta, gemm_info));
568 break;
569 }
570 case GEMMType::RESHAPED_ONLY_RHS:
571 {
572 ARM_COMPUTE_RETURN_ON_ERROR(validate_reshaped_only_rhs(a, b, c, output, alpha, beta, gemm_info));
573 break;
574 }
575 default:
576 {
577 ARM_COMPUTE_RETURN_ERROR_MSG("GEMMType not supported");
578 }
579 }
580
581 return Status{};
582}
583
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100584void CLGEMM::run()
585{
Georgios Pinitase0437672018-05-02 14:07:55 +0100586 prepare();
587
Georgios Pinitasda953f22019-04-02 17:27:03 +0100588 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100589
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100590 // Run matrix multiply kernel
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000591 switch(_gemm_type)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000592 {
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000593 case GEMMType::NATIVE:
594 {
595 CLScheduler::get().enqueue(_mm_kernel, !_run_addition);
596 break;
597 }
598 case GEMMType::RESHAPED_V1:
599 {
600 // Run interleave kernel
601 CLScheduler::get().enqueue(_reshape_lhs_kernel, false);
602
603 if(!_reshape_b_only_on_first_run)
604 {
605 // Run transpose kernel
606 CLScheduler::get().enqueue(_reshape_rhs_kernel, false);
607 }
608
609 CLScheduler::get().enqueue(_mm_kernel, !_run_addition);
610 break;
611 }
612 case GEMMType::RESHAPED_V2:
613 {
614 // Run interleave kernel
615 CLScheduler::get().enqueue(_reshape_lhs_kernel, false);
616
617 if(!_reshape_b_only_on_first_run)
618 {
619 // Run transpose kernel
620 CLScheduler::get().enqueue(_reshape_rhs_kernel, false);
621 }
622
623 CLScheduler::get().enqueue(_mm_reshaped_kernel, !_run_addition);
624 break;
625 }
626 case GEMMType::RESHAPED_ONLY_RHS:
627 {
628 if(!_reshape_b_only_on_first_run)
629 {
630 // Run transpose kernel
631 CLScheduler::get().enqueue(_reshape_rhs_kernel, false);
632 }
633
634 CLScheduler::get().enqueue(_mm_reshaped_only_rhs_kernel, !_run_addition);
635 break;
636 }
637 default:
638 {
639 ARM_COMPUTE_ERROR("GEMMType not supported");
640 }
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000641 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100642
643 // Run matrix addition kernel
644 if(_run_addition)
645 {
646 CLScheduler::get().enqueue(_ma_kernel);
647 }
Georgios Pinitase0437672018-05-02 14:07:55 +0100648}
Georgios Pinitas82b51482018-04-24 15:14:12 +0100649
Georgios Pinitase0437672018-05-02 14:07:55 +0100650void CLGEMM::prepare()
651{
652 if(!_is_prepared)
653 {
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000654 if(_gemm_type != GEMMType::NATIVE && _reshape_b_only_on_first_run)
Georgios Pinitase0437672018-05-02 14:07:55 +0100655 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100656 // Run transpose kernel and mark original weights tensor as unused
Georgios Pinitase0437672018-05-02 14:07:55 +0100657 _tmp_b.allocator()->allocate();
giuros018b6b4a92018-12-18 19:01:33 +0000658 CLScheduler::get().enqueue(_reshape_rhs_kernel, false);
Georgios Pinitase0437672018-05-02 14:07:55 +0100659 _original_b->mark_as_unused();
660 }
661 CLScheduler::get().queue().finish();
662 _is_prepared = true;
663 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100664}
giuros011c9efeb2019-01-11 14:04:43 +0000665} // namespace arm_compute