Compute Library
 21.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"
34 
35 #include <arm_neon.h>
36 #include <cstdint>
37 
39 
40 namespace arm_compute
41 {
42 namespace
43 {
44 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *block_info, const ITensorInfo *paddings, const ITensorInfo *output)
45 {
46  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_info, paddings, output);
47  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
49  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
50  ARM_COMPUTE_RETURN_ERROR_ON(block_info->num_dimensions() > 1);
51  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(block_info->tensor_shape(), TensorShape{ 2 });
52  ARM_COMPUTE_RETURN_ERROR_ON(paddings->num_dimensions() > 2);
53  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(paddings->tensor_shape(), TensorShape{ 2, 2 });
54 
55  // Validate output if initialized
56  if(output->total_size() != 0)
57  {
58  const DataLayout data_layout = input->data_layout();
59  const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
60  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] != output->tensor_shape()[idx_channel]);
63  }
64 
65  return Status{};
66 }
67 Status validate_arguments_static(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right,
68  const ITensorInfo *output)
69 {
71  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
72  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
73  ARM_COMPUTE_RETURN_ERROR_ON(block_shape_x < 1 || block_shape_y < 1);
74 
75  // Validate output if initialized
76  if(output->total_size() != 0)
77  {
78  TensorShape expected_output_shape = misc::shape_calculator::compute_space_to_batch_shape(input, block_shape_x, block_shape_y, padding_left, padding_right);
79  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), expected_output_shape);
82  }
83 
84  return Status{};
85 }
86 } // namespace
87 
89  : _input(nullptr), _block_shape(nullptr), _paddings(nullptr), _output(nullptr), _data_layout(DataLayout::UNKNOWN), _padding_left(), _block_shape_x(), _block_shape_y()
90 {
91 }
92 
93 void NESpaceToBatchLayerKernel::configure(const ITensor *input, const ITensor *block_shape, const ITensor *paddings, ITensor *output)
94 {
95  ARM_COMPUTE_ERROR_ON_NULLPTR(input, block_shape, paddings, output);
96  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), block_shape->info(), paddings->info(), output->info()));
97 
98  _input = input;
99  _block_shape = block_shape;
100  _paddings = paddings;
101  _output = output;
102  _data_layout = input->info()->data_layout();
103 
104  // Configure kernel window
105  Window win = calculate_max_window(*output->info(), Steps());
106  ICPPKernel::configure(win);
107 }
108 
109 void NESpaceToBatchLayerKernel::configure(const ITensor *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right,
110  ITensor *output)
111 {
112  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
113 
114  TensorShape output_shape = misc::shape_calculator::compute_space_to_batch_shape(input->info(), block_shape_x, block_shape_y, padding_left, padding_right);
115  auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->quantization_info());
116 
117  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, padding_left, padding_right, output->info()));
118 
119  _input = input;
120  _output = output;
121  _block_shape_x = block_shape_x;
122  _block_shape_y = block_shape_y;
123  _padding_left = padding_left;
124  _data_layout = input->info()->data_layout();
125 
126  // Configure kernel window
127  Window win = calculate_max_window(*output->info(), Steps());
128  INEKernel::configure(win);
129 }
130 
131 Status NESpaceToBatchLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *paddings, const ITensorInfo *output)
132 {
133  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, paddings, output));
134  return Status{};
135 }
136 Status NESpaceToBatchLayerKernel::validate(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right,
137  const ITensorInfo *output)
138 {
139  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, padding_left, padding_right, output));
140  return Status{};
141 }
142 
144 {
145  ARM_COMPUTE_UNUSED(info);
148 
149  if(_block_shape != nullptr)
150  {
151  // Retrieve the block shapes dynamically
152  _block_shape_x = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(0)));
153  _block_shape_y = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(1)));
154  }
155 
156  if(_paddings != nullptr)
157  {
158  const size_t pad_left_x = *reinterpret_cast<const size_t *>(_paddings->ptr_to_element({ 0, 0 }));
159  const size_t pad_left_y = *reinterpret_cast<const size_t *>(_paddings->ptr_to_element({ 1, 0 }));
160  _padding_left = Size2D(pad_left_x, pad_left_y);
161  }
162  const int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
163  const int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
164  const int batch_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES);
165  const int element_size = _input->info()->element_size();
166 
167  const size_t height = _input->info()->dimension(height_idx);
168  const size_t width = _input->info()->dimension(width_idx);
169  const size_t batch_size = _input->info()->dimension(batch_idx);
170 
171  Window slice_out = window.first_slice_window_3D();
172 
173  int batch_id = 0;
174 
175  // Main loop for NCHW and NHWC
176  if(_data_layout == DataLayout::NCHW)
177  {
178  do
179  {
180  Iterator out(_output, slice_out);
181  execute_window_loop(slice_out, [&](const Coordinates & id)
182  {
183  const size_t out_x = id.x();
184  const size_t out_y = id.y();
185  const size_t z = id.z();
186  const size_t pos_x = out_x * _block_shape_x + (batch_id / batch_size) % _block_shape_x;
187  const size_t pos_y = out_y * _block_shape_y + (batch_id / batch_size) / _block_shape_x;
188  if(pos_y >= _padding_left.y() && pos_y < _padding_left.y() + height && pos_x >= _padding_left.x() && pos_x < _padding_left.x() + width)
189  {
190  const int w = batch_id % batch_size;
191  const int in_x = pos_x - _padding_left.x();
192  const int in_y = pos_y - _padding_left.y();
193  Coordinates input_coords{ in_x, in_y, z, w };
194  memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
195  }
196  },
197  out);
198  ++batch_id;
199  }
200  while(window.slide_window_slice_3D(slice_out));
201  }
202  else
203  {
204  do
205  {
206  Iterator out(_output, slice_out);
207  execute_window_loop(slice_out, [&](const Coordinates & id)
208  {
209  const size_t out_x = id.y();
210  const size_t out_y = id.z();
211  const size_t z = id.x();
212  const size_t pos_x = out_x * _block_shape_x + (batch_id / batch_size) % _block_shape_x;
213  const size_t pos_y = out_y * _block_shape_y + (batch_id / batch_size) / _block_shape_x;
214  if(pos_y >= _padding_left.y() && pos_y < _padding_left.y() + height && pos_x >= _padding_left.x() && pos_x < _padding_left.x() + width)
215  {
216  const int w = batch_id % batch_size;
217  const int in_x = pos_x - _padding_left.x();
218  const int in_y = pos_y - _padding_left.y();
219  Coordinates input_coords{ z, in_x, in_y, w };
220  memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
221  }
222  },
223  out);
224  ++batch_id;
225  }
226  while(window.slide_window_slice_3D(slice_out));
227  }
228 }
229 } // namespace arm_compute
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
SimpleTensor< float > w
Definition: DFT.cpp:156
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
Shape of a tensor.
Definition: TensorShape.h:39
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
Definition: Validate.h:606
Unknown CL kernel type.
Definition: CLTypes.h:81
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
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...
TensorShape compute_space_to_batch_shape(const ITensorInfo *input, const int block_x, const int block_y, const Size2D &padding_left, const Size2D &padding_right)
Calculate the space to batch output shape of a tensor.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
size_t x() const
Semantic accessor for width as x.
Definition: Size2D.h:75
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for CPU tensor.
Definition: ITensor.h:36
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:284
Copyright (c) 2017-2021 Arm Limited.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
1 channel, 1 S32 per channel
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
void configure(const ITensor *input, const ITensor *block_shape, const ITensor *paddings, ITensor *output)
Initialise the kernel&#39;s inputs and output.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
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 ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:335
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
Num samples, channels, height, width.
size_t y() const
Semantic accessor for height as y.
Definition: Size2D.h:84
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:158
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
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:154
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
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:77
Includes all wrapper headers at once.
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
DataLayout
[DataLayout enum definition]
Definition: Types.h:113
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145