xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/operators/CpuGemm.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2021-2023 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_CPU_GEMM_H
25*c217d954SCole Faust #define ARM_COMPUTE_CPU_GEMM_H
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "src/cpu/ICpuOperator.h"
28*c217d954SCole Faust 
29*c217d954SCole Faust #include "arm_compute/core/ITensorPack.h"
30*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
31*c217d954SCole Faust #include "arm_compute/core/Types.h"
32*c217d954SCole Faust #include "src/cpu/kernels/CpuGemmInterleave4x4Kernel.h"
33*c217d954SCole Faust #include "src/cpu/kernels/CpuGemmMatrixAdditionKernel.h"
34*c217d954SCole Faust #include "src/cpu/kernels/CpuGemmMatrixMultiplyKernel.h"
35*c217d954SCole Faust #include "src/cpu/kernels/CpuGemmTranspose1xWKernel.h"
36*c217d954SCole Faust #include "src/cpu/operators/CpuActivation.h"
37*c217d954SCole Faust #include "src/cpu/operators/CpuAdd.h"
38*c217d954SCole Faust #include "src/cpu/operators/internal/CpuGemmAssemblyDispatch.h"
39*c217d954SCole Faust 
40*c217d954SCole Faust #include <memory>
41*c217d954SCole Faust 
42*c217d954SCole Faust namespace arm_compute
43*c217d954SCole Faust {
44*c217d954SCole Faust namespace cpu
45*c217d954SCole Faust {
46*c217d954SCole Faust /** Basic function to execute GEMM. This function calls the following kernels:
47*c217d954SCole Faust  *
48*c217d954SCole Faust  * If optimized assembly is available:
49*c217d954SCole Faust  *  -# @ref cpu::CpuGemmAssemblyDispatch
50*c217d954SCole Faust  *  -# @ref cpu::CpuActivation (if alpha != 1.0)
51*c217d954SCole Faust  * Else:
52*c217d954SCole Faust  *  -# @ref cpu::kernels::CpuGemmInterleave4x4Kernel (if the output tensor is a matrix)
53*c217d954SCole Faust  *  -# @ref cpu::kernels::CpuGemmTranspose1xWKernel (if the output tensor is a matrix)
54*c217d954SCole Faust  *  -# @ref cpu::kernels::CpuGemmMatrixMultiplyKernel
55*c217d954SCole Faust  * In both cases:
56*c217d954SCole Faust  *  -# @ref cpu::kernels::CpuGemmMatrixAdditionKernel (if c != nullptr and beta != 0.0 and is not reshaped once)
57*c217d954SCole Faust  * Else:
58*c217d954SCole Faust  *  -# @ref cpu::CpuAdd (if c != nullptr and is reshaped once and not optimized assembly in place)
59*c217d954SCole Faust  *
60*c217d954SCole Faust  *  -# @ref cpu::CpuActivation (if activation is specified in GEMMInfo)
61*c217d954SCole Faust  */
62*c217d954SCole Faust class CpuGemm : public ICpuOperator
63*c217d954SCole Faust {
64*c217d954SCole Faust public:
65*c217d954SCole Faust     /** Default constructor */
66*c217d954SCole Faust     CpuGemm() = default;
67*c217d954SCole Faust     /** Default destructor */
68*c217d954SCole Faust     ~CpuGemm() = default;
69*c217d954SCole Faust     /** Configure operator for a given list of arguments
70*c217d954SCole Faust      *
71*c217d954SCole Faust      * Valid data layouts:
72*c217d954SCole Faust      * - All
73*c217d954SCole Faust      *
74*c217d954SCole Faust      * Valid data type configurations:
75*c217d954SCole Faust      * |src0         |src1        |src2      |dst            |
76*c217d954SCole Faust      * |:------------|:-----------|:---------|:--------------|
77*c217d954SCole Faust      * |F32          |F32         |F32       |F32            |
78*c217d954SCole Faust      * |F16          |F16         |F16       |F16            |
79*c217d954SCole Faust      * |BFLOAT16     |BFLOAT16    |BFLOAT16  |FP32           |
80*c217d954SCole Faust      *
81*c217d954SCole Faust      * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
82*c217d954SCole Faust      * @note GEMM: The tensors a, b, c, d must have the same data type. You should not mix data types when calling this function.
83*c217d954SCole Faust      *
84*c217d954SCole Faust      * @note Batched GEMM only supports broadcasting cases where RHS rank < LHS rank but not the other way around
85*c217d954SCole Faust      *
86*c217d954SCole Faust      * @param[in]  a         First input tensor info (Matrix A or Vector A). Data type supported: BFLOAT16/F16/F32
87*c217d954SCole Faust      * @param[in]  b         Second input tensor info (Matrix B). Data type supported: same as @p a
88*c217d954SCole Faust      * @param[in]  c         Third input tensor info (Matrix C). It can be a nullptr if just the multiplication between @p a and @p b is needed. Data type supported: same as @p a
89*c217d954SCole Faust      * @param[out] d         Output tensor info. Data type supported: same as @p a
90*c217d954SCole Faust      * @param[in]  alpha     Weight of the matrix product
91*c217d954SCole Faust      * @param[in]  beta      Weight of matrix C
92*c217d954SCole Faust      * @param[in]  gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
93*c217d954SCole Faust      *                       if the reshape of matrix B should happen only for the first run
94*c217d954SCole Faust      */
95*c217d954SCole Faust     void configure(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d,
96*c217d954SCole Faust                    float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
97*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration of @ref CpuGemm.
98*c217d954SCole Faust      *
99*c217d954SCole Faust      * Similar to @ref CpuGemm::configure()
100*c217d954SCole Faust      *
101*c217d954SCole Faust      * @return a status
102*c217d954SCole Faust      */
103*c217d954SCole Faust     static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d,
104*c217d954SCole Faust                            float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
105*c217d954SCole Faust 
106*c217d954SCole Faust     /** Indicates whether or not there is an optimal assembly implementation that can be used to process the given parameters.
107*c217d954SCole Faust      *
108*c217d954SCole Faust      * This method has the same use of @ref
109*c217d954SCole Faust      * NEGEMMConvolutionLayer::has_opt_impl, with the only caveat that
110*c217d954SCole Faust      * the value of arm_compute::WeightFormat need to be passed via the
111*c217d954SCole Faust      * parameter gemm_info.
112*c217d954SCole Faust      */
113*c217d954SCole Faust     static Status has_opt_impl(arm_compute::WeightFormat &weight_format, const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d,
114*c217d954SCole Faust                                const GEMMInfo &gemm_info = GEMMInfo());
115*c217d954SCole Faust 
116*c217d954SCole Faust     // Inherited methods overridden:
117*c217d954SCole Faust     void run(ITensorPack &tensors) override;
118*c217d954SCole Faust     void prepare(ITensorPack &constants) override;
119*c217d954SCole Faust     experimental::MemoryRequirements workspace() const override;
120*c217d954SCole Faust 
121*c217d954SCole Faust     /** Indicates if the convolution executes in variable weights mode.
122*c217d954SCole Faust      *
123*c217d954SCole Faust      * When ACL executes convolution in variable weights mode, it does
124*c217d954SCole Faust      * not perform any processing of the weights tensor. Instead, it
125*c217d954SCole Faust      * utilizes the data as it is given by the user.
126*c217d954SCole Faust      */
127*c217d954SCole Faust     bool isVarWeightsKernel() const;
128*c217d954SCole Faust 
129*c217d954SCole Faust private:
130*c217d954SCole Faust     enum AuxTensorIdx
131*c217d954SCole Faust     {
132*c217d954SCole Faust         AsmGemmWorkspace = 0,
133*c217d954SCole Faust         Pretraspose,
134*c217d954SCole Faust         InterleavedLHS,
135*c217d954SCole Faust         TransposedRHS,
136*c217d954SCole Faust         TempResult,
137*c217d954SCole Faust         Count
138*c217d954SCole Faust     };
139*c217d954SCole Faust 
140*c217d954SCole Faust     std::unique_ptr<kernels::CpuGemmInterleave4x4Kernel>  _interleave_kernel{ nullptr };
141*c217d954SCole Faust     std::unique_ptr<kernels::CpuGemmTranspose1xWKernel>   _transpose_kernel{ nullptr };
142*c217d954SCole Faust     std::unique_ptr<kernels::CpuGemmMatrixMultiplyKernel> _mm_kernel{ nullptr };
143*c217d954SCole Faust     std::unique_ptr<CpuGemmAssemblyDispatch>              _asm_glue{ nullptr };
144*c217d954SCole Faust     std::unique_ptr<kernels::CpuGemmMatrixAdditionKernel> _ma_kernel{ nullptr };
145*c217d954SCole Faust     std::unique_ptr<CpuActivation>                        _alpha_scale_func{ nullptr };
146*c217d954SCole Faust     std::unique_ptr<CpuAdd>                               _add_bias{ nullptr };
147*c217d954SCole Faust     std::unique_ptr<CpuActivation>                        _activation_func{ nullptr };
148*c217d954SCole Faust 
149*c217d954SCole Faust     TensorInfo _tmp_a{};
150*c217d954SCole Faust     TensorInfo _tmp_b{};
151*c217d954SCole Faust     TensorInfo _tmp_d{};
152*c217d954SCole Faust 
153*c217d954SCole Faust     bool _run_vector_matrix_multiplication{ false };
154*c217d954SCole Faust     bool _run_alpha_scale{ false };
155*c217d954SCole Faust     bool _run_addition{ false };
156*c217d954SCole Faust     bool _run_bias_addition{ false };
157*c217d954SCole Faust     bool _run_activation{ false };
158*c217d954SCole Faust     bool _reshape_b_only_on_first_run{ false };
159*c217d954SCole Faust     bool _is_prepared{ false };
160*c217d954SCole Faust 
161*c217d954SCole Faust     experimental::MemoryRequirements _aux_mem{ Count };
162*c217d954SCole Faust };
163*c217d954SCole Faust } // namespace cpu
164*c217d954SCole Faust } // namespace arm_compute
165*c217d954SCole Faust #endif /*ARM_COMPUTE_CPU_GEMM_H */
166