Compute Library
 21.08
CLFullyConnectedLayer Class Reference

Basic function to compute a Fully Connected layer on OpenCL. More...

#include <CLFullyConnectedLayer.h>

Collaboration diagram for CLFullyConnectedLayer:
[legend]

Public Member Functions

 CLFullyConnectedLayer (std::shared_ptr< IMemoryManager > memory_manager=nullptr, IWeightsManager *weights_manager=nullptr)
 Constructor. More...
 
 ~CLFullyConnectedLayer ()
 Default destructor. More...
 
 CLFullyConnectedLayer (const CLFullyConnectedLayer &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLFullyConnectedLayer (CLFullyConnectedLayer &&)=default
 Default move constructor. More...
 
CLFullyConnectedLayeroperator= (const CLFullyConnectedLayer &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLFullyConnectedLayeroperator= (CLFullyConnectedLayer &&)=default
 Default move assignment operator. More...
 
void configure (const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
 Set the input and output tensors. More...
 
void configure (const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
 Set the input and output tensors. 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 *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
 Static function to check if given info will lead to a valid configuration of CLFullyConnectedLayer. More...
 

Detailed Description

Basic function to compute a Fully Connected layer on OpenCL.

This function calls the following OpenCL kernels:

  1. opencl::kernels::ClIm2ColKernel (called when the input comes from a convolutional layer)
  2. CLTranspose (if are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
  3. opencl::kernels::ClGemmMatrixMultiplyKernel or CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
Note
The fully connected layer accepts "weights" tensors only with 2 dimensions.

Definition at line 43 of file CLFullyConnectedLayer.h.

Constructor & Destructor Documentation

◆ CLFullyConnectedLayer() [1/3]

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

Constructor.

Definition at line 51 of file CLFullyConnectedLayer.cpp.

References CLFullyConnectedLayer::~CLFullyConnectedLayer().

52  : _impl(std::make_unique<Impl>())
53 {
54  _impl->memory_group = MemoryGroup(std::move(memory_manager));
55  _impl->weights_manager = weights_manager;
56 }

◆ ~CLFullyConnectedLayer()

~CLFullyConnectedLayer ( )
default

Default destructor.

Referenced by CLFullyConnectedLayer::CLFullyConnectedLayer().

◆ CLFullyConnectedLayer() [2/3]

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

◆ CLFullyConnectedLayer() [3/3]

Default move constructor.

Member Function Documentation

◆ configure() [1/2]

void configure ( const CLCompileContext compile_context,
const ICLTensor input,
const ICLTensor weights,
const ICLTensor biases,
ICLTensor output,
FullyConnectedLayerInfo  fc_info = FullyConnectedLayerInfo() 
)

Set the input and output tensors.

Valid data layouts:

  • NHWC
  • NCHW

Valid data type configurations:

src0 src1 src2 dst
F16 F16 F16 F16
F32 F32 F32 F32
QASYMM8 QASYMM8 S32 QASYMM8
QASYMM8_SIGNED QASYMM8_SIGNED S32 QASYMM8_SIGNED
Parameters
[in]compile_contextThe compile context to be used.
[in]inputSource tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
[in]weightsWeights tensor. The weights must be 2 dimensional. If this function is called after a Convolution Layer, the (transposed) weights will have as many rows as the product of the first 3 input's dimensions. If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. Data type supported: Same as input.
[in]biasesBias tensor. Can be nullptr. Data type supported:Same as input.
[out]outputDestination tensor. Its shape should be equal to the output of a matrix multiplication between:
  • The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
  • The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. Data type supported: Same as input.
[in]fc_info(Optional) Fully connected layer additional info

Definition at line 66 of file CLFullyConnectedLayer.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_ERROR_THROW_ON, ITensor::info(), FullyConnectedLayerInfo::retain_internal_weights, and CLFullyConnectedLayer::validate().

Referenced by CLRNNLayer::configure(), CLFullyConnectedLayer::configure(), and CLLSTMLayer::configure().

68 {
69  // Perform validate step
70  ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
72  weights->info(),
73  biases != nullptr ? biases->info() : nullptr,
74  output->info(),
75  fc_info));
76 
77  _impl->op = std::make_unique<opencl::ClFullyConnected>();
78  _impl->original_weights = weights;
79  _impl->is_prepared = fc_info.retain_internal_weights;
80 
81  _impl->op->configure(compile_context, input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), fc_info);
82 
83  if(_impl->weights_manager != nullptr)
84  {
85  _impl->weights_manager->manage(weights);
86  }
87 
88  if(!_impl->is_prepared)
89  {
90  _impl->aux_mem_req = _impl->op->workspace();
91  _impl->run_pack = { { ACL_SRC_0, input }, { ACL_SRC_1, weights }, { ACL_SRC_2, biases }, { ACL_DST, output } };
92  _impl->workspace = manage_workspace<CLTensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->run_pack);
93  }
94  else
95  {
96  _impl->run_pack.add_tensor(ACL_SRC_0, input);
97  _impl->run_pack.add_tensor(ACL_DST, output);
98  }
99 }
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Static function to check if given info will lead to a valid configuration of CLFullyConnectedLayer.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157

◆ configure() [2/2]

void configure ( const ICLTensor input,
const ICLTensor weights,
const ICLTensor biases,
ICLTensor output,
FullyConnectedLayerInfo  fc_info = FullyConnectedLayerInfo() 
)

Set the input and output tensors.

Similar to CLFullyConnectedLayer

Definition at line 60 of file CLFullyConnectedLayer.cpp.

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

62 {
63  configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, fc_info);
64 }
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Set the input and output tensors.

