Compute Library
 19.08
CLPadLayer Class Reference

Basic function to pad a tensor. More...

#include <CLPadLayer.h>

Collaboration diagram for CLPadLayer:
[legend]

Public Member Functions

 CLPadLayer ()
 Default constructor. More...
 
void configure (ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value=PixelValue(), 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, PixelValue constant_value=PixelValue(), PaddingMode mode=PaddingMode::CONSTANT)
 Static function to check if given info will lead to a valid configuration of CLPadLayer. More...
 

Detailed Description

Basic function to pad a tensor.

This function calls the following OpenCL kernels:

  1. CLMemsetKernel
  2. CLFillBorderKernel
  3. CLCopyKernel

Definition at line 47 of file CLPadLayer.h.

Constructor & Destructor Documentation

◆ CLPadLayer()

Default constructor.

Definition at line 33 of file CLPadLayer.cpp.

34  : _copy_kernel(), _mode(), _padding(), _memset_kernel(), _num_dimensions(0), _slice_functions(), _concat_functions(), _slice_results(), _concat_results()
35 {
36 }

Member Function Documentation

◆ configure()

void configure ( ICLTensor input,
ICLTensor output,
const PaddingList padding,
PixelValue  constant_value = PixelValue(),
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). Only CONSTANT is currently supported.

Definition at line 164 of file CLPadLayer.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  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  int64_t last_padding_dimension = _padding.size() - 1;
177  for(; last_padding_dimension >= 0; --last_padding_dimension)
178  {
179  if(_padding[last_padding_dimension].first > 0 || _padding[last_padding_dimension].second > 0)
180  {
181  break;
182  }
183  }
184  _num_dimensions = last_padding_dimension + 1;
185  if(_num_dimensions > 0)
186  {
187  switch(_mode)
188  {
190  {
191  configure_constant_mode(input, output, padding, constant_value);
192  break;
193  }
196  {
197  configure_reflect_symmetric_mode(input, output);
198  break;
199  }
200  default:
201  ARM_COMPUTE_ERROR("Padding mode not supported.");
202  }
203  }
204  else
205  {
206  // Copy the input to the whole output if no padding is applied
207  _copy_kernel.configure(input, output);
208  }
209 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
void configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding=PaddingList(), Window *output_window=nullptr)
Initialize the kernel's input, output.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value=PixelValue(), PaddingMode mode=PaddingMode::CONSTANT)
Static function to check if given info will lead to a valid configuration of CLPadLayer.
Definition: CLPadLayer.cpp:211
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
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(), ICloneable< T >::clone(), arm_compute::misc::shape_calculator::compute_padded_shape(), CLCopyKernel::configure(), arm_compute::CONSTANT, ITensor::info(), arm_compute::test::validation::padding, arm_compute::REFLECT, arm_compute::SYMMETRIC, ITensorInfo::tensor_shape(), and CLPadLayer::validate().

Referenced by CLFFTConvolutionLayer::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 274 of file CLPadLayer.cpp.

275 {
276  if(_num_dimensions > 0)
277  {
278  switch(_mode)
279  {
281  {
282  CLScheduler::get().enqueue(_memset_kernel, false);
283  CLScheduler::get().enqueue(_copy_kernel, true);
284  break;
285  }
288  {
289  for(uint32_t i = 0; i < _num_dimensions; ++i)
290  {
291  if(_padding[i].first > 0 || _padding[i].second > 0)
292  {
293  if(_padding[i].first > 0 && _slice_results[2 * i].info()->total_size() > 0)
294  {
295  _slice_functions[2 * i].run();
296  }
297  if(_padding[i].second > 0 && _slice_results[2 * i + 1].info()->total_size() > 0)
298  {
299  _slice_functions[2 * i + 1].run();
300  }
302  _concat_functions[i].run();
304  }
305  }
306  break;
307  }
308  default:
309  ARM_COMPUTE_ERROR("Padding mode not supported.");
310  }
311  }
312  else
313  {
314  CLScheduler::get().enqueue(_copy_kernel, true);
315  }
316 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:41
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: CLScheduler.cpp:95
void sync()
Blocks until all commands in the associated command queue have finished.
Definition: CLScheduler.h:151

References ARM_COMPUTE_ERROR, arm_compute::CONSTANT, CLScheduler::enqueue(), CLScheduler::get(), arm_compute::test::validation::info, arm_compute::REFLECT, arm_compute::SYMMETRIC, and CLScheduler::sync().

Referenced by CLFFTConvolutionLayer::prepare(), and CLFFTConvolutionLayer::run().

◆ validate()

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

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

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). Only CONSTANT is currently supported.

