Compute Library
 21.08
CLGEMM Class Reference

Basic function to execute GEMM on OpenCL. More...

#include <CLGEMM.h>

Collaboration diagram for CLGEMM:
[legend]

Public Member Functions

 CLGEMM (std::shared_ptr< IMemoryManager > memory_manager=nullptr, IWeightsManager *weights_manager=nullptr)
 Default constructor. More...
 
 ~CLGEMM ()
 Default destructor. More...
 
 CLGEMM (const CLGEMM &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLGEMM (CLGEMM &&)
 Default move constructor. More...
 
CLGEMMoperator= (const CLGEMM &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLGEMMoperator= (CLGEMM &&)
 Default move assignment operator. More...
 
void configure (const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info=GEMMInfo())
 Initialise the kernel's inputs and output. More...
 
void configure (const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info=GEMMInfo())
 Initialise the kernel's inputs and output. More...
 
void run () override
 Run the kernels contained in the function. More...
 
void prepare () override
 Prepare the function for executing. More...
 
- Public Member Functions inherited from IFunction
virtual ~IFunction ()=default
 Destructor. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info=GEMMInfo())
 Static function to check if given info will lead to a valid configuration of CLGEMM. More...
 

Detailed Description

Basic function to execute GEMM on OpenCL.

Definition at line 44 of file CLGEMM.h.

Constructor & Destructor Documentation

◆ CLGEMM() [1/3]

CLGEMM ( std::shared_ptr< IMemoryManager memory_manager = nullptr,
IWeightsManager weights_manager = nullptr 
)

Default constructor.

Parameters
[in]memory_manager(Optional) Memory manager.
[in]weights_manager(Optional) Weights manager.

Definition at line 55 of file CLGEMM.cpp.

References CLGEMM::~CLGEMM().

56  : _impl(std::make_unique<Impl>())
57 {
58  _impl->memory_group = MemoryGroup(memory_manager);
59  _impl->weights_manager = weights_manager;
60 }

◆ ~CLGEMM()

~CLGEMM ( )
default

Default destructor.

Referenced by CLGEMM::CLGEMM().

◆ CLGEMM() [2/3]

CLGEMM ( const CLGEMM )
delete

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

◆ CLGEMM() [3/3]

CLGEMM ( CLGEMM &&  )

Default move constructor.

Member Function Documentation

◆ configure() [1/2]

void configure ( const CLCompileContext compile_context,
const ICLTensor a,
const ICLTensor b,
const ICLTensor c,
ICLTensor output,
float  alpha,
float  beta,
const GEMMInfo gemm_info = GEMMInfo() 
)

Initialise the kernel's inputs and output.

Valid data layouts:

  • All

Valid data type configurations:

src0 src1 src2 dst
F32 F32 F32 F32
F16 F16 F16 F16
Note
GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
All tensors must have the same data type.
Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix
Parameters
[in]compile_contextThe compile context to be used.
[in]aFirst input tensor (Matrix or Vector A). Data types supported: F16/F32
[in]bSecond input tensor (Matrix B). Data type supported: same as a.
[in]cThird input tensor (Matrix C). It can be a nullptr if just the multiplication between a and b is needed. Data type supported: same as a.
[out]outputOutput tensor. Data type supported: same as a
[in]alphaWeight of the matrix product
[in]betaWeight of matrix C
[in]gemm_info(Optional) Specifies if the matrix A and/or matrix B have been reshaped and if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping in case matrix A and matrix B have been already transformed.

Definition at line 69 of file CLGEMM.cpp.

References arm_compute::ACL_DST, arm_compute::ACL_SRC_0, arm_compute::ACL_SRC_1, arm_compute::ACL_SRC_2, ARM_COMPUTE_ERROR_ON_NULLPTR, arm_compute::test::validation::b, ITensor::info(), and GEMMInfo::retain_internal_weights().

Referenced by CLRNNLayer::configure(), CLGEMM::configure(), CLGEMMDeconvolutionLayer::configure(), and CLLSTMLayer::configure().

70 {
71  ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
72 
73  _impl->b = b;
74  _impl->op = std::make_unique<OperatorType>();
75  _impl->is_prepared = gemm_info.retain_internal_weights();
76 
77  _impl->op->configure(compile_context, a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info);
78  _impl->aux_mem_req = _impl->op->workspace();
79 
80  // Manage/allocate auxilairy tensors
81  if(_impl->is_prepared)
82  {
83  _impl->run_pack.add_const_tensor(ACL_SRC_0, a);
84  _impl->run_pack.add_tensor(ACL_DST, output);
85  }
86  else
87  {
88  _impl->run_pack = { { ACL_SRC_0, a }, { ACL_SRC_2, c }, { ACL_DST, output } };
89  _impl->prep_pack = { { ACL_SRC_1, _impl->b } };
90 
91  _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack, _impl->prep_pack);
92  }
93 }
SimpleTensor< float > b
Definition: DFT.cpp:157
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157

