| 1 |
/* |
| 2 |
* Copyright (c) 2019-2021, 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_NEFFTCONVOLUTIONLAYER_H |
| 25 |
#define ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEFFTCONVOLUTIONLAYER_H |
| 26 |
|
| 27 |
#include "arm_compute/core/Types.h" |
| 28 |
#include "arm_compute/runtime/IFunction.h" |
| 29 |
#include "arm_compute/runtime/MemoryManagerOnDemand.h" |
| 30 |
#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" |
| 31 |
#include "arm_compute/runtime/NEON/functions/NEArithmeticAddition.h" |
| 32 |
#include "arm_compute/runtime/NEON/functions/NEFFT2D.h" |
| 33 |
#include "arm_compute/runtime/NEON/functions/NEPadLayer.h" |
| 34 |
#include "arm_compute/runtime/NEON/functions/NEPermute.h" |
| 35 |
#include "arm_compute/runtime/NEON/functions/NEPixelWiseMultiplication.h" |
| 36 |
#include "arm_compute/runtime/NEON/functions/NEReductionOperation.h" |
| 37 |
#include "arm_compute/runtime/NEON/functions/NEReshapeLayer.h" |
| 38 |
#include "arm_compute/runtime/NEON/functions/NEReverse.h" |
| 39 |
#include "arm_compute/runtime/NEON/functions/NESlice.h" |
| 40 |
|
| 41 |
namespace arm_compute |
| 42 |
{ |
| 43 |
// Forward declarations |
| 44 |
class ITensor; |
| 45 |
|
| 46 |
/** Basic function to execute FFT-based convolution on CPU. This function calls the following functions/kernels: |
| 47 |
* |
| 48 |
* -# @ref NEPermute Permute input if NHWC(only NCHW is supported). |
| 49 |
* -# @ref NEPadLayer Pad input. |
| 50 |
* -# @ref NEFFT2D Forward transform to the frequency domain. |
| 51 |
* -# @ref NEComplexPixelWiseMultiplication Complex element-wise product of input and the weights. |
| 52 |
* -# @ref NEReductionOperation Reduction across channels. |
| 53 |
* -# @ref NEFFT2D Inverse transform back to the time domain. |
| 54 |
* -# @ref NEStridedSlice Extract valid output. |
| 55 |
* -# @ref NEArithmeticAddition Add bias. |
| 56 |
* -# @ref NEActivationLayer Perform activation. |
| 57 |
* -# @ref NEPermute Permute output if NHWC(only NCHW is supported). |
| 58 |
*/ |
| 59 |
class NEFFTConvolutionLayer : public IFunction |
| 60 |
{ |
| 61 |
public: |
| 62 |
/** Default constructor */ |
| 63 |
NEFFTConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager); |
| 64 |
NEFFTConvolutionLayer() : NEFFTConvolutionLayer(MemoryManagerOnDemand::make_default()) |
| 65 |
{ |
| 66 |
} |
| 67 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 68 |
NEFFTConvolutionLayer(const NEFFTConvolutionLayer &) = delete; |
| 69 |
/** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 70 |
NEFFTConvolutionLayer(NEFFTConvolutionLayer &&) = delete; |
| 71 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 72 |
NEFFTConvolutionLayer &operator=(const NEFFTConvolutionLayer &) = delete; |
| 73 |
/** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 74 |
NEFFTConvolutionLayer &operator=(NEFFTConvolutionLayer &&) = delete; |
| 75 |
/** Default destructor */ |
| 76 |
~NEFFTConvolutionLayer(); |
| 77 |
/** Set the input and output tensors. |
| 78 |
* |
| 79 |
* Valid data layouts: |
| 80 |
* - All |
| 81 |
* |
| 82 |
* Valid data type configurations: |
| 83 |
* |src |dst | |
| 84 |
* |:------|:------| |
| 85 |
* |F32 |F32 | |
| 86 |
* |
| 87 |
* @note: This function only works with any square kernel size and unit strides for both NCHW and NHWC data layout |
| 88 |
* |
| 89 |
* @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 90 |
* while every optional dimension from 4 and above represent a batch of inputs. |
| 91 |
* Data types supported: F32. |
| 92 |
* @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 93 |
* @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input |
| 94 |
* @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 95 |
* Data types supported: Same as @p input. |
| 96 |
* @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 97 |
* @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 98 |
* @param[in] enable_fast_math (Optional) Enable fast math computation. Unused for CPU backend. |
| 99 |
*/ |
| 100 |
void configure(ITensor *input, |
| 101 |
const ITensor *weights, |
| 102 |
const ITensor *biases, |
| 103 |
ITensor *output, |
| 104 |
const PadStrideInfo &conv_info, |
| 105 |
const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 106 |
bool enable_fast_math = false); |
| 107 |
/** Static function to check if given info will lead to a valid configuration of @ref NEFFTConvolutionLayer |
| 108 |
* |
| 109 |
* @note: This function only works with any square kernel size and unit strides for both NCHW and NHWC data layout |
| 110 |
* |
| 111 |
* @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 112 |
* while every optional dimension from 4 and above represent a batch of inputs. |
| 113 |
* Data types supported: F32. |
| 114 |
* @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 115 |
* @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input |
| 116 |
* @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 117 |
* Data types supported: Same as @p input. |
| 118 |
* @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 119 |
* @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 120 |
* @param[in] enable_fast_math (Optional) Enable fast math computation. Unused for CPU backend. |
| 121 |
* |
| 122 |
* @return a status |
| 123 |
*/ |
| 124 |
static Status validate(const ITensorInfo *input, |
| 125 |
const ITensorInfo *weights, |
| 126 |
const ITensorInfo *biases, |
| 127 |
const ITensorInfo *output, |
| 128 |
const PadStrideInfo &conv_info, |
| 129 |
const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 130 |
bool enable_fast_math = false); |
| 131 |
|
| 132 |
// Inherited methods overridden: |
| 133 |
void run() override; |
| 134 |
void prepare() override; |
| 135 |
|
| 136 |
private: |
| 137 |
MemoryGroup _memory_group; |
| 138 |
NEReverse _flip_weights_func; |
| 139 |
NEPermute _permute_input_func; |
| 140 |
NEPermute _permute_output_func; |
| 141 |
NEPermute _permute_weights_func; |
| 142 |
NEPermute _permute_bias_func; |
| 143 |
NEPadLayer _pad_input_func; |
| 144 |
NEPadLayer _pad_weights_func; |
| 145 |
NEFFT2D _transform_input_func; |
| 146 |
std::unique_ptr<NEFFT2D> _transform_weights_func; |
| 147 |
NEFFT2D _itransform_output_func; |
| 148 |
NEComplexPixelWiseMultiplication _prod_func; |
| 149 |
NEReductionOperation _reduce_func; |
| 150 |
NESlice _extract_output_func; |
| 151 |
NEArithmeticAddition _bias_add_func; |
| 152 |
NEActivationLayer _activation_layer_func; |
| 153 |
|
| 154 |
Tensor _permuted_input; |
| 155 |
Tensor _permuted_weights; |
| 156 |
Tensor _permuted_bias; |
| 157 |
Tensor _permuted_output; |
| 158 |
Tensor _padded_input; |
| 159 |
Tensor _padded_weights; |
| 160 |
Tensor _flip_axis; |
| 161 |
Tensor _flipped_weights; |
| 162 |
Tensor _transformed_input; |
| 163 |
Tensor _transformed_weights; |
| 164 |
Tensor _input_weights_product; |
| 165 |
Tensor _output_product; |
| 166 |
Tensor _output_reduced; |
| 167 |
Tensor _itransformed_output; |
| 168 |
Tensor _reshaped_output; |
| 169 |
Tensor _bias_output; |
| 170 |
|
| 171 |
const ITensor *_original_weights; |
| 172 |
const ITensor *_original_bias; |
| 173 |
bool _is_activationlayer_enabled; |
| 174 |
bool _needs_permute; |
| 175 |
bool _has_bias; |
| 176 |
bool _is_prepared; |
| 177 |
}; |
| 178 |
} // namespace arm_compute |
| 179 |
#endif // ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEFFTCONVOLUTIONLAYER_H |