blob: 08b76a6c46b9d47a442f4c324ee4e198472504c8 [file] [log] [blame]
Manuel Bottini29599d02021-07-06 15:01:35 +01001/*
Francesco.Petrogalli@arm.comfa6877f2022-04-13 09:28:25 +00002 * Copyright (c) 2021-2022 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
27#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Types.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010029#include "src/cpu/ICpuOperator.h"
Manuel Bottini29599d02021-07-06 15:01:35 +010030
31#include <memory>
32
33namespace arm_compute
34{
35namespace cpu
36{
37class CpuGemm;
38class CpuGemmLowpMatrixMultiplyCore;
39class CpuGemmLowpOutputStage;
40namespace kernels
41{
42class CpuWeightsReshapeKernel;
43class CpuIm2ColKernel;
44class CpuCol2ImKernel;
45class CpuReshapeKernel;
46} // namespace kernels
47
48/** Basic function to compute the convolution layer. This function calls the following kernels/functions:
49 *
50 * -# @ref cpu::kernels::CpuIm2ColKernel
51 * -# @ref CpuGemm (if the data type is BFLOAT16/FP16/FP32)
52 * -# @ref CpuGemmLowpMatrixMultiplyCore (if the data type is QASYMM8/QASYMM8_SIGNED)
53 * -# @ref CpuGemmLowpOutputStage (if the data type is QASYMM8/QASYMM8_SIGNED)
54 * -# @ref cpu::kernels::CpuCol2ImKernel (if NCHW data layout)
55 * -# @ref kernels::CpuWeightsReshapeKernel
56 *
57 */
Georgios Pinitas19884632021-08-16 12:38:54 +010058class CpuGemmConv2d : public ICpuOperator
Manuel Bottini29599d02021-07-06 15:01:35 +010059{
60public:
61 /** Constructor */
Georgios Pinitas19884632021-08-16 12:38:54 +010062 CpuGemmConv2d();
Manuel Bottini29599d02021-07-06 15:01:35 +010063 /** Prevent instances of this class from being copied (As this class contains pointers) */
Georgios Pinitas19884632021-08-16 12:38:54 +010064 CpuGemmConv2d(const CpuGemmConv2d &) = delete;
Manuel Bottini29599d02021-07-06 15:01:35 +010065 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
Georgios Pinitas19884632021-08-16 12:38:54 +010066 CpuGemmConv2d(CpuGemmConv2d &&) = delete;
Manuel Bottini29599d02021-07-06 15:01:35 +010067 /** Prevent instances of this class from being copied (As this class contains pointers) */
Georgios Pinitas19884632021-08-16 12:38:54 +010068 CpuGemmConv2d &operator=(const CpuGemmConv2d &) = delete;
Manuel Bottini29599d02021-07-06 15:01:35 +010069 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
Georgios Pinitas19884632021-08-16 12:38:54 +010070 CpuGemmConv2d &operator=(CpuGemmConv2d &&) = delete;
Manuel Bottini29599d02021-07-06 15:01:35 +010071 /** Destructor */
Georgios Pinitas19884632021-08-16 12:38:54 +010072 ~CpuGemmConv2d();
Manuel Bottini29599d02021-07-06 15:01:35 +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 * |BFLOAT16 |BFLOAT16 |BFLOAT16 |BFLOAT16 |
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 *
Georgios Pinitas69a9ac42021-07-22 13:30:13 +010090 * @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/BFLOAT16/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: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
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 QASYMM8/QASYMM8_SIGNED 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] conv_info Contains padding and stride information described in @ref PadStrideInfo.
100 * @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
101 * tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input.
102 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
103 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
104 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
105 * available which may introduce a drop of accuracy as well. Default is false
106 * @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 +0100107 */
108 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 +0100109 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 +0100110 /** Static function to check if given info will lead to a valid configuration
111 *
112 * Similar to CpuGemmConvolution::configure()
113 *
114 * @return a status
115 */
116 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 +0100117 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(),
118 bool enable_fast_math = false, unsigned int num_groups = 1);
Manuel Bottini29599d02021-07-06 15:01:35 +0100119
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000120 /** Indicates whether or not there is an optimal assembly implementation that can be used to process the given parameters.
121 *
122 * The paramter list is the same as @ref NEGEMMConvolutionLayer::has_opt_impl
123 *
124 * @return a status.
125 */
Ramy Elgammal91780022022-07-20 14:57:37 +0100126 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 +0000127 const PadStrideInfo &conv_info,
128 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(),
129 const bool enable_fast_math = false);
130
Manuel Bottini29599d02021-07-06 15:01:35 +0100131 // Inherited methods overridden:
Ramy Elgammal91780022022-07-20 14:57:37 +0100132 void run(ITensorPack &tensors) override;
133 void prepare(ITensorPack &tensors) override;
Manuel Bottini29599d02021-07-06 15:01:35 +0100134 experimental::MemoryRequirements workspace() const override;
135
136private:
137 /** Configures the appropriate matrix multiply routine
138 *
Georgios Pinitas69a9ac42021-07-22 13:30:13 +0100139 * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
140 * @param[in] weights Weights tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
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 QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
143 * @param[out] dst Output tensor info. Data types supported: Same as @p input,
144 * except for input of QASYMM8/QASYMM8_SIGNED type where output should be of S32 type.
145 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
146 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
147 * available which may introduce a drop of accuracy as well. Default is false
148 * @param[in] gemm_3d_depth (Optional) Depth of GEMM 3D (Defaults to 1)
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000149 * @param[in] fixed_format (Optional) Select GEMM execution with variable weights.
150 * @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 +0100151 */
152 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 +0100153 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 +0100154 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer matrix multiply routines
155 *
Georgios Pinitas69a9ac42021-07-22 13:30:13 +0100156 * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
157 * @param[in] weights Weights tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
158 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
159 * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
160 * @param[in] dst Output tensor info. Data types supported: Same as @p input,
161 * except for input of QASYMM8/QASYMM8_SIGNED type where output should be of S32 type.
162 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
163 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
164 * available which may introduce a drop of accuracy as well. Default is false
165 * @param[in] gemm_3d_depth (Optional) Depth of GEMM 3D (Defaults to 1)
166 * @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 +0000167 * @param[in] fixed_format (Optional) Select GEMM execution with variable weights.
168 * @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 +0100169 *
170 * @return a status
171 */
172 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 +0100173 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 +0100174 /** Static function to check if GEMM3D is supported in @ref NEGEMM or in @ref CpuGemmMLowpMatrixMultiplyCore
175 *
176 * @param[in] src Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
177 * @param[in] weights Weights tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
178 * @param[in] act_info Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
179 * @param[in] gemm_3d_depth Depth of GEMM 3D
180 * @param[in] skip_im2col Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout
181 *
182 * @return a status
183 */
184 static Status validate_gemm3d(const ITensorInfo *src, const ITensorInfo *weights, const ActivationLayerInfo &act_info, int gemm_3d_depth, bool skip_im2col);
185
Francesco.Petrogalli@arm.comfa6877f2022-04-13 09:28:25 +0000186 struct SkipInfo
187 {
188 bool skip_im2col;
189 bool skip_col2im;
190 };
191
192 /** Static function to provide skip_im2col and skip_col2im information.
193 *
194 * @param[in] src Input tensor info.
195 * @param[in] weights Weights tensor info.
196 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
197 * @param[in] dilation Dilation, in elements, across x and y.
198 * @param[in] act_info Activation layer information in case of a fused activation.
199 *
200 * @return a SkipInfo instance.
201 */
202 static SkipInfo skip_im_col_info(const ITensorInfo *src, const ITensorInfo *weights, const PadStrideInfo &conv_info,
203 const Size2D &dilation, const ActivationLayerInfo &act_info);
204
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000205 /** Indicates if the convolution executes in variable weights mode.
206 *
207 * Similar to @ref CpuGemm::isVarWeightsKernel
208 */
209 bool isVarWeightsKernel() const;
Manuel Bottini29599d02021-07-06 15:01:35 +0100210 enum AuxTensorIdx
211 {
212 // CpuGemmLowpMatrixMultiplyCore has up to 8 internal tensors
213 Im2ColOutput = 9,
214 WeightsReshaped,
215 GemmOutput,
Manuel Bottini29599d02021-07-06 15:01:35 +0100216 Count
217 };
218
219 std::unique_ptr<kernels::CpuWeightsReshapeKernel> _weights_reshape_kernel;
220 std::unique_ptr<cpu::kernels::CpuIm2ColKernel> _im2col_kernel;
221 std::unique_ptr<CpuGemm> _mm_gemm;
222 std::unique_ptr<CpuGemmLowpMatrixMultiplyCore> _mm_gemmlowp;
223 std::unique_ptr<kernels::CpuCol2ImKernel> _col2im_kernel;
224 std::unique_ptr<kernels::CpuReshapeKernel> _reshape_kernel;
225
226 TensorInfo _im2col_output;
227 TensorInfo _weights_reshaped;
228 TensorInfo _gemm_output;
229 TensorInfo _gemm_output_3d;
230
231 DataLayout _data_layout;
232
233 bool _skip_im2col;
234 bool _skip_col2im;
235 bool _is_quantized;
236 bool _is_prepared;
237
238 experimental::MemoryRequirements _aux_mem{ Count };
239};
240} // namespace cpu
241} // namespace arm_compute
Georgios Pinitas19884632021-08-16 12:38:54 +0100242#endif /* ARM_COMPUTE_CPU_GEMM_CONV2D_H */