blob: 2e1c56c3ba5fc467e1e1f45a737f3c863f2a6635 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * 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"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Utils.h"
36#include "arm_compute/core/Validate.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{
63 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
64 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16);
65 ARM_COMPUTE_ERROR_ON(conv == nullptr);
66
67 _input = input;
68 _output = output;
69
Georgios Pinitase9146ed2018-02-07 16:05:47 +000070 std::stringstream kernel_name;
71 CLBuildOptions build_opts;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 kernel_name << "convolution" << matrix_size << "x" << matrix_size << "_static";
73
74 if(scale == 0)
75 {
76 scale = calculate_matrix_scale(conv, matrix_size);
77 }
78
79 for(unsigned int i = 0; i < matrix_size * matrix_size; i++)
80 {
81 std::stringstream mat_str;
82 mat_str << "-DMAT" << i << "=" << conv[i];
Georgios Pinitase9146ed2018-02-07 16:05:47 +000083 build_opts.add_option(mat_str.str());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 }
85
Georgios Pinitase9146ed2018-02-07 16:05:47 +000086 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(scale));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087
88 DataType data_type = data_type_for_convolution_matrix(conv, matrix_size * matrix_size);
Georgios Pinitase9146ed2018-02-07 16:05:47 +000089 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
91 std::stringstream out_type;
92 out_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
Georgios Pinitase9146ed2018-02-07 16:05:47 +000093 build_opts.add_option(out_type.str());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094
Georgios Pinitase9146ed2018-02-07 16:05:47 +000095 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name.str(), build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
97 // Configure kernel window
98 constexpr unsigned int num_elems_processed_per_iteration = 8;
99 constexpr unsigned int num_elems_written_per_iteration = 8;
100 constexpr unsigned int num_elems_read_per_iteration = 16;
101 constexpr unsigned int num_rows_read_per_iteration = matrix_size;
102
103 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
104
105 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
106 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
107
108 update_window_and_padding(win, input_access, output_access);
109
110 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
111
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100112 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113}
114
115/****************************************************************************************\
116 * Separable Convolution *
117\****************************************************************************************/
118template <unsigned int matrix_size>
119CLSeparableConvolutionHorKernel<matrix_size>::CLSeparableConvolutionHorKernel()
120 : _border_size(0)
121{
122}
123
124template <unsigned int matrix_size>
125BorderSize CLSeparableConvolutionHorKernel<matrix_size>::border_size() const
126{
127 return _border_size;
128}
129
130template <unsigned int matrix_size>
131void CLSeparableConvolutionHorKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined)
132{
133 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
134 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U16, DataType::S16, DataType::S32);
135
136 ARM_COMPUTE_ERROR_ON((matrix_size != 5) && (matrix_size != 7) && (matrix_size != 9));
137
138 _input = input;
139 _output = output;
140 _border_size = BorderSize(border_undefined ? 0 : matrix_size / 2, matrix_size / 2);
141
142 // Set build options
143 std::set<std::string> build_opts;
144
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100145 std::array<int16_t, matrix_size *matrix_size> mat = { 0 };
146 memcpy(mat.data(), conv, matrix_size * sizeof(int16_t));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147
148 for(unsigned int j = 0; j < matrix_size * matrix_size; j++)
149 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100150 build_opts.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 }
152
153 build_opts.insert("-DSCALE=0");
154
155 build_opts.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(output->info()->data_type()));
156
157 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100158 const std::string kernel_name = "convolution_separable1x" + support::cpp11::to_string(matrix_size) + "_static";
159 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160
161 // Configure kernel window
162 constexpr unsigned int num_elems_processed_per_iteration = 8;
163 constexpr unsigned int num_elems_read_per_iteration = 16;
164 constexpr unsigned int num_elems_written_per_iteration = 8;
165
166 Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
167
168 AccessWindowHorizontal input_access(input->info(), -border_size().left, num_elems_read_per_iteration);
169 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
170
171 update_window_and_padding(win, input_access, output_access);
172
173 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
174
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100175 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100176
177 // Set config_id for enabling LWS tuning
178 _config_id = kernel_name;
179 _config_id += "_";
180 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
181 _config_id += "_";
182 _config_id += support::cpp11::to_string(input->info()->dimension(0));
183 _config_id += "_";
184 _config_id += support::cpp11::to_string(input->info()->dimension(1));
185 _config_id += "_";
186 _config_id += support::cpp11::to_string(output->info()->dimension(0));
187 _config_id += "_";
188 _config_id += support::cpp11::to_string(output->info()->dimension(1));
189 _config_id += "_";
190 _config_id += support::cpp11::to_string(border_undefined);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191}
192
193template <unsigned int matrix_size>
194BorderSize CLSeparableConvolutionVertKernel<matrix_size>::border_size() const
195{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100196 return BorderSize{ matrix_size / 2, 0 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197}
198
199template <unsigned int matrix_size>
200void CLSeparableConvolutionVertKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output,
201 const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type)
202{
203 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16, DataType::S16, DataType::S32);
204 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16);
205 ARM_COMPUTE_ERROR_ON((matrix_size != 5) && (matrix_size != 7) && (matrix_size != 9));
206 ARM_COMPUTE_ERROR_ON(scale == 0);
207
208 _input = input;
209 _output = output;
210
211 std::set<std::string> build_opts;
212
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100213 std::array<int16_t, matrix_size *matrix_size> mat = { 0 };
214 memcpy(mat.data() + matrix_size, conv, matrix_size * sizeof(int16_t));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215
216 for(unsigned int j = 0; j < matrix_size * matrix_size; j++)
217 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100218 build_opts.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219 }
220
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100221 build_opts.insert("-DSCALE=" + support::cpp11::to_string(scale));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222
223 build_opts.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
224
225 build_opts.insert("-DCOMPUTE_TYPE=" + get_cl_type_from_data_type(data_type));
226
227 std::stringstream out_type;
228 out_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
229 build_opts.insert(out_type.str());
230
231 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100232 const std::string kernel_name = "convolution_separable" + support::cpp11::to_string(matrix_size) + "x1_static";
233 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234
235 // Configure kernel window
236 constexpr unsigned int num_elems_processed_per_iteration = 8;
237 constexpr unsigned int num_elems_written_per_iteration = 8;
238 constexpr unsigned int num_elems_read_per_iteration = 8;
239 constexpr unsigned int num_rows_read_per_iteration = matrix_size;
240
241 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
242
243 AccessWindowRectangle input_access(input->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
244 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
245
246 update_window_and_padding(win, input_access, output_access);
247
248 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
249
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100250 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100251
252 // Set config_id for enabling LWS tuning
253 _config_id = kernel_name;
254 _config_id += "_";
255 _config_id += lower_string(string_from_data_type(data_type));
256 _config_id += "_";
257 _config_id += support::cpp11::to_string(input->info()->dimension(0));
258 _config_id += "_";
259 _config_id += support::cpp11::to_string(input->info()->dimension(1));
260 _config_id += "_";
261 _config_id += support::cpp11::to_string(output->info()->dimension(0));
262 _config_id += "_";
263 _config_id += support::cpp11::to_string(output->info()->dimension(1));
264 _config_id += "_";
265 _config_id += support::cpp11::to_string(border_undefined);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100266}
267
268/****************************************************************************************\
269 * Rectangle Convolution *
270\****************************************************************************************/
271
272CLConvolutionRectangleKernel::CLConvolutionRectangleKernel()
273 : _border_size(0), _input(nullptr), _output(nullptr)
274{
275}
276
277BorderSize CLConvolutionRectangleKernel::border_size() const
278{
279 return _border_size;
280}
281
282void CLConvolutionRectangleKernel::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale, bool border_undefined)
283{
284 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
285 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16);
286 ARM_COMPUTE_ERROR_ON(nullptr == conv);
287 ARM_COMPUTE_ERROR_ON(3 != width && 5 != width && 7 != width && 9 != width);
288 ARM_COMPUTE_ERROR_ON(3 != height && 5 != height && 7 != height && 9 != height);
289 ARM_COMPUTE_ERROR_ON(0 == scale);
290
291 _input = input;
292 _output = output;
293 _border_size = BorderSize(height / 2, width / 2);
294
295 std::set<std::string> options;
296
297 std::stringstream output_type;
298 output_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
299 options.insert(output_type.str());
300
301 uint32_t matrix_size = width * height;
302
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100303 std::array<int16_t, max_matrix_size> mat = { 0 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100304
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100305 memcpy(mat.data(), conv, matrix_size * sizeof(int16_t));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100306
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100307 for(unsigned int j = 0; j < max_matrix_size; j++)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100308 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100309 options.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100310 }
311
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100312 options.insert("-DSCALE=" + support::cpp11::to_string(scale));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100313
314 DataType data_type = data_type_for_convolution_matrix(conv, matrix_size);
315 options.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
316
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100317 options.insert("-DMATRIX_WIDTH=" + support::cpp11::to_string(width));
318 options.insert("-DMATRIX_HEIGHT=" + support::cpp11::to_string(height));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319
320 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("convolution_rectangle", options));
321
322 // Configure kernel window
323 constexpr unsigned int num_elems_processed_per_iteration = 8;
324 constexpr unsigned int num_elems_read_per_iteration = 16;
325 constexpr unsigned int num_elems_written_per_iteration = 8;
326 const unsigned int num_rows_read_per_iteration = height;
327
328 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
329
330 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
331 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
332
333 update_window_and_padding(win, input_access, output_access);
334
335 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
336
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100337 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338}
339
340void CLConvolutionRectangleKernel::run(const Window &window, cl::CommandQueue &queue)
341{
342 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
343 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
344
345 Window slice = window.first_slice_window_2D();
346
347 do
348 {
349 unsigned int idx = 0;
350 add_2D_tensor_argument(idx, _input, slice);
351 add_2D_tensor_argument(idx, _output, slice);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100352 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100353 }
354 while(window.slide_window_slice_2D(slice));
355}
356
357template class arm_compute::CLConvolutionKernel<3>;
358template class arm_compute::CLConvolutionKernel<5>;
359template class arm_compute::CLConvolutionKernel<7>;
360template class arm_compute::CLConvolutionKernel<9>;
361template class arm_compute::CLSeparableConvolutionVertKernel<5>;
362template class arm_compute::CLSeparableConvolutionVertKernel<7>;
363template class arm_compute::CLSeparableConvolutionVertKernel<9>;
364template class arm_compute::CLSeparableConvolutionHorKernel<5>;
365template class arm_compute::CLSeparableConvolutionHorKernel<7>;
366template class arm_compute::CLSeparableConvolutionHorKernel<9>;
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100367} // namespace arm_compute