Compute Library
 19.08
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...
 
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 NEON kernels:

  1. NEMemsetKernel
  2. NECopyKernel

Definition at line 44 of file NEPadLayer.h.

Constructor & Destructor Documentation

◆ NEPadLayer()

Default constructor.

Definition at line 78 of file NEPadLayer.cpp.

79  : _copy_kernel(), _mode(), _padding(), _memset_kernel(), _num_dimensions(0), _slice_functions(), _concat_functions(), _slice_results(), _concat_results(), _output_subtensor()
80 {
81 }

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.

Parameters
[in]inputSource tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32.
[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 201 of file NEPadLayer.cpp.

202 {
203  ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
204 
205  _padding = padding;
206  _mode = mode;
207 
208  const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), _padding);
209 
210  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(padded_shape));
211 
212  // Find the last dimension requiring padding so that it is known when to write to output and whether any padding is applied.
213  _num_dimensions = last_padding_dimension(padding) + 1;
214  if(_num_dimensions > 0)
215  {
216  switch(_mode)
217  {
219  {
220  configure_constant_mode(input, output, padding, constant_value);
221  break;
222  }
225  {
226  configure_reflect_symmetric_mode(input, output);
227  break;
228  }
229  default:
230  ARM_COMPUTE_ERROR("Padding mode not supported.");
231  }
232  }
233  else
234  {
235  // Copy the input to the whole output if no padding is applied
236  _copy_kernel.configure(input, output);
237  }
238 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
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: Helpers.inl:201
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:240
TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
Calculate the padded shape of a tensor.
void configure(const ITensor *input, ITensor *output)
Initialize the kernel's input, output.

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::auto_init_if_empty(), ICloneable< T >::clone(), arm_compute::misc::shape_calculator::compute_padded_shape(), NECopyKernel::configure(), arm_compute::CONSTANT, ITensor::info(), arm_compute::test::validation::padding, arm_compute::REFLECT, arm_compute::SYMMETRIC, ITensorInfo::tensor_shape(), and NEPadLayer::validate().

Referenced by NEFFTConvolutionLayer::configure().

◆ run()

void run ( )
overridevirtual

Run the kernels contained in the function.

For NEON 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 288 of file NEPadLayer.cpp.

289 {
290  if(_num_dimensions > 0)
291  {
292  switch(_mode)
293  {
295  {
296  NEScheduler::get().schedule(&_memset_kernel, Window::DimY);
297  NEScheduler::get().schedule(&_copy_kernel, Window::DimY);
298  break;
299  }
302  {
303  for(uint32_t i = 0; i < _num_dimensions; ++i)
304  {
305  if(_padding[i].first > 0 || _padding[i].second > 0)
306  {
307  if(_padding[i].first > 0 && _slice_results[2 * i].info()->total_size() > 0)
308  {
309  _slice_functions[2 * i].run();
310  }
311  if(_padding[i].second > 0 && _slice_results[2 * i + 1].info()->total_size() > 0)
312  {
313  _slice_functions[2 * i + 1].run();
314  }
315  _concat_functions[i].run();
316  }
317  }
318  break;
319  }
320  default:
321  ARM_COMPUTE_ERROR("Padding mode not supported.");
322  }
323  }
324  else
325  {
326  NEScheduler::get().schedule(&_copy_kernel, Window::DimY);
327  }
328 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:96

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

Referenced by NEFFTConvolutionLayer::prepare(), 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: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32.
[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 240 of file NEPadLayer.cpp.

241 {
242  ARM_COMPUTE_UNUSED(constant_value);
243 
244  const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
245 
246  if(output->total_size() > 0)
247  {
248  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
250  }
251 
252  switch(mode)
253  {
255  {
256  auto output_clone = output->clone();
257  SubTensorInfo output_subtensor_info(output_clone.get(), input->tensor_shape(), get_subtensor_coords(padding), true);
258  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input, *output_clone, padding));
259  ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(input, &output_subtensor_info));
260  break;
261  }
264  {
265  for(uint32_t i = 0; i < padding.size(); ++i)
266  {
267  if(mode == PaddingMode::REFLECT)
268  {
269  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first >= input->dimension(i));
270  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second >= input->dimension(i));
271  }
272  else
273  {
274  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first > input->dimension(i));
275  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second > input->dimension(i));
276  }
277  }
278  break;
279  }
280  default:
281  {
282  ARM_COMPUTE_ERROR("Invalid mode");
283  }
284  }
285  return Status{};
286 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:244
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of NECopyKernel.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
Calculate the padded shape of a tensor.

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_RETURN_ON_ERROR, ARM_COMPUTE_UNUSED, ICloneable< T >::clone(), arm_compute::misc::shape_calculator::compute_padded_shape(), arm_compute::CONSTANT, ITensorInfo::dimension(), arm_compute::test::validation::padding, arm_compute::REFLECT, arm_compute::SYMMETRIC, ITensorInfo::tensor_shape(), ITensorInfo::total_size(), and NECopyKernel::validate().

Referenced by NEPadLayer::configure().


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