Compute Library
 21.02
NEBatchToSpaceLayerKernel Class Reference

Interface for the batch to space kernel. More...

#include <NEBatchToSpaceLayerKernel.h>

Collaboration diagram for NEBatchToSpaceLayerKernel:
[legend]

Public Member Functions

const char * name () const override
 Name of the kernel. More...
 
 NEBatchToSpaceLayerKernel ()
 Default constructor. More...
 
 NEBatchToSpaceLayerKernel (const NEBatchToSpaceLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NEBatchToSpaceLayerKerneloperator= (const NEBatchToSpaceLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NEBatchToSpaceLayerKernel (NEBatchToSpaceLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
NEBatchToSpaceLayerKerneloperator= (NEBatchToSpaceLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~NEBatchToSpaceLayerKernel ()=default
 Default destructor. More...
 
void configure (const ITensor *input, const ITensor *block_shape, ITensor *output)
 Initialise the kernel's inputs and output. More...
 
void configure (const ITensor *input, const int32_t block_shape_x, const int32_t block_shape_y, ITensor *output)
 Initialise the kernel's inputs and output (Static block shape). More...
 
void run (const Window &window, const ThreadInfo &info) override
 Execute the kernel on the passed window. More...
 
- Public Member Functions inherited from ICPPKernel
virtual ~ICPPKernel ()=default
 Default destructor. More...
 
virtual void run_nd (const Window &window, const ThreadInfo &info, const Window &thread_locator)
 legacy compatibility layer for implemantions which do not support thread_locator In these cases we simply narrow the interface down the legacy version More...
 
virtual void run_op (ITensorPack &tensors, const Window &window, const ThreadInfo &info)
 Execute the kernel on the passed window. More...
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
virtual BorderSize border_size () const
 The size of the border for that kernel. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Static Public Member Functions

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 NEBatchToSpaceLayerKernel. More...
 
static Status validate (const ITensorInfo *input, const int32_t block_shape_x, const int32_t block_shape_y, const ITensorInfo *output)
 Static function to check if given info will lead to a valid configuration of NEBatchToSpaceLayerKernel (Static block shape). More...
 

Detailed Description

Interface for the batch to space kernel.

Definition at line 34 of file NEBatchToSpaceLayerKernel.h.

Constructor & Destructor Documentation

◆ NEBatchToSpaceLayerKernel() [1/3]

Default constructor.

Definition at line 83 of file NEBatchToSpaceLayerKernel.cpp.

Referenced by NEBatchToSpaceLayerKernel::name().

84  : _input(nullptr), _block_shape(nullptr), _output(nullptr), _data_layout(DataLayout::UNKNOWN), _block_shape_x(), _block_shape_y()
85 {
86 }

◆ NEBatchToSpaceLayerKernel() [2/3]

Prevent instances of this class from being copied (As this class contains pointers)

◆ NEBatchToSpaceLayerKernel() [3/3]

Allow instances of this class to be moved.

◆ ~NEBatchToSpaceLayerKernel()

Default destructor.

Referenced by NEBatchToSpaceLayerKernel::name().

Member Function Documentation

◆ configure() [1/2]

void configure ( const ITensor input,
const ITensor block_shape,
ITensor output 
)

Initialise the kernel's inputs and output.

Parameters
[in]inputTensor input. Supported tensor rank: 4. Data types supported: All.
[in]block_shape1-D tensor with shape [M]. Data types supported: S32
[out]outputTensor output. Data types supported: same as input

Definition at line 88 of file NEBatchToSpaceLayerKernel.cpp.

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::calculate_max_window(), ITensorInfo::data_layout(), ITensor::info(), arm_compute::test::validation::input, and arm_compute::validate_arguments().

Referenced by NEBatchToSpaceLayerKernel::name().

89 {
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 }
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
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

◆ configure() [2/2]

void configure ( const ITensor input,
const int32_t  block_shape_x,
const int32_t  block_shape_y,
ITensor output 
)

Initialise the kernel's inputs and output (Static block shape).

Parameters
[in]inputTensor input. Supported tensor rank: 4. Data types supported: All.
[in]block_shape_xBlock shape x value.
[in]block_shape_yBlock shape y value.
[out]outputTensor output. Data types supported: same as input

Definition at line 103 of file NEBatchToSpaceLayerKernel.cpp.

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::auto_init_if_empty(), arm_compute::calculate_max_window(), ICloneable< T >::clone(), arm_compute::misc::shape_calculator::compute_batch_to_space_shape(), ITensorInfo::data_layout(), ITensor::info(), arm_compute::test::validation::input, and arm_compute::test::validation::output_shape.

104 {
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 }
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
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...
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.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161

◆ name()

◆ operator=() [1/2]

NEBatchToSpaceLayerKernel& operator= ( const NEBatchToSpaceLayerKernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

Referenced by NEBatchToSpaceLayerKernel::name().

◆ operator=() [2/2]

Allow instances of this class to be moved.

◆ run()

void run ( const Window window,
const ThreadInfo info 
)
overridevirtual

Execute the kernel on the passed window.

Warning
If is_parallelisable() returns false then the passed window must be equal to window()
Note
The window has to be a region within the window returned by the window() method
The width of the window has to be a multiple of num_elems_processed_per_iteration().
Parameters
[in]windowRegion on which to execute the kernel. (Must be a region of the window returned by window())
[in]infoInfo about executing thread and CPU.

Reimplemented from ICPPKernel.

Definition at line 138 of file NEBatchToSpaceLayerKernel.cpp.

References ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, ITensorInfo::dimension(), Window::DimX, Window::DimY, Window::DimZ, ITensorInfo::element_size(), arm_compute::execute_window_loop(), Window::first_slice_window_3D(), Window::first_slice_window_4D(), ITensor::info(), arm_compute::NCHW, Iterator::ptr(), ITensor::ptr_to_element(), Window::set(), Window::slide_window_slice_3D(), arm_compute::test::validation::w, IKernel::window(), and Window::x().

Referenced by NEBatchToSpaceLayerKernel::name().

139 {
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 }
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
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
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
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
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)
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Window first_slice_window_4D() const
First 4D slice of the window.
Definition: Window.h:299
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
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205

◆ validate() [1/2]

Status validate ( const ITensorInfo input,
const ITensorInfo block_shape,
const ITensorInfo output 
)
static

Static function to check if given info will lead to a valid configuration of NEBatchToSpaceLayerKernel.

Parameters
[in]inputTensor input. Supported tensor rank: 4. Data types supported: All.
[in]block_shape1-D tensor with shape [M]. Data types supported: S32
[in]outputTensor output. Data types supported: same as input
Returns
a status

Definition at line 124 of file NEBatchToSpaceLayerKernel.cpp.

References ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, ARM_COMPUTE_RETURN_ON_ERROR, and arm_compute::validate_arguments().

Referenced by NEBatchToSpaceLayerKernel::name(), and NEBatchToSpaceLayer::validate().

125 {
126  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_shape, output);
128  return Status{};
129 }
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)

◆ validate() [2/2]

Status validate ( const ITensorInfo input,
const int32_t  block_shape_x,
const int32_t  block_shape_y,
const ITensorInfo output 
)
static

Static function to check if given info will lead to a valid configuration of NEBatchToSpaceLayerKernel (Static block shape).

Parameters
[in]inputTensor input. Supported tensor rank: 4. Data types supported: All.
[in]block_shape_xBlock shape x value.
[in]block_shape_yBlock shape y value.
[in]outputTensor output. Data types supported: same as input
Returns
a status

Definition at line 131 of file NEBatchToSpaceLayerKernel.cpp.

References ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, and ARM_COMPUTE_RETURN_ON_ERROR.

132 {
134  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, output));
135  return Status{};
136 }
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163

The documentation for this class was generated from the following files: