Compute Library
 21.02
NESpaceToDepthLayerKernel.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 *output, int32_t block_shape)
45 {
47  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
48  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
49 
50  ARM_COMPUTE_RETURN_ERROR_ON(block_shape < 1);
51 
52  // Validate output if initialized
53  if(output->total_size() != 0)
54  {
55  const DataLayout data_layout = input->data_layout();
58  const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
59  const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
60  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_width] % block_shape != 0);
61  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_height] % block_shape != 0);
62  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_batch] != output->tensor_shape()[idx_batch]);
63  ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_channel] % (block_shape * block_shape) != 0);
64  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size() != output->tensor_shape().total_size());
66  }
67 
68  return Status{};
69 }
70 } // namespace
71 
73  : _input(nullptr), _output(nullptr), _block_shape(), _data_layout(DataLayout::UNKNOWN)
74 {
75 }
76 
77 void NESpaceToDepthLayerKernel::configure(const ITensor *input, ITensor *output, int32_t block_shape)
78 {
79  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
80 
82  auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
83 
84  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), block_shape));
85 
86  _input = input;
87  _block_shape = block_shape;
88  _output = output;
89  _data_layout = input->info()->data_layout();
90 
91  // Configure kernel window
92  Window win = calculate_max_window(*output->info(), Steps());
93  INEKernel::configure(win);
94 }
95 
96 Status NESpaceToDepthLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape)
97 {
98  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, block_shape));
99  return Status{};
100 }
101 
103 {
104  ARM_COMPUTE_UNUSED(info);
107 
108  const int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
109  const int element_size = _input->info()->element_size();
110 
111  const size_t channel_size = _input->info()->dimension(channel_idx);
112 
113  Window slice_out = window.first_slice_window_3D();
114 
115  int batch_id = 0;
116 
117  // Main loop for NCHW and NHWC
118  if(_data_layout == DataLayout::NCHW)
119  {
120  do
121  {
122  Iterator out(_output, slice_out);
123  execute_window_loop(slice_out, [&](const Coordinates & id)
124  {
125  const size_t channel_id = id.z();
126  const size_t in_x = id.x() * _block_shape + (channel_id / channel_size) % _block_shape;
127  const size_t in_y = id.y() * _block_shape + (channel_id / channel_size) / _block_shape;
128  const int z = channel_id % channel_size;
129  Coordinates input_coords{ in_x, in_y, z, batch_id };
130  memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
131  },
132  out);
133  ++batch_id;
134  }
135  while(window.slide_window_slice_3D(slice_out));
136  }
137  else
138  {
139  do
140  {
141  Iterator out(_output, slice_out);
142  execute_window_loop(slice_out, [&](const Coordinates & id)
143  {
144  const size_t channel_id = id.x();
145  const size_t in_x = id.y() * _block_shape + (channel_id / channel_size) % _block_shape;
146  const size_t in_y = id.z() * _block_shape + (channel_id / channel_size) / _block_shape;
147  const int z = channel_id % channel_size;
148  Coordinates input_coords{ z, in_x, in_y, batch_id };
149  memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
150  },
151  out);
152  ++batch_id;
153  }
154  while(window.slide_window_slice_3D(slice_out));
155  }
156 }
157 } // namespace arm_compute
static Status validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape)
Static function to check if given info will lead to a valid configuration of NESpaceToDepthLayerKerne...
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
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
virtual DataType data_type() const =0
Data type used for each element of the tensor.
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
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
void configure(const ITensor *input, ITensor *output, int32_t block_shape)
Initialise the kernel&#39;s inputs and output.
constexpr const Dimension & z() const
Alias to access the third dimension of the window.
Definition: Window.h:163
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
#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
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
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
Num samples, channels, height, width.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
#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
Includes all wrapper headers at once.
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
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.
TensorShape compute_space_to_depth_shape(const ITensorInfo *input, int32_t block_shape)
Calculate the space to batch output shape of a tensor.
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145