blob: 1739ea61036a8eecc8b9fa54eb4757a5c2758853 [file] [log] [blame]
Manuel Bottini29599d02021-07-06 15:01:35 +01001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2021-2023 Arm Limited.
Manuel Bottini29599d02021-07-06 15:01:35 +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 */
Georgios Pinitas19884632021-08-16 12:38:54 +010024#ifndef ARM_COMPUTE_CPU_GEMM_CONV2D_H
25#define ARM_COMPUTE_CPU_GEMM_CONV2D_H
Manuel Bottini29599d02021-07-06 15:01:35 +010026
Matthew Benthamf1aeab92023-05-30 13:35:34 +000027#include "arm_compute/core/ActivationLayerInfo.h"
Manuel Bottini29599d02021-07-06 15:01:35 +010028#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/Types.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010030#include "src/cpu/ICpuOperator.h"
Manuel Bottini29599d02021-07-06 15:01:35 +010031
32#include <memory>
33
34namespace arm_compute
35{
36namespace cpu
37{
38class CpuGemm;
39class CpuGemmLowpMatrixMultiplyCore;
40class CpuGemmLowpOutputStage;
41namespace kernels
42{
43class CpuWeightsReshapeKernel;
44class CpuIm2ColKernel;
45class CpuCol2ImKernel;
46class CpuReshapeKernel;
47} // namespace kernels
48
49/** Basic function to compute the convolution layer. This function calls the following kernels/functions:
50 *
51 * -# @ref cpu::kernels::CpuIm2ColKernel
52 * -# @ref CpuGemm (if the data type is BFLOAT16/FP16/FP32)
53 * -# @ref CpuGemmLowpMatrixMultiplyCore (if the data type is QASYMM8/QASYMM8_SIGNED)
54 * -# @ref CpuGemmLowpOutputStage (if the data type is QASYMM8/QASYMM8_SIGNED)
55 * -# @ref cpu::kernels::CpuCol2ImKernel (if NCHW data layout)
56 * -# @ref kernels::CpuWeightsReshapeKernel
57 *
58 */
Georgios Pinitas19884632021-08-16 12:38:54 +010059class CpuGemmConv2d : public ICpuOperator
Manuel Bottini29599d02021-07-06 15:01:35 +010060{
61public:
62 /** Constructor */
Georgios Pinitas19884632021-08-16 12:38:54 +010063 CpuGemmConv2d();
Manuel Bottini29599d02021-07-06 15:01:35 +010064 /** Prevent instances of this class from being copied (As this class contains pointers) */
Georgios Pinitas19884632021-08-16 12:38:54 +010065 CpuGemmConv2d(const CpuGemmConv2d &) = delete;
Manuel Bottini29599d02021-07-06 15:01:35 +010066 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
Georgios Pinitas19884632021-08-16 12:38:54 +010067 CpuGemmConv2d(CpuGemmConv2d &&) = delete;
Manuel Bottini29599d02021-07-06 15:01:35 +010068 /** Prevent instances of this class from being copied (As this class contains pointers) */
Georgios Pinitas19884632021-08-16 12:38:54 +010069 CpuGemmConv2d &operator=(const CpuGemmConv2d &) = delete;
Manuel Bottini29599d02021-07-06 15:01:35 +010070 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
Georgios Pinitas19884632021-08-16 12:38:54 +010071 CpuGemmConv2d &operator=(CpuGemmConv2d &&) = delete;
Manuel Bottini29599d02021-07-06 15:01:35 +010072 /** Destructor */
Georgios Pinitas19884632021-08-16 12:38:54 +010073 ~CpuGemmConv2d();
Manuel Bottini29599d02021-07-06 15:01:35 +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 * |BFLOAT16 |BFLOAT16 |BFLOAT16 |BFLOAT16 |
86 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
87 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
88 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
89 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
90 *
Georgios Pinitas69a9ac42021-07-22 13:30:13 +010091 * @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/BFLOAT16/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: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
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 QASYMM8/QASYMM8_SIGNED 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] conv_info Contains padding and stride information described in @ref PadStrideInfo.
101 * @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights
102 * tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input.
103 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
104 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
105 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
106 * available which may introduce a drop of accuracy as well. Default is false
107 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported
Manuel Bottini29599d02021-07-06 15:01:35 +0100108 */
109 void configure(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
Georgios Pinitas69a9ac42021-07-22 13:30:13 +0100110 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, unsigned int num_groups = 1);
Manuel Bottini29599d02021-07-06 15:01:35 +0100111 /** Static function to check if given info will lead to a valid configuration
112 *
113 * Similar to CpuGemmConvolution::configure()
114 *
115 * @return a status
116 */
117 static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Georgios Pinitas69a9ac42021-07-22 13:30:13 +0100118 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(),
119 bool enable_fast_math = false, unsigned int num_groups = 1);
Manuel Bottini29599d02021-07-06 15:01:35 +0100120
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000121 /** Indicates whether or not there is an optimal assembly implementation that can be used to process the given parameters.
122 *
123 * The paramter list is the same as @ref NEGEMMConvolutionLayer::has_opt_impl
124 *
125 * @return a status.
126 */
Ramy Elgammal91780022022-07-20 14:57:37 +0100127 static Status has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000128 const PadStrideInfo &conv_info,
129 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(),
130 const bool enable_fast_math = false);
131
Manuel Bottini29599d02021-07-06 15:01:35 +0100132 // Inherited methods overridden:
Ramy Elgammal91780022022-07-20 14:57:37 +0100133 void run(ITensorPack &tensors) override;
134 void prepare(ITensorPack &tensors) override;
Manuel Bottini29599d02021-07-06 15:01:35 +0100135 experimental::MemoryRequirements workspace() const override;
136
137private:
138 /** Configures the appropriate matrix multiply routine
139 *
Georgios Pinitas69a9ac42021-07-22 13:30:13 +0100140 * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
141 * @param[in] weights Weights tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
142 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
143 * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
144 * @param[out] dst Output tensor info. Data types supported: Same as @p input,
145 * except for input of QASYMM8/QASYMM8_SIGNED type where output should be of S32 type.
146 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
147 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
148 * available which may introduce a drop of accuracy as well. Default is false
149 * @param[in] gemm_3d_depth (Optional) Depth of GEMM 3D (Defaults to 1)
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000150 * @param[in] fixed_format (Optional) Select GEMM execution with variable weights.
151 * @param[in] weight_format (Optional) The layout to be used for the weights tensor when running GEMM with variable weights.
Manuel Bottini29599d02021-07-06 15:01:35 +0100152 */
153 void configure_mm(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *output, const ActivationLayerInfo &act_info = ActivationLayerInfo(),
Ramy Elgammal91780022022-07-20 14:57:37 +0100154 bool enable_fast_math = false, int gemm_3d_depth = 1, bool fixed_format = false, arm_compute::WeightFormat weight_format = arm_compute::WeightFormat::UNSPECIFIED);
Manuel Bottini29599d02021-07-06 15:01:35 +0100155 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer matrix multiply routines
156 *
Georgios Pinitas69a9ac42021-07-22 13:30:13 +0100157 * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
158 * @param[in] weights Weights tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
159 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
160 * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
161 * @param[in] dst Output tensor info. Data types supported: Same as @p input,
162 * except for input of QASYMM8/QASYMM8_SIGNED type where output should be of S32 type.
163 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
164 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
165 * available which may introduce a drop of accuracy as well. Default is false
166 * @param[in] gemm_3d_depth (Optional) Depth of GEMM 3D (Defaults to 1)
167 * @param[in] skip_im2col (Optional) Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout. (Default to false)
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000168 * @param[in] fixed_format (Optional) Select GEMM execution with variable weights.
169 * @param[in] weight_format (Optional) The layout to be used for the weights tensor when running GEMM with variable weights.
Manuel Bottini29599d02021-07-06 15:01:35 +0100170 *
171 * @return a status
172 */
173 static Status validate_mm(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const ActivationLayerInfo &act_info = ActivationLayerInfo(),
Ramy Elgammal91780022022-07-20 14:57:37 +0100174 bool enable_fast_math = false, int gemm_3d_depth = 1, bool skip_im2col = false, bool fixed_format = false, arm_compute::WeightFormat weight_format = arm_compute::WeightFormat::UNSPECIFIED);
Manuel Bottini29599d02021-07-06 15:01:35 +0100175 /** Static function to check if GEMM3D is supported in @ref NEGEMM or in @ref CpuGemmMLowpMatrixMultiplyCore
176 *
177 * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
178 * @param[in] weights Weights tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
179 * @param[in] act_info Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
180 * @param[in] gemm_3d_depth Depth of GEMM 3D
181 * @param[in] skip_im2col Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout
182 *
183 * @return a status
184 */
185 static Status validate_gemm3d(const ITensorInfo *src, const ITensorInfo *weights, const ActivationLayerInfo &act_info, int gemm_3d_depth, bool skip_im2col);
186
Francesco.Petrogalli@arm.comfa6877f2022-04-13 09:28:25 +0000187 struct SkipInfo
188 {
189 bool skip_im2col;
190 bool skip_col2im;
191 };
192
193 /** Static function to provide skip_im2col and skip_col2im information.
194 *
195 * @param[in] src Input tensor info.
196 * @param[in] weights Weights tensor info.
197 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
198 * @param[in] dilation Dilation, in elements, across x and y.
199 * @param[in] act_info Activation layer information in case of a fused activation.
200 *
201 * @return a SkipInfo instance.
202 */
203 static SkipInfo skip_im_col_info(const ITensorInfo *src, const ITensorInfo *weights, const PadStrideInfo &conv_info,
204 const Size2D &dilation, const ActivationLayerInfo &act_info);
205
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000206 /** Indicates if the convolution executes in variable weights mode.
207 *
208 * Similar to @ref CpuGemm::isVarWeightsKernel
209 */
210 bool isVarWeightsKernel() const;
Manuel Bottini29599d02021-07-06 15:01:35 +0100211 enum AuxTensorIdx
212 {
213 // CpuGemmLowpMatrixMultiplyCore has up to 8 internal tensors
214 Im2ColOutput = 9,
215 WeightsReshaped,
216 GemmOutput,
Manuel Bottini29599d02021-07-06 15:01:35 +0100217 Count
218 };
219
220 std::unique_ptr<kernels::CpuWeightsReshapeKernel> _weights_reshape_kernel;
221 std::unique_ptr<cpu::kernels::CpuIm2ColKernel> _im2col_kernel;
222 std::unique_ptr<CpuGemm> _mm_gemm;
223 std::unique_ptr<CpuGemmLowpMatrixMultiplyCore> _mm_gemmlowp;
224 std::unique_ptr<kernels::CpuCol2ImKernel> _col2im_kernel;
225 std::unique_ptr<kernels::CpuReshapeKernel> _reshape_kernel;
226
227 TensorInfo _im2col_output;
228 TensorInfo _weights_reshaped;
229 TensorInfo _gemm_output;
230 TensorInfo _gemm_output_3d;
231
232 DataLayout _data_layout;
233
234 bool _skip_im2col;
235 bool _skip_col2im;
236 bool _is_quantized;
237 bool _is_prepared;
238
239 experimental::MemoryRequirements _aux_mem{ Count };
240};
241} // namespace cpu
242} // namespace arm_compute
Georgios Pinitas19884632021-08-16 12:38:54 +0100243#endif /* ARM_COMPUTE_CPU_GEMM_CONV2D_H */