Compute Library
 21.08
CpuIm2ColKernel Class Reference

Interface for the im2col reshape kernel. More...

#include <CpuIm2ColKernel.h>

Collaboration diagram for CpuIm2ColKernel:
[legend]

Public Member Functions

 CpuIm2ColKernel ()=default
 Default constructor. More...
 
 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE (CpuIm2ColKernel)
 
void configure (const ITensorInfo *src, ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U), unsigned int num_groups=1)
 Set the input and output of the kernel. More...
 
void run_op (ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
 Execute the kernel on the passed window. More...
 
const char * name () const override
 Name of the kernel. More...
 
- Public Member Functions inherited from ICPPKernel
virtual ~ICPPKernel ()=default
 Default destructor. More...
 
virtual void run (const Window &window, const ThreadInfo &info)
 Execute the kernel on the passed window. More...
 
virtual void run_nd (const Window &window, const ThreadInfo &info, const Window &thread_locator)
 legacy compatibility layer for implemantions which do not support thread_locator In these cases we simply narrow the interface down the legacy version More...
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
virtual BorderSize border_size () const
 The size of the border for that kernel. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 
bool is_window_configured () const
 Function to check if the embedded window of this kernel has been configured. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *src, const ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U), unsigned int num_groups=1)
 Static function to check if given info will lead to a valid configuration. More...
 

Detailed Description

Interface for the im2col reshape kernel.

Rearranges image blocks into columns. It is used to strip out each convolution block to a single column. It is used to transform a convolution to a plain matrix multiplication.

For example taking into account the image below and assuming 3x3 image blocks with stride of 1 we have:

\[ \left( \begin{array}{cccc} a00 & a01 & a02 & a03 \\ a10 & a11 & a12 & a13 \\ a20 & a21 & a22 & a23 \\ a30 & a31 & a32 & a33 \\ \end{array} \right) \rightarrow \left( \begin{array}{ccccccccc} a00 & a01 & a02 & a10 & a11 & a12 & a20 & a21 & a22 \\ a01 & a02 & a03 & a11 & a12 & a13 & a21 & a22 & a23 \\ a10 & a11 & a12 & a20 & a21 & a22 & a30 & a31 & a32 \\ a11 & a12 & a13 & a21 & a22 & a23 & a31 & a32 & a33 \\ \end{array} \right) \]

Definition at line 61 of file CpuIm2ColKernel.h.

Constructor & Destructor Documentation

◆ CpuIm2ColKernel()

CpuIm2ColKernel ( )
default

Default constructor.

Member Function Documentation

◆ ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE()

ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE ( CpuIm2ColKernel  )

◆ configure()

void configure ( const ITensorInfo src,
ITensorInfo dst,
const Size2D kernel_dims,
const PadStrideInfo conv_info,
bool  has_bias,
const Size2D dilation = Size2D(1U, 1U),
unsigned int  num_groups = 1 
)

Set the input and output of the kernel.

