1 /* 2 * Copyright (c) 2018-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_CLWINOGRADCONVOLUTIONLAYER_H 25 #define ARM_COMPUTE_CLWINOGRADCONVOLUTIONLAYER_H 26 27 #include "arm_compute/core/Types.h" 28 #include "arm_compute/runtime/CL/functions/CLGEMM.h" 29 #include "arm_compute/runtime/CL/functions/CLWinogradInputTransform.h" 30 #include "arm_compute/runtime/IFunction.h" 31 32 namespace arm_compute 33 { 34 class CLCompileContext; 35 class CLWinogradFilterTransformKernel; 36 class CLWinogradOutputTransformKernel; 37 class ICLTensor; 38 class ITensorInfo; 39 40 /** Basic function to execute Winograd-based convolution on OpenCL. This function calls the following OpenCL functions/kernels: 41 * 42 * -# @ref CLWinogradInputTransform 43 * -# @ref CLWinogradFilterTransformKernel (only once) 44 * -# @ref CLGEMM 45 * -# @ref CLWinogradOutputTransformKernel 46 * 47 */ 48 class CLWinogradConvolutionLayer : public IFunction 49 { 50 public: 51 /** Default constructor */ 52 CLWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); 53 /** Prevent instances of this class from being copied (As this class contains pointers) */ 54 CLWinogradConvolutionLayer(const CLWinogradConvolutionLayer &) = delete; 55 /** Default move constructor */ 56 CLWinogradConvolutionLayer(CLWinogradConvolutionLayer &&) = default; 57 /** Prevent instances of this class from being copied (As this class contains pointers) */ 58 CLWinogradConvolutionLayer &operator=(const CLWinogradConvolutionLayer &) = delete; 59 /** Default move assignment operator */ 60 CLWinogradConvolutionLayer &operator=(CLWinogradConvolutionLayer &&) = default; 61 /** Default destructor */ 62 ~CLWinogradConvolutionLayer(); 63 /** Set the input and output tensors. 64 * 65 * @note: This function only works with 3x3,3x1,1x3,5x5,5x1,1x5,7x1 and 1x7 kernels along with unit strides for both NCHW and NHWC data layout 66 * @note Some Winograd configurations (i.e. F(4x4, 5x5)) are supported only with enable_fast_math = true 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 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input 73 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 74 * Data types supported: Same as @p input. 75 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. 76 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. 77 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation 78 * available which may introduce a drop of accuracy as well. Default is false 79 */ 80 void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, 81 const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false); 82 /** Set the input and output tensors. 83 * 84 * @note: This function only works with 3x3,3x1,1x3,5x5,5x1,1x5,7x1 and 1x7 kernels along with unit strides for both NCHW and NHWC data layout 85 * @note Some Winograd configurations (i.e. F(4x4, 5x5)) are supported only with enable_fast_math = true 86 * 87 * @param[in] compile_context The compile context to be used. 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: F16/F32. 91 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. 92 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input 93 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 94 * Data types supported: Same as @p input. 95 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. 96 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. 97 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation 98 * available which may introduce a drop of accuracy as well. Default is false 99 */ 100 void configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, 101 const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false); 102 /** Static function to check if given info will lead to a valid configuration of @ref CLWinogradConvolutionLayer 103 * 104 * @note: This function only works with 3x3,3x1,1x3,5x5,5x1 and 1x5 kernels along with unit strides for both NCHW and NHWC data layout 105 * @note Some Winograd configurations (i.e. F(4x4, 5x5)) are supported only with enable_fast_math = true 106 * 107 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], 108 * while every optional dimension from 4 and above represent a batch of inputs. 109 * Data types supported: F16/F32. 110 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. 111 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input 112 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 113 * Data types supported: Same as @p input. 114 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. 115 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. 116 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation 117 * available which may introduce a drop of accuracy as well. Default is false 118 * 119 * @return a status 120 */ 121 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, 122 const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false); 123 124 // Inherited methods overridden: 125 void run() override; 126 void prepare() override; 127 128 private: 129 MemoryGroup _memory_group; 130 CLGEMM _batched_mm; 131 CLWinogradInputTransform _input_transform; 132 std::unique_ptr<CLWinogradFilterTransformKernel> _filter_transform; 133 std::unique_ptr<CLWinogradOutputTransformKernel> _output_transform; 134 CLTensor _input0; 135 CLTensor _input1; 136 CLTensor _batched_mm_output; 137 const ICLTensor *_original_weights; 138 bool _is_prepared; 139 }; 140 } // namespace arm_compute 141 #endif /* ARM_COMPUTE_CLWINOGRADCONVOLUTIONLAYER_H */ 142