blob: 91c0430b2110a77e5c0c48b49015c475459bc55d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco54f18c42018-02-07 23:13:06 +00002 * Copyright (c) 2017-2018 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 */
24#include "arm_compute/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/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/core/Validate.h"
Georgios Pinitas78c00902018-01-09 17:33:11 +000034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36#include <cmath>
37
38using namespace arm_compute;
Georgios Pinitas78c00902018-01-09 17:33:11 +000039using namespace arm_compute::misc::shape_calculator;
40
41namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, std::pair<unsigned int, unsigned int> convolved_dims)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
46 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
47
48 // Checks performed when output is configured
49 if(output->total_size() != 0)
50 {
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_col2im_shape(*input, convolved_dims));
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
Vidhya Sudhan Loganathanedf357c2018-04-27 14:25:30 +010054 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Georgios Pinitas78c00902018-01-09 17:33:11 +000055 }
56
57 return Status{};
58}
59
60std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, std::pair<unsigned int, unsigned int> convolved_dims)
61{
62 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
63 // Output auto inizialitation if not yet initialized
64 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_col2im_shape(*input, convolved_dims)));
65
66 const unsigned int num_elems_read_per_iteration = is_data_type_fixed_point(input->data_type()) ? 1 : 8;
67
68 // Configure window
69 Window win = calculate_max_window(*input, Steps(num_elems_read_per_iteration));
70
71 // Update window and padding just for the input tensor as we cannot access out-of-bounds elements in the output one
72 AccessWindowHorizontal input_access(input, 0, num_elems_read_per_iteration);
73 bool window_changed = update_window_and_padding(win, input_access);
74
75 Coordinates coord;
76 coord.set_num_dimensions(output->num_dimensions());
77 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
78
79 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
80 return std::make_pair(err, win);
81}
82} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083
84CLCol2ImKernel::CLCol2ImKernel()
85 : _input(nullptr), _output(nullptr), _convolved_dims()
86{
87}
88
89void CLCol2ImKernel::configure(const ICLTensor *input, ICLTensor *output, std::pair<unsigned int, unsigned int> convolved_dims)
90{
Georgios Pinitas78c00902018-01-09 17:33:11 +000091 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbierfcdbc662017-08-04 18:05:29 +010092
Georgios Pinitas78c00902018-01-09 17:33:11 +000093 // Perform validation step
94 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), convolved_dims));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
96 _input = input;
97 _output = output;
98 _convolved_dims = convolved_dims;
99
Chunosov5124be52017-11-22 20:42:13 +0700100 const DataType data_type = input->info()->data_type();
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100101
Chunosov5124be52017-11-22 20:42:13 +0700102 // Create kernel
103 CLBuildOptions build_opts;
104 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
Gian Marco54f18c42018-02-07 23:13:06 +0000105 build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->info()->element_size()));
106 build_opts.add_option("-DWIDTH_INPUT=" + support::cpp11::to_string(input->info()->dimension(0)));
Chunosov5124be52017-11-22 20:42:13 +0700107 build_opts.add_option("-DWIDTH_OUTPUT=" + support::cpp11::to_string(_convolved_dims.first));
108 build_opts.add_option_if(is_data_type_fixed_point(data_type), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
109
110 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("col2im", build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000112 // Configure the local work size for Bifrost with a value obtained
113 // via exhaustive autotuning over 30 representative tensor shapes.
Michalis Spyroua9676112018-02-22 18:07:43 +0000114 const GPUTarget gpu_target = get_target();
Sam Laynton56e8e862018-04-05 13:26:08 +0100115 if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX))
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000116 {
117 if((_convolved_dims.first == 7) || (_convolved_dims.first == 14))
118 {
119 _lws_hint = cl::NDRange(1, 7, 1);
120 }
121 else
122 {
123 _lws_hint = cl::NDRange(1, 8, 1);
124 }
125 }
126
Georgios Pinitas78c00902018-01-09 17:33:11 +0000127 // Configure kernel window
128 auto win_config = validate_and_configure_window(input->info(), output->info(), _convolved_dims);
129 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
130 ICLKernel::configure(win_config.second);
Giorgio Arena63485ce2017-11-15 16:04:20 +0000131
132 // Set config_id for enabling LWS tuning
133 _config_id = "col2im_";
134 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
135 _config_id += "_";
136 _config_id += support::cpp11::to_string(input->info()->dimension(0));
137 _config_id += "_";
138 _config_id += support::cpp11::to_string(input->info()->dimension(1));
139 _config_id += "_";
140 _config_id += support::cpp11::to_string(output->info()->dimension(0));
141 _config_id += "_";
142 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143}
144
Georgios Pinitas78c00902018-01-09 17:33:11 +0000145Status CLCol2ImKernel::validate(const ITensorInfo *input, const ITensorInfo *output, std::pair<unsigned int, unsigned int> convolved_dims)
146{
Michele Di Giorgio80eb8032018-02-27 11:22:17 +0000147 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000148 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims));
Michele Di Giorgio80eb8032018-02-27 11:22:17 +0000149 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), convolved_dims).first);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000150 return Status{};
151}
152
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153void CLCol2ImKernel::run(const Window &window, cl::CommandQueue &queue)
154{
155 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
156 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
steniu01cfc6fe82017-07-27 15:42:44 +0100157 // The collapse method rely on the assumption that the third dimension of input buffer is 1
158 ARM_COMPUTE_ERROR_ON(window.z().end() != 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159
steniu01cfc6fe82017-07-27 15:42:44 +0100160 Window collapsed_window = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
161 Window slice = collapsed_window.first_slice_window_3D();
162
Gian Marco Iodice2eac5bd2017-08-14 14:22:23 +0100163 // Set static kernel arguments
164 unsigned int idx = 2 * num_arguments_per_3D_tensor();
165 _kernel.setArg<cl_uint>(idx++, _output->info()->strides_in_bytes()[3]);
166
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 do
168 {
169 // Set inputs
170 unsigned int idx = 0;
steniu01cfc6fe82017-07-27 15:42:44 +0100171 add_3D_tensor_argument(idx, _input, slice);
172 add_3D_tensor_argument(idx, _output, slice);
Gian Marcod7779da2017-11-22 14:46:28 +0000173 enqueue(queue, *this, slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 }
steniu01cfc6fe82017-07-27 15:42:44 +0100175 while(collapsed_window.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176}