Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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 | */ |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 24 | #ifndef ACL_ARM_COMPUTE_RUNTIME_CL_FUNCTIONS_CLCONVOLUTIONLAYER_H |
| 25 | #define ACL_ARM_COMPUTE_RUNTIME_CL_FUNCTIONS_CLCONVOLUTIONLAYER_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLCompileContext.h" |
| 28 | #include "arm_compute/core/Types.h" |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 29 | #include "arm_compute/function_info/ActivationLayerInfo.h" |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/CLTensor.h" |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 31 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/IMemoryManager.h" |
| 33 | |
| 34 | #include <memory> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | |
| 36 | namespace arm_compute |
| 37 | { |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 38 | class CLCompileContext; |
| 39 | class ICLTensor; |
| 40 | class ITensorInfo; |
| 41 | |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 42 | /** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | * |
Georgios Pinitas | 1988463 | 2021-08-16 12:38:54 +0100 | [diff] [blame] | 44 | * -# @ref opencl::ClGemmConv2d |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 45 | * -# @ref opencl::ClWinogradConv2d |
| 46 | * -# @ref opencl::ClDirectConv2d |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 47 | * -# @ref CLFFTConvolutionLayer |
Pablo Tello | 8f309ab | 2019-07-19 17:46:12 +0100 | [diff] [blame] | 48 | * |
| 49 | * The function selects one of the algorithms mentioned above based on: |
| 50 | * - The size of the kernel |
| 51 | * - Number of input/output feature maps |
| 52 | * - Amount of memory needed |
| 53 | * |
| 54 | * Generally GEMM-based convolution is executed when neither Winograd nor FFT nor Direct convolution can be performed. |
| 55 | * |
| 56 | * FP32 Algorithm| Filter Size | Input/Output feature maps | |
| 57 | * --------------|-------------------------------------------------------------|-------------------------------------------| |
| 58 | * Winograd | 3x3 1x3 3x1 5x1 1x5 5x5(fast maths) 7x1 1x7 | Input channels is greater than 3 | |
| 59 | * FFT | Squared kernels and greater than 9x9 | Input feature maps > Output feature maps | |
| 60 | * DirectConv | 9x9 | | |
| 61 | * GEMM | Any size | | |
| 62 | * |
| 63 | * Winograd 5x5 requires fast maths enabled. |
| 64 | * |
| 65 | * FP16 Algorithm| Filter Size | Input/Output feature maps | |
| 66 | * --------------|----------------------------|-------------------------------------------| |
| 67 | * Winograd | 3x3 1x3 3x1 5x1 1x5 5x5 | Input channels is greater than 3 | |
| 68 | * FFT | Not supported | | |
| 69 | * DirectConv | 9x9 | | |
| 70 | * GEMM | Any size | | |
| 71 | * |
| 72 | * Winograd FP16 requires fast maths enabled. |
| 73 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | */ |
| 75 | class CLConvolutionLayer : public IFunction |
| 76 | { |
| 77 | public: |
| 78 | /** Default constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 79 | CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 80 | /** Default Destructor */ |
| 81 | ~CLConvolutionLayer(); |
| 82 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 83 | CLConvolutionLayer(const CLConvolutionLayer &) = delete; |
| 84 | /** Default move constructor */ |
| 85 | CLConvolutionLayer(CLConvolutionLayer &&) = default; |
| 86 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 87 | CLConvolutionLayer &operator=(const CLConvolutionLayer &) = delete; |
| 88 | /** Default move assignment operator */ |
| 89 | CLConvolutionLayer &operator=(CLConvolutionLayer &&) = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | /** Set the input and output tensors. |
| 91 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 92 | * Valid data layouts: |
| 93 | * - NHWC |
| 94 | * - NCHW |
| 95 | * |
| 96 | * Valid data type configurations: |
| 97 | * |src0 |src1 |src2 |dst | |
| 98 | * |:--------------|:------------------|:------|:--------------| |
| 99 | * |F16 |F16 |F16 |F16 | |
| 100 | * |F32 |F32 |F32 |F32 | |
| 101 | * |QASYMM8 |QASYMM8 |S32 |QASYMM8 | |
| 102 | * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 | |
| 103 | * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED | |
| 104 | * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED | |
| 105 | * |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 106 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 107 | * while every optional dimension from 4 and above represent a batch of inputs. |
Sang-Hoon Park | 70d33bd | 2020-01-08 16:29:15 +0000 | [diff] [blame] | 108 | * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 109 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 110 | * Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 111 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 112 | * Data type supported: Same as @p input, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 113 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 114 | * Data types supported: Same as @p input. |
| 115 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 116 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. Data type supported: Same as @p input. |
| 117 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 118 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 119 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 120 | * available which may introduce a drop of accuracy as well. Default is false |
| 121 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 123 | void configure(ICLTensor *input, |
| 124 | const ICLTensor *weights, |
| 125 | const ICLTensor *biases, |
| 126 | ICLTensor *output, |
| 127 | const PadStrideInfo &conv_info, |
| 128 | const WeightsInfo &weights_info = WeightsInfo(), |
| 129 | const Size2D &dilation = Size2D(1U, 1U), |
| 130 | const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 131 | bool enable_fast_math = false, |
| 132 | unsigned int num_groups = 1); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 133 | /** Set the input and output tensors. |
| 134 | * |
| 135 | * @param[in] compile_context The compile context to be used. |
| 136 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 137 | * while every optional dimension from 4 and above represent a batch of inputs. |
| 138 | * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
| 139 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 140 | * Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED. |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 141 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 142 | * Data type supported: Same as @p input, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type. |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 143 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 144 | * Data types supported: Same as @p input. |
| 145 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 146 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. Data type supported: Same as @p input. |
| 147 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 148 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 149 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 150 | * available which may introduce a drop of accuracy as well. Default is false |
| 151 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout |
| 152 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 153 | void configure(const CLCompileContext &compile_context, |
| 154 | ICLTensor *input, |
| 155 | const ICLTensor *weights, |
| 156 | const ICLTensor *biases, |
| 157 | ICLTensor *output, |
| 158 | const PadStrideInfo &conv_info, |
| 159 | const WeightsInfo &weights_info = WeightsInfo(), |
| 160 | const Size2D &dilation = Size2D(1U, 1U), |
| 161 | const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 162 | bool enable_fast_math = false, |
| 163 | unsigned int num_groups = 1); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 164 | /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayer |
| 165 | * |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 166 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 167 | * while every optional dimension from 4 and above represent a batch of inputs. |
Sang-Hoon Park | 70d33bd | 2020-01-08 16:29:15 +0000 | [diff] [blame] | 168 | * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 169 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 170 | * Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED. |
| 171 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 172 | * Data type supported: Same as @p input, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 173 | * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 174 | * Data types supported: Same as @p input. |
| 175 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
Michele Di Giorgio | 70ba7d6 | 2018-06-06 17:03:36 +0100 | [diff] [blame] | 176 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 177 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 178 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 179 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 180 | * available which may introduce a drop of accuracy as well. Default is false |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 181 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 182 | * |
| 183 | * @return a status |
| 184 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 185 | static Status validate(const ITensorInfo *input, |
| 186 | const ITensorInfo *weights, |
| 187 | const ITensorInfo *biases, |
| 188 | const ITensorInfo *output, |
| 189 | const PadStrideInfo &conv_info, |
| 190 | const WeightsInfo &weights_info = WeightsInfo(), |
| 191 | const Size2D &dilation = Size2D(1U, 1U), |
| 192 | const ActivationLayerInfo &act_info = ActivationLayerInfo(), |
| 193 | bool enable_fast_math = false, |
| 194 | unsigned int num_groups = 1); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 195 | /** Static function to check if given info will return the convolution called by @ref CLConvolutionLayer |
| 196 | * |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 197 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 198 | * while every optional dimension from 4 and above represent a batch of inputs. |
Sang-Hoon Park | 70d33bd | 2020-01-08 16:29:15 +0000 | [diff] [blame] | 199 | * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 200 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 201 | * Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 202 | * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 203 | * Data types supported: Same as @p input. |
| 204 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
Michele Di Giorgio | 70ba7d6 | 2018-06-06 17:03:36 +0100 | [diff] [blame] | 205 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 206 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 207 | * @param[in] gpu_target Specifies the @p GPUTarget. |
| 208 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 209 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 210 | * available which may introduce a drop of accuracy as well. Default is false |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 211 | * |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 212 | * @return the Convolution Method Hint |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 213 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 214 | static ConvolutionMethod get_convolution_method(const ITensorInfo *input, |
| 215 | const ITensorInfo *weights, |
| 216 | const ITensorInfo *output, |
| 217 | const PadStrideInfo &conv_info, |
| 218 | const WeightsInfo &weights_info, |
| 219 | const ActivationLayerInfo &act_info, |
| 220 | const GPUTarget gpu_target, |
| 221 | const Size2D &dilation = Size2D(1U, 1U), |
| 222 | bool enable_fast_math = false); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 223 | // Inherited methods overridden: |
| 224 | void run() override; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 225 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 226 | |
| 227 | private: |
Sheri Zhang | 06d1efd | 2021-07-28 11:20:04 +0100 | [diff] [blame] | 228 | struct Impl; |
| 229 | std::unique_ptr<Impl> _impl; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 230 | }; |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 231 | } // namespace arm_compute |
| 232 | #endif // ACL_ARM_COMPUTE_RUNTIME_CL_FUNCTIONS_CLCONVOLUTIONLAYER_H |