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_CLCONVOLUTIONLAYER_H 25 #define ARM_COMPUTE_CLCONVOLUTIONLAYER_H 26 27 #include "arm_compute/runtime/CL/functions/CLDirectConvolutionLayer.h" 28 #include "arm_compute/runtime/CL/functions/CLFFTConvolutionLayer.h" 29 #include "arm_compute/runtime/CL/functions/CLGEMMConvolutionLayer.h" 30 #include "arm_compute/runtime/CL/functions/CLWinogradConvolutionLayer.h" 31 #include "arm_compute/runtime/IFunction.h" 32 #include "arm_compute/runtime/IMemoryManager.h" 33 34 #include <memory> 35 36 namespace arm_compute 37 { 38 /** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions: 39 * 40 * -# @ref CLGEMMConvolutionLayer 41 * -# @ref CLWinogradConvolutionLayer 42 * -# @ref CLDirectConvolutionLayer 43 * -# @ref CLFFTConvolutionLayer 44 * 45 * The function selects one of the algorithms mentioned above based on: 46 * - The size of the kernel 47 * - Number of input/output feature maps 48 * - Amount of memory needed 49 * 50 * Generally GEMM-based convolution is executed when neither Winograd nor FFT nor Direct convolution can be performed. 51 * 52 * FP32 Algorithm| Filter Size | Input/Output feature maps | 53 * --------------|-------------------------------------------------------------|-------------------------------------------| 54 * Winograd | 3x3 1x3 3x1 5x1 1x5 5x5(fast maths) 7x1 1x7 | Input channels is greater than 3 | 55 * FFT | Squared kernels and greater than 9x9 | Input feature maps > Output feature maps | 56 * DirectConv | 9x9 | | 57 * GEMM | Any size | | 58 * 59 * Winograd 5x5 requires fast maths enabled. 60 * 61 * FP16 Algorithm| Filter Size | Input/Output feature maps | 62 * --------------|----------------------------|-------------------------------------------| 63 * Winograd | 3x3 1x3 3x1 5x1 1x5 5x5 | Input channels is greater than 3 | 64 * FFT | Not supported | | 65 * DirectConv | 9x9 | | 66 * GEMM | Any size | | 67 * 68 * Winograd FP16 requires fast maths enabled. 69 * 70 */ 71 class CLConvolutionLayer : public IFunction 72 { 73 public: 74 /** Default constructor */ 75 CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); 76 /** Default Destructor */ 77 ~CLConvolutionLayer(); 78 /** Prevent instances of this class from being copied (As this class contains pointers) */ 79 CLConvolutionLayer(const CLConvolutionLayer &) = delete; 80 /** Default move constructor */ 81 CLConvolutionLayer(CLConvolutionLayer &&) = default; 82 /** Prevent instances of this class from being copied (As this class contains pointers) */ 83 CLConvolutionLayer &operator=(const CLConvolutionLayer &) = delete; 84 /** Default move assignment operator */ 85 CLConvolutionLayer &operator=(CLConvolutionLayer &&) = default; 86 /** Set the input and output tensors. 87 * 88 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], 89 * while every optional dimension from 4 and above represent a batch of inputs. 90 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 91 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. 92 * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8. 93 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. 94 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. 95 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 96 * Data types supported: Same as @p input. 97 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. 98 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. Data type supported: Same as @p input. 99 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). 100 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. 101 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation 102 * available which may introduce a drop of accuracy as well. Default is false 103 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout 104 */ 105 void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(), 106 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, unsigned int num_groups = 1); 107 /** Set the input and output tensors. 108 * 109 * @param[in] compile_context The compile context to be used. 110 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], 111 * while every optional dimension from 4 and above represent a batch of inputs. 112 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 113 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. 114 * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8. 115 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. 116 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. 117 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 118 * Data types supported: Same as @p input. 119 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. 120 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. Data type supported: Same as @p input. 121 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). 122 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. 123 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation 124 * available which may introduce a drop of accuracy as well. Default is false 125 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout 126 */ 127 void configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, 128 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, 129 unsigned int num_groups = 1); 130 /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayer 131 * 132 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], 133 * while every optional dimension from 4 and above represent a batch of inputs. 134 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 135 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. 136 * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8. 137 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported:Same as @p input. 138 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 139 * Data types supported: Same as @p input. 140 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. 141 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. 142 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). 143 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. 144 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation 145 * available which may introduce a drop of accuracy as well. Default is false 146 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout 147 * 148 * @return a status 149 */ 150 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, 151 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, 152 unsigned int num_groups = 1); 153 /** Static function to check if given info will return the convolution called by @ref CLConvolutionLayer 154 * 155 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], 156 * while every optional dimension from 4 and above represent a batch of inputs. 157 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 158 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. 159 * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8. 160 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 161 * Data types supported: Same as @p input. 162 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. 163 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. 164 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. 165 * @param[in] gpu_target Specifies the @p GPUTarget. 166 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). 167 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation 168 * available which may introduce a drop of accuracy as well. Default is false 169 * 170 * @return a status 171 */ 172 static ConvolutionMethod get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info, 173 const WeightsInfo &weights_info, const ActivationLayerInfo &act_info, const GPUTarget gpu_target, const Size2D &dilation = Size2D(1U, 1U), bool enable_fast_math = false); 174 // Inherited methods overridden: 175 void run() override; 176 void prepare() override; 177 178 private: 179 std::shared_ptr<IMemoryManager> _memory_manager; 180 std::unique_ptr<IFunction> _function; 181 }; 182 } 183 #endif /* ARM_COMPUTE_CLCONVOLUTIONLAYER_H */ 184