blob: 94dc0e071cbc3e4218ed5dc6f350482a4474570d [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Gian Marco7b4d5472018-01-10 15:56:30 +00002 * Copyright (c) 2017-2018 ARM Limited.
Gian Marco05288a22017-11-21 10:57:50 +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/CLGEMMLowpMatrixMultiplyCore.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco05288a22017-11-21 10:57:50 +000033#include "arm_compute/runtime/CL/CLScheduler.h"
34
35using namespace arm_compute;
Georgios Pinitas358ca202017-12-07 16:47:52 +000036using namespace arm_compute::misc::shape_calculator;
Gian Marco05288a22017-11-21 10:57:50 +000037
Gian Marco19835e52018-01-30 13:35:54 +000038namespace
39{
40inline bool is_interleaved_transposed(int m, int n, int k, bool reshape_b_only_on_first_run, GPUTarget gpu_target)
41{
42 bool flag = true;
43
Sam Laynton56e8e862018-04-05 13:26:08 +010044 if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX))
Gian Marco19835e52018-01-30 13:35:54 +000045 {
46 // COMPMID-852
47 if(k > 256 && m > 4 && reshape_b_only_on_first_run)
48 {
49 flag = ((0.72f + n * 0.10766f) < (n * 0.1284f));
50 }
51 else
52 {
53 flag = false;
54 }
55 }
56
57 return flag;
58}
59} // namespace
60
Gian Marco05288a22017-11-21 10:57:50 +000061CLGEMMLowpMatrixMultiplyCore::CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitas72219332018-06-05 14:56:06 +010062 : _memory_group(std::move(memory_manager)),
63 _mm_kernel(),
64 _mtx_a_reshape_kernel(),
65 _mtx_b_reshape_kernel(),
66 _mtx_a_reduction_kernel(),
67 _mtx_b_reduction_kernel(),
68 _offset_contribution_kernel(),
69 _vector_sum_col(),
70 _vector_sum_row(),
71 _tmp_a(),
72 _tmp_b(),
73 _original_b(nullptr),
74 _a_offset(0),
75 _b_offset(0),
76 _is_interleaved_transposed(true),
77 _reshape_b_only_on_first_run(false),
78 _is_prepared(false)
Gian Marco05288a22017-11-21 10:57:50 +000079{
80}
81
Chunosov5124be52017-11-22 20:42:13 +070082void CLGEMMLowpMatrixMultiplyCore::configure(const ICLTensor *a, const ICLTensor *b, ICLTensor *output, const GEMMInfo &gemm_info)
Gian Marco05288a22017-11-21 10:57:50 +000083{
Georgios Pinitas358ca202017-12-07 16:47:52 +000084 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
85 ARM_COMPUTE_UNUSED(gemm_info);
86 ARM_COMPUTE_ERROR_THROW_ON(CLGEMMLowpMatrixMultiplyCore::validate(a->info(), b->info(), output->info(), gemm_info));
Gian Marco05288a22017-11-21 10:57:50 +000087
Georgios Pinitas72219332018-06-05 14:56:06 +010088 _is_prepared = false;
89 _original_b = b;
Chunosov5124be52017-11-22 20:42:13 +070090 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
91 _a_offset = a->info()->quantization_info().offset;
92 _b_offset = b->info()->quantization_info().offset;
Gian Marco05288a22017-11-21 10:57:50 +000093
Gian Marco19835e52018-01-30 13:35:54 +000094 // Get the GPU target
95 const GPUTarget gpu_target = CLScheduler::get().target();
Gian Marco7b4d5472018-01-10 15:56:30 +000096
Gian Marco19835e52018-01-30 13:35:54 +000097 // Set the target for the kernels
98 _mtx_a_reshape_kernel.set_target(gpu_target);
99 _mm_kernel.set_target(gpu_target);
Gian Marco05288a22017-11-21 10:57:50 +0000100
101 const ICLTensor *matrix_a = a;
102 const ICLTensor *matrix_b = b;
103
Gian Marco19835e52018-01-30 13:35:54 +0000104 // 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 constexpr int mult_transpose1xW_width = 1;
111 constexpr int mult_interleave4x4_height = 1;
112
113 // Check if we need to reshape the matrix A and matrix B
114 _is_interleaved_transposed = is_interleaved_transposed(m, n, k, _reshape_b_only_on_first_run, gpu_target);
115
Gian Marco05288a22017-11-21 10:57:50 +0000116 if(_is_interleaved_transposed)
117 {
118 matrix_a = &_tmp_a;
119 matrix_b = &_tmp_b;
120
Gian Marco05288a22017-11-21 10:57:50 +0000121 _memory_group.manage(&_tmp_a);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100122 if(!_reshape_b_only_on_first_run)
123 {
124 _memory_group.manage(&_tmp_b);
125 }
Gian Marco05288a22017-11-21 10:57:50 +0000126
127 // Configure interleave kernel
Gian Marco19835e52018-01-30 13:35:54 +0000128 _mtx_a_reshape_kernel.configure(a, &_tmp_a, mult_interleave4x4_height);
Gian Marco05288a22017-11-21 10:57:50 +0000129
130 // Configure transpose kernel
Gian Marco19835e52018-01-30 13:35:54 +0000131 _mtx_b_reshape_kernel.configure(b, &_tmp_b, mult_transpose1xW_width);
Gian Marco05288a22017-11-21 10:57:50 +0000132 }
133
134 // Configure matrix multiply kernel
Gian Marco19835e52018-01-30 13:35:54 +0000135 _mm_kernel.configure(matrix_a, matrix_b, output, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height));
Gian Marco05288a22017-11-21 10:57:50 +0000136
137 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
138 if(_a_offset != 0)
139 {
Georgios Pinitas358ca202017-12-07 16:47:52 +0000140 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
Gian Marco05288a22017-11-21 10:57:50 +0000141 _vector_sum_col.allocator()->init(info_vector_sum_col);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100142 if(!_reshape_b_only_on_first_run)
143 {
144 _memory_group.manage(&_vector_sum_col);
145 }
Gian Marco05288a22017-11-21 10:57:50 +0000146
147 // Configure Matrix B reduction kernel
148 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col);
149 }
150
151 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
152 if(_b_offset != 0)
153 {
Georgios Pinitas358ca202017-12-07 16:47:52 +0000154 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a->info()), 1, DataType::S32);
Gian Marco05288a22017-11-21 10:57:50 +0000155 _vector_sum_row.allocator()->init(info_vector_sum_row);
156 _memory_group.manage(&_vector_sum_row);
157
158 // Configure matrix A reduction kernel
159 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row);
160 }
161
162 // Configure offset contribution kernel
163 _offset_contribution_kernel.configure(output, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, a->info()->dimension(0), _a_offset, _b_offset);
164
165 // Allocate tensors
166 if(_is_interleaved_transposed)
167 {
168 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100169 if(!_reshape_b_only_on_first_run)
170 {
171 _tmp_b.allocator()->allocate();
172 }
Gian Marco05288a22017-11-21 10:57:50 +0000173 }
174
Georgios Pinitas72219332018-06-05 14:56:06 +0100175 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Gian Marco05288a22017-11-21 10:57:50 +0000176 {
177 _vector_sum_col.allocator()->allocate();
178 }
179
180 if(_b_offset != 0)
181 {
182 _vector_sum_row.allocator()->allocate();
183 }
184}
185
Georgios Pinitas358ca202017-12-07 16:47:52 +0000186Status CLGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *output, const GEMMInfo &gemm_info)
187{
188 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
189 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
190 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
191 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1),
192 "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
193 ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(1) != (output)->dimension(1),
194 "The output matrix must have the same number of rows as the matrix A");
195 ARM_COMPUTE_RETURN_ERROR_ON_MSG((b)->dimension(0) != (output)->dimension(0),
196 "The output matrix must have the same number of columns as the matrix B");
197 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
198 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
199
Gian Marco19835e52018-01-30 13:35:54 +0000200 int32_t a_offset = a->quantization_info().offset;
201 int32_t b_offset = b->quantization_info().offset;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000202
Gian Marco19835e52018-01-30 13:35:54 +0000203 const int m = a->dimension(1);
204 const int n = b->dimension(0);
205 const int k = a->dimension(0);
206 constexpr int mult_transpose1xW_width = 1;
207 constexpr int mult_interleave4x4_height = 1;
208 const GEMMReshapeInfo reshape_info(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height);
209
210 bool reshape_matrices = is_interleaved_transposed(m, n, k, gemm_info.reshape_b_only_on_first_run(), CLScheduler::get().target());
211
212 if(reshape_matrices)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000213 {
Gian Marco19835e52018-01-30 13:35:54 +0000214 TensorInfo info_a(compute_interleaved_shape(*a, mult_interleave4x4_height), 1, a->data_type());
215 TensorInfo info_b(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width), 1, b->data_type());
Georgios Pinitas358ca202017-12-07 16:47:52 +0000216
Gian Marco19835e52018-01-30 13:35:54 +0000217 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMInterleave4x4Kernel::validate(a, &info_a, mult_interleave4x4_height));
218 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMTranspose1xWKernel::validate(b, &info_b, mult_transpose1xW_width));
219 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(&info_a, &info_b, output, reshape_matrices, reshape_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000220 }
221 else
222 {
Gian Marco19835e52018-01-30 13:35:54 +0000223 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(a, b, output, reshape_matrices, reshape_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000224 }
225
226 TensorInfo info_vector_sum_col, info_vector_sum_row;
227
228 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
229 if(a_offset != 0)
230 {
231 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
232
233 // Configure Matrix B reduction kernel
234 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col));
235 }
236
237 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
238 if(b_offset != 0)
239 {
240 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
241
242 // Configure matrix A reduction kernel
243 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row));
244 }
245
246 // Validate offset contribution kernel
247 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionKernel::validate(output,
248 a_offset == 0 ? nullptr : &info_vector_sum_col,
249 b_offset == 0 ? nullptr : &info_vector_sum_row,
250 a_offset, b_offset));
251
252 return Status{};
253}
254
Gian Marco05288a22017-11-21 10:57:50 +0000255void CLGEMMLowpMatrixMultiplyCore::run()
256{
Georgios Pinitas72219332018-06-05 14:56:06 +0100257 prepare();
258
Gian Marco05288a22017-11-21 10:57:50 +0000259 _memory_group.acquire();
260
261 if(_is_interleaved_transposed)
262 {
263 // Run reshape matrix A
264 CLScheduler::get().enqueue(_mtx_a_reshape_kernel, false);
265
Georgios Pinitas72219332018-06-05 14:56:06 +0100266 if(!_reshape_b_only_on_first_run)
Chunosov5124be52017-11-22 20:42:13 +0700267 {
268 // Run reshape matrix B
269 CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false);
270 }
271 }
272
Georgios Pinitas72219332018-06-05 14:56:06 +0100273 // Run matrix B reduction kernel only if _a_offset is not equal to 0
274 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Chunosov5124be52017-11-22 20:42:13 +0700275 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100276 CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false);
Gian Marco05288a22017-11-21 10:57:50 +0000277 }
278
279 // Run matrix multiply
280 CLScheduler::get().enqueue(_mm_kernel, false);
281
282 // Run matrix A reduction kernel only if _b_offset is not equal to 0
283 if(_b_offset != 0)
284 {
285 CLScheduler::get().enqueue(_mtx_a_reduction_kernel, false);
286 }
287
Gian Marco05288a22017-11-21 10:57:50 +0000288 // Run offset contribution kernel
289 CLScheduler::get().enqueue(_offset_contribution_kernel, true);
290
291 _memory_group.release();
Georgios Pinitas72219332018-06-05 14:56:06 +0100292}
Chunosov5124be52017-11-22 20:42:13 +0700293
Georgios Pinitas72219332018-06-05 14:56:06 +0100294void CLGEMMLowpMatrixMultiplyCore::prepare()
295{
296 if(!_is_prepared)
297 {
298 if(_is_interleaved_transposed && _reshape_b_only_on_first_run)
299 {
300 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
301
302 // Run reshape kernel and mark original weights tensor as unused
303 _tmp_b.allocator()->allocate();
304 CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false);
305 _original_b->mark_as_unused();
306 }
307
308 // Run matrix B reduction kernel only if _a_offset is not equal to 0
309 if(_a_offset != 0 && _reshape_b_only_on_first_run)
310 {
311 _vector_sum_col.allocator()->allocate();
312 CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false);
313 }
314
315 CLScheduler::get().queue().finish();
316 _is_prepared = true;
317 }
Gian Marco05288a22017-11-21 10:57:50 +0000318}