blob: f81da6c0a5379e1fbdc25ffb7ed7ad082a4bc073 [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"
27#include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h"
28#include "arm_compute/core/CL/kernels/CLGEMMMatrixAdditionKernel.h"
29#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
30#include "arm_compute/core/CL/kernels/CLGEMMTranspose1xWKernel.h"
31#include "arm_compute/core/Error.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010032#include "arm_compute/core/GPUTarget.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000033#include "arm_compute/core/Helpers.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Types.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010036#include "arm_compute/core/Utils.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000037#include "arm_compute/core/Validate.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010038#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000039#include "arm_compute/runtime/CL/CLScheduler.h"
40#include "arm_compute/runtime/ITensorAllocator.h"
41
42using namespace arm_compute;
Jenkinsb3a371b2018-05-23 11:36:53 +010043using namespace arm_compute::misc::shape_calculator;
Anthony Barbier871448e2017-03-24 14:54:29 +000044
Anthony Barbier06ea0482018-02-22 15:45:35 +000045namespace
46{
47inline bool is_interleaved_transposed(int m, int n, int k, DataType data_type, bool reshape_b_only_on_first_run, GPUTarget gpu_target)
48{
49 bool flag = true;
50
Jenkinsb3a371b2018-05-23 11:36:53 +010051 if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX))
Anthony Barbier06ea0482018-02-22 15:45:35 +000052 {
Jenkinsb3a371b2018-05-23 11:36:53 +010053 // COMPMID-852
54 if(k > 256 && m > 4 && is_data_type_float(data_type) && reshape_b_only_on_first_run)
Anthony Barbier06ea0482018-02-22 15:45:35 +000055 {
56 const float scale = k < 1024 ? 2.0f : 2.5f;
57 flag = (scale * n) > ((1.66f * n) + 38.4f);
58 }
59 else
60 {
61 flag = false;
62 }
63 }
Jenkinsb3a371b2018-05-23 11:36:53 +010064 else
Anthony Barbier06ea0482018-02-22 15:45:35 +000065 {
Jenkinsb3a371b2018-05-23 11:36:53 +010066 // We reshape the matrices only if we do not have the vector-by-matrix case and we reshape the matrix B only once
67 flag = m != 1 && reshape_b_only_on_first_run;
Anthony Barbier06ea0482018-02-22 15:45:35 +000068 }
69
Jenkinsb3a371b2018-05-23 11:36:53 +010070 return flag;
Anthony Barbier06ea0482018-02-22 15:45:35 +000071}
72} // namespace
73
Kaizen8938bd32017-09-28 14:38:23 +010074CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Jenkinsb3a371b2018-05-23 11:36:53 +010075 : _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),
76 _run_addition(false), _reshape_b_only_on_first_run(false), _is_prepared(false)
Anthony Barbier871448e2017-03-24 14:54:29 +000077{
78}
79
Anthony Barbierf45d5a92018-01-24 16:23:15 +000080void 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 +000081{
Anthony Barbier06ea0482018-02-22 15:45:35 +000082 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Anthony Barbier871448e2017-03-24 14:54:29 +000083
Anthony Barbier06ea0482018-02-22 15:45:35 +000084 // Perform validation step
Jenkinsb3a371b2018-05-23 11:36:53 +010085 ARM_COMPUTE_ERROR_THROW_ON(validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info));
86
87 // Store original b matrix
88 _original_b = b;
Anthony Barbierf45d5a92018-01-24 16:23:15 +000089
90 // Check if we need to reshape the matrix B only on the first run
91 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Jenkinsb3a371b2018-05-23 11:36:53 +010092 _is_prepared = false;
Kaizen8938bd32017-09-28 14:38:23 +010093
94 const ICLTensor *matrix_a = a;
95 const ICLTensor *matrix_b = b;
96
Anthony Barbier06ea0482018-02-22 15:45:35 +000097 // Get the GPU target
98 const GPUTarget gpu_target = CLScheduler::get().target();
99
100 // Set the target for the kernels
101 _interleave_kernel.set_target(gpu_target);
102 _mm_kernel.set_target(gpu_target);
103
104 // Arguments used by GEMMReshapeInfo
105 // 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
106 // in order to know how the matrices have been reshaped
107 const int m = a->info()->dimension(1);
108 const int n = b->info()->dimension(0);
109 const int k = a->info()->dimension(0);
110 int mult_transpose1xW_width = 1;
111 int mult_interleave4x4_height = 1;
112
Jenkinsb3a371b2018-05-23 11:36:53 +0100113 if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000114 {
115 mult_transpose1xW_width = 4;
116 mult_interleave4x4_height = 2;
117 }
118
119 // Check if we need to reshape the matrix A and matrix B
120 _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 +0000121
Kaizen8938bd32017-09-28 14:38:23 +0100122 if(_is_interleaved_transposed)
Anthony Barbier871448e2017-03-24 14:54:29 +0000123 {
Kaizen8938bd32017-09-28 14:38:23 +0100124 matrix_a = &_tmp_a;
125 matrix_b = &_tmp_b;
Anthony Barbier871448e2017-03-24 14:54:29 +0000126
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000127 // Manage intermediate buffers
128 _memory_group.manage(&_tmp_a);
Jenkinsb3a371b2018-05-23 11:36:53 +0100129 if(!_reshape_b_only_on_first_run)
130 {
131 _memory_group.manage(&_tmp_b);
132 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000133 // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel
134
135 // Configure interleave kernel
136 _interleave_kernel.configure(a, &_tmp_a, mult_interleave4x4_height);
137
138 // Configure transpose kernel
139 _transpose_kernel.configure(b, &_tmp_b, mult_transpose1xW_width);
Kaizen8938bd32017-09-28 14:38:23 +0100140 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000141
Anthony Barbier06ea0482018-02-22 15:45:35 +0000142 _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height));
Kaizen8938bd32017-09-28 14:38:23 +0100143
144 if(_is_interleaved_transposed)
145 {
Anthony Barbier871448e2017-03-24 14:54:29 +0000146 // Allocate intermediate tensors
147 _tmp_a.allocator()->allocate();
Jenkinsb3a371b2018-05-23 11:36:53 +0100148 if(!_reshape_b_only_on_first_run)
149 {
150 _tmp_b.allocator()->allocate();
151 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000152 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000153
154 // Configure matrix addition kernel
155 if(beta != 0 && c != nullptr)
156 {
157 _ma_kernel.configure(c, output, beta);
158 _run_addition = true;
159 }
160}
161
Jenkinsb3a371b2018-05-23 11:36:53 +0100162Status 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 +0000163{
Jenkinsb3a371b2018-05-23 11:36:53 +0100164 ARM_COMPUTE_UNUSED(alpha);
165
166 // Check if we need to reshape the matrix B only on the first run
167 const bool reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
168
169 const ITensorInfo *matrix_a_info = a;
170 const ITensorInfo *matrix_b_info = b;
171
172 TensorInfo tmp_a_info{};
173 TensorInfo tmp_b_info{};
174 TensorInfo tmp_output_info = *output->clone();
175
176 // Get the GPU target
177 const GPUTarget gpu_target = CLScheduler::get().target();
178
179 // Arguments used by GEMMReshapeInfo
180 // 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
181 // in order to know how the matrices have been reshaped
182 const int m = a->dimension(1);
183 const int n = b->dimension(0);
184 const int k = a->dimension(0);
185 int mult_transpose1xW_width = 1;
186 int mult_interleave4x4_height = 1;
187
188 if(get_arch_from_target(gpu_target) == GPUTarget::BIFROST)
189 {
190 mult_transpose1xW_width = 4;
191 mult_interleave4x4_height = 2;
192 }
193
194 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height);
195
196 // Check if we need to reshape the matrix A and matrix B
197 const bool run_interleave_transpose = is_interleaved_transposed(m, n, k, a->data_type(), reshape_b_only_on_first_run, gpu_target);
198
199 if(run_interleave_transpose)
200 {
201 matrix_a_info = &tmp_a_info;
202 matrix_b_info = &tmp_b_info;
203
204 // Validate interleave kernel
205 auto_init_if_empty(tmp_a_info, a->clone()->set_tensor_shape(compute_interleaved_shape(*a, mult_interleave4x4_height)));
206 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMInterleave4x4Kernel::validate(a, &tmp_a_info, mult_interleave4x4_height));
207
208 // Validate transpose kernel
209 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width)));
210 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMTranspose1xWKernel::validate(b, &tmp_b_info, mult_transpose1xW_width));
211 }
212
213 // Validate matrix multiply
214 auto_init_if_empty(tmp_output_info, matrix_a_info->clone()->set_tensor_shape(compute_mm_shape(*matrix_a_info, *matrix_b_info, run_interleave_transpose, reshape_info)));
215 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &tmp_output_info, alpha, run_interleave_transpose, reshape_info, gpu_target));
216
217 if(beta != 0 && c != nullptr)
218 {
219 // Validate matrix addition kernel
220 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAdditionKernel::validate(c, &tmp_output_info, beta));
221 }
222
Anthony Barbier06ea0482018-02-22 15:45:35 +0000223 return Status{};
224}
225
Anthony Barbier871448e2017-03-24 14:54:29 +0000226void CLGEMM::run()
227{
Jenkinsb3a371b2018-05-23 11:36:53 +0100228 prepare();
229
Kaizen8938bd32017-09-28 14:38:23 +0100230 _memory_group.acquire();
231
232 if(_is_interleaved_transposed)
Anthony Barbier871448e2017-03-24 14:54:29 +0000233 {
234 // Run interleave kernel
235 CLScheduler::get().enqueue(_interleave_kernel, false);
236
Jenkinsb3a371b2018-05-23 11:36:53 +0100237 if(!_reshape_b_only_on_first_run)
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000238 {
239 // Run transpose kernel
240 CLScheduler::get().enqueue(_transpose_kernel, false);
241 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000242 }
243
244 // Run matrix multiply kernel
245 CLScheduler::get().enqueue(_mm_kernel, !_run_addition);
246
247 // Run matrix addition kernel
248 if(_run_addition)
249 {
250 CLScheduler::get().enqueue(_ma_kernel);
251 }
Kaizen8938bd32017-09-28 14:38:23 +0100252
253 _memory_group.release();
Anthony Barbier871448e2017-03-24 14:54:29 +0000254}
Jenkinsb3a371b2018-05-23 11:36:53 +0100255
256void CLGEMM::prepare()
257{
258 if(!_is_prepared)
259 {
260 if(_is_interleaved_transposed && _reshape_b_only_on_first_run)
261 {
262 // Run transpose kernel
263 _tmp_b.allocator()->allocate();
264 CLScheduler::get().enqueue(_transpose_kernel, false);
265 _original_b->mark_as_unused();
266 }
267 CLScheduler::get().queue().finish();
268 _is_prepared = true;
269 }
270}