1 /* 2 * Copyright (c) 2017-2020 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_NEWINOGRADCONVOLUTIONLAYER_H 25 #define ARM_COMPUTE_NEWINOGRADCONVOLUTIONLAYER_H 26 27 #include "arm_compute/runtime/IFunction.h" 28 29 #include "arm_compute/core/Types.h" 30 #include "arm_compute/runtime/CPP/functions/CPPPermute.h" 31 #include "arm_compute/runtime/MemoryGroup.h" 32 #include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" 33 #include "arm_compute/runtime/NEON/functions/NEGEMM.h" 34 35 #include "arm_compute/runtime/Tensor.h" 36 37 #include <memory> 38 39 namespace arm_compute 40 { 41 // Forward declarations 42 class ITensor; 43 class ICPPKernel; 44 45 /** Basic function to simulate a convolution layer. This function calls the following NEON kernels: 46 * -# @ref NEWinogradLayerTransformWeightsKernel (executed only once in the first call to the run() method ) 47 * -# @ref NEWinogradLayerTransformInputKernel 48 * -# @ref NEWinogradLayerTransformOutputKernel 49 * -# @ref NEGEMMAssemblyDispatch 50 * -# @ref CPPPermute (three times: weights, input and output) 51 * 52 * @note Some Winograd configurations (i.e. F(2x2, 5x5), F(4x4, 5x5)) are supported only with enable_fast_math = true 53 */ 54 class NEWinogradConvolutionLayer : public IFunction 55 { 56 public: 57 /** Constructor */ 58 NEWinogradConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager = nullptr); 59 /** Prevent instances of this class from being moved (As this class contains non movable objects) */ 60 NEWinogradConvolutionLayer(NEWinogradConvolutionLayer &&) = delete; 61 /** Prevent instances of this class from being moved (As this class contains non movable objects) */ 62 NEWinogradConvolutionLayer &operator=(NEWinogradConvolutionLayer &&) = delete; 63 /** Default destructor */ 64 ~NEWinogradConvolutionLayer() = default; 65 66 /** Set the input and output tensors. 67 * 68 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], 69 * while every optional dimension from 4 and above represent a batch of inputs. 70 * Data types supported: F16/F32. 71 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. 72 * Currently only 3x3 and 5x5 kernels are supported. 73 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. 74 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 75 * Data types supported: Same as @p input. 76 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. Currently only unit strides are supported. 77 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. 78 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation 79 * available which may introduce a drop of accuracy as well. Default is false 80 */ 81 void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info = ActivationLayerInfo(), 82 bool enable_fast_math = false); 83 84 // Inherited methods overridden: 85 void run() override; 86 void prepare() override; 87 88 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer 89 * 90 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], 91 * while every optional dimension from 4 and above represent a batch of inputs. 92 * Data types supported: F16/F32. 93 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. 94 * Currently only 3x3 and 5x5 kernels are supported. 95 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights. 96 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 97 * Data types supported: Same as @p input. 98 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. Currently only unit strides are supported. 99 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. 100 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation 101 * available which may introduce a drop of accuracy as well. Default is false 102 * 103 * @return a status 104 */ 105 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, 106 const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false); 107 108 /** Prevent instances of this class from being copied (As this class contains pointers) */ 109 NEWinogradConvolutionLayer(const NEWinogradConvolutionLayer &) = delete; 110 /** Prevent instances of this class from being copied (As this class contains pointers) */ 111 NEWinogradConvolutionLayer &operator=(const NEWinogradConvolutionLayer &) = delete; 112 113 private: 114 MemoryGroup _memory_group; 115 NEGEMM _gemm_function; 116 std::unique_ptr<ICPPKernel> _transform_input_kernel; 117 std::unique_ptr<ICPPKernel> _transform_output_kernel; 118 std::unique_ptr<ICPPKernel> _transform_weights_kernel; 119 NEActivationLayer _activationlayer_function; 120 121 CPPPermute _permute_input; 122 CPPPermute _permute_weights; 123 CPPPermute _permute_output; 124 Tensor _input_transformed; 125 Tensor _output_transformed; 126 Tensor _input_workspace; 127 Tensor _output_workspace; 128 Tensor _kernel_storage; 129 Tensor _input_nhwc; 130 Tensor _output_nhwc; 131 Tensor _weights_hwio; 132 const ITensor *_input; 133 const ITensor *_weights; 134 ITensor *_output; 135 bool _is_prepared; 136 bool _is_activationlayer_enabled; 137 }; 138 } // namespace arm_compute 139 #endif /* ARM_COMPUTE_NEWINOGRADCONVOLUTIONLAYER_H */ 140