1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "src/gpu/cl/kernels/ClCol2ImKernel.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/CL/CLHelpers.h"
27*c217d954SCole Faust #include "arm_compute/core/CL/CLKernelLibrary.h"
28*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
29*c217d954SCole Faust #include "arm_compute/core/CL/OpenCL.h"
30*c217d954SCole Faust #include "arm_compute/core/Helpers.h"
31*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
32*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
33*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
34*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
35*c217d954SCole Faust #include "support/Cast.h"
36*c217d954SCole Faust #include "support/StringSupport.h"
37*c217d954SCole Faust
38*c217d954SCole Faust #include <cmath>
39*c217d954SCole Faust
40*c217d954SCole Faust namespace arm_compute
41*c217d954SCole Faust {
42*c217d954SCole Faust using namespace misc::shape_calculator;
43*c217d954SCole Faust namespace opencl
44*c217d954SCole Faust {
45*c217d954SCole Faust namespace kernels
46*c217d954SCole Faust {
47*c217d954SCole Faust namespace
48*c217d954SCole Faust {
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const Size2D & convolved_dims,unsigned int num_groups)49*c217d954SCole Faust Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const Size2D &convolved_dims, unsigned int num_groups)
50*c217d954SCole Faust {
51*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
52*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
53*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
54*c217d954SCole Faust
55*c217d954SCole Faust // Checks performed when output is configured
56*c217d954SCole Faust if(dst->total_size() != 0)
57*c217d954SCole Faust {
58*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), compute_col2im_shape(*src, convolved_dims, true, num_groups));
59*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
60*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
61*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_layout() != DataLayout::NCHW, "Col2Im output's data layout must always be NCHW");
62*c217d954SCole Faust }
63*c217d954SCole Faust
64*c217d954SCole Faust return Status{};
65*c217d954SCole Faust }
66*c217d954SCole Faust
validate_and_configure_window(ITensorInfo * src,ITensorInfo * dst,const Size2D & convolved_dims,unsigned int num_groups)67*c217d954SCole Faust std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst, const Size2D &convolved_dims, unsigned int num_groups)
68*c217d954SCole Faust {
69*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
70*c217d954SCole Faust // Output auto inizialitation if not yet initialized
71*c217d954SCole Faust auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_col2im_shape(*src, convolved_dims, true, num_groups)).set_data_layout(DataLayout::NCHW));
72*c217d954SCole Faust
73*c217d954SCole Faust constexpr unsigned int num_elems_read_per_iteration = 8;
74*c217d954SCole Faust
75*c217d954SCole Faust // Configure window
76*c217d954SCole Faust Window win = calculate_max_window(*src, Steps(num_elems_read_per_iteration));
77*c217d954SCole Faust
78*c217d954SCole Faust // Update window and padding just for the input tensor as we cannot access out-of-bounds elements in the output one
79*c217d954SCole Faust AccessWindowHorizontal input_access(src, 0, num_elems_read_per_iteration);
80*c217d954SCole Faust bool window_changed = update_window_and_padding(win, input_access);
81*c217d954SCole Faust
82*c217d954SCole Faust Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
83*c217d954SCole Faust return std::make_pair(err, win);
84*c217d954SCole Faust }
85*c217d954SCole Faust } // namespace
86*c217d954SCole Faust
ClCol2ImKernel()87*c217d954SCole Faust ClCol2ImKernel::ClCol2ImKernel()
88*c217d954SCole Faust : _convolved_dims()
89*c217d954SCole Faust {
90*c217d954SCole Faust _type = CLKernelType::ELEMENTWISE;
91*c217d954SCole Faust }
92*c217d954SCole Faust
configure(const CLCompileContext & compile_context,ITensorInfo * src,ITensorInfo * dst,const Size2D & convolved_dims,unsigned int num_groups)93*c217d954SCole Faust void ClCol2ImKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const Size2D &convolved_dims, unsigned int num_groups)
94*c217d954SCole Faust {
95*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
96*c217d954SCole Faust
97*c217d954SCole Faust // Perform validation step
98*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, convolved_dims, num_groups));
99*c217d954SCole Faust
100*c217d954SCole Faust _convolved_dims = convolved_dims;
101*c217d954SCole Faust
102*c217d954SCole Faust const DataType data_type = src->data_type();
103*c217d954SCole Faust
104*c217d954SCole Faust // Create kernel
105*c217d954SCole Faust CLBuildOptions build_opts;
106*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
107*c217d954SCole Faust build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(src->element_size()));
108*c217d954SCole Faust build_opts.add_option("-DWIDTH_INPUT=" + support::cpp11::to_string(src->dimension(0)));
109*c217d954SCole Faust build_opts.add_option("-DWIDTH_OUTPUT=" + support::cpp11::to_string(_convolved_dims.width));
110*c217d954SCole Faust build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups));
111*c217d954SCole Faust
112*c217d954SCole Faust _kernel = create_kernel(compile_context, "col2im", build_opts.options());
113*c217d954SCole Faust
114*c217d954SCole Faust // Configure kernel window
115*c217d954SCole Faust auto win_config = validate_and_configure_window(src, dst, _convolved_dims, num_groups);
116*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
117*c217d954SCole Faust IClKernel::configure_internal(win_config.second);
118*c217d954SCole Faust
119*c217d954SCole Faust // Set config_id for enabling LWS tuning
120*c217d954SCole Faust _config_id = "col2im_";
121*c217d954SCole Faust _config_id += lower_string(string_from_data_type(src->data_type()));
122*c217d954SCole Faust _config_id += "_";
123*c217d954SCole Faust _config_id += support::cpp11::to_string(num_groups);
124*c217d954SCole Faust _config_id += "_";
125*c217d954SCole Faust _config_id += support::cpp11::to_string(src->dimension(0));
126*c217d954SCole Faust _config_id += "_";
127*c217d954SCole Faust _config_id += support::cpp11::to_string(src->dimension(1));
128*c217d954SCole Faust _config_id += "_";
129*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(0));
130*c217d954SCole Faust _config_id += "_";
131*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(1));
132*c217d954SCole Faust }
133*c217d954SCole Faust
validate(const ITensorInfo * src,const ITensorInfo * dst,const Size2D & convolved_dims,unsigned int num_groups)134*c217d954SCole Faust Status ClCol2ImKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const Size2D &convolved_dims, unsigned int num_groups)
135*c217d954SCole Faust {
136*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
137*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, convolved_dims, num_groups));
138*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get(), convolved_dims, num_groups).first);
139*c217d954SCole Faust return Status{};
140*c217d954SCole Faust }
141*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)142*c217d954SCole Faust void ClCol2ImKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
143*c217d954SCole Faust {
144*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
145*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IClKernel::window(), window);
146*c217d954SCole Faust
147*c217d954SCole Faust bool is_collapsed = false;
148*c217d954SCole Faust bool is_collapsed_out = false;
149*c217d954SCole Faust
150*c217d954SCole Faust auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
151*c217d954SCole Faust auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
152*c217d954SCole Faust
153*c217d954SCole Faust Window out_window;
154*c217d954SCole Faust out_window.use_tensor_dimensions(dst->info()->tensor_shape());
155*c217d954SCole Faust
156*c217d954SCole Faust Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &is_collapsed);
157*c217d954SCole Faust Window collapsed_out = out_window.collapse_if_possible(out_window, 3, &is_collapsed_out);
158*c217d954SCole Faust
159*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(is_collapsed != is_collapsed_out);
160*c217d954SCole Faust
161*c217d954SCole Faust Window slice = collapsed.first_slice_window_3D();
162*c217d954SCole Faust Window slice_out = collapsed_out.first_slice_window_4D();
163*c217d954SCole Faust do
164*c217d954SCole Faust {
165*c217d954SCole Faust // Set inputs
166*c217d954SCole Faust unsigned int idx = 0;
167*c217d954SCole Faust add_3D_tensor_argument(idx, src, slice);
168*c217d954SCole Faust add_4D_tensor_argument(idx, dst, slice_out);
169*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
170*c217d954SCole Faust }
171*c217d954SCole Faust while(collapsed.slide_window_slice_3D(slice) && collapsed_out.slide_window_slice_4D(slice_out));
172*c217d954SCole Faust }
173*c217d954SCole Faust } // namespace kernels
174*c217d954SCole Faust } // namespace opencl
175*c217d954SCole Faust } // namespace arm_compute
176