blob: e73ac7df766e582b66a637862a811066cbf3b1cc [file] [log] [blame]
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +00001/*
2 * Copyright (c) 2018 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 "arm_compute/core/CL/kernels/CLWinogradInputTransformKernel.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/utils/misc/ShapeCalculator.h"
34#include "support/ToolchainSupport.h"
35
36using namespace arm_compute;
37
38namespace
39{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000040Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000041{
42 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000043
44 const PadStrideInfo conv_info = winograd_info.convolution_info;
45 const Size2D output_tile_size = winograd_info.output_tile_size;
46 const Size2D kernel_size = winograd_info.kernel_size;
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000047 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1, "Winograd input transform only supports unit strides");
Giorgio Arenafe5ef382018-04-17 10:14:10 +010048 ARM_COMPUTE_RETURN_ERROR_ON_MSG(kernel_size != Size2D(3U, 3U) && kernel_size != Size2D(5U, 5U), "Winograd input transform only supports 3x3 and 5x5 kernels");
Giorgio Arenac42f28d2018-04-26 11:33:05 +010049 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::NHWC && (output_tile_size != Size2D(4U, 4U) || kernel_size != Size2D(3U, 3U)));
Gian Marco Iodicee52a3002018-04-11 15:59:10 +010050 ARM_COMPUTE_RETURN_ERROR_ON_MSG(kernel_size == Size2D(3U, 3U) && output_tile_size != Size2D(2U, 2U)
51 && output_tile_size != Size2D(4U, 4U),
52 "Winograd input transform only supports 2x2 or 4x4 output tile for 3x3 kernels");
Giorgio Arenafe5ef382018-04-17 10:14:10 +010053 ARM_COMPUTE_RETURN_ERROR_ON_MSG(kernel_size == Size2D(5U, 5U) && output_tile_size != Size2D(4U, 4U), "Winograd input transform only supports 4x4 output tile for 5x5 kernels");
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000054 ARM_COMPUTE_UNUSED(conv_info);
55 ARM_COMPUTE_UNUSED(output_tile_size);
56 ARM_COMPUTE_UNUSED(kernel_size);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000057
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000058 // Validate configured output
59 if(output->total_size() != 0)
60 {
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000061 const TensorShape output_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000062
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000063 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
64 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
65 }
66
67 return Status{};
68}
69
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000070std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const WinogradInfo &winograd_info)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000071{
72 ARM_COMPUTE_UNUSED(output);
73 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000074 const PadStrideInfo conv_info = winograd_info.convolution_info;
75 const Size2D output_tile_size = winograd_info.output_tile_size;
76 const Size2D kernel_size = winograd_info.kernel_size;
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000077
Giorgio Arenac42f28d2018-04-26 11:33:05 +010078 unsigned int num_elems_read_per_iteration_x = 0;
79 unsigned int num_elems_read_per_iteration_y = 0;
80 unsigned int pad_left = 0;
81 unsigned int pad_top = 0;
82
83 if(input->data_layout() == DataLayout::NCHW)
84 {
85 num_elems_read_per_iteration_x = output_tile_size.width + kernel_size.width - 1;
86 num_elems_read_per_iteration_y = output_tile_size.height + kernel_size.height - 1;
87 pad_left = conv_info.pad_left();
88 pad_top = conv_info.pad_top();
89 }
90 else
91 {
92 num_elems_read_per_iteration_x = 1;
93 num_elems_read_per_iteration_y = output_tile_size.width + kernel_size.width - 1;
94 pad_top = 1;
95 }
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000096
97 Window win = calculate_max_window(*input, Steps(1, 1));
98
Giorgio Arenac42f28d2018-04-26 11:33:05 +010099 AccessWindowRectangle input_access(input, -pad_left, -pad_top, num_elems_read_per_iteration_x, num_elems_read_per_iteration_y);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000100
101 bool window_changed = update_window_and_padding(win, input_access);
102
103 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
104 return std::make_pair(err, win);
105}
106} // namespace
107
108CLWinogradInputTransformKernel::CLWinogradInputTransformKernel()
109 : _border_size(0), _input(nullptr), _output(nullptr), _num_tiles_x(0), _num_tiles_y(0), _step_z(1)
110{
111}
112
113BorderSize CLWinogradInputTransformKernel::border_size() const
114{
115 return _border_size;
116}
117
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000118void CLWinogradInputTransformKernel::configure(const ICLTensor *input, ICLTensor *output, const WinogradInfo &winograd_info)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000119{
120 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000121 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), winograd_info));
122
123 const PadStrideInfo conv_info = winograd_info.convolution_info;
124 const Size2D output_tile_size = winograd_info.output_tile_size;
125 const Size2D kernel_size = winograd_info.kernel_size;
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000126
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100127 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
128 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
129
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000130 // Compute number of elements to process in the X and Y direction
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100131 const int num_elements_x = input->info()->dimension(idx_w) - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right();
132 const int num_elements_y = input->info()->dimension(idx_h) - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom();
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000133
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100134 _input = input;
135 _output = output;
136 if(input->info()->data_layout() == DataLayout::NCHW)
137 {
138 // Check if we need to extend the right or bottom border
139 const unsigned int extra_border_right = ((num_elements_x % output_tile_size.width) == 0) ? 0u : static_cast<unsigned int>(output_tile_size.width - 1);
140 const unsigned int extra_border_bottom = ((num_elements_y % output_tile_size.height) == 0) ? 0u : static_cast<unsigned int>(output_tile_size.height - 1);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000141
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100142 _border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right() + extra_border_right, conv_info.pad_bottom() + extra_border_bottom, conv_info.pad_left());
143 }
144 else
145 {
146 _border_size = BorderSize(1U, 0U, 1U, 0);
147 }
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000148 _num_tiles_x = std::ceil(num_elements_x / static_cast<float>(output_tile_size.width));
149 _num_tiles_y = std::ceil(num_elements_y / static_cast<float>(output_tile_size.height));
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000150
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000151 const TensorShape output_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input->info(), winograd_info);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000152
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000153 // Output auto initialization if not yet initialized
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000154 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
155
156 ARM_COMPUTE_ERROR_ON(_num_tiles_x * _num_tiles_y != static_cast<int>(output->info()->dimension(1)));
157
158 CLBuildOptions build_opts;
159 build_opts.add_option("-DNUM_TILES_X=" + support::cpp11::to_string(_num_tiles_x));
160 build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
161 build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
162
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100163 if(input->info()->data_layout() == DataLayout::NHWC)
164 {
165 build_opts.add_option("-DSRC_DIM_1=" + support::cpp11::to_string(_input->info()->dimension(1)));
166 build_opts.add_option("-DSRC_DIM_2=" + support::cpp11::to_string(_input->info()->dimension(2)));
167 }
168
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000169 // Create kernel
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000170 std::string kernel_name = "winograd_input_transform_" + output_tile_size.to_string() + "_" + kernel_size.to_string();
171
172 // Check optimized kernel if output_dims == 2x2
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100173 if(output_tile_size == Size2D(2U, 2U))
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000174 {
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100175 _step_z = (_input->info()->dimension(2) % 2) != 0 ? 1 : 2;
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000176 }
177
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100178 _lws_hint = cl::NDRange(1, 1, 8);
179
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000180 // Append stepz and data layout
181 kernel_name += "_stepz";
182 kernel_name += support::cpp11::to_string(_step_z);
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100183 kernel_name += "_" + lower_string(string_from_data_layout(input->info()->data_layout()));
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000184
185 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
186
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000187 // Create window and update padding
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000188 auto win_config = validate_and_configure_window(input->info(), output->info(), winograd_info);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000189 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
190 ICLKernel::configure(win_config.second);
191
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000192 _config_id = kernel_name;
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000193 _config_id += support::cpp11::to_string(input->info()->dimension(0));
194 _config_id += "_";
195 _config_id += support::cpp11::to_string(input->info()->dimension(1));
196 _config_id += "_";
197 _config_id += support::cpp11::to_string(input->info()->dimension(2));
198 _config_id += "_";
199 _config_id += support::cpp11::to_string(conv_info.pad_left());
200 _config_id += "_";
201 _config_id += support::cpp11::to_string(conv_info.pad_top());
202}
203
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000204Status CLWinogradInputTransformKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000205{
206 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000207 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, winograd_info));
208 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), winograd_info).first);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000209
210 return Status{};
211}
212
213void CLWinogradInputTransformKernel::run(const Window &window, cl::CommandQueue &queue)
214{
215 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
216 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
217
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100218 const size_t idx_w = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::WIDTH);
219 const size_t idx_h = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::HEIGHT);
220 const size_t idx_c = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000221
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100222 Window slice = window.first_slice_window_3D();
223 slice.set(idx_w, Window::Dimension(0, _num_tiles_x, 1));
224 slice.set(idx_h, Window::Dimension(0, _num_tiles_y, 1));
225
226 ARM_COMPUTE_ERROR_ON(((slice[idx_c].end() - slice[idx_c].start()) % _step_z) != 0);
227 slice.set(idx_c, Window::Dimension(slice[idx_c].start(), slice[idx_c].end(), _step_z));
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000228
229 do
230 {
231 unsigned int idx = 0;
232 add_3D_tensor_argument(idx, _input, slice);
233 add_3D_tensor_argument(idx, _output, slice);
234
235 enqueue(queue, *this, slice, _lws_hint);
236 }
237 while(window.slide_window_slice_3D(slice));
238}