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 | |
Georgios Pinitas | 09b1912 | 2018-06-21 13:07:35 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLValidate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/CL/ICLTensor.h" |
| 31 | #include "arm_compute/core/CL/OpenCL.h" |
| 32 | #include "arm_compute/core/Error.h" |
| 33 | #include "arm_compute/core/Helpers.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> |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 42 | #include <utility> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | |
| 44 | using namespace arm_compute; |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 45 | using namespace arm_compute::misc::shape_calculator; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 47 | namespace |
| 48 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 49 | struct Im2ColConfiguration |
| 50 | { |
| 51 | std::string kernel_name{}; |
| 52 | std::set<std::string> build_options{}; |
| 53 | unsigned int num_elems_processed_per_iteration{}; |
| 54 | bool is_padding_required_nchw{}; |
| 55 | }; |
| 56 | |
| 57 | Status validate_arguments(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] | 58 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::QASYMM8 && has_bias); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 62 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 63 | ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1)); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 65 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 66 | if(output->total_size() > 0) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 67 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 68 | const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation, true)); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return Status{}; |
| 74 | } |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 75 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 76 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation, |
| 77 | unsigned int num_elems_processed_per_iteration, bool is_padding_required_nchw) |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 78 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 79 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 80 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 81 | // Output tensor auto initialization if not yet initialized |
| 82 | TensorShape expected_output_shape = compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation, true); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 83 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 84 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(expected_output_shape)); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 85 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 86 | const DataLayout data_layout = input->data_layout(); |
| 87 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 88 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 89 | const unsigned int input_width = input->dimension(width_idx); |
| 90 | const unsigned int input_height = input->dimension(height_idx); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 91 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 92 | // Configure the execute window based on the selected optimal OpenCL kernel |
| 93 | bool window_changed = false; |
| 94 | Window win; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 96 | if(data_layout == DataLayout::NHWC) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 98 | win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 99 | |
| 100 | const int xin_start = 0; |
| 101 | const int xin_end = input->dimension(0) < num_elems_processed_per_iteration ? ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration) : input->dimension(0); |
| 102 | const int yin_start = 0; |
| 103 | const int yin_end = input->dimension(1); |
| 104 | |
| 105 | const int xout_start = 0; |
| 106 | const int xout_end = input->dimension(0) < num_elems_processed_per_iteration ? ceil_to_multiple(output->dimension(0), num_elems_processed_per_iteration) : output->dimension(0); |
| 107 | const int yout_start = 0; |
| 108 | const int yout_end = output->dimension(1); |
| 109 | |
| 110 | AccessWindowStatic input_access(input, xin_start, yin_start, xin_end, yin_end); |
| 111 | AccessWindowStatic output_access(output, xout_start, yout_start, xout_end, yout_end); |
| 112 | window_changed = window_changed || update_window_and_padding(win, input_access, output_access); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | if(is_padding_required_nchw) |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 117 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 118 | const BorderSize border(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left()); |
| 119 | win = calculate_max_window(*input, |
| 120 | Steps(num_elems_processed_per_iteration * conv_info.stride().first, conv_info.stride().second)); |
| 121 | AccessWindowStatic input_access(input, |
| 122 | -border.left, |
| 123 | -border.top, |
| 124 | ceil_to_multiple(input_width + border.right, kernel_dims.width * num_elems_processed_per_iteration), |
| 125 | input_height + border.bottom); |
| 126 | window_changed = window_changed || update_window_and_padding(win, input_access); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 127 | } |
| 128 | else |
| 129 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 130 | // For the generic case, CLIm2ColKernel doesn't need padding (we do not read out-of-bounds elements) so |
| 131 | // update_window_and_padding() can be skipped |
| 132 | win = calculate_max_window(*input, Steps()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape())); |
| 137 | // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension |
| 138 | win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start()); |
| 139 | |
| 140 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 141 | return std::make_pair(err, win); |
| 142 | } |
| 143 | |
| 144 | Im2ColConfiguration configure_opencl_kernel(const ITensorInfo *input, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation) |
| 145 | { |
| 146 | const DataLayout data_layout = input->data_layout(); |
| 147 | const DataType data_type = input->data_type(); |
| 148 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 149 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 150 | const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 151 | const unsigned int input_width = input->dimension(width_idx); |
| 152 | const unsigned int input_height = input->dimension(height_idx); |
| 153 | const unsigned int input_channel = input->dimension(channel_idx); |
| 154 | |
| 155 | const std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation); |
| 156 | |
| 157 | // Im2Col configuration |
| 158 | std::string kernel_name = "im2col_generic_"; |
| 159 | CLBuildOptions build_opts; |
| 160 | unsigned int num_elems_processed_per_iteration = 1; |
| 161 | bool is_padding_required_nchw = false; |
| 162 | |
| 163 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 164 | build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->element_size())); |
| 165 | build_opts.add_option("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width)); |
| 166 | build_opts.add_option("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height)); |
| 167 | build_opts.add_option("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(convolved_dims.first)); |
| 168 | build_opts.add_option("-DCONVOLVED_HEIGHT=" + support::cpp11::to_string(convolved_dims.second)); |
| 169 | build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first)); |
| 170 | build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second)); |
| 171 | build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left())); |
| 172 | build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top())); |
| 173 | build_opts.add_option("-DPAD_RIGHT=" + support::cpp11::to_string(conv_info.pad_right())); |
| 174 | build_opts.add_option("-DPAD_BOTTOM=" + support::cpp11::to_string(conv_info.pad_bottom())); |
| 175 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width)); |
| 176 | build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height)); |
| 177 | build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input_channel)); |
| 178 | build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(dilation.x())); |
| 179 | build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(dilation.y())); |
| 180 | build_opts.add_option_if_else(is_data_type_quantized(data_type), "-DPAD_VALUE=" + support::cpp11::to_string(input->quantization_info().offset), "-DPAD_VALUE=0"); |
| 181 | build_opts.add_option_if(has_bias, "-DHAS_BIAS"); |
| 182 | |
| 183 | if(data_layout == DataLayout::NHWC) |
| 184 | { |
| 185 | num_elems_processed_per_iteration = 2; |
| 186 | is_padding_required_nchw = false; |
| 187 | |
| 188 | // Only the 3x3 case is optimized for NHWC |
| 189 | if(kernel_dims == Size2D(3U, 3U)) |
| 190 | { |
| 191 | kernel_name = "im2col3x3_"; |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 192 | } |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 193 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 194 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
| 195 | build_opts.add_option("-DLAST_ACCESSED=" + support::cpp11::to_string(std::max(static_cast<int>(input_channel - num_elems_processed_per_iteration), 0))); |
| 196 | } |
| 197 | else |
| 198 | { |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 199 | if(dilation == Size2D(1U, 1U)) |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 200 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 201 | const bool squared_im2col = kernel_dims.width == kernel_dims.height; |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 202 | if(squared_im2col) |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 203 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 204 | // Check if we can run an optimized im2col for NCHW |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 205 | switch(kernel_dims.width) |
| 206 | { |
| 207 | case 1: |
| 208 | // Optimized im2col1x1 if stride_x = 1 and conv_info.has_padding() = false |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 209 | if(conv_info.stride().first == 1 && !conv_info.has_padding()) |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 210 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 211 | kernel_name = "im2col1x1_stridex1_"; |
| 212 | num_elems_processed_per_iteration = 4; |
| 213 | is_padding_required_nchw = true; |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 214 | } |
| 215 | break; |
| 216 | case 3: |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 217 | kernel_name = "im2col3x3_"; |
| 218 | num_elems_processed_per_iteration = 1; |
| 219 | is_padding_required_nchw = true; |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 220 | break; |
| 221 | case 5: |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 222 | kernel_name = "im2col5x5_"; |
| 223 | num_elems_processed_per_iteration = 1; |
| 224 | is_padding_required_nchw = true; |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 225 | break; |
| 226 | case 11: |
| 227 | // Optimized im2col11x11 if pad_x = pad_y = 0 |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 228 | if(!conv_info.has_padding()) |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 229 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 230 | kernel_name = "im2col11x11_padx0_pady0_"; |
| 231 | num_elems_processed_per_iteration = 1; |
| 232 | is_padding_required_nchw = true; |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 233 | } |
| 234 | break; |
| 235 | default: |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 236 | kernel_name = "im2col_generic_"; |
| 237 | num_elems_processed_per_iteration = 1; |
| 238 | is_padding_required_nchw = false; |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 239 | break; |
| 240 | } |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 241 | } |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 242 | else if(kernel_dims.width > 1 && !conv_info.has_padding()) |
| 243 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 244 | kernel_name = "im2col_generic_padx0_pady0_"; |
| 245 | num_elems_processed_per_iteration = 1; |
| 246 | is_padding_required_nchw = false; |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 247 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 248 | // Optimized im2col is performed using one or more vector operations with the specified vector size |
| 249 | // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4 |
| 250 | // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3. |
| 251 | // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3. |
| 252 | // Using the vector size of 8, however, may be faster. |
| 253 | // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0 |
| 254 | // is used instead.) |
| 255 | const size_t vector_size = std::min(static_cast<size_t>(4), kernel_dims.width); |
| 256 | const size_t width_mod_vector_size = kernel_dims.width % vector_size; |
| 257 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); |
| 258 | build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size)); |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 259 | } |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 260 | } |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 263 | // Append the data layout to the kernel_name |
| 264 | kernel_name += lower_string(string_from_data_layout(data_layout)); |
| 265 | |
| 266 | Im2ColConfiguration im2col_config; |
| 267 | im2col_config.kernel_name = kernel_name; |
| 268 | im2col_config.build_options = build_opts.options(); |
| 269 | im2col_config.num_elems_processed_per_iteration = num_elems_processed_per_iteration; |
| 270 | im2col_config.is_padding_required_nchw = is_padding_required_nchw; |
| 271 | |
| 272 | return im2col_config; |
| 273 | } |
| 274 | } // namespace |
| 275 | |
| 276 | CLIm2ColKernel::CLIm2ColKernel() |
| 277 | : _input(nullptr), _output(nullptr), _convolved_dims(), _num_elems_processed_per_iteration(1), _kernel_dims(), _conv_info() |
| 278 | { |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation) |
| 282 | { |
| 283 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 284 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), kernel_dims, conv_info, has_bias, dilation)); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 285 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 286 | const DataLayout data_layout = input->info()->data_layout(); |
| 287 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 288 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 289 | const unsigned int input_width = input->info()->dimension(width_idx); |
| 290 | const unsigned int input_height = input->info()->dimension(height_idx); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 291 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 292 | // Select and configure the optimal OpenCL kernel to run. |
| 293 | // This function returns the OpenCL kernel's name, the arguments to pass at compile time, the number of elements processed per iteration |
| 294 | // and the padding requirement flag |
| 295 | Im2ColConfiguration im2col_config = configure_opencl_kernel(input->info(), kernel_dims, conv_info, has_bias, dilation); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 296 | |
| 297 | // Create kernel |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 298 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(im2col_config.kernel_name, im2col_config.build_options)); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 299 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 300 | _input = input; |
| 301 | _output = output; |
| 302 | _convolved_dims = scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation); |
| 303 | _num_elems_processed_per_iteration = im2col_config.num_elems_processed_per_iteration; |
| 304 | _kernel_dims = kernel_dims; // Only needed by the Tuner |
| 305 | _conv_info = conv_info; // Only needed by the Tuner |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 306 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 307 | // Configure kernel window |
| 308 | auto win_config = validate_and_configure_window(input->info(), output->info(), kernel_dims, conv_info, has_bias, dilation, im2col_config.num_elems_processed_per_iteration, |
| 309 | im2col_config.is_padding_required_nchw); |
| 310 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame^] | 311 | ICLKernel::configure_internal(win_config.second); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 312 | |
| 313 | // Set config_id for enabling LWS tuning |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 314 | _config_id = im2col_config.kernel_name; |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 315 | _config_id += "_"; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 316 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 317 | _config_id += "_"; |
| 318 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 319 | _config_id += "_"; |
| 320 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
Giorgio Arena | 00b93f5 | 2018-06-28 17:18:50 +0100 | [diff] [blame] | 321 | _config_id += "_"; |
| 322 | _config_id += lower_string(string_from_data_layout(input->info()->data_layout())); |
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 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 327 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, kernel_dims, conv_info, has_bias, dilation)); |
| 328 | Im2ColConfiguration im2col_config = configure_opencl_kernel(input, kernel_dims, conv_info, has_bias, dilation); |
| 329 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), kernel_dims, conv_info, has_bias, dilation, im2col_config.num_elems_processed_per_iteration, |
| 330 | im2col_config.is_padding_required_nchw) |
| 331 | .first); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 332 | return Status{}; |
| 333 | } |
| 334 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 335 | void CLIm2ColKernel::run(const Window &window, cl::CommandQueue &queue) |
| 336 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 337 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 338 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 339 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 340 | const DataLayout data_layout = _input->info()->data_layout(); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 341 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 342 | // Get initial windows |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 343 | // Collapse in order to have (SRC_DEPTH * BATCH_SIZE) on the 3rd dimension |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 344 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 345 | window_collapsed.set_dimension_step(Window::DimZ, 1); |
| 346 | |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 347 | Window window_output; |
| 348 | window_output.use_tensor_dimensions(_output->info()->tensor_shape()); |
| 349 | |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 350 | const Window first_slice_3d = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 351 | |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 352 | Window slice = first_slice_3d; |
| 353 | Window slice_in = first_slice_3d; |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 354 | Window slice_out = window_output.first_slice_window_2D(); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 355 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 356 | if(data_layout == DataLayout::NHWC) |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 357 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 358 | const Window tmp_win = window.collapse_if_possible(ICLKernel::window(), 3); |
| 359 | const int num_batches = tmp_win[3].end(); |
| 360 | |
| 361 | slice.set(1, Window::Dimension(0, static_cast<int>(_output->info()->tensor_shape()[1]), 1)); |
| 362 | slice.set(2, Window::Dimension(0, static_cast<int>(num_batches), 1)); |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | slice.set(0, Window::Dimension(0, static_cast<int>(ceil_to_multiple(_convolved_dims.first, _num_elems_processed_per_iteration)), _num_elems_processed_per_iteration)); |
| 367 | slice.set(1, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1)); |
| 368 | // Note: In case of NCHW the 3rd dimension is already set collapsing the input window |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 369 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 370 | |
| 371 | // Setup input slice |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 372 | // The dimensions of the input are increased within the OpenCL kernel |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 373 | slice_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 374 | slice_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 375 | slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 376 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 377 | // Setup output slice |
| 378 | // The dimensions of the output are increased within the OpenCL kernel |
| 379 | slice_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 380 | slice_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 381 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 382 | do |
| 383 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 384 | unsigned int idx = 0; |
| 385 | add_3D_tensor_argument(idx, _input, slice_in); |
| 386 | add_2D_tensor_argument(idx, _output, slice_out); |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 387 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3])); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 388 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[2])); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame^] | 389 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 390 | } |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 391 | while(window_collapsed.slide_window_slice_3D(slice) && window_output.slide_window_slice_2D(slice_out) && window_collapsed.slide_window_slice_3D(slice_in)); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame^] | 392 | } |