Debian Codegraph @

arm_compute/runtime/experimental/low_level/CpuGemmAssemblyDispatch.h

raw
1 /*
2 * Copyright (c) 2018-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_LOW_LEVEL_CPUGEMMASSEMBLYDISPATCH_H
26 #define ACL_ARM_COMPUTE_RUNTIME_EXPERIMENTAL_LOW_LEVEL_CPUGEMMASSEMBLYDISPATCH_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 class to expose CpuGemmAssemblyDispatch.
35 * New functionality should be added to src/cpu/operators/internal/CpuGemmAssemblyDispatch.h.
36 */
37
38 namespace arm_compute
39 {
40 namespace experimental
41 {
42 namespace op
43 {
44 namespace ll
45 {
46 /** Wrapper class for CpuGemmAssemblyDispatch. For information on the functions,
47 * see "src/cpu/operators/CpuGemmAssemblyDispatch.h".
48 *
49 * Following fields will be ignored if passed in through GEMMInfo in configure()
50 * and has_opt_impl(). If these fields are set incorrectly, validate() will
51 * return false:
52 * GEMMInfo.method, GEMMInfo.reinterpret_input_as_3d, GEMMInfo.depth_output_gemm3d,
53 * GEMMInfo.output_stage, GEMMInfo.reshape_b_only_on_first_run
54 */
55 class CpuGemmAssemblyDispatch : arm_compute::experimental::IOperator
56 {
57 public:
58 /** Constructor **/
59 CpuGemmAssemblyDispatch();
60 /** Prevent instances of this class from being copied (As this class contains pointers) */
61 CpuGemmAssemblyDispatch(const CpuGemmAssemblyDispatch &) = delete;
62 /** Default move constructor */
63 CpuGemmAssemblyDispatch(CpuGemmAssemblyDispatch &&) = default;
64 /** Default destructor */
65 ~CpuGemmAssemblyDispatch();
66
67 /** If supported create a Compute Library function else fallback to the arm_gemm function.
68 *
69 * @note Configuring "batches"
70 * The shapes of @p a @p b and @p d are arranged as follows:
71 * Lowest dimension <-> Highest dimension
72 * a: [K, M, Batch, Multi]
73 * b: [N, K, Multi]
74 * d: [N, M, Batch, Multi]
75 *
76 * The "Batch" refers to where "Batch" number of MxK slices of tensor a multiplies with a single KxN slice of b
77 * The "Multi" refers to where "Multi" number of individual multiplication of a with b
78 *
79 * E.g. the following are some example input shape configurations
80 *
81 * (1) Normal 2D gemm
82 * a: [K=3, M=4]
83 * b: [N=5, K=3]
84 * d: [N=5, M=4]
85 *
86 * (2) Batches of a sharing b (e.g. gemm-based batched convolution where b is the shared )
87 * a: [K=3, M=4, Batch=9]
88 * b: [N=5, K=3]
89 * d: [N=5, M=4, Batch=9]
90 *
91 * (3) "Batches" of independent gemm (e.g. batched matmul)
92 * a: [K=3, M=4, Batch=1, Multi=7]
93 * b: [N=5, K=3, Multi=7]
94 * d: [N=5, M=4, Batch=1, Multi=7]
95 *
96 * (4) "Batches" of independent gemm where b is also shared
97 * a: [K=3, M=4, Batch=4, Multi=7]
98 * b: [N=5, K=3, Multi=7]
99 * d: [N=5, M=4, Batch=4, Multi=7]
100 *
101 * @param[in] a Input tensor (Matrix A)
102 * @param[in] b Input tensor (Matrix B)
103 * @param[in] c Input tensor (Matrix C) used to pass the bias for quantized calculations
104 * @param[out] d Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0.
105 * @param[in] gemm_info GEMM meta-data
106 */
107 void configure(const ITensorInfo *a,
108 const ITensorInfo *b,
109 const ITensorInfo *c,
110 ITensorInfo *d,
111 const GEMMInfo &gemm_info = GEMMInfo());
112
113 /** Indicates whether or not this function can be used to process the given parameters.
114 * Valid data type configurations:
115 * |src0 |src1 |src2 |dst |
116 * |:------------|:-----------|:---------|:--------------|
117 * |F32 |F32 |nullptr |F32 |
118 * |F16 |F16 |nullptr |F16 |
119 * |BFLOAT16 |BFLOAT16 |nullptr |BFLOAT16 |
120 * |BFLOAT16 |BFLOAT16 |nullptr |BFLOAT32 |
121 *
122 * @param[in] a Input tensor info (Matrix A)
123 * @param[in] b Input tensor info (Matrix B)
124 * @param[in] c Input tensor info (Matrix C) used to pass the bias for quantized calculations
125 * @param[in] d Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0.
126 * @param[in] gemm_info GEMM meta-data
127 *
128 * @return a status.
129 */
130 static Status validate(const ITensorInfo *a,
131 const ITensorInfo *b,
132 const ITensorInfo *c,
133 const ITensorInfo *d,
134 const GEMMInfo &gemm_info = GEMMInfo());
135
136 /** Indicates whether or not there is an optimal assembly implementation that can be used to process the given parameters.
137 *
138 * This method has the same use of @ref
139 * NEGEMMConvolutionLayer::has_opt_impl, with the only caveat that
140 * the value of arm_compute::WeightFormat need to be passed via the
141 * parameter info.
142 *
143 * @return a status.
144 */
145 static Status has_opt_impl(arm_compute::WeightFormat &weight_format,
146 const ITensorInfo *a,
147 const ITensorInfo *b,
148 const ITensorInfo *c,
149 const ITensorInfo *d,
150 const GEMMInfo &gemm_info = GEMMInfo());
151
152 /** Checks if activation is supported by the gemm assembly dispatcher
153 *
154 * @param[in] activation Activation to check
155 *
156 * @return True if activation is supported else false
157 */
158 static bool is_activation_supported(const ActivationLayerInfo &activation);
159
160 /** Was the function successfully configured ?
161 *
162 * @return True if the function is configured and ready to run
163 */
164 bool is_configured() const;
165
166 // Inherited methods overridden:
167 void run(ITensorPack &tensors);
168 void prepare(ITensorPack &constants);
169 experimental::MemoryRequirements workspace() const;
170
171 private:
172 struct Impl;
173 std::unique_ptr<Impl> _impl;
174 };
175 } // namespace ll
176 } // namespace op
177 } // namespace experimental
178 } // namespace arm_compute
179
180 #endif // ACL_ARM_COMPUTE_RUNTIME_EXPERIMENTAL_LOW_LEVEL_CPUGEMMASSEMBLYDISPATCH_H