Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H__ |
| 25 | #define __ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H__ |
| 26 | |
| 27 | #include "arm_compute/runtime/IFunction.h" |
| 28 | |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/NEON/kernels/NEFlattenLayerKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/NEON/kernels/NEGEMMMatrixAccumulateBiasesKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/NEON/kernels/NETransposeKernel.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/MemoryGroup.h" |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/NEON/functions/NEConvertFullyConnectedWeights.h" |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/NEON/functions/NEGEMM.h" |
| 35 | #include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h" |
| 36 | #include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | #include "arm_compute/runtime/Tensor.h" |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | /** Basic function to reshape the weights of Fully Connected layer with NEON. This function calls the following kernels: |
| 42 | * |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 43 | * -# @ref NETransposeKernel |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | * |
| 45 | * @note The fully connected layer accepts "weights" tensors only with 2 dimensions. |
| 46 | */ |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 47 | class NEFullyConnectedLayerReshapeWeights : public INESimpleFunction |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | { |
| 49 | public: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | /** Set the input and output tensors. |
| 51 | * |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 52 | * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/F16/F32. |
| 53 | * @param[out] output Destination tensor. Data type supported: Same as @p input. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | */ |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 55 | void configure(const ITensor *input, ITensor *output); |
Giorgio Arena | 6200fa4 | 2018-07-06 17:06:36 +0100 | [diff] [blame] | 56 | /** 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] | 57 | * |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 58 | * @param[in] input Weights tensor info. The weights must be 2 dimensional. Data types supported: QASYMM8/F16/F32. |
| 59 | * @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] | 60 | * |
| 61 | * @return a status |
| 62 | */ |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 63 | static Status validate(const ITensorInfo *input, const ITensorInfo *output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | /** 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] | 67 | * -# @ref NEIm2ColKernel (called when the input comes from a convolutional layer) |
| 68 | * -# @ref NEFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once) |
| 69 | * -# @ref NEGEMMMatrixMultiplyKernel or @ref NEGEMMLowpMatrixMultiplyCore (if quantized asymmetric) |
| 70 | * -# @ref NEGEMMMatrixAccumulateBiasesKernel 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] | 71 | * |
| 72 | * @note The fully connected layer accepts "weights" tensors only with 2 dimensions. |
| 73 | */ |
| 74 | class NEFullyConnectedLayer : public IFunction |
| 75 | { |
| 76 | public: |
| 77 | /** Constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 78 | NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 79 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 80 | NEFullyConnectedLayer(const NEFullyConnectedLayer &) = delete; |
| 81 | /** Default move constructor */ |
| 82 | NEFullyConnectedLayer(NEFullyConnectedLayer &&) = default; |
| 83 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 84 | NEFullyConnectedLayer &operator=(const NEFullyConnectedLayer &) = delete; |
| 85 | /** Default move assignment operator */ |
| 86 | NEFullyConnectedLayer &operator=(NEFullyConnectedLayer &&) = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | /** Set the input and output tensors. |
| 88 | * |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 89 | * @param[in] input Source tensor. Data type supported: QASYMM8/F16/F32. |
| 90 | * @param[in] weights Weights tensor. The weights must be 2 dimensional. |
| 91 | * 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. |
| 92 | * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. |
| 93 | * Data type supported: Same as @p input. |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 94 | * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input. |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 95 | * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between: |
| 96 | * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer |
| 97 | * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. |
| 98 | * Data type supported: Same as @p input. |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 99 | * @param[in] fc_info (Optional) Fully connected layer additional info |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | */ |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 101 | void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, |
| 102 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 103 | /** 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] | 104 | * |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 105 | * @param[in] input Source tensor info. Data type supported: QASYMM8/F16/F32. |
| 106 | * @param[in] weights Weights tensor info. The weights must be 2 dimensional. |
| 107 | * 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. |
| 108 | * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. |
| 109 | * Data type supported: Same as @p input. |
| 110 | * @param[in] biases Bias tensor info. Can be nullptr. Data type supported:Same as @p input. |
| 111 | * @param[out] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between: |
| 112 | * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer |
| 113 | * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. |
| 114 | * Data type supported: Same as @p input. |
| 115 | * @param[in] fc_info (Optional) Fully connected layer additional info |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 116 | * |
| 117 | * @return a status |
| 118 | */ |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 119 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 120 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | |
| 122 | //Inherited methods override |
| 123 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 124 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | |
| 126 | private: |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 127 | void configure_fc_fc(const ITensor *input, const ITensor *weights, ITensor *output); |
| 128 | void configure_conv_fc(const ITensor *input, const ITensor *weights, ITensor *output); |
| 129 | void configure_mm(const ITensor *input, const ITensor *weights, ITensor *output); |
| 130 | |
| 131 | MemoryGroup _memory_group; |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 132 | NEFlattenLayerKernel _flatten_kernel; |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 133 | NEConvertFullyConnectedWeights _convert_weights; |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 134 | NEFullyConnectedLayerReshapeWeights _reshape_weights_function; |
| 135 | NEGEMM _mm_gemm; |
| 136 | NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp; |
| 137 | NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage; |
| 138 | NEGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel; |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 139 | Tensor _flatten_output; |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 140 | Tensor _gemmlowp_output; |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 141 | Tensor _converted_weights_output; |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 142 | Tensor _reshape_weights_output; |
| 143 | const ITensor *_original_weights; |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 144 | bool _are_weights_converted; |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 145 | bool _are_weights_reshaped; |
| 146 | bool _is_fc_after_conv; |
| 147 | bool _accumulate_biases; |
| 148 | bool _is_quantized; |
| 149 | bool _is_prepared; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | }; |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 151 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | #endif /* __ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H__ */ |