Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 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_NECONVOLUTIONLAYER_H |
| 25 | #define ARM_COMPUTE_NECONVOLUTIONLAYER_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/runtime/IFunction.h" |
| 28 | |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/ITensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Types.h" |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 31 | #include "arm_compute/function_info/ActivationLayerInfo.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/MemoryGroup.h" |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 33 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 34 | #include <memory> |
| 35 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | namespace arm_compute |
| 37 | { |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 38 | // Forward declarations |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | class ITensor; |
| 40 | |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 41 | /** Basic function to simulate a convolution layer. This function calls one of the following functions: |
Michalis Spyrou | b55f8e8 | 2021-07-22 11:23:11 +0100 | [diff] [blame] | 42 | * -# @ref cpu::CpuGemm (executed only in case GEMM is required for the operation) |
| 43 | * -# @ref cpu::CpuWinogradConv2d (executed only in case Winograd is required for the operation) |
| 44 | * -# @ref cpu::CpuDirectConv2d (executed only in case Direct Convolution is required for the operation) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 45 | * -# @ref NEFFTConvolutionLayer (executed only in case FFT is required for the operation) |
Pablo Tello | 8f309ab | 2019-07-19 17:46:12 +0100 | [diff] [blame] | 46 | * |
| 47 | * |
| 48 | * The function selects one of the algorithms mentioned above based on: |
| 49 | * - The size of the kernel |
| 50 | * - Number of input/output feature maps |
| 51 | * - Amount of memory needed |
| 52 | * |
| 53 | * Generally GEMM-based convolution is executed when neither Winograd nor FFT nor Direct convolution can be performed. |
| 54 | * |
| 55 | * FP32 Algorithm| Filter Size | Input/Output feature maps | |
| 56 | * --------------|----------------------------------------------------|-------------------------------------------| |
| 57 | * Winograd | 3x3 1x3 3x1 5x1 1x5 5x5(fast maths) 7x1 1x7 | Input channels is greater than 3 | |
| 58 | * FFT | Squared kernels and greater than 9x9 | Input feature maps > Output feature maps | |
| 59 | * DirectConv | 9x9 | | |
| 60 | * GEMM | Any size | | |
| 61 | * |
| 62 | * Winograd 5x5 requires fast maths enabled. |
| 63 | * |
| 64 | * FP16 Algorithm| Filter Size | |
| 65 | * --------------|------------------| |
| 66 | * Winograd | Not supported | |
| 67 | * FFT | Not supported | |
| 68 | * DirectConv | 9x9 | |
| 69 | * GEMM | Any size | |
| 70 | * |
| 71 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | */ |
| 73 | class NEConvolutionLayer : public IFunction |
| 74 | { |
| 75 | public: |
| 76 | /** Constructor */ |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 77 | NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 78 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 79 | NEConvolutionLayer(const NEConvolutionLayer &) = delete; |
| 80 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 81 | NEConvolutionLayer &operator=(const NEConvolutionLayer &) = delete; |
Michalis Spyrou | b55f8e8 | 2021-07-22 11:23:11 +0100 | [diff] [blame] | 82 | /** Default move constructor */ |
| 83 | NEConvolutionLayer(NEConvolutionLayer &&) = default; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 84 | /** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
Michalis Spyrou | b55f8e8 | 2021-07-22 11:23:11 +0100 | [diff] [blame] | 85 | NEConvolutionLayer &operator=(NEConvolutionLayer &&) = default; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 86 | /** Default destructor */ |
Michalis Spyrou | b55f8e8 | 2021-07-22 11:23:11 +0100 | [diff] [blame] | 87 | ~NEConvolutionLayer(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | /** Set the input and output tensors. |
| 89 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 90 | * Valid data layouts: |
| 91 | * - NHWC |
| 92 | * - NCHW |
| 93 | * |
| 94 | * Valid data type configurations: |
| 95 | * |src0 |src1 |src2 |dst | |
| 96 | * |:--------------|:------------------|:------|:--------------| |
| 97 | * |F16 |F16 |F16 |F16 | |
| 98 | * |F32 |F32 |F32 |F32 | |
| 99 | * |QASYMM8 |QASYMM8 |S32 |QASYMM8 | |
| 100 | * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 | |
| 101 | * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED | |
| 102 | * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED | |
| 103 | * |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 104 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 105 | * while every optional dimension from 4 and above represent a batch of inputs. |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 106 | * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 107 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 108 | * Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED. |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 109 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 110 | * Data type supported: Same as @p input, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type. |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 111 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 112 | * Data types supported: Same as @p input. |
| 113 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 114 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights |
Michele Di Giorgio | 93b75e0 | 2021-06-21 12:00:43 +0100 | [diff] [blame] | 115 | * tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input. |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 116 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 117 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported. |
| 118 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 119 | * available which may introduce a drop of accuracy as well. Default is false |
| 120 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | */ |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 122 | void configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(), |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 123 | const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, unsigned int num_groups = 1); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 124 | /** Static function to check if given info will lead to a valid configuration of @ref NEConvolutionLayer |
| 125 | * |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 126 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 127 | * while every optional dimension from 4 and above represent a batch of inputs. |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 128 | * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 129 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 130 | * Data type supported:Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED. |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 131 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 132 | * Data type supported: Same as @p input, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type. |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 133 | * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 134 | * Data types supported: Same as @p input. |
| 135 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 136 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights |
Michele Di Giorgio | 93b75e0 | 2021-06-21 12:00:43 +0100 | [diff] [blame] | 137 | * tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input. |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 138 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 139 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 140 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 141 | * available which may introduce a drop of accuracy as well. Default is false |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 142 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 143 | * |
| 144 | * @return a status |
| 145 | */ |
| 146 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 147 | const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, |
| 148 | unsigned int num_groups = 1); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 149 | /** Static function to check if given info will return the convolution called by @ref NEConvolutionLayer |
| 150 | * |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 151 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 152 | * while every optional dimension from 4 and above represent a batch of inputs. |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 153 | * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 154 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 155 | * Data type supported:Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED. |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 156 | * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 157 | * Data types supported: Same as @p input. |
| 158 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 159 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights |
Michele Di Giorgio | 93b75e0 | 2021-06-21 12:00:43 +0100 | [diff] [blame] | 160 | * tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input. |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 161 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 162 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 163 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 164 | * available which may introduce a drop of accuracy as well. Default is false |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 165 | * |
| 166 | * @return the Convolution Method Hint |
| 167 | */ |
Andrew Mundy | 4d9379a | 2018-03-15 16:47:03 +0000 | [diff] [blame] | 168 | static ConvolutionMethod get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Giorgio Arena | a3221e6 | 2018-05-03 15:57:48 +0100 | [diff] [blame] | 169 | const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | // Inherited methods overridden: |
| 171 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 172 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 173 | |
| 174 | private: |
Michalis Spyrou | b55f8e8 | 2021-07-22 11:23:11 +0100 | [diff] [blame] | 175 | struct Impl; |
| 176 | std::unique_ptr<Impl> _impl; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | }; |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 178 | } // namespace arm_compute |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 179 | #endif /* ARM_COMPUTE_NECONVOLUTIONLAYER_H */ |