mlx-native 0.22.0

Pure-Rust Metal GPU compute library for MLX-compatible inference on Apple Silicon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
// Prepared Q4_K/Q6_K expert planes and grouped staged-M8 decode tiles.
//
// The compact expert schedule groups live routed rows by expert. Decode keeps
// activations and expert intermediates in BF16, uses the one/two-row column
// path for sparse tiles, and otherwise runs exact eight-row tensor tiles.

#include <metal_stdlib>
#include <metal_tensor>
#include <MetalPerformancePrimitives/MetalPerformancePrimitives.h>
using namespace metal;
using namespace mpp::tensor_ops;

constant constexpr uint M8_TILE_ROWS = 64;
constant constexpr uint M8_Q4 = 0;
constant constexpr uint M8_Q6 = 1;

struct GroupedM8PrepareParams {
    uint n_experts;
    uint n;
    uint k;
    uint format;
    uint64_t source_expert_stride;
    uint64_t prepared_expert_stride;
    uint plane1_offset;
    uint meta_offset;
    uint source_row_bytes;
    uint _pad;
};

inline ulong grouped_m8_tiled_index(uint row, uint unit, uint units) {
    return (ulong(row / M8_TILE_ROWS) * units + unit) * M8_TILE_ROWS + row % M8_TILE_ROWS;
}

inline uint grouped_m8_quant_slot(uint e) {
    return 8 * ((e >> 2) & 3) + 4 * (e >> 4) + (e & 3);
}

inline uint grouped_m8_pair_word(thread const uchar *slots, uint c) {
    uint word = 0;
    for (uint i = 0; i < 8; ++i)
        word |= uint(slots[8 * c + i]) << ((i & 1) * 16 + 4 * (i >> 1));
    return word;
}

inline uint grouped_m8_bit_word(thread const uchar *slots, uint bits, uint word_index) {
    const uint per = 32 / bits;
    uint word = 0;
    for (uint i = 0; i < per; ++i)
        word |= uint(slots[word_index * per + i]) << (bits * i);
    return word;
}

kernel void grouped_m8_prepare_q4k_q6k(
    device const uchar *source [[buffer(0)]],
    device uchar *prepared [[buffer(1)]],
    constant GroupedM8PrepareParams &p [[buffer(2)]],
    uint index [[thread_position_in_grid]]) {
    const uint groups = p.k / 32;
    const uint matrix_units = p.n * groups;
    if (index >= p.n_experts * matrix_units)
        return;
    const uint expert = index / matrix_units;
    const uint matrix_index = index % matrix_units;
    const uint row = matrix_index / groups;
    const uint group = matrix_index % groups;
    const uint block = group / 8;
    const uint within = group % 8;
    device const uchar *expert_source = source + uint64_t(expert) * p.source_expert_stride;
    device uchar *expert_prepared = prepared + uint64_t(expert) * p.prepared_expert_stride;
    device const uchar *b = expert_source + uint64_t(row) * p.source_row_bytes +
                            uint64_t(block) * (p.format == M8_Q4 ? 144 : 210);
    device uchar *plane0 = expert_prepared + grouped_m8_tiled_index(row, group, groups) * 16;
    uchar low[32], high[32];
    if (p.format == M8_Q4) {
        for (uint e = 0; e < 32; ++e)
            low[grouped_m8_quant_slot(e)] =
                (b[16 + (within / 2) * 32 + e] >> (4 * (within % 2))) & 15;
        for (uint c = 0; c < 4; ++c)
            reinterpret_cast<device uint *>(plane0)[c] = grouped_m8_pair_word(low, c);
        if (within == 0) {
            device uchar *meta = expert_prepared + p.meta_offset +
                grouped_m8_tiled_index(row, block, groups / 8) * 16;
            for (uint i = 0; i < 16; ++i)
                meta[i] = b[i];
        }
    } else {
        const uint high_block = within / 4;
        const uint quarter = within % 4;
        for (uint e = 0; e < 32; ++e) {
            low[grouped_m8_quant_slot(e)] =
                (b[64 * high_block + 32 * (quarter & 1) + e] >> (4 * (quarter >> 1))) & 15;
            high[grouped_m8_quant_slot(e)] =
                (b[128 + 32 * high_block + e] >> (2 * quarter)) & 3;
        }
        for (uint c = 0; c < 4; ++c)
            reinterpret_cast<device uint *>(plane0)[c] = grouped_m8_pair_word(low, c);
        device uchar *plane1 = expert_prepared + p.plane1_offset +
            grouped_m8_tiled_index(row, group, groups) * 8;
        reinterpret_cast<device uint *>(plane1)[0] = grouped_m8_bit_word(high, 2, 0);
        reinterpret_cast<device uint *>(plane1)[1] = grouped_m8_bit_word(high, 2, 1);
        if (within == 0) {
            device uchar *meta = expert_prepared + p.meta_offset +
                grouped_m8_tiled_index(row, block, groups / 8) * 20;
            for (uint i = 0; i < 16; ++i)
                meta[i] = b[192 + i];
            meta[16] = b[208];
            meta[17] = b[209];
            meta[18] = 0;
            meta[19] = 0;
        }
    }
}

