Compute Library
 21.02
CLGEMMLowpOffsetContributionKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 
29 #include "arm_compute/core/Utils.h"
33 #include "support/StringSupport.h"
34 
35 namespace arm_compute
36 {
37 namespace
38 {
39 Status validate_arguments(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias,
40  int32_t a_offset, int32_t b_offset)
41 {
43 
44  if(bias != nullptr)
45  {
47  ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
48  ARM_COMPUTE_RETURN_ERROR_ON(mm_result->dimension(0) != bias->dimension(0));
49  }
50 
51  // If a_offset == 0, vector_sum_col can be a nullptr
52  if(a_offset != 0)
53  {
55  ARM_COMPUTE_RETURN_ERROR_ON(vector_sum_col->dimension(0) != mm_result->dimension(0));
56  }
57 
58  // If b_offset == 0, vector_sum_row can be a nullptr
59  if(b_offset != 0)
60  {
62 
63  // Check if input is a 3D reinterpretation
64  const bool reinterpret_as_3d = mm_result->num_dimensions() > 1 && mm_result->tensor_shape().y() != vector_sum_row->tensor_shape().x();
65 
66  // Validate input
67  ARM_COMPUTE_RETURN_ERROR_ON(reinterpret_as_3d && vector_sum_row->dimension(0) != (mm_result->dimension(1) * mm_result->dimension(2)));
68  ARM_COMPUTE_RETURN_ERROR_ON(!reinterpret_as_3d && vector_sum_row->dimension(0) != mm_result->dimension(1));
69 
70  TensorShape output_shape = mm_result->tensor_shape();
71  if(output_shape.num_dimensions() > 1)
72  {
73  const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2;
74 
75  TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
76  vector_sum_row_shape.collapse_from(1);
77  output_shape.collapse_from(output_batch_idx);
78 
79  ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != output_shape[output_batch_idx],
80  "mm_result tensor must have the same number of batches of output tensor");
81 
82  if(a_offset != 0)
83  {
84  TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
85  vector_sum_col_shape.collapse_from(1);
86 
87  ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_col_shape[1] != 1 && vector_sum_col_shape[1] != vector_sum_row_shape[1],
88  "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");
89  }
90  }
91  }
92 
93  return Status{};
94 }
95 } // namespace
96 
98  : _vector_sum_col(nullptr), _vector_sum_row(nullptr), _mm_result(nullptr), _bias(nullptr)
99 {
100 }
101 
102 void CLGEMMLowpOffsetContributionKernel::configure(ICLTensor *mm_result, const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias, int32_t k, int32_t a_offset,
103  int32_t b_offset)
104 {
105  configure(CLKernelLibrary::get().get_compile_context(), mm_result, vector_sum_col, vector_sum_row, bias, k, a_offset, b_offset);
106 }
107 
108 void CLGEMMLowpOffsetContributionKernel::configure(const CLCompileContext &compile_context, ICLTensor *mm_result, const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row,
109  const ICLTensor *bias,
110  int32_t k, int32_t a_offset,
111  int32_t b_offset)
112 {
113  // Perform validate step
114  ARM_COMPUTE_ERROR_ON_NULLPTR(mm_result);
116  vector_sum_col != nullptr ? vector_sum_col->info() : nullptr,
117  vector_sum_row != nullptr ? vector_sum_row->info() : nullptr,
118  bias != nullptr ? bias->info() : nullptr,
119  a_offset, b_offset)); // NOLINT
120 
121  auto padding_info = get_padding_info({ mm_result, vector_sum_col, vector_sum_row, bias });
122 
123  _vector_sum_col = vector_sum_col;
124  _vector_sum_row = vector_sum_row;
125  _mm_result = mm_result;
126  _bias = bias;
127 
128  // Check if input is a 3D reinterpretation
129  const bool reinterpret_as_3d = vector_sum_row != nullptr
130  && mm_result->info()->num_dimensions() > 1
131  && mm_result->info()->tensor_shape().y() != vector_sum_row->info()->tensor_shape().x();
132 
133  const unsigned int num_elems_processed_per_iteration = adjust_vec_size(4, mm_result->info()->dimension(0));
134 
135  // Set the arguments to pass at compile time
136  CLBuildOptions build_opts;
137  build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
138  build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(mm_result->info()->dimension(0) % num_elems_processed_per_iteration));
139 
140  // If a_offset == 0, vector_sum_col can be a nullptr
141  if(a_offset != 0)
142  {
143  build_opts.add_option("-DA_OFFSET=" + support::cpp11::to_string(a_offset));
144  build_opts.add_option_if(vector_sum_col->info()->tensor_shape().num_dimensions() > 1, "-DSUM_COL_HAS_BATCHES");
145  }
146  // If b_offset == 0, vector_sum_row can be a nullptr
147  build_opts.add_option_if(b_offset != 0, "-DB_OFFSET=" + support::cpp11::to_string(b_offset));
148  build_opts.add_option("-DK_OFFSET=" + support::cpp11::to_string(a_offset * b_offset * k));
149  build_opts.add_option_if(reinterpret_as_3d, "-DHEIGHT_INPUT3D=" + support::cpp11::to_string(mm_result->info()->dimension(1)));
150  build_opts.add_option_if(reinterpret_as_3d, "-DDEPTH_INPUT3D=" + support::cpp11::to_string(mm_result->info()->dimension(2)));
151  build_opts.add_option_if(bias != nullptr, "-DADD_BIAS");
152 
153  std::string kernel_name("gemmlowp_offset_contribution");
154 
155  // Create kernel
156  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
157 
158  // Configure kernel window
159  Window win = calculate_max_window(*mm_result->info(), Steps(num_elems_processed_per_iteration));
160  ICLKernel::configure_internal(win);
161 
162  // Set config_id for enabling LWS tuning
163  _config_id = kernel_name + "_";
164  _config_id += support::cpp11::to_string(mm_result->info()->dimension(0));
165  _config_id += "_";
166  _config_id += support::cpp11::to_string(mm_result->info()->dimension(1));
167  _config_id += "_";
168  _config_id += support::cpp11::to_string(mm_result->info()->dimension(2));
169 
171 }
172 
173 Status CLGEMMLowpOffsetContributionKernel::validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias,
174  int32_t a_offset, int32_t b_offset)
175 {
176  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(mm_result, vector_sum_col, vector_sum_row, bias, a_offset, b_offset));
177  return Status{};
178 }
179 
180 void CLGEMMLowpOffsetContributionKernel::run(const Window &window, cl::CommandQueue &queue)
181 {
184 
186  Window slice = collapsed.first_slice_window_3D();
187 
188  // Set window for vector_sum_col
189  Window win_vector_sum_col = slice;
190  win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
191  win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
192 
193  // Set window for vector_sum_row
194  Window win_vector_sum_row = slice;
195  win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
196  win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
197  win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
198 
199  Window biases_slice = slice;
200  biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1));
201  biases_slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
202 
203  do
204  {
205  unsigned int idx = 0;
206  add_3D_tensor_argument(idx, _mm_result, slice);
207  add_2D_tensor_argument_if((_vector_sum_col != nullptr), idx, _vector_sum_col, win_vector_sum_col);
208  add_2D_tensor_argument_if((_vector_sum_row != nullptr), idx, _vector_sum_row, win_vector_sum_row);
209  add_1D_tensor_argument_if((_bias != nullptr), idx, _bias, biases_slice);
210 
211  enqueue(queue, *this, slice, lws_hint());
212  }
213  while(collapsed.slide_window_slice_3D(slice));
214 }
215 } // 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:135
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
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
void configure(ICLTensor *mm_result, const ICLTensor *vector_sum_col, const ICLTensor *vector_sum_row, const ICLTensor *bias, int32_t k, int32_t a_offset, int32_t b_offset)
Initialise the kernel's input and output.
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: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.
static Status validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias, int32_t a_offset, int32_t b_offset)
Static function to check if given info will lead to a valid configuration of CLGEMMLowpOffsetContribu...
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
#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.
#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.
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:77
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx...
Definition: ICLKernel.h:172
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 S32 per channel
void add_option(std::string option)
Adds option to the existing build option list.
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:87
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
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
void collapse_from(size_t start)
Collapse dimensions starting from a given point.
Definition: Dimensions.h:183
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
std::string kernel_name
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:335
#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
CLCompileContext class.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
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:143
#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)
unsigned int num_elems_processed_per_iteration
#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
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
unsigned int adjust_vec_size(unsigned int vec_size, size_t dim0)
Returns the adjusted vector size in case it is less than the input&#39;s first dimension, getting rounded down to its closest valid vector size.
Definition: Utils.h:1358
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:92
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
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)