Compute Library
 19.08
CLLocallyConnectedLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 ARM Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
27 #include "arm_compute/core/Utils.h"
30 
31 #include <cmath>
32 #include <tuple>
33 
34 using namespace arm_compute;
35 
36 namespace
37 {
38 void calculate_shapes(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
39  TensorShape &shape_wr, TensorShape &shape_im2col, TensorShape &shape_gemm)
40 {
41  ARM_COMPUTE_UNUSED(output);
42 
43  const unsigned int kernel_width = weights->dimension(0);
44  const unsigned int kernel_height = weights->dimension(1);
45 
46  bool has_bias = (biases != nullptr);
47 
48  // Get convolved dimensions
49  unsigned int conv_w = 0;
50  unsigned int conv_h = 0;
51  std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0),
52  input->dimension(1),
53  kernel_width,
54  kernel_height,
55  conv_info);
56 
57  const size_t mat_weights_cols = weights->dimension(3);
58  const size_t mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + ((has_bias) ? 1 : 0);
59  const size_t mat_weights_num = weights->dimension(4);
60 
61  shape_wr = TensorShape(mat_weights_cols, mat_weights_rows, mat_weights_num);
62 
63  const size_t mat_input_cols = mat_weights_rows;
64  const size_t mat_input_rows = conv_w * conv_h;
65 
66  shape_im2col = input->tensor_shape();
67  if(shape_im2col.num_dimensions() >= 3)
68  {
69  shape_im2col.remove_dimension(2);
70  }
71  shape_im2col.set(0, mat_input_cols);
72  shape_im2col.set(1, mat_input_rows);
73 
74  shape_gemm = shape_im2col;
75  shape_gemm.set(0, mat_weights_cols);
76  shape_gemm.set(1, mat_input_rows);
77 }
78 } // namespace
79 
80 CLLocallyConnectedLayer::CLLocallyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager)
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 }
85 
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 }
129 
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 }
174 
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 }
190 
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 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
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 remove_dimension(size_t n)
Accessor to remove the dimension n from the tensor shape.
Definition: TensorShape.h:110
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
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
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:41
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
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.
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
void prepare() override
Prepare the function for executing.
Store the tensor's metadata.
Definition: ITensorInfo.h:40
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
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
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
Copyright (c) 2017-2018 ARM Limited.
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.
DataType data_type() const override
Data type used for each element of the tensor.
Definition: TensorInfo.h:256
void mark_as_unused() const
Marks a tensor as unused.
Definition: ITensor.cpp:167
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_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
void manage(TensorType *obj)
Sets a object to be managed by the given memory group.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
void configure(const ICLTensor *input, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups=1)
Set the input and output of the kernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond,...)
If the condition is true, an error is returned.
Definition: Error.h:214
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
void run() override
Run the kernels contained in the function.
CLLocallyConnectedLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Default constructor.
Padding and stride information class.
Definition: Types.h:676
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
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Memory group resources scope handling class.
Definition: IMemoryGroup.h:46
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
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
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:122
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:78
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, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
Set the input and output tensors.
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.