blob: 48b185f78da4c5796c6bfb8cde86fe1dfbc68134 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 "arm_compute/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/ICLKernel.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39#include <set>
40#include <sstream>
41#include <string>
42
Michalis Spyrou299fdd32019-05-01 13:03:59 +010043namespace arm_compute
44{
45namespace
46{
47constexpr unsigned int max_matrix_size = 81;
48} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049
50/****************************************************************************************\
51 * Square Convolution *
52\****************************************************************************************/
53
54template <unsigned int matrix_size>
55BorderSize CLConvolutionKernel<matrix_size>::border_size() const
56{
57 return BorderSize(matrix_size / 2);
58}
59
60template <unsigned int matrix_size>
61void CLConvolutionKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined)
62{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010063 configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, scale, border_undefined);
64}
65
66template <unsigned int matrix_size>
Manuel Bottini256c0b92020-04-21 13:29:30 +010067void CLConvolutionKernel<matrix_size>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010068{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 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
Georgios Pinitase9146ed2018-02-07 16:05:47 +000076 std::stringstream kernel_name;
77 CLBuildOptions build_opts;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 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];
Georgios Pinitase9146ed2018-02-07 16:05:47 +000089 build_opts.add_option(mat_str.str());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 }
91
Georgios Pinitase9146ed2018-02-07 16:05:47 +000092 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(scale));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093
94 DataType data_type = data_type_for_convolution_matrix(conv, matrix_size * matrix_size);
Georgios Pinitase9146ed2018-02-07 16:05:47 +000095 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
97 std::stringstream out_type;
98 out_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
Georgios Pinitase9146ed2018-02-07 16:05:47 +000099 build_opts.add_option(out_type.str());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100101 _kernel = create_kernel(compile_context, kernel_name.str(), build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
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
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100118 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119}
120
121/****************************************************************************************\
122 * Separable Convolution *
123\****************************************************************************************/
124template <unsigned int matrix_size>
125CLSeparableConvolutionHorKernel<matrix_size>::CLSeparableConvolutionHorKernel()
126 : _border_size(0)
127{
128}
129
130template <unsigned int matrix_size>
131BorderSize CLSeparableConvolutionHorKernel<matrix_size>::border_size() const
132{
133 return _border_size;
134}
135
136template <unsigned int matrix_size>
137void CLSeparableConvolutionHorKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined)
138{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100139 configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, border_undefined);
140}
141
142template <unsigned int matrix_size>
Manuel Bottini256c0b92020-04-21 13:29:30 +0100143void CLSeparableConvolutionHorKernel<matrix_size>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100144{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 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
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100157 std::array<int16_t, matrix_size *matrix_size> mat = { 0 };
158 memcpy(mat.data(), conv, matrix_size * sizeof(int16_t));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159
160 for(unsigned int j = 0; j < matrix_size * matrix_size; j++)
161 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100162 build_opts.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 }
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
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100170 const std::string kernel_name = "convolution_separable1x" + support::cpp11::to_string(matrix_size) + "_static";
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100171 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172
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
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100187 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100188
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);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203}
204
205template <unsigned int matrix_size>
206BorderSize CLSeparableConvolutionVertKernel<matrix_size>::border_size() const
207{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100208 return BorderSize{ matrix_size / 2, 0 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209}
210
211template <unsigned int matrix_size>
212void CLSeparableConvolutionVertKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output,
213 const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type)
214{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100215 configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, scale, border_undefined, data_type);
216}
217
218template <unsigned int matrix_size>
Manuel Bottini256c0b92020-04-21 13:29:30 +0100219void CLSeparableConvolutionVertKernel<matrix_size>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100220 const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type)
221{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222 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
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100232 std::array<int16_t, matrix_size *matrix_size> mat = { 0 };
233 memcpy(mat.data() + matrix_size, conv, matrix_size * sizeof(int16_t));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234
235 for(unsigned int j = 0; j < matrix_size * matrix_size; j++)
236 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100237 build_opts.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238 }
239
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100240 build_opts.insert("-DSCALE=" + support::cpp11::to_string(scale));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241
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
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100251 const std::string kernel_name = "convolution_separable" + support::cpp11::to_string(matrix_size) + "x1_static";
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100252 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253
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
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100269 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100270
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);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100285}
286
287/****************************************************************************************\
288 * Rectangle Convolution *
289\****************************************************************************************/
290
291CLConvolutionRectangleKernel::CLConvolutionRectangleKernel()
292 : _border_size(0), _input(nullptr), _output(nullptr)
293{
294}
295
296BorderSize CLConvolutionRectangleKernel::border_size() const
297{
298 return _border_size;
299}
300
301void CLConvolutionRectangleKernel::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale, bool border_undefined)
302{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100303 configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, width, height, scale, border_undefined);
304}
305
Manuel Bottini256c0b92020-04-21 13:29:30 +0100306void CLConvolutionRectangleKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100307 bool border_undefined)
308{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309 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
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100328 std::array<int16_t, max_matrix_size> mat = { 0 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100329
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100330 memcpy(mat.data(), conv, matrix_size * sizeof(int16_t));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100332 for(unsigned int j = 0; j < max_matrix_size; j++)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100333 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100334 options.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335 }
336
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100337 options.insert("-DSCALE=" + support::cpp11::to_string(scale));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338
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
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100342 options.insert("-DMATRIX_WIDTH=" + support::cpp11::to_string(width));
343 options.insert("-DMATRIX_HEIGHT=" + support::cpp11::to_string(height));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100345 _kernel = create_kernel(compile_context, "convolution_rectangle", options);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100346
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
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100362 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100363}
364
365void 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);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100377 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378 }
379 while(window.slide_window_slice_2D(slice));
380}
381
382template class arm_compute::CLConvolutionKernel<3>;
383template class arm_compute::CLConvolutionKernel<5>;
384template class arm_compute::CLConvolutionKernel<7>;
385template class arm_compute::CLConvolutionKernel<9>;
386template class arm_compute::CLSeparableConvolutionVertKernel<5>;
387template class arm_compute::CLSeparableConvolutionVertKernel<7>;
388template class arm_compute::CLSeparableConvolutionVertKernel<9>;
389template class arm_compute::CLSeparableConvolutionHorKernel<5>;
390template class arm_compute::CLSeparableConvolutionHorKernel<7>;
391template class arm_compute::CLSeparableConvolutionHorKernel<9>;
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100392} // namespace arm_compute