Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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" |
| 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" |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/Types.h" |
| 34 | #include "arm_compute/core/Validate.h" |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 35 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
| 37 | #include <cmath> |
| 38 | #include <tuple> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | CLIm2ColKernel::CLIm2ColKernel() |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 43 | : _input(nullptr), _output(nullptr), _convolved_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 47 | void CLIm2ColKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | |
| 53 | _input = input; |
| 54 | _output = output; |
| 55 | |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 56 | const DataType data_type = input->info()->data_type(); |
| 57 | const GPUTarget gpu_target = get_arch_from_target(get_target()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 59 | // Create kernel |
| 60 | CLBuildOptions build_opts; |
| 61 | build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type))); |
| 62 | build_opts.add_option_if(has_bias, "-DHAS_BIAS"); |
| 63 | build_opts.add_option_if(is_data_type_fixed_point(data_type), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 64 | build_opts.add_option_if(is_data_type_quantized_asymmetric(data_type), "-DOFFSET=" + support::cpp11::to_string(input->info()->quantization_info().offset)); |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 65 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | int stride_x = 0; |
| 67 | int stride_y = 0; |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 68 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 70 | |
| 71 | const bool run_img2col_reduced = (output->info()->dimension(0) == (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))) && (TensorShape::num_max_dimensions >= 4) |
| 72 | && (std::equal(input->info()->tensor_shape().cbegin() + 3, |
| 73 | input->info()->tensor_shape().cend(), |
| 74 | output->info()->tensor_shape().cbegin() + 1)) |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 75 | && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 77 | std::string kernel_name = "im2col_generic"; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | if(!run_img2col_reduced) |
| 79 | { |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 80 | _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), |
| 81 | kernel_dims.width, kernel_dims.height, |
| 82 | conv_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | _num_elems_processed_per_iteration = output->info()->dimension(0); |
| 84 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 85 | build_opts.add_option("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width)); |
| 86 | build_opts.add_option("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height)); |
| 87 | build_opts.add_option("-DKERNEL_DEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 88 | build_opts.add_option("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(_convolved_dims.first)); |
| 89 | build_opts.add_option("-DCONVOLVED_HEIGHT=" + support::cpp11::to_string(_convolved_dims.second)); |
| 90 | build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first)); |
| 91 | build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second)); |
| 92 | build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left())); |
| 93 | build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top())); |
| 94 | build_opts.add_option("-DPAD_RIGHT=" + support::cpp11::to_string(conv_info.pad_right())); |
| 95 | build_opts.add_option("-DPAD_BOTTOM=" + support::cpp11::to_string(conv_info.pad_bottom())); |
| 96 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0))); |
| 97 | build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 98 | |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 99 | if(kernel_dims.width == 3 && kernel_dims.height == 3 && !conv_info.has_padding()) |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 100 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 101 | kernel_name = "im2col_kernel3x3_padx0_pady0"; |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 102 | |
| 103 | // Local work size optimized for the 3x3 MobileNets convolution on Bifrost. |
| 104 | if(gpu_target == GPUTarget::BIFROST && input->info()->dimension(0) == 224) |
| 105 | { |
| 106 | _lws_hint = cl::NDRange(2, 3, 3); |
| 107 | } |
| 108 | } |
| 109 | else if(kernel_dims.width > 1 && !conv_info.has_padding()) |
| 110 | { |
| 111 | kernel_name = "im2col_generic_padx0_pady0"; |
| 112 | |
| 113 | // Optimized im2col is performed using one or more vector operations with the specified vector size |
| 114 | // and a remainder. For example, for 5x5 convolutions, im2col is performed using vectors of size 4 |
| 115 | // and scalars; for 7x7 convolutions, using vectors of size 4 and vectors of size 3. |
| 116 | // Using the vector size of 4 is always safe since OpenCL supports vectors of size 2 and 3. |
| 117 | // Using the vector size of 8, however, may be faster. |
| 118 | size_t vector_size = 4; |
| 119 | // For 2x2 convolutions, use vectors of size 2. (For 3x3 convolutions, im2col_kernel3x3_padx0_pady0 |
| 120 | // is used instead.) |
| 121 | if(kernel_dims.width < vector_size) |
| 122 | { |
| 123 | vector_size = kernel_dims.width; |
| 124 | } |
| 125 | // Local work size and vector size optimized for the 11x11 AlexNet convolution on Bifrost. |
| 126 | if(gpu_target == GPUTarget::BIFROST && kernel_dims.width == 11) |
| 127 | { |
| 128 | _lws_hint = cl::NDRange(1, 1, 1); |
| 129 | vector_size = 8; |
| 130 | } |
| 131 | const size_t width_mod_vector_size = kernel_dims.width % vector_size; |
| 132 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(vector_size)); |
| 133 | build_opts.add_option("-DWIDTH_MOD_VECTOR_SIZE=" + support::cpp11::to_string(width_mod_vector_size)); |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | if(gpu_target == GPUTarget::BIFROST) |
| 138 | { |
| 139 | const size_t input_channels = input->info()->dimension(2); |
| 140 | if((input_channels & (input_channels - 1)) == 0) |
| 141 | { |
| 142 | // input_channels is a power of two |
| 143 | _lws_hint = cl::NDRange(1, 1, 4); |
| 144 | } |
| 145 | else if(input_channels < 192 && (input_channels % 4) == 0) |
| 146 | { |
| 147 | // input_channels is less than 192 and is a multiple of 4 |
| 148 | _lws_hint = cl::NDRange(1, 1, 2); |
| 149 | } |
| 150 | // otherwise the default is optimal |
| 151 | } |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 152 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | _run_func = &CLIm2ColKernel::run_generic; |
| 154 | } |
| 155 | else |
| 156 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 157 | kernel_name = "im2col_reduced"; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 158 | _num_elems_processed_per_iteration = 1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | _run_func = &CLIm2ColKernel::run_reduced; |
| 160 | } |
| 161 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 162 | // Create kernel |
| 163 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
| 164 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | // Configure kernel window |
| 166 | Window win = calculate_max_window(*input->info(), Steps()); |
| 167 | // The CLIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped |
| 168 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 169 | if(!run_img2col_reduced) |
| 170 | { |
| 171 | // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension |
| 172 | win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start()); |
| 173 | } |
| 174 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 175 | ICLKernel::configure(win); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 176 | |
| 177 | // Set config_id for enabling LWS tuning |
| 178 | _config_id = "im2col_"; |
| 179 | _config_id += (run_img2col_reduced ? "reduced_" : ""); |
| 180 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 181 | _config_id += "_"; |
| 182 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 183 | _config_id += "_"; |
| 184 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void CLIm2ColKernel::run(const Window &window, cl::CommandQueue &queue) |
| 188 | { |
| 189 | ARM_COMPUTE_ERROR_ON(_run_func == nullptr); |
| 190 | (this->*_run_func)(window, queue); |
| 191 | } |
| 192 | |
| 193 | void CLIm2ColKernel::run_generic(const Window &window, cl::CommandQueue &queue) |
| 194 | { |
| 195 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 196 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 197 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 198 | // Get initial windows |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 199 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 200 | // Change the Z dimension's step back to 1 |
| 201 | window_collapsed.set_dimension_step(Window::DimZ, 1); |
| 202 | |
| 203 | Window slice = window_collapsed.first_slice_window_3D(); |
| 204 | Window slice_in = window_collapsed.first_slice_window_3D(); |
| 205 | Window slice_out = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 206 | |
| 207 | // Setup slice |
| 208 | slice.set(Window::DimX, Window::Dimension(0, static_cast<int>(_convolved_dims.first), 1)); |
| 209 | slice.set(Window::DimY, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 210 | |
| 211 | // Setup input slice |
| 212 | // The first three dimensions of the input are increased by the inner loops |
| 213 | slice_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 214 | slice_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 215 | slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 216 | |
| 217 | // Setup output slice |
| 218 | slice_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _num_elems_processed_per_iteration)); |
| 219 | slice_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1)); |
| 220 | slice_out.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
| 221 | |
| 222 | do |
| 223 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 224 | unsigned int idx = 0; |
| 225 | add_3D_tensor_argument(idx, _input, slice_in); |
| 226 | add_2D_tensor_argument(idx, _output, slice_out); |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 227 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3])); |
| 228 | _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] | 229 | enqueue(queue, *this, slice, _lws_hint); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 230 | } |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 231 | 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] | 232 | } |
| 233 | |
| 234 | void CLIm2ColKernel::run_reduced(const Window &window, cl::CommandQueue &queue) |
| 235 | { |
| 236 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 237 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 238 | |
| 239 | Window out_window; |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 240 | out_window.use_tensor_dimensions(_output->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 241 | |
| 242 | Window out_slice = out_window.first_slice_window_1D(); |
| 243 | Window in_slice = window.first_slice_window_3D(); |
| 244 | |
| 245 | // Run kernel |
| 246 | do |
| 247 | { |
| 248 | // Set arguments |
| 249 | unsigned int idx = 0; |
| 250 | add_3D_tensor_argument(idx, _input, in_slice); |
| 251 | add_1D_tensor_argument(idx, _output, out_slice); |
| 252 | |
| 253 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(0)); |
| 254 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(1)); |
Gian Marco | d7779da | 2017-11-22 14:46:28 +0000 | [diff] [blame] | 255 | enqueue(queue, *this, in_slice, _lws_hint); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 256 | } |
| 257 | while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice)); |
| 258 | } |