Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +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/GLES_COMPUTE/kernels/GCIm2ColKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" |
| 29 | #include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" |
| 30 | #include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" |
| 31 | #include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/Types.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "support/ToolchainSupport.h" |
| 36 | |
| 37 | #include <cmath> |
| 38 | #include <tuple> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | GCIm2ColKernel::GCIm2ColKernel() |
| 43 | : _input(nullptr), _output(nullptr), _convolved_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void GCIm2ColKernel::configure(const IGCTensor *input, IGCTensor *output, std::pair<unsigned int, unsigned int> kernel_dims, const PadStrideInfo &conv_info, bool has_bias) |
| 48 | { |
| 49 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 50 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 51 | ARM_COMPUTE_UNUSED(kernel_dims); |
| 52 | |
| 53 | _input = input; |
| 54 | _output = output; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 55 | |
| 56 | std::set<std::string> build_opts; |
| 57 | std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; |
| 58 | build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); |
| 59 | build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); |
| 60 | build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); |
| 61 | build_opts.insert("#define " + dt_name); |
| 62 | |
| 63 | if(has_bias) |
| 64 | { |
| 65 | build_opts.emplace("#define HAS_BIAS"); |
| 66 | } |
| 67 | |
| 68 | int pad_x = 0; |
| 69 | int pad_y = 0; |
| 70 | int stride_x = 0; |
| 71 | int stride_y = 0; |
| 72 | std::tie(pad_x, pad_y) = conv_info.pad(); |
| 73 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 74 | |
| 75 | 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) |
| 76 | && (std::equal(input->info()->tensor_shape().cbegin() + 3, |
| 77 | input->info()->tensor_shape().cend(), |
| 78 | output->info()->tensor_shape().cbegin() + 1)) |
| 79 | && ((stride_x == 1) && (stride_y == 1) && (pad_x == 0) && (pad_y == 0)); |
| 80 | |
| 81 | if(!run_img2col_reduced) |
| 82 | { |
| 83 | // this path is currently not used and not validated |
| 84 | build_opts.insert("#define IM2COL_GENERIC"); |
| 85 | _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), |
| 86 | kernel_dims.first, kernel_dims.second, |
| 87 | conv_info); |
| 88 | _num_elems_processed_per_iteration = output->info()->dimension(0); |
| 89 | |
| 90 | build_opts.emplace("#define KERNEL_WIDTH " + support::cpp11::to_string(kernel_dims.first)); |
| 91 | build_opts.emplace("#define KERNEL_HEIGHT " + support::cpp11::to_string(kernel_dims.second)); |
| 92 | build_opts.emplace("#define KERNEL_DEPTH " + support::cpp11::to_string(input->info()->dimension(2))); |
| 93 | build_opts.emplace("#define CONVOLVED_WIDTH " + support::cpp11::to_string(_convolved_dims.first)); |
| 94 | build_opts.emplace("#define CONVOLVED_HEIGHT " + support::cpp11::to_string(_convolved_dims.second)); |
| 95 | build_opts.emplace("#define STRIDE_X " + support::cpp11::to_string(conv_info.stride().first)); |
| 96 | build_opts.emplace("#define STRIDE_Y " + support::cpp11::to_string(conv_info.stride().second)); |
| 97 | build_opts.emplace("#define PAD_X " + support::cpp11::to_string(conv_info.pad().first)); |
| 98 | build_opts.emplace("#define PAD_Y " + support::cpp11::to_string(conv_info.pad().second)); |
| 99 | build_opts.emplace("#define SRC_WIDTH " + support::cpp11::to_string(input->info()->dimension(0))); |
| 100 | build_opts.emplace("#define SRC_HEIGHT " + support::cpp11::to_string(input->info()->dimension(1))); |
| 101 | |
| 102 | // Create kernel |
| 103 | _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("im2col_generic", build_opts)); |
| 104 | |
| 105 | _run_func = &GCIm2ColKernel::run_generic; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | build_opts.insert("#define IM2COL_REDUCED"); |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame^] | 110 | |
| 111 | if(input->info()->data_type() == DataType::F32) |
| 112 | { |
| 113 | _num_elems_processed_per_iteration = 4 / input->info()->element_size(); |
| 114 | } |
| 115 | else if(input->info()->data_type() == DataType::F16) |
| 116 | { |
| 117 | int input_width = input->info()->dimension(0); |
| 118 | int input_height = input->info()->dimension(1); |
| 119 | |
| 120 | build_opts.insert("#define IMAGE_SIZE " + support::cpp11::to_string(input_width * input_height)); |
| 121 | if(input_width % 8 == 0) |
| 122 | { |
| 123 | _num_elems_processed_per_iteration = 8; |
| 124 | build_opts.insert("#define IM2COL_REDUCED_8X"); |
| 125 | } |
| 126 | else if(input_width % 4 == 0) |
| 127 | { |
| 128 | _num_elems_processed_per_iteration = 4; |
| 129 | build_opts.insert("#define IM2COL_REDUCED_4X"); |
| 130 | } |
| 131 | else if(input_width % 2 == 0) |
| 132 | { |
| 133 | _num_elems_processed_per_iteration = 2; |
| 134 | build_opts.insert("#define IM2COL_REDUCED_2X"); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | _num_elems_processed_per_iteration = 2; |
| 139 | build_opts.insert("#define IM2COL_REDUCED_GENERIC"); |
| 140 | } |
| 141 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 142 | |
| 143 | // Create kernel |
| 144 | _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("im2col_reduced", build_opts)); |
| 145 | |
| 146 | _run_func = &GCIm2ColKernel::run_reduced; |
| 147 | } |
| 148 | |
| 149 | // Configure kernel window |
| 150 | Window win = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration)); |
| 151 | |
| 152 | if(input->info()->data_type() == DataType::F16) |
| 153 | { |
| 154 | // Calculate input right and bottom border |
| 155 | AccessWindowHorizontal input_access(input->info(), 0, _num_elems_processed_per_iteration); |
| 156 | |
| 157 | // Calculate output right and bottom border |
| 158 | const int output_width = output->info()->dimension(0); |
| 159 | const int output_height = output->info()->dimension(1); |
| 160 | const int output_padding_right = ceil_to_multiple(output_width, _num_elems_processed_per_iteration) - output_width; |
| 161 | AccessWindowStatic output_access(output->info(), 0, 0, output_width + output_padding_right, output_height); |
| 162 | |
| 163 | update_window_and_padding(win, input_access, output_access); |
| 164 | } |
| 165 | |
| 166 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 167 | |
| 168 | if(!run_img2col_reduced) |
| 169 | { |
| 170 | // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension |
| 171 | win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start()); |
| 172 | } |
| 173 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 174 | IGCKernel::configure(win); |
| 175 | } |
| 176 | |
| 177 | void GCIm2ColKernel::run(const Window &window) |
| 178 | { |
| 179 | ARM_COMPUTE_ERROR_ON(_run_func == nullptr); |
| 180 | (this->*_run_func)(window); |
| 181 | } |
| 182 | |
| 183 | void GCIm2ColKernel::run_generic(const Window &window) |
| 184 | { |
| 185 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 186 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window); |
| 187 | |
| 188 | // Get initial windows |
| 189 | Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ); |
| 190 | // Change the Z dimension's step back to 1 |
| 191 | window_collapsed.set_dimension_step(Window::DimZ, 1); |
| 192 | |
| 193 | Window slice = window_collapsed.first_slice_window_3D(); |
| 194 | Window slice_in = window_collapsed.first_slice_window_3D(); |
| 195 | Window slice_out = window_collapsed.first_slice_window_3D(); |
| 196 | |
| 197 | // Setup slice |
| 198 | slice.set(Window::DimX, Window::Dimension(0, static_cast<int>(_convolved_dims.first), 1)); |
| 199 | slice.set(Window::DimY, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1)); |
| 200 | |
| 201 | // Setup input slice |
| 202 | // The first three dimensions of the input are increased by the inner loops |
| 203 | slice_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 204 | slice_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 205 | slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 206 | |
| 207 | // Setup output slice |
| 208 | slice_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _num_elems_processed_per_iteration)); |
| 209 | slice_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1)); |
| 210 | slice_out.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
| 211 | |
| 212 | _kernel.use(); |
| 213 | |
| 214 | do |
| 215 | { |
| 216 | unsigned int idx = 0; |
| 217 | add_3D_tensor_argument(idx, _input, 1, slice_in); |
| 218 | add_2D_tensor_argument(idx, _output, 2, slice_out); |
| 219 | |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 220 | _kernel.set_argument(idx++, static_cast<unsigned int>(_input->info()->dimension(2))); |
| 221 | _kernel.set_argument(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3])); |
| 222 | _kernel.set_argument(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[3])); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 223 | _kernel.update_shader_params(); |
| 224 | |
| 225 | enqueue(*this, slice); |
| 226 | } |
| 227 | while(window_collapsed.slide_window_slice_3D(slice) && window_collapsed.slide_window_slice_3D(slice_out) && window_collapsed.slide_window_slice_3D(slice_in)); |
| 228 | } |
| 229 | |
| 230 | void GCIm2ColKernel::run_reduced(const Window &window) |
| 231 | { |
| 232 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 233 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window); |
| 234 | |
| 235 | Window out_window; |
| 236 | out_window.use_tensor_dimensions(_output->info()->tensor_shape()); |
| 237 | |
| 238 | Window out_slice = out_window.first_slice_window_1D(); |
| 239 | Window in_slice = window.first_slice_window_3D(); |
| 240 | |
| 241 | _kernel.use(); |
| 242 | |
| 243 | // Run kernel |
| 244 | do |
| 245 | { |
| 246 | // Set arguments |
| 247 | unsigned int idx = 0; |
| 248 | |
| 249 | add_3D_tensor_argument(idx, _input, 1, in_slice); |
| 250 | add_1D_tensor_argument(idx, _output, 2, out_slice); |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 251 | _kernel.set_argument(idx++, _input->info()->dimension(0)); |
| 252 | _kernel.set_argument(idx++, _input->info()->dimension(1)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 253 | _kernel.update_shader_params(); |
| 254 | |
| 255 | enqueue(*this, in_slice); |
| 256 | } |
| 257 | while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice)); |
| 258 | } |