blob: e8f3147ac30188213f5b57b6543dba16515b6def [file] [log] [blame]
Manuel Bottinid87aded2021-07-16 10:23:31 +01001/*
Jakub Sujak0d27b2e2023-08-24 14:01:20 +01002 * Copyright (c) 2021, 2023 Arm Limited.
Manuel Bottinid87aded2021-07-16 10:23:31 +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_SRC_GPU_CL_OPERATORS_CLGEMMCONV2D_H
25#define ACL_SRC_GPU_CL_OPERATORS_CLGEMMCONV2D_H
Manuel Bottinid87aded2021-07-16 10:23:31 +010026
27#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/runtime/FunctionDescriptors.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030
Georgios Pinitas7891a732021-08-20 21:39:25 +010031#include "src/gpu/cl/ClCompileContext.h"
32#include "src/gpu/cl/IClOperator.h"
Manuel Bottinid87aded2021-07-16 10:23:31 +010033
34#include <memory>
35
36namespace arm_compute
37{
38namespace opencl
39{
40class ClGemm;
41class ClGemmLowpMatrixMultiplyCore;
42namespace kernels
43{
44class ClIm2ColKernel;
45class ClCol2ImKernel;
46class ClWeightsReshapeKernel;
47class ClActivationKernel;
48} // namespace kernels
49
50/** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions:
51 *
52 * -# @ref opencl::kernels::ClIm2ColKernel
53 * -# @ref ClGemm (if the data type is FP32 or FP16)
54 * -# @ref CLGEMMLowpMatrixMultiplyCore (if the data type is QASYMM8/QASYMM8_SIGNED)
55 * -# @ref ClGemmLowpOutputStage with QUANTIZE_DOWN_FIXEDPOINT type of quantization (if the data type is QASYMM8/QASYMM8_SIGNED)
56 * -# @ref opencl::kernels::ClCol2ImKernel (if NCHW data layout)
57 * -# @ref opencl::kernels::ClActivationKernel
58 */
Georgios Pinitas19884632021-08-16 12:38:54 +010059class ClGemmConv2d : public IClOperator
Manuel Bottinid87aded2021-07-16 10:23:31 +010060{
61public:
62 /** Constructor */
Georgios Pinitas19884632021-08-16 12:38:54 +010063 ClGemmConv2d();
Manuel Bottinid87aded2021-07-16 10:23:31 +010064 /** Prevent instances of this class from being copied (As this class contains pointers) */
Georgios Pinitas19884632021-08-16 12:38:54 +010065 ClGemmConv2d(const ClGemmConv2d &) = delete;
Manuel Bottinid87aded2021-07-16 10:23:31 +010066 /** Default move constructor */
Georgios Pinitas19884632021-08-16 12:38:54 +010067 ClGemmConv2d(ClGemmConv2d &&) = default;
Manuel Bottinid87aded2021-07-16 10:23:31 +010068 /** Prevent instances of this class from being copied (As this class contains pointers) */
Georgios Pinitas19884632021-08-16 12:38:54 +010069 ClGemmConv2d &operator=(const ClGemmConv2d &) = delete;
Manuel Bottinid87aded2021-07-16 10:23:31 +010070 /** Default move assignment operator */
Georgios Pinitas19884632021-08-16 12:38:54 +010071 ClGemmConv2d &operator=(ClGemmConv2d &&) = default;
Manuel Bottinid87aded2021-07-16 10:23:31 +010072 /**Default destructor */
Georgios Pinitas19884632021-08-16 12:38:54 +010073 ~ClGemmConv2d();
Manuel Bottinid87aded2021-07-16 10:23:31 +010074 /** Set the input and output tensors.
75 *
76 * Valid data layouts:
77 * - NHWC
78 * - NCHW
79 *
80 * Valid data type configurations:
81 * |src0 |src1 |src2 |dst |
82 * |:--------------|:------------------|:--------|:--------------|
83 * |F16 |F16 |F16 |F16 |
84 * |F32 |F32 |F32 |F32 |
85 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
86 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
87 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
88 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
89 *
90 * @param[in] compile_context The compile context to be used.
91 * @param[in] src Source tensor info. 3 lower dimensions represent a single input [width, height, IFM],
92 * while every optional dimension from 4 and above represent a batch of inputs.
93 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
94 * @param[in] weights Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
95 * 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.
96 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
97 * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
98 * @param[out] dst Destination tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
99 * Data types supported: Same as @p input.
100 * @param[in] conv2d_info Contains convolution 2d info described in @ref Conv2dInfo.
101 * @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
102 * tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input.
103 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100104 void configure(const ClCompileContext &compile_context,
105 ITensorInfo *src,
106 ITensorInfo *weights,
107 ITensorInfo *biases,
108 ITensorInfo *dst,
109 const Conv2dInfo &conv2d_info,
110 const WeightsInfo &weights_info = WeightsInfo());
Manuel Bottinid87aded2021-07-16 10:23:31 +0100111 /** Static function to check if given info will lead to a valid configuration
112 *
113 * Similar to ClGemmConvolution::configure()
114 *
115 * @return a status
116 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 static Status validate(const ITensorInfo *input,
118 const ITensorInfo *weights,
119 const ITensorInfo *biases,
120 const ITensorInfo *output,
121 const Conv2dInfo &conv2d_info,
Manuel Bottinid87aded2021-07-16 10:23:31 +0100122 const WeightsInfo &weights_info = WeightsInfo());
123
124 // Inherited methods overridden:
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100125 void run(ITensorPack &tensors) override;
126 void prepare(ITensorPack &constants) override;
Manuel Bottinid87aded2021-07-16 10:23:31 +0100127 experimental::MemoryRequirements workspace() const override;
128
129private:
130 /** Configures the appropriate matrix multiply routine
131 *
132 * @param[in] compile_context The compile context to be used.
133 * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
134 * @param[in] weights Weights tensor info. Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or
135 * QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED.
136 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
137 * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
138 * @param[in, out] dst Output tensor info. Data types supported: same as @p input.
139 * @param[in] gemmlowp_output_stage GEMMLowp output stage info
140 * @param[in] gemm_3d_depth Depth of GEMM 3D
141 * @param[in] act_info Activation to apply after the matrix multiplication
142 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100143 void configure_mm(const CLCompileContext &compile_context,
144 const ITensorInfo *src,
145 ITensorInfo *weights,
146 ITensorInfo *biases,
147 ITensorInfo *dst,
Manuel Bottinid87aded2021-07-16 10:23:31 +0100148 const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100149 int gemm_3d_depth,
150 const ActivationLayerInfo &act_info);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100151 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer matrix multiply routines
152 *
153 * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
154 * @param[in] weights Weights tensor info. Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or
155 * QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED.
156 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
157 * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
158 * @param[in] dst Output tensor info. Data types supported: same as @p input.
159 * @param[in] gemmlowp_output_stage GEMMLowp output stage info
160 * @param[in] gemm_3d_depth Depth of GEMM 3D
161 * @param[in] skip_im2col Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout.
162 * @param[in] act_info Activation to apply after the matrix multiplication
163 *
164 * @return a status
165 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100166 static Status validate_mm(const ITensorInfo *src,
167 const ITensorInfo *weights,
168 const ITensorInfo *biases,
169 const ITensorInfo *dst,
170 const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
171 int gemm_3d_depth,
172 bool skip_im2col,
173 const ActivationLayerInfo &act_info);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100174
175 enum AuxTensorIdx
176 {
177 // ClGemmLowpMatrixMultiplyCore has up to 7 internal tensors
178 Im2ColOutput = 8,
179 WeightsReshaped,
180 GemmOutput,
181 Count
182 };
183
184 std::unique_ptr<kernels::ClWeightsReshapeKernel> _weights_reshape_kernel;
185 std::unique_ptr<kernels::ClIm2ColKernel> _im2col_kernel;
186 std::unique_ptr<ClGemm> _mm_gemm;
187 std::unique_ptr<ClGemmLowpMatrixMultiplyCore> _mm_gemmlowp;
188 std::unique_ptr<opencl::kernels::ClCol2ImKernel> _col2im_kernel;
189 std::unique_ptr<kernels::ClActivationKernel> _activation_kernel;
190
191 TensorInfo _im2col_output;
192 TensorInfo _weights_reshaped;
193 TensorInfo _gemm_output;
194
195 bool _skip_im2col;
196 bool _skip_col2im;
197 bool _is_quantized;
198 bool _fuse_activation;
199 bool _append_bias;
200 bool _is_prepared;
201
202 experimental::MemoryRequirements _aux_mem;
203};
204} // namespace opencl
205} // namespace arm_compute
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100206#endif // ACL_SRC_GPU_CL_OPERATORS_CLGEMMCONV2D_H