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