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

Additional Inherited Members

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

Detailed Description

Interface for the batch to space kernel.

Definition at line 36 of file NEBatchToSpaceLayerKernel.h.

Constructor & Destructor Documentation

◆ NEBatchToSpaceLayerKernel() [1/3]

Default constructor.

Definition at line 88 of file NEBatchToSpaceLayerKernel.cpp.

89  : _input(nullptr),
90  _block_shape(nullptr),
91  _output(nullptr),
92  _data_layout(DataLayout::UNKNOWN),
93  _block_shape_x(),
94  _block_shape_y(),
95  _crop_info()
96 {
97 }

References arm_compute::UNKNOWN.

◆ 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.

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
Deprecated:
This method for dynamic block shape is not fully mature and will be removed in 23.08 release

Definition at line 99 of file NEBatchToSpaceLayerKernel.cpp.

100 {
102  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), block_shape->info(), output->info()));
103 
104  _input = input;
105  _block_shape = block_shape;
106  _output = output;
107  _data_layout = input->info()->data_layout();
108 
109  // Configure kernel window
110  Window win = calculate_max_window(*output->info(), Steps());
111  ICPPKernel::configure(win);
112 }

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().

◆ configure() [2/2]

void configure ( const ITensor input,
int32_t  block_shape_x,
int32_t  block_shape_y,
ITensor output,
const CropInfo crop_info = CropInfo{} 
)

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
[in]crop_infoSpecifies how the output shape is cropped after batch to space is performed

Definition at line 114 of file NEBatchToSpaceLayerKernel.cpp.

116 {
118  const TensorShape output_shape = compute_batch_to_space_shape(
119  input->info()->data_layout(), input->info()->tensor_shape(), block_shape_x, block_shape_y);
120  // Output auto initialization if not yet initialized
121  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
122 
123  // Perform validation step
125  validate_arguments_static(input->info(), block_shape_x, block_shape_y, output->info(), crop_info));
126 
127  _input = input;
128  _output = output;
129  _block_shape_x = block_shape_x;
130  _block_shape_y = block_shape_y;
131  _data_layout = input->info()->data_layout();
132  _crop_info = crop_info;
133 
134  // Configure kernel window
135  Window win = calculate_max_window(*output->info(), Steps());
136  ICPPKernel::configure(win);
137 }

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_batch_to_space_shape(), ITensor::info(), arm_compute::test::validation::input, and arm_compute::test::validation::output_shape.

◆ name()

const char* name ( ) const
inlineoverridevirtual

Name of the kernel.

Returns
Kernel name

Implements ICPPKernel.

Definition at line 39 of file NEBatchToSpaceLayerKernel.h.

40  {
41  return "NEBatchToSpaceLayerKernel";
42  }

◆ operator=() [1/2]

NEBatchToSpaceLayerKernel& operator= ( const NEBatchToSpaceLayerKernel )
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 158 of file NEBatchToSpaceLayerKernel.cpp.

159 {
163 
164  if (_block_shape != nullptr)
165  {
166  // Retrieve the block shapes dynamically
167  _block_shape_x = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(0)));
168  _block_shape_y = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(1)));
169  }
170 
171  const int batch_size = _output->info()->dimension(3);
172  const int element_size = _output->info()->element_size();
173 
174  Window slice_out = window.first_slice_window_3D();
175 
176  int batch_id = 0;
177  // Main loop for NCHW and NHWC
178  if (_data_layout == DataLayout::NCHW)
179  {
180  do
181  {
182  Iterator out(_output, slice_out);
184  slice_out,
185  [&](const Coordinates &id)
186  {
187  const int x = id.x();
188  const int y = id.y();
189  const int z = id.z();
190  // Translate x, y to uncropped version
191  const int x_c = x + _crop_info.left;
192  const int y_c = y + _crop_info.top;
193 
194  const int in_batch =
195  batch_id + ((x_c % _block_shape_x) + (y_c % _block_shape_y) * _block_shape_x) * batch_size;
196  const int in_x = x_c / _block_shape_x;
197  const int in_y = y_c / _block_shape_y;
198  Coordinates input_coords{in_x, in_y, z, in_batch};
199  memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
200  },
201  out);
202  ++batch_id;
203  } while (window.slide_window_slice_3D(slice_out));
204  }
205  else
206  {
207  // For NHWC we can perform a block copy on the Channel (first) dimension. Thus we do not need to iterate over this dimension
208  slice_out.set(0U, Window::Dimension(0U, 1U, 1U));
209  do
210  {
211  Iterator out(_output, slice_out);
213  slice_out,
214  [&](const Coordinates &id)
215  {
216  const int x = id.y();
217  const int y = id.z();
218 
219  // Translate x, y to uncropped version
220  const int x_c = x + _crop_info.left;
221  const int y_c = y + _crop_info.top;
222 
223  const int in_batch =
224  batch_id + ((x_c % _block_shape_x) + (y_c % _block_shape_y) * _block_shape_x) * batch_size;
225  const int in_x = x_c / _block_shape_x;
226  const int in_y = y_c / _block_shape_y;
227  Coordinates input_coords{0, in_x, in_y, in_batch};
228  memcpy(out.ptr(), _input->ptr_to_element(input_coords),
229  element_size * _input->info()->dimension(0));
230  },
231  out);
232  ++batch_id;
233  } while (window.slide_window_slice_3D(slice_out));
234  }
235 }

References ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, ITensorInfo::dimension(), ITensorInfo::element_size(), arm_compute::execute_window_loop(), Window::first_slice_window_3D(), ITensor::info(), arm_compute::test::validation::info, Padding2D::left, arm_compute::NCHW, Iterator::ptr(), ITensor::ptr_to_element(), Window::set(), Window::slide_window_slice_3D(), Padding2D::top, arm_compute::utils::cast::U, IKernel::window(), Window::x(), and Window::y().

◆ 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
Deprecated:
This method for dynamic block shape is not fully mature and will be removed in 23.08 release

Definition at line 140 of file NEBatchToSpaceLayerKernel.cpp.

141 {
142  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_shape, output);
144  return Status{};
145 }

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

Referenced by NEBatchToSpaceLayer::validate().

◆ validate() [2/2]

Status validate ( const ITensorInfo input,
int32_t  block_shape_x,
int32_t  block_shape_y,
const ITensorInfo output,
const CropInfo crop_info = CropInfo{} 
)
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
[in]crop_infoSpecifies how the output shape is cropped after batch to space is performed
Returns
a status

Definition at line 147 of file NEBatchToSpaceLayerKernel.cpp.

152 {
154  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, output, crop_info));
155  return Status{};
156 }

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


The documentation for this class was generated from the following files:
arm_compute::DataLayout::NCHW
@ NCHW
Num samples, channels, height, width.
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::Padding2D::top
size_t top
Padding across the height dimension on the top, in elements.
Definition: Types.h:617
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::Padding2D::left
size_t left
Padding across the width dimension on the left, in elements.
Definition: Types.h:615
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::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_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_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:151
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::misc::shape_calculator::compute_batch_to_space_shape
TensorShape compute_batch_to_space_shape(DataLayout data_layout, const TensorShape &input, int block_x, int block_y, const CropInfo &crop_info=CropInfo{})
Calculate the batch to space output shape of a tensor.
Definition: ShapeCalculator.h:1201
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_RETURN_ERROR_ON_NULLPTR
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
arm_compute::DataLayout::UNKNOWN
@ UNKNOWN
Unknown data layout.
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