Compute Library
 19.11
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 functions/kernels:

Definition at line 49 of file NEPadLayer.h.

Constructor & Destructor Documentation

◆ NEPadLayer()

Default constructor.

Definition at line 51 of file NEPadLayer.cpp.

52  : _copy_kernel(), _pad_kernel(), _mode(), _padding(), _num_dimensions(0), _slice_functions(), _concat_functions(), _slice_results(), _concat_results()
53 {
54 }

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 164 of file NEPadLayer.cpp.

165 {
166  ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), padding, constant_value, mode));
167 
168  _padding = padding;
169  _mode = mode;
170 
171  const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), _padding);
172 
173  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(padded_shape));
174 
175  // Find the last dimension requiring padding so that it is known when to write to output and whether any padding is applied.
176  _num_dimensions = last_padding_dimension(padding) + 1;
177  if(_num_dimensions > 0)
178  {
179  switch(_mode)
180  {
182  {
183  configure_constant_mode(input, output, padding, constant_value);
184  break;
185  }
188  {
189  configure_reflect_symmetric_mode(input, output);
190  break;
191  }
192  default:
193  ARM_COMPUTE_ERROR("Padding mode not supported.");
194  }
195  }
196  else
197  {
198  // Copy the input to the whole output if no padding is applied
199  _copy_kernel.configure(input, output);
200  }
201 }
#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
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:202
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:203
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, const PaddingList &padding=PaddingList())
Initialize the kernel's input, output.

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::auto_init_if_empty(), arm_compute::misc::shape_calculator::compute_padded_shape(), NECopyKernel::configure(), arm_compute::CONSTANT, ITensor::info(), arm_compute::test::validation::input, arm_compute::test::validation::padding, arm_compute::REFLECT, arm_compute::SYMMETRIC, 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 247 of file NEPadLayer.cpp.

248 {
249  if(_num_dimensions > 0)
250  {
251  switch(_mode)
252  {
254  {
255  NEScheduler::get().schedule(&_pad_kernel, Window::DimZ);
256  break;
257  }
260  {
261  for(uint32_t i = 0; i < _num_dimensions; ++i)
262  {
263  if(_padding[i].first > 0 || _padding[i].second > 0)
264  {
265  if(_padding[i].first > 0 && _slice_results[2 * i].info()->total_size() > 0)
266  {
267  _slice_functions[2 * i].run();
268  }
269  if(_padding[i].second > 0 && _slice_results[2 * i + 1].info()->total_size() > 0)
270  {
271  _slice_functions[2 * i + 1].run();
272  }
273  _concat_functions[i].run();
274  }
275  }
276  break;
277  }
278  default:
279  ARM_COMPUTE_ERROR("Padding mode not supported.");
280  }
281  }
282  else
283  {
284  NEScheduler::get().schedule(&_copy_kernel, Window::DimY);
285  }
286 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
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 constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:95

References ARM_COMPUTE_ERROR, arm_compute::CONSTANT, Window::DimY, Window::DimZ, 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 203 of file NEPadLayer.cpp.

204 {
205  ARM_COMPUTE_UNUSED(constant_value);
206 
207  const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
208 
209  if(output->total_size() > 0)
210  {
211  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
213  }
214 
215  switch(mode)
216  {
218  {
219  return NEPadLayerKernel::validate(input, output, padding, constant_value, mode);
220  }
223  {
224  for(uint32_t i = 0; i < padding.size(); ++i)
225  {
226  if(mode == PaddingMode::REFLECT)
227  {
228  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first >= input->dimension(i));
229  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second >= input->dimension(i));
230  }
231  else
232  {
233  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first > input->dimension(i));
234  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second > input->dimension(i));
235  }
236  }
237  break;
238  }
239  default:
240  {
241  ARM_COMPUTE_ERROR("Invalid mode");
242  }
243  }
244  return Status{};
245 }
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#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_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#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.
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.

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, arm_compute::test::validation::padding, arm_compute::REFLECT, arm_compute::SYMMETRIC, ITensorInfo::tensor_shape(), ITensorInfo::total_size(), and NEPadLayerKernel::validate().

Referenced by NEPadLayer::configure().


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