Compute Library
 21.02
CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021 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 
32 #include "arm_compute/core/Utils.h"
38 #include "support/StringSupport.h"
39 
40 #include <cstddef>
41 #include <cstdint>
42 #include <tuple>
43 
45 
46 namespace arm_compute
47 {
48 namespace
49 {
50 using ElementsProcessed = Steps;
51 
52 Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, const GEMMKernelInfo &gemm_info,
53  const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias,
54  const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts)
55 {
56  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output);
58  if(input0->data_type() == DataType::QASYMM8)
59  {
61  }
62  else
63  {
65  }
66  ARM_COMPUTE_RETURN_ERROR_ON_MSG(input0->num_dimensions() > 4, "The number of dimensions for the LHS matrix must be <= 4");
67  ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->num_dimensions() > 3, "The number of dimensions for the RHS matrix must be <= 3");
68 
69  const GEMMRHSMatrixInfo rhs_info = gemm_info.rhs_info;
70  const GEMMLHSMatrixInfo lhs_info = gemm_info.lhs_info;
71  const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
72 
73  ARM_COMPUTE_RETURN_ERROR_ON_MSG((((rhs_info.k0 & (rhs_info.k0 - 1)) && rhs_info.k0 != 3) || (rhs_info.k0 > 16)), "Only 2,3,4,8,16 are supported for k0");
74  ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.m0 < 1 || lhs_info.m0 > 8);
75  ARM_COMPUTE_RETURN_ERROR_ON_MSG((((rhs_info.n0 & (rhs_info.n0 - 1)) && rhs_info.n0 != 3) || rhs_info.n0 > 16), "Only 2,3,4,8,16 are supported for n0");
76  ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.export_to_cl_image, "Export to CLImage not supported for quantized GEMM");
77 
78  const int m = gemm_info.m;
79  const int n = gemm_info.n;
80  const int k = gemm_info.k;
81 
82  TensorShape tensor_shape1{ input1->tensor_shape() };
83  tensor_shape1.set(0, n);
84  tensor_shape1.set(1, k);
85 
86  const TensorInfo tensor_info1 = input1->clone()->set_tensor_shape(tensor_shape1);
87  const TensorInfo tensor_info_reshaped1 = input1->clone()->set_tensor_shape(compute_rhs_reshaped_shape(tensor_info1, rhs_info));
88 
89  ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(0) != static_cast<unsigned int>(k));
90  if(gemm_info.reinterpret_input_as_3d)
91  {
92  ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) * input0->dimension(2) != static_cast<unsigned int>(m));
93  }
94  else
95  {
96  ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) != static_cast<unsigned int>(m));
97  }
98  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, &tensor_info_reshaped1);
99 
100  const TensorShape expected_output_shape = compute_mm_shape(*input0, *input1, gemm_info);
101  if(output->total_size() != 0)
102  {
103  const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(expected_output_shape);
104  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
105  if(output_stage.type == GEMMLowpOutputStageType::NONE)
106  {
108  }
109  else
110  {
112  }
113  }
114 
115  if(bias != nullptr)
116  {
118  ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
119  ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != bias->dimension(0));
120  }
121 
123  "Only GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT is supported");
124 
125  // Checks performed if the output stage needs to be fused
127  {
128  // If a_offset == 0, vector_sum_col can be a nullptr
129  if(gemm_info.a_offset != 0)
130  {
132  ARM_COMPUTE_RETURN_ERROR_ON(vector_sum_col->dimension(0) != expected_output_shape[0]);
133  }
134 
135  // If b_offset == 0, vector_sum_row can be a nullptr
136  if(gemm_info.b_offset != 0)
137  {
139 
140  // Check if mm result is a 3D reinterpretation
141  const bool reinterpret_as_3d = expected_output_shape.num_dimensions() > 1 && expected_output_shape.y() != vector_sum_row->tensor_shape().x();
142 
143  // Validate input
144  ARM_COMPUTE_RETURN_ERROR_ON(reinterpret_as_3d && vector_sum_row->dimension(0) != (expected_output_shape[1] * expected_output_shape[2]));
145  ARM_COMPUTE_RETURN_ERROR_ON(!reinterpret_as_3d && vector_sum_row->dimension(0) != expected_output_shape[1]);
146 
147  if(expected_output_shape.num_dimensions() > 1)
148  {
149  const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2;
150 
151  TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
152  vector_sum_row_shape.collapse_from(1);
153  TensorShape collapsed_output_shape(expected_output_shape);
154  collapsed_output_shape.collapse_from(output_batch_idx);
155 
156  ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != collapsed_output_shape[output_batch_idx],
157  "vector_sum_row must have the same number of batches of output tensor");
158 
159  if(gemm_info.a_offset != 0)
160  {
161  TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
162  vector_sum_col_shape.collapse_from(1);
163 
164  ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_col_shape[1] != 1 && vector_sum_col_shape[1] != vector_sum_row_shape[1],
165  "vector_sum_col tensor must have the same number of batches of vector_sum_row_shape or the number of batches must be set to 1");
166  }
167  }
168  }
169 
170  if(output->total_size() != 0)
171  {
172  ARM_COMPUTE_RETURN_ERROR_ON(output_stage.output_data_type != output->data_type());
173  }
174  ARM_COMPUTE_RETURN_ERROR_ON(output_stage.gemmlowp_min_bound > output_stage.gemmlowp_max_bound);
175 
176  if(output_multipliers != nullptr && output_shifts != nullptr)
177  {
179  ARM_COMPUTE_RETURN_ERROR_ON(output_multipliers->num_dimensions() > 1);
181  ARM_COMPUTE_RETURN_ERROR_ON(output_shifts->num_dimensions() > 1);
182  if(output_stage.is_quantized_per_channel)
183  {
184  ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != output_shifts->dimension(0));
185  ARM_COMPUTE_RETURN_ERROR_ON(expected_output_shape[0] != output_multipliers->dimension(0));
186  }
187  }
188  }
189  return Status{};
190 }
191 
192 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output, const GEMMKernelInfo &gemm_info,
193  ITensorInfo *vector_sum_col, ITensorInfo *vector_sum_row, ITensorInfo *bias,
194  ITensorInfo *output_multipliers, ITensorInfo *output_shifts, ElementsProcessed &num_elements_processed)
195 {
196  const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
197 
198  unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
199  unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
200  bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
201  bool reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d != 0);
202 
203  Window win{};
204  Window win_out{};
205  bool window_changed = false;
206 
207  // In case both input and output have to be reinterpreted as 3D tensors,
208  // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
209  if(reinterpret_input_as_3d == reinterpret_output_as_3d)
210  {
211  reinterpret_output_as_3d = false;
212  }
213 
214  // Output tensor auto initialization if not yet initialized
215  const TensorShape expected_output_shape = compute_mm_shape(*input0, *input1, gemm_info);
216  if(output_stage.type != GEMMLowpOutputStageType::NONE)
217  {
218  auto_init_if_empty(*output, input0->clone()->set_tensor_shape(expected_output_shape).set_data_type(output_stage.output_data_type));
219  }
220  else
221  {
222  auto_init_if_empty(*output, input0->clone()->set_tensor_shape(expected_output_shape).set_data_type(DataType::S32));
223  }
224 
225  TensorInfo tmp_info(*output);
226 
227  if(reinterpret_output_as_3d)
228  {
229  // Since the output tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
230  // the window needs to be constructed on the 2D collapsed version of the tensor
231  TensorShape tmp_shape(output->tensor_shape());
232  tmp_shape.collapse(2U, 1U);
233  tmp_info.set_tensor_shape(tmp_shape);
234  }
235 
236  // Configure kernel window
237  num_elems_processed_per_iteration_x = gemm_info.rhs_info.n0;
238  num_elems_processed_per_iteration_y = gemm_info.lhs_info.m0;
239 
240  win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
241  win_out = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
242 
244  {
245  if(gemm_info.a_offset != 0)
246  {
247  AccessWindowHorizontal vector_sum_col_access(vector_sum_col, 0, num_elems_processed_per_iteration_x);
248  window_changed = window_changed || update_window_and_padding(win_out, vector_sum_col_access);
249  }
250  // No access window needed for vector_sum_row
251  ARM_COMPUTE_UNUSED(vector_sum_row);
252 
253  if(bias != nullptr)
254  {
255  AccessWindowHorizontal bias_access(bias, 0, num_elems_processed_per_iteration_x);
256  window_changed = window_changed || update_window_and_padding(win_out, bias_access);
257  }
258 
259  if(output_multipliers != nullptr && output_multipliers->dimension(0) > 1)
260  {
261  AccessWindowHorizontal output_multipliers_access(output_multipliers, 0, num_elems_processed_per_iteration_x);
262  AccessWindowHorizontal output_shifts_access(output_shifts, 0, num_elems_processed_per_iteration_x);
263  window_changed = window_changed || update_window_and_padding(win_out, output_multipliers_access, output_shifts_access);
264  }
265  }
266 
267  // Collapse along the Z direction
268  // This collapse needs to be here in order to tune the Z dimension of LWS
269  Window collapsed = win;
270  const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(output->num_dimensions()), 2u);
271  collapsed = win.collapse(win, dimension_to_collapse);
272 
273  Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
274  return std::make_pair(err, collapsed);
275 }
276 } // namespace
277 
279  : _input0(nullptr),
280  _input1(nullptr),
281  _output(nullptr),
282  _vector_sum_col(nullptr),
283  _vector_sum_row(nullptr),
284  _bias(nullptr),
285  _output_multipliers(nullptr),
286  _output_shifts(nullptr),
287  _slide_matrix_b(true),
288  _reinterpret_input_as_3d(false),
289  _reinterpret_output_as_3d(false),
290  _use_dummy_work_items(false),
291  _is_quantized_per_channel(false),
292  _fuse_output_stage(false)
293 {
294 }
295 
296 void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, const GEMMKernelInfo &gemm_info,
297  const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias,
298  const ICLTensor *output_multipliers, const ICLTensor *output_shifts)
299 {
300  configure(CLKernelLibrary::get().get_compile_context(), input0, input1, output, gemm_info, vector_sum_col, vector_sum_row, bias, output_multipliers, output_shifts);
301 }
302 
303 void CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output,
304  const GEMMKernelInfo &gemm_info,
305  const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias,
306  const ICLTensor *output_multipliers, const ICLTensor *output_shifts)
307 {
308  ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
310  input1->info(),
311  output->info(),
312  gemm_info,
313  vector_sum_col != nullptr ? vector_sum_col->info() : nullptr,
314  vector_sum_row != nullptr ? vector_sum_row->info() : nullptr,
315  bias != nullptr ? bias->info() : nullptr,
316  output_multipliers != nullptr ? output_multipliers->info() : nullptr,
317  output_shifts != nullptr ? output_shifts->info() : nullptr));
318 
319  auto padding_info = get_padding_info({ input0, input1, output, vector_sum_row });
320  const GEMMRHSMatrixInfo rhs_info = gemm_info.rhs_info;
321  const GEMMLHSMatrixInfo lhs_info = gemm_info.lhs_info;
322  const GEMMLowpOutputStageInfo output_stage = gemm_info.output_stage;
323  const int32_t a_offset = gemm_info.a_offset;
324  const int32_t b_offset = gemm_info.b_offset;
325 
326  _input0 = input0;
327  _input1 = input1;
328  _output = output;
329  _vector_sum_col = vector_sum_col;
330  _vector_sum_row = vector_sum_row;
331  _bias = bias;
332  _output_multipliers = output_multipliers;
333  _output_shifts = output_shifts;
334  _reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
335  _reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d != 0);
336  _use_dummy_work_items = preferred_dummy_work_items_support(CLKernelLibrary::get().get_device());
337  _is_quantized_per_channel = output_stage.is_quantized_per_channel;
338 
339  // In case both input and output have to be reinterpreted as 3D tensors,
340  // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
341  if(_reinterpret_input_as_3d == _reinterpret_output_as_3d)
342  {
343  _reinterpret_input_as_3d = false;
344  _reinterpret_output_as_3d = false;
345  }
346 
347  // Check if we need to slide the matrix B
348  const unsigned int num_dimensions_input0 = _input0->info()->num_dimensions();
349  _slide_matrix_b = (_input1->info()->num_dimensions() >= num_dimensions_input0);
350 
351  ElementsProcessed num_elements_processed{};
352 
353  // Configure kernel window
354  auto win_config = validate_and_configure_window(input0->info(),
355  input1->info(),
356  output->info(),
357  gemm_info,
358  vector_sum_col != nullptr ? vector_sum_col->info() : nullptr,
359  vector_sum_row != nullptr ? vector_sum_row->info() : nullptr,
360  bias != nullptr ? bias->info() : nullptr,
361  output_multipliers != nullptr ? output_multipliers->info() : nullptr,
362  output_shifts != nullptr ? output_shifts->info() : nullptr,
363  num_elements_processed);
364  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
365  ICLKernel::configure_internal(win_config.second);
366 
367  // If _reinterpret_input_as_3d = _reinterpret_output_as_3d = true,
368  // we will dispatch a batched-GEMM to reduce the complexity of the address calculation within the OpenCL kernel.
369  // This means that the actual m used by the kernel is given by output->info()->dimension(1) and not by gemm_info.m
370  const unsigned int internal_m = _reinterpret_output_as_3d ? gemm_info.m : output->info()->dimension(1);
371 
372  // Shrink M0 to be always <= M (internal_m) to prevent out-of-bounds reads.
373  // NOTE: This might have implications on heuristics and performance
374  const unsigned int internal_m0 = std::min(internal_m, lhs_info.m0);
375 
376  // Calculate partial (store instead of load) M0 and partial N0 for the partial blocks at the end of a row/column if any. This is to avoid padding.
377  const unsigned int partial_store_m0 = internal_m % internal_m0;
378  const unsigned int partial_store_n0 = gemm_info.n % rhs_info.n0;
379 
380  // Create build options
381  CLBuildOptions build_opts;
382  build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
383  build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D");
384  build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(1)));
385  build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(2)));
386  build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(input1->info()->dimension(2)));
387  build_opts.add_option_if(rhs_info.interleave, "-DRHS_INTERLEAVE");
388  build_opts.add_option_if(_use_dummy_work_items, "-DDUMMY_WORK_ITEMS");
389  build_opts.add_option("-DM=" + support::cpp11::to_string(internal_m));
390  build_opts.add_option("-DN=" + support::cpp11::to_string(gemm_info.n));
391  build_opts.add_option("-DK=" + support::cpp11::to_string(gemm_info.k));
392  build_opts.add_option("-DM0=" + support::cpp11::to_string(internal_m0));
393  build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
394  build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0));
395  build_opts.add_option("-DH0=" + support::cpp11::to_string(rhs_info.h0));
396  build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(partial_store_m0));
397  build_opts.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(partial_store_n0));
398  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input0->info()->data_type()));
399  build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_dot8_acc_type_from_data_type(input0->info()->data_type()));
400 
401  std::string kernel_name("gemmlowp_mm_reshaped_only_rhs_");
402  kernel_name += rhs_info.transpose ? "t" : "nt";
403 
405  {
406  kernel_name += "_fused_output_stage_fixedpoint";
407  _fuse_output_stage = true;
408  // If a_offset == 0, vector_sum_col can be a nullptr
409  if(a_offset != 0)
410  {
411  build_opts.add_option("-DA_OFFSET=" + support::cpp11::to_string(a_offset));
412  build_opts.add_option_if(vector_sum_col->info()->tensor_shape().num_dimensions() > 1, "-DSUM_COL_HAS_BATCHES");
413  }
414  // If b_offset == 0, vector_sum_row can be a nullptr
415  build_opts.add_option_if(b_offset != 0, "-DB_OFFSET=" + support::cpp11::to_string(b_offset));
416  build_opts.add_option("-DK_OFFSET=" + support::cpp11::to_string(a_offset * b_offset * input0->info()->dimension(0)));
417  build_opts.add_option_if(bias != nullptr, "-DADD_BIAS");
418  build_opts.add_option("-DRESULT_OFFSET=" + support::cpp11::to_string(output_stage.gemmlowp_offset));
419  build_opts.add_option("-DRESULT_MULTIPLIER=" + support::cpp11::to_string(output_stage.gemmlowp_multipliers[0]));
420  build_opts.add_option("-DRESULT_SHIFT=" + support::cpp11::to_string(output_stage.gemmlowp_shifts[0]));
421  build_opts.add_option_if(_is_quantized_per_channel, "-DPER_CHANNEL_QUANTIZATION");
422 
423  const int min = output_stage.gemmlowp_min_bound;
424  const int max = output_stage.gemmlowp_max_bound;
425 
426  PixelValue min_val{};
427  PixelValue max_val{};
428  std::tie(min_val, max_val) = get_min_max(output->info()->data_type());
429  build_opts.add_option_if(min != min_val.get<int32_t>(), "-DMIN_BOUND=" + support::cpp11::to_string(min));
430  build_opts.add_option_if(max != max_val.get<int32_t>(), "-DMAX_BOUND=" + support::cpp11::to_string(max));
431  }
432 
433  // Create kernel
434  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
435 
436  // Set config_id for enabling LWS tuning
437  _config_id = kernel_name;
438  _config_id += "_";
439  _config_id += dot8_supported(CLKernelLibrary::get().get_device()) ? "_dot8" : "";
440  _config_id += "_";
441  _config_id += (_reinterpret_input_as_3d ? "3di_" : "");
442  _config_id += (_reinterpret_output_as_3d ? "3do_" : "");
443  _config_id += support::cpp11::to_string(output->info()->dimension(1));
444  _config_id += "_";
445  _config_id += support::cpp11::to_string(output->info()->dimension(0));
446  _config_id += "_";
447  _config_id += support::cpp11::to_string(gemm_info.k);
448  _config_id += "_";
449  _config_id += support::cpp11::to_string(output->info()->dimension(2));
450  _config_id += "_";
451  _config_id += support::cpp11::to_string(lhs_info.m0);
452  _config_id += "_";
453  _config_id += support::cpp11::to_string(rhs_info.n0);
454  _config_id += "_";
455  _config_id += support::cpp11::to_string(rhs_info.k0);
456  _config_id += "_";
457  _config_id += support::cpp11::to_string(rhs_info.h0);
458  _config_id += "_";
459  _config_id += support::cpp11::to_string(rhs_info.interleave);
461 }
462 
464  const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias,
465  const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts)
466 {
467  ElementsProcessed num_elements_processed{};
468  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output, gemm_info, vector_sum_col, vector_sum_row, bias, output_multipliers, output_shifts));
469  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
470  input1->clone().get(),
471  output->clone().get(),
472  gemm_info,
473  vector_sum_col != nullptr ? vector_sum_col->clone().get() : nullptr,
474  vector_sum_row != nullptr ? vector_sum_row->clone().get() : nullptr,
475  bias != nullptr ? bias->clone().get() : nullptr,
476  output_multipliers != nullptr ? output_multipliers->clone().get() : nullptr,
477  output_shifts != nullptr ? output_shifts->clone().get() : nullptr,
478  num_elements_processed)
479  .first);
480 
481  return Status{};
482 }
483 
485 {
488 
489  if(_input1->info()->num_dimensions() < 3)
490  {
491  // The stride_z for matrix B must be zero if we do not slice
492  ARM_COMPUTE_ERROR_ON(_input1->info()->strides_in_bytes()[3] != 0);
493  }
494 
496  Window slice_matrix_b = slice;
497 
498  slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
499  slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
500 
501  if(_reinterpret_input_as_3d)
502  {
503  // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
504  const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3;
505  const unsigned int total_cross_plane_pad = _input0->info()->padding().top + _input0->info()->padding().bottom;
506  _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
507  }
508 
509  if(_reinterpret_output_as_3d)
510  {
511  // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor
512  const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3 + (_reinterpret_input_as_3d ? 1 : 0);
513  const unsigned int total_cross_plane_pad = _output->info()->padding().top + _output->info()->padding().bottom;
514  _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
515  }
516 
517  // Set window for vector_sum_col
518  Window win_vector_sum_col = slice;
519  win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
520  win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
521 
522  // Set window for vector_sum_row
523  Window win_vector_sum_row = slice;
524  win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
525  win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
526  win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
527 
528  Window biases_slice = slice;
529  biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1));
530  biases_slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
531 
532  do
533  {
534  Window slice_b = slice;
535  // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
536  // This scenario can happen when the matrix multiplication is used to perform a convolution operation
537  if(!_slide_matrix_b)
538  {
539  slice_b = slice_matrix_b;
540  }
541 
542  unsigned int idx = 0;
543  add_2D_tensor_argument(idx, _input0, slice);
544  add_2D_tensor_argument(idx, _input1, slice_b);
545  add_2D_tensor_argument(idx, _output, slice);
546  _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input0->info()->strides_in_bytes()[2]));
547  _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input1->info()->strides_in_bytes()[2]));
548  _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[2]));
549  if(_reinterpret_input_as_3d)
550  {
551  // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
552  idx++;
553  }
554 
555  if(_reinterpret_output_as_3d)
556  {
557  // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor
558  idx++;
559  }
560 
561  if(_fuse_output_stage)
562  {
563  add_2D_tensor_argument_if((_vector_sum_col != nullptr), idx, _vector_sum_col, win_vector_sum_col);
564  add_2D_tensor_argument_if((_vector_sum_row != nullptr), idx, _vector_sum_row, win_vector_sum_row);
565  add_1D_tensor_argument_if((_bias != nullptr), idx, _bias, biases_slice);
566  add_1D_tensor_argument_if(_is_quantized_per_channel, idx, _output_multipliers, biases_slice);
567  add_1D_tensor_argument_if(_is_quantized_per_channel, idx, _output_shifts, biases_slice);
568  }
569  enqueue(queue, *this, slice, lws_hint(), _use_dummy_work_items);
570  }
571  while(window.slide_window_slice_3D(slice));
572 }
573 } // 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&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx ...
Definition: ICLKernel.h:135
unsigned int top
top of the border
Definition: Types.h:375
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
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
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&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx ...
Definition: ICLKernel.h:159
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
Definition: IGCKernel.cpp:41
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
const StringSet & options() const
Gets the current options list set.
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:276
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_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:1968
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
unsigned int bottom
bottom of the border
Definition: Types.h:377
int32_t gemmlowp_offset
GEMMLowp output stage offset used for quantizing to QASYMM8.
Definition: Types.h:1955
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:1959
#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:1954
GEMMLHSMatrixInfo lhs_info
LHS matrix information used to retrieve the number of rows processed by each thread.
Copyright (c) 2017-2021 Arm Limited.
bool is_quantized_per_channel
GEMMLowp quantized per-channel flag.
Definition: Types.h:1963
std::vector< int32_t > gemmlowp_shifts
GEMMLowp output stage multiplier used for quantizing to QASYMM8.
Definition: Types.h:1961
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
1 channel, 1 S32 per channel
void add_option(std::string option)
Adds option to the existing build option list.
unsigned int m
Number of LHS rows.
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: WindowHelpers.h:46
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
GEMM RHS (Right Hand Side) matrix information.
Definition: Types.h:1983
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.
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:1960
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 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...
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:1952
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;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:206
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:335
Quantize using an integer multiplication.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
bool has_padding_changed(const std::unordered_map< const ITensorInfo *, PaddingSize > &padding_map)
Check if the previously stored padding info has changed after configuring a kernel.
Definition: Utils.cpp:528
quantized, symmetric fixed-point 8-bit number
CLCompileContext class.
quantized, symmetric per channel fixed-point 8-bit number
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&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:148
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
GEMMRHSMatrixInfo rhs_info
RHS matrix information used for reshaping the RHS matrix.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
#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
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo *> infos)
Stores padding information before configuring a kernel.
Definition: Utils.cpp:513
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
Wrapper to configure the Khronos OpenCL C++ header.
unsigned int k
Number of LHS columns or RHS rows.
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
unsigned int m0
Number of rows processed by the matrix multiplication.
Definition: Types.h:1975
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:1958
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
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&#39;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:564
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)