Compute Library
 19.08
NELocallyConnectedLayer Class Reference

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

#include <NELocallyConnectedLayer.h>

Collaboration diagram for NELocallyConnectedLayer:
[legend]

Public Member Functions

 NELocallyConnectedLayer (std::shared_ptr< IMemoryManager > memory_manager=nullptr)
 Default constructor. More...
 
 NELocallyConnectedLayer (const NELocallyConnectedLayer &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NELocallyConnectedLayer (NELocallyConnectedLayer &&)=default
 Default move constructor. More...
 
NELocallyConnectedLayeroperator= (const NELocallyConnectedLayer &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NELocallyConnectedLayeroperator= (NELocallyConnectedLayer &&)=default
 Default move assignment operator. More...
 
void configure (const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *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 NELocallyConnectedLayer. More...
 

Detailed Description

Basic function to compute the locally connected layer.

This function calls the following NEON kernels:

  1. NEWeightsReshapeKernel (executed only once for each configuration)
  2. NEIm2ColKernel
  3. NELocallyConnectedMatrixMultiplyKernel
  4. NECol2ImKernel

Definition at line 51 of file NELocallyConnectedLayer.h.

Constructor & Destructor Documentation

◆ NELocallyConnectedLayer() [1/3]

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

Default constructor.

Definition at line 74 of file NELocallyConnectedLayer.cpp.

75  : _memory_group(std::move(memory_manager)), _input_im2col_kernel(), _weights_reshape_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(), _weights_reshaped(), _gemm_output(),
76  _is_prepared(false), _original_weights(nullptr)
77 {
78 }

◆ NELocallyConnectedLayer() [2/3]

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

◆ NELocallyConnectedLayer() [3/3]

Default move constructor.

Member Function Documentation

◆ configure()

void configure ( const ITensor input,
const ITensor weights,
const ITensor biases,
ITensor 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: F16, 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 124 of file NELocallyConnectedLayer.cpp.

125 {
126  ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
127  ARM_COMPUTE_ERROR_THROW_ON(NELocallyConnectedLayer::validate(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output->info(), conv_info));
128 
129  bool _has_bias = (biases != nullptr);
130  _is_prepared = false;
131  _original_weights = weights;
132 
133  const unsigned int kernel_width = weights->info()->dimension(0);
134  const unsigned int kernel_height = weights->info()->dimension(1);
135 
136  // Get convolved dimensions
137  unsigned int conv_w = 0;
138  unsigned int conv_h = 0;
139  std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
140  conv_info);
141 
142  // Calculate intermediate buffer shapes
143  TensorShape shape_wr;
144  TensorShape shape_im2col;
145  TensorShape shape_gemm;
146  calculate_shapes(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output->info(), conv_info, shape_wr, shape_im2col, shape_gemm);
147 
148  _weights_reshaped.allocator()->init(TensorInfo(shape_wr, 1, weights->info()->data_type()));
149  _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type()));
150  _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, input->info()->data_type()));
151 
152  // Manage intermediate buffers
153  _memory_group.manage(&_input_im2col_reshaped);
154  _memory_group.manage(&_gemm_output);
155 
156  // Configure kernels
157  _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
158  _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
159  _mm_kernel.configure(&_input_im2col_reshaped, &_weights_reshaped, &_gemm_output);
160  _output_col2im_kernel.configure(&_gemm_output, output, Size2D(conv_w, conv_h));
161 
162  // Allocate intermediate tensors
163  _input_im2col_reshaped.allocator()->allocate();
164  _gemm_output.allocator()->allocate();
165 }
void configure(const ITensor *input, const ITensor *bias, ITensor *output)
Set the input and output of the kernel.
Shape of a tensor.
Definition: TensorShape.h:39
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: CLTensor.cpp:35
void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
Shares the same backing memory with another tensor allocator, while the tensor info might be differen...
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
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.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
void configure(const ITensor *input, ITensor *output, const Size2D &convolved_dims)
Set the input and output of the kernel.
TensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: Tensor.cpp:48
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.
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 NELocallyConnectedLayer.
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
void configure(const ITensor *input0, const ITensor *input1, ITensor *output)
Initialise the kernel's input and output.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
void configure(const ITensor *input, ITensor *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.
Store the tensor's metadata.
Definition: TensorInfo.h:45

References TensorAllocator::allocate(), Tensor::allocator(), ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, NELocallyConnectedMatrixMultiplyKernel::configure(), NECol2ImKernel::configure(), NEWeightsReshapeKernel::configure(), NEIm2ColKernel::configure(), arm_compute::test::validation::conv_info, ITensorInfo::data_type(), TensorInfo::data_type(), ITensorInfo::dimension(), TensorInfo::dimension(), ITensor::info(), CLTensor::info(), TensorAllocator::init(), MemoryGroupBase< TensorType >::manage(), arm_compute::scaled_dimensions(), NELocallyConnectedLayer::validate(), and arm_compute::test::validation::weights.

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

◆ operator=() [1/2]

NELocallyConnectedLayer& operator= ( const NELocallyConnectedLayer )
delete

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

◆ operator=() [2/2]

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

184 {
185  if(!_is_prepared)
186  {
187  ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
188 
189  // Run weights reshaping and mark original weights tensor as unused
190  _weights_reshaped.allocator()->allocate();
191  NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
192  _original_weights->mark_as_unused();
193 
194  _is_prepared = true;
195  }
196 }
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
TensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: Tensor.cpp:48
void mark_as_unused() const
Marks a tensor as unused.
Definition: ITensor.cpp:167
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:96

References TensorAllocator::allocate(), Tensor::allocator(), ARM_COMPUTE_ERROR_ON, Scheduler::get(), ITensor::is_used(), ITensor::mark_as_unused(), and IScheduler::schedule().

Referenced by NELocallyConnectedLayer::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 167 of file NELocallyConnectedLayer.cpp.

168 {
169  prepare();
170 
171  MemoryGroupResourceScope scope_mg(_memory_group);
172 
173  // Run input reshaping
174  NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
175 
176  // Runs GEMM on reshaped matrices
177  NEScheduler::get().schedule(&_mm_kernel, Window::DimX);
178 
179  // Reshape output matrix
180  NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
181 }
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
void prepare() override
Prepare the function for executing.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
Memory group resources scope handling class.
Definition: IMemoryGroup.h:46
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:96

References Window::DimX, Window::DimY, Scheduler::get(), NELocallyConnectedLayer::prepare(), and IScheduler::schedule().

◆ 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 NELocallyConnectedLayer.

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: F16, 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 80 of file NELocallyConnectedLayer.cpp.

81 {
83  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != input->dimension(2));
84  ARM_COMPUTE_RETURN_ERROR_ON(!conv_info.padding_is_symmetric());
85 
86  bool has_bias = (biases != nullptr);
87 
88  if(has_bias)
89  {
90  ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
92  }
93 
94  const unsigned int kernel_width = weights->dimension(0);
95  const unsigned int kernel_height = weights->dimension(1);
96 
97  // Get convolved dimensions
98  unsigned int conv_w = 0;
99  unsigned int conv_h = 0;
100  std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height,
101  conv_info);
102 
103  ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != conv_w) || (output->dimension(1) != conv_h), "Output shape does not match the expected one");
104  ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(4) != (conv_w * conv_h), "Weights shape does not match the expected one");
105 
106  // Calculate intermediate buffer shapes
107  TensorShape shape_wr;
108  TensorShape shape_im2col;
109  TensorShape shape_gemm;
110  calculate_shapes(input, weights, biases, output, conv_info, shape_wr, shape_im2col, shape_gemm);
111 
112  TensorInfo weights_reshaped_info(shape_wr, 1, weights->data_type());
113  TensorInfo input_im2col_reshaped_info(shape_im2col, 1, input->data_type());
114  TensorInfo gemm_output_info(shape_gemm, 1, input->data_type());
115 
116  ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &input_im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, has_bias));
117  ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, &weights_reshaped_info));
118  ARM_COMPUTE_RETURN_ON_ERROR(NELocallyConnectedMatrixMultiplyKernel::validate(&input_im2col_reshaped_info, &weights_reshaped_info, &gemm_output_info));
119  ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h)));
120 
121  return Status{};
122 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Shape of a tensor.
Definition: TensorShape.h:39
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 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 NELocallyConnectedMatrix...
#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 *biases, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of NEWeightsReshapeKernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond,...)
If the condition is true, an error is returned.
Definition: Error.h:214
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
Static function to check if given info will lead to a valid configuration of NECol2ImKernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
Store the tensor's metadata.
Definition: TensorInfo.h:45
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 NEIm2ColKernel.

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(), NELocallyConnectedMatrixMultiplyKernel::validate(), NECol2ImKernel::validate(), NEWeightsReshapeKernel::validate(), NEIm2ColKernel::validate(), and arm_compute::test::validation::weights.

Referenced by NELocallyConnectedLayer::configure().


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