blob: ef2629d208165c2667b000e075df0b0f12af9e29 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou299fdd32019-05-01 13:03:59 +01002 * Copyright (c) 2016-2019 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"
37
38#include <set>
39#include <sstream>
40#include <string>
41
Michalis Spyrou299fdd32019-05-01 13:03:59 +010042namespace arm_compute
43{
44namespace
45{
46constexpr unsigned int max_matrix_size = 81;
47} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048
49/****************************************************************************************\
50 * Square Convolution *
51\****************************************************************************************/
52
53template <unsigned int matrix_size>
54BorderSize CLConvolutionKernel<matrix_size>::border_size() const
55{
56 return BorderSize(matrix_size / 2);
57}
58
59template <unsigned int matrix_size>
60void CLConvolutionKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined)
61{
62 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
63 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16);
64 ARM_COMPUTE_ERROR_ON(conv == nullptr);
65
66 _input = input;
67 _output = output;
68
Georgios Pinitase9146ed2018-02-07 16:05:47 +000069 std::stringstream kernel_name;
70 CLBuildOptions build_opts;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 kernel_name << "convolution" << matrix_size << "x" << matrix_size << "_static";
72
73 if(scale == 0)
74 {
75 scale = calculate_matrix_scale(conv, matrix_size);
76 }
77
78 for(unsigned int i = 0; i < matrix_size * matrix_size; i++)
79 {
80 std::stringstream mat_str;
81 mat_str << "-DMAT" << i << "=" << conv[i];
Georgios Pinitase9146ed2018-02-07 16:05:47 +000082 build_opts.add_option(mat_str.str());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 }
84
Georgios Pinitase9146ed2018-02-07 16:05:47 +000085 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(scale));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086
87 DataType data_type = data_type_for_convolution_matrix(conv, matrix_size * matrix_size);
Georgios Pinitase9146ed2018-02-07 16:05:47 +000088 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089
90 std::stringstream out_type;
91 out_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
Georgios Pinitase9146ed2018-02-07 16:05:47 +000092 build_opts.add_option(out_type.str());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093
Georgios Pinitase9146ed2018-02-07 16:05:47 +000094 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name.str(), build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
96 // Configure kernel window
97 constexpr unsigned int num_elems_processed_per_iteration = 8;
98 constexpr unsigned int num_elems_written_per_iteration = 8;
99 constexpr unsigned int num_elems_read_per_iteration = 16;
100 constexpr unsigned int num_rows_read_per_iteration = matrix_size;
101
102 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
103
104 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
105 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
106
107 update_window_and_padding(win, input_access, output_access);
108
109 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
110
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100111 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112}
113
114/****************************************************************************************\
115 * Separable Convolution *
116\****************************************************************************************/
117template <unsigned int matrix_size>
118CLSeparableConvolutionHorKernel<matrix_size>::CLSeparableConvolutionHorKernel()
119 : _border_size(0)
120{
121}
122
123template <unsigned int matrix_size>
124BorderSize CLSeparableConvolutionHorKernel<matrix_size>::border_size() const
125{
126 return _border_size;
127}
128
129template <unsigned int matrix_size>
130void CLSeparableConvolutionHorKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined)
131{
132 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
133 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U16, DataType::S16, DataType::S32);
134
135 ARM_COMPUTE_ERROR_ON((matrix_size != 5) && (matrix_size != 7) && (matrix_size != 9));
136
137 _input = input;
138 _output = output;
139 _border_size = BorderSize(border_undefined ? 0 : matrix_size / 2, matrix_size / 2);
140
141 // Set build options
142 std::set<std::string> build_opts;
143
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100144 std::array<int16_t, matrix_size *matrix_size> mat = { 0 };
145 memcpy(mat.data(), conv, matrix_size * sizeof(int16_t));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146
147 for(unsigned int j = 0; j < matrix_size * matrix_size; j++)
148 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100149 build_opts.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 }
151
152 build_opts.insert("-DSCALE=0");
153
154 build_opts.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(output->info()->data_type()));
155
156 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100157 const std::string kernel_name = "convolution_separable1x" + support::cpp11::to_string(matrix_size) + "_static";
158 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159
160 // Configure kernel window
161 constexpr unsigned int num_elems_processed_per_iteration = 8;
162 constexpr unsigned int num_elems_read_per_iteration = 16;
163 constexpr unsigned int num_elems_written_per_iteration = 8;
164
165 Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
166
167 AccessWindowHorizontal input_access(input->info(), -border_size().left, num_elems_read_per_iteration);
168 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
169
170 update_window_and_padding(win, input_access, output_access);
171
172 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
173
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100174 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100175
176 // Set config_id for enabling LWS tuning
177 _config_id = kernel_name;
178 _config_id += "_";
179 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
180 _config_id += "_";
181 _config_id += support::cpp11::to_string(input->info()->dimension(0));
182 _config_id += "_";
183 _config_id += support::cpp11::to_string(input->info()->dimension(1));
184 _config_id += "_";
185 _config_id += support::cpp11::to_string(output->info()->dimension(0));
186 _config_id += "_";
187 _config_id += support::cpp11::to_string(output->info()->dimension(1));
188 _config_id += "_";
189 _config_id += support::cpp11::to_string(border_undefined);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190}
191
192template <unsigned int matrix_size>
193BorderSize CLSeparableConvolutionVertKernel<matrix_size>::border_size() const
194{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100195 return BorderSize{ matrix_size / 2, 0 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196}
197
198template <unsigned int matrix_size>
199void CLSeparableConvolutionVertKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output,
200 const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type)
201{
202 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16, DataType::S16, DataType::S32);
203 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16);
204 ARM_COMPUTE_ERROR_ON((matrix_size != 5) && (matrix_size != 7) && (matrix_size != 9));
205 ARM_COMPUTE_ERROR_ON(scale == 0);
206
207 _input = input;
208 _output = output;
209
210 std::set<std::string> build_opts;
211
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100212 std::array<int16_t, matrix_size *matrix_size> mat = { 0 };
213 memcpy(mat.data() + matrix_size, conv, matrix_size * sizeof(int16_t));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214
215 for(unsigned int j = 0; j < matrix_size * matrix_size; j++)
216 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100217 build_opts.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218 }
219
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100220 build_opts.insert("-DSCALE=" + support::cpp11::to_string(scale));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221
222 build_opts.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
223
224 build_opts.insert("-DCOMPUTE_TYPE=" + get_cl_type_from_data_type(data_type));
225
226 std::stringstream out_type;
227 out_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
228 build_opts.insert(out_type.str());
229
230 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100231 const std::string kernel_name = "convolution_separable" + support::cpp11::to_string(matrix_size) + "x1_static";
232 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233
234 // Configure kernel window
235 constexpr unsigned int num_elems_processed_per_iteration = 8;
236 constexpr unsigned int num_elems_written_per_iteration = 8;
237 constexpr unsigned int num_elems_read_per_iteration = 8;
238 constexpr unsigned int num_rows_read_per_iteration = matrix_size;
239
240 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
241
242 AccessWindowRectangle input_access(input->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
243 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
244
245 update_window_and_padding(win, input_access, output_access);
246
247 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
248
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100249 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100250
251 // Set config_id for enabling LWS tuning
252 _config_id = kernel_name;
253 _config_id += "_";
254 _config_id += lower_string(string_from_data_type(data_type));
255 _config_id += "_";
256 _config_id += support::cpp11::to_string(input->info()->dimension(0));
257 _config_id += "_";
258 _config_id += support::cpp11::to_string(input->info()->dimension(1));
259 _config_id += "_";
260 _config_id += support::cpp11::to_string(output->info()->dimension(0));
261 _config_id += "_";
262 _config_id += support::cpp11::to_string(output->info()->dimension(1));
263 _config_id += "_";
264 _config_id += support::cpp11::to_string(border_undefined);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100265}
266
267/****************************************************************************************\
268 * Rectangle Convolution *
269\****************************************************************************************/
270
271CLConvolutionRectangleKernel::CLConvolutionRectangleKernel()
272 : _border_size(0), _input(nullptr), _output(nullptr)
273{
274}
275
276BorderSize CLConvolutionRectangleKernel::border_size() const
277{
278 return _border_size;
279}
280
281void CLConvolutionRectangleKernel::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale, bool border_undefined)
282{
283 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
284 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16);
285 ARM_COMPUTE_ERROR_ON(nullptr == conv);
286 ARM_COMPUTE_ERROR_ON(3 != width && 5 != width && 7 != width && 9 != width);
287 ARM_COMPUTE_ERROR_ON(3 != height && 5 != height && 7 != height && 9 != height);
288 ARM_COMPUTE_ERROR_ON(0 == scale);
289
290 _input = input;
291 _output = output;
292 _border_size = BorderSize(height / 2, width / 2);
293
294 std::set<std::string> options;
295
296 std::stringstream output_type;
297 output_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
298 options.insert(output_type.str());
299
300 uint32_t matrix_size = width * height;
301
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100302 std::array<int16_t, max_matrix_size> mat = { 0 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100304 memcpy(mat.data(), conv, matrix_size * sizeof(int16_t));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100305
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100306 for(unsigned int j = 0; j < max_matrix_size; j++)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100308 options.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309 }
310
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100311 options.insert("-DSCALE=" + support::cpp11::to_string(scale));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100312
313 DataType data_type = data_type_for_convolution_matrix(conv, matrix_size);
314 options.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
315
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100316 options.insert("-DMATRIX_WIDTH=" + support::cpp11::to_string(width));
317 options.insert("-DMATRIX_HEIGHT=" + support::cpp11::to_string(height));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100318
319 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("convolution_rectangle", options));
320
321 // Configure kernel window
322 constexpr unsigned int num_elems_processed_per_iteration = 8;
323 constexpr unsigned int num_elems_read_per_iteration = 16;
324 constexpr unsigned int num_elems_written_per_iteration = 8;
325 const unsigned int num_rows_read_per_iteration = height;
326
327 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
328
329 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
330 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
331
332 update_window_and_padding(win, input_access, output_access);
333
334 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
335
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100336 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100337}
338
339void CLConvolutionRectangleKernel::run(const Window &window, cl::CommandQueue &queue)
340{
341 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
342 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
343
344 Window slice = window.first_slice_window_2D();
345
346 do
347 {
348 unsigned int idx = 0;
349 add_2D_tensor_argument(idx, _input, slice);
350 add_2D_tensor_argument(idx, _output, slice);
351 enqueue(queue, *this, slice);
352 }
353 while(window.slide_window_slice_2D(slice));
354}
355
356template class arm_compute::CLConvolutionKernel<3>;
357template class arm_compute::CLConvolutionKernel<5>;
358template class arm_compute::CLConvolutionKernel<7>;
359template class arm_compute::CLConvolutionKernel<9>;
360template class arm_compute::CLSeparableConvolutionVertKernel<5>;
361template class arm_compute::CLSeparableConvolutionVertKernel<7>;
362template class arm_compute::CLSeparableConvolutionVertKernel<9>;
363template class arm_compute::CLSeparableConvolutionHorKernel<5>;
364template class arm_compute::CLSeparableConvolutionHorKernel<7>;
365template class arm_compute::CLSeparableConvolutionHorKernel<9>;
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100366} // namespace arm_compute