xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/runtime/CL/functions/CLGEMM.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2016-2021, 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_CLGEMM_H
25 #define ARM_COMPUTE_CLGEMM_H
26 
27 #include "arm_compute/runtime/CL/CLTensor.h"
28 #include "arm_compute/runtime/CL/CLTypes.h"
29 #include "arm_compute/runtime/IFunction.h"
30 #include "arm_compute/runtime/IMemoryManager.h"
31 #include "arm_compute/runtime/IWeightsManager.h"
32 #include "arm_compute/runtime/MemoryGroup.h"
33 
34 #include <memory>
35 
36 namespace arm_compute
37 {
38 // Forward declarations
39 class CLCompileContext;
40 class ICLTensor;
41 class ITensorInfo;
42 
43 /** Basic function to execute GEMM on OpenCL */
44 class CLGEMM : public IFunction
45 {
46 public:
47     /** Default constructor.
48      *
49      * @param[in] memory_manager  (Optional) Memory manager.
50      * @param[in] weights_manager (Optional) Weights manager.
51      */
52     CLGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
53     /** Default destructor */
54     ~CLGEMM();
55     /** Prevent instances of this class from being copied (As this class contains pointers) */
56     CLGEMM(const CLGEMM &) = delete;
57     /** Default move constructor */
58     CLGEMM(CLGEMM &&);
59     /** Prevent instances of this class from being copied (As this class contains pointers) */
60     CLGEMM &operator=(const CLGEMM &) = delete;
61     /** Default move assignment operator */
62     CLGEMM &operator=(CLGEMM &&);
63     /** Initialise the kernel's inputs and output
64      *
65      * Valid data layouts:
66      * - All
67      *
68      * Valid data type configurations:
69      * |src0         |src1        |src2      |dst            |
70      * |:------------|:-----------|:---------|:--------------|
71      * |F32          |F32         |F32       |F32            |
72      * |F16          |F16         |F16       |F16            |
73      *
74      * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
75      *
76      * @note All tensors must have the same data type.
77      *
78      * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix
79      *
80      * @note Batched GEMM only allows RHS tensor's rank to be <= 3
81      * @note Batched GEMM only supports broadcasting cases where RHS rank < LHS rank but not the other way around
82      *
83      * @param[in]  compile_context The compile context to be used.
84      * @param[in]  a               First input tensor  (Matrix or Vector A). Data types supported: F16/F32
85      * @param[in]  b               Second input tensor (Matrix B). Data type supported: same as @p a.
86      * @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.
87      * @param[out] output          Output tensor. Data type supported: same as @p a
88      * @param[in]  alpha           Weight of the matrix product
89      * @param[in]  beta            Weight of matrix C
90      * @param[in]  gemm_info       (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
91      *                             if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping
92      *                             in case matrix A and matrix B have been already transformed.
93      */
94     void configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
95 
96     /** Initialise the kernel's inputs and output
97      *
98      * Similar to @ref CLGEMM::configure()
99      */
100     void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
101 
102     /** Static function to check if given info will lead to a valid configuration of @ref CLGEMM.
103      *
104      * Similar to @ref CLGEMM::configure()
105      *
106      * @return a status
107      */
108     static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
109 
110     // Inherited methods overridden:
111     void run() override;
112     void prepare() override;
113 
114 private:
115     struct Impl;
116     std::unique_ptr<Impl> _impl;
117 };
118 } // namespace arm_compute
119 
120 #endif /* ARM_COMPUTE_CLGEMM_H */
121