Compute Library
 21.02
NEConvolutionKernel< matrix_size > Class Template Reference

Interface for the kernel to run an arbitrary size convolution on a tensor. More...

#include <NEConvolution.h>

Collaboration diagram for NEConvolutionKernel< matrix_size >:
[legend]

Public Member Functions

const char * name () const override
 Name of the kernel. More...
 
 NEConvolutionKernel ()
 Default constructor. More...
 
 NEConvolutionKernel (const NEConvolutionKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers). More...
 
NEConvolutionKerneloperator= (const NEConvolutionKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers). More...
 
 NEConvolutionKernel (NEConvolutionKernel &&)=default
 Allow instances of this class to be moved. More...
 
NEConvolutionKerneloperator= (NEConvolutionKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~NEConvolutionKernel ()=default
 Default destructor. More...
 
void configure (const ITensor *input, ITensor *output, const int16_t *conv, uint32_t scale, bool border_undefined)
 Initialise the kernel's input, output and border mode. More...
 
void run (const Window &window, const ThreadInfo &info) override
 Execute the kernel on the passed window. More...
 
BorderSize border_size () const override
 The size of the border for that kernel. More...
 
- Public Member Functions inherited from ICPPSimpleKernel
 ICPPSimpleKernel ()
 Constructor. More...
 
 ICPPSimpleKernel (const ICPPSimpleKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
ICPPSimpleKerneloperator= (const ICPPSimpleKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 ICPPSimpleKernel (ICPPSimpleKernel &&)=default
 Allow instances of this class to be moved. More...
 
ICPPSimpleKerneloperator= (ICPPSimpleKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~ICPPSimpleKernel ()=default
 Default destructor. More...
 
- Public Member Functions inherited from ICPPKernel
virtual ~ICPPKernel ()=default
 Default destructor. 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...
 
virtual void run_op (ITensorPack &tensors, const Window &window, const ThreadInfo &info)
 Execute the kernel on the passed window. 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...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Detailed Description

template<unsigned int matrix_size>
class arm_compute::NEConvolutionKernel< matrix_size >

Interface for the kernel to run an arbitrary size convolution on a tensor.

(Currently supports 3x3, 5x5, 7x7 and 9x9). The client can supply a convolution matrix \( C_{m,n} \).

\begin{eqnarray} k_0 &=& \frac{m}{2} \\ l_0 &=& \frac{n}{2} \\ sum &=& \sum_{k=0,l=0}^{k=m-1,l=n-1} input(x+k-k_0, y+l-l_0) C_{k,l} \end{eqnarray}

Note
The above equation for this function is similar to the default OpenCV Filter2D function, which actually computes a correlation and not a convolution. In case of a real convolution the convolution matrix should be flipped both horizontally and vertically.

Definition at line 42 of file NEConvolution.h.

Constructor & Destructor Documentation

◆ NEConvolutionKernel() [1/3]

Default constructor.

Definition at line 314 of file NEConvolutionKernel.cpp.

Referenced by NEConvolutionKernel< matrix_size >::name().

315  : INESimpleKernel(), _scale(0), _convolution{ {} }
316 {
317 }
ICPPSimpleKernel INESimpleKernel
Interface for simple Neon kernels having 1 tensor input and 1 tensor output.

◆ NEConvolutionKernel() [2/3]

NEConvolutionKernel ( const NEConvolutionKernel< matrix_size > &  )
delete

Prevent instances of this class from being copied (As this class contains pointers).

◆ NEConvolutionKernel() [3/3]

NEConvolutionKernel ( NEConvolutionKernel< matrix_size > &&  )
default

Allow instances of this class to be moved.

◆ ~NEConvolutionKernel()

~NEConvolutionKernel ( )
default

Default destructor.

Referenced by NEConvolutionKernel< matrix_size >::name().

Member Function Documentation

◆ border_size()

BorderSize border_size ( ) const
overridevirtual

The size of the border for that kernel.

Returns
The width in number of elements of the border.

Reimplemented from IKernel.

Definition at line 320 of file NEConvolutionKernel.cpp.

Referenced by NEConvolutionKernel< matrix_size >::configure(), NEConvolutionKernel< matrix_size >::name(), NESeparableConvolutionHorKernel< matrix_size >::name(), NESeparableConvolutionVertKernel< matrix_size >::name(), and NEConvolutionRectangleKernel::name().

321 {
322  return BorderSize{ matrix_size / 2 };
323 }

◆ configure()

void configure ( const ITensor input,
ITensor output,
const int16_t *  conv,
uint32_t  scale,
bool  border_undefined 
)

Initialise the kernel's input, output and border mode.

Parameters
[in]inputSource tensor. Data type supported: U8.
[out]outputDestination tensor. Data types supported: U8, S16.
[in]convConvolution matrix to apply to the input tensor.
[in]scaleScale of the convolution matrix. If 0 is passed, it will be set to the sum of the coefficients of the convolution or 1 if they add up to 0.
[in]border_undefinedTrue if the border mode is undefined. False if it's replicate or constant.

Definition at line 326 of file NEConvolutionKernel.cpp.

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES, ARM_COMPUTE_ERROR_ON_NULLPTR, NEConvolutionKernel< matrix_size >::border_size(), arm_compute::calculate_matrix_scale(), arm_compute::calculate_max_window(), arm_compute::execute_window_loop(), ITensor::info(), arm_compute::test::validation::input, BorderSize::left, num_elems_processed_per_iteration, Iterator::ptr(), arm_compute::S16, arm_compute::test::validation::scale, arm_compute::set_shape_if_empty(), arm_compute::detail::store_results(), ITensorInfo::tensor_shape(), BorderSize::top, arm_compute::U8, arm_compute::update_window_and_padding(), and ITensorInfo::valid_region().

Referenced by NEConvolutionKernel< matrix_size >::name(), NESeparableConvolutionHorKernel< matrix_size >::name(), NESeparableConvolutionVertKernel< matrix_size >::name(), and NEConvolutionRectangleKernel::name().

327 {
328  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, conv);
329 
330  set_shape_if_empty(*output->info(), input->info()->tensor_shape());
331 
335 
336  _input = input;
337  _output = output;
338 
339  std::copy_n(conv, _convolution.size(), _convolution.begin());
340 
341  if(scale == 0)
342  {
343  _scale = calculate_matrix_scale(_convolution.data(), matrix_size);
344  }
345  else
346  {
347  _scale = scale;
348  }
349 
350  // Configure kernel window
351  constexpr unsigned int num_elems_processed_per_iteration = 8;
352  constexpr unsigned int num_elems_read_per_iteration = 16;
353  constexpr unsigned int num_elems_written_per_iteration = 8;
354 
355  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
356  AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
357 
359  AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, matrix_size),
360  output_access);
361 
362  output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
363 
364  INEKernel::configure(win);
365 }
unsigned int top
top of the border
Definition: Types.h:375
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
1 channel, 1 U8 per channel
uint32_t calculate_matrix_scale(const int16_t *matrix, unsigned int matrix_size)
Calculate the scale of the given square matrix.
Definition: Utils.h:727
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:441
bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
Set the shape to the specified value if the current assignment is empty.
BorderSize border_size() const override
The size of the border for that kernel.
unsigned int left
left of the border
Definition: Types.h:378
1 channel, 1 S16 per channel
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
unsigned int num_elems_processed_per_iteration
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161

◆ name()

◆ operator=() [1/2]

NEConvolutionKernel& operator= ( const NEConvolutionKernel< matrix_size > &  )
delete

◆ operator=() [2/2]

NEConvolutionKernel& operator= ( NEConvolutionKernel< matrix_size > &&  )
default

Allow instances of this class to be moved.

◆ run()

void run ( 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]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 626 of file NEConvolutionKernel.cpp.

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, arm_compute::S16, arm_compute::U8, and IKernel::window().

Referenced by NEConvolutionKernel< matrix_size >::name(), NESeparableConvolutionHorKernel< matrix_size >::name(), NESeparableConvolutionVertKernel< matrix_size >::name(), and NEConvolutionRectangleKernel::name().

627 {
631 
632  switch(_output->info()->data_type())
633  {
634  case DataType::U8:
635  convolution<uint8_t>(window);
636  break;
637  case DataType::S16:
638  convolution<int16_t>(window);
639  break;
640  default:
641  ARM_COMPUTE_ERROR("Not supported Data type!");
642  break;
643  }
644 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 U8 per channel
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
1 channel, 1 S16 per channel
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205

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