blob: 4b8c5504429ed45d270b23603b03b1b531160ea1 [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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLCONVOLUTIONLAYER_H
25#define ARM_COMPUTE_CLCONVOLUTIONLAYER_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Matthew Benthamf1aeab92023-05-30 13:35:34 +000027#include "arm_compute/core/ActivationLayerInfo.h"
Sheri Zhang06d1efd2021-07-28 11:20:04 +010028#include "arm_compute/core/CL/CLCompileContext.h"
29#include "arm_compute/core/Types.h"
SiCongLi579ca842021-10-18 09:38:33 +010030#include "arm_compute/core/experimental/IPostOp.h"
Georgios Pinitas19884632021-08-16 12:38:54 +010031#include "arm_compute/runtime/CL/CLTensor.h"
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000032#include "arm_compute/runtime/IFunction.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010033#include "arm_compute/runtime/IMemoryManager.h"
34
35#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37namespace arm_compute
38{
Sheri Zhang06d1efd2021-07-28 11:20:04 +010039class CLCompileContext;
40class ICLTensor;
41class ITensorInfo;
42
Gian Marco20d78482018-01-11 15:10:58 +000043/** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 *
Georgios Pinitas19884632021-08-16 12:38:54 +010045 * -# @ref opencl::ClGemmConv2d
Sheri Zhang06d1efd2021-07-28 11:20:04 +010046 * -# @ref opencl::ClWinogradConv2d
47 * -# @ref opencl::ClDirectConv2d
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +010048 * -# @ref CLFFTConvolutionLayer
Pablo Tello8f309ab2019-07-19 17:46:12 +010049 *
50 * The function selects one of the algorithms mentioned above based on:
51 * - The size of the kernel
52 * - Number of input/output feature maps
53 * - Amount of memory needed
54 *
55 * Generally GEMM-based convolution is executed when neither Winograd nor FFT nor Direct convolution can be performed.
56 *
57 * FP32 Algorithm| Filter Size | Input/Output feature maps |
58 * --------------|-------------------------------------------------------------|-------------------------------------------|
59 * Winograd | 3x3 1x3 3x1 5x1 1x5 5x5(fast maths) 7x1 1x7 | Input channels is greater than 3 |
60 * FFT | Squared kernels and greater than 9x9 | Input feature maps > Output feature maps |
61 * DirectConv | 9x9 | |
62 * GEMM | Any size | |
63 *
64 * Winograd 5x5 requires fast maths enabled.
65 *
66 * FP16 Algorithm| Filter Size | Input/Output feature maps |
67 * --------------|----------------------------|-------------------------------------------|
68 * Winograd | 3x3 1x3 3x1 5x1 1x5 5x5 | Input channels is greater than 3 |
69 * FFT | Not supported | |
70 * DirectConv | 9x9 | |
71 * GEMM | Any size | |
72 *
73 * Winograd FP16 requires fast maths enabled.
74 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 */
76class CLConvolutionLayer : public IFunction
77{
78public:
79 /** Default constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010080 CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010081 /** Default Destructor */
82 ~CLConvolutionLayer();
83 /** Prevent instances of this class from being copied (As this class contains pointers) */
84 CLConvolutionLayer(const CLConvolutionLayer &) = delete;
85 /** Default move constructor */
86 CLConvolutionLayer(CLConvolutionLayer &&) = default;
87 /** Prevent instances of this class from being copied (As this class contains pointers) */
88 CLConvolutionLayer &operator=(const CLConvolutionLayer &) = delete;
89 /** Default move assignment operator */
90 CLConvolutionLayer &operator=(CLConvolutionLayer &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 /** Set the input and output tensors.
92 *
Teresa Charlin62687422021-04-28 10:58:49 +010093 * Valid data layouts:
94 * - NHWC
95 * - NCHW
96 *
97 * Valid data type configurations:
98 * |src0 |src1 |src2 |dst |
99 * |:--------------|:------------------|:------|:--------------|
100 * |F16 |F16 |F16 |F16 |
101 * |F32 |F32 |F32 |F32 |
102 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
103 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
104 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
105 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
106 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100107 * @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.
Sang-Hoon Park70d33bd2020-01-08 16:29:15 +0000109 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000110 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100111 * 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 +0100112 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100113 * 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 +0100114 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
115 * Data types supported: Same as @p input.
116 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
117 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. Data type supported: Same as @p input.
118 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
119 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
120 * @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 +0100121 * available which may introduce a drop of accuracy as well. Default is false
122 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
SiCongLi579ca842021-10-18 09:38:33 +0100123 * @param[in] post_ops (Optional) A sequence of post operations that are performed after the main operation.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 */
Alex Gilday7da29b62018-03-23 14:16:00 +0000125 void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
SiCongLi579ca842021-10-18 09:38:33 +0100126 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, unsigned int num_groups = 1,
127 const experimental::PostOpList<ICLTensor *> &post_ops = experimental::PostOpList<ICLTensor *> {});
Manuel Bottini2b84be52020-04-08 10:15:51 +0100128 /** Set the input and output tensors.
129 *
130 * @param[in] compile_context The compile context to be used.
131 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
132 * while every optional dimension from 4 and above represent a batch of inputs.
133 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
134 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100135 * 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 +0100136 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100137 * 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 +0100138 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
139 * Data types supported: Same as @p input.
140 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
141 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. Data type supported: Same as @p input.
142 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
143 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
144 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
145 * available which may introduce a drop of accuracy as well. Default is false
146 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
SiCongLi579ca842021-10-18 09:38:33 +0100147 * @param[in] post_ops (Optional) A sequence of post operations that are performed after the main operation.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100148 */
149 void configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
150 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false,
SiCongLi579ca842021-10-18 09:38:33 +0100151 unsigned int num_groups = 1, const experimental::PostOpList<ICLTensor *> &post_ops = experimental::PostOpList<ICLTensor *> {});
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000152 /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayer
153 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100154 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
155 * while every optional dimension from 4 and above represent a batch of inputs.
Sang-Hoon Park70d33bd2020-01-08 16:29:15 +0000156 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000157 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100158 * Data type supported: Same as @p input, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED.
159 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
160 * 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 +0100161 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
162 * Data types supported: Same as @p input.
163 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100164 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100165 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
166 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
167 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
168 * available which may introduce a drop of accuracy as well. Default is false
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100169 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
SiCongLi579ca842021-10-18 09:38:33 +0100170 * @param[in] post_ops (Optional) A sequence of post operations that are performed after the main operation.
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000171 *
172 * @return a status
173 */
174 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 +0100175 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false,
SiCongLi579ca842021-10-18 09:38:33 +0100176 unsigned int num_groups = 1, const experimental::PostOpList<ITensorInfo *> &post_ops = experimental::PostOpList<ITensorInfo *> {});
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000177 /** Static function to check if given info will return the convolution called by @ref CLConvolutionLayer
178 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100179 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
180 * while every optional dimension from 4 and above represent a batch of inputs.
Sang-Hoon Park70d33bd2020-01-08 16:29:15 +0000181 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000182 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Teresa Charlin62687422021-04-28 10:58:49 +0100183 * 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 +0100184 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
185 * Data types supported: Same as @p input.
186 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100187 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100188 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
189 * @param[in] gpu_target Specifies the @p GPUTarget.
190 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
191 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
192 * available which may introduce a drop of accuracy as well. Default is false
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000193 *
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100194 * @return the Convolution Method Hint
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000195 */
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100196 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 +0100197 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 +0100198 // Inherited methods overridden:
199 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100200 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201
202private:
Sheri Zhang06d1efd2021-07-28 11:20:04 +0100203 struct Impl;
204 std::unique_ptr<Impl> _impl;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205};
206}
Michalis Spyrouf4643372019-11-29 16:17:13 +0000207#endif /* ARM_COMPUTE_CLCONVOLUTIONLAYER_H */