Compute Library
 21.02
NEFFTConvolutionLayer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021 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 ARM_COMPUTE_NEFFTCONVOLUTIONLAYER_H
25 #define ARM_COMPUTE_NEFFTCONVOLUTIONLAYER_H
26 
28 
29 #include "arm_compute/core/Types.h"
40 
41 namespace arm_compute
42 {
43 // Forward declarations
44 class ITensor;
45 
46 /** Basic function to execute FFT-based convolution on Neon. This function calls the following Neon 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  */
60 {
61 public:
62  /** Default constructor */
63  NEFFTConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
64  /** Prevent instances of this class from being copied (As this class contains pointers) */
66  /** Prevent instances of this class from being moved (As this class contains non movable objects) */
68  /** Prevent instances of this class from being copied (As this class contains pointers) */
70  /** Prevent instances of this class from being moved (As this class contains non movable objects) */
72  /** Default destructor */
74  /** Set the input and output tensors.
75  *
76  * @note: This function only works with any square kernel size and unit strides for both NCHW and NHWC data layout
77  *
78  * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
79  * while every optional dimension from 4 and above represent a batch of inputs.
80  * Data types supported: F32.
81  * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input.
82  * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input
83  * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
84  * Data types supported: Same as @p input.
85  * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
86  * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
87  * @param[in] enable_fast_math (Optional) Enable fast math computation. Unused for Neon backend.
88  */
89  void configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
90  const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false);
91  /** Static function to check if given info will lead to a valid configuration of @ref NEFFTConvolutionLayer
92  *
93  * @note: This function only works with any square kernel size and unit strides for both NCHW and NHWC data layout
94  *
95  * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
96  * while every optional dimension from 4 and above represent a batch of inputs.
97  * Data types supported: F32.
98  * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input.
99  * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input
100  * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
101  * Data types supported: Same as @p input.
102  * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
103  * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
104  * @param[in] enable_fast_math (Optional) Enable fast math computation. Unused for Neon backend.
105  *
106  * @return a status
107  */
108  static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
109  const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false);
110 
111  // Inherited methods overridden:
112  void run() override;
113  void prepare() override;
114 
115 private:
116  MemoryGroup _memory_group;
117  NEReverse _flip_weights_func;
118  NEPermute _permute_input_func;
119  NEPermute _permute_output_func;
120  NEPermute _permute_weights_func;
121  NEPermute _permute_bias_func;
122  NEPadLayer _pad_input_func;
123  NEPadLayer _pad_weights_func;
124  NEFFT2D _transform_input_func;
125  std::unique_ptr<NEFFT2D> _transform_weights_func;
126  NEFFT2D _itransform_output_func;
128  NEReductionOperation _reduce_func;
129  NESlice _extract_output_func;
130  NEArithmeticAddition _bias_add_func;
131  NEActivationLayer _activation_layer_func;
132 
133  Tensor _permuted_input;
134  Tensor _permuted_weights;
135  Tensor _permuted_bias;
136  Tensor _permuted_output;
137  Tensor _padded_input;
138  Tensor _padded_weights;
139  Tensor _flip_axis;
140  Tensor _flipped_weights;
141  Tensor _transformed_input;
142  Tensor _transformed_weights;
143  Tensor _input_weights_product;
144  Tensor _output_product;
145  Tensor _output_reduced;
146  Tensor _itransformed_output;
147  Tensor _reshaped_output;
148  Tensor _bias_output;
149 
150  const ITensor *_original_weights;
151  const ITensor *_original_bias;
152  bool _is_activationlayer_enabled;
153  bool _needs_permute;
154  bool _has_bias;
155  bool _is_prepared;
156 };
157 } // namespace arm_compute
158 #endif /* ARM_COMPUTE_NEFFTCONVOLUTIONLAYER_H */
void prepare() override
Prepare the function for executing.
Base class for all functions.
Definition: IFunction.h:30
Basic function to run cpu::kernels::CpuAddKernel.
Basic function to run NEComplexPixelWiseMultiplicationKernel.
Basic function to perform tensor slicing.
Definition: NESlice.h:74
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info=ActivationLayerInfo(), bool enable_fast_math=false)
Static function to check if given info will lead to a valid configuration of NEFFTConvolutionLayer.
Status class.
Definition: Error.h:52
Basic function to run cpu::kernels::CpuPermuteKernel.
Definition: NEPermute.h:40
Activation Layer Information class.
Definition: Types.h:1550
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
NEFFTConvolutionLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Default constructor.
Basic function to execute two dimensional FFT.
Definition: NEFFT2D.h:44
Basic implementation of the tensor interface.
Definition: Tensor.h:37
Padding and stride information class.
Definition: Types.h:722
Basic function to run cpu::kernels::CpuActivationKernel.
void run() override
Run the kernels contained in the function.
NEFFTConvolutionLayer & operator=(const NEFFTConvolutionLayer &)=delete
Prevent instances of this class from being copied (As this class contains pointers) ...
Basic function to pad a tensor.
Definition: NEPadLayer.h:51
void configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info=ActivationLayerInfo(), bool enable_fast_math=false)
Set the input and output tensors.
Basic function to simulate a reduction operation.
Basic function to run NEReverseKernel.
Definition: NEReverse.h:37
~NEFFTConvolutionLayer()
Default destructor.
Basic function to execute FFT-based convolution on Neon.