Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLConvolutionKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
| 30 | #include "arm_compute/core/Error.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/Utils.h" |
| 34 | #include "arm_compute/core/Validate.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 35 | #include "src/core/CL/ICLKernel.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 36 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 37 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
| 39 | #include <set> |
| 40 | #include <sstream> |
| 41 | #include <string> |
| 42 | |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 43 | namespace arm_compute |
| 44 | { |
| 45 | namespace |
| 46 | { |
| 47 | constexpr unsigned int max_matrix_size = 81; |
| 48 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | |
| 50 | /****************************************************************************************\ |
| 51 | * Square Convolution * |
| 52 | \****************************************************************************************/ |
| 53 | |
| 54 | template <unsigned int matrix_size> |
| 55 | BorderSize CLConvolutionKernel<matrix_size>::border_size() const |
| 56 | { |
| 57 | return BorderSize(matrix_size / 2); |
| 58 | } |
| 59 | |
| 60 | template <unsigned int matrix_size> |
| 61 | void CLConvolutionKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined) |
| 62 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 63 | configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, scale, border_undefined); |
| 64 | } |
| 65 | |
| 66 | template <unsigned int matrix_size> |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 67 | void CLConvolutionKernel<matrix_size>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 68 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | 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 Pinitas | e9146ed | 2018-02-07 16:05:47 +0000 | [diff] [blame] | 76 | std::stringstream kernel_name; |
| 77 | CLBuildOptions build_opts; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | 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 Pinitas | e9146ed | 2018-02-07 16:05:47 +0000 | [diff] [blame] | 89 | build_opts.add_option(mat_str.str()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Georgios Pinitas | e9146ed | 2018-02-07 16:05:47 +0000 | [diff] [blame] | 92 | build_opts.add_option("-DSCALE=" + support::cpp11::to_string(scale)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | |
| 94 | 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] | 95 | 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] | 96 | |
| 97 | std::stringstream out_type; |
| 98 | 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] | 99 | build_opts.add_option(out_type.str()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 101 | _kernel = create_kernel(compile_context, kernel_name.str(), build_opts.options()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | |
| 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 Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 118 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /****************************************************************************************\ |
| 122 | * Separable Convolution * |
| 123 | \****************************************************************************************/ |
| 124 | template <unsigned int matrix_size> |
| 125 | CLSeparableConvolutionHorKernel<matrix_size>::CLSeparableConvolutionHorKernel() |
| 126 | : _border_size(0) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | template <unsigned int matrix_size> |
| 131 | BorderSize CLSeparableConvolutionHorKernel<matrix_size>::border_size() const |
| 132 | { |
| 133 | return _border_size; |
| 134 | } |
| 135 | |
| 136 | template <unsigned int matrix_size> |
| 137 | void CLSeparableConvolutionHorKernel<matrix_size>::configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined) |
| 138 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 139 | configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, border_undefined); |
| 140 | } |
| 141 | |
| 142 | template <unsigned int matrix_size> |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 143 | void CLSeparableConvolutionHorKernel<matrix_size>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 144 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | 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 Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 157 | std::array<int16_t, matrix_size *matrix_size> mat = { 0 }; |
| 158 | memcpy(mat.data(), conv, matrix_size * sizeof(int16_t)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | |
| 160 | for(unsigned int j = 0; j < matrix_size * matrix_size; j++) |
| 161 | { |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 162 | 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] | 163 | } |
| 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 Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 170 | const std::string kernel_name = "convolution_separable1x" + support::cpp11::to_string(matrix_size) + "_static"; |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 171 | _kernel = create_kernel(compile_context, kernel_name, build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | |
| 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 Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 187 | ICLKernel::configure_internal(win); |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 188 | |
| 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | template <unsigned int matrix_size> |
| 206 | BorderSize CLSeparableConvolutionVertKernel<matrix_size>::border_size() const |
| 207 | { |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 208 | return BorderSize{ matrix_size / 2, 0 }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | template <unsigned int matrix_size> |
| 212 | void 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 Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 215 | configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, scale, border_undefined, data_type); |
| 216 | } |
| 217 | |
| 218 | template <unsigned int matrix_size> |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 219 | void CLSeparableConvolutionVertKernel<matrix_size>::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 220 | const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type) |
| 221 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 222 | 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 Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 232 | std::array<int16_t, matrix_size *matrix_size> mat = { 0 }; |
| 233 | memcpy(mat.data() + matrix_size, conv, matrix_size * sizeof(int16_t)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 234 | |
| 235 | for(unsigned int j = 0; j < matrix_size * matrix_size; j++) |
| 236 | { |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 237 | 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] | 238 | } |
| 239 | |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 240 | build_opts.insert("-DSCALE=" + support::cpp11::to_string(scale)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 241 | |
| 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 Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 251 | const std::string kernel_name = "convolution_separable" + support::cpp11::to_string(matrix_size) + "x1_static"; |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 252 | _kernel = create_kernel(compile_context, kernel_name, build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 253 | |
| 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 Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 269 | ICLKernel::configure_internal(win); |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 270 | |
| 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /****************************************************************************************\ |
| 288 | * Rectangle Convolution * |
| 289 | \****************************************************************************************/ |
| 290 | |
| 291 | CLConvolutionRectangleKernel::CLConvolutionRectangleKernel() |
| 292 | : _border_size(0), _input(nullptr), _output(nullptr) |
| 293 | { |
| 294 | } |
| 295 | |
| 296 | BorderSize CLConvolutionRectangleKernel::border_size() const |
| 297 | { |
| 298 | return _border_size; |
| 299 | } |
| 300 | |
| 301 | void 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 Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 303 | configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, width, height, scale, border_undefined); |
| 304 | } |
| 305 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 306 | void 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 Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 307 | bool border_undefined) |
| 308 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 309 | 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 Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 328 | std::array<int16_t, max_matrix_size> mat = { 0 }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 329 | |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 330 | memcpy(mat.data(), conv, matrix_size * sizeof(int16_t)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 331 | |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 332 | for(unsigned int j = 0; j < max_matrix_size; j++) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 333 | { |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 334 | 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] | 335 | } |
| 336 | |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 337 | options.insert("-DSCALE=" + support::cpp11::to_string(scale)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 338 | |
| 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 Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 342 | options.insert("-DMATRIX_WIDTH=" + support::cpp11::to_string(width)); |
| 343 | options.insert("-DMATRIX_HEIGHT=" + support::cpp11::to_string(height)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 344 | |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 345 | _kernel = create_kernel(compile_context, "convolution_rectangle", options); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 346 | |
| 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 Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 362 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void 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 Giorgio | 6b9f388 | 2019-07-01 16:37:04 +0100 | [diff] [blame] | 377 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 378 | } |
| 379 | while(window.slide_window_slice_2D(slice)); |
| 380 | } |
| 381 | |
| 382 | template class arm_compute::CLConvolutionKernel<3>; |
| 383 | template class arm_compute::CLConvolutionKernel<5>; |
| 384 | template class arm_compute::CLConvolutionKernel<7>; |
| 385 | template class arm_compute::CLConvolutionKernel<9>; |
| 386 | template class arm_compute::CLSeparableConvolutionVertKernel<5>; |
| 387 | template class arm_compute::CLSeparableConvolutionVertKernel<7>; |
| 388 | template class arm_compute::CLSeparableConvolutionVertKernel<9>; |
| 389 | template class arm_compute::CLSeparableConvolutionHorKernel<5>; |
| 390 | template class arm_compute::CLSeparableConvolutionHorKernel<7>; |
| 391 | template class arm_compute::CLSeparableConvolutionHorKernel<9>; |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 392 | } // namespace arm_compute |