inline uint4 grouped_m8_nibble_pairs(uint word) {
    return (uint4(word) >> uint4(0, 4, 8, 12)) & 0x000F000Fu;
}

inline uint grouped_m8_spread2(uint bits) {
    return (bits & 0x3333u) | ((bits & 0xCCCCu) << 14);
}

inline uint grouped_m8_scale_pair(packed_uint4 bytes, ushort j) {
    const uint word = j < 2 ? bytes.x : j < 4 ? bytes.y : j < 6 ? bytes.z : bytes.w;
    return word >> (16u * (j & 1));
}

inline float2 grouped_m8_q4_coef(uint4 h, ushort j) {
    uint scale, minimum;
    if (j < 4) {
        const uint shift = 8u * j;
        scale = (h.y >> shift) & 63u;
        minimum = (h.z >> shift) & 63u;
    } else {
        const uint shift = 8u * (j - 4);
        const uint word = h.w >> shift;
        scale = (word & 15u) | ((((h.y >> shift) >> 6) & 3u) << 4);
        minimum = ((word >> 4) & 15u) | ((((h.z >> shift) >> 6) & 3u) << 4);
    }
    const half d = as_type<half>(ushort(h.x));
    const half dmin = as_type<half>(ushort(h.x >> 16));
    return float2(float(d) * float(scale), -float(dmin) * float(minimum));
}

inline bfloat2 grouped_m8_linear_pair(uint pair, float scale, float minimum, ushort zero) {
    const float2 code =
        float2(as_type<half2>(pair | 0x64006400u) - half2(half(1024 + zero)));
    return zero ? bfloat2(code * scale) : bfloat2(fma(code, float2(scale), float2(minimum)));
}

inline void grouped_m8_decode_q4(
    uint4 payload,
    uint4 meta,
    ushort group,
    ushort half_index,
    threadgroup bfloat *out) {
    const float2 coefficient = grouped_m8_q4_coef(meta, group);
    for (ushort chunk = 0; chunk < 4; ++chunk) {
        const uint4 pairs = grouped_m8_nibble_pairs(payload[chunk]);
        const uint2 selected = half_index ? pairs.zw : pairs.xy;
        *reinterpret_cast<threadgroup bfloat2 *>(out + 4 * chunk) =
            grouped_m8_linear_pair(selected.x, coefficient.x, coefficient.y, 0);
        *reinterpret_cast<threadgroup bfloat2 *>(out + 4 * chunk + 2) =
            grouped_m8_linear_pair(selected.y, coefficient.x, coefficient.y, 0);
    }
}

inline void grouped_m8_decode_q6(
    uint4 low,
    uint2 high,
    packed_uint4 scales,
    uint dh,
    ushort group,
    ushort half_index,
    threadgroup bfloat *out) {
    const float dscale = float(as_type<half>(ushort(dh)));
    const uint pair = grouped_m8_scale_pair(scales, group);
    const float2 scale = float2(
        dscale * float(as_type<char>(uchar(pair))),
        dscale * float(as_type<char>(uchar(pair >> 8))));
    for (ushort chunk = 0; chunk < 4; ++chunk) {
        const uint high_bits = grouped_m8_spread2(high[chunk >> 1] >> (16 * (chunk & 1)));
        const uint4 pairs = grouped_m8_nibble_pairs(low[chunk]) |
            (((uint4(high_bits) >> uint4(0, 4, 8, 12)) & 0x00030003u) << 4);
        const uint2 selected = half_index ? pairs.zw : pairs.xy;
        const float selected_scale = half_index ? scale.y : scale.x;
        *reinterpret_cast<threadgroup bfloat2 *>(out + 4 * chunk) =
            grouped_m8_linear_pair(selected.x, selected_scale, 0, 32);
        *reinterpret_cast<threadgroup bfloat2 *>(out + 4 * chunk + 2) =
            grouped_m8_linear_pair(selected.y, selected_scale, 0, 32);
    }
}

