blob: 6b1b86a777880cca5d8c0e5d8ce5ce5efb28d6d6 [file] [log] [blame]
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +00003 *
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
Giorgio Arena149fdf32018-07-04 17:03:33 +010026#include "arm_compute/core/AccessWindowStatic.h"
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000027#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +010029#include "arm_compute/core/CL/CLValidate.h"
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000030#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/CL/OpenCL.h"
32#include "arm_compute/core/Error.h"
33#include "arm_compute/core/Helpers.h"
34#include "arm_compute/core/Types.h"
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010035#include "arm_compute/core/Utils.h"
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000036#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000038
39using namespace arm_compute;
40
41namespace
42{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000043Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000044{
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +010045 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16);
46 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +000047
48 const PadStrideInfo conv_info = winograd_info.convolution_info;
49 const Size2D output_tile_size = winograd_info.output_tile_size;
50 const Size2D kernel_size = winograd_info.kernel_size;
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000051 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1, "Winograd input transform only supports unit strides");
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010052 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!cl_winograd_convolution_layer_supported(output_tile_size, kernel_size, input->data_layout()), "Winograd input transform not supported");
53
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);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000074
Giorgio Arena149fdf32018-07-04 17:03:33 +010075 bool window_changed = false;
76 Window win = calculate_max_window(*input, Steps(1, 1));
Giorgio Arenac42f28d2018-04-26 11:33:05 +010077
78 if(input->data_layout() == DataLayout::NCHW)
79 {
Giorgio Arena149fdf32018-07-04 17:03:33 +010080 const PadStrideInfo conv_info = winograd_info.convolution_info;
81 const Size2D output_tile_size = winograd_info.output_tile_size;
82 const Size2D kernel_size = winograd_info.kernel_size;
83
84 unsigned int num_elems_read_per_iteration_x = output_tile_size.width + kernel_size.width - 1;
85 unsigned int num_elems_read_per_iteration_y = output_tile_size.height + kernel_size.height - 1;
86
87 AccessWindowRectangle input_access(input, -conv_info.pad_left(), -conv_info.pad_top(), num_elems_read_per_iteration_x, num_elems_read_per_iteration_y);
88 window_changed = update_window_and_padding(win, input_access);
Giorgio Arenac42f28d2018-04-26 11:33:05 +010089 }
90 else
91 {
Giorgio Arena149fdf32018-07-04 17:03:33 +010092 AccessWindowStatic input_access(input, 0, -1, input->dimension(0), input->dimension(1) + 1);
93 window_changed = update_window_and_padding(win, input_access);
Giorgio Arenac42f28d2018-04-26 11:33:05 +010094 }
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000095
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000096 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
97 return std::make_pair(err, win);
98}
99} // namespace
100
101CLWinogradInputTransformKernel::CLWinogradInputTransformKernel()
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000102 : _border_size(0), _input(nullptr), _output(nullptr), _data_layout(DataLayout::UNKNOWN), _num_tiles_x(0), _num_tiles_y(0), _step_z(1)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000103{
104}
105
106BorderSize CLWinogradInputTransformKernel::border_size() const
107{
108 return _border_size;
109}
110
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000111void CLWinogradInputTransformKernel::configure(const ICLTensor *input, ICLTensor *output, const WinogradInfo &winograd_info)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000112{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100113 configure(CLKernelLibrary::get().get_compile_context(), input, output, winograd_info);
114}
115
Manuel Bottini2803f702020-04-21 16:20:03 +0100116void CLWinogradInputTransformKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const WinogradInfo &winograd_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100117{
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000118 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000119 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), winograd_info));
120
121 const PadStrideInfo conv_info = winograd_info.convolution_info;
122 const Size2D output_tile_size = winograd_info.output_tile_size;
123 const Size2D kernel_size = winograd_info.kernel_size;
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000124
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000125 _data_layout = input->info()->data_layout();
126
127 const size_t idx_w = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
128 const size_t idx_h = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100129
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
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000134 if(_data_layout == DataLayout::NCHW)
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100135 {
136 // Check if we need to extend the right or bottom border
137 const unsigned int extra_border_right = ((num_elements_x % output_tile_size.width) == 0) ? 0u : static_cast<unsigned int>(output_tile_size.width - 1);
138 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 +0000139
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100140 _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());
141 }
142 else
143 {
144 _border_size = BorderSize(1U, 0U, 1U, 0);
145 }
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100146
147 // Compute the number of output tiles along the x and y direction of size "output_tile_size"
148 const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input->info()->dimension(idx_w), input->info()->dimension(idx_h)),
149 kernel_size,
150 output_tile_size,
151 conv_info);
152
153 _input = input;
154 _output = output;
155 _num_tiles_x = num_tiles.width;
156 _num_tiles_y = num_tiles.height;
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000157
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000158 const TensorShape output_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input->info(), winograd_info);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000159
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000160 // Output auto initialization if not yet initialized
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000161 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
162
163 ARM_COMPUTE_ERROR_ON(_num_tiles_x * _num_tiles_y != static_cast<int>(output->info()->dimension(1)));
Georgios Pinitasffb57a02018-10-29 18:01:52 +0000164 const size_t total_batches = input->info()->tensor_shape().total_size_upper(3);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000165
166 CLBuildOptions build_opts;
167 build_opts.add_option("-DNUM_TILES_X=" + support::cpp11::to_string(_num_tiles_x));
168 build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
169 build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100170 build_opts.add_option("-DOUTPUT_TILE_W=" + support::cpp11::to_string(output_tile_size.width));
171 build_opts.add_option("-DOUTPUT_TILE_H=" + support::cpp11::to_string(output_tile_size.height));
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100172 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100173 build_opts.add_option_if(winograd_info.kernel_size.height == 1, "-DWINOGRAD_INPUT_TRANSFORM_HORIZONTAL");
174 build_opts.add_option_if(winograd_info.kernel_size.width == 1, "-DWINOGRAD_INPUT_TRANSFORM_VERTICAL");
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000175 if(_data_layout == DataLayout::NHWC)
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100176 {
Georgios Pinitasffb57a02018-10-29 18:01:52 +0000177 build_opts.add_option_if(total_batches > 1, "-DNUM_TILES_Y=" + support::cpp11::to_string(_num_tiles_y));
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100178 build_opts.add_option("-DSRC_DIM_1=" + support::cpp11::to_string(_input->info()->dimension(1)));
179 build_opts.add_option("-DSRC_DIM_2=" + support::cpp11::to_string(_input->info()->dimension(2)));
180 }
Georgios Pinitasc55beee2018-10-23 15:23:23 +0100181 else
182 {
Georgios Pinitasffb57a02018-10-29 18:01:52 +0000183 build_opts.add_option_if(total_batches > 1, "-DSRC_DEPTH=" + support::cpp11::to_string(_input->info()->dimension(2)));
Georgios Pinitasc55beee2018-10-23 15:23:23 +0100184 }
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100185
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000186 // Create kernel
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000187 std::string kernel_name = "winograd_input_transform_" + output_tile_size.to_string() + "_" + kernel_size.to_string();
188
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100189 // Get the maximum dimension from the tile size
190 const unsigned int tile_max_dim = std::max(output_tile_size.width, output_tile_size.height);
191
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000192 // Check optimized kernel if output_dims == 2x2
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000193 if((tile_max_dim == 2) && (_data_layout == DataLayout::NCHW))
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000194 {
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100195 _step_z = (_input->info()->dimension(2) % 2) != 0 ? 1 : 2;
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000196 }
197
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000198 // Append stepz and data layout
199 kernel_name += "_stepz";
200 kernel_name += support::cpp11::to_string(_step_z);
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000201 kernel_name += "_" + lower_string(string_from_data_layout(_data_layout));
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000202
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100203 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000204
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000205 // Create window and update padding
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000206 auto win_config = validate_and_configure_window(input->info(), output->info(), winograd_info);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000207 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100208 ICLKernel::configure_internal(win_config.second, cl::NDRange(1, 1, 8));
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000209
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000210 _config_id = kernel_name;
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000211 _config_id += support::cpp11::to_string(input->info()->dimension(0));
212 _config_id += "_";
213 _config_id += support::cpp11::to_string(input->info()->dimension(1));
214 _config_id += "_";
215 _config_id += support::cpp11::to_string(input->info()->dimension(2));
216 _config_id += "_";
217 _config_id += support::cpp11::to_string(conv_info.pad_left());
218 _config_id += "_";
219 _config_id += support::cpp11::to_string(conv_info.pad_top());
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100220 _config_id += "_";
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000221 _config_id += lower_string(string_from_data_layout(_data_layout));
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000222}
223
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000224Status CLWinogradInputTransformKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000225{
226 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000227 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, winograd_info));
228 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 +0000229
230 return Status{};
231}
232
233void CLWinogradInputTransformKernel::run(const Window &window, cl::CommandQueue &queue)
234{
235 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
236 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
237
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000238 const size_t idx_w = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
239 const size_t idx_h = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
240 const size_t idx_c = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
241 const size_t total_batches = window.shape().total_size_upper(3);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000242
Georgios Pinitasc55beee2018-10-23 15:23:23 +0100243 // Collapse window
244 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
245
246 Window slice = window_collapsed.first_slice_window_3D();
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100247 slice.set(idx_w, Window::Dimension(0, _num_tiles_x, 1));
248 slice.set(idx_h, Window::Dimension(0, _num_tiles_y, 1));
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000249 if(_data_layout == DataLayout::NHWC)
Georgios Pinitasc55beee2018-10-23 15:23:23 +0100250 {
251 slice.set(idx_h, Window::Dimension(0, _num_tiles_y * total_batches, 1));
252 }
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100253
254 ARM_COMPUTE_ERROR_ON(((slice[idx_c].end() - slice[idx_c].start()) % _step_z) != 0);
255 slice.set(idx_c, Window::Dimension(slice[idx_c].start(), slice[idx_c].end(), _step_z));
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000256
Georgios Pinitasc55beee2018-10-23 15:23:23 +0100257 unsigned int idx = 2 * num_arguments_per_3D_tensor();
258 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input->info()->strides_in_bytes()[3]));
259 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[3]));
260
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000261 do
262 {
263 unsigned int idx = 0;
264 add_3D_tensor_argument(idx, _input, slice);
265 add_3D_tensor_argument(idx, _output, slice);
266
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100267 enqueue(queue, *this, slice, lws_hint());
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000268 }
Georgios Pinitasc55beee2018-10-23 15:23:23 +0100269 while(window_collapsed.slide_window_slice_3D(slice));
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000270}