Compute Library
 21.02
CLConvolutionKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-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 
30 #include "arm_compute/core/Error.h"
33 #include "arm_compute/core/Utils.h"
35 #include "src/core/CL/ICLKernel.h"
37 #include "support/StringSupport.h"
38 
39 #include <set>
40 #include <sstream>
41 #include <string>
42 
43 namespace arm_compute
44 {
45 namespace
46 {
47 constexpr unsigned int max_matrix_size = 81;
48 } // namespace
49 
50 /****************************************************************************************\
51  * Square Convolution *
52 \****************************************************************************************/
53 
54 template <unsigned int matrix_size>
56 {
57  return BorderSize(matrix_size / 2);
58 }
59 
60 template <unsigned int matrix_size>
61 void CLConvolutionKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined)
62 {
63  configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, scale, border_undefined);
64 }
65 
66 template <unsigned int matrix_size>
67 void CLConvolutionKernel<matrix_size>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined)
68 {
71  ARM_COMPUTE_ERROR_ON(conv == nullptr);
72 
73  _input = input;
74  _output = output;
75 
76  std::stringstream kernel_name;
77  CLBuildOptions build_opts;
78  kernel_name << "convolution" << matrix_size << "x" << matrix_size << "_static";
79 
80  if(scale == 0)
81  {
82  scale = calculate_matrix_scale(conv, matrix_size);
83  }
84 
85  for(unsigned int i = 0; i < matrix_size * matrix_size; i++)
86  {
87  std::stringstream mat_str;
88  mat_str << "-DMAT" << i << "=" << conv[i];
89  build_opts.add_option(mat_str.str());
90  }
91 
92  build_opts.add_option("-DSCALE=" + support::cpp11::to_string(scale));
93 
94  DataType data_type = data_type_for_convolution_matrix(conv, matrix_size * matrix_size);
95  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
96 
97  std::stringstream out_type;
98  out_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
99  build_opts.add_option(out_type.str());
100 
101  _kernel = create_kernel(compile_context, kernel_name.str(), build_opts.options());
102 
103  // Configure kernel window
104  constexpr unsigned int num_elems_processed_per_iteration = 8;
105  constexpr unsigned int num_elems_written_per_iteration = 8;
106  constexpr unsigned int num_elems_read_per_iteration = 16;
107  constexpr unsigned int num_rows_read_per_iteration = matrix_size;
108 
109  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
110 
111  AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
112  AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
113 
114  update_window_and_padding(win, input_access, output_access);
115 
116  output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
117 
118  ICLKernel::configure_internal(win);
119 }
120 
121 /****************************************************************************************\
122  * Separable Convolution *
123 \****************************************************************************************/
124 template <unsigned int matrix_size>
126  : _border_size(0)
127 {
128 }
129 
130 template <unsigned int matrix_size>
132 {
133  return _border_size;
134 }
135 
136 template <unsigned int matrix_size>
137 void CLSeparableConvolutionHorKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined)
138 {
139  configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, border_undefined);
140 }
141 
142 template <unsigned int matrix_size>
143 void CLSeparableConvolutionHorKernel<matrix_size>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined)
144 {
147 
148  ARM_COMPUTE_ERROR_ON((matrix_size != 5) && (matrix_size != 7) && (matrix_size != 9));
149 
150  _input = input;
151  _output = output;
152  _border_size = BorderSize(border_undefined ? 0 : matrix_size / 2, matrix_size / 2);
153 
154  // Set build options
155  std::set<std::string> build_opts;
156 
157  std::array<int16_t, matrix_size *matrix_size> mat = { 0 };
158  memcpy(mat.data(), conv, matrix_size * sizeof(int16_t));
159 
160  for(unsigned int j = 0; j < matrix_size * matrix_size; j++)
161  {
162  build_opts.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
163  }
164 
165  build_opts.insert("-DSCALE=0");
166 
167  build_opts.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(output->info()->data_type()));
168 
169  // Create kernel
170  const std::string kernel_name = "convolution_separable1x" + support::cpp11::to_string(matrix_size) + "_static";
171  _kernel = create_kernel(compile_context, kernel_name, build_opts);
172 
173  // Configure kernel window
174  constexpr unsigned int num_elems_processed_per_iteration = 8;
175  constexpr unsigned int num_elems_read_per_iteration = 16;
176  constexpr unsigned int num_elems_written_per_iteration = 8;
177 
178  Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
179 
180  AccessWindowHorizontal input_access(input->info(), -border_size().left, num_elems_read_per_iteration);
181  AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
182 
183  update_window_and_padding(win, input_access, output_access);
184 
185  output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
186 
187  ICLKernel::configure_internal(win);
188 
189  // Set config_id for enabling LWS tuning
190  _config_id = kernel_name;
191  _config_id += "_";
192  _config_id += lower_string(string_from_data_type(input->info()->data_type()));
193  _config_id += "_";
194  _config_id += support::cpp11::to_string(input->info()->dimension(0));
195  _config_id += "_";
196  _config_id += support::cpp11::to_string(input->info()->dimension(1));
197  _config_id += "_";
198  _config_id += support::cpp11::to_string(output->info()->dimension(0));
199  _config_id += "_";
200  _config_id += support::cpp11::to_string(output->info()->dimension(1));
201  _config_id += "_";
202  _config_id += support::cpp11::to_string(border_undefined);
203 }
204 
205 template <unsigned int matrix_size>
207 {
208  return BorderSize{ matrix_size / 2, 0 };
209 }
210 
211 template <unsigned int matrix_size>
213  const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type)
214 {
215  configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, scale, border_undefined, data_type);
216 }
217 
218 template <unsigned int matrix_size>
220  const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type)
221 {
224  ARM_COMPUTE_ERROR_ON((matrix_size != 5) && (matrix_size != 7) && (matrix_size != 9));
225  ARM_COMPUTE_ERROR_ON(scale == 0);
226 
227  _input = input;
228  _output = output;
229 
230  std::set<std::string> build_opts;
231 
232  std::array<int16_t, matrix_size *matrix_size> mat = { 0 };
233  memcpy(mat.data() + matrix_size, conv, matrix_size * sizeof(int16_t));
234 
235  for(unsigned int j = 0; j < matrix_size * matrix_size; j++)
236  {
237  build_opts.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
238  }
239 
240  build_opts.insert("-DSCALE=" + support::cpp11::to_string(scale));
241 
242  build_opts.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
243 
244  build_opts.insert("-DCOMPUTE_TYPE=" + get_cl_type_from_data_type(data_type));
245 
246  std::stringstream out_type;
247  out_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
248  build_opts.insert(out_type.str());
249 
250  // Create kernel
251  const std::string kernel_name = "convolution_separable" + support::cpp11::to_string(matrix_size) + "x1_static";
252  _kernel = create_kernel(compile_context, kernel_name, build_opts);
253 
254  // Configure kernel window
255  constexpr unsigned int num_elems_processed_per_iteration = 8;
256  constexpr unsigned int num_elems_written_per_iteration = 8;
257  constexpr unsigned int num_elems_read_per_iteration = 8;
258  constexpr unsigned int num_rows_read_per_iteration = matrix_size;
259 
260  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
261 
262  AccessWindowRectangle input_access(input->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
263  AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
264 
265  update_window_and_padding(win, input_access, output_access);
266 
267  output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
268 
269  ICLKernel::configure_internal(win);
270 
271  // Set config_id for enabling LWS tuning
272  _config_id = kernel_name;
273  _config_id += "_";
274  _config_id += lower_string(string_from_data_type(data_type));
275  _config_id += "_";
276  _config_id += support::cpp11::to_string(input->info()->dimension(0));
277  _config_id += "_";
278  _config_id += support::cpp11::to_string(input->info()->dimension(1));
279  _config_id += "_";
280  _config_id += support::cpp11::to_string(output->info()->dimension(0));
281  _config_id += "_";
282  _config_id += support::cpp11::to_string(output->info()->dimension(1));
283  _config_id += "_";
284  _config_id += support::cpp11::to_string(border_undefined);
285 }
286 
287 /****************************************************************************************\
288  * Rectangle Convolution *
289 \****************************************************************************************/
290 
292  : _border_size(0), _input(nullptr), _output(nullptr)
293 {
294 }
295 
297 {
298  return _border_size;
299 }
300 
301 void CLConvolutionRectangleKernel::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale, bool border_undefined)
302 {
303  configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, width, height, scale, border_undefined);
304 }
305 
306 void CLConvolutionRectangleKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale,
307  bool border_undefined)
308 {
311  ARM_COMPUTE_ERROR_ON(nullptr == conv);
312  ARM_COMPUTE_ERROR_ON(3 != width && 5 != width && 7 != width && 9 != width);
313  ARM_COMPUTE_ERROR_ON(3 != height && 5 != height && 7 != height && 9 != height);
314  ARM_COMPUTE_ERROR_ON(0 == scale);
315 
316  _input = input;
317  _output = output;
318  _border_size = BorderSize(height / 2, width / 2);
319 
320  std::set<std::string> options;
321 
322  std::stringstream output_type;
323  output_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
324  options.insert(output_type.str());
325 
326  uint32_t matrix_size = width * height;
327 
328  std::array<int16_t, max_matrix_size> mat = { 0 };
329 
330  memcpy(mat.data(), conv, matrix_size * sizeof(int16_t));
331 
332  for(unsigned int j = 0; j < max_matrix_size; j++)
333  {
334  options.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
335  }
336 
337  options.insert("-DSCALE=" + support::cpp11::to_string(scale));
338 
340  options.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
341 
342  options.insert("-DMATRIX_WIDTH=" + support::cpp11::to_string(width));
343  options.insert("-DMATRIX_HEIGHT=" + support::cpp11::to_string(height));
344 
345  _kernel = create_kernel(compile_context, "convolution_rectangle", options);
346 
347  // Configure kernel window
348  constexpr unsigned int num_elems_processed_per_iteration = 8;
349  constexpr unsigned int num_elems_read_per_iteration = 16;
350  constexpr unsigned int num_elems_written_per_iteration = 8;
351  const unsigned int num_rows_read_per_iteration = height;
352 
353  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
354 
355  AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
356  AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
357 
358  update_window_and_padding(win, input_access, output_access);
359 
360  output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
361 
362  ICLKernel::configure_internal(win);
363 }
364 
365 void CLConvolutionRectangleKernel::run(const Window &window, cl::CommandQueue &queue)
366 {
369 
371 
372  do
373  {
374  unsigned int idx = 0;
375  add_2D_tensor_argument(idx, _input, slice);
376  add_2D_tensor_argument(idx, _output, slice);
377  enqueue(queue, *this, slice, lws_hint());
378  }
379  while(window.slide_window_slice_2D(slice));
380 }
381 
392 } // namespace arm_compute
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
unsigned int top
top of the border
Definition: Types.h:375
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 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
Kernel for the Horizontal pass of a Separable Convolution.
Definition: CLConvolution.h:42
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Container for 2D border size.
Definition: Types.h:273
const StringSet & options() const
Gets the current options list set.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
1 channel, 1 U8 per channel
std::string to_string(T &&value)
Convert integer and float values to string.
void configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined)
Initialise the kernel&#39;s input, output and border mode.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
BorderSize border_size() const override
The size of the border for that kernel.
#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.
1 channel, 1 U16 per channel
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:350
uint32_t calculate_matrix_scale(const int16_t *matrix, unsigned int matrix_size)
Calculate the scale of the given square matrix.
Definition: Utils.h:727
Window calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:323
Copyright (c) 2017-2021 Arm Limited.
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
1 channel, 1 S32 per channel
void add_option(std::string option)
Adds option to the existing build option list.
const DataType data_type
Definition: Im2Col.cpp:150
void configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type=DataType::S32)
Initialise the kernel&#39;s input, output and border mode.
Implementation of a rectangular access pattern.
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
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:135
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
BorderSize border_size() const override
The size of the border for that kernel.
Implementation of a row access pattern.
std::string kernel_name
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
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
Interface for the kernel to run an arbitrary size convolution on a tensor.
Definition: CLConvolution.h:40
unsigned int left
left of the border
Definition: Types.h:378
BorderSize border_size() const override
The size of the border for that kernel.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
1 channel, 1 S16 per channel
Kernel for the Vertical pass of a Separable Convolution.
Definition: CLConvolution.h:44
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
CLCompileContext class.
void configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined)
Initialise the kernel&#39;s input, output and border mode.
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
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
Wrapper to configure the Khronos OpenCL C++ header.
BorderSize border_size() const override
The size of the border for that kernel.
void configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale, bool border_undefined)
Initialise the kernel&#39;s input, output and border mode.
unsigned int num_elems_processed_per_iteration
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
DataType
Available data types.
Definition: Types.h:77
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)
DataType data_type_for_convolution_matrix(const int16_t *conv, size_t size)
Calculate the accuracy required by the squared convolution calculation.
Definition: Utils.h:862