◆ configure() [2/2]

void configure ( const ICLTensor a,
const ICLTensor b,
const ICLTensor c,
ICLTensor output,
float  alpha,
float  beta,
const GEMMInfo gemm_info = GEMMInfo() 
)

Initialise the kernel's inputs and output.

Similar to CLGEMM::configure()

Definition at line 64 of file CLGEMM.cpp.

References CLGEMM::configure(), and CLKernelLibrary::get().

65 {
66  configure(CLKernelLibrary::get().get_compile_context(), a, b, c, output, alpha, beta, gemm_info);
67 }
SimpleTensor< float > b
Definition: DFT.cpp:157
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
void configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info=GEMMInfo())
Initialise the kernel&#39;s inputs and output.
Definition: CLGEMM.cpp:69

◆ operator=() [1/2]

CLGEMM& operator= ( const CLGEMM )
delete

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

◆ operator=() [2/2]

CLGEMM& operator= ( CLGEMM &&  )

Default move assignment operator.

◆ prepare()

void prepare ( )
overridevirtual

Prepare the function for executing.

Any one off pre-processing step required by the function is handled here

Note
Prepare stage might not need all the function's buffers' backing memory to be available in order to execute

Reimplemented from IFunction.

Definition at line 109 of file CLGEMM.cpp.

References arm_compute::ACL_SRC_1, and arm_compute::mlgo::parser::end().

Referenced by CLRNNLayer::prepare(), CLGEMMDeconvolutionLayer::prepare(), and CLGEMM::run().

110 {
111  if(!_impl->is_prepared)
112  {
113  _impl->op->prepare(_impl->prep_pack);
114 
115  auto has_reshape = std::find_if(_impl->aux_mem_req.begin(),
116  _impl->aux_mem_req.end(),
117  [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
118 
119  if(has_reshape != std::end(_impl->aux_mem_req))
120  {
121  _impl->b->mark_as_unused();
122  }
123  else
124  {
125  // Pack the B matrix to be used as the underlying GEMM performs no reshapes
126  _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->b);
127  }
128  _impl->is_prepared = true;
129  }
130 }
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290

◆ run()

void run ( )
overridevirtual

Run the kernels contained in the function.

For CPU 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 100 of file CLGEMM.cpp.

References CLGEMM::prepare().

Referenced by CLRNNLayer::run(), CLGEMMDeconvolutionLayer::run(), and CLLSTMLayer::run().

101 {
102  prepare();
103 
104  MemoryGroupResourceScope scope_mg(_impl->memory_group);
105 
106  _impl->op->run(_impl->run_pack);
107 }
void prepare() override
Prepare the function for executing.
Definition: CLGEMM.cpp:109

◆ validate()

Status validate ( const ITensorInfo a,
const ITensorInfo b,
const ITensorInfo c,
const ITensorInfo output,
float  alpha,
float  beta,
const GEMMInfo gemm_info = GEMMInfo() 
)
static

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

Similar to CLGEMM::configure()

Returns
a status

Definition at line 95 of file CLGEMM.cpp.

References ClGemm::validate().

Referenced by CLRNNLayer::validate(), CLGEMMDeconvolutionLayer::validate(), and CLLSTMLayer::validate().

96 {
97  return OperatorType::validate(a, b, c, output, alpha, beta, gemm_info);
98 }
SimpleTensor< float > b
Definition: DFT.cpp:157
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
Static function to check if given info will lead to a valid configuration.
Definition: ClGemm.cpp:612

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