Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | e9146ed | 2018-02-07 16:05:47 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 "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 | |
| 42 | using namespace arm_compute; |
| 43 | |
| 44 | #define MAX_MATRIX_SIZE 81 |
| 45 | |
| 46 | /****************************************************************************************\ |
| 47 | * Square Convolution * |
| 48 | \****************************************************************************************/ |
| 49 | |
| 50 | template <unsigned int matrix_size> |
| 51 | BorderSize CLConvolutionKernel<matrix_size>::border_size() const |
| 52 | { |
| 53 | return BorderSize(matrix_size / 2); |
| 54 | } |
| 55 | |
| 56 | template <unsigned int matrix_size> |
| 57 | void CLConvolutionKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined) |
| 58 | { |
| 59 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 60 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16); |
| 61 | ARM_COMPUTE_ERROR_ON(conv == nullptr); |
| 62 | |
| 63 | _input = input; |
| 64 | _output = output; |
| 65 | |
Georgios Pinitas | e9146ed | 2018-02-07 16:05:47 +0000 | [diff] [blame] | 66 | std::stringstream kernel_name; |
| 67 | CLBuildOptions build_opts; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | kernel_name << "convolution" << matrix_size << "x" << matrix_size << "_static"; |
| 69 | |
| 70 | if(scale == 0) |
| 71 | { |
| 72 | scale = calculate_matrix_scale(conv, matrix_size); |
| 73 | } |
| 74 | |
| 75 | for(unsigned int i = 0; i < matrix_size * matrix_size; i++) |
| 76 | { |
| 77 | std::stringstream mat_str; |
| 78 | mat_str << "-DMAT" << i << "=" << conv[i]; |
Georgios Pinitas | e9146ed | 2018-02-07 16:05:47 +0000 | [diff] [blame] | 79 | build_opts.add_option(mat_str.str()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Georgios Pinitas | e9146ed | 2018-02-07 16:05:47 +0000 | [diff] [blame] | 82 | build_opts.add_option("-DSCALE=" + support::cpp11::to_string(scale)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | |
| 84 | DataType data_type = data_type_for_convolution_matrix(conv, matrix_size * matrix_size); |
Georgios Pinitas | e9146ed | 2018-02-07 16:05:47 +0000 | [diff] [blame] | 85 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | |
| 87 | std::stringstream out_type; |
| 88 | out_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type()); |
Georgios Pinitas | e9146ed | 2018-02-07 16:05:47 +0000 | [diff] [blame] | 89 | build_opts.add_option(out_type.str()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | |
Georgios Pinitas | e9146ed | 2018-02-07 16:05:47 +0000 | [diff] [blame] | 91 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name.str(), build_opts.options())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | |
| 93 | // Configure kernel window |
| 94 | constexpr unsigned int num_elems_processed_per_iteration = 8; |
| 95 | constexpr unsigned int num_elems_written_per_iteration = 8; |
| 96 | constexpr unsigned int num_elems_read_per_iteration = 16; |
| 97 | constexpr unsigned int num_rows_read_per_iteration = matrix_size; |
| 98 | |
| 99 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size()); |
| 100 | |
| 101 | AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration); |
| 102 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); |
| 103 | |
| 104 | update_window_and_padding(win, input_access, output_access); |
| 105 | |
| 106 | output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size()); |
| 107 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 108 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /****************************************************************************************\ |
| 112 | * Separable Convolution * |
| 113 | \****************************************************************************************/ |
| 114 | template <unsigned int matrix_size> |
| 115 | CLSeparableConvolutionHorKernel<matrix_size>::CLSeparableConvolutionHorKernel() |
| 116 | : _border_size(0) |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | template <unsigned int matrix_size> |
| 121 | BorderSize CLSeparableConvolutionHorKernel<matrix_size>::border_size() const |
| 122 | { |
| 123 | return _border_size; |
| 124 | } |
| 125 | |
| 126 | template <unsigned int matrix_size> |
| 127 | void CLSeparableConvolutionHorKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined) |
| 128 | { |
| 129 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 130 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U16, DataType::S16, DataType::S32); |
| 131 | |
| 132 | ARM_COMPUTE_ERROR_ON((matrix_size != 5) && (matrix_size != 7) && (matrix_size != 9)); |
| 133 | |
| 134 | _input = input; |
| 135 | _output = output; |
| 136 | _border_size = BorderSize(border_undefined ? 0 : matrix_size / 2, matrix_size / 2); |
| 137 | |
| 138 | // Set build options |
| 139 | std::set<std::string> build_opts; |
| 140 | |
| 141 | int16_t mat[matrix_size * matrix_size] = { 0 }; |
| 142 | memcpy(mat, conv, matrix_size * sizeof(int16_t)); |
| 143 | |
| 144 | for(unsigned int j = 0; j < matrix_size * matrix_size; j++) |
| 145 | { |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 146 | build_opts.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j])); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | build_opts.insert("-DSCALE=0"); |
| 150 | |
| 151 | build_opts.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(output->info()->data_type())); |
| 152 | |
| 153 | // Create kernel |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 154 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("convolution_separable1x" + support::cpp11::to_string(matrix_size) + "_static", build_opts)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | |
| 156 | // Configure kernel window |
| 157 | constexpr unsigned int num_elems_processed_per_iteration = 8; |
| 158 | constexpr unsigned int num_elems_read_per_iteration = 16; |
| 159 | constexpr unsigned int num_elems_written_per_iteration = 8; |
| 160 | |
| 161 | Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size()); |
| 162 | |
| 163 | AccessWindowHorizontal input_access(input->info(), -border_size().left, num_elems_read_per_iteration); |
| 164 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); |
| 165 | |
| 166 | update_window_and_padding(win, input_access, output_access); |
| 167 | |
| 168 | output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size()); |
| 169 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 170 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | template <unsigned int matrix_size> |
| 174 | BorderSize CLSeparableConvolutionVertKernel<matrix_size>::border_size() const |
| 175 | { |
| 176 | return BorderSize(matrix_size / 2, 0); |
| 177 | } |
| 178 | |
| 179 | template <unsigned int matrix_size> |
| 180 | void CLSeparableConvolutionVertKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, |
| 181 | const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type) |
| 182 | { |
| 183 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16, DataType::S16, DataType::S32); |
| 184 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16); |
| 185 | ARM_COMPUTE_ERROR_ON((matrix_size != 5) && (matrix_size != 7) && (matrix_size != 9)); |
| 186 | ARM_COMPUTE_ERROR_ON(scale == 0); |
| 187 | |
| 188 | _input = input; |
| 189 | _output = output; |
| 190 | |
| 191 | std::set<std::string> build_opts; |
| 192 | |
| 193 | int16_t mat[matrix_size * matrix_size] = { 0 }; |
| 194 | memcpy(mat + matrix_size, conv, matrix_size * sizeof(int16_t)); |
| 195 | |
| 196 | for(unsigned int j = 0; j < matrix_size * matrix_size; j++) |
| 197 | { |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 198 | build_opts.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j])); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 201 | build_opts.insert("-DSCALE=" + support::cpp11::to_string(scale)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 202 | |
| 203 | build_opts.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 204 | |
| 205 | build_opts.insert("-DCOMPUTE_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 206 | |
| 207 | std::stringstream out_type; |
| 208 | out_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type()); |
| 209 | build_opts.insert(out_type.str()); |
| 210 | |
| 211 | // Create kernel |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 212 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("convolution_separable" + support::cpp11::to_string(matrix_size) + "x1_static", build_opts)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 213 | |
| 214 | // Configure kernel window |
| 215 | constexpr unsigned int num_elems_processed_per_iteration = 8; |
| 216 | constexpr unsigned int num_elems_written_per_iteration = 8; |
| 217 | constexpr unsigned int num_elems_read_per_iteration = 8; |
| 218 | constexpr unsigned int num_rows_read_per_iteration = matrix_size; |
| 219 | |
| 220 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size()); |
| 221 | |
| 222 | AccessWindowRectangle input_access(input->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration); |
| 223 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); |
| 224 | |
| 225 | update_window_and_padding(win, input_access, output_access); |
| 226 | |
| 227 | output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size()); |
| 228 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 229 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /****************************************************************************************\ |
| 233 | * Rectangle Convolution * |
| 234 | \****************************************************************************************/ |
| 235 | |
| 236 | CLConvolutionRectangleKernel::CLConvolutionRectangleKernel() |
| 237 | : _border_size(0), _input(nullptr), _output(nullptr) |
| 238 | { |
| 239 | } |
| 240 | |
| 241 | BorderSize CLConvolutionRectangleKernel::border_size() const |
| 242 | { |
| 243 | return _border_size; |
| 244 | } |
| 245 | |
| 246 | void CLConvolutionRectangleKernel::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale, bool border_undefined) |
| 247 | { |
| 248 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 249 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16); |
| 250 | ARM_COMPUTE_ERROR_ON(nullptr == conv); |
| 251 | ARM_COMPUTE_ERROR_ON(3 != width && 5 != width && 7 != width && 9 != width); |
| 252 | ARM_COMPUTE_ERROR_ON(3 != height && 5 != height && 7 != height && 9 != height); |
| 253 | ARM_COMPUTE_ERROR_ON(0 == scale); |
| 254 | |
| 255 | _input = input; |
| 256 | _output = output; |
| 257 | _border_size = BorderSize(height / 2, width / 2); |
| 258 | |
| 259 | std::set<std::string> options; |
| 260 | |
| 261 | std::stringstream output_type; |
| 262 | output_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type()); |
| 263 | options.insert(output_type.str()); |
| 264 | |
| 265 | uint32_t matrix_size = width * height; |
| 266 | |
| 267 | int16_t mat[MAX_MATRIX_SIZE] = { 0 }; |
| 268 | |
| 269 | memcpy(mat, conv, matrix_size * sizeof(int16_t)); |
| 270 | |
| 271 | for(unsigned int j = 0; j < MAX_MATRIX_SIZE; j++) |
| 272 | { |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 273 | options.insert("-DMAT" + support::cpp11::to_string(j) + "=" + support::cpp11::to_string(mat[j])); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 276 | options.insert("-DSCALE=" + support::cpp11::to_string(scale)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 277 | |
| 278 | DataType data_type = data_type_for_convolution_matrix(conv, matrix_size); |
| 279 | options.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 280 | |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 281 | options.insert("-DMATRIX_WIDTH=" + support::cpp11::to_string(width)); |
| 282 | options.insert("-DMATRIX_HEIGHT=" + support::cpp11::to_string(height)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 283 | |
| 284 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("convolution_rectangle", options)); |
| 285 | |
| 286 | // Configure kernel window |
| 287 | constexpr unsigned int num_elems_processed_per_iteration = 8; |
| 288 | constexpr unsigned int num_elems_read_per_iteration = 16; |
| 289 | constexpr unsigned int num_elems_written_per_iteration = 8; |
| 290 | const unsigned int num_rows_read_per_iteration = height; |
| 291 | |
| 292 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size()); |
| 293 | |
| 294 | AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration); |
| 295 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); |
| 296 | |
| 297 | update_window_and_padding(win, input_access, output_access); |
| 298 | |
| 299 | output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size()); |
| 300 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 301 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void CLConvolutionRectangleKernel::run(const Window &window, cl::CommandQueue &queue) |
| 305 | { |
| 306 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 307 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 308 | |
| 309 | Window slice = window.first_slice_window_2D(); |
| 310 | |
| 311 | do |
| 312 | { |
| 313 | unsigned int idx = 0; |
| 314 | add_2D_tensor_argument(idx, _input, slice); |
| 315 | add_2D_tensor_argument(idx, _output, slice); |
| 316 | enqueue(queue, *this, slice); |
| 317 | } |
| 318 | while(window.slide_window_slice_2D(slice)); |
| 319 | } |
| 320 | |
| 321 | template class arm_compute::CLConvolutionKernel<3>; |
| 322 | template class arm_compute::CLConvolutionKernel<5>; |
| 323 | template class arm_compute::CLConvolutionKernel<7>; |
| 324 | template class arm_compute::CLConvolutionKernel<9>; |
| 325 | template class arm_compute::CLSeparableConvolutionVertKernel<5>; |
| 326 | template class arm_compute::CLSeparableConvolutionVertKernel<7>; |
| 327 | template class arm_compute::CLSeparableConvolutionVertKernel<9>; |
| 328 | template class arm_compute::CLSeparableConvolutionHorKernel<5>; |
| 329 | template class arm_compute::CLSeparableConvolutionHorKernel<7>; |
| 330 | template class arm_compute::CLSeparableConvolutionHorKernel<9>; |