blob: 6d7b83471f5c223bddedb8f1d86e59837f2a04d3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou702dc0c2021-03-19 15:06:07 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44: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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLCol2ImKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44: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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Helpers.h"
Georgios Pinitas78c00902018-01-09 17:33:11 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/CL/CLValidate.h"
33#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37#include <cmath>
38
Georgios Pinitas78c00902018-01-09 17:33:11 +000039using namespace arm_compute::misc::shape_calculator;
40
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010041namespace arm_compute
42{
Georgios Pinitas78c00902018-01-09 17:33:11 +000043namespace
44{
Giorgio Arena226e4b92018-08-23 12:00:02 +010045Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims, unsigned int num_groups)
Georgios Pinitas78c00902018-01-09 17:33:11 +000046{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010048 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Georgios Pinitas78c00902018-01-09 17:33:11 +000050
51 // Checks performed when output is configured
52 if(output->total_size() != 0)
53 {
Giorgio Arena226e4b92018-08-23 12:00:02 +010054 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_col2im_shape(*input, convolved_dims, true, num_groups));
Georgios Pinitas78c00902018-01-09 17:33:11 +000055 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Vidhya Sudhan Loganathanedf357c2018-04-27 14:25:30 +010056 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michele Di Giorgio980002b2018-08-08 09:25:51 +010057 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_layout() != DataLayout::NCHW, "Col2Im output's data layout must always be NCHW");
Georgios Pinitas78c00902018-01-09 17:33:11 +000058 }
59
60 return Status{};
61}
62
Giorgio Arena226e4b92018-08-23 12:00:02 +010063std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &convolved_dims, unsigned int num_groups)
Georgios Pinitas78c00902018-01-09 17:33:11 +000064{
65 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
66 // Output auto inizialitation if not yet initialized
Giorgio Arena226e4b92018-08-23 12:00:02 +010067 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 Pinitas78c00902018-01-09 17:33:11 +000068
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010069 constexpr unsigned int num_elems_read_per_iteration = 8;
Georgios Pinitas78c00902018-01-09 17:33:11 +000070
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
Georgios Pinitas78c00902018-01-09 17:33:11 +000078 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
79 return std::make_pair(err, win);
80}
81} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082
83CLCol2ImKernel::CLCol2ImKernel()
84 : _input(nullptr), _output(nullptr), _convolved_dims()
85{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010086 _type = CLKernelType::ELEMENTWISE;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087}
88
Giorgio Arena226e4b92018-08-23 12:00:02 +010089void CLCol2ImKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &convolved_dims, unsigned int num_groups)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010091 configure(CLKernelLibrary::get().get_compile_context(), input, output, convolved_dims, num_groups);
92}
93
Manuel Bottini256c0b92020-04-21 13:29:30 +010094void CLCol2ImKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const Size2D &convolved_dims, unsigned int num_groups)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010095{
Georgios Pinitas78c00902018-01-09 17:33:11 +000096 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbierfcdbc662017-08-04 18:05:29 +010097
Georgios Pinitas78c00902018-01-09 17:33:11 +000098 // Perform validation step
Michele Di Giorgio980002b2018-08-08 09:25:51 +010099 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), convolved_dims, num_groups));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
101 _input = input;
102 _output = output;
103 _convolved_dims = convolved_dims;
104
Chunosov5124be52017-11-22 20:42:13 +0700105 const DataType data_type = input->info()->data_type();
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100106
Chunosov5124be52017-11-22 20:42:13 +0700107 // Create kernel
108 CLBuildOptions build_opts;
109 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
Gian Marco54f18c42018-02-07 23:13:06 +0000110 build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->info()->element_size()));
111 build_opts.add_option("-DWIDTH_INPUT=" + support::cpp11::to_string(input->info()->dimension(0)));
Giorgio Arena226e4b92018-08-23 12:00:02 +0100112 build_opts.add_option("-DWIDTH_OUTPUT=" + support::cpp11::to_string(_convolved_dims.width));
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100113 build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups));
Chunosov5124be52017-11-22 20:42:13 +0700114
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100115 _kernel = create_kernel(compile_context, "col2im", build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116
Georgios Pinitas78c00902018-01-09 17:33:11 +0000117 // Configure kernel window
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100118 auto win_config = validate_and_configure_window(input->info(), output->info(), _convolved_dims, num_groups);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000119 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100120 ICLKernel::configure_internal(win_config.second);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000121
122 // Set config_id for enabling LWS tuning
123 _config_id = "col2im_";
124 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
125 _config_id += "_";
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100126 _config_id += support::cpp11::to_string(num_groups);
127 _config_id += "_";
Giorgio Arena63485ce2017-11-15 16:04:20 +0000128 _config_id += support::cpp11::to_string(input->info()->dimension(0));
129 _config_id += "_";
130 _config_id += support::cpp11::to_string(input->info()->dimension(1));
131 _config_id += "_";
132 _config_id += support::cpp11::to_string(output->info()->dimension(0));
133 _config_id += "_";
134 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135}
136
Giorgio Arena226e4b92018-08-23 12:00:02 +0100137Status CLCol2ImKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims, unsigned int num_groups)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000138{
Michele Di Giorgio80eb8032018-02-27 11:22:17 +0000139 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100140 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims, num_groups));
141 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), convolved_dims, num_groups).first);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000142 return Status{};
143}
144
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145void CLCol2ImKernel::run(const Window &window, cl::CommandQueue &queue)
146{
147 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
148 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100149
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100150 bool is_collapsed = false;
151 bool is_collapsed_out = false;
152
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100153 Window out_window;
154 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100156 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ, &is_collapsed);
157 Window collapsed_out = out_window.collapse_if_possible(out_window, 3, &is_collapsed_out);
steniu01cfc6fe82017-07-27 15:42:44 +0100158
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100159 ARM_COMPUTE_ERROR_ON(is_collapsed != is_collapsed_out);
Gian Marco Iodice2eac5bd2017-08-14 14:22:23 +0100160
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100161 Window slice = collapsed.first_slice_window_3D();
162 Window slice_out = collapsed_out.first_slice_window_4D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 do
164 {
165 // Set inputs
166 unsigned int idx = 0;
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100167 add_3D_tensor_argument(idx, _input, slice);
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100168 add_4D_tensor_argument(idx, _output, slice_out);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100169 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 }
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100171 while(collapsed.slide_window_slice_3D(slice) && collapsed_out.slide_window_slice_4D(slice_out));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100173} // namespace arm_compute