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