blob: c4ed691f2e9b7102e09f4b0ffbe3dabf60cf47e0 [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
163 PixelValue min_val{};
164 PixelValue max_val{};
165 if(output->total_size() != 0)
166 {
167 ARM_COMPUTE_RETURN_ERROR_ON(output_stage.output_data_type != output->data_type());
168 std::tie(min_val, max_val) = get_min_max(output->data_type());
169 ARM_COMPUTE_RETURN_ERROR_ON(output_stage.gemmlowp_max_bound > max_val.get<int32_t>());
170 ARM_COMPUTE_RETURN_ERROR_ON(output_stage.gemmlowp_min_bound < min_val.get<int32_t>() || output_stage.gemmlowp_min_bound > output_stage.gemmlowp_max_bound);
171 }
172 else
173 {
174 std::tie(min_val, max_val) = get_min_max(output_stage.output_data_type);
175 ARM_COMPUTE_RETURN_ERROR_ON(output_stage.gemmlowp_max_bound > max_val.get<int32_t>());
176 ARM_COMPUTE_RETURN_ERROR_ON(output_stage.gemmlowp_min_bound < min_val.get<int32_t>() || output_stage.gemmlowp_min_bound > output_stage.gemmlowp_max_bound);
177 }
178
179 if(output_multipliers != nullptr && output_shifts != nullptr)
180 {
181 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_multipliers, 1, DataType::S32);
182 ARM_COMPUTE_RETURN_ERROR_ON(output_multipliers->num_dimensions() > 1);
183 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_shifts, 1, DataType::S32);
184 ARM_COMPUTE_RETURN_ERROR_ON(output_shifts->num_dimensions() > 1);
185 if(output_stage.is_quantized_per_channel)
186 {
187 ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != output_shifts->dimension(0));
188 ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != output_multipliers->dimension(0));
189 }
190 }
191 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000192 return Status{};
193}
194
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000195std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output, const GEMMKernelInfo &gemm_info,
196 ITensorInfo *vector_sum_col, ITensorInfo *vector_sum_row, ITensorInfo *bias,
197 ITensorInfo *output_multipliers, ITensorInfo *output_shifts, ElementsProcessed &num_elements_processed)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000198{
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000199 const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
200
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000201 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
202 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000203 bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
204 bool reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d != 0);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000205
206 Window win{};
207 Window win_out{};
208 bool window_changed = false;
209
210 // In case both input and output have to be reinterpreted as 3D tensors,
211 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100212 if(reinterpret_input_as_3d == reinterpret_output_as_3d)
213 {
214 reinterpret_output_as_3d = false;
215 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000216
217 // Output tensor auto initialization if not yet initialized
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000218 const TensorShape expected_output_shape = compute_mm_shape(*input0, *input1, gemm_info);
219 if(output_stage.type != GEMMLowpOutputStageType::NONE)
220 {
221 auto_init_if_empty(*output, input0->clone()->set_tensor_shape(expected_output_shape).set_data_type(output_stage.output_data_type));
222 }
223 else
224 {
225 auto_init_if_empty(*output, input0->clone()->set_tensor_shape(expected_output_shape).set_data_type(DataType::S32));
226 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000227
228 TensorInfo tmp_info(*output);
229
230 if(reinterpret_output_as_3d)
231 {
232 // Since the output tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
233 // the window needs to be constructed on the 2D collapsed version of the tensor
234 TensorShape tmp_shape(output->tensor_shape());
235 tmp_shape.collapse(2U, 1U);
236 tmp_info.set_tensor_shape(tmp_shape);
237 }
238
239 // Configure kernel window
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000240 num_elems_processed_per_iteration_x = gemm_info.rhs_info.n0;
241 num_elems_processed_per_iteration_y = gemm_info.lhs_info.m0;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000242
243 // Note: bottom paddings are calculated manually as the output can be reinterpreted as 3D tensor
244 // 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 +0000245 const int m = reinterpret_output_as_3d ? gemm_info.m : input0->dimension(1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000246 const int bottom_pad = (num_elems_processed_per_iteration_y - (m % num_elems_processed_per_iteration_y)) % num_elems_processed_per_iteration_y;
247
248 win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
249 win_out = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
250
251 AccessWindowStatic input0_access(input0, 0, 0,
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000252 ceil_to_multiple(input0->dimension(0), gemm_info.lhs_info.k0),
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000253 input0->dimension(1) + bottom_pad);
254 AccessWindowStatic input1_access(input1, 0, 0,
255 input1->dimension(0),
256 input1->dimension(1));
257 AccessWindowStatic output_access(output, 0, 0,
258 ceil_to_multiple(output->dimension(0), num_elems_processed_per_iteration_x),
259 output->dimension(1) + bottom_pad);
260
261 window_changed = update_window_and_padding(win, input0_access, input1_access) || // window used by the execute_window_loop
262 update_window_and_padding(win_out, output_access); // window used to update the padding requirements of output tensor
263
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000264 if(output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
265 {
266 if(gemm_info.a_offset != 0)
267 {
268 AccessWindowHorizontal vector_sum_col_access(vector_sum_col, 0, num_elems_processed_per_iteration_x);
269 window_changed = window_changed || update_window_and_padding(win_out, vector_sum_col_access);
270 }
271 // No access window needed for vector_sum_row
272 ARM_COMPUTE_UNUSED(vector_sum_row);
273
274 if(bias != nullptr)
275 {
276 AccessWindowHorizontal bias_access(bias, 0, num_elems_processed_per_iteration_x);
277 window_changed = window_changed || update_window_and_padding(win_out, bias_access);
278 }
279
280 if(output_multipliers != nullptr && output_multipliers->dimension(0) > 1)
281 {
282 AccessWindowHorizontal output_multipliers_access(output_multipliers, 0, num_elems_processed_per_iteration_x);
283 AccessWindowHorizontal output_shifts_access(output_shifts, 0, num_elems_processed_per_iteration_x);
284 window_changed = window_changed || update_window_and_padding(win_out, output_multipliers_access, output_shifts_access);
285 }
286 }
287
Gian Marco Iodice2ec6c1e2019-04-09 12:03:05 +0100288 output_access.set_valid_region(win_out, ValidRegion(Coordinates(), output->tensor_shape()));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000289
290 // Collapse along the Z direction
291 // This collapse needs to be here in order to tune the Z dimension of LWS
292 Window collapsed = win;
293 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(output->num_dimensions()), 2u);
294 collapsed = win.collapse(win, dimension_to_collapse);
295
296 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
297 return std::make_pair(err, collapsed);
298}
299} // namespace
300
301CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel()
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000302 : _input0(nullptr),
303 _input1(nullptr),
304 _output(nullptr),
305 _vector_sum_col(nullptr),
306 _vector_sum_row(nullptr),
307 _bias(nullptr),
308 _output_multipliers(nullptr),
309 _output_shifts(nullptr),
310 _slide_matrix_b(true),
311 _reinterpret_input_as_3d(false),
312 _reinterpret_output_as_3d(false),
313 _use_dummy_work_items(false),
314 _is_quantized_per_channel(false),
315 _fuse_output_stage(false)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000316{
317}
318
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000319void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, const GEMMKernelInfo &gemm_info,
320 const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias,
321 const ICLTensor *output_multipliers, const ICLTensor *output_shifts)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000322{
323 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000324 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(),
325 input1->info(),
326 output->info(),
327 gemm_info,
328 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr,
329 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr,
330 bias != nullptr ? bias->info() : nullptr,
331 output_multipliers != nullptr ? output_multipliers->info() : nullptr,
332 output_shifts != nullptr ? output_shifts->info() : nullptr));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000333
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000334 const GEMMRHSMatrixInfo rhs_info = gemm_info.rhs_info;
335 const GEMMLHSMatrixInfo lhs_info = gemm_info.lhs_info;
336 const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
337 const int32_t a_offset = gemm_info.a_offset;
338 const int32_t b_offset = gemm_info.b_offset;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000339
340 _input0 = input0;
341 _input1 = input1;
342 _output = output;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000343 _vector_sum_col = vector_sum_col;
344 _vector_sum_row = vector_sum_row;
345 _bias = bias;
346 _output_multipliers = output_multipliers;
347 _output_shifts = output_shifts;
348 _reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
349 _reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d != 0);
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100350 _use_dummy_work_items = preferred_dummy_work_items_support(CLKernelLibrary::get().get_device());
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000351 _is_quantized_per_channel = output_stage.is_quantized_per_channel;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000352
353 // In case both input and output have to be reinterpreted as 3D tensors,
354 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100355 if(_reinterpret_input_as_3d == _reinterpret_output_as_3d)
356 {
357 _reinterpret_input_as_3d = false;
358 _reinterpret_output_as_3d = false;
359 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000360
361 // Check if we need to slide the matrix B
362 const unsigned int num_dimensions_input0 = _input0->info()->num_dimensions();
363 _slide_matrix_b = (_input1->info()->num_dimensions() >= num_dimensions_input0);
364
365 ElementsProcessed num_elements_processed{};
366
367 // Configure kernel window
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000368 auto win_config = validate_and_configure_window(input0->info(),
369 input1->info(),
370 output->info(),
371 gemm_info,
372 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr,
373 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr,
374 bias != nullptr ? bias->info() : nullptr,
375 output_multipliers != nullptr ? output_multipliers->info() : nullptr,
376 output_shifts != nullptr ? output_shifts->info() : nullptr,
377 num_elements_processed);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000378 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
379 ICLKernel::configure_internal(win_config.second);
380
381 // Create build options
382 CLBuildOptions build_opts;
383 build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
384 build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D");
385 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(1)));
386 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(2)));
387 build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(input1->info()->dimension(2)));
388 build_opts.add_option_if(rhs_info.interleave, "-DRHS_INTERLEAVE");
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100389 build_opts.add_option_if(_use_dummy_work_items, "-DDUMMY_WORK_ITEMS");
390 build_opts.add_option("-DM=" + support::cpp11::to_string(input0->info()->dimension(1)));
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000391 build_opts.add_option("-DN=" + support::cpp11::to_string(gemm_info.n));
392 build_opts.add_option("-DK=" + support::cpp11::to_string(gemm_info.k));
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000393 build_opts.add_option("-DM0=" + support::cpp11::to_string(lhs_info.m0));
394 build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
395 build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
396 build_opts.add_option("-DH0=" + support::cpp11::to_string(rhs_info.h0));
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000397 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input0->info()->data_type()));
398 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 +0000399
400 std::string kernel_name("gemmlowp_mm_reshaped_only_rhs_");
401 kernel_name += rhs_info.transpose ? "t" : "nt";
402
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000403 if(output_stage.type == GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)
404 {
405 kernel_name += "_fused_output_stage_fixedpoint";
406 _fuse_output_stage = true;
407 // If a_offset == 0, vector_sum_col can be a nullptr
408 if(a_offset != 0)
409 {
410 build_opts.add_option("-DA_OFFSET=" + support::cpp11::to_string(a_offset));
411 build_opts.add_option_if(vector_sum_col->info()->tensor_shape().num_dimensions() > 1, "-DSUM_COL_HAS_BATCHES");
412 }
413 // If b_offset == 0, vector_sum_row can be a nullptr
414 build_opts.add_option_if(b_offset != 0, "-DB_OFFSET=" + support::cpp11::to_string(b_offset));
415 build_opts.add_option("-DK_OFFSET=" + support::cpp11::to_string(a_offset * b_offset * input0->info()->dimension(0)));
416 build_opts.add_option_if(bias != nullptr, "-DADD_BIAS");
417 build_opts.add_option("-DRESULT_OFFSET=" + support::cpp11::to_string(output_stage.gemmlowp_offset));
418 build_opts.add_option("-DRESULT_MULTIPLIER=" + support::cpp11::to_string(output_stage.gemmlowp_multipliers[0]));
419 build_opts.add_option("-DRESULT_SHIFT=" + support::cpp11::to_string(output_stage.gemmlowp_shifts[0]));
420 build_opts.add_option_if(_is_quantized_per_channel, "-DPER_CHANNEL_QUANTIZATION");
421
422 const int min = output_stage.gemmlowp_min_bound;
423 const int max = output_stage.gemmlowp_max_bound;
424
425 PixelValue min_val{};
426 PixelValue max_val{};
427 std::tie(min_val, max_val) = get_min_max(output->info()->data_type());
428 build_opts.add_option_if((min != min_val.get<int32_t>()) && (min != max), "-DMIN_BOUND=" + support::cpp11::to_string(min));
429 build_opts.add_option_if((max != max_val.get<int32_t>()) && (min != max), "-DMAX_BOUND=" + support::cpp11::to_string(max));
430 }
431
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000432 // Create kernel
433 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
434
435 // Set config_id for enabling LWS tuning
436 _config_id = kernel_name;
437 _config_id += "_";
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100438 _config_id += dot8_supported(CLKernelLibrary::get().get_device()) ? "_dot8" : "";
439 _config_id += "_";
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000440 _config_id += (_reinterpret_input_as_3d ? "3di_" : "");
441 _config_id += (_reinterpret_output_as_3d ? "3do_" : "");
442 _config_id += support::cpp11::to_string(output->info()->dimension(1));
443 _config_id += "_";
444 _config_id += support::cpp11::to_string(output->info()->dimension(0));
445 _config_id += "_";
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000446 _config_id += support::cpp11::to_string(gemm_info.k);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000447 _config_id += "_";
448 _config_id += support::cpp11::to_string(output->info()->dimension(2));
449 _config_id += "_";
450 _config_id += support::cpp11::to_string(lhs_info.m0);
451 _config_id += "_";
452 _config_id += support::cpp11::to_string(rhs_info.n0);
453 _config_id += "_";
454 _config_id += support::cpp11::to_string(rhs_info.k0);
455 _config_id += "_";
456 _config_id += support::cpp11::to_string(rhs_info.h0);
457 _config_id += "_";
458 _config_id += support::cpp11::to_string(rhs_info.interleave);
459}
460
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000461Status CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, const GEMMKernelInfo &gemm_info,
462 const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias,
463 const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000464{
465 ElementsProcessed num_elements_processed{};
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000466 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 +0000467 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
468 input1->clone().get(),
469 output->clone().get(),
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000470 gemm_info,
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000471 vector_sum_col != nullptr ? vector_sum_col->clone().get() : nullptr,
472 vector_sum_row != nullptr ? vector_sum_row->clone().get() : nullptr,
473 bias != nullptr ? bias->clone().get() : nullptr,
474 output_multipliers != nullptr ? output_multipliers->clone().get() : nullptr,
475 output_shifts != nullptr ? output_shifts->clone().get() : nullptr,
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000476 num_elements_processed)
477 .first);
478
479 return Status{};
480}
481
482void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::run(const Window &window, cl::CommandQueue &queue)
483{
484 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
485 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
486
487 if(_input1->info()->num_dimensions() < 3)
488 {
489 // The stride_z for matrix B must be zero if we do not slice
490 ARM_COMPUTE_ERROR_ON(_input1->info()->strides_in_bytes()[3] != 0);
491 }
492
493 Window slice = window.first_slice_window_3D();
494 Window slice_matrix_b = slice;
495
496 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
497 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
498
499 if(_reinterpret_input_as_3d)
500 {
501 // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
502 const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3;
503 const unsigned int total_cross_plane_pad = _input0->info()->padding().top + _input0->info()->padding().bottom;
504 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
505 }
506
507 if(_reinterpret_output_as_3d)
508 {
509 // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor
510 const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3 + (_reinterpret_input_as_3d ? 1 : 0);
511 const unsigned int total_cross_plane_pad = _output->info()->padding().top + _output->info()->padding().bottom;
512 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
513 }
514
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000515 // Set window for vector_sum_col
516 Window win_vector_sum_col = slice;
517 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
518 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
519
520 // Set window for vector_sum_row
521 Window win_vector_sum_row = slice;
522 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
523 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
524 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
525
526 Window biases_slice = slice;
527 biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1));
528 biases_slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
529
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000530 do
531 {
532 Window slice_b = slice;
533 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
534 // This scenario can happen when the matrix multiplication is used to perform a convolution operation
535 if(!_slide_matrix_b)
536 {
537 slice_b = slice_matrix_b;
538 }
539
540 unsigned int idx = 0;
541 add_2D_tensor_argument(idx, _input0, slice);
542 add_2D_tensor_argument(idx, _input1, slice_b);
543 add_2D_tensor_argument(idx, _output, slice);
544 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input0->info()->strides_in_bytes()[2]));
545 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input1->info()->strides_in_bytes()[2]));
546 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[2]));
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000547 if(_reinterpret_input_as_3d)
548 {
549 // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
550 idx++;
551 }
552
553 if(_reinterpret_output_as_3d)
554 {
555 // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor
556 idx++;
557 }
558
559 if(_fuse_output_stage)
560 {
561 add_2D_tensor_argument_if((_vector_sum_col != nullptr), idx, _vector_sum_col, win_vector_sum_col);
562 add_2D_tensor_argument_if((_vector_sum_row != nullptr), idx, _vector_sum_row, win_vector_sum_row);
563 add_1D_tensor_argument_if((_bias != nullptr), idx, _bias, biases_slice);
564 add_1D_tensor_argument_if(_is_quantized_per_channel, idx, _output_multipliers, biases_slice);
565 add_1D_tensor_argument_if(_is_quantized_per_channel, idx, _output_shifts, biases_slice);
566 }
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100567 enqueue(queue, *this, slice, lws_hint(), _use_dummy_work_items);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000568 }
569 while(window.slide_window_slice_3D(slice));
570}
giuros0146a49a02019-04-01 13:50:22 +0100571} // namespace arm_compute