Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 1 | /* |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLWINOGRADCONVOLUTIONLAYER_H |
| 25 | #define ARM_COMPUTE_CLWINOGRADCONVOLUTIONLAYER_H |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 26 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Types.h" |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 28 | #include "arm_compute/runtime/IFunction.h" |
Manuel Bottini | c6f4ec3 | 2021-05-18 18:41:56 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/IMemoryManager.h" |
| 30 | |
| 31 | #include <memory> |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 32 | |
| 33 | namespace arm_compute |
| 34 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 35 | class CLCompileContext; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 36 | class ICLTensor; |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 37 | class ITensorInfo; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 38 | |
| 39 | /** Basic function to execute Winograd-based convolution on OpenCL. This function calls the following OpenCL functions/kernels: |
| 40 | * |
Manuel Bottini | c6f4ec3 | 2021-05-18 18:41:56 +0100 | [diff] [blame] | 41 | * -# @ref opencl::ClWinogradConv2d |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 42 | * |
| 43 | */ |
| 44 | class CLWinogradConvolutionLayer : public IFunction |
| 45 | { |
| 46 | public: |
Manuel Bottini | c6f4ec3 | 2021-05-18 18:41:56 +0100 | [diff] [blame] | 47 | /** Default Constructor */ |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 48 | CLWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Manuel Bottini | c6f4ec3 | 2021-05-18 18:41:56 +0100 | [diff] [blame] | 49 | /** Default Destructor */ |
| 50 | ~CLWinogradConvolutionLayer(); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 51 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 52 | CLWinogradConvolutionLayer(const CLWinogradConvolutionLayer &) = delete; |
| 53 | /** Default move constructor */ |
| 54 | CLWinogradConvolutionLayer(CLWinogradConvolutionLayer &&) = default; |
| 55 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 56 | CLWinogradConvolutionLayer &operator=(const CLWinogradConvolutionLayer &) = delete; |
| 57 | /** Default move assignment operator */ |
| 58 | CLWinogradConvolutionLayer &operator=(CLWinogradConvolutionLayer &&) = default; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 59 | /** Set the input and output tensors. |
| 60 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 61 | * Valid data layouts: |
| 62 | * - NHWC |
| 63 | * - NCHW |
| 64 | * |
| 65 | * Valid data type configurations: |
| 66 | * |src0 |src1 |src2 |dst | |
| 67 | * |:--------------|:--------------|:------|:--------------| |
| 68 | * |F16 |F16 |F16 |F16 | |
| 69 | * |F32 |F32 |F32 |F32 | |
| 70 | * |
giuros01 | 3bfacb2 | 2019-04-01 12:07:02 +0100 | [diff] [blame] | 71 | * @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 |
Gian Marco Iodice | 876be2a | 2018-07-03 12:22:09 +0100 | [diff] [blame] | 72 | * @note Some Winograd configurations (i.e. F(4x4, 5x5)) are supported only with enable_fast_math = true |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 73 | * |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 74 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 75 | * while every optional dimension from 4 and above represent a batch of inputs. |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 76 | * Data types supported: F16/F32. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 77 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 78 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input |
| 79 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 80 | * Data types supported: Same as @p input. |
| 81 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 82 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 83 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 84 | * available which may introduce a drop of accuracy as well. Default is false |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 85 | */ |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 86 | void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 87 | const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 88 | /** Set the input and output tensors. |
| 89 | * |
| 90 | * @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 |
| 91 | * @note Some Winograd configurations (i.e. F(4x4, 5x5)) are supported only with enable_fast_math = true |
| 92 | * |
| 93 | * @param[in] compile_context The compile context to be used. |
| 94 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 95 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 96 | * Data types supported: F16/F32. |
| 97 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 98 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input |
| 99 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 100 | * Data types supported: Same as @p input. |
| 101 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 102 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 103 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 104 | * available which may introduce a drop of accuracy as well. Default is false |
| 105 | */ |
| 106 | void configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, |
| 107 | const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 108 | /** Static function to check if given info will lead to a valid configuration of @ref CLWinogradConvolutionLayer |
| 109 | * |
Gian Marco Iodice | d28b751 | 2018-07-06 12:59:28 +0100 | [diff] [blame] | 110 | * @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 |
Gian Marco Iodice | 876be2a | 2018-07-03 12:22:09 +0100 | [diff] [blame] | 111 | * @note Some Winograd configurations (i.e. F(4x4, 5x5)) are supported only with enable_fast_math = true |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 112 | * |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 113 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 114 | * while every optional dimension from 4 and above represent a batch of inputs. |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 115 | * Data types supported: F16/F32. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 116 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 117 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].Data type supported: Same as @p input |
| 118 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 119 | * Data types supported: Same as @p input. |
| 120 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 121 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 122 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 123 | * available which may introduce a drop of accuracy as well. Default is false |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 124 | * |
| 125 | * @return a status |
| 126 | */ |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 127 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 128 | const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 129 | |
| 130 | // Inherited methods overridden: |
| 131 | void run() override; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 132 | void prepare() override; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 133 | |
| 134 | private: |
Manuel Bottini | c6f4ec3 | 2021-05-18 18:41:56 +0100 | [diff] [blame] | 135 | struct Impl; |
| 136 | std::unique_ptr<Impl> _impl; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 137 | }; |
Manuel Bottini | 0d0028c | 2018-10-02 16:41:52 +0100 | [diff] [blame] | 138 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 139 | #endif /* ARM_COMPUTE_CLWINOGRADCONVOLUTIONLAYER_H */ |