Compute Library
 22.11
ClWinogradInputTransformKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2022 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  */
25 
30 #include "arm_compute/core/Error.h"
32 #include "arm_compute/core/Types.h"
33 #include "arm_compute/core/Utils.h"
36 #include "src/core/CL/CLValidate.h"
39 #include "support/Cast.h"
40 #include "support/StringSupport.h"
41 
42 namespace arm_compute
43 {
44 namespace opencl
45 {
46 namespace kernels
47 {
48 namespace
49 {
50 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info)
51 {
54 
55  const PadStrideInfo conv_info = winograd_info.convolution_info;
56  const Size2D output_tile_size = winograd_info.output_tile_size;
57  const Size2D kernel_size = winograd_info.kernel_size;
58  ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1, "Winograd input transform only supports unit strides");
59  ARM_COMPUTE_RETURN_ERROR_ON_MSG(!cl_winograd_convolution_layer_supported(output_tile_size, kernel_size, input->data_layout()), "Winograd input transform not supported");
60 
61  ARM_COMPUTE_UNUSED(conv_info);
62  ARM_COMPUTE_UNUSED(output_tile_size);
63  ARM_COMPUTE_UNUSED(kernel_size);
64 
65  // Validate configured output
66  if(output->total_size() != 0)
67  {
68  const TensorShape output_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
69 
72  }
73 
74  return Status{};
75 }
76 
77 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const WinogradInfo &winograd_info)
78 {
79  ARM_COMPUTE_UNUSED(output);
80  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
81 
82  bool window_changed = false;
83  Window win = calculate_max_window(*input, Steps(1, 1));
84 
85  if(input->data_layout() == DataLayout::NCHW)
86  {
87  const PadStrideInfo conv_info = winograd_info.convolution_info;
88  const Size2D output_tile_size = winograd_info.output_tile_size;
89  const Size2D kernel_size = winograd_info.kernel_size;
90 
91  unsigned int num_elems_read_per_iteration_x = output_tile_size.width + kernel_size.width - 1;
92  unsigned int num_elems_read_per_iteration_y = output_tile_size.height + kernel_size.height - 1;
93 
94  AccessWindowRectangle input_access(input, -conv_info.pad_left(), -conv_info.pad_top(), num_elems_read_per_iteration_x, num_elems_read_per_iteration_y);
95  window_changed = update_window_and_padding(win, input_access);
96  }
97 
98  Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
99  return std::make_pair(err, win);
100 }
101 } // namespace
102 
104 {
105  _type = CLKernelType::WINOGRAD;
106 }
107 
109 {
110  return _border_size;
111 }
112 
114 {
116  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, winograd_info));
117 
118  auto padding_info = get_padding_info({ src, dst });
119 
120  const PadStrideInfo conv_info = winograd_info.convolution_info;
121  const Size2D output_tile_size = winograd_info.output_tile_size;
122  const Size2D kernel_size = winograd_info.kernel_size;
123 
124  _data_layout = src->data_layout();
125 
126  const size_t idx_w = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
127  const size_t idx_h = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
128 
129  // Compute the number of output tiles along the x and y direction of size "output_tile_size"
130  const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(src->dimension(idx_w), src->dimension(idx_h)),
131  kernel_size,
132  output_tile_size,
133  conv_info);
134 
135  _num_tiles_x = num_tiles.width;
136  _num_tiles_y = num_tiles.height;
137 
139 
140  // Output auto initialization if not yet initialized
141  auto_init_if_empty(*dst, src->clone()->set_tensor_shape(output_shape));
142 
143  ARM_COMPUTE_ERROR_ON(_num_tiles_x * _num_tiles_y != static_cast<int>(dst->dimension(1)));
144  const size_t total_batches = src->tensor_shape().total_size_upper(3);
145 
146  CLBuildOptions build_opts;
147  if(_data_layout == DataLayout::NHWC)
148  {
149  build_opts.add_option("-DNHWC");
150  _src_width = src->dimension(idx_w);
151  _src_height = src->dimension(idx_h);
152  build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
153  build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
154  build_opts.add_option("-DOUTPUT_TILE_W=" + support::cpp11::to_string(output_tile_size.width));
155  build_opts.add_option("-DOUTPUT_TILE_H=" + support::cpp11::to_string(output_tile_size.height));
156  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
157  build_opts.add_option_if(winograd_info.kernel_size.height == 1, "-DWINOGRAD_INPUT_TRANSFORM_HORIZONTAL");
158  build_opts.add_option_if(winograd_info.kernel_size.width == 1, "-DWINOGRAD_INPUT_TRANSFORM_VERTICAL");
159  }
160  else
161  {
162  build_opts.add_option("-DNUM_TILES_X=" + support::cpp11::to_string(_num_tiles_x));
163  build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
164  build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
165  build_opts.add_option("-DOUTPUT_TILE_W=" + support::cpp11::to_string(output_tile_size.width));
166  build_opts.add_option("-DOUTPUT_TILE_H=" + support::cpp11::to_string(output_tile_size.height));
167  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
168  build_opts.add_option_if(winograd_info.kernel_size.height == 1, "-DWINOGRAD_INPUT_TRANSFORM_HORIZONTAL");
169  build_opts.add_option_if(winograd_info.kernel_size.width == 1, "-DWINOGRAD_INPUT_TRANSFORM_VERTICAL");
170  build_opts.add_option_if(total_batches > 1, "-DSRC_DEPTH=" + support::cpp11::to_string(src->dimension(2)));
171  }
172 
173  // Create kernel
174  std::string kernel_name = "winograd_input_transform_" + output_tile_size.to_string() + "_" + kernel_size.to_string();
175 
176  // Get the maximum dimension from the tile size
177  const unsigned int tile_max_dim = std::max(output_tile_size.width, output_tile_size.height);
178 
179  // Check optimized kernel if output_dims == 2x2
180  if((tile_max_dim == 2) && (_data_layout == DataLayout::NCHW))
181  {
182  _step_z = (src->dimension(2) % 2) != 0 ? 1 : 2;
183  }
184 
185  // Append stepz and data layout
186  kernel_name += "_stepz";
187  kernel_name += support::cpp11::to_string(_step_z);
188  kernel_name += "_" + lower_string(string_from_data_layout(_data_layout));
189 
190  // A macro guard to compile ONLY the kernel of interest
191  build_opts.add_option("-D" + upper_string(kernel_name));
192  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
193 
194  // Create window and update padding
195  auto win_config = validate_and_configure_window(src, dst, winograd_info);
196  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
197  IClKernel::configure_internal(win_config.second, cl::NDRange(1, 1, 8));
198 
199  _border_size = BorderSize(src->padding());
200 
202 
203  _config_id = kernel_name;
204  _config_id += support::cpp11::to_string(src->dimension(0));
205  _config_id += "_";
206  _config_id += support::cpp11::to_string(src->dimension(1));
207  _config_id += "_";
208  _config_id += support::cpp11::to_string(src->dimension(2));
209  _config_id += "_";
210  _config_id += support::cpp11::to_string(conv_info.pad_left());
211  _config_id += "_";
212  _config_id += support::cpp11::to_string(conv_info.pad_top());
213  _config_id += "_";
214  _config_id += lower_string(string_from_data_layout(_data_layout));
215 }
216 
218 {
220  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, winograd_info));
221  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get(), winograd_info).first);
222  return Status{};
223 }
224 
225 void ClWinogradInputTransformKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
226 {
229 
230  auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
231  auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
232 
233  const size_t idx_w = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
234  const size_t idx_h = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
235  const size_t idx_c = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
236  const size_t total_batches = window.shape().total_size_upper(3);
237 
238  // Collapse window
239  Window window_collapsed = window.collapse_if_possible(IClKernel::window(), Window::DimZ);
240 
241  if(_data_layout == DataLayout::NHWC)
242  {
243  Window slice = window_collapsed.first_slice_window_3D();
244  slice.set(1, Window::Dimension(0, _num_tiles_x * _num_tiles_y, 1));
245  slice.set(2, Window::Dimension(0, total_batches, 1));
246 
247  unsigned int idx = 0;
248  add_4D_tensor_argument(idx, src, slice);
249  add_4D_tensor_argument(idx, dst, slice);
250  _kernel.setArg<cl_uint>(idx++, _src_width);
251  _kernel.setArg<cl_uint>(idx++, _src_height);
252  _kernel.setArg<cl_uint>(idx++, _num_tiles_x);
253  _kernel.setArg<cl_uint>(idx++, _num_tiles_y);
254  enqueue(queue, *this, slice, lws_hint());
255  }
256  else
257  {
258  Window slice = window_collapsed.first_slice_window_3D();
259  slice.set(idx_w, Window::Dimension(0, _num_tiles_x, 1));
260  slice.set(idx_h, Window::Dimension(0, _num_tiles_y, 1));
261 
262  ARM_COMPUTE_ERROR_ON(((slice[idx_c].end() - slice[idx_c].start()) % _step_z) != 0);
263  slice.set(idx_c, Window::Dimension(slice[idx_c].start(), slice[idx_c].end(), _step_z));
264 
265  unsigned int idx = 2 * num_arguments_per_3D_tensor();
266  _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src->info()->strides_in_bytes()[3]));
267  _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(dst->info()->strides_in_bytes()[3]));
268 
269  do
270  {
271  unsigned int idx = 0;
272  add_3D_tensor_argument(idx, src, slice);
273  add_3D_tensor_argument(idx, dst, slice);
274 
275  enqueue(queue, *this, slice, lws_hint());
276  }
277  while(window_collapsed.slide_window_slice_3D(slice));
278  }
279 }
280 } // namespace kernels
281 } // namespace opencl
282 } // namespace arm_compute
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:35
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const WinogradInfo &winograd_info)
Static function to check if given info will lead to a valid configuration.
Shape of a tensor.
Definition: TensorShape.h:39
TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Calculate the winograd input transform shape.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Container for 2D border size.
Definition: Types.h:282
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:32
const StringSet & options() const
Gets the current options list set.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:383
Winograd information.
Definition: Types.h:2617
PadStrideInfo convolution_info
Convolution info (Pads, strides,...)
Definition: Types.h:2635
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
size_t total_size_upper(size_t dimension) const
Collapses given dimension and above.
Definition: TensorShape.h:182
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
Size2D compute_winograd_convolution_tiles(const Size2D &in_dims, const Size2D &kernel_size, const Size2D &output_tile_size, const PadStrideInfo &conv_info)
Calculate the number of output tiles required by Winograd Convolution layer.
Definition: Helpers.h:227
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
Status class.
Definition: Error.h:52
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:353
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:284
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:226
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:91
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
void add_option(std::string option)
Adds option to the existing build option list.
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
std::string upper_string(const std::string &val)
Raise a given string to upper case.
Definition: Utils.cpp:360
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:404
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:313
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Size2D output_tile_size
Width and height of the output tile.
Definition: Types.h:2632
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:39
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
Padding and stride information class.
Definition: Types.h:669
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
virtual PaddingSize padding() const =0
Padding of tensor.
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:349
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
bool has_padding_changed(const std::unordered_map< const ITensorInfo *, PaddingSize > &padding_map)
Check if the previously stored padding info has changed after configuring a kernel.
Definition: Utils.cpp:603
Num samples, channels, height, width.
CLCompileContext class.
Convolution using Winograd.
void configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const WinogradInfo &winograd_info)
Set the input and output of the kernel.
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:64
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
Definition: Utils.cpp:123
#define ARM_COMPUTE_CREATE_ERROR(error_code, msg)
Creates an error with a given message.
Definition: Error.h:159
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:90
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
Num samples, height, width, channels.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo *> infos)
Stores padding information before configuring a kernel.
Definition: Utils.cpp:588
Wrapper to configure the Khronos OpenCL C++ header.
TensorShape shape() const
Return the shape of the window in number of steps.
Definition: Window.inl:284
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
Size2D kernel_size
Width and height of the kernel.
Definition: Types.h:2633
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
BorderSize border_size() const override
The size of the border for that kernel.
bool cl_winograd_convolution_layer_supported(const Size2D &output_tile, const Size2D &kernel_size, DataLayout data_layout)
This function checks if the Winograd configuration (defined through the output tile, kernel size and the data layout) is supported on OpenCL.
Definition: CLHelpers.cpp:290
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:305
std::string kernel_name
void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 4D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:236
Describe a multidimensional execution window.
Definition: Window.h:39
void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
std::string to_string() const
Definition: Size2D.cpp:29