inline void grouped_m8_decode_q4_thread(
    uint4 payload,
    uint4 meta,
    ushort group,
    thread bfloat *out) {
    const float2 coefficient = grouped_m8_q4_coef(meta, group);
    for (ushort half_index = 0; half_index < 2; ++half_index) {
        for (ushort chunk = 0; chunk < 4; ++chunk) {
            const uint4 pairs = grouped_m8_nibble_pairs(payload[chunk]);
            const uint2 selected = half_index ? pairs.zw : pairs.xy;
            *reinterpret_cast<thread bfloat2 *>(out + half_index * 16 + 4 * chunk) =
                grouped_m8_linear_pair(selected.x, coefficient.x, coefficient.y, 0);
            *reinterpret_cast<thread bfloat2 *>(out + half_index * 16 + 4 * chunk + 2) =
                grouped_m8_linear_pair(selected.y, coefficient.x, coefficient.y, 0);
        }
    }
}

inline void grouped_m8_decode_q6_thread(
    uint4 low,
    uint2 high,
    packed_uint4 scales,
    uint dh,
    ushort group,
    thread bfloat *out) {
    const float dscale = float(as_type<half>(ushort(dh)));
    const uint pair = grouped_m8_scale_pair(scales, group);
    const float2 scale = float2(
        dscale * float(as_type<char>(uchar(pair))),
        dscale * float(as_type<char>(uchar(pair >> 8))));
    for (ushort half_index = 0; half_index < 2; ++half_index) {
        for (ushort chunk = 0; chunk < 4; ++chunk) {
            const uint high_bits = grouped_m8_spread2(high[chunk >> 1] >> (16 * (chunk & 1)));
            const uint4 pairs = grouped_m8_nibble_pairs(low[chunk]) |
                (((uint4(high_bits) >> uint4(0, 4, 8, 12)) & 0x00030003u) << 4);
            const uint2 selected = half_index ? pairs.zw : pairs.xy;
            const float selected_scale = half_index ? scale.y : scale.x;
            *reinterpret_cast<thread bfloat2 *>(out + half_index * 16 + 4 * chunk) =
                grouped_m8_linear_pair(selected.x, selected_scale, 0, 32);
            *reinterpret_cast<thread bfloat2 *>(out + half_index * 16 + 4 * chunk + 2) =
                grouped_m8_linear_pair(selected.y, selected_scale, 0, 32);
        }
    }
}

struct GroupedM8Params {
    int32_t k;
    int32_t n;
    int32_t n_tokens;
    int32_t top_k;
    int32_t n_experts;
    uint32_t slotted_input;
    uint64_t prepared_expert_stride;
    uint32_t plane1_offset;
    uint32_t meta_offset;
};

kernel void grouped_m8_gather_f32_bf16(
    device const float *input [[buffer(0)]],
    device bfloat *gathered [[buffer(1)]],
    device const uint32_t *schedule [[buffer(2)]],
    device const int32_t *routes [[buffer(3)]],
    constant GroupedM8Params &p [[buffer(4)]],
    uint2 group [[threadgroup_position_in_grid]],
    uint thread_index [[thread_index_in_threadgroup]]) {
    const uint state = schedule[0];
    if ((state & 0x80000000u) != 0u || group.y >= state)
        return;
    device const uint32_t *descriptor = schedule + 1 + 3 * group.y;
    const uint first_route = descriptor[1];
    const uint route_count = descriptor[2];
    for (uint row = 0; row < route_count; ++row) {
        const int packed = routes[first_route + row];
        const int input_row = p.slotted_input ? packed : packed / p.top_k;
        for (uint column = thread_index; column < uint(p.k); column += 256) {
            gathered[(uint64_t(first_route) + row) * p.k + column] =
                bfloat(input[int64_t(input_row) * p.k + column]);
        }
    }
}

