blob: 1e8c6478d09283ae863078cc3f0b083afcbef55c [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 */
24#ifndef ARM_COMPUTE_CPU_FULLY_CONNECTED_H
25#define ARM_COMPUTE_CPU_FULLY_CONNECTED_H
26
Georgios Pinitas7891a732021-08-20 21:39:25 +010027#include "src/cpu/ICpuOperator.h"
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010028
29#include "arm_compute/core/TensorInfo.h"
SiCong Li91295492023-07-21 18:16:13 +010030#include "arm_compute/function_info/FullyConnectedLayerInfo.h"
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010031
32#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 */
89 void configure(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst,
Milos Puzovic13b623e2022-07-27 17:53:21 +000090 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo(), const WeightsInfo &weights_info = WeightsInfo());
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010091 /** Static function to check if given info will lead to a valid configuration of @ref CpuFullyConnected
92 *
Jonathan Deakin464ed202023-01-12 11:41:14 +000093 * Similar to @ref CpuFullyConnected::configure()
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010094 *
95 * @return a status
96 */
97 static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
Jonathan Deakin464ed202023-01-12 11:41:14 +000098 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo(), const WeightsInfo &weights_info = WeightsInfo());
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010099
Milos Puzovic13b623e2022-07-27 17:53:21 +0000100 /** Static function that queries whether there exists fixed-format kernel and if it exists it will return in the first argument in what format
101 * weights are expected to be reshaped as defined by WeightFormat class. Apart from the first argument the rest of the arguments are the same
102 * as in @ref CpuFullyConnectedLayer::validate() except that all arguments are required.
103 *
104 * @return a status
105 */
106 static Status has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *src, const ITensorInfo *weights,
107 const ITensorInfo *biases, const ITensorInfo *dst,
108 FullyConnectedLayerInfo fc_info, WeightsInfo weights_info);
109
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100110 //Inherited methods override
SiCong Li91295492023-07-21 18:16:13 +0100111 void run(ITensorPack &tensors) override;
112 void prepare(ITensorPack &tensors) override;
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100113 experimental::MemoryRequirements workspace() const override;
114
115private:
116 void configure_fc_fc(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ActivationLayerInfo &act);
117 void configure_conv_fc(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ActivationLayerInfo &act);
118 void configure_mm(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ActivationLayerInfo &act);
119
120 enum AuxTensorIdx
121 {
122 AsmGemmWorkspace = 0,
123 Pretranspose,
124 GemmTemp1, // Both CpuGemm and CpuGemmLowpMatrixMultiplyCore
125 GemmTemp2, // Both CpuGemm and CpuGemmLowpMatrixMultiplyCore
126 GemmTemp3, // Both CpuGemm and CpuGemmLowpMatrixMultiplyCore
127 GemmTemp4, // CpuGemmLowpMatrixMultiplyCore only
128 GemmTemp5, // CpuGemmLowpMatrixMultiplyCore only
129 GemmTemp6, // CpuGemmLowpMatrixMultiplyCore only
130 GemmTemp7, // CpuGemmLowpMatrixMultiplyCore only
131 TransposedWeights,
132 ConvertedWeights,
133 FlattenedSrc,
134 Count
135 };
136
137 std::unique_ptr<CpuFlatten> _flatten;
138 std::unique_ptr<CpuConvertFullyConnectedWeights> _convert_weights;
139 std::unique_ptr<kernels::CpuTransposeKernel> _transpose_weights;
140 std::unique_ptr<CpuGemm> _mm_gemm;
141 std::unique_ptr<CpuGemmLowpMatrixMultiplyCore> _mm_gemmlowp;
142
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100143 TensorInfo _flattened_src;
144 TensorInfo _converted_weights;
145 TensorInfo _reshaped_weights;
146 TensorInfo _trans_weights;
147 AuxTensorIdx _trans_weights_idx;
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100148
149 experimental::MemoryRequirements _aux_mem;
150
Milos Puzovic13b623e2022-07-27 17:53:21 +0000151 bool _needs_weights_conversion;
152 bool _needs_weights_reshape;
153 bool _is_fc_after_conv;
154 bool _is_quantized_asymmetric;
155 bool _is_prepared;
156 bool _enable_fast_math;
157 bool _fixed_format;
158 arm_compute::WeightFormat _weight_format;
Viet-Hoa Doa3e57c22023-03-13 16:20:04 +0000159 bool _dynamic_weights;
160
161#ifdef ARM_COMPUTE_ASSERTS_ENABLED
SiCong Li91295492023-07-21 18:16:13 +0100162 int _asrt_run_count{};
163 int _asrt_prepare_count{};
Viet-Hoa Doa3e57c22023-03-13 16:20:04 +0000164#endif // ARM_COMPUTE_ASSERTS_ENABLED
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100165};
166} // namespace cpu
167} // namespace arm_compute
168#endif /* ARM_COMPUTE_CPU_FULLY_CONNECTED_H */