interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-sycl / fusion.cpp

351 lines · 14806 bytes Raw Blame History
1#include "fusion.hpp"
2#include "binbcast.hpp"
3
4#include <algorithm>
5
6// mul_mat(gate) + mul_mat(up) + GLU: graph shape and tensor properties only. Backend state
7// (weight layout, split buffers, DMMV) is checked by ggml_sycl_op_mul_mat_glu_fused().
8static bool ggml_sycl_should_fuse_mul_mat_glu(const ggml_tensor * gate, const ggml_tensor * up,
9 const ggml_tensor * glu) {
10 // the fused epilogue implements these two; the rest fall back to the standalone GLU kernels
11 const ggml_glu_op glu_op = ggml_get_glu_op(glu);
12 if (glu_op != GGML_GLU_OP_SWIGLU && glu_op != GGML_GLU_OP_GEGLU) {
13 return false;
14 }
15
16 // the kernel always treats src[0] as the activated operand and src[1] as the multiplier
17 if (ggml_get_op_params_i32(glu, 1) /* swapped */) {
18 return false;
19 }
20
21 const ggml_tensor * wu = up->src[0];
22 const ggml_tensor * wg = gate->src[0];
23 const ggml_tensor * act = up->src[1];
24
25 // one activation and one output indexing must serve both weights; the block types
26 // may differ, since the plain-layout fused kernel runs each operand's own vec_dot
27 // (different types then imply different byte strides, so only the shape must agree)
28 if (!ggml_are_same_shape(wu, wg)) {
29 return false;
30 }
31 if (act != gate->src[1]) {
32 return false;
33 }
34
35 // fused K-quant GEMVs walk whole QK_K super-blocks: the reorder kernel covers same-type
36 // q4_K, the plain-layout kernel covers q5_K / iq4_xs pairs incl. mixed gate/up types
37 const bool reorder_pair = wu->type == GGML_TYPE_Q4_K && wg->type == GGML_TYPE_Q4_K;
38 const bool plain_pair = (wu->type == GGML_TYPE_Q5_K || wu->type == GGML_TYPE_IQ4_XS) &&
39 (wg->type == GGML_TYPE_Q5_K || wg->type == GGML_TYPE_IQ4_XS);
40#ifdef GGML_SYCL_MMVQ_HAS_XMX
41 // same-type pairs of the types of the XMX kernels, which also take a few more columns
42 // (the XMX kernels walk whole QK_K super-blocks, so they need rows of a multiple of QK_K)
43 const bool xmx_pair = wu->type == wg->type && ggml_sycl_xmx_supports_type(wu->type) && wu->ne[0] % QK_K == 0;
44#else
45 const bool xmx_pair = false;
46#endif // GGML_SYCL_MMVQ_HAS_XMX
47 // the ESIMD kernel covers same-type q2_K..q6_K and q8_0, single column only (as unfused DMMV)
48#ifdef GGML_SYCL_DMMV_HAS_ESIMD
49 const bool esimd_pair = g_ggml_sycl_enable_esimd && wu->type == wg->type && act->ne[1] == 1 &&
50 (wu->type == GGML_TYPE_Q2_K || wu->type == GGML_TYPE_Q3_K || wu->type == GGML_TYPE_Q4_K ||
51 wu->type == GGML_TYPE_Q5_K || wu->type == GGML_TYPE_Q6_K || wu->type == GGML_TYPE_Q8_0);
52#else
53 const bool esimd_pair = false;
54#endif
55 // q8_0 rows are walked in 32-wide blocks, so they only need to be a multiple of QK8_0
56 const int64_t row_align = esimd_pair && wu->type == GGML_TYPE_Q8_0 ? QK8_0 : QK_K;
57 if ((!reorder_pair && !plain_pair && !xmx_pair && !esimd_pair) || wu->ne[0] % row_align != 0) {
58 return false;
59 }
60
61 // one 2D reorder-layout matrix in, a plain column stride out: no broadcast or padding
62 if (!ggml_is_contiguous(wu) || !ggml_is_contiguous(wg) || !ggml_is_contiguous(act) ||
63 !ggml_is_contiguous(glu)) {
64 return false;
65 }
66 if (act->type != GGML_TYPE_F32 || glu->type != GGML_TYPE_F32) {
67 return false;
68 }
69 if (act->ne[2] != 1 || act->ne[3] != 1 || wu->ne[2] != 1 || wu->ne[3] != 1) {
70 return false;
71 }
72 // the kernel writes rows [0, wu->ne[1]) of each glu column, strided by glu->ne[0]
73 if (glu->ne[0] != wu->ne[1] || glu->ne[1] != act->ne[1]) {
74 return false;
75 }
76 // mat-vec only: one column per decoded token, up to the batch the reorder kernels cover
77 const int max_cols = xmx_pair ? std::max(MMVQ_MAX_BATCH_SIZE, g_ggml_sycl_xmx_glu_max_cols) : MMVQ_MAX_BATCH_SIZE;
78 if (act->ne[1] > max_cols) {
79 return false;
80 }
81
82 return true;
83}
84
85// the unary ops the fused unary chains in element_wise.cpp have a functor for
86static bool ggml_sycl_fused_unary_has_kernel(ggml_unary_op unary_op) {
87 return unary_op == GGML_UNARY_OP_SILU || unary_op == GGML_UNARY_OP_SIGMOID ||
88 unary_op == GGML_UNARY_OP_SOFTPLUS;
89}
90
91bool ggml_sycl_can_fuse(const ggml_cgraph * cgraph, int node_idx, std::initializer_list<enum ggml_op> ops,
92 std::initializer_list<enum ggml_unary_op> unary_ops) {
93#ifndef NDEBUG
94 const size_t num_unary = std::count(ops.begin(), ops.end(), GGML_OP_UNARY);
95 GGML_ASSERT(unary_ops.size() == num_unary);
96#endif
97
98 if (!g_ggml_sycl_enable_fusion) {
99 return false;
100 }
101
102 // gate and up are siblings, not a chain, so ggml_can_fuse cannot express this: use the
103 // subgraph form with the GLU as the only materialised output.
104 if (ops.size() == 3 && ops.begin()[0] == GGML_OP_MUL_MAT && ops.begin()[1] == GGML_OP_MUL_MAT &&
105 ops.begin()[2] == GGML_OP_GLU) {
106 if (!ggml_can_fuse_subgraph(cgraph, node_idx, ops, { node_idx + 2 })) {
107 return false;
108 }
109
110 const ggml_tensor * glu = cgraph->nodes[node_idx + 2];
111 const ggml_tensor * gate = glu->src[0];
112 const ggml_tensor * up = glu->src[1];
113
114 // don't assume which of the two mat-muls is the gate; infer it from the GLU's operands
115 const bool ok = (gate == cgraph->nodes[node_idx] && up == cgraph->nodes[node_idx + 1]) ||
116 (gate == cgraph->nodes[node_idx + 1] && up == cgraph->nodes[node_idx]);
117 if (!ok) {
118 return false;
119 }
120
121 return ggml_sycl_should_fuse_mul_mat_glu(gate, up, glu);
122 }
123
124 if (!ggml_can_fuse(cgraph, node_idx, ops)) {
125 return false;
126 }
127
128 if ((ops.size() == 2 || ops.size() == 3) && ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_MUL) {
129 if (ops.size() == 3 && ops.begin()[2] != GGML_OP_ADD) {
130 return false;
131 }
132
133 const ggml_tensor * rms_norm = cgraph->nodes[node_idx];
134 const ggml_tensor * mul = cgraph->nodes[node_idx + 1];
135 const ggml_tensor * add = ops.size() == 3 ? cgraph->nodes[node_idx + 2] : nullptr;
136
137 GGML_ASSERT(rms_norm->src[0]->type == GGML_TYPE_F32);
138 GGML_ASSERT(rms_norm->type == GGML_TYPE_F32);
139
140 if (mul->src[0]->type != GGML_TYPE_F32 ||
141 mul->src[1]->type != GGML_TYPE_F32 ||
142 mul->type != GGML_TYPE_F32) {
143 return false;
144 }
145
146 // if rms norm is the B operand, then we don't handle broadcast
147 if (rms_norm == mul->src[1] && !ggml_are_same_shape(mul->src[0], rms_norm)) {
148 return false;
149 }
150
151 const ggml_tensor * mul_w = (mul->src[0] == rms_norm) ? mul->src[1] : mul->src[0];
152 // the fused kernel indexes the weight as mul[col], so it must span ncols contiguously
153 if (mul_w->ne[0] != rms_norm->ne[0] || mul_w->nb[0] != ggml_type_size(mul_w->type)) {
154 return false;
155 }
156
157 if (!ggml_is_contiguous_rows(mul->src[0]) || !ggml_is_contiguous_rows(mul->src[1])) {
158 return false;
159 }
160
161 if (add != nullptr) {
162 if (add->src[0]->type != GGML_TYPE_F32 ||
163 add->src[1]->type != GGML_TYPE_F32 ||
164 add->type != GGML_TYPE_F32) {
165 return false;
166 }
167
168 // the fused kernel indexes the residual as add[col] and does not broadcast it
169 const ggml_tensor * add_w = (add->src[0] == mul) ? add->src[1] : add->src[0];
170 if (!ggml_are_same_shape(add_w, add)) {
171 return false;
172 }
173
174 if (!ggml_is_contiguous(add->src[0]) || !ggml_is_contiguous_rows(add->src[1])) {
175 return false;
176 }
177 }
178
179 return true;
180 }
181
182 if (ops.size() == 2 && ops.begin()[0] == GGML_OP_ADD && ops.begin()[1] == GGML_OP_ADD) {
183 const ggml_tensor * add0 = cgraph->nodes[node_idx];
184 const ggml_tensor * add1 = cgraph->nodes[node_idx + 1];
185 // ggml_can_fuse already guarantees add1 consumes add0 and that add0 has a single use.
186 // Keep the CUDA association: the running sum is src0 of the next ADD so the fused
187 // float fold matches two sequential add() launches.
188 if (add1->src[0] != add0) {
189 return false;
190 }
191
192 const ggml_tensor * c = add1->src[1];
193 if (!ggml_sycl_add_kernel_supports(add0->src[0]->type, add0->src[1]->type, add0->type) ||
194 !ggml_sycl_add_kernel_supports(add0->type, c->type, add1->type)) {
195 return false;
196 }
197
198 return true;
199 }
200
201 if (ops.size() == 2 && ops.begin()[0] == GGML_OP_UNARY && ops.begin()[1] == GGML_OP_MUL &&
202 unary_ops.size() == 1) {
203 const ggml_tensor * unary = cgraph->nodes[node_idx];
204 const ggml_tensor * mul = cgraph->nodes[node_idx + 1];
205
206 const ggml_unary_op unary_op = ggml_get_unary_op(unary);
207 if (unary_op != unary_ops.begin()[0]) {
208 return false;
209 }
210
211 if (!ggml_sycl_fused_unary_has_kernel(unary_op)) {
212 return false;
213 }
214
215 if (unary->type != GGML_TYPE_F32 && unary->type != GGML_TYPE_F16) {
216 return false;
217 }
218
219 const ggml_tensor * other = (mul->src[0] == unary) ? mul->src[1] : mul->src[0];
220 if (other->type != unary->type) {
221 return false;
222 }
223
224 // one row stride per source comes from nb[1], so rows must be contiguous and equally
225 // shaped; the destination is written flat, so it must be fully contiguous
226 if (!ggml_is_contiguous_1(unary->src[0]) || !ggml_is_contiguous_1(other) ||
227 !ggml_are_same_shape(other, unary) || !ggml_is_contiguous(mul)) {
228 return false;
229 }
230
231 // the 32-bit fastdiv is inexact past 2^31; decline, the unfused path handles it
232 if (ggml_nelements(mul) >= ((int64_t) 1 << 31)) {
233 return false;
234 }
235
236 return true;
237 }
238
239 if (ops.size() == 2 && ops.begin()[0] == GGML_OP_SSM_CONV && ops.begin()[1] == GGML_OP_UNARY &&
240 unary_ops.size() == 1 && unary_ops.begin()[0] == GGML_UNARY_OP_SILU) {
241 const ggml_tensor * ssm_conv = cgraph->nodes[node_idx];
242 const ggml_tensor * silu = cgraph->nodes[node_idx + 1];
243
244 if (ggml_get_unary_op(silu) != unary_ops.begin()[0]) {
245 return false;
246 }
247 if (ssm_conv->type != GGML_TYPE_F32 || silu->type != GGML_TYPE_F32) {
248 return false;
249 }
250 // the fused kernel writes the SiLU output with dense strides, so it must be contiguous
251 if (!ggml_is_contiguous(silu)) {
252 return false;
253 }
254
255 return true;
256 }
257
258 // ADD(bias) + UNARY + MUL(scale): the delta-net alpha gate, softplus(alpha + dt) * a.
259 // The broadcast is what stops the same-shape UNARY + MUL branch above firing past one token.
260 if (ops.size() == 3 && ops.begin()[0] == GGML_OP_ADD && ops.begin()[1] == GGML_OP_UNARY &&
261 ops.begin()[2] == GGML_OP_MUL && unary_ops.size() == 1) {
262 const ggml_tensor * add = cgraph->nodes[node_idx];
263 const ggml_tensor * unary = cgraph->nodes[node_idx + 1];
264 const ggml_tensor * mul = cgraph->nodes[node_idx + 2];
265
266 const ggml_unary_op unary_op = ggml_get_unary_op(unary);
267 if (unary_op != unary_ops.begin()[0]) {
268 return false;
269 }
270
271 if (!ggml_sycl_fused_unary_has_kernel(unary_op)) {
272 return false;
273 }
274
275 // ggml_can_fuse() has already pinned the chain: unary consumes add, mul consumes
276 // unary, add and unary have one use each, and all three have the same shape
277 const ggml_tensor * a = add->src[0];
278 const ggml_tensor * bias = add->src[1];
279 const ggml_tensor * scale = (mul->src[0] == unary) ? mul->src[1] : mul->src[0];
280
281 if (a->type != GGML_TYPE_F32 || bias->type != GGML_TYPE_F32 ||
282 scale->type != GGML_TYPE_F32 || mul->type != GGML_TYPE_F32) {
283 return false;
284 }
285
286 // the activation and the destination are indexed flat
287 if (!ggml_is_contiguous(a) || !ggml_is_contiguous(mul) || !ggml_are_same_shape(a, mul)) {
288 return false;
289 }
290
291 // the kernel reads the bias and the scale as v[col], so each must be a single
292 // contiguous row spanning ne0
293 if (bias->ne[0] != a->ne[0] || scale->ne[0] != a->ne[0] ||
294 ggml_nrows(bias) != 1 || ggml_nrows(scale) != 1 ||
295 !ggml_is_contiguous(bias) || !ggml_is_contiguous(scale)) {
296 return false;
297 }
298
299 // the 32-bit fastdiv is inexact past 2^31; decline, the unfused path handles it
300 if (ggml_nelements(mul) >= ((int64_t) 1 << 31)) {
301 return false;
302 }
303
304 return true;
305 }
306
307 if (ops.size() == 3 && ops.begin()[0] == GGML_OP_SSM_CONV && ops.begin()[1] == GGML_OP_ADD &&
308 ops.begin()[2] == GGML_OP_UNARY && unary_ops.size() == 1 && unary_ops.begin()[0] == GGML_UNARY_OP_SILU) {
309 const ggml_tensor * ssm_conv = cgraph->nodes[node_idx];
310 const ggml_tensor * add = cgraph->nodes[node_idx + 1];
311 const ggml_tensor * silu = cgraph->nodes[node_idx + 2];
312
313 if (ggml_get_unary_op(silu) != unary_ops.begin()[0]) {
314 return false;
315 }
316 if (ssm_conv->type != GGML_TYPE_F32 || add->type != GGML_TYPE_F32 || silu->type != GGML_TYPE_F32) {
317 return false;
318 }
319 // the fused kernel writes the SiLU output with dense strides, so it must be contiguous
320 if (!ggml_is_contiguous(silu)) {
321 return false;
322 }
323
324 // ADD must consume ssm_conv's output and broadcast a 1-D channel-wise bias
325 const ggml_tensor * bias = (add->src[0] == ssm_conv) ? add->src[1] : add->src[0];
326 if (bias->type != GGML_TYPE_F32 || !ggml_is_contiguous(bias)) {
327 return false;
328 }
329 if (ggml_nelements(bias) != ssm_conv->ne[0] || bias->ne[0] != ssm_conv->ne[0]) {
330 return false;
331 }
332
333 return true;
334 }
335
336 if (ops.size() == 2 && ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_SCALE) {
337 const ggml_tensor * rms_norm = cgraph->nodes[node_idx];
338 const ggml_tensor * scale = cgraph->nodes[node_idx + 1];
339 GGML_ASSERT(rms_norm->src[0]->type == GGML_TYPE_F32);
340 GGML_ASSERT(rms_norm->type == GGML_TYPE_F32);
341 if (scale->src[0]->type != GGML_TYPE_F32 || scale->type != GGML_TYPE_F32) {
342 return false;
343 }
344 // the fused kernel reads/writes rows flat like the unfused pair
345 if (!ggml_is_contiguous_rows(rms_norm) || !ggml_is_contiguous_rows(scale)) {
346 return false;
347 }
348 return true;
349 }
350 return false;
351}