Compute Library
 21.02
CLConvolutionKernel< matrix_size > Class Template Reference

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

#include <CLConvolution.h>

Collaboration diagram for CLConvolutionKernel< matrix_size >:
[legend]

Public Member Functions

void configure (const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined)
 Initialise the kernel's input, output and border mode. More...
 
void configure (const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined)
 Initialise the kernel's input, output and border mode. More...
 
BorderSize border_size () const override
 The size of the border for that kernel. More...
 
- Public Member Functions inherited from ICLSimple2DKernel
void run (const Window &window, cl::CommandQueue &queue) override
 Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue. More...
 
- Public Member Functions inherited from ICLSimpleKernel
 ICLSimpleKernel ()
 Constructor. More...
 
 ICLSimpleKernel (const ICLSimpleKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
ICLSimpleKerneloperator= (const ICLSimpleKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 ICLSimpleKernel (ICLSimpleKernel &&)=default
 Allow instances of this class to be moved. More...
 
ICLSimpleKerneloperator= (ICLSimpleKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~ICLSimpleKernel ()=default
 Default destructor. More...
 
void configure (const ICLTensor *input, ICLTensor *output, unsigned int num_elems_processed_per_iteration, bool border_undefined=false, const BorderSize &border_size=BorderSize())
 Configure the kernel. More...
 
- Public Member Functions inherited from ICLKernel
 ICLKernel ()
 Constructor. More...
 
cl::Kernel & kernel ()
 Returns a reference to the OpenCL kernel of this object. More...
 
template<typename T >
void add_1D_array_argument (unsigned int &idx, const ICLArray< T > *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
 Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_1D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_1D_tensor_argument_if (bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true. More...
 
void add_2D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_2D_tensor_argument_if (bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true. More...
 
void add_3D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_4D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
virtual void run_op (ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
 Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue. More...
 
template<typename T >
void add_argument (unsigned int &idx, T value)
 Add the passed parameters to the object's kernel's arguments starting from the index idx. More...
 
void set_lws_hint (const cl::NDRange &lws_hint)
 Set the Local-Workgroup-Size hint. More...
 
cl::NDRange lws_hint () const
 Return the Local-Workgroup-Size hint. More...
 
void set_wbsm_hint (const cl_int &wbsm_hint)
 Set the workgroup batch size modifier hint. More...
 
cl_int wbsm_hint () const
 Return the workgroup batch size modifier hint. More...
 
const std::string & config_id () const
 Get the configuration ID. More...
 
void set_target (GPUTarget target)
 Set the targeted GPU architecture. More...
 
void set_target (cl::Device &device)
 Set the targeted GPU architecture according to the CL device. More...
 
GPUTarget get_target () const
 Get the targeted GPU architecture. More...
 
size_t get_max_workgroup_size ()
 Get the maximum workgroup size for the device the CLKernelLibrary uses. More...
 
template<unsigned int dimension_size>
void add_tensor_argument (unsigned &idx, const ICLTensor *tensor, const Window &window)
 
template<typename T , unsigned int dimension_size>
void add_array_argument (unsigned &idx, const ICLArray< T > *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
 Add the passed array's parameters to the object's kernel's arguments starting from the index idx. 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...
 

Additional Inherited Members

- Static Public Member Functions inherited from ICLKernel
static constexpr unsigned int num_arguments_per_1D_array ()
 Returns the number of arguments enqueued per 1D array object. More...
 
static constexpr unsigned int num_arguments_per_1D_tensor ()
 Returns the number of arguments enqueued per 1D tensor object. More...
 
static constexpr unsigned int num_arguments_per_2D_tensor ()
 Returns the number of arguments enqueued per 2D tensor object. More...
 
static constexpr unsigned int num_arguments_per_3D_tensor ()
 Returns the number of arguments enqueued per 3D tensor object. More...
 
static constexpr unsigned int num_arguments_per_4D_tensor ()
 Returns the number of arguments enqueued per 4D tensor object. More...
 
static cl::NDRange gws_from_window (const Window &window)
 Get the global work size given an execution window. More...
 

Detailed Description

template<unsigned int matrix_size>
class arm_compute::CLConvolutionKernel< 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 40 of file CLConvolution.h.

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 55 of file CLConvolutionKernel.cpp.

56 {
57  return BorderSize(matrix_size / 2);
58 }

◆ configure() [1/2]

void configure ( const ICLTensor input,
ICLTensor 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 types 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 61 of file CLConvolutionKernel.cpp.

References CLConvolutionKernel< matrix_size >::configure(), and CLKernelLibrary::get().

Referenced by CLConvolutionKernel< matrix_size >::configure().

62 {
63  configure(CLKernelLibrary::get().get_compile_context(), input, output, conv, scale, border_undefined);
64 }
void configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined)
Initialise the kernel&#39;s input, output and border mode.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.

◆ configure() [2/2]

void configure ( const CLCompileContext compile_context,
const ICLTensor input,
ICLTensor output,
const int16_t *  conv,
uint32_t  scale,
bool  border_undefined 
)

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

Parameters
[in]compile_contextThe compile context to be used.
[in]inputSource tensor. Data types 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 67 of file CLConvolutionKernel.cpp.

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, arm_compute::test::validation::input, arm_compute::S16, and arm_compute::U8.

68 {
71  ARM_COMPUTE_ERROR_ON(conv == nullptr);
72 
73  _input = input;
74  _output = output;
75 
76  std::stringstream kernel_name;
77  CLBuildOptions build_opts;
78  kernel_name << "convolution" << matrix_size << "x" << matrix_size << "_static";
79 
80  if(scale == 0)
81  {
82  scale = calculate_matrix_scale(conv, matrix_size);
83  }
84 
85  for(unsigned int i = 0; i < matrix_size * matrix_size; i++)
86  {
87  std::stringstream mat_str;
88  mat_str << "-DMAT" << i << "=" << conv[i];
89  build_opts.add_option(mat_str.str());
90  }
91 
92  build_opts.add_option("-DSCALE=" + support::cpp11::to_string(scale));
93 
94  DataType data_type = data_type_for_convolution_matrix(conv, matrix_size * matrix_size);
95  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
96 
97  std::stringstream out_type;
98  out_type << "-DDATA_TYPE_OUT=" << get_cl_type_from_data_type(output->info()->data_type());
99  build_opts.add_option(out_type.str());
100 
101  _kernel = create_kernel(compile_context, kernel_name.str(), build_opts.options());
102 
103  // Configure kernel window
104  constexpr unsigned int num_elems_processed_per_iteration = 8;
105  constexpr unsigned int num_elems_written_per_iteration = 8;
106  constexpr unsigned int num_elems_read_per_iteration = 16;
107  constexpr unsigned int num_rows_read_per_iteration = matrix_size;
108 
109  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
110 
111  AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
112  AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
113 
114  update_window_and_padding(win, input_access, output_access);
115 
116  output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
117 
118  ICLKernel::configure_internal(win);
119 }
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
std::string to_string(T &&value)
Convert integer and float values to string.
BorderSize border_size() const override
The size of the border for that kernel.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
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
const DataType data_type
Definition: Im2Col.cpp:150
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:403
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
std::string kernel_name
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:37
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
DataType
Available data types.
Definition: Types.h:77
DataType data_type_for_convolution_matrix(const int16_t *conv, size_t size)
Calculate the accuracy required by the squared convolution calculation.
Definition: Utils.h:862

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