blob: 7aeade1e3e67fe44f4e47d6e6ca7a73f3970c1c0 [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
Georgios Pinitasa34286e2018-09-04 12:18:50 +010044 if(gpu_target_is_in(gpu_target,
45 GPUTarget::G71, GPUTarget::G72, GPUTarget::G76,
46 GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT,
47 GPUTarget::G52, GPUTarget::G52LIT))
Gian Marco19835e52018-01-30 13:35:54 +000048 {
49 // COMPMID-852
50 if(k > 256 && m > 4 && reshape_b_only_on_first_run)
51 {
52 flag = ((0.72f + n * 0.10766f) < (n * 0.1284f));
53 }
54 else
55 {
56 flag = false;
57 }
58 }
59
60 return flag;
61}
62} // namespace
63
Gian Marco05288a22017-11-21 10:57:50 +000064CLGEMMLowpMatrixMultiplyCore::CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitas72219332018-06-05 14:56:06 +010065 : _memory_group(std::move(memory_manager)),
66 _mm_kernel(),
67 _mtx_a_reshape_kernel(),
68 _mtx_b_reshape_kernel(),
69 _mtx_a_reduction_kernel(),
70 _mtx_b_reduction_kernel(),
71 _offset_contribution_kernel(),
72 _vector_sum_col(),
73 _vector_sum_row(),
74 _tmp_a(),
75 _tmp_b(),
76 _original_b(nullptr),
77 _a_offset(0),
78 _b_offset(0),
79 _is_interleaved_transposed(true),
80 _reshape_b_only_on_first_run(false),
81 _is_prepared(false)
Gian Marco05288a22017-11-21 10:57:50 +000082{
83}
84
Chunosov5124be52017-11-22 20:42:13 +070085void CLGEMMLowpMatrixMultiplyCore::configure(const ICLTensor *a, const ICLTensor *b, ICLTensor *output, const GEMMInfo &gemm_info)
Gian Marco05288a22017-11-21 10:57:50 +000086{
Georgios Pinitas358ca202017-12-07 16:47:52 +000087 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
88 ARM_COMPUTE_UNUSED(gemm_info);
89 ARM_COMPUTE_ERROR_THROW_ON(CLGEMMLowpMatrixMultiplyCore::validate(a->info(), b->info(), output->info(), gemm_info));
Gian Marco05288a22017-11-21 10:57:50 +000090
Georgios Pinitas72219332018-06-05 14:56:06 +010091 _is_prepared = false;
92 _original_b = b;
Chunosov5124be52017-11-22 20:42:13 +070093 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
94 _a_offset = a->info()->quantization_info().offset;
95 _b_offset = b->info()->quantization_info().offset;
Gian Marco05288a22017-11-21 10:57:50 +000096
Gian Marco19835e52018-01-30 13:35:54 +000097 // Get the GPU target
98 const GPUTarget gpu_target = CLScheduler::get().target();
Gian Marco7b4d5472018-01-10 15:56:30 +000099
Gian Marco19835e52018-01-30 13:35:54 +0000100 // Set the target for the kernels
101 _mtx_a_reshape_kernel.set_target(gpu_target);
102 _mm_kernel.set_target(gpu_target);
Gian Marco05288a22017-11-21 10:57:50 +0000103
104 const ICLTensor *matrix_a = a;
105 const ICLTensor *matrix_b = b;
106
Gian Marco19835e52018-01-30 13:35:54 +0000107 // Arguments used by GEMMReshapeInfo
108 // 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
109 // in order to know how the matrices have been reshaped
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100110 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
Isabella Gottardif02e5272018-10-01 12:26:28 +0100111 bool reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d() != 1);
112 int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1);
Gian Marco19835e52018-01-30 13:35:54 +0000113 const int n = b->info()->dimension(0);
114 const int k = a->info()->dimension(0);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100115 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Gian Marco19835e52018-01-30 13:35:54 +0000116 constexpr int mult_transpose1xW_width = 1;
117 constexpr int mult_interleave4x4_height = 1;
118
Isabella Gottardif02e5272018-10-01 12:26:28 +0100119 // In case both input and output have to be reinterpreted as 3D tensors,
120 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
121 if(reinterpret_input_as_3d == reinterpret_output_as_3d)
122 {
123 reinterpret_input_as_3d = false;
124 reinterpret_output_as_3d = false;
125 }
126
Gian Marco19835e52018-01-30 13:35:54 +0000127 // Check if we need to reshape the matrix A and matrix B
128 _is_interleaved_transposed = is_interleaved_transposed(m, n, k, _reshape_b_only_on_first_run, gpu_target);
129
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100130 if(_is_interleaved_transposed)
131 {
Isabella Gottardif02e5272018-10-01 12:26:28 +0100132 // if _is_interleaved_transposed is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100133 reinterpret_input_as_3d = false;
Isabella Gottardif02e5272018-10-01 12:26:28 +0100134 m = a->info()->dimension(1);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100135
Gian Marco05288a22017-11-21 10:57:50 +0000136 matrix_a = &_tmp_a;
137 matrix_b = &_tmp_b;
138
Gian Marco05288a22017-11-21 10:57:50 +0000139 _memory_group.manage(&_tmp_a);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100140 if(!_reshape_b_only_on_first_run)
141 {
142 _memory_group.manage(&_tmp_b);
143 }
Gian Marco05288a22017-11-21 10:57:50 +0000144
145 // Configure interleave kernel
Isabella Gottardif02e5272018-10-01 12:26:28 +0100146 _mtx_a_reshape_kernel.configure(a, &_tmp_a, mult_interleave4x4_height, reinterpret_input_as_3d);
Gian Marco05288a22017-11-21 10:57:50 +0000147
148 // Configure transpose kernel
Gian Marco19835e52018-01-30 13:35:54 +0000149 _mtx_b_reshape_kernel.configure(b, &_tmp_b, mult_transpose1xW_width);
Gian Marco05288a22017-11-21 10:57:50 +0000150 }
Gian Marco05288a22017-11-21 10:57:50 +0000151 // Configure matrix multiply kernel
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100152 _mm_kernel.configure(matrix_a, matrix_b, output, _is_interleaved_transposed, GEMMReshapeInfo(m, n, k,
153 mult_transpose1xW_width, mult_interleave4x4_height,
Isabella Gottardif02e5272018-10-01 12:26:28 +0100154 reinterpret_output_as_3d ? depth_output_gemm3d : 1, reinterpret_input_as_3d));
Gian Marco05288a22017-11-21 10:57:50 +0000155
156 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
157 if(_a_offset != 0)
158 {
Georgios Pinitas358ca202017-12-07 16:47:52 +0000159 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
Gian Marco05288a22017-11-21 10:57:50 +0000160 _vector_sum_col.allocator()->init(info_vector_sum_col);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100161 if(!_reshape_b_only_on_first_run)
162 {
163 _memory_group.manage(&_vector_sum_col);
164 }
Gian Marco05288a22017-11-21 10:57:50 +0000165
166 // Configure Matrix B reduction kernel
167 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col);
168 }
169
170 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
171 if(_b_offset != 0)
172 {
Georgios Pinitas358ca202017-12-07 16:47:52 +0000173 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a->info()), 1, DataType::S32);
Gian Marco05288a22017-11-21 10:57:50 +0000174 _vector_sum_row.allocator()->init(info_vector_sum_row);
175 _memory_group.manage(&_vector_sum_row);
176
177 // Configure matrix A reduction kernel
178 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row);
179 }
180
181 // Configure offset contribution kernel
182 _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);
183
184 // Allocate tensors
185 if(_is_interleaved_transposed)
186 {
187 _tmp_a.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100188 if(!_reshape_b_only_on_first_run)
189 {
190 _tmp_b.allocator()->allocate();
191 }
Gian Marco05288a22017-11-21 10:57:50 +0000192 }
193
Georgios Pinitas72219332018-06-05 14:56:06 +0100194 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Gian Marco05288a22017-11-21 10:57:50 +0000195 {
196 _vector_sum_col.allocator()->allocate();
197 }
198
199 if(_b_offset != 0)
200 {
201 _vector_sum_row.allocator()->allocate();
202 }
203}
204
Georgios Pinitas358ca202017-12-07 16:47:52 +0000205Status CLGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *output, const GEMMInfo &gemm_info)
206{
207 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
208 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
209 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000210 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
211 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
212
Gian Marco19835e52018-01-30 13:35:54 +0000213 int32_t a_offset = a->quantization_info().offset;
214 int32_t b_offset = b->quantization_info().offset;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000215
Georgios Pinitas932491f2018-09-21 16:33:15 +0100216 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
Isabella Gottardif02e5272018-10-01 12:26:28 +0100217 bool reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d() != 1);
218 int m = a->dimension(1);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100219 const int n = b->dimension(0);
220 const int k = a->dimension(0);
221 constexpr int mult_transpose1xW_width = 1;
222 constexpr int mult_interleave4x4_height = 1;
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100223 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Gian Marco19835e52018-01-30 13:35:54 +0000224
Isabella Gottardif02e5272018-10-01 12:26:28 +0100225 // In case both input and output have to be reinterpreted as 3D tensors,
226 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
227 if(reinterpret_input_as_3d == reinterpret_output_as_3d)
228 {
229 reinterpret_input_as_3d = false;
230 reinterpret_output_as_3d = false;
231 }
232
Gian Marco19835e52018-01-30 13:35:54 +0000233 bool reshape_matrices = is_interleaved_transposed(m, n, k, gemm_info.reshape_b_only_on_first_run(), CLScheduler::get().target());
234
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100235 // if reshape_matrices is set, force reinterpret_input_as_3d to be false as the output of CLGEMMInterleaveKernel will be 2D
236 if(reshape_matrices)
237 {
238 reinterpret_input_as_3d = false;
239 }
240
Isabella Gottardif02e5272018-10-01 12:26:28 +0100241 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, mult_transpose1xW_width, mult_interleave4x4_height, reinterpret_output_as_3d ? depth_output_gemm3d : 1, reinterpret_input_as_3d);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100242
Gian Marco19835e52018-01-30 13:35:54 +0000243 if(reshape_matrices)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000244 {
Isabella Gottardif02e5272018-10-01 12:26:28 +0100245 TensorInfo info_a(compute_interleaved_shape(*a, mult_interleave4x4_height, reinterpret_input_as_3d), 1, a->data_type());
Gian Marco19835e52018-01-30 13:35:54 +0000246 TensorInfo info_b(compute_transpose1xW_with_element_size_shape(*b, mult_transpose1xW_width), 1, b->data_type());
Georgios Pinitas358ca202017-12-07 16:47:52 +0000247
Isabella Gottardif02e5272018-10-01 12:26:28 +0100248 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMInterleave4x4Kernel::validate(a, &info_a, mult_interleave4x4_height, reinterpret_input_as_3d));
Gian Marco19835e52018-01-30 13:35:54 +0000249 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMTranspose1xWKernel::validate(b, &info_b, mult_transpose1xW_width));
250 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(&info_a, &info_b, output, reshape_matrices, reshape_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000251 }
252 else
253 {
Gian Marco19835e52018-01-30 13:35:54 +0000254 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(a, b, output, reshape_matrices, reshape_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000255 }
256
257 TensorInfo info_vector_sum_col, info_vector_sum_row;
258
259 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
260 if(a_offset != 0)
261 {
262 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
263
264 // Configure Matrix B reduction kernel
265 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col));
266 }
267
268 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
269 if(b_offset != 0)
270 {
271 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
272
273 // Configure matrix A reduction kernel
274 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row));
275 }
276
277 // Validate offset contribution kernel
278 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionKernel::validate(output,
279 a_offset == 0 ? nullptr : &info_vector_sum_col,
280 b_offset == 0 ? nullptr : &info_vector_sum_row,
281 a_offset, b_offset));
282
283 return Status{};
284}
285
Gian Marco05288a22017-11-21 10:57:50 +0000286void CLGEMMLowpMatrixMultiplyCore::run()
287{
Georgios Pinitas72219332018-06-05 14:56:06 +0100288 prepare();
289
Gian Marco05288a22017-11-21 10:57:50 +0000290 _memory_group.acquire();
291
292 if(_is_interleaved_transposed)
293 {
294 // Run reshape matrix A
295 CLScheduler::get().enqueue(_mtx_a_reshape_kernel, false);
296
Georgios Pinitas72219332018-06-05 14:56:06 +0100297 if(!_reshape_b_only_on_first_run)
Chunosov5124be52017-11-22 20:42:13 +0700298 {
299 // Run reshape matrix B
300 CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false);
301 }
302 }
303
Georgios Pinitas72219332018-06-05 14:56:06 +0100304 // Run matrix B reduction kernel only if _a_offset is not equal to 0
305 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Chunosov5124be52017-11-22 20:42:13 +0700306 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100307 CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false);
Gian Marco05288a22017-11-21 10:57:50 +0000308 }
309
310 // Run matrix multiply
311 CLScheduler::get().enqueue(_mm_kernel, false);
312
313 // Run matrix A reduction kernel only if _b_offset is not equal to 0
314 if(_b_offset != 0)
315 {
316 CLScheduler::get().enqueue(_mtx_a_reduction_kernel, false);
317 }
318
Gian Marco05288a22017-11-21 10:57:50 +0000319 // Run offset contribution kernel
320 CLScheduler::get().enqueue(_offset_contribution_kernel, true);
321
322 _memory_group.release();
Georgios Pinitas72219332018-06-05 14:56:06 +0100323}
Chunosov5124be52017-11-22 20:42:13 +0700324
Georgios Pinitas72219332018-06-05 14:56:06 +0100325void CLGEMMLowpMatrixMultiplyCore::prepare()
326{
327 if(!_is_prepared)
328 {
329 if(_is_interleaved_transposed && _reshape_b_only_on_first_run)
330 {
331 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
332
333 // Run reshape kernel and mark original weights tensor as unused
334 _tmp_b.allocator()->allocate();
335 CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false);
336 _original_b->mark_as_unused();
337 }
338
339 // Run matrix B reduction kernel only if _a_offset is not equal to 0
340 if(_a_offset != 0 && _reshape_b_only_on_first_run)
341 {
342 _vector_sum_col.allocator()->allocate();
343 CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false);
344 }
345
346 CLScheduler::get().queue().finish();
347 _is_prepared = true;
348 }
Gian Marco05288a22017-11-21 10:57:50 +0000349}