Compute Library
 20.08
NEFullyConnectedLayer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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  */
24 #ifndef ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H
25 #define ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H
26 
28 
36 
37 namespace arm_compute
38 {
39 /** Basic function to reshape the weights of Fully Connected layer with NEON. This function calls the following kernels:
40  *
41  * -# @ref NETransposeKernel
42  *
43  * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
44  */
46 {
47 public:
48  /** Set the input and output tensors.
49  *
50  * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
51  * @param[out] output Destination tensor. Data type supported: Same as @p input.
52  */
53  void configure(const ITensor *input, ITensor *output);
54  /** Static function to check if given info will lead to a valid configuration of @ref NEFullyConnectedLayerReshapeWeights
55  *
56  * @param[in] input Weights tensor info. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
57  * @param[in] output Destination tensor info. Data type supported: Same as @p input.
58  *
59  * @return a status
60  */
61  static Status validate(const ITensorInfo *input, const ITensorInfo *output);
62 };
63 
64 namespace weights_transformations
65 {
66 /** Basic function to manage the reshape weights generated from @ref NEFullyConnectedLayerReshapeWeights */
68 {
69 public:
70  void run() override
71  {
72  _output.allocator()->allocate();
73  _func.run();
74  _reshape_run = true;
75  }
76 
77  void release() override
78  {
79  _output.allocator()->free();
80  }
81 
82  ITensor *get_weights() override
83  {
84  return &_output;
85  }
86 
87  uint32_t uid() override
88  {
89  return _uid;
90  }
91 
92  void configure(const ITensor *input)
93  {
94  _func.configure(input, &_output);
95  }
96 
97 private:
98  static constexpr uint32_t _uid = 0x0;
99  Tensor _output{};
101 };
102 } // namespace weights_transformations
103 
104 /** Basic function to compute a Fully Connected layer on NEON. This function calls the following NEON kernels:
105  * -# @ref NEIm2ColKernel (called when the input comes from a convolutional layer)
106  * -# @ref NEFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
107  * -# @ref NEGEMMMatrixMultiplyKernel or @ref NEGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
108  * -# @ref NEGEMMMatrixAdditionKernel or @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if quantized asymmetric) (if @p biases is not equal to nullptr)
109  *
110  * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
111  */
113 {
114 public:
115  /** Constructor */
116  NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
117  /** Prevent instances of this class from being copied (As this class contains pointers) */
119  /** Default move constructor */
121  /** Prevent instances of this class from being copied (As this class contains pointers) */
123  /** Default move assignment operator */
125  /** Set the input and output tensors.
126  *
127  * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
128  * @param[in] weights Weights tensor. The weights must be 2 dimensional.
129  * 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.
130  * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
131  * Data type supported: Same as @p input.
132  * @param[in] biases Bias tensor. Can be nullptr. Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED.
133  * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
134  * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
135  * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
136  * Data type supported: Same as @p input.
137  * @param[in] fc_info (Optional) Fully connected layer additional info
138  */
139  void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
141  /** Static function to check if given info will lead to a valid configuration of @ref NEFullyConnectedLayer
142  *
143  * @param[in] input Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
144  * @param[in] weights Weights tensor info. The weights must be 2 dimensional.
145  * 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.
146  * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
147  * Data type supported: Same as @p input.
148  * @param[in] biases Bias tensor. Can be nullptr. Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED.
149  * @param[in] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between:
150  * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
151  * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
152  * Data type supported: Same as @p input.
153  * @param[in] fc_info (Optional) Fully connected layer additional info
154  *
155  * @return a status
156  */
157  static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
159 
160  //Inherited methods override
161  void run() override;
162  void prepare() override;
163 
164 private:
165  void configure_fc_fc(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act);
166  void configure_conv_fc(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act);
167  void configure_mm(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act);
168 
169  MemoryGroup _memory_group;
170  IWeightsManager *_weights_manager;
171  NEFlattenLayerKernel _flatten_kernel;
172  NEConvertFullyConnectedWeights _convert_weights;
174  NEFullyConnectedLayerReshapeWeights _reshape_weights_function;
176  NEGEMM _mm_gemm;
177  NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
178  Tensor _flatten_output;
179  Tensor _converted_weights_output;
180  Tensor _reshape_weights_output;
181  const ITensor *_original_weights;
182  bool _are_weights_converted;
183  bool _are_weights_reshaped;
184  bool _is_fc_after_conv;
185  bool _is_quantized_asymmetric;
186  bool _is_prepared;
187 };
188 } // namespace arm_compute
189 #endif /* ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H */
Base class for all functions.
Definition: IFunction.h:30
void run() override final
Run the kernels contained in the function.
Basic function to reshape the weights of Fully Connected layer with NEON.
Basic function to execute GEMM on NEON.
Definition: NEGEMM.h:59
ITensor * get_weights() override
Get a pointer to the transformed weights.
Fully connected layer info.
Definition: Types.h:1580
Store the tensor's metadata.
Definition: ITensorInfo.h:40
Status class.
Definition: Error.h:52
Activation Layer Information class.
Definition: Types.h:1517
Interface for NEON tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2020 Arm Limited.
Interface for the flatten layer kernel.
TensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: Tensor.cpp:48
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of NEFullyConnectedLayerRes...
Basic function to run NEConvertFullyConnectedWeightsKernel.
Basic interface for functions which have a single NEON kernel and no border.
void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Set the input and output tensors.
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
NEFullyConnectedLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr, IWeightsManager *weights_manager=nullptr)
Constructor.
Basic implementation of the tensor interface.
Definition: Tensor.h:37
void free() override
Free allocated CPU memory.
Weights manager interface to handle weights transformations.
Basic function to compute a Fully Connected layer on NEON.
void run() override
Run the kernels contained in the function.
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 NEFullyConnectedLayer.
Weights tensor transform interface In order to identify the different reshape functions,...
void configure(const ITensor *input, ITensor *output)
Set the input and output tensors.
uint32_t uid() override
Function that returns a unique id of the reshape function.
void prepare() override
Prepare the function for executing.
Basic function to manage the reshape weights generated from NEFullyConnectedLayerReshapeWeights.
NEFullyConnectedLayer & operator=(const NEFullyConnectedLayer &)=delete
Prevent instances of this class from being copied (As this class contains pointers)
Basic function to execute GEMMLowpMatrixMultiplyCore on NEON.