blob: 95aa30d14ad02b1102020722bf6c53b074282b05 [file] [log] [blame]
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Gian Marco Iodice62251f72019-03-11 16:07:12 +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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel.h"
Gian Marco Iodice62251f72019-03-11 16:07:12 +000025
Gian Marco Iodice62251f72019-03-11 16:07:12 +000026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
Gian Marco Iodice62251f72019-03-11 16:07:12 +000030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
Gian Marco Iodice62251f72019-03-11 16:07:12 +000032#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
Gian Marco Iodice62251f72019-03-11 16:07:12 +000034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/AccessWindowStatic.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000038#include "support/StringSupport.h"
Gian Marco Iodice62251f72019-03-11 16:07:12 +000039
40#include <cstddef>
41#include <cstdint>
42#include <tuple>
43
44using namespace arm_compute::misc::shape_calculator;
45
46namespace arm_compute
47{
48namespace
49{
50using ElementsProcessed = Steps;
51
Michele Di Giorgiob54ba282020-01-14 15:31:55 +000052Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, const GEMMKernelInfo &gemm_info,
53 const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias,
54 const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts)
Gian Marco Iodice62251f72019-03-11 16:07:12 +000055{
56 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output);
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000057 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010058 if(input0->data_type() == DataType::QASYMM8)
59 {
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1);
61 }
62 else
63 {
morgolockd13931d2020-06-23 15:49:35 +010064 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::QASYMM8, DataType::QSYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010065 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +000066 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input0->num_dimensions() > 4, "The number of dimensions for the LHS matrix must be <= 4");
67 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->num_dimensions() > 3, "The number of dimensions for the RHS matrix must be <= 3");
Michele Di Giorgiob54ba282020-01-14 15:31:55 +000068
69 const GEMMRHSMatrixInfo rhs_info = gemm_info.rhs_info;
70 const GEMMLHSMatrixInfo lhs_info = gemm_info.lhs_info;
71 const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
72
Gian Marco Iodice62251f72019-03-11 16:07:12 +000073 ARM_COMPUTE_RETURN_ERROR_ON_MSG((((rhs_info.k0 & (rhs_info.k0 - 1)) && rhs_info.k0 != 3) || (rhs_info.k0 > 16)), "Only 2,3,4,8,16 are supported for k0");
74 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.m0 < 1 || lhs_info.m0 > 8);
75 ARM_COMPUTE_RETURN_ERROR_ON_MSG((((rhs_info.n0 & (rhs_info.n0 - 1)) && rhs_info.n0 != 3) || rhs_info.n0 > 16), "Only 2,3,4,8,16 are supported for n0");
Gian Marco Iodicedd717c32020-05-28 10:22:03 +010076 ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.export_to_cl_image, "Export to CLImage not supported for quantized GEMM");
Gian Marco Iodice62251f72019-03-11 16:07:12 +000077
Michele Di Giorgiob54ba282020-01-14 15:31:55 +000078 const int m = gemm_info.m;
79 const int n = gemm_info.n;
80 const int k = gemm_info.k;
Gian Marco Iodice62251f72019-03-11 16:07:12 +000081
Gian Marco Iodice62251f72019-03-11 16:07:12 +000082 TensorShape tensor_shape1{ input1->tensor_shape() };
83 tensor_shape1.set(0, n);
84 tensor_shape1.set(1, k);
85
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +010086 const TensorInfo tensor_info1 = input1->clone()->set_tensor_shape(tensor_shape1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +000087 const TensorInfo tensor_info_reshaped1 = input1->clone()->set_tensor_shape(compute_rhs_reshaped_shape(tensor_info1, rhs_info));
88
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +010089 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(0) != static_cast<unsigned int>(k));
Michele Di Giorgiob54ba282020-01-14 15:31:55 +000090 if(gemm_info.reinterpret_input_as_3d)
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +010091 {
92 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) * input0->dimension(2) != static_cast<unsigned int>(m));
93 }
94 else
95 {
96 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) != static_cast<unsigned int>(m));
97 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +000098 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, &tensor_info_reshaped1);
99
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000100 const TensorShape expected_output_shape = compute_mm_shape(*input0, *input1, gemm_info);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000101 if(output->total_size() != 0)
102 {
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000103 const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(expected_output_shape);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000104 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000105 if(output_stage.type == GEMMLowpOutputStageType::NONE)
106 {
107 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
108 }
109 else
110 {
111 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, output);
112 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000113 }
114
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000115 if(bias != nullptr)
116 {
117 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
118 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
119 ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != bias->dimension(0));
120 }
121
122 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN) || (output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT),
123 "Only GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT is supported");
124
125 // Checks performed if the output stage needs to be fused
126 if(output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
127 {
128 // If a_offset == 0, vector_sum_col can be a nullptr
129 if(gemm_info.a_offset != 0)
130 {
131 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_col, 1, DataType::S32);
132 ARM_COMPUTE_RETURN_ERROR_ON(vector_sum_col->dimension(0) != expected_output_shape[0]);
133 }
134
135 // If b_offset == 0, vector_sum_row can be a nullptr
136 if(gemm_info.b_offset != 0)
137 {
138 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_row, 1, DataType::S32);
139
140 // Check if mm result is a 3D reinterpretation
141 const bool reinterpret_as_3d = expected_output_shape.num_dimensions() > 1 && expected_output_shape.y() != vector_sum_row->tensor_shape().x();
142
143 // Validate input
144 ARM_COMPUTE_RETURN_ERROR_ON(reinterpret_as_3d && vector_sum_row->dimension(0) != (expected_output_shape[1] * expected_output_shape[2]));
145 ARM_COMPUTE_RETURN_ERROR_ON(!reinterpret_as_3d && vector_sum_row->dimension(0) != expected_output_shape[1]);
146
147 if(expected_output_shape.num_dimensions() > 1)
148 {
149 const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2;
150
151 TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
152 vector_sum_row_shape.collapse_from(1);
153 TensorShape collapsed_output_shape(expected_output_shape);
154 collapsed_output_shape.collapse_from(output_batch_idx);
155
156 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != collapsed_output_shape[output_batch_idx],
157 "vector_sum_row must have the same number of batches of output tensor");
158
159 if(gemm_info.a_offset != 0)
160 {
161 TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
162 vector_sum_col_shape.collapse_from(1);
163
164 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_col_shape[1] != 1 && vector_sum_col_shape[1] != vector_sum_row_shape[1],
165 "vector_sum_col tensor must have the same number of batches of vector_sum_row_shape or the number of batches must be set to 1");
166 }
167 }
168 }
169
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000170 if(output->total_size() != 0)
171 {
172 ARM_COMPUTE_RETURN_ERROR_ON(output_stage.output_data_type != output->data_type());
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000173 }
Michele Di Giorgio398b8e42020-03-06 13:56:54 +0000174 ARM_COMPUTE_RETURN_ERROR_ON(output_stage.gemmlowp_min_bound > output_stage.gemmlowp_max_bound);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000175
176 if(output_multipliers != nullptr && output_shifts != nullptr)
177 {
178 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_multipliers, 1, DataType::S32);
179 ARM_COMPUTE_RETURN_ERROR_ON(output_multipliers->num_dimensions() > 1);
180 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_shifts, 1, DataType::S32);
181 ARM_COMPUTE_RETURN_ERROR_ON(output_shifts->num_dimensions() > 1);
182 if(output_stage.is_quantized_per_channel)
183 {
184 ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != output_shifts->dimension(0));
185 ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != output_multipliers->dimension(0));
186 }
187 }
188 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000189 return Status{};
190}
191
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000192std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output, const GEMMKernelInfo &gemm_info,
193 ITensorInfo *vector_sum_col, ITensorInfo *vector_sum_row, ITensorInfo *bias,
194 ITensorInfo *output_multipliers, ITensorInfo *output_shifts, ElementsProcessed &num_elements_processed)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000195{
Manuel Bottini488f5082020-10-29 13:51:23 +0000196 ARM_COMPUTE_UNUSED(vector_sum_row, vector_sum_col, output_multipliers, bias, output_shifts);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000197
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100198 const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
199 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
200 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
201 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
202 bool reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d != 0);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000203
204 Window win{};
205 Window win_out{};
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000206
207 // In case both input and output have to be reinterpreted as 3D tensors,
208 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100209 if(reinterpret_input_as_3d == reinterpret_output_as_3d)
210 {
211 reinterpret_output_as_3d = false;
212 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000213
214 // Output tensor auto initialization if not yet initialized
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000215 const TensorShape expected_output_shape = compute_mm_shape(*input0, *input1, gemm_info);
216 if(output_stage.type != GEMMLowpOutputStageType::NONE)
217 {
218 auto_init_if_empty(*output, input0->clone()->set_tensor_shape(expected_output_shape).set_data_type(output_stage.output_data_type));
219 }
220 else
221 {
222 auto_init_if_empty(*output, input0->clone()->set_tensor_shape(expected_output_shape).set_data_type(DataType::S32));
223 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000224
225 TensorInfo tmp_info(*output);
226
227 if(reinterpret_output_as_3d)
228 {
229 // Since the output tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
230 // the window needs to be constructed on the 2D collapsed version of the tensor
231 TensorShape tmp_shape(output->tensor_shape());
232 tmp_shape.collapse(2U, 1U);
233 tmp_info.set_tensor_shape(tmp_shape);
234 }
235
236 // Configure kernel window
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000237 num_elems_processed_per_iteration_x = gemm_info.rhs_info.n0;
238 num_elems_processed_per_iteration_y = gemm_info.lhs_info.m0;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000239
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000240 win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
241 win_out = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
242
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000243 AccessWindowStatic output_access(output, 0, 0,
Manuel Bottini488f5082020-10-29 13:51:23 +0000244 output->dimension(0),
245 output->dimension(1));
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000246
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100247 output_access.set_valid_region(win_out, ValidRegion(Coordinates(), output->tensor_shape()));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000248
249 // Collapse along the Z direction
250 // This collapse needs to be here in order to tune the Z dimension of LWS
251 Window collapsed = win;
252 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(output->num_dimensions()), 2u);
253 collapsed = win.collapse(win, dimension_to_collapse);
254
Manuel Bottini488f5082020-10-29 13:51:23 +0000255 return std::make_pair(Status{}, collapsed);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000256}
257} // namespace
258
259CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel()
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000260 : _input0(nullptr),
261 _input1(nullptr),
262 _output(nullptr),
263 _vector_sum_col(nullptr),
264 _vector_sum_row(nullptr),
265 _bias(nullptr),
266 _output_multipliers(nullptr),
267 _output_shifts(nullptr),
268 _slide_matrix_b(true),
269 _reinterpret_input_as_3d(false),
270 _reinterpret_output_as_3d(false),
271 _use_dummy_work_items(false),
272 _is_quantized_per_channel(false),
273 _fuse_output_stage(false)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000274{
275}
276
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000277void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, const GEMMKernelInfo &gemm_info,
278 const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias,
279 const ICLTensor *output_multipliers, const ICLTensor *output_shifts)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000280{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100281 configure(CLKernelLibrary::get().get_compile_context(), input0, input1, output, gemm_info, vector_sum_col, vector_sum_row, bias, output_multipliers, output_shifts);
282}
283
Gian Marco Iodicedd717c32020-05-28 10:22:03 +0100284void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output,
285 const GEMMKernelInfo &gemm_info,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100286 const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias,
287 const ICLTensor *output_multipliers, const ICLTensor *output_shifts)
288{
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000289 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000290 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(),
291 input1->info(),
292 output->info(),
293 gemm_info,
294 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr,
295 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr,
296 bias != nullptr ? bias->info() : nullptr,
297 output_multipliers != nullptr ? output_multipliers->info() : nullptr,
298 output_shifts != nullptr ? output_shifts->info() : nullptr));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000299
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100300 auto padding_info = get_padding_info({ input0, input1, output, vector_sum_col, vector_sum_row, bias, output_multipliers, output_shifts });
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000301 const GEMMRHSMatrixInfo rhs_info = gemm_info.rhs_info;
302 const GEMMLHSMatrixInfo lhs_info = gemm_info.lhs_info;
303 const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
304 const int32_t a_offset = gemm_info.a_offset;
305 const int32_t b_offset = gemm_info.b_offset;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000306
307 _input0 = input0;
308 _input1 = input1;
309 _output = output;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000310 _vector_sum_col = vector_sum_col;
311 _vector_sum_row = vector_sum_row;
312 _bias = bias;
313 _output_multipliers = output_multipliers;
314 _output_shifts = output_shifts;
315 _reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
316 _reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d != 0);
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100317 _use_dummy_work_items = preferred_dummy_work_items_support(CLKernelLibrary::get().get_device());
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000318 _is_quantized_per_channel = output_stage.is_quantized_per_channel;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000319
320 // In case both input and output have to be reinterpreted as 3D tensors,
321 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100322 if(_reinterpret_input_as_3d == _reinterpret_output_as_3d)
323 {
324 _reinterpret_input_as_3d = false;
325 _reinterpret_output_as_3d = false;
326 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000327
328 // Check if we need to slide the matrix B
329 const unsigned int num_dimensions_input0 = _input0->info()->num_dimensions();
330 _slide_matrix_b = (_input1->info()->num_dimensions() >= num_dimensions_input0);
331
332 ElementsProcessed num_elements_processed{};
333
334 // Configure kernel window
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000335 auto win_config = validate_and_configure_window(input0->info(),
336 input1->info(),
337 output->info(),
338 gemm_info,
339 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr,
340 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr,
341 bias != nullptr ? bias->info() : nullptr,
342 output_multipliers != nullptr ? output_multipliers->info() : nullptr,
343 output_shifts != nullptr ? output_shifts->info() : nullptr,
344 num_elements_processed);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000345 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
346 ICLKernel::configure_internal(win_config.second);
347
Manuel Bottini488f5082020-10-29 13:51:23 +0000348 // If _reinterpret_input_as_3d = _reinterpret_output_as_3d = true,
349 // we will dispatch a batched-GEMM to reduce the complexity of the address calculation within the OpenCL kernel.
350 // This means that the actual m used by the kernel is given by output->info()->dimension(1) and not by gemm_info.m
351 const unsigned int internal_m = _reinterpret_output_as_3d ? gemm_info.m : output->info()->dimension(1);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100352 // Calculate partial (store instead of load) M0 and partial N0 for the partial blocks at the end of a row/column if any. This is to avoid padding.
Manuel Bottini488f5082020-10-29 13:51:23 +0000353 const unsigned int partial_store_m0 = internal_m % lhs_info.m0;
354 const unsigned int partial_store_n0 = gemm_info.n % rhs_info.n0;
355
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000356 // Create build options
357 CLBuildOptions build_opts;
358 build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
359 build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D");
360 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(1)));
361 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(2)));
362 build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(input1->info()->dimension(2)));
363 build_opts.add_option_if(rhs_info.interleave, "-DRHS_INTERLEAVE");
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100364 build_opts.add_option_if(_use_dummy_work_items, "-DDUMMY_WORK_ITEMS");
365 build_opts.add_option("-DM=" + support::cpp11::to_string(input0->info()->dimension(1)));
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000366 build_opts.add_option("-DN=" + support::cpp11::to_string(gemm_info.n));
367 build_opts.add_option("-DK=" + support::cpp11::to_string(gemm_info.k));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000368 build_opts.add_option("-DM0=" + support::cpp11::to_string(lhs_info.m0));
369 build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
370 build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
371 build_opts.add_option("-DH0=" + support::cpp11::to_string(rhs_info.h0));
Manuel Bottini488f5082020-10-29 13:51:23 +0000372 build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(partial_store_m0));
373 build_opts.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(partial_store_n0));
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000374 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input0->info()->data_type()));
375 build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_dot8_acc_type_from_data_type(input0->info()->data_type()));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000376
377 std::string kernel_name("gemmlowp_mm_reshaped_only_rhs_");
378 kernel_name += rhs_info.transpose ? "t" : "nt";
379
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000380 if(output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
381 {
382 kernel_name += "_fused_output_stage_fixedpoint";
383 _fuse_output_stage = true;
384 // If a_offset == 0, vector_sum_col can be a nullptr
385 if(a_offset != 0)
386 {
387 build_opts.add_option("-DA_OFFSET=" + support::cpp11::to_string(a_offset));
388 build_opts.add_option_if(vector_sum_col->info()->tensor_shape().num_dimensions() > 1, "-DSUM_COL_HAS_BATCHES");
389 }
390 // If b_offset == 0, vector_sum_row can be a nullptr
391 build_opts.add_option_if(b_offset != 0, "-DB_OFFSET=" + support::cpp11::to_string(b_offset));
392 build_opts.add_option("-DK_OFFSET=" + support::cpp11::to_string(a_offset * b_offset * input0->info()->dimension(0)));
393 build_opts.add_option_if(bias != nullptr, "-DADD_BIAS");
394 build_opts.add_option("-DRESULT_OFFSET=" + support::cpp11::to_string(output_stage.gemmlowp_offset));
395 build_opts.add_option("-DRESULT_MULTIPLIER=" + support::cpp11::to_string(output_stage.gemmlowp_multipliers[0]));
396 build_opts.add_option("-DRESULT_SHIFT=" + support::cpp11::to_string(output_stage.gemmlowp_shifts[0]));
397 build_opts.add_option_if(_is_quantized_per_channel, "-DPER_CHANNEL_QUANTIZATION");
398
399 const int min = output_stage.gemmlowp_min_bound;
400 const int max = output_stage.gemmlowp_max_bound;
401
402 PixelValue min_val{};
403 PixelValue max_val{};
404 std::tie(min_val, max_val) = get_min_max(output->info()->data_type());
Michele Di Giorgio398b8e42020-03-06 13:56:54 +0000405 build_opts.add_option_if(min != min_val.get<int32_t>(), "-DMIN_BOUND=" + support::cpp11::to_string(min));
406 build_opts.add_option_if(max != max_val.get<int32_t>(), "-DMAX_BOUND=" + support::cpp11::to_string(max));
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000407 }
408
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000409 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100410 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000411
412 // Set config_id for enabling LWS tuning
413 _config_id = kernel_name;
414 _config_id += "_";
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100415 _config_id += dot8_supported(CLKernelLibrary::get().get_device()) ? "_dot8" : "";
416 _config_id += "_";
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000417 _config_id += (_reinterpret_input_as_3d ? "3di_" : "");
418 _config_id += (_reinterpret_output_as_3d ? "3do_" : "");
419 _config_id += support::cpp11::to_string(output->info()->dimension(1));
420 _config_id += "_";
421 _config_id += support::cpp11::to_string(output->info()->dimension(0));
422 _config_id += "_";
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000423 _config_id += support::cpp11::to_string(gemm_info.k);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000424 _config_id += "_";
425 _config_id += support::cpp11::to_string(output->info()->dimension(2));
426 _config_id += "_";
427 _config_id += support::cpp11::to_string(lhs_info.m0);
428 _config_id += "_";
429 _config_id += support::cpp11::to_string(rhs_info.n0);
430 _config_id += "_";
431 _config_id += support::cpp11::to_string(rhs_info.k0);
432 _config_id += "_";
433 _config_id += support::cpp11::to_string(rhs_info.h0);
434 _config_id += "_";
435 _config_id += support::cpp11::to_string(rhs_info.interleave);
Manuel Bottini488f5082020-10-29 13:51:23 +0000436 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000437}
438
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000439Status CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, const GEMMKernelInfo &gemm_info,
440 const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias,
441 const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000442{
443 ElementsProcessed num_elements_processed{};
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000444 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output, gemm_info, vector_sum_col, vector_sum_row, bias, output_multipliers, output_shifts));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000445 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
446 input1->clone().get(),
447 output->clone().get(),
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000448 gemm_info,
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000449 vector_sum_col != nullptr ? vector_sum_col->clone().get() : nullptr,
450 vector_sum_row != nullptr ? vector_sum_row->clone().get() : nullptr,
451 bias != nullptr ? bias->clone().get() : nullptr,
452 output_multipliers != nullptr ? output_multipliers->clone().get() : nullptr,
453 output_shifts != nullptr ? output_shifts->clone().get() : nullptr,
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000454 num_elements_processed)
455 .first);
456
457 return Status{};
458}
459
460void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::run(const Window &window, cl::CommandQueue &queue)
461{
462 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
463 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
464
465 if(_input1->info()->num_dimensions() < 3)
466 {
467 // The stride_z for matrix B must be zero if we do not slice
468 ARM_COMPUTE_ERROR_ON(_input1->info()->strides_in_bytes()[3] != 0);
469 }
470
471 Window slice = window.first_slice_window_3D();
472 Window slice_matrix_b = slice;
473
474 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
475 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
476
477 if(_reinterpret_input_as_3d)
478 {
479 // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
480 const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3;
481 const unsigned int total_cross_plane_pad = _input0->info()->padding().top + _input0->info()->padding().bottom;
482 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
483 }
484
485 if(_reinterpret_output_as_3d)
486 {
487 // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor
488 const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3 + (_reinterpret_input_as_3d ? 1 : 0);
489 const unsigned int total_cross_plane_pad = _output->info()->padding().top + _output->info()->padding().bottom;
490 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
491 }
492
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000493 // Set window for vector_sum_col
494 Window win_vector_sum_col = slice;
495 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
496 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
497
498 // Set window for vector_sum_row
499 Window win_vector_sum_row = slice;
500 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
501 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
502 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
503
504 Window biases_slice = slice;
505 biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1));
506 biases_slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
507
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000508 do
509 {
510 Window slice_b = slice;
511 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
512 // This scenario can happen when the matrix multiplication is used to perform a convolution operation
513 if(!_slide_matrix_b)
514 {
515 slice_b = slice_matrix_b;
516 }
517
518 unsigned int idx = 0;
519 add_2D_tensor_argument(idx, _input0, slice);
520 add_2D_tensor_argument(idx, _input1, slice_b);
521 add_2D_tensor_argument(idx, _output, slice);
522 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input0->info()->strides_in_bytes()[2]));
523 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input1->info()->strides_in_bytes()[2]));
524 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[2]));
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000525 if(_reinterpret_input_as_3d)
526 {
527 // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
528 idx++;
529 }
530
531 if(_reinterpret_output_as_3d)
532 {
533 // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor
534 idx++;
535 }
536
537 if(_fuse_output_stage)
538 {
539 add_2D_tensor_argument_if((_vector_sum_col != nullptr), idx, _vector_sum_col, win_vector_sum_col);
540 add_2D_tensor_argument_if((_vector_sum_row != nullptr), idx, _vector_sum_row, win_vector_sum_row);
541 add_1D_tensor_argument_if((_bias != nullptr), idx, _bias, biases_slice);
542 add_1D_tensor_argument_if(_is_quantized_per_channel, idx, _output_multipliers, biases_slice);
543 add_1D_tensor_argument_if(_is_quantized_per_channel, idx, _output_shifts, biases_slice);
544 }
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100545 enqueue(queue, *this, slice, lws_hint(), _use_dummy_work_items);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000546 }
547 while(window.slide_window_slice_3D(slice));
548}
giuros0146a49a02019-04-01 13:50:22 +0100549} // namespace arm_compute