Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / arm_compute / runtime / NEON / functions / NEFullyConnectedHybridLayer.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Copyright (c) 2017-2019 ARM Limited.
19  *
20  * SPDX-License-Identifier: MIT
21  *
22  * Permission is hereby granted, free of charge, to any person obtaining a copy
23  * of this software and associated documentation files (the "Software"), to
24  * deal in the Software without restriction, including without limitation the
25  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26  * sell copies of the Software, and to permit persons to whom the Software is
27  * furnished to do so, subject to the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be included in all
30  * copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38  * SOFTWARE.
39  */
40
41 #ifndef __ARM_COMPUTE_NEFULLYCONNECTEDHYBRIDLAYER_H__
42 #define __ARM_COMPUTE_NEFULLYCONNECTEDHYBRIDLAYER_H__
43
44 #include "arm_compute/core/NEON/kernels/NEQuantizationSymmetricKernel.h"
45 #include "arm_compute/core/NEON/kernels/NEGEMMMatrixAccumulateBiasesKernel.h"
46 #include "arm_compute/core/NEON/kernels/NEMuliplyScaleFactorKernel.h"
47 #include "arm_compute/core/NEON/kernels/NETransposeKernel.h"
48 #include "arm_compute/runtime/MemoryGroup.h"
49 #include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
50 #include "arm_compute/runtime/NEON/INESimpleFunctionNoBorder.h"
51 #include "arm_compute/runtime/Tensor.h"
52
53 namespace arm_compute
54 {
55 /** Basic function to reshape the weights of Fully Connected layer with NEON. This function calls
56  * the following kernels:
57  *
58  *  -# @ref NETransposeKernel
59  *
60  * @note  The fully connected layer accepts "weights" tensors only with 2 dimensions.
61  */
62 class NEFullyConnectedHybridLayerReshapeWeights : public INESimpleFunctionNoBorder
63 {
64 public:
65   /** Set the input and output tensors.
66    *
67    * @param[in]  input  Weights tensor. The weights must be 2 dimensional. Data types supported:
68    * QASYMM8/F16/F32.
69    * @param[out] output Destination tensor. Data type supported: Same as @p input.
70    */
71   void configure(const ITensor *input, ITensor *output);
72   /** Static function to check if given info will lead to a valid configuration of @ref
73    * NEFullyConnectedHybridLayerReshapeWeights
74    *
75    * @param[in] input  Weights tensor info. The weights must be 2 dimensional. Data types supported:
76    * QASYMM8/F16/F32.
77    * @param[in] output Destination tensor info. Data type supported: Same as @p input.
78    *
79    * @return a status
80    */
81   static Status validate(const ITensorInfo *input, const ITensorInfo *output);
82 };
83
84 /** Basic function to compute a Fully Connected layer on NEON. This function calls the following
85  * NEON kernels:
86  *  -# @ref NEIm2ColKernel (called when the input comes from a convolutional layer)
87  *  -# @ref NEFullyConnectedHybridLayerReshapeWeights (if @p are_weights_reshaped is set to false
88  * and transpose_weights is set to true ) (called once)
89  *  -# @ref NEGEMMMatrixMultiplyKernel or @ref NEGEMMLowpMatrixMultiplyCore (if quantized
90  * asymmetric)
91  *  -# @ref NEGEMMMatrixAccumulateBiasesKernel or @ref
92  * NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if quantized asymmetric) (if @p biases is
93  * not equal to nullptr)
94  *
95  * @note  The fully connected layer accepts "weights" tensors only with 2 dimensions.
96  */
97 class NEFullyConnectedHybridLayer : public IFunction
98 {
99 public:
100   /** Constructor */
101   NEFullyConnectedHybridLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
102   /** Prevent instances of this class from being copied (As this class contains pointers) */
103   NEFullyConnectedHybridLayer(const NEFullyConnectedHybridLayer &) = delete;
104   /** Default move constructor */
105   NEFullyConnectedHybridLayer(NEFullyConnectedHybridLayer &&) = default;
106   /** Prevent instances of this class from being copied (As this class contains pointers) */
107   NEFullyConnectedHybridLayer &operator=(const NEFullyConnectedHybridLayer &) = delete;
108   /** Default move assignment operator */
109   NEFullyConnectedHybridLayer &operator=(NEFullyConnectedHybridLayer &&) = default;
110   /** Set the input and output tensors.
111    *
112    * @param[in]  input   Source tensor. Data type supported: F16/F32.
113    * @param[in]  weights Weights tensor. The weights must be 2 dimensional.
114    *                     If this function is called after a Convolution Layer, the (transposed)
115    * weights will have as many rows as the product of the first 3 input's dimensions.
116    *                     If it is called after another FullyConnected Layer, the (transposed)
117    * weights will have as many rows as the input's first dimension.
118    *                     Data type supported: S8.
119    * @param[in]  biases  Bias tensor. Can be nullptr. Data type supported:Same as @p input.
120    * @param[out] output  Destination tensor. Its shape should be equal to the output of a matrix
121    * multiplication between:
122    *                     - The output of im2col on the input and the (transposed) 2D weights, if the
123    * function is called after a Convolution Layer
124    *                     - The input tensor and the (transposed) 2D weights, if the function is
125    * called after another FullyConnected Layer.
126    *                     Data type supported: Same as @p input.
127    * @param[in]  fc_info (Optional) Fully connected layer additional info
128    */
129   void configure(const ITensor *input, const ITensor *weights, const ITensor *biases,
130                  ITensor *output, FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
131   /** Static function to check if given info will lead to a valid configuration of @ref
132    * NEFullyConnectedHybridLayer
133    *
134    * @param[in]  input   Source tensor info. Data type supported: F16/F32.
135    * @param[in]  weights Weights tensor info. The weights must be 2 dimensional.
136    *                     If this function is called after a Convolution Layer, the (transposed)
137    * weights will have as many rows as the product of the first 3 input's dimensions.
138    *                     If it is called after another FullyConnected Layer, the (transposed)
139    * weights will have as many rows as the input's first dimension.
140    *                     Data type supported: S8.
141    * @param[in]  biases  Bias tensor info. Can be nullptr. Data type supported:Same as @p input.
142    * @param[out] output  Destination tensor info. Its shape should be equal to the output of a
143    * matrix multiplication between:
144    *                     - The output of im2col on the input and the (transposed) 2D weights, if the
145    * function is called after a Convolution Layer
146    *                     - The input tensor and the (transposed) 2D weights, if the function is
147    * 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    * @return a status
152    */
153   static Status validate(const ITensorInfo *input, const ITensorInfo *weights,
154                          const ITensorInfo *biases, const ITensorInfo *output,
155                          FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
156
157   // Inherited methods override
158   void run() override;
159   void prepare() override;
160
161 private:
162   void configure_mm(const ITensor *input, const ITensor *weights, ITensor *output);
163
164   MemoryGroup _memory_group;
165   NEFullyConnectedHybridLayerReshapeWeights _reshape_weights_function;
166   NEQuantizationSymmetricKernel _quant_input_kernel;
167   NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
168   NEMultiplyScaleFactorKernel _multiply_scale_kernel;
169   NEGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel;
170   Tensor _reshape_weights_output;
171   Tensor _quantized_input;
172   Tensor _scale_factor;
173   Tensor _gemmlowp_output;
174   const ITensor *_original_weights;
175   bool _are_weights_reshaped;
176   bool _accumulate_biases;
177   bool _is_prepared;
178 };
179 } // namespace arm_compute
180 #endif /* __ARM_COMPUTE_NEFULLYCONNECTEDHYBRIDLAYER_H__ */