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