Parameters
[in]srcThe input tensor info to convert. 3 lower dimensions represent a single input [width, height, IFM], while every optional dimension from 4 and above represent a batch of inputs. Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32 Note: QASYMM8/QASYMM8_SIGNED works only for has_bias = false
[out]dstThe output tensor info. Data types supported: Same as input
[in]kernel_dimsThe kernel dimensions (width and height).
[in]conv_infoContains padding and stride information described in PadStrideInfo.
[in]has_biasIn case biases are provided expands the matrix with 1.
[in]dilation(Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
[in]num_groups(Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported

Definition at line 334 of file CpuIm2ColKernel.cpp.

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, ARM_COMPUTE_UNUSED, arm_compute::auto_init_if_empty(), arm_compute::BFLOAT16, arm_compute::calculate_max_window(), arm_compute::CHANNEL, ICloneable< T >::clone(), arm_compute::misc::shape_calculator::compute_im2col_conv_shape(), arm_compute::test::validation::configure(), arm_compute::test::validation::conv_info, ITensorInfo::data_layout(), ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::F16, arm_compute::F32, arm_compute::get_data_layout_dimension_index(), arm_compute::test::validation::has_bias, PadStrideInfo::has_padding(), Size2D::height, arm_compute::HEIGHT, arm_compute::NCHW, arm_compute::QASYMM8, arm_compute::QASYMM8_SIGNED, arm_compute::scaled_dimensions(), Size2D::width, and arm_compute::WIDTH.

336 {
338  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, kernel_dims, conv_info, has_bias, dilation, num_groups));
340 
341  _data_layout = src->data_layout();
342  const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
343  const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
344  const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
345 
346  _conv_info = conv_info;
347  _kernel_width = kernel_dims.width;
348  _kernel_height = kernel_dims.height;
349  _dilation = dilation;
350  _convolved_dims = scaled_dimensions(src->dimension(width_idx), dst->dimension(height_idx),
351  _kernel_width, _kernel_height,
352  _conv_info, _dilation);
353  _has_bias = has_bias;
354 
355  if(_data_layout == DataLayout::NCHW)
356  {
357  switch(src->data_type())
358  {
359  case DataType::F32:
360  _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float, false, true> : &CpuIm2ColKernel::run_im2col<float, true, true>;
361  break;
362 #if defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16)
363  case DataType::BFLOAT16:
364  _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<bfloat16, false, true> : &CpuIm2ColKernel::run_im2col<bfloat16, true, true>;
365  break;
366 #endif /* defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16) */
367 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
368  case DataType::F16:
369  _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float16_t, false, true> : &CpuIm2ColKernel::run_im2col<float16_t, true, true>;
370  break;
371 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
373  case DataType::QASYMM8:
374  _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<qasymm8_t, false, true> : &CpuIm2ColKernel::run_im2col<qasymm8_t, true, true>;
375  break;
376  default:
377  ARM_COMPUTE_ERROR("Data type not supported");
378  break;
379  }
380  }
381  else
382  {
383  switch(src->data_type())
384  {
385  case DataType::F32:
386  _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float, false, false> : &CpuIm2ColKernel::run_im2col<float, true, false>;
387  break;
388 #if defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16)
389  case DataType::BFLOAT16:
390  _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<bfloat16, false, false> : &CpuIm2ColKernel::run_im2col<bfloat16, true, false>;
391  break;
392 #endif /* defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16) */
393 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
394  case DataType::F16:
395  _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float16_t, false, false> : &CpuIm2ColKernel::run_im2col<float16_t, true, false>;
396  break;
397 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
398  case DataType::QASYMM8:
399  _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<uint8_t, false, false> : &CpuIm2ColKernel::run_im2col<qasymm8_t, true, false>;
400  break;
402  _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<int8_t, false, false> : &CpuIm2ColKernel::run_im2col<qasymm8_t, true, false>;
403  break;
404  default:
405  ARM_COMPUTE_ERROR("Data type not supported");
406  break;
407  }
408  }
409 
410  // Output tensor auto initialization if not yet initialized
411  auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_im2col_conv_shape(src, kernel_dims, conv_info, has_bias, dilation, false)));
412 
413  std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(src->dimension(width_idx), src->dimension(height_idx),
414  kernel_dims.width, kernel_dims.height,
415  conv_info, dilation);
416 
417  Window win = calculate_max_window(*src, Steps());
418  win.set(width_idx, Window::Dimension(0, convolved_dims.first, 1));
419  win.set(height_idx, Window::Dimension(0, convolved_dims.second, 1));
420  win.set(channel_idx, Window::Dimension(0, 1, 1));
421  // Configure kernel window
422  ICpuKernel::configure(win);
423 }
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
SimpleTensor< float > src
Definition: DFT.cpp:155
1 channel, 1 F16 per channel
std::pair< unsigned int, unsigned int > scaled_dimensions(int width, int height, int kernel_width, int kernel_height, const PadStrideInfo &pad_stride_info, const Size2D &dilation=Size2D(1U, 1U))
Returns expected width and height of output scaled tensor depending on dimensions rounding mode...
Definition: Utils.cpp:395
16-bit brain floating-point number
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
quantized, asymmetric fixed-point 8-bit number unsigned
const unsigned int num_groups
Definition: Im2Col.cpp:153
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...
Num samples, channels, height, width.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
quantized, asymmetric fixed-point 8-bit number signed
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
TensorShape compute_im2col_conv_shape(const ITensorInfo *input, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation, bool batch_size_on_z, unsigned int num_groups=1)
Calculate the im2col output shape of a tensor.

◆ name()

const char * name ( ) const
overridevirtual

Name of the kernel.

Returns
Kernel name

Implements ICPPKernel.

Definition at line 442 of file CpuIm2ColKernel.cpp.

443 {
444  return "CpuIm2ColKernel";
445 }

◆ run_op()

void run_op ( ITensorPack tensors,
const Window window,
const ThreadInfo info 
)
overridevirtual

Execute the kernel on the passed window.

Warning
If is_parallelisable() returns false then the passed window must be equal to window()
Note
The window has to be a region within the window returned by the window() method
The width of the window has to be a multiple of num_elems_processed_per_iteration().
Parameters
[in]tensorsA vector containing the tensors to operate on.
[in]windowRegion on which to execute the kernel. (Must be a region of the window returned by window())
[in]infoInfo about executing thread and CPU.

Reimplemented from ICPPKernel.

Definition at line 432 of file CpuIm2ColKernel.cpp.

References arm_compute::ACL_DST, arm_compute::ACL_SRC, ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, ITensorPack::get_const_tensor(), and ITensorPack::get_tensor().

433 {
437 
438  auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
439  auto dst = tensors.get_tensor(TensorType::ACL_DST);
440  (this->*_func)(src, dst, window);
441 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
SimpleTensor< float > src
Definition: DFT.cpp:155
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201

◆ validate()

Status validate ( const ITensorInfo src,
const ITensorInfo dst,
const Size2D kernel_dims,
const PadStrideInfo conv_info,
bool  has_bias,
const Size2D dilation = Size2D(1U, 1U),
unsigned int  num_groups = 1 
)
static

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

Similar to CpuIm2ColKernel::configure()

Returns
a status

Definition at line 425 of file CpuIm2ColKernel.cpp.

References ARM_COMPUTE_RETURN_ON_ERROR.

Referenced by arm_compute::test::validation::DATA_TEST_CASE(), and CpuGemmConvolution::validate().

427 {
428  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, kernel_dims, conv_info, has_bias, dilation, num_groups));
429  return Status{};
430 }
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
SimpleTensor< float > src
Definition: DFT.cpp:155
const unsigned int num_groups
Definition: Im2Col.cpp:153

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