Repository navigation
Expand file tree
/
Copy pathdense.metal
More file actions
More file actions
4433 lines (3721 loc) · 168 KB
/
Copy pathdense.metal
File metadata and controls
4433 lines (3721 loc) · 168 KB
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// DS4 Metal matvec kernels used by generation.
constant short FC_mul_mv_nsg [[function_constant(FC_MUL_MV + 0)]];
constant short FC_mul_mv_nxpsg [[function_constant(FC_MUL_MV + 1)]];
// Round to the nearest bfloat16 value in f32 storage (DeepSeek V4.1 keeps its
// activations at bf16 precision between operators).
static inline float ds4_bf16_round(float v) {
uint bits = as_type<uint>(v);
if ((bits & 0x7f800000u) != 0x7f800000u) bits += 0x7fffu + ((bits >> 16u) & 1u);
return as_type<float>(bits & 0xffff0000u);
}
static inline float4 ds4_bf16_round(float4 v) {
uint4 bits = as_type<uint4>(v);
const bool4 finite = (bits & 0x7f800000u) != 0x7f800000u;
bits += select(uint4(0), uint4(0x7fffu) + ((bits >> 16u) & 1u), finite);
return as_type<float4>(bits & 0xffff0000u);
}
struct ds4_metal_args_mul_mv {
int ne00;
int ne01;
int ne02;
ulong nb00;
ulong nb01;
ulong nb02;
ulong nb03;
int ne10;
int ne11;
int ne12;
ulong nb10;
ulong nb11;
ulong nb12;
ulong nb13;
int ne0;
int ne1;
int nr0;
short r2;
short r3;
};
struct ds4_metal_args_compressor_pair_store {
uint32_t width;
uint32_t ratio;
uint32_t pos;
uint32_t ape_type;
};
struct ds4_metal_args_mul_mm {
int32_t ne00;
int32_t ne02;
uint64_t nb01;
uint64_t nb02;
uint64_t nb03;
int32_t ne12;
uint64_t nb10;
uint64_t nb11;
uint64_t nb12;
uint64_t nb13;
int32_t ne0;
int32_t ne1;
int16_t r2;
int16_t r3;
};
struct ds4_metal_args_mul_mv_ext {
int32_t ne00;
int32_t ne01;
int32_t ne02;
uint64_t nb00;
uint64_t nb01;
uint64_t nb02;
uint64_t nb03;
int32_t ne10;
int32_t ne11;
int32_t ne12;
uint64_t nb10;
uint64_t nb11;
uint64_t nb12;
uint64_t nb13;
int32_t ne0;
int32_t ne1;
int16_t r2;
int16_t r3;
};
template<short NR0, bool ROUND = false>
static inline void helper_mv_reduce_and_write(
device float * dst_f32,
float sumf[NR0],
const int r0,
const int ne01,
ushort tiisg,
ushort sgitg,
threadgroup char * shmem) {
constexpr short NW = N_SIMDWIDTH;
threadgroup float * shmem_f32[NR0];
for (short row = 0; row < NR0; ++row) {
shmem_f32[row] = (threadgroup float *) shmem + NW*row;
if (sgitg == 0) {
shmem_f32[row][tiisg] = 0.0f;
}
sumf[row] = simd_sum(sumf[row]);
}
threadgroup_barrier(mem_flags::mem_threadgroup);
for (short row = 0; row < NR0; ++row) {
if (tiisg == 0) {
shmem_f32[row][sgitg] = sumf[row];
}
}
threadgroup_barrier(mem_flags::mem_threadgroup);
for (short row = 0; row < NR0 && r0 + row < ne01; ++row) {
float tot = simd_sum(shmem_f32[row][tiisg]);
if (tiisg == 0 && sgitg == 0) {
dst_f32[r0 + row] = ROUND ? ds4_bf16_round(tot) : tot;
}
}
}
// Q8's single-use reduction keeps the same two simd_sum stages and the
// same positive-zero lanes as helper_mv_reduce_and_write. Each SIMD group
// publishes only its own partial; padding is supplied in registers instead
// of clearing shared memory before a separate barrier. Only group 0 needs
// the final reduction. No caller may reuse this scratch before another
// threadgroup barrier: the other groups can finish before group 0 reads it.
template<short NR0>
static inline void helper_q8_mv_reduce_and_write(
device float * dst_f32,
float sumf[NR0],
const int r0,
const int ne01,
ushort tiisg,
ushort sgitg,
threadgroup char * shmem) {
constexpr short NW = N_SIMDWIDTH;
const short NSG = FC_mul_mv_nsg;
threadgroup float * partials = (threadgroup float *) shmem;
for (short row = 0; row < NR0; ++row) {
sumf[row] = simd_sum(sumf[row]);
if (NSG > 1 && tiisg == 0) {
partials[NW*row + sgitg] = sumf[row];
}
}
if (NSG > 1) {
threadgroup_barrier(mem_flags::mem_threadgroup);
}
if (sgitg == 0) {
for (short row = 0; row < NR0 && r0 + row < ne01; ++row) {
// Preserve the second reduction even for NSG=1, including its
// positive-zero padding rather than directly storing sumf.
float value = 0.0f;
if (NSG == 1) {
if (tiisg == 0) value = sumf[row];
} else if (tiisg < NSG) {
value = partials[NW*row + tiisg];
}
const float tot = simd_sum(value);
if (tiisg == 0) dst_f32[r0 + row] = tot;
}
}
}
template<short NR0, typename args_t>
void kernel_mul_mv_q8_0_f32_plain_impl(
args_t args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem,
uint3 tgpig,
ushort tiisg,
ushort sgitg) {
const short NSG = FC_mul_mv_nsg;
constexpr short NW = N_SIMDWIDTH;
constexpr short NQ = 8;
const int nb = args.ne00/QK8_0;
const int r0 = tgpig.x*NR0;
const int r1 = tgpig.y;
const int im = tgpig.z;
const uint i12 = im%args.ne12;
const uint i13 = im/args.ne12;
const uint64_t offset1 = r1*args.nb11 + (i12)*args.nb12 + (i13)*args.nb13;
device const float * y = (device const float *) (src1 + offset1);
device const block_q8_0 * ax[NR0];
FOR_UNROLL (short row = 0; row < NR0; ++row) {
const uint64_t offset0 = (r0 + row)*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
ax[row] = (device const block_q8_0 *) ((device char *) src0 + offset0);
}
float sumf[NR0] = { 0.f };
const short ix = tiisg/(NW/NQ);
const short il = tiisg%(NW/NQ);
const int ib0 = sgitg*NQ + ix;
float yl[NQ];
device const float * yb = y + ib0*QK8_0 + il*NQ;
for (int ib = ib0; ib < nb; ib += NSG*NQ) {
// Packed activation loads also permit scalar-aligned tensor views.
const float4 y0 = float4(((device const packed_float4 *)yb)[0]);
const float4 y1 = float4(((device const packed_float4 *)yb)[1]);
FOR_UNROLL (short i = 0; i < 4; ++i) {
yl[i] = y0[i];
yl[i + 4] = y1[i];
}
for (short row = 0; row < NR0; row++) {
device const int8_t * qs = ax[row][ib].qs + il*NQ;
float sumq = 0.f;
// Preserve the reference eight-term loop. Pairing quants as
// char2 changes Metal fast-math code generation and rounding;
// small projection errors can amplify through recurrent decode.
// The full GEMV oracle pins this arithmetic, not just reduction.
FOR_UNROLL (short i = 0; i < NQ; ++i) {
sumq += qs[i] * yl[i];
}
sumf[row] += sumq*ax[row][ib].d;
}
yb += NSG*NQ*QK8_0;
}
device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
helper_q8_mv_reduce_and_write<NR0>(dst_f32, sumf, r0, args.ne01, tiisg, sgitg, shmem);
}
// Rope folded into a matvec: the last 64 columns of every `width`-wide head
// rotate at position `start` (ROPE_IN on the activation as it loads, after
// rounding it to bf16; ROPE_OUT on the bf16 outputs before they store).
struct ds4_metal_args_mv_rope {
uint width;
uint start;
uint inverse;
float frequencies[32];
};
template<short NR0>
static inline void helper_mv_reduce_and_write_rope(
device float * dst_f32,
float sumf[NR0],
const int r0,
const int ne01,
ushort tiisg,
ushort sgitg,
threadgroup char * shmem,
constant ds4_metal_args_mv_rope & rope) {
static_assert(NR0 % 2 == 0, "rope pairs are adjacent output rows");
constexpr short NW = N_SIMDWIDTH;
threadgroup float * shmem_f32[NR0];
for (short row = 0; row < NR0; ++row) {
shmem_f32[row] = (threadgroup float *) shmem + NW*row;
if (sgitg == 0) {
shmem_f32[row][tiisg] = 0.0f;
}
sumf[row] = simd_sum(sumf[row]);
}
threadgroup_barrier(mem_flags::mem_threadgroup);
for (short row = 0; row < NR0; ++row) {
if (tiisg == 0) {
shmem_f32[row][sgitg] = sumf[row];
}
}
threadgroup_barrier(mem_flags::mem_threadgroup);
float tot[NR0];
for (short row = 0; row < NR0; ++row) {
tot[row] = ds4_bf16_round(simd_sum(shmem_f32[row][tiisg]));
}
if (tiisg == 0 && sgitg == 0) {
for (short p = 0; p < NR0 && r0 + p + 1 < ne01; p += 2) {
const uint col = (uint)(r0 + p) % rope.width;
if (col >= rope.width - 64u) {
const float theta = float(rope.sta
10BC8
rt) * rope.frequencies[(col - (rope.width - 64u)) >> 1u];
const float c = precise::cos(theta);
const float s = rope.inverse ? -precise::sin(theta) : precise::sin(theta);
const float re = tot[p], im = tot[p + 1];
dst_f32[r0 + p] = ds4_bf16_round(fma(re, c, -(im * s)));
dst_f32[r0 + p + 1] = ds4_bf16_round(fma(im, c, re * s));
} else {
dst_f32[r0 + p] = tot[p];
dst_f32[r0 + p + 1] = tot[p + 1];
}
}
}
}
// The HC expand (kernel_dsv4_hc_expand4, one token, four streams) applied to
// each bf16 output as it is written: the row joins `add` when has_add and
// spreads into the streams with the same per-stream accumulation order.
struct ds4_metal_args_mv_hc_expand4 {
uint n_embd;
uint has_add;
uint split_stride; /* floats between the rows' post/comb weights */
};
template<short NR0>
static inline void helper_mv_reduce_and_write_hc_expand4(
device float * dst_f32,
float sumf[NR0],
const int r0,
const int ne01,
ushort tiisg,
ushort sgitg,
threadgroup char * shmem,
constant ds4_metal_args_mv_hc_expand4 & hc,
device const float * add,
device const float * residual,
device const float * post,
device const float * comb) {
constexpr short NW = N_SIMDWIDTH;
threadgroup float * shmem_f32[NR0];
for (short row = 0; row < NR0; ++row) {
shmem_f32[row] = (threadgroup float *) shmem + NW*row;
if (sgitg == 0) {
shmem_f32[row][tiisg] = 0.0f;
}
sumf[row] = simd_sum(sumf[row]);
}
threadgroup_barrier(mem_flags::mem_threadgroup);
for (short row = 0; row < NR0; ++row) {
if (tiisg == 0) {
shmem_f32[row][sgitg] = sumf[row];
}
}
threadgroup_barrier(mem_flags::mem_threadgroup);
for (short row = 0; row < NR0 && r0 + row < ne01; ++row) {
float block_v = ds4_bf16_round(simd_sum(shmem_f32[row][tiisg]));
if (tiisg == 0 && sgitg == 0) {
const int d = r0 + row;
if (hc.has_add) {
block_v += add[d];
block_v = ds4_bf16_round(block_v);
}
const float r0v = residual[d];
const float r1v = residual[d + hc.n_embd];
const float r2v = residual[d + 2*hc.n_embd];
const float r3v = residual[d + 3*hc.n_embd];
for (int dst_hc = 0; dst_hc < 4; ++dst_hc) {
float acc = block_v * post[dst_hc];
acc += comb[dst_hc + 0*4] * r0v;
acc += comb[dst_hc + 1*4] * r1v;
acc += comb[dst_hc + 2*4] * r2v;
acc += comb[dst_hc + 3*4] * r3v;
dst_f32[d + dst_hc*hc.n_embd] = ds4_bf16_round(acc);
}
}
}
}
template<short NR0, typename args_t, bool ROUND = false, bool ROUND_IN = false,
bool ROPE_IN = false, bool ROPE_OUT = false, bool HC_OUT = false>
void kernel_mul_mv_q8_0_f32_rope_impl(
args_t args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem,
uint3 tgpig,
ushort tiisg,
ushort sgitg,
constant ds4_metal_args_mv_rope * rope,
constant ds4_metal_args_mv_hc_expand4 * hc = nullptr,
device const float * hc_add = nullptr,
device const float * hc_residual = nullptr,
device const float * hc_post = nullptr,
device const float * hc_comb = nullptr) {
const short NSG = FC_mul_mv_nsg;
constexpr short NW = N_SIMDWIDTH;
constexpr short NQ = 8;
const int nb = args.ne00/QK8_0;
const int r0 = tgpig.x*NR0;
const int r1 = tgpig.y;
const int im = tgpig.z;
const uint i12 = im%args.ne12;
const uint i13 = im/args.ne12;
const uint64_t offset1 = r1*args.nb11 + (i12)*args.nb12 + (i13)*args.nb13;
device const float * y = (device const float *) (src1 + offset1);
device const block_q8_0 * ax[NR0];
FOR_UNROLL (short row = 0; row < NR0; ++row) {
const uint64_t offset0 = (r0 + row)*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
ax[row] = (device const block_q8_0 *) ((device char *) src0 + offset0);
}
float sumf[NR0] = { 0.f };
const short ix = tiisg/(NW/NQ);
const short il = tiisg%(NW/NQ);
const int ib0 = sgitg*NQ + ix;
float yl[NQ];
device const float * yb = y + ib0*QK8_0 + il*NQ;
for (int ib = ib0; ib < nb; ib += NSG*NQ) {
for (short i = 0; i < NQ; ++i) {
yl[i] = ROUND_IN ? ds4_bf16_round(yb[i]) : yb[i];
}
if (ROPE_IN) {
const uint col = (uint)(ib*QK8_0 + il*NQ) % rope->width;
if (col >= rope->width - 64u) {
const uint pair0 = (col - (rope->width - 64u)) >> 1u;
FOR_UNROLL (short p = 0; p < NQ/2; ++p) {
const float theta = float(rope->start) * rope->frequencies[pair0 + p];
float c;
const float sn = precise::sincos(theta, c);
const float s = rope->inverse ? -sn : sn;
const float re = yl[2*p], im = yl[2*p + 1];
yl[2*p] = ds4_bf16_round(fma(re, c, -(im * s)));
yl[2*p + 1] = ds4_bf16_round(fma(im, c, re * s));
}
}
}
for (short row = 0; row < NR0; row++) {
device const int8_t * qs = ax[row][ib].qs + il*NQ;
float sumq = 0.f;
FOR_UNROLL (short i = 0; i < NQ; ++i) {
sumq += qs[i] * yl[i];
}
sumf[row] += sumq*ax[row][ib].d;
}
yb += NSG*NQ*QK8_0;
}
device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
if (HC_OUT) {
helper_mv_reduce_and_write_hc_expand4<NR0>(dst_f32, sumf, r0, args.ne01, tiisg, sgitg, shmem,
*hc, hc_add, hc_residual, hc_post, hc_comb);
} else if (ROPE_OUT) {
helper_mv_reduce_and_write_rope<NR0>(dst_f32, sumf, r0, args.ne01, tiisg, sgitg, shmem, *rope);
} else {
helper_mv_reduce_and_write<NR0, ROUND>(dst_f32, sumf, r0, args.ne01, tiisg, sgitg, shmem);
}
}
template<short NR0, typename args_t, bool ROUND = false, bool ROUND_IN = false>
void kernel_mul_mv_q8_0_f32_impl(
args_t args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem,
uint3 tgpig,
ushort tiisg,
ushort sgitg) {
kernel_mul_mv_q8_0_f32_rope_impl<NR0, args_t, ROUND, ROUND_IN>(
args, src0, src1, dst, shmem, tgpig, tiisg, sgitg, (constant ds4_metal_args_mv_rope *)nullptr);
}
/* Short V4.1 Q8 projections have only five or nine eight-block K tiles.
* Map a whole output row to one physical SIMD group while retaining the four
* logical accumulators and both reductions of the NSG=4 reference kernel.
* All physical groups then execute the last K tile, without a cross-group
* barrier. */
template<short NB, bool ROUND_IN>
static inline float ds4_q8_short_value(
constant ds4_metal_args_mul_mv &args,
device const char *weights, device const char *input,
uint3 group, ushort lane, ushort simd) {
const uint row = group.x * 4u + simd;
device const block_q8_0 *w = (device const block_q8_0 *)(weights + row * args.nb01);
device const float *x = (device const float *)input;
const short ix = lane / 4, il = lane % 4;
float partial[4] = {0.f, 0.f, 0.f, 0.f};
FOR_UNROLL (short tile = 0; tile < NB / 8; ++tile) {
const short ib = tile * 8 + ix;
float yl[8];
FOR_UNROLL (short i = 0; i < 8; ++i) {
const float v = x[ib * 32 + il * 8 + i];
yl[i] = ROUND_IN ? ds4_bf16_round(v) : v;
}
device const int8_t *q = w[ib].qs + il * 8;
float sumq = 0.f;
FOR_UNROLL (short i = 0; i < 8; ++i) sumq += q[i] * yl[i];
partial[tile % 4] += sumq * w[ib].d;
}
FOR_UNROLL (short i = 0; i < 4; ++i) partial[i] = simd_sum(partial[i]);
const float slot = lane < 4 ? partial[lane] : 0.f;
const float value = simd_sum(slot);
return ds4_bf16_round(value);
}
// One simdgroup group per activation row, each running the single-row
// kernel's walk over the same weight blocks: every row's result matches the
// one-row kernel, and the rows share the weight loads through the cache.
template<short NR0, bool ROUND = false>
static inline void helper_mv_reduce_and_write_rows(
device float * dst_f32,
float sumf[NR0],
const int r0,
const int ne01,
ushort tiisg,
ushort sg,
short r1,
threadgroup char * shmem) {
constexpr short NW = N_SIMDWIDTH;
threadgroup float * shmem_f32[NR0];
for (short row = 0; row < NR0; ++row) {
shmem_f32[row] = (threadgroup float *) shmem + NW*(r1*NR0 + row);
if (sg == 0) {
shmem_f32[row][tiisg] = 0.0f;
}
sumf[row] = simd_sum(sumf[row]);
}
threadgroup_barrier(mem_flags::mem_threadgroup);
for (short row = 0; row < NR0; ++row) {
if (tiisg == 0) {
shmem_f32[row][sg] = sumf[row];
}
}
threadgroup_barrier(mem_flags::mem_threadgroup);
for (short row = 0; row < NR0 && r0 + row < ne01; ++row) {
float tot = simd_sum(shmem_f32[row][tiisg]);
if (tiisg == 0 && sg == 0) {
dst_f32[r0 + row] = ROUND ? ds4_bf16_round(tot) : tot;
}
}
}
// A few rows side by side: one simdgroup group per row in the threadgroup,
// each with the single-row K walk and reduction.
template<short NR0, typename args_t, bool ROUND = false, bool ROUND_IN = false>
void kernel_mul_mv_q8_0_f32_rows_impl(
args_t args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem,
uint3 tgpig,
ushort tiisg,
ushort sgitg) {
const short NSG = FC_mul_mv_nsg;
constexpr short NW = N_SIMDWIDTH;
constexpr short NQ = 8;
const int nb = args.ne00/QK8_0;
const int r0 = tgpig.x*NR0;
const int r1 = sgitg/NSG;
const ushort sg = sgitg%NSG;
const int im = tgpig.z;
const uint i12 = im%args.ne12;
const uint i13 = im/args.ne12;
const uint64_t offset1 = r1*args.nb11 + (i12)*args.nb12 + (i13)*args.nb13;
device const float * y = (device const float *) (src1 + offset1);
device const block_q8_0 * ax[NR0];
FOR_UNROLL (short row = 0; row < NR0; ++row) {
const uint64_t offset0 = (r0 + row)*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
ax[row] = (device const block_q8_0 *) ((device char *) src0 + offset0);
}
float sumf[NR0] = { 0.f };
const short ix = tiisg/(NW/NQ);
const short il = tiisg%(NW/NQ);
const int ib0 = sg*NQ + ix;
float yl[NQ];
device const float * yb = y + ib0*QK8_0 + il*NQ;
for (int ib = ib0; ib < nb; ib += NSG*NQ) {
for (short i = 0; i < NQ; ++i) {
yl[i] = ROUND_IN ? ds4_bf16_round(yb[i]) : yb[i];
}
for (short row = 0; row < NR0; row++) {
device const int8_t * qs = ax[row][ib].qs + il*NQ;
float sumq = 0.f;
FOR_UNROLL (short i = 0; i < NQ; ++i) {
sumq += qs[i] * yl[i];
}
sumf[row] += sumq*ax[row][ib].d;
}
yb += NSG*NQ*QK8_0;
}
device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
helper_mv_reduce_and_write_rows<NR0, ROUND>(dst_f32, sumf, r0, args.ne01, tiisg, sg, (short)r1, shmem);
}
// The rows one after another on the single-row simdgroup layout: the
// threadgroup keeps the weight lines hot across rows and runs at the
// single-row occupancy; every row's result matches the one-row kernel.
template<short NR0, typename args_t, bool ROUND = false, bool ROUND_IN = false, bool HC_OUT = false>
void kernel_mul_mv_q8_0_f32_rows_seq_impl(
args_t args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem,
uint3 tgpig,
ushort tiisg,
ushort sgitg,
constant ds4_metal_args_mv_hc_expand4 * hc = nullptr,
device const float * hc_add = nullptr,
device const float * hc_residual = nullptr,
device const float * hc_post = nullptr,
device const float * hc_comb = nullptr) {
const short NSG = FC_mul_mv_nsg;
constexpr short NW = N_SIMDWIDTH;
constexpr short NQ = 8;
const int nb = args.ne00/QK8_0;
const int r0 = tgpig.x*NR0;
const int im = tgpig.z;
const uint i12 = im%args.ne12;
const uint i13 = im/args.ne12;
device const block_q8_0 * ax[NR0];
FOR_UNROLL (short row = 0; row < NR0; ++row) {
const uint64_t offset0 = (r0 + row)*args.nb01 + (i12/args.r2)*args.nb02 + (i13/args.r3)*args.nb03;
ax[row] = (device const block_q8_0 *) ((device char *) src0 + offset0);
}
const short ix = tiisg/(NW/NQ);
const short il = tiisg%(NW/NQ);
const int ib0 = sgitg*NQ + ix;
for (int r1 = 0; r1 < args.ne11; ++r1) {
const uint64_t offset1 = r1*args.nb11 + (i12)*args.nb12 + (i13)*args.nb13;
device const float * y = (device const float *) (src1 + offset1);
float sumf[NR0] = { 0.f };
float yl[NQ];
device const float * yb = y + ib0*QK8_0 + il*NQ;
for (int ib = ib0; ib < nb; ib += NSG*NQ) {
for (short i = 0; i < NQ; ++i) {
yl[i] = ROUND_IN ? ds4_bf16_round(yb[i]) : yb[i];
}
for (short row = 0; row < NR0; row++) {
device const int8_t * qs = ax[row][ib].qs + il*NQ;
float sumq = 0.f;
FOR_UNROLL (short i = 0; i < NQ; ++i) {
sumq += qs[i] * yl[i];
}
sumf[row] += sumq*ax[row][ib].d;
}
yb += NSG*NQ*QK8_0;
}
if (HC_OUT) {
const uint64_t hc_dim = 4ull * hc->n_embd, stride = hc->split_stride;
helper_mv_reduce_and_write_hc_expand4<NR0>((device float *) dst + r1*hc_dim, sumf, r0, args.ne01,
tiisg, sgitg, shmem, *hc, hc_add + r1*hc->n_embd, hc_residual + r1*hc_dim,
hc_post + r1*stride, hc_comb + r1*stride);
} else {
device float * dst_f32 = (device float *) dst + (uint64_t)im*args.ne0*args.ne1 + (uint64_t)r1*args.ne0;
helper_mv_reduce_and_write<NR0, ROUND>(dst_f32, sumf, r0, args.ne01, tiisg, sgitg, shmem);
}
threadgroup_barrier(mem_flags::mem_threadgroup); /* the reduce scratch is reused */
}
}
// NR0 output rows by NT activation rows per threadgroup. Each lane runs the
// one-row kernel's walk for every activation row, with the weight block
// dequantized once into registers, and each row then takes the one-row
// reduction: every result matches kernel_mul_mv_q8_0_f32_impl<NR0>.
template<short NR0, short NT>
void kernel_mul_mv_q8_0_f32_rows_tok_impl(
constant ds4_metal_args_mul_mv & args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem,
uint3 tgpig,
ushort tiisg,
ushort sgitg) {
const short NSG = FC_mul_mv_nsg;
constexpr short NW = N_SIMDWIDTH;
constexpr short NQ = 8;
const int nb = args.ne00/QK8_0;
const int r0 = tgpig.x*NR0;
const int t0 = tgpig.y*NT;
const int nt = min((int)NT, args.ne11 - t0);
device const block_q8_0 * ax[NR0];
FOR_UNROLL (short row = 0; row < NR0; ++row) {
ax[row] = (device const block_q8_0 *) ((device char *) src0 + (uint64_t)(r0 + row)*args.nb01);
}
const short ix = tiisg/(NW/NQ);
const short il = tiisg%(NW/NQ);
const int ib0 = sgitg*NQ + ix;
float sumf[NT][NR0];
FOR_UNROLL (short t = 0; t < NT; ++t) {
FOR_UNROLL (short row = 0; row < NR0; ++row) sumf[t][row] = 0.f;
}
for (int ib = ib0; ib < nb; ib += NSG*NQ) {
float yl[NT][NQ];
FOR_UNROLL (short t = 0; t < NT; ++t) {
device const float * yb = (device const float *) (src1 + (uint64_t)min(t0 + t, args.ne11 - 1)*args.nb11) +
ib*QK8_0 + il*NQ;
FOR_UNROLL (short i = 0; i < NQ; ++i) yl[t][i] = yb[i];
}
FOR_UNROLL (short row = 0; row < NR0; ++row) {
device const int8_t * qs = ax[row][ib].qs + il*NQ;
const float d = ax[row][ib].d;
float q[NQ];
FOR_UNROLL (short i = 0; i < NQ; ++i) q[i] = qs[i];
FOR_UNROLL (short t = 0; t < NT; ++t) {
float sumq = 0.f;
FOR_UNROLL (short i = 0; i < NQ; ++i) {
sumq += q[i] * yl[t][i];
}
sumf[t][row] += sumq*d;
}
}
}
for (short t = 0; t < nt; ++t) {
device float * dst_f32 = (device float *) dst + (uint64_t)(t0 + t)*args.ne0;
helper_mv_reduce_and_write<NR0, false>(dst_f32, sumf[t], r0, args.ne01, tiisg, sgitg, shmem);
threadgroup_barrier(mem_flags::mem_threadgroup); /* the reduce scratch is reused */
}
}
#define DS4_MUL_MV_Q8_0_ROWS_TOK(NAME, NR0, NT) \
[[host_name(#NAME)]] \
kernel void NAME( \
constant ds4_metal_args_mul_mv & args, \
device const char * src0, \
device const char * src1, \
device char * dst, \
threadgroup char * shmem [[threadgroup(0)]], \
uint3 tgpig[[threadgroup_position_in_grid]], \
ushort tiisg[[thread_index_in_simdgroup]], \
ushort sgitg[[simdgroup_index_in_threadgroup]]) { \
kernel_mul_mv_q8_0_f32_rows_tok_impl<NR0, NT>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); \
}
DS4_MUL_MV_Q8_0_ROWS_TOK(kernel_mul_mv_q8_0_f32_rows8_tok2, 8, 2)
#undef DS4_MUL_MV_Q8_0_ROWS_TOK
#define DS4_MUL_MV_Q8_0_ROWS_SEQ(NAME, NR0, ...) \
[[host_name(#NAME)]] \
kernel void NAME( \
constant ds4_metal_args_mul_mv & args, \
device const char * src0, \
device const char * src1, \
device char * dst, \
threadgroup char * shmem [[threadgroup(0)]], \
uint3 tgpig[[threadgroup_position_in_grid]], \
ushort tiisg[[thread_index_in_simdgroup]], \
ushort sgitg[[simdgroup_index_in_threadgroup]]) { \
kernel_mul_mv_q8_0_f32_rows_seq_impl<NR0, constant ds4_metal_args_mul_mv &, ##__VA_ARGS__>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg); \
}
DS4_MUL_MV_Q8_0_ROWS_SEQ(kernel_mul_mv_q8_0_f32_rows_seq4, 4)
DS4_MUL_MV_Q8_0_ROWS_SEQ(kernel_mul_mv_q8_0_f32_bf16_rows_seq4, 4, true)
DS4_MUL_MV_Q8_0_ROWS_SEQ(kernel_mul_mv_q8_0_f32_bf16io_rows_seq4, 4, true, true)
#undef DS4_MUL_MV_Q8_0_ROWS_SEQ
#define DS4_MUL_MV_Q8_0_HC_ROWS_SEQ(NAME, NR0) \
[[host_name(#NAME)]] \
kernel void NAME( \
constant ds4_metal_args_mul_mv & args, \
device const char * src0, \
device const char * src1, \
device char * dst, \
constant ds4_metal_args_mv_hc_expand4 & hc, \
device const float * add, \
device const float * residual, \
device const float * post, \
device const float * comb, \
threadgroup char * shmem [[threadgroup(0)]], \
uint3 tgpig[[threadgroup_position_in_grid]], \
ushort tiisg[[thread_index_in_simdgroup]], \
ushort sgitg[[simdgroup_index_in_threadgroup]]) { \
kernel_mul_mv_q8_0_f32_rows_seq_impl<NR0, constant ds4_metal_args_mul_mv &, true, true, true>( \
args, src0, src1, dst, shmem, tgpig, tiisg, sgitg, &hc, add, residual, post, comb); \
}
DS4_MUL_MV_Q8_0_HC_ROWS_SEQ(kernel_mul_mv_q8_0_f32_bf16io_hc_expand4_rows_seq4, 4)
#undef DS4_MUL_MV_Q8_0_HC_ROWS_SEQ
// Decode-time Q8_0 matrix-vector multiply. DS4 uses this for Q8_0 dense
// projections such as shared experts and output-side small matvecs.
[[host_name("kernel_mul_mv_q8_0_f32")]]
kernel void kernel_mul_mv_q8_0_f32(
constant ds4_metal_args_mul_mv & args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem [[threadgroup(0)]],
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiisg[[thread_index_in_simdgroup]],
ushort sgitg[[simdgroup_index_in_threadgroup]]) {
kernel_mul_mv_q8_0_f32_plain_impl<N_R0_Q8_0, constant ds4_metal_args_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
}
// Four output rows reuse each activation load. The K walk, eight-term
// quantized dot and reduction tree are exactly the same as the two-row
// specialization above. Callers must provide complete four-row weight tiles
// (or explicitly padded weights): the shared implementation reads all NR0
// rows before its output bounds check. This needs 4*32*sizeof(float) scratch.
[[host_name("kernel_mul_mv_q8_0_f32_nr4")]]
kernel void kernel_mul_mv_q8_0_f32_nr4(
constant ds4_metal_args_mul_mv & args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem [[threadgroup(0)]],
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiisg[[thread_index_in_simdgroup]],
ushort sgitg[[simdgroup_index_in_threadgroup]]) {
kernel_mul_mv_q8_0_f32_plain_impl<4, constant ds4_metal_args_mul_mv &>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
}
[[host_name("kernel_mul_mv_q8_0_f32_bf16")]]
kernel void kernel_mul_mv_q8_0_f32_bf16(
constant ds4_metal_args_mul_mv & args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem [[threadgroup(0)]],
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiisg[[thread_index_in_simdgroup]],
ushort sgitg[[simdgroup_index_in_threadgroup]]) {
kernel_mul_mv_q8_0_f32_impl<N_R0_Q8_0, constant ds4_metal_args_mul_mv &, true>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
}
[[host_name("kernel_mul_mv_q8_0_f32_rows8")]]
kernel void kernel_mul_mv_q8_0_f32_rows8(
constant ds4_metal_args_mul_mv & args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem [[threadgroup(0)]],
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiisg[[thread_index_in_simdgroup]],
ushort sgitg[[simdgroup_index_in_threadgroup]]) {
kernel_mul_mv_q8_0_f32_impl<8, constant ds4_metal_args_mul_mv &, false>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
}
[[host_name("kernel_mul_mv_q8_0_f32_bf16io")]]
kernel void kernel_mul_mv_q8_0_f32_bf16io(
constant ds4_metal_args_mul_mv & args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem [[threadgroup(0)]],
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiisg[[thread_index_in_simdgroup]],
ushort sgitg[[simdgroup_index_in_threadgroup]]) {
kernel_mul_mv_q8_0_f32_impl<N_R0_Q8_0, constant ds4_metal_args_mul_mv &, true, true>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
}
// BF16-rounded input, F32 output: a TP rank's K slice of a bf16 projection,
// whose partial is rounded only after the peer's half joins it.
[[host_name("kernel_mul_mv_q8_0_f32_bf16in")]]
kernel void kernel_mul_mv_q8_0_f32_bf16in(
constant ds4_metal_args_mul_mv & args,
device const char * src0,
device const char * src1,
device char * dst,
threadgroup char * shmem [[threadgroup(0)]],
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiisg[[thread_index_in_simdgroup]],
ushort sgitg[[simdgroup_index_in_threadgroup]]) {
kernel_mul_mv_q8_0_f32_impl<N_R0_Q8_0, constant ds4_metal_args_mul_mv &, false, true>(args, src0, src1, dst, shmem, tgpig, tiisg, sgitg);
}
// The q projection with its rope applied to the bf16 outputs.
[[host_name("kernel_mul_mv_q8_0_f32_bf16_rope")]]
kernel void kernel_mul_mv_q8_0_f32_bf16_rope(
constant ds4_metal_args_mul_mv & args,
device const char * src0,
device const char * src1,
device char * dst,
constant ds4_metal_args_mv_rope & rope,
threadgroup char * shmem [[threadgroup(0)]],
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiisg[[thread_index_in_simdgroup]],
ushort sgitg[[simdgroup_index_in_threadgroup]]) {
kernel_mul_mv_q8_0_f32_rope_impl<N_R0_Q8_0, constant ds4_metal_args_mul_mv &, true, false, false, true>(
args, src0, src1, dst, shmem, tgpig, tiisg, sgitg, &rope);
}
// A bf16 in/out matvec whose outputs expand straight into the HC streams.
[[host_name("kernel_mul_mv_q8_0_f32_bf16io_hc_expand4")]]
kernel void kernel_mul_mv_q8_0_f32_bf16io_hc_expand4(
constant ds4_metal_args_mul_mv & args,
device const char * src0,
device const char * src1,
device char * dst,
constant ds4_metal_args_mv_hc_expand4 & hc,
device const float * add,
device const float * residual,
device const float * post,
device const float * comb,
threadgroup char * shmem [[threadgroup(0)]],
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiisg[[thread_index_in_simdgroup]],
ushort sgitg[[simdgroup_index_in_threadgroup]]) {
kernel_mul_mv_q8_0_f32_rope_impl<N_R0_Q8_0, constant ds4_metal_args_mul_mv &, true, true, false, false, true>(
args, src0, src1, dst, shmem, tgpig, tiisg, sgitg, nullptr, &hc, add, residual, post, comb);
}
/* A one-row Q8_0 matvec's arguments (nr0 2); called with literals, they fold into the address arithmetic. */
static inline ds4_metal_args_mul_mv ds4_q8_0_static_args(int in, int out) {
const ulong row = (ulong)(in / 32) * 34;
return ds4_metal_args_mul_mv{in, out, 1, 34, row, row * out, row * out, in, 1, 1, 4,
(ulong)in * 4, (ulong)in * 4, (ulong)in * 4, out, 1, 2, 1, 1};
}
/* Attention output-B into the HC streams at its V4.1 shape (8192 -> 5120) as literals. */
constant ds4_metal_args_mv_hc_expand4 ds4_hc_static_8192_5120 = { /*.n_embd =*/ 5120, /*.has_add =*/ 0, /*.split_stride =*/ 24 };
[[host_name("kernel_mul_mv_q8_0_f32_bf16io_hc_expand4_static")]]
kernel void kernel_mul_mv_q8_0_f32_bf16io_hc_expand4_static(
constant ds4_metal_args_mul_mv & args,
device const char * src0,
device const char * src1,
device char * dst,
constant ds4_metal_args_mv_hc_expand4 & hc,
device const float * add,
device const float * residual,
device const float * post,
device const float * comb,
threadgroup char * shmem [[threadgroup(0)]],
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiisg[[thread_index_in_simdgroup]],
ushort sgitg[[simdgroup_index_in_threadgroup]]) {
(void)args; (void)hc;
ds4_metal_args_mul_mv sa = ds4_q8_0_static_args(8192, 5120);
kernel_mul_mv_q8_0_f32_rope_impl<N_R0_Q8_0, thread ds4_metal_args_mul_mv &, true, true, false, false, true>(
sa, src0, src1, dst, shmem, tgpig, tiisg, sgitg, nullptr, &ds4_hc_static_8192_5120, add, residual, post, comb);
}
// Two Q8_0 matrices over one activation row in one dispatch: the leading
// threadgroups take the first matrix's row groups, the rest the second's.
[[host_name("kernel_mul_mv_q8_0_f32_bf16_pair")]]