blob: 71b4d76520a001af9d596afcb997467e6544f789 [file] [log] [blame]
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001/*
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00002 * 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 */
24#include "arm_compute/core/CL/kernels/CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Utils.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/core/Window.h"
38#include "arm_compute/core/utils/misc/ShapeCalculator.h"
39#include "support/ToolchainSupport.h"
40
41#include <cstddef>
42#include <cstdint>
43#include <tuple>
44
45using namespace arm_compute::misc::shape_calculator;
46
47namespace arm_compute
48{
49namespace
50{
51using ElementsProcessed = Steps;
52
Michele Di Giorgiob54ba282020-01-14 15:31:55 +000053Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, const GEMMKernelInfo &gemm_info,
54 const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias,
55 const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts)
Gian Marco Iodice62251f72019-03-11 16:07:12 +000056{
57 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output);
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000058 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
Gian Marco Iodice62251f72019-03-11 16:07:12 +000059 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1);
60 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input0->num_dimensions() > 4, "The number of dimensions for the LHS matrix must be <= 4");
61 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 +000062
63 const GEMMRHSMatrixInfo rhs_info = gemm_info.rhs_info;
64 const GEMMLHSMatrixInfo lhs_info = gemm_info.lhs_info;
65 const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
66
Gian Marco Iodice62251f72019-03-11 16:07:12 +000067 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");
68 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.m0 < 1 || lhs_info.m0 > 8);
69 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");
70
Michele Di Giorgiob54ba282020-01-14 15:31:55 +000071 const int m = gemm_info.m;
72 const int n = gemm_info.n;
73 const int k = gemm_info.k;
Gian Marco Iodice62251f72019-03-11 16:07:12 +000074
Gian Marco Iodice62251f72019-03-11 16:07:12 +000075 TensorShape tensor_shape1{ input1->tensor_shape() };
76 tensor_shape1.set(0, n);
77 tensor_shape1.set(1, k);
78
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +010079 const TensorInfo tensor_info1 = input1->clone()->set_tensor_shape(tensor_shape1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +000080 const TensorInfo tensor_info_reshaped1 = input1->clone()->set_tensor_shape(compute_rhs_reshaped_shape(tensor_info1, rhs_info));
81
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +010082 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(0) != static_cast<unsigned int>(k));
Michele Di Giorgiob54ba282020-01-14 15:31:55 +000083 if(gemm_info.reinterpret_input_as_3d)
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +010084 {
85 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) * input0->dimension(2) != static_cast<unsigned int>(m));
86 }
87 else
88 {
89 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) != static_cast<unsigned int>(m));
90 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +000091 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, &tensor_info_reshaped1);
92
Michele Di Giorgiob54ba282020-01-14 15:31:55 +000093 const TensorShape expected_output_shape = compute_mm_shape(*input0, *input1, gemm_info);
Gian Marco Iodice62251f72019-03-11 16:07:12 +000094 if(output->total_size() != 0)
95 {
Michele Di Giorgiob54ba282020-01-14 15:31:55 +000096 const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(expected_output_shape);
Gian Marco Iodice62251f72019-03-11 16:07:12 +000097 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +000098 if(output_stage.type == GEMMLowpOutputStageType::NONE)
99 {
100 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
101 }
102 else
103 {
104 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, output);
105 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000106 }
107
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000108 if(bias != nullptr)
109 {
110 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
111 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
112 ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != bias->dimension(0));
113 }
114
115 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN) || (output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT),
116 "Only GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT is supported");
117
118 // Checks performed if the output stage needs to be fused
119 if(output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
120 {
121 // If a_offset == 0, vector_sum_col can be a nullptr
122 if(gemm_info.a_offset != 0)
123 {
124 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_col, 1, DataType::S32);
125 ARM_COMPUTE_RETURN_ERROR_ON(vector_sum_col->dimension(0) != expected_output_shape[0]);
126 }
127
128 // If b_offset == 0, vector_sum_row can be a nullptr
129 if(gemm_info.b_offset != 0)
130 {
131 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_row, 1, DataType::S32);
132
133 // Check if mm result is a 3D reinterpretation
134 const bool reinterpret_as_3d = expected_output_shape.num_dimensions() > 1 && expected_output_shape.y() != vector_sum_row->tensor_shape().x();
135
136 // Validate input
137 ARM_COMPUTE_RETURN_ERROR_ON(reinterpret_as_3d && vector_sum_row->dimension(0) != (expected_output_shape[1] * expected_output_shape[2]));
138 ARM_COMPUTE_RETURN_ERROR_ON(!reinterpret_as_3d && vector_sum_row->dimension(0) != expected_output_shape[1]);
139
140 if(expected_output_shape.num_dimensions() > 1)
141 {
142 const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2;
143
144 TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
145 vector_sum_row_shape.collapse_from(1);
146 TensorShape collapsed_output_shape(expected_output_shape);
147 collapsed_output_shape.collapse_from(output_batch_idx);
148
149 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != collapsed_output_shape[output_batch_idx],
150 "vector_sum_row must have the same number of batches of output tensor");
151
152 if(gemm_info.a_offset != 0)
153 {
154 TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
155 vector_sum_col_shape.collapse_from(1);
156
157 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_col_shape[1] != 1 && vector_sum_col_shape[1] != vector_sum_row_shape[1],
158 "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");
159 }
160 }
161 }
162
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000163 if(output->total_size() != 0)
164 {
165 ARM_COMPUTE_RETURN_ERROR_ON(output_stage.output_data_type != output->data_type());
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000166 }
Michele Di Giorgio398b8e42020-03-06 13:56:54 +0000167 ARM_COMPUTE_RETURN_ERROR_ON(output_stage.gemmlowp_min_bound > output_stage.gemmlowp_max_bound);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000168
169 if(output_multipliers != nullptr && output_shifts != nullptr)
170 {
171 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_multipliers, 1, DataType::S32);
172 ARM_COMPUTE_RETURN_ERROR_ON(output_multipliers->num_dimensions() > 1);
173 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_shifts, 1, DataType::S32);
174 ARM_COMPUTE_RETURN_ERROR_ON(output_shifts->num_dimensions() > 1);
175 if(output_stage.is_quantized_per_channel)
176 {
177 ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != output_shifts->dimension(0));
178 ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != output_multipliers->dimension(0));
179 }
180 }
181 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000182 return Status{};
183}
184
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000185std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output, const GEMMKernelInfo &gemm_info,
186 ITensorInfo *vector_sum_col, ITensorInfo *vector_sum_row, ITensorInfo *bias,
187 ITensorInfo *output_multipliers, ITensorInfo *output_shifts, ElementsProcessed &num_elements_processed)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000188{
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000189 const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
190
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000191 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
192 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000193 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
194 bool reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d != 0);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000195
196 Window win{};
197 Window win_out{};
198 bool window_changed = false;
199
200 // In case both input and output have to be reinterpreted as 3D tensors,
201 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100202 if(reinterpret_input_as_3d == reinterpret_output_as_3d)
203 {
204 reinterpret_output_as_3d = false;
205 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000206
207 // Output tensor auto initialization if not yet initialized
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000208 const TensorShape expected_output_shape = compute_mm_shape(*input0, *input1, gemm_info);
209 if(output_stage.type != GEMMLowpOutputStageType::NONE)
210 {
211 auto_init_if_empty(*output, input0->clone()->set_tensor_shape(expected_output_shape).set_data_type(output_stage.output_data_type));
212 }
213 else
214 {
215 auto_init_if_empty(*output, input0->clone()->set_tensor_shape(expected_output_shape).set_data_type(DataType::S32));
216 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000217
218 TensorInfo tmp_info(*output);
219
220 if(reinterpret_output_as_3d)
221 {
222 // Since the output tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
223 // the window needs to be constructed on the 2D collapsed version of the tensor
224 TensorShape tmp_shape(output->tensor_shape());
225 tmp_shape.collapse(2U, 1U);
226 tmp_info.set_tensor_shape(tmp_shape);
227 }
228
229 // Configure kernel window
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000230 num_elems_processed_per_iteration_x = gemm_info.rhs_info.n0;
231 num_elems_processed_per_iteration_y = gemm_info.lhs_info.m0;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000232
233 // Note: bottom paddings are calculated manually as the output can be reinterpreted as 3D tensor
234 // The only way to set properly the paddings, it is to set those explicitly through the AccessWindowStatic
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000235 const int m = reinterpret_output_as_3d ? gemm_info.m : input0->dimension(1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000236 const int bottom_pad = (num_elems_processed_per_iteration_y - (m % num_elems_processed_per_iteration_y)) % num_elems_processed_per_iteration_y;
237
238 win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
239 win_out = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
240
241 AccessWindowStatic input0_access(input0, 0, 0,
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000242 ceil_to_multiple(input0->dimension(0), gemm_info.lhs_info.k0),
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000243 input0->dimension(1) + bottom_pad);
244 AccessWindowStatic input1_access(input1, 0, 0,
245 input1->dimension(0),
246 input1->dimension(1));
247 AccessWindowStatic output_access(output, 0, 0,
248 ceil_to_multiple(output->dimension(0), num_elems_processed_per_iteration_x),
249 output->dimension(1) + bottom_pad);
250
251 window_changed = update_window_and_padding(win, input0_access, input1_access) || // window used by the execute_window_loop
252 update_window_and_padding(win_out, output_access); // window used to update the padding requirements of output tensor
253
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000254 if(output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
255 {
256 if(gemm_info.a_offset != 0)
257 {
258 AccessWindowHorizontal vector_sum_col_access(vector_sum_col, 0, num_elems_processed_per_iteration_x);
259 window_changed = window_changed || update_window_and_padding(win_out, vector_sum_col_access);
260 }
261 // No access window needed for vector_sum_row
262 ARM_COMPUTE_UNUSED(vector_sum_row);
263
264 if(bias != nullptr)
265 {
266 AccessWindowHorizontal bias_access(bias, 0, num_elems_processed_per_iteration_x);
267 window_changed = window_changed || update_window_and_padding(win_out, bias_access);
268 }
269
270 if(output_multipliers != nullptr && output_multipliers->dimension(0) > 1)
271 {
272 AccessWindowHorizontal output_multipliers_access(output_multipliers, 0, num_elems_processed_per_iteration_x);
273 AccessWindowHorizontal output_shifts_access(output_shifts, 0, num_elems_processed_per_iteration_x);
274 window_changed = window_changed || update_window_and_padding(win_out, output_multipliers_access, output_shifts_access);
275 }
276 }
277
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100278 output_access.set_valid_region(win_out, ValidRegion(Coordinates(), output->tensor_shape()));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000279
280 // Collapse along the Z direction
281 // This collapse needs to be here in order to tune the Z dimension of LWS
282 Window collapsed = win;
283 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(output->num_dimensions()), 2u);
284 collapsed = win.collapse(win, dimension_to_collapse);
285
286 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
287 return std::make_pair(err, collapsed);
288}
289} // namespace
290
291CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel()
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000292 : _input0(nullptr),
293 _input1(nullptr),
294 _output(nullptr),
295 _vector_sum_col(nullptr),
296 _vector_sum_row(nullptr),
297 _bias(nullptr),
298 _output_multipliers(nullptr),
299 _output_shifts(nullptr),
300 _slide_matrix_b(true),
301 _reinterpret_input_as_3d(false),
302 _reinterpret_output_as_3d(false),
303 _use_dummy_work_items(false),
304 _is_quantized_per_channel(false),
305 _fuse_output_stage(false)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000306{
307}
308
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000309void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, const GEMMKernelInfo &gemm_info,
310 const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias,
311 const ICLTensor *output_multipliers, const ICLTensor *output_shifts)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000312{
313 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000314 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(),
315 input1->info(),
316 output->info(),
317 gemm_info,
318 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr,
319 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr,
320 bias != nullptr ? bias->info() : nullptr,
321 output_multipliers != nullptr ? output_multipliers->info() : nullptr,
322 output_shifts != nullptr ? output_shifts->info() : nullptr));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000323
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000324 const GEMMRHSMatrixInfo rhs_info = gemm_info.rhs_info;
325 const GEMMLHSMatrixInfo lhs_info = gemm_info.lhs_info;
326 const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
327 const int32_t a_offset = gemm_info.a_offset;
328 const int32_t b_offset = gemm_info.b_offset;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000329
330 _input0 = input0;
331 _input1 = input1;
332 _output = output;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000333 _vector_sum_col = vector_sum_col;
334 _vector_sum_row = vector_sum_row;
335 _bias = bias;
336 _output_multipliers = output_multipliers;
337 _output_shifts = output_shifts;
338 _reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
339 _reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d != 0);
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100340 _use_dummy_work_items = preferred_dummy_work_items_support(CLKernelLibrary::get().get_device());
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000341 _is_quantized_per_channel = output_stage.is_quantized_per_channel;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000342
343 // In case both input and output have to be reinterpreted as 3D tensors,
344 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100345 if(_reinterpret_input_as_3d == _reinterpret_output_as_3d)
346 {
347 _reinterpret_input_as_3d = false;
348 _reinterpret_output_as_3d = false;
349 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000350
351 // Check if we need to slide the matrix B
352 const unsigned int num_dimensions_input0 = _input0->info()->num_dimensions();
353 _slide_matrix_b = (_input1->info()->num_dimensions() >= num_dimensions_input0);
354
355 ElementsProcessed num_elements_processed{};
356
357 // Configure kernel window
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000358 auto win_config = validate_and_configure_window(input0->info(),
359 input1->info(),
360 output->info(),
361 gemm_info,
362 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr,
363 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr,
364 bias != nullptr ? bias->info() : nullptr,
365 output_multipliers != nullptr ? output_multipliers->info() : nullptr,
366 output_shifts != nullptr ? output_shifts->info() : nullptr,
367 num_elements_processed);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000368 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
369 ICLKernel::configure_internal(win_config.second);
370
371 // Create build options
372 CLBuildOptions build_opts;
373 build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
374 build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D");
375 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(1)));
376 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(2)));
377 build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(input1->info()->dimension(2)));
378 build_opts.add_option_if(rhs_info.interleave, "-DRHS_INTERLEAVE");
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100379 build_opts.add_option_if(_use_dummy_work_items, "-DDUMMY_WORK_ITEMS");
380 build_opts.add_option("-DM=" + support::cpp11::to_string(input0->info()->dimension(1)));
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000381 build_opts.add_option("-DN=" + support::cpp11::to_string(gemm_info.n));
382 build_opts.add_option("-DK=" + support::cpp11::to_string(gemm_info.k));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000383 build_opts.add_option("-DM0=" + support::cpp11::to_string(lhs_info.m0));
384 build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
385 build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
386 build_opts.add_option("-DH0=" + support::cpp11::to_string(rhs_info.h0));
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000387 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input0->info()->data_type()));
388 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 +0000389
390 std::string kernel_name("gemmlowp_mm_reshaped_only_rhs_");
391 kernel_name += rhs_info.transpose ? "t" : "nt";
392
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000393 if(output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
394 {
395 kernel_name += "_fused_output_stage_fixedpoint";
396 _fuse_output_stage = true;
397 // If a_offset == 0, vector_sum_col can be a nullptr
398 if(a_offset != 0)
399 {
400 build_opts.add_option("-DA_OFFSET=" + support::cpp11::to_string(a_offset));
401 build_opts.add_option_if(vector_sum_col->info()->tensor_shape().num_dimensions() > 1, "-DSUM_COL_HAS_BATCHES");
402 }
403 // If b_offset == 0, vector_sum_row can be a nullptr
404 build_opts.add_option_if(b_offset != 0, "-DB_OFFSET=" + support::cpp11::to_string(b_offset));
405 build_opts.add_option("-DK_OFFSET=" + support::cpp11::to_string(a_offset * b_offset * input0->info()->dimension(0)));
406 build_opts.add_option_if(bias != nullptr, "-DADD_BIAS");
407 build_opts.add_option("-DRESULT_OFFSET=" + support::cpp11::to_string(output_stage.gemmlowp_offset));
408 build_opts.add_option("-DRESULT_MULTIPLIER=" + support::cpp11::to_string(output_stage.gemmlowp_multipliers[0]));
409 build_opts.add_option("-DRESULT_SHIFT=" + support::cpp11::to_string(output_stage.gemmlowp_shifts[0]));
410 build_opts.add_option_if(_is_quantized_per_channel, "-DPER_CHANNEL_QUANTIZATION");
411
412 const int min = output_stage.gemmlowp_min_bound;
413 const int max = output_stage.gemmlowp_max_bound;
414
415 PixelValue min_val{};
416 PixelValue max_val{};
417 std::tie(min_val, max_val) = get_min_max(output->info()->data_type());
Michele Di Giorgio398b8e42020-03-06 13:56:54 +0000418 build_opts.add_option_if(min != min_val.get<int32_t>(), "-DMIN_BOUND=" + support::cpp11::to_string(min));
419 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 +0000420 }
421
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000422 // Create kernel
423 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
424
425 // Set config_id for enabling LWS tuning
426 _config_id = kernel_name;
427 _config_id += "_";
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100428 _config_id += dot8_supported(CLKernelLibrary::get().get_device()) ? "_dot8" : "";
429 _config_id += "_";
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000430 _config_id += (_reinterpret_input_as_3d ? "3di_" : "");
431 _config_id += (_reinterpret_output_as_3d ? "3do_" : "");
432 _config_id += support::cpp11::to_string(output->info()->dimension(1));
433 _config_id += "_";
434 _config_id += support::cpp11::to_string(output->info()->dimension(0));
435 _config_id += "_";
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000436 _config_id += support::cpp11::to_string(gemm_info.k);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000437 _config_id += "_";
438 _config_id += support::cpp11::to_string(output->info()->dimension(2));
439 _config_id += "_";
440 _config_id += support::cpp11::to_string(lhs_info.m0);
441 _config_id += "_";
442 _config_id += support::cpp11::to_string(rhs_info.n0);
443 _config_id += "_";
444 _config_id += support::cpp11::to_string(rhs_info.k0);
445 _config_id += "_";
446 _config_id += support::cpp11::to_string(rhs_info.h0);
447 _config_id += "_";
448 _config_id += support::cpp11::to_string(rhs_info.interleave);
449}
450
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000451Status CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, const GEMMKernelInfo &gemm_info,
452 const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias,
453 const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000454{
455 ElementsProcessed num_elements_processed{};
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000456 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 +0000457 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
458 input1->clone().get(),
459 output->clone().get(),
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000460 gemm_info,
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000461 vector_sum_col != nullptr ? vector_sum_col->clone().get() : nullptr,
462 vector_sum_row != nullptr ? vector_sum_row->clone().get() : nullptr,
463 bias != nullptr ? bias->clone().get() : nullptr,
464 output_multipliers != nullptr ? output_multipliers->clone().get() : nullptr,
465 output_shifts != nullptr ? output_shifts->clone().get() : nullptr,
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000466 num_elements_processed)
467 .first);
468
469 return Status{};
470}
471
472void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::run(const Window &window, cl::CommandQueue &queue)
473{
474 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
475 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
476
477 if(_input1->info()->num_dimensions() < 3)
478 {
479 // The stride_z for matrix B must be zero if we do not slice
480 ARM_COMPUTE_ERROR_ON(_input1->info()->strides_in_bytes()[3] != 0);
481 }
482
483 Window slice = window.first_slice_window_3D();
484 Window slice_matrix_b = slice;
485
486 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
487 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
488
489 if(_reinterpret_input_as_3d)
490 {
491 // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
492 const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3;
493 const unsigned int total_cross_plane_pad = _input0->info()->padding().top + _input0->info()->padding().bottom;
494 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
495 }
496
497 if(_reinterpret_output_as_3d)
498 {
499 // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor
500 const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3 + (_reinterpret_input_as_3d ? 1 : 0);
501 const unsigned int total_cross_plane_pad = _output->info()->padding().top + _output->info()->padding().bottom;
502 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
503 }
504
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000505 // Set window for vector_sum_col
506 Window win_vector_sum_col = slice;
507 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
508 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
509
510 // Set window for vector_sum_row
511 Window win_vector_sum_row = slice;
512 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
513 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
514 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
515
516 Window biases_slice = slice;
517 biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1));
518 biases_slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
519
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000520 do
521 {
522 Window slice_b = slice;
523 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
524 // This scenario can happen when the matrix multiplication is used to perform a convolution operation
525 if(!_slide_matrix_b)
526 {
527 slice_b = slice_matrix_b;
528 }
529
530 unsigned int idx = 0;
531 add_2D_tensor_argument(idx, _input0, slice);
532 add_2D_tensor_argument(idx, _input1, slice_b);
533 add_2D_tensor_argument(idx, _output, slice);
534 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input0->info()->strides_in_bytes()[2]));
535 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input1->info()->strides_in_bytes()[2]));
536 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[2]));
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000537 if(_reinterpret_input_as_3d)
538 {
539 // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
540 idx++;
541 }
542
543 if(_reinterpret_output_as_3d)
544 {
545 // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor
546 idx++;
547 }
548
549 if(_fuse_output_stage)
550 {
551 add_2D_tensor_argument_if((_vector_sum_col != nullptr), idx, _vector_sum_col, win_vector_sum_col);
552 add_2D_tensor_argument_if((_vector_sum_row != nullptr), idx, _vector_sum_row, win_vector_sum_row);
553 add_1D_tensor_argument_if((_bias != nullptr), idx, _bias, biases_slice);
554 add_1D_tensor_argument_if(_is_quantized_per_channel, idx, _output_multipliers, biases_slice);
555 add_1D_tensor_argument_if(_is_quantized_per_channel, idx, _output_shifts, biases_slice);
556 }
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100557 enqueue(queue, *this, slice, lws_hint(), _use_dummy_work_items);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000558 }
559 while(window.slide_window_slice_3D(slice));
560}
giuros0146a49a02019-04-01 13:50:22 +0100561} // namespace arm_compute