Compute Library
 19.08
CLGEMMReshapeLHSMatrixKernel Class Reference

OpenCL kernel to reshape the LHS matrix when performing the matrix multiplication. More...

#include <CLGEMMReshapeLHSMatrixKernel.h>

Collaboration diagram for CLGEMMReshapeLHSMatrixKernel:
[legend]

Public Member Functions

 CLGEMMReshapeLHSMatrixKernel ()
 Default constructor. More...
 
 CLGEMMReshapeLHSMatrixKernel (const CLGEMMReshapeLHSMatrixKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLGEMMReshapeLHSMatrixKerneloperator= (const CLGEMMReshapeLHSMatrixKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLGEMMReshapeLHSMatrixKernel (CLGEMMReshapeLHSMatrixKernel &&)=default
 Allow instances of this class to be moved. More...
 
CLGEMMReshapeLHSMatrixKerneloperator= (CLGEMMReshapeLHSMatrixKernel &&)=default
 Allow instances of this class to be moved. More...
 
void configure (const ICLTensor *input, ICLTensor *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d=false)
 Initialise the kernel's input and output. More...
 
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 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...
 
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...
 
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<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...
 
template<unsigned int dimension_size>
void add_tensor_argument (unsigned &idx, const ICLTensor *tensor, const Window &window)
 
- 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...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input, const ITensorInfo *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d)
 Static function to check if given info will lead to a valid configuration of CLGEMMReshapeLHSMatrixKernel. More...
 
- 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

OpenCL kernel to reshape the LHS matrix when performing the matrix multiplication.

In particular, this function splits the input matrix in blocks of size M0xK0 (defined through GEMMLHSInfo) and stores each one in the output matrix unrolling the values

Definition at line 37 of file CLGEMMReshapeLHSMatrixKernel.h.

Constructor & Destructor Documentation

◆ CLGEMMReshapeLHSMatrixKernel() [1/3]

Default constructor.

Definition at line 116 of file CLGEMMReshapeLHSMatrixKernel.cpp.

117  : _input(nullptr), _output(nullptr), _reinterpret_input_as_3d(false)
118 {
119 }

◆ CLGEMMReshapeLHSMatrixKernel() [2/3]

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

◆ CLGEMMReshapeLHSMatrixKernel() [3/3]

Allow instances of this class to be moved.

Member Function Documentation

◆ configure()

void configure ( const ICLTensor input,
ICLTensor output,
const GEMMLHSMatrixInfo lhs_info,
bool  reinterpret_input_as_3d = false 
)

Initialise the kernel's input and output.

Parameters
[in]inputInput tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32
[out]outputOutput tensor. Data type supported: same as input
[in]lhs_infoLHS matrix information to be used for reshaping. This object contains all the necessary information to reshape the input tensor. Only the following values are supported: lhs_info.m0: 2,3,4,5,6,7,8 lhs_info.k0: 2,3,4,8,16 lhs_info.v0: greater than 0 lhs_info.transpose: true, false lhs_info.interleave: true, false
[in]reinterpret_input_as_3d(Optional) True if the input has to be reinterpreted as 3D tensor

Definition at line 121 of file CLGEMMReshapeLHSMatrixKernel.cpp.

122 {
123  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
124 
125  // Perform validate step
126  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), lhs_info, reinterpret_input_as_3d));
127 
128  _input = input;
129  _output = output;
130  _reinterpret_input_as_3d = reinterpret_input_as_3d;
131 
132  // Create build options
133  CLBuildOptions build_opts;
134  build_opts.add_option("-DM0=" + support::cpp11::to_string(lhs_info.m0));
135  build_opts.add_option("-DK0=" + support::cpp11::to_string(lhs_info.k0));
136  build_opts.add_option("-DV0=" + support::cpp11::to_string(lhs_info.v0));
137  build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0)));
138  build_opts.add_option_if(lhs_info.interleave, "-DINTERLEAVE");
139  build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
140  build_opts.add_option_if(_reinterpret_input_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(input->info()->dimension(1)));
141  build_opts.add_option_if(_reinterpret_input_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(input->info()->dimension(2)));
142 
143  switch(input->info()->element_size())
144  {
145  case 1:
146  build_opts.add_option("-DDATA_TYPE=uchar");
147  break;
148  case 2:
149  build_opts.add_option("-DDATA_TYPE=ushort");
150  break;
151  case 4:
152  build_opts.add_option("-DDATA_TYPE=uint");
153  break;
154  default:
155  ARM_COMPUTE_ERROR("Data type not supported");
156  }
157 
158  std::string kernel_name("gemm_reshape_lhs_matrix_");
159  kernel_name += lhs_info.transpose ? "t" : "nt";
160 
161  // Create kernel
162  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
163 
164  // Configure kernel window
165  auto win_config = validate_and_configure_window(input->info(), output->info(), lhs_info, reinterpret_input_as_3d);
166  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
167  ICLKernel::configure_internal(win_config.second);
168 
169  // Set config_id for enabling LWS tuning
170  _config_id = "gemm_reshape_lhs_matrix_";
171  _config_id += (_reinterpret_input_as_3d ? "3d_" : "");
172  _config_id += lower_string(string_from_data_type(input->info()->data_type()));
173  _config_id += "_";
174  _config_id += support::cpp11::to_string(output->info()->dimension(0));
175  _config_id += "_";
176  _config_id += support::cpp11::to_string(output->info()->dimension(1));
177  _config_id += "_";
178  _config_id += support::cpp11::to_string(output->info()->dimension(2));
179  _config_id += "_";
180  _config_id += support::cpp11::to_string(lhs_info.m0);
181  _config_id += "_";
182  _config_id += support::cpp11::to_string(lhs_info.k0);
183  _config_id += "_";
184  _config_id += support::cpp11::to_string(lhs_info.v0);
185  _config_id += "_";
186  _config_id += support::cpp11::to_string(lhs_info.interleave);
187  _config_id += "_";
188  _config_id += support::cpp11::to_string(lhs_info.transpose);
189 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
const StringSet & options() const
Gets the current options list set.
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
unsigned int v0
Number of vertical blocks of size (m0xk0) stored on the same output row.
Definition: Types.h:1860
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:327
bool interleave
True if the v0 (m0xk0) blocks have to be interleaved in the output row.
Definition: Types.h:1862
void add_option(std::string option)
Adds option to the existing build option list.
bool transpose
True if the (m0xk0) block has to be transposed before been stored.
Definition: Types.h:1861
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:144
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
std::unique_ptr< Kernel > create_kernel()
Helper function to create and return a unique_ptr pointed to a CL/GLES kernel object.
Definition: Helpers.h:86
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
unsigned int k0
Number of partial accumulations performed by the matrix multiplication.
Definition: Types.h:1859
unsigned int m0
Number of rows processed by the matrix multiplication.
Definition: Types.h:1858