◆ operator=() [1/2]

CLFullyConnectedLayer& operator= ( const CLFullyConnectedLayer )
delete

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

◆ operator=() [2/2]

CLFullyConnectedLayer& operator= ( CLFullyConnectedLayer &&  )
default

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 115 of file CLFullyConnectedLayer.cpp.

References ITensor::is_used().

Referenced by CLRNNLayer::prepare(), and CLFullyConnectedLayer::run().

116 {
117  if(!_impl->is_prepared)
118  {
119  _impl->op->prepare(_impl->run_pack);
120 
121  // Release temporary tensors that are only used in prepare stage
122  release_temporaries<CLTensor>(_impl->aux_mem_req, _impl->workspace);
123  _impl->is_prepared = true;
124 
125  // Handle weights managed infrastructure
126  if(_impl->weights_manager != nullptr && _impl->weights_manager->are_weights_managed(_impl->original_weights))
127  {
128  // If function marks b as unused ensure that all prepare stages are done before releasing
129  const ITensor *original_b = _impl->original_weights;
130  if(!original_b->is_used())
131  {
132  _impl->weights_manager->mark_as_unused(original_b);
133  }
134  _impl->original_weights->mark_as_used();
135  _impl->weights_manager->release(_impl->original_weights);
136  }
137  }
138 }

◆ 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 107 of file CLFullyConnectedLayer.cpp.

References CLFullyConnectedLayer::prepare().

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

108 {
109  prepare();
110 
111  MemoryGroupResourceScope scope_mg(_impl->memory_group);
112  _impl->op->run(_impl->run_pack);
113 }
void prepare() override
Prepare the function for executing.

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo weights,
const ITensorInfo biases,
const ITensorInfo output,
FullyConnectedLayerInfo  fc_info = FullyConnectedLayerInfo() 
)
static

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

Similar to CLFullyConnectedLayer

Returns
a status

Definition at line 101 of file CLFullyConnectedLayer.cpp.

References ClFullyConnected::validate().

Referenced by CLFullyConnectedLayer::configure(), arm_compute::test::validation::DATA_TEST_CASE(), CLRNNLayer::validate(), and CLLSTMLayer::validate().

103 {
104  return opencl::ClFullyConnected::validate(input, weights, biases, output, fc_info);
105 }
static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Static function to check if given info will lead to a valid configuration.

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