kernel void grouped_m8_gather_bf16(
    device const bfloat *input [[buffer(0)]],
    device bfloat *gathered [[buffer(1)]],
    device const uint32_t *schedule [[buffer(2)]],
    device const int32_t *routes [[buffer(3)]],
    constant GroupedM8Params &p [[buffer(4)]],
    uint2 group [[threadgroup_position_in_grid]],
    uint thread_index [[thread_index_in_threadgroup]]) {
    const uint state = schedule[0];
    if ((state & 0x80000000u) != 0u || group.y >= state)
        return;
    device const uint32_t *descriptor = schedule + 1 + 3 * group.y;
    const uint first_route = descriptor[1];
    const uint route_count = descriptor[2];
    for (uint row = 0; row < route_count; ++row) {
        const int packed = routes[first_route + row];
        const int input_row = p.slotted_input ? packed : packed / p.top_k;
        for (uint column = thread_index; column < uint(p.k); column += 256) {
            gathered[(uint64_t(first_route) + row) * p.k + column] =
                input[int64_t(input_row) * p.k + column];
        }
    }
}


constant constexpr ushort M8_EP_F32 = 0;
constant constexpr ushort M8_EP_BF16 = 1;
constant constexpr ushort M8_EP_SILU_MUL_BF16 = 2;

