Compute Library
 19.08
CLLocallyConnectedLayer Class Reference

Basic function to compute the locally connected layer. More...

#include <CLLocallyConnectedLayer.h>

Collaboration diagram for CLLocallyConnectedLayer:
[legend]

Public Member Functions

 CLLocallyConnectedLayer (std::shared_ptr< IMemoryManager > memory_manager=nullptr)
 Default constructor. More...
 
 CLLocallyConnectedLayer (const CLLocallyConnectedLayer &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLLocallyConnectedLayer (CLLocallyConnectedLayer &&)=default
 Default move constructor. More...
 
CLLocallyConnectedLayeroperator= (const CLLocallyConnectedLayer &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLLocallyConnectedLayeroperator= (CLLocallyConnectedLayer &&)=default
 Default move assignment operator. More...
 
void configure (const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
 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, const PadStrideInfo &conv_info)
 Static function to check if given info will lead to a valid configuration of CLLocallyConnectedLayer. More...
 

Detailed Description

Basic function to compute the locally connected layer.

This function calls the following OpenCL kernels:

  1. CLWeightsReshapeKernel (executed only once for each configuration)
  2. CLIm2ColKernel
  3. CLLocallyConnectedMatrixMultiplyKernel
  4. CLCol2ImKernel

Definition at line 51 of file CLLocallyConnectedLayer.h.

Constructor & Destructor Documentation

◆ CLLocallyConnectedLayer() [1/3]

CLLocallyConnectedLayer ( std::shared_ptr< IMemoryManager memory_manager = nullptr)

Default constructor.

Definition at line 80 of file CLLocallyConnectedLayer.cpp.

81  : _memory_group(std::move(memory_manager)), _input_im2col_kernel(), _weights_reshape_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(), _weights_reshaped(), _gemm_output(),
82  _is_prepared(false), _original_weights(nullptr)
83 {
84 }

◆ CLLocallyConnectedLayer() [2/3]

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

◆ CLLocallyConnectedLayer() [3/3]

Default move constructor.

Member Function Documentation

◆ configure()

void configure ( const ICLTensor input,
const ICLTensor weights,
const ICLTensor biases,
ICLTensor output,
const PadStrideInfo conv_info 
)

Set the input and output tensors.

Parameters
[in]inputSource tensor. 3 lower dimensions represent a single input [width, height, IFM], while every optional dimension from 4 and above represent a batch of inputs. Data types supported: F32.
[in]weightsWeights tensor. Weights are 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, num_patches]. Data type supported:Same as input.
[in]biasesBiases tensor. Shared biases supported. Biases are 2D tensor with dimensions [OFM, num_patches]. Data type supported:Same as input.
[out]outputDestination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. Data types supported: Same as input.
[in]conv_infoContains padding and stride information described in PadStrideInfo.

Definition at line 130 of file CLLocallyConnectedLayer.cpp.

131 {
132  ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
133  ARM_COMPUTE_ERROR_THROW_ON(CLLocallyConnectedLayer::validate(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output->info(), conv_info));
134 
135  bool _has_bias = (biases != nullptr);
136  _original_weights = weights;
137  _is_prepared = false;
138 
139  const unsigned int kernel_width = weights->info()->dimension(0);
140  const unsigned int kernel_height = weights->info()->dimension(1);
141 
142  // Get convolved dimensions
143  unsigned int conv_w = 0;
144  unsigned int conv_h = 0;
145  std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
146  conv_info);
147 
148  // Calculate intermediate buffer shapes
149  TensorShape shape_wr;
150  TensorShape shape_im2col;
151  TensorShape shape_gemm;
152  calculate_shapes(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output->info(), conv_info, shape_wr, shape_im2col, shape_gemm);
153 
154  _weights_reshaped.allocator()->init(TensorInfo(shape_wr, 1, weights->info()->data_type()));
155  _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type()));
156  _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, input->info()->data_type()));
157 
158  // Manage intermediate buffers
159  _memory_group.manage(&_input_im2col_reshaped);
160  _memory_group.manage(&_gemm_output);
161 
162  // Configure kernels
163  _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
164  _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
165  _mm_kernel.configure(&_input_im2col_reshaped, &_weights_reshaped, &_gemm_output);
166  _output_col2im_kernel.configure(&_gemm_output, output, Size2D(conv_w, conv_h));
167 
168  // Allocate intermediate tensors
169  _input_im2col_reshaped.allocator()->allocate();
170  _gemm_output.allocator()->allocate();
171 
172  CLScheduler::get().tune_kernel_static(_input_im2col_kernel);
173 }
void configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U), unsigned int num_groups=1)
Set the input and output of the kernel.
Shape of a tensor.
Definition: TensorShape.h:39
void configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output)
Initialise the kernel's input, output and alpha.
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: CLTensor.cpp:35
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
std::pair< unsigned int, unsigned int > scaled_dimensions(unsigned int width, unsigned int height, unsigned int kernel_width, unsigned int kernel_height, const PadStrideInfo &pad_stride_info, const Size2D &dilation=Size2D(1U, 1U))
Returns expected width and height of output scaled tensor depending on dimensions rounding mode.
Definition: Utils.cpp:387
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:41
size_t dimension(size_t index) const override
Return the size of the requested dimension.
Definition: TensorInfo.h:223
virtual DataType data_type() const =0
Data type used for each element of the tensor.
CLTensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: CLTensor.cpp:55
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
DataType data_type() const override
Data type used for each element of the tensor.
Definition: TensorInfo.h:256
void manage(TensorType *obj)
Sets a object to be managed by the given memory group.
void configure(const ICLTensor *input, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups=1)
Set the input and output of the kernel.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
Store the tensor's metadata.
Definition: TensorInfo.h:45
void tune_kernel_static(ICLKernel &kernel)
Tunes OpenCL kernel.
Definition: CLScheduler.h:172
void configure(const ICLTensor *input, ICLTensor *output, const Size2D &convolved_dims, unsigned int num_groups=1)
Set the input and output of the kernel.
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info)
Static function to check if given info will lead to a valid configuration of CLLocallyConnectedLayer.

