Compute Library
 20.08
CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 Arm Limited.
3  *
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  */
25 
31 #include "arm_compute/core/Error.h"
34 #include "arm_compute/core/Types.h"
35 #include "arm_compute/core/Utils.h"
39 #include "support/StringSupport.h"
40 
41 #include <cstddef>
42 #include <cstdint>
43 #include <tuple>
44 
46 
47 namespace arm_compute
48 {
49 namespace
50 {
51 using ElementsProcessed = Steps;
52 
53 Status 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)
56 {
57  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output);
59  if(input0->data_type() == DataType::QASYMM8)
60  {
62  }
63  else
64  {
66  }
67  ARM_COMPUTE_RETURN_ERROR_ON_MSG(input0->num_dimensions() > 4, "The number of dimensions for the LHS matrix must be <= 4");
68  ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->num_dimensions() > 3, "The number of dimensions for the RHS matrix must be <= 3");
69 
70  const GEMMRHSMatrixInfo rhs_info = gemm_info.rhs_info;
71  const GEMMLHSMatrixInfo lhs_info = gemm_info.lhs_info;
72  const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
73 
74  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");
75  ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.m0 < 1 || lhs_info.m0 > 8);
76  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");
77  ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.export_to_cl_image, "Export to CLImage not supported for quantized GEMM");
78 
79  const int m = gemm_info.m;
80  const int n = gemm_info.n;
81  const int k = gemm_info.k;
82 
83  TensorShape tensor_shape1{ input1->tensor_shape() };
84  tensor_shape1.set(0, n);
85  tensor_shape1.set(1, k);
86 
87  const TensorInfo tensor_info1 = input1->clone()->set_tensor_shape(tensor_shape1);
88  const TensorInfo tensor_info_reshaped1 = input1->clone()->set_tensor_shape(compute_rhs_reshaped_shape(tensor_info1, rhs_info));
89 
90  ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(0) != static_cast<unsigned int>(k));
91  if(gemm_info.reinterpret_input_as_3d)
92  {
93  ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) * input0->dimension(2) != static_cast<unsigned int>(m));
94  }
95  else
96  {
97  ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) != static_cast<unsigned int>(m));
98  }
99  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, &tensor_info_reshaped1);
100 
101  const TensorShape expected_output_shape = compute_mm_shape(*input0, *input1, gemm_info);
102  if(output->total_size() != 0)
103  {
104  const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(expected_output_shape);
105  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
106  if(output_stage.type == GEMMLowpOutputStageType::NONE)
107  {
109  }
110  else
111  {
113  }
114  }
115 
116  if(bias != nullptr)
117  {
119  ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
120  ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != bias->dimension(0));
121  }
122 
124  "Only GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT is supported");
125 
126  // Checks performed if the output stage needs to be fused
128  {
129  // If a_offset == 0, vector_sum_col can be a nullptr
130  if(gemm_info.a_offset != 0)
131  {
133  ARM_COMPUTE_RETURN_ERROR_ON(vector_sum_col->dimension(0) != expected_output_shape[0]);
134  }
135 
136  // If b_offset == 0, vector_sum_row can be a nullptr
137  if(gemm_info.b_offset != 0)
138  {
140 
141  // Check if mm result is a 3D reinterpretation
142  const bool reinterpret_as_3d = expected_output_shape.num_dimensions() > 1 && expected_output_shape.y() != vector_sum_row->tensor_shape().x();
143 
144  // Validate input
145  ARM_COMPUTE_RETURN_ERROR_ON(reinterpret_as_3d && vector_sum_row->dimension(0) != (expected_output_shape[1] * expected_output_shape[2]));
146  ARM_COMPUTE_RETURN_ERROR_ON(!reinterpret_as_3d && vector_sum_row->dimension(0) != expected_output_shape[1]);
147 
148  if(expected_output_shape.num_dimensions() > 1)
149  {
150  const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2;
151 
152  TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
153  vector_sum_row_shape.collapse_from(1);
154  TensorShape collapsed_output_shape(expected_output_shape);
155  collapsed_output_shape.collapse_from(output_batch_idx);
156 
157  ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != collapsed_output_shape[output_batch_idx],
158  "vector_sum_row must have the same number of batches of output tensor");
159 
160  if(gemm_info.a_offset != 0)
161  {
162  TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
163  vector_sum_col_shape.collapse_from(1);
164 
165  ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_col_shape[1] != 1 && vector_sum_col_shape[1] != vector_sum_row_shape[1],
166  "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");
167  }
168  }
169  }
170 
171  if(output->total_size() != 0)
172  {
173  ARM_COMPUTE_RETURN_ERROR_ON(output_stage.output_data_type != output->data_type());
174  }
175  ARM_COMPUTE_RETURN_ERROR_ON(output_stage.gemmlowp_min_bound > output_stage.gemmlowp_max_bound);
176 
177  if(output_multipliers != nullptr && output_shifts != nullptr)
178  {
180  ARM_COMPUTE_RETURN_ERROR_ON(output_multipliers->num_dimensions() > 1);
182  ARM_COMPUTE_RETURN_ERROR_ON(output_shifts->num_dimensions() > 1);
183  if(output_stage.is_quantized_per_channel)
184  {
185  ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != output_shifts->dimension(0));
186  ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != output_multipliers->dimension(0));
187  }
188  }
189  }
190  return Status{};
191 }
192 
193 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output, const GEMMKernelInfo &gemm_info,
194  ITensorInfo *vector_sum_col, ITensorInfo *vector_sum_row, ITensorInfo *bias,
195  ITensorInfo *output_multipliers, ITensorInfo *output_shifts, ElementsProcessed &num_elements_processed)
196 {
197  const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
198 
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);
203 
204  Window win{};
205  Window win_out{};
206  bool window_changed = false;
207 
208  // In case both input and output have to be reinterpreted as 3D tensors,
209  // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
210  if(reinterpret_input_as_3d == reinterpret_output_as_3d)
211  {
212  reinterpret_output_as_3d = false;
213  }
214 
215  // Output tensor auto initialization if not yet initialized
216  const TensorShape expected_output_shape = compute_mm_shape(*input0, *input1, gemm_info);
217  if(output_stage.type != GEMMLowpOutputStageType::NONE)
218  {
219  auto_init_if_empty(*output, input0->clone()->set_tensor_shape(expected_output_shape).set_data_type(output_stage.output_data_type));
220  }
221  else
222  {
223  auto_init_if_empty(*output, input0->clone()->set_tensor_shape(expected_output_shape).set_data_type(DataType::S32));
224  }
225 
226  TensorInfo tmp_info(*output);
227 
228  if(reinterpret_output_as_3d)
229  {
230  // Since the output tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
231  // the window needs to be constructed on the 2D collapsed version of the tensor
232  TensorShape tmp_shape(output->tensor_shape());
233  tmp_shape.collapse(2U, 1U);
234  tmp_info.set_tensor_shape(tmp_shape);
235  }
236 
237  // Configure kernel window
238  num_elems_processed_per_iteration_x = gemm_info.rhs_info.n0;
239  num_elems_processed_per_iteration_y = gemm_info.lhs_info.m0;
240 
241  // Note: bottom paddings are calculated manually as the output can be reinterpreted as 3D tensor
242  // The only way to set properly the paddings, it is to set those explicitly through the AccessWindowStatic
243  const int m = reinterpret_output_as_3d ? gemm_info.m : input0->dimension(1);
244  const int bottom_pad = (num_elems_processed_per_iteration_y - (m % num_elems_processed_per_iteration_y)) % num_elems_processed_per_iteration_y;
245 
246  win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
247  win_out = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
248 
249  AccessWindowStatic input0_access(input0, 0, 0,
250  ceil_to_multiple(input0->dimension(0), gemm_info.lhs_info.k0),
251  input0->dimension(1) + bottom_pad);
252  AccessWindowStatic input1_access(input1, 0, 0,
253  input1->dimension(0),
254  input1->dimension(1));
255  AccessWindowStatic output_access(output, 0, 0,
256  ceil_to_multiple(output->dimension(0), num_elems_processed_per_iteration_x),
257  output->dimension(1) + bottom_pad);
258 
259  window_changed = update_window_and_padding(win, input0_access, input1_access) || // window used by the execute_window_loop
260  update_window_and_padding(win_out, output_access); // window used to update the padding requirements of output tensor
261 
263  {
264  if(gemm_info.a_offset != 0)
265  {
266  AccessWindowHorizontal vector_sum_col_access(vector_sum_col, 0, num_elems_processed_per_iteration_x);
267  window_changed = window_changed || update_window_and_padding(win_out, vector_sum_col_access);
268  }
269  // No access window needed for vector_sum_row
270  ARM_COMPUTE_UNUSED(vector_sum_row);
271 
272  if(bias != nullptr)
273  {
274  AccessWindowHorizontal bias_access(bias, 0, num_elems_processed_per_iteration_x);
275  window_changed = window_changed || update_window_and_padding(win_out, bias_access);
276  }
277 
278  if(output_multipliers != nullptr && output_multipliers->dimension(0) > 1)
279  {
280  AccessWindowHorizontal output_multipliers_access(output_multipliers, 0, num_elems_processed_per_iteration_x);
281  AccessWindowHorizontal output_shifts_access(output_shifts, 0, num_elems_processed_per_iteration_x);
282  window_changed = window_changed || update_window_and_padding(win_out, output_multipliers_access, output_shifts_access);
283  }
284  }
285 
286  output_access.set_valid_region(win_out, ValidRegion(Coordinates(), output->tensor_shape()));
287 
288  // Collapse along the Z direction
289  // This collapse needs to be here in order to tune the Z dimension of LWS
290  Window collapsed = win;
291  const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(output->num_dimensions()), 2u);
292  collapsed = win.collapse(win, dimension_to_collapse);
293 
294  Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
295  return std::make_pair(err, collapsed);
296 }
297 } // namespace
298 
300  : _input0(nullptr),
301  _input1(nullptr),
302  _output(nullptr),
303  _vector_sum_col(nullptr),
304  _vector_sum_row(nullptr),
305  _bias(nullptr),
306  _output_multipliers(nullptr),
307  _output_shifts(nullptr),
308  _slide_matrix_b(true),
309  _reinterpret_input_as_3d(false),
310  _reinterpret_output_as_3d(false),
311  _use_dummy_work_items(false),
312  _is_quantized_per_channel(false),
313  _fuse_output_stage(false)
314 {
315 }
316 
317 void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, const GEMMKernelInfo &gemm_info,
318  const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias,
319  const ICLTensor *output_multipliers, const ICLTensor *output_shifts)
320 {
321  configure(CLKernelLibrary::get().get_compile_context(), input0, input1, output, gemm_info, vector_sum_col, vector_sum_row, bias, output_multipliers, output_shifts);
322 }
323 
324 void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output,
325  const GEMMKernelInfo &gemm_info,
326  const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias,
327  const ICLTensor *output_multipliers, const ICLTensor *output_shifts)
328 {
329  ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
331  input1->info(),
332  output->info(),
333  gemm_info,
334  vector_sum_col != nullptr ? vector_sum_col->info() : nullptr,
335  vector_sum_row != nullptr ? vector_sum_row->info() : nullptr,
336  bias != nullptr ? bias->info() : nullptr,
337  output_multipliers != nullptr ? output_multipliers->info() : nullptr,
338  output_shifts != nullptr ? output_shifts->info() : nullptr));
339 
340  const GEMMRHSMatrixInfo rhs_info = gemm_info.rhs_info;
341  const GEMMLHSMatrixInfo lhs_info = gemm_info.lhs_info;
342  const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
343  const int32_t a_offset = gemm_info.a_offset;
344  const int32_t b_offset = gemm_info.b_offset;
345 
346  _input0 = input0;
347  _input1 = input1;
348  _output = output;
349  _vector_sum_col = vector_sum_col;
350  _vector_sum_row = vector_sum_row;
351  _bias = bias;
352  _output_multipliers = output_multipliers;
353  _output_shifts = output_shifts;
354  _reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
355  _reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d != 0);
356  _use_dummy_work_items = preferred_dummy_work_items_support(CLKernelLibrary::get().get_device());
357  _is_quantized_per_channel = output_stage.is_quantized_per_channel;
358 
359  // In case both input and output have to be reinterpreted as 3D tensors,
360  // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
361  if(_reinterpret_input_as_3d == _reinterpret_output_as_3d)
362  {
363  _reinterpret_input_as_3d = false;
364  _reinterpret_output_as_3d = false;
365  }
366 
367  // Check if we need to slide the matrix B
368  const unsigned int num_dimensions_input0 = _input0->info()->num_dimensions();
369  _slide_matrix_b = (_input1->info()->num_dimensions() >= num_dimensions_input0);
370 
371  ElementsProcessed num_elements_processed{};
372 
373  // Configure kernel window
374  auto win_config = validate_and_configure_window(input0->info(),
375  input1->info(),
376  output->info(),
377  gemm_info,
378  vector_sum_col != nullptr ? vector_sum_col->info() : nullptr,
379  vector_sum_row != nullptr ? vector_sum_row->info() : nullptr,
380  bias != nullptr ? bias->info() : nullptr,
381  output_multipliers != nullptr ? output_multipliers->info() : nullptr,
382  output_shifts != nullptr ? output_shifts->info() : nullptr,
383  num_elements_processed);
384  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
385  ICLKernel::configure_internal(win_config.second);
386 
387  // Create build options
388  CLBuildOptions build_opts;
389  build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
390  build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D");
391  build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(1)));
392  build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(2)));
393  build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(input1->info()->dimension(2)));
394  build_opts.add_option_if(rhs_info.interleave, "-DRHS_INTERLEAVE");
395  build_opts.add_option_if(_use_dummy_work_items, "-DDUMMY_WORK_ITEMS");
396  build_opts.add_option("-DM=" + support::cpp11::to_string(input0->info()->dimension(1)));
397  build_opts.add_option("-DN=" + support::cpp11::to_string(gemm_info.n));
398  build_opts.add_option("-DK=" + support::cpp11::to_string(gemm_info.k));
399  build_opts.add_option("-DM0=" + support::cpp11::to_string(lhs_info.m0));
400  build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
401  build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
402  build_opts.add_option("-DH0=" + support::cpp11::to_string(rhs_info.h0));
403  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input0->info()->data_type()));
404  build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_dot8_acc_type_from_data_type(input0->info()->data_type()));
405 
406  std::string kernel_name("gemmlowp_mm_reshaped_only_rhs_");
407  kernel_name += rhs_info.transpose ? "t" : "nt";
408 
410  {
411  kernel_name += "_fused_output_stage_fixedpoint";
412  _fuse_output_stage = true;
413  // If a_offset == 0, vector_sum_col can be a nullptr
414  if(a_offset != 0)
415  {
416  build_opts.add_option("-DA_OFFSET=" + support::cpp11::to_string(a_offset));
417  build_opts.add_option_if(vector_sum_col->info()->tensor_shape().num_dimensions() > 1, "-DSUM_COL_HAS_BATCHES");
418  }
419  // If b_offset == 0, vector_sum_row can be a nullptr
420  build_opts.add_option_if(b_offset != 0, "-DB_OFFSET=" + support::cpp11::to_string(b_offset));
421  build_opts.add_option("-DK_OFFSET=" + support::cpp11::to_string(a_offset * b_offset * input0->info()->dimension(0)));
422  build_opts.add_option_if(bias != nullptr, "-DADD_BIAS");
423  build_opts.add_option("-DRESULT_OFFSET=" + support::cpp11::to_string(output_stage.gemmlowp_offset));
424  build_opts.add_option("-DRESULT_MULTIPLIER=" + support::cpp11::to_string(output_stage.gemmlowp_multipliers[0]));
425  build_opts.add_option("-DRESULT_SHIFT=" + support::cpp11::to_string(output_stage.gemmlowp_shifts[0]));
426  build_opts.add_option_if(_is_quantized_per_channel, "-DPER_CHANNEL_QUANTIZATION");
427 
428  const int min = output_stage.gemmlowp_min_bound;
429  const int max = output_stage.gemmlowp_max_bound;
430 
431  PixelValue min_val{};
432  PixelValue max_val{};
433  std::tie(min_val, max_val) = get_min_max(output->info()->data_type());
434  build_opts.add_option_if(min != min_val.get<int32_t>(), "-DMIN_BOUND=" + support::cpp11::to_string(min));
435  build_opts.add_option_if(max != max_val.get<int32_t>(), "-DMAX_BOUND=" + support::cpp11::to_string(max));
436  }
437 
438  // Create kernel
439  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
440 
441  // Set config_id for enabling LWS tuning
442  _config_id = kernel_name;
443  _config_id += "_";
444  _config_id += dot8_supported(CLKernelLibrary::get().get_device()) ? "_dot8" : "";
445  _config_id += "_";
446  _config_id += (_reinterpret_input_as_3d ? "3di_" : "");
447  _config_id += (_reinterpret_output_as_3d ? "3do_" : "");
448  _config_id += support::cpp11::to_string(output->info()->dimension(1));
449  _config_id += "_";
450  _config_id += support::cpp11::to_string(output->info()->dimension(0));
451  _config_id += "_";
452  _config_id += support::cpp11::to_string(gemm_info.k);
453  _config_id += "_";
454  _config_id += support::cpp11::to_string(output->info()->dimension(2));
455  _config_id += "_";
456  _config_id += support::cpp11::to_string(lhs_info.m0);
457  _config_id += "_";
458  _config_id += support::cpp11::to_string(rhs_info.n0);
459  _config_id += "_";
460  _config_id += support::cpp11::to_string(rhs_info.k0);
461  _config_id += "_";
462  _config_id += support::cpp11::to_string(rhs_info.h0);
463  _config_id += "_";
464  _config_id += support::cpp11::to_string(rhs_info.interleave);
465 }
466 
468  const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias,
469  const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts)
470 {
471  ElementsProcessed num_elements_processed{};
472  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output, gemm_info, vector_sum_col, vector_sum_row, bias, output_multipliers, output_shifts));
473  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
474  input1->clone().get(),
475  output->clone().get(),
476  gemm_info,
477  vector_sum_col != nullptr ? vector_sum_col->clone().get() : nullptr,
478  vector_sum_row != nullptr ? vector_sum_row->clone().get() : nullptr,
479  bias != nullptr ? bias->clone().get() : nullptr,
480  output_multipliers != nullptr ? output_multipliers->clone().get() : nullptr,
481  output_shifts != nullptr ? output_shifts->clone().get() : nullptr,
482  num_elements_processed)
483  .first);
484 
485  return Status{};
486 }
487 
488 void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::run(const Window &window, cl::CommandQueue &queue)
489 {
492 
493  if(_input1->info()->num_dimensions() < 3)
494  {
495  // The stride_z for matrix B must be zero if we do not slice
496  ARM_COMPUTE_ERROR_ON(_input1->info()->strides_in_bytes()[3] != 0);
497  }
498 
500  Window slice_matrix_b = slice;
501 
502  slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
503  slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
504 
505  if(_reinterpret_input_as_3d)
506  {
507  // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
508  const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3;
509  const unsigned int total_cross_plane_pad = _input0->info()->padding().top + _input0->info()->padding().bottom;
510  _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
511  }
512 
513  if(_reinterpret_output_as_3d)
514  {
515  // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor
516  const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3 + (_reinterpret_input_as_3d ? 1 : 0);
517  const unsigned int total_cross_plane_pad = _output->info()->padding().top + _output->info()->padding().bottom;
518  _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
519  }
520 
521  // Set window for vector_sum_col
522  Window win_vector_sum_col = slice;
523  win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
524  win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
525 
526  // Set window for vector_sum_row
527  Window win_vector_sum_row = slice;
528  win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
529  win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
530  win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
531 
532  Window biases_slice = slice;
533  biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1));
534  biases_slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
535 
536  do
537  {
538  Window slice_b = slice;
539  // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
540  // This scenario can happen when the matrix multiplication is used to perform a convolution operation
541  if(!_slide_matrix_b)
542  {
543  slice_b = slice_matrix_b;
544  }
545 
546  unsigned int idx = 0;
547  add_2D_tensor_argument(idx, _input0, slice);
548  add_2D_tensor_argument(idx, _input1, slice_b);
549  add_2D_tensor_argument(idx, _output, slice);
550  _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input0->info()->strides_in_bytes()[2]));
551  _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input1->info()->strides_in_bytes()[2]));
552  _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[2]));
553  if(_reinterpret_input_as_3d)
554  {
555  // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
556  idx++;
557  }
558 
559  if(_reinterpret_output_as_3d)
560  {
561  // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor
562  idx++;
563  }
564 
565  if(_fuse_output_stage)
566  {
567  add_2D_tensor_argument_if((_vector_sum_col != nullptr), idx, _vector_sum_col, win_vector_sum_col);
568  add_2D_tensor_argument_if((_vector_sum_row != nullptr), idx, _vector_sum_row, win_vector_sum_row);
569  add_1D_tensor_argument_if((_bias != nullptr), idx, _bias, biases_slice);
570  add_1D_tensor_argument_if(_is_quantized_per_channel, idx, _output_multipliers, biases_slice);
571  add_1D_tensor_argument_if(_is_quantized_per_channel, idx, _output_shifts, biases_slice);
572  }
573  enqueue(queue, *this, slice, lws_hint(), _use_dummy_work_items);
574  }
576 }
577 } // namespace arm_compute
void add_1D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx ...
Definition: ICLKernel.h:122
unsigned int top
top of the border
Definition: Types.h:352
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
Quantize using a fixed point multiplication.
bool dot8_supported(const cl::Device &device)
Helper function to check whether the cl_arm_integer_dot_product_int8 extension is supported.
Definition: CLHelpers.cpp:239
Descriptor used by the GEMM kernels.
void add_2D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx ...
Definition: ICLKernel.h:146
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:39
const StringSet & options() const
Gets the current options list set.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
unsigned int depth_output_gemm3d
Depth of the output tensor in case is reinterpreted as 3D.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:263
bool preferred_dummy_work_items_support(const cl::Device &device)
Helper function to check if "dummy work-items" are preferred to have a power of two NDRange In case d...
Definition: CLHelpers.cpp:361
std::string get_cl_dot8_acc_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL dot8 accumulator type.
Definition: CLHelpers.cpp:173
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
Calculate the matrix multiplication output shape of two tensors.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
unsigned int h0
Number of horizontal blocks of size (k0xn0) stored on the same output row.
Definition: Types.h:1921
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
GEMM LHS (Left Hand Side) matrix information.
Definition: Types.h:1897
Store the tensor's metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
unsigned int bottom
bottom of the border
Definition: Types.h:354
int32_t gemmlowp_offset
GEMMLowp output stage offset used for quantizing to QASYMM8.
Definition: Types.h:1884
Status class.
Definition: Error.h:52
int32_t gemmlowp_max_bound
GEMMLowp max value used to saturate down the output result before converting back to QASYMM8.
Definition: Types.h:1888
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
GEMMLowpOutputStageType type
GEMMLowp output stage type.
Definition: Types.h:1883
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps=Steps(), bool skip_border=false, BorderSize border_size=BorderSize())
Calculate the maximum window for a given tensor shape and border setting.
Definition: Helpers.cpp:28
GEMMLHSMatrixInfo lhs_info
LHS matrix information used to retrieve the number of rows processed by each thread.
bool transpose
True if the (k0xn0) block has to be transposed before been stored.
Definition: Types.h:1922
Copyright (c) 2017-2020 Arm Limited.
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
Definition: Helpers.inl:207
ITensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: Tensor.cpp:33
bool is_quantized_per_channel
GEMMLowp quantized per-channel flag.
Definition: Types.h:1892
std::vector< int32_t > gemmlowp_shifts
GEMMLowp output stage multiplier used for quantizing to QASYMM8.
Definition: Types.h:1890
1 channel, 1 S32 per channel
void add_option(std::string option)
Adds option to the existing build option list.
unsigned int k0
Number of partial accumulations performed by the matrix multiplication.
Definition: Types.h:1920
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:403
unsigned int n
Number of RHS columns.
static Status validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, const GEMMKernelInfo &gemm_info, const ITensorInfo *vector_sum_col=nullptr, const ITensorInfo *vector_sum_row=nullptr, const ITensorInfo *bias=nullptr, const ITensorInfo *output_multipliers=nullptr, const ITensorInfo *output_shifts=nullptr)
Static function to check if given info will lead to a valid configuration of CLGEMMLowpMatrixMultiply...
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: Helpers.h:437
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
GEMM RHS (Right Hand Side) matrix information.
Definition: Types.h:1912
int32_t b_offset
Offset to be added to each element of the matrix B.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
auto ceil_to_multiple(S value, T divisor) -> decltype(((value+divisor - 1)/divisor) *divisor)
Computes the smallest number larger or equal to value that is a multiple of divisor.
Definition: Utils.h:67
unsigned int n0
Number of columns processed by the matrix multiplication.
Definition: Types.h:1919
quantized, asymmetric fixed-point 8-bit number unsigned
std::string kernel_name
std::vector< int32_t > gemmlowp_multipliers
GEMMLowp output stage multiplier used for quantizing to QASYMM8.
Definition: Types.h:1889
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:37
GEMMLowpOutputStageInfo output_stage
GEMMLowp output stage information.
TensorShape compute_rhs_reshaped_shape(const ITensorInfo &a, const GEMMRHSMatrixInfo &rhs_info)
Calculate the Right Hand Side matrix reshaped shape.
bool reinterpret_input_as_3d
Flag used to reinterpret the input as 3D.
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
GEMMLowp output stage info.
Definition: Types.h:1881
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
Quantize using a floating point multiplication.
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
virtual PaddingSize padding() const =0
Padding of tensor.
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:193
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:333
Quantize using an integer multiplication.
quantized, symmetric fixed-point 8-bit number
CLCompileContext class.
quantized, symmetric per channel fixed-point 8-bit number
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
int32_t a_offset
Offset to be added to each element of the matrix A.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:135
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
GEMMRHSMatrixInfo rhs_info
RHS matrix information used for reshaping the RHS matrix.
#define ARM_COMPUTE_CREATE_ERROR(error_code, msg)
Creates an error with a given message.
Definition: Error.h:159
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:122
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
unsigned int k
Number of LHS columns or RHS rows.
bool interleave
True if the h0 (k0xn0) blocks have to be interleaved in the output row.
Definition: Types.h:1923
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
unsigned int m0
Number of rows processed by the matrix multiplication.
Definition: Types.h:1904
quantized, asymmetric fixed-point 8-bit number signed
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
int32_t gemmlowp_min_bound
GEMMLowp min value used to saturate down the output result before converting back to QASYMM8.
Definition: Types.h:1887
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:289
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
void configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, const GEMMKernelInfo &gemm_info, const ICLTensor *vector_sum_col=nullptr, const ICLTensor *vector_sum_row=nullptr, const ICLTensor *bias=nullptr, const ICLTensor *output_multipliers=nullptr, const ICLTensor *output_shifts=nullptr)
Initialise the kernel's input and output.
std::tuple< PixelValue, PixelValue > get_min_max(DataType dt)
Compute the mininum and maximum values a data type can take.
Definition: Utils.h:560
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)