Definition at line 211 of file CLPadLayer.cpp.

212 {
213  ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > input->num_dimensions());
214 
215  TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
216 
217  // Use CLCopyKernel and CLMemsetKernel to validate all padding modes as this includes all of the shape and info validation.
218  PaddingList padding_extended = padding;
219  for(size_t i = padding.size(); i < TensorShape::num_max_dimensions; i++)
220  {
221  padding_extended.emplace_back(PaddingInfo{ 0, 0 });
222  }
223 
224  Window copy_window = Window();
225  for(uint32_t i = 0; i < padded_shape.num_dimensions(); ++i)
226  {
227  copy_window.set(i, Window::Dimension(padding_extended[i].first, padding_extended[i].first + input->dimension(i), 1));
228  }
229  if(output->total_size() > 0)
230  {
231  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
233  ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(input, output, PaddingList(), &copy_window));
234  ARM_COMPUTE_RETURN_ON_ERROR(CLMemsetKernel::validate(output, constant_value));
235  }
236  else
237  {
238  ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(input, &input->clone()->set_tensor_shape(padded_shape), PaddingList(), &copy_window));
239  ARM_COMPUTE_RETURN_ON_ERROR(CLMemsetKernel::validate(&input->clone()->set_tensor_shape(padded_shape), constant_value));
240  }
241 
242  switch(mode)
243  {
245  {
246  break;
247  }
250  {
251  for(uint32_t i = 0; i < padding.size(); ++i)
252  {
253  if(mode == PaddingMode::REFLECT)
254  {
255  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first >= input->dimension(i));
256  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second >= input->dimension(i));
257  }
258  else
259  {
260  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].first > input->dimension(i));
261  ARM_COMPUTE_RETURN_ERROR_ON(padding[i].second > input->dimension(i));
262  }
263  }
264  break;
265  }
266  default:
267  {
268  ARM_COMPUTE_ERROR("Invalid mode");
269  }
270  }
271  return Status{};
272 }
static Status validate(const ITensorInfo *tensor, const PixelValue &constant_value, Window *window=nullptr)
Static function to check if given info will lead to a valid configuration of CLMemsetKernel.
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
std::vector< PaddingInfo > PaddingList
List of padding information.
Definition: Types.h:445
#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
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding=PaddingList(), Window *output_window=nullptr)
Static function to check if given info will lead to a valid configuration of CLCopyKernel.
TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
Calculate the padded shape of a tensor.
std::pair< uint32_t, uint32_t > PaddingInfo
Padding information as a pair of unsigned int start/end.
Definition: Types.h:442
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45

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, ICloneable< T >::clone(), arm_compute::misc::shape_calculator::compute_padded_shape(), arm_compute::CONSTANT, ITensorInfo::dimension(), ITensorInfo::num_dimensions(), Dimensions< size_t >::num_max_dimensions, arm_compute::test::validation::padding, arm_compute::REFLECT, Window::set(), arm_compute::SYMMETRIC, ITensorInfo::tensor_shape(), ITensorInfo::total_size(), CLCopyKernel::validate(), and CLMemsetKernel::validate().

Referenced by CLPadLayer::configure().


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