Compute Library
 21.05
NEPadLayer Class Reference

Basic function to pad a tensor. More...

#include <NEPadLayer.h>

Collaboration diagram for NEPadLayer:
[legend]

Public Member Functions

 NEPadLayer ()
 Default Constructor. More...
 
 NEPadLayer (const NEPadLayer &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NEPadLayeroperator= (const NEPadLayer &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NEPadLayer (NEPadLayer &&)=delete
 Prevent instances of this class from being moved (As this class contains non movable objects) More...
 
NEPadLayeroperator= (NEPadLayer &&)=delete
 Prevent instances of this class from being moved (As this class contains non movable objects) More...
 
 ~NEPadLayer ()
 Default destructor. More...
 
void configure (ITensor *input, ITensor *output, const PaddingList &padding, const PixelValue constant_value=PixelValue(), const PaddingMode mode=PaddingMode::CONSTANT)
 Initialize the function. More...
 
void run () override
 Run the kernels contained in the function. More...
 
- Public Member Functions inherited from IFunction
virtual ~IFunction ()=default
 Destructor. More...
 
virtual void prepare ()
 Prepare the function for executing. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, const PixelValue constant_value=PixelValue(), const PaddingMode mode=PaddingMode::CONSTANT)
 Static function to check if given info will lead to a valid configuration of NEPadLayer. More...
 

Detailed Description

Basic function to pad a tensor.

This function calls the following functions/kernels:

Definition at line 51 of file NEPadLayer.h.

Constructor & Destructor Documentation

◆ NEPadLayer() [1/3]

Default Constructor.

Definition at line 53 of file NEPadLayer.cpp.

54  : _copy_function(), _pad_kernel(), _mode(), _padding(), _num_dimensions(0), _slice_functions(), _concat_functions(), _slice_results(), _concat_results()
55 {
56 }

◆ NEPadLayer() [2/3]

NEPadLayer ( const NEPadLayer )
delete

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

◆ NEPadLayer() [3/3]

NEPadLayer ( NEPadLayer &&  )
delete

Prevent instances of this class from being moved (As this class contains non movable objects)

◆ ~NEPadLayer()

~NEPadLayer ( )
default

Default destructor.

Member Function Documentation

◆ configure()

void configure ( ITensor input,
ITensor output,
const PaddingList padding,
const PixelValue  constant_value = PixelValue(),
const PaddingMode  mode = PaddingMode::CONSTANT 
)

Initialize the function.

Valid data layouts:

  • NHWC
  • NCHW

Valid data type configurations:

src dst
All All
Parameters
[in]inputSource tensor. Data types supported: All.
[out]outputOutput tensor. Data type supported: same as input
[in]paddingThe padding for each spatial dimension of the input tensor. The pair padding[i] specifies the front and the end padding in the i-th dimension.
[in]constant_value(Optional) Constant value to be used for the padding
[in]mode(Optional) Controls whether the padding should be filled with constant_value using CONSTANT, or reflect the input, either including the border values (SYMMETRIC) or not (REFLECT).

Definition at line 167 of file NEPadLayer.cpp.

168 {
169  ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
170 
171  _padding = padding;
172  _mode = mode;
173 
174  const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), _padding);
175 
176  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(padded_shape));
177 
178  // Find the last dimension requiring padding so that it is known when to write to output and whether any padding is applied.
179  _num_dimensions = last_padding_dimension(padding) + 1;
180  if(_num_dimensions > 0)
181  {
182  switch(_mode)
183  {
185  {
186  configure_constant_mode(input, output, padding, constant_value);
187  break;
188  }
191  {
192  configure_reflect_symmetric_mode(input, output);
193  break;
194  }
195  default:
196  ARM_COMPUTE_ERROR("Padding mode not supported.");
197  }
198  }
199  else
200  {
201  // Copy the input to the whole output if no padding is applied
202  _copy_function.configure(input, output);
203  }
204 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, const PixelValue constant_value=PixelValue(), const PaddingMode mode=PaddingMode::CONSTANT)
Static function to check if given info will lead to a valid configuration of NEPadLayer.
Definition: NEPadLayer.cpp:206
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_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
Calculate the padded shape of a tensor.
void configure(ITensor *input, ITensor *output)
Initialise the function's source and destination.
Definition: NECopy.cpp:48

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::auto_init_if_empty(), arm_compute::misc::shape_calculator::compute_padded_shape(), NECopy::configure(), arm_compute::CONSTANT, ITensor::info(), arm_compute::test::validation::input, clang_tidy_rules::mode, arm_compute::REFLECT, arm_compute::SYMMETRIC, and NEPadLayer::validate().

Referenced by NEFFTConvolutionLayer::configure(), and NEGenerateProposalsLayer::configure().

◆ operator=() [1/2]

NEPadLayer& operator= ( const NEPadLayer )
delete

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

◆ operator=() [2/2]

NEPadLayer& operator= ( NEPadLayer &&  )
delete

Prevent instances of this class from being moved (As this class contains non movable objects)

◆ run()

void run ( )
overridevirtual

Run the kernels contained in the function.

For CPU kernels:

  • Multi-threading is used for the kernels which are parallelisable.
  • By default std::thread::hardware_concurrency() threads are used.
Note
CPPScheduler::set_num_threads() can be used to manually set the number of threads

For OpenCL kernels:

  • All the kernels are enqueued on the queue associated with CLScheduler.
  • The queue is then flushed.
Note
The function will not block until the kernels are executed. It is the user's responsibility to wait.
Will call prepare() on first run if hasn't been done

Implements IFunction.

Definition at line 250 of file NEPadLayer.cpp.

251 {
252  if(_num_dimensions > 0)
253  {
254  switch(_mode)
255  {
257  {
258  NEScheduler::get().schedule(_pad_kernel.get(), Window::DimZ);
259  break;
260  }
263  {
264  for(uint32_t i = 0; i < _num_dimensions; ++i)
265  {
266  if(_padding[i].first > 0 || _padding[i].second > 0)
267  {
268  if(_padding[i].first > 0 && _slice_results[2 * i].info()->total_size() > 0)
269  {
270  _slice_functions[2 * i].run();
271  }
272  if(_padding[i].second > 0 && _slice_results[2 * i + 1].info()->total_size() > 0)
273  {
274  _slice_functions[2 * i + 1].run();
275  }
276  _concat_functions[i].run();
277  }
278  }
279  break;
280  }
281  default:
282  ARM_COMPUTE_ERROR("Padding mode not supported.");
283  }
284  }
285  else
286  {
287  _copy_function.run();
288  }
289 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
void run() override
Run the kernels contained in the function.
Definition: NECopy.cpp:66
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94

References ARM_COMPUTE_ERROR, arm_compute::CONSTANT, Window::DimZ, Scheduler::get(), arm_compute::test::validation::info, arm_compute::REFLECT, NECopy::run(), IScheduler::schedule(), and arm_compute::SYMMETRIC.

Referenced by NEFFTConvolutionLayer::prepare(), NEGenerateProposalsLayer::run(), and NEFFTConvolutionLayer::run().

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
const PaddingList padding,
const PixelValue  constant_value = PixelValue(),
const PaddingMode  mode = PaddingMode::CONSTANT 
)
static

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

Parameters
[in]inputSource tensor info. Data types supported: All.
[in]outputOutput tensor info. Data type supported: same as input
[in]paddingThe padding for each spatial dimension of the input tensor. The pair padding[i] specifies the front and the end padding in the i-th dimension.
[in]constant_value(Optional) Constant value to be used for the padding
[in]mode(Optional) Controls whether the padding should be filled with constant_value using CONSTANT, or reflect the input, either including the border values (SYMMETRIC) or not (REFLECT).
Returns
a status

Definition at line 206 of file NEPadLayer.cpp.

207 {
208  ARM_COMPUTE_UNUSED(constant_value);
209 
210  const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
211 
212  if(output->total_size() > 0)
213  {
214  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
216  }
217 
218  switch(mode)
219  {
221  {
222  return NEPadLayerKernel::validate(input, output, padding, constant_value, mode);
223  }
226  {
227  for(uint32_t i = 0; i < padding.size(); ++i)
228  {
230  {
231  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first >= input->dimension(i));
232  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second >= input->dimension(i));
233  }
234  else
235  {
236  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first > input->dimension(i));
237  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second > input->dimension(i));
238  }
239  }
240  break;
241  }
242  default:
243  {
244  ARM_COMPUTE_ERROR("Invalid mode");
245  }
246  }
247  return Status{};
248 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:284
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
Calculate the padded shape of a tensor.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, const PixelValue constant_value=PixelValue(), const PaddingMode mode=PaddingMode::CONSTANT)
Static function to check if given info will lead to a valid configuration of NEPadLayer.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541

References ARM_COMPUTE_ERROR, ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS, ARM_COMPUTE_UNUSED, arm_compute::misc::shape_calculator::compute_padded_shape(), arm_compute::CONSTANT, arm_compute::test::validation::input, clang_tidy_rules::mode, arm_compute::REFLECT, arm_compute::SYMMETRIC, ITensorInfo::tensor_shape(), ITensorInfo::total_size(), and NEPadLayerKernel::validate().

Referenced by NEPadLayer::configure(), and NEGenerateProposalsLayer::validate().


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