blob: d9c7ede068a1b0d968d58928b6fffac1bb55cac9 [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
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100157 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("convolution_separable1x" + support::cpp11::to_string(matrix_size) + "_static", build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158
159 // Configure kernel window
160 constexpr unsigned int num_elems_processed_per_iteration = 8;
161 constexpr unsigned int num_elems_read_per_iteration = 16;
162 constexpr unsigned int num_elems_written_per_iteration = 8;
163
164 Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
165
166 AccessWindowHorizontal input_access(input->info(), -border_size().left, num_elems_read_per_iteration);
167 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
168
169 update_window_and_padding(win, input_access, output_access);
170
171 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
172
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100173 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174}
175
176template <unsigned int matrix_size>
177BorderSize CLSeparableConvolutionVertKernel<matrix_size>::border_size() const
178{
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100179 return BorderSize{ matrix_size / 2, 0 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180}
181
182template <unsigned int matrix_size>
183void CLSeparableConvolutionVertKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output,
184 const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type)
185{
186 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16, DataType::S16, DataType::S32);
187 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16);
188 ARM_COMPUTE_ERROR_ON((matrix_size != 5) && (matrix_size != 7) && (matrix_size != 9));
189 ARM_COMPUTE_ERROR_ON(scale == 0);
190
191 _input = input;
192 _output = output;
193
194 std::set<std::string> build_opts;
195
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100196 std::array<int16_t, matrix_size *matrix_size> mat = { 0 };
197 memcpy(mat.data() + matrix_size, conv, matrix_size * sizeof(int16_t));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198
199 for(unsigned int j = 0; j < matrix_size * matrix_size; j++)
200 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100201 build_opts.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202 }
203
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100204 build_opts.insert("-DSCALE=" + support::cpp11::to_string(scale));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205
206 build_opts.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
207
208 build_opts.insert("-DCOMPUTE_TYPE=" + get_cl_type_from_data_type(data_type));
209
210 std::stringstream out_type;
211 out_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
212 build_opts.insert(out_type.str());
213
214 // Create kernel
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100215 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("convolution_separable" + support::cpp11::to_string(matrix_size) + "x1_static", build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216
217 // Configure kernel window
218 constexpr unsigned int num_elems_processed_per_iteration = 8;
219 constexpr unsigned int num_elems_written_per_iteration = 8;
220 constexpr unsigned int num_elems_read_per_iteration = 8;
221 constexpr unsigned int num_rows_read_per_iteration = matrix_size;
222
223 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
224
225 AccessWindowRectangle input_access(input->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
226 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
227
228 update_window_and_padding(win, input_access, output_access);
229
230 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
231
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100232 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233}
234
235/****************************************************************************************\
236 * Rectangle Convolution *
237\****************************************************************************************/
238
239CLConvolutionRectangleKernel::CLConvolutionRectangleKernel()
240 : _border_size(0), _input(nullptr), _output(nullptr)
241{
242}
243
244BorderSize CLConvolutionRectangleKernel::border_size() const
245{
246 return _border_size;
247}
248
249void CLConvolutionRectangleKernel::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale, bool border_undefined)
250{
251 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
252 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16);
253 ARM_COMPUTE_ERROR_ON(nullptr == conv);
254 ARM_COMPUTE_ERROR_ON(3 != width && 5 != width && 7 != width && 9 != width);
255 ARM_COMPUTE_ERROR_ON(3 != height && 5 != height && 7 != height && 9 != height);
256 ARM_COMPUTE_ERROR_ON(0 == scale);
257
258 _input = input;
259 _output = output;
260 _border_size = BorderSize(height / 2, width / 2);
261
262 std::set<std::string> options;
263
264 std::stringstream output_type;
265 output_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
266 options.insert(output_type.str());
267
268 uint32_t matrix_size = width * height;
269
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100270 std::array<int16_t, max_matrix_size> mat = { 0 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100271
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100272 memcpy(mat.data(), conv, matrix_size * sizeof(int16_t));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100273
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100274 for(unsigned int j = 0; j < max_matrix_size; j++)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100275 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100276 options.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100277 }
278
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100279 options.insert("-DSCALE=" + support::cpp11::to_string(scale));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100280
281 DataType data_type = data_type_for_convolution_matrix(conv, matrix_size);
282 options.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
283
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100284 options.insert("-DMATRIX_WIDTH=" + support::cpp11::to_string(width));
285 options.insert("-DMATRIX_HEIGHT=" + support::cpp11::to_string(height));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100286
287 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("convolution_rectangle", options));
288
289 // Configure kernel window
290 constexpr unsigned int num_elems_processed_per_iteration = 8;
291 constexpr unsigned int num_elems_read_per_iteration = 16;
292 constexpr unsigned int num_elems_written_per_iteration = 8;
293 const unsigned int num_rows_read_per_iteration = height;
294
295 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
296
297 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
298 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
299
300 update_window_and_padding(win, input_access, output_access);
301
302 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
303
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100304 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100305}
306
307void CLConvolutionRectangleKernel::run(const Window &window, cl::CommandQueue &queue)
308{
309 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
310 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
311
312 Window slice = window.first_slice_window_2D();
313
314 do
315 {
316 unsigned int idx = 0;
317 add_2D_tensor_argument(idx, _input, slice);
318 add_2D_tensor_argument(idx, _output, slice);
319 enqueue(queue, *this, slice);
320 }
321 while(window.slide_window_slice_2D(slice));
322}
323
324template class arm_compute::CLConvolutionKernel<3>;
325template class arm_compute::CLConvolutionKernel<5>;
326template class arm_compute::CLConvolutionKernel<7>;
327template class arm_compute::CLConvolutionKernel<9>;
328template class arm_compute::CLSeparableConvolutionVertKernel<5>;
329template class arm_compute::CLSeparableConvolutionVertKernel<7>;
330template class arm_compute::CLSeparableConvolutionVertKernel<9>;
331template class arm_compute::CLSeparableConvolutionHorKernel<5>;
332template class arm_compute::CLSeparableConvolutionHorKernel<7>;
333template class arm_compute::CLSeparableConvolutionHorKernel<9>;
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100334} // namespace arm_compute