| 1 |
/* |
| 2 |
* Copyright (c) 2018-2021, 2023-2024 Arm Limited. |
| 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 ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NECONVOLUTIONLAYER_H |
| 25 |
#define ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NECONVOLUTIONLAYER_H |
| 26 |
|
| 27 |
#include "arm_compute/core/ITensorInfo.h" |
| 28 |
#include "arm_compute/core/Types.h" |
| 29 |
#include "arm_compute/function_info/ActivationLayerInfo.h" |
| 30 |
#include "arm_compute/runtime/IFunction.h" |
| 31 |
#include "arm_compute/runtime/MemoryGroup.h" |
| 32 |
#include "arm_compute/runtime/MemoryManagerOnDemand.h" |
| 33 |
|
| 34 |
#include <memory> |
| 35 |
|
| 36 |
namespace arm_compute |
| 37 |
{ |
| 38 |
// Forward declarations |
| 39 |
class ITensor; |
| 40 |
|
| 41 |
/** Basic function to simulate a convolution layer. This function calls one of the following functions: |
| 42 |
* -# cpu::CpuGemmConv2d (executed only in case GEMM is required for the operation) |
| 43 |
* -# cpu::CpuWinogradConv2d (executed only in case Winograd is required for the operation) |
| 44 |
* -# cpu::CpuDirectConv2d (executed only in case Direct Convolution is required for the operation) |
| 45 |
* -# @ref NEFFTConvolutionLayer (executed only in case FFT is required for the operation) |
| 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 |
* |
| 72 |
*/ |
| 73 |
class NEConvolutionLayer : public IFunction |
| 74 |
{ |
| 75 |
public: |
| 76 |
/** Constructor */ |
| 77 |
NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager); |
| 78 |
NEConvolutionLayer() : NEConvolutionLayer(MemoryManagerOnDemand::make_default()) |
| 79 |
{ |
| 80 |
} |
| 81 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 82 |
NEConvolutionLayer(const NEConvolutionLayer &) = delete; |
| 83 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 84 |
NEConvolutionLayer &operator=(const NEConvolutionLayer &) = delete; |
| 85 |
/** Default move constructor */ |
| 86 |
NEConvolutionLayer(NEConvolutionLayer &&) = default; |
| 87 |
/** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 88 |
NEConvolutionLayer &operator=(NEConvolutionLayer &&) = default; |
| 89 |
/** Default destructor */ |
| 90 |
~NEConvolutionLayer(); |
| 91 |
/** Set the input and output tensors. |
| 92 |
* |
| 93 |
* Valid data layouts: |
| 94 |
* - NHWC |
| 95 |
* - NCHW |
| 96 |
* |
| 97 |
* Valid data type configurations: |
| 98 |
* |src0 |src1 |src2 |dst | |
| 99 |
* |:--------------|:------------------|:------|:--------------| |
| 100 |
* |F16 |F16 |F16 |F16 | |
| 101 |
* |F32 |F32 |F32 |F32 | |
| 102 |
* |QASYMM8 |QASYMM8 |S32 |QASYMM8 | |
| 103 |
* |QASYMM8 |QASYMM8_SIGNED |S32 |QASYMM8 | |
| 104 |
* |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 | |
| 105 |
* |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED | |
| 106 |
* |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED | |
| 107 |
* |
| 108 |
* @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 109 |
* while every optional dimension from 4 and above represent a batch of inputs. |
| 110 |
* Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
| 111 |
* @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 112 |
* Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL or QASYMM8_SIGNED if input is QASYMM8/QASYMM8_SIGNED. |
| 113 |
* @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 114 |
* Data type supported: Same as @p input, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type. |
| 115 |
* @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 116 |
* Data types supported: Same as @p input. |
| 117 |
* @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 118 |
* @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 |
| 119 |
* tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input. |
| 120 |
* @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 121 |
* @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported. |
| 122 |
* @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 123 |
* available which may introduce a drop of accuracy as well. Default is false |
| 124 |
* @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported |
| 125 |
*/ |
| 126 |
void configure(ITensor *input, |
| 127 |
const ITensor *weights, |
| 128 |
const ITensor *biases, |
| 129 |
ITensor *output, |
| 130 |
const PadStrideInfo &conv_info, |
| 131 |
const WeightsInfo &weights_info = WeightsInfo(), |
| 132 |
const Size2D &dilation = Size2D(1U, 1U), |
| 133 |
const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 134 |
bool enable_fast_math = false, |
| 135 |
unsigned int num_groups = 1); |
| 136 |
/** Static function to check if given info will lead to a valid configuration of @ref NEConvolutionLayer |
| 137 |
* |
| 138 |
* @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 139 |
* while every optional dimension from 4 and above represent a batch of inputs. |
| 140 |
* Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
| 141 |
* @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 142 |
* Data type supported:Same as @p input, also could be QSYMM8_PER_CHANNEL or QASYMM8_SIGNED if input is QASYMM8/QASYMM8_SIGNED. |
| 143 |
* @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 144 |
* Data type supported: Same as @p input, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type. |
| 145 |
* @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 146 |
* Data types supported: Same as @p input. |
| 147 |
* @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 148 |
* @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 |
| 149 |
* tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input. |
| 150 |
* @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 151 |
* @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 152 |
* @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 153 |
* available which may introduce a drop of accuracy as well. Default is false |
| 154 |
* @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported |
| 155 |
* |
| 156 |
* @return a status |
| 157 |
*/ |
| 158 |
static Status validate(const ITensorInfo *input, |
| 159 |
const ITensorInfo *weights, |
| 160 |
const ITensorInfo *biases, |
| 161 |
const ITensorInfo *output, |
| 162 |
const PadStrideInfo &conv_info, |
| 163 |
const WeightsInfo &weights_info = WeightsInfo(), |
| 164 |
const Size2D &dilation = Size2D(1U, 1U), |
| 165 |
const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 166 |
bool enable_fast_math = false, |
| 167 |
unsigned int num_groups = 1); |
| 168 |
/** Static function to check if given info will return the convolution called by @ref NEConvolutionLayer |
| 169 |
* |
| 170 |
* @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 171 |
* while every optional dimension from 4 and above represent a batch of inputs. |
| 172 |
* Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
| 173 |
* @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
| 174 |
* Data type supported:Same as @p input, also could be QSYMM8_PER_CHANNEL or QASYMM8_SIGNED if input is QASYMM8/QASYMM8_SIGNED. |
| 175 |
* @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 176 |
* Data types supported: Same as @p input. |
| 177 |
* @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 178 |
* @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 |
| 179 |
* tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input. |
| 180 |
* @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 181 |
* @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 182 |
* @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 183 |
* available which may introduce a drop of accuracy as well. Default is false |
| 184 |
* |
| 185 |
* @return the Convolution Method Hint |
| 186 |
*/ |
| 187 |
static ConvolutionMethod get_convolution_method(const ITensorInfo *input, |
| 188 |
const ITensorInfo *weights, |
| 189 |
const ITensorInfo *output, |
| 190 |
const PadStrideInfo &conv_info, |
| 191 |
const WeightsInfo &weights_info = WeightsInfo(), |
| 192 |
const Size2D &dilation = Size2D(1U, 1U), |
| 193 |
const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 194 |
bool enable_fast_math = false); |
| 195 |
// Inherited methods overridden: |
| 196 |
void run() override; |
| 197 |
void prepare() override; |
| 198 |
|
| 199 |
private: |
| 200 |
struct Impl; |
| 201 |
std::unique_ptr<Impl> _impl; |
| 202 |
}; |
| 203 |
} // namespace arm_compute |
| 204 |
#endif // ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NECONVOLUTIONLAYER_H |