blob: 8487be71c37675ef97ea8607ccdaeb7841b92311 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2017-2021, 2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Sujak0d27b2e2023-08-24 14:01:20 +010024#ifndef ACL_ARM_COMPUTE_RUNTIME_CL_FUNCTIONS_CLCONVOLUTIONLAYER_H
25#define ACL_ARM_COMPUTE_RUNTIME_CL_FUNCTIONS_CLCONVOLUTIONLAYER_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Sheri Zhang06d1efd2021-07-28 11:20:04 +010027#include "arm_compute/core/CL/CLCompileContext.h"
28#include "arm_compute/core/Types.h"
SiCong Li91295492023-07-21 18:16:13 +010029#include "arm_compute/function_info/ActivationLayerInfo.h"
Georgios Pinitas19884632021-08-16 12:38:54 +010030#include "arm_compute/runtime/CL/CLTensor.h"
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000031#include "arm_compute/runtime/IFunction.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010032#include "arm_compute/runtime/IMemoryManager.h"
33
34#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36namespace arm_compute
37{
Sheri Zhang06d1efd2021-07-28 11:20:04 +010038class CLCompileContext;
39class ICLTensor;
40class ITensorInfo;
41
Gian Marco20d78482018-01-11 15:10:58 +000042/** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 *
Georgios Pinitas19884632021-08-16 12:38:54 +010044 * -# @ref opencl::ClGemmConv2d
Sheri Zhang06d1efd2021-07-28 11:20:04 +010045 * -# @ref opencl::ClWinogradConv2d
46 * -# @ref opencl::ClDirectConv2d
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +010047 * -# @ref CLFFTConvolutionLayer
Pablo Tello8f309ab2019-07-19 17:46:12 +010048 *
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 Barbier6ff3b192017-09-04 18:44:23 +010074 */
75class CLConvolutionLayer : public IFunction
76{
77public:
78 /** Default constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010079 CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010080 /** 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 Barbier6ff3b192017-09-04 18:44:23 +010090 /** Set the input and output tensors.
91 *
Teresa Charlin62687422021-04-28 10:58:49 +010092 * 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 Iodice2213d4b2018-04-27 10:39:06 +0100106 * @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 Park70d33bd2020-01-08 16:29:15 +0000108 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000109 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100110 * Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100111 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100112 * Data type supported: Same as @p input, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100113 * @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 Iodice916d1bc2018-08-13 11:20:41 +0100120 * 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 Barbier6ff3b192017-09-04 18:44:23 +0100122 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 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 Bottini2b84be52020-04-08 10:15:51 +0100133 /** 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 Charlin62687422021-04-28 10:58:49 +0100140 * Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100141 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100142 * Data type supported: Same as @p input, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100143 * @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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153 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 Gottardif07d28d2018-02-06 14:52:43 +0000164 /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayer
165 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100166 * @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 Park70d33bd2020-01-08 16:29:15 +0000168 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000169 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100170 * 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 Iodice2213d4b2018-04-27 10:39:06 +0100173 * @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 Giorgio70ba7d62018-06-06 17:03:36 +0100176 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100177 * @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 Iodice916d1bc2018-08-13 11:20:41 +0100181 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000182 *
183 * @return a status
184 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100185 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 Gottardif07d28d2018-02-06 14:52:43 +0000195 /** Static function to check if given info will return the convolution called by @ref CLConvolutionLayer
196 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100197 * @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 Park70d33bd2020-01-08 16:29:15 +0000199 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000200 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100201 * Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100202 * @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 Giorgio70ba7d62018-06-06 17:03:36 +0100205 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100206 * @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 Gottardif07d28d2018-02-06 14:52:43 +0000211 *
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100212 * @return the Convolution Method Hint
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000213 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100214 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 Barbier6ff3b192017-09-04 18:44:23 +0100223 // Inherited methods overridden:
224 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100225 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226
227private:
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100228 struct Impl;
229 std::unique_ptr<Impl> _impl;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100230};
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100231} // namespace arm_compute
232#endif // ACL_ARM_COMPUTE_RUNTIME_CL_FUNCTIONS_CLCONVOLUTIONLAYER_H