blob: b72f77e5c43269e2dc4ed071a55f711b0c8d3751 [file] [log] [blame]
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +01001/*
Jonathan Deakin464ed202023-01-12 11:41:14 +00002 * Copyright (c) 2021-2023 Arm Limited.
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +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 */
SiCong Lic5ab4df2023-10-17 17:38:57 +010024#ifndef ACL_SRC_CPU_OPERATORS_CPUFULLYCONNECTED_H
25#define ACL_SRC_CPU_OPERATORS_CPUFULLYCONNECTED_H
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010026
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010027#include "arm_compute/core/TensorInfo.h"
SiCong Li91295492023-07-21 18:16:13 +010028#include "arm_compute/function_info/FullyConnectedLayerInfo.h"
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010029
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "src/cpu/ICpuOperator.h"
31
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010032#include <memory>
33
34namespace arm_compute
35{
36namespace cpu
37{
38// Forward declarations
39class CpuConvertFullyConnectedWeights;
40class CpuFlatten;
41class CpuGemm;
42class CpuGemmLowpMatrixMultiplyCore;
43namespace kernels
44{
45class CpuTransposeKernel;
46} // namespace kernels
47/** Basic function to compute a Fully Connected layer. This function calls the following kernels:
48 * -# @ref kernels::CpuIm2ColKernel (called when the input comes from a convolutional layer)
49 * -# @ref kernels::CpuTransposeKernel (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
50 * -# @ref CpuGemm or @ref CpuGemmLowpMatrixMultiplyCore (if quantized asymmetric)
51 * -# @ref kernels::CpuGemmMatrixAdditionKernel or @ref CpuGemmLowpOutputStage (if quantized asymmetric) (if @p biases is not equal to nullptr)
52 *
53 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
54 */
55class CpuFullyConnected : public ICpuOperator
56{
57public:
58 /** Constructor */
59 CpuFullyConnected();
60 /** Destructor */
61 ~CpuFullyConnected();
62 /** Set the input and output tensors.
63 *
64 * Valid data layouts:
65 * - NHWC
66 * - NCHW
67 *
68 * Valid data type configurations:
69 * |src0 |src1 |src2 |dst |
70 * |:--------------|:------------------|:------|:--------------|
71 * |F16 |F16 |F16 |F16 |
72 * |F32 |F32 |F32 |F32 |
73 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
74 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
75 *
Milos Puzovic13b623e2022-07-27 17:53:21 +000076 * @param[in] src Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
77 * @param[in] weights Weights tensor info. The weights must be 2 dimensional.
78 * 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.
79 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
80 * Data type supported: Same as @p src.
81 * @param[in] biases Bias tensor info. Can be nullptr. Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED.
82 * @param[out] dst Destination tensor info. Its shape should be equal to the output of a matrix multiplication between:
83 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
84 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
85 * Data type supported: Same as @p src.
86 * @param[in] fc_info (Optional) Fully connected layer additional info
87 * @param[in] weights_info (Optional) Stores neccessary compute information when weights are already reshaped
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010088 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 void configure(const ITensorInfo *src,
90 const ITensorInfo *weights,
91 const ITensorInfo *biases,
92 ITensorInfo *dst,
93 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo(),
94 const WeightsInfo &weights_info = WeightsInfo());
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010095 /** Static function to check if given info will lead to a valid configuration of @ref CpuFullyConnected
96 *
Jonathan Deakin464ed202023-01-12 11:41:14 +000097 * Similar to @ref CpuFullyConnected::configure()
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010098 *
99 * @return a status
100 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100101 static Status validate(const ITensorInfo *src,
102 const ITensorInfo *weights,
103 const ITensorInfo *biases,
104 const ITensorInfo *dst,
105 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo(),
106 const WeightsInfo &weights_info = WeightsInfo());
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100107
Milos Puzovic13b623e2022-07-27 17:53:21 +0000108 /** Static function that queries whether there exists fixed-format kernel and if it exists it will return in the first argument in what format
109 * weights are expected to be reshaped as defined by WeightFormat class. Apart from the first argument the rest of the arguments are the same
110 * as in @ref CpuFullyConnectedLayer::validate() except that all arguments are required.
111 *
112 * @return a status
113 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 static Status has_opt_impl(arm_compute::WeightFormat &expected_weight_format,
115 const ITensorInfo *src,
116 const ITensorInfo *weights,
117 const ITensorInfo *biases,
118 const ITensorInfo *dst,
119 FullyConnectedLayerInfo fc_info,
120 WeightsInfo weights_info);
Milos Puzovic13b623e2022-07-27 17:53:21 +0000121
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100122 //Inherited methods override
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 void run(ITensorPack &tensors) override;
124 void prepare(ITensorPack &tensors) override;
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100125 experimental::MemoryRequirements workspace() const override;
126
127private:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100128 void configure_fc_fc(const ITensorInfo *src,
129 const ITensorInfo *weights,
130 const ITensorInfo *biases,
131 ITensorInfo *dst,
132 const ActivationLayerInfo &act);
133 void configure_conv_fc(const ITensorInfo *src,
134 const ITensorInfo *weights,
135 const ITensorInfo *biases,
136 ITensorInfo *dst,
137 const ActivationLayerInfo &act);
138 void configure_mm(const ITensorInfo *src,
139 const ITensorInfo *weights,
140 const ITensorInfo *biases,
141 ITensorInfo *dst,
142 const ActivationLayerInfo &act);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100143
144 enum AuxTensorIdx
145 {
146 AsmGemmWorkspace = 0,
147 Pretranspose,
SiCong Lic5ab4df2023-10-17 17:38:57 +0100148 GemmTemp1,
149 GemmTemp2,
150 GemmTemp3,
151 GemmTemp4,
152 GemmTemp5,
153 GemmTemp6,
154 GemmTemp7,
155 GemmTemp8,
156 // Slots above (0-9) reserved for either CpuGemm or CpuGemmLowpMatrixMultiplyCore
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100157 TransposedWeights,
158 ConvertedWeights,
159 FlattenedSrc,
160 Count
161 };
162
163 std::unique_ptr<CpuFlatten> _flatten;
164 std::unique_ptr<CpuConvertFullyConnectedWeights> _convert_weights;
165 std::unique_ptr<kernels::CpuTransposeKernel> _transpose_weights;
166 std::unique_ptr<CpuGemm> _mm_gemm;
167 std::unique_ptr<CpuGemmLowpMatrixMultiplyCore> _mm_gemmlowp;
168
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100169 TensorInfo _flattened_src;
170 TensorInfo _converted_weights;
171 TensorInfo _reshaped_weights;
172 TensorInfo _trans_weights;
173 AuxTensorIdx _trans_weights_idx;
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100174
175 experimental::MemoryRequirements _aux_mem;
176
Milos Puzovic13b623e2022-07-27 17:53:21 +0000177 bool _needs_weights_conversion;
178 bool _needs_weights_reshape;
179 bool _is_fc_after_conv;
180 bool _is_quantized_asymmetric;
181 bool _is_prepared;
182 bool _enable_fast_math;
183 bool _fixed_format;
184 arm_compute::WeightFormat _weight_format;
Viet-Hoa Doa3e57c22023-03-13 16:20:04 +0000185 bool _dynamic_weights;
186
187#ifdef ARM_COMPUTE_ASSERTS_ENABLED
SiCong Li91295492023-07-21 18:16:13 +0100188 int _asrt_run_count{};
189 int _asrt_prepare_count{};
Viet-Hoa Doa3e57c22023-03-13 16:20:04 +0000190#endif // ARM_COMPUTE_ASSERTS_ENABLED
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100191};
192} // namespace cpu
193} // namespace arm_compute
SiCong Lic5ab4df2023-10-17 17:38:57 +0100194#endif // ACL_SRC_CPU_OPERATORS_CPUFULLYCONNECTED_H