blob: 8bf3e95f81344d150f695ed90354b5ec01a653b9 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H
25#define ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/runtime/IFunction.h"
28
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010029#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitasef776a82018-07-25 17:57:49 +010030#include "arm_compute/runtime/NEON/functions/NEConvertFullyConnectedWeights.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010031#include "arm_compute/runtime/NEON/functions/NEFlattenLayer.h"
Giorgio Arenaa855af12018-07-16 17:20:38 +010032#include "arm_compute/runtime/NEON/functions/NEGEMM.h"
33#include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/runtime/Tensor.h"
35
36namespace arm_compute
37{
38/** Basic function to reshape the weights of Fully Connected layer with NEON. This function calls the following kernels:
39 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
41 */
Michalis Spyrou95abfdd2018-11-28 14:59:47 +000042class NEFullyConnectedLayerReshapeWeights : public INESimpleFunctionNoBorder
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043{
44public:
Michalis Spyrouebcebf12020-10-21 00:04:14 +010045 /** Constructor */
46 NEFullyConnectedLayerReshapeWeights() = default;
47 /** Prevent instances of this class from being copied (As this class contains pointers) */
48 NEFullyConnectedLayerReshapeWeights(const NEFullyConnectedLayerReshapeWeights &) = delete;
49 /** Prevent instances of this class from being copied (As this class contains pointers) */
50 NEFullyConnectedLayerReshapeWeights &operator=(const NEFullyConnectedLayerReshapeWeights &) = delete;
51 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
52 NEFullyConnectedLayerReshapeWeights(NEFullyConnectedLayerReshapeWeights &&) = delete;
53 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
54 NEFullyConnectedLayerReshapeWeights &operator=(NEFullyConnectedLayerReshapeWeights &&) = delete;
55 /** Default destructor */
56 ~NEFullyConnectedLayerReshapeWeights() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 /** Set the input and output tensors.
58 *
Georgios Pinitas33843562019-12-10 13:33:18 +000059 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Giorgio Arenaa855af12018-07-16 17:20:38 +010060 * @param[out] output Destination tensor. Data type supported: Same as @p input.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 */
Giorgio Arenaa855af12018-07-16 17:20:38 +010062 void configure(const ITensor *input, ITensor *output);
Giorgio Arena6200fa42018-07-06 17:06:36 +010063 /** Static function to check if given info will lead to a valid configuration of @ref NEFullyConnectedLayerReshapeWeights
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000064 *
Georgios Pinitas33843562019-12-10 13:33:18 +000065 * @param[in] input Weights tensor info. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Giorgio Arenaa855af12018-07-16 17:20:38 +010066 * @param[in] output Destination tensor info. Data type supported: Same as @p input.
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000067 *
68 * @return a status
69 */
Giorgio Arenaa855af12018-07-16 17:20:38 +010070 static Status validate(const ITensorInfo *input, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071};
72
Michalis Spyrou1a569a32019-09-10 17:20:34 +010073namespace weights_transformations
74{
75/** Basic function to manage the reshape weights generated from @ref NEFullyConnectedLayerReshapeWeights */
76class NEFullyConnectedLayerReshapeWeightsManaged : public ITransformWeights
77{
78public:
79 void run() override
80 {
81 _output.allocator()->allocate();
82 _func.run();
83 _reshape_run = true;
84 }
85
86 void release() override
87 {
88 _output.allocator()->free();
89 }
90
91 ITensor *get_weights() override
92 {
93 return &_output;
94 }
95
96 uint32_t uid() override
97 {
98 return _uid;
99 }
100
101 void configure(const ITensor *input)
102 {
103 _func.configure(input, &_output);
104 }
105
106private:
107 static constexpr uint32_t _uid = 0x0;
108 Tensor _output{};
109 NEFullyConnectedLayerReshapeWeights _func{};
110};
111} // namespace weights_transformations
112
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113/** Basic function to compute a Fully Connected layer on NEON. This function calls the following NEON kernels:
Giorgio Arenaa855af12018-07-16 17:20:38 +0100114 * -# @ref NEIm2ColKernel (called when the input comes from a convolutional layer)
115 * -# @ref NEFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
116 * -# @ref NEGEMMMatrixMultiplyKernel or @ref NEGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
SiCong Liadb32912020-02-17 16:39:27 +0000117 * -# @ref NEGEMMMatrixAdditionKernel or @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if quantized asymmetric) (if @p biases is not equal to nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118 *
119 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
120 */
121class NEFullyConnectedLayer : public IFunction
122{
123public:
124 /** Constructor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100125 NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +0000126 /** Prevent instances of this class from being copied (As this class contains pointers) */
127 NEFullyConnectedLayer(const NEFullyConnectedLayer &) = delete;
Michalis Spyrou770dfeb2020-11-04 18:55:34 +0000128 /** Prevent instances of this class from being moved (As this class contains pointers) */
129 NEFullyConnectedLayer(NEFullyConnectedLayer &&) = delete;
Georgios Pinitas1562be32018-03-08 19:09:19 +0000130 /** Prevent instances of this class from being copied (As this class contains pointers) */
131 NEFullyConnectedLayer &operator=(const NEFullyConnectedLayer &) = delete;
Michalis Spyrou770dfeb2020-11-04 18:55:34 +0000132 /** Prevent instances of this class from being moved (As this class contains pointers) */
133 NEFullyConnectedLayer &operator=(NEFullyConnectedLayer &&) = delete;
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100134 /** Default destructor */
135 ~NEFullyConnectedLayer();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 /** Set the input and output tensors.
137 *
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000138 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Giorgio Arenaa855af12018-07-16 17:20:38 +0100139 * @param[in] weights Weights tensor. The weights must be 2 dimensional.
140 * 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.
141 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
142 * Data type supported: Same as @p input.
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000143 * @param[in] biases Bias tensor. Can be nullptr. Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED.
Giorgio Arenaa855af12018-07-16 17:20:38 +0100144 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
145 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
146 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
147 * Data type supported: Same as @p input.
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100148 * @param[in] fc_info (Optional) Fully connected layer additional info
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100150 void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
151 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Giorgio Arenaa855af12018-07-16 17:20:38 +0100152 /** Static function to check if given info will lead to a valid configuration of @ref NEFullyConnectedLayer
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000153 *
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000154 * @param[in] input Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000155 * @param[in] weights Weights tensor info. The weights must be 2 dimensional.
156 * 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.
157 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
158 * Data type supported: Same as @p input.
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000159 * @param[in] biases Bias tensor. Can be nullptr. Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED.
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000160 * @param[in] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between:
161 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
162 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
163 * Data type supported: Same as @p input.
164 * @param[in] fc_info (Optional) Fully connected layer additional info
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000165 *
166 * @return a status
167 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100168 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
169 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170
171 //Inherited methods override
172 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100173 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174
175private:
SiCongLi2e5fd632020-03-02 15:39:15 +0000176 void configure_fc_fc(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act);
177 void configure_conv_fc(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act);
178 void configure_mm(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100179
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100180 MemoryGroup _memory_group;
181 IWeightsManager *_weights_manager;
Georgios Pinitase2696b12020-12-03 20:37:43 +0000182 NEFlattenLayer _flatten;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100183 NEConvertFullyConnectedWeights _convert_weights;
184 weights_transformations::NEConvertFullyConnectedWeightsManaged _convert_weights_managed;
185 NEFullyConnectedLayerReshapeWeights _reshape_weights_function;
186 weights_transformations::NEFullyConnectedLayerReshapeWeightsManaged _reshape_weights_managed_function;
187 NEGEMM _mm_gemm;
188 NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100189 Tensor _flatten_output;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100190 Tensor _converted_weights_output;
191 Tensor _reshape_weights_output;
192 const ITensor *_original_weights;
193 bool _are_weights_converted;
194 bool _are_weights_reshaped;
195 bool _is_fc_after_conv;
SiCongLi2e5fd632020-03-02 15:39:15 +0000196 bool _is_quantized_asymmetric;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100197 bool _is_prepared;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198};
Georgios Pinitas1562be32018-03-08 19:09:19 +0000199} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000200#endif /* ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H */