Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H |
| 25 | #define ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/runtime/IFunction.h" |
| 28 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/MemoryGroup.h" |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/NEON/functions/NEConvertFullyConnectedWeights.h" |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/NEON/functions/NEFlattenLayer.h" |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/NEON/functions/NEGEMM.h" |
| 33 | #include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h" |
Teresa Charlin | 28fcc35 | 2021-04-07 20:39:49 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/NEON/functions/NETranspose.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | #include "arm_compute/runtime/Tensor.h" |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 39 | namespace weights_transformations |
| 40 | { |
Teresa Charlin | 28fcc35 | 2021-04-07 20:39:49 +0100 | [diff] [blame] | 41 | /** Basic function to manage the reshape weights generated from @ref NETranspose */ |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 42 | class NEFullyConnectedLayerReshapeWeightsManaged : public ITransformWeights |
| 43 | { |
| 44 | public: |
| 45 | void run() override |
| 46 | { |
| 47 | _output.allocator()->allocate(); |
| 48 | _func.run(); |
| 49 | _reshape_run = true; |
| 50 | } |
| 51 | |
| 52 | void release() override |
| 53 | { |
| 54 | _output.allocator()->free(); |
| 55 | } |
| 56 | |
| 57 | ITensor *get_weights() override |
| 58 | { |
| 59 | return &_output; |
| 60 | } |
| 61 | |
| 62 | uint32_t uid() override |
| 63 | { |
| 64 | return _uid; |
| 65 | } |
| 66 | |
| 67 | void configure(const ITensor *input) |
| 68 | { |
| 69 | _func.configure(input, &_output); |
| 70 | } |
| 71 | |
| 72 | private: |
Teresa Charlin | 28fcc35 | 2021-04-07 20:39:49 +0100 | [diff] [blame] | 73 | static constexpr uint32_t _uid = 0x0; |
| 74 | Tensor _output{}; |
| 75 | NETranspose _func{}; |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 76 | }; |
| 77 | } // namespace weights_transformations |
| 78 | |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 79 | /** Basic function to compute a Fully Connected layer. This function calls the following kernels: |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 80 | * -# @ref NEIm2ColKernel (called when the input comes from a convolutional layer) |
Teresa Charlin | 28fcc35 | 2021-04-07 20:39:49 +0100 | [diff] [blame] | 81 | * -# @ref NETranspose (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once) |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 82 | * -# @ref NEGEMMMatrixMultiplyKernel or @ref NEGEMMLowpMatrixMultiplyCore (if quantized asymmetric) |
SiCong Li | adb3291 | 2020-02-17 16:39:27 +0000 | [diff] [blame] | 83 | * -# @ref NEGEMMMatrixAdditionKernel or @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if quantized asymmetric) (if @p biases is not equal to nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | * |
| 85 | * @note The fully connected layer accepts "weights" tensors only with 2 dimensions. |
| 86 | */ |
| 87 | class NEFullyConnectedLayer : public IFunction |
| 88 | { |
| 89 | public: |
| 90 | /** Constructor */ |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 91 | NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 92 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 93 | NEFullyConnectedLayer(const NEFullyConnectedLayer &) = delete; |
Michalis Spyrou | 770dfeb | 2020-11-04 18:55:34 +0000 | [diff] [blame] | 94 | /** Prevent instances of this class from being moved (As this class contains pointers) */ |
| 95 | NEFullyConnectedLayer(NEFullyConnectedLayer &&) = delete; |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 96 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 97 | NEFullyConnectedLayer &operator=(const NEFullyConnectedLayer &) = delete; |
Michalis Spyrou | 770dfeb | 2020-11-04 18:55:34 +0000 | [diff] [blame] | 98 | /** Prevent instances of this class from being moved (As this class contains pointers) */ |
| 99 | NEFullyConnectedLayer &operator=(NEFullyConnectedLayer &&) = delete; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 100 | /** Default destructor */ |
| 101 | ~NEFullyConnectedLayer(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | /** Set the input and output tensors. |
| 103 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame^] | 104 | * Valid data layouts: |
| 105 | * - NHWC |
| 106 | * - NCHW |
| 107 | * |
| 108 | * Valid data type configurations: |
| 109 | * |src0 |src1 |src2 |dst | |
| 110 | * |:--------------|:------------------|:------|:--------------| |
| 111 | * |F16 |F16 |F16 |F16 | |
| 112 | * |F32 |F32 |F32 |F32 | |
| 113 | * |QASYMM8 |QASYMM8 |S32 |QASYMM8 | |
| 114 | * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED | |
| 115 | * |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 116 | * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 117 | * @param[in] weights Weights tensor. The weights must be 2 dimensional. |
| 118 | * 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. |
| 119 | * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. |
| 120 | * Data type supported: Same as @p input. |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 121 | * @param[in] biases Bias tensor. Can be nullptr. Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED. |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 122 | * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between: |
| 123 | * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer |
| 124 | * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. |
| 125 | * Data type supported: Same as @p input. |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 126 | * @param[in] fc_info (Optional) Fully connected layer additional info |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 127 | */ |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 128 | void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, |
| 129 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 130 | /** Static function to check if given info will lead to a valid configuration of @ref NEFullyConnectedLayer |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 131 | * |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 132 | * @param[in] input Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 133 | * @param[in] weights Weights tensor info. The weights must be 2 dimensional. |
| 134 | * 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. |
| 135 | * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. |
| 136 | * Data type supported: Same as @p input. |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 137 | * @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 Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 138 | * @param[in] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between: |
| 139 | * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer |
| 140 | * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. |
| 141 | * Data type supported: Same as @p input. |
| 142 | * @param[in] fc_info (Optional) Fully connected layer additional info |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 143 | * |
| 144 | * @return a status |
| 145 | */ |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 146 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 147 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | |
| 149 | //Inherited methods override |
| 150 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 151 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | |
| 153 | private: |
SiCongLi | 2e5fd63 | 2020-03-02 15:39:15 +0000 | [diff] [blame] | 154 | void configure_fc_fc(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act); |
| 155 | void configure_conv_fc(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act); |
| 156 | void configure_mm(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 157 | |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 158 | MemoryGroup _memory_group; |
| 159 | IWeightsManager *_weights_manager; |
Georgios Pinitas | e2696b1 | 2020-12-03 20:37:43 +0000 | [diff] [blame] | 160 | NEFlattenLayer _flatten; |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 161 | NEConvertFullyConnectedWeights _convert_weights; |
| 162 | weights_transformations::NEConvertFullyConnectedWeightsManaged _convert_weights_managed; |
Teresa Charlin | 28fcc35 | 2021-04-07 20:39:49 +0100 | [diff] [blame] | 163 | NETranspose _reshape_weights_function; |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 164 | weights_transformations::NEFullyConnectedLayerReshapeWeightsManaged _reshape_weights_managed_function; |
| 165 | NEGEMM _mm_gemm; |
| 166 | NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp; |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 167 | Tensor _flatten_output; |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 168 | Tensor _converted_weights_output; |
| 169 | Tensor _reshape_weights_output; |
| 170 | const ITensor *_original_weights; |
| 171 | bool _are_weights_converted; |
| 172 | bool _are_weights_reshaped; |
| 173 | bool _is_fc_after_conv; |
SiCongLi | 2e5fd63 | 2020-03-02 15:39:15 +0000 | [diff] [blame] | 174 | bool _is_quantized_asymmetric; |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 175 | bool _is_prepared; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | }; |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 177 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 178 | #endif /* ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H */ |