Compute Library
 23.11
NESpaceToBatchLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 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 
28 #include "arm_compute/core/Types.h"
31 
35 
36 #include <arm_neon.h>
37 #include <cstdint>
38 
40 
41 namespace arm_compute
42 {
43 namespace
44 {
45 Status validate_arguments(const ITensorInfo *input,
46  const ITensorInfo *block_info,
47  const ITensorInfo *paddings,
48  const ITensorInfo *output)
49 {
50  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_info, paddings, output);
53  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
54  ARM_COMPUTE_RETURN_ERROR_ON(block_info->num_dimensions() > 1);
55  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(block_info->tensor_shape(), TensorShape{2});
56  ARM_COMPUTE_RETURN_ERROR_ON(paddings->num_dimensions() > 2);
57  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(paddings->tensor_shape(), TensorShape{2, 2});
58 
59  // Validate output if initialized
60  if (output->total_size() != 0)
61  {
62  const DataLayout data_layout = input->data_layout();
64  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] != output->tensor_shape()[idx_channel]);
67  }
68 
69  return Status{};
70 }
71 Status validate_arguments_static(const ITensorInfo *input,
72  const int block_shape_x,
73  const int block_shape_y,
74  const Size2D &padding_left,
75  const Size2D &padding_right,
76  const ITensorInfo *output)
77 {
80  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
81  ARM_COMPUTE_RETURN_ERROR_ON(block_shape_x < 1 || block_shape_y < 1);
82 
83  // Validate output if initialized
84  if (output->total_size() != 0)
85  {
86  TensorShape expected_output_shape = misc::shape_calculator::compute_space_to_batch_shape(
87  input, block_shape_x, block_shape_y, padding_left, padding_right);
88  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), expected_output_shape);
91  }
92 
93  return Status{};
94 }
95 } // namespace
96 
98  : _input(nullptr),
99  _block_shape(nullptr),
100  _paddings(nullptr),
101  _output(nullptr),
102  _data_layout(DataLayout::UNKNOWN),
103  _padding_left(),
104  _block_shape_x(),
105  _block_shape_y()
106 {
107 }
108 
110  const ITensor *block_shape,
111  const ITensor *paddings,
112  ITensor *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 
118  _input = input;
119  _block_shape = block_shape;
120  _paddings = paddings;
121  _output = output;
122  _data_layout = input->info()->data_layout();
123 
124  // Configure kernel window
125  Window win = calculate_max_window(*output->info(), Steps());
126  ICPPKernel::configure(win);
127 }
128 
130  const int block_shape_x,
131  const int block_shape_y,
132  const Size2D &padding_left,
133  const Size2D &padding_right,
134  ITensor *output)
135 {
137 
139  input->info(), block_shape_x, block_shape_y, padding_left, padding_right);
140  auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(),
141  input->info()->quantization_info());
142 
143  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, padding_left,
144  padding_right, output->info()));
145 
146  _input = input;
147  _output = output;
148  _block_shape_x = block_shape_x;
149  _block_shape_y = block_shape_y;
150  _padding_left = padding_left;
151  _data_layout = input->info()->data_layout();
152 
153  // Configure kernel window
154  Window win = calculate_max_window(*output->info(), Steps());
155  INEKernel::configure(win);
156 }
157 
159  const ITensorInfo *block_shape,
160  const ITensorInfo *paddings,
161  const ITensorInfo *output)
162 {
163  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, paddings, output));
164  return Status{};
165 }
167  const int block_shape_x,
168  const int block_shape_y,
169  const Size2D &padding_left,
170  const Size2D &padding_right,
171  const ITensorInfo *output)
172 {
174  validate_arguments_static(input, block_shape_x, block_shape_y, padding_left, padding_right, output));
175  return Status{};
176 }
177 
179 {
183 
184  if (_block_shape != nullptr)
185  {
186  // Retrieve the block shapes dynamically
187  _block_shape_x = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(0)));
188  _block_shape_y = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(1)));
189  }
190 
191  if (_paddings != nullptr)
192  {
193  const size_t pad_left_x = *reinterpret_cast<const size_t *>(_paddings->ptr_to_element({0, 0}));
194  const size_t pad_left_y = *reinterpret_cast<const size_t *>(_paddings->ptr_to_element({1, 0}));
195  _padding_left = Size2D(pad_left_x, pad_left_y);
196  }
199  const int batch_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES);
200  const int element_size = _input->info()->element_size();
201 
202  const size_t height = _input->info()->dimension(height_idx);
203  const size_t width = _input->info()->dimension(width_idx);
204  const size_t batch_size = _input->info()->dimension(batch_idx);
205 
206  Window slice_out = window.first_slice_window_3D();
207 
208  int batch_id = 0;
209 
210  // Main loop for NCHW and NHWC
211  if (_data_layout == DataLayout::NCHW)
212  {
213  do
214  {
215  Iterator out(_output, slice_out);
217  slice_out,
218  [&](const Coordinates &id)
219  {
220  const size_t out_x = id.x();
221  const size_t out_y = id.y();
222  const size_t z = id.z();
223  const size_t pos_x = out_x * _block_shape_x + (batch_id / batch_size) % _block_shape_x;
224  const size_t pos_y = out_y * _block_shape_y + (batch_id / batch_size) / _block_shape_x;
225  if (pos_y >= _padding_left.y() && pos_y < _padding_left.y() + height &&
226  pos_x >= _padding_left.x() && pos_x < _padding_left.x() + width)
227  {
228  const int w = batch_id % batch_size;
229  const int in_x = pos_x - _padding_left.x();
230  const int in_y = pos_y - _padding_left.y();
231  Coordinates input_coords{in_x, in_y, z, w};
232  memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
233  }
234  },
235  out);
236  ++batch_id;
237  } while (window.slide_window_slice_3D(slice_out));
238  }
239  else
240  {
241  do
242  {
243  Iterator out(_output, slice_out);
245  slice_out,
246  [&](const Coordinates &id)
247  {
248  const size_t out_x = id.y();
249  const size_t out_y = id.z();
250  const size_t z = id.x();
251  const size_t pos_x = out_x * _block_shape_x + (batch_id / batch_size) % _block_shape_x;
252  const size_t pos_y = out_y * _block_shape_y + (batch_id / batch_size) / _block_shape_x;
253  if (pos_y >= _padding_left.y() && pos_y < _padding_left.y() + height &&
254  pos_x >= _padding_left.x() && pos_x < _padding_left.x() + width)
255  {
256  const int w = batch_id % batch_size;
257  const int in_x = pos_x - _padding_left.x();
258  const int in_y = pos_y - _padding_left.y();
259  Coordinates input_coords{z, in_x, in_y, w};
260  memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
261  }
262  },
263  out);
264  ++batch_id;
265  } while (window.slide_window_slice_3D(slice_out));
266  }
267 }
268 } // namespace arm_compute
arm_compute::DataLayout::NCHW
@ NCHW
Num samples, channels, height, width.
arm_compute::Steps
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
arm_compute::Size2D::y
size_t y() const
Semantic accessor for height as y.
Definition: Size2D.h:82
Helpers.h
arm_compute::DataLayout
DataLayout
[DataLayout enum definition]
Definition: CoreTypes.h:110
arm_compute::DataLayoutDimension::CHANNEL
@ CHANNEL
channel
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::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::ITensorInfo::element_size
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
Types.h
arm_compute::Size2D
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
arm_compute::NESpaceToBatchLayerKernel::run
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Definition: NESpaceToBatchLayerKernel.cpp:178
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
wrapper.h
Includes all wrapper headers at once.
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
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::Iterator::ptr
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:147
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
arm_compute::execute_window_loop
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&...iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:74
arm_compute::DataLayoutDimension::HEIGHT
@ HEIGHT
height
arm_compute::Iterator
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
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::test::validation::w
SimpleTensor< float > w
Definition: DFT.cpp:156
arm_compute::Status
Status class.
Definition: Error.h:52
WindowHelpers.h
arm_compute::Window::y
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:167
ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:203
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::NESpaceToBatchLayerKernel::configure
void configure(const ITensor *input, const ITensor *block_shape, const ITensor *paddings, ITensor *output)
Initialise the kernel's inputs and output.
Definition: NESpaceToBatchLayerKernel.cpp:109
arm_compute::Size2D::x
size_t x() const
Semantic accessor for width as x.
Definition: Size2D.h:73
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:151
arm_compute::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
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
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
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::ThreadInfo
Information about executing thread and CPU.
Definition: CPPTypes.h:180
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::NESpaceToBatchLayerKernel::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 NESpaceToBatchLayerKerne...
Definition: NESpaceToBatchLayerKernel.cpp:158
arm_compute::NESpaceToBatchLayerKernel::NESpaceToBatchLayerKernel
NESpaceToBatchLayerKernel()
Default constructor.
Definition: NESpaceToBatchLayerKernel.cpp:97
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::ITensor::ptr_to_element
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:294
arm_compute::UNKNOWN
@ UNKNOWN
Unknown CL kernel type.
Definition: CLTypes.h:80
arm_compute::DataType::S32
@ S32
signed 32-bit number
ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
NESpaceToBatchLayerKernel.h
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
arm_compute::DataLayoutDimension::BATCHES
@ BATCHES
batches
arm_compute::misc::shape_calculator
Definition: ShapeCalculator.h:41
arm_compute::cpu::width_idx
const size_t width_idx
Definition: impl.h:37
ITensor.h
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
arm_compute::DataType::UNKNOWN
@ UNKNOWN
Unknown data type.
Validate.h
arm_compute::Window::x
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:158
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486
arm_compute::cpu::height_idx
const size_t height_idx
Definition: impl.h:38