blob: 0286cb3d6dbfa6383ff94b30903d3cf9f77fb377 [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
giuros011c9efeb2019-01-11 14:04:43 +00002 * Copyright (c) 2017-2019 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"
Gian Marco Iodice06be6f82019-06-24 17:47:51 +010027#include "arm_compute/core/CL/gemm/native/CLGEMMNativeKernelConfiguration.h"
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +010028#include "arm_compute/core/CL/gemm/reshaped_only_rhs/CLGEMMReshapedOnlyRHSKernelConfiguration.h"
Gian Marco05288a22017-11-21 10:57:50 +000029#include "arm_compute/core/Error.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/core/Validate.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco05288a22017-11-21 10:57:50 +000035#include "arm_compute/runtime/CL/CLScheduler.h"
36
giuros011c9efeb2019-01-11 14:04:43 +000037namespace arm_compute
38{
Georgios Pinitas358ca202017-12-07 16:47:52 +000039using namespace arm_compute::misc::shape_calculator;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +000040using namespace arm_compute::cl_gemm;
Gian Marco05288a22017-11-21 10:57:50 +000041
Gian Marco19835e52018-01-30 13:35:54 +000042namespace
43{
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +010044inline bool is_gemm_reshaped(bool reshape_b_only_on_first_run, GPUTarget gpu_target)
Gian Marco19835e52018-01-30 13:35:54 +000045{
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +010046 return (get_arch_from_target(gpu_target) != GPUTarget::MIDGARD) && (reshape_b_only_on_first_run);
Gian Marco19835e52018-01-30 13:35:54 +000047}
48} // namespace
49
Gian Marco05288a22017-11-21 10:57:50 +000050CLGEMMLowpMatrixMultiplyCore::CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitas72219332018-06-05 14:56:06 +010051 : _memory_group(std::move(memory_manager)),
Gian Marco Iodice06be6f82019-06-24 17:47:51 +010052 _mm_midgard_kernel(),
53 _mm_native_kernel(),
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +010054 _mm_reshaped_only_rhs_kernel(),
Georgios Pinitas72219332018-06-05 14:56:06 +010055 _mtx_b_reshape_kernel(),
56 _mtx_a_reduction_kernel(),
57 _mtx_b_reduction_kernel(),
58 _offset_contribution_kernel(),
Gian Marco Iodice4b908652018-10-18 10:21:02 +010059 _offset_contribution_output_stage_kernel(),
Georgios Pinitas72219332018-06-05 14:56:06 +010060 _vector_sum_col(),
61 _vector_sum_row(),
Georgios Pinitas72219332018-06-05 14:56:06 +010062 _tmp_b(),
Gian Marco Iodice4b908652018-10-18 10:21:02 +010063 _mm_result_s32(),
Georgios Pinitas72219332018-06-05 14:56:06 +010064 _original_b(nullptr),
65 _a_offset(0),
66 _b_offset(0),
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +000067 _is_gemm_reshaped(true),
Gian Marco Iodice06be6f82019-06-24 17:47:51 +010068 _is_midgard(false),
Georgios Pinitas72219332018-06-05 14:56:06 +010069 _reshape_b_only_on_first_run(false),
Gian Marco Iodice4b908652018-10-18 10:21:02 +010070 _is_prepared(false),
71 _fuse_output_stage(false)
Gian Marco05288a22017-11-21 10:57:50 +000072{
73}
74
Gian Marco Iodice4b908652018-10-18 10:21:02 +010075void CLGEMMLowpMatrixMultiplyCore::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info)
Gian Marco05288a22017-11-21 10:57:50 +000076{
Georgios Pinitas358ca202017-12-07 16:47:52 +000077 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Gian Marco Iodice4b908652018-10-18 10:21:02 +010078 ARM_COMPUTE_ERROR_THROW_ON(CLGEMMLowpMatrixMultiplyCore::validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), gemm_info));
Gian Marco05288a22017-11-21 10:57:50 +000079
Georgios Pinitas72219332018-06-05 14:56:06 +010080 _is_prepared = false;
81 _original_b = b;
Chunosov5124be52017-11-22 20:42:13 +070082 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010083 _a_offset = a->info()->quantization_info().uniform().offset;
84 _b_offset = b->info()->quantization_info().uniform().offset;
Gian Marco05288a22017-11-21 10:57:50 +000085
Gian Marco19835e52018-01-30 13:35:54 +000086 // Get the GPU target
87 const GPUTarget gpu_target = CLScheduler::get().target();
Gian Marco7b4d5472018-01-10 15:56:30 +000088
Gian Marco19835e52018-01-30 13:35:54 +000089 // Set the target for the kernels
Gian Marco Iodice06be6f82019-06-24 17:47:51 +010090 _mm_midgard_kernel.set_target(gpu_target);
91 _mm_native_kernel.set_target(gpu_target);
92 _mm_reshaped_only_rhs_kernel.set_target(gpu_target);
Gian Marco05288a22017-11-21 10:57:50 +000093
94 const ICLTensor *matrix_a = a;
95 const ICLTensor *matrix_b = b;
giuros018b6b4a92018-12-18 19:01:33 +000096 GEMMRHSMatrixInfo rhs_info;
giuros011c9efeb2019-01-11 14:04:43 +000097 GEMMLHSMatrixInfo lhs_info;
Gian Marco05288a22017-11-21 10:57:50 +000098
Gian Marco19835e52018-01-30 13:35:54 +000099 // Arguments used by GEMMReshapeInfo
100 // 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
101 // in order to know how the matrices have been reshaped
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000102 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
103 const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1);
104 const unsigned int n = b->info()->dimension(0);
105 const unsigned int k = a->info()->dimension(0);
106 const unsigned int batch_size = reinterpret_input_as_3d ? a->info()->dimension(3) : a->info()->dimension(2);
107 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Gian Marco19835e52018-01-30 13:35:54 +0000108
109 // Check if we need to reshape the matrix A and matrix B
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100110 _is_gemm_reshaped = is_gemm_reshaped(_reshape_b_only_on_first_run, gpu_target);
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100111 _is_midgard = gpu_target == GPUTarget::MIDGARD;
Gian Marco19835e52018-01-30 13:35:54 +0000112
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000113 if(_is_gemm_reshaped)
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100114 {
Gian Marco05288a22017-11-21 10:57:50 +0000115 matrix_b = &_tmp_b;
116
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100117 if(!_reshape_b_only_on_first_run)
118 {
119 _memory_group.manage(&_tmp_b);
120 }
Gian Marco05288a22017-11-21 10:57:50 +0000121
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000122 // Pick up the GEMM configuration
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100123 std::tie(lhs_info, rhs_info) = CLGEMMReshapedOnlyRHSKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8);
Gian Marco05288a22017-11-21 10:57:50 +0000124
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000125 // Configure reshape RHS kernel
giuros018b6b4a92018-12-18 19:01:33 +0000126 _mtx_b_reshape_kernel.configure(b, &_tmp_b, rhs_info);
Gian Marco05288a22017-11-21 10:57:50 +0000127 }
Gian Marco05288a22017-11-21 10:57:50 +0000128
129 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
130 if(_a_offset != 0)
131 {
Georgios Pinitas358ca202017-12-07 16:47:52 +0000132 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
Gian Marco05288a22017-11-21 10:57:50 +0000133 _vector_sum_col.allocator()->init(info_vector_sum_col);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100134 if(!_reshape_b_only_on_first_run)
135 {
136 _memory_group.manage(&_vector_sum_col);
137 }
Gian Marco05288a22017-11-21 10:57:50 +0000138
139 // Configure Matrix B reduction kernel
140 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col);
141 }
142
143 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
144 if(_b_offset != 0)
145 {
Georgios Pinitas358ca202017-12-07 16:47:52 +0000146 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a->info()), 1, DataType::S32);
Gian Marco05288a22017-11-21 10:57:50 +0000147 _vector_sum_row.allocator()->init(info_vector_sum_row);
148 _memory_group.manage(&_vector_sum_row);
149
150 // Configure matrix A reduction kernel
151 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row);
152 }
153
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100154 // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage
155 if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE)
156 {
157 _fuse_output_stage = true;
158
159 _memory_group.manage(&_mm_result_s32);
160
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000161 if(_is_gemm_reshaped)
162 {
163 // Configure and tune matrix multiply kernel
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100164 _mm_reshaped_only_rhs_kernel.configure(matrix_a, matrix_b, &_mm_result_s32, lhs_info, rhs_info, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d));
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000165 }
166 else
167 {
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100168 if(_is_midgard)
169 {
170 // Configure matrix multiply kernel
171 _mm_midgard_kernel.configure(matrix_a, matrix_b, &_mm_result_s32, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d));
172 }
173 else
174 {
175 // Pick up the GEMM configuration
176 std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8);
177
178 // Configure matrix multiply kernel
179 _mm_native_kernel.configure(matrix_a, matrix_b, &_mm_result_s32, lhs_info, rhs_info, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d));
180 }
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000181 }
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100182
183 // Configure offset contribution kernel
184 _offset_contribution_output_stage_kernel.configure(&_mm_result_s32, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, c, output, a->info()->dimension(0),
185 _a_offset, _b_offset, gemm_info.gemmlowp_output_stage());
186
187 _mm_result_s32.allocator()->allocate();
188 }
189 else
190 {
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000191 if(_is_gemm_reshaped)
192 {
193 // Configure and tune matrix multiply kernel
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100194 _mm_reshaped_only_rhs_kernel.configure(matrix_a, matrix_b, output, lhs_info, rhs_info, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d));
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000195 }
196 else
197 {
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100198 if(_is_midgard)
199 {
200 // Configure matrix multiply kernel
201 _mm_midgard_kernel.configure(matrix_a, matrix_b, output, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d));
202 }
203 else
204 {
205 // Pick up the GEMM configuration
206 std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8);
207
208 // Configure matrix multiply kernel
209 _mm_native_kernel.configure(matrix_a, matrix_b, output, lhs_info, rhs_info, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d));
210 }
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000211 }
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100212
213 // Configure offset contribution kernel
214 _offset_contribution_kernel.configure(output, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, c, a->info()->dimension(0), _a_offset, _b_offset);
215 }
Gian Marco05288a22017-11-21 10:57:50 +0000216
217 // Allocate tensors
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000218 if(_is_gemm_reshaped)
Gian Marco05288a22017-11-21 10:57:50 +0000219 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100220 if(!_reshape_b_only_on_first_run)
221 {
222 _tmp_b.allocator()->allocate();
223 }
Gian Marco05288a22017-11-21 10:57:50 +0000224 }
225
Georgios Pinitas72219332018-06-05 14:56:06 +0100226 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Gian Marco05288a22017-11-21 10:57:50 +0000227 {
228 _vector_sum_col.allocator()->allocate();
229 }
230
231 if(_b_offset != 0)
232 {
233 _vector_sum_row.allocator()->allocate();
234 }
235}
236
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100237Status CLGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000238{
239 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000240 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000241 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
242 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
243
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100244 int32_t a_offset = a->quantization_info().uniform().offset;
245 int32_t b_offset = b->quantization_info().uniform().offset;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000246
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100247 const ITensorInfo *matrix_a_info = a;
248 const ITensorInfo *matrix_b_info = b;
249
giuros018b6b4a92018-12-18 19:01:33 +0000250 TensorInfo tmp_b_info{};
251 GEMMRHSMatrixInfo rhs_info;
giuros011c9efeb2019-01-11 14:04:43 +0000252 GEMMLHSMatrixInfo lhs_info;
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100253
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000254 // Get the GPU target
255 const GPUTarget gpu_target = CLScheduler::get().target();
256
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000257 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
258 const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1);
259 const unsigned int n = b->dimension(0);
260 const unsigned int k = a->dimension(0);
261 const unsigned int batch_size = reinterpret_input_as_3d ? a->dimension(3) : a->dimension(2);
262 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100263 const bool is_midgard = gpu_target == GPUTarget::MIDGARD;
Gian Marco19835e52018-01-30 13:35:54 +0000264
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100265 bool reshape_matrix_b = is_gemm_reshaped(gemm_info.reshape_b_only_on_first_run(), CLScheduler::get().target());
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100266
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000267 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100268
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100269 if(reshape_matrix_b)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000270 {
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100271 matrix_b_info = &tmp_b_info;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000272
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000273 // Pick up the GEMM configuration
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100274 std::tie(lhs_info, rhs_info) = CLGEMMReshapedOnlyRHSKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8);
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100275
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000276 // Validate reshape RHS kernel
giuros018b6b4a92018-12-18 19:01:33 +0000277 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info)));
278 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000279 }
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100280
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100281 TensorInfo info_vector_sum_col{};
282 TensorInfo info_vector_sum_row{};
Georgios Pinitas358ca202017-12-07 16:47:52 +0000283
284 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
285 if(a_offset != 0)
286 {
287 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
288
289 // Configure Matrix B reduction kernel
290 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col));
291 }
292
293 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
294 if(b_offset != 0)
295 {
296 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
297
298 // Configure matrix A reduction kernel
299 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row));
300 }
301
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100302 if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE)
303 {
304 TensorInfo mm_result_s32_info{};
305
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100306 if(reshape_matrix_b)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000307 {
308 // Output tensor auto inizialitation if not yet initialized
309 auto_init_if_empty(mm_result_s32_info, a->clone()->set_tensor_shape(compute_mm_shape(*matrix_a_info, *matrix_b_info, reshape_info)).set_data_type(DataType::S32));
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100310
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000311 // Validate matrix multiply
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100312 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info, lhs_info, rhs_info, reshape_info));
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000313 }
314 else
315 {
316 // Output tensor auto inizialitation if not yet initialized
317 auto_init_if_empty(mm_result_s32_info, a->clone()->set_tensor_shape(compute_mm_shape(*matrix_a_info, *matrix_b_info, false, reshape_info)).set_data_type(DataType::S32));
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100318
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100319 if(is_midgard)
320 {
321 // Validate matrix multiply
322 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info, reshape_info));
323 }
324 else
325 {
326 // Pick up the GEMM configuration
327 std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8);
328
329 // Validate matrix multiply
330 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyNativeKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info, lhs_info, rhs_info, reshape_info));
331 }
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000332 }
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100333
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100334 // Validate offset contribution kernel
335 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info,
336 a_offset == 0 ? nullptr : &info_vector_sum_col,
337 b_offset == 0 ? nullptr : &info_vector_sum_row,
338 c,
339 output,
340 a_offset, b_offset,
341 gemm_info.gemmlowp_output_stage()));
342 }
343 else
344 {
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100345 if(reshape_matrix_b)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000346 {
347 // Validate matrix multiply
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100348 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::validate(matrix_a_info, matrix_b_info, output, lhs_info, rhs_info, reshape_info));
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000349 }
350 else
351 {
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100352 if(is_midgard)
353 {
354 // Validate matrix multiply
355 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output, reshape_info));
356 }
357 else
358 {
359 // Pick up the GEMM configuration
360 std::tie(lhs_info, rhs_info) = CLGEMMNativeKernelConfigurationFactory::create(gpu_target)->configure(m, n, k, batch_size, DataType::QASYMM8);
361
362 // Validate matrix multiply
363 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyNativeKernel::validate(matrix_a_info, matrix_b_info, output, lhs_info, rhs_info, reshape_info));
364 }
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000365 }
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100366
giuros012f7c1492019-03-18 12:30:02 +0000367 if(output->total_size() != 0)
368 {
369 // Validate offset contribution kernel
370 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionKernel::validate(output,
371 a_offset == 0 ? nullptr : &info_vector_sum_col,
372 b_offset == 0 ? nullptr : &info_vector_sum_row,
373 c,
374 a_offset, b_offset));
375 }
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100376 }
Georgios Pinitas358ca202017-12-07 16:47:52 +0000377
378 return Status{};
379}
380
Gian Marco05288a22017-11-21 10:57:50 +0000381void CLGEMMLowpMatrixMultiplyCore::run()
382{
Georgios Pinitas72219332018-06-05 14:56:06 +0100383 prepare();
384
Georgios Pinitasda953f22019-04-02 17:27:03 +0100385 MemoryGroupResourceScope scope_mg(_memory_group);
Gian Marco05288a22017-11-21 10:57:50 +0000386
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000387 if(_is_gemm_reshaped)
Gian Marco05288a22017-11-21 10:57:50 +0000388 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100389 if(!_reshape_b_only_on_first_run)
Chunosov5124be52017-11-22 20:42:13 +0700390 {
391 // Run reshape matrix B
392 CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false);
393 }
394 }
395
Georgios Pinitas72219332018-06-05 14:56:06 +0100396 // Run matrix B reduction kernel only if _a_offset is not equal to 0
397 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Chunosov5124be52017-11-22 20:42:13 +0700398 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100399 CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false);
Gian Marco05288a22017-11-21 10:57:50 +0000400 }
401
402 // Run matrix multiply
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000403 if(_is_gemm_reshaped)
404 {
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100405 CLScheduler::get().enqueue(_mm_reshaped_only_rhs_kernel, false);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000406 }
407 else
408 {
Gian Marco Iodice06be6f82019-06-24 17:47:51 +0100409 if(_is_midgard)
410 {
411 CLScheduler::get().enqueue(_mm_midgard_kernel, false);
412 }
413 else
414 {
415 CLScheduler::get().enqueue(_mm_native_kernel, false);
416 }
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000417 }
Gian Marco05288a22017-11-21 10:57:50 +0000418
419 // Run matrix A reduction kernel only if _b_offset is not equal to 0
420 if(_b_offset != 0)
421 {
422 CLScheduler::get().enqueue(_mtx_a_reduction_kernel, false);
423 }
424
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100425 if(_fuse_output_stage)
426 {
427 // Run offset contribution/output stage kernel
428 CLScheduler::get().enqueue(_offset_contribution_output_stage_kernel, true);
429 }
430 else
431 {
432 // Run offset contribution kernel
433 CLScheduler::get().enqueue(_offset_contribution_kernel, true);
434 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100435}
Chunosov5124be52017-11-22 20:42:13 +0700436
Georgios Pinitas72219332018-06-05 14:56:06 +0100437void CLGEMMLowpMatrixMultiplyCore::prepare()
438{
439 if(!_is_prepared)
440 {
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000441 if(_is_gemm_reshaped && _reshape_b_only_on_first_run)
Georgios Pinitas72219332018-06-05 14:56:06 +0100442 {
443 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
444
445 // Run reshape kernel and mark original weights tensor as unused
446 _tmp_b.allocator()->allocate();
447 CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false);
448 _original_b->mark_as_unused();
449 }
450
451 // Run matrix B reduction kernel only if _a_offset is not equal to 0
452 if(_a_offset != 0 && _reshape_b_only_on_first_run)
453 {
454 _vector_sum_col.allocator()->allocate();
455 CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false);
456 }
457
458 CLScheduler::get().queue().finish();
459 _is_prepared = true;
460 }
Gian Marco05288a22017-11-21 10:57:50 +0000461}
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100462} // namespace arm_compute