References CLBuildOptions::add_option(), CLBuildOptions::add_option_if(), ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::create_kernel(), ITensorInfo::data_type(), ITensorInfo::dimension(), ITensorInfo::element_size(), CLKernelLibrary::get(), ITensor::info(), GEMMLHSMatrixInfo::interleave, GEMMLHSMatrixInfo::k0, arm_compute::lower_string(), GEMMLHSMatrixInfo::m0, CLBuildOptions::options(), arm_compute::string_from_data_type(), arm_compute::support::cpp11::to_string(), GEMMLHSMatrixInfo::transpose, GEMMLHSMatrixInfo::v0, and arm_compute::validate_and_configure_window().

◆ operator=() [1/2]

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

◆ operator=() [2/2]

Allow instances of this class to be moved.

◆ run()

void run ( const Window window,
cl::CommandQueue &  queue 
)
overridevirtual

Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.

Note
The queue is not flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
Parameters
[in]windowRegion on which to execute the kernel. (Must be a valid region of the window returned by window()).
[in,out]queueCommand queue on which to enqueue the kernel.

Implements ICLKernel.

Definition at line 199 of file CLGEMMReshapeLHSMatrixKernel.cpp.

200 {
203 
205 
206  if(_reinterpret_input_as_3d)
207  {
208  // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
209  const unsigned int idx0 = 2 * num_arguments_per_3D_tensor();
210  const unsigned int total_cross_plane_pad = _input->info()->padding().top + _input->info()->padding().bottom;
211  _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
212  }
213 
214  do
215  {
216  unsigned int idx = 0;
217  add_3D_tensor_argument(idx, _input, slice);
218  add_3D_tensor_argument(idx, _output, slice);
219  enqueue(queue, *this, slice, lws_hint());
220  }
222 }
unsigned int top
top of the border
Definition: Types.h:339
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:39
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:247
unsigned int bottom
bottom of the border
Definition: Types.h:341
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.
Definition: ICLKernel.h:158
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:200
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
virtual PaddingSize padding() const =0
Padding of tensor.
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:319
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:275
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

References ICLKernel::add_3D_tensor_argument(), ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, BorderSize::bottom, arm_compute::enqueue(), Window::first_slice_window_3D(), ITensor::info(), ICLKernel::lws_hint(), ICLKernel::num_arguments_per_3D_tensor(), ITensorInfo::padding(), arm_compute::test::validation::reference::slice(), Window::slide_window_slice_3D(), BorderSize::top, and IKernel::window().

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
const GEMMLHSMatrixInfo lhs_info,
bool  reinterpret_input_as_3d 
)
static

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

Parameters
[in]inputInput tensor info. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32
[in]outputOutput tensor info which stores the interleaved matrix. Data type supported: same as input.
[in]lhs_infoLHS matrix information to be used for reshaping. This object contains all the necessary information to reshape the input tensor. Only the following values are supported: lhs_info.m0: 2,3,4,5,6,7,8 lhs_info.k0: 2,3,4,8,16 lhs_info.v0: greater than 0 lhs_info.transpose: true, false lhs_info.interleave: true, false
[in]reinterpret_input_as_3dTrue if the input has to be reinterpreted as 3D tensor
Returns
a status

Definition at line 191 of file CLGEMMReshapeLHSMatrixKernel.cpp.

192 {
193  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, lhs_info, reinterpret_input_as_3d));
194  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), lhs_info, reinterpret_input_as_3d).first);
195 
196  return Status{};
197 }
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
Status class.
Definition: Error.h:52
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.

References ARM_COMPUTE_RETURN_ON_ERROR, ICloneable< T >::clone(), and arm_compute::validate_and_configure_window().


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