blob: 875e3a2a00b86f276739bf3dbc67f9423bc93254 [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 Iodice2ec6c1e2019-04-09 12:03:05 +010027#include "arm_compute/core/CL/gemm/reshaped_only_rhs/CLGEMMReshapedOnlyRHSKernelConfiguration.h"
Gian Marco05288a22017-11-21 10:57:50 +000028#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Validate.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Gian Marco05288a22017-11-21 10:57:50 +000034#include "arm_compute/runtime/CL/CLScheduler.h"
35
giuros011c9efeb2019-01-11 14:04:43 +000036namespace arm_compute
37{
Georgios Pinitas358ca202017-12-07 16:47:52 +000038using namespace arm_compute::misc::shape_calculator;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +000039using namespace arm_compute::cl_gemm;
Gian Marco05288a22017-11-21 10:57:50 +000040
Gian Marco19835e52018-01-30 13:35:54 +000041namespace
42{
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +010043inline bool is_gemm_reshaped(bool reshape_b_only_on_first_run, GPUTarget gpu_target)
Gian Marco19835e52018-01-30 13:35:54 +000044{
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +010045 return (get_arch_from_target(gpu_target) != GPUTarget::MIDGARD) && (reshape_b_only_on_first_run);
Gian Marco19835e52018-01-30 13:35:54 +000046}
47} // namespace
48
Gian Marco05288a22017-11-21 10:57:50 +000049CLGEMMLowpMatrixMultiplyCore::CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitas72219332018-06-05 14:56:06 +010050 : _memory_group(std::move(memory_manager)),
51 _mm_kernel(),
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +010052 _mm_reshaped_only_rhs_kernel(),
Georgios Pinitas72219332018-06-05 14:56:06 +010053 _mtx_b_reshape_kernel(),
54 _mtx_a_reduction_kernel(),
55 _mtx_b_reduction_kernel(),
56 _offset_contribution_kernel(),
Gian Marco Iodice4b908652018-10-18 10:21:02 +010057 _offset_contribution_output_stage_kernel(),
Georgios Pinitas72219332018-06-05 14:56:06 +010058 _vector_sum_col(),
59 _vector_sum_row(),
Georgios Pinitas72219332018-06-05 14:56:06 +010060 _tmp_b(),
Gian Marco Iodice4b908652018-10-18 10:21:02 +010061 _mm_result_s32(),
Georgios Pinitas72219332018-06-05 14:56:06 +010062 _original_b(nullptr),
63 _a_offset(0),
64 _b_offset(0),
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +000065 _is_gemm_reshaped(true),
Georgios Pinitas72219332018-06-05 14:56:06 +010066 _reshape_b_only_on_first_run(false),
Gian Marco Iodice4b908652018-10-18 10:21:02 +010067 _is_prepared(false),
68 _fuse_output_stage(false)
Gian Marco05288a22017-11-21 10:57:50 +000069{
70}
71
Gian Marco Iodice4b908652018-10-18 10:21:02 +010072void CLGEMMLowpMatrixMultiplyCore::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info)
Gian Marco05288a22017-11-21 10:57:50 +000073{
Georgios Pinitas358ca202017-12-07 16:47:52 +000074 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Gian Marco Iodice4b908652018-10-18 10:21:02 +010075 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 +000076
Georgios Pinitas72219332018-06-05 14:56:06 +010077 _is_prepared = false;
78 _original_b = b;
Chunosov5124be52017-11-22 20:42:13 +070079 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010080 _a_offset = a->info()->quantization_info().uniform().offset;
81 _b_offset = b->info()->quantization_info().uniform().offset;
Gian Marco05288a22017-11-21 10:57:50 +000082
Gian Marco19835e52018-01-30 13:35:54 +000083 // Get the GPU target
84 const GPUTarget gpu_target = CLScheduler::get().target();
Gian Marco7b4d5472018-01-10 15:56:30 +000085
Gian Marco19835e52018-01-30 13:35:54 +000086 // Set the target for the kernels
Gian Marco19835e52018-01-30 13:35:54 +000087 _mm_kernel.set_target(gpu_target);
Gian Marco05288a22017-11-21 10:57:50 +000088
89 const ICLTensor *matrix_a = a;
90 const ICLTensor *matrix_b = b;
giuros018b6b4a92018-12-18 19:01:33 +000091 GEMMRHSMatrixInfo rhs_info;
giuros011c9efeb2019-01-11 14:04:43 +000092 GEMMLHSMatrixInfo lhs_info;
Gian Marco05288a22017-11-21 10:57:50 +000093
Gian Marco19835e52018-01-30 13:35:54 +000094 // Arguments used by GEMMReshapeInfo
95 // 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
96 // in order to know how the matrices have been reshaped
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +000097 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
98 const unsigned int m = reinterpret_input_as_3d ? (a->info()->dimension(1) * a->info()->dimension(2)) : a->info()->dimension(1);
99 const unsigned int n = b->info()->dimension(0);
100 const unsigned int k = a->info()->dimension(0);
101 const unsigned int batch_size = reinterpret_input_as_3d ? a->info()->dimension(3) : a->info()->dimension(2);
102 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Gian Marco19835e52018-01-30 13:35:54 +0000103
104 // Check if we need to reshape the matrix A and matrix B
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100105 _is_gemm_reshaped = is_gemm_reshaped(_reshape_b_only_on_first_run, gpu_target);
Gian Marco19835e52018-01-30 13:35:54 +0000106
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000107 if(_is_gemm_reshaped)
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100108 {
Gian Marco05288a22017-11-21 10:57:50 +0000109 matrix_b = &_tmp_b;
110
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100111 if(!_reshape_b_only_on_first_run)
112 {
113 _memory_group.manage(&_tmp_b);
114 }
Gian Marco05288a22017-11-21 10:57:50 +0000115
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000116 // Pick up the GEMM configuration
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100117 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 +0000118
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000119 // Configure reshape RHS kernel
giuros018b6b4a92018-12-18 19:01:33 +0000120 _mtx_b_reshape_kernel.configure(b, &_tmp_b, rhs_info);
Gian Marco05288a22017-11-21 10:57:50 +0000121 }
Gian Marco05288a22017-11-21 10:57:50 +0000122
123 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
124 if(_a_offset != 0)
125 {
Georgios Pinitas358ca202017-12-07 16:47:52 +0000126 TensorInfo info_vector_sum_col(compute_reductionA_shape(*b->info()), 1, DataType::S32);
Gian Marco05288a22017-11-21 10:57:50 +0000127 _vector_sum_col.allocator()->init(info_vector_sum_col);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100128 if(!_reshape_b_only_on_first_run)
129 {
130 _memory_group.manage(&_vector_sum_col);
131 }
Gian Marco05288a22017-11-21 10:57:50 +0000132
133 // Configure Matrix B reduction kernel
134 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col);
135 }
136
137 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
138 if(_b_offset != 0)
139 {
Georgios Pinitas358ca202017-12-07 16:47:52 +0000140 TensorInfo info_vector_sum_row(compute_reductionB_shape(*a->info()), 1, DataType::S32);
Gian Marco05288a22017-11-21 10:57:50 +0000141 _vector_sum_row.allocator()->init(info_vector_sum_row);
142 _memory_group.manage(&_vector_sum_row);
143
144 // Configure matrix A reduction kernel
145 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row);
146 }
147
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100148 // If GEMMLowpOutputStage != NONE, fuse the offset contribution with the output stage
149 if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE)
150 {
151 _fuse_output_stage = true;
152
153 _memory_group.manage(&_mm_result_s32);
154
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000155 if(_is_gemm_reshaped)
156 {
157 // Configure and tune matrix multiply kernel
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100158 _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 +0000159 }
160 else
161 {
162 // Configure matrix multiply kernel
163 _mm_kernel.configure(matrix_a, matrix_b, &_mm_result_s32, false, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d));
164 }
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100165
166 // Configure offset contribution kernel
167 _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),
168 _a_offset, _b_offset, gemm_info.gemmlowp_output_stage());
169
170 _mm_result_s32.allocator()->allocate();
171 }
172 else
173 {
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000174 if(_is_gemm_reshaped)
175 {
176 // Configure and tune matrix multiply kernel
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100177 _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 +0000178 }
179 else
180 {
181 // Configure matrix multiply kernel
182 _mm_kernel.configure(matrix_a, matrix_b, output, false, GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d));
183 }
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100184
185 // Configure offset contribution kernel
186 _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);
187 }
Gian Marco05288a22017-11-21 10:57:50 +0000188
189 // Allocate tensors
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000190 if(_is_gemm_reshaped)
Gian Marco05288a22017-11-21 10:57:50 +0000191 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100192 if(!_reshape_b_only_on_first_run)
193 {
194 _tmp_b.allocator()->allocate();
195 }
Gian Marco05288a22017-11-21 10:57:50 +0000196 }
197
Georgios Pinitas72219332018-06-05 14:56:06 +0100198 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Gian Marco05288a22017-11-21 10:57:50 +0000199 {
200 _vector_sum_col.allocator()->allocate();
201 }
202
203 if(_b_offset != 0)
204 {
205 _vector_sum_row.allocator()->allocate();
206 }
207}
208
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100209Status 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 +0000210{
211 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000212 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000213 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
214 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
215
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100216 int32_t a_offset = a->quantization_info().uniform().offset;
217 int32_t b_offset = b->quantization_info().uniform().offset;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000218
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100219 const ITensorInfo *matrix_a_info = a;
220 const ITensorInfo *matrix_b_info = b;
221
giuros018b6b4a92018-12-18 19:01:33 +0000222 TensorInfo tmp_b_info{};
223 GEMMRHSMatrixInfo rhs_info;
giuros011c9efeb2019-01-11 14:04:43 +0000224 GEMMLHSMatrixInfo lhs_info;
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100225
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000226 // Get the GPU target
227 const GPUTarget gpu_target = CLScheduler::get().target();
228
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000229 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
230 const unsigned int m = reinterpret_input_as_3d ? (a->dimension(1) * a->dimension(2)) : a->dimension(1);
231 const unsigned int n = b->dimension(0);
232 const unsigned int k = a->dimension(0);
233 const unsigned int batch_size = reinterpret_input_as_3d ? a->dimension(3) : a->dimension(2);
234 const int depth_output_gemm3d = gemm_info.depth_output_gemm3d();
Gian Marco19835e52018-01-30 13:35:54 +0000235
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100236 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 +0100237
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000238 const GEMMReshapeInfo reshape_info = GEMMReshapeInfo(m, n, k, 1, 1, depth_output_gemm3d, reinterpret_input_as_3d);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100239
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100240 if(reshape_matrix_b)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000241 {
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100242 matrix_b_info = &tmp_b_info;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000243
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000244 // Pick up the GEMM configuration
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100245 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 +0100246
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000247 // Validate reshape RHS kernel
giuros018b6b4a92018-12-18 19:01:33 +0000248 auto_init_if_empty(tmp_b_info, b->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*b, rhs_info)));
249 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMReshapeRHSMatrixKernel::validate(b, &tmp_b_info, rhs_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000250 }
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100251
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100252 TensorInfo info_vector_sum_col{};
253 TensorInfo info_vector_sum_row{};
Georgios Pinitas358ca202017-12-07 16:47:52 +0000254
255 // Validate matrix B reduction kernel only if _a_offset is not equal to 0
256 if(a_offset != 0)
257 {
258 info_vector_sum_col = TensorInfo(compute_reductionA_shape(*b), 1, DataType::S32);
259
260 // Configure Matrix B reduction kernel
261 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixBReductionKernel::validate(b, &info_vector_sum_col));
262 }
263
264 // Validate Matrix A reduction kernel only if _b_offset is not equal to 0
265 if(b_offset != 0)
266 {
267 info_vector_sum_row = TensorInfo(compute_reductionB_shape(*a), 1, DataType::S32);
268
269 // Configure matrix A reduction kernel
270 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(a, &info_vector_sum_row));
271 }
272
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100273 if(gemm_info.gemmlowp_output_stage().type != GEMMLowpOutputStageType::NONE)
274 {
275 TensorInfo mm_result_s32_info{};
276
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100277 if(reshape_matrix_b)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000278 {
279 // Output tensor auto inizialitation if not yet initialized
280 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 +0100281
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000282 // Validate matrix multiply
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100283 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 +0000284 }
285 else
286 {
287 // Output tensor auto inizialitation if not yet initialized
288 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 +0100289
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000290 // Validate matrix multiply
291 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, &mm_result_s32_info, false, reshape_info));
292 }
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100293 // Validate offset contribution kernel
294 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionOutputStageKernel::validate(&mm_result_s32_info,
295 a_offset == 0 ? nullptr : &info_vector_sum_col,
296 b_offset == 0 ? nullptr : &info_vector_sum_row,
297 c,
298 output,
299 a_offset, b_offset,
300 gemm_info.gemmlowp_output_stage()));
301 }
302 else
303 {
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100304 if(reshape_matrix_b)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000305 {
306 // Validate matrix multiply
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100307 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 +0000308 }
309 else
310 {
311 // Validate matrix multiply
312 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyKernel::validate(matrix_a_info, matrix_b_info, output, false, reshape_info));
313 }
giuros012f7c1492019-03-18 12:30:02 +0000314 if(output->total_size() != 0)
315 {
316 // Validate offset contribution kernel
317 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOffsetContributionKernel::validate(output,
318 a_offset == 0 ? nullptr : &info_vector_sum_col,
319 b_offset == 0 ? nullptr : &info_vector_sum_row,
320 c,
321 a_offset, b_offset));
322 }
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100323 }
Georgios Pinitas358ca202017-12-07 16:47:52 +0000324
325 return Status{};
326}
327
Gian Marco05288a22017-11-21 10:57:50 +0000328void CLGEMMLowpMatrixMultiplyCore::run()
329{
Georgios Pinitas72219332018-06-05 14:56:06 +0100330 prepare();
331
Georgios Pinitasda953f22019-04-02 17:27:03 +0100332 MemoryGroupResourceScope scope_mg(_memory_group);
Gian Marco05288a22017-11-21 10:57:50 +0000333
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000334 if(_is_gemm_reshaped)
Gian Marco05288a22017-11-21 10:57:50 +0000335 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100336 if(!_reshape_b_only_on_first_run)
Chunosov5124be52017-11-22 20:42:13 +0700337 {
338 // Run reshape matrix B
339 CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false);
340 }
341 }
342
Georgios Pinitas72219332018-06-05 14:56:06 +0100343 // Run matrix B reduction kernel only if _a_offset is not equal to 0
344 if(_a_offset != 0 && !_reshape_b_only_on_first_run)
Chunosov5124be52017-11-22 20:42:13 +0700345 {
Georgios Pinitas72219332018-06-05 14:56:06 +0100346 CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false);
Gian Marco05288a22017-11-21 10:57:50 +0000347 }
348
349 // Run matrix multiply
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000350 if(_is_gemm_reshaped)
351 {
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100352 CLScheduler::get().enqueue(_mm_reshaped_only_rhs_kernel, false);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000353 }
354 else
355 {
356 CLScheduler::get().enqueue(_mm_kernel, false);
357 }
Gian Marco05288a22017-11-21 10:57:50 +0000358
359 // Run matrix A reduction kernel only if _b_offset is not equal to 0
360 if(_b_offset != 0)
361 {
362 CLScheduler::get().enqueue(_mtx_a_reduction_kernel, false);
363 }
364
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100365 if(_fuse_output_stage)
366 {
367 // Run offset contribution/output stage kernel
368 CLScheduler::get().enqueue(_offset_contribution_output_stage_kernel, true);
369 }
370 else
371 {
372 // Run offset contribution kernel
373 CLScheduler::get().enqueue(_offset_contribution_kernel, true);
374 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100375}
Chunosov5124be52017-11-22 20:42:13 +0700376
Georgios Pinitas72219332018-06-05 14:56:06 +0100377void CLGEMMLowpMatrixMultiplyCore::prepare()
378{
379 if(!_is_prepared)
380 {
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000381 if(_is_gemm_reshaped && _reshape_b_only_on_first_run)
Georgios Pinitas72219332018-06-05 14:56:06 +0100382 {
383 ARM_COMPUTE_ERROR_ON(!_original_b->is_used());
384
385 // Run reshape kernel and mark original weights tensor as unused
386 _tmp_b.allocator()->allocate();
387 CLScheduler::get().enqueue(_mtx_b_reshape_kernel, false);
388 _original_b->mark_as_unused();
389 }
390
391 // Run matrix B reduction kernel only if _a_offset is not equal to 0
392 if(_a_offset != 0 && _reshape_b_only_on_first_run)
393 {
394 _vector_sum_col.allocator()->allocate();
395 CLScheduler::get().enqueue(_mtx_b_reduction_kernel, false);
396 }
397
398 CLScheduler::get().queue().finish();
399 _is_prepared = true;
400 }
Gian Marco05288a22017-11-21 10:57:50 +0000401}
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100402} // namespace arm_compute