Compute Library
 23.11
NESpaceToBatchLayerKernel Class Reference

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

#include <NESpaceToBatchLayerKernel.h>

Collaboration diagram for NESpaceToBatchLayerKernel:
[legend]

Public Member Functions

const char * name () const override
 Name of the kernel. More...
 
 NESpaceToBatchLayerKernel ()
 Default constructor. More...
 
 NESpaceToBatchLayerKernel (const NESpaceToBatchLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NESpaceToBatchLayerKerneloperator= (const NESpaceToBatchLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NESpaceToBatchLayerKernel (NESpaceToBatchLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
NESpaceToBatchLayerKerneloperator= (NESpaceToBatchLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~NESpaceToBatchLayerKernel ()=default
 Default destructor. More...
 
void configure (const ITensor *input, const ITensor *block_shape, const ITensor *paddings, ITensor *output)
 Initialise the kernel's inputs and output. More...
 
void configure (const ITensor *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right, ITensor *output)
 Initialise the kernel's input and output. 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...
 
virtual size_t get_mws (const CPUInfo &platform, size_t thread_count) const
 Return minimum workload size of the relevant kernel. 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...
 
bool is_window_configured () const
 Function to check if the embedded window of this kernel has been configured. More...
 

Static Public Member Functions

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 NESpaceToBatchLayerKernel. More...
 
static Status validate (const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right, const ITensorInfo *output)
 Static function to check if given info will lead to a valid configuration of NESpaceToBatchLayerKernel (Static block shape and paddings) More...
 

Additional Inherited Members

- Static Public Attributes inherited from ICPPKernel
static constexpr size_t default_mws = 1
 

Detailed Description

Interface for the space to batch kernel.

Definition at line 37 of file NESpaceToBatchLayerKernel.h.

Constructor & Destructor Documentation

◆ NESpaceToBatchLayerKernel() [1/3]

Default constructor.

Definition at line 97 of file NESpaceToBatchLayerKernel.cpp.

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 }

References arm_compute::UNKNOWN.

◆ NESpaceToBatchLayerKernel() [2/3]

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

◆ NESpaceToBatchLayerKernel() [3/3]

Allow instances of this class to be moved.

◆ ~NESpaceToBatchLayerKernel()

Default destructor.

Member Function Documentation

◆ configure() [1/2]

void configure ( const ITensor input,
const int  block_shape_x,
const int  block_shape_y,
const Size2D padding_left,
const Size2D padding_right,
ITensor output 
)

Initialise the kernel's input and output.

(Static block shape and paddings)

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]padding_leftThe padding at the beginning of every dimension of the output tensor.
[in]padding_rightThe padding at the end of every dimension of the output tensor.
[out]outputTensor output. Data types supported: same as input

Definition at line 129 of file NESpaceToBatchLayerKernel.cpp.

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 }

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::auto_init_if_empty(), arm_compute::calculate_max_window(), arm_compute::misc::shape_calculator::compute_space_to_batch_shape(), ITensor::info(), arm_compute::test::validation::input, and arm_compute::test::validation::output_shape.

◆ configure() [2/2]

void configure ( const ITensor input,
const ITensor block_shape,
const ITensor paddings,
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]. Supported M: 2. Data types supported: S32
[in]paddings2-D tensor with shape [2, M] (First dimension is the fastest-changing dimension). Supported M: 2. Data types supported: S32
[out]outputTensor output. Data types supported: same as input

Definition at line 109 of file NESpaceToBatchLayerKernel.cpp.

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 }

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

◆ name()

const char* name ( ) const
inlineoverridevirtual

Name of the kernel.

Returns
Kernel name

Implements ICPPKernel.

Definition at line 40 of file NESpaceToBatchLayerKernel.h.

41  {
42  return "NESpaceToBatchLayerKernel";
43  }

◆ operator=() [1/2]

NESpaceToBatchLayerKernel& operator= ( const NESpaceToBatchLayerKernel )
delete

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

◆ 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 178 of file NESpaceToBatchLayerKernel.cpp.

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 }

References ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, arm_compute::BATCHES, ITensorInfo::dimension(), ITensorInfo::element_size(), arm_compute::execute_window_loop(), Window::first_slice_window_3D(), arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, arm_compute::cpu::height_idx, ITensor::info(), arm_compute::test::validation::info, arm_compute::NCHW, Iterator::ptr(), ITensor::ptr_to_element(), Window::slide_window_slice_3D(), arm_compute::test::validation::w, arm_compute::WIDTH, arm_compute::cpu::width_idx, IKernel::window(), Size2D::x(), Window::x(), Size2D::y(), and Window::y().

◆ validate() [1/2]

Status validate ( const ITensorInfo input,
const int  block_shape_x,
const int  block_shape_y,
const Size2D padding_left,
const Size2D padding_right,
const ITensorInfo output 
)
static

Static function to check if given info will lead to a valid configuration of NESpaceToBatchLayerKernel (Static block shape and paddings)

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]padding_leftThe padding at the beginning of every dimension of the output tensor.
[in]padding_rightThe padding at the end of every dimension of the output tensor.
[in]outputTensor output. Data types supported: same as input
Returns
a status

Definition at line 166 of file NESpaceToBatchLayerKernel.cpp.

172 {
174  validate_arguments_static(input, block_shape_x, block_shape_y, padding_left, padding_right, output));
175  return Status{};
176 }

References ARM_COMPUTE_RETURN_ON_ERROR, and arm_compute::test::validation::input.

◆ validate() [2/2]

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

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

Parameters
[in]inputTensor input. Supported tensor rank: 4. Data types supported: All.
[in]block_shape1-D tensor with shape [M]. Supported M: 2. Data types supported: S32
[in]paddings2-D tensor with shape [2, M] (First dimension is the fastest-changing dimension). Supported M: 2. Data types supported: S32
[in]outputTensor output. Data types supported: same as input
Returns
a status

Definition at line 158 of file NESpaceToBatchLayerKernel.cpp.

162 {
163  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, paddings, output));
164  return Status{};
165 }

References ARM_COMPUTE_RETURN_ON_ERROR, arm_compute::test::validation::input, and arm_compute::cpu::kernels::validate_arguments().

Referenced by NESpaceToBatchLayer::validate().


The documentation for this class was generated from the following files:
arm_compute::DataLayout::NCHW
@ NCHW
Num samples, channels, height, width.
arm_compute::Size2D::y
size_t y() const
Semantic accessor for height as y.
Definition: Size2D.h:82
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::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()
arm_compute::DataLayoutDimension::WIDTH
@ WIDTH
width
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_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::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_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::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::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
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::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
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::DataLayout::UNKNOWN
@ UNKNOWN
Unknown data layout.
arm_compute::DataLayoutDimension::BATCHES
@ BATCHES
batches
arm_compute::cpu::width_idx
const size_t width_idx
Definition: impl.h:37
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
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