Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 54f18c4 | 2018-02-07 23:13:06 +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/CLCol2ImKernel.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" |
| 33 | #include "arm_compute/core/Types.h" |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 34 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | |
| 36 | #include <cmath> |
| 37 | |
| 38 | using namespace arm_compute; |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 39 | using namespace arm_compute::misc::shape_calculator; |
| 40 | |
| 41 | namespace |
| 42 | { |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 43 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims, unsigned int num_groups) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 44 | { |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 48 | |
| 49 | // Checks performed when output is configured |
| 50 | if(output->total_size() != 0) |
| 51 | { |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_col2im_shape(*input, convolved_dims, true, num_groups)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Vidhya Sudhan Loganathan | edf357c | 2018-04-27 14:25:30 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michele Di Giorgio | 980002b | 2018-08-08 09:25:51 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_layout() != DataLayout::NCHW, "Col2Im output's data layout must always be NCHW"); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return Status{}; |
| 59 | } |
| 60 | |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 61 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &convolved_dims, unsigned int num_groups) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 62 | { |
| 63 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 64 | // Output auto inizialitation if not yet initialized |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 65 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_col2im_shape(*input, convolved_dims, true, num_groups)).set_data_layout(DataLayout::NCHW)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 66 | |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 67 | const unsigned int num_elems_read_per_iteration = 8; |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 68 | |
| 69 | // Configure window |
| 70 | Window win = calculate_max_window(*input, Steps(num_elems_read_per_iteration)); |
| 71 | |
| 72 | // Update window and padding just for the input tensor as we cannot access out-of-bounds elements in the output one |
| 73 | AccessWindowHorizontal input_access(input, 0, num_elems_read_per_iteration); |
| 74 | bool window_changed = update_window_and_padding(win, input_access); |
| 75 | |
| 76 | Coordinates coord; |
| 77 | coord.set_num_dimensions(output->num_dimensions()); |
| 78 | output->set_valid_region(ValidRegion(coord, output->tensor_shape())); |
| 79 | |
| 80 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 81 | return std::make_pair(err, win); |
| 82 | } |
| 83 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | |
| 85 | CLCol2ImKernel::CLCol2ImKernel() |
| 86 | : _input(nullptr), _output(nullptr), _convolved_dims() |
| 87 | { |
| 88 | } |
| 89 | |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 90 | void CLCol2ImKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &convolved_dims, unsigned int num_groups) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 92 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | fcdbc66 | 2017-08-04 18:05:29 +0100 | [diff] [blame] | 93 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 94 | // Perform validation step |
Michele Di Giorgio | 980002b | 2018-08-08 09:25:51 +0100 | [diff] [blame] | 95 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), convolved_dims, num_groups)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | |
| 97 | _input = input; |
| 98 | _output = output; |
| 99 | _convolved_dims = convolved_dims; |
| 100 | |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 101 | const DataType data_type = input->info()->data_type(); |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 102 | |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 103 | // Create kernel |
| 104 | CLBuildOptions build_opts; |
| 105 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
Gian Marco | 54f18c4 | 2018-02-07 23:13:06 +0000 | [diff] [blame] | 106 | build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->info()->element_size())); |
| 107 | build_opts.add_option("-DWIDTH_INPUT=" + support::cpp11::to_string(input->info()->dimension(0))); |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 108 | build_opts.add_option("-DWIDTH_OUTPUT=" + support::cpp11::to_string(_convolved_dims.width)); |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 109 | build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups)); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 110 | |
| 111 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("col2im", build_opts.options())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 113 | // Configure kernel window |
Michele Di Giorgio | 980002b | 2018-08-08 09:25:51 +0100 | [diff] [blame] | 114 | auto win_config = validate_and_configure_window(input->info(), output->info(), _convolved_dims, num_groups); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 115 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 116 | ICLKernel::configure_internal(win_config.second); |
Giorgio Arena | 63485ce | 2017-11-15 16:04:20 +0000 | [diff] [blame] | 117 | |
| 118 | // Set config_id for enabling LWS tuning |
| 119 | _config_id = "col2im_"; |
| 120 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 121 | _config_id += "_"; |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 122 | _config_id += support::cpp11::to_string(num_groups); |
| 123 | _config_id += "_"; |
Giorgio Arena | 63485ce | 2017-11-15 16:04:20 +0000 | [diff] [blame] | 124 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 125 | _config_id += "_"; |
| 126 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 127 | _config_id += "_"; |
| 128 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 129 | _config_id += "_"; |
| 130 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 133 | Status CLCol2ImKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims, unsigned int num_groups) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 134 | { |
Michele Di Giorgio | 80eb803 | 2018-02-27 11:22:17 +0000 | [diff] [blame] | 135 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Michele Di Giorgio | 980002b | 2018-08-08 09:25:51 +0100 | [diff] [blame] | 136 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims, num_groups)); |
| 137 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), convolved_dims, num_groups).first); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 138 | return Status{}; |
| 139 | } |
| 140 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | void CLCol2ImKernel::run(const Window &window, cl::CommandQueue &queue) |
| 142 | { |
| 143 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 144 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 145 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 146 | bool is_collapsed = false; |
| 147 | bool is_collapsed_out = false; |
| 148 | |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 149 | Window out_window; |
| 150 | out_window.use_tensor_dimensions(_output->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 152 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &is_collapsed); |
| 153 | Window collapsed_out = out_window.collapse_if_possible(out_window, 3, &is_collapsed_out); |
steniu01 | cfc6fe8 | 2017-07-27 15:42:44 +0100 | [diff] [blame] | 154 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 155 | ARM_COMPUTE_ERROR_ON(is_collapsed != is_collapsed_out); |
Gian Marco Iodice | 2eac5bd | 2017-08-14 14:22:23 +0100 | [diff] [blame] | 156 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 157 | Window slice = collapsed.first_slice_window_3D(); |
| 158 | Window slice_out = collapsed_out.first_slice_window_4D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | do |
| 160 | { |
| 161 | // Set inputs |
| 162 | unsigned int idx = 0; |
Michele Di Giorgio | 980002b | 2018-08-08 09:25:51 +0100 | [diff] [blame] | 163 | add_3D_tensor_argument(idx, _input, slice); |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 164 | add_4D_tensor_argument(idx, _output, slice_out); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 165 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | } |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 167 | while(collapsed.slide_window_slice_3D(slice) && collapsed_out.slide_window_slice_4D(slice_out)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | } |