ferrum-kernels 0.8.7

Unified compute kernels (CUDA/Metal/CPU) and model runner for Ferrum inference
Documentation
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
#include <metal_stdlib>
using namespace metal;

struct MoeRouteParams {
    uint tokens;
    uint expert_count;
    uint experts_per_token;
    uint normalize_topk;
};

kernel void vnext_moe_route_topk_f16(
    device const half *logits [[buffer(0)]],
    device int *route_ids [[buffer(1)]],
    device float *route_weights [[buffer(2)]],
    constant MoeRouteParams &p [[buffer(3)]],
    threadgroup float *scratch [[threadgroup(0)]],
    uint3 group [[threadgroup_position_in_grid]],
    ushort lane [[thread_index_in_simdgroup]]) {
    const uint token = group.x;
    if (token >= p.tokens) {
        return;
    }

    threadgroup float *probabilities = scratch;
    threadgroup float *selected_weights = probabilities + p.expert_count;
    threadgroup int *selected_ids =
        reinterpret_cast<threadgroup int *>(selected_weights + p.experts_per_token);
    threadgroup float *selected_sum =
        reinterpret_cast<threadgroup float *>(selected_ids + p.experts_per_token);

    float local_max = -INFINITY;
    for (uint expert = lane; expert < p.expert_count; expert += 32) {
        const float raw = float(logits[token * p.expert_count + expert]);
        const float value = isfinite(raw)
            ? raw
            : (raw > 0.0f ? 65504.0f : -65504.0f);
        probabilities[expert] = value;
        local_max = max(local_max, value);
    }
    const float row_max = simd_max(local_max);

    float local_sum = 0.0f;
    for (uint expert = lane; expert < p.expert_count; expert += 32) {
        const float value = exp(probabilities[expert] - row_max);
        probabilities[expert] = value;
        local_sum += value;
    }
    const float inverse_sum = 1.0f / max(simd_sum(local_sum), 0x1.0p-14f);
    for (uint expert = lane; expert < p.expert_count; expert += 32) {
        probabilities[expert] *= inverse_sum;
    }
    if (lane == 0) {
        selected_sum[0] = 0.0f;
    }
    threadgroup_barrier(mem_flags::mem_threadgroup);

    for (uint slot = 0; slot < p.experts_per_token; ++slot) {
        float local_best = -INFINITY;
        int local_id = -1;
        for (uint expert = lane; expert < p.expert_count; expert += 32) {
            const float value = probabilities[expert];
            if (value > local_best) {
                local_best = value;
                local_id = int(expert);
            }
        }
        const float best = simd_max(local_best);
        const int candidate = local_best == best ? local_id : 0x7fffffff;
        const int selected = simd_min(candidate);
        if (lane == 0) {
            selected_weights[slot] = best;
            selected_ids[slot] = selected;
            selected_sum[0] += best;
            probabilities[selected] = -INFINITY;
        }
        threadgroup_barrier(mem_flags::mem_threadgroup);
    }

    if (uint(lane) < p.experts_per_token) {
        const float scale = p.normalize_topk == 0
            ? 1.0f
            : 1.0f / max(selected_sum[0], 0x1.0p-14f);
        const uint output = token * p.experts_per_token + uint(lane);
        route_ids[output] = selected_ids[lane];
        route_weights[output] = selected_weights[lane] * scale;
    }
}

#define VNEXT_QK_K 256
#define VNEXT_MOE_ROWS_PER_SIMDGROUP 2
#define VNEXT_MOE_SIMDGROUPS 2
#define VNEXT_UNROLL _Pragma("clang loop unroll(full)")

struct VNextBlockQ4K {
    half d;
    half dmin;
    uchar scales[12];
    uchar qs[VNEXT_QK_K / 2];
};

struct MoeQ4GateUpParams {
    uint output_features;
    uint input_features;
    uint row_stride_bytes;
    uint expert_stride_bytes;
    uint expert_count;
    uint experts_per_token;
    uint pair_count;
};

kernel void vnext_moe_q4k_gate_up_silu_f16(
    device const VNextBlockQ4K *gate_weights [[buffer(0)]],
    device const VNextBlockQ4K *up_weights [[buffer(1)]],
    device const half *input [[buffer(2)]],
    device const int *route_ids [[buffer(3)]],
    device half *activation [[buffer(4)]],
    constant MoeQ4GateUpParams &p [[buffer(5)]],
    uint3 group [[threadgroup_position_in_grid]],
    ushort lane [[thread_index_in_simdgroup]],
    ushort simdgroup [[simdgroup_index_in_threadgroup]]) {
    const uint pair = group.z;
    if (pair >= p.pair_count) {
        return;
    }
    const int expert = route_ids[pair];
    if (expert < 0 || uint(expert) >= p.expert_count) {
        return;
    }
    const uint token = pair / p.experts_per_token;

    constexpr uint16_t scale_mask = 0x3f3f;
    constexpr uint16_t low_mask = 0x0f0f;
    constexpr uint16_t high_mask = 0xc0c0;
    const short block_lane = lane / 8;
    const short item = lane % 8;
    const short quant_group = item / 4;
    const short quant_item = item % 4;
    const uint blocks_per_row = p.input_features / VNEXT_QK_K;
    const uint first_row =
        (group.x * VNEXT_MOE_SIMDGROUPS + simdgroup) * VNEXT_MOE_ROWS_PER_SIMDGROUP;
    if (first_row >= p.output_features) {
        return;
    }

    device const VNextBlockQ4K *gate =
        reinterpret_cast<device const VNextBlockQ4K *>(
            reinterpret_cast<device const char *>(gate_weights)
            + uint(expert) * p.expert_stride_bytes
            + first_row * p.row_stride_bytes);
    device const VNextBlockQ4K *up =
        reinterpret_cast<device const VNextBlockQ4K *>(
            reinterpret_cast<device const char *>(up_weights)
            + uint(expert) * p.expert_stride_bytes
            + first_row * p.row_stride_bytes);
    device const half *input_row = input + token * p.input_features;
    device const half *input_block =
        input_row + block_lane * VNEXT_QK_K + 64 * quant_group + 8 * quant_item;

    float low[16];
    float high[16];
    float gate_sum[VNEXT_MOE_ROWS_PER_SIMDGROUP] = {0.0f};
    float up_sum[VNEXT_MOE_ROWS_PER_SIMDGROUP] = {0.0f};
    uint16_t unpacked_scales[4];
    thread const uint8_t *scales8 =
        reinterpret_cast<thread const uint8_t *>(unpacked_scales);

    for (uint block = uint(block_lane); block < blocks_per_row; block += 4) {
        float4 input_sums = 0.0f;
        for (short index = 0; index < 8; ++index) {
            low[index] = float(input_block[index]);
            input_sums[0] += low[index];
            low[index + 8] = float(input_block[index + 32]);
            input_sums[1] += low[index + 8];
            high[index] = float(input_block[index + 128]);
            input_sums[2] += high[index];
            high[index + 8] = float(input_block[index + 160]);
            input_sums[3] += high[index + 8];
        }

        device const uint16_t *gate_scales =
            reinterpret_cast<device const uint16_t *>(gate[block].scales) + quant_group;
        device const uint16_t *gate_quants =
            reinterpret_cast<device const uint16_t *>(gate[block].qs)
            + 16 * quant_group + 4 * quant_item;
        device const half *gate_delta = &gate[block].d;
        device const uint16_t *up_scales =
            reinterpret_cast<device const uint16_t *>(up[block].scales) + quant_group;
        device const uint16_t *up_quants =
            reinterpret_cast<device const uint16_t *>(up[block].qs)
            + 16 * quant_group + 4 * quant_item;
        device const half *up_delta = &up[block].d;

        for (short row = 0; row < VNEXT_MOE_ROWS_PER_SIMDGROUP; ++row) {
            unpacked_scales[0] = gate_scales[0] & scale_mask;
            unpacked_scales[1] = gate_scales[2] & scale_mask;
            unpacked_scales[2] = ((gate_scales[4] >> 0) & low_mask)
                | ((gate_scales[0] & high_mask) >> 2);
            unpacked_scales[3] = ((gate_scales[4] >> 4) & low_mask)
                | ((gate_scales[2] & high_mask) >> 2);
            device const uint16_t *gate_high_quants = gate_quants + 32;
            float4 gate_acc_low = 0.0f;
            float4 gate_acc_high = 0.0f;
            VNEXT_UNROLL for (short index = 0; index < 4; ++index) {
                gate_acc_low[0] += low[2 * index] * (gate_quants[index] & 0x000f);
                gate_acc_low[1] += low[2 * index + 1] * (gate_quants[index] & 0x0f00);
                gate_acc_low[2] += low[2 * index + 8] * (gate_quants[index] & 0x00f0);
                gate_acc_low[3] += low[2 * index + 9] * (gate_quants[index] & 0xf000);
                gate_acc_high[0] += high[2 * index] * (gate_high_quants[index] & 0x000f);
                gate_acc_high[1] += high[2 * index + 1] * (gate_high_quants[index] & 0x0f00);
                gate_acc_high[2] += high[2 * index + 8] * (gate_high_quants[index] & 0x00f0);
                gate_acc_high[3] += high[2 * index + 9] * (gate_high_quants[index] & 0xf000);
            }
            gate_sum[row] += float(gate_delta[0]) * (
                (gate_acc_low[0] + gate_acc_low[1] / 256.0f) * scales8[0]
                + (gate_acc_low[2] + gate_acc_low[3] / 256.0f) * scales8[1] / 16.0f
                + (gate_acc_high[0] + gate_acc_high[1] / 256.0f) * scales8[4]
                + (gate_acc_high[2] + gate_acc_high[3] / 256.0f) * scales8[5] / 16.0f)
                - float(gate_delta[1]) * (
                    input_sums[0] * scales8[2] + input_sums[1] * scales8[3]
                    + input_sums[2] * scales8[6] + input_sums[3] * scales8[7]);

            unpacked_scales[0] = up_scales[0] & scale_mask;
            unpacked_scales[1] = up_scales[2] & scale_mask;
            unpacked_scales[2] = ((up_scales[4] >> 0) & low_mask)
                | ((up_scales[0] & high_mask) >> 2);
            unpacked_scales[3] = ((up_scales[4] >> 4) & low_mask)
                | ((up_scales[2] & high_mask) >> 2);
            device const uint16_t *up_high_quants = up_quants + 32;
            float4 up_acc_low = 0.0f;
            float4 up_acc_high = 0.0f;
            VNEXT_UNROLL for (short index = 0; index < 4; ++index) {
                up_acc_low[0] += low[2 * index] * (up_quants[index] & 0x000f);
                up_acc_low[1] += low[2 * index + 1] * (up_quants[index] & 0x0f00);
                up_acc_low[2] += low[2 * index + 8] * (up_quants[index] & 0x00f0);
                up_acc_low[3] += low[2 * index + 9] * (up_quants[index] & 0xf000);
                up_acc_high[0] += high[2 * index] * (up_high_quants[index] & 0x000f);
                up_acc_high[1] += high[2 * index + 1] * (up_high_quants[index] & 0x0f00);
                up_acc_high[2] += high[2 * index + 8] * (up_high_quants[index] & 0x00f0);
                up_acc_high[3] += high[2 * index + 9] * (up_high_quants[index] & 0xf000);
            }
            up_sum[row] += float(up_delta[0]) * (
                (up_acc_low[0] + up_acc_low[1] / 256.0f) * scales8[0]
                + (up_acc_low[2] + up_acc_low[3] / 256.0f) * scales8[1] / 16.0f
                + (up_acc_high[0] + up_acc_high[1] / 256.0f) * scales8[4]
                + (up_acc_high[2] + up_acc_high[3] / 256.0f) * scales8[5] / 16.0f)
                - float(up_delta[1]) * (
                    input_sums[0] * scales8[2] + input_sums[1] * scales8[3]
                    + input_sums[2] * scales8[6] + input_sums[3] * scales8[7]);

            gate_quants += p.row_stride_bytes / 2;
            gate_scales += p.row_stride_bytes / 2;
            gate_delta += p.row_stride_bytes / 2;
            up_quants += p.row_stride_bytes / 2;
            up_scales += p.row_stride_bytes / 2;
            up_delta += p.row_stride_bytes / 2;
        }
        input_block += 4 * VNEXT_QK_K;
    }

    device half *output = activation + pair * p.output_features;
    for (short row = 0;
         row < VNEXT_MOE_ROWS_PER_SIMDGROUP && first_row + uint(row) < p.output_features;
         ++row) {
        const float gate_value = simd_sum(gate_sum[row]);
        const float up_value = simd_sum(up_sum[row]);
        if (lane == 0) {
            output[first_row + uint(row)] = half(
                (gate_value / (1.0f + exp(-gate_value))) * up_value);
        }
    }
}

struct MoeQ4DownParams {
    uint output_features;
    uint input_features;
    uint row_stride_bytes;
    uint expert_stride_bytes;
    uint expert_count;
    uint experts_per_token;
    uint pair_count;
};

kernel void vnext_moe_q4k_down_f16(
    device const VNextBlockQ4K *weights [[buffer(0)]],
    device const half *activation [[buffer(1)]],
    device const int *route_ids [[buffer(2)]],
    device half *down_slots [[buffer(3)]],
    constant MoeQ4DownParams &p [[buffer(4)]],
    uint3 group [[threadgroup_position_in_grid]],
    ushort lane [[thread_index_in_simdgroup]],
    ushort simdgroup [[simdgroup_index_in_threadgroup]]) {
    const uint pair = group.z;
    if (pair >= p.pair_count) {
        return;
    }
    const int expert = route_ids[pair];
    if (expert < 0 || uint(expert) >= p.expert_count) {
        return;
    }

    constexpr uint16_t scale_mask = 0x3f3f;
    constexpr uint16_t low_mask = 0x0f0f;
    constexpr uint16_t high_mask = 0xc0c0;
    const short block_lane = lane / 8;
    const short item = lane % 8;
    const short quant_group = item / 4;
    const short quant_item = item % 4;
    const uint blocks_per_row = p.input_features / VNEXT_QK_K;
    const uint first_row =
        (group.x * VNEXT_MOE_SIMDGROUPS + simdgroup) * VNEXT_MOE_ROWS_PER_SIMDGROUP;
    if (first_row >= p.output_features) {
        return;
    }

    device const VNextBlockQ4K *expert_weights =
        reinterpret_cast<device const VNextBlockQ4K *>(
            reinterpret_cast<device const char *>(weights)
            + uint(expert) * p.expert_stride_bytes
            + first_row * p.row_stride_bytes);
    device const half *input = activation + pair * p.input_features;
    device const half *input_block =
        input + block_lane * VNEXT_QK_K + 64 * quant_group + 8 * quant_item;
    float low[16];
    float high[16];
    float sums[VNEXT_MOE_ROWS_PER_SIMDGROUP] = {0.0f};
    uint16_t unpacked_scales[4];
    thread const uint8_t *scales8 =
        reinterpret_cast<thread const uint8_t *>(unpacked_scales);

    for (uint block = uint(block_lane); block < blocks_per_row; block += 4) {
        float4 input_sums = 0.0f;
        for (short index = 0; index < 8; ++index) {
            low[index] = float(input_block[index]);
            input_sums[0] += low[index];
            low[index + 8] = float(input_block[index + 32]);
            input_sums[1] += low[index + 8];
            high[index] = float(input_block[index + 128]);
            input_sums[2] += high[index];
            high[index + 8] = float(input_block[index + 160]);
            input_sums[3] += high[index + 8];
        }
        device const uint16_t *scales =
            reinterpret_cast<device const uint16_t *>(expert_weights[block].scales)
            + quant_group;
        device const uint16_t *quants =
            reinterpret_cast<device const uint16_t *>(expert_weights[block].qs)
            + 16 * quant_group + 4 * quant_item;
        device const half *delta = &expert_weights[block].d;
        for (short row = 0; row < VNEXT_MOE_ROWS_PER_SIMDGROUP; ++row) {
            unpacked_scales[0] = scales[0] & scale_mask;
            unpacked_scales[1] = scales[2] & scale_mask;
            unpacked_scales[2] = ((scales[4] >> 0) & low_mask)
                | ((scales[0] & high_mask) >> 2);
            unpacked_scales[3] = ((scales[4] >> 4) & low_mask)
                | ((scales[2] & high_mask) >> 2);
            device const uint16_t *high_quants = quants + 32;
            float4 acc_low = 0.0f;
            float4 acc_high = 0.0f;
            VNEXT_UNROLL for (short index = 0; index < 4; ++index) {
                acc_low[0] += low[2 * index] * (quants[index] & 0x000f);
                acc_low[1] += low[2 * index + 1] * (quants[index] & 0x0f00);
                acc_low[2] += low[2 * index + 8] * (quants[index] & 0x00f0);
                acc_low[3] += low[2 * index + 9] * (quants[index] & 0xf000);
                acc_high[0] += high[2 * index] * (high_quants[index] & 0x000f);
                acc_high[1] += high[2 * index + 1] * (high_quants[index] & 0x0f00);
                acc_high[2] += high[2 * index + 8] * (high_quants[index] & 0x00f0);
                acc_high[3] += high[2 * index + 9] * (high_quants[index] & 0xf000);
            }
            sums[row] += float(delta[0]) * (
                (acc_low[0] + acc_low[1] / 256.0f) * scales8[0]
                + (acc_low[2] + acc_low[3] / 256.0f) * scales8[1] / 16.0f
                + (acc_high[0] + acc_high[1] / 256.0f) * scales8[4]
                + (acc_high[2] + acc_high[3] / 256.0f) * scales8[5] / 16.0f)
                - float(delta[1]) * (
                    input_sums[0] * scales8[2] + input_sums[1] * scales8[3]
                    + input_sums[2] * scales8[6] + input_sums[3] * scales8[7]);
            quants += p.row_stride_bytes / 2;
            scales += p.row_stride_bytes / 2;
            delta += p.row_stride_bytes / 2;
        }
        input_block += 4 * VNEXT_QK_K;
    }

    device half *output = down_slots + pair * p.output_features;
    for (short row = 0;
         row < VNEXT_MOE_ROWS_PER_SIMDGROUP && first_row + uint(row) < p.output_features;
         ++row) {
        const float value = simd_sum(sums[row]);
        if (lane == 0) {
            output[first_row + uint(row)] = half(value);
        }
    }
}

// Q6_K routed expert down projection. This keeps both the expert stack and
// the activation/output ABI native: packed Q6_K bytes in, F16 slots out.
struct VNextBlockQ6K {
    uchar ql[VNEXT_QK_K / 2];
    uchar qh[VNEXT_QK_K / 4];
    int8_t scales[VNEXT_QK_K / 16];
    half d;
};

kernel void vnext_moe_q6k_down_f16(
    device const VNextBlockQ6K *weights [[buffer(0)]],
    device const half *activation [[buffer(1)]],
    device const int *route_ids [[buffer(2)]],
    device half *down_slots [[buffer(3)]],
    constant MoeQ4DownParams &p [[buffer(4)]],
    uint3 group [[threadgroup_position_in_grid]],
    ushort lane [[thread_index_in_simdgroup]],
    ushort simdgroup [[simdgroup_index_in_threadgroup]]) {
    const uint pair = group.z;
    if (pair >= p.pair_count) {
        return;
    }
    const int expert = route_ids[pair];
    if (expert < 0 || uint(expert) >= p.expert_count) {
        return;
    }

    constexpr uint8_t mask_0 = 0x03;
    constexpr uint8_t mask_1 = 0x0c;
    constexpr uint8_t mask_2 = 0x30;
    constexpr uint8_t mask_3 = 0xc0;
    const uint blocks_per_row = p.input_features / VNEXT_QK_K;
    const uint first_row =
        (group.x * VNEXT_MOE_SIMDGROUPS + simdgroup) * VNEXT_MOE_ROWS_PER_SIMDGROUP;
    if (first_row >= p.output_features) {
        return;
    }

    device const VNextBlockQ6K *expert_weights =
        reinterpret_cast<device const VNextBlockQ6K *>(
            reinterpret_cast<device const char *>(weights)
            + uint(expert) * p.expert_stride_bytes
            + first_row * p.row_stride_bytes);
    device const half *input = activation + pair * p.input_features;
    float sums[VNEXT_MOE_ROWS_PER_SIMDGROUP] = {0.0f};
    float inputs[16];

    const short lane_pair = lane / 2;
    const short block_parity = lane % 2;
    const short input_partition = lane_pair / 8;
    const short input_lane = lane_pair % 8;
    const short lane_offset = 4 * input_lane;
    const short scale_offset = 8 * input_partition + lane_offset / 16;
    const short input_offset = 128 * input_partition + lane_offset;
    const short low_offset = 64 * input_partition + lane_offset;
    const short high_offset = 32 * input_partition + lane_offset;

    for (uint block = uint(block_parity); block < blocks_per_row; block += 2) {
        device const uchar *low = expert_weights[block].ql + low_offset;
        device const uchar *low_second = low + 32;
        device const uchar *high = expert_weights[block].qh + high_offset;
        device const int8_t *scales = expert_weights[block].scales + scale_offset;
        device const half *delta = &expert_weights[block].d;
        device const half *input_block = input + block * VNEXT_QK_K + input_offset;

        VNEXT_UNROLL for (short index = 0; index < 4; ++index) {
            inputs[4 * index + 0] = float(input_block[index + 0]);
            inputs[4 * index + 1] = float(input_block[index + 32]);
            inputs[4 * index + 2] = float(input_block[index + 64]);
            inputs[4 * index + 3] = float(input_block[index + 96]);
        }

        for (short row = 0; row < VNEXT_MOE_ROWS_PER_SIMDGROUP; ++row) {
            float4 partial = 0.0f;
            VNEXT_UNROLL for (short index = 0; index < 4; ++index) {
                partial[0] += inputs[4 * index + 0]
                    * (int8_t((low[index] & 0x0f) | ((high[index] & mask_0) << 4)) - 32);
                partial[1] += inputs[4 * index + 1]
                    * (int8_t((low_second[index] & 0x0f) | ((high[index] & mask_1) << 2)) - 32);
                partial[2] += inputs[4 * index + 2]
                    * (int8_t((low[index] >> 4) | (high[index] & mask_2)) - 32);
                partial[3] += inputs[4 * index + 3]
                    * (int8_t((low_second[index] >> 4) | ((high[index] & mask_3) >> 2)) - 32);
            }
            sums[row] += float(delta[0]) * (
                partial[0] * float(scales[0])
                + partial[1] * float(scales[2])
                + partial[2] * float(scales[4])
                + partial[3] * float(scales[6]));
            low += p.row_stride_bytes;
            low_second += p.row_stride_bytes;
            high += p.row_stride_bytes;
            scales += p.row_stride_bytes;
            delta += p.row_stride_bytes / 2;
        }
    }

    device half *output = down_slots + pair * p.output_features;
    for (short row = 0;
         row < VNEXT_MOE_ROWS_PER_SIMDGROUP && first_row + uint(row) < p.output_features;
         ++row) {
        const float value = simd_sum(sums[row]);
        if (lane == 0) {
            output[first_row + uint(row)] = half(value);
        }
    }
}

