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 / mul_mm.metal

1017 lines · 58613 bytes Raw Blame History
1#include "common.h"
2#include "dequantize.h"
3
4constant bool FC_mul_mm_bc_inp [[function_constant(FC_MUL_MM + 0)]];
5constant bool FC_mul_mm_bc_out [[function_constant(FC_MUL_MM + 1)]];
6constant short FC_mul_mm_ne12 [[function_constant(FC_MUL_MM + 2)]];
7constant short FC_mul_mm_ne13 [[function_constant(FC_MUL_MM + 3)]];
8constant short FC_mul_mm_r2 [[function_constant(FC_MUL_MM + 4)]];
9constant short FC_mul_mm_r3 [[function_constant(FC_MUL_MM + 5)]];
10constant int32_t FC_mul_mm_id_map0_ne02 [[function_constant(FC_MUL_MM + 7)]];
11
12// each block_q contains 16*nl weights
13#ifdef GGML_METAL_HAS_TENSOR
14template<
15 typename SA, typename SA_4x4, typename SA_8x8,
16 typename SB, typename SB_2x4, typename SB_8x8,
17 typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread SA_4x4 &),
18 typename T0, typename T0_4x4, typename T1, typename T1_2x4>
19kernel void kernel_mul_mm(
20 constant ggml_metal_kargs_mul_mm & args,
21 device const char * srcA,
22 device const char * srcB,
23 device char * dst,
24 threadgroup char * shmem [[threadgroup(0)]],
25 uint3 tgpig [[threadgroup_position_in_grid]],
26 ushort tiitg [[thread_index_in_threadgroup]],
27 ushort sgitg [[simdgroup_index_in_threadgroup]]) {
28 (void) sgitg;
29
30 // Matrix dimensions: A(M,K) x B(K,N) -> C(M,N)
31 const int K = args.ne00;
32 const int M = args.ne0;
33 const int N = args.ne1;
34
35 // Batch dimension handling
36 const int im = tgpig.z;
37 const int i12 = im % FC_mul_mm_ne12;
38 const int i13 = im / FC_mul_mm_ne12;
39
40 // Batch offsets for srcA and srcB
41 const uint64_t offset0 = (i12/FC_mul_mm_r2)*args.nb02 + (i13/FC_mul_mm_r3)*args.nb03;
42
43 // Tile dimensions
44 constexpr int NRB = SZ_SIMDGROUP * N_MM_BLOCK_X * N_MM_SIMD_GROUP_X;
45 constexpr int NRA = SZ_SIMDGROUP * N_MM_BLOCK_Y * N_MM_SIMD_GROUP_Y;
46
47 // Tile offsets in output matrix
48 const int ra = tgpig.y * NRA;
49 const int rb = tgpig.x * NRB;
50
51 // Threadgroup memory for dequantized A tile only
52 threadgroup SA * sa = (threadgroup SA *)(shmem);
53
54 // Work-item count for A loading
55 constexpr int A_WORK_ITEMS = NRA * N_MM_NK;
56 constexpr int NUM_THREADS = N_SIMDWIDTH * N_MM_SIMD_GROUP_X * N_MM_SIMD_GROUP_Y;
57
58 // tA wraps threadgroup memory
59 auto tA = tensor(sa, dextents<int32_t, 2>(N_MM_NK_TOTAL, NRA));
60
61 // tB wraps device memory directly
62 device T1 * ptrB = (device T1 *)(srcB + args.nb12*i12 + args.nb13*i13);
63 const int strideB = args.nb11 / sizeof(T1);
64 auto tB = tensor(ptrB, dextents<int32_t, 2>(K, N), array<int, 2>({1, strideB}));
65
66 // Configure matmul operation
67 // note: K is dynamic_extent (clamped to the valid range in PHASE 2), since a static
68 // N_MM_NK_TOTAL K tile would read src1 out of bounds when K % N_MM_NK_TOTAL != 0
69 // ref: https://github.com/ggml-org/llama.cpp/pull/27064
70 mpp::tensor_ops::matmul2d<
71 mpp::tensor_ops::matmul2d_descriptor(
72 NRB, NRA, static_cast<int>(dynamic_extent), false, true, true,
73 mpp::tensor_ops::matmul2d_descriptor::mode::multiply_accumulate),
74 execution_simdgroups<N_MM_SIMD_GROUP_X * N_MM_SIMD_GROUP_Y>> mm;
75
76 auto cT = mm.get_destination_cooperative_tensor<decltype(tB), decltype(tA), float>();
77
78 // Accumulate partial results over K dimension
79 for (int loop_k = 0; loop_k < K; loop_k += N_MM_NK_TOTAL) {
80 // === PHASE 1: Dequantization of A into threadgroup memory ===
81 for (int work = tiitg; work < A_WORK_ITEMS; work += NUM_THREADS) {
82 const int row = work / N_MM_NK;
83 const int k_chunk = work % N_MM_NK;
84 const int k_pos = loop_k + k_chunk * 16;
85 const short k_base = k_chunk * 16;
86
87 // Bounds check: skip device read if row is out of matrix bounds
88 if (ra + row < M) {
89 if (is_same<T0_4x4, block_q>::value && FC_mul_mm_bc_inp) {
90 // Element-wise reads when K is not aligned (nb01 not aligned for half4x4/float4x4).
91 // MSL spec Table 2.5: half4x4 requires 8-byte alignment. When K is odd,
92 // nb01 = K*2 is not 8-byte aligned, so odd-row pointers are misaligned.
93 // Mirrors the legacy kernel's existing guard.
94 device const T0 * row_ptr = (device const T0 *)(srcA + args.nb01 * (ra + row) + offset0);
95
96 FOR_UNROLL (short i = 0; i < 16; i++) {
97 sa[row * N_MM_NK_TOTAL + (k_base + i)] = (k_pos + i < K) ? (SA) row_ptr[k_pos + i] : (SA)0;
98 }
99 } else {
100 const int block_idx = k_pos / (16 * nl);
101 const short il = (k_pos / 16) % nl;
102
103 device const block_q * row_ptr = (device const block_q *)(srcA + args.nb01 * (ra + row) + offset0);
104
105 SA_4x4 temp_a;
106 dequantize_func(row_ptr + block_idx, il, temp_a);
107
108 FOR_UNROLL (short i = 0; i < 16; i++) {
109 // Zero-pad A for K positions beyond valid range (handles partial K iterations)
110 sa[row * N_MM_NK_TOTAL + (k_base + i)] = (k_pos + i < K) ? temp_a[i/4][i%4] : (SA)0;
111 }
112 }
113 } else {
114 // Zero-pad rows beyond matrix bounds
115 FOR_UNROLL (short i = 0; i < 16; i++) {
116 sa[row * N_MM_NK_TOTAL + (k_base + i)] = (SA)0;
117 }
118 }
119 }
120
121 threadgroup_barrier(mem_flags::mem_threadgroup);
122
123 // === PHASE 2: Tensor matmul ===
124 // Clamp the K extent of both operand tensors to the remaining valid K range so
125 // the dynamic-K op never reads past the K extent of src1 (or the staged A tile).
126 const int kExt = min(N_MM_NK_TOTAL, K - loop_k);
127
128 auto tAv = tensor(sa, dextents<int32_t, 2>(kExt, NRA), array<int, 2>({1, N_MM_NK_TOTAL}));
129 auto tBv = tensor(ptrB + loop_k + rb * strideB, dextents<int32_t, 2>(kExt, N - rb), array<int, 2>({1, strideB}));
130
131 mm.run(tBv, tAv, cT);
132
133 threadgroup_barrier(mem_flags::mem_threadgroup);
134 }
135
136 // Store result tile to output matrix (with batch offset)
137 // a slice is not used because its byte offset is int32 and wraps at 2 GiB
138 // ref: https://github.com/ggml-org/llama.cpp/pull/28748
139 const int tileM = min(NRA, M - ra);
140 const int tileN = min(NRB, N - rb);
141
142 device float * dstTile = (device float *)dst + (uint64_t) im * N * M + (uint64_t) rb * M + ra;
143
144 auto tD = tensor(dstTile, dextents<int32_t, 2>(tileM, tileN), array<int, 2>({1, M}));
145 cT.store(tD);
146}
147
148#else
149
150template<
151 typename S0, typename S0_4x4, typename S0_8x8,
152 typename S1, typename S1_2x4, typename S1_8x8,
153 typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread S0_4x4 &),
154 typename T0, typename T0_4x4, typename T1, typename T1_2x4>
155kernel void kernel_mul_mm(
156 constant ggml_metal_kargs_mul_mm & args,
157 device const char * src0,
158 device const char * src1,
159 device char * dst,
160 threadgroup char * shmem [[threadgroup(0)]],
161 uint3 tgpig[[threadgroup_position_in_grid]],
162 ushort tiitg[[thread_index_in_threadgroup]],
163 ushort sgitg[[simdgroup_index_in_threadgroup]]) {
164
165 threadgroup S0 * sa = (threadgroup S0 *)(shmem);
166 threadgroup S1 * sb = (threadgroup S1 *)(shmem + 4096);
167
168 constexpr int NR0 = 64;
169 constexpr int NR1 = 32;
170
171 constexpr int NK = 32;
172 constexpr int NL0 = NK/16;
173 constexpr int NL1 = NK/8;
174
175 const int im = tgpig.z;
176 const int r0 = tgpig.y*NR0;
177 const int r1 = tgpig.x*NR1;
178
179 // if this block is of 64x32 shape or smaller
180 const short nr0 = (args.ne0 - r0 < NR0) ? (args.ne0 - r0) : NR0;
181 const short nr1 = (args.ne1 - r1 < NR1) ? (args.ne1 - r1) : NR1;
182
183 // a thread shouldn't load data outside of the matrix
184 const short lr0 = ((short)tiitg/NL0) < nr0 ? ((short)tiitg/NL0) : nr0 - 1; // 0 .. 63
185 const short lr1 = ((short)tiitg/NL1) < nr1 ? ((short)tiitg/NL1) : nr1 - 1; // 0 .. 31
186
187 const short il0 = (tiitg % NL0);
188
189 short il = il0;
190
191 const int i12 = im % FC_mul_mm_ne12;
192 const int i13 = im / FC_mul_mm_ne12;
193
194 const uint64_t offset0 = (i12/FC_mul_mm_r2)*args.nb02 + (i13/FC_mul_mm_r3)*args.nb03;
195 const short offset1 = il0/nl;
196
197 device const block_q * x = (device const block_q *)(src0 + args.nb01*(r0 + lr0) + offset0) + offset1;
198
199 const short iy = 8*(tiitg % NL1);
200
201 device const T1 * y = (device const T1 *)(src1
202 + args.nb13*i13
203 + args.nb12*i12
204 + args.nb11*(r1 + lr1)
205 + args.nb10*iy);
206
207 S0_8x8 ma[4];
208 S1_8x8 mb[2];
209
210 simdgroup_float8x8 mc[8];
211
212 for (short i = 0; i < 8; i++){
213 mc[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
214 }
215
216 for (int loop_k = 0; loop_k < args.ne00; loop_k += NK) {
217 // load data and store to threadgroup memory
218 if (is_same<T0_4x4, block_q>::value && FC_mul_mm_bc_inp) {
219 threadgroup_barrier(mem_flags::mem_threadgroup);
220
221 // no need for dequantization
222 for (short i = 0; i < 16; i++) {
223 const short sx = 2*il0 + i/8;
224 const short sy = (tiitg/NL0)/8;
225
226 //const short lx = i%8;
227 //const short ly = (tiitg/NL0)%8;
228 const short lx = (tiitg/NL0)%8;
229 const short ly = i%8;
230
231 const short ib = 8*sx + sy;
232
233 *(sa + 64*ib + 8*ly + lx) = loop_k + 16*il + i < args.ne00 ? (S0) *((device T0 *) x + i) : 0;
234 }
235 } else {
236 S0_4x4 temp_a;
237 dequantize_func(x, il, temp_a);
238
239 threadgroup_barrier(mem_flags::mem_threadgroup);
240
241 FOR_UNROLL (short i = 0; i < 16; i++) {
242 const short sx = 2*il0 + i/8;
243 const short sy = (tiitg/NL0)/8;
244
245 //const short lx = i%8;
246 //const short ly = (tiitg/NL0)%8;
247 const short lx = (tiitg/NL0)%8;
248 const short ly = i%8;
249
250 const short ib = 8*sx + sy;
251
252 // NOTE: this is massively slower.. WTF?
253 //sa[64*ib + 8*ly + lx] = temp_a[i/4][i%4];
254
255 *(sa + 64*ib + 8*ly + lx) = temp_a[i/4][i%4];
256 }
257 }
258
259 if (FC_mul_mm_bc_inp) {
260 for (short i = 0; i < 8; ++i) {
261 const short sx = (tiitg%NL1);
262 const short sy = (tiitg/NL1)/8;
263
264 const short lx = i;
265 const short ly = (tiitg/NL1)%8;
266 //const short lx = (tiitg/NL1)%8;
267 //const short ly = i;
268
269 const short ib = 4*sx + sy;
270
271 *(sb + 64*ib + 8*ly + lx) = loop_k + iy + i < args.ne00 ? (S1) *((device T1 *) y + i) : 0;
272 }
273 } else {
274 const short sx = (tiitg%NL1);
275 const short sy = (tiitg/NL1)/8;
276
277 //const short dx = sx;
278 //const short dy = sy;
279
280 const short ly = (tiitg/NL1)%8;
281
282 const short ib = 4*sx + sy;
283
284 *(threadgroup S1_2x4 *)(sb + 64*ib + 8*ly) = (S1_2x4)(*((device T1_2x4 *) y));
285 }
286
287 il = (il + 2 < nl) ? il + 2 : il % 2;
288 x = (il < 2) ? x + (2 + nl - 1)/nl : x;
289
290 y += NK;
291
292 threadgroup_barrier(mem_flags::mem_threadgroup);
293
294 // load matrices from threadgroup memory and conduct outer products
295 threadgroup const S0 * lsma = (sa + 4*64*(sgitg%2));
296 threadgroup const S1 * lsmb = (sb + 2*64*(sgitg/2));
297
298 FOR_UNROLL (short ik = 0; ik < NK/8; ik++) {
299 simdgroup_barrier(mem_flags::mem_none);
300
301 FOR_UNROLL (short i = 0; i < 4; i++) {
302 simdgroup_load(ma[i], lsma + 64*i, 8, 0, false);
303 }
304
305 simdgroup_barrier(mem_flags::mem_none);
306
307 FOR_UNROLL (short i = 0; i < 2; i++) {
308 simdgroup_load(mb[i], lsmb + 64*i, 8, 0, false);
309 }
310
311 simdgroup_barrier(mem_flags::mem_none);
312
313 FOR_UNROLL (short i = 0; i < 8; i++){
314 simdgroup_multiply_accumulate(mc[i], mb[i/4], ma[i%4], mc[i]);
315 }
316
317 lsma += 8*64;
318 lsmb += 4*64;
319 }
320 }
321
322 if (!FC_mul_mm_bc_out || (r0 + NR0 <= args.ne0 && r1 + NR1 <= args.ne1)) {
323 // if no bounds checks on the output are needed, we can directly write to device memory
324 device float * C = (device float *) dst +
325 (r0 + 32*(sgitg & 1)) + \
326 (uint64_t) (r1 + 16*(sgitg >> 1))*args.ne0 + (uint64_t) im*args.ne1*args.ne0;
327
328 for (short i = 0; i < 8; i++) {
329 simdgroup_store(mc[i], C + 8*(i%4) + (uint64_t) 8*args.ne0*(i/4), args.ne0, 0, false);
330 }
331 } else {
332 // block is smaller than 64x32, we should avoid writing data outside of the matrix
333 threadgroup_barrier(mem_flags::mem_threadgroup);
334
335 threadgroup float * temp_str = ((threadgroup float *) shmem) + 32*(sgitg&1) + (16*(sgitg >> 1))*NR0;
336
337 for (short i = 0; i < 8; i++) {
338 simdgroup_store(mc[i], temp_str + 8*(i%4) + 8*NR0*(i/4), NR0, 0, false);
339 }
340
341 threadgroup_barrier(mem_flags::mem_threadgroup);
342
343 if (sgitg == 0) {
344 for (int j = tiitg; j < nr1; j += NR1) {
345 device float * D = (device float *) dst + r0 + (uint64_t) (r1 + j)*args.ne0 + (uint64_t) im*args.ne1*args.ne0;
346 device float4 * D4 = (device float4 *) D;
347
348 threadgroup float * C = temp_str + (j*NR0);
349 threadgroup float4 * C4 = (threadgroup float4 *) C;
350
351 int i = 0;
352 for (; i < nr0/4; i++) {
353 *(D4 + i) = *(C4 + i);
354 }
355
356 i *= 4;
357 for (; i < nr0; i++) {
358 *(D + i) = *(C + i);
359 }
360 }
361 }
362 }
363}
364
365#endif // GGML_METAL_HAS_TENSOR
366
367template<short ne20> // n_expert_used
368kernel void kernel_mul_mm_id_map0(
369 constant ggml_metal_kargs_mul_mm_id_map0 & args,
370 device const char * src2,
371 device char * htpe,
372 device char * hids,
373 threadgroup char * shmem [[threadgroup(0)]],
374 ushort tpitg[[thread_position_in_threadgroup]],
375 ushort ntg[[threads_per_threadgroup]]) {
376 const short ide = tpitg; // expert id
377
378 // per expert: entry count (pass 1), replaced by the base offset of the slice (pass 2)
379 threadgroup uint32_t * cnt = (threadgroup uint32_t *) (shmem);
380 threadgroup uint32_t * bas = cnt + FC_mul_mm_id_map0_ne02;
381
382 // staging of the ids of ntg tokens
383 threadgroup uint16_t * sids = (threadgroup uint16_t *) (bas + FC_mul_mm_id_map0_ne02);
384
385 // pass 1: count the entries of this expert - every occurrence of this expert within the
386 // ids is an entry (duplicate ids produce multiple entries for the same token)
387 uint32_t n_all = 0;
388
389 for (int i21 = 0; i21 < args.ne21; i21 += ntg) { // n_tokens
390 if (i21 + tpitg < args.ne21) {
391 device const int32_t * src2_i32 = (device const int32_t *) (src2 + (i21 + tpitg)*args.nb21);
392
393 // 0xFFFF marks a skipped slot, it never matches an expert since ne02 fits in a threadgroup
394 FOR_UNROLL (short i20 = 0; i20 < ne20; i20++) {
395 const int32_t id = src2_i32[i20];
396 sids[tpitg*ne20 + i20] = id == -1 ? 0xFFFF : (uint16_t) id;
397 }
398 }
399
400 threadgroup_barrier(mem_flags::mem_threadgroup);
401
402 for (short t = 0; t < ntg; t++) {
403 if (i21 + t >= args.ne21) {
404 break;
405 }
406
407 threadgroup const uint16_t * sids_t = sids + t*ne20;
408
409 FOR_UNROLL (short i20 = 0; i20 < ne20; i20++) {
410 n_all += sids_t[i20] == ide;
411 }
412 }
413
414 threadgroup_barrier(mem_flags::mem_threadgroup);
415 }
416
417 cnt[ide] = n_all;
418
419 threadgroup_barrier(mem_flags::mem_threadgroup);
420
421 // exclusive prefix sum over the counts: the entries of this expert start at bas[ide] -
422 // every slice is padded by one entry for the uniform dummy store of pass 2 - and emit the
423 // entry tile descriptors of kernel_mul_mm_id: (expert, first entry, valid rows) per tile
424 if (tpitg == 0) {
425 constexpr uint32_t NTILE = 32; // must match NR1 of kernel_mul_mm_id
426
427 uint32_t base = 0;
428 uint32_t tile = 0;
429
430 device uint32_t * tpe_u32 = (device uint32_t *) (htpe);
431
432 for (int e = 0; e < FC_mul_mm_id_map0_ne02; ++e) {
433 const uint32_t n = cnt[e];
434
435 bas[e] = base;
436
437 for (uint32_t k = 0; k < n; k += NTILE) {
438 tpe_u32[1 + 3*tile ] = e;
439 tpe_u32[1 + 3*tile + 1] = base + k;
440 tpe_u32[1 + 3*tile + 2] = n - k < NTILE ? n - k : NTILE;
441
442 tile++;
443 }
444
445 base += n + 1;
446 }
447
448 tpe_u32[0] = tile;
449 }
450
451 threadgroup_barrier(mem_flags::mem_threadgroup);
452
453 // pass 2: write the entries of this expert into its slice of the compact list
454 device int32_t * ids_i32 = (device int32_t *) (hids);
455
456 uint32_t ient = bas[ide];
457
458 for (int i21 = 0; i21 < args.ne21; i21 += ntg) { // n_tokens
459 if (i21 + tpitg < args.ne21) {
460 device const int32_t * src2_i32 = (device const int32_t *) (src2 + (i21 + tpitg)*args.nb21);
461
462 FOR_UNROLL (short i20 = 0; i20 < ne20; i20++) {
463 sids[tpitg*ne20 + i20] = src2_i32[i20];
464 }
465 }
466
467 threadgroup_barrier(mem_flags::mem_threadgroup);
468
469 for (short t = 0; t < ntg; t++) {
470 if (i21 + t >= args.ne21) {
471 break;
472 }
473
474 threadgroup const uint16_t * sids_t = sids + t*ne20;
475
476 short first = -1;
477 short n_ents = 0;
478
479 FOR_UNROLL (short i20 = 0; i20 < ne20; i20++) {
480 const short hit = sids_t[i20] == ide;
481
482 first = hit && first < 0 ? i20 : first;
483 n_ents += hit;
484 }
485
486 // one store per token for every thread, regardless of the match - conditional
487 // stores of the divergent lanes are very slow - the dummy value of the tokens
488 // without a match lands on the padding slot of this slice and is never read
489 ids_i32[ient] = first >= 0 ? (i21 + t)*ne20 + first : 0;
490
491 if (n_ents > 1) {
492 // duplicate ids: the remaining matches of this token
493 short j = 1;
494
495 FOR_UNROLL (short i20 = 0; i20 < ne20; i20++) {
496 if (sids_t[i20] == ide && i20 != first) {
497 ids_i32[ient + j] = (i21 + t)*ne20 + i20;
498 j += 1;
499 }
500 }
501 }
502
503 ient += n_ents;
504 }
505
506 threadgroup_barrier(mem_flags::mem_threadgroup);
507 }
508}
509
510// kernel_mul_mm_id only writes the rows that an expert owns, so the skipped rows are zeroed here
511kernel void kernel_mul_mm_id_zero(
512 constant ggml_metal_kargs_mul_mm_id_zero & args,
513 device const char * src2,
514 device char * dst,
515 uint3 tgpig[[threadgroup_position_in_grid]],
516 ushort3 tpitg[[thread_position_in_threadgroup]],
517 ushort3 ntg[[threads_per_threadgroup]]) {
518 const int i20 = tgpig.x; // slot
519 const int i21 = tgpig.y; // token
520
521 const int32_t id = ((device const int32_t *) (src2 + i21*args.nb21))[i20];
522
523 if (id != -1) {
524 return;
525 }
526
527 device float * dst_row = (device float *) (dst + i20*args.nb1 + i21*args.nb2);
528
529 for (int i0 = tpitg.x; i0 < args.ne0; i0 += ntg.x) {
530 dst_row[i0] = 0.0f;
531 }
532}
533
534typedef decltype(kernel_mul_mm_id_map0<1>) kernel_mul_mm_id_map0_t;
535
536template [[host_name("kernel_mul_mm_id_map0_ne20_1" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<1>;
537template [[host_name("kernel_mul_mm_id_map0_ne20_2" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<2>;
538template [[host_name("kernel_mul_mm_id_map0_ne20_4" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<4>;
539template [[host_name("kernel_mul_mm_id_map0_ne20_5" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<5>;
540template [[host_name("kernel_mul_mm_id_map0_ne20_6" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<6>;
541template [[host_name("kernel_mul_mm_id_map0_ne20_8" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<8>;
542template [[host_name("kernel_mul_mm_id_map0_ne20_9" )]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<9>;
543template [[host_name("kernel_mul_mm_id_map0_ne20_10")]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<10>;
544template [[host_name("kernel_mul_mm_id_map0_ne20_16")]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<16>;
545template [[host_name("kernel_mul_mm_id_map0_ne20_22")]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<22>;
546
547// TODO: refactor into two seperate functions to avoid ifdef branching: with and without tensor support
548template<typename S0, typename S0_4x4, typename S0_8x8, typename S1, typename S1_2x4, typename S1_8x8, typename block_q, short nl, void (*dequantize_func)(device const block_q *, short, thread S0_4x4 &), typename T0, typename T0_4x4, typename T1, typename T1_2x4>
549kernel void kernel_mul_mm_id(
550 constant ggml_metal_kargs_mul_mm_id & args,
551 device const char * src0,
552 device const char * src1,
553 device const char * htpe,
554 device const char * hids,
555 device char * dst,
556 threadgroup char * shmem [[threadgroup(0)]],
557 uint3 tgpig[[threadgroup_position_in_grid]],
558 ushort tiitg[[thread_index_in_threadgroup]],
559 ushort tiisg[[thread_index_in_simdgroup]],
560 ushort sgitg[[simdgroup_index_in_threadgroup]]) {
561 threadgroup S0 * sa = (threadgroup S0 *)(shmem);
562 threadgroup S1 * sb = (threadgroup S1 *)(shmem + 4096);
563
564#ifdef GGML_METAL_HAS_TENSOR
565 threadgroup float * sc = (threadgroup float *)(shmem);
566#endif
567
568 constexpr int NR0 = 64;
569 constexpr int NR1 = 32;
570
571 constexpr int NK = 32;
572 constexpr int NL0 = NK/16;
573 constexpr int NL1 = NK/8;
574
575 const int tile = tgpig.x; // entry tile, described by the prepass
576 const int r0 = tgpig.y*NR0;
577
578 device const uint32_t * tpe_u32 = (device const uint32_t *) (htpe);
579 device const int32_t * ids_i32 = (device const int32_t *) (hids);
580
581 // the prepass emits one (expert, first entry, valid rows) descriptor per tile that has
582 // work - the grid is sized by a static upper bound, so the tail early exits
583 if (tile >= (int) tpe_u32[0]) {
584 return;
585 }
586
587 const int im = tpe_u32[1 + 3*tile]; // expert
588 const int idb = tpe_u32[1 + 3*tile + 1]; // first entry of this tile in hids
589
590 // number of entries in this tile, always <= NR1
591 const short nr1 = (short) ((tpe_u32[1 + 3*tile + 2] < NR1) ? tpe_u32[1 + 3*tile + 2] : NR1);
592
593 // if this block is of 64x32 shape or smaller
594 const short nr0 = (args.ne0 - r0 < NR0) ? (args.ne0 - r0) : NR0;
595
596 // a thread shouldn't load data outside of the matrix
597 const short lr0 = ((short)tiitg/NL0) < nr0 ? ((short)tiitg/NL0) : nr0 - 1; // 0 .. 63
598
599 const short il0 = (tiitg % NL0);
600
601 const short i13 = 0;
602
603 const uint64_t offset0 = im*args.nb02 + i13*args.nb03;
604 const short offset1 = il0/nl;
605
606 const short iy = 8*(tiitg % NL1);
607
608 // skip the upper half of the token tile when the expert did not fill it
609 constexpr short NR1H = NR1/2;
610
611#ifndef GGML_METAL_HAS_TENSOR
612 S0_8x8 ma[4];
613 S1_8x8 mb[2];
614
615 simdgroup_float8x8 mc[8];
616#else
617 auto tA = tensor<threadgroup S0, dextents<int32_t, 2>, tensor_inline>(sa, dextents<int32_t, 2>(NK, NR0));
618
619 // sb is [NR1][NK] row-major
620 auto tB0 = tensor<threadgroup S1, dextents<int32_t, 2>, tensor_inline>(sb, dextents<int32_t, 2>(NK, NR1H));
621 auto tB1 = tensor<threadgroup S1, dextents<int32_t, 2>, tensor_inline>(sb + NR1H*NK, dextents<int32_t, 2>(NK, NR1H));
622
623 mpp::tensor_ops::matmul2d<
624 mpp::tensor_ops::matmul2d_descriptor(NR1H, NR0, NK, false, true, false, mpp::tensor_ops::matmul2d_descriptor::mode::multiply_accumulate),
625 execution_simdgroups<4>> mm;
626
627#endif
628
629 const short lr1 = ((short)tiitg/NL1) < nr1 ? ((short)tiitg/NL1) : nr1 - 1; // 0 .. 31
630
631 short il = il0;
632
633 const int id = ids_i32[idb + lr1];
634
635 const short i11 = (id % args.ne20) % args.ne11;
636 const int i12 = (id / args.ne20);
637
638 device const block_q * x = (device const block_q *)(src0 + args.nb01*(r0 + lr0) + offset0) + offset1;
639
640 device const T1 * y = (device const T1 *)(src1
641 + args.nb13*i13
642 + args.nb12*i12
643 + args.nb11*i11
644 + args.nb10*iy);
645
646 const bool has_hi = nr1 > NR1H;
647
648#ifndef GGML_METAL_HAS_TENSOR
649 for (short i = 0; i < 8; i++){
650 mc[i] = make_filled_simdgroup_matrix<float, 8>(0.f);
651 }
652
653 // simdgroups 2,3 own rows NR1H..NR1-1
654 const bool sg_active = has_hi || sgitg < 2;
655#else
656 auto cT0 = mm.get_destination_cooperative_tensor<decltype(tA), decltype(tB0), float>();
657 auto cT1 = mm.get_destination_cooperative_tensor<decltype(tA), decltype(tB1), float>();
658#endif
659
660 for (int loop_k = 0; loop_k < args.ne00; loop_k += NK) {
661#ifndef GGML_METAL_HAS_TENSOR
662 // load data and store to threadgroup memory
663 if (is_same<T0_4x4, block_q>::value && FC_mul_mm_bc_inp) {
664 threadgroup_barrier(mem_flags::mem_threadgroup);
665
666 // no need for dequantization
667 for (short i = 0; i < 16; i++) {
668 const short sx = 2*il0 + i/8;
669 const short sy = (tiitg/NL0)/8;
670
671 //const short lx = i%8;
672 //const short ly = (tiitg/NL0)%8;
673 const short lx = (tiitg/NL0)%8;
674 const short ly = i%8;
675
676 const short ib = 8*sx + sy;
677
678 *(sa + 64*ib + 8*ly + lx) = loop_k + 16*il + i < args.ne00 ? (S0) *((device T0 *) x + i) : (S0) 0;
679 }
680 } else {
681 S0_4x4 temp_a;
682 dequantize_func(x, il, temp_a);
683
684 threadgroup_barrier(mem_flags::mem_threadgroup);
685
686 FOR_UNROLL (short i = 0; i < 16; i++) {
687 const short sx = 2*il0 + i/8;
688 const short sy = (tiitg/NL0)/8;
689
690 //const short lx = i%8;
691 //const short ly = (tiitg/NL0)%8;
692 const short lx = (tiitg/NL0)%8;
693 const short ly = i%8;
694
695 const short ib = 8*sx + sy;
696
697 // NOTE: this is massively slower.. WTF?
698 //sa[64*ib + 8*ly + lx] = temp_a[i/4][i%4];
699
700 *(sa + 64*ib + 8*ly + lx) = temp_a[i/4][i%4];
701 }
702 }
703
704 if (FC_mul_mm_bc_inp) {
705 for (short i = 0; i < 8; ++i) {
706 const short sx = (tiitg%NL1);
707 const short sy = (tiitg/NL1)/8;
708
709 const short lx = i;
710 const short ly = (tiitg/NL1)%8;
711 //const short lx = (tiitg/NL1)%8;
712 //const short ly = i;
713
714 const short ib = 4*sx + sy;
715
716 *(sb + 64*ib + 8*ly + lx) = loop_k + iy + i < args.ne00 ? (S1) *((device T1 *) y + i) : 0;
717 }
718 } else {
719 const short sx = (tiitg%NL1);
720 const short sy = (tiitg/NL1)/8;
721
722 //const short dx = sx;
723 //const short dy = sy;
724
725 const short ly = (tiitg/NL1)%8;
726
727 const short ib = 4*sx + sy;
728
729 *(threadgroup S1_2x4 *)(sb + 64*ib + 8*ly) = (S1_2x4)(*((device T1_2x4 *) y));
730 }
731#else
732 // load data and store to threadgroup memory
733 if (is_same<T0_4x4, block_q>::value && FC_mul_mm_bc_inp) {
734 threadgroup_barrier(mem_flags::mem_threadgroup);
735
736 // no need for dequantization
737 for (short i = 0; i < 16; i++) {
738 const short sx = 2*il0 + i/8;
739 const short sy = (tiitg/NL0)/8;
740
741 const short lx = i%8;
742 const short ly = (tiitg/NL0)%8;
743 //const short lx = (tiitg/NL0)%8;
744 //const short ly = i%8;
745
746 *(sa + NK*(8*sy + ly) + 8*sx + lx) = loop_k + 16*il + i < args.ne00 ? (S0) *((device T0 *) x + i) : 0;
747 }
748 } else {
749 S0_4x4 temp_a;
750 dequantize_func(x, il, temp_a);
751
752 threadgroup_barrier(mem_flags::mem_threadgroup);
753
754 FOR_UNROLL (short i = 0; i < 16; i++) {
755 const short sx = 2*il0 + i/8;
756 const short sy = (tiitg/NL0)/8;
757
758 const short lx = i%8;
759 const short ly = (tiitg/NL0)%8;
760 //const short lx = (tiitg/NL0)%8;
761 //const short ly = i%8;
762
763 *(sa + NK*(8*sy + ly) + 8*sx + lx) = temp_a[i/4][i%4];
764 }
765 }
766
767 if (FC_mul_mm_bc_inp) {
768 for (short i = 0; i < 8; ++i) {
769 const short sx = (tiitg%NL1);
770 const short sy = (tiitg/NL1)/8;
771
772 const short lx = i;
773 const short ly = (tiitg/NL1)%8;
774 //const short lx = (tiitg/NL1)%8;
775 //const short ly = i;
776
777 *(sb + NK*(8*sy + ly) + 8*sx + lx) = loop_k + iy + i < args.ne00 ? (S1) *((device T1 *) y + i) : 0;
778 }
779 } else {
780 const short sx = (tiitg%NL1);
781 const short sy = (tiitg/NL1)/8;
782
783 //const short lx = i;
784 const short ly = (tiitg/NL1)%8;
785 //const short lx = (tiitg/NL1)%8;
786 //const short ly = i;
787
788 *(threadgroup S1_2x4 *)(sb + NK*(8*sy + ly) + 8*sx) = (S1_2x4)(*((device T1_2x4 *) y));
789 }
790#endif
791
792 il = (il + 2 < nl) ? il + 2 : il % 2;
793 x = (il < 2) ? x + (2 + nl - 1)/nl : x;
794
795 y += NK;
796
797 threadgroup_barrier(mem_flags::mem_threadgroup);
798
799#ifndef GGML_METAL_HAS_TENSOR
800 if (sg_active) {
801 // load matrices from threadgroup memory and conduct outer products
802 threadgroup const S0 * lsma = (sa + 4*64*(sgitg%2));
803 threadgroup const S1 * lsmb = (sb + 2*64*(sgitg/2));
804
805 FOR_UNROLL (short ik = 0; ik < NK/8; ik++) {
806 simdgroup_barrier(mem_flags::mem_none);
807
808 FOR_UNROLL (short i = 0; i < 4; i++) {
809 simdgroup_load(ma[i], lsma + 64*i, 8, 0, false);
810 }
811
812 simdgroup_barrier(mem_flags::mem_none);
813
814 FOR_UNROLL (short i = 0; i < 2; i++) {
815 simdgroup_load(mb[i], lsmb + 64*i, 8, 0, false);
816 }
817
818 simdgroup_barrier(mem_flags::mem_none);
819
820 FOR_UNROLL (short i = 0; i < 8; i++){
821 simdgroup_multiply_accumulate(mc[i], mb[i/4], ma[i%4], mc[i]);
822 }
823
824 lsma += 8*64;
825 lsmb += 4*64;
826 }
827 }
828#else
829 auto sA = tA.slice(0, 0);
830 auto sB0 = tB0.slice(0, 0);
831
832 mm.run(sB0, sA, cT0);
833
834 if (has_hi) {
835 auto sB1 = tB1.slice(0, 0);
836
837 mm.run(sB1, sA, cT1);
838 }
839#endif
840 }
841
842 // block is smaller than 64x32, we should avoid writing data outside of the matrix
843 threadgroup_barrier(mem_flags::mem_threadgroup);
844
845#ifdef GGML_METAL_HAS_TENSOR
846 auto tC0 = tensor<threadgroup float, dextents<int32_t, 2>, tensor_inline>(sc, dextents<int32_t, 2>(NR0, NR1H));
847 cT0.store(tC0);
848
849 if (has_hi) {
850 auto tC1 = tensor<threadgroup float, dextents<int32_t, 2>, tensor_inline>(sc + NR1H*NR0, dextents<int32_t, 2>(NR0, NR1H));
851 cT1.store(tC1);
852 }
853#else
854 if (sg_active) {
855 threadgroup float * temp_str = ((threadgroup float *) shmem) + 32*(sgitg&1) + (16*(sgitg >> 1))*NR0;
856
857 for (short i = 0; i < 8; i++) {
858 simdgroup_store(mc[i], temp_str + 8*(i%4) + 8*NR0*(i/4), NR0, 0, false);
859 }
860 }
861#endif
862
863 threadgroup_barrier(mem_flags::mem_threadgroup);
864
865 for (short j = sgitg; j < nr1; j += 4) {
866 const int id = ids_i32[idb + j];
867
868 const short ide = id % args.ne20;
869 const int idt = id / args.ne20;
870
871 device float * D = (device float *) dst + r0 + (uint64_t) ide*args.ne0 + (uint64_t) idt*args.ne1*args.ne0;
872 device float4 * D4 = (device float4 *) D;
873
874 threadgroup float * C = (threadgroup float *) shmem + j*NR0;
875 threadgroup float4 * C4 = (threadgroup float4 *) C;
876
877 int i = tiisg;
878 for (; i < nr0/4; i += 32) {
879 *(D4 + i) = *(C4 + i);
880 }
881
882 i = (4*(nr0/4)) + tiisg;
883 for (; i < nr0; i += 32) {
884 *(D + i) = *(C + i);
885 }
886 }
887}
888
889//
890// matrix-matrix multiplication
891//
892
893typedef decltype(kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, float, float2x4>) mul_mm_t;
894
895template [[host_name("kernel_mul_mm_f32_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, float, float2x4>;
896template [[host_name("kernel_mul_mm_f16_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, half4x4, 1, dequantize_f16, half, half4x4, float, float2x4>;
897#if defined(GGML_METAL_HAS_BF16)
898template [[host_name("kernel_mul_mm_bf16_f32")]] kernel mul_mm_t kernel_mul_mm<bfloat, bfloat4x4, simdgroup_bfloat8x8, bfloat, bfloat2x4, simdgroup_bfloat8x8, bfloat4x4, 1, dequantize_bf16, bfloat, bfloat4x4, float, float2x4>;
899#endif
900template [[host_name("kernel_mul_mm_q1_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q1_0, 8, dequantize_q1_0, float, float4x4, float, float2x4>;
901template [[host_name("kernel_mul_mm_q2_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_0, 4, dequantize_q2_0, float, float4x4, float, float2x4>;
902template [[host_name("kernel_mul_mm_q4_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_0, 2, dequantize_q4_0, float, float4x4, float, float2x4>;
903template [[host_name("kernel_mul_mm_q4_1_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_1, 2, dequantize_q4_1, float, float4x4, float, float2x4>;
904template [[host_name("kernel_mul_mm_q5_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_0, 2, dequantize_q5_0, float, float4x4, float, float2x4>;
905template [[host_name("kernel_mul_mm_q5_1_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_1, 2, dequantize_q5_1, float, float4x4, float, float2x4>;
906template [[host_name("kernel_mul_mm_q8_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q8_0, 2, dequantize_q8_0, float, float4x4, float, float2x4>;
907template [[host_name("kernel_mul_mm_mxfp4_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_mxfp4, 2, dequantize_mxfp4, float, float4x4, float, float2x4>;
908template [[host_name("kernel_mul_mm_q2_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_K, QK_NL, dequantize_q2_K, float, float4x4, float, float2x4>;
909template [[host_name("kernel_mul_mm_q3_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q3_K, QK_NL, dequantize_q3_K, float, float4x4, float, float2x4>;
910template [[host_name("kernel_mul_mm_q4_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_K, QK_NL, dequantize_q4_K, float, float4x4, float, float2x4>;
911template [[host_name("kernel_mul_mm_q5_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_K, QK_NL, dequantize_q5_K, float, float4x4, float, float2x4>;
912template [[host_name("kernel_mul_mm_q6_K_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q6_K, QK_NL, dequantize_q6_K, float, float4x4, float, float2x4>;
913template [[host_name("kernel_mul_mm_iq2_xxs_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xxs, QK_NL, dequantize_iq2_xxs, float, float4x4, float, float2x4>;
914template [[host_name("kernel_mul_mm_iq2_xs_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xs, QK_NL, dequantize_iq2_xs, float, float4x4, float, float2x4>;
915template [[host_name("kernel_mul_mm_iq3_xxs_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_xxs, QK_NL, dequantize_iq3_xxs, float, float4x4, float, float2x4>;
916template [[host_name("kernel_mul_mm_iq3_s_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_s, QK_NL, dequantize_iq3_s, float, float4x4, float, float2x4>;
917template [[host_name("kernel_mul_mm_iq2_s_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_s, QK_NL, dequantize_iq2_s, float, float4x4, float, float2x4>;
918template [[host_name("kernel_mul_mm_iq1_s_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_s, QK_NL, dequantize_iq1_s, float, float4x4, float, float2x4>;
919template [[host_name("kernel_mul_mm_iq1_m_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_m, QK_NL, dequantize_iq1_m, float, float4x4, float, float2x4>;
920template [[host_name("kernel_mul_mm_iq4_nl_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_nl, 2, dequantize_iq4_nl, float, float4x4, float, float2x4>;
921template [[host_name("kernel_mul_mm_iq4_xs_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_xs, QK_NL, dequantize_iq4_xs, float, float4x4, float, float2x4>;
922template [[host_name("kernel_mul_mm_tq2_0_f32")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_tq2_0, QK_NL, dequantize_tq2_0, float, float4x4, float, float2x4>;
923
924template [[host_name("kernel_mul_mm_f32_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, half, half2x4>;
925template [[host_name("kernel_mul_mm_f16_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, half4x4, 1, dequantize_f16, half, half4x4, half, half2x4>;
926template [[host_name("kernel_mul_mm_q1_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q1_0, 8, dequantize_q1_0, float, float4x4, half, half2x4>;
927template [[host_name("kernel_mul_mm_q2_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_0, 4, dequantize_q2_0, float, float4x4, half, half2x4>;
928template [[host_name("kernel_mul_mm_q4_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_0, 2, dequantize_q4_0, float, float4x4, half, half2x4>;
929template [[host_name("kernel_mul_mm_q4_1_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_1, 2, dequantize_q4_1, float, float4x4, half, half2x4>;
930template [[host_name("kernel_mul_mm_q5_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_0, 2, dequantize_q5_0, float, float4x4, half, half2x4>;
931template [[host_name("kernel_mul_mm_q5_1_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_1, 2, dequantize_q5_1, float, float4x4, half, half2x4>;
932template [[host_name("kernel_mul_mm_q8_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q8_0, 2, dequantize_q8_0, float, float4x4, half, half2x4>;
933template [[host_name("kernel_mul_mm_mxfp4_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_mxfp4, 2, dequantize_mxfp4, float, float4x4, half, half2x4>;
934template [[host_name("kernel_mul_mm_q2_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_K, QK_NL, dequantize_q2_K, float, float4x4, half, half2x4>;
935template [[host_name("kernel_mul_mm_q3_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q3_K, QK_NL, dequantize_q3_K, float, float4x4, half, half2x4>;
936template [[host_name("kernel_mul_mm_q4_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_K, QK_NL, dequantize_q4_K, float, float4x4, half, half2x4>;
937template [[host_name("kernel_mul_mm_q5_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_K, QK_NL, dequantize_q5_K, float, float4x4, half, half2x4>;
938template [[host_name("kernel_mul_mm_q6_K_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q6_K, QK_NL, dequantize_q6_K, float, float4x4, half, half2x4>;
939template [[host_name("kernel_mul_mm_iq2_xxs_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xxs, QK_NL, dequantize_iq2_xxs, float, float4x4, half, half2x4>;
940template [[host_name("kernel_mul_mm_iq2_xs_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xs, QK_NL, dequantize_iq2_xs, float, float4x4, half, half2x4>;
941template [[host_name("kernel_mul_mm_iq3_xxs_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_xxs, QK_NL, dequantize_iq3_xxs, float, float4x4, half, half2x4>;
942template [[host_name("kernel_mul_mm_iq3_s_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_s, QK_NL, dequantize_iq3_s, float, float4x4, half, half2x4>;
943template [[host_name("kernel_mul_mm_iq2_s_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_s, QK_NL, dequantize_iq2_s, float, float4x4, half, half2x4>;
944template [[host_name("kernel_mul_mm_iq1_s_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_s, QK_NL, dequantize_iq1_s, float, float4x4, half, half2x4>;
945template [[host_name("kernel_mul_mm_iq1_m_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_m, QK_NL, dequantize_iq1_m, float, float4x4, half, half2x4>;
946template [[host_name("kernel_mul_mm_iq4_nl_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_nl, 2, dequantize_iq4_nl, float, float4x4, half, half2x4>;
947template [[host_name("kernel_mul_mm_iq4_xs_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_xs, QK_NL, dequantize_iq4_xs, float, float4x4, half, half2x4>;
948template [[host_name("kernel_mul_mm_tq2_0_f16")]] kernel mul_mm_t kernel_mul_mm<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_tq2_0, QK_NL, dequantize_tq2_0, float, float4x4, half, half2x4>;
949
950//
951// indirect matrix-matrix multiplication
952//
953
954typedef decltype(kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, float, float2x4>) mul_mm_id;
955
956template [[host_name("kernel_mul_mm_id_f32_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, float, float2x4>;
957template [[host_name("kernel_mul_mm_id_f16_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, half4x4, 1, dequantize_f16, half, half4x4, float, float2x4>;
958#if defined(GGML_METAL_HAS_BF16)
959template [[host_name("kernel_mul_mm_id_bf16_f32")]] kernel mul_mm_id kernel_mul_mm_id<bfloat, bfloat4x4, simdgroup_bfloat8x8, bfloat, bfloat2x4, simdgroup_bfloat8x8, bfloat4x4, 1, dequantize_bf16, bfloat, bfloat4x4, float, float2x4>;
960#endif
961template [[host_name("kernel_mul_mm_id_q1_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q1_0, 8, dequantize_q1_0, float, float4x4, float, float2x4>;
962template [[host_name("kernel_mul_mm_id_q2_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_0, 4, dequantize_q2_0, float, float4x4, float, float2x4>;
963template [[host_name("kernel_mul_mm_id_q4_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_0, 2, dequantize_q4_0, float, float4x4, float, float2x4>;
964template [[host_name("kernel_mul_mm_id_q4_1_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_1, 2, dequantize_q4_1, float, float4x4, float, float2x4>;
965template [[host_name("kernel_mul_mm_id_q5_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_0, 2, dequantize_q5_0, float, float4x4, float, float2x4>;
966template [[host_name("kernel_mul_mm_id_q5_1_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_1, 2, dequantize_q5_1, float, float4x4, float, float2x4>;
967template [[host_name("kernel_mul_mm_id_q8_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q8_0, 2, dequantize_q8_0, float, float4x4, float, float2x4>;
968#if defined(GGML_METAL_HAS_BF16)
969template [[host_name("kernel_mul_mm_id_mxfp4_f32")]] kernel mul_mm_id kernel_mul_mm_id<bfloat, bfloat4x4, simdgroup_bfloat8x8, bfloat, bfloat2x4, simdgroup_bfloat8x8, block_mxfp4, 2, dequantize_mxfp4, float, float4x4, float, float2x4>;
970#else
971template [[host_name("kernel_mul_mm_id_mxfp4_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_mxfp4, 2, dequantize_mxfp4, float, float4x4, float, float2x4>;
972#endif
973template [[host_name("kernel_mul_mm_id_q2_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_K, QK_NL, dequantize_q2_K, float, float4x4, float, float2x4>;
974template [[host_name("kernel_mul_mm_id_q3_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q3_K, QK_NL, dequantize_q3_K, float, float4x4, float, float2x4>;
975template [[host_name("kernel_mul_mm_id_q4_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_K, QK_NL, dequantize_q4_K, float, float4x4, float, float2x4>;
976template [[host_name("kernel_mul_mm_id_q5_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_K, QK_NL, dequantize_q5_K, float, float4x4, float, float2x4>;
977template [[host_name("kernel_mul_mm_id_q6_K_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q6_K, QK_NL, dequantize_q6_K, float, float4x4, float, float2x4>;
978template [[host_name("kernel_mul_mm_id_iq2_xxs_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xxs, QK_NL, dequantize_iq2_xxs, float, float4x4, float, float2x4>;
979template [[host_name("kernel_mul_mm_id_iq2_xs_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xs, QK_NL, dequantize_iq2_xs, float, float4x4, float, float2x4>;
980template [[host_name("kernel_mul_mm_id_iq3_xxs_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_xxs, QK_NL, dequantize_iq3_xxs, float, float4x4, float, float2x4>;
981template [[host_name("kernel_mul_mm_id_iq3_s_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_s, QK_NL, dequantize_iq3_s, float, float4x4, float, float2x4>;
982template [[host_name("kernel_mul_mm_id_iq2_s_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_s, QK_NL, dequantize_iq2_s, float, float4x4, float, float2x4>;
983template [[host_name("kernel_mul_mm_id_iq1_s_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_s, QK_NL, dequantize_iq1_s, float, float4x4, float, float2x4>;
984template [[host_name("kernel_mul_mm_id_iq1_m_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_m, QK_NL, dequantize_iq1_m, float, float4x4, float, float2x4>;
985template [[host_name("kernel_mul_mm_id_iq4_nl_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_nl, 2, dequantize_iq4_nl, float, float4x4, float, float2x4>;
986template [[host_name("kernel_mul_mm_id_iq4_xs_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_xs, QK_NL, dequantize_iq4_xs, float, float4x4, float, float2x4>;
987template [[host_name("kernel_mul_mm_id_tq2_0_f32")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_tq2_0, QK_NL, dequantize_tq2_0, float, float4x4, float, float2x4>;
988
989template [[host_name("kernel_mul_mm_id_f32_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, float4x4, 1, dequantize_f32, float, float4x4, half, half2x4>;
990template [[host_name("kernel_mul_mm_id_f16_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, half4x4, 1, dequantize_f16, half, half4x4, half, half2x4>;
991template [[host_name("kernel_mul_mm_id_q1_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q1_0, 8, dequantize_q1_0, float, float4x4, half, half2x4>;
992template [[host_name("kernel_mul_mm_id_q2_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_0, 4, dequantize_q2_0, float, float4x4, half, half2x4>;
993template [[host_name("kernel_mul_mm_id_q4_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_0, 2, dequantize_q4_0, float, float4x4, half, half2x4>;
994template [[host_name("kernel_mul_mm_id_q4_1_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_1, 2, dequantize_q4_1, float, float4x4, half, half2x4>;
995template [[host_name("kernel_mul_mm_id_q5_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_0, 2, dequantize_q5_0, float, float4x4, half, half2x4>;
996template [[host_name("kernel_mul_mm_id_q5_1_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_1, 2, dequantize_q5_1, float, float4x4, half, half2x4>;
997template [[host_name("kernel_mul_mm_id_q8_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q8_0, 2, dequantize_q8_0, float, float4x4, half, half2x4>;
998#if defined(GGML_METAL_HAS_BF16)
999template [[host_name("kernel_mul_mm_id_mxfp4_f16")]] kernel mul_mm_id kernel_mul_mm_id<bfloat, bfloat4x4, simdgroup_bfloat8x8, bfloat, bfloat2x4, simdgroup_bfloat8x8, block_mxfp4, 2, dequantize_mxfp4, float, float4x4, half, half2x4>;
1000#else
1001template [[host_name("kernel_mul_mm_id_mxfp4_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_mxfp4, 2, dequantize_mxfp4, float, float4x4, half, half2x4>;
1002#endif
1003template [[host_name("kernel_mul_mm_id_q2_K_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q2_K, QK_NL, dequantize_q2_K, float, float4x4, half, half2x4>;
1004template [[host_name("kernel_mul_mm_id_q3_K_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q3_K, QK_NL, dequantize_q3_K, float, float4x4, half, half2x4>;
1005template [[host_name("kernel_mul_mm_id_q4_K_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q4_K, QK_NL, dequantize_q4_K, float, float4x4, half, half2x4>;
1006template [[host_name("kernel_mul_mm_id_q5_K_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q5_K, QK_NL, dequantize_q5_K, float, float4x4, half, half2x4>;
1007template [[host_name("kernel_mul_mm_id_q6_K_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_q6_K, QK_NL, dequantize_q6_K, float, float4x4, half, half2x4>;
1008template [[host_name("kernel_mul_mm_id_iq2_xxs_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xxs, QK_NL, dequantize_iq2_xxs, float, float4x4, half, half2x4>;
1009template [[host_name("kernel_mul_mm_id_iq2_xs_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_xs, QK_NL, dequantize_iq2_xs, float, float4x4, half, half2x4>;
1010template [[host_name("kernel_mul_mm_id_iq3_xxs_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_xxs, QK_NL, dequantize_iq3_xxs, float, float4x4, half, half2x4>;
1011template [[host_name("kernel_mul_mm_id_iq3_s_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq3_s, QK_NL, dequantize_iq3_s, float, float4x4, half, half2x4>;
1012template [[host_name("kernel_mul_mm_id_iq2_s_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq2_s, QK_NL, dequantize_iq2_s, float, float4x4, half, half2x4>;
1013template [[host_name("kernel_mul_mm_id_iq1_s_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_s, QK_NL, dequantize_iq1_s, float, float4x4, half, half2x4>;
1014template [[host_name("kernel_mul_mm_id_iq1_m_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq1_m, QK_NL, dequantize_iq1_m, float, float4x4, half, half2x4>;
1015template [[host_name("kernel_mul_mm_id_iq4_nl_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_nl, 2, dequantize_iq4_nl, float, float4x4, half, half2x4>;
1016template [[host_name("kernel_mul_mm_id_iq4_xs_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_iq4_xs, QK_NL, dequantize_iq4_xs, float, float4x4, half, half2x4>;
1017template [[host_name("kernel_mul_mm_id_tq2_0_f16")]] kernel mul_mm_id kernel_mul_mm_id<half, half4x4, simdgroup_half8x8, half, half2x4, simdgroup_half8x8, block_tq2_0, QK_NL, dequantize_tq2_0, float, float4x4, half, half2x4>;