Debian Codegraph @

arm_compute/runtime/experimental/operators/CpuGemm.h

raw
1 /*
2 * Copyright (c) 2024 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
25 #ifndef ACL_ARM_COMPUTE_RUNTIME_EXPERIMENTAL_OPERATORS_CPUGEMM_H
26 #define ACL_ARM_COMPUTE_RUNTIME_EXPERIMENTAL_OPERATORS_CPUGEMM_H
27
28 #include "arm_compute/core/ITensorPack.h"
29 #include "arm_compute/core/TensorInfo.h"
30 #include "arm_compute/function_info/GEMMInfo.h"
31 #include "arm_compute/runtime/IOperator.h"
32
33 /*
34 * A shallow wrapper for arm_compute::cpu::CpuGemm.
35 * Any new features should be added to arm_compute::cpu::CpuGemm and
36 * arm_compute::experimental::op::CpuGemm should remain a shallow wrapper.
37 */
38
39 namespace arm_compute
40 {
41 namespace experimental
42 {
43 namespace op
44 {
45 /** Wrapper class for CpuGemm. For information on the operators,
46 * see "src/cpu/operators/CpuGemm.h"
47 */
48 class CpuGemm : public IOperator
49 {
50 public:
51 /** Constructor **/
52 CpuGemm();
53 /** Prevent instances of this class from being copied (As this class contains pointers) */
54 CpuGemm(const CpuGemm &) = delete;
55 /** Prevent copy assignment */
56 CpuGemm operator=(const CpuGemm &) = delete;
57 /** Default move constructor */
58 CpuGemm(CpuGemm &&) = default;
59 /** Default move assignment */
60 CpuGemm &operator=(CpuGemm &&) = default;
61 /** Default destructor */
62 ~CpuGemm() override;
63
64 /** Configure operator for a given list of arguments
65 *
66 * Valid data layouts:
67 * - All
68 *
69 * Valid data type configurations:
70 * |a |b |c |d |
71 * |:------------|:-----------|:---------|:--------------|
72 * |F32 |F32 |F32 |F32 |
73 * |F16 |F16 |F16 |F16 |
74 * |BFLOAT16 |BFLOAT16 |BFLOAT16 |FP32 |
75 *
76 * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
77 * @note GEMM: The tensors a, b, c, d must have the same data type. You should not mix data types when calling this function.
78 *
79 * @note Batched GEMM only supports broadcasting cases where RHS rank < LHS rank but not the other way around
80 *
81 * @param[in] a First input tensor info (Matrix A or Vector A). Data type supported: BFLOAT16/F16/F32
82 * @param[in] b Second input tensor info (Matrix B). Data type supported: same as @p a
83 * @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
84 * @param[out] d Output tensor info. Data type supported: same as @p a
85 * @param[in] alpha Weight of the matrix product
86 * @param[in] beta Weight of matrix C
87 * @param[in, out] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
88 * if the reshape of matrix B should happen only for the first run
89 */
90 void configure(const ITensorInfo *a,
91 const ITensorInfo *b,
92 const ITensorInfo *c,
93 ITensorInfo *d,
94 float alpha,
95 float beta,
96 const GEMMInfo &gemm_info = GEMMInfo());
97
98 /** Static function to check if given info will lead to a valid configuration of @ref CpuGemm.
99 *
100 * Similar to @ref CpuGemm::configure()
101 *
102 * @return a status
103 */
104 static Status validate(const ITensorInfo *a,
105 const ITensorInfo *b,
106 const ITensorInfo *c,
107 const ITensorInfo *d,
108 float alpha,
109 float beta,
110 const GEMMInfo &gemm_info = GEMMInfo());
111
112 /** Indicates whether or not there is an optimal assembly implementation that can be used to process the given parameters.
113 *
114 * This method has the same use of @ref
115 * NEGEMMConvolutionLayer::has_opt_impl, with the only caveat that
116 * the value of arm_compute::WeightFormat need to be passed via the
117 * parameter gemm_info.
118 */
119 static Status has_opt_impl(arm_compute::WeightFormat &weight_format,
120 const ITensorInfo *a,
121 const ITensorInfo *b,
122 const ITensorInfo *c,
123 const ITensorInfo *d,
124 const GEMMInfo &gemm_info = GEMMInfo());
125
126 void run(ITensorPack &tensors) override;
127 void prepare(ITensorPack &constants) override;
128 experimental::MemoryRequirements workspace() const override;
129
130 private:
131 struct Impl;
132 std::unique_ptr<Impl> _impl;
133 };
134 } // namespace op
135 } // namespace experimental
136 } // namespace arm_compute
137
138 #endif // ACL_ARM_COMPUTE_RUNTIME_EXPERIMENTAL_OPERATORS_CPUGEMM_H