struct MoeCombineParams {
    uint tokens;
    uint experts_per_token;
    uint hidden_size;
};

kernel void vnext_moe_combine_routed_f16(
    device const half *routed_slots [[buffer(0)]],
    device const float *route_weights [[buffer(1)]],
    device half *output [[buffer(2)]],
    constant MoeCombineParams &p [[buffer(3)]],
    uint index [[thread_position_in_grid]]) {
    const uint element_count = p.tokens * p.hidden_size;
    if (index >= element_count) {
        return;
    }
    const uint token = index / p.hidden_size;
    const uint column = index - token * p.hidden_size;
    float value = 0.0f;
    for (uint slot = 0; slot < p.experts_per_token; ++slot) {
        const uint pair = token * p.experts_per_token + slot;
        value += route_weights[pair]
            * float(routed_slots[pair * p.hidden_size + column]);
    }
    output[index] = half(value);
}

kernel void vnext_moe_combine_f16(
    device const half *routed_slots [[buffer(0)]],
    device const float *route_weights [[buffer(1)]],
    device const half *shared_gate [[buffer(2)]],
    device const half *shared_output [[buffer(3)]],
    device half *output [[buffer(4)]],
    constant MoeCombineParams &p [[buffer(5)]],
    uint index [[thread_position_in_grid]]) {
    const uint element_count = p.tokens * p.hidden_size;
    if (index >= element_count) {
        return;
    }
    const uint token = index / p.hidden_size;
    const uint column = index - token * p.hidden_size;
    float value = 0.0f;
    for (uint slot = 0; slot < p.experts_per_token; ++slot) {
        const uint pair = token * p.experts_per_token + slot;
        value += route_weights[pair]
            * float(routed_slots[pair * p.hidden_size + column]);
    }
    const float gate = float(shared_gate[token]);
    value += (1.0f / (1.0f + exp(-gate))) * float(shared_output[index]);
    output[index] = half(value);
}