Compute Library
 21.02
NEBatchToSpaceLayerKernel.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"
33 
35 
36 namespace arm_compute
37 {
38 namespace
39 {
40 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *block_info, const ITensorInfo *output)
41 {
42  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_info, output);
44  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
45  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
46 
47  // Validate output if initialized
48  if(output->total_size() != 0)
49  {
50  ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4);
52  }
53 
54  return Status{};
55 }
56 Status validate_arguments_static(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const ITensorInfo *output)
57 {
59  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
60  ARM_COMPUTE_RETURN_ERROR_ON(block_shape_x <= 0);
61  ARM_COMPUTE_RETURN_ERROR_ON(block_shape_y <= 0);
62 
63  const DataLayout data_layout = input->data_layout();
64  const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
65  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_batch] % (block_shape_x * block_shape_y) != 0);
66  // Validate output if initialized
67  if(output->total_size() != 0)
68  {
71  const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
72  ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_width] != (block_shape_x * input->tensor_shape()[idx_width]));
73  ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_height] != (block_shape_y * input->tensor_shape()[idx_height]));
74  ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_channel] != input->tensor_shape()[idx_channel]);
75  ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4);
77  }
78 
79  return Status{};
80 }
81 } // namespace
82 
84  : _input(nullptr), _block_shape(nullptr), _output(nullptr), _data_layout(DataLayout::UNKNOWN), _block_shape_x(), _block_shape_y()
85 {
86 }
87 
88 void NEBatchToSpaceLayerKernel::configure(const ITensor *input, const ITensor *block_shape, ITensor *output)
89 {
90  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
91  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), block_shape->info(), output->info()));
92 
93  _input = input;
94  _block_shape = block_shape;
95  _output = output;
96  _data_layout = input->info()->data_layout();
97 
98  // Configure kernel window
99  Window win = calculate_max_window(*input->info(), Steps());
100  ICPPKernel::configure(win);
101 }
102 
103 void NEBatchToSpaceLayerKernel::configure(const ITensor *input, const int32_t block_shape_x, const int32_t block_shape_y, ITensor *output)
104 {
105  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
106  TensorShape output_shape = compute_batch_to_space_shape(input->info(), block_shape_x, block_shape_y);
107  // Output auto inizialitation if not yet initialized
108  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
109 
110  // Perform validation step
111  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, output->info()));
112 
113  _input = input;
114  _output = output;
115  _block_shape_x = block_shape_x;
116  _block_shape_y = block_shape_y;
117  _data_layout = input->info()->data_layout();
118 
119  // Configure kernel window
120  Window win = calculate_max_window(*input->info(), Steps());
121  ICPPKernel::configure(win);
122 }
123 
124 Status NEBatchToSpaceLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *output)
125 {
126  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_shape, output);
127  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, output));
128  return Status{};
129 }
130 
131 Status NEBatchToSpaceLayerKernel::validate(const ITensorInfo *input, const int32_t block_shape_x, const int32_t block_shape_y, const ITensorInfo *output)
132 {
134  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, output));
135  return Status{};
136 }
137 
139 {
140  ARM_COMPUTE_UNUSED(info);
143 
144  if(_block_shape != nullptr)
145  {
146  // Retrieve the block shapes dynamically
147  _block_shape_x = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(0)));
148  _block_shape_y = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(1)));
149  }
150 
151  const int batch_size = _input->info()->dimension(3);
152  const int r = (batch_size / (_block_shape_x * _block_shape_y));
153  const int element_size = _input->info()->element_size();
154 
155  Window slice_in = window.first_slice_window_3D();
156  Window slice_out = window.first_slice_window_4D();
157 
158  // The slice_out slice does not move
159  slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
160  slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
161  slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
162  slice_out.set(3, Window::Dimension(0, 0, 0));
163 
164  int batch_id = 0;
165  // Main loop for NCHW and NHWC
166  if(_data_layout == DataLayout::NCHW)
167  {
168  do
169  {
170  Iterator in(_input, slice_in);
171  execute_window_loop(slice_in, [&](const Coordinates & id)
172  {
173 
174  const int x = id.x();
175  const int y = id.y();
176  const int z = id.z();
177 
178  const int w = batch_id % r;
179  const int out_x = x * _block_shape_x + (batch_id / r) % _block_shape_x;
180  const int out_y = y * _block_shape_y + (batch_id / r) / _block_shape_x;
181  Coordinates output_coords{ out_x, out_y, z, w };
182  memcpy(_output->ptr_to_element(output_coords), in.ptr(), element_size);
183  },
184  in);
185  ++batch_id;
186  }
187  while(window.slide_window_slice_3D(slice_in));
188  }
189  else
190  {
191  do
192  {
193  Iterator in(_input, slice_in);
194  execute_window_loop(slice_in, [&](const Coordinates & id)
195  {
196 
197  const int z = id.x();
198  const int x = id.y();
199  const int y = id.z();
200 
201  const int w = batch_id % r;
202  const int out_x = x * _block_shape_x + (batch_id / r) % _block_shape_x;
203  const int out_y = y * _block_shape_y + (batch_id / r) / _block_shape_x;
204  Coordinates output_coords{ z, out_x, out_y, w };
205  memcpy(_output->ptr_to_element(output_coords), in.ptr(), element_size);
206  },
207  in);
208  ++batch_id;
209  }
210  while(window.slide_window_slice_3D(slice_in));
211  }
212 }
213 } // 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_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
const DataLayout data_layout
Definition: Im2Col.cpp:151
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:77
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 Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
1 channel, 1 S32 per channel
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
static Status validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of NEBatchToSpaceLayerKerne...
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.
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()
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:335
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
Num samples, channels, height, width.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
TensorShape compute_batch_to_space_shape(const ITensorInfo *input, const int block_x, const int block_y)
Calculate the batch to space output shape of a tensor.
Window first_slice_window_4D() const
First 4D slice of the window.
Definition: Window.h:299
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
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
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
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
void configure(const ITensor *input, const ITensor *block_shape, ITensor *output)
Initialise the kernel&#39;s inputs and output.
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
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