• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
24 #include "src/core/CL/kernels/CLConvolutionKernel.h"
25 
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #include "arm_compute/core/CL/ICLTensor.h"
29 #include "arm_compute/core/CL/OpenCL.h"
30 #include "arm_compute/core/Error.h"
31 #include "arm_compute/core/Helpers.h"
32 #include "arm_compute/core/TensorInfo.h"
33 #include "arm_compute/core/Utils.h"
34 #include "arm_compute/core/Validate.h"
35 #include "src/core/CL/ICLKernel.h"
36 #include "src/core/helpers/WindowHelpers.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>
border_size() const55 BorderSize             CLConvolutionKernel<matrix_size>::border_size() const
56 {
57     return BorderSize(matrix_size / 2);
58 }
59 
60 template <unsigned int matrix_size>
configure(const ICLTensor * input,ICLTensor * output,const int16_t * conv,uint32_t scale,bool border_undefined)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>
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output,const int16_t * conv,uint32_t scale,bool border_undefined)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 {
69     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
70     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16);
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>
CLSeparableConvolutionHorKernel()125 CLSeparableConvolutionHorKernel<matrix_size>::CLSeparableConvolutionHorKernel()
126     : _border_size(0)
127 {
128 }
129 
130 template <unsigned int matrix_size>
border_size() const131 BorderSize             CLSeparableConvolutionHorKernel<matrix_size>::border_size() const
132 {
133     return _border_size;
134 }
135 
136 template <unsigned int matrix_size>
configure(const ICLTensor * input,ICLTensor * output,const int16_t * conv,bool border_undefined)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>
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output,const int16_t * conv,bool border_undefined)143 void CLSeparableConvolutionHorKernel<matrix_size>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined)
144 {
145     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
146     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U16, DataType::S16, DataType::S32);
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>
border_size() const206 BorderSize             CLSeparableConvolutionVertKernel<matrix_size>::border_size() const
207 {
208     return BorderSize{ matrix_size / 2, 0 };
209 }
210 
211 template <unsigned int matrix_size>
configure(const ICLTensor * input,ICLTensor * output,const int16_t * conv,uint32_t scale,bool border_undefined,DataType data_type)212 void CLSeparableConvolutionVertKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output,
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>
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output,const int16_t * conv,uint32_t scale,bool border_undefined,DataType data_type)219 void CLSeparableConvolutionVertKernel<matrix_size>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output,
220                                                               const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type)
221 {
222     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16, DataType::S16, DataType::S32);
223     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16);
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 
CLConvolutionRectangleKernel()291 CLConvolutionRectangleKernel::CLConvolutionRectangleKernel()
292     : _border_size(0), _input(nullptr), _output(nullptr)
293 {
294 }
295 
border_size() const296 BorderSize CLConvolutionRectangleKernel::border_size() const
297 {
298     return _border_size;
299 }
300 
configure(const ICLTensor * input,ICLTensor * output,const int16_t * conv,uint32_t width,uint32_t height,uint32_t scale,bool border_undefined)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 
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output,const int16_t * conv,uint32_t width,uint32_t height,uint32_t scale,bool border_undefined)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 {
309     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
310     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16);
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 
339     DataType data_type = data_type_for_convolution_matrix(conv, matrix_size);
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 
run(const Window & window,cl::CommandQueue & queue)365 void CLConvolutionRectangleKernel::run(const Window &window, cl::CommandQueue &queue)
366 {
367     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
368     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
369 
370     Window slice = window.first_slice_window_2D();
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 
382 template class arm_compute::CLConvolutionKernel<3>;
383 template class arm_compute::CLConvolutionKernel<5>;
384 template class arm_compute::CLConvolutionKernel<7>;
385 template class arm_compute::CLConvolutionKernel<9>;
386 template class arm_compute::CLSeparableConvolutionVertKernel<5>;
387 template class arm_compute::CLSeparableConvolutionVertKernel<7>;
388 template class arm_compute::CLSeparableConvolutionVertKernel<9>;
389 template class arm_compute::CLSeparableConvolutionHorKernel<5>;
390 template class arm_compute::CLSeparableConvolutionHorKernel<7>;
391 template class arm_compute::CLSeparableConvolutionHorKernel<9>;
392 } // namespace arm_compute
393