References CLTensorAllocator::allocate(), CLTensor::allocator(), ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, CLLocallyConnectedMatrixMultiplyKernel::configure(), CLCol2ImKernel::configure(), CLWeightsReshapeKernel::configure(), CLIm2ColKernel::configure(), arm_compute::test::validation::conv_info, ITensorInfo::data_type(), TensorInfo::data_type(), ITensorInfo::dimension(), TensorInfo::dimension(), CLScheduler::get(), ITensor::info(), CLTensor::info(), ITensorAllocator::init(), MemoryGroupBase< TensorType >::manage(), arm_compute::scaled_dimensions(), CLScheduler::tune_kernel_static(), CLLocallyConnectedLayer::validate(), and arm_compute::test::validation::weights.

Referenced by arm_compute::test::validation::DATA_TEST_CASE().

◆ operator=() [1/2]

CLLocallyConnectedLayer& operator= ( const CLLocallyConnectedLayer )
delete

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

◆ operator=() [2/2]

CLLocallyConnectedLayer& operator= ( CLLocallyConnectedLayer &&  )
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 191 of file CLLocallyConnectedLayer.cpp.

192 {
193  if(!_is_prepared)
194  {
195  ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
196 
197  // Run weights reshaping and mark original weights tensor as unused
198  _weights_reshaped.allocator()->allocate();
199  CLScheduler::get().enqueue(_weights_reshape_kernel);
200  _original_weights->mark_as_unused();
201 
202  CLScheduler::get().queue().finish();
203  _is_prepared = true;
204  }
205 }
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:41
bool is_used() const
Flags if the tensor is used or not.
Definition: ITensor.cpp:162
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
CLTensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: CLTensor.cpp:55
void mark_as_unused() const
Marks a tensor as unused.
Definition: ITensor.cpp:167
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: CLScheduler.cpp:95
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.h:102
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.

References CLTensorAllocator::allocate(), CLTensor::allocator(), ARM_COMPUTE_ERROR_ON, CLScheduler::enqueue(), CLScheduler::get(), ITensor::is_used(), ITensor::mark_as_unused(), and CLScheduler::queue().

Referenced by CLLocallyConnectedLayer::run().

◆ run()

void run ( )
overridevirtual

Run the kernels contained in the function.

For NEON 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 175 of file CLLocallyConnectedLayer.cpp.

176 {
177  prepare();
178 
179  MemoryGroupResourceScope scope_mg(_memory_group);
180 
181  // Run input reshaping
182  CLScheduler::get().enqueue(_input_im2col_kernel);
183 
184  // Runs vector matrix multiply on reshaped matrices
185  CLScheduler::get().enqueue(_mm_kernel);
186 
187  // Reshape output matrix
188  CLScheduler::get().enqueue(_output_col2im_kernel, false);
189 }
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:41
void prepare() override
Prepare the function for executing.
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: CLScheduler.cpp:95
Memory group resources scope handling class.
Definition: IMemoryGroup.h:46

