Compute Library
 23.11
CLSpaceToBatchLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021, 2023 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 
31 #include "src/core/CL/CLValidate.h"
34 #include "support/StringSupport.h"
35 
37 
38 namespace arm_compute
39 {
40 namespace
41 {
42 Status validate_arguments(const ITensorInfo *input,
43  const ITensorInfo *block_info,
44  const ITensorInfo *paddings,
45  const ITensorInfo *output)
46 {
47  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_info, paddings, output);
50  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
51  ARM_COMPUTE_RETURN_ERROR_ON(block_info->num_dimensions() > 1);
52  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(block_info->tensor_shape(), TensorShape{2});
53  ARM_COMPUTE_RETURN_ERROR_ON(paddings->num_dimensions() > 2);
54  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(paddings->tensor_shape(), TensorShape{2, 2});
55 
56  // Validate output if initialized
57  if (output->total_size() != 0)
58  {
59  const DataLayout data_layout = input->data_layout();
61  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] != output->tensor_shape()[idx_channel]);
64  }
65 
66  return Status{};
67 }
68 Status validate_arguments_static(const ITensorInfo *input,
69  const int block_shape_x,
70  const int block_shape_y,
71  const Size2D &padding_left,
72  const Size2D &padding_right,
73  const ITensorInfo *output)
74 {
77  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
78  ARM_COMPUTE_RETURN_ERROR_ON(block_shape_x < 1 || block_shape_y < 1);
79 
80  // Validate output if initialized
81  if (output->total_size() != 0)
82  {
83  TensorShape expected_output_shape = misc::shape_calculator::compute_space_to_batch_shape(
84  input, block_shape_x, block_shape_y, padding_left, padding_right);
85  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), expected_output_shape);
88  }
89 
90  return Status{};
91 }
92 } // namespace
93 
95  : _input(nullptr), _block_shape(nullptr), _paddings(nullptr), _output(nullptr)
96 {
98 }
99 
101  const ICLTensor *block_shape,
102  const ICLTensor *paddings,
103  ICLTensor *output)
104 {
105  configure(CLKernelLibrary::get().get_compile_context(), input, block_shape, paddings, output);
106 }
107 
109  const ICLTensor *input,
110  const ICLTensor *block_shape,
111  const ICLTensor *paddings,
112  ICLTensor *output)
113 {
114  ARM_COMPUTE_ERROR_ON_NULLPTR(input, block_shape, paddings, output);
116  validate_arguments(input->info(), block_shape->info(), paddings->info(), output->info()));
117  auto padding_info = get_padding_info({input, block_shape, paddings, output});
118 
119  _input = input;
120  _block_shape = block_shape;
121  _paddings = paddings;
122  _output = output;
123 
124  const DataLayout data_layout = input->info()->data_layout();
128 
129  // Create kernel
130  CLBuildOptions build_opts;
131  build_opts.add_option("-DDATA_TYPE=" +
133  build_opts.add_option("-DWIDTH_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_width)));
134  build_opts.add_option("-DHEIGHT_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_height)));
135  build_opts.add_option("-DBATCH_SIZE=" + support::cpp11::to_string(output->info()->dimension(idx_batch)));
136  build_opts.add_option("-DWIDTH_IN=" + support::cpp11::to_string(input->info()->dimension(idx_width)));
137  build_opts.add_option("-DHEIGHT_IN=" + support::cpp11::to_string(input->info()->dimension(idx_height)));
138  build_opts.add_option("-DBATCH_IN=" + support::cpp11::to_string(input->info()->dimension(idx_batch)));
139  _kernel = create_kernel(compile_context,
140  "space_to_batch_" + lower_string(string_from_data_layout(input->info()->data_layout())),
141  build_opts.options());
142 
143  // Configure kernel window
144  Window win = calculate_max_window(*output->info(), Steps());
145  ICLKernel::configure_internal(win);
147 }
148 
150  const int block_shape_x,
151  const int block_shape_y,
152  const Size2D &padding_left,
153  const Size2D &padding_right,
154  ICLTensor *output)
155 {
156  configure(CLKernelLibrary::get().get_compile_context(), input, block_shape_x, block_shape_y, padding_left,
157  padding_right, output);
158 }
159 
161  const ICLTensor *input,
162  const int block_shape_x,
163  const int block_shape_y,
164  const Size2D &padding_left,
165  const Size2D &padding_right,
166  ICLTensor *output)
167 {
169 
171  input->info(), block_shape_x, block_shape_y, padding_left, padding_right);
172  auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(),
173  input->info()->quantization_info());
174 
175  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, padding_left,
176  padding_right, output->info()));
177 
178  _input = input;
179  _output = output;
180 
181  const DataLayout data_layout = input->info()->data_layout();
185 
186  // Create kernel
187  CLBuildOptions build_opts;
188  build_opts.add_option("-DDATA_TYPE=" +
190  build_opts.add_option("-DWIDTH_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_width)));
191  build_opts.add_option("-DHEIGHT_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_height)));
192  build_opts.add_option("-DBATCH_SIZE=" + support::cpp11::to_string(output->info()->dimension(idx_batch)));
193  build_opts.add_option("-DWIDTH_IN=" + support::cpp11::to_string(input->info()->dimension(idx_width)));
194  build_opts.add_option("-DHEIGHT_IN=" + support::cpp11::to_string(input->info()->dimension(idx_height)));
195  build_opts.add_option("-DBATCH_IN=" + support::cpp11::to_string(input->info()->dimension(idx_batch)));
196  build_opts.add_option("-DBLOCK_SHAPE_X=" + support::cpp11::to_string(block_shape_x));
197  build_opts.add_option("-DBLOCK_SHAPE_Y=" + support::cpp11::to_string(block_shape_y));
198  build_opts.add_option("-DPAD_LEFT_X=" + support::cpp11::to_string(padding_left.x()));
199  build_opts.add_option("-DPAD_RIGHT_X=" + support::cpp11::to_string(padding_right.x()));
200  build_opts.add_option("-DPAD_LEFT_Y=" + support::cpp11::to_string(padding_left.y()));
201  build_opts.add_option("-DPAD_RIGHT_Y=" + support::cpp11::to_string(padding_right.y()));
202  _kernel = create_kernel(
203  compile_context, "space_to_batch_static_" + lower_string(string_from_data_layout(input->info()->data_layout())),
204  build_opts.options());
205 
206  // Configure kernel window
207  Window win = calculate_max_window(*output->info(), Steps());
208  ICLKernel::configure_internal(win);
209 }
210 
212  const ITensorInfo *block_shape,
213  const ITensorInfo *paddings,
214  const ITensorInfo *output)
215 {
216  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, paddings, output));
217  return Status{};
218 }
220  const int block_shape_x,
221  const int block_shape_y,
222  const Size2D &padding_left,
223  const Size2D &padding_right,
224  const ITensorInfo *output)
225 {
227  validate_arguments_static(input, block_shape_x, block_shape_y, padding_left, padding_right, output));
228  return Status{};
229 }
230 
231 void CLSpaceToBatchLayerKernel::run(const Window &window, cl::CommandQueue &queue)
232 {
235 
236  Window slice_out = window.first_slice_window_3D();
237 
238  Window slice_in = window.first_slice_window_4D();
239  slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
240  slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
241  slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
242  slice_in.set(3, Window::Dimension(0, 0, 0));
243 
244  Window vector_slice = window.first_slice_window_1D();
245  vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
246 
247  Window padding_slice = window.first_slice_window_2D();
248  padding_slice.set(Window::DimX, Window::Dimension(0, 0, 0));
249  padding_slice.set(Window::DimY, Window::Dimension(0, 0, 0));
250 
251  int batch_id = 0;
252  do
253  {
254  unsigned int idx = 0;
255  const bool cond = (_paddings != nullptr && _block_shape != nullptr);
256  add_4D_tensor_argument(idx, _input, slice_in);
257  add_2D_tensor_argument_if(cond, idx, _paddings, padding_slice);
258  add_1D_tensor_argument_if(cond, idx, _block_shape, vector_slice);
259 
260  add_argument(idx, batch_id);
261  add_3D_tensor_argument(idx, _output, slice_out);
262  enqueue(queue, *this, slice_out, lws_hint());
263  ++batch_id;
264  } while (window.slide_window_slice_3D(slice_out));
265 }
266 } // namespace arm_compute
arm_compute::Steps
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
arm_compute::support::cpp11::to_string
std::string to_string(T &&value)
Convert integer and float values to string.
Definition: StringSupport.h:168
arm_compute::Size2D::y
size_t y() const
Semantic accessor for height as y.
Definition: Size2D.h:82
StringSupport.h
CLSpaceToBatchLayerKernel.h
ICLTensor.h
arm_compute::test::validation::idx_height
const int idx_height
Definition: Scale.cpp:263
arm_compute::DataLayout
DataLayout
[DataLayout enum definition]
Definition: CoreTypes.h:110
arm_compute::DataLayoutDimension::CHANNEL
@ CHANNEL
channel
arm_compute::CLBuildOptions::options
const StringSet & options() const
Gets the current options list set.
Definition: CLCompileContext.cpp:72
arm_compute::calculate_max_window
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Definition: WindowHelpers.cpp:29
arm_compute::test::validation::output_shape
TensorShape output_shape
Definition: LSTMLayerQuantized.cpp:469
arm_compute::TensorShape
Shape of a tensor.
Definition: TensorShape.h:39
arm_compute::lower_string
std::string lower_string(const std::string &val)
Lower a given string.
Definition: StringUtils.cpp:38
arm_compute::cpu::kernels::validate_arguments
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
Definition: CpuDirectConv2dKernel.cpp:57
ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:1079
arm_compute::Window::DimX
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
arm_compute::ICLTensor
Interface for OpenCL tensor.
Definition: ICLTensor.h:41
arm_compute::Size2D
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
arm_compute::test::validation::idx_width
const int idx_width
Definition: Scale.cpp:262
arm_compute::Window::first_slice_window_1D
Window first_slice_window_1D() const
First 1D slice of the window.
Definition: Window.h:290
arm_compute::CLSpaceToBatchLayerKernel::configure
void configure(const ICLTensor *input, const ICLTensor *block_shape, const ICLTensor *paddings, ICLTensor *output)
Initialise the kernel's inputs and output.
Definition: CLSpaceToBatchLayerKernel.cpp:100
arm_compute::CLKernelLibrary::get
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Definition: CLKernelLibrary.cpp:41
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:677
arm_compute::DataLayoutDimension::WIDTH
@ WIDTH
width
ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:952
arm_compute::cpu::data_layout
constexpr auto data_layout
Definition: impl.h:36
StringUtils.h
ARM_COMPUTE_RETURN_ON_ERROR
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:205
arm_compute::ITensorInfo::dimension
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
arm_compute::ITensor::info
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
ARM_COMPUTE_ERROR_ON
#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
arm_compute::data_size_from_type
size_t data_size_from_type(DataType data_type)
The size in bytes of the data type.
Definition: DataTypeUtils.h:38
arm_compute::ICLKernel::add_2D_tensor_argument_if
void add_2D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx ...
Definition: ICLKernel.h:221
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
arm_compute::CLBuildOptions::add_option
void add_option(std::string option)
Adds option to the existing build option list.
Definition: CLCompileContext.cpp:41
arm_compute::CLSpaceToBatchLayerKernel::CLSpaceToBatchLayerKernel
CLSpaceToBatchLayerKernel()
Default constructor.
Definition: CLSpaceToBatchLayerKernel.cpp:94
arm_compute::CLCompileContext
CLCompileContext class.
Definition: CLCompileContext.h:204
arm_compute::CLSpaceToBatchLayerKernel::validate
static Status validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *paddings, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of CLSpaceToBatchLayerKerne...
Definition: CLSpaceToBatchLayerKernel.cpp:211
arm_compute::DataLayoutDimension::HEIGHT
@ HEIGHT
height
ARM_COMPUTE_RETURN_ERROR_ON
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:298
arm_compute::auto_init_if_empty
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...
Definition: AutoConfiguration.h:43
arm_compute::create_kernel
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:409
arm_compute::Status
Status class.
Definition: Error.h:52
WindowHelpers.h
ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:203
arm_compute::ICLKernel::add_argument
void add_argument(unsigned int &idx, T value)
Add the passed parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:362
arm_compute::Window::slide_window_slice_3D
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:350
arm_compute::Size2D::x
size_t x() const
Semantic accessor for width as x.
Definition: Size2D.h:73
arm_compute::ICLKernel::add_4D_tensor_argument
void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:244
arm_compute::Window::Dimension
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:79
arm_compute::Window::set
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:53
arm_compute::misc::shape_calculator::compute_space_to_batch_shape
TensorShape compute_space_to_batch_shape(const ITensorInfo *input, int block_x, int block_y, const Size2D &padding_left, const Size2D &padding_right)
Calculate the space to batch output shape of a tensor.
Definition: ShapeCalculator.h:1295
arm_compute::Window::DimY
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
AutoConfiguration.h
arm_compute::Window::first_slice_window_3D
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:306
CLValidate.h
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
Definition: Validate.h:753
arm_compute::string_from_data_layout
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
Definition: DataLayoutUtils.cpp:30
arm_compute::ICLKernel::add_3D_tensor_argument
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:234
arm_compute::get_data_layout_dimension_index
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:201
ShapeCalculator.h
arm_compute::get_cl_unsigned_type_from_element_size
std::string get_cl_unsigned_type_from_element_size(size_t element_size)
Translates the element size to an unsigned integer data type.
Definition: CLHelpers.cpp:107
arm_compute::ICLKernel::add_1D_tensor_argument_if
void add_1D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx ...
Definition: ICLKernel.h:197
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute::ELEMENTWISE
@ ELEMENTWISE
Elementwise CL kernel type.
Definition: CLTypes.h:83
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:294
arm_compute::has_padding_changed
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:491
arm_compute::Window::DimZ
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
arm_compute::DataType::S32
@ S32
signed 32-bit number
arm_compute::ICLKernel::lws_hint
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:383
ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
arm_compute::Window::first_slice_window_4D
Window first_slice_window_4D() const
First 4D slice of the window.
Definition: Window.h:314
arm_compute::DataLayoutDimension::BATCHES
@ BATCHES
batches
arm_compute::misc::shape_calculator
Definition: ShapeCalculator.h:41
arm_compute::DataType::UNKNOWN
@ UNKNOWN
Unknown data type.
arm_compute::CLBuildOptions
Build options.
Definition: CLCompileContext.h:38
arm_compute::get_padding_info
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:476
arm_compute::CLSpaceToBatchLayerKernel::run
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
Definition: CLSpaceToBatchLayerKernel.cpp:231
arm_compute::Window::first_slice_window_2D
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:298
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486
CLHelpers.h
arm_compute::enqueue
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:33