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 | { |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame^] | 49 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, 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 | |
| 56 | // Create kernel |
| 57 | std::set<std::string> build_opts; |
| 58 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 59 | build_opts.emplace((has_bias ? "-DHAS_BIAS" : "")); |
| 60 | |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame^] | 61 | if(input->info()->data_type() == DataType::QS8) |
| 62 | { |
| 63 | build_opts.emplace("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 64 | } |
| 65 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | int pad_x = 0; |
| 67 | int pad_y = 0; |
| 68 | int stride_x = 0; |
| 69 | int stride_y = 0; |
| 70 | std::tie(pad_x, pad_y) = conv_info.pad(); |
| 71 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 72 | |
| 73 | 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) |
| 74 | && (std::equal(input->info()->tensor_shape().cbegin() + 3, |
| 75 | input->info()->tensor_shape().cend(), |
| 76 | output->info()->tensor_shape().cbegin() + 1)) |
| 77 | && ((stride_x == 1) && (stride_y == 1) && (pad_x == 0) && (pad_y == 0)); |
| 78 | |
| 79 | if(!run_img2col_reduced) |
| 80 | { |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 81 | _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), |
| 82 | kernel_dims.width, kernel_dims.height, |
| 83 | conv_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | _num_elems_processed_per_iteration = output->info()->dimension(0); |
| 85 | |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 86 | build_opts.emplace("-DKERNEL_WIDTH=" + support::cpp11::to_string(kernel_dims.width)); |
| 87 | build_opts.emplace("-DKERNEL_HEIGHT=" + support::cpp11::to_string(kernel_dims.height)); |
| 88 | build_opts.emplace("-DKERNEL_DEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 89 | build_opts.emplace("-DCONVOLVED_WIDTH=" + support::cpp11::to_string(_convolved_dims.first)); |
| 90 | build_opts.emplace("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.stride().first)); |
| 91 | build_opts.emplace("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second)); |
| 92 | build_opts.emplace("-DPAD_X=" + support::cpp11::to_string(conv_info.pad().first)); |
| 93 | build_opts.emplace("-DPAD_Y=" + support::cpp11::to_string(conv_info.pad().second)); |
| 94 | build_opts.emplace("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0))); |
| 95 | build_opts.emplace("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); |
| 96 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("im2col_generic", build_opts)); |
| 98 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | _run_func = &CLIm2ColKernel::run_generic; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | _num_elems_processed_per_iteration = 1; |
| 104 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("im2col_reduced", build_opts)); |
| 105 | _run_func = &CLIm2ColKernel::run_reduced; |
| 106 | } |
| 107 | |
| 108 | // Configure kernel window |
| 109 | Window win = calculate_max_window(*input->info(), Steps()); |
| 110 | // The CLIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped |
| 111 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 112 | ICLKernel::configure(win); |
| 113 | } |
| 114 | |
| 115 | void CLIm2ColKernel::run(const Window &window, cl::CommandQueue &queue) |
| 116 | { |
| 117 | ARM_COMPUTE_ERROR_ON(_run_func == nullptr); |
| 118 | (this->*_run_func)(window, queue); |
| 119 | } |
| 120 | |
| 121 | void CLIm2ColKernel::run_generic(const Window &window, cl::CommandQueue &queue) |
| 122 | { |
| 123 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 124 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 125 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | // Get initial windows |
| 127 | Window slice = window.first_slice_window_3D(); |
| 128 | Window slice_in = window.first_slice_window_3D(); |
| 129 | Window slice_out = window.first_slice_window_3D(); |
| 130 | |
| 131 | // Setup slice |
| 132 | slice.set(Window::DimX, Window::Dimension(0, static_cast<int>(_convolved_dims.first), 1)); |
| 133 | slice.set(Window::DimY, Window::Dimension(0, static_cast<int>(_convolved_dims.second), 1)); |
| 134 | slice.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
| 135 | |
| 136 | // Setup input slice |
| 137 | // The first three dimensions of the input are increased by the inner loops |
| 138 | slice_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 139 | slice_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 140 | slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 141 | |
| 142 | // Setup output slice |
| 143 | slice_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _num_elems_processed_per_iteration)); |
| 144 | slice_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 1)); |
| 145 | slice_out.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
| 146 | |
| 147 | do |
| 148 | { |
| 149 | // Set inputs |
| 150 | unsigned int idx = 0; |
| 151 | add_3D_tensor_argument(idx, _input, slice_in); |
| 152 | add_2D_tensor_argument(idx, _output, slice_out); |
| 153 | enqueue(queue, *this, slice); |
| 154 | } |
| 155 | while(window.slide_window_slice_3D(slice) && window.slide_window_slice_3D(slice_out) && window.slide_window_slice_3D(slice_in)); |
| 156 | } |
| 157 | |
| 158 | void CLIm2ColKernel::run_reduced(const Window &window, cl::CommandQueue &queue) |
| 159 | { |
| 160 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 161 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 162 | |
| 163 | Window out_window; |
| 164 | out_window.use_tensor_dimensions(_output->info()); |
| 165 | |
| 166 | Window out_slice = out_window.first_slice_window_1D(); |
| 167 | Window in_slice = window.first_slice_window_3D(); |
| 168 | |
| 169 | // Run kernel |
| 170 | do |
| 171 | { |
| 172 | // Set arguments |
| 173 | unsigned int idx = 0; |
| 174 | add_3D_tensor_argument(idx, _input, in_slice); |
| 175 | add_1D_tensor_argument(idx, _output, out_slice); |
| 176 | |
| 177 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(0)); |
| 178 | _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(1)); |
| 179 | enqueue(queue, *this, in_slice); |
| 180 | } |
| 181 | while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice)); |
| 182 | } |