blob: 77bf48d6136bfa0f9789657810eabb4bc89f2cfe [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 */
Alex Gilday7da29b62018-03-23 14:16:00 +0000123 void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100124 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, unsigned int num_groups = 1);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100125 /** Set the input and output tensors.
126 *
127 * @param[in] compile_context The compile context to be used.
128 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
129 * while every optional dimension from 4 and above represent a batch of inputs.
130 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
131 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100132 * 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 +0100133 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100134 * 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 +0100135 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
136 * Data types supported: Same as @p input.
137 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
138 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. Data type supported: Same as @p input.
139 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
140 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
141 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
142 * available which may introduce a drop of accuracy as well. Default is false
143 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
144 */
145 void configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
146 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false,
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100147 unsigned int num_groups = 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000148 /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayer
149 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100150 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
151 * while every optional dimension from 4 and above represent a batch of inputs.
Sang-Hoon Park70d33bd2020-01-08 16:29:15 +0000152 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000153 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100154 * Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED.
155 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
156 * 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 +0100157 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
158 * Data types supported: Same as @p input.
159 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100160 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100161 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
162 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
163 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
164 * available which may introduce a drop of accuracy as well. Default is false
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100165 * @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 +0000166 *
167 * @return a status
168 */
169 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100170 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false,
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100171 unsigned int num_groups = 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000172 /** Static function to check if given info will return the convolution called by @ref CLConvolutionLayer
173 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100174 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
175 * while every optional dimension from 4 and above represent a batch of inputs.
Sang-Hoon Park70d33bd2020-01-08 16:29:15 +0000176 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000177 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100178 * 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 +0100179 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
180 * Data types supported: Same as @p input.
181 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100182 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100183 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
184 * @param[in] gpu_target Specifies the @p GPUTarget.
185 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
186 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
187 * available which may introduce a drop of accuracy as well. Default is false
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000188 *
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100189 * @return the Convolution Method Hint
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000190 */
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100191 static ConvolutionMethod get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info,
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100192 const WeightsInfo &weights_info, const ActivationLayerInfo &act_info, const GPUTarget gpu_target, const Size2D &dilation = Size2D(1U, 1U), bool enable_fast_math = false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193 // Inherited methods overridden:
194 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100195 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196
197private:
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100198 struct Impl;
199 std::unique_ptr<Impl> _impl;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200};
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100201} // namespace arm_compute
202#endif // ACL_ARM_COMPUTE_RUNTIME_CL_FUNCTIONS_CLCONVOLUTIONLAYER_H