xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/runtime/NEON/functions/NEGEMM.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2023 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  */
24 #ifndef ARM_COMPUTE_NEGEMM_H
25 #define ARM_COMPUTE_NEGEMM_H
26 
27 #include "arm_compute/runtime/IFunction.h"
28 #include "arm_compute/runtime/IMemoryManager.h"
29 #include "arm_compute/runtime/IWeightsManager.h"
30 
31 #include <memory>
32 
33 namespace arm_compute
34 {
35 /** Basic function to execute GEMM. This function calls the following kernels:
36  *
37  *  -# @ref cpu::CpuGemm
38  */
39 class NEGEMM : public IFunction
40 {
41 public:
42     /** Constructor */
43     NEGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
44     /** Prevent instances of this class from being copied (As this class contains pointers) */
45     NEGEMM(const NEGEMM &) = delete;
46     /** Default move constructor */
47     NEGEMM(NEGEMM &&) = default;
48     /** Prevent instances of this class from being copied (As this class contains pointers) */
49     NEGEMM &operator=(const NEGEMM &) = delete;
50     /** Default move assignment operator */
51     NEGEMM &operator=(NEGEMM &&) = default;
52     /** Default destructor */
53     ~NEGEMM();
54     /** Initialise the kernel's inputs, output
55      *
56      * Valid data layouts:
57      * - All
58      *
59      * Valid data type configurations:
60      * |src0         |src1        |src2      |dst            |
61      * |:------------|:-----------|:---------|:--------------|
62      * |F32          |F32         |F32       |F32            |
63      * |F16          |F16         |F16       |F16            |
64      * |BFLOAT16     |BFLOAT16    |BFLOAT16  |BFLOAT16       |
65      *
66      * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
67      * @note GEMM: The tensors a, b, c, d must have the same data type. You should not mix data types when calling this function.
68      *
69      * @note Batched GEMM only supports broadcasting cases where RHS rank < LHS rank but not the other way around
70      *
71      * @param[in]  a         First input tensor  (Matrix A or Vector A). Data type supported: BFLOAT16/F16/F32
72      * @param[in]  b         Second input tensor (Matrix B). Data type supported: same as @p a
73      * @param[in]  c         Third input tensor  (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
74      * @param[out] d         Output tensor. Data type supported: same as @p a
75      * @param[in]  alpha     Weight of the matrix product
76      * @param[in]  beta      Weight of matrix C
77      * @param[in]  gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
78      *                       if the reshape of matrix B should happen only for the first run
79      */
80     void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
81     /** Static function to check if given info will lead to a valid configuration of @ref NEGEMM.
82      *
83      * Similar to @ref NEGEMM::configure()
84      *
85      * @return a status
86      */
87     static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
88 
89     /** Static function that queries whether there exists fixed-format kernel and if it exists it will return in the first argument in what format
90      * weights are expected to be reshaped as defined by WeightFormat class. Apart from the first argument the rest of the arguments are the same
91      * as in @ref NEGEMM::validate() except that all arguments are required.
92      *
93      * @return a status
94      */
95     static Status has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output,
96                                float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
97 
98     // Inherited methods overridden:
99     void run() override;
100     void prepare() override;
101 
102 private:
103     struct Impl;
104     std::unique_ptr<Impl> _impl;
105 };
106 } // namespace arm_compute
107 #endif /*ARM_COMPUTE_NEGEMM_H */
108