blob: 78f12daf9cd354321eec759b4ec7eb1cc9d10420 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgio9c700372020-01-08 11:33:44 +00002 * 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
Giorgio Arena368e6352018-08-20 15:06:07 +010029#include "arm_compute/core/NEON/kernels/NEFlattenLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/NEON/kernels/NEGEMMMatrixAccumulateBiasesKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/NEON/kernels/NETransposeKernel.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010032#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitasef776a82018-07-25 17:57:49 +010033#include "arm_compute/runtime/NEON/functions/NEConvertFullyConnectedWeights.h"
Giorgio Arenaa855af12018-07-16 17:20:38 +010034#include "arm_compute/runtime/NEON/functions/NEGEMM.h"
35#include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
36#include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037#include "arm_compute/runtime/Tensor.h"
38
39namespace arm_compute
40{
41/** Basic function to reshape the weights of Fully Connected layer with NEON. This function calls the following kernels:
42 *
Giorgio Arenaa855af12018-07-16 17:20:38 +010043 * -# @ref NETransposeKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 *
45 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
46 */
Michalis Spyrou95abfdd2018-11-28 14:59:47 +000047class NEFullyConnectedLayerReshapeWeights : public INESimpleFunctionNoBorder
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048{
49public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050 /** Set the input and output tensors.
51 *
Georgios Pinitas33843562019-12-10 13:33:18 +000052 * @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 +010053 * @param[out] output Destination tensor. Data type supported: Same as @p input.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 */
Giorgio Arenaa855af12018-07-16 17:20:38 +010055 void configure(const ITensor *input, ITensor *output);
Giorgio Arena6200fa42018-07-06 17:06:36 +010056 /** Static function to check if given info will lead to a valid configuration of @ref NEFullyConnectedLayerReshapeWeights
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000057 *
Georgios Pinitas33843562019-12-10 13:33:18 +000058 * @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 +010059 * @param[in] output Destination tensor info. Data type supported: Same as @p input.
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000060 *
61 * @return a status
62 */
Giorgio Arenaa855af12018-07-16 17:20:38 +010063 static Status validate(const ITensorInfo *input, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064};
65
Michalis Spyrou1a569a32019-09-10 17:20:34 +010066namespace weights_transformations
67{
68/** Basic function to manage the reshape weights generated from @ref NEFullyConnectedLayerReshapeWeights */
69class NEFullyConnectedLayerReshapeWeightsManaged : public ITransformWeights
70{
71public:
72 void run() override
73 {
74 _output.allocator()->allocate();
75 _func.run();
76 _reshape_run = true;
77 }
78
79 void release() override
80 {
81 _output.allocator()->free();
82 }
83
84 ITensor *get_weights() override
85 {
86 return &_output;
87 }
88
89 uint32_t uid() override
90 {
91 return _uid;
92 }
93
94 void configure(const ITensor *input)
95 {
96 _func.configure(input, &_output);
97 }
98
99private:
100 static constexpr uint32_t _uid = 0x0;
101 Tensor _output{};
102 NEFullyConnectedLayerReshapeWeights _func{};
103};
104} // namespace weights_transformations
105
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106/** Basic function to compute a Fully Connected layer on NEON. This function calls the following NEON kernels:
Giorgio Arenaa855af12018-07-16 17:20:38 +0100107 * -# @ref NEIm2ColKernel (called when the input comes from a convolutional layer)
108 * -# @ref NEFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
109 * -# @ref NEGEMMMatrixMultiplyKernel or @ref NEGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
110 * -# @ref NEGEMMMatrixAccumulateBiasesKernel or @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if quantized asymmetric) (if @p biases is not equal to nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 *
112 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
113 */
114class NEFullyConnectedLayer : public IFunction
115{
116public:
117 /** Constructor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100118 NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +0000119 /** Prevent instances of this class from being copied (As this class contains pointers) */
120 NEFullyConnectedLayer(const NEFullyConnectedLayer &) = delete;
121 /** Default move constructor */
122 NEFullyConnectedLayer(NEFullyConnectedLayer &&) = default;
123 /** Prevent instances of this class from being copied (As this class contains pointers) */
124 NEFullyConnectedLayer &operator=(const NEFullyConnectedLayer &) = delete;
125 /** Default move assignment operator */
126 NEFullyConnectedLayer &operator=(NEFullyConnectedLayer &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 /** Set the input and output tensors.
128 *
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000129 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Giorgio Arenaa855af12018-07-16 17:20:38 +0100130 * @param[in] weights Weights tensor. The weights must be 2 dimensional.
131 * 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.
132 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
133 * Data type supported: Same as @p input.
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000134 * @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 +0100135 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
136 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
137 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
138 * Data type supported: Same as @p input.
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100139 * @param[in] fc_info (Optional) Fully connected layer additional info
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100141 void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
142 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Giorgio Arenaa855af12018-07-16 17:20:38 +0100143 /** Static function to check if given info will lead to a valid configuration of @ref NEFullyConnectedLayer
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000144 *
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000145 * @param[in] input Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000146 * @param[in] weights Weights tensor info. The weights must be 2 dimensional.
147 * 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.
148 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
149 * Data type supported: Same as @p input.
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000150 * @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 +0000151 * @param[in] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between:
152 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
153 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
154 * Data type supported: Same as @p input.
155 * @param[in] fc_info (Optional) Fully connected layer additional info
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000156 *
157 * @return a status
158 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100159 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
160 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161
162 //Inherited methods override
163 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100164 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
166private:
Giorgio Arenaa855af12018-07-16 17:20:38 +0100167 void configure_fc_fc(const ITensor *input, const ITensor *weights, ITensor *output);
168 void configure_conv_fc(const ITensor *input, const ITensor *weights, ITensor *output);
169 void configure_mm(const ITensor *input, const ITensor *weights, ITensor *output);
170
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100171 MemoryGroup _memory_group;
172 IWeightsManager *_weights_manager;
173 NEFlattenLayerKernel _flatten_kernel;
174 NEConvertFullyConnectedWeights _convert_weights;
175 weights_transformations::NEConvertFullyConnectedWeightsManaged _convert_weights_managed;
176 NEFullyConnectedLayerReshapeWeights _reshape_weights_function;
177 weights_transformations::NEFullyConnectedLayerReshapeWeightsManaged _reshape_weights_managed_function;
178 NEGEMM _mm_gemm;
179 NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000180 NEGEMMLowpOutputStage _gemmlowp_output_stage;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100181 NEGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel;
182 Tensor _flatten_output;
183 Tensor _gemmlowp_output;
184 Tensor _converted_weights_output;
185 Tensor _reshape_weights_output;
186 const ITensor *_original_weights;
187 bool _are_weights_converted;
188 bool _are_weights_reshaped;
189 bool _is_fc_after_conv;
190 bool _accumulate_biases;
191 bool _is_quantized;
192 bool _is_prepared;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193};
Georgios Pinitas1562be32018-03-08 19:09:19 +0000194} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000195#endif /* ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H */