Compute Library
 20.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...
 
 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 NEON functions/kernels:

Definition at line 51 of file NEPadLayer.h.

Constructor & Destructor Documentation

◆ NEPadLayer() [1/3]

Default Constructor.

Definition at line 55 of file NEPadLayer.cpp.

56  : _copy_kernel(), _pad_kernel(), _mode(), _padding(), _num_dimensions(0), _slice_functions(), _concat_functions(), _slice_results(), _concat_results()
57 {
58 }

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

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

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

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::auto_init_if_empty(), arm_compute::misc::shape_calculator::compute_padded_shape(), 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 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 253 of file NEPadLayer.cpp.

254 {
255  if(_num_dimensions > 0)
256  {
257  switch(_mode)
258  {
260  {
261  NEScheduler::get().schedule(_pad_kernel.get(), Window::DimZ);
262  break;
263  }
266  {
267  for(uint32_t i = 0; i < _num_dimensions; ++i)
268  {
269  if(_padding[i].first > 0 || _padding[i].second > 0)
270  {
271  if(_padding[i].first > 0 && _slice_results[2 * i].info()->total_size() > 0)
272  {
273  _slice_functions[2 * i].run();
274  }
275  if(_padding[i].second > 0 && _slice_results[2 * i + 1].info()->total_size() > 0)
276  {
277  _slice_functions[2 * i + 1].run();
278  }
279  _concat_functions[i].run();
280  }
281  }
282  break;
283  }
284  default:
285  ARM_COMPUTE_ERROR("Padding mode not supported.");
286  }
287  }
288  else
289  {
290  NEScheduler::get().schedule(_copy_kernel.get(), Window::DimY);
291  }
292 }
#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
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
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(), 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 209 of file NEPadLayer.cpp.

210 {
211  ARM_COMPUTE_UNUSED(constant_value);
212 
213  const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
214 
215  if(output->total_size() > 0)
216  {
217  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
219  }
220 
221  switch(mode)
222  {
224  {
225  return NEPadLayerKernel::validate(input, output, padding, constant_value, mode);
226  }
229  {
230  for(uint32_t i = 0; i < padding.size(); ++i)
231  {
233  {
234  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first >= input->dimension(i));
235  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second >= input->dimension(i));
236  }
237  else
238  {
239  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first > input->dimension(i));
240  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second > input->dimension(i));
241  }
242  }
243  break;
244  }
245  default:
246  {
247  ARM_COMPUTE_ERROR("Invalid mode");
248  }
249  }
250  return Status{};
251 }
#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:288
#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:545

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: