Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-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/CLIm2ColKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLValidate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/Size2D.h" |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 34 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | #include "arm_compute/core/Types.h" |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 36 | #include "arm_compute/core/Validate.h" |
| 37 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 38 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 40 | #include <cmath> |
| 41 | #include <tuple> |
| 42 | |
| 43 | using namespace arm_compute; |
| 44 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 45 | namespace |
| 46 | { |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 47 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, bool has_bias, const Size2D &dilation) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 48 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::QASYMM8 && has_bias); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1)); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 54 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 55 | |
| 56 | // Checks performed when output is configured |
| 57 | if(output->total_size() != 0) |
| 58 | { |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 61 | } |
| 62 | |
| 63 | return Status{}; |
| 64 | } |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 65 | |
| 66 | inline bool run_im2col_reduced(ITensorInfo *input, ITensorInfo *output, const PadStrideInfo &conv_info) |
| 67 | { |
| 68 | int stride_x = 0; |
| 69 | int stride_y = 0; |
| 70 | |
| 71 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 72 | |
| 73 | return (output->dimension(0) == (input->dimension(0) * input->dimension(1) * input->dimension(2))) && (TensorShape::num_max_dimensions >= 4) |
| 74 | && (std::equal(input->tensor_shape().cbegin() + 3, |
| 75 | input->tensor_shape().cend(), |
| 76 | output->tensor_shape().cbegin() + 1)) |
| 77 | && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding()); |
| 78 | } |
| 79 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 80 | } // namespace |
| 81 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | CLIm2ColKernel::CLIm2ColKernel() |
Georgios Pinitas | 17812ba | 2018-06-04 19:27:13 +0100 | [diff] [blame] | 83 | : _input(nullptr), _output(nullptr), _conv_info(), _convolved_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr), _kernel_dims() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | { |
| 85 | } |
| 86 | |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 87 | std::string |
| 88 | CLIm2ColKernel::configure_window(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, |
| 89 | const Size2D &dilation, const PadStrideInfo &conv_info, CLBuildOptions &build_opts) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | { |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 91 | std::string kernel_name; |
| 92 | bool is_optimized_path = false; |
| 93 | const bool reduced = run_im2col_reduced(input->info(), output->info(), conv_info); |
| 94 | const DataType data_type = input->info()->data_type(); |
| 95 | const bool squared_im2col = kernel_dims.width == kernel_dims.height; |
| 96 | const DataLayout data_layout = input->info()->data_layout(); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 97 | |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 98 | if(!reduced) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | { |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 100 | // Default kernel name |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 101 | if(data_layout == DataLayout::NCHW) |
| 102 | { |
| 103 | kernel_name = "im2col_generic_dchw"; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | kernel_name = "im2col_generic_nhwc"; |
| 108 | } |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 109 | |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 110 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 111 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 112 | const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 113 | const unsigned int input_width = input->info()->dimension(width_idx); |
| 114 | const unsigned int input_height = input->info()->dimension(height_idx); |
| 115 | const unsigned int input_channel = input->info()->dimension(channel_idx); |
| 116 | |
| 117 | _convolved_dims = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 119 | build_opts.add_option("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width)); |
| 120 | build_opts.add_option("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height)); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 121 | build_opts.add_option("-DKERNEL_DEPTH=" + support::cpp11::to_string(input_channel)); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 122 | build_opts.add_option("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(_convolved_dims.first)); |
| 123 | build_opts.add_option("-DCONVOLVED_HEIGHT=" + support::cpp11::to_string(_convolved_dims.second)); |
| 124 | build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first)); |
| 125 | build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second)); |
| 126 | build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left())); |
| 127 | build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top())); |
| 128 | build_opts.add_option("-DPAD_RIGHT=" + support::cpp11::to_string(conv_info.pad_right())); |
| 129 | build_opts.add_option("-DPAD_BOTTOM=" + support::cpp11::to_string(conv_info.pad_bottom())); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 130 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width)); |
| 131 | build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height)); |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 132 | build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(dilation.x())); |
| 133 | build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(dilation.y())); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 134 | build_opts.add_option_if_else(is_data_type_quantized(data_type), "-DPAD_VALUE=" + support::cpp11::to_string(input->info()->quantization_info().offset), "-DPAD_VALUE=0"); |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 135 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 136 | if(dilation == Size2D(1U, 1U)) |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 137 | { |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 138 | if(squared_im2col && !is_data_type_fixed_point(data_type)) |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 139 | { |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 140 | // Check if we can run an optimized im2col |
| 141 | switch(kernel_dims.width) |
| 142 | { |
| 143 | case 1: |
| 144 | // Optimized im2col1x1 if stride_x = 1 and conv_info.has_padding() = false |
| 145 | if(conv_info.stride().first == 1 && !conv_info.has_padding()) |
| 146 | { |
| 147 | // Set hint for LWS |
| 148 | _lws_hint = cl::NDRange(1, 1, 8); |
| 149 | _num_elems_processed_per_iteration = 4; |
| 150 | is_optimized_path = true; |
| 151 | kernel_name = "im2col1x1_stridex1_dchw"; |
| 152 | } |
| 153 | break; |
| 154 | case 3: |
Gian Marco | 562deff | 2018-02-07 10:18:06 +0000 | [diff] [blame] | 155 | _lws_hint = cl::NDRange(1, 1, 8); |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 156 | _num_elems_processed_per_iteration = 1; |
| 157 | is_optimized_path = true; |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 158 | switch(data_layout) |
| 159 | { |
| 160 | case DataLayout::NCHW: |
| 161 | kernel_name = "im2col3x3_dchw"; |
| 162 | break; |
| 163 | case DataLayout::NHWC: |
| 164 | kernel_name = "im2col3x3_nhwc"; |
| 165 | break; |
| 166 | default: |
| 167 | ARM_COMPUTE_ERROR("Not supported."); |
| 168 | break; |
| 169 | } |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 170 | break; |
| 171 | case 5: |
| 172 | _num_elems_processed_per_iteration = 1; |
| 173 | is_optimized_path = true; |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 174 | switch(data_layout) |
| 175 | { |
| 176 | case DataLayout::NCHW: |
| 177 | kernel_name = "im2col5x5_dchw"; |
| 178 | break; |
| 179 | default: |
| 180 | // using generic_nhwc |
| 181 | break; |
| 182 | } |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 183 | break; |
| 184 | case 11: |
| 185 | // Optimized im2col11x11 if pad_x = pad_y = 0 |
| 186 | if(!conv_info.has_padding()) |
| 187 | { |
| 188 | _num_elems_processed_per_iteration = 1; |
| 189 | is_optimized_path = true; |
| 190 | kernel_name = "im2col11x11_padx0_pady0_dchw"; |
| 191 | } |
| 192 | break; |
| 193 | default: |
| 194 | is_optimized_path = false; |
| 195 | break; |
| 196 | } |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 197 | } |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 198 | else if(kernel_dims.width > 1 && !conv_info.has_padding()) |
| 199 | { |
| 200 | _num_elems_processed_per_iteration = 1; |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 201 | is_optimized_path = false; |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 202 | |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 203 | if(data_layout == DataLayout::NCHW) |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 204 | { |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 205 | kernel_name = "im2col_generic_padx0_pady0_dchw"; |
| 206 | |
| 207 | // Optimized im2col is performed using one or more vector operations with the specified vector size |
| 208 | // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4 |
| 209 | // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3. |
| 210 | // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3. |
| 211 | // Using the vector size of 8, however, may be faster. |
| 212 | size_t vector_size = 4; |
| 213 | // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0 |
| 214 | // is used instead.) |
| 215 | if(kernel_dims.width < vector_size) |
| 216 | { |
| 217 | vector_size = kernel_dims.width; |
| 218 | } |
| 219 | // Vector size optimized for the 11x11 AlexNet convolution on Bifrost. |
| 220 | const GPUTarget gpu_target = get_target(); |
| 221 | if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX) && kernel_dims.width == 11) |
| 222 | { |
| 223 | vector_size = 8; |
| 224 | } |
| 225 | const size_t width_mod_vector_size = kernel_dims.width % vector_size; |
| 226 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); |
| 227 | build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size)); |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 228 | } |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 229 | } |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 230 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 231 | _run_func = &CLIm2ColKernel::run_generic; |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | _num_elems_processed_per_iteration = 1; |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 236 | kernel_name = "im2col_reduced_dchw"; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 237 | _run_func = &CLIm2ColKernel::run_reduced; |
| 238 | } |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 239 | // Configure kernel window |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 240 | Window win; |
| 241 | if(is_optimized_path) |
| 242 | { |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 243 | if(data_layout == DataLayout::NHWC) |
| 244 | { |
| 245 | win = calculate_max_window(*input->info(), |
| 246 | Steps(_num_elems_processed_per_iteration), |
| 247 | false, |
| 248 | BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left())); |
| 249 | const int x = -conv_info.pad_left(); |
| 250 | const int y = -conv_info.pad_top(); |
| 251 | const int h = kernel_dims.width * _num_elems_processed_per_iteration; |
| 252 | const int w = 1; |
| 253 | AccessWindowRectangle input_access(input->info(), x, y, w, h); |
| 254 | update_window_and_padding(win, input_access); |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | win = calculate_max_window(*input->info(), |
| 259 | Steps(_num_elems_processed_per_iteration), |
| 260 | false, |
| 261 | BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left())); |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 262 | |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 263 | const int x = -conv_info.pad_left(); |
| 264 | const int y = -conv_info.pad_top(); |
| 265 | const int w = kernel_dims.width * _num_elems_processed_per_iteration; |
| 266 | const int h = kernel_dims.height; |
| 267 | AccessWindowRectangle input_access(input->info(), x, y, w, h); |
| 268 | update_window_and_padding(win, input_access); |
| 269 | } |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 270 | } |
| 271 | else |
| 272 | { |
| 273 | // For the generic case, CLIm2ColKernel doesn't need padding (we do not read out-of-bounds elements) so |
| 274 | // update_window_and_padding() can be skipped |
| 275 | win = calculate_max_window(*input->info(), Steps()); |
| 276 | } |
| 277 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 278 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 279 | if(!reduced) |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 280 | { |
| 281 | // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension |
| 282 | win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start()); |
| 283 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 284 | ICLKernel::configure(win); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 285 | return kernel_name; |
| 286 | } |
| 287 | |
| 288 | void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation) |
| 289 | { |
| 290 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 291 | ARM_COMPUTE_ERROR_ON(input->info()->data_layout() == DataLayout::UNKNOWN); |
| 292 | ARM_COMPUTE_ERROR_ON_MSG(output->info()->data_layout() != DataLayout::NCHW, "Special case Im2Col output layout is NCHW"); |
| 293 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), has_bias, dilation)); |
| 294 | |
| 295 | _input = input; |
| 296 | _output = output; |
| 297 | _kernel_dims = kernel_dims; |
| 298 | _conv_info = conv_info; |
| 299 | |
| 300 | const DataType data_type = input->info()->data_type(); |
| 301 | |
| 302 | // Create kernel |
| 303 | CLBuildOptions build_opts; |
| 304 | build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type))); |
| 305 | build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->info()->element_size())); |
| 306 | build_opts.add_option_if(has_bias, "-DHAS_BIAS"); |
| 307 | build_opts.add_option_if(is_data_type_fixed_point(data_type), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 308 | |
| 309 | _num_elems_processed_per_iteration = 1; |
| 310 | |
| 311 | const std::string kernel_name = configure_window(input, output, kernel_dims, dilation, conv_info, build_opts); |
| 312 | // Create kernel |
| 313 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 314 | |
| 315 | // Set config_id for enabling LWS tuning |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 316 | _config_id = kernel_name; |
| 317 | _config_id += "_"; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 318 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 319 | _config_id += "_"; |
| 320 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 321 | _config_id += "_"; |
| 322 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 325 | Status CLIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 326 | { |
| 327 | ARM_COMPUTE_UNUSED(kernel_dims); |
| 328 | ARM_COMPUTE_UNUSED(conv_info); |
| 329 | ARM_COMPUTE_UNUSED(has_bias); |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 330 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, has_bias, dilation)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 331 | return Status{}; |
| 332 | } |
| 333 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 334 | void CLIm2ColKernel::run(const Window &window, cl::CommandQueue &queue) |
| 335 | { |
| 336 | ARM_COMPUTE_ERROR_ON(_run_func == nullptr); |
| 337 | (this->*_run_func)(window, queue); |
| 338 | } |
| 339 | |
| 340 | void CLIm2ColKernel::run_generic(const Window &window, cl::CommandQueue &queue) |
| 341 | { |
| 342 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 343 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 344 | |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 345 | const DataLayout data_layout = _input->info()->data_layout(); |
| 346 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 347 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 348 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 349 | // Get initial windows |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 350 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 351 | // Change the Z dimension's step back to 1 |
| 352 | window_collapsed.set_dimension_step(Window::DimZ, 1); |
| 353 | |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 354 | const Window first_slice_3d = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 355 | |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 356 | Window slice = first_slice_3d; |
| 357 | Window slice_in = first_slice_3d; |
| 358 | Window slice_out = first_slice_3d; |
| 359 | |
| 360 | const bool out_dim_not_same_input_dim = _convolved_dims.first != _input->info()->dimension(width_idx) || _convolved_dims.second != _input->info()->dimension(height_idx); |
| 361 | |
| 362 | // Setup slice if convolved dims are not the same as input dims |
| 363 | if(out_dim_not_same_input_dim) |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 364 | { |
| 365 | // If the stride_x or stride_y are not 1, the output tensor of matrix multiply (Convolved tensor) will not |
| 366 | // have the same shape of the im2col input tensor |
| 367 | // In this case we need to re-compute the window using the shape of the tensor after matrix multiply (convolved_dims) |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 368 | slice.set(width_idx, Window::Dimension(0, static_cast<int>(_convolved_dims.first), 1)); |
| 369 | if(data_layout == DataLayout::NHWC) |
| 370 | { |
| 371 | // if layout is NHWC, we need to multiply convolved_dims.height by the number of batches as for this |
| 372 | // format we collapsed HEIGHT and all subsequent dimensions (batches) together. This is necessary to ensure |
| 373 | // global_id(2) values are in the correct range. |
| 374 | const Window tmp_win = window.collapse_if_possible(ICLKernel::window(), 3); |
| 375 | const int num_batches = tmp_win[3].end(); |
| 376 | slice.set(height_idx, Window::Dimension(0, static_cast<int>(_convolved_dims.second) * num_batches, 1)); |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | slice.set(height_idx, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1)); |
| 381 | } |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 382 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 383 | |
| 384 | // Setup input slice |
| 385 | // The first three dimensions of the input are increased by the inner loops |
| 386 | slice_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 387 | slice_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 388 | slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 389 | |
| 390 | // Setup output slice |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 391 | slice_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _kernel_dims.area())); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame^] | 392 | slice_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _output->info()->dimension(1))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 393 | slice_out.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
| 394 | |
| 395 | do |
| 396 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 397 | unsigned int idx = 0; |
| 398 | add_3D_tensor_argument(idx, _input, slice_in); |
| 399 | add_2D_tensor_argument(idx, _output, slice_out); |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 400 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3])); |
| 401 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[3])); |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 402 | enqueue(queue, *this, slice, _lws_hint); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 403 | } |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 404 | while(window_collapsed.slide_window_slice_3D(slice) && window_collapsed.slide_window_slice_3D(slice_out) && window_collapsed.slide_window_slice_3D(slice_in)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void CLIm2ColKernel::run_reduced(const Window &window, cl::CommandQueue &queue) |
| 408 | { |
| 409 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 410 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 411 | |
| 412 | Window out_window; |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 413 | out_window.use_tensor_dimensions(_output->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 414 | |
| 415 | Window out_slice = out_window.first_slice_window_1D(); |
| 416 | Window in_slice = window.first_slice_window_3D(); |
| 417 | |
| 418 | // Run kernel |
| 419 | do |
| 420 | { |
| 421 | // Set arguments |
| 422 | unsigned int idx = 0; |
| 423 | add_3D_tensor_argument(idx, _input, in_slice); |
| 424 | add_1D_tensor_argument(idx, _output, out_slice); |
| 425 | |
| 426 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(0)); |
| 427 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(1)); |
Gian Marco | d7779da | 2017-11-22 14:46:28 +0000 | [diff] [blame] | 428 | enqueue(queue, *this, in_slice, _lws_hint); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 429 | } |
| 430 | while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice)); |
| 431 | } |