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-metal / kernels / ssm.metal

476 lines · 20441 bytes Raw Blame History
1#include "common.h"
2
3constant bool FC_ssm_conv_silu [[function_constant(FC_SSM_CONV + 1)]];
4constant int FC_ssm_conv_nc [[function_constant(FC_SSM_CONV + 2)]];
5
6// ref: ggml.c:ggml_compute_forward_ssm_conv_f32
7kernel void kernel_ssm_conv_f32_f32(
8 constant ggml_metal_kargs_ssm_conv & args,
9 device const void * src0,
10 device const void * src1,
11 device float * dst,
12 uint3 tgpig[[threadgroup_position_in_grid]],
13 uint3 tpitg[[thread_position_in_threadgroup]],
14 uint3 ntg[[threads_per_threadgroup]]) {
15 const int64_t ir = tgpig.x;
16 const int64_t i2 = tgpig.y;
17 const int64_t i3 = tgpig.z;
18
19 const int64_t nc = FC_ssm_conv_nc;
20 //const int64_t ncs = args.ne00;
21 //const int64_t nr = args.ne01;
22 //const int64_t n_t = args.ne1;
23 //const int64_t n_s = args.ne2;
24
25 device const float * s = (device const float *) ((device const char *) src0 + ir*args.nb01 + i2*args.nb00 + i3*args.nb02);
26 device const float * c = (device const float *) ((device const char *) src1 + ir*args.nb11);
27 device float * x = (device float *) ((device char *) dst + ir*args.nb0 + i2*args.nb1 + i3*args.nb2);
28
29 float sumf = 0.0f;
30
31 FOR_UNROLL (int64_t i0 = 0; i0 < nc; ++i0) {
32 sumf += s[i0] * c[i0];
33 }
34
35 x[0] = FC_ssm_conv_silu ? sumf/(1.0f + exp(-sumf)) : sumf;
36}
37
38kernel void kernel_ssm_conv_f32_f32_4(
39 constant ggml_metal_kargs_ssm_conv & args,
40 device const void * src0,
41 device const void * src1,
42 device float * dst,
43 uint3 tgpig[[threadgroup_position_in_grid]],
44 uint3 tpitg[[thread_position_in_threadgroup]],
45 uint3 ntg[[threads_per_threadgroup]]) {
46 const int64_t ir = tgpig.x;
47 const int64_t i2 = tgpig.y;
48 const int64_t i3 = tgpig.z;
49
50 const int64_t nc = FC_ssm_conv_nc;
51 //const int64_t ncs = args.ne00;
52 //const int64_t nr = args.ne01;
53 //const int64_t n_t = args.ne1;
54 //const int64_t n_s = args.ne2;
55
56 device const float4 * s = (device const float4 *) ((device const char *) src0 + ir*args.nb01 + i2*args.nb00 + i3*args.nb02);
57 device const float4 * c = (device const float4 *) ((device const char *) src1 + ir*args.nb11);
58 device float * x = (device float *) ((device char *) dst + ir*args.nb0 + i2*args.nb1 + i3*args.nb2);
59
60 float sumf = 0.0f;
61
62 FOR_UNROLL (int64_t i0 = 0; i0 < nc/4; ++i0) {
63 sumf += dot(s[i0], c[i0]);
64 }
65
66 x[0] = FC_ssm_conv_silu ? sumf/(1.0f + exp(-sumf)) : sumf;
67}
68
69constant short FC_ssm_conv_bs [[function_constant(FC_SSM_CONV + 0)]];
70
71// Batched version: each threadgroup processes multiple tokens for better efficiency
72// Thread layout: each thread handles one token, threadgroup covers BATCH_SIZE tokens
73kernel void kernel_ssm_conv_f32_f32_batched(
74 constant ggml_metal_kargs_ssm_conv & args,
75 device const void * src0,
76 device const void * src1,
77 device float * dst,
78 uint3 tgpig[[threadgroup_position_in_grid]],
79 uint3 tpitg[[thread_position_in_threadgroup]],
80 uint3 ntg[[threads_per_threadgroup]]) {
81 // tgpig.x = row index (ir)
82 // tgpig.y = batch of tokens (i2_base / BATCH_SIZE)
83 // tgpig.z = sequence index (i3)
84 // tpitg.x = thread within batch (0..BATCH_SIZE-1)
85 const short BATCH_SIZE = FC_ssm_conv_bs;
86
87 const int64_t ir = tgpig.x;
88 const int64_t i2_base = tgpig.y * BATCH_SIZE;
89 const int64_t i3 = tgpig.z;
90 const int64_t i2_off = tpitg.x;
91 const int64_t i2 = i2_base + i2_off;
92
93 const int64_t nc = FC_ssm_conv_nc; // conv kernel size (typically 4)
94 const int64_t n_t = args.ne1; // number of tokens
95
96 // Bounds check for partial batches at the end
97 if (i2 >= n_t) {
98 return;
99 }
100
101 // Load conv weights (shared across all tokens for this row)
102 device const float * c = (device const float *) ((device const char *) src1 + ir*args.nb11);
103
104 // Load source for this specific token
105 device const float * s = (device const float *) ((device const char *) src0 + ir*args.nb01 + i2*args.nb00 + i3*args.nb02);
106
107 // Output location for this token
108 device float * x = (device float *) ((device char *) dst + ir*args.nb0 + i2*args.nb1 + i3*args.nb2);
109
110 float sumf = 0.0f;
111 FOR_UNROLL (int64_t i0 = 0; i0 < nc; ++i0) {
112 sumf += s[i0] * c[i0];
113 }
114
115 x[0] = FC_ssm_conv_silu ? sumf/(1.0f + exp(-sumf)) : sumf;
116}
117
118kernel void kernel_ssm_conv_f32_f32_batched_4(
119 constant ggml_metal_kargs_ssm_conv & args,
120 device const void * src0,
121 device const void * src1,
122 device float * dst,
123 uint3 tgpig[[threadgroup_position_in_grid]],
124 uint3 tpitg[[thread_position_in_threadgroup]],
125 uint3 ntg[[threads_per_threadgroup]]) {
126 // tgpig.x = row index (ir)
127 // tgpig.y = batch of tokens (i2_base / BATCH_SIZE)
128 // tgpig.z = sequence index (i3)
129 // tpitg.x = thread within batch (0..BATCH_SIZE-1)
130 const short BATCH_SIZE = FC_ssm_conv_bs;
131
132 const int64_t ir = tgpig.x;
133 const int64_t i2_base = tgpig.y * BATCH_SIZE;
134 const int64_t i3 = tgpig.z;
135 const int64_t i2_off = tpitg.x;
136 const int64_t i2 = i2_base + i2_off;
137
138 const int64_t nc = FC_ssm_conv_nc; // conv kernel size (typically 4)
139 const int64_t n_t = args.ne1; // number of tokens
140
141 // Bounds check for partial batches at the end
142 if (i2 >= n_t) {
143 return;
144 }
145
146 // Load conv weights (shared across all tokens for this row)
147 device const float4 * c = (device const float4 *) ((device const char *) src1 + ir*args.nb11);
148
149 // Load source for this specific token
150 device const float4 * s = (device const float4 *) ((device const char *) src0 + ir*args.nb01 + i2*args.nb00 + i3*args.nb02);
151
152 // Output location for this token
153 device float * x = (device float *) ((device char *) dst + ir*args.nb0 + i2*args.nb1 + i3*args.nb2);
154
155 float sumf = 0.0f;
156 FOR_UNROLL (int64_t i0 = 0; i0 < nc/4; ++i0) {
157 sumf += dot(s[i0], c[i0]);
158 }
159
160 x[0] = FC_ssm_conv_silu ? sumf/(1.0f + exp(-sumf)) : sumf;
161}
162
163// ref: ggml.c:ggml_compute_forward_ssm_scan_f32, Mamba-2 part
164// Optimized version: reduces redundant memory loads by having one thread load shared values
165// TAIL == false is the whole-sequence / decode path: token_offset folds away at compile time.
166template<bool TAIL>
167kernel void kernel_ssm_scan_impl(
168 constant ggml_metal_kargs_ssm_scan & args,
169 device const void * src0,
170 device const void * src1,
171 device const void * src2,
172 device const void * src3,
173 device const void * src4,
174 device const void * src5,
175 device const void * src6,
176 device float * dst,
177 threadgroup float * shared [[threadgroup(0)]],
178 uint3 tgpig[[threadgroup_position_in_grid]],
179 ushort3 tpitg[[thread_position_in_threadgroup]],
180 ushort sgitg[[simdgroup_index_in_threadgroup]],
181 ushort tiisg[[thread_index_in_simdgroup]],
182 ushort sgptg[[simdgroups_per_threadgroup]],
183 uint3 tgpg[[threadgroups_per_grid]]) {
184 constexpr short NW = N_SIMDWIDTH;
185
186 // Shared memory layout:
187 // [0..sgptg*NW-1]: partial sums for reduction (existing)
188 // [sgptg*NW..sgptg*NW+sgptg-1]: pre-computed x_dt values for each token in batch
189 // [sgptg*NW+sgptg..sgptg*NW+2*sgptg-1]: pre-computed dA values for each token in batch
190 threadgroup float * shared_sums = shared;
191 threadgroup float * shared_x_dt = shared + sgptg * NW;
192 threadgroup float * shared_dA = shared + sgptg * NW + sgptg;
193
194 shared_sums[tpitg.x] = 0.0f;
195
196 const int32_t i0 = tpitg.x;
197 const int32_t i1 = tgpig.x;
198 const int32_t ir = tgpig.y; // current head
199 const int32_t i3 = tgpig.z; // current seq
200
201 const int32_t nc = args.d_state;
202 const int32_t nr = args.d_inner;
203 const int32_t nh = args.n_head;
204 const int32_t ng = args.n_group;
205 const int32_t n_t = args.n_seq_tokens;
206 const int32_t n_s = args.n_seqs;
207 const int32_t K = args.K;
208 const int32_t n_t_total = TAIL ? args.n_seq_tokens_total : n_t;
209 const int32_t t_off = TAIL ? args.token_offset : 0;
210
211 const int32_t s_off = args.s_off;
212
213 device const int32_t * ids = (device const int32_t *) src6;
214
215 device float * s_buff = (device float *) ((device char *) dst + ir*args.nb02 + i3*args.nb03 + s_off);
216 device const float * s0_buff = t_off != 0 ?
217 s_buff :
218 (device const float *) ((device const char *) src0 + ir*args.nb02 + ids[i3]*args.nb03);
219
220 const int32_t i = i0 + i1*nc;
221 const int32_t g = ir / (nh / ng); // repeat_interleave
222
223 float s0 = s0_buff[i];
224 float s = 0.0f;
225
226 device const float * A = (device const float *) ((device const char *) src3 + ir*args.nb31); // {ne30, nh}
227
228 const float A0 = A[i0%args.ne30];
229
230 device const float * x = (device const float *)((device const char *) src1 + i1*args.nb10 + ir*args.nb11 + t_off*args.nb12 + i3*args.nb13); // {dim, nh, nt, ns}
231 device const float * dt = (device const float *)((device const char *) src2 + ir*args.nb20 + t_off*args.nb21 + i3*args.nb22); // {nh, nt, ns}
232 device const float * B = (device const float *)((device const char *) src4 + g*args.nb41 + t_off*args.nb42 + i3*args.nb43); // {d_state, ng, nt, ns}
233 device const float * C = (device const float *)((device const char *) src5 + g*args.nb51 + t_off*args.nb52 + i3*args.nb53); // {d_state, ng, nt, ns}
234
235 device float * y = dst + (i1 + ir*nr + t_off*nh*nr + i3*(n_t_total*nh*nr)); // {dim, nh, nt, ns}
236
237 for (int i2 = 0; i2 < n_t; i2 += sgptg) {
238 threadgroup_barrier(mem_flags::mem_threadgroup);
239
240 // Pre-compute x_dt and dA for this batch of tokens
241 // Only first sgptg threads do the loads and expensive math
242 if (i0 < sgptg && i2 + i0 < n_t) {
243 // ns12 and ns21 are element strides (nb12/nb10, nb21/nb20)
244 device const float * x_t = x + i0 * args.ns12;
245 device const float * dt_t = dt + i0 * args.ns21;
246
247 const float dt0 = dt_t[0];
248 const float dtsp = dt0 <= 20.0f ? log(1.0f + exp(dt0)) : dt0;
249 shared_x_dt[i0] = x_t[0] * dtsp;
250 shared_dA[i0] = dtsp; // Store dtsp, compute exp(dtsp * A0) per-thread since A0 varies
251 }
252
253 threadgroup_barrier(mem_flags::mem_threadgroup);
254
255 for (int t = 0; t < sgptg && i2 + t < n_t; t++) {
256 const float x_dt = shared_x_dt[t];
257 const float dA = exp(shared_dA[t] * A0);
258
259 s = (s0 * dA) + (B[i0] * x_dt);
260
261 const float sumf = simd_sum(s * C[i0]);
262
263 if (tiisg == 0) {
264 shared_sums[t*NW + sgitg] = sumf;
265 }
266
267 // recurse
268 s0 = s;
269
270 const int32_t slot = n_t - 1 - (i2 + t);
271 if (slot > 0 && slot < K) {
272 device float * s_snapshot = (device float *) ((device char *) s_buff + (int64_t) slot*n_s*args.nb03);
273 s_snapshot[i] = s;
274 }
275
276 B += args.ns42;
277 C += args.ns52;
278 }
279
280 // Advance pointers for next batch
281 x += sgptg * args.ns12;
282 dt += sgptg * args.ns21;
283
284 threadgroup_barrier(mem_flags::mem_threadgroup);
285
286 const float sumf = simd_sum(shared_sums[sgitg*NW + tiisg]);
287
288 if (tiisg == 0 && i2 + sgitg < n_t) {
289 y[sgitg*nh*nr] = sumf;
290 }
291
292 y += sgptg*nh*nr;
293 }
294
295 s_buff[i] = s;
296}
297
298typedef decltype(kernel_ssm_scan_impl<false>) kernel_ssm_scan_t;
299
300template [[host_name("kernel_ssm_scan_f32")]] kernel kernel_ssm_scan_t kernel_ssm_scan_impl<false>;
301template [[host_name("kernel_ssm_scan_f32_tail")]] kernel kernel_ssm_scan_t kernel_ssm_scan_impl<true>;
302
303// Chunked SSD SSM scan via Metal simdgroup MMatrix Multiply-Accumulate (simdgroup_float8x8) fast path.
304// One threadgroup per (head, sequence) and tokens are processed in chunks.
305// C*B^T computed in each chunk one time and reused across the head_dim channel tiles.
306kernel void kernel_ssm_scan_ssd_mma_f32(
307 constant ggml_metal_kargs_ssm_scan & args,
308 device const void * src0,
309 device const void * src1,
310 device const void * src2,
311 device const void * src3,
312 device const void * src4,
313 device const void * src5,
314 device const void * src6,
315 device float * dst,
316 threadgroup float * shared [[threadgroup(0)]],
317 uint3 tgpig[[threadgroup_position_in_grid]],
318 ushort tiitg[[thread_index_in_threadgroup]],
319 ushort sgitg[[simdgroup_index_in_threadgroup]],
320 ushort tiisg[[thread_index_in_simdgroup]]) {
321 constexpr short CS = OP_SSM_SCAN_SSD_CS;
322 constexpr short TC = 8; // Tile Count of each edge in a simdgroup 8x8 tile
323 constexpr short HD = OP_SSM_SCAN_SSD_HD;
324 constexpr short NSG = OP_SSM_SCAN_SSD_NSG;
325
326 // acs/exp(acs)/state-decay vectors, dtX[CS][HD], four private SAM row tiles [8][CS],
327 // and two 8x8 scratch tiles per simdgroup. Total: 26.75 KiB.
328 threadgroup float * shared_acs = shared;
329 threadgroup float * shared_exp_acs = shared + CS;
330 threadgroup float * shared_state_decay = shared + 2*CS;
331 threadgroup float * shared_dtx = shared + 3*CS;
332 threadgroup float * shared_sam = shared + 3*CS + CS*HD;
333 threadgroup float * sam_rows = shared_sam + sgitg*TC*CS;
334 threadgroup float * shared_tile = shared_sam + NSG*TC*CS;
335 threadgroup float * tile0 = shared_tile + sgitg*2*TC*TC;
336 threadgroup float * tile1 = tile0 + TC*TC;
337
338 const int32_t ir = tgpig.y; // current head
339 const int32_t i3 = tgpig.z; // current seq
340
341 const int32_t nc = args.d_state;
342 const int32_t nr = args.d_inner;
343 const int32_t nh = args.n_head;
344 const int32_t ng = args.n_group;
345 const int32_t n_t = args.n_seq_tokens;
346 const int32_t n_t_total = args.n_seq_tokens_total;
347 const int32_t g = ir / (nh / ng);
348
349 device const int32_t * ids = (device const int32_t *) src6;
350
351 device const float * s0_buff = (device const float *) ((device const char *) src0 + ir*args.nb02 + ids[i3]*args.nb03);
352 device float * s_buff = (device float *) ((device char *) dst + ir*args.nb02 + i3*args.nb03 + args.s_off);
353
354 device const float * A = (device const float *) ((device const char *) src3 + ir*args.nb31);
355 device const float * x = (device const float *) ((device const char *) src1 + ir*args.nb11 + i3*args.nb13);
356 device const float * dt = (device const float *) ((device const char *) src2 + ir*args.nb20 + i3*args.nb22);
357 device const float * B = (device const float *) ((device const char *) src4 + g*args.nb41 + i3*args.nb43);
358 device const float * C = (device const float *) ((device const char *) src5 + g*args.nb51 + i3*args.nb53);
359
360 device float * y = dst + (ir*nr + i3*(n_t_total*nh*nr));
361
362 for (int32_t t0 = 0; t0 < n_t; t0 += CS) {
363 for (int32_t idx = tiitg; idx < CS*HD; idx += NSG*N_SIMDWIDTH) {
364 const int32_t t = idx / HD;
365 const int32_t c = idx % HD;
366 const float dt0 = dt[(t0 + t) * (int32_t) args.ns21];
367 const float dtsp = dt0 <= 20.0f ? log(1.0f + exp(dt0)) : dt0;
368 shared_dtx[idx] = x[(t0 + t) * (int32_t) args.ns12 + c] * dtsp;
369 }
370 if (tiitg < CS) {
371 const float dt0 = dt[(t0 + tiitg) * (int32_t) args.ns21];
372 const float dtsp = dt0 <= 20.0f ? log(1.0f + exp(dt0)) : dt0;
373 shared_acs[tiitg] = dtsp * A[0];
374 }
375 threadgroup_barrier(mem_flags::mem_threadgroup);
376
377 if (tiitg == 0) {
378 float acc = 0.0f;
379 for (short t = 0; t < CS; ++t) {
380 acc += shared_acs[t];
381 shared_acs[t] = acc;
382 }
383 }
384 threadgroup_barrier(mem_flags::mem_threadgroup);
385 if (tiitg < CS) {
386 shared_exp_acs[tiitg] = exp(shared_acs[tiitg]);
387 shared_state_decay[tiitg] = exp(shared_acs[CS - 1] - shared_acs[tiitg]);
388 }
389 threadgroup_barrier(mem_flags::mem_threadgroup);
390
391 device const float * state = t0 == 0 ? s0_buff : s_buff;
392
393 // Build one 8x64 row tile of SAM per simdgroup, then reuse it across every channel tile.
394 for (short ib = sgitg; ib < CS/TC; ib += NSG) {
395 for (short jb = 0; jb <= ib; ++jb) {
396 simdgroup_float8x8 cb = make_filled_simdgroup_matrix<float, 8>(0.0f);
397
398 for (int32_t k0 = 0; k0 < nc; k0 += TC) {
399 simdgroup_float8x8 mc;
400 simdgroup_float8x8 mb;
401 simdgroup_load(mc, C + (t0 + ib*TC)*(int32_t) args.ns52 + k0, args.ns52);
402 simdgroup_load(mb, B + (t0 + jb*TC)*(int32_t) args.ns42 + k0, args.ns42, 0, true);
403 simdgroup_multiply_accumulate(cb, mc, mb, cb);
404 }
405
406 threadgroup float * sam = sam_rows + jb*TC;
407 simdgroup_store(cb, sam, CS);
408 simdgroup_barrier(mem_flags::mem_threadgroup);
409 for (short e = tiisg; e < TC*TC; e += N_SIMDWIDTH) {
410 const short ri = e / TC;
411 const short rj = e % TC;
412 const short i = ib*TC + ri;
413 const short j = jb*TC + rj;
414 sam[ri*CS + rj] = j <= i ?
415 sam[ri*CS + rj] * exp(shared_acs[i] - shared_acs[j]) : 0.0f;
416 }
417 simdgroup_barrier(mem_flags::mem_threadgroup);
418 }
419
420 for (short ch = 0; ch < HD/TC; ++ch) {
421 simdgroup_float8x8 y_diag = make_filled_simdgroup_matrix<float, 8>(0.0f);
422 simdgroup_float8x8 y_inter = make_filled_simdgroup_matrix<float, 8>(0.0f);
423
424 for (short jb = 0; jb <= ib; ++jb) {
425 simdgroup_float8x8 sam;
426 simdgroup_float8x8 mdtx;
427 simdgroup_load(sam, sam_rows + jb*TC, CS);
428 simdgroup_load(mdtx, shared_dtx + jb*TC*HD + ch*TC, HD);
429 simdgroup_multiply_accumulate(y_diag, sam, mdtx, y_diag);
430 }
431
432 for (int32_t k0 = 0; k0 < nc; k0 += TC) {
433 simdgroup_float8x8 mc;
434 simdgroup_float8x8 ms;
435 simdgroup_load(mc, C + (t0 + ib*TC)*(int32_t) args.ns52 + k0, args.ns52);
436 simdgroup_load(ms, state + ch*TC*nc + k0, nc, 0, true);
437 simdgroup_multiply_accumulate(y_inter, mc, ms, y_inter);
438 }
439
440 simdgroup_store(y_diag, tile0, TC);
441 simdgroup_store(y_inter, tile1, TC);
442 simdgroup_barrier(mem_flags::mem_threadgroup);
443 for (short e = tiisg; e < TC*TC; e += N_SIMDWIDTH) {
444 const short ri = e / TC;
445 const short ci = e % TC;
446 const int32_t token = t0 + ib*TC + ri;
447 y[token*nh*nr + ch*TC + ci] =
448 tile0[e] + shared_exp_acs[ib*TC + ri] * tile1[e];
449 }
450 simdgroup_barrier(mem_flags::mem_threadgroup);
451 }
452 }
453
454 // All simdgroups must finish reading s_buff before any thread overwrites it.
455 threadgroup_barrier(mem_flags::mem_device | mem_flags::mem_threadgroup);
456
457 // Keep the carried-state reduction in token order. Reassociating this particular product
458 // with MMA compounds rounding differences at every chunk boundary; CB, y_diag, and C*S
459 // remain on the matrix unit.
460 const float chunk_decay = exp(shared_acs[CS - 1]);
461 for (int32_t idx = tiitg; idx < nc*HD; idx += NSG*N_SIMDWIDTH) {
462 const int32_t ci = idx / nc;
463 const int32_t si = idx % nc;
464 float state_c = 0.0f;
465 for (short t = 0; t < CS; ++t) {
466 state_c += shared_state_decay[t] *
467 B[(t0 + t)*(int32_t) args.ns42 + si] *
468 shared_dtx[t*HD + ci];
469 }
470 s_buff[idx] = chunk_decay * state[idx] + state_c;
471 }
472
473 // All state tiles must be visible before the next chunk consumes s_buff as S_prev.
474 threadgroup_barrier(mem_flags::mem_device | mem_flags::mem_threadgroup);
475 }
476}