References CLScheduler::enqueue(), CLScheduler::get(), and CLLocallyConnectedLayer::prepare().

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo weights,
const ITensorInfo biases,
const ITensorInfo output,
const PadStrideInfo conv_info 
)
static

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

Parameters
[in]inputInput tensor info. 3 lower dimensions represent a single input [width, height, IFM], while every optional dimension from 4 and above represent a batch of inputs. Data types supported: F32.
[in]weightsWeights tensor info. Weights are 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, num_patches]. Data type supported:Same as input.
[in]biasesBiases tensor info. Shared biases supported. Biases are 2D tensor with dimensions [OFM, num_patches]. Data type supported:Same as input.
[in]outputOutput tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. Data types supported: Same as input.
[in]conv_infoContains padding and stride information described in PadStrideInfo.
Returns
a status

Definition at line 86 of file CLLocallyConnectedLayer.cpp.

87 {
89  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != input->dimension(2));
90  ARM_COMPUTE_RETURN_ERROR_ON(!conv_info.padding_is_symmetric());
91 
92  bool has_bias = (biases != nullptr);
93 
94  if(has_bias)
95  {
96  ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
98  }
99 
100  const unsigned int kernel_width = weights->dimension(0);
101  const unsigned int kernel_height = weights->dimension(1);
102 
103  // Get convolved dimensions
104  unsigned int conv_w = 0;
105  unsigned int conv_h = 0;
106  std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height,
107  conv_info);
108 
109  ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != conv_w) || (output->dimension(1) != conv_h), "Output shape does not match the expected one");
110  ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(4) != (conv_w * conv_h), "Weights shape does not match the expected one");
111 
112  // Calculate intermediate buffer shapes
113  TensorShape shape_wr;
114  TensorShape shape_im2col;
115  TensorShape shape_gemm;
116  calculate_shapes(input, weights, biases, output, conv_info, shape_wr, shape_im2col, shape_gemm);
117 
118  TensorInfo weights_reshaped_info(shape_wr, 1, weights->data_type());
119  TensorInfo input_im2col_reshaped_info(shape_im2col, 1, input->data_type());
120  TensorInfo gemm_output_info(shape_gemm, 1, input->data_type());
121 
122  ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &input_im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, has_bias));
123  ARM_COMPUTE_RETURN_ON_ERROR(CLWeightsReshapeKernel::validate(weights, biases, &weights_reshaped_info));
124  ARM_COMPUTE_RETURN_ON_ERROR(CLLocallyConnectedMatrixMultiplyKernel::validate(&input_im2col_reshaped_info, &weights_reshaped_info, &gemm_output_info));
125  ARM_COMPUTE_RETURN_ON_ERROR(CLCol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h)));
126 
127  return Status{};
128 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Shape of a tensor.
Definition: TensorShape.h:39
static Status validate(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups=1)
Static function to check if given info will lead to a valid configuration of CLWeightsReshapeKernel.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
std::pair< unsigned int, unsigned int > scaled_dimensions(unsigned int width, unsigned int height, unsigned int kernel_width, unsigned int kernel_height, const PadStrideInfo &pad_stride_info, const Size2D &dilation=Size2D(1U, 1U))
Returns expected width and height of output scaled tensor depending on dimensions rounding mode.
Definition: Utils.cpp:387
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
virtual DataType data_type() const =0
Data type used for each element of the tensor.
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:244
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims, unsigned int num_groups=1)
Static function to check if given info will lead to a valid configuration of CLCol2ImKernel.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U), unsigned int num_groups=1)
Static function to check if given info will lead to a valid configuration of CLIm2ColKernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond,...)
If the condition is true, an error is returned.
Definition: Error.h:214
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
static Status validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of CLLocallyConnectedMatrix...
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
Store the tensor's metadata.
Definition: TensorInfo.h:45

References ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_MSG, ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, ARM_COMPUTE_RETURN_ON_ERROR, arm_compute::test::validation::conv_info, ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::test::validation::has_bias, ITensorInfo::num_dimensions(), arm_compute::scaled_dimensions(), CLLocallyConnectedMatrixMultiplyKernel::validate(), CLCol2ImKernel::validate(), CLWeightsReshapeKernel::validate(), CLIm2ColKernel::validate(), and arm_compute::test::validation::weights.

Referenced by CLLocallyConnectedLayer::configure().


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