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