Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 Arm Limited. |
Manuel Bottini | 7b23732 | 2021-07-14 17:07: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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/gpu/cl/kernels/ClCol2ImKernel.h" |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 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/Helpers.h" |
| 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 34 | #include "src/core/CL/CLValidate.h" |
| 35 | #include "src/core/helpers/AutoConfiguration.h" |
| 36 | #include "src/core/helpers/WindowHelpers.h" |
| 37 | #include "support/Cast.h" |
| 38 | #include "support/StringSupport.h" |
| 39 | |
| 40 | #include <cmath> |
| 41 | |
| 42 | namespace arm_compute |
| 43 | { |
| 44 | using namespace misc::shape_calculator; |
| 45 | namespace opencl |
| 46 | { |
| 47 | namespace kernels |
| 48 | { |
| 49 | namespace |
| 50 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 51 | Status validate_arguments(const ITensorInfo *src, |
| 52 | const ITensorInfo *dst, |
| 53 | const Size2D &convolved_dims, |
| 54 | unsigned int num_groups) |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 59 | DataType::F16, DataType::F32); |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 60 | |
| 61 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 62 | if (dst->total_size() != 0) |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 63 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS( |
| 65 | dst->tensor_shape(), compute_col2im_shape(*src, convolved_dims, true, num_groups)); |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 68 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_layout() != DataLayout::NCHW, |
| 69 | "Col2Im output's data layout must always be NCHW"); |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return Status{}; |
| 73 | } |
| 74 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 75 | std::pair<Status, Window> |
| 76 | validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst, const Size2D &convolved_dims, unsigned int num_groups) |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 77 | { |
| 78 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 79 | // Output auto inizialitation if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | auto_init_if_empty(*dst, src->clone() |
| 81 | ->set_tensor_shape(compute_col2im_shape(*src, convolved_dims, true, num_groups)) |
| 82 | .set_data_layout(DataLayout::NCHW)); |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 83 | |
| 84 | constexpr unsigned int num_elems_read_per_iteration = 8; |
| 85 | |
| 86 | // Configure window |
| 87 | Window win = calculate_max_window(*src, Steps(num_elems_read_per_iteration)); |
| 88 | |
| 89 | // Update window and padding just for the input tensor as we cannot access out-of-bounds elements in the output one |
| 90 | AccessWindowHorizontal input_access(src, 0, num_elems_read_per_iteration); |
| 91 | bool window_changed = update_window_and_padding(win, input_access); |
| 92 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 93 | Status err = |
| 94 | (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 95 | return std::make_pair(err, win); |
| 96 | } |
| 97 | } // namespace |
| 98 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 99 | ClCol2ImKernel::ClCol2ImKernel() : _convolved_dims() |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 100 | { |
| 101 | _type = CLKernelType::ELEMENTWISE; |
| 102 | } |
| 103 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 104 | void ClCol2ImKernel::configure(const CLCompileContext &compile_context, |
| 105 | ITensorInfo *src, |
| 106 | ITensorInfo *dst, |
| 107 | const Size2D &convolved_dims, |
| 108 | unsigned int num_groups) |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 109 | { |
| 110 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 111 | |
| 112 | // Perform validation step |
| 113 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, convolved_dims, num_groups)); |
| 114 | |
| 115 | _convolved_dims = convolved_dims; |
| 116 | |
| 117 | const DataType data_type = src->data_type(); |
| 118 | |
| 119 | // Create kernel |
| 120 | CLBuildOptions build_opts; |
| 121 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 122 | build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(src->element_size())); |
| 123 | build_opts.add_option("-DWIDTH_INPUT=" + support::cpp11::to_string(src->dimension(0))); |
| 124 | build_opts.add_option("-DWIDTH_OUTPUT=" + support::cpp11::to_string(_convolved_dims.width)); |
| 125 | build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups)); |
| 126 | |
| 127 | _kernel = create_kernel(compile_context, "col2im", build_opts.options()); |
| 128 | |
| 129 | // Configure kernel window |
| 130 | auto win_config = validate_and_configure_window(src, dst, _convolved_dims, num_groups); |
| 131 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 132 | IClKernel::configure_internal(win_config.second); |
| 133 | |
| 134 | // Set config_id for enabling LWS tuning |
| 135 | _config_id = "col2im_"; |
| 136 | _config_id += lower_string(string_from_data_type(src->data_type())); |
| 137 | _config_id += "_"; |
| 138 | _config_id += support::cpp11::to_string(num_groups); |
| 139 | _config_id += "_"; |
| 140 | _config_id += support::cpp11::to_string(src->dimension(0)); |
| 141 | _config_id += "_"; |
| 142 | _config_id += support::cpp11::to_string(src->dimension(1)); |
| 143 | _config_id += "_"; |
| 144 | _config_id += support::cpp11::to_string(dst->dimension(0)); |
| 145 | _config_id += "_"; |
| 146 | _config_id += support::cpp11::to_string(dst->dimension(1)); |
| 147 | } |
| 148 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 149 | Status ClCol2ImKernel::validate(const ITensorInfo *src, |
| 150 | const ITensorInfo *dst, |
| 151 | const Size2D &convolved_dims, |
| 152 | unsigned int num_groups) |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 153 | { |
| 154 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 155 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, convolved_dims, num_groups)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 156 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 157 | validate_and_configure_window(src->clone().get(), dst->clone().get(), convolved_dims, num_groups).first); |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 158 | return Status{}; |
| 159 | } |
| 160 | |
| 161 | void ClCol2ImKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
| 162 | { |
| 163 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 164 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IClKernel::window(), window); |
| 165 | |
| 166 | bool is_collapsed = false; |
| 167 | bool is_collapsed_out = false; |
| 168 | |
| 169 | auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC)); |
| 170 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
| 171 | |
| 172 | Window out_window; |
| 173 | out_window.use_tensor_dimensions(dst->info()->tensor_shape()); |
| 174 | |
| 175 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &is_collapsed); |
| 176 | Window collapsed_out = out_window.collapse_if_possible(out_window, 3, &is_collapsed_out); |
| 177 | |
| 178 | ARM_COMPUTE_ERROR_ON(is_collapsed != is_collapsed_out); |
| 179 | |
| 180 | Window slice = collapsed.first_slice_window_3D(); |
| 181 | Window slice_out = collapsed_out.first_slice_window_4D(); |
| 182 | do |
| 183 | { |
| 184 | // Set inputs |
| 185 | unsigned int idx = 0; |
| 186 | add_3D_tensor_argument(idx, src, slice); |
| 187 | add_4D_tensor_argument(idx, dst, slice_out); |
| 188 | enqueue(queue, *this, slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 189 | } while (collapsed.slide_window_slice_3D(slice) && collapsed_out.slide_window_slice_4D(slice_out)); |
Manuel Bottini | 7b23732 | 2021-07-14 17:07:23 +0100 | [diff] [blame] | 190 | } |
| 191 | } // namespace kernels |
| 192 | } // namespace opencl |
| 193 | } // namespace arm_compute |