blob: f16d1c0cdae21783233d1963a6160d94468760b6 [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
Anthony Barbierf45d5a92018-01-24 16:23:15 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier871448e2017-03-24 14:54:29 +00003 *
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
26#include "arm_compute/core/CL/ICLTensor.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000027#include "arm_compute/core/Error.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010028#include "arm_compute/core/GPUTarget.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010032#include "arm_compute/core/Utils.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000033#include "arm_compute/core/Validate.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000035#include "arm_compute/runtime/CL/CLScheduler.h"
36#include "arm_compute/runtime/ITensorAllocator.h"
37
38using namespace arm_compute;
Jenkinsb3a371b2018-05-23 11:36:53 +010039using namespace arm_compute::misc::shape_calculator;
Anthony Barbier871448e2017-03-24 14:54:29 +000040
Anthony Barbier06ea0482018-02-22 15:45:35 +000041namespace
42{
43inline bool is_interleaved_transposed(int m, int n, int k, DataType data_type, bool reshape_b_only_on_first_run, GPUTarget gpu_target)
44{
45 bool flag = true;
46
Jenkins52ba29e2018-08-29 15:32:11 +000047 if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G76))
Anthony Barbier06ea0482018-02-22 15:45:35 +000048 {
Jenkinsb3a371b2018-05-23 11:36:53 +010049 if(k > 256 && m > 4 && is_data_type_float(data_type) && reshape_b_only_on_first_run)
Anthony Barbier06ea0482018-02-22 15:45:35 +000050 {
Jenkins52ba29e2018-08-29 15:32:11 +000051 constexpr float alpha = 3.2f;
52 constexpr float fact0 = 1.51f;
53 constexpr float fact1 = 1.66f;
54 constexpr float ops = 12.0f;
55 const float scale = k > 1024 ? 1.07f : 1.0f;
56 flag = alpha + ((n * fact0) / ops) < ((fact1 * n * scale) / ops);
Anthony Barbier06ea0482018-02-22 15:45:35 +000057 }
58 else
59 {
60 flag = false;
61 }
62 }
Jenkinsb3a371b2018-05-23 11:36:53 +010063 else
Anthony Barbier06ea0482018-02-22 15:45:35 +000064 {
Jenkinsb3a371b2018-05-23 11:36:53 +010065 // We reshape the matrices only if we do not have the vector-by-matrix case and we reshape the matrix B only once
66 flag = m != 1 && reshape_b_only_on_first_run;
Anthony Barbier06ea0482018-02-22 15:45:35 +000067 }
68
Jenkinsb3a371b2018-05-23 11:36:53 +010069 return flag;
Anthony Barbier06ea0482018-02-22 15:45:35 +000070}
71} // namespace
72
Kaizen8938bd32017-09-28 14:38:23 +010073CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Jenkinsb3a371b2018-05-23 11:36:53 +010074 : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _ma_kernel(), _tmp_a(), _tmp_b(), _original_b(nullptr), _is_interleaved_transposed(false),
75 _run_addition(false), _reshape_b_only_on_first_run(false), _is_prepared(false)
Anthony Barbier871448e2017-03-24 14:54:29 +000076{
77}
78
Anthony Barbierf45d5a92018-01-24 16:23:15 +000079void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbier871448e2017-03-24 14:54:29 +000080{
Anthony Barbier06ea0482018-02-22 15:45:35 +000081 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Anthony Barbier871448e2017-03-24 14:54:29 +000082
Anthony Barbier06ea0482018-02-22 15:45:35 +000083 // Perform validation step
Jenkinsb3a371b2018-05-23 11:36:53 +010084 ARM_COMPUTE_ERROR_THROW_ON(validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info));
85
Anthony Barbierf45d5a92018-01-24 16:23:15 +000086 // Check if we need to reshape the matrix B only on the first run
87 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Jenkins52ba29e2018-08-29 15:32:11 +000088 _is_prepared = gemm_info.retain_internal_weights();
89 _original_b = b;
Kaizen8938bd32017-09-28 14:38:23 +010090
91 const ICLTensor *matrix_a = a;
92 const ICLTensor *matrix_b = b;
93
Anthony Barbier06ea0482018-02-22 15:45:35 +000094 // Get the GPU target
95 const GPUTarget gpu_target = CLScheduler::get().target();
96
97 // Set the target for the kernels
98 _interleave_kernel.set_target(gpu_target);
99 _mm_kernel.set_target(gpu_target);
100
101 // Arguments used by GEMMReshapeInfo
102 // If we pass the matrix A and matrix B reshaped to CLGEMMMatrixMultiplyKernel, we need to pass m, n, k, mult_transpose1xW_width and mult_interleave4x4_height to CLGEMMReshapeInfo
103 // in order to know how the matrices have been reshaped
Jenkins52ba29e2018-08-29 15:32:11 +0000104 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
105 const int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000106 const int n = b->info()->dimension(0);
107 const int k = a->info()->dimension(0);
Jenkins52ba29e2018-08-29 15:32:11 +0000108 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000109 int mult_transpose1xW_width = 1;
110 int mult_interleave4x4_height = 1;
111
Jenkinsb3a371b2018-05-23 11:36:53 +0100112 if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000113 {
114 mult_transpose1xW_width = 4;
115 mult_interleave4x4_height = 2;
116 }
117
118 // Check if we need to reshape the matrix A and matrix B
119 _is_interleaved_transposed = is_interleaved_transposed(m, n, k, a->info()->data_type(), _reshape_b_only_on_first_run, gpu_target);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000120
Jenkins52ba29e2018-08-29 15:32:11 +0000121 // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D
122 if(_is_interleaved_transposed)
123 {
124 reinterpret_input_as_3d = false;
125 }
126
Kaizen8938bd32017-09-28 14:38:23 +0100127 if(_is_interleaved_transposed)
Anthony Barbier871448e2017-03-24 14:54:29 +0000128 {
Kaizen8938bd32017-09-28 14:38:23 +0100129 matrix_a = &_tmp_a;
130 matrix_b = &_tmp_b;
Anthony Barbier871448e2017-03-24 14:54:29 +0000131
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000132 // Manage intermediate buffers
133 _memory_group.manage(&_tmp_a);
Jenkinsb3a371b2018-05-23 11:36:53 +0100134 if(!_reshape_b_only_on_first_run)
135 {
136 _memory_group.manage(&_tmp_b);
137 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000138 // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel
139
140 // Configure interleave kernel
Jenkins52ba29e2018-08-29 15:32:11 +0000141 _interleave_kernel.configure(a, &_tmp_a, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d());
Anthony Barbier06ea0482018-02-22 15:45:35 +0000142
143 // Configure transpose kernel
144 _transpose_kernel.configure(b, &_tmp_b, mult_transpose1xW_width);
Kaizen8938bd32017-09-28 14:38:23 +0100145 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000146
Jenkins52ba29e2018-08-29 15:32:11 +0000147 // Configure and tune matrix multiply kernel
148 _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d,
149 reinterpret_input_as_3d));
150 CLScheduler::get().tune_kernel_static(_mm_kernel);
Kaizen8938bd32017-09-28 14:38:23 +0100151
152 if(_is_interleaved_transposed)
153 {
Anthony Barbier871448e2017-03-24 14:54:29 +0000154 // Allocate intermediate tensors
155 _tmp_a.allocator()->allocate();
Jenkinsb3a371b2018-05-23 11:36:53 +0100156 if(!_reshape_b_only_on_first_run)
157 {
158 _tmp_b.allocator()->allocate();
159 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000160 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000161
162 // Configure matrix addition kernel
163 if(beta != 0 && c != nullptr)
164 {
165 _ma_kernel.configure(c, output, beta);
166 _run_addition = true;
167 }
168}
169
Jenkinsb3a371b2018-05-23 11:36:53 +0100170Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000171{
Jenkinsb3a371b2018-05-23 11:36:53 +0100172 ARM_COMPUTE_UNUSED(alpha);
Jenkins52ba29e2018-08-29 15:32:11 +0000173 ARM_COMPUTE_UNUSED(output);
Jenkinsb3a371b2018-05-23 11:36:53 +0100174
175 // Check if we need to reshape the matrix B only on the first run
176 const bool reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
177
178 const ITensorInfo *matrix_a_info = a;
179 const ITensorInfo *matrix_b_info = b;
180
181 TensorInfo tmp_a_info{};
182 TensorInfo tmp_b_info{};
Jenkinsb3a371b2018-05-23 11:36:53 +0100183
184 // Get the GPU target
185 const GPUTarget gpu_target = CLScheduler::get().target();
186
187 // Arguments used by GEMMReshapeInfo
188 // If we pass the matrix A and matrix B reshaped to CLGEMMMatrixMultiplyKernel, we need to pass m, n, k, mult_transpose1xW_width and mult_interleave4x4_height to CLGEMMReshapeInfo
189 // in order to know how the matrices have been reshaped
Jenkins52ba29e2018-08-29 15:32:11 +0000190 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
191 const int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1);
Jenkinsb3a371b2018-05-23 11:36:53 +0100192 const int n = b->dimension(0);
193 const int k = a->dimension(0);
194 int mult_transpose1xW_width = 1;
195 int mult_interleave4x4_height = 1;
Jenkins52ba29e2018-08-29 15:32:11 +0000196 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Jenkinsb3a371b2018-05-23 11:36:53 +0100197
198 if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST)
199 {
200 mult_transpose1xW_width = 4;
201 mult_interleave4x4_height = 2;
202 }
203
Jenkinsb3a371b2018-05-23 11:36:53 +0100204 // Check if we need to reshape the matrix A and matrix B
205 const bool run_interleave_transpose = is_interleaved_transposed(m, n, k, a->data_type(), reshape_b_only_on_first_run, gpu_target);
206
Jenkins52ba29e2018-08-29 15:32:11 +0000207 // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D
208 if(run_interleave_transpose)
209 {
210 reinterpret_input_as_3d = false;
211 }
212
213 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, depth_output_gemm3d, reinterpret_input_as_3d);
214
Jenkinsb3a371b2018-05-23 11:36:53 +0100215 if(run_interleave_transpose)
216 {
217 matrix_a_info = &tmp_a_info;
218 matrix_b_info = &tmp_b_info;
219
220 // Validate interleave kernel
Jenkins52ba29e2018-08-29 15:32:11 +0000221 auto_init_if_empty(tmp_a_info, a->clone()->set_tensor_shape(compute_interleaved_shape(*a, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d())));
222 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMInterleave4x4Kernel::validate(a, &tmp_a_info, mult_interleave4x4_height, gemm_info.reinterpret_input_as_3d()));
Jenkinsb3a371b2018-05-23 11:36:53 +0100223
224 // Validate transpose kernel
225 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width)));
226 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMTranspose1xWKernel::validate(b, &tmp_b_info, mult_transpose1xW_width));
227 }
228
229 // Validate matrix multiply
Jenkins52ba29e2018-08-29 15:32:11 +0000230 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output, alpha, run_interleave_transpose, reshape_info, gpu_target));
Jenkinsb3a371b2018-05-23 11:36:53 +0100231
232 if(beta != 0 && c != nullptr)
233 {
234 // Validate matrix addition kernel
Jenkins52ba29e2018-08-29 15:32:11 +0000235 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, output, beta));
Jenkinsb3a371b2018-05-23 11:36:53 +0100236 }
237
Anthony Barbier06ea0482018-02-22 15:45:35 +0000238 return Status{};
239}
240
Anthony Barbier871448e2017-03-24 14:54:29 +0000241void CLGEMM::run()
242{
Jenkinsb3a371b2018-05-23 11:36:53 +0100243 prepare();
244
Kaizen8938bd32017-09-28 14:38:23 +0100245 _memory_group.acquire();
246
247 if(_is_interleaved_transposed)
Anthony Barbier871448e2017-03-24 14:54:29 +0000248 {
249 // Run interleave kernel
250 CLScheduler::get().enqueue(_interleave_kernel, false);
251
Jenkinsb3a371b2018-05-23 11:36:53 +0100252 if(!_reshape_b_only_on_first_run)
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000253 {
254 // Run transpose kernel
255 CLScheduler::get().enqueue(_transpose_kernel, false);
256 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000257 }
258
259 // Run matrix multiply kernel
260 CLScheduler::get().enqueue(_mm_kernel, !_run_addition);
261
262 // Run matrix addition kernel
263 if(_run_addition)
264 {
265 CLScheduler::get().enqueue(_ma_kernel);
266 }
Kaizen8938bd32017-09-28 14:38:23 +0100267
268 _memory_group.release();
Anthony Barbier871448e2017-03-24 14:54:29 +0000269}
Jenkinsb3a371b2018-05-23 11:36:53 +0100270
271void CLGEMM::prepare()
272{
273 if(!_is_prepared)
274 {
275 if(_is_interleaved_transposed && _reshape_b_only_on_first_run)
276 {
Jenkins52ba29e2018-08-29 15:32:11 +0000277 // Run transpose kernel and mark original weights tensor as unused
Jenkinsb3a371b2018-05-23 11:36:53 +0100278 _tmp_b.allocator()->allocate();
279 CLScheduler::get().enqueue(_transpose_kernel, false);
280 _original_b->mark_as_unused();
281 }
282 CLScheduler::get().queue().finish();
283 _is_prepared = true;
284 }
285}