Michele Di Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 1 | /* |
Jonathan Deakin | 464ed20 | 2023-01-12 11:41:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2023 Arm Limited. |
Michele Di Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 3 | * |
| 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 | |
Michele Di Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/TensorInfo.h" |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 28 | #include "arm_compute/function_info/FullyConnectedLayerInfo.h" |
Michele Di Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 29 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 30 | #include "src/cpu/ICpuOperator.h" |
| 31 | |
Michele Di Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 32 | #include <memory> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace cpu |
| 37 | { |
| 38 | // Forward declarations |
| 39 | class CpuConvertFullyConnectedWeights; |
| 40 | class CpuFlatten; |
| 41 | class CpuGemm; |
| 42 | class CpuGemmLowpMatrixMultiplyCore; |
| 43 | namespace kernels |
| 44 | { |
| 45 | class 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 | */ |
| 55 | class CpuFullyConnected : public ICpuOperator |
| 56 | { |
| 57 | public: |
| 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 Puzovic | 13b623e | 2022-07-27 17:53:21 +0000 | [diff] [blame] | 76 | * @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 Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 88 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 89 | 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 Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 95 | /** Static function to check if given info will lead to a valid configuration of @ref CpuFullyConnected |
| 96 | * |
Jonathan Deakin | 464ed20 | 2023-01-12 11:41:14 +0000 | [diff] [blame] | 97 | * Similar to @ref CpuFullyConnected::configure() |
Michele Di Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 98 | * |
| 99 | * @return a status |
| 100 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 101 | 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 Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 107 | |
Milos Puzovic | 13b623e | 2022-07-27 17:53:21 +0000 | [diff] [blame] | 108 | /** 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 114 | 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 Puzovic | 13b623e | 2022-07-27 17:53:21 +0000 | [diff] [blame] | 121 | |
Michele Di Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 122 | //Inherited methods override |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 123 | void run(ITensorPack &tensors) override; |
| 124 | void prepare(ITensorPack &tensors) override; |
Michele Di Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 125 | experimental::MemoryRequirements workspace() const override; |
| 126 | |
| 127 | private: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 128 | 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 Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 143 | |
| 144 | enum AuxTensorIdx |
| 145 | { |
| 146 | AsmGemmWorkspace = 0, |
| 147 | Pretranspose, |
| 148 | GemmTemp1, // Both CpuGemm and CpuGemmLowpMatrixMultiplyCore |
| 149 | GemmTemp2, // Both CpuGemm and CpuGemmLowpMatrixMultiplyCore |
| 150 | GemmTemp3, // Both CpuGemm and CpuGemmLowpMatrixMultiplyCore |
| 151 | GemmTemp4, // CpuGemmLowpMatrixMultiplyCore only |
| 152 | GemmTemp5, // CpuGemmLowpMatrixMultiplyCore only |
| 153 | GemmTemp6, // CpuGemmLowpMatrixMultiplyCore only |
| 154 | GemmTemp7, // CpuGemmLowpMatrixMultiplyCore only |
| 155 | TransposedWeights, |
| 156 | ConvertedWeights, |
| 157 | FlattenedSrc, |
| 158 | Count |
| 159 | }; |
| 160 | |
| 161 | std::unique_ptr<CpuFlatten> _flatten; |
| 162 | std::unique_ptr<CpuConvertFullyConnectedWeights> _convert_weights; |
| 163 | std::unique_ptr<kernels::CpuTransposeKernel> _transpose_weights; |
| 164 | std::unique_ptr<CpuGemm> _mm_gemm; |
| 165 | std::unique_ptr<CpuGemmLowpMatrixMultiplyCore> _mm_gemmlowp; |
| 166 | |
Georgios Pinitas | fa1db17 | 2021-08-12 06:28:09 +0100 | [diff] [blame] | 167 | TensorInfo _flattened_src; |
| 168 | TensorInfo _converted_weights; |
| 169 | TensorInfo _reshaped_weights; |
| 170 | TensorInfo _trans_weights; |
| 171 | AuxTensorIdx _trans_weights_idx; |
Michele Di Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 172 | |
| 173 | experimental::MemoryRequirements _aux_mem; |
| 174 | |
Milos Puzovic | 13b623e | 2022-07-27 17:53:21 +0000 | [diff] [blame] | 175 | bool _needs_weights_conversion; |
| 176 | bool _needs_weights_reshape; |
| 177 | bool _is_fc_after_conv; |
| 178 | bool _is_quantized_asymmetric; |
| 179 | bool _is_prepared; |
| 180 | bool _enable_fast_math; |
| 181 | bool _fixed_format; |
| 182 | arm_compute::WeightFormat _weight_format; |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 183 | bool _dynamic_weights; |
| 184 | |
| 185 | #ifdef ARM_COMPUTE_ASSERTS_ENABLED |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 186 | int _asrt_run_count{}; |
| 187 | int _asrt_prepare_count{}; |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 188 | #endif // ARM_COMPUTE_ASSERTS_ENABLED |
Michele Di Giorgio | d9cdf14 | 2021-07-02 15:17:08 +0100 | [diff] [blame] | 189 | }; |
| 190 | } // namespace cpu |
| 191 | } // namespace arm_compute |
| 192 | #endif /* ARM_COMPUTE_CPU_FULLY_CONNECTED_H */ |