template <bool Q6, ushort Ep>
kernel void grouped_m8_impl(
    device const uchar *weights [[buffer(0)]],
    device bfloat *input [[buffer(1)]],
    device uchar *output [[buffer(2)]],
    device const uint32_t *schedule [[buffer(3)]],
    device const int32_t *routes [[buffer(4)]],
    constant GroupedM8Params &p [[buffer(5)]],
    device const bfloat *aux [[buffer(6)]],
    uint2 group [[threadgroup_position_in_grid]],
    ushort thread_index [[thread_index_in_threadgroup]],
    ushort lane [[thread_index_in_simdgroup]],
    ushort simdgroup [[simdgroup_index_in_threadgroup]]) {
    constexpr uint ROWS = 8;
    constexpr uint COLS = 32;
    constexpr uint STEP = 32;
    const uint state = schedule[0];
    if ((state & 0x80000000u) != 0u || group.y >= state)
        return;
    device const uint32_t *descriptor = schedule + 1 + 3 * group.y;
    const uint expert = descriptor[0];
    const uint first_route = descriptor[1];
    const uint route_count = descriptor[2];
    const uint output_origin = group.x * 64 + uint(simdgroup) * COLS;
    device const uchar *expert_weights =
        weights + uint64_t(expert) * p.prepared_expert_stride;
    const uint groups = uint(p.k) / STEP;
    const uint units = groups / 8;

    if (route_count <= 2) {
        int packed[2];
        float sums[2] = {0.0f, 0.0f};
        for (uint row = 0; row < route_count; ++row)
            packed[row] = routes[first_route + row];
        const uint weight_row = output_origin + lane;
        for (uint g = 0; g < groups; ++g) {
            const ulong tiled = grouped_m8_tiled_index(weight_row, g, groups);
            const uint4 low = *reinterpret_cast<device const uint4 *>(
                expert_weights + tiled * 16);
            bfloat4 decoded[8];
            if constexpr (Q6) {
                const uint2 high = *reinterpret_cast<device const uint2 *>(
                    expert_weights + p.plane1_offset + tiled * 8);
                device const uchar *meta = expert_weights + p.meta_offset +
                    grouped_m8_tiled_index(weight_row, g / 8, units) * 20;
                const packed_uint4 scales =
                    *reinterpret_cast<device const packed_uint4 *>(meta);
                const uint dh = *reinterpret_cast<device const uint *>(meta + 16);
                grouped_m8_decode_q6_thread(
                    low, high, scales, dh, ushort(g % 8),
                    reinterpret_cast<thread bfloat *>(decoded));
            } else {
                const uint4 meta = *reinterpret_cast<device const uint4 *>(
                    expert_weights + p.meta_offset +
                    grouped_m8_tiled_index(weight_row, g / 8, units) * 16);
                grouped_m8_decode_q4_thread(
                    low, meta, ushort(g % 8),
                    reinterpret_cast<thread bfloat *>(decoded));
            }
            for (uint row = 0; row < route_count; ++row) {
                device const bfloat4 *x = reinterpret_cast<device const bfloat4 *>(
                    input + (uint64_t(first_route) + row) * p.k + g * STEP);
                float group_sum = 0.0f;
#pragma unroll
                for (ushort i = 0; i < 8; ++i) {
                    const float4 xv = float4(x[i]);
                    const float4 wv = float4(decoded[i]);
                    group_sum += wv.x * xv.x;
                    group_sum += wv.y * xv.y;
                    group_sum += wv.z * xv.z;
                    group_sum += wv.w * xv.w;
                }
                sums[row] += group_sum;
            }
        }
        for (uint row = 0; row < route_count; ++row) {
            const int64_t output_index = int64_t(packed[row]) * p.n + weight_row;
            if constexpr (Ep == M8_EP_F32)
                reinterpret_cast<device float *>(output)[output_index] = sums[row];
            else if constexpr (Ep == M8_EP_BF16)
                reinterpret_cast<device bfloat *>(output)[output_index] = bfloat(sums[row]);
            else {
                const float gate = float(aux[output_index]);
                reinterpret_cast<device bfloat *>(output)[output_index] =
                    bfloat((gate / (1.0f + exp(-gate))) * sums[row]);
            }
        }
        return;
    }

    threadgroup bfloat weight_tile[2 * COLS * STEP];
    threadgroup float result_tile[2 * ROWS * COLS];
    threadgroup int packed_routes[ROWS];
    threadgroup ushort live_rows;

    matmul2d<matmul2d_descriptor(
        ROWS, COLS, STEP, false, true, false,
        matmul2d_descriptor::mode::multiply_accumulate),
        execution_simdgroups<1>> mm;

    for (uint route_base = 0; route_base < route_count; route_base += ROWS) {
        if (thread_index == 0)
            live_rows = ushort(min(ROWS, route_count - route_base));
        if (thread_index < ROWS)
            packed_routes[thread_index] =
                thread_index < min(ROWS, route_count - route_base)
                    ? routes[first_route + route_base + thread_index]
                    : -1;
        threadgroup_barrier(mem_flags::mem_threadgroup);

        auto all_left = tensor(
            input + (uint64_t(first_route) + route_base) * p.k,
            dextents<int32_t, 2>{p.k, ROWS},
            array<int32_t, 2>{1, p.k});
        auto first_left = all_left.template slice<STEP, ROWS>(0, 0);
        threadgroup bfloat *simd_weight = weight_tile + uint(simdgroup) * COLS * STEP;
        auto right = tensor<threadgroup bfloat, dextents<int32_t, 2>, tensor_inline>(
            simd_weight, dextents<int32_t, 2>{STEP, COLS},
            array<int32_t, 2>{1, STEP});
        auto sum = mm.get_destination_cooperative_tensor<decltype(first_left), decltype(right), float>();
        for (ushort i = 0; i < sum.get_capacity(); ++i)
            sum[i] = 0.0f;

        for (uint g = 0; g < groups; ++g) {

            const uint weight_row = output_origin + lane;
            const ulong tiled = grouped_m8_tiled_index(weight_row, g, groups);
            const uint4 low = *reinterpret_cast<device const uint4 *>(
                expert_weights + tiled * 16);
            if constexpr (Q6) {
                const uint2 high = *reinterpret_cast<device const uint2 *>(
                    expert_weights + p.plane1_offset + tiled * 8);
                device const uchar *meta = expert_weights + p.meta_offset +
                    grouped_m8_tiled_index(weight_row, g / 8, units) * 20;
                const packed_uint4 scales =
                    *reinterpret_cast<device const packed_uint4 *>(meta);
                const uint dh = *reinterpret_cast<device const uint *>(meta + 16);
                grouped_m8_decode_q6(
                    low, high, scales, dh, ushort(g % 8), 0,
                    simd_weight + lane * STEP);
                grouped_m8_decode_q6(
                    low, high, scales, dh, ushort(g % 8), 1,
                    simd_weight + lane * STEP + 16);
            } else {
                const uint4 meta = *reinterpret_cast<device const uint4 *>(
                    expert_weights + p.meta_offset +
                    grouped_m8_tiled_index(weight_row, g / 8, units) * 16);
                grouped_m8_decode_q4(
                    low, meta, ushort(g % 8), 0,
                    simd_weight + lane * STEP);
                grouped_m8_decode_q4(
                    low, meta, ushort(g % 8), 1,
                    simd_weight + lane * STEP + 16);
            }
            simdgroup_barrier(mem_flags::mem_threadgroup);
            auto left = all_left.template slice<STEP, ROWS>(g * STEP, 0);
            mm.run(left, right, sum);
            simdgroup_barrier(mem_flags::mem_threadgroup);
        }

        threadgroup float *simd_result = result_tile + uint(simdgroup) * ROWS * COLS;
        auto stored = tensor<threadgroup float, dextents<int32_t, 2>, tensor_inline>(
            simd_result, dextents<int32_t, 2>{COLS, ROWS},
            array<int32_t, 2>{1, COLS});
        sum.store(stored);
        threadgroup_barrier(mem_flags::mem_threadgroup);
        for (uint index = thread_index; index < uint(live_rows) * 64; index += 64) {
            const uint row = index / 64;
            const uint column = index % 64;
            const uint source_simdgroup = column / COLS;
            const uint source_column = column % COLS;
            const int64_t output_index =
                int64_t(packed_routes[row]) * p.n + group.x * 64 + column;
            const float value =
                result_tile[source_simdgroup * ROWS * COLS + row * COLS + source_column];
            if constexpr (Ep == M8_EP_F32)
                reinterpret_cast<device float *>(output)[output_index] = value;
            else if constexpr (Ep == M8_EP_BF16)
                reinterpret_cast<device bfloat *>(output)[output_index] = bfloat(value);
            else {
                const float gate = float(aux[output_index]);
                reinterpret_cast<device bfloat *>(output)[output_index] =
                    bfloat((gate / (1.0f + exp(-gate))) * value);
            }
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
}

template <bool Q6>
kernel void grouped_m8_gate_up_impl(
    device const uchar *gate_weights [[buffer(0)]],
    device const uchar *up_weights [[buffer(1)]],
    device bfloat *input [[buffer(2)]],
    device bfloat *output [[buffer(3)]],
    device const uint32_t *schedule [[buffer(4)]],
    device const int32_t *routes [[buffer(5)]],
    constant GroupedM8Params &p [[buffer(6)]],
    uint2 group [[threadgroup_position_in_grid]],
    ushort thread_index [[thread_index_in_threadgroup]],
    ushort lane [[thread_index_in_simdgroup]],
    ushort simdgroup [[simdgroup_index_in_threadgroup]]) {
    constexpr uint ROWS = 8;
    constexpr uint COLS = 32;
    constexpr uint STEP = 32;
    const uint state = schedule[0];
    if ((state & 0x80000000u) != 0u || group.y >= state)
        return;
    device const uint32_t *descriptor = schedule + 1 + 3 * group.y;
    const uint expert = descriptor[0];
    const uint first_route = descriptor[1];
    const uint route_count = descriptor[2];
    const uint output_origin = group.x * COLS;
    const bool up = simdgroup != 0;
    device const uchar *weights = up ? up_weights : gate_weights;
    device const uchar *expert_weights =
        weights + uint64_t(expert) * p.prepared_expert_stride;
    const uint groups = uint(p.k) / STEP;
    const uint units = groups / 8;

    if (route_count <= 2) {
        threadgroup bfloat gates[2 * COLS];
        int packed[2];
        float sums[2] = {0.0f, 0.0f};
        for (uint row = 0; row < route_count; ++row)
            packed[row] = routes[first_route + row];
        const uint weight_row = output_origin + lane;
        for (uint g = 0; g < groups; ++g) {
            const ulong tiled = grouped_m8_tiled_index(weight_row, g, groups);
            const uint4 low = *reinterpret_cast<device const uint4 *>(
                expert_weights + tiled * 16);
            bfloat4 decoded[8];
            if constexpr (Q6) {
                const uint2 high = *reinterpret_cast<device const uint2 *>(
                    expert_weights + p.plane1_offset + tiled * 8);
                device const uchar *meta = expert_weights + p.meta_offset +
                    grouped_m8_tiled_index(weight_row, g / 8, units) * 20;
                const packed_uint4 scales =
                    *reinterpret_cast<device const packed_uint4 *>(meta);
                const uint dh = *reinterpret_cast<device const uint *>(meta + 16);
                grouped_m8_decode_q6_thread(
                    low, high, scales, dh, ushort(g % 8),
                    reinterpret_cast<thread bfloat *>(decoded));
            } else {
                const uint4 meta = *reinterpret_cast<device const uint4 *>(
                    expert_weights + p.meta_offset +
                    grouped_m8_tiled_index(weight_row, g / 8, units) * 16);
                grouped_m8_decode_q4_thread(
                    low, meta, ushort(g % 8),
                    reinterpret_cast<thread bfloat *>(decoded));
            }
            for (uint row = 0; row < route_count; ++row) {
                device const bfloat4 *x = reinterpret_cast<device const bfloat4 *>(
                    input + (uint64_t(first_route) + row) * p.k + g * STEP);
                float group_sum = 0.0f;
#pragma unroll
                for (ushort i = 0; i < 8; ++i) {
                    const float4 xv = float4(x[i]);
                    const float4 wv = float4(decoded[i]);
                    group_sum += wv.x * xv.x;
                    group_sum += wv.y * xv.y;
                    group_sum += wv.z * xv.z;
                    group_sum += wv.w * xv.w;
                }
                sums[row] += group_sum;
            }
        }
        if (!up)
            for (uint row = 0; row < route_count; ++row)
                gates[row * COLS + lane] = bfloat(sums[row]);
        threadgroup_barrier(mem_flags::mem_threadgroup);
        if (up) {
            for (uint row = 0; row < route_count; ++row) {
                const int64_t at = int64_t(packed[row]) * p.n + weight_row;
                const float gate = float(gates[row * COLS + lane]);
                const float up_value = float(bfloat(sums[row]));
                output[at] = bfloat((gate / (1.0f + exp(-gate))) * up_value);
            }
        }
        return;
    }

    threadgroup bfloat weight_tile[2 * COLS * STEP];
    threadgroup float result_tile[2 * ROWS * COLS];
    threadgroup int packed_routes[ROWS];
    threadgroup ushort live_rows;
    matmul2d<matmul2d_descriptor(
        ROWS, COLS, STEP, false, true, false,
        matmul2d_descriptor::mode::multiply_accumulate),
        execution_simdgroups<1>> mm;

    for (uint route_base = 0; route_base < route_count; route_base += ROWS) {
        if (thread_index == 0)
            live_rows = ushort(min(ROWS, route_count - route_base));
        if (thread_index < ROWS)
            packed_routes[thread_index] =
                thread_index < min(ROWS, route_count - route_base)
                    ? routes[first_route + route_base + thread_index]
                    : -1;
        threadgroup_barrier(mem_flags::mem_threadgroup);

        auto all_left = tensor(
            input + (uint64_t(first_route) + route_base) * p.k,
            dextents<int32_t, 2>{p.k, ROWS},
            array<int32_t, 2>{1, p.k});
        auto first_left = all_left.template slice<STEP, ROWS>(0, 0);
        threadgroup bfloat *simd_weight =
            weight_tile + uint(simdgroup) * COLS * STEP;
        auto right = tensor<threadgroup bfloat, dextents<int32_t, 2>, tensor_inline>(
            simd_weight, dextents<int32_t, 2>{STEP, COLS},
            array<int32_t, 2>{1, STEP});
        auto sum = mm.get_destination_cooperative_tensor<
            decltype(first_left), decltype(right), float>();
        for (ushort i = 0; i < sum.get_capacity(); ++i)
            sum[i] = 0.0f;

        for (uint g = 0; g < groups; ++g) {
            const uint weight_row = output_origin + lane;
            const ulong tiled = grouped_m8_tiled_index(weight_row, g, groups);
            const uint4 low = *reinterpret_cast<device const uint4 *>(
                expert_weights + tiled * 16);
            if constexpr (Q6) {
                const uint2 high = *reinterpret_cast<device const uint2 *>(
                    expert_weights + p.plane1_offset + tiled * 8);
                device const uchar *meta = expert_weights + p.meta_offset +
                    grouped_m8_tiled_index(weight_row, g / 8, units) * 20;
                const packed_uint4 scales =
                    *reinterpret_cast<device const packed_uint4 *>(meta);
                const uint dh = *reinterpret_cast<device const uint *>(meta + 16);
                grouped_m8_decode_q6(
                    low, high, scales, dh, ushort(g % 8), 0,
                    simd_weight + lane * STEP);
                grouped_m8_decode_q6(
                    low, high, scales, dh, ushort(g % 8), 1,
                    simd_weight + lane * STEP + 16);
            } else {
                const uint4 meta = *reinterpret_cast<device const uint4 *>(
                    expert_weights + p.meta_offset +
                    grouped_m8_tiled_index(weight_row, g / 8, units) * 16);
                grouped_m8_decode_q4(
                    low, meta, ushort(g % 8), 0,
                    simd_weight + lane * STEP);
                grouped_m8_decode_q4(
                    low, meta, ushort(g % 8), 1,
                    simd_weight + lane * STEP + 16);
            }
            simdgroup_barrier(mem_flags::mem_threadgroup);
            auto left = all_left.template slice<STEP, ROWS>(g * STEP, 0);
            mm.run(left, right, sum);
            simdgroup_barrier(mem_flags::mem_threadgroup);
        }

        threadgroup float *simd_result =
            result_tile + uint(simdgroup) * ROWS * COLS;
        auto stored = tensor<threadgroup float, dextents<int32_t, 2>, tensor_inline>(
            simd_result, dextents<int32_t, 2>{COLS, ROWS},
            array<int32_t, 2>{1, COLS});
        sum.store(stored);
        threadgroup_barrier(mem_flags::mem_threadgroup);
        if (up) {
            for (uint index = lane; index < uint(live_rows) * COLS; index += 32) {
                const uint row = index / COLS;
                const uint column = index % COLS;
                const int64_t at =
                    int64_t(packed_routes[row]) * p.n + output_origin + column;
                const float gate = float(bfloat(result_tile[row * COLS + column]));
                const float up_value =
                    float(bfloat(result_tile[ROWS * COLS + row * COLS + column]));
                output[at] =
                    bfloat((gate / (1.0f + exp(-gate))) * up_value);
            }
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }
}


template [[host_name("grouped_m8_id_q4_K_f32")]]
kernel void grouped_m8_impl<false, M8_EP_F32>(
    device const uchar *, device bfloat *, device uchar *,
    device const uint32_t *, device const int32_t *,
    constant GroupedM8Params &, device const bfloat *, uint2, ushort, ushort, ushort);

template [[host_name("grouped_m8_id_q6_K_f32")]]
kernel void grouped_m8_impl<true, M8_EP_F32>(
    device const uchar *, device bfloat *, device uchar *,
    device const uint32_t *, device const int32_t *,
    constant GroupedM8Params &, device const bfloat *, uint2, ushort, ushort, ushort);

template [[host_name("grouped_m8_id_q4_K_bf16")]]
kernel void grouped_m8_impl<false, M8_EP_BF16>(
    device const uchar *, device bfloat *, device uchar *,
    device const uint32_t *, device const int32_t *,
    constant GroupedM8Params &, device const bfloat *, uint2, ushort, ushort, ushort);

template [[host_name("grouped_m8_id_q6_K_bf16")]]
kernel void grouped_m8_impl<true, M8_EP_BF16>(
    device const uchar *, device bfloat *, device uchar *,
    device const uint32_t *, device const int32_t *,
    constant GroupedM8Params &, device const bfloat *, uint2, ushort, ushort, ushort);

template [[host_name("grouped_m8_id_q4_K_silu_mul_bf16")]]
kernel void grouped_m8_impl<false, M8_EP_SILU_MUL_BF16>(
    device const uchar *, device bfloat *, device uchar *,
    device const uint32_t *, device const int32_t *,
    constant GroupedM8Params &, device const bfloat *, uint2, ushort, ushort, ushort);

template [[host_name("grouped_m8_id_q6_K_silu_mul_bf16")]]
kernel void grouped_m8_impl<true, M8_EP_SILU_MUL_BF16>(
    device const uchar *, device bfloat *, device uchar *,
    device const uint32_t *, device const int32_t *,
    constant GroupedM8Params &, device const bfloat *, uint2, ushort, ushort, ushort);

template [[host_name("grouped_m8_gate_up_q4_K_bf16")]]
kernel void grouped_m8_gate_up_impl<false>(
    device const uchar *, device const uchar *, device bfloat *, device bfloat *,
    device const uint32_t *, device const int32_t *,
    constant GroupedM8Params &, uint2, ushort, ushort, ushort);

template [[host_name("grouped_m8_gate_up_q6_K_bf16")]]
kernel void grouped_m8_gate_up_impl<true>(
    device const uchar *, device const uchar *, device bfloat *, device bfloat *,
    device const uint32_t *, device const int32_t *,
    constant GroupedM8Params &, uint2, ushort, ushort, ushort);