xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/runtime/CL/functions/CLGEMMConvolutionLayer.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2021 Arm Limited.
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  */
24 #ifndef ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H
25 #define ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H
26 
27 #include "arm_compute/core/experimental/IPostOp.h"
28 #include "arm_compute/runtime/CL/CLTensor.h"
29 #include "arm_compute/runtime/CL/CLTypes.h"
30 #include "arm_compute/runtime/IFunction.h"
31 #include "arm_compute/runtime/IMemoryManager.h"
32 #include "arm_compute/runtime/IWeightsManager.h"
33 
34 #include <memory>
35 
36 namespace arm_compute
37 {
38 // Forward declarations
39 class CLCompileContext;
40 class ICLTensor;
41 class ITensorInfo;
42 
43 /** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions:
44  *
45  * -# @ref opencl::ClGemmConv2d
46  */
47 class CLGEMMConvolutionLayer : public IFunction
48 {
49 public:
50     /** Constructor
51      *
52      * @param[in] memory_manager  (Optional) Memory manager.
53      * @param[in] weights_manager (Optional) Weights manager.
54      */
55     CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
56     /** Prevent instances of this class from being copied (As this class contains pointers) */
57     CLGEMMConvolutionLayer(const CLGEMMConvolutionLayer &) = delete;
58     /** Default move constructor */
59     CLGEMMConvolutionLayer(CLGEMMConvolutionLayer &&) = default;
60     /** Prevent instances of this class from being copied (As this class contains pointers) */
61     CLGEMMConvolutionLayer &operator=(const CLGEMMConvolutionLayer &) = delete;
62     /** Default move assignment operator */
63     CLGEMMConvolutionLayer &operator=(CLGEMMConvolutionLayer &&) = default;
64     /**Default destructor */
65     ~CLGEMMConvolutionLayer();
66     /** Set the input and output tensors.
67      *
68      * Valid data layouts:
69      * - NHWC
70      * - NCHW
71      *
72      * Valid data type configurations:
73      * |src0           |src1               |src2     |dst            |
74      * |:--------------|:------------------|:--------|:--------------|
75      * |F16            |F16                |F16      |F16            |
76      * |F32            |F32                |F32      |F32            |
77      * |QASYMM8        |QASYMM8            |S32      |QASYMM8        |
78      * |QASYMM8        |QSYMM8_PER_CHANNEL |S32      |QASYMM8        |
79      * |QASYMM8_SIGNED |QASYMM8_SIGNED     |S32      |QASYMM8_SIGNED |
80      * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32      |QASYMM8_SIGNED |
81      *
82      * @param[in]  input        Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
83      *                          while every optional dimension from 4 and above represent a batch of inputs.
84      *                          Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
85      * @param[in]  weights      Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
86      *                          Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED.
87      * @param[in]  biases       Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
88      *                          Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
89      * @param[out] output       Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
90      *                          Data types supported: Same as @p input.
91      * @param[in]  conv_info    Contains padding and stride information described in @ref PadStrideInfo.
92      * @param[in]  weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. If this is not part of the fully connected layer the weights
93      *                          tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input.
94      * @param[in]  dilation     (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
95      * @param[in]  act_info     (Optional) Activation layer information in case of a fused activation.
96      * @param[in]  num_groups   (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
97      * @param[in]  post_ops     (Optional) A sequence of post operations that are performed after the main operation.
98      */
99     void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
100                    const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1,
101                    const experimental::PostOpList<ICLTensor *> &post_ops = experimental::PostOpList<ICLTensor *> {});
102     /** Set the input and output tensors.
103      *
104      * @param[in]  compile_context The compile context to be used.
105      * @param[in]  input           Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
106      *                             while every optional dimension from 4 and above represent a batch of inputs.
107      *                             Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
108      * @param[in]  weights         Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
109      *                             Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED.
110      * @param[in]  biases          Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
111      *                             Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
112      * @param[out] output          Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
113      *                             Data types supported: Same as @p input.
114      * @param[in]  conv_info       Contains padding and stride information described in @ref PadStrideInfo.
115      * @param[in]  weights_info    Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. If this is not part of the fully connected layer the weights
116      *                             tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. 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]  num_groups      (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
120      * @param[in]  post_ops        (Optional) A sequence of post operations that are performed after the main operation.
121      */
122     void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
123                    const WeightsInfo &weights_info = WeightsInfo(),
124                    const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1,
125                    const experimental::PostOpList<ICLTensor *> &post_ops = experimental::PostOpList<ICLTensor *> {});
126     /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer.
127      *
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].
132      *                          Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED.
133      * @param[in]  biases       Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
134      *                          Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
135      * @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. If this is not part of the fully connected layer the weights
139      *                          tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input.
140      * @param[in]  dilation     (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
141      * @param[in]  act_info     (Optional) Activation layer information in case of a fused activation.
142      * @param[in]  num_groups   (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
143      * @param[in]  post_ops     (Optional) A sequence of post operations that are performed after the main operation.
144      *
145      * @return a status
146      */
147     static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
148                            const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1,
149                            const experimental::PostOpList<ITensorInfo *> &post_ops = experimental::PostOpList<ITensorInfo *> {});
150 
151     // Inherited methods overridden:
152     void run() override;
153     void prepare() override;
154 
155 private:
156     struct Impl;
157     std::unique_ptr<Impl> _impl;
158 };
159 } // namespace arm_compute
160 #endif /* ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H */
161