blob: a1b34291d06fb367f5193e278ae937ef84a4b232 [file] [log] [blame]
Manuel Bottinicfac51c2021-06-18 15:47:28 +01001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2021, 2023 Arm Limited.
Manuel Bottinicfac51c2021-06-18 15:47:28 +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 */
24#ifndef ARM_COMPUTE_CPU_GEMMLOWP_MATRIXMULTIPLY_CORE_H
25#define ARM_COMPUTE_CPU_GEMMLOWP_MATRIXMULTIPLY_CORE_H
26
27#include "arm_compute/core/TensorInfo.h"
SiCong Li91295492023-07-21 18:16:13 +010028#include "arm_compute/function_info/GEMMInfo.h"
Manuel Bottinicfac51c2021-06-18 15:47:28 +010029#include "src/core/common/Macros.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010030#include "src/cpu/ICpuOperator.h"
Manuel Bottinicfac51c2021-06-18 15:47:28 +010031
32#include <memory>
33
34namespace arm_compute
35{
36namespace cpu
37{
38namespace kernels
39{
40class CpuGemmInterleave4x4Kernel;
41class CpuGemmLowpMatrixMultiplyKernel;
42class CpuGemmLowpOffsetContributionKernel;
43class CpuGemmLowpOffsetContributionOutputStageKernel;
44class CpuGemmLowpMatrixAReductionKernel;
45class CpuGemmLowpMatrixBReductionKernel;
46class CpuGemmTranspose1xWKernel;
47class CpuConvertQuantizedSignednessKernel;
48} // namespace kernels
49class CpuGemmAssemblyDispatch;
50class CpuActivation;
51
52/** Basic function to execute GEMMLowpMatrixMultiplyCore. This function calls the following kernels if the DOT product instruction is not available:
53 *
54 * -# @ref kernels::CpuGemmInterleave4x4Kernel
55 * -# @ref kernels::CpuGemmTranspose1xWKernel
56 * -# @ref kernels::CpuGemmLowpMatrixMultiplyKernel
57 * -# @ref kernels::CpuGemmLowpOffsetContributionKernel
58 * -# @ref CpuActivation
59 *
60 * otherwise if the DOT product instruction is available:
61 *
62 * -# @ref kernels::CpuGemmLowpOffsetContributionKernel
63 *
64*/
65class CpuGemmLowpMatrixMultiplyCore : public ICpuOperator
66{
67public:
68 /** Constructor */
69 CpuGemmLowpMatrixMultiplyCore();
70 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuGemmLowpMatrixMultiplyCore);
71 /** Destructor */
72 ~CpuGemmLowpMatrixMultiplyCore();
73 /** Initialise the kernel's inputs, output
74 *
75 * Valid data layouts:
76 * - NHWC
77 * - NCHW
78 *
79 * Valid data type configurations:
80 * |src0 |src1 |src2 |dst |
81 * |:--------------|:------------------|:--------|:--------------|
82 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
83 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
84 * |QASYMM8 |QSYMM8 |S32 |QASYMM8 |
85 * |QASYMM8 |QASYMM8 |S32 |S32 |
86 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |S32 |
87 * |QASYMM8 |QSYMM8 |S32 |S32 |
88 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
89 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
90 * |QASYMM8_SIGNED |QSYMM8 |S32 |QASYMM8_SIGNED |
91 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |S32 |
92 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |S32 |
93 * |QASYMM8_SIGNED |QSYMM8 |S32 |S32 |
94 *
95 * @note GEMM_LOWP: low precision GEMM kernel
96 * This kernel performs the following computations:
97 *
98 * -# Convert a values from QASYMM8 to int32 and add a_offset to each of them.
99 * -# Convert b values from QASYMM8 to int32 add b_offset to each of them.
100 * -# Compute the matrix product of the resulting a * b in int32.
101 *
102 * @note The @p output type is S32 if @p gemm_info.type == GEMMLowpOutputStageType::NONE. It is QASYMM8/QASYMM8_SIGNED otherwise
103 *
104 * @param[in] a First input tensor info (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED.
105 * @param[in] b Second input tensor info (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL.
106 * @param[in] c Third input tensor info (Matrix C). It can be a nullptr. Data type supported: S32
107 * @param[out] dst Output tensor info. Data type supported: Data type supported: S32/QASYMM8/QASYMM8_SIGNED
108 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
109 * if the reshape of matrix B should be executed only for the first run
110 */
111 void configure(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *dst, const GEMMInfo &gemm_info = GEMMInfo());
112 /** Static function to check if given info will lead to a valid configuration
113 *
114 * Similar to CpuGemmLowpMatrixMultiplyCore::configure()
115 *
116 * @return a status
117 */
118 static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *dst, const GEMMInfo &gemm_info = GEMMInfo());
119
120 // Inherited methods overridden:
121 void run(ITensorPack &tensors) override;
122 void prepare(ITensorPack &tensors) override;
123 experimental::MemoryRequirements workspace() const override;
124
125private:
126 enum AuxTensorIdx
127 {
128 AsmGemmWorkspace = 0,
129 Pretranspose,
130 VectorSumCol,
131 VectorSumRow,
132 TmpA,
133 TmpB,
134 MMResultS32,
135 SignedA,
136 SignedOutput,
137 Count
138 };
139
140 std::unique_ptr<CpuGemmAssemblyDispatch> _asm_glue;
141 std::unique_ptr<kernels::CpuGemmLowpMatrixMultiplyKernel> _mm_kernel;
142 std::unique_ptr<kernels::CpuGemmInterleave4x4Kernel> _mtx_a_reshape_kernel;
143 std::unique_ptr<kernels::CpuGemmTranspose1xWKernel> _mtx_b_reshape_kernel;
144 std::unique_ptr<kernels::CpuGemmLowpMatrixAReductionKernel> _mtx_a_reduction_kernel;
145 std::unique_ptr<kernels::CpuGemmLowpMatrixBReductionKernel> _mtx_b_reduction_kernel;
146 std::unique_ptr<kernels::CpuGemmLowpOffsetContributionKernel> _offset_contribution_kernel;
147 std::unique_ptr<kernels::CpuGemmLowpOffsetContributionOutputStageKernel> _offset_contribution_output_stage_kernel;
148 std::unique_ptr<CpuActivation> _activation_func;
149 std::unique_ptr<kernels::CpuConvertQuantizedSignednessKernel> _convert_to_signed_asymm;
150 std::unique_ptr<kernels::CpuConvertQuantizedSignednessKernel> _convert_from_signed_asymm;
151
152 TensorInfo _vector_sum_col;
153 TensorInfo _vector_sum_row;
154 TensorInfo _tmp_a;
155 TensorInfo _tmp_b;
156 TensorInfo _mm_result_s32;
157 TensorInfo _signed_a;
158 TensorInfo _signed_output;
159 int32_t _a_offset;
160 int32_t _b_offset;
161
162 bool _run_vector_matrix_multiplication;
163 bool _assembly_path;
164 bool _fused_assembly_path;
165 bool _reshape_b_only_on_first_run;
166 bool _is_prepared;
167 bool _fuse_output_stage;
168 bool _run_activation;
169 bool _flip_signedness;
170 GEMMInfo _gemm_info;
171 experimental::MemoryRequirements _aux_mem{};
172};
173} // namespace cpu
174} // namespace arm_compute
175#endif /*ARM_COMPUTE_CPU_GEMMLOWP_MATRIXMULTIPLY_CORE_H */