Compute Library
 21.02
NEFullyConnectedLayer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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 
29 
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  * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
42  */
44 {
45 public:
46  /** Constructor */
48  /** Prevent instances of this class from being copied (As this class contains pointers) */
50  /** Prevent instances of this class from being copied (As this class contains pointers) */
52  /** Prevent instances of this class from being moved (As this class contains non movable objects) */
54  /** Prevent instances of this class from being moved (As this class contains non movable objects) */
56  /** Default destructor */
58  /** Set the input and output tensors.
59  *
60  * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
61  * @param[out] output Destination tensor. Data type supported: Same as @p input.
62  */
63  void configure(const ITensor *input, ITensor *output);
64  /** Static function to check if given info will lead to a valid configuration of @ref NEFullyConnectedLayerReshapeWeights
65  *
66  * @param[in] input Weights tensor info. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
67  * @param[in] output Destination tensor info. Data type supported: Same as @p input.
68  *
69  * @return a status
70  */
71  static Status validate(const ITensorInfo *input, const ITensorInfo *output);
72 };
73 
74 namespace weights_transformations
75 {
76 /** Basic function to manage the reshape weights generated from @ref NEFullyConnectedLayerReshapeWeights */
78 {
79 public:
80  void run() override
81  {
82  _output.allocator()->allocate();
83  _func.run();
84  _reshape_run = true;
85  }
86 
87  void release() override
88  {
89  _output.allocator()->free();
90  }
91 
92  ITensor *get_weights() override
93  {
94  return &_output;
95  }
96 
97  uint32_t uid() override
98  {
99  return _uid;
100  }
101 
102  void configure(const ITensor *input)
103  {
104  _func.configure(input, &_output);
105  }
106 
107 private:
108  static constexpr uint32_t _uid = 0x0;
109  Tensor _output{};
111 };
112 } // namespace weights_transformations
113 
114 /** Basic function to compute a Fully Connected layer on Neon. This function calls the following Neon kernels:
115  * -# @ref NEIm2ColKernel (called when the input comes from a convolutional layer)
116  * -# @ref NEFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
117  * -# @ref NEGEMMMatrixMultiplyKernel or @ref NEGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
118  * -# @ref NEGEMMMatrixAdditionKernel or @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if quantized asymmetric) (if @p biases is not equal to nullptr)
119  *
120  * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
121  */
123 {
124 public:
125  /** Constructor */
126  NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
127  /** Prevent instances of this class from being copied (As this class contains pointers) */
129  /** Prevent instances of this class from being moved (As this class contains pointers) */
131  /** Prevent instances of this class from being copied (As this class contains pointers) */
133  /** Prevent instances of this class from being moved (As this class contains pointers) */
135  /** Default destructor */
137  /** Set the input and output tensors.
138  *
139  * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
140  * @param[in] weights Weights tensor. The weights must be 2 dimensional.
141  * 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.
142  * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
143  * Data type supported: Same as @p input.
144  * @param[in] biases Bias tensor. Can be nullptr. Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED.
145  * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
146  * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
147  * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
148  * Data type supported: Same as @p input.
149  * @param[in] fc_info (Optional) Fully connected layer additional info
150  */
151  void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
153  /** Static function to check if given info will lead to a valid configuration of @ref NEFullyConnectedLayer
154  *
155  * @param[in] input Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
156  * @param[in] weights Weights tensor info. The weights must be 2 dimensional.
157  * 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.
158  * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
159  * Data type supported: Same as @p input.
160  * @param[in] biases Bias tensor. Can be nullptr. Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED.
161  * @param[in] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between:
162  * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
163  * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
164  * Data type supported: Same as @p input.
165  * @param[in] fc_info (Optional) Fully connected layer additional info
166  *
167  * @return a status
168  */
169  static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
171 
172  //Inherited methods override
173  void run() override;
174  void prepare() override;
175 
176 private:
177  void configure_fc_fc(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act);
178  void configure_conv_fc(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act);
179  void configure_mm(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act);
180 
181  MemoryGroup _memory_group;
182  IWeightsManager *_weights_manager;
183  NEFlattenLayer _flatten;
184  NEConvertFullyConnectedWeights _convert_weights;
186  NEFullyConnectedLayerReshapeWeights _reshape_weights_function;
188  NEGEMM _mm_gemm;
189  NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
190  Tensor _flatten_output;
191  Tensor _converted_weights_output;
192  Tensor _reshape_weights_output;
193  const ITensor *_original_weights;
194  bool _are_weights_converted;
195  bool _are_weights_reshaped;
196  bool _is_fc_after_conv;
197  bool _is_quantized_asymmetric;
198  bool _is_prepared;
199 };
200 } // namespace arm_compute
201 #endif /* ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H */
~NEFullyConnectedLayerReshapeWeights()=default
Default destructor.
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:62
ITensor * get_weights() override
Get a pointer to the transformed weights.
NEFullyConnectedLayerReshapeWeights & operator=(const NEFullyConnectedLayerReshapeWeights &)=delete
Prevent instances of this class from being copied (As this class contains pointers) ...
Fully connected layer info.
Definition: Types.h:1613
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Status class.
Definition: Error.h:52
Activation Layer Information class.
Definition: Types.h:1550
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
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.
virtual void prepare()
Prepare the function for executing.
Definition: IFunction.h:57
NEFullyConnectedLayerReshapeWeights()=default
Constructor.
Basic implementation of the tensor interface.
Definition: Tensor.h:37
Weights manager interface to handle weights transformations.
Basic function to compute a Fully Connected layer on Neon.
Weights tensor transform interface In order to identify the different reshape functions, each reshape function has to generate a unique id.
Basic function to execute flatten layer kernel.
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.
Basic function to manage the reshape weights generated from NEFullyConnectedLayerReshapeWeights.
Basic function to execute GEMMLowpMatrixMultiplyCore on Neon.