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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
//! Raw-Vulkan (`ash` 0.38) GPU backend dispatching native SPIR-V kernels.
//!
//! v1 scope: f32, contiguous tensors only. Generalizes the proven `vulkan-probe`
//! `Ctx` (GPU select, host-visible buffers, descriptor sets, SPIR-V pipeline,
//! dispatch, readback) into `VulkanDevice` + `VulkanStorage`. Anything outside the
//! covered op set bails with `Error::Msg`.
use crate::backend::{BackendDevice, BackendStorage};
use crate::op::{BinaryOpT, CmpOp, ReduceOp, UnaryOpT};
use crate::{CpuStorage, DType, Error, Layout, Result, Shape};
use ash::vk;
use rand::Rng;
use std::collections::HashMap;
use std::ffi::CStr;
use std::sync::{Arc, Mutex};
// --- SPIR-V kernels (compiled by hanzo-ml build.rs from src/vulkan/shaders/*.comp) ---
macro_rules! spv {
($name:literal) => {
include_bytes!(concat!(env!("OUT_DIR"), "/", $name, ".spv"))
};
}
// kernel name -> SPIR-V bytes. Matches the contract ABI (inputs-then-output buffers, one push block).
fn kernel_spv(name: &str) -> Result<&'static [u8]> {
// Dev shader override: with VK_SHADER_DIR=<dir> set, load <dir>/<name>.spv instead of the embedded
// bytes when that file exists. Lets a kernel be re-benched by recompiling one .spv (glslc, ~ms) and
// re-running the engine -- no hanzo-ml recompile. Bytes are leaked (shaders load once at device init).
if let Some(dir) = std::env::var_os("VK_SHADER_DIR") {
let path = std::path::Path::new(&dir).join(format!("{name}.spv"));
if let Ok(bytes) = std::fs::read(&path) {
return Ok(Box::leak(bytes.into_boxed_slice()));
}
}
let b: &'static [u8] = match name {
"add" => spv!("add"),
"sub" => spv!("sub"),
"mul" => spv!("mul"),
"div" => spv!("div"),
"affine" => spv!("affine"),
"neg" => spv!("neg"),
"exp" => spv!("exp"),
"silu" => spv!("silu"),
"silu_mul" => spv!("silu_mul"),
"sigmoid" => spv!("sigmoid"),
"gelu" => spv!("gelu"),
"relu" => spv!("relu"),
"sqr" => spv!("sqr"),
"sqrt" => spv!("sqrt"),
"recip" => spv!("recip"),
"tanh" => spv!("tanh"),
"matmul" => spv!("matmul"),
"bmm" => spv!("bmm"),
"bmm_reg" => spv!("bmm_reg"),
"bmm_reg_nt" => spv!("bmm_reg_nt"),
"bmm_coopmat" => spv!("bmm_coopmat"),
"bmm_coopmat_rb" => spv!("bmm_coopmat_rb"),
"bmm_coopmat_rb_nt" => spv!("bmm_coopmat_rb_nt"),
"cast_f2h" => spv!("cast_f2h"),
"cast_h2f" => spv!("cast_h2f"),
"mul_mat_vec_q8" => spv!("mul_mat_vec_q8"),
"mul_mat_vec_q8_sg" => spv!("mul_mat_vec_q8_sg"),
"mul_mat_vec_q8_0" => spv!("mul_mat_vec_q8_0"),
"mul_mat_vec_q4_0" => spv!("mul_mat_vec_q4_0"),
"mul_mat_q4_0" => spv!("mul_mat_q4_0"),
"mul_mat_q8" => spv!("mul_mat_q8"),
"mul_mat_q4k" => spv!("mul_mat_q4k"),
"mul_mm_q4k_tiled" => spv!("mul_mm_q4k_tiled"),
"mul_mm_q4k_tiled_dp4a" => spv!("mul_mm_q4k_tiled_dp4a"),
// Larger-BM tiles: same kernel, BM=128/256 so a 512-row prefill re-reads each cold weight
// column 4x/2x instead of 8x (weight bytes read = full-weight * m/BM). Cold-weight-bound win.
"mul_mm_q4k_tiled_dp4a_bm128" => spv!("mul_mm_q4k_tiled_dp4a_bm128"),
"mul_mm_q4k_tiled_dp4a_bm256" => spv!("mul_mm_q4k_tiled_dp4a_bm256"),
"mul_mat_q4k_dp4a" => spv!("mul_mat_q4k_dp4a"),
"mul_mm_q4k_coopmat" => spv!("mul_mm_q4k_coopmat"),
"quantize_act_q8" => spv!("quantize_act_q8"),
"mul_mat_q5k" => spv!("mul_mat_q5k"),
"mul_mat_q6k" => spv!("mul_mat_q6k"),
"mul_mm_q6k_tiled_dp4a" => spv!("mul_mm_q6k_tiled_dp4a"),
"mul_mm_q6k_coopmat" => spv!("mul_mm_q6k_coopmat"),
"mul_mat_vec_q4k" => spv!("mul_mat_vec_q4k"),
"mul_mat_vec_q4k_sg" => spv!("mul_mat_vec_q4k_sg"),
"mul_mat_vec_q4k_cm" => spv!("mul_mat_vec_q4k_cm"),
"mul_mat_vec_q5k" => spv!("mul_mat_vec_q5k"),
"mul_mat_vec_q5k_sg" => spv!("mul_mat_vec_q5k_sg"),
"mul_mat_vec_q6k" => spv!("mul_mat_vec_q6k"),
"mul_mat_vec_q6k_sg" => spv!("mul_mat_vec_q6k_sg"),
"mul_mat_vec_q6k_cm" => spv!("mul_mat_vec_q6k_cm"),
"mul_mat_vec_q2k" => spv!("mul_mat_vec_q2k"),
"mul_mat_vec_q3k" => spv!("mul_mat_vec_q3k"),
"mul_mat_vec_iq4xs" => spv!("mul_mat_vec_iq4xs"),
"mul_mat_vec_iq2xxs" => spv!("mul_mat_vec_iq2xxs"),
"mul_mat_vec_iq2xs" => spv!("mul_mat_vec_iq2xs"),
"mul_mat_vec_iq1m" => spv!("mul_mat_vec_iq1m"),
"mul_mat_vec_iq1s" => spv!("mul_mat_vec_iq1s"),
"mul_mat_vec_iq3s" => spv!("mul_mat_vec_iq3s"),
"mul_mat_vec_iq3xxs" => spv!("mul_mat_vec_iq3xxs"),
"mul_mat_vec_iq2s" => spv!("mul_mat_vec_iq2s"),
"mul_mat_vec_tq2_0" => spv!("mul_mat_vec_tq2_0"),
"mul_mat_vec_iq4nl" => spv!("mul_mat_vec_iq4nl"),
"moe_matvec_q4k" => spv!("moe_matvec_q4k"),
"moe_matvec_q6k" => spv!("moe_matvec_q6k"),
// DSL block-reduced MoE (hanzo-kernel quant::moe_matvec_q{4,6}k_blk, lowered per live shape):
// planar bank, one workgroup per output, shared-mem tree reduce. `_gu` = gate/up (k=2048,
// nt=64), `_dn` = down (k=768, nt=32). ~2-3x the packed `moe_matvec_q4k` naive path on evo.
// DSL fused MoE top-k router (hanzo-kernel quant::moe_route, E=128 top-8): one workgroup per
// token does softmax + top-k in shared memory, replacing the ~11-dispatch softmax+sort+gather
// op-chain. Bindings: logits(0), ids_out(1), w_out(2).
"moe_route" => include_bytes!("vulkan/spv/moe_route.spv"),
// DSL block flash SDPA (hanzo-kernel attn::sdpa_blk, d=128 nt=64): one workgroup per
// (head,query) splits the keys, each thread runs an online softmax over its key slice, then the
// workgroup flash-combines the partials. GQA-native (reads the shared KV head, no repeat_kv), so
// it collapses the copy2d+bmm+softmax+bmm decode-attention chain to ONE dispatch. Bindings:
// q(0), k(1), v(2), out(3), scale(4), meta(5)=[seq_q,seq_k,n_kv_groups,causal].
"sdpa_blk" => include_bytes!("vulkan/spv/sdpa_blk.spv"),
// DSL flash attention on the cooperative-matrix path (hanzo-kernel flash::flash_attn, d=128
// plane=64 BR=BC=16 baked): tiled online softmax where BOTH matmuls (Q@Kᵀ, P@V) run as f16
// 16x16x16 coopmat (OpCooperativeMatrixMulAddKHR, f16 A/B -> f32 acc, subgroup scope). One
// workgroup (cube) computes one (batch,head,query-tile) of 16 rows; the whole grid launches at
// once (meta[8]=cube_base=0). plane=64 matches the RADV gfx1151 coopmat subgroupSize (a 32-lane
// launch left the fragment half-fed -> garbage past query-row 1). GQA-native, causal-optional,
// runtime seq_q/seq_k via meta. Bindings: q(0), k(1), v(2), out(3), scale(4), meta(5)=[seq_q,
// seq_k,n_heads,n_kv,causal,kv_batch_stride,kv_head_stride,key_stride,cube_base].
"flash_attn_dsl" => include_bytes!("vulkan/spv/flash_attn_dsl_d128.spv"),
// Flash-decoding (seq_q==1): split the KV sequence across n_split workgroups per (batch,head) so
// the lone decode query fills the GPU, then combine the partials. Register-light wave64-subgroup
// (lane owns head_dim {t,t+64}, subgroupAdd dot) -- high occupancy vs sdpa_blk's 256-VGPR path.
// Phase 1 bindings: q,k,v,pacc,pm,pl; phase 2: pacc,pm,pl,out. Shapes ride the push constant.
"sdpa_decode_split" => spv!("sdpa_decode_split"),
"sdpa_decode_reduce" => spv!("sdpa_decode_reduce"),
// DSL f32 GEMV (hanzo-kernel quant::gemv, nt=128): out[n]=W[n,k]@x[k], one workgroup/output row,
// threads tree-reduce over k. Replaces the tiled GEMM for m==1 (the MoE router gate), where the
// 64x64-tile GEMM runs ~2 occupancy-starved workgroups. Bindings: w(0), x(1), out(2), meta(3)=[k].
"gemv" => include_bytes!("vulkan/spv/gemv.spv"),
"moe_matvec_q4k_blk_gu" => include_bytes!("vulkan/spv/moe_matvec_q4k_blk_gu.spv"),
"moe_matvec_q4k_blk_dn" => include_bytes!("vulkan/spv/moe_matvec_q4k_blk_dn.spv"),
// dp4a (int8 OpSDot) MoE matvec: activation q8-quantized, Q4_K nibbles int8-dotted. Same split
// bank as the f32 block kernels; ~1.4-1.6x their throughput. Bindings: wqs,wsc,wd,wdm,xq,xs,
// xsum,ids,out. Gated on the device's integer-dot capability (int_dot8).
"moe_matvec_q4k_dp4a_blk_gu" => include_bytes!("vulkan/spv/moe_matvec_q4k_dp4a_blk_gu.spv"),
"moe_matvec_q4k_dp4a_blk_dn" => include_bytes!("vulkan/spv/moe_matvec_q4k_dp4a_blk_dn.spv"),
// Affine Q4_K PREFILL GEMM on the coopmat/tensor-core path (the mmq_q4k_wmma_blk DSL kernel,
// n=2048 k=2048, LocalSize 512 = 8*plane64). Decodes packed Q4_K in-kernel + affine epilogue
// xs*(D*dot - M*xsum). Bindings: xq,xs,xsum,wqs,wsc,wd,wdm,out. The prefill twin of the dp4a
// decode matvec: 3.4 TFLOP/s vs decode's ~92 GB/s, the path toward llama-Vulkan pp512.
"mmq_q4k" => include_bytes!("vulkan/spv/mmq_q4k.spv"),
// Runtime-dims twin: m/n/k ride a meta SSBO (binding 8) so ONE .spv serves every prefill shape.
"mmq_q4k_rt" => include_bytes!("vulkan/spv/mmq_q4k_rt.spv"),
// DENSE dp4a Q4_K matvec (block-reduce, one workgroup/output row, nt=64): reads the verbatim
// packed weight; the m=1 decode fix for the attention projections that the prefill dp4a GEMM
// (mul_mat_q4k_dp4a, 1-thread/row) starves. Bindings: wq(packed), xq, xs, xsum, out, meta=[k].
"matvec_q4k_dp4a_blk" => include_bytes!("vulkan/spv/matvec_q4k_dp4a_blk.spv"),
"moe_matvec_q6k_blk_dn" => include_bytes!("vulkan/spv/moe_matvec_q6k_blk_dn.spv"),
"moe_matvec_q6k_dp4a_blk_dn" => include_bytes!("vulkan/spv/moe_matvec_q6k_dp4a_blk_dn.spv"),
"moe_matvec_q8_0" => spv!("moe_matvec_q8_0"),
"moe_matvec_q4_0" => spv!("moe_matvec_q4_0"),
"flash_attn" => spv!("flash_attn"),
"copy" => spv!("copy"),
"copy2d" => spv!("copy2d"),
"copy2d_off" => spv!("copy2d_off"),
"const_fill" => spv!("const_fill"),
"reduce_sum" => spv!("reduce_sum"),
"reduce_max" => spv!("reduce_max"),
"strided_copy" => spv!("strided_copy"),
"index_select" => spv!("index_select"),
"where_cond" => spv!("where_cond"),
"softmax_rows" => include_bytes!("vulkan/spv/softmax_rows_blk.spv"),
"rms_norm" => include_bytes!("vulkan/spv/rms_norm_blk.spv"),
"rope" => spv!("rope"),
"rope_norm" => spv!("rope_norm"),
"add_rmsnorm" => include_bytes!("vulkan/spv/add_rmsnorm_blk.spv"),
"sin" => spv!("sin"),
"cos" => spv!("cos"),
"log" => spv!("log"),
"abs" => spv!("abs"),
"floor" => spv!("floor"),
"ceil" => spv!("ceil"),
"round" => spv!("round"),
"sign" => spv!("sign"),
"erf" => spv!("erf"),
"gelu_erf" => spv!("gelu_erf"),
"powf" => spv!("powf"),
"elu" => spv!("elu"),
"maximum" => spv!("maximum"),
"minimum" => spv!("minimum"),
"cmp" => spv!("cmp"),
"cast_f2u" => spv!("cast_f2u"),
"cast_u2f" => spv!("cast_u2f"),
"reduce_min" => spv!("reduce_min"),
"reduce_argmin" => spv!("reduce_argmin"),
"reduce_argmax" => spv!("reduce_argmax"),
"argsort" => spv!("argsort"),
"gather" => spv!("gather"),
"scatter_set" => spv!("scatter_set"),
"scatter_add_set" => spv!("scatter_add_set"),
"conv1d" => spv!("conv1d"),
"gdn_step" => spv!("gdn_step"),
"gdn_conv1d_step" => spv!("gdn_conv1d_step"),
"conv2d" => spv!("conv2d"),
"conv_transpose1d" => spv!("conv_transpose1d"),
"conv_transpose2d" => spv!("conv_transpose2d"),
"avg_pool2d" => spv!("avg_pool2d"),
"max_pool2d" => spv!("max_pool2d"),
"upsample_nearest1d" => spv!("upsample_nearest1d"),
"upsample_nearest2d" => spv!("upsample_nearest2d"),
"upsample_bilinear2d" => spv!("upsample_bilinear2d"),
"gdn_recurrence" => spv!("gdn_recurrence"),
"gdn_chunked" => spv!("gdn_chunked"),
"gdn_conv_update" => spv!("gdn_conv_update"),
"gdn_conv_full" => spv!("gdn_conv_full"),
"gdn_conv_state_save" => spv!("gdn_conv_state_save"),
"gdn_gating" => spv!("gdn_gating"),
"paged_attn" => spv!("paged_attn"),
"reshape_and_cache" => spv!("reshape_and_cache"),
"paged_attn_q8" => spv!("paged_attn_q8"),
"reshape_and_cache_q8" => spv!("reshape_and_cache_q8"),
"dsl_mul" => include_bytes!("vulkan/spv/dsl_mul.spv"),
"dsl_matvec" => include_bytes!("vulkan/spv/dsl_matvec.spv"),
_ => crate::bail!("vulkan: no SPIR-V kernel for `{name}`"),
};
Ok(b)
}
#[derive(thiserror::Error, Debug)]
pub enum VulkanError {
#[error("{0}")]
Message(String),
}
impl From<String> for VulkanError {
fn from(e: String) -> Self {
VulkanError::Message(e)
}
}
impl From<VulkanError> for Error {
fn from(e: VulkanError) -> Self {
Error::Msg(e.to_string())
}
}
// A compute pipeline + its descriptor-set layout, cached by kernel name. `n_buffers`
// is the binding count this layout was built for so callers can sanity-check.
#[derive(Clone)]
struct CachedPipeline {
pipeline: vk::Pipeline,
layout: vk::PipelineLayout,
set_layout: vk::DescriptorSetLayout,
n_buffers: usize,
}
// Device-memory placement strategy for storage buffers, set once at init from
// VK_DEVICE_MEMORY_STRATEGY. On this RDNA3.5 UMA APU the host-visible "VRAM carveout" heap
// is small (a few hundred MB), while the large unified pool (~GTT, tens of GB of the 128GB system
// RAM) is exposed as a DEVICE_LOCAL-only heap. A pure host-visible policy therefore OOMs an 18.6GB
// model even though there is ample memory; we must be able to place big buffers in the large heap.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum MemStrategy {
// Only ever use host-visible memory types (legacy behaviour; correct on classic discrete GPUs
// with a single large host-visible heap or where staging is handled elsewhere).
HostOnly,
// Prefer the largest DEVICE_LOCAL heap for every buffer; fall back to host-visible.
DeviceFirst,
// Per-allocation: use a host-visible type when its heap is large enough to hold the buffer,
// otherwise place it in the largest usable (typically DEVICE_LOCAL) heap. Default.
Auto,
}
struct VkInner {
_entry: ash::Entry,
#[allow(dead_code)] // held to keep the Vulkan instance alive for the device's lifetime
instance: ash::Instance,
// Physical device handle, retained so memory budget (free bytes) can be re-queried at runtime
// for the scratch-allocation guard (heap `size` is total capacity, not what is currently free).
pdev: vk::PhysicalDevice,
device: ash::Device,
queue: vk::Queue,
#[allow(dead_code)] // queue family index, retained for completeness
qfi: u32,
gpu_id: usize,
mem_props: vk::PhysicalDeviceMemoryProperties,
// Buffer-placement policy (see MemStrategy).
mem_strategy: MemStrategy,
// VK_EXT_memory_budget advertised: lets us query per-heap *free* bytes (heapBudget) at runtime
// instead of only the static total heap `size`. When absent we fall back to total heap size as
// a conservative upper bound for the scratch guard.
has_mem_budget: bool,
// Subgroup arithmetic support for the COMPUTE stage (queried from PhysicalDeviceSubgroupProperties
// at init). When true the q8 mat-vec uses the subgroup-reduced kernel (one subgroup per output
// row, fused subgroupAdd) instead of the scalar one-thread-per-row kernel; this raises memory-
// level parallelism per row and halves the dispatch count on the decode hot path. Gated because
// the subgroup SPIR-V needs GroupNonUniformArithmetic, which not every driver (e.g. some WSL/
// Dozen configs) provides. `subgroup_size` is the reported subgroup width.
subgroup_matvec: bool,
subgroup_size: u32,
// SPV_KHR_integer_dot_product (OpSDotAccSat 4x8) availability -- gates the int8 dp4a prefill GEMM
// (mul_mm_q4k_tiled_dp4a, 9.35x over the column kernel). Present on RDNA3.5/native AMD+NV; absent
// on old/WSL drivers (those fall back to the universal f32 2D tile). VK_INT_DOT=0 forces off.
int_dot8: bool,
// Cooperative-matrix (matrix-core / WMMA) availability and the chosen MxNxK tile for an
// fp16xfp16 -> fp32 subgroup config. Present on native AMD/NV drivers (RDNA3.5 8060S),
// absent on WSL/Dozen.
coopmat: bool,
cm_mnk: (u32, u32, u32),
// Whether matmul uses the register-blocked coopmat kernel (bmm_coopmat_rb). Default ON when the
// device advertises coopmat (measured 1.3-2.7x over fp32 bmm_reg on the real AMD driver, full
// forward argmax matches CPU); VK_COOPMAT=0 forces the fp32 path.
cm_use: bool,
// CPU-side RNG seed (kernels are deterministic; randoms are generated on the CPU then uploaded).
seed: Mutex<u64>,
// Per-flush phase profiling, gated on VK_PROFILE=1 (read once at init). When set,
// `flush_locked` prints, per submitted batch, the time spent recording dispatches, in
// queue_submit, in the fence wait, plus the dispatch and emitted-barrier counts; readbacks
// print their map+copy time. Lets the 8060S show where the per-token milliseconds actually go.
// Strictly zero-overhead when unset: the recording timer and all prints are behind this bool.
profile: bool,
// Per-op GPU-time profiling opt-in (VK_PROFILE_GPU=1) plus the device's timestamp resolution
// (ns per tick). Only true when the compute queue advertises timestamp support; flush_locked then
// prints per-op on-GPU milliseconds so fusion targets the dispatches that actually cost.
gpu_profile: bool,
timestamp_period: f32,
// Per-op roofline opt-in (VK_ROOFLINE=1, forces gpu_profile on). When set, flush_locked joins each
// matmul dispatch's exact moved-bytes + FLOPs (from its push shape, via `roofline_model`) to the
// measured GPU time and prints achieved GFLOP/s + achieved cold-weight GB/s + total GB/s per kernel,
// ranked. This is the honest bound (weight-bandwidth vs compute-peak) that says whether a kernel is
// memory-, compute-, or occupancy-bound BEFORE it is optimized. `peak_bw_gbps` is a one-shot
// device-to-device copy bandwidth measured at init when the flag is set (0.0 = not measured), the
// denominator for the %-of-peak column. f32 stored as bits in an atomic so &self stays shared.
roofline: bool,
peak_bw_gbps: std::sync::atomic::AtomicU32,
// Max workgroups (Σ grid x·y·z) recorded into one command buffer before an automatic flush, so a
// single queue submission stays well under the GPU driver's ring lockup timeout (RADV/amdgpu
// default 2 s). BATCH_CAP bounds the descriptor budget by dispatch COUNT; this bounds SUBMISSION
// TIME by dispatched work, which is what a long (2k+ token) prefill overruns -- its per-op counts
// are unchanged from a short prefill, but each attention/matmul dispatch does O(seq²)/O(seq) more
// work. Initialized from WORK_CAP / VK_WORK_CAP; `set_work_cap` retunes it (0 disables the bound).
work_cap: std::sync::atomic::AtomicU64,
// VK_KHR_push_descriptor device fns, present iff the driver advertises the extension (native
// AMD/NV; typically absent on WSL/Dozen). When set, `dispatch` pushes buffer handles inline
// into the command buffer via `vkCmdPushDescriptorSetKHR` instead of allocating + updating +
// binding a descriptor set per op — three driver calls and two heap Vecs per dispatch collapse
// to one recorded command, which is the dominant CPU cost on the decode hot path (the same op
// graph, hundreds of dispatches x 28 layers, re-recorded every token). Set VK_PUSH_DESC=0
// to force the legacy alloc+update path. Pipelines' set layouts are created with the
// PUSH_DESCRIPTOR_KHR flag exactly when this is `Some`, so the two paths never mix.
push_descriptor: Option<ash::khr::push_descriptor::Device>,
// kernel name -> built pipeline. &'static str keys: kernel names are compile-time literals.
pipelines: Mutex<HashMap<&'static str, CachedPipeline>>,
// Autotune scratch: an uncommitted `mul_mm_q4k_coopmat` variant (raw SPIR-V, plus its BM/BN tile
// geometry for the dispatch grid), so the evolutionary tuner can bench a genome through the real
// dispatch path before it is committed. Empty in every production build -- consulted only under the
// dedicated `__coopmat_variant` pipeline name, which nothing else dispatches.
coopmat_variant: Mutex<Option<(Vec<u8>, u32, u32)>>,
// Persistent per-dispatch Vulkan objects (command pool/buffer, fence, descriptor pool),
// reset and reused each dispatch instead of created+destroyed. The whole submit path is
// serialized through this Mutex (ops are sequential per tensor graph anyway), which both
// makes reuse sound and kills the per-op allocation churn that dominated dispatch latency.
submitter: Mutex<Submitter>,
// Deferred-safe buffer reuse pool (see BufPool). Separate mutex from `submitter`: drop only
// touches this lock, never `submitter`, so there's no lock cycle.
bufpool: Mutex<BufPool>,
}
// Reusable submit resources, created once at device init. Many dispatches are recorded into
// the single `cmd` and submitted together on flush (see `dispatch`/`flush_locked`), so the
// per-op CPU<->GPU fence stall is paid once per batch instead of once per op.
struct Submitter {
#[allow(dead_code)] // owns the pool that `cmd` is allocated from; freed with the device
cpool: vk::CommandPool,
cmd: vk::CommandBuffer,
fence: vk::Fence,
dpool: vk::DescriptorPool,
// Is `cmd` currently open with recorded-but-unsubmitted dispatches?
recording: bool,
// Dispatches recorded into `cmd` since it was begun (bounded by BATCH_CAP).
n: u32,
// Total workgroups (Σ grid x·y·z) recorded into `cmd` since it was begun. A cheap record-time
// proxy for the batch's on-GPU runtime: workgroups are the unit the GPU schedules onto CUs, and
// for the matmul/attention kernels that dominate a prefill they scale with the problem size, so
// this tracks the O(seq²) attention blow-up that makes a long prefill's single submission overrun
// the driver ring timeout. Bounded by `work_cap` (see the flush in `dispatch_outs`).
work: u64,
// Cumulative queue submissions (one per flush), never reset. A decode/short-prefill forward is one
// submission; a long prefill is several once the work bound splits it. Read via `submit_count` so a
// test can assert the split without racing a process-global counter (each device owns its Submitter).
submits: u64,
// Hazard tracking for selective barriers. Holds the output buffers written by dispatches
// recorded since the last memory barrier in the current command buffer. A new dispatch needs
// a barrier only if it READS a buffer in this set (a genuine read-after-write on data produced
// earlier in this same batch); independent dispatches (disjoint buffers, or that only read
// weights/inputs uploaded in an earlier already-fenced batch) record back-to-back with no
// barrier, so the GPU can overlap their fixed launch/drain overhead. Cleared on each emitted
// barrier and at the start of every batch. See `dispatch` for the correctness argument (WAW/WAR
// can't occur within a batch because the buffer pool only recycles handles across fences).
written_since_barrier: std::collections::HashSet<vk::Buffer>,
// Per-batch profiling accumulators (VK_PROFILE=1). `record_ns` is the wall time spent in
// the dispatch recording path (descriptor push/update + cmd_dispatch + barrier bookkeeping)
// since the batch began; `barriers` counts memory barriers emitted this batch. Both reset per
// batch and are read by `flush_locked` when profiling is on. Zero-overhead otherwise (the
// recording timer is only sampled when the device's `profile` flag is set).
record_ns: u128,
barriers: u32,
// Per-op GPU-time profiling (VK_PROFILE_GPU=1). `qpool` holds one TIMESTAMP query per dispatch
// plus a batch baseline at index 0; `op_names[i]` is the kernel of query i+1, so
// (ts[i+1]-ts[i]) x timestamp_period is that dispatch's on-GPU duration. flush_locked reads and
// aggregates them by op name -- the measurement that separates EXPENSIVE dispatches from merely
// numerous ones. Unused (empty op_names, pool never written) when the flag is off.
qpool: vk::QueryPool,
op_names: Vec<&'static str>,
// Per-dispatch push-constant snapshot (first 5 u32), parallel to `op_names`, filled only when
// gpu_profile is on. For matmul kernels the push is [m0, mcount, nout, k, woff]; `roofline_model`
// reads it to compute exact moved-bytes/FLOPs. Non-matmul kernels store whatever their push holds
// (or zeros) and are modelled as (0,0,0) -- shown with GPU time only. Cleared with op_names.
op_push: Vec<[u32; 5]>,
// Command-graph capture. When `capturing`, `dispatch_outs` records into `graph_cmd` (a dedicated,
// re-submittable command buffer owned by the in-flight capture) instead of `cmd`, and never
// auto-flushes: the whole decode forward records into one buffer that later replays per token
// with a single queue submit + fence wait (no per-op re-record, no descriptor churn). `graph_cmd`
// is null except between begin/end capture. The hazard-barrier bookkeeping (`written_since_barrier`)
// and the selective barriers are recorded into `graph_cmd` exactly as for eager batches, so a
// replay reproduces the identical dependency chain.
capturing: bool,
graph_cmd: vk::CommandBuffer,
}
// Safety: the contained handles are only ever touched while holding VkInner.submitter's Mutex.
unsafe impl Send for Submitter {}
#[derive(Clone)]
pub struct VulkanDevice {
inner: Arc<VkInner>,
}
impl std::fmt::Debug for VulkanDevice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "VulkanDevice({})", self.inner.gpu_id)
}
}
pub struct VulkanStorage {
buffer: vk::Buffer,
memory: vk::DeviceMemory,
count: usize,
dtype: DType,
// Is `memory` HOST_VISIBLE (directly CPU-mappable)? False when the buffer was placed in a
// DEVICE_LOCAL-only heap (the large GTT pool on this UMA APU), in which case uploads/readbacks
// go through a transient host-visible staging buffer + a GPU copy instead of a direct map.
host_visible: bool,
device: VulkanDevice,
}
impl std::fmt::Debug for VulkanStorage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"VulkanStorage(count={}, dtype={:?})",
self.count, self.dtype
)
}
}
/// A quantized expert bank de-interleaved into PLANAR field arrays (the layout the DSL block-reduced
/// MoE kernels bind), in the kernel's parameter order: Q4_K = `[wqs, wsc, wd, wdm]`, Q6_K =
/// `[wql, wqh, wsc, wd]`. Built once at model load by `quantize_q{4,6}k_split`, held resident and
/// reused every token via the MoE bank cache. One newtype for both quant types keeps the cache and
/// dispatch uniform -- the dtype-specific field count lives only in the repack + the .spv.
pub struct MoeBankSplit(pub Vec<VulkanStorage>);
/// A captured, replayable Vulkan decode command-graph. Holds a closed primary command buffer that
/// records one whole decode forward against stable input / KV / weight buffers. [`Self::replay`]
/// re-submits it in a single `queue_submit` + fence wait; the caller refreshes only the CONTENTS of
/// the stable input buffers (token embedding, decode position, attention seq_k) in place before each
/// replay -- none of the recorded commands change. This collapses the eager path's per-token re-record
/// of ~1.7k dispatches (record + submit CPU) into one replay, mirroring the shipped ROCm
/// `RocmGraphHandle` for the command-buffer-based Vulkan backend. The intermediates the capture
/// touched are reserved out of the buffer pool for the graph's lifetime (see BufPool), so every
/// replay reads/writes the exact storage it was captured against.
pub struct VkGraph {
cmd: vk::CommandBuffer,
fence: vk::Fence,
n_dispatch: u32,
device: VulkanDevice,
/// The pool buffers reserved during this graph's capture (its recorded descriptors bake their
/// handles). Owned for the graph's lifetime; drop returns them to the pool's `pending` list so a
/// retired graph's working set is recycled instead of stranded (see BufPool).
reserved: Vec<(u64, PooledBuf)>,
}
// Safety: the contained handles are only ever touched while holding VkInner.submitter's Mutex (replay)
// or after device_wait_idle (drop). Mirrors `unsafe impl Send for RocmGraphHandle`.
unsafe impl Send for VkGraph {}
impl VkGraph {
/// Number of dispatches captured into the graph (diagnostics).
pub fn n_dispatch(&self) -> u32 {
self.n_dispatch
}
/// Submit the captured decode forward and block until it completes. Serialized with eager submits
/// through the submitter lock (a Vulkan queue is externally synchronized). The caller must have
/// refreshed the stable input buffers (and awaited those writes) before calling; cross-submit
/// ordering on the single queue makes the refreshed inputs visible to the replay, exactly as the
/// eager path relies on queue_submit + fence for its cross-batch ordering.
pub fn replay(&self) -> Result<()> {
let dev = self.device.dev();
let queue = self.device.inner.queue;
let s = self.device.inner.submitter.lock().unwrap();
unsafe {
dev.reset_fences(&[self.fence]).map_err(vkerr)?;
let cmds = [self.cmd];
dev.queue_submit(
queue,
&[vk::SubmitInfo::default().command_buffers(&cmds)],
self.fence,
)
.map_err(vkerr)?;
dev.wait_for_fences(&[self.fence], true, u64::MAX)
.map_err(vkerr)?;
}
drop(s);
Ok(())
}
}
impl Drop for VkGraph {
fn drop(&mut self) {
let dev = self.device.dev();
unsafe {
// The teardown runs under the submitter lock: queue access (device_wait_idle waits every
// queue) must be externally synchronized with eager submits, which makes dropping a graph
// sound from any thread. The engine retires graphs at capture points, so this cost never
// lands on a first token.
if let Ok(s) = self.device.inner.submitter.lock() {
let _ = dev.device_wait_idle();
dev.destroy_fence(self.fence, None);
dev.free_command_buffers(s.cpool, &[self.cmd]);
}
}
// Return the capture-reserved working set straight to the free lists. The device was just
// quiesced and the recorded commands that baked these handles no longer exist, so this is
// strictly stronger than reclaim()'s post-flush-fence precondition — and it matters that the
// buffers become allocatable NOW: the next sequence's first forward allocates its transients
// before it ever flushes, so a `pending` hand-off would leave the returned set unreachable
// exactly when it is needed and force a fresh working-set allocation.
if let Ok(mut pool) = self.device.inner.bufpool.lock() {
for (bytes, p) in self.reserved.drain(..) {
pool.free.entry(bytes).or_default().push(p);
pool.free_bytes += bytes;
}
}
}
}
/// The shared, refreshable attention buffers a captured decode graph binds for [`VulkanDevice::
/// sdpa_blk_vk_graph`]. Decode attention has the identical shape and KV-cache strides in every layer,
/// so ONE `scale` (constant) and ONE `meta` buffer serve the whole forward; the only per-token change
/// is the attended key count `seq_k`, refreshed in `meta[1]` in place by [`Self::set_seq_k`] before
/// each replay. This owns the buffers so their handles stay stable for the graph's lifetime and keeps
/// the `meta` field layout in one place next to the kernel that reads it (the engine supplies dims,
/// never the raw layout). `meta` layout matches `sdpa_blk_vk`:
/// `[seq_q, seq_k, n_heads, n_kv, causal, kv_batch_stride, kv_head_stride, key_stride]` (u32, elements).
pub struct VkGraphAttn {
scale: VulkanStorage,
meta: VulkanStorage,
// Host copies of the fixed decode-attention geometry, so the flash-decoding graph variant
// ([`VulkanDevice::sdpa_decode_split_vk_graph`]) can build its push constants without reading them
// back off the `meta` SSBO. All constant for the graph's life; only `meta[1]` (seq_k) advances.
n_kv: usize,
softmax_scale: f32,
kv_batch_stride: usize,
kv_head_stride: usize,
key_stride: usize,
}
// Safety: the contained storages' handles are only touched under the device locks (build/replay) or
// via the host-coherent map in `set_seq_k`, exactly as the eager sdpa scalar SSBOs are.
unsafe impl Send for VkGraphAttn {}
impl VkGraphAttn {
/// The shared softmax-scale SSBO (binding 4 of `sdpa_blk`). Constant for the graph's life.
pub fn scale(&self) -> &VulkanStorage {
&self.scale
}
/// The shared attention-meta SSBO (binding 5 of `sdpa_blk`). `meta[1]` (seq_k) advances per replay.
pub fn meta(&self) -> &VulkanStorage {
&self.meta
}
/// Fixed decode-attention geometry for the flash-decoding graph variant (`sdpa_decode_split_vk_graph`).
pub fn n_kv(&self) -> usize {
self.n_kv
}
pub fn softmax_scale(&self) -> f32 {
self.softmax_scale
}
pub fn kv_batch_stride(&self) -> usize {
self.kv_batch_stride
}
pub fn kv_head_stride(&self) -> usize {
self.kv_head_stride
}
pub fn key_stride(&self) -> usize {
self.key_stride
}
/// Refresh the attended key count in place before a replay. A captured decode graph binds the FULL
/// fixed-shape KV cache once; this advances the span it attends to `[0, seq_k)` without re-record.
pub fn set_seq_k(&self, seq_k: usize) -> Result<()> {
// seq_q stays 1; only meta[1] moves. Host-coherent map (small SSBO placed host-visible), so the
// write is visible to the next queue_submit exactly as the eager per-call meta upload is.
unsafe {
self.meta.device.write_u32(
self.meta.buffer,
self.meta.memory,
self.meta.host_visible,
&[1, seq_k as u32],
)
}
}
}
/// Arguments for [`VulkanDevice::paged_attention_vk`]. All tensors are f32 `VulkanStorage`
/// except `block_tables`/`context_lens` (u32). Strides are in f32 ELEMENTS. Grouped into a
/// struct because the kernel needs many invariants (see the codebase 6+ arg convention).
pub struct PagedAttnArgs<'a> {
pub q: &'a VulkanStorage,
pub key_cache: &'a VulkanStorage,
pub value_cache: &'a VulkanStorage,
pub block_tables: &'a VulkanStorage,
pub context_lens: &'a VulkanStorage,
pub num_seqs: usize,
pub num_heads: usize,
pub num_kv_heads: usize,
pub head_size: usize,
pub block_size: usize,
pub max_num_blocks_per_seq: usize,
pub q_stride: usize,
pub kv_block_stride: usize,
pub kv_head_stride: usize,
pub x: usize,
pub max_context_len: usize,
pub scale: f32,
}
/// Arguments for [`VulkanDevice::reshape_and_cache_vk`]. f32 tensors except `slot_mapping`
/// (u32, pad = 0xFFFFFFFF). Strides in f32 elements.
pub struct ReshapeCacheArgs<'a> {
pub key: &'a VulkanStorage,
pub value: &'a VulkanStorage,
pub key_cache: &'a VulkanStorage,
pub value_cache: &'a VulkanStorage,
pub slot_mapping: &'a VulkanStorage,
pub num_tokens: usize,
pub num_heads: usize,
pub head_size: usize,
pub block_size: usize,
pub key_stride: usize,
pub value_stride: usize,
pub x: usize,
}
// Deferred-safe buffer pool. Buffers are never freed inline: deferred dispatch may still hold a
// buffer's handle in an unflushed command buffer, so freeing on drop would be use-after-free. On
// drop a buffer parks in `pending`; after the next flush+fence (the awaited batch is provably done
// on the GPU) `reclaim` moves it to the size-keyed `free` list, where `raw_buffer` reuses it. This
// bounds device memory to the peak working set and reuses buffers across tokens, instead of leaking
// every allocation (which OOM'd a full unquantized forward on Dozen's ~8GB heap).
// Pool entries carry whether their backing memory is HOST_VISIBLE (mappable from the CPU). With the
// device-memory placement strategy a large buffer may be DEVICE_LOCAL-only, so reuse must preserve
// this flag — the upload/readback path branches on it (direct map vs staging copy).
#[derive(Clone, Copy)]
struct PooledBuf {
buffer: vk::Buffer,
memory: vk::DeviceMemory,
host_visible: bool,
}
// Size-class for the reuse pool. Decode attention allocates transient tensors whose size grows by
// one token each step (score [b,h,1,cur_len], softmax, att@v); keying `free` by exact byte size
// means every token requests a never-before-seen size, so nothing is ever reused and each token
// leaks a fresh buffer -> O(seq^2) device memory -> OOM on long generations. That LEAK is bounded by
// the idle-pool cap below (reclaim() destroys idle buffers over POOL_FREE_CAP_BYTES); the cap alone
// is sufficient to prevent the OOM.
//
// CORRECTNESS: keys MUST be exact byte size. Power-of-two bucketing (rounding a >64KiB request up to
// the next pow2 so a later differently-sized request can reuse the buffer) was tried (commit
// d9b70910) and silently CORRUPTED inference: a reused bucket buffer is physically larger than the
// current logical tensor, and a consumer reads into the stale tail [logical, physical), producing
// garbled (non-deterministic) logits on EVERY model (0.6B and 8B both reproduced; CPU + native
// llama.cpp Vulkan stayed coherent on the same GPU+model, proving it was this pool, not the
// GPU/driver/model). The invariant the bucketing assumed ("kernels touch only the first n elems via
// their push-constant count, tail unused") does not hold somewhere on the readback/copy path. Exact
// keys keep physical == logical so there is never a stale tail. Re-enabling cross-size reuse for the
// growing attention-score buffers is a PERF follow-up (find the physical-size-reading consumer and
// make it honor the logical count, or zero reused buffers) -- not a correctness one; the cap below
// already bounds memory.
const POOL_EXACT_MAX: u64 = u64::MAX; // always exact: pow2 bucketing corrupts inference (see above).
const fn pool_bucket(bytes: u64) -> u64 {
let b = if bytes < 4 { 4 } else { bytes };
if b <= POOL_EXACT_MAX {
b
} else {
b.next_power_of_two()
}
}
#[cfg(test)]
mod bufpool_key_invariant {
use super::{pool_bucket, POOL_EXACT_MAX};
/// A pool key must equal the requested byte size at every size. Rounding a large request up to a
/// size class hands the caller a buffer physically larger than its logical tensor, and a consumer
/// on the readback/copy path then reads the stale tail [logical, physical) -- garbled,
/// non-deterministic logits.
///
/// This guards a regression that has landed twice. d9b70910 introduced the bucketing; eac0ddee
/// removed it; merge 8c2b480d then grafted onto a base that predated the region and silently
/// dropped the removal, and merge 3a4cf7b0 re-seeded the bucketing from a branch that never had
/// the fix. The commits stayed reachable, so the history read as fixed for eight weeks while the
/// defect was live. eac0ddee carried no test; this is that test, and it fails on the pre-fix
/// constant rather than trusting the log.
#[test]
fn keys_are_exact_at_every_size() {
assert_eq!(
POOL_EXACT_MAX,
u64::MAX,
"size-class bucketing corrupts inference: keys must be exact at every size"
);
for bytes in [4u64, 64 * 1024, 64 * 1024 + 1, 100_000, 1 << 20, (1 << 20) + 7, 1 << 30] {
assert_eq!(
pool_bucket(bytes),
bytes,
"pool key must equal the request exactly, got a rounded class for {bytes} bytes"
);
}
}
}
// Cap the idle (free, unreferenced) pool so a workload that touches many distinct large size-classes
// can't retain them all forever. reclaim() destroys real device buffers once free exceeds this. The
// peak working set of a forward fits well under this; it only bounds the long tail. ~12 GiB.
const POOL_FREE_CAP_BYTES: u64 = 12 * 1024 * 1024 * 1024;
#[derive(Default)]
struct BufPool {
pending: Vec<(u64, PooledBuf)>,
free: HashMap<u64, Vec<PooledBuf>>,
// Sum of bucket sizes currently held in `free` (idle, reusable). Tracked so reclaim can enforce
// POOL_FREE_CAP_BYTES without walking the whole map.
free_bytes: u64,
// Command-graph capture reservation (mirrors the shipped ROCm `PoolInner` capture pinning). A
// captured decode command buffer bakes the vk::Buffer handle of every intermediate it touches
// into its recorded descriptors; on every replay it reads/writes those exact buffers. If such a
// buffer were dropped back into `pending`/`free` and later handed to an unrelated allocation, the
// replay would alias — and corrupt — live tensor storage: the fluent-but-stale decode loop. While
// `capture_depth > 0`, a dropped buffer is instead parked in `reserved` (bucket key kept for its
// later return) so no later allocation can reuse a handle the in-flight graph captured. The
// reservation follows the GRAPH's lifetime, not the process's: `end_graph_capture` moves this
// era's reservations into the returned `VkGraph`, whose drop hands them back to `pending`.
// Without that return, every recapture (sequences retire graphs on the naive KV cache) would
// strand a full transient working set here — an unbounded leak that also forces each new
// sequence to allocate a fresh working set instead of reusing the pool.
capture_depth: usize,
reserved: Vec<(u64, PooledBuf)>,
}
// drop: park (size, buffer, memory) for reuse after the next fence. No Vulkan calls here, just
// bookkeeping, so it's cheap and can't race the GPU.
impl Drop for VulkanStorage {
fn drop(&mut self) {
if self.buffer == vk::Buffer::null() {
return;
}
// Park under the bucket key (raw_buffer allocated this buffer at its bucket size), so the
// physical buffer matches the key a later same-bucket request looks up.
let bytes = pool_bucket((self.count * self.dtype.size_in_bytes()) as u64);
if let Ok(mut pool) = self.device.inner.bufpool.lock() {
let pooled = PooledBuf {
buffer: self.buffer,
memory: self.memory,
host_visible: self.host_visible,
};
// During a command-graph capture, this handle may be baked into the graph; reserve it
// (recycled only when the graph is torn down) so no later allocation aliases the graph's
// storage on replay. Gating at drop time (not alloc time) is correct and needs no
// per-storage flag: every buffer the capture touches and then releases is dropped while
// `capture_depth > 0`; buffers the engine keeps live across the graph (weights, KV cache,
// logits) never hit this path.
if pool.capture_depth > 0 {
pool.reserved.push((bytes, pooled));
} else {
pool.pending.push((bytes, pooled));
}
}
}
}
// --- low level ash plumbing (generalized from the probe Ctx) ---
impl VulkanDevice {
fn dev(&self) -> &ash::Device {
&self.inner.device
}
/// Cooperative-matrix (matrix-core) tile `(M, N, K)` if the device supports an
/// fp16xfp16 -> fp32 subgroup config, else `None`. Used to pick the coopmat matmul path.
pub fn coopmat_info(&self) -> Option<(u32, u32, u32)> {
self.inner.coopmat.then_some(self.inner.cm_mnk)
}
/// Quantize `W[nout x k]` (row-major fp32) to Q8_0 and upload it to the GPU once. Per 32-block:
/// one fp16 scale + 32 int8, packed into 9 u32. Returns the device buffer to reuse across many
/// matvecs (weights are constant during decode). `k` must be a multiple of 32.
pub fn quantize_q8(&self, w: &[f32], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(32) {
crate::bail!("quantize_q8: k must be a multiple of 32, got {k}");
}
if w.len() != nout * k {
crate::bail!("quantize_q8: w len {} != {nout}*{k}", w.len());
}
let nblocks = k / 32;
let mut packed = vec![0u32; nout * nblocks * 9];
for n in 0..nout {
for b in 0..nblocks {
let blk = &w[n * k + b * 32..n * k + b * 32 + 32];
let amax = blk.iter().fold(0f32, |m, &v| m.max(v.abs()));
let inv = if amax > 0.0 { 127.0 / amax } else { 0.0 };
let scale = if amax > 0.0 { amax / 127.0 } else { 1.0 };
let o = (n * nblocks + b) * 9;
packed[o] = half::f16::from_f32(scale).to_bits() as u32;
for j in 0..8 {
let mut word = 0u32;
for l in 0..4 {
let q = (blk[j * 4 + l] * inv).round().clamp(-127.0, 127.0) as i32 as i8;
word |= ((q as u8) as u32) << (l * 8);
}
packed[o + 1 + j] = word;
}
}
}
self.upload_u32(&packed)
}
/// Upload an already-Q8_0-quantized weight to the GPU verbatim. `data` is the GGUF `BlockQ8_0`
/// bytes (34 B/block = f16 scale + 32 int8), repacked LOSSLESLY into the kernel's 9-u32 layout
/// (scale in u32[0] low half, 32 int8 four-per-word in u32[1..9]) -- the same layout
/// [`quantize_q8`] emits, so the matvec/matmul decode is identical and no f32 round-trip is
/// needed. This is the MoE-bank path: a Q8_0 [E,n,k] expert bank uploads once with `nout = E*n`
/// and stays quantized. `data` must be exactly `nout * (k/32) * 34` bytes; `k` a multiple of 32.
pub fn quantize_q8_blocks(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(32) {
crate::bail!("quantize_q8_blocks: k must be a multiple of 32, got {k}");
}
let nblocks = k / 32;
let want = nout * nblocks * 34;
if data.len() != want {
crate::bail!(
"quantize_q8_blocks: data len {} != {nout}*{nblocks}*34 = {want}",
data.len()
);
}
// Source block: bytes [0,2) = f16 scale (LE), bytes [2,34) = 32 int8. Repack each block into
// 9 u32: u32[0] low half = the scale bits verbatim; u32[1..9] = the 32 int8, 4 per word.
let mut packed = vec![0u32; nout * nblocks * 9];
for blk in 0..nout * nblocks {
let src = &data[blk * 34..blk * 34 + 34];
let o = blk * 9;
packed[o] = u16::from_le_bytes([src[0], src[1]]) as u32;
for j in 0..8 {
let b = 2 + j * 4;
packed[o + 1 + j] =
u32::from_le_bytes([src[b], src[b + 1], src[b + 2], src[b + 3]]);
}
}
self.upload_u32(&packed)
}
/// Q8_0 matrix-vector: `y[nout] = Wq * x[k]` where `Wq` came from [`quantize_q8`]. The kernel
/// reads weights at ~1.125 bytes/elem instead of 4 — the bandwidth lever for memory-bound
/// decode on this APU. Expect ~1e-2 error (fp16-scale int8 quantization).
pub fn matvec_q8(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_q8: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_q8_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Q8_0 matvec with both operands already on the GPU: `y[nout] = Wq * x[k]`, no host round-trip.
/// This is the engine decode path -- weights stay quantized in VRAM (~1.125 B/elem) instead of
/// dequantizing to f32, so decode reads ~3.5x less memory (the bandwidth lever vs llama.cpp).
pub fn matvec_q8_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
self.matvec_q8_gpu_off(wq, x, nout, k, 0)
}
/// Q8_0 matvec into a slice of `wq` starting at `woff` u32 words: `y[nout] = Wq[woff..] * x[k]`.
/// `woff == 0` is bit-identical to [`matvec_q8_gpu`]; a non-zero offset selects one expert's row
/// block of a resident MoE bank (`woff = e * nout * (k/32) * 9`), so the whole [E,n,k] Q8 bank
/// stays uploaded once and each routed expert reads only its own slice (no per-token re-upload).
pub fn matvec_q8_gpu_off(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
woff: usize,
) -> Result<VulkanStorage> {
if x.count < k {
crate::bail!("matvec_q8_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32, woff as u32]);
if self.inner.subgroup_matvec {
// Subgroup-reduced kernel: one subgroup per output row, fused subgroupAdd. A workgroup
// of WG1D (64) invocations holds WG1D/subgroup_size subgroups, so it produces that many
// rows; dispatch ceil(nout / rows_per_wg) workgroups. subgroup_size is >=2 (checked at
// init) and <=64 in practice, so rows_per_wg is in [1, 32].
let rows_per_wg = (WG1D / self.inner.subgroup_size).max(1);
self.dispatch(
"mul_mat_vec_q8_sg",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(rows_per_wg), 1, 1),
)?;
} else {
self.dispatch(
"mul_mat_vec_q8",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
}
Ok(out)
}
/// Q4_0 matrix-vector reading the *native GGML* 18-byte block format straight from a buffer
/// uploaded verbatim by [`upload_qweight`] (no requantize): `y[nout] = Wq * x[k]`. Weights stay
/// quantized (~0.56 B/elem) instead of dequantizing to f32, so decode reads ~7x less weight
/// memory — the bandwidth lever on this APU. Decode is byte-exact with `BlockQ4_0::to_float`.
/// `k` must be a multiple of 32. One invocation per output row (scalar kernel).
pub fn matvec_q4_0_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(32) {
crate::bail!("matvec_q4_0_gpu: k must be a multiple of 32, got {k}");
}
if x.count < k {
crate::bail!("matvec_q4_0_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_q4_0",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// Q8_0 matrix-vector reading the *native GGML* 34-byte block format straight from a buffer
/// uploaded verbatim by [`upload_qweight`] (no re-pack): `y[nout] = Wq * x[k]`. Distinct from
/// [`matvec_q8_gpu`], which consumes the repacked 9-u32 layout from [`quantize_q8_blocks`]; this
/// one byte-addresses raw GGUF bytes. Weights stay quantized (~1.06 B/elem) so decode reads ~3.8x
/// less weight memory. Decode is byte-exact with `BlockQ8_0::to_float`. `k` a multiple of 32.
pub fn matvec_q8_0_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(32) {
crate::bail!("matvec_q8_0_gpu: k must be a multiple of 32, got {k}");
}
if x.count < k {
crate::bail!("matvec_q8_0_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_q8_0",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// Upload Q4_K weights to the GPU once, verbatim in the GGUF super-block layout (144 B = 36 u32
/// per 256-weight block, `nout * k/256` blocks total). No requantize: the bytes are the same
/// blocks the CPU `k_quants::BlockQ4K` holds, so the in-shader decode matches the CPU dequant
/// exactly. Returns the device buffer to reuse across matvecs. `data` must be exactly the
/// `nout * (k/256) * 144` packed block bytes; `k` must be a multiple of 256.
pub fn quantize_q4k(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_q4k: k must be a multiple of 256, got {k}");
}
let nblocks = k / 256;
let want = nout * nblocks * 144;
if data.len() != want {
crate::bail!(
"quantize_q4k: data len {} != {nout}*{nblocks}*144 = {want}",
data.len()
);
}
// Reinterpret the packed block bytes as u32 words for the kernel's `uint w[]` binding. GGUF
// blocks are 144 bytes (a multiple of 4), so the length is u32-aligned by construction.
let words: &[u32] =
unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u32, data.len() / 4) };
self.upload_u32(words)
}
/// Flash-attention over Q/K/V already in VRAM; output `[BH, Lq, D]`. `scale` multiplies QK^T
/// scores, `causal` applies aligned causal masking (query qi attends to keys <= qi + (Lk - Lq)).
/// One dispatch per (bh, query) output row.
#[allow(clippy::too_many_arguments)]
pub fn flash_attn_gpu(
&self,
q: &VulkanStorage,
k: &VulkanStorage,
v: &VulkanStorage,
bh: usize,
lq: usize,
lk: usize,
d: usize,
scale: f32,
causal: bool,
) -> Result<VulkanStorage> {
if d > 256 {
crate::bail!("flash_attn_gpu: head_dim {d} > 256 (kernel limit)");
}
if q.count < bh * lq * d {
crate::bail!(
"flash_attn_gpu: q count {} < bh*lq*d {}",
q.count,
bh * lq * d
);
}
if k.count < bh * lk * d || v.count < bh * lk * d {
crate::bail!(
"flash_attn_gpu: k/v count too small for bh*lk*d {}",
bh * lk * d
);
}
let out = self.alloc_f32(bh * lq * d)?;
// Push block matches flash_attn.comp: {u32 bh, lq, lk, d; f32 scale; u32 causal}.
let mut push = push_u32(&[bh as u32, lq as u32, lk as u32, d as u32]);
push.extend_from_slice(&scale.to_ne_bytes());
push.extend_from_slice(&(causal as u32).to_ne_bytes());
let total = bh * lq;
self.dispatch(
"flash_attn",
&[q.buffer, k.buffer, v.buffer, out.buffer],
&push,
((total as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// Host-input convenience for [`flash_attn_gpu`]: uploads Q/K/V and returns the `[BH*Lq*D]`
/// output as an f32 vector. For tests/standalone use.
#[allow(clippy::too_many_arguments)]
pub fn flash_attn(
&self,
q: &[f32],
k: &[f32],
v: &[f32],
bh: usize,
lq: usize,
lk: usize,
d: usize,
scale: f32,
causal: bool,
) -> Result<Vec<f32>> {
let qs = self.upload_f32(q)?;
let ks = self.upload_f32(k)?;
let vs = self.upload_f32(v)?;
self.flash_attn_gpu(&qs, &ks, &vs, bh, lq, lk, d, scale, causal)?
.to_vec_f32()
}
/// Q4_K matrix-vector: `y[nout] = Wq * x[k]` where `Wq` came from [`quantize_q4k`]. Reads weights
/// at ~4.5 bits/elem instead of 32 -- the bandwidth lever for memory-bound decode on this APU.
/// Decode matches the CPU `BlockQ4K::to_float` so expect ~1e-3 relative error vs CPU f32 (the
/// quantization error is already baked into the stored blocks).
/// Q4_0 matvec with the activation supplied as a host f32 slice (`x.len() == k`): uploads `x`
/// to the GPU then dispatches [`matvec_q4_0_gpu`], reading the result back to host. Mirrors the
/// [`matvec_q4k`] host wrapper so the backend-parametric bench can call one method per dtype.
pub fn matvec_q4_0(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_q4_0: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_q4_0_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Q8_0 matvec with the activation supplied as a host f32 slice (`x.len() == k`): uploads `x`
/// to the GPU then dispatches [`matvec_q8_0_gpu`], reading the result back to host. Mirrors the
/// [`matvec_q4k`] host wrapper so the backend-parametric bench can call one method per dtype.
pub fn matvec_q8_0(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_q8_0: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_q8_0_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
pub fn matvec_q4k(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_q4k: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_q4k_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Q4_K decode matvec via int8 dp4a (default where int_dot8; VK_DP4A_DECODE_OFF forces scalar):
/// quantize x to q8_1, then dp4a the Q4_K codes (column dp4a at mcount=1). ~1.8x faster than the scalar subgroup matvec
/// on gfx1151; the q8_1 activation quant adds ~0.5-1% vs the scalar reference (gated < 2e-2).
pub fn matvec_q4k_dp4a(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
let xin = self.upload_f32(x)?;
let (xq, xs, xsum) = self.quantize_act_q8(&xin, 1, k)?;
let out = self.alloc_f32(nout)?;
let push = push_u32(&[0u32, 1u32, nout as u32, k as u32, 0u32]);
self.dispatch(
"mul_mat_q4k_dp4a",
&[wq.buffer, xq.buffer, xs.buffer, xsum.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
out.to_vec_f32()
}
/// Q4_K decode matvec via the SCALAR float path (forces the non-dp4a kernel regardless of the
/// VK_DP4A_DECODE_OFF default): the exact CPU-faithful reference for the dp4a gates.
pub fn matvec_q4k_scalar(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
let xin = self.upload_f32(x)?;
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32, 0u32]);
if self.inner.subgroup_matvec {
let rows_per_wg = (WG1D / self.inner.subgroup_size).max(1);
self.dispatch(
"mul_mat_vec_q4k_sg",
&[wq.buffer, xin.buffer, out.buffer],
&push,
((nout as u32).div_ceil(rows_per_wg), 1, 1),
)?;
} else {
self.dispatch(
"mul_mat_vec_q4k",
&[wq.buffer, xin.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
}
out.to_vec_f32()
}
/// Host helper for the fused rope_norm gate/bench: f32 in -> Vec<f32> out (mirrors `matvec_q4k`).
/// `x` is [b,h,t,d], `weight` [d], `cos`/`sin` [t,d/2]. Runs `rms_norm(x,weight,eps)` then NeoX
/// rope in ONE dispatch; compare against the two-op chain to validate bit-exactness.
#[allow(clippy::too_many_arguments)]
pub fn rope_norm_f32(
&self,
x: &[f32],
weight: &[f32],
cos: &[f32],
sin: &[f32],
b: usize,
h: usize,
t: usize,
d: usize,
eps: f32,
) -> Result<Vec<f32>> {
let xs = self.upload_f32(x)?;
let ws = self.upload_f32(weight)?;
let cs = self.upload_f32(cos)?;
let ss = self.upload_f32(sin)?;
let out = xs.rope_norm(
&Layout::contiguous((b, h, t, d)),
&ws,
&Layout::contiguous(d),
eps,
&cs,
&Layout::contiguous((t, d / 2)),
&ss,
&Layout::contiguous((t, d / 2)),
)?;
out.to_vec_f32()
}
/// Host helper for the fused add_rmsnorm gate: f32 in -> (s, y) out. `x`/`residual` are [nrows,m],
/// `alpha` [m]. s = x+residual; y = rms_norm(s)*alpha. Compare against the add-then-rms_norm chain.
pub fn add_rmsnorm_f32(
&self,
x: &[f32],
residual: &[f32],
alpha: &[f32],
nrows: usize,
m: usize,
eps: f32,
) -> Result<(Vec<f32>, Vec<f32>)> {
let xs = self.upload_f32(x)?;
let rs = self.upload_f32(residual)?;
let al = self.upload_f32(alpha)?;
let (s, y) = xs.add_rmsnorm(
&Layout::contiguous((nrows, m)),
&rs,
&Layout::contiguous((nrows, m)),
&al,
&Layout::contiguous(m),
eps,
)?;
Ok((s.to_vec_f32()?, y.to_vec_f32()?))
}
/// Q4_K matvec with both operands already on the GPU: `y[nout] = Wq * x[k]`, no host round-trip.
/// Weights stay quantized in VRAM (~4.5 bits/elem) instead of dequantizing to f32, so decode
/// reads ~7x less weight memory. One invocation per output row (scalar kernel).
pub fn matvec_q4k_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
self.matvec_q4k_gpu_off(wq, x, nout, k, 0)
}
/// Q4_K matvec into a slice of `wq` starting at `woff` u32 words: `y[nout] = Wq[woff..] * x[k]`.
/// `woff == 0` is bit-identical to [`matvec_q4k_gpu`]; a non-zero offset selects one expert's row
/// block of a resident MoE bank (`woff = e * nout * (k/256) * 36`), so the whole [E,n,k] Q4_K
/// bank stays uploaded once and each routed expert reads only its own slice.
pub fn matvec_q4k_gpu_off(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
woff: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_q4k_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_q4k_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
// DEFAULT Q4_K decode path where the device advertises subgroup arithmetic: the coalesced
// multi-thread f32 matvec (mul_mat_vec_q4k_cm). TPB=16 threads cooperate on one super-block,
// adjacent threads reading adjacent qs u32 words (a coalesced 64-byte burst); Q4K_CM_ROWS rows
// per workgroup amortise the shared activation load; the per-row partial reduces with a subgroup
// add. The memory-BW lever for decode on this bandwidth-bound UMA APU -- the Q4_K analogue of
// mul_mat_vec_q6k_cm, and it reads x directly so the dp4a path's per-matvec q8 activation
// quantize dispatch drops. Decode is bit-identical to BlockQ4K::to_float; unpackHalf2x16 decodes
// the f16 d/dmin so subnormal scales stay exact. VK_Q4K_CM_OFF reverts to the dp4a block kernel
// for the A/B. Q4K_CM_ROWS MUST equal the shader's `NR` constant.
if self.inner.subgroup_matvec && std::env::var_os("VK_Q4K_CM_OFF").is_none() {
const Q4K_CM_ROWS: u32 = 2;
let push = push_u32(&[nout as u32, k as u32, woff as u32]);
self.dispatch(
"mul_mat_vec_q4k_cm",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(Q4K_CM_ROWS), 1, 1),
)?;
return Ok(out);
}
// dp4a Q4_K decode where the device has int8 dot-product: quantize the activation to
// q8_1 once, then dp4a the Q4_K codes against it -- Vulkan decode was matvec-compute-bound and
// int8 dp4a is the lever (the scalar f32 decode+MAC per weight was the wall). The COLUMN dp4a
// (one thread/output-row, 64 rows/workgroup) is the optimum: 1.8x over the scalar matvec on
// gfx1151, and MEASURED best vs subgroup/ILP/vectorized variants (decode wants many rows in
// flight, not per-row parallelism). VK_DP4A_DECODE_OFF forces the scalar fallback (the exact
// reference for the bit-exact gate vulkan_q4k_dp4a_decode_matches_scalar).
if self.inner.int_dot8 && std::env::var_os("VK_DP4A_DECODE_OFF").is_none() {
let (xq, xs, xsum) = self.quantize_act_q8(x, 1, k)?;
// Block-reduce dp4a (matvec_q4k_dp4a_blk: one workgroup/row, 64 threads tree-reduce over k)
// is the fast dense decode matvec: quantize the activation to q8 once, then dp4a the Q4_K
// codes. Reads the verbatim packed weight; dense (woff == 0) only -- a resident MoE bank
// (woff != 0) uses the column kernel below. VK_DP4A_BLK_OFF forces the column kernel for the
// A/B. (The block kernel's f16 scale decode was subnormal-broken and garbled qwen3 decode;
// fixed in hanzo-kernel f16lo_to_f32 + the regenerated .spv, gated by
// dense_q4k_block_real_qwen3_weight_repro.)
if woff == 0 && std::env::var_os("VK_DP4A_BLK_OFF").is_none() {
let meta = self.upload_u32(&[k as u32])?;
let bufs = [wq.buffer, xq.buffer, xs.buffer, xsum.buffer, out.buffer, meta.buffer];
self.dispatch_out("matvec_q4k_dp4a_blk", &bufs, 4, &[], (nout as u32, 1, 1))?;
return Ok(out);
}
let bufs = [wq.buffer, xq.buffer, xs.buffer, xsum.buffer, out.buffer];
let pushd = push_u32(&[0u32, 1u32, nout as u32, k as u32, woff as u32]);
self.dispatch(
"mul_mat_q4k_dp4a",
&bufs,
&pushd,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
return Ok(out);
}
let push = push_u32(&[nout as u32, k as u32, woff as u32]);
if self.inner.subgroup_matvec {
// Subgroup-reduced kernel: one subgroup per output row, fused subgroupAdd, more
// memory-level parallelism on this bandwidth-bound APU. Q4_K decode is the dense-layer
// decode hot path. A WG1D (64) workgroup holds WG1D/subgroup_size subgroups (rows), so
// dispatch ceil(nout / rows_per_wg) workgroups. Decode is bit-identical to the scalar
// mul_mat_vec_q4k kernel.
let rows_per_wg = (WG1D / self.inner.subgroup_size).max(1);
self.dispatch(
"mul_mat_vec_q4k_sg",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(rows_per_wg), 1, 1),
)?;
} else {
self.dispatch(
"mul_mat_vec_q4k",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
}
Ok(out)
}
/// Fused MoE grouped quant matvec on the GPU: for each routed slot `s` (`0..nrows`) and output
/// row `r` (`0..n`), computes `y[s, r] = sum_k W[ids[s], r, k] * x[s, k]`, reading the per-expert
/// slice from a single resident GGML weight bank `[E, n, k]` (uploaded via [`quantize_q4k`] /
/// [`quantize_q8_blocks`]). The router gather (which expert) and the per-expert GEMM run in ONE
/// dispatch -- the whole MoE expert compute stays on the GPU: no CPU expert loop, no routing-id
/// readback, no per-token weight upload, no index_add scatter. The output is already in slot order
/// `[nrows, n]`, so the engine's `broadcast_mul(scores).sum` combine handles the rest.
///
/// `kernel` selects the bank dtype variant ("moe_matvec_q4k" for a verbatim 144-B Q4_K bank, or
/// "moe_matvec_q8_0" for the 9-u32/36-B repacked Q8_0 bank from [`quantize_q8_blocks`]). The
/// expert-id -> bank-row mapping is `(eid*n + r)` rows of `k/block` blocks each, identical to the
/// host path's `woff = eid * per_expert_words`. `wbank`/`x`/`ids` are device buffers; `ids` holds
/// one u32 expert id per slot and is consumed on the GPU (never read to host).
pub fn moe_matvec_gpu(
&self,
kernel: &'static str,
wbank: &VulkanStorage,
x: &VulkanStorage,
ids: &VulkanStorage,
nrows: usize,
n: usize,
k: usize,
) -> Result<VulkanStorage> {
if x.count < nrows * k {
crate::bail!(
"moe_matvec_gpu: x count {} < nrows*k {}",
x.count,
nrows * k
);
}
if ids.count < nrows {
crate::bail!("moe_matvec_gpu: ids count {} < nrows {nrows}", ids.count);
}
let total = nrows * n;
let out = self.alloc_f32(total)?;
let push = push_u32(&[n as u32, k as u32, nrows as u32]);
self.dispatch(
kernel,
&[wbank.buffer, x.buffer, ids.buffer, out.buffer],
&push,
((total as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// Q4_0 matrix-matrix (prefill): `y[m, nout] = x[m, k] * Wq^T`, weights stay quantized in VRAM
/// (verbatim native GGML Q4_0 18-byte blocks from [`upload_qweight`], same decode as
/// [`matvec_q4_0_gpu`]). Decodes each weight value once and reuses it across a tile of up to
/// [`MATMUL_Q_MAX_M`] rows, so weight memory traffic matches a single matvec per output column
/// instead of dequantizing the whole weight to f32 every forward -- the prefill bandwidth lever.
/// `x` must be a contiguous `[m, k]` device buffer; returns `[m, nout]` row-major. `k` a multiple
/// of 32.
pub fn matmul_q4_0_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
m: usize,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(32) {
crate::bail!("matmul_q4_0_gpu: k must be a multiple of 32, got {k}");
}
if x.count < m * k {
crate::bail!("matmul_q4_0_gpu: x count {} < m*k {}", x.count, m * k);
}
let out = self.alloc_f32(m * nout)?;
let cols = (nout as u32).div_ceil(WG1D);
let mut m0 = 0usize;
while m0 < m {
let mcount = (m - m0).min(MATMUL_Q_MAX_M);
// woff = 0: a plain 2D weight starts at word 0 (Q4_0 MoE banks ride the per-slot matvec,
// not this GEMM, so no bank offset is ever needed here).
let push = push_u32(&[m0 as u32, mcount as u32, nout as u32, k as u32, 0u32]);
self.dispatch(
"mul_mat_q4_0",
&[wq.buffer, x.buffer, out.buffer],
&push,
(cols, 1, 1),
)?;
m0 += mcount;
}
Ok(out)
}
/// Q8_0 matrix-matrix (prefill): `y[m, nout] = x[m, k] * Wq^T`, weights stay quantized in VRAM
/// (same blocks as [`quantize_q8`] / [`matvec_q8_gpu`]). Decodes each weight block once and reuses
/// it across a tile of up to [`MATMUL_Q_MAX_M`] rows, so weight memory traffic matches a single
/// matvec per output column instead of dequantizing the whole weight to f32 every forward. `x`
/// must be a contiguous `[m, k]` device buffer; returns `[m, nout]` row-major.
pub fn matmul_q8_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
m: usize,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
self.matmul_q8_gpu_off(wq, x, m, nout, k, 0)
}
/// Q8_0 prefill matmul into a slice of `wq` starting at `woff` u32 words. `woff == 0` is
/// bit-identical to [`matmul_q8_gpu`]; a non-zero offset selects one expert's weight block of a
/// resident MoE bank (`woff = e * nout * (k/32) * 9`) so a prefill expert with M>1 routed rows
/// runs one banked matmul without re-uploading the weight.
pub fn matmul_q8_gpu_off(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
m: usize,
nout: usize,
k: usize,
woff: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(32) {
crate::bail!("matmul_q8_gpu: k must be a multiple of 32, got {k}");
}
if x.count < m * k {
crate::bail!("matmul_q8_gpu: x count {} < m*k {}", x.count, m * k);
}
let out = self.alloc_f32(m * nout)?;
let cols = (nout as u32).div_ceil(WG1D);
let mut m0 = 0usize;
while m0 < m {
let mcount = (m - m0).min(MATMUL_Q_MAX_M);
let push = push_u32(&[m0 as u32, mcount as u32, nout as u32, k as u32, woff as u32]);
self.dispatch(
"mul_mat_q8",
&[wq.buffer, x.buffer, out.buffer],
&push,
(cols, 1, 1),
)?;
m0 += mcount;
}
Ok(out)
}
/// Quantize GPU-resident f32 activations `x[m, k]` to q8_1-style int8 for the dp4a prefill GEMM:
/// returns (xq `[m*k/32*8]` u32, xs `[m*k/32]` f32 scale, xsum `[m*k/32]` f32 dequant block sum).
/// One O(m*k) pass amortized over the O(m*nout*k) matmul; layout matches `mul_mm_q4k_tiled_dp4a`.
/// q8-quantize an activation for the dp4a kernels: `(xq, xs, xsum)`. Public so a caller feeding the
/// SAME activation to several matvecs (gate and up share one routed token) can quantize once and
/// hand the result to [`Self::moe_matvec_blk_dp4a_pre_gpu`], rather than re-deriving it per matvec.
pub fn quantize_act_q8(
&self,
x: &VulkanStorage,
m: usize,
k: usize,
) -> Result<(VulkanStorage, VulkanStorage, VulkanStorage)> {
let kb = k / 32;
let xq = self.alloc_u32(m * kb * 8)?;
let xs = self.alloc_f32(m * kb)?;
let xsum = self.alloc_f32(m * kb)?;
let push = push_u32(&[m as u32, k as u32]);
self.dispatch_outs(
"quantize_act_q8",
&[x.buffer, xq.buffer, xs.buffer, xsum.buffer],
&[1, 2, 3],
&push,
(((m * kb) as u32).div_ceil(64), 1, 1),
)?;
Ok((xq, xs, xsum))
}
/// Kernel-isolated Q4_K prefill timing: upload `x` + reuse `wq` once, then loop the GPU GEMM
/// `iters` times with a single final `synchronize` (no per-iter host upload or readback). Returns
/// ms/call -- the realistic engine kernel cost (in a real forward `x` is already GPU-resident), vs
/// the host-wrapper bench whose per-iter 8 MB upload+readback swamps the kernel. Used by the perf gates.
pub fn bench_matmul_q4k(
&self,
wq: &VulkanStorage,
x: &[f32],
m: usize,
nout: usize,
k: usize,
iters: usize,
) -> Result<f64> {
let xs = self.upload_f32(x)?;
let _ = self.matmul_q4k_gpu(wq, &xs, m, nout, k)?;
self.synchronize()?;
let t = std::time::Instant::now();
for _ in 0..iters {
let _ = self.matmul_q4k_gpu(wq, &xs, m, nout, k)?;
}
self.synchronize()?;
Ok(t.elapsed().as_secs_f64() * 1e3 / iters as f64)
}
/// Host-operand Q4_K prefill matmul: uploads `x` (`[m, k]` row-major), runs the GPU GEMM, returns
/// `[m, nout]` row-major. Mirrors [`matvec_q4k`] for the M>1 path; used by the bit-exact A/B gate.
pub fn matmul_q4k(
&self,
wq: &VulkanStorage,
x: &[f32],
m: usize,
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
let xs = self.upload_f32(x)?;
self.matmul_q4k_gpu(wq, &xs, m, nout, k)?.to_vec_f32()
}
/// Q4_K matrix-matrix (prefill): `y[m, nout] = x[m, k] * Wq^T`, weights stay quantized in VRAM
/// (verbatim GGUF Q4_K super-blocks, same decode as [`matvec_q4k_gpu`]). Decodes each weight value
/// once and reuses it across a tile of up to [`MATMUL_Q_MAX_M`] rows, so weight memory traffic
/// matches a single matvec per output column instead of dequantizing the whole weight to f32 every
/// forward. `x` must be a contiguous `[m, k]` device buffer; returns `[m, nout]` row-major.
pub fn matmul_q4k_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
m: usize,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
self.matmul_q4k_gpu_off(wq, x, m, nout, k, 0)
}
/// Install an uncommitted `mul_mm_q4k_coopmat` variant (raw SPIR-V plus its BM/BN tile geometry) for
/// the evolutionary autotuner to bench through the real dispatch path before it is committed. Evicts
/// and destroys any previously installed variant's cached pipeline first -- the bench synchronizes
/// between variants so the device is idle and the destroy is safe. Tuner/test-only; nothing
/// dispatches this pipeline name in a production build.
#[cfg(test)]
pub(crate) fn install_coopmat_variant(&self, spv: &[u8], bm: u32, bn: u32) {
if let Some(old) = self.inner.pipelines.lock().unwrap().remove("__coopmat_variant") {
unsafe {
let dev = self.dev();
dev.destroy_pipeline(old.pipeline, None);
dev.destroy_pipeline_layout(old.layout, None);
dev.destroy_descriptor_set_layout(old.set_layout, None);
}
}
*self.inner.coopmat_variant.lock().unwrap() = Some((spv.to_vec(), bm, bn));
}
/// Run the installed coopmat variant as a Q4_K prefill matmul `y[m,nout] = x[m,k] * W[nout,k]`.
/// Mirrors the committed coopmat branch of [`matmul_q4k_gpu`] exactly but for the grid divisors --
/// BM/BN come from the installed variant, not the fixed 128 -- so a genome with a different tile is
/// dispatched correctly.
#[cfg(test)]
pub(crate) fn matmul_q4k_coopmat_variant(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
m: usize,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
let (bm, bn) = self
.inner
.coopmat_variant
.lock()
.unwrap()
.as_ref()
.map(|(_, bm, bn)| (*bm, *bn))
.ok_or_else(|| Error::Msg("vulkan: no coopmat variant installed".into()))?;
let out = self.alloc_f32(m * nout)?;
let push = push_u32(&[0, m as u32, nout as u32, k as u32, 0u32]);
self.dispatch(
"__coopmat_variant",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(bn), (m as u32).div_ceil(bm), 1),
)?;
Ok(out)
}
/// Q4_K prefill matmul into a slice of `wq` starting at `woff` u32 words. `woff == 0` is
/// bit-identical to [`matmul_q4k_gpu`]; a non-zero offset selects one expert's weight block of a
/// resident MoE bank (`woff = e * nout * (k/256) * 36`) so a prefill expert with M>1 routed rows
/// runs one banked matmul without re-uploading the weight.
pub fn matmul_q4k_gpu_off(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
m: usize,
nout: usize,
k: usize,
woff: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matmul_q4k_gpu: k must be a multiple of 256, got {k}");
}
if x.count < m * k {
crate::bail!("matmul_q4k_gpu: x count {} < m*k {}", x.count, m * k);
}
// Q4_K prefill path selection. DEFAULT for dense prefill (woff==0, m>1): the f16 coopmat
// matrix-core GEMM (mul_mm_q4k_coopmat), the llama-Vulkan-parity path. MoE banks (woff!=0),
// m==1, and VK_Q4K_LEGACY fall to the column-per-invocation mul_mat_q4k. The dp4a/2d tiles
// stay reachable via the per-kernel A/B env vars (VK_Q4K_{DP4A,TILED2D,COOPMAT_OFF,LEGACY}).
let legacy = std::env::var_os("VK_Q4K_LEGACY").is_some();
let force_dp4a = std::env::var_os("VK_Q4K_DP4A").is_some();
let force_2d = std::env::var_os("VK_Q4K_TILED2D").is_some();
// L4 coopmat (matrix cores) -- decode the Q4_K weight to f16 LDS tiles + coopMatMulAdd, the
// llama-parity path (llama-Vulkan uses KHR_coopmat for Q4_K prefill on this device). The 16x16
// store writes whole tiles, so the output rows are padded up to a 16-multiple (nout already is
// one for every projection): the trailing <16 scratch rows are fed by stage()'s zero-padded
// activation and never read back (`count` stays m*nout). This is what lets a ragged token count
// -- m % 16 != 0, i.e. nearly every real prompt -- take the coopmat path instead of falling
// through to the ~1.6x slower dp4a tile. llama's mul_mm.comp reaches the same result by
// bounds-checking each partial-tile element; padding the allocation is that store with no
// divergent path. VK_Q4K_COOPMAT_OFF (and the dp4a/2d/legacy A-B gates) force the older tiles.
let coopmat_path = !legacy
&& !force_dp4a
&& !force_2d
&& self.inner.coopmat
&& woff == 0
&& m > 1
&& nout % 16 == 0
&& std::env::var_os("VK_Q4K_COOPMAT_OFF").is_none();
let m_alloc = if coopmat_path { m.next_multiple_of(16) } else { m };
let mut out = self.alloc_f32(m_alloc * nout)?;
out.count = m * nout;
if coopmat_path {
let push = push_u32(&[0, m as u32, nout as u32, k as u32, woff as u32]);
// grid: BN=128 cols/wg, BM=128 rows/wg (must match the shader's BM/BN).
self.dispatch(
"mul_mm_q4k_coopmat",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(128), (m as u32).div_ceil(128), 1),
)?;
return Ok(out);
}
let dense_default = !legacy && woff == 0 && m > 1;
let use_dp4a =
!legacy && (force_dp4a || (dense_default && self.inner.int_dot8 && !force_2d));
let use_2d = !legacy && !use_dp4a && (force_2d || dense_default);
if use_dp4a {
let (xq, xs, xsum) = self.quantize_act_q8(x, m, k)?;
let push = push_u32(&[0, m as u32, nout as u32, k as u32, woff as u32]);
// BM (rows per workgroup) sets the cold-weight re-read factor m/BM: a 512-row prefill reads
// each weight column m/BM times from GTT. BM=64 default; VK_Q4K_BM=128/256 picks the taller
// tile that reads cold weights 2x/4x fewer times (register pressure vs bandwidth -- measure
// in-engine, the only sound regime for a cold-weight-bound GEMM). The grid.y divisor MUST
// match the shader's BM.
let (kernel, bm) = match std::env::var("VK_Q4K_BM").ok().as_deref() {
Some("128") => ("mul_mm_q4k_tiled_dp4a_bm128", 128u32),
Some("256") => ("mul_mm_q4k_tiled_dp4a_bm256", 256u32),
_ => ("mul_mm_q4k_tiled_dp4a", 64u32),
};
self.dispatch(
kernel,
&[wq.buffer, xq.buffer, xs.buffer, xsum.buffer, out.buffer],
&push,
((nout as u32).div_ceil(64), (m as u32).div_ceil(bm), 1),
)?;
return Ok(out);
}
if use_2d {
let push = push_u32(&[0, m as u32, nout as u32, k as u32, woff as u32]);
self.dispatch(
"mul_mm_q4k_tiled",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(64), (m as u32).div_ceil(64), 1),
)?;
return Ok(out);
}
let cols = (nout as u32).div_ceil(WG1D);
let mut m0 = 0usize;
while m0 < m {
let mcount = (m - m0).min(MATMUL_Q_MAX_M);
let push = push_u32(&[m0 as u32, mcount as u32, nout as u32, k as u32, woff as u32]);
self.dispatch(
"mul_mat_q4k",
&[wq.buffer, x.buffer, out.buffer],
&push,
(cols, 1, 1),
)?;
m0 += mcount;
}
Ok(out)
}
/// Q5_K matrix-matrix (prefill): `y[m, nout] = x[m, k] * Wq^T`, weights stay quantized in VRAM
/// (verbatim GGUF Q5_K super-blocks, same decode as [`matvec_q5k_gpu`]). Decodes each weight value
/// once and reuses it across a tile of up to [`MATMUL_Q_MAX_M`] rows, so weight memory traffic
/// matches a single matvec per output column instead of issuing M independent matvecs (the old
/// per-row prefill loop) or dequantizing the whole weight to f32. `x` is a contiguous `[m, k]`
/// device buffer; returns `[m, nout]` row-major.
pub fn matmul_q5k_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
m: usize,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
self.matmul_q5k_gpu_off(wq, x, m, nout, k, 0)
}
/// Q5_K prefill matmul into a slice of `wq` starting at `woff` u32 words. `woff == 0` is
/// bit-identical to [`matmul_q5k_gpu`]; a non-zero offset selects one expert's weight block of a
/// resident MoE bank (`woff = e * nout * (k/256) * 44`) so a prefill expert with M>1 routed rows
/// runs one banked matmul without re-uploading the weight.
pub fn matmul_q5k_gpu_off(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
m: usize,
nout: usize,
k: usize,
woff: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matmul_q5k_gpu: k must be a multiple of 256, got {k}");
}
if x.count < m * k {
crate::bail!("matmul_q5k_gpu: x count {} < m*k {}", x.count, m * k);
}
let out = self.alloc_f32(m * nout)?;
let cols = (nout as u32).div_ceil(WG1D);
let mut m0 = 0usize;
while m0 < m {
let mcount = (m - m0).min(MATMUL_Q_MAX_M);
let push = push_u32(&[m0 as u32, mcount as u32, nout as u32, k as u32, woff as u32]);
self.dispatch(
"mul_mat_q5k",
&[wq.buffer, x.buffer, out.buffer],
&push,
(cols, 1, 1),
)?;
m0 += mcount;
}
Ok(out)
}
/// Q6_K matrix-matrix (prefill): `y[m, nout] = x[m, k] * Wq^T`, weights stay quantized in VRAM
/// (padded 53-u32 Q6_K super-blocks from [`quantize_q6k`], same decode as [`matvec_q6k_gpu`]).
/// Decodes each weight value once and reuses it across a tile of up to [`MATMUL_Q_MAX_M`] rows, so
/// weight memory traffic matches a single matvec per output column instead of issuing M
/// independent matvecs (the old per-row prefill loop). Q6_K is the common Q4_K_M down-expert
/// dtype, so this is the prefill hot path for that quant. `x` is a contiguous `[m, k]` device
/// buffer; returns `[m, nout]` row-major.
pub fn matmul_q6k_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
m: usize,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
self.matmul_q6k_gpu_off(wq, x, m, nout, k, 0)
}
/// Q6_K prefill matmul into a slice of `wq` starting at `woff` u32 words. `woff == 0` is
/// bit-identical to [`matmul_q6k_gpu`]; a non-zero offset selects one expert's weight block of a
/// resident MoE bank (`woff = e * nout * (k/256) * 53`, 53 u32 = padded Q6_K block) so a prefill
/// expert with M>1 routed rows runs one banked matmul without re-uploading the weight.
pub fn matmul_q6k_gpu_off(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
m: usize,
nout: usize,
k: usize,
woff: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matmul_q6k_gpu: k must be a multiple of 256, got {k}");
}
if x.count < m * k {
crate::bail!("matmul_q6k_gpu: x count {} < m*k {}", x.count, m * k);
}
// Q6_K prefill path selection. DEFAULT for dense prefill (woff==0, m>1): the f16 coopmat
// matrix-core GEMM (mul_mm_q6k_coopmat), the llama-Vulkan-parity path. MoE banks (woff!=0),
// m==1, and VK_Q6K_LEGACY fall to the column-per-invocation mul_mat_q6k; the int8-dp4a tile
// stays reachable via VK_Q6K_COOPMAT_OFF for the A/B gate.
let legacy = std::env::var_os("VK_Q6K_LEGACY").is_some();
// f16 coopmat (matrix cores) -- decode the Q6_K weight to f16 LDS tiles + coopMatMulAdd, the
// llama-parity path (llama-Vulkan runs Q6_K prefill on KHR_coopmat). The 16x16 store writes
// whole tiles, so the output rows are padded up to a 16-multiple (nout already is one for every
// projection): the trailing <16 scratch rows are fed by stage()'s zero-padded activation and
// never read back (`count` stays m*nout). This lets a ragged token count (m % 16 != 0, nearly
// every real prompt) take the coopmat path instead of the ~1.6x slower dp4a tile -- the same
// result as llama's per-element partial-tile store, with no divergent path. VK_Q6K_COOPMAT_OFF
// forces the dp4a tile for the A/B gate.
let coopmat_path = !legacy
&& self.inner.coopmat
&& woff == 0
&& m > 1
&& nout % 16 == 0
&& std::env::var_os("VK_Q6K_COOPMAT_OFF").is_none();
let m_alloc = if coopmat_path { m.next_multiple_of(16) } else { m };
let mut out = self.alloc_f32(m_alloc * nout)?;
out.count = m * nout;
if coopmat_path {
let push = push_u32(&[0, m as u32, nout as u32, k as u32, woff as u32]);
// grid: BN=128 cols/wg, BM=128 rows/wg (must match the shader's BM/BN).
self.dispatch(
"mul_mm_q6k_coopmat",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(128), (m as u32).div_ceil(128), 1),
)?;
return Ok(out);
}
if !legacy && self.inner.int_dot8 && m > 1 {
let (xq, xsq, _xsum) = self.quantize_act_q8(x, m, k)?;
let push = push_u32(&[0, m as u32, nout as u32, k as u32, woff as u32]);
self.dispatch(
"mul_mm_q6k_tiled_dp4a",
&[wq.buffer, xq.buffer, xsq.buffer, out.buffer],
&push,
((nout as u32).div_ceil(64), (m as u32).div_ceil(64), 1),
)?;
return Ok(out);
}
let cols = (nout as u32).div_ceil(WG1D);
let mut m0 = 0usize;
while m0 < m {
let mcount = (m - m0).min(MATMUL_Q_MAX_M);
let push = push_u32(&[m0 as u32, mcount as u32, nout as u32, k as u32, woff as u32]);
self.dispatch(
"mul_mat_q6k",
&[wq.buffer, x.buffer, out.buffer],
&push,
(cols, 1, 1),
)?;
m0 += mcount;
}
Ok(out)
}
/// Gated delta-rule recurrence on the GPU (hybrid GDN / Qwen3.6 mixer), mirroring the CUDA
/// `gated_delta_rule_recurrence` math. All operands are f32 device buffers laid out exactly as
/// the CUDA kernel expects: `q,k` `[BH,S,K]`, `v` `[BH,S,V]`, `g,beta` `[BH,S]`, `state` `[BH,K,V]`
/// (updated in place). Returns the output `[BH,S,V]`. `seq_len==1` is the decode path (sequential
/// `gdn_recurrence`); a longer prefill uses the chunked kernel when `k_dim<=128`, else the
/// sequential one (its private state array bounds `k_dim<=256`, covering shipping GDN configs).
/// One workgroup per (V-tile, batch*head); V-tile width is 64 (matches the kernels' BV).
#[allow(clippy::too_many_arguments)]
pub fn gdn_recurrence_gpu(
&self,
q: &VulkanStorage,
k: &VulkanStorage,
v: &VulkanStorage,
g: &VulkanStorage,
beta: &VulkanStorage,
state: &VulkanStorage,
bh: usize,
seq_len: usize,
k_dim: usize,
v_dim: usize,
) -> Result<VulkanStorage> {
if k_dim > 256 {
crate::bail!("gdn_recurrence_gpu: k_dim {k_dim} exceeds the kernel bound of 256");
}
let out = self.alloc_f32(bh * seq_len * v_dim)?;
let push = push_u32(&[seq_len as u32, k_dim as u32, v_dim as u32]);
let bufs = [
q.buffer,
k.buffer,
v.buffer,
g.buffer,
beta.buffer,
state.buffer,
out.buffer,
];
let v_tiles = (v_dim as u32).div_ceil(WG1D);
// Chunked prefill kernel bounds its shared key array at K<=128; fall back to the sequential
// kernel (private array bound K<=256) for the rare wider config. Decode (seq_len==1) always
// uses the sequential kernel -- there are no chunks to amortize over a single token.
let kernel = if seq_len > 1 && k_dim <= 128 {
"gdn_chunked"
} else {
"gdn_recurrence"
};
// The kernel writes BOTH `state` (binding 5, in place) and `out` (binding 6). Mark both
// live so a later dispatch in this batch that reads either inserts the RAW barrier.
self.dispatch_multi_out(kernel, &bufs, &[5, 6], &push, (v_tiles, bh as u32, 1))?;
Ok(out)
}
/// Causal conv1d single-step update (GDN decode path), mirroring CUDA `causal_conv1d_update`.
/// `x` `[B,conv_dim,1]`, `weight` `[conv_dim,kernel_size]`, `conv_state` `[B,conv_dim,kernel_size]`
/// (shifted+updated in place). Returns the SiLU-activated output `[B,conv_dim,1]`. f32 on this
/// backend (f16/bf16 GDN tensors are stored as f32). `kernel_size<=8` (the kernel's window bound).
pub fn gdn_conv_update_gpu(
&self,
x: &VulkanStorage,
weight: &VulkanStorage,
conv_state: &VulkanStorage,
batch_size: usize,
conv_dim: usize,
kernel_size: usize,
) -> Result<VulkanStorage> {
if !(1..=8).contains(&kernel_size) {
crate::bail!("gdn_conv_update_gpu: kernel_size {kernel_size} out of range 1..=8");
}
let out = self.alloc_f32(batch_size * conv_dim)?;
let push = push_u32(&[batch_size as u32, conv_dim as u32, kernel_size as u32]);
let bufs = [x.buffer, weight.buffer, conv_state.buffer, out.buffer];
// Writes conv_state (binding 2, in place) and out (binding 3); track both for hazards.
self.dispatch_multi_out(
"gdn_conv_update",
&bufs,
&[2, 3],
&push,
((conv_dim as u32).div_ceil(WG1D), batch_size as u32, 1),
)?;
Ok(out)
}
/// Causal conv1d over a full sequence (GDN prefill path), mirroring CUDA `causal_conv1d_full`
/// plus `save_conv_state`. `x` `[B,conv_dim,S]`, `weight` `[conv_dim,kernel_size]`. Returns the
/// SiLU output `[B,conv_dim,S]` and the trailing window saved into a fresh conv_state
/// `[B,conv_dim,kernel_size]` (left zero-padded when `S<kernel_size`). f32 throughout.
pub fn gdn_conv_full_gpu(
&self,
x: &VulkanStorage,
weight: &VulkanStorage,
batch_size: usize,
conv_dim: usize,
seq_len: usize,
kernel_size: usize,
) -> Result<(VulkanStorage, VulkanStorage)> {
let out = self.alloc_f32(batch_size * conv_dim * seq_len)?;
let cs = self.alloc_f32(batch_size * conv_dim * kernel_size)?;
let push_full = push_u32(&[
batch_size as u32,
conv_dim as u32,
seq_len as u32,
kernel_size as u32,
]);
let total = (batch_size * conv_dim * seq_len) as u32;
self.dispatch(
"gdn_conv_full",
&[x.buffer, weight.buffer, out.buffer],
&push_full,
(total.div_ceil(WG1D), 1, 1),
)?;
// Independent of the conv output (reads x, writes a disjoint buffer), so no barrier needed.
self.dispatch(
"gdn_conv_state_save",
&[x.buffer, cs.buffer],
&push_full,
((conv_dim as u32).div_ceil(WG1D), batch_size as u32, 1),
)?;
Ok((out, cs))
}
/// Fused GDN gating, mirroring CUDA `fused_gdn_gating`: `beta=sigmoid(b)`,
/// `g=-exp(a_log)*softplus(a+dt_bias)`. `b,a` are `[total]`; `a_log,dt_bias` are per-head
/// `[num_heads]` (indexed by `idx % num_heads`). Returns `(beta, g)`, each `[total]`. f32.
pub fn gdn_gating_gpu(
&self,
b: &VulkanStorage,
a: &VulkanStorage,
a_log: &VulkanStorage,
dt_bias: &VulkanStorage,
total: usize,
num_heads: usize,
) -> Result<(VulkanStorage, VulkanStorage)> {
let beta = self.alloc_f32(total)?;
let g = self.alloc_f32(total)?;
let push = push_u32(&[total as u32, num_heads as u32]);
let bufs = [
b.buffer,
a.buffer,
a_log.buffer,
dt_bias.buffer,
beta.buffer,
g.buffer,
];
// Writes beta (binding 4) and g (binding 5); both are consumed downstream, track both.
self.dispatch_multi_out(
"gdn_gating",
&bufs,
&[4, 5],
&push,
((total as u32).div_ceil(WG1D), 1, 1),
)?;
Ok((beta, g))
}
/// PagedAttention decode kernel (v1, f32). For each (seq, head) computes
/// `softmax(scale * Q.K^T) . V` over the sequence's cached KV, gathered from the
/// non-contiguous paged blocks named by `block_tables`. One workgroup per
/// (head, seq). See `src/vulkan/shaders/paged_attn.comp` for the layout contract.
///
/// Buffers carry f32 (Vulkan upcasts f16/bf16 on upload). `block_tables` and
/// `context_lens` are u32 storages. Strides are in ELEMENTS (f32 slots), computed
/// host-side from the actual f32 cache layout (NOT the logical dtype). The output is
/// a fresh `[num_seqs, num_heads, head_size]` f32 storage.
#[allow(clippy::too_many_arguments)]
pub fn paged_attention_vk(&self, p: &PagedAttnArgs<'_>) -> Result<VulkanStorage> {
if p.head_size as u64 > 256 {
crate::bail!(
"vulkan paged_attn: head_size {} > 256 (shared mem bound)",
p.head_size
);
}
let out = self.alloc_f32(p.num_seqs * p.num_heads * p.head_size)?;
// push: 10 u32 then 1 f32 (scale). std430 scalar packing, tightly packed.
let mut push = push_u32(&[
p.num_kv_heads as u32,
p.num_heads as u32,
p.head_size as u32,
p.block_size as u32,
p.max_num_blocks_per_seq as u32,
p.q_stride as u32,
p.kv_block_stride as u32,
p.kv_head_stride as u32,
p.x as u32,
p.max_context_len as u32,
]);
push.extend_from_slice(&p.scale.to_ne_bytes());
let bufs = [
p.q.buffer,
p.key_cache.buffer,
p.value_cache.buffer,
p.block_tables.buffer,
p.context_lens.buffer,
out.buffer,
];
// grid: x = heads, y = seqs (matches gl_WorkGroupID.x/.y in the shader).
self.dispatch_out(
"paged_attn",
&bufs,
5,
&push,
(p.num_heads as u32, p.num_seqs as u32, 1),
)?;
Ok(out)
}
/// Write new per-token K/V into the paged cache at the `slot_mapping` positions
/// (f32). `slot_mapping` is a u32 storage; the host maps the engine's i64 slots
/// (>=0) to u32 and the -1 pad sentinel to 0xFFFFFFFF (skipped in-shader). Strides
/// are in f32 elements. Mutates `key_cache`/`value_cache` in place.
#[allow(clippy::too_many_arguments)]
pub fn reshape_and_cache_vk(&self, p: &ReshapeCacheArgs<'_>) -> Result<()> {
let push = push_u32(&[
p.key_stride as u32,
p.value_stride as u32,
p.num_heads as u32,
p.head_size as u32,
p.block_size as u32,
p.x as u32,
]);
let bufs = [
p.key.buffer,
p.value.buffer,
p.key_cache.buffer,
p.value_cache.buffer,
p.slot_mapping.buffer,
];
// One workgroup per token; kernel writes bindings 2 (key_cache) and 3 (value_cache).
self.dispatch_multi_out(
"reshape_and_cache",
&bufs,
&[2, 3],
&push,
(p.num_tokens as u32, 1, 1),
)?;
Ok(())
}
/// PagedAttention decode over an int8-packed KV cache (KIVI / KVQuant per-token). Same math as
/// [`Self::paged_attention_vk`] but K/V are dequantized on read (`code * per-token-scale`); the
/// caches are u32 buffers holding `head_size/4` code words + 1 scale word per token vector
/// (see `src/vulkan/shaders/paged_attn_q8.comp`). Strides are computed in-shader from
/// head_size/block_size/num_kv_heads, so no cache strides are passed. Output is fresh f32.
#[allow(clippy::too_many_arguments)]
pub fn paged_attention_q8_vk(&self, p: &PagedAttnArgs<'_>) -> Result<VulkanStorage> {
if p.head_size as u64 > 256 {
crate::bail!("vulkan paged_attn_q8: head_size {} > 256", p.head_size);
}
let out = self.alloc_f32(p.num_seqs * p.num_heads * p.head_size)?;
// push: 7 u32 then 1 f32 (scale), std430 scalar packing (matches paged_attn_q8.comp Pc).
let mut push = push_u32(&[
p.num_kv_heads as u32,
p.num_heads as u32,
p.head_size as u32,
p.block_size as u32,
p.max_num_blocks_per_seq as u32,
p.q_stride as u32,
p.max_context_len as u32,
]);
push.extend_from_slice(&p.scale.to_ne_bytes());
let bufs = [
p.q.buffer,
p.key_cache.buffer,
p.value_cache.buffer,
p.block_tables.buffer,
p.context_lens.buffer,
out.buffer,
];
self.dispatch_out(
"paged_attn_q8",
&bufs,
5,
&push,
(p.num_heads as u32, p.num_seqs as u32, 1),
)?;
Ok(out)
}
/// Write new per-token K/V into the int8-packed paged cache: symmetric per-token quantization
/// (amax/127, clamp to +-127), codes packed 4-per-u32 with a co-located f32 scale word. One
/// workgroup per (token, kv-head). See `src/vulkan/shaders/reshape_and_cache_q8.comp`.
#[allow(clippy::too_many_arguments)]
pub fn reshape_and_cache_q8_vk(&self, p: &ReshapeCacheArgs<'_>) -> Result<()> {
// push: key_stride, value_stride, num_kv_heads, head_size, block_size (matches Pc).
let push = push_u32(&[
p.key_stride as u32,
p.value_stride as u32,
p.num_heads as u32, // the write path carries kv heads in `num_heads`
p.head_size as u32,
p.block_size as u32,
]);
let bufs = [
p.key.buffer,
p.value.buffer,
p.key_cache.buffer,
p.value_cache.buffer,
p.slot_mapping.buffer,
];
// grid: (token, kv-head); kernel writes bindings 2 (key_cache) and 3 (value_cache).
self.dispatch_multi_out(
"reshape_and_cache_q8",
&bufs,
&[2, 3],
&push,
(p.num_tokens as u32, p.num_heads as u32, 1),
)?;
Ok(())
}
/// Like [`Self::dispatch_out`] but the kernel writes several output bindings (named by
/// `out_idxs`); each is marked live in the in-batch hazard set so a later dispatch reading any of
/// them gets the RAW barrier. Used by the GDN kernels (recurrence updates state AND writes output;
/// gating writes beta AND g; conv-update updates state AND writes output). Shares `dispatch_out`'s
/// recording path by issuing the dispatch with the first output index, then registering the rest.
fn dispatch_multi_out(
&self,
name: &'static str,
bufs: &[vk::Buffer],
out_idxs: &[usize],
push: &[u8],
groups: (u32, u32, u32),
) -> Result<()> {
let first = out_idxs
.first()
.copied()
.unwrap_or(bufs.len().saturating_sub(1));
self.dispatch_out(name, bufs, first, push, groups)?;
if out_idxs.len() > 1 {
let mut s = self.inner.submitter.lock().unwrap();
for &i in &out_idxs[1..] {
if let Some(&buf) = bufs.get(i) {
s.written_since_barrier.insert(buf);
}
}
}
Ok(())
}
/// Upload Q5_K weights to the GPU once, verbatim in the GGUF super-block layout (176 B = 44 u32
/// per 256-weight block, `nout * k/256` blocks total). No requantize: the bytes are the same
/// blocks the CPU `k_quants::BlockQ5K` holds (d, dmin, 12 scale bytes, 32 qh bytes, 128 qs
/// bytes), so the in-shader decode matches the CPU dequant exactly. `data` must be exactly the
/// `nout * (k/256) * 176` packed block bytes; `k` must be a multiple of 256.
pub fn quantize_q5k(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_q5k: k must be a multiple of 256, got {k}");
}
let nblocks = k / 256;
let want = nout * nblocks * 176;
if data.len() != want {
crate::bail!(
"quantize_q5k: data len {} != {nout}*{nblocks}*176 = {want}",
data.len()
);
}
// Q5_K blocks are 176 bytes (a multiple of 4), so the buffer is u32-aligned by construction
// and can be reinterpreted verbatim for the kernel's `uint w[]` binding.
let words: &[u32] =
unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u32, data.len() / 4) };
self.upload_u32(words)
}
/// Q5_K matrix-vector: `y[nout] = Wq * x[k]` where `Wq` came from [`quantize_q5k`]. Reads weights
/// at ~5.5 bits/elem instead of 32 -- the bandwidth lever for memory-bound decode on this APU.
/// Decode matches the CPU `BlockQ5K::to_float`.
pub fn matvec_q5k(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_q5k: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_q5k_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Q5_K matvec with both operands already on the GPU: `y[nout] = Wq * x[k]`, no host round-trip.
/// Weights stay quantized in VRAM (~5.5 bits/elem) instead of dequantizing to f32, so decode
/// reads ~6x less weight memory. One invocation per output row (scalar kernel).
pub fn matvec_q5k_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
self.matvec_q5k_gpu_off(wq, x, nout, k, 0)
}
/// Q5_K matvec into a slice of `wq` starting at `woff` u32 words: `y[nout] = Wq[woff..] * x[k]`.
/// `woff == 0` is bit-identical to [`matvec_q5k_gpu`]; a non-zero offset selects one expert's row
/// block of a resident MoE bank (`woff = e * nout * (k/256) * 44`).
pub fn matvec_q5k_gpu_off(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
woff: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_q5k_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_q5k_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32, woff as u32]);
if self.inner.subgroup_matvec {
// Subgroup-reduced kernel (one subgroup per output row, fused subgroupAdd); bit-identical
// decode to the scalar mul_mat_vec_q5k. See matvec_q4k_gpu_off for the dispatch geometry.
let rows_per_wg = (WG1D / self.inner.subgroup_size).max(1);
self.dispatch(
"mul_mat_vec_q5k_sg",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(rows_per_wg), 1, 1),
)?;
} else {
self.dispatch(
"mul_mat_vec_q5k",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
}
Ok(out)
}
/// Upload Q6_K weights to the GPU once. The CPU `k_quants::BlockQ6K` block is 210 bytes
/// (ql[128], qh[64], scales[16] i8, d f16) which is NOT u32-aligned, so each block is repacked
/// into a PADDED 212-byte (53 u32) stride here (trailing 2 bytes unused); the shader uses the
/// same 53 u32 stride and byte-addressed reads, so the in-shader decode matches the CPU dequant
/// exactly. `data` must be exactly the `nout * (k/256) * 210` packed block bytes; `k` must be a
/// multiple of 256.
pub fn quantize_q6k(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_q6k: k must be a multiple of 256, got {k}");
}
let nblocks = k / 256;
let total = nout * nblocks;
let want = total * 210;
if data.len() != want {
crate::bail!(
"quantize_q6k: data len {} != {nout}*{nblocks}*210 = {want}",
data.len()
);
}
// Repack 210-byte source blocks into 53-u32 (212-byte) padded blocks so the device buffer is
// u32-aligned and every block starts on a u32 boundary. Trailing 2 bytes of each block are
// zero pad and never read by the shader.
let mut words = vec![0u32; total * 53];
for blk in 0..total {
let src = &data[blk * 210..blk * 210 + 210];
let dst = &mut words[blk * 53..blk * 53 + 53];
// Copy the 210 bytes into the low 210 bytes of the 212-byte (53 u32) padded block.
let dst_bytes: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u8, 53 * 4) };
dst_bytes[..210].copy_from_slice(src);
}
self.upload_u32(&words)
}
/// De-interleave a packed Q4_K expert bank into the PLANAR layout the DSL block kernel
/// (`hanzo_kernel::quant::moe_matvec_q4k_blk`) binds: four device arrays instead of one packed
/// blob, so `nt` threads read coalesced runs of a single field. Each 144-byte GGUF `block_q4_K`
/// (d f16, dmin f16, 12 scale bytes, 128 qs bytes) splits to: `wqs` (32 u32 = the 128 qs bytes),
/// `wsc` (3 u32 = the 12 scale bytes), `wd`/`wdm` (f16->f32 of d/dmin). Block order is preserved
/// (`(expert*n+r)*nb + b`), so the kernel's `blk = wrow*nb + sup` indexes identically. One-time,
/// at model load; the resident bank is reused every token via `cache_or_upload`.
pub fn quantize_q4k_split(&self, data: &[u8], rows: usize, k: usize) -> Result<MoeBankSplit> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_q4k_split: k must be a multiple of 256, got {k}");
}
let nb = k / 256;
let nblk = rows * nb;
let want = nblk * 144;
if data.len() != want {
crate::bail!("quantize_q4k_split: data len {} != {rows}*{nb}*144 = {want}", data.len());
}
let mut qs = vec![0u32; nblk * 32];
let mut sc = vec![0u32; nblk * 3];
let mut d = vec![0f32; nblk];
let mut dm = vec![0f32; nblk];
let rd16 = |b: &[u8]| half::f16::from_bits(u16::from_le_bytes([b[0], b[1]])).to_f32();
for blk in 0..nblk {
let src = &data[blk * 144..blk * 144 + 144];
d[blk] = rd16(&src[0..2]);
dm[blk] = rd16(&src[2..4]);
let scb: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(sc[blk * 3..blk * 3 + 3].as_mut_ptr() as *mut u8, 12) };
scb.copy_from_slice(&src[4..16]);
let qsb: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(qs[blk * 32..blk * 32 + 32].as_mut_ptr() as *mut u8, 128) };
qsb.copy_from_slice(&src[16..144]);
}
Ok(MoeBankSplit(vec![
self.upload_u32(&qs)?,
self.upload_u32(&sc)?,
self.upload_f32(&d)?,
self.upload_f32(&dm)?,
]))
}
/// De-interleave a packed Q6_K expert bank into the planar layout the DSL block kernel
/// (`hanzo_kernel::quant::moe_matvec_q6k_blk`) binds. Each 210-byte GGUF `block_q6_K`
/// (ql[128], qh[64], scales[16] i8, d f16) splits to: `wql` (32 u32 = ql), `wqh` (16 u32 = qh),
/// `wsc` (4 u32 = the 16 signed scale bytes, read sign-extended in-shader), `wd` (f16->f32 d).
pub fn quantize_q6k_split(&self, data: &[u8], rows: usize, k: usize) -> Result<MoeBankSplit> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_q6k_split: k must be a multiple of 256, got {k}");
}
let nb = k / 256;
let nblk = rows * nb;
let want = nblk * 210;
if data.len() != want {
crate::bail!("quantize_q6k_split: data len {} != {rows}*{nb}*210 = {want}", data.len());
}
let mut ql = vec![0u32; nblk * 32];
let mut qh = vec![0u32; nblk * 16];
let mut sc = vec![0u32; nblk * 4];
let mut d = vec![0f32; nblk];
for blk in 0..nblk {
let src = &data[blk * 210..blk * 210 + 210];
let qlb: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(ql[blk * 32..blk * 32 + 32].as_mut_ptr() as *mut u8, 128) };
qlb.copy_from_slice(&src[0..128]);
let qhb: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(qh[blk * 16..blk * 16 + 16].as_mut_ptr() as *mut u8, 64) };
qhb.copy_from_slice(&src[128..192]);
let scb: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(sc[blk * 4..blk * 4 + 4].as_mut_ptr() as *mut u8, 16) };
scb.copy_from_slice(&src[192..208]);
d[blk] = half::f16::from_bits(u16::from_le_bytes([src[208], src[209]])).to_f32();
}
Ok(MoeBankSplit(vec![
self.upload_u32(&ql)?,
self.upload_u32(&qh)?,
self.upload_u32(&sc)?,
self.upload_f32(&d)?,
]))
}
/// Dispatch a DSL block-reduced MoE matvec (`moe_matvec_q{4,6}k_blk_{gu,dn}`) over a planar
/// [`MoeBankSplit`]. ONE workgroup per output element `out[slot*n + r]` (the kernel's `CUBE_POS`),
/// `nt` threads baked into the .spv; comptime dims mean NO push constants. Bindings are the bank's
/// planar arrays followed by `x` (`[nrows, k]`), `ids` (`[nrows]`), `out` (`[nrows, n]`) -- exactly
/// the DSL kernel's parameter order, which cubecl lowered to bindings 0..N.
pub fn moe_matvec_blk_gpu(
&self,
kernel: &'static str,
bank: &MoeBankSplit,
x: &VulkanStorage,
ids: &VulkanStorage,
nrows: usize,
n: usize,
k: usize,
) -> Result<VulkanStorage> {
if x.count < nrows * k {
crate::bail!("moe_matvec_blk_gpu: x count {} < nrows*k {}", x.count, nrows * k);
}
if ids.count < nrows {
crate::bail!("moe_matvec_blk_gpu: ids count {} < nrows {nrows}", ids.count);
}
let out = self.alloc_f32(nrows * n)?;
let mut bufs: Vec<vk::Buffer> = bank.0.iter().map(|s| s.buffer).collect();
bufs.push(x.buffer);
bufs.push(ids.buffer);
bufs.push(out.buffer);
// One workgroup per output element (workgroup size nt is baked into the .spv). Grid is 2D
// (n, nrows): cubecl flattens CUBE_POS = x + y*NumWorkGroups.x = r + slot*n = outrow, which the
// kernel maps back to (slot, r). This keeps each grid dimension well under the Vulkan
// maxComputeWorkGroupCount ceiling at prefill (nrows = tokens*topk), where a flat (nrows*n,1,1)
// would overflow dim 0 and silently drop outputs.
self.dispatch(kernel, &bufs, &[], (n as u32, nrows as u32, 1))?;
Ok(out)
}
/// Whether the device advertises the integer dot-product extension (gates the dp4a MoE path).
pub fn has_int_dot8(&self) -> bool {
self.inner.int_dot8
}
/// dp4a twin of `moe_matvec_blk_gpu`: quantize the activation to q8 ONCE (reused across all n
/// outputs), then dispatch the int8-dot MoE kernel. Same split bank + 2D (n, nrows) grid. Bindings
/// follow the kernel's parameter order: bank arrays, then xq,xs[,xsum],ids,out — `with_xsum` says
/// whether the kernel folds its bias against the per-32 activation sums (Q4_K's dmin fold) or
/// derives its own half-block sums in-register (Q6_K's −32 fold), i.e. whether xsum is a binding.
#[allow(clippy::too_many_arguments)]
pub fn moe_matvec_blk_dp4a_gpu(
&self,
kernel: &'static str,
with_xsum: bool,
bank: &MoeBankSplit,
x: &VulkanStorage,
ids: &VulkanStorage,
nrows: usize,
n: usize,
k: usize,
) -> Result<VulkanStorage> {
if x.count < nrows * k {
crate::bail!("moe_matvec_blk_dp4a_gpu: x count {} < nrows*k {}", x.count, nrows * k);
}
if ids.count < nrows {
crate::bail!("moe_matvec_blk_dp4a_gpu: ids count {} < nrows {nrows}", ids.count);
}
let (xq, xs, xsum) = self.quantize_act_q8(x, nrows, k)?;
self.moe_matvec_blk_dp4a_pre_gpu(kernel, with_xsum, bank, &xq, &xs, &xsum, ids, nrows, n)
}
/// [`Self::moe_matvec_blk_dp4a_gpu`] against an ALREADY q8-quantized activation. Gate and up read
/// the same routed token, so the caller quantizes once via [`Self::quantize_act_q8`] and dispatches
/// twice against it; `moe_matvec_blk_dp4a_gpu` is this with the quantize folded back in, so the
/// dispatch itself is written once.
#[allow(clippy::too_many_arguments)]
pub fn moe_matvec_blk_dp4a_pre_gpu(
&self,
kernel: &'static str,
with_xsum: bool,
bank: &MoeBankSplit,
xq: &VulkanStorage,
xs: &VulkanStorage,
xsum: &VulkanStorage,
ids: &VulkanStorage,
nrows: usize,
n: usize,
) -> Result<VulkanStorage> {
// No `k`: the .spv bakes the shape, so the dispatch carries no push constants.
let out = self.alloc_f32(nrows * n)?;
let mut bufs: Vec<vk::Buffer> = bank.0.iter().map(|s| s.buffer).collect();
bufs.push(xq.buffer);
bufs.push(xs.buffer);
if with_xsum {
bufs.push(xsum.buffer);
}
bufs.push(ids.buffer);
bufs.push(out.buffer);
self.dispatch(kernel, &bufs, &[], (n as u32, nrows as u32, 1))?;
Ok(out)
}
/// Affine Q4_K PREFILL GEMM on the coopmat path: `out[m,n] = sum_k W[n,k]*x[m,k]` with W the packed
/// Q4_K split bank (wqs/wsc/wd/wdm, decoded in-kernel) and the activation q8-quantized to
/// (xq, xs, xsum). The `mmq_q4k` .spv bakes n/k/plane, so the caller must match the committed shape
/// (n=2048, k=2048); M rides grid.y and is free. Bindings in kernel arg order: xq,xs,xsum + the four
/// bank arrays + out. This is the tensor-core prefill twin of the dp4a decode matvec.
#[allow(clippy::too_many_arguments)]
pub fn mmq_q4k_gpu(
&self,
xq: &VulkanStorage,
xs: &VulkanStorage,
xsum: &VulkanStorage,
bank: &MoeBankSplit,
m: usize,
n: usize,
) -> Result<VulkanStorage> {
let out = self.alloc_f32(m * n)?;
let mut bufs = vec![xq.buffer, xs.buffer, xsum.buffer];
for s in &bank.0 {
bufs.push(s.buffer); // wqs, wsc, wd, wdm
}
bufs.push(out.buffer);
self.dispatch("mmq_q4k", &bufs, &[], ((n / 64) as u32, (m / 32) as u32, 1))?;
Ok(out)
}
/// Runtime-dims affine Q4_K prefill MMQ: same tensor-core coopmat GEMM as [`Self::mmq_q4k_gpu`],
/// but m/n/k ride a meta SSBO (binding 8) instead of being baked into the .spv -- ONE `mmq_q4k_rt`
/// artifact serves EVERY prefill shape, and the grid rounds up so partial tiles are covered and
/// clipped by the kernel's tail guards. This is the seam that replaces `mul_mm_q4k_tiled_dp4a` for
/// all rows>1 Q4_K matmuls. k must be a Q4_K super-block multiple (256); m and n are free.
#[allow(clippy::too_many_arguments)]
pub fn mmq_q4k_rt_gpu(
&self,
xq: &VulkanStorage,
xs: &VulkanStorage,
xsum: &VulkanStorage,
bank: &MoeBankSplit,
m: usize,
n: usize,
k: usize,
) -> Result<VulkanStorage> {
let out = self.alloc_f32(m * n)?;
let meta = self.upload_u32(&[m as u32, n as u32, k as u32])?;
let mut bufs = vec![xq.buffer, xs.buffer, xsum.buffer];
for s in &bank.0 {
bufs.push(s.buffer); // wqs, wsc, wd, wdm
}
bufs.push(out.buffer);
bufs.push(meta.buffer);
self.dispatch("mmq_q4k_rt", &bufs, &[], (n.div_ceil(64) as u32, m.div_ceil(32) as u32, 1))?;
Ok(out)
}
/// Fused MoE top-k router: `logits[ntok, n_experts]` -> (ids[ntok, topk] u32, weights[ntok, topk]
/// f32), softmax + top-k + renormalize in ONE kernel (the DSL `moe_route` .spv, one workgroup per
/// token). Replaces the generic softmax_last_dim + sort_last_dim + narrow + gather + norm op-chain
/// (~11 dispatches/layer, each with a layout copy). The .spv bakes n_experts/topk/nt, so the caller
/// must match the committed shape (E=128, top-8, nt=128).
pub fn moe_route_vk(
&self,
logits: &VulkanStorage,
ntok: usize,
n_experts: usize,
topk: usize,
) -> Result<(VulkanStorage, VulkanStorage)> {
if logits.count < ntok * n_experts {
crate::bail!("moe_route_vk: logits count {} < ntok*n_experts {}", logits.count, ntok * n_experts);
}
let ids = self.alloc_u32(ntok * topk)?;
let w = self.alloc_f32(ntok * topk)?;
// Bindings match the kernel param order: logits(0), ids_out(1), w_out(2). No push (comptime
// dims). One workgroup per token; two outputs (ids, weights) for the RAW-hazard tracking.
let bufs = [logits.buffer, ids.buffer, w.buffer];
self.dispatch_outs("moe_route", &bufs, &[1, 2], &[], (ntok as u32, 1, 1))?;
Ok((ids, w))
}
/// Fused GQA flash SDPA (the DSL `sdpa_blk` .spv): `softmax(QKᵀ·scale + causal_mask)V` in ONE
/// dispatch, one workgroup per (batch,head,query). Collapses the decode-attention chain
/// repeat_kv(copy2d) → bmm(QKᵀ) → softmax → bmm(·V): the kernel reads the shared KV head directly
/// (GQA-native, no repeat_kv) and streams keys with a per-thread online softmax combined across the
/// workgroup. `q` is `[b·n_heads·seq_q·d]`, `k`/`v` are `[b·n_kv·seq_k·d]`, all contiguous f32.
/// `causal=false` for decode (the single query attends the whole cache). d=128, nt=64 baked in .spv.
#[allow(clippy::too_many_arguments)]
pub fn sdpa_blk_vk(
&self,
q: &VulkanStorage,
k: &VulkanStorage,
v: &VulkanStorage,
b: usize,
n_heads: usize,
n_kv: usize,
seq_q: usize,
seq_k: usize,
d: usize,
softmax_scale: f32,
causal: bool,
// KV strides in elements (from the k/v Layout): lets the kernel read a max_seq-sized cache
// sliced to seq_k IN PLACE, with no `.contiguous()` copy of the active cache each layer.
kv_batch_stride: usize,
kv_head_stride: usize,
key_stride: usize,
) -> Result<VulkanStorage> {
if q.count < b * n_heads * seq_q * d {
crate::bail!("sdpa_blk_vk: q count {} < b*n_heads*seq_q*d {}", q.count, b * n_heads * seq_q * d);
}
// k/v may be a strided view into a larger cache; bound-check against the furthest element read
// (last batch, last kv head, last key) rather than a packed size.
let kv_max = (b.saturating_sub(1)) * kv_batch_stride
+ (n_kv.saturating_sub(1)) * kv_head_stride
+ (seq_k.saturating_sub(1)) * key_stride
+ d;
if k.count < kv_max || v.count < kv_max {
crate::bail!("sdpa_blk_vk: k/v count < strided extent {kv_max}");
}
let out = self.alloc_f32(b * n_heads * seq_q * d)?;
// Small runtime-scalar SSBOs (cubecl has no push constants): scale(4) + meta(5). They drop at
// method end but park in the BufPool `pending` list; reclaim is post-fence-only, so they stay
// live for the whole deferred batch that runs this dispatch.
let scale = self.upload_f32(&[softmax_scale])?;
let meta = self.upload_u32(&[
seq_q as u32, seq_k as u32, n_heads as u32, n_kv as u32, causal as u32,
kv_batch_stride as u32, kv_head_stride as u32, key_stride as u32,
])?;
// Bindings match kernel param order: q(0) k(1) v(2) out(3) scale(4) meta(5). One workgroup per
// (batch,head,query); nt=64 (LocalSize) baked into the .spv. Only `out` (binding 3) is written.
let bufs = [q.buffer, k.buffer, v.buffer, out.buffer, scale.buffer, meta.buffer];
self.dispatch_outs("sdpa_blk", &bufs, &[3], &[], ((b * n_heads * seq_q) as u32, 1, 1))?;
Ok(out)
}
/// Flash-decoding SDPA (seq_q==1): the occupancy fix for [`Self::sdpa_blk_vk`]. That kernel launches
/// one workgroup per (batch,head) -- only `b*n_heads` (=16 for a 16-head model) workgroups for the
/// lone decode query, at 256 VGPR (min occupancy), so most CUs idle and the KV-read latency is
/// unhidden. This splits the KV sequence across `n_split` workgroups per (batch,head) (grid
/// `(rows, n_split)`), each a register-light wave64 subgroup, then a reduce dispatch combines the
/// per-split flash partials. Same GQA-native strided KV read as `sdpa_blk_vk`; `causal` is unused
/// (a decode query attends the whole cache). Bit-exact with `sdpa_blk_vk` up to reduction-order f32.
#[allow(clippy::too_many_arguments)]
pub fn sdpa_decode_split_vk(
&self,
q: &VulkanStorage,
k: &VulkanStorage,
v: &VulkanStorage,
b: usize,
n_heads: usize,
n_kv: usize,
seq_q: usize,
seq_k: usize,
d: usize,
softmax_scale: f32,
n_split: usize,
kv_batch_stride: usize,
kv_head_stride: usize,
key_stride: usize,
) -> Result<VulkanStorage> {
let rows = b * n_heads * seq_q;
if q.count < rows * d {
crate::bail!("sdpa_decode_split_vk: q count {} < rows*d {}", q.count, rows * d);
}
let kv_max = (b.saturating_sub(1)) * kv_batch_stride
+ (n_kv.saturating_sub(1)) * kv_head_stride
+ (seq_k.saturating_sub(1)) * key_stride
+ d;
if k.count < kv_max || v.count < kv_max {
crate::bail!("sdpa_decode_split_vk: k/v count < strided extent {kv_max}");
}
let nsplit = n_split.max(1);
let out = self.alloc_f32(rows * d)?;
// Per-split partials (un-normalized acc, running max, running denom). Transient: they park in the
// BufPool `pending` list and reclaim post-fence, so they stay live for the whole deferred batch.
let pacc = self.alloc_f32(rows * nsplit * d)?;
let pm = self.alloc_f32(rows * nsplit)?;
let pl = self.alloc_f32(rows * nsplit)?;
// seq_k rides an SSBO (VkGraphAttn `meta` layout [seq_q, seq_k, ...]); the shader reads meta[1].
let meta = self.upload_u32(&[seq_q as u32, seq_k as u32])?;
// Phase 1 push matches sdpa_decode_split.comp: H,Hkv,D,scale,n_split,kv strides (seq_k is in meta).
let mut p1 = Vec::with_capacity(32);
for x in [n_heads as u32, n_kv as u32, d as u32] {
p1.extend_from_slice(&x.to_le_bytes());
}
p1.extend_from_slice(&softmax_scale.to_le_bytes());
for x in [
nsplit as u32,
kv_batch_stride as u32,
kv_head_stride as u32,
key_stride as u32,
] {
p1.extend_from_slice(&x.to_le_bytes());
}
let bufs1 = [q.buffer, k.buffer, v.buffer, pacc.buffer, pm.buffer, pl.buffer, meta.buffer];
self.dispatch_outs(
"sdpa_decode_split",
&bufs1,
&[3, 4, 5],
&p1,
(rows as u32, nsplit as u32, 1),
)?;
// Phase 2 push: D, n_split.
let mut p2 = Vec::with_capacity(8);
p2.extend_from_slice(&(d as u32).to_le_bytes());
p2.extend_from_slice(&(nsplit as u32).to_le_bytes());
let bufs2 = [pacc.buffer, pm.buffer, pl.buffer, out.buffer];
self.dispatch_outs("sdpa_decode_reduce", &bufs2, &[3], &p2, (rows as u32, 1, 1))?;
Ok(out)
}
/// Fused DSL flash attention on the cooperative-matrix path (the `flash_attn_dsl` .spv): the same
/// `softmax(QKᵀ·scale + causal_mask)V` as [`Self::sdpa_blk_vk`], but both matmuls run as f16
/// 16x16x16 coopmat instead of scalar per-thread MACs. One workgroup (cube) computes one
/// `(batch, head, query-tile)` of BR=16 query rows over BC=16-key tiles with an online softmax; the
/// whole grid is launched at once (`cube_base=0`). GQA-native (reads the shared KV head, no
/// repeat_kv), causal-optional, runtime `seq_q`/`seq_k` via `meta`, KV read in place at its real
/// strides. `d`=128, `plane`(LocalSize)=64, BR=BC=16 are baked into the .spv. `q`/`out` are
/// `[b·n_heads·seq_q·d]`, `k`/`v` are `[b·n_kv··d]` (strided). Bindings match kernel param order:
/// q(0) k(1) v(2) out(3) scale(4) meta(5); only `out` (binding 3) is written.
#[allow(clippy::too_many_arguments)]
pub fn flash_attn_dsl_vk(
&self,
q: &VulkanStorage,
k: &VulkanStorage,
v: &VulkanStorage,
b: usize,
n_heads: usize,
n_kv: usize,
seq_q: usize,
seq_k: usize,
d: usize,
softmax_scale: f32,
causal: bool,
kv_batch_stride: usize,
kv_head_stride: usize,
key_stride: usize,
) -> Result<VulkanStorage> {
// The committed .spv bakes d=128 (the coopmat contracts d in d/16 chunks). Other head dims
// need their own dumped .spv; guard rather than silently read past the 128-wide staged tiles.
if d != 128 {
crate::bail!("flash_attn_dsl_vk: committed .spv is baked d=128, got {d}");
}
if q.count < b * n_heads * seq_q * d {
crate::bail!("flash_attn_dsl_vk: q count {} < b*n_heads*seq_q*d {}", q.count, b * n_heads * seq_q * d);
}
// k/v may be a strided view into a larger cache; bound-check the furthest element read.
let kv_max = (b.saturating_sub(1)) * kv_batch_stride
+ (n_kv.saturating_sub(1)) * kv_head_stride
+ (seq_k.saturating_sub(1)) * key_stride
+ d;
if k.count < kv_max || v.count < kv_max {
crate::bail!("flash_attn_dsl_vk: k/v count < strided extent {kv_max}");
}
let out = self.alloc_f32(b * n_heads * seq_q * d)?;
let scale = self.upload_f32(&[softmax_scale])?;
let meta = self.upload_u32(&[
seq_q as u32, seq_k as u32, n_heads as u32, n_kv as u32, causal as u32,
kv_batch_stride as u32, kv_head_stride as u32, key_stride as u32,
0u32, // cube_base = 0: dispatch the whole grid (production launch)
])?;
let bufs = [q.buffer, k.buffer, v.buffer, out.buffer, scale.buffer, meta.buffer];
// Grid = one cube per (batch, head, query-tile); BR=16 query rows per tile. plane=64 (LocalSize).
let cubes = (b * n_heads * seq_q.div_ceil(16)) as u32;
self.dispatch_outs("flash_attn_dsl", &bufs, &[3], &[], (cubes, 1, 1))?;
Ok(out)
}
/// Command-graph variant of [`Self::sdpa_decode_split_vk`]: the flash-decoding kernels with a
/// CALLER-OWNED stable `out` and `meta` (the shared [`VkGraphAttn`] buffers, `meta[1]`=seq_k advanced
/// per replay), matching how [`Self::sdpa_blk_vk_graph`] runs under capture. The per-split partials
/// are allocated fresh here; during capture the BufPool reserves every intermediate the forward
/// touches, so their handles stay stable for the graph's life (same mechanism as the per-layer `out`).
/// Writes `out` in place; `seq_q`==1. Bit-exact with `sdpa_decode_split_vk` (same .spv).
#[allow(clippy::too_many_arguments)]
pub fn sdpa_decode_split_vk_graph(
&self,
q: &VulkanStorage,
k: &VulkanStorage,
v: &VulkanStorage,
out: &VulkanStorage,
meta: &VulkanStorage,
b: usize,
n_heads: usize,
n_kv: usize,
d: usize,
softmax_scale: f32,
n_split: usize,
kv_batch_stride: usize,
kv_head_stride: usize,
key_stride: usize,
) -> Result<()> {
let rows = b * n_heads; // decode: seq_q == 1
let nsplit = n_split.max(1);
let pacc = self.alloc_f32(rows * nsplit * d)?;
let pm = self.alloc_f32(rows * nsplit)?;
let pl = self.alloc_f32(rows * nsplit)?;
let mut p1 = Vec::with_capacity(32);
for x in [n_heads as u32, n_kv as u32, d as u32] {
p1.extend_from_slice(&x.to_le_bytes());
}
p1.extend_from_slice(&softmax_scale.to_le_bytes());
for x in [
nsplit as u32,
kv_batch_stride as u32,
kv_head_stride as u32,
key_stride as u32,
] {
p1.extend_from_slice(&x.to_le_bytes());
}
let bufs1 = [q.buffer, k.buffer, v.buffer, pacc.buffer, pm.buffer, pl.buffer, meta.buffer];
self.dispatch_outs("sdpa_decode_split", &bufs1, &[3, 4, 5], &p1, (rows as u32, nsplit as u32, 1))?;
let mut p2 = Vec::with_capacity(8);
p2.extend_from_slice(&(d as u32).to_le_bytes());
p2.extend_from_slice(&(nsplit as u32).to_le_bytes());
let bufs2 = [pacc.buffer, pm.buffer, pl.buffer, out.buffer];
self.dispatch_outs("sdpa_decode_reduce", &bufs2, &[3], &p2, (rows as u32, 1, 1))?;
Ok(())
}
/// Command-graph variant of [`Self::sdpa_blk_vk`]: the same `sdpa_blk` .spv, but `out`, `scale`
/// and `meta` are CALLER-OWNED STABLE buffers instead of freshly allocated/uploaded each call.
/// The kernel reads the attended key count from `meta[1]` (seq_k), so a captured decode graph
/// records this attention once against the FULL, fixed-shape KV cache and every replay attends the
/// ADVANCING span by refreshing `meta[1]` in place -- no re-narrow, no re-upload, no re-record.
/// Because every layer's decode attention has the identical shape and cache strides, ONE shared
/// `meta` (and `scale`, and a per-role `out`) serves the whole forward. `meta` layout matches
/// `sdpa_blk_vk`: `[seq_q, seq_k, n_heads, n_kv, causal, kv_batch_stride, kv_head_stride, key_stride]`
/// (u32, elements). Writes `out` (binding 3) in place; returns nothing.
#[allow(clippy::too_many_arguments)]
pub fn sdpa_blk_vk_graph(
&self,
q: &VulkanStorage,
k: &VulkanStorage,
v: &VulkanStorage,
out: &VulkanStorage,
scale: &VulkanStorage,
meta: &VulkanStorage,
b: usize,
n_heads: usize,
seq_q: usize,
) -> Result<()> {
let bufs = [q.buffer, k.buffer, v.buffer, out.buffer, scale.buffer, meta.buffer];
self.dispatch_outs("sdpa_blk", &bufs, &[3], &[], ((b * n_heads * seq_q) as u32, 1, 1))
}
/// Build the shared [`VkGraphAttn`] buffers a decode command-graph binds for every layer's
/// [`Self::sdpa_blk_vk_graph`]. The KV cache is the contiguous `[b=1, n_kv, capacity, head_dim]`
/// buffer each layer appends into, so the per-head/per-key strides are fixed for the graph's life
/// and encoded once here; the caller advances only `seq_k` per replay via [`VkGraphAttn::set_seq_k`].
/// `softmax_scale`/`causal` match the eager [`Self::sdpa_blk_vk`] call for the same decode step.
#[allow(clippy::too_many_arguments)]
pub fn new_graph_attn(
&self,
n_heads: usize,
n_kv: usize,
head_dim: usize,
capacity: usize,
softmax_scale: f32,
causal: bool,
seq_k: usize,
) -> Result<VkGraphAttn> {
let key_stride = head_dim;
let kv_head_stride = capacity * head_dim;
let kv_batch_stride = n_kv * kv_head_stride;
let meta = self.upload_u32(&[
1, // seq_q (decode: single query)
seq_k as u32,
n_heads as u32,
n_kv as u32,
causal as u32,
kv_batch_stride as u32,
kv_head_stride as u32,
key_stride as u32,
])?;
let scale = self.upload_f32(&[softmax_scale])?;
Ok(VkGraphAttn {
scale,
meta,
n_kv,
softmax_scale,
kv_batch_stride,
kv_head_stride,
key_stride,
})
}
/// Q6_K matrix-vector: `y[nout] = Wq * x[k]` where `Wq` came from [`quantize_q6k`]. Reads weights
/// at ~6.5 bits/elem (incl. pad) instead of 32 -- the bandwidth lever for memory-bound decode on
/// this APU. Decode matches the CPU `BlockQ6K::to_float`.
pub fn matvec_q6k(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_q6k: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_q6k_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Q6_K matvec with both operands already on the GPU: `y[nout] = Wq * x[k]`, no host round-trip.
/// Weights stay quantized in VRAM (~6.5 bits/elem incl. pad) instead of dequantizing to f32, so
/// decode reads ~5x less weight memory. One invocation per output row (scalar kernel).
pub fn matvec_q6k_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
self.matvec_q6k_gpu_off(wq, x, nout, k, 0)
}
/// Q6_K matvec into a slice of `wq` starting at `woff` u32 words: `y[nout] = Wq[woff..] * x[k]`.
/// `woff == 0` is bit-identical to [`matvec_q6k_gpu`]; a non-zero offset selects one expert's row
/// block of a resident MoE bank (`woff = e * nout * (k/256) * 53`, 53 u32 = padded Q6_K block).
pub fn matvec_q6k_gpu_off(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
woff: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_q6k_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_q6k_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32, woff as u32]);
if self.inner.subgroup_matvec && std::env::var_os("VK_Q6K_CM_OFF").is_none() {
// Coalesced multi-thread kernel: THREADS_PER_BLOCK=16 threads cooperate on one super-block
// (adjacent threads read adjacent u32 weight words = coalesced), Q6K_CM_ROWS rows per
// workgroup amortise the activation loads. The memory-BW lever for Q6_K decode; bit-identical
// to mul_mat_vec_q6k_sg. VK_Q6K_CM_OFF reverts to the per-row subgroup kernel for the A/B.
// Q6K_CM_ROWS MUST equal the shader's `NR` constant.
const Q6K_CM_ROWS: u32 = 2;
self.dispatch(
"mul_mat_vec_q6k_cm",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(Q6K_CM_ROWS), 1, 1),
)?;
} else if self.inner.subgroup_matvec {
// Subgroup-reduced kernel (one subgroup per output row, fused subgroupAdd); bit-identical
// decode to the scalar mul_mat_vec_q6k. See matvec_q4k_gpu_off for the dispatch geometry.
let rows_per_wg = (WG1D / self.inner.subgroup_size).max(1);
self.dispatch(
"mul_mat_vec_q6k_sg",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(rows_per_wg), 1, 1),
)?;
} else {
self.dispatch(
"mul_mat_vec_q6k",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
}
Ok(out)
}
// -----------------------------------------------------------------------------------------
// Additional native-GGML decode matvec coverage (memory-bound decode path): Q2_K, Q3_K,
// IQ4_XS, IQ4_NL, TQ2_0. Each reads its raw GGML block bytes (uploaded by `upload_qweight`,
// except Q3_K which repacks below) and its in-shader decode matches the CPU `*::to_float`. One
// invocation per output row (scalar kernel), push {nout, k}; no MoE woff (decode only).
/// Upload Q3_K weights to the GPU once. The CPU `k_quants::BlockQ3K` block is 110 bytes
/// (hmask[32], qs[64], scales[12], d f16) which is NOT u32-aligned, so each block is repacked
/// into a PADDED 112-byte (28 u32) stride here (trailing 2 bytes unused) -- mirrors
/// [`quantize_q6k`] -- so the shader can read the three 6-bit-packed scale words as aligned u32.
/// `data` must be exactly the `nout * (k/256) * 110` packed block bytes; `k` a multiple of 256.
pub fn quantize_q3k(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_q3k: k must be a multiple of 256, got {k}");
}
let nblocks = k / 256;
let total = nout * nblocks;
let want = total * 110;
if data.len() != want {
crate::bail!(
"quantize_q3k: data len {} != {nout}*{nblocks}*110 = {want}",
data.len()
);
}
// Repack 110-byte source blocks into 28-u32 (112-byte) padded blocks so every block starts on
// a u32 boundary. Trailing 2 bytes of each block are zero pad and never read by the shader.
let mut words = vec![0u32; total * 28];
for blk in 0..total {
let src = &data[blk * 110..blk * 110 + 110];
let dst = &mut words[blk * 28..blk * 28 + 28];
let dst_bytes: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u8, 28 * 4) };
dst_bytes[..110].copy_from_slice(src);
}
self.upload_u32(&words)
}
/// Q2_K matvec with both operands already on the GPU: `y[nout] = Wq * x[k]`, weights stay
/// quantized in VRAM (~2.6 bits/elem). `Wq` is the raw GGML bytes from [`upload_qweight`].
pub fn matvec_q2k_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_q2k_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_q2k_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_q2k",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// Q2_K matvec with the activation supplied as a host f32 slice (`x.len() == k`).
pub fn matvec_q2k(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_q2k: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_q2k_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Q3_K matvec with both operands already on the GPU: `y[nout] = Wq * x[k]`, weights stay
/// quantized in VRAM (~3.4 bits/elem incl. pad). `Wq` is the padded 28-u32 stride from
/// [`quantize_q3k`].
pub fn matvec_q3k_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_q3k_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_q3k_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_q3k",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// Q3_K matvec with the activation supplied as a host f32 slice (`x.len() == k`).
pub fn matvec_q3k(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_q3k: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_q3k_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// IQ4_XS matvec with both operands already on the GPU: `y[nout] = Wq * x[k]`, weights stay
/// quantized in VRAM (~4.25 bits/elem). `Wq` is the raw GGML bytes from [`upload_qweight`].
pub fn matvec_iq4xs_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_iq4xs_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_iq4xs_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_iq4xs",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// IQ4_XS matvec with the activation supplied as a host f32 slice (`x.len() == k`).
pub fn matvec_iq4xs(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_iq4xs: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_iq4xs_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Upload IQ2_XXS weights, repacking each 66-byte GGML block (d f16 + qs[32] u16) into a padded
/// 17-u32 (68-byte) stride so the device buffer is u32-aligned (66 is not). Trailing 2 bytes are
/// zero pad and never read by the shader (it byte-addresses within the 66 real bytes).
pub fn quantize_iq2xxs(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_iq2xxs: k must be a multiple of 256, got {k}");
}
let nblocks = k / 256;
let total = nout * nblocks;
let want = total * 66;
if data.len() != want {
crate::bail!(
"quantize_iq2xxs: data len {} != {nout}*{nblocks}*66 = {want}",
data.len()
);
}
let mut words = vec![0u32; total * 17];
for blk in 0..total {
let src = &data[blk * 66..blk * 66 + 66];
let dst = &mut words[blk * 17..blk * 17 + 17];
let dst_bytes: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u8, 17 * 4) };
dst_bytes[..66].copy_from_slice(src);
}
self.upload_u32(&words)
}
/// IQ2_XXS matvec: `y[nout] = Wq * x[k]` where `Wq` is the 17-u32/block repack from
/// [`quantize_iq2xxs`]. Codebook-grid decode (2.06 bpw) straight out of VRAM, no host requantize.
pub fn matvec_iq2xxs_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_iq2xxs_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_iq2xxs_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_iq2xxs",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// IQ2_XXS matvec, host activation. `wq` is the [`quantize_iq2xxs`] repack.
pub fn matvec_iq2xxs(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_iq2xxs: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_iq2xxs_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Upload IQ2_XS weights, repacking each 74-byte GGML block (d f16 + qs[32] u16 + scales[8]) into
/// a padded 19-u32 (76-byte) stride for u32 alignment (74 is not). Trailing 2 bytes are zero pad.
pub fn quantize_iq2xs(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_iq2xs: k must be a multiple of 256, got {k}");
}
let nblocks = k / 256;
let total = nout * nblocks;
let want = total * 74;
if data.len() != want {
crate::bail!(
"quantize_iq2xs: data len {} != {nout}*{nblocks}*74 = {want}",
data.len()
);
}
let mut words = vec![0u32; total * 19];
for blk in 0..total {
let src = &data[blk * 74..blk * 74 + 74];
let dst = &mut words[blk * 19..blk * 19 + 19];
let dst_bytes: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u8, 19 * 4) };
dst_bytes[..74].copy_from_slice(src);
}
self.upload_u32(&words)
}
/// IQ2_XS matvec: `y[nout] = Wq * x[k]`, `Wq` from [`quantize_iq2xs`]. 2.31 bpw codebook decode.
pub fn matvec_iq2xs_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_iq2xs_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_iq2xs_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_iq2xs",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// IQ2_XS matvec, host activation. `wq` is the [`quantize_iq2xs`] repack.
pub fn matvec_iq2xs(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_iq2xs: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_iq2xs_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Upload IQ1_M weights, repacking each 56-byte GGML block into a padded 14-u32 stride
/// (u32 alignment; 56 is not a multiple of 4). Trailing pad bytes are zero and never read.
pub fn quantize_iq1m(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_iq1m: k must be a multiple of 256, got {k}");
}
let nblocks = k / 256;
let total = nout * nblocks;
let want = total * 56;
if data.len() != want {
crate::bail!(
"quantize_iq1m: data len {} != {nout}*{nblocks}*56 = {want}",
data.len()
);
}
let mut words = vec![0u32; total * 14];
for blk in 0..total {
let src = &data[blk * 56..blk * 56 + 56];
let dst = &mut words[blk * 14..blk * 14 + 14];
let dst_bytes: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u8, 14 * 4) };
dst_bytes[..56].copy_from_slice(src);
}
self.upload_u32(&words)
}
/// IQ1_M matvec: `y[nout] = Wq * x[k]`, `Wq` from [`quantize_iq1m`]. 1.75bpw codebook decode.
pub fn matvec_iq1m_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_iq1m_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_iq1m_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_iq1m",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// IQ1_M matvec, host activation. `wq` is the [`quantize_iq1m`] repack.
pub fn matvec_iq1m(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_iq1m: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_iq1m_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Upload IQ1_S weights, repacking each 50-byte GGML block into a padded 13-u32 stride
/// (u32 alignment; 50 is not a multiple of 4). Trailing pad bytes are zero and never read.
pub fn quantize_iq1s(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_iq1s: k must be a multiple of 256, got {k}");
}
let nblocks = k / 256;
let total = nout * nblocks;
let want = total * 50;
if data.len() != want {
crate::bail!(
"quantize_iq1s: data len {} != {nout}*{nblocks}*50 = {want}",
data.len()
);
}
let mut words = vec![0u32; total * 13];
for blk in 0..total {
let src = &data[blk * 50..blk * 50 + 50];
let dst = &mut words[blk * 13..blk * 13 + 13];
let dst_bytes: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u8, 13 * 4) };
dst_bytes[..50].copy_from_slice(src);
}
self.upload_u32(&words)
}
/// IQ1_S matvec: `y[nout] = Wq * x[k]`, `Wq` from [`quantize_iq1s`]. 1.5bpw codebook decode.
pub fn matvec_iq1s_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_iq1s_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_iq1s_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_iq1s",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// IQ1_S matvec, host activation. `wq` is the [`quantize_iq1s`] repack.
pub fn matvec_iq1s(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_iq1s: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_iq1s_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Upload IQ3_S weights, repacking each 110-byte GGML block into a padded 28-u32 stride
/// (u32 alignment; 110 is not a multiple of 4). Trailing pad bytes are zero and never read.
pub fn quantize_iq3s(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_iq3s: k must be a multiple of 256, got {k}");
}
let nblocks = k / 256;
let total = nout * nblocks;
let want = total * 110;
if data.len() != want {
crate::bail!(
"quantize_iq3s: data len {} != {nout}*{nblocks}*110 = {want}",
data.len()
);
}
let mut words = vec![0u32; total * 28];
for blk in 0..total {
let src = &data[blk * 110..blk * 110 + 110];
let dst = &mut words[blk * 28..blk * 28 + 28];
let dst_bytes: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u8, 28 * 4) };
dst_bytes[..110].copy_from_slice(src);
}
self.upload_u32(&words)
}
/// IQ3_S matvec: `y[nout] = Wq * x[k]`, `Wq` from [`quantize_iq3s`]. 3.31 codebook decode.
pub fn matvec_iq3s_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_iq3s_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_iq3s_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_iq3s",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// IQ3_S matvec, host activation. `wq` is the [`quantize_iq3s`] repack.
pub fn matvec_iq3s(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_iq3s: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_iq3s_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Upload IQ3_XXS weights, repacking each 98-byte GGML block into a padded 25-u32 stride
/// (u32 alignment; 98 is not a multiple of 4). Trailing pad bytes are zero and never read.
pub fn quantize_iq3xxs(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_iq3xxs: k must be a multiple of 256, got {k}");
}
let nblocks = k / 256;
let total = nout * nblocks;
let want = total * 98;
if data.len() != want {
crate::bail!(
"quantize_iq3xxs: data len {} != {nout}*{nblocks}*98 = {want}",
data.len()
);
}
let mut words = vec![0u32; total * 25];
for blk in 0..total {
let src = &data[blk * 98..blk * 98 + 98];
let dst = &mut words[blk * 25..blk * 25 + 25];
let dst_bytes: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u8, 25 * 4) };
dst_bytes[..98].copy_from_slice(src);
}
self.upload_u32(&words)
}
/// IQ3_XXS matvec: `y[nout] = Wq * x[k]`, `Wq` from [`quantize_iq3xxs`]. 3.06 codebook decode.
pub fn matvec_iq3xxs_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_iq3xxs_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_iq3xxs_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_iq3xxs",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// IQ3_XXS matvec, host activation. `wq` is the [`quantize_iq3xxs`] repack.
pub fn matvec_iq3xxs(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_iq3xxs: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_iq3xxs_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// Upload IQ2_S weights, repacking each 82-byte GGML block into a padded 21-u32 stride
/// (u32 alignment; 82 is not a multiple of 4). Trailing pad bytes are zero and never read.
pub fn quantize_iq2s(&self, data: &[u8], nout: usize, k: usize) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("quantize_iq2s: k must be a multiple of 256, got {k}");
}
let nblocks = k / 256;
let total = nout * nblocks;
let want = total * 82;
if data.len() != want {
crate::bail!(
"quantize_iq2s: data len {} != {nout}*{nblocks}*82 = {want}",
data.len()
);
}
let mut words = vec![0u32; total * 21];
for blk in 0..total {
let src = &data[blk * 82..blk * 82 + 82];
let dst = &mut words[blk * 21..blk * 21 + 21];
let dst_bytes: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u8, 21 * 4) };
dst_bytes[..82].copy_from_slice(src);
}
self.upload_u32(&words)
}
/// IQ2_S matvec: `y[nout] = Wq * x[k]`, `Wq` from [`quantize_iq2s`]. 2.56 codebook decode.
pub fn matvec_iq2s_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_iq2s_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_iq2s_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_iq2s",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// IQ2_S matvec, host activation. `wq` is the [`quantize_iq2s`] repack.
pub fn matvec_iq2s(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_iq2s: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_iq2s_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// IQ4_NL matvec with both operands already on the GPU: `y[nout] = Wq * x[k]`, weights stay
/// quantized in VRAM (~4.5 bits/elem). `Wq` is the raw GGML bytes from [`upload_qweight`].
pub fn matvec_iq4nl_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(32) {
crate::bail!("matvec_iq4nl_gpu: k must be a multiple of 32, got {k}");
}
if x.count < k {
crate::bail!("matvec_iq4nl_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_iq4nl",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// IQ4_NL matvec with the activation supplied as a host f32 slice (`x.len() == k`).
pub fn matvec_iq4nl(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_iq4nl: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_iq4nl_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
/// TQ2_0 matvec with both operands already on the GPU: `y[nout] = Wq * x[k]`, weights stay
/// quantized in VRAM (~2.06 bits/elem). `Wq` is the raw GGML bytes from [`upload_qweight`].
pub fn matvec_tq2_0_gpu(
&self,
wq: &VulkanStorage,
x: &VulkanStorage,
nout: usize,
k: usize,
) -> Result<VulkanStorage> {
if !k.is_multiple_of(256) {
crate::bail!("matvec_tq2_0_gpu: k must be a multiple of 256, got {k}");
}
if x.count < k {
crate::bail!("matvec_tq2_0_gpu: x count {} < k {k}", x.count);
}
let out = self.alloc_f32(nout)?;
let push = push_u32(&[nout as u32, k as u32]);
self.dispatch(
"mul_mat_vec_tq2_0",
&[wq.buffer, x.buffer, out.buffer],
&push,
((nout as u32).div_ceil(WG1D), 1, 1),
)?;
Ok(out)
}
/// TQ2_0 matvec with the activation supplied as a host f32 slice (`x.len() == k`).
pub fn matvec_tq2_0(
&self,
wq: &VulkanStorage,
x: &[f32],
nout: usize,
k: usize,
) -> Result<Vec<f32>> {
if x.len() != k {
crate::bail!("matvec_tq2_0: x len {} != k {k}", x.len());
}
let xs = self.upload_f32(x)?;
self.matvec_tq2_0_gpu(wq, &xs, nout, k)?.to_vec_f32()
}
// Allocate a storage buffer of `bytes` bytes, returning it plus whether its memory is
// HOST_VISIBLE (directly CPU-mappable). Placement follows the configured MemStrategy: on this
// UMA APU a large buffer may be placed in a DEVICE_LOCAL-only heap (the big GTT pool) that the
// CPU cannot map — callers then upload/read back through a staging buffer. Buffers carry
// TRANSFER_SRC|TRANSFER_DST usage so that staging GPU copy is always legal.
unsafe fn raw_buffer(&self, bytes: u64) -> Result<(vk::Buffer, vk::DeviceMemory, bool)> {
// Allocate at the bucket size so every pooled buffer matches its `free` key exactly; a reused
// buffer is then always physically >= the request (kernels touch only the first `n` elems via
// their push-constant count, and descriptors bind WHOLE_SIZE, so the extra tail is unused).
let bytes = pool_bucket(bytes);
// Reuse a same-bucket buffer reclaimed from a completed batch before allocating fresh.
{
let mut pool = self.inner.bufpool.lock().unwrap();
if let Some(p) = pool.free.get_mut(&bytes).and_then(Vec::pop) {
pool.free_bytes = pool.free_bytes.saturating_sub(bytes);
POOL_HIT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return Ok((p.buffer, p.memory, p.host_visible));
}
}
POOL_FRESH.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
POOL_FRESH_BYTES.fetch_add(bytes, std::sync::atomic::Ordering::Relaxed);
let dev = self.dev();
let info = vk::BufferCreateInfo::default()
.size(bytes)
.usage(
vk::BufferUsageFlags::STORAGE_BUFFER
| vk::BufferUsageFlags::TRANSFER_SRC
| vk::BufferUsageFlags::TRANSFER_DST,
)
.sharing_mode(vk::SharingMode::EXCLUSIVE);
let buf = dev.create_buffer(&info, None).map_err(vkerr)?;
let req = dev.get_buffer_memory_requirements(buf);
let idx = self
.pick_memory_type(req.memory_type_bits, req.size)
.ok_or_else(|| {
Error::Msg(format!(
"vulkan: no usable memory type for a {req_size}-byte buffer (type_bits={bits:#x}, strategy={strat:?})",
req_size = req.size,
bits = req.memory_type_bits,
strat = self.inner.mem_strategy,
))
})?;
let (mem, used_idx) = match dev.allocate_memory(
&vk::MemoryAllocateInfo::default()
.allocation_size(req.size)
.memory_type_index(idx),
None,
) {
Ok(m) => (m, idx),
Err(e) => {
// The chosen heap accounting said the type was usable, but the driver still refused
// (fragmentation, racing allocations, or a too-optimistic budget). Try every other
// usable type, largest heap first, before giving up — this is the concrete OOM
// recovery for the 18.6GB-model case where the first pick is the small host-visible
// carveout and the real space is in the DEVICE_LOCAL heap.
let mut fallback = None;
for alt in self.usable_types_by_heap_desc(req.memory_type_bits, req.size) {
if alt == idx {
continue;
}
if let Ok(m) = dev.allocate_memory(
&vk::MemoryAllocateInfo::default()
.allocation_size(req.size)
.memory_type_index(alt),
None,
) {
fallback = Some((m, alt));
break;
}
}
match fallback {
Some(p) => p,
None => {
dev.destroy_buffer(buf, None);
return Err(vkerr(e));
}
}
}
};
dev.bind_buffer_memory(buf, mem, 0).map_err(vkerr)?;
let host_visible = self.inner.mem_props.memory_types[used_idx as usize]
.property_flags
.contains(vk::MemoryPropertyFlags::HOST_VISIBLE);
Ok((buf, mem, host_visible))
}
// Free bytes available in heap `h`. Uses VK_EXT_memory_budget (heapBudget = driver's estimate of
// what this process may still allocate from the heap) when advertised; otherwise falls back to
// the heap's total `size` as a conservative upper bound. Budget is re-queried each call because
// it shifts as buffers are allocated/freed (this is the point of the scratch guard below).
fn free_heap_bytes(&self, h: u32) -> u64 {
let mp = &self.inner.mem_props;
let total = mp.memory_heaps[h as usize].size;
if !self.inner.has_mem_budget {
return total;
}
unsafe {
let mut budget = vk::PhysicalDeviceMemoryBudgetPropertiesEXT::default();
{
// props2 mutably borrows budget via push_next; scope it so the borrow ends before
// we read budget back.
let mut props2 =
vk::PhysicalDeviceMemoryProperties2::default().push_next(&mut budget);
self.inner
.instance
.get_physical_device_memory_properties2(self.inner.pdev, &mut props2);
}
// heap_budget is 0 for heaps the driver doesn't report; treat that as "unknown" and use
// the static size so we never under-report and wrongly refuse a valid allocation.
let b = budget.heap_budget[h as usize];
if b == 0 {
total
} else {
b
}
}
}
// Usable memory types for `type_bits` whose heap can (per free_heap_bytes) hold `bytes`, ordered
// largest free-heap first. The placement primitive for both pick_memory_type and the OOM retry.
fn usable_types_by_heap_desc(&self, type_bits: u32, bytes: u64) -> Vec<u32> {
let mp = &self.inner.mem_props;
let mut v: Vec<u32> = (0..mp.memory_type_count)
.filter(|&i| (type_bits & (1 << i)) != 0)
.filter(|&i| {
let h = mp.memory_types[i as usize].heap_index;
self.free_heap_bytes(h) >= bytes
})
.collect();
v.sort_by_key(|&i| {
let h = mp.memory_types[i as usize].heap_index;
std::cmp::Reverse(self.free_heap_bytes(h))
});
v
}
// Will a transient scratch buffer of `bytes` total bytes fit in the largest usable heap's free
// space, keeping a margin so we don't allocate the very last byte (which the driver may need for
// command-buffer / descriptor backing)? Used to decide between the fp16 coopmat path (extra
// scratch) and the fp32 path (none) without risking an OOM abort.
fn scratch_fits(&self, bytes: u64) -> bool {
// 64 MiB margin or 1/16 of the request, whichever is larger.
let margin = (bytes / 16).max(64 * 1024 * 1024);
let need = bytes.saturating_add(margin);
let mp = &self.inner.mem_props;
(0..mp.memory_heap_count)
.map(|h| self.free_heap_bytes(h))
.max()
.map(|free| free >= need)
.unwrap_or(false)
}
// Choose a memory type index for a `bytes`-sized buffer with the given `type_bits`, honouring the
// configured MemStrategy. Returns None only when no type's heap can fit the request.
//
// The shapes we must handle on the AMD 8060S UMA part:
// - a small HOST_VISIBLE|DEVICE_LOCAL "carveout" heap (hundreds of MB), and
// - a large DEVICE_LOCAL-only heap (the GTT pool, tens of GB).
// Placing an 18.6GB weight buffer demands the large heap, which may not be host-visible; the
// upload/readback path stages through a host-visible buffer for those (see write_/read_).
fn pick_memory_type(&self, type_bits: u32, bytes: u64) -> Option<u32> {
let mp = &self.inner.mem_props;
// host-visible candidates whose heap fits, cached-coherent preferred then plain coherent.
let host_cached = vk::MemoryPropertyFlags::HOST_VISIBLE
| vk::MemoryPropertyFlags::HOST_COHERENT
| vk::MemoryPropertyFlags::HOST_CACHED;
let host_base =
vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT;
let fits = |i: u32| -> bool {
let h = mp.memory_types[i as usize].heap_index;
self.free_heap_bytes(h) >= bytes
};
let host_visible_pick = |flags: vk::MemoryPropertyFlags| -> Option<u32> {
(0..mp.memory_type_count).find(|&i| {
(type_bits & (1 << i)) != 0
&& mp.memory_types[i as usize].property_flags.contains(flags)
&& fits(i)
})
};
// Largest DEVICE_LOCAL heap that fits (the GTT pool on UMA), then largest heap of any kind.
let device_local_pick = || -> Option<u32> {
self.usable_types_by_heap_desc(type_bits, bytes)
.into_iter()
.find(|&i| {
mp.memory_types[i as usize]
.property_flags
.contains(vk::MemoryPropertyFlags::DEVICE_LOCAL)
})
};
let any_largest = || {
self.usable_types_by_heap_desc(type_bits, bytes)
.into_iter()
.next()
};
match self.inner.mem_strategy {
MemStrategy::HostOnly => {
host_visible_pick(host_cached).or_else(|| host_visible_pick(host_base))
}
MemStrategy::DeviceFirst => device_local_pick()
.or_else(|| host_visible_pick(host_cached))
.or_else(|| host_visible_pick(host_base))
.or_else(any_largest),
// Auto: prefer host-visible when it fits (cheap upload + readback, no staging on UMA),
// else spill to the largest DEVICE_LOCAL heap — the path that makes the 18.6GB model load.
MemStrategy::Auto => host_visible_pick(host_cached)
.or_else(|| host_visible_pick(host_base))
.or_else(device_local_pick)
.or_else(any_largest),
}
}
// Allocate a buffer that is guaranteed HOST_VISIBLE (for staging). Forces the host-visible
// policy regardless of the device strategy; staging buffers are transient and always small
// relative to the host-visible heap (one tensor's worth at a time). Not pooled.
unsafe fn raw_buffer_host_visible(&self, bytes: u64) -> Result<(vk::Buffer, vk::DeviceMemory)> {
let bytes = bytes.max(4);
let dev = self.dev();
let info = vk::BufferCreateInfo::default()
.size(bytes)
.usage(vk::BufferUsageFlags::TRANSFER_SRC | vk::BufferUsageFlags::TRANSFER_DST)
.sharing_mode(vk::SharingMode::EXCLUSIVE);
let buf = dev.create_buffer(&info, None).map_err(vkerr)?;
let req = dev.get_buffer_memory_requirements(buf);
let mp = &self.inner.mem_props;
let host = vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT;
let idx = match (0..mp.memory_type_count).find(|&i| {
(req.memory_type_bits & (1 << i)) != 0
&& mp.memory_types[i as usize].property_flags.contains(host)
}) {
Some(i) => i,
None => {
dev.destroy_buffer(buf, None);
return Err(Error::Msg(
"vulkan: no host-visible memory type for staging buffer".into(),
));
}
};
let mem = match dev.allocate_memory(
&vk::MemoryAllocateInfo::default()
.allocation_size(req.size)
.memory_type_index(idx),
None,
) {
Ok(m) => m,
Err(e) => {
dev.destroy_buffer(buf, None);
return Err(vkerr(e));
}
};
dev.bind_buffer_memory(buf, mem, 0).map_err(vkerr)?;
Ok((buf, mem))
}
// Copy `bytes` from `src[src_off..]` to `dst[dst_off..]` on a one-shot command buffer and block
// until done. Uses the submitter's command pool + its fence. The submitter lock is held for the
// whole copy so it can't interleave with a concurrent batch on the same `cmd`; callers flush
// first so `cmd` is idle on entry.
unsafe fn copy_buffer_blocking(
&self,
src: vk::Buffer,
src_off: u64,
dst: vk::Buffer,
dst_off: u64,
bytes: u64,
) -> Result<()> {
let dev = self.dev();
let mut s = self.inner.submitter.lock().unwrap();
dev.reset_command_buffer(s.cmd, vk::CommandBufferResetFlags::empty())
.map_err(vkerr)?;
dev.begin_command_buffer(
s.cmd,
&vk::CommandBufferBeginInfo::default()
.flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT),
)
.map_err(vkerr)?;
let region = [vk::BufferCopy::default()
.src_offset(src_off)
.dst_offset(dst_off)
.size(bytes)];
dev.cmd_copy_buffer(s.cmd, src, dst, ®ion);
dev.end_command_buffer(s.cmd).map_err(vkerr)?;
dev.reset_fences(&[s.fence]).map_err(vkerr)?;
let cmds = [s.cmd];
let submit = [vk::SubmitInfo::default().command_buffers(&cmds)];
dev.queue_submit(self.inner.queue, &submit, s.fence)
.map_err(vkerr)?;
dev.wait_for_fences(&[s.fence], true, u64::MAX)
.map_err(vkerr)?;
s.recording = false;
s.n = 0;
s.written_since_barrier.clear();
Ok(())
}
// One-shot device-to-device copy bandwidth = the roofline BW denominator. Copies a large buffer a
// few times and reports 2*bytes/time (read src + write dst = the memory subsystem's streaming BW).
// Best-effort: any allocation/copy failure leaves peak_bw_gbps at 0.0 (the table then shows 0%).
// Runs once at init only when VK_ROOFLINE is set, so it is off the normal path entirely.
fn measure_and_store_peak_bw(&self) {
let bytes: u64 = 512 * 1024 * 1024;
let count = (bytes / 4) as usize;
let (Ok(a), Ok(b)) = (self.alloc_f32(count), self.alloc_f32(count)) else {
return;
};
unsafe {
if self.copy_buffer_blocking(a.buffer, 0, b.buffer, 0, bytes).is_err() {
return;
}
let iters = 5u32;
let t0 = std::time::Instant::now();
for _ in 0..iters {
if self.copy_buffer_blocking(a.buffer, 0, b.buffer, 0, bytes).is_err() {
return;
}
}
let secs = t0.elapsed().as_secs_f64();
let gbps = (2.0 * bytes as f64 * iters as f64) / 1e9 / secs.max(1e-9);
self.inner
.peak_bw_gbps
.store((gbps as f32).to_bits(), std::sync::atomic::Ordering::Relaxed);
}
}
// Bound on a single staging chunk. A non-host-visible buffer can be many GB (the 18.6GB model),
// but the host-visible heap on this UMA part is a small carveout — so we stage in chunks of at
// most this size, reusing one small staging buffer, instead of needing a full-size host-visible
// mirror. 256 MiB is a good balance of per-copy submit overhead vs staging footprint.
const STAGE_CHUNK: u64 = 256 * 1024 * 1024;
// Upload raw `data` into device-local (non-host-visible) `dst` via a transient host-visible
// staging buffer + GPU copy. `dst` carries TRANSFER_DST usage (set in raw_buffer). Any recorded
// batch is flushed first (the copy mutates `dst`), then the copy runs on its own one-shot submit.
unsafe fn staged_upload(&self, dst: vk::Buffer, data: &[u8]) -> Result<()> {
if data.is_empty() {
return Ok(());
}
self.flush()?;
let dev = self.dev();
let total = data.len() as u64;
// One small staging buffer (<= STAGE_CHUNK), reused across chunks, so the host-visible heap
// never needs the full buffer's worth — the lever that lets an 18.6GB DEVICE_LOCAL buffer
// load through a few-hundred-MB host-visible carveout.
let chunk = total.min(Self::STAGE_CHUNK);
let (staging, staging_mem) = self.raw_buffer_host_visible(chunk)?;
let mut off = 0u64;
let result = (|| -> Result<()> {
while off < total {
let n = (total - off).min(chunk);
let ptr = dev
.map_memory(staging_mem, 0, n, vk::MemoryMapFlags::empty())
.map_err(vkerr)? as *mut u8;
std::ptr::copy_nonoverlapping(data.as_ptr().add(off as usize), ptr, n as usize);
dev.unmap_memory(staging_mem);
self.copy_buffer_blocking(staging, 0, dst, off, n)?;
off += n;
}
Ok(())
})();
dev.destroy_buffer(staging, None);
dev.free_memory(staging_mem, None);
result
}
// Read `bytes` out of device-local (non-host-visible) `src` via a transient host-visible staging
// buffer + GPU copy, chunked like staged_upload. Mirror of staged_upload.
unsafe fn staged_readback(&self, src: vk::Buffer, bytes: u64) -> Result<Vec<u8>> {
if bytes == 0 {
return Ok(Vec::new());
}
self.flush()?;
let dev = self.dev();
let chunk = bytes.min(Self::STAGE_CHUNK);
let (staging, staging_mem) = self.raw_buffer_host_visible(chunk)?;
let mut out = vec![0u8; bytes as usize];
let mut off = 0u64;
let result = (|| -> Result<()> {
while off < bytes {
let n = (bytes - off).min(chunk);
self.copy_buffer_blocking(src, off, staging, 0, n)?;
let ptr = dev
.map_memory(staging_mem, 0, n, vk::MemoryMapFlags::empty())
.map_err(vkerr)? as *const u8;
std::ptr::copy_nonoverlapping(ptr, out.as_mut_ptr().add(off as usize), n as usize);
dev.unmap_memory(staging_mem);
off += n;
}
Ok(())
})();
dev.destroy_buffer(staging, None);
dev.free_memory(staging_mem, None);
result.map(|()| out)
}
// Upload f32 `data` into a storage buffer. Host-visible memory takes the direct-map fast path;
// DEVICE_LOCAL-only memory (big weight buffers on UMA) goes through a staging copy. `buffer` is
// unused on the host-visible path but required to issue the staging GPU copy on the other.
unsafe fn write_f32(
&self,
buffer: vk::Buffer,
mem: vk::DeviceMemory,
host_visible: bool,
data: &[f32],
) -> Result<()> {
if data.is_empty() {
return Ok(());
}
if !host_visible {
let bytes = std::slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * 4);
return self.staged_upload(buffer, bytes);
}
let dev = self.dev();
let ptr = dev
.map_memory(mem, 0, (data.len() * 4) as u64, vk::MemoryMapFlags::empty())
.map_err(vkerr)? as *mut f32;
std::ptr::copy_nonoverlapping(data.as_ptr(), ptr, data.len());
dev.unmap_memory(mem);
Ok(())
}
unsafe fn read_f32(
&self,
buffer: vk::Buffer,
mem: vk::DeviceMemory,
host_visible: bool,
n: usize,
) -> Result<Vec<f32>> {
if n == 0 {
return Ok(Vec::new());
}
if !host_visible {
let raw = self.staged_readback(buffer, (n * 4) as u64)?;
let mut v = vec![0f32; n];
std::ptr::copy_nonoverlapping(raw.as_ptr(), v.as_mut_ptr() as *mut u8, n * 4);
return Ok(v);
}
// Ensure all recorded GPU work that may write this buffer has completed.
self.flush()?;
let dev = self.dev();
// Time just the map+copy (the flush above already reports its own phases). On the decode
// hot path the only readback per token is the logits, so this is the per-token readback cost.
let t0 = self.inner.profile.then(std::time::Instant::now);
let ptr = dev
.map_memory(mem, 0, (n * 4) as u64, vk::MemoryMapFlags::empty())
.map_err(vkerr)? as *const f32;
let v = std::slice::from_raw_parts(ptr, n).to_vec();
dev.unmap_memory(mem);
if let Some(t) = t0 {
eprintln!(
"[VK_PROFILE] readback(f32): {n} elems map+copy={:.3}ms",
t.elapsed().as_secs_f64() * 1e3
);
}
Ok(v)
}
// u32 mirrors of write_f32/read_f32 (buffers are just bytes; 4 bytes/elem).
unsafe fn write_u32(
&self,
buffer: vk::Buffer,
mem: vk::DeviceMemory,
host_visible: bool,
data: &[u32],
) -> Result<()> {
if data.is_empty() {
return Ok(());
}
if !host_visible {
let bytes = std::slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * 4);
return self.staged_upload(buffer, bytes);
}
let dev = self.dev();
let ptr = dev
.map_memory(mem, 0, (data.len() * 4) as u64, vk::MemoryMapFlags::empty())
.map_err(vkerr)? as *mut u32;
std::ptr::copy_nonoverlapping(data.as_ptr(), ptr, data.len());
dev.unmap_memory(mem);
Ok(())
}
unsafe fn read_u32(
&self,
buffer: vk::Buffer,
mem: vk::DeviceMemory,
host_visible: bool,
n: usize,
) -> Result<Vec<u32>> {
if n == 0 {
return Ok(Vec::new());
}
if !host_visible {
let raw = self.staged_readback(buffer, (n * 4) as u64)?;
let mut v = vec![0u32; n];
std::ptr::copy_nonoverlapping(raw.as_ptr(), v.as_mut_ptr() as *mut u8, n * 4);
return Ok(v);
}
// Ensure all recorded GPU work that may write this buffer has completed.
self.flush()?;
let dev = self.dev();
let t0 = self.inner.profile.then(std::time::Instant::now);
let ptr = dev
.map_memory(mem, 0, (n * 4) as u64, vk::MemoryMapFlags::empty())
.map_err(vkerr)? as *const u32;
let v = std::slice::from_raw_parts(ptr, n).to_vec();
dev.unmap_memory(mem);
if let Some(t) = t0 {
eprintln!(
"[VK_PROFILE] readback(u32): {n} elems map+copy={:.3}ms",
t.elapsed().as_secs_f64() * 1e3
);
}
Ok(v)
}
// Build (or fetch cached) compute pipeline for `name` with `n_buffers` storage bindings
// and a push-constant range of `push_size` bytes.
fn pipeline(
&self,
name: &'static str,
n_buffers: usize,
push_size: usize,
) -> Result<CachedPipeline> {
if let Some(p) = self.inner.pipelines.lock().unwrap().get(name) {
return Ok(p.clone());
}
let dev = self.dev();
// A set layout consumed by vkCmdPushDescriptorSetKHR must be created with the
// PUSH_DESCRIPTOR_KHR flag; the legacy allocate-from-pool path requires it absent. The
// backend commits to one or the other at init (`push_descriptor`), so every layout is built
// to match and the two paths can't be crossed for a given device.
let layout_flags = if self.inner.push_descriptor.is_some() {
vk::DescriptorSetLayoutCreateFlags::PUSH_DESCRIPTOR_KHR
} else {
vk::DescriptorSetLayoutCreateFlags::empty()
};
let cached = unsafe {
let binds: Vec<_> = (0..n_buffers as u32)
.map(|i| {
vk::DescriptorSetLayoutBinding::default()
.binding(i)
.descriptor_type(vk::DescriptorType::STORAGE_BUFFER)
.descriptor_count(1)
.stage_flags(vk::ShaderStageFlags::COMPUTE)
})
.collect();
let set_layout = dev
.create_descriptor_set_layout(
&vk::DescriptorSetLayoutCreateInfo::default()
.flags(layout_flags)
.bindings(&binds),
None,
)
.map_err(vkerr)?;
let set_layouts = [set_layout];
let pcr = [vk::PushConstantRange::default()
.stage_flags(vk::ShaderStageFlags::COMPUTE)
.offset(0)
.size(push_size.max(4) as u32)];
let layout = dev
.create_pipeline_layout(
&vk::PipelineLayoutCreateInfo::default()
.set_layouts(&set_layouts)
.push_constant_ranges(&pcr),
None,
)
.map_err(vkerr)?;
// The autotuner's uncommitted variant is served from the registry under one dedicated name;
// every other name loads its committed module. Cloned only on the (rare) cache miss.
let variant_bytes = (name == "__coopmat_variant")
.then(|| self.inner.coopmat_variant.lock().unwrap().as_ref().map(|(b, _, _)| b.clone()))
.flatten();
let spv_bytes: &[u8] = match &variant_bytes {
Some(b) => b,
None => kernel_spv(name)?,
};
let spv = ash::util::read_spv(&mut std::io::Cursor::new(spv_bytes))
.map_err(|e| Error::Msg(format!("vulkan: bad SPIR-V `{name}`: {e}")))?;
let module = dev
.create_shader_module(&vk::ShaderModuleCreateInfo::default().code(&spv), None)
.map_err(vkerr)?;
let stage = vk::PipelineShaderStageCreateInfo::default()
.stage(vk::ShaderStageFlags::COMPUTE)
.module(module)
.name(c"main");
let pipeline = dev
.create_compute_pipelines(
vk::PipelineCache::null(),
&[vk::ComputePipelineCreateInfo::default()
.stage(stage)
.layout(layout)],
None,
)
.map_err(|(_, e)| vkerr(e))?[0];
dev.destroy_shader_module(module, None);
CachedPipeline {
pipeline,
layout,
set_layout,
n_buffers,
}
};
self.inner
.pipelines
.lock()
.unwrap()
.insert(name, cached.clone());
Ok(cached)
}
// Dispatch kernel `name` over `bufs` (bound 0..N) with raw push bytes and group counts. The
// dispatch is recorded into the current batch's command buffer (deferred; submitted on flush).
// Convention: the LAST buffer is the kernel's output (the one it writes); all others are inputs.
// The two scatter kernels write binding 0 instead, so they call `dispatch_out` with out_idx=0.
fn dispatch(
&self,
name: &'static str,
bufs: &[vk::Buffer],
push: &[u8],
groups: (u32, u32, u32),
) -> Result<()> {
let out_idx = bufs.len().saturating_sub(1);
self.dispatch_outs(name, bufs, &[out_idx], push, groups)
}
// Like `dispatch`, but `out_idx` names which binding the kernel writes (its output). Used for
// selective hazard barriers: only a dispatch that reads a buffer produced earlier in this same
// batch needs a barrier before it (see `written_since_barrier`).
fn dispatch_out(
&self,
name: &'static str,
bufs: &[vk::Buffer],
out_idx: usize,
push: &[u8],
groups: (u32, u32, u32),
) -> Result<()> {
self.dispatch_outs(name, bufs, &[out_idx], push, groups)
}
// Like `dispatch_out`, but names ALL bindings the kernel writes. A fused kernel can both update a
// state buffer in place and write a fresh output (gdn_step); every such write must be tracked so a
// later in-batch reader of either gets the RAW barrier. The read-side check already tests all
// bindings, so the only generalization needed is marking each output below.
fn dispatch_outs(
&self,
name: &'static str,
bufs: &[vk::Buffer],
out_idxs: &[usize],
push: &[u8],
groups: (u32, u32, u32),
) -> Result<()> {
// Per-kernel dispatch tally (VK_PROFILE): the decode wall is launch-overhead-bound
// (~3266 dispatches/token), so `[VK_OP] <name>` | sort | uniq -c names exactly which unfused
// op-chains to collapse first. One line/dispatch, only on the profiling path.
if self.inner.profile {
eprintln!("[VK_OP] {name} grid={}", groups.0 * groups.1 * groups.2);
}
let p = self.pipeline(name, bufs.len(), push.len())?;
debug_assert_eq!(
p.n_buffers,
bufs.len(),
"vulkan: kernel `{name}` binding count drift"
);
let dev = self.dev();
let queue = self.inner.queue;
let profile = self.inner.profile;
let rec_t0 = profile.then(std::time::Instant::now);
let mut s = self.inner.submitter.lock().unwrap();
unsafe {
if !s.capturing && !s.recording {
// Start a fresh batch: free the previous batch's descriptor sets (its GPU work
// already completed at the last flush) and open the command buffer. The pool reset
// is a no-op for the push-descriptor path (no sets are allocated from it) but stays
// harmless and keeps the legacy path correct.
dev.reset_descriptor_pool(s.dpool, vk::DescriptorPoolResetFlags::empty())
.map_err(vkerr)?;
dev.begin_command_buffer(s.cmd, &vk::CommandBufferBeginInfo::default())
.map_err(vkerr)?;
s.recording = true;
s.n = 0;
s.work = 0;
s.written_since_barrier.clear();
s.record_ns = 0;
s.barriers = 0;
if self.inner.gpu_profile {
// Reset the timestamp pool on the GPU timeline and stamp a batch baseline at
// query 0; each dispatch then stamps at op_names.len()+1 so consecutive deltas
// are per-op GPU durations.
dev.cmd_reset_query_pool(s.cmd, s.qpool, 0, BATCH_CAP + 1);
dev.cmd_write_timestamp(
s.cmd,
vk::PipelineStageFlags::TOP_OF_PIPE,
s.qpool,
0,
);
s.op_names.clear();
s.op_push.clear();
}
}
// Command-graph capture records into the dedicated `graph_cmd` (begun by
// `begin_graph_capture`, never auto-flushed); eager work records into the batch `cmd`.
// Both paths share the identical descriptor-push + barrier + dispatch recording below, so a
// captured graph replays the exact op/barrier sequence the eager forward would run.
let cmd = if s.capturing { s.graph_cmd } else { s.cmd };
// RAW hazard: if this dispatch touches a buffer that an earlier dispatch in this same
// batch wrote (and we haven't barriered since), insert ONE memory barrier first, then
// start a new barrier-free group. Independent dispatches (disjoint buffers, or reading
// only weights/inputs from an earlier already-fenced batch) need no barrier and the GPU
// can overlap their fixed launch/drain overhead -- the win on the decode hot path, which
// is hundreds of tiny dispatches/token where that overhead, not compute, dominates.
// We test ALL bindings (not just the inputs): a fresh output handle is never already in
// the set (the pool recycles handles only across fences, so live handles are unique
// in-batch), so this is exact for normal ops AND also catches the in-place scatter
// kernels' read-modify-write of binding 0.
let reads_inflight_write = bufs.iter().any(|b| s.written_since_barrier.contains(b));
if reads_inflight_write {
let bar = [vk::MemoryBarrier::default()
.src_access_mask(vk::AccessFlags::SHADER_WRITE)
.dst_access_mask(vk::AccessFlags::SHADER_READ | vk::AccessFlags::SHADER_WRITE)];
dev.cmd_pipeline_barrier(
cmd,
vk::PipelineStageFlags::COMPUTE_SHADER,
vk::PipelineStageFlags::COMPUTE_SHADER,
vk::DependencyFlags::empty(),
&bar,
&[],
&[],
);
s.written_since_barrier.clear();
s.barriers += 1;
}
// Buffer infos are needed by both paths; build them once. (WriteDescriptorSet borrows
// these, so they must outlive the update/push call below.) Built on the stack into a
// fixed array — never a heap Vec — because this is the decode hot path: hundreds of
// dispatches x N layers re-recorded every token, where a per-dispatch Vec alloc+free for
// both `infos` and `writes` was pure CPU churn the GPU then stalled on. MAX_BINDINGS (10)
// comfortably covers the widest kernel (4 buffers: where_cond/index_select); only the
// first `nb = bufs.len()` entries are populated and passed on, and the debug_assert
// above (kernel binding-count drift) plus the one here pin the bound, so it can never be
// exceeded in a correct build. The arrays live for the rest of this unsafe block,
// satisfying the borrows the push/update calls hold on them.
const MAX_BINDINGS: usize = 10;
let nb = bufs.len();
debug_assert!(
nb <= MAX_BINDINGS,
"vulkan: kernel `{name}` binds {nb} buffers > MAX_BINDINGS {MAX_BINDINGS}"
);
let mut infos = [[vk::DescriptorBufferInfo::default(); 1]; MAX_BINDINGS];
for (i, &b) in bufs.iter().enumerate() {
infos[i] = [vk::DescriptorBufferInfo::default()
.buffer(b)
.range(vk::WHOLE_SIZE)];
}
dev.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::COMPUTE, p.pipeline);
if let Some(pd) = &self.inner.push_descriptor {
// Fast path: push buffer handles inline into the command buffer. No descriptor-set
// object is allocated and nothing is written to pool-backed GPU memory — the driver
// records the bindings directly, eliminating the per-op allocate + update + bind
// (three driver calls + descriptor-pool traffic) that dominated decode CPU time.
let mut writes = [vk::WriteDescriptorSet::default(); MAX_BINDINGS];
for (i, w) in writes.iter_mut().enumerate().take(nb) {
// dst_set is ignored by vkCmdPushDescriptorSetKHR (left default/null).
*w = vk::WriteDescriptorSet::default()
.dst_binding(i as u32)
.descriptor_type(vk::DescriptorType::STORAGE_BUFFER)
.buffer_info(&infos[i]);
}
pd.cmd_push_descriptor_set(
cmd,
vk::PipelineBindPoint::COMPUTE,
p.layout,
0,
&writes[..nb],
);
} else {
// Legacy path: allocate a fresh descriptor set for this dispatch. Sets accumulate
// within the batch (each recorded dispatch keeps its own); the pool is reset only
// when the next batch begins, after the current one has been submitted and awaited.
let set_layouts = [p.set_layout];
let set = dev
.allocate_descriptor_sets(
&vk::DescriptorSetAllocateInfo::default()
.descriptor_pool(s.dpool)
.set_layouts(&set_layouts),
)
.map_err(vkerr)?[0];
let mut writes = [vk::WriteDescriptorSet::default(); MAX_BINDINGS];
for (i, w) in writes.iter_mut().enumerate().take(nb) {
*w = vk::WriteDescriptorSet::default()
.dst_set(set)
.dst_binding(i as u32)
.descriptor_type(vk::DescriptorType::STORAGE_BUFFER)
.buffer_info(&infos[i]);
}
dev.update_descriptor_sets(&writes[..nb], &[]);
dev.cmd_bind_descriptor_sets(
cmd,
vk::PipelineBindPoint::COMPUTE,
p.layout,
0,
&[set],
&[],
);
}
if !push.is_empty() {
dev.cmd_push_constants(cmd, p.layout, vk::ShaderStageFlags::COMPUTE, 0, push);
}
dev.cmd_dispatch(cmd, groups.0, groups.1, groups.2);
if self.inner.gpu_profile && !s.capturing {
let q = s.op_names.len() as u32 + 1;
if q <= BATCH_CAP {
dev.cmd_write_timestamp(
cmd,
vk::PipelineStageFlags::BOTTOM_OF_PIPE,
s.qpool,
q,
);
s.op_names.push(name);
// Snapshot the first 5 push u32 (matmul shape [m0,mcount,nout,k,woff]); the roofline
// model reads it at flush. Cheap fixed copy on the profiling path only.
let mut p5 = [0u32; 5];
for (i, w) in p5.iter_mut().enumerate() {
let o = i * 4;
if push.len() >= o + 4 {
*w = u32::from_le_bytes([push[o], push[o + 1], push[o + 2], push[o + 3]]);
}
}
s.op_push.push(p5);
}
}
// Mark this dispatch's output live in the current barrier-free group so a later
// dispatch that READS it triggers the barrier above. WAW/WAR can't arise within a
// batch: a buffer is written only as some op's freshly-allocated output, the pool only
// recycles a freed buffer's handle into a new allocation AFTER a flush+fence (reclaim
// runs post-fence), so no two live allocations in one batch share a handle -- thus the
// only intra-batch hazard is RAW, which this set captures. Cross-batch ordering is the
// full queue_submit + fence wait in flush_locked.
for &out_idx in out_idxs {
if let Some(&out_buf) = bufs.get(out_idx) {
s.written_since_barrier.insert(out_buf);
}
}
s.n += 1;
s.work += u64::from(groups.0) * u64::from(groups.1) * u64::from(groups.2);
if profile {
if let Some(t0) = rec_t0 {
s.record_ns += t0.elapsed().as_nanos();
}
}
// Flush when EITHER bound trips: BATCH_CAP caps the descriptor-set budget (dispatch
// COUNT); work_cap caps the SUBMISSION TIME (dispatched workgroups) so a single queue
// submission stays under the driver ring timeout. The count bound alone doesn't help a
// long prefill -- its op count matches a short one, but each op does O(seq²)/O(seq) more
// work, so only the work bound splits its one oversized submission into safe pieces.
// Never mid-capture: a graph must record whole into one command buffer (a partial submit
// would tear the replayable forward in two). Cross-batch ordering after a flush is the
// queue_submit + fence wait, so splitting here is bit-identical to one big submission.
if (s.n >= BATCH_CAP
|| s.work >= self.inner.work_cap.load(std::sync::atomic::Ordering::Relaxed))
&& !s.capturing
{
flush_locked(dev, queue, &mut s, profile, self.inner.gpu_profile, self.inner.timestamp_period, self.inner.roofline, f32::from_bits(self.inner.peak_bw_gbps.load(std::sync::atomic::Ordering::Relaxed)))?;
drop(s);
self.reclaim();
}
}
Ok(())
}
// Submit and await any pending recorded dispatches. Must be called before the host maps a
// buffer (readback) or relies on prior GPU work having completed.
fn flush(&self) -> Result<()> {
let dev = self.dev();
let queue = self.inner.queue;
let profile = self.inner.profile;
{
let mut s = self.inner.submitter.lock().unwrap();
flush_locked(dev, queue, &mut s, profile, self.inner.gpu_profile, self.inner.timestamp_period, self.inner.roofline, f32::from_bits(self.inner.peak_bw_gbps.load(std::sync::atomic::Ordering::Relaxed)))?;
}
self.reclaim();
Ok(())
}
/// Retune the per-submission workgroup bound (see [`WORK_CAP`]). `0` disables the bound, restoring
/// the single-submission-per-forward path. Used for A/B tuning and by the long-prefill test.
pub fn set_work_cap(&self, cap: u64) {
let cap = if cap == 0 { u64::MAX } else { cap };
self.inner
.work_cap
.store(cap, std::sync::atomic::Ordering::Relaxed);
}
/// Cumulative queue submissions on this device (one per flush). A forward that stays under both
/// bounds submits once; a long prefill submits several times once the work bound splits it.
pub fn submit_count(&self) -> u64 {
self.inner.submitter.lock().unwrap().submits
}
/// Begin capturing every subsequent dispatch into a dedicated, re-submittable command buffer --
/// the decode command-graph. Any pending eager batch is flushed first so the capture buffer starts
/// clean and independent. While the capture is in flight, `dispatch_outs` records into it and never
/// auto-flushes, and the buffer pool reserves (never recycles) every intermediate the capture
/// touches, so a later replay reads/writes stable storage instead of aliasing live tensors.
///
/// Requires `VK_KHR_push_descriptor`: buffer handles are then pushed inline into the command
/// buffer, so no per-dispatch descriptor set has to remain live for the replay. Without it the
/// legacy path would allocate descriptor sets whose pool is reset every batch -- unsound to
/// replay -- so we return an error and the caller stays on the (correct) eager path.
pub fn begin_graph_capture(&self) -> Result<()> {
if self.inner.push_descriptor.is_none() {
crate::bail!("vulkan graph capture requires VK_KHR_push_descriptor");
}
// Drain recorded-but-unsubmitted eager work; the capture buffer must be independent of it.
self.flush()?;
let dev = self.dev();
let cmd = {
let mut s = self.inner.submitter.lock().unwrap();
if s.capturing {
crate::bail!("vulkan graph capture already in flight");
}
// A fresh primary command buffer owned by this capture (freed on VkGraph drop). Begun with
// DEFAULT flags -- explicitly NOT ONE_TIME_SUBMIT -- so it is legal to re-submit per token.
let cmd = unsafe {
dev.allocate_command_buffers(
&vk::CommandBufferAllocateInfo::default()
.command_pool(s.cpool)
.level(vk::CommandBufferLevel::PRIMARY)
.command_buffer_count(1),
)
.map_err(vkerr)?[0]
};
unsafe {
dev.begin_command_buffer(cmd, &vk::CommandBufferBeginInfo::default())
.map_err(vkerr)?;
}
s.graph_cmd = cmd;
s.capturing = true;
s.written_since_barrier.clear();
s.n = 0;
s.barriers = 0;
cmd
};
// Reserve every buffer the capture touches until the graph is torn down (see BufPool).
self.inner.bufpool.lock().unwrap().capture_depth += 1;
let _ = cmd;
Ok(())
}
/// End the in-flight capture and return the replayable decode graph. The command buffer is closed
/// (never submitted here); the caller replays it per token via [`VkGraph::replay`] after refreshing
/// the stable input buffers in place. Mirrors `RocmGraphHandle::end_capture`.
pub fn end_graph_capture(&self) -> Result<VkGraph> {
let dev = self.dev();
let (cmd, n) = {
let mut s = self.inner.submitter.lock().unwrap();
if !s.capturing {
crate::bail!("vulkan graph end_capture with no capture in flight");
}
let cmd = s.graph_cmd;
let n = s.n;
unsafe {
dev.end_command_buffer(cmd).map_err(vkerr)?;
}
s.capturing = false;
s.graph_cmd = vk::CommandBuffer::null();
s.n = 0;
s.written_since_barrier.clear();
(cmd, n)
};
// This capture era's reservations transfer to the graph (captures never nest: begin bails on
// an in-flight capture), so tearing the graph down returns exactly its own working set.
let reserved = {
let mut pool = self.inner.bufpool.lock().unwrap();
pool.capture_depth -= 1;
std::mem::take(&mut pool.reserved)
};
// A dedicated fence per graph so replays of distinct graphs never contend on one fence.
let fence = unsafe {
dev.create_fence(&vk::FenceCreateInfo::default(), None)
.map_err(vkerr)?
};
Ok(VkGraph {
cmd,
fence,
n_dispatch: n,
device: self.clone(),
reserved,
})
}
/// Abort an in-flight capture without producing a graph (used when the forward errors mid-capture).
/// Closes and frees the capture buffer and releases the pool reservation so eager decode resumes.
pub fn abort_graph_capture(&self) {
let dev = self.dev();
let mut s = self.inner.submitter.lock().unwrap();
if !s.capturing {
return;
}
let cmd = s.graph_cmd;
unsafe {
let _ = dev.end_command_buffer(cmd);
dev.free_command_buffers(s.cpool, &[cmd]);
}
s.capturing = false;
s.graph_cmd = vk::CommandBuffer::null();
s.n = 0;
s.written_since_barrier.clear();
drop(s);
let mut pool = self.inner.bufpool.lock().unwrap();
pool.capture_depth = pool.capture_depth.saturating_sub(1);
// Nothing was baked (the capture buffer was freed unsubmitted), so this era's reservations
// are plain droppable transients again; return them for post-fence reclaim.
let mut reserved = std::mem::take(&mut pool.reserved);
pool.pending.append(&mut reserved);
}
// Move buffers dropped before this point into the reuse pool. Sound only right after a
// flush+fence: the awaited batch (the last that could reference them) is done on the GPU -- which
// is also why it's safe to actually destroy buffers here when over the cap (no in-flight work can
// still reference a free-list entry).
fn reclaim(&self) {
let dev = self.dev();
let mut pool = self.inner.bufpool.lock().unwrap();
let pending = std::mem::take(&mut pool.pending);
for (bytes, p) in pending {
pool.free.entry(bytes).or_default().push(p);
pool.free_bytes += bytes;
}
// Enforce the idle-pool cap: destroy real device buffers (largest buckets first, since those
// dominate the bytes and are the least likely to be reused) until back under the cap. The cap
// is sized to the device's memory, not a fixed constant: the prefill working set (activations +
// attention scratch) scales with model/seq, and on a large UMA APU it exceeds a small fixed cap.
// A too-small cap evicts the cycling working set, so every forward re-allocates it fresh through
// the amdgpu kernel driver (bo_alloc) -- pure CPU/ioctl waste on the shared memory bus that a
// retained pool avoids. Measured on gfx1151: with a 12 GiB cap a 512-tok prefill re-allocated
// ~1.8 GB/forward (pool_fresh>0); heap-relative it re-allocates nothing (pool_fresh==0).
let cap = self.pool_free_cap();
while pool.free_bytes > cap {
let Some(&bucket) = pool.free.keys().max() else {
break;
};
let Some(bufs) = pool.free.get_mut(&bucket) else {
break;
};
let Some(p) = bufs.pop() else {
pool.free.remove(&bucket);
continue;
};
if bufs.is_empty() {
pool.free.remove(&bucket);
}
pool.free_bytes = pool.free_bytes.saturating_sub(bucket);
unsafe {
dev.destroy_buffer(p.buffer, None);
dev.free_memory(p.memory, None);
}
}
}
// Idle buffer-pool cap, sized to the device rather than a fixed constant. Returns 60% of the
// largest DEVICE_LOCAL heap, floored at the legacy `POOL_FREE_CAP_BYTES` so constrained GPUs keep
// their prior behaviour and only large-memory (UMA) devices raise it. The pool never holds more
// than the workload actually allocates and frees; this cap only decides when idle buffers are
// destroyed, so a generous value on a big-memory box merely retains the reusable working set.
fn pool_free_cap(&self) -> u64 {
// Explicit override (GiB) for A/B measurement and constrained deployments.
if let Some(gb) = std::env::var("HANZO_VK_POOL_CAP_GB").ok().and_then(|s| s.parse::<u64>().ok()) {
return gb * 1024 * 1024 * 1024;
}
let mp = &self.inner.mem_props;
let mut biggest_device_local = 0u64;
for h in 0..mp.memory_heap_count as usize {
let heap = mp.memory_heaps[h];
if heap.flags.contains(vk::MemoryHeapFlags::DEVICE_LOCAL) {
biggest_device_local = biggest_device_local.max(heap.size);
}
}
(biggest_device_local / 5 * 3).max(POOL_FREE_CAP_BYTES)
}
// Allocate an f32 storage holding `count` elements (uninitialized device memory).
fn alloc_f32(&self, count: usize) -> Result<VulkanStorage> {
let (buffer, memory, host_visible) = unsafe { self.raw_buffer((count * 4) as u64)? };
Ok(VulkanStorage {
buffer,
memory,
count,
dtype: DType::F32,
host_visible,
device: self.clone(),
})
}
// fp16 scratch (2 bytes/elem) for coopmat matmul inputs. Returns the buffer plus its memory,
// host-visibility, and byte size so the caller can return it to the pool via free_scratch.
fn alloc_f16(&self, count: usize) -> Result<(vk::Buffer, vk::DeviceMemory, bool, u64)> {
let bytes = ((count * 2).max(4)) as u64;
let (buffer, memory, host_visible) = unsafe { self.raw_buffer(bytes)? };
Ok((buffer, memory, host_visible, bytes))
}
// Return a scratch buffer to the pool. Deferred-safe like VulkanStorage::drop: it parks in
// `pending` and is reclaimed only after the next flush+fence, by which point the dispatch that
// referenced it has completed on the GPU.
fn free_scratch(
&self,
bytes: u64,
buffer: vk::Buffer,
memory: vk::DeviceMemory,
host_visible: bool,
) {
// Park under the bucket key, matching the size raw_buffer allocated and looks up.
let bytes = pool_bucket(bytes);
if let Ok(mut pool) = self.inner.bufpool.lock() {
let pooled = PooledBuf {
buffer,
memory,
host_visible,
};
// Reserve rather than recycle while a graph capture is in flight (see VulkanStorage::drop).
if pool.capture_depth > 0 {
pool.reserved.push((bytes, pooled));
} else {
pool.pending.push((bytes, pooled));
}
}
}
pub(crate) fn upload_f32(&self, data: &[f32]) -> Result<VulkanStorage> {
let s = self.alloc_f32(data.len())?;
unsafe { self.write_f32(s.buffer, s.memory, s.host_visible, data)? };
Ok(s)
}
// u32 storage (ids/cond). 4 bytes/elem, same as f32.
fn alloc_u32(&self, count: usize) -> Result<VulkanStorage> {
let (buffer, memory, host_visible) = unsafe { self.raw_buffer((count * 4) as u64)? };
Ok(VulkanStorage {
buffer,
memory,
count,
dtype: DType::U32,
host_visible,
device: self.clone(),
})
}
fn upload_u32(&self, data: &[u32]) -> Result<VulkanStorage> {
let s = self.alloc_u32(data.len())?;
unsafe { self.write_u32(s.buffer, s.memory, s.host_visible, data)? };
Ok(s)
}
/// Upload raw GGML quantized weight bytes to the GPU VERBATIM (no requantize, no re-pack), as a
/// `uint w[]` buffer the native-GGML quant kernels (`mul_mat_vec_q4_0`/`q8_0`, `mul_mat_vec_q4k`,
/// `moe_matvec_q4_0`/`q4k`) byte-address straight out of. This is the decode/MoE-bank weight path:
/// the bytes stay quantized in VRAM and the in-shader decode matches the CPU `BlockQ*::to_float`
/// exactly. `data` is a packed run of GGML blocks (e.g. 18 B Q4_0, 34 B Q8_0, 144 B Q4_K); its
/// length need not be a u32 multiple (an 18 B Q4_0 row is not), so the buffer is rounded up to the
/// next u32 and the trailing pad bytes are never read (every kernel bounds its block walk by `k`).
pub fn upload_qweight(&self, data: &[u8]) -> Result<VulkanStorage> {
// Round the byte length up to a whole u32; copy the bytes into the (possibly 1-3 B larger)
// word buffer so a non-4-aligned block run (Q4_0=18 B, Q8_0=34 B, Q6_K=210 B) is legal.
let nwords = data.len().div_ceil(4);
let mut words = vec![0u32; nwords.max(1)];
let dst: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(words.as_mut_ptr() as *mut u8, nwords * 4) };
dst[..data.len()].copy_from_slice(data);
self.upload_u32(&words)
}
/// Upload a slice of u32 routing ids (one expert id per routed MoE slot) to a device buffer,
/// consumed by the fused `moe_matvec_*` kernels' `Ids` binding (never read back to host).
pub fn upload_ids(&self, ids: &[u32]) -> Result<VulkanStorage> {
self.upload_u32(ids)
}
// Name a CPU-fallback round-trip when VK_PROFILE is set. Each fallback op reads its
// operand(s) back to the host, computes on the (UNtimed) CPU, and re-uploads -- a hidden
// bottleneck the size-only readback log can't attribute to an op. This names the culprit so a
// GPU re-run can prioritize which op to port native next. Zero-cost when profiling is off (the
// call is behind the `profile` bool and `op`/`extra` are cheap &str / Display formatting only
// evaluated on the slow path). `op` is the op name; `extra` is a shape/size descriptor.
#[inline]
fn profile_fallback(&self, op: &str, extra: std::fmt::Arguments<'_>) {
if self.inner.profile {
eprintln!("[VK_PROFILE] cpu-fallback op={op} {extra} (GPU->CPU->GPU round-trip)");
}
}
}
fn vkerr(e: vk::Result) -> Error {
Error::Msg(format!("vulkan: {e:?}"))
}
// Max dispatches recorded into one command buffer before an automatic flush. Bounds the
// descriptor-set pool; this is large enough that a typical transformer forward submits once
// (or a few times), turning hundreds of per-op fence stalls into a handful.
const BATCH_CAP: u32 = 4096;
// Max workgroups (Σ grid x·y·z) recorded into one command buffer before an automatic flush, so one
// queue submission's on-GPU runtime stays well under the driver ring lockup timeout (RADV/amdgpu
// default 2 s) AND its live intermediates are reclaimed+reused every few layers instead of held for
// the whole forward. Where BATCH_CAP bounds dispatch COUNT (descriptor budget), this bounds dispatched
// WORK: a long prefill's op count matches a short one, but each attention/matmul dispatch does
// O(seq²)/O(seq) more work, so only a work bound splits its one oversized submission -- and caps the
// peak-live buffer footprint that otherwise grows with seq. A decode / short-prefill forward stays
// well under it (no measurable fast-path cost); a 2k-4k-token prefill crosses it into ring-safe pieces.
// Calibrated on gfx1151 for a submission an order of magnitude under the ring timeout with margin for
// slower per-workgroup kernels and GPU contention. VK_WORK_CAP overrides it (0 disables the bound).
const WORK_CAP: u64 = 12_000_000;
// Per-flush buffer-pool fresh-alloc vs reuse tally (printed under VK_PROFILE). Fresh allocations go
// through the amdgpu kernel driver (bo_alloc) and their memory traffic contends with in-flight GPU
// GEMMs on a UMA APU; a rising `pool_fresh` is the signature of the idle-pool cap evicting a working
// set that is about to be reused. Relaxed atomics: a coarse per-batch counter, never a correctness gate.
static POOL_FRESH: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
static POOL_HIT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
static POOL_FRESH_BYTES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
// Exact moved-bytes + FLOPs for one matmul dispatch, from its push shape [m0, mcount, nout, k, woff].
// Returns (cold_weight_bytes, total_global_bytes, flops); a non-matmul or unknown kernel returns
// (0,0,0) and is dropped from the roofline table. ONE place owns every kernel's byte/FLOP model --
// the Q-block size, the tile BM/BN (hence the cold weight re-read factor ceil(m/BM), one full weight
// pass per row-tile band), and the activation element width -- so the achieved-rate columns are exact
// rather than eyeballed. `wbytes` is the cold-GTT weight traffic that must approach peak BW; the
// activation is re-read once per column tile (ceil(nout/BN)) but is MALL-resident, so it lands in
// `tbytes` (all global traffic) not `wbytes`. Output is written once.
fn roofline_model(name: &str, p: &[u32; 5]) -> (u64, u64, u64) {
let (m, nout, k) = (p[1] as u64, p[2] as u64, p[3] as u64);
if m == 0 || nout == 0 || k == 0 {
return (0, 0, 0);
}
// (block bytes per 256-elem superblock, activation element bytes, BM, BN)
let (blk, act, bm, bn): (u64, u64, u64, u64) = match name {
"mul_mm_q4k_coopmat" => (144, 4, 128, 128), // f16 coopmat, x is f32
"mul_mm_q4k_tiled_dp4a" => (144, 1, 64, 64), // int8-dp4a tile, x quantized to q8
"mul_mm_q4k_tiled" => (144, 4, 64, 64), // f32 tile
"mul_mm_q6k_tiled_dp4a" => (212, 1, 64, 64), // Q6_K block padded to 212 B
"mul_mm_q6k_coopmat" => (212, 4, 128, 128), // Q6_K f16 coopmat, x is f32
_ => return (0, 0, 0),
};
let blocks = k.div_ceil(256);
let reread = m.div_ceil(bm);
let wbytes = nout * blocks * blk * reread;
let abytes = nout.div_ceil(bn) * m * k * act;
let obytes = m * nout * 4;
(wbytes, wbytes + abytes + obytes, 2 * m * nout * k)
}
// End, submit, and block on the current command batch, then mark the submitter idle. A no-op
// when nothing is recorded. Caller must hold the submitter lock. When `profile` is set, prints the
// per-batch phase breakdown (recording / submit / fence-wait time, dispatch + barrier counts) so
// the real GPU shows where per-token milliseconds go; the timers are only sampled when profiling.
fn flush_locked(
dev: &ash::Device,
queue: vk::Queue,
s: &mut Submitter,
profile: bool,
gpu_profile: bool,
timestamp_period: f32,
roofline: bool,
peak_bw_gbps: f32,
) -> Result<()> {
if !s.recording {
return Ok(());
}
let n = s.n;
let barriers = s.barriers;
let record_ms = s.record_ns as f64 / 1e6;
let (mut submit_ms, mut wait_ms) = (0.0f64, 0.0f64);
unsafe {
dev.end_command_buffer(s.cmd).map_err(vkerr)?;
dev.reset_fences(&[s.fence]).map_err(vkerr)?;
let cmds = [s.cmd];
let t_sub = profile.then(std::time::Instant::now);
dev.queue_submit(
queue,
&[vk::SubmitInfo::default().command_buffers(&cmds)],
s.fence,
)
.map_err(vkerr)?;
if let Some(t) = t_sub {
submit_ms = t.elapsed().as_secs_f64() * 1e3;
}
let t_wait = profile.then(std::time::Instant::now);
dev.wait_for_fences(&[s.fence], true, u64::MAX)
.map_err(vkerr)?;
if let Some(t) = t_wait {
wait_ms = t.elapsed().as_secs_f64() * 1e3;
}
}
s.submits += 1;
if profile {
// One line per submitted batch. On decode this is ~one batch per token, so this is the
// per-token GPU breakdown: `dispatch` = ops recorded, `barriers` = memory barriers emitted
// (lower is better -- with selective barriers, independent ops in a row emit none).
let fresh = POOL_FRESH.swap(0, std::sync::atomic::Ordering::Relaxed);
let hit = POOL_HIT.swap(0, std::sync::atomic::Ordering::Relaxed);
let fresh_mb = POOL_FRESH_BYTES.swap(0, std::sync::atomic::Ordering::Relaxed) as f64 / 1e6;
eprintln!(
"[VK_PROFILE] flush: dispatch={n} workgroups={work} barriers={barriers} \
record={record_ms:.3}ms submit={submit_ms:.3}ms fence_wait={wait_ms:.3}ms \
pool_fresh={fresh} pool_hit={hit} fresh_mb={fresh_mb:.1}",
work = s.work,
);
}
if gpu_profile && !s.op_names.is_empty() {
// Fence is signalled -> timestamps are ready. Read [0, op_names.len()] and turn consecutive
// deltas into per-op GPU nanoseconds, then aggregate by kernel name. This is the measurement
// that ranks dispatches by ACTUAL cost, not count (removing cheap-but-numerous ops is
// weightless -- proven by the router fusion). Top rows name the fusion targets.
let cnt = s.op_names.len() + 1;
let mut ts = vec![0u64; cnt];
let read = unsafe {
dev.get_query_pool_results(s.qpool, 0, &mut ts, vk::QueryResultFlags::TYPE_64)
};
if read.is_ok() {
let mut agg: std::collections::HashMap<&'static str, (u32, u128)> =
std::collections::HashMap::new();
for (i, &nm) in s.op_names.iter().enumerate() {
let dt = ts[i + 1].saturating_sub(ts[i]);
let ns = (dt as f64 * timestamp_period as f64) as u128;
let e = agg.entry(nm).or_insert((0, 0));
e.0 += 1;
e.1 += ns;
}
let mut rows: Vec<_> = agg.into_iter().collect();
rows.sort_by(|a, b| b.1 .1.cmp(&a.1 .1));
let total: u128 = rows.iter().map(|r| r.1 .1).sum();
eprintln!(
"[VK_GPU] batch total={:.3}ms over {} dispatches, {} barriers -- by GPU time:",
total as f64 / 1e6,
s.op_names.len(),
barriers,
);
for (nm, (c, ns)) in rows.iter().take(20) {
eprintln!(
"[VK_GPU] {:<28} {:>8.3}ms n={:<5} avg={:>6.1}us {:>5.1}%",
nm,
*ns as f64 / 1e6,
c,
*ns as f64 / 1e3 / (*c).max(1) as f64,
*ns as f64 / total.max(1) as f64 * 100.0,
);
}
if roofline {
// Join each dispatch's exact moved-bytes + FLOPs (from its push shape) to the measured
// GPU ns, aggregate by kernel, and print achieved rates. `wbytes` = cold weight bytes
// (Q-block size x re-read factor) -- the number that must approach `peak_bw_gbps`;
// `tbytes` = all global traffic (weight + activation + output); `flops` = 2*m*n*k.
// A kernel far below BOTH peak-BW and peak-FLOP is occupancy/latency-bound, not
// bandwidth- or compute-bound -- that distinction is the whole point of this table.
let mut ragg: std::collections::HashMap<&'static str, (u32, u128, u128, u128, u128)> =
std::collections::HashMap::new();
for (i, &nm) in s.op_names.iter().enumerate() {
let dt = ts[i + 1].saturating_sub(ts[i]);
let ns = (dt as f64 * timestamp_period as f64) as u128;
let p5 = s.op_push.get(i).copied().unwrap_or([0u32; 5]);
let (wb, tb, fl) = roofline_model(nm, &p5);
let e = ragg.entry(nm).or_insert((0, 0, 0, 0, 0));
e.0 += 1;
e.1 += ns;
e.2 += wb as u128;
e.3 += tb as u128;
e.4 += fl as u128;
}
let mut rr: Vec<_> = ragg.into_iter().filter(|r| r.1 .4 > 0).collect();
rr.sort_by(|a, b| b.1 .1.cmp(&a.1 .1));
eprintln!(
"[VK_ROOF] peak_bw={:.0}GB/s (measured d2d) -- achieved per matmul kernel:",
peak_bw_gbps,
);
for (nm, (c, ns, wb, tb, fl)) in rr.iter() {
let secs = *ns as f64 / 1e9;
let w_gbps = *wb as f64 / 1e9 / secs.max(1e-12);
let t_gbps = *tb as f64 / 1e9 / secs.max(1e-12);
let gflops = *fl as f64 / 1e9 / secs.max(1e-12);
let pct = if peak_bw_gbps > 0.0 { w_gbps / peak_bw_gbps as f64 * 100.0 } else { 0.0 };
eprintln!(
"[VK_ROOF] {:<24} n={:<4} {:>7.2}ms wt={:>6.1}GB/s({:>4.1}% peak) tot={:>6.1}GB/s {:>7.0}GFLOP/s",
nm, c, *ns as f64 / 1e6, w_gbps, pct, t_gbps, gflops,
);
}
}
}
s.op_push.clear();
s.op_names.clear();
}
s.recording = false;
s.n = 0;
Ok(())
}
// push-constant helpers (std430 scalar layout: tightly packed u32/f32).
fn push_u32(v: &[u32]) -> Vec<u8> {
let mut b = Vec::with_capacity(v.len() * 4);
for x in v {
b.extend_from_slice(&x.to_ne_bytes());
}
b
}
const WG1D: u32 = 64;
// Activation rows decoded per invocation in the quantized prefill matmul kernels (mul_mat_q8 /
// mul_mat_q4k). MUST equal the MAX_M const in those .comp shaders (their register accumulator array
// bound). The host tiles the M dimension by this so each weight block is still read once per output
// column across a tile of up to MATMUL_Q_MAX_M rows.
const MATMUL_Q_MAX_M: usize = 8;
// Compile-time per-invocation array bounds in gdn_step.comp / gdn_conv1d_step.comp (MAX_K #defines).
// head_k_dim must be <= GDN_STEP_MAX_K and the conv kernel <= GDN_CONV_MAX_K; both checked host-side.
const GDN_STEP_MAX_K: usize = 256;
const GDN_CONV_MAX_K: usize = 8;
// Compile-time MAX_COLS_PAD bound on argsort.comp's shared index scratch. cols_pad (next pow2 of the
// sorted dim) must be <= ARGSORT_MAX_COLS_PAD; wider rows fall back to the CPU argsort (not on the
// MoE routing hot path, where cols == num_experts ~128).
const ARGSORT_MAX_COLS_PAD: usize = 1024;
impl BackendDevice for VulkanDevice {
type Storage = VulkanStorage;
fn new(ordinal: usize) -> Result<Self> {
unsafe {
let entry = ash::Entry::load()
.map_err(|e| Error::Msg(format!("vulkan: loader not found: {e}")))?;
let app = vk::ApplicationInfo::default().api_version(vk::make_api_version(0, 1, 3, 0));
let instance = entry
.create_instance(
&vk::InstanceCreateInfo::default().application_info(&app),
None,
)
.map_err(vkerr)?;
// Collect non-CPU adapters in enumeration order; pick the `ordinal`-th (like the probe).
let mut gpus = Vec::new();
for pd in instance.enumerate_physical_devices().map_err(vkerr)? {
let p = instance.get_physical_device_properties(pd);
let name = CStr::from_ptr(p.device_name.as_ptr())
.to_string_lossy()
.into_owned();
let is_cpu = p.device_type == vk::PhysicalDeviceType::CPU
|| name.to_lowercase().contains("llvmpipe");
if !is_cpu {
gpus.push(pd);
}
}
if gpus.is_empty() {
instance.destroy_instance(None);
return Err(Error::Msg("vulkan: no non-CPU Vulkan device".into()));
}
let pdev = *gpus
.get(ordinal)
.ok_or_else(|| Error::Msg(format!("vulkan: no device at ordinal {ordinal}")))?;
let qfi = instance
.get_physical_device_queue_family_properties(pdev)
.iter()
.position(|q| q.queue_flags.contains(vk::QueueFlags::COMPUTE))
.ok_or_else(|| Error::Msg("vulkan: no compute queue".into()))?
as u32;
let prios = [1.0f32];
let qci = [vk::DeviceQueueCreateInfo::default()
.queue_family_index(qfi)
.queue_priorities(&prios)];
// Probe cooperative-matrix support: need the device extension AND a config with
// fp16 A/B, fp32 C/result at subgroup scope. Guard the query on the extension being
// advertised (the loader's fn pointer is only valid then) so WSL/Dozen stays on the
// plain tiled path.
let dev_exts = instance
.enumerate_device_extension_properties(pdev)
.unwrap_or_default();
let has_cm_ext = dev_exts.iter().any(|e| {
CStr::from_ptr(e.extension_name.as_ptr()) == ash::khr::cooperative_matrix::NAME
});
let cm_mnk = if has_cm_ext {
let cm = ash::khr::cooperative_matrix::Instance::new(&entry, &instance);
cm.get_physical_device_cooperative_matrix_properties(pdev)
.ok()
.and_then(|props| {
props.into_iter().find(|p| {
p.a_type == vk::ComponentTypeKHR::FLOAT16
&& p.b_type == vk::ComponentTypeKHR::FLOAT16
&& p.c_type == vk::ComponentTypeKHR::FLOAT32
&& p.result_type == vk::ComponentTypeKHR::FLOAT32
&& p.scope == vk::ScopeKHR::SUBGROUP
})
})
.map(|p| (p.m_size, p.n_size, p.k_size))
} else {
None
};
let coopmat = cm_mnk.is_some();
let cm_mnk = cm_mnk.unwrap_or((0, 0, 0));
// Default ON when the device advertises coopmat: the register-blocked kernel
// (bmm_coopmat_rb) measured 1.3-2.7x over the fp32 bmm_reg on the real AMD driver and a
// full Qwen3-0.6B forward's argmax matched CPU exactly (fp16 inputs, fp32 accumulate).
// Set VK_COOPMAT=0 to force the fp32 path (e.g. if precision matters).
let cm_use = coopmat
&& std::env::var("VK_COOPMAT")
.map(|v| v != "0")
.unwrap_or(true);
// VK_KHR_push_descriptor: lets `dispatch` push buffer handles inline into the command
// buffer (vkCmdPushDescriptorSetKHR) instead of allocating + updating + binding a
// descriptor set per op. That per-op churn is the dominant CPU cost on the decode hot
// path (same op graph, hundreds of dispatches x 28 layers, re-recorded every token), so
// collapsing it to one recorded command is the lever. Enabled when advertised (native
// AMD/NV; typically absent on WSL/Dozen, which keeps the legacy path). VK_PUSH_DESC=0
// forces the legacy path. The extension's guaranteed maxPushDescriptors >= 32 dwarfs our
// widest kernel (4 storage buffers), so no per-pipeline limit check is needed.
let has_pd_ext = dev_exts.iter().any(|e| {
CStr::from_ptr(e.extension_name.as_ptr()) == ash::khr::push_descriptor::NAME
});
let use_pd = has_pd_ext
&& std::env::var("VK_PUSH_DESC")
.map(|v| v != "0")
.unwrap_or(true);
// Buffer-memory placement policy. Default `auto`: host-visible when it fits, else spill
// big buffers (e.g. an 18.6GB model's weights) to the largest DEVICE_LOCAL heap (the GTT
// pool on this UMA APU), which is where the real capacity lives. `host_only` restores the
// legacy host-visible-only behaviour; `device_first` forces big-heap placement always.
let mem_strategy = match std::env::var("VK_DEVICE_MEMORY_STRATEGY")
.ok()
.as_deref()
.map(str::trim)
{
Some("host_only") | Some("host") => MemStrategy::HostOnly,
Some("device_first") | Some("device") => MemStrategy::DeviceFirst,
Some("auto") | None | Some("") => MemStrategy::Auto,
Some(other) => {
eprintln!(
"[vulkan] unknown VK_DEVICE_MEMORY_STRATEGY=`{other}` (expected host_only|device_first|auto); using auto"
);
MemStrategy::Auto
}
};
// VK_EXT_memory_budget: enables querying per-heap *free* bytes at runtime (the scratch
// guard needs this; the static heap `size` is total capacity only). Enable it when the
// device advertises it; otherwise the guard conservatively falls back to total size.
let has_mem_budget = dev_exts.iter().any(|e| {
CStr::from_ptr(e.extension_name.as_ptr()) == ash::ext::memory_budget::NAME
});
// Subgroup capability (core in Vulkan 1.1+, which the 1.3 instance guarantees). The q8
// mat-vec subgroup kernel uses subgroupAdd (ARITHMETIC) at COMPUTE-stage scope, so we
// require both before enabling it. VK_SUBGROUP_MATVEC=0 forces the scalar kernel.
let mut sg_props = vk::PhysicalDeviceSubgroupProperties::default();
{
// p2 mutably borrows sg_props via push_next; scope it so the borrow ends before we
// read sg_props back below.
let mut p2 = vk::PhysicalDeviceProperties2::default().push_next(&mut sg_props);
instance.get_physical_device_properties2(pdev, &mut p2);
}
let sg_compute = sg_props
.supported_stages
.contains(vk::ShaderStageFlags::COMPUTE);
let sg_arith = sg_props
.supported_operations
.contains(vk::SubgroupFeatureFlags::BASIC | vk::SubgroupFeatureFlags::ARITHMETIC);
// subgroupElect needs BASIC; subgroupAdd needs ARITHMETIC. Require a sane width (>=2)
// so the reduction is actually parallel.
let subgroup_size = sg_props.subgroup_size;
let subgroup_matvec = sg_compute
&& sg_arith
&& subgroup_size >= 2
&& std::env::var("VK_SUBGROUP_MATVEC")
.map(|v| v != "0")
.unwrap_or(true);
// Integer dot-product (OpSDotAccSat 4x8) feature: gates the int8 dp4a prefill GEMM. Core in
// Vulkan 1.3 but optional, so query it; the dp4a kernels declare the SPIR-V capability and
// only validate where this is true. VK_INT_DOT=0 forces the f32 2D-tile path instead.
let mut idot_feat = vk::PhysicalDeviceShaderIntegerDotProductFeatures::default();
{
let mut f2 = vk::PhysicalDeviceFeatures2::default().push_next(&mut idot_feat);
instance.get_physical_device_features2(pdev, &mut f2);
}
let int_dot8 = idot_feat.shader_integer_dot_product != 0
&& std::env::var("VK_INT_DOT")
.map(|v| v != "0")
.unwrap_or(true);
// Build the enabled-extension list dynamically: coopmat and push_descriptor are
// independent and either may be present. push_descriptor needs no extra device feature
// struct (just the extension + a fn-pointer load), so it's a bare name here.
let mut ext_names: Vec<*const std::os::raw::c_char> = Vec::new();
if coopmat {
ext_names.push(ash::khr::cooperative_matrix::NAME.as_ptr());
}
if use_pd {
ext_names.push(ash::khr::push_descriptor::NAME.as_ptr());
}
if has_mem_budget {
ext_names.push(ash::ext::memory_budget::NAME.as_ptr());
}
// Coopmat's SPIR-V needs these features (cooperative matrix, Vulkan memory model, fp16
// arithmetic, 16-bit storage); push_descriptor needs none.
let mut cm_feat =
vk::PhysicalDeviceCooperativeMatrixFeaturesKHR::default().cooperative_matrix(true);
let mut mm_feat =
vk::PhysicalDeviceVulkanMemoryModelFeatures::default().vulkan_memory_model(true);
let mut f16_feat =
vk::PhysicalDeviceShaderFloat16Int8Features::default().shader_float16(true);
let mut s16_feat =
vk::PhysicalDevice16BitStorageFeatures::default().storage_buffer16_bit_access(true);
let mut dci = vk::DeviceCreateInfo::default().queue_create_infos(&qci);
if !ext_names.is_empty() {
dci = dci.enabled_extension_names(&ext_names);
}
if coopmat {
dci = dci
.push_next(&mut cm_feat)
.push_next(&mut mm_feat)
.push_next(&mut f16_feat)
.push_next(&mut s16_feat);
}
let device = instance.create_device(pdev, &dci, None).map_err(vkerr)?;
// Load push_descriptor device fns now that the device exists with the extension enabled.
let push_descriptor =
use_pd.then(|| ash::khr::push_descriptor::Device::new(&instance, &device));
let queue = device.get_device_queue(qfi, 0);
let mem_props = instance.get_physical_device_memory_properties(pdev);
// Persistent submit resources. RESET_COMMAND_BUFFER lets us re-record `cmd` each
// dispatch; the descriptor pool holds enough STORAGE_BUFFER slots for the widest
// kernel (where_cond/index_select use 4) and is reset (1 set freed) per dispatch.
let cpool = device
.create_command_pool(
&vk::CommandPoolCreateInfo::default()
.queue_family_index(qfi)
.flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER),
None,
)
.map_err(vkerr)?;
let cmd = device
.allocate_command_buffers(
&vk::CommandBufferAllocateInfo::default()
.command_pool(cpool)
.level(vk::CommandBufferLevel::PRIMARY)
.command_buffer_count(1),
)
.map_err(vkerr)?[0];
let fence = device
.create_fence(&vk::FenceCreateInfo::default(), None)
.map_err(vkerr)?;
// One TIMESTAMP query per dispatch in a batch (BATCH_CAP) + a baseline at index 0, for
// per-op GPU timing under VK_PROFILE_GPU. Created unconditionally (cheap: (BATCH_CAP+1)*8
// bytes); only written when gpu_profile is on.
let qpool = device
.create_query_pool(
&vk::QueryPoolCreateInfo::default()
.query_type(vk::QueryType::TIMESTAMP)
.query_count(BATCH_CAP + 1),
None,
)
.map_err(vkerr)?;
// Sized for a whole batch: one descriptor set per recorded dispatch (up to
// BATCH_CAP), each binding up to MAX_BINDINGS (10) storage buffers. The widest kernels
// are the GDN mixers (gdn_recurrence/gdn_chunked bind 7: q,k,v,g,beta,state,out); sizing
// at 4 starved the pool on the legacy (non-push-descriptor: Dozen/WSL) allocate-set path,
// which drew STORAGE_BUFFER descriptors here and hit OUT_OF_POOL_MEMORY. MAX_BINDINGS
// matches the per-dispatch bind cap in `dispatch_outs`, so no kernel can exceed it.
const MAX_BINDINGS: u32 = 10;
let dpool_sizes = [vk::DescriptorPoolSize::default()
.ty(vk::DescriptorType::STORAGE_BUFFER)
.descriptor_count(BATCH_CAP * MAX_BINDINGS)];
let dpool = device
.create_descriptor_pool(
&vk::DescriptorPoolCreateInfo::default()
.pool_sizes(&dpool_sizes)
.max_sets(BATCH_CAP),
None,
)
.map_err(vkerr)?;
let submitter = Mutex::new(Submitter {
cpool,
cmd,
fence,
dpool,
recording: false,
n: 0,
work: 0,
submits: 0,
written_since_barrier: std::collections::HashSet::new(),
record_ns: 0,
barriers: 0,
qpool,
op_names: Vec::new(),
op_push: Vec::new(),
capturing: false,
graph_cmd: vk::CommandBuffer::null(),
});
// Phase profiling: opt-in, read once here so the hot path only checks a bool.
let profile = std::env::var("VK_PROFILE")
.map(|v| v != "0")
.unwrap_or(false);
// Per-op GPU timing: opt-in AND the compute queue must advertise timestamp support
// (valid bits + non-zero period), else the timestamp commands would be invalid.
let timestamp_period = instance
.get_physical_device_properties(pdev)
.limits
.timestamp_period;
let ts_supported = instance
.get_physical_device_queue_family_properties(pdev)
.get(qfi as usize)
.map(|q| q.timestamp_valid_bits > 0)
.unwrap_or(false);
// VK_ROOFLINE implies the timestamp path (it joins bytes/FLOPs to the same GPU ns) and
// adds the per-kernel achieved-rate table + the one-shot peak-BW measurement below.
let roofline = ts_supported
&& timestamp_period > 0.0
&& std::env::var("VK_ROOFLINE").map(|v| v != "0").unwrap_or(false);
let gpu_profile = roofline
|| (ts_supported
&& timestamp_period > 0.0
&& std::env::var("VK_PROFILE_GPU").map(|v| v != "0").unwrap_or(false));
// Per-submission workgroup bound: caps one queue submission's on-GPU runtime under the
// driver ring timeout so a long prefill can't hang the GPU. VK_WORK_CAP overrides the
// calibrated default; 0 disables the bound (restores the pre-fix single-submission path).
let work_cap = std::env::var("VK_WORK_CAP")
.ok()
.and_then(|v| v.parse::<u64>().ok())
.map(|v| if v == 0 { u64::MAX } else { v })
.unwrap_or(WORK_CAP);
let inner = VkInner {
_entry: entry,
instance,
pdev,
device,
queue,
qfi,
gpu_id: ordinal,
mem_props,
mem_strategy,
has_mem_budget,
subgroup_matvec,
subgroup_size,
int_dot8,
seed: Mutex::new(299792458),
profile,
gpu_profile,
timestamp_period,
roofline,
peak_bw_gbps: std::sync::atomic::AtomicU32::new(0),
work_cap: std::sync::atomic::AtomicU64::new(work_cap),
push_descriptor,
pipelines: Mutex::new(HashMap::new()),
coopmat_variant: Mutex::new(None),
submitter,
bufpool: Mutex::new(BufPool::default()),
coopmat,
cm_mnk,
cm_use,
};
let dev = Self {
inner: Arc::new(inner),
};
if roofline {
dev.measure_and_store_peak_bw();
}
Ok(dev)
}
}
fn location(&self) -> crate::DeviceLocation {
crate::DeviceLocation::Vulkan {
gpu_id: self.inner.gpu_id,
}
}
fn same_device(&self, rhs: &Self) -> bool {
Arc::ptr_eq(&self.inner, &rhs.inner)
}
fn zeros_impl(&self, shape: &Shape, dtype: DType) -> Result<Self::Storage> {
let count = shape.elem_count();
// Allocate uninitialized device memory and clear it ON the GPU (const_fill) rather than
// uploading a host zero-vec. The upload path allocates a host Vec and memcpy's the whole tensor
// host->device on every call; a transformer forward calls zeros() hundreds of times, so this is
// ~835 MB/forward of pure host memory traffic on a UMA APU (measured on gfx1151, Qwen3-4B @512).
// const_fill is a GPU dispatch: no host allocation, no host->device copy. Zero is bit-identical
// across the f32/u32 storage reprs (f16/bf16 live as f32, u8/i64/i32 as u32), so one fill with
// 0 bits serves every dtype. Deferred like every other op -- a later CPU readback flushes first.
match dtype {
DType::F32 | DType::F16 | DType::BF16 | DType::U32 | DType::U8 | DType::I64 | DType::I32 => {}
_ => crate::bail!("vulkan: only f32/u32/f16/bf16/u8/i64/i32 supported, got {dtype:?}"),
}
let s = unsafe { self.alloc_uninit(shape, dtype)? };
if count > 0 {
self.dispatch(
"const_fill",
&[s.buffer],
&push_u32(&[count as u32, 0u32]),
((count as u32).div_ceil(WG1D), 1, 1),
)?;
}
Ok(s)
}
unsafe fn alloc_uninit(&self, shape: &Shape, dtype: DType) -> Result<Self::Storage> {
// Same dtype mapping as zeros_impl: f16/bf16 -> f32 storage, u8/i64/i32 -> u32 storage.
match dtype {
DType::F32 | DType::F16 | DType::BF16 => self.alloc_f32(shape.elem_count()),
DType::U32 | DType::U8 | DType::I64 | DType::I32 => self.alloc_u32(shape.elem_count()),
_ => crate::bail!("vulkan: only f32/u32/f16/bf16/u8/i64/i32 supported, got {dtype:?}"),
}
}
fn storage_from_slice<T: crate::WithDType>(&self, s: &[T]) -> Result<Self::Storage> {
self.storage_from_cpu_storage(&T::to_cpu_storage(s))
}
fn storage_from_cpu_storage(&self, s: &CpuStorage) -> Result<Self::Storage> {
// Vulkan computes in f32; f16/bf16 weights are upcast on upload so real
// fp16/bf16 safetensors load on the GPU (uniform, so half tensors stay dtype-consistent).
match s {
CpuStorage::F32(v) => self.upload_f32(v),
CpuStorage::U32(v) => self.upload_u32(v),
CpuStorage::F16(v) => {
self.upload_f32(&v.iter().map(|x| x.to_f32()).collect::<Vec<_>>())
}
CpuStorage::BF16(v) => {
self.upload_f32(&v.iter().map(|x| x.to_f32()).collect::<Vec<_>>())
}
// u8 (e.g. boolean attention masks) -> u32: where_cond and casts use u32 on Vulkan.
CpuStorage::U8(v) => self.upload_u32(&v.iter().map(|&x| x as u32).collect::<Vec<_>>()),
// i64/i32 index metadata (paged-attn slot_mapping/block_tables/context_lens, argsort ids)
// -> u32: the Vulkan shaders read these as `uint`, and index values fit in u32. `-1 as u32`
// = u32::MAX, which is exactly the pad sentinel slot_mapping uses (the shader skips it),
// so the coercion is value-preserving for every case the engine produces.
CpuStorage::I64(v) => self.upload_u32(&v.iter().map(|&x| x as u32).collect::<Vec<_>>()),
CpuStorage::I32(v) => self.upload_u32(&v.iter().map(|&x| x as u32).collect::<Vec<_>>()),
_ => crate::bail!(
"vulkan: only f32/u32/f16/bf16/u8/i64/i32 supported, got {:?}",
s.dtype()
),
}
}
fn storage_from_cpu_storage_owned(&self, s: CpuStorage) -> Result<Self::Storage> {
self.storage_from_cpu_storage(&s)
}
fn rand_uniform(
&self,
shape: &Shape,
dtype: DType,
min: f64,
max: f64,
) -> Result<Self::Storage> {
if dtype != DType::F32 {
crate::bail!("vulkan: rand_uniform only f32, got {dtype:?}");
}
// Generate on the CPU (rand 0.9 API, mirrors cpu_backend) then upload.
let mut rng = rand::rng();
let n = shape.elem_count();
let uniform = rand::distr::Uniform::new(min as f32, max as f32).map_err(Error::wrap)?;
let mut data = Vec::with_capacity(n);
for _ in 0..n {
data.push(rng.sample::<f32, _>(uniform));
}
self.upload_f32(&data)
}
fn rand_normal(
&self,
shape: &Shape,
dtype: DType,
mean: f64,
std: f64,
) -> Result<Self::Storage> {
if dtype != DType::F32 {
crate::bail!("vulkan: rand_normal only f32, got {dtype:?}");
}
use rand_distr::Distribution;
let mut rng = rand::rng();
let n = shape.elem_count();
let normal = rand_distr::Normal::new(mean as f32, std as f32).map_err(Error::wrap)?;
let mut data = Vec::with_capacity(n);
for _ in 0..n {
data.push(normal.sample(&mut rng));
}
self.upload_f32(&data)
}
fn set_seed(&self, seed: u64) -> Result<()> {
*self.inner.seed.lock().unwrap() = seed;
Ok(())
}
fn get_current_seed(&self) -> Result<u64> {
Ok(*self.inner.seed.lock().unwrap())
}
fn synchronize(&self) -> Result<()> {
self.flush()?;
unsafe { self.dev().device_wait_idle().map_err(vkerr) }
}
}
impl VulkanStorage {
fn count(&self) -> usize {
self.count
}
/// Device-offset copy2d for the decode command-graph KV append: identical to the `copy2d`
/// primitive except the destination base offset is `off[0] * off_mult`, read from the `off` device
/// buffer instead of a push constant. A captured graph records this append once; each replay writes
/// the new token's K/V at the ADVANCING cache slot by refreshing `off[0]` (the decode position) in
/// place -- a push-constant offset would bake the warmup slot into the graph and freeze every
/// replay's write there (the fluent-but-stale decode bug). `off_mult` scales the position to
/// elements (the KV row width = head_dim), so one shared position buffer serves every layer's
/// append. `off` is read (binding 2); only `dst` (binding 1) is written, so the hazard tracker
/// barriers a later reader of the cache, not the position buffer.
///
/// `dst` is taken by shared reference: the write lands in device memory through the bound buffer
/// handle, the `VulkanStorage` value itself is untouched. This mirrors [`Self`]'s in-place cache
/// writers (e.g. `reshape_and_cache_vk`) and lets a caller holding only read guards on the source
/// and destination cache tensors (the engine KV-append seam) drive the append without a mutable
/// borrow it cannot obtain across the tensor storage lock.
#[allow(clippy::too_many_arguments)]
pub fn copy2d_off(
&self,
dst: &Self,
off: &Self,
d1: usize,
d2: usize,
src_stride1: usize,
dst_stride1: usize,
src_offset: usize,
off_mult: usize,
) -> Result<()> {
let total = d1 * d2;
if total == 0 {
return Ok(());
}
let push = push_u32(&[
d1 as u32,
d2 as u32,
src_stride1 as u32,
dst_stride1 as u32,
src_offset as u32,
off_mult as u32,
]);
self.device.dispatch_out(
"copy2d_off",
&[self.buffer, dst.buffer, off.buffer],
1,
&push,
Self::groups_1d(total),
)
}
/// Number of u32 words in this buffer. For a quantized weight/bank uploaded via the `quantize_*`
/// helpers (all `upload_u32`-backed, dtype U32) this is the block-word count -- used to assert a
/// resident MoE bank's per-expert stride covers exactly E experts.
pub fn len_words(&self) -> usize {
self.count
}
// Download the whole buffer as f32 (used internally + by to_cpu_storage).
fn to_vec_f32(&self) -> Result<Vec<f32>> {
unsafe {
self.device
.read_f32(self.buffer, self.memory, self.host_visible, self.count)
}
}
// Download the whole buffer as u32 (used by to_cpu_storage for U32 storage).
fn to_vec_u32(&self) -> Result<Vec<u32>> {
unsafe {
self.device
.read_u32(self.buffer, self.memory, self.host_visible, self.count)
}
}
// 1D elementwise dispatch over `n` elements: ceil(n/64) workgroups.
fn groups_1d(n: usize) -> (u32, u32, u32) {
((n as u32).div_ceil(WG1D), 1, 1)
}
// Materialize any (strided/broadcast) f32 layout into a fresh contiguous buffer
// of `layout.elem_count()` elements via the strided_copy kernel. Identity for
// already-contiguous layouts. rank <= 6.
fn contiguous(&self, layout: &Layout) -> Result<VulkanStorage> {
let dims = layout.dims();
let rank = dims.len();
if rank > 6 {
crate::bail!("vulkan: contiguous supports rank <= 6, got {rank}");
}
let n = layout.shape().elem_count();
if self.device.inner.profile && n >= 1_000_000 {
eprintln!("[VK_CONTIG] dims={:?} strides={:?} n={n}", layout.dims(), layout.stride());
}
let out = self.device.alloc_f32(n)?;
let strides = layout.stride();
// Push block MUST match strided_copy.comp exactly: {n, rank, offset, dst_offset, shape[6],
// strides[6]}. dst_offset is 0 here (we materialize into a packed offset-0 buffer); it was
// the missing 4th field that previously shifted shape/strides by one slot and silently
// corrupted every materialized (broadcast/transpose) operand consumed in-batch.
let mut p = vec![n as u32, rank as u32, layout.start_offset() as u32, 0u32];
let mut shape6 = [0u32; 6];
let mut stride6 = [0u32; 6];
for d in 0..rank {
shape6[d] = dims[d] as u32;
stride6[d] = strides[d] as u32;
}
p.extend_from_slice(&shape6);
p.extend_from_slice(&stride6);
self.device.dispatch(
"strided_copy",
&[self.buffer, out.buffer],
&push_u32(&p),
Self::groups_1d(n),
)?;
Ok(out)
}
// Materialize a u32 storage (ids / where_cond mask) into a fresh contiguous, offset-0 buffer.
// Identity (raw clone) when already contiguous from offset 0. Done on the CPU via the layout's
// strided index so it's bit-exact for arbitrary u32 values -- reusing the float strided_copy
// would reinterpret small integers as denormal floats, which load-time flush-to-zero could
// corrupt. These tensors (token/position ids, attention masks) are tiny, so the round-trip is
// cheap relative to correctness.
fn contiguous_u32(&self, layout: &Layout) -> Result<VulkanStorage> {
debug_assert_eq!(self.dtype, DType::U32);
if layout.is_contiguous() && layout.start_offset() == 0 {
return self.device.upload_u32(&self.to_vec_u32()?);
}
let src = self.to_vec_u32()?;
let gathered: Vec<u32> = layout.strided_index().map(|i| src[i]).collect();
self.device.upload_u32(&gathered)
}
// Buffer for a contiguous, offset-0 view of `layout`: no copy when the storage already is one
// (returns its own buffer), otherwise materializes a packed copy returned via `keep` (held
// alive by the caller until the dispatch is recorded). Each avoided contiguous() is one fewer
// dispatch, which dominates per-op cost in a forward. Use only for transient op inputs -- the
// result `out` must be a fresh alloc, never this buffer (it may alias `self`).
fn contig_buf(&self, layout: &Layout, keep: &mut Option<VulkanStorage>) -> Result<vk::Buffer> {
if layout.is_contiguous() && layout.start_offset() == 0 {
Ok(self.buffer)
} else {
let s = self.contiguous(layout)?;
let b = s.buffer;
*keep = Some(s);
Ok(b)
}
}
// --- native fused ops (replace the GPU<->CPU round-trip fallbacks) ---
// Row-wise argsort over the last (contiguous) dim via the bitonic argsort kernel. Returns the u32
// index permutation (shape == input shape) entirely on the GPU. `Ok(None)` when cols_pad exceeds
// the shader's shared-scratch bound (ARGSORT_MAX_COLS_PAD); the caller then uses the CPU argsort.
// MoE routing (cols == num_experts ~128) always stays on the GPU path.
pub fn arg_sort_last_dim(
&self,
layout: &Layout,
asc: bool,
last_dim: usize,
) -> Result<Option<VulkanStorage>> {
let cols = last_dim;
let cols_pad = cols.next_power_of_two();
if cols_pad > ARGSORT_MAX_COLS_PAD {
return Ok(None);
}
let mut xk = None;
let xb = self.contig_buf(layout, &mut xk)?;
let n = layout.shape().elem_count();
let rows = n / cols.max(1);
let out = self.device.alloc_u32(n)?;
let push = push_u32(&[rows as u32, cols as u32, cols_pad as u32, asc as u32]);
// One workgroup per row; the shader threads cooperate over cols_pad inside it.
self.device
.dispatch("argsort", &[xb, out.buffer], &push, (rows as u32, 1, 1))?;
Ok(Some(out))
}
// softmax over the last dim. `self` is the input; `layout` its layout. One row per thread.
pub fn softmax_last_dim(&self, layout: &Layout) -> Result<VulkanStorage> {
let mut xk = None;
let xb = self.contig_buf(layout, &mut xk)?;
let dims = layout.dims();
let m = *dims.last().unwrap_or(&1);
let nrows = layout.shape().elem_count() / m.max(1);
let out = self.device.alloc_f32(nrows * m)?;
// DSL block-per-row kernel (one workgroup/row, 256 threads, coalesced reads + shared-mem
// max/sum reductions): replaces the naive one-invocation-per-row softmax_rows.comp. cubecl
// has no push-constants, so n rides a pooled SSBO -- lifetime-safe across the deferred batch
// (BufPool reclaim is post-fence-only). out is binding 1, so dispatch_out (not the last-binding
// default) tracks its RAW barrier.
let ndb = self.device.upload_u32(&[m as u32])?;
self.device.dispatch_out(
"softmax_rows",
&[xb, out.buffer, ndb.buffer],
1,
&[],
(nrows as u32, 1, 1),
)?;
Ok(out)
}
// rms-norm over the last dim. `self`=x, `alpha`=scale [m]. Matches hanzo-ml rms-norm.
pub fn rms_norm(
&self,
layout: &Layout,
alpha: &VulkanStorage,
alpha_l: &Layout,
eps: f32,
) -> Result<VulkanStorage> {
let mut xk = None;
let mut ak = None;
let xb = self.contig_buf(layout, &mut xk)?;
let ab = alpha.contig_buf(alpha_l, &mut ak)?;
let dims = layout.dims();
let m = *dims.last().unwrap_or(&1);
let nrows = layout.shape().elem_count() / m.max(1);
let out = self.device.alloc_f32(nrows * m)?;
// DSL block-per-row kernel (one workgroup/row, 256 threads, coalesced reads + shared-mem
// reduce): 10.86x the naive one-invocation-per-row .comp it replaced. cubecl has no
// push-constants, so eps/ndim ride pooled SSBOs -- lifetime-safe across the deferred batch
// because the BufPool parks dropped buffers in `pending` and reclaim() runs only after a
// flush+fence (post-fence-only), identical to every activation buffer in the forward pass.
// out is binding 2, so dispatch_out (not the last-binding default) tracks its RAW barrier.
let epsb = self.device.upload_f32(&[eps])?;
let ndb = self.device.upload_u32(&[m as u32])?;
self.device.dispatch_out(
"rms_norm",
&[xb, ab, out.buffer, epsb.buffer, ndb.buffer],
2,
&[],
(nrows as u32, 1, 1),
)?;
Ok(out)
}
// Fused residual-add + RMSNorm in ONE dispatch. Returns (s, y): s = self + residual (the new
// residual stream), y = rms_norm(s) * alpha. Bit-identical to add then rms_norm, no barrier
// between them. Mirrors ROCm add_rms_norm; the decode lever on barrier-serialized Vulkan.
pub fn add_rmsnorm(
&self,
layout: &Layout,
residual: &VulkanStorage,
residual_l: &Layout,
alpha: &VulkanStorage,
alpha_l: &Layout,
eps: f32,
) -> Result<(VulkanStorage, VulkanStorage)> {
let mut xk = None;
let mut rk = None;
let mut ak = None;
let xb = self.contig_buf(layout, &mut xk)?;
let rb = residual.contig_buf(residual_l, &mut rk)?;
let ab = alpha.contig_buf(alpha_l, &mut ak)?;
let dims = layout.dims();
let m = *dims.last().unwrap_or(&1);
let nrows = layout.shape().elem_count() / m.max(1);
let s_out = self.device.alloc_f32(nrows * m)?;
let y = self.device.alloc_f32(nrows * m)?;
// DSL block-per-row fused add+rmsnorm (add_rmsnorm_blk.spv): one workgroup/row, coalesced reads
// + shared-mem reduce -- the coalesced twin of the naive per-row add_rmsnorm.comp it replaced
// (the same ~10x uncoalesced penalty rms_norm proved). cubecl has no push-constants, so eps/ndim
// ride pooled SSBOs -- lifetime-safe across the deferred batch (BufPool reclaim is post-fence-only,
// identical to rms_norm). Both s (binding 3) and y (binding 4) are outputs, so dispatch_outs tracks
// each write for the selective RAW barrier (mirrors gdn_step's state+output pair).
let epsb = self.device.upload_f32(&[eps])?;
let ndb = self.device.upload_u32(&[m as u32])?;
self.device.dispatch_outs(
"add_rmsnorm",
&[xb, rb, ab, s_out.buffer, y.buffer, epsb.buffer, ndb.buffer],
&[3, 4],
&[],
(nrows as u32, 1, 1),
)?;
Ok((s_out, y))
}
// Fused SwiGLU: out = silu(self) * rhs, elementwise. One dispatch instead of silu + mul.
pub fn silu_mul(
&self,
layout: &Layout,
rhs: &VulkanStorage,
rhs_l: &Layout,
) -> Result<VulkanStorage> {
let mut ak = None;
let mut bk = None;
let ab = self.contig_buf(layout, &mut ak)?;
let bb = rhs.contig_buf(rhs_l, &mut bk)?;
let n = layout.shape().elem_count();
let out = self.device.alloc_f32(n)?;
self.device.dispatch(
"silu_mul",
&[ab, bb, out.buffer],
&(n as u32).to_ne_bytes(),
Self::groups_1d(n),
)?;
Ok(out)
}
// Unary sigmoid: out = 1 / (1 + exp(-self)). Vulkan path for hanzo_nn::ops::sigmoid.
pub fn sigmoid(&self, layout: &Layout) -> Result<VulkanStorage> {
let mut ck = None;
let cb = self.contig_buf(layout, &mut ck)?;
let n = layout.shape().elem_count();
let mut out = self.device.alloc_f32(n)?;
// Preserve the input's logical dtype (storage is f32 regardless); callers multiply the
// result against a same-dtype tensor without an intervening cast (output-gate path).
out.dtype = self.dtype;
self.device.dispatch(
"sigmoid",
&[cb, out.buffer],
&(n as u32).to_ne_bytes(),
Self::groups_1d(n),
)?;
Ok(out)
}
// GPT-NeoX rotary embedding. `self`=src [b,h,t,d], cos/sin [t,d/2] or [b,t,d/2].
pub fn rope(
&self,
layout: &Layout,
cos: &VulkanStorage,
cos_l: &Layout,
sin: &VulkanStorage,
sin_l: &Layout,
) -> Result<VulkanStorage> {
let mut srck = None;
let mut ck = None;
let mut sk = None;
let srcb = self.contig_buf(layout, &mut srck)?;
let cb = cos.contig_buf(cos_l, &mut ck)?;
let sb = sin.contig_buf(sin_l, &mut sk)?;
let (b, h, t, d) = layout.shape().dims4()?;
let unbatched = (cos_l.dims().len() == 3 && sin_l.dims().len() == 3) as u32;
let out = self.device.alloc_f32(b * h * t * d)?;
let pairs = b * h * t * (d / 2);
self.device.dispatch(
"rope",
&[srcb, cb, sb, out.buffer],
&push_u32(&[b as u32, h as u32, t as u32, d as u32, unbatched]),
Self::groups_1d(pairs),
)?;
Ok(out)
}
// Fused per-head RMSNorm + NeoX RoPE: rms_norm(self, weight, eps) then rope(., cos, sin) in ONE
// dispatch. `self` = x [b,h,t,d]; cos/sin [t,d/2] or [b,t,d/2]. Bit-identical to the two-op chain
// (rope_norm.comp), but emits no inter-op barrier -- the decode lever on Vulkan (barrier-serialized).
#[allow(clippy::too_many_arguments)]
pub fn rope_norm(
&self,
layout: &Layout,
weight: &VulkanStorage,
weight_l: &Layout,
eps: f32,
cos: &VulkanStorage,
cos_l: &Layout,
sin: &VulkanStorage,
sin_l: &Layout,
) -> Result<VulkanStorage> {
let mut xk = None;
let mut wk = None;
let mut ck = None;
let mut sk = None;
let xb = self.contig_buf(layout, &mut xk)?;
let wb = weight.contig_buf(weight_l, &mut wk)?;
let cb = cos.contig_buf(cos_l, &mut ck)?;
let sb = sin.contig_buf(sin_l, &mut sk)?;
let (b, h, t, d) = layout.shape().dims4()?;
let unbatched = (cos_l.dims().len() == 3 && sin_l.dims().len() == 3) as u32;
let out = self.device.alloc_f32(b * h * t * d)?;
let mut push = push_u32(&[b as u32, h as u32, t as u32, d as u32]);
push.extend_from_slice(&eps.to_ne_bytes());
push.extend_from_slice(&unbatched.to_ne_bytes());
self.device.dispatch(
"rope_norm",
&[xb, wb, cb, sb, out.buffer],
&push,
Self::groups_1d(b * h * t),
)?;
Ok(out)
}
// Gated delta rule, single decode step (seq_len==1). `self`=q, plus k,v,g,beta; `state` is the
// recurrent state [BH, K, V], updated IN PLACE in VRAM (kept across tokens by the caller's state
// pool). q,k: [BH, K]; v: [BH, V]; g,beta: [BH]. q must be pre-scaled by 1/sqrt(K). Returns
// y [BH, V]. Mirrors gated_delta_rule_recurrence for seq=1 and the CUDA single-step kernel.
// GDN_STEP_MAX_K bounds the shader's per-invocation k array; head_k_dim must not exceed it.
#[allow(clippy::too_many_arguments)]
pub fn gdn_step(
&self,
q_l: &Layout,
k: &VulkanStorage,
k_l: &Layout,
v: &VulkanStorage,
v_l: &Layout,
g: &VulkanStorage,
g_l: &Layout,
beta: &VulkanStorage,
beta_l: &Layout,
state: &VulkanStorage,
state_l: &Layout,
bh: usize,
k_dim: usize,
v_dim: usize,
) -> Result<VulkanStorage> {
if k_dim > GDN_STEP_MAX_K {
crate::bail!(
"vulkan: gdn_step head_k_dim {k_dim} exceeds GDN_STEP_MAX_K {GDN_STEP_MAX_K}"
);
}
if !(state_l.is_contiguous() && state_l.start_offset() == 0) {
crate::bail!(
"vulkan: gdn_step state must be contiguous and offset 0 (it is updated in place)"
);
}
let mut qk = None;
let mut kk = None;
let mut vk_ = None;
let mut gk = None;
let mut bk = None;
let qb = self.contig_buf(q_l, &mut qk)?;
let kb = k.contig_buf(k_l, &mut kk)?;
let vb = v.contig_buf(v_l, &mut vk_)?;
let gb = g.contig_buf(g_l, &mut gk)?;
let betab = beta.contig_buf(beta_l, &mut bk)?;
let out = self.device.alloc_f32(bh * v_dim)?;
// State (binding 5) is read-modify-written in place; the fresh output (binding 6) is written
// too. Mark BOTH so a later in-batch reader (scatter of state, RMSNorm of the output) gets the
// RAW barrier. The read-side hazard check already tests all bindings, including the state input
// gathered earlier this batch.
self.device.dispatch_outs(
"gdn_step",
&[qb, kb, vb, gb, betab, state.buffer, out.buffer],
&[5, 6],
&push_u32(&[bh as u32, k_dim as u32, v_dim as u32]),
Self::groups_1d(bh * v_dim),
)?;
Ok(out)
}
// Causal depthwise conv1d, single decode step (seq_len==1, batch==1). `self`=conv_state
// [conv_dim, k_size], updated IN PLACE (drop oldest column, append `x`); `x` is the new column
// [conv_dim]; `w` the weight [conv_dim, k_size]. Returns silu(conv) [conv_dim]. Mirrors
// causal_conv1d_update for seq=1.
#[allow(clippy::too_many_arguments)]
pub fn gdn_conv1d_step(
&self,
state_l: &Layout,
x: &VulkanStorage,
x_l: &Layout,
w: &VulkanStorage,
w_l: &Layout,
conv_dim: usize,
k_size: usize,
) -> Result<VulkanStorage> {
if k_size > GDN_CONV_MAX_K {
crate::bail!(
"vulkan: gdn_conv1d_step kernel {k_size} exceeds GDN_CONV_MAX_K {GDN_CONV_MAX_K}"
);
}
if !(state_l.is_contiguous() && state_l.start_offset() == 0) {
crate::bail!("vulkan: gdn_conv1d_step conv_state must be contiguous and offset 0 (updated in place)");
}
let mut xk = None;
let mut wk = None;
let xb = x.contig_buf(x_l, &mut xk)?;
let wb = w.contig_buf(w_l, &mut wk)?;
let out = self.device.alloc_f32(conv_dim)?;
// conv_state (binding 0) is updated in place; output (binding 3) is fresh. Mark both.
self.device.dispatch_outs(
"gdn_conv1d_step",
&[self.buffer, xb, wb, out.buffer],
&[0, 3],
&push_u32(&[conv_dim as u32, k_size as u32]),
Self::groups_1d(conv_dim),
)?;
Ok(out)
}
// Shared scatter: write/accumulate src into dst (self) along `dim` at positions `ids`.
// dst is assumed contiguous (its layout `l` gives the dim sizes). ids/src share a shape.
#[allow(clippy::too_many_arguments)]
fn scatter_impl(
&self,
kernel: &'static str,
l: &Layout,
ids: &VulkanStorage,
ids_l: &Layout,
src: &VulkanStorage,
src_l: &Layout,
dim: usize,
) -> Result<()> {
if ids.dtype != DType::U32 {
crate::bail!("vulkan: scatter requires u32 ids, got {:?}", ids.dtype);
}
let idc = ids.contiguous(ids_l)?;
let srcc = src.contiguous(src_l)?;
let src_dims = src_l.dims();
let dst_dims = l.dims();
let right: usize = src_dims[dim + 1..].iter().product();
let dim_src = src_dims[dim];
let dim_dst = dst_dims[dim];
let n = src_l.shape().elem_count();
// scatter writes (and scatter_add reads) binding 0 (`self.buffer`), not the last binding,
// so name out_idx=0 for the hazard tracker.
self.device.dispatch_out(
kernel,
&[self.buffer, srcc.buffer, idc.buffer],
0,
&push_u32(&[n as u32, right as u32, dim_src as u32, dim_dst as u32]),
Self::groups_1d(n),
)?;
Ok(())
}
// Shared 2D pooling (no padding): out_h = (ih-kh)/sh + 1, out_w = (iw-kw)/sw + 1.
fn pool2d(
&self,
kernel: &'static str,
l: &Layout,
k: (usize, usize),
stride: (usize, usize),
) -> Result<VulkanStorage> {
let inp = self.contiguous(l)?;
let (b, c, ih, iw) = l.shape().dims4()?;
let (kh, kw) = k;
let (sh, sw) = stride;
let oh = (ih - kh) / sh + 1;
let ow = (iw - kw) / sw + 1;
let out = self.device.alloc_f32(b * c * oh * ow)?;
self.device.dispatch(
kernel,
&[inp.buffer, out.buffer],
&push_u32(&[
b as u32, c as u32, ih as u32, iw as u32, oh as u32, ow as u32, kh as u32,
kw as u32, sh as u32, sw as u32,
]),
Self::groups_1d(b * c * oh * ow),
)?;
Ok(out)
}
}
impl BackendStorage for VulkanStorage {
type Device = VulkanDevice;
fn try_clone(&self, _: &Layout) -> Result<Self> {
// Raw device-memory copy of the whole buffer (ignores layout; assumes contiguous).
// Buffers are just bytes (4 bytes/elem). Vulkan storage physically holds f32 or u32; per
// the upload invariant f16/bf16 live as f32 and u8 as u32, so clone them through the same
// 4-byte representation rather than bailing.
match self.dtype {
DType::U32 | DType::U8 => self.device.upload_u32(&self.to_vec_u32()?),
_ => self.device.upload_f32(&self.to_vec_f32()?),
}
}
fn dtype(&self) -> DType {
self.dtype
}
fn device(&self) -> &Self::Device {
&self.device
}
fn to_cpu_storage(&self) -> Result<CpuStorage> {
match self.dtype {
DType::U32 => Ok(CpuStorage::U32(self.to_vec_u32()?)),
_ => Ok(CpuStorage::F32(self.to_vec_f32()?)),
}
}
fn affine(&self, layout: &Layout, mul: f64, add: f64) -> Result<Self> {
let mut ck = None;
let cb = self.contig_buf(layout, &mut ck)?;
let n = layout.shape().elem_count();
let out = self.device.alloc_f32(n)?;
let mut push = (n as u32).to_ne_bytes().to_vec();
push.extend_from_slice(&(mul as f32).to_ne_bytes());
push.extend_from_slice(&(add as f32).to_ne_bytes());
self.device
.dispatch("affine", &[cb, out.buffer], &push, Self::groups_1d(n))?;
Ok(out)
}
fn powf(&self, layout: &Layout, e: f64) -> Result<Self> {
let mut ck = None;
let cb = self.contig_buf(layout, &mut ck)?;
let n = layout.shape().elem_count();
let out = self.device.alloc_f32(n)?;
let mut push = (n as u32).to_ne_bytes().to_vec();
push.extend_from_slice(&(e as f32).to_ne_bytes());
self.device
.dispatch("powf", &[cb, out.buffer], &push, Self::groups_1d(n))?;
Ok(out)
}
fn elu(&self, layout: &Layout, alpha: f64) -> Result<Self> {
let c = self.contiguous(layout)?;
let n = layout.shape().elem_count();
let out = self.device.alloc_f32(n)?;
let mut push = (n as u32).to_ne_bytes().to_vec();
push.extend_from_slice(&(alpha as f32).to_ne_bytes());
self.device
.dispatch("elu", &[c.buffer, out.buffer], &push, Self::groups_1d(n))?;
Ok(out)
}
fn reduce_op(&self, op: ReduceOp, layout: &Layout, sum_dims: &[usize]) -> Result<Self> {
let (kernel, is_arg) = match op {
ReduceOp::Sum => ("reduce_sum", false),
ReduceOp::Max => ("reduce_max", false),
ReduceOp::Min => ("reduce_min", false),
ReduceOp::ArgMin => ("reduce_argmin", true),
ReduceOp::ArgMax => ("reduce_argmax", true),
};
// Reduce over the last dim only, contiguously => rows x cols, one out per row.
let dims = layout.dims();
let rank = dims.len();
if rank == 0 {
crate::bail!("vulkan: reduce_op on scalar not supported");
}
// The SPIR-V reduce kernel collapses the last (contiguous) dim into one output per row. For
// any other single axis, permute it to the end first so the same kernel runs on the GPU; the
// resulting row-major order matches the framework's keep-dim wrap exactly.
let (c, cols) = if sum_dims == [rank - 1] {
(self.contiguous(layout)?, dims[rank - 1])
} else if sum_dims.len() == 1 {
let d = sum_dims[0];
let mut perm: Vec<usize> = (0..rank).filter(|&x| x != d).collect();
perm.push(d);
(self.contiguous(&layout.permute(&perm)?)?, dims[d])
} else {
crate::bail!(
"vulkan: reduce over multiple axes at once not supported (got {sum_dims:?})"
);
};
let rows: usize = layout.shape().elem_count() / cols;
// arg-reductions return u32 indices; value reductions return f32.
let out = if is_arg {
self.device.alloc_u32(rows)?
} else {
self.device.alloc_f32(rows)?
};
let push = push_u32(&[rows as u32, cols as u32]);
// one invocation per row
self.device.dispatch(
kernel,
&[c.buffer, out.buffer],
&push,
Self::groups_1d(rows),
)?;
Ok(out)
}
fn cmp(&self, op: CmpOp, rhs: &Self, lhs_l: &Layout, rhs_l: &Layout) -> Result<Self> {
// Elementwise compare on f32 operands -> u32 {0,1} mask. (Backend has no U8; U32 is a
// valid integer mask and composes with where_cond / index ops.)
let lc = self.contiguous(lhs_l)?;
let rc = rhs.contiguous(rhs_l)?;
let n = lhs_l.shape().elem_count();
let out = self.device.alloc_u32(n)?;
let code: u32 = match op {
CmpOp::Eq => 0,
CmpOp::Ne => 1,
CmpOp::Le => 2,
CmpOp::Ge => 3,
CmpOp::Lt => 4,
CmpOp::Gt => 5,
};
self.device.dispatch(
"cmp",
&[lc.buffer, rc.buffer, out.buffer],
&push_u32(&[n as u32, code]),
Self::groups_1d(n),
)?;
Ok(out)
}
fn to_dtype(&self, layout: &Layout, dtype: DType) -> Result<Self> {
let n = layout.shape().elem_count();
match (self.dtype, dtype) {
// Same dtype: just contiguous-ize (respects layout/offset).
(DType::F32, DType::F32) | (DType::U32, DType::U32) => self.contiguous(layout),
(DType::F32, DType::U32) => {
let c = self.contiguous(layout)?;
let out = self.device.alloc_u32(n)?;
self.device.dispatch(
"cast_f2u",
&[c.buffer, out.buffer],
&push_u32(&[n as u32]),
Self::groups_1d(n),
)?;
Ok(out)
}
(DType::U32, DType::F32) => {
let c = self.contiguous(layout)?;
let out = self.device.alloc_f32(n)?;
self.device.dispatch(
"cast_u2f",
&[c.buffer, out.buffer],
&push_u32(&[n as u32]),
Self::groups_1d(n),
)?;
Ok(out)
}
// f16/bf16 are REPRESENTED as f32 on this backend (see zeros_impl / alloc_uninit), so any
// cast among {f32, f16, bf16} is a representation no-op -- just contiguous-ize (preserves
// full f32 precision, same f32-backed result the old CPU path produced). The model casts
// q/k/v to the model dtype and the attention output back to f32 every layer; routing those
// through the CPU fired ~4200 GPU->CPU->GPU syncs/run -> ~0.6 T/s.
(DType::F32, DType::F16)
| (DType::F32, DType::BF16)
| (DType::F16, DType::F32)
| (DType::BF16, DType::F32)
| (DType::F16, DType::F16)
| (DType::BF16, DType::BF16)
| (DType::F16, DType::BF16)
| (DType::BF16, DType::F16) => self.contiguous(layout),
// Other dtypes aren't held by this backend: CPU-convert then upload.
_ => {
self.device.profile_fallback(
"to_dtype",
format_args!("{:?}->{:?} elems={n}", self.dtype, dtype),
);
let cpu = self.to_cpu_storage()?;
let converted = crate::backend::BackendStorage::to_dtype(&cpu, layout, dtype)?;
self.device.storage_from_cpu_storage(&converted)
}
}
}
fn unary_impl<B: UnaryOpT>(&self, layout: &Layout) -> Result<Self> {
let kernel: &'static str = match B::NAME {
"silu" => "silu",
"gelu" => "gelu",
"relu" => "relu",
"exp" => "exp",
"neg" => "neg",
"sqr" => "sqr",
"sqrt" => "sqrt",
"recip" => "recip",
"tanh" => "tanh",
"sin" => "sin",
"cos" => "cos",
"log" => "log",
"abs" => "abs",
"floor" => "floor",
"ceil" => "ceil",
"round" => "round",
"sign" => "sign",
"erf" => "erf",
"gelu_erf" => "gelu_erf",
// Anything still without a SPIR-V kernel: fall back to CPU (correct, slow).
_ => {
self.device.profile_fallback(
B::NAME,
format_args!("unary elems={}", layout.shape().elem_count()),
);
let cpu = self.to_cpu_storage()?;
let r = crate::backend::BackendStorage::unary_impl::<B>(&cpu, layout)?;
return self.device.storage_from_cpu_storage(&r);
}
};
let mut ck = None;
let cb = self.contig_buf(layout, &mut ck)?;
let n = layout.shape().elem_count();
let out = self.device.alloc_f32(n)?;
let push = (n as u32).to_ne_bytes();
self.device
.dispatch(kernel, &[cb, out.buffer], &push, Self::groups_1d(n))?;
Ok(out)
}
fn binary_impl<B: BinaryOpT>(
&self,
rhs: &Self,
lhs_l: &Layout,
rhs_l: &Layout,
) -> Result<Self> {
let kernel: &'static str = match B::NAME {
"add" => "add",
"sub" => "sub",
"mul" => "mul",
"div" => "div",
"maximum" => "maximum",
"minimum" => "minimum",
// Anything still without a SPIR-V kernel: fall back to CPU (correct, slow).
_ => {
self.device.profile_fallback(
B::NAME,
format_args!("binary elems={}", lhs_l.shape().elem_count()),
);
let lc = self.to_cpu_storage()?;
let rc = rhs.to_cpu_storage()?;
let r = crate::backend::BackendStorage::binary_impl::<B>(&lc, &rc, lhs_l, rhs_l)?;
return self.device.storage_from_cpu_storage(&r);
}
};
// hanzo-ml pre-broadcasts both layouts to the output shape (possibly with stride-0 dims);
// broadcast layouts aren't contiguous so contig_buf still materializes them, but
// same-shape contiguous operands skip the copy (one fewer dispatch each).
let mut lk = None;
let mut rk = None;
let lb = self.contig_buf(lhs_l, &mut lk)?;
let rb = rhs.contig_buf(rhs_l, &mut rk)?;
let n = lhs_l.shape().elem_count();
let out = self.device.alloc_f32(n)?;
let push = (n as u32).to_ne_bytes();
self.device
.dispatch(kernel, &[lb, rb, out.buffer], &push, Self::groups_1d(n))?;
Ok(out)
}
fn where_cond(
&self,
l: &Layout,
t: &Self,
t_l: &Layout,
f: &Self,
f_l: &Layout,
) -> Result<Self> {
// self is the condition mask (u32). The kernel reads it linearly, so materialize a
// contiguous, offset-0 copy when the condition layout isn't already one.
if self.dtype != DType::U32 {
crate::bail!(
"vulkan: where_cond requires u32 condition, got {:?}",
self.dtype
);
}
let mut condk = None;
let condb = if l.is_contiguous() && l.start_offset() == 0 {
self.buffer
} else {
let s = self.contiguous_u32(l)?;
let b = s.buffer;
condk = Some(s);
b
};
let _ = &condk; // keep the materialized condition alive until the dispatch is recorded
let mut tk = None;
let mut fk = None;
let tb = t.contig_buf(t_l, &mut tk)?;
let fb = f.contig_buf(f_l, &mut fk)?;
let n = l.shape().elem_count();
let out = self.device.alloc_f32(n)?;
let push = push_u32(&[n as u32]);
self.device.dispatch(
"where_cond",
&[condb, tb, fb, out.buffer],
&push,
Self::groups_1d(n),
)?;
Ok(out)
}
fn conv1d(
&self,
l: &Layout,
kernel: &Self,
kernel_l: &Layout,
p: &crate::conv::ParamsConv1D,
) -> Result<Self> {
let inp = self.contiguous(l)?;
let w = kernel.contiguous(kernel_l)?;
let l_out = p.l_out();
let out = self.device.alloc_f32(p.b_size * p.c_out * l_out)?;
let push = push_u32(&[
p.b_size as u32,
p.c_in as u32,
p.c_out as u32,
p.l_in as u32,
l_out as u32,
p.k_size as u32,
p.padding as u32,
p.stride as u32,
p.dilation as u32,
]);
self.device.dispatch(
"conv1d",
&[inp.buffer, w.buffer, out.buffer],
&push,
Self::groups_1d(p.b_size * p.c_out * l_out),
)?;
Ok(out)
}
fn conv_transpose1d(
&self,
l: &Layout,
kernel: &Self,
kernel_l: &Layout,
p: &crate::conv::ParamsConvTranspose1D,
) -> Result<Self> {
let inp = self.contiguous(l)?;
let w = kernel.contiguous(kernel_l)?;
let l_out = p.l_out();
let out = self.device.alloc_f32(p.b_size * p.c_out * l_out)?;
let push = push_u32(&[
p.b_size as u32,
p.c_in as u32,
p.c_out as u32,
p.l_in as u32,
l_out as u32,
p.k_size as u32,
p.padding as u32,
p.stride as u32,
p.dilation as u32,
]);
self.device.dispatch(
"conv_transpose1d",
&[inp.buffer, w.buffer, out.buffer],
&push,
Self::groups_1d(p.b_size * p.c_out * l_out),
)?;
Ok(out)
}
fn conv2d(
&self,
l: &Layout,
kernel: &Self,
kernel_l: &Layout,
p: &crate::conv::ParamsConv2D,
) -> Result<Self> {
let inp = self.contiguous(l)?;
let w = kernel.contiguous(kernel_l)?;
let (oh, ow) = (p.out_h(), p.out_w());
let out = self.device.alloc_f32(p.b_size * p.c_out * oh * ow)?;
let push = push_u32(&[
p.b_size as u32,
p.c_in as u32,
p.c_out as u32,
p.i_h as u32,
p.i_w as u32,
oh as u32,
ow as u32,
p.k_h as u32,
p.k_w as u32,
p.padding as u32,
p.stride as u32,
p.dilation as u32,
]);
self.device.dispatch(
"conv2d",
&[inp.buffer, w.buffer, out.buffer],
&push,
Self::groups_1d(p.b_size * p.c_out * oh * ow),
)?;
Ok(out)
}
fn conv_transpose2d(
&self,
l: &Layout,
kernel: &Self,
kernel_l: &Layout,
p: &crate::conv::ParamsConvTranspose2D,
) -> Result<Self> {
let inp = self.contiguous(l)?;
let w = kernel.contiguous(kernel_l)?;
let (oh, ow) = (p.out_h(), p.out_w());
let out = self.device.alloc_f32(p.b_size * p.c_out * oh * ow)?;
let push = push_u32(&[
p.b_size as u32,
p.c_in as u32,
p.c_out as u32,
p.i_h as u32,
p.i_w as u32,
oh as u32,
ow as u32,
p.k_h as u32,
p.k_w as u32,
p.padding as u32,
p.stride as u32,
p.dilation as u32,
]);
self.device.dispatch(
"conv_transpose2d",
&[inp.buffer, w.buffer, out.buffer],
&push,
Self::groups_1d(p.b_size * p.c_out * oh * ow),
)?;
Ok(out)
}
fn avg_pool2d(&self, l: &Layout, k: (usize, usize), stride: (usize, usize)) -> Result<Self> {
self.pool2d("avg_pool2d", l, k, stride)
}
fn max_pool2d(&self, l: &Layout, k: (usize, usize), stride: (usize, usize)) -> Result<Self> {
self.pool2d("max_pool2d", l, k, stride)
}
fn upsample_nearest1d(&self, l: &Layout, sz: usize) -> Result<Self> {
let inp = self.contiguous(l)?;
let (b, c, l_in) = l.shape().dims3()?;
let out = self.device.alloc_f32(b * c * sz)?;
self.device.dispatch(
"upsample_nearest1d",
&[inp.buffer, out.buffer],
&push_u32(&[b as u32, c as u32, l_in as u32, sz as u32]),
Self::groups_1d(b * c * sz),
)?;
Ok(out)
}
fn upsample_nearest2d(&self, l: &Layout, oh: usize, ow: usize) -> Result<Self> {
let inp = self.contiguous(l)?;
let (b, c, ih, iw) = l.shape().dims4()?;
let out = self.device.alloc_f32(b * c * oh * ow)?;
self.device.dispatch(
"upsample_nearest2d",
&[inp.buffer, out.buffer],
&push_u32(&[
b as u32, c as u32, ih as u32, iw as u32, oh as u32, ow as u32,
]),
Self::groups_1d(b * c * oh * ow),
)?;
Ok(out)
}
fn upsample_bilinear2d(
&self,
l: &Layout,
oh: usize,
ow: usize,
align_corners: bool,
scale_h: Option<f64>,
scale_w: Option<f64>,
) -> Result<Self> {
let inp = self.contiguous(l)?;
let (b, c, ih, iw) = l.shape().dims4()?;
// PyTorch area_pixel scale logic, mirrored from the CPU backend.
let sh = if align_corners {
if oh > 1 {
(ih - 1) as f64 / (oh - 1) as f64
} else {
0.0
}
} else {
scale_h.map(|s| 1.0 / s).unwrap_or(ih as f64 / oh as f64)
};
let sw = if align_corners {
if ow > 1 {
(iw - 1) as f64 / (ow - 1) as f64
} else {
0.0
}
} else {
scale_w.map(|s| 1.0 / s).unwrap_or(iw as f64 / ow as f64)
};
let out = self.device.alloc_f32(b * c * oh * ow)?;
let mut push = push_u32(&[
b as u32,
c as u32,
ih as u32,
iw as u32,
oh as u32,
ow as u32,
align_corners as u32,
]);
push.extend_from_slice(&(sh as f32).to_ne_bytes());
push.extend_from_slice(&(sw as f32).to_ne_bytes());
self.device.dispatch(
"upsample_bilinear2d",
&[inp.buffer, out.buffer],
&push,
Self::groups_1d(b * c * oh * ow),
)?;
Ok(out)
}
fn gather(&self, l: &Layout, ids: &Self, ids_l: &Layout, dim: usize) -> Result<Self> {
if ids.dtype != DType::U32 {
crate::bail!("vulkan: gather requires u32 ids, got {:?}", ids.dtype);
}
let src = self.contiguous(l)?;
// u32 ids: use contiguous_u32 (bit-exact). The float strided_copy would reinterpret small
// ids as denormal floats that load-time flush-to-zero can corrupt (see contiguous_u32).
let idc = ids.contiguous_u32(ids_l)?;
let out_dims = ids_l.dims();
let src_dims = l.dims();
let right: usize = out_dims[dim + 1..].iter().product();
let dim_out = out_dims[dim];
let dim_src = src_dims[dim];
let n = ids_l.shape().elem_count();
let out = self.device.alloc_f32(n)?;
self.device.dispatch(
"gather",
&[src.buffer, idc.buffer, out.buffer],
&push_u32(&[n as u32, right as u32, dim_out as u32, dim_src as u32]),
Self::groups_1d(n),
)?;
Ok(out)
}
fn scatter_set(
&mut self,
l: &Layout,
ids: &Self,
ids_l: &Layout,
src: &Self,
src_l: &Layout,
dim: usize,
) -> Result<()> {
self.scatter_impl("scatter_set", l, ids, ids_l, src, src_l, dim)
}
fn scatter_add_set(
&mut self,
l: &Layout,
ids: &Self,
ids_l: &Layout,
src: &Self,
src_l: &Layout,
dim: usize,
) -> Result<()> {
self.scatter_impl("scatter_add_set", l, ids, ids_l, src, src_l, dim)
}
fn index_select(&self, ids: &Self, l: &Layout, ids_l: &Layout, dim: usize) -> Result<Self> {
if ids.dtype != DType::U32 {
crate::bail!("vulkan: index_select requires u32 ids, got {:?}", ids.dtype);
}
// The kernel reads ids linearly (ids[i]), so materialize a contiguous, offset-0 copy when
// the ids layout isn't already one.
let mut idk = None;
let ids_buf = if ids_l.is_contiguous() && ids_l.start_offset() == 0 {
ids.buffer
} else {
let s = ids.contiguous_u32(ids_l)?;
let b = s.buffer;
idk = Some(s);
b
};
let _ = &idk; // keep the materialized ids alive until the dispatch is recorded
// Only materialize the source contiguous when it ISN'T already packed at offset 0. The kernel
// gathers whole `right`-sized rows by their leading index, so a source that is already
// contiguous is read directly -- a blind `contiguous()` here copied the WHOLE source every call
// (e.g. the [max_pos=262144, 64] RoPE cos/sin cache, 67MB, materialized twice per layer just to
// gather one position -- 62% of decode GPU time). Non-contiguous sources still get one copy.
let mut srck = None;
let src_buf = if l.is_contiguous() && l.start_offset() == 0 {
self.buffer
} else {
let s = self.contiguous(l)?;
let b = s.buffer;
srck = Some(s);
b
};
let _ = &srck;
let dims = l.dims();
let left: usize = dims[..dim].iter().product();
let dim_size = dims[dim];
let right: usize = dims[dim + 1..].iter().product();
let n_ids = ids_l.shape().elem_count();
let total = left * n_ids * right;
let out = self.device.alloc_f32(total)?;
let push = push_u32(&[left as u32, dim_size as u32, right as u32, n_ids as u32]);
self.device.dispatch(
"index_select",
&[ids_buf, src_buf, out.buffer],
&push,
Self::groups_1d(total),
)?;
Ok(out)
}
fn index_add(
&self,
l: &Layout,
ids: &Self,
ids_l: &Layout,
src: &Self,
src_l: &Layout,
dim: usize,
) -> Result<Self> {
// index_add: out = self.clone(); out[.., ids[i], ..] += src[.., i, ..] along `dim`. ids is
// 1D of length src.dims()[dim]. We reuse the device scatter_add kernel, which wants ids
// shaped like src, so broadcast the 1D ids across src's pre/post dims first. ids is tiny
// (one entry per src dim slot) so the broadcast is built on the host and re-uploaded.
if ids.dtype != DType::U32 {
crate::bail!("vulkan: index_add requires u32 ids, got {:?}", ids.dtype);
}
let ids_host = ids.to_vec_u32()?;
let ids_host = match ids_l.contiguous_offsets() {
Some((a, b)) => &ids_host[a..b],
None => crate::bail!("vulkan: index_add requires contiguous ids"),
};
let src_dims = src_l.dims();
let dim_src = src_dims[dim];
if ids_host.len() != dim_src {
crate::bail!(
"vulkan: index_add ids len {} != src dim {dim} size {dim_src}",
ids_host.len()
);
}
let pre: usize = src_dims[..dim].iter().product();
let post: usize = src_dims[dim + 1..].iter().product();
let mut ids_full = vec![0u32; pre * dim_src * post];
for p in 0..pre {
for (s, &id) in ids_host.iter().enumerate() {
let base = (p * dim_src + s) * post;
for r in 0..post {
ids_full[base + r] = id;
}
}
}
let ids_full = self.device.upload_u32(&ids_full)?;
let ids_full_layout = Layout::contiguous(src_dims);
// Fresh contiguous copy of self to accumulate into (scatter writes binding 0 in place).
let mut out = self.contiguous(l)?;
out.scatter_add_set(
&Layout::contiguous(l.dims()),
&ids_full,
&ids_full_layout,
src,
src_l,
dim,
)?;
Ok(out)
}
fn matmul(
&self,
rhs: &Self,
(b, m, n, k): (usize, usize, usize, usize),
lhs_l: &Layout,
rhs_l: &Layout,
) -> Result<Self> {
// lhs packed [b,m,k]: use directly if contiguous from offset 0, else materialize.
let mut lkeep = None;
let lc_buf = if lhs_l.is_contiguous() && lhs_l.start_offset() == 0 {
self.buffer
} else {
let s = self.contiguous(lhs_l)?;
let bf = s.buffer;
lkeep = Some(s);
bf
};
// rhs is [b,k,n]. A Linear passes W.t() where W is contiguous [b,n,k]; detect that
// transposed-contiguous layout and feed W's natural [n,k] buffer to an NT kernel -- skipping
// the transpose strided_copy, which the bench showed is 14-23x the matmul itself and the
// dominant per-matmul cost in a forward. Otherwise use the buffer directly (already packed)
// or materialize a [b,k,n] copy.
let d = rhs_l.dims();
let st = rhs_l.stride();
let nt = rhs_l.start_offset() == 0
&& ((d.len() == 2 && d[0] == k && d[1] == n && st[0] == 1 && st[1] == k)
|| (d.len() == 3
&& d[0] == b
&& d[1] == k
&& d[2] == n
&& st[0] == n * k
&& st[1] == 1
&& st[2] == k));
let mut rkeep = None;
let rc_buf = if nt || (rhs_l.is_contiguous() && rhs_l.start_offset() == 0) {
rhs.buffer
} else {
let s = rhs.contiguous(rhs_l)?;
let bf = s.buffer;
rkeep = Some(s);
bf
};
let _ = (&lkeep, &rkeep); // keep any materialized copies alive until the dispatch is recorded
// C[b,m,n] = A[b,m,k] * B[b,k,n] (B = W[n,k]^T when nt), row-major. Push order {batch,m,k,n}.
let out = self.device.alloc_f32(b * m * n)?;
// f32 GEMV fast path: a single-row (m==1) matmul against a transposed-contiguous weight W[n,k]
// (the `nt` case) is out[n] = W[n,k] @ x[k] -- e.g. the MoE router gate [128,4096]@[4096]. The
// tiled GEMM runs this at ~2 occupancy-starved workgroups (m=1 wastes 63/64 of every 64x64
// tile); the block-reduce `gemv` kernel runs it as n workgroups (~30x). rc_buf is W's natural
// [n,k] buffer (nt), lc_buf is x[k]. meta/out drop here but park in the BufPool (reclaim is
// post-fence-only), so they outlive the deferred dispatch.
if nt && b == 1 && m == 1 {
let meta = self.device.upload_u32(&[k as u32])?;
self.device.dispatch_out(
"gemv",
&[rc_buf, lc_buf, out.buffer, meta.buffer],
2,
&[],
(n as u32, 1, 1),
)?;
return Ok(out);
}
let push = push_u32(&[b as u32, m as u32, k as u32, n as u32]);
// Matrix-core path: 16x16x16 fp16 coopmat when every tile dim is a multiple of 16. Casts
// operands to fp16 (as llama.cpp does) and runs the register-blocked WMMA GEMM.
//
// The fp16 scratch (a16 + b16) is an extra, transient 2 B/elem allocation on top of the
// already-resident operands and output. On the UMA part a large model already fills most of
// the GTT heap, so a big GEMM's scratch can be the allocation that tips over the edge. Guard
// it: if the two scratch buffers won't fit in the largest usable heap's *free* bytes (plus a
// margin), fall through to the fp32 tiled GEMM, which needs no extra f16 buffers — correct
// result, just slower, instead of an OOM abort.
let scratch_bytes =
((b * m * k * 2).max(4) as u64).saturating_add((b * k * n * 2).max(4) as u64);
if self.device.inner.cm_use
&& self.device.scratch_fits(scratch_bytes)
&& matches!(self.device.coopmat_info(), Some((16, 16, 16)))
&& m % 16 == 0
&& n % 16 == 0
&& k % 16 == 0
{
let (a16, a16_mem, a16_hv, a16_bytes) = self.device.alloc_f16(b * m * k)?;
let (b16, b16_mem, b16_hv, b16_bytes) = self.device.alloc_f16(b * k * n)?;
self.device.dispatch(
"cast_f2h",
&[lc_buf, a16],
&push_u32(&[(b * m * k) as u32]),
Self::groups_1d(b * m * k),
)?;
self.device.dispatch(
"cast_f2h",
&[rc_buf, b16],
&push_u32(&[(b * k * n) as u32]),
Self::groups_1d(b * k * n),
)?;
let mt = (m / 16) as u32;
let nt_tiles = (n / 16) as u32;
let groups = (nt_tiles.div_ceil(4), mt.div_ceil(4), b as u32);
let kernel = if nt {
"bmm_coopmat_rb_nt"
} else {
"bmm_coopmat_rb"
};
self.device
.dispatch(kernel, &[a16, b16, out.buffer], &push, groups)?;
// Scratch is dead after the kernel reads it; return to the pool (reclaimed post-flush).
self.device.free_scratch(a16_bytes, a16, a16_mem, a16_hv);
self.device.free_scratch(b16_bytes, b16, b16_mem, b16_hv);
return Ok(out);
}
// Register-blocked fp32 tiled GEMM (64x64 tile, 4x4 per thread); NT variant reads W[n,k].
let groups = ((n as u32).div_ceil(64), (m as u32).div_ceil(64), b as u32);
let kernel = if nt { "bmm_reg_nt" } else { "bmm_reg" };
self.device
.dispatch(kernel, &[lc_buf, rc_buf, out.buffer], &push, groups)?;
Ok(out)
}
fn copy_strided_src(&self, dst: &mut Self, dst_offset: usize, src_l: &Layout) -> Result<()> {
let n = src_l.shape().elem_count();
if n == 0 {
return Ok(());
}
if dst.count() < dst_offset + n {
crate::bail!(
"vulkan: copy_strided_src dst too small ({} < {})",
dst.count(),
dst_offset + n
);
}
// Materialize src_l (any strided/broadcast layout) into dst starting at dst_offset (the
// KV-cache append pattern: write new tokens' K/V at an offset into the cache buffer).
let dims = src_l.dims();
let rank = dims.len();
if rank > 6 {
crate::bail!("vulkan: copy_strided_src supports rank <= 6, got {rank}");
}
let strides = src_l.stride();
let mut p = vec![
n as u32,
rank as u32,
src_l.start_offset() as u32,
dst_offset as u32,
];
let mut shape6 = [0u32; 6];
let mut stride6 = [0u32; 6];
for d in 0..rank {
shape6[d] = dims[d] as u32;
stride6[d] = strides[d] as u32;
}
p.extend_from_slice(&shape6);
p.extend_from_slice(&stride6);
self.device.dispatch(
"strided_copy",
&[self.buffer, dst.buffer],
&push_u32(&p),
Self::groups_1d(n),
)
}
fn copy2d(
&self,
dst: &mut Self,
d1: usize,
d2: usize,
src_stride1: usize,
dst_stride1: usize,
src_offset: usize,
dst_offset: usize,
) -> Result<()> {
// Native on-GPU 2D strided block copy: d1 rows of d2 contiguous elems, per-row
// src/dst strides + base offsets. This is the cat / slice_set primitive (KV-cache
// append + GQA repeat_kv every layer), previously the single hottest GPU<->CPU
// round-trip in the forward (each call read both buffers to the host, copied on CPU,
// re-uploaded). The `copy2d` kernel is uint-typed so it's bit-exact for f32 AND u32
// storage. The kernel only writes the addressed elements, leaving the rest of `dst`
// intact, exactly like the previous read-modify-write, so successive copies compose.
let total = d1 * d2;
if total == 0 {
return Ok(());
}
if self.device.inner.profile {
eprintln!(
"[VK_PROFILE] copy2d(native): d1={d1} d2={d2} elems={total} \
(was a CPU round-trip; now on-GPU)"
);
}
let push = push_u32(&[
d1 as u32,
d2 as u32,
src_stride1 as u32,
dst_stride1 as u32,
src_offset as u32,
dst_offset as u32,
]);
self.device.dispatch(
"copy2d",
&[self.buffer, dst.buffer],
&push,
Self::groups_1d(total),
)
}
fn const_set(&mut self, s: crate::scalar::Scalar, layout: &Layout) -> Result<()> {
// Fast path: a contiguous, offset-0 view that covers the WHOLE buffer (the
// Tensor::full / ones / fill primitive) is filled on-GPU by const_fill, no readback.
// The value is passed as raw 32-bit bits so one uint kernel serves f32 and u32 storage
// bit-exactly. Every element is written, so nothing outside the addressed set exists to
// preserve -- the read-modify-write the slow path needs is unnecessary here.
let n = layout.shape().elem_count();
if layout.is_contiguous() && layout.start_offset() == 0 && n == self.count {
let bits: u32 = if self.dtype == DType::U32 || self.dtype == DType::U8 {
s.to_f64() as u32
} else {
(s.to_f64() as f32).to_bits()
};
return self.device.dispatch(
"const_fill",
&[self.buffer],
&push_u32(&[n as u32, bits]),
Self::groups_1d(n),
);
}
// Slow path (partial / strided / offset view): host-visible read-modify-write so elements
// outside the addressed set are preserved. u32 storage is set as an integer; everything
// else (f32, and f16/bf16 which live as f32) as a float. Named for the profiler since it
// still round-trips.
self.device.profile_fallback(
"const_set",
format_args!("elems={n} buf={} (partial/strided)", self.count),
);
if self.dtype == DType::U32 || self.dtype == DType::U8 {
let v = s.to_f64() as u32;
let mut data = self.to_vec_u32()?;
for i in layout.strided_index() {
if i >= data.len() {
crate::bail!("vulkan: const_set out of range");
}
data[i] = v;
}
unsafe {
self.device
.write_u32(self.buffer, self.memory, self.host_visible, &data)
}
} else {
let v = s.to_f64() as f32;
let mut data = self.to_vec_f32()?;
for i in layout.strided_index() {
if i >= data.len() {
crate::bail!("vulkan: const_set out of range");
}
data[i] = v;
}
unsafe {
self.device
.write_f32(self.buffer, self.memory, self.host_visible, &data)
}
}
}
}
// Evolutionary autotuner hunt over the Q4_K coopmat prefill genome. A child of this module (so it
// reaches the pub(crate)/private dispatch surface) that reuses the hanzo-kernel `tune` evolutionary
// search as a dev-dependency. Test/tuner-only.
#[cfg(test)]
#[path = "coopmat_hunt.rs"]
mod coopmat_hunt;
#[cfg(test)]
mod dsl_dispatch_proof {
/// Enumerate every VkCooperativeMatrixPropertiesKHR config the device advertises and print the
/// A/B/C/result component types + M/N/K. Documents whether the f16-accumulate variant
/// (A=f16 B=f16 C=f16 result=f16, 16x16x16, subgroup) exists -- the prerequisite for the f16-acc
/// coopmat prefill GEMMs. vulkaninfo on some Mesa builds prints only the feature summary, not the
/// per-config list, so this is the authoritative probe. Never fails; it only reports.
#[test]
fn probe_coopmat_configs() {
unsafe {
let entry = match ash::Entry::load() {
Ok(e) => e,
Err(e) => { eprintln!("[cm-probe] no vulkan loader ({e}); skipping"); return; }
};
let app = vk::ApplicationInfo::default().api_version(vk::make_api_version(0, 1, 3, 0));
let instance = match entry
.create_instance(&vk::InstanceCreateInfo::default().application_info(&app), None)
{
Ok(i) => i,
Err(e) => { eprintln!("[cm-probe] no instance ({e}); skipping"); return; }
};
let ct = |c: vk::ComponentTypeKHR| -> &'static str {
match c {
vk::ComponentTypeKHR::FLOAT16 => "f16",
vk::ComponentTypeKHR::FLOAT32 => "f32",
vk::ComponentTypeKHR::SINT8 => "i8",
vk::ComponentTypeKHR::UINT8 => "u8",
vk::ComponentTypeKHR::SINT32 => "i32",
vk::ComponentTypeKHR::UINT32 => "u32",
_ => "?",
}
};
for pd in instance.enumerate_physical_devices().unwrap_or_default() {
let p = instance.get_physical_device_properties(pd);
let name = CStr::from_ptr(p.device_name.as_ptr()).to_string_lossy().into_owned();
if p.device_type == vk::PhysicalDeviceType::CPU || name.to_lowercase().contains("llvmpipe") {
continue;
}
let exts = instance.enumerate_device_extension_properties(pd).unwrap_or_default();
let has_cm = exts.iter().any(|e| {
CStr::from_ptr(e.extension_name.as_ptr()) == ash::khr::cooperative_matrix::NAME
});
if !has_cm {
eprintln!("[cm-probe] {name}: no VK_KHR_cooperative_matrix");
continue;
}
let cm = ash::khr::cooperative_matrix::Instance::new(&entry, &instance);
let props = cm.get_physical_device_cooperative_matrix_properties(pd).unwrap_or_default();
eprintln!("[cm-probe] {name}: {} coopmat configs", props.len());
let mut has_f16acc = false;
let mut has_f32acc = false;
for cfg in &props {
let f16acc = cfg.a_type == vk::ComponentTypeKHR::FLOAT16
&& cfg.b_type == vk::ComponentTypeKHR::FLOAT16
&& cfg.c_type == vk::ComponentTypeKHR::FLOAT16
&& cfg.result_type == vk::ComponentTypeKHR::FLOAT16
&& (cfg.m_size, cfg.n_size, cfg.k_size) == (16, 16, 16)
&& cfg.scope == vk::ScopeKHR::SUBGROUP;
let f32acc = cfg.a_type == vk::ComponentTypeKHR::FLOAT16
&& cfg.b_type == vk::ComponentTypeKHR::FLOAT16
&& cfg.c_type == vk::ComponentTypeKHR::FLOAT32
&& cfg.result_type == vk::ComponentTypeKHR::FLOAT32
&& (cfg.m_size, cfg.n_size, cfg.k_size) == (16, 16, 16)
&& cfg.scope == vk::ScopeKHR::SUBGROUP;
has_f16acc |= f16acc;
has_f32acc |= f32acc;
eprintln!(
"[cm-probe] {}x{}x{} A={} B={} C={} D={} scope={}{}{}",
cfg.m_size, cfg.n_size, cfg.k_size,
ct(cfg.a_type), ct(cfg.b_type), ct(cfg.c_type), ct(cfg.result_type),
cfg.scope.as_raw(),
if f16acc { " <- f16-acc 16x16x16 subgroup" } else { "" },
if f32acc { " <- f32-acc 16x16x16 subgroup" } else { "" },
);
}
eprintln!("[cm-probe] {name}: f16acc_16x16x16_subgroup={has_f16acc} f32acc_16x16x16_subgroup={has_f32acc}");
}
instance.destroy_instance(None);
}
}
use super::*;
// Proof that the CubeCL kernel DSL plugs into the engine as a CODE GENERATOR, not a second
// runtime: a `#[kernel]`-authored elementwise-mul kernel, compiled by cubecl to SPIR-V (entry
// renamed `main`, cubecl's unused info buffer stripped via spirv-opt --remove-unused-interface-
// variables), is dispatched through hanzo-ml's OWN VulkanDevice::dispatch and matches the CPU
// reference bit-exactly -- using the same pipeline/descriptor/command-buffer path every hand-
// written `.comp` shader uses. The DSL .spv lives at src/vulkan/spv/dsl_mul.spv, registered in
// kernel_spv as "dsl_mul".
#[test]
fn dsl_generated_kernel_runs_through_ml_vulkan() {
const N: usize = 256;
const WG: u32 = 64;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[dsl-proof] no vulkan device ({e}); skipping");
return;
}
};
let x: Vec<f32> = (0..N).map(|i| i as f32 * 0.5 - 3.0).collect();
let w: Vec<f32> = (0..N).map(|i| i as f32 * 0.01 + 1.0).collect();
let refout: Vec<f32> = x.iter().zip(&w).map(|(a, b)| a * b).collect();
let xs = dev.upload_f32(&x).unwrap();
let ws = dev.upload_f32(&w).unwrap();
let out = dev.alloc_f32(N).unwrap();
let groups = (N as u32).div_ceil(WG);
dev.dispatch(
"dsl_mul",
&[xs.buffer, ws.buffer, out.buffer],
&[],
(groups, 1, 1),
)
.unwrap();
dev.flush().unwrap();
let got = out.to_vec_f32().unwrap();
let maxerr = got
.iter()
.zip(&refout)
.map(|(a, b)| (a - b).abs())
.fold(0.0f32, f32::max);
eprintln!("[dsl-proof] DSL kernel via ml::VulkanDevice::dispatch N={N} groups={groups} maxerr={maxerr:.2e}");
eprintln!(
"[dsl-proof] first4 got={:?} ref={:?}",
&got[..4],
&refout[..4]
);
assert_eq!(
maxerr, 0.0,
"DSL-generated kernel not bit-exact through ml dispatch"
);
}
// Generalization: a REDUCTION kernel (matvec, comptime k=32/rows=64) auto-processed by the
// reusable spv_to_ml codegen (entry->main, cubecl info-var removed from the entry interface),
// dispatched through ml's own VulkanDevice. Proves the codegen isn't limited to elementwise.
// Note: cubecl encodes runtime scalars as a buffer binding, not push constants -- so kernels use
// comptime dims here; ml would bind a small scalar SSBO for the runtime-param path.
#[test]
fn dsl_matvec_runs_through_ml_vulkan() {
const K: usize = 32;
const ROWS: usize = 64;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[dsl-proof] no vulkan device ({e}); skipping");
return;
}
};
let w: Vec<f32> = (0..ROWS * K)
.map(|i| (i as f32 % 7.0) * 0.25 - 1.0)
.collect();
let x: Vec<f32> = (0..K).map(|i| (i as f32 % 5.0) * 0.5 - 1.0).collect();
let refout: Vec<f32> = (0..ROWS)
.map(|r| {
let mut acc = 0.0f32;
for i in 0..K {
acc += w[r * K + i] * x[i];
}
acc
})
.collect();
let ws = dev.upload_f32(&w).unwrap();
let xs = dev.upload_f32(&x).unwrap();
let out = dev.alloc_f32(ROWS).unwrap();
dev.dispatch(
"dsl_matvec",
&[ws.buffer, xs.buffer, out.buffer],
&[],
(1, 1, 1),
)
.unwrap();
dev.flush().unwrap();
let got = out.to_vec_f32().unwrap();
let maxerr = got
.iter()
.zip(&refout)
.map(|(a, b)| (a - b).abs())
.fold(0.0f32, f32::max);
eprintln!("[dsl-proof] DSL matvec via ml::VulkanDevice::dispatch rows={ROWS} k={K} maxerr={maxerr:.2e}");
eprintln!(
"[dsl-proof] first4 got={:?} ref={:?}",
&got[..4],
&refout[..4]
);
assert!(
maxerr < 1e-4,
"DSL matvec through ml diverged: maxerr={maxerr:.3e}"
);
}
// The production `rms_norm` method now dispatches the DSL block-per-row kernel (rms_norm_blk.spv):
// one workgroup/row, 256 threads, coalesced reads + shared-mem reduce -- 10.86x the naive
// one-invocation-per-row `.comp` it replaced. This tests it through the REAL method, whose eps/ndim
// ride pooled SSBOs (cubecl has no push-constants) with NO explicit synchronize before they drop:
// proving the BufPool retains them across the deferred batch (park-on-drop; reclaim is
// post-fence-only). Chaining calls stresses that lifetime -- all-bit-exact vs CPU == no use-after-free.
#[test]
fn rms_norm_production_path_is_dsl_kernel_bit_exact() {
const N: usize = 4096;
const ROWS: usize = 4096;
const EPS: f32 = 1e-6;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[rms_norm-prod] no vulkan device ({e}); skipping");
return;
}
};
let gen_x = |row: usize, i: usize| (((row * 7 + i * 13) % 1000) as f32) / 500.0 - 1.0;
let gen_w = |i: usize| (((i * 3) % 100) as f32) / 100.0 + 0.5;
let x: Vec<f32> = (0..ROWS * N).map(|idx| gen_x(idx / N, idx % N)).collect();
let w: Vec<f32> = (0..N).map(gen_w).collect();
let mut cpu = vec![0f32; ROWS * N];
for r in 0..ROWS {
let ss: f32 = (0..N).map(|i| x[r * N + i] * x[r * N + i]).sum();
let denom = (ss / N as f32 + EPS).sqrt();
for i in 0..N {
cpu[r * N + i] = x[r * N + i] / denom * w[i];
}
}
let xs = dev.upload_f32(&x).unwrap();
let ws = dev.upload_f32(&w).unwrap();
let x_l = Layout::contiguous((ROWS, N));
let w_l = Layout::contiguous(N);
// Chain 8 production calls with NO intermediate sync: 8 recorded dispatches + 16 pooled eps/ndim
// buffers dropped into `pending`. If any were freed/reused mid-batch, an earlier dispatch's
// scalars would corrupt its output. Hold the outs, then read (the first readback flushes+fences).
let mut outs = Vec::new();
for _ in 0..8 {
outs.push(xs.rms_norm(&x_l, &ws, &w_l, EPS).unwrap());
}
let mut worst = 0f32;
for out in &outs {
let got = out.to_vec_f32().unwrap();
let err = got
.iter()
.zip(&cpu)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max);
worst = worst.max(err);
}
eprintln!("[rms_norm-prod] {ROWS}x{N} x8-batched maxerr={worst:.2e} (DSL kernel via production path, pooled scalars, no sync crutch)");
assert!(
worst < 1e-3,
"production rms_norm (DSL kernel) diverged from CPU: {worst:.3e}"
);
// Clean kernel-only bench: FIXED buffers (no per-iter 64MB alloc/upload) dispatched in a loop
// with one fence -- the true kernel cost, not the method's allocation overhead. Same 5-SSBO /
// (nrows,1,1) shape the production `rms_norm` invokes.
let iters = 50;
let bytes = (3 * ROWS * N * 4) as f64; // read x twice + write out (w negligible)
let out = dev.alloc_f32(ROWS * N).unwrap();
let epsb = dev.upload_f32(&[EPS]).unwrap();
let ndb = dev.upload_u32(&[N as u32]).unwrap();
let bufs = [xs.buffer, ws.buffer, out.buffer, epsb.buffer, ndb.buffer];
let grid = (ROWS as u32, 1, 1);
dev.dispatch_out("rms_norm", &bufs, 2, &[], grid).unwrap();
dev.synchronize().unwrap();
let t = std::time::Instant::now();
for _ in 0..iters {
dev.dispatch_out("rms_norm", &bufs, 2, &[], grid).unwrap();
}
dev.synchronize().unwrap();
let ms = t.elapsed().as_secs_f64() * 1e3 / iters as f64;
eprintln!(
"[rms_norm-prod] {ROWS}x{N} kernel {ms:.3} ms ({:.0} GB/s) -- the DSL block kernel is now the production rms_norm",
bytes / (ms * 1e6)
);
}
// Qwen3 decode garbles on Vulkan while qwen2 is coherent (same backend) and qwen3-on-CPU is
// coherent. The ONE structural qwen3 addition upstream of attention is per-head q/k RMSNorm
// (qk-norm) over head_dim, which qwen2 never exercises: it feeds `rms_norm` a many-row /
// small-column shape ([b,h,t,128]) the 4096x4096 test above never covers, then `rope`. This
// gates BOTH Vulkan kernels at the live decode/prefill shapes against a hand CPU oracle, plus
// the rms_norm->rope chain (buffer-lifetime across the deferred batch).
fn cpu_rms_norm(x: &[f32], w: &[f32], rows: usize, m: usize, eps: f32) -> Vec<f32> {
let mut out = vec![0f32; rows * m];
for r in 0..rows {
let ss: f32 = (0..m).map(|i| x[r * m + i] * x[r * m + i]).sum();
let denom = (ss / m as f32 + eps).sqrt();
for i in 0..m {
out[r * m + i] = x[r * m + i] / denom * w[i];
}
}
out
}
// NeoX rope reference matching rope.comp exactly. src [b,h,t,d]; cos/sin [t,d/2] (batched=false)
// or [b,t,d/2] (batched=true).
fn cpu_neox_rope(
src: &[f32],
cos: &[f32],
sin: &[f32],
b: usize,
h: usize,
t: usize,
d: usize,
batched: bool,
) -> Vec<f32> {
let hd = d / 2;
let mut out = vec![0f32; b * h * t * d];
for bh_i in 0..b * h {
let b_i = bh_i / h;
for i_t in 0..t {
for i_d in 0..hd {
let sbase = bh_i * t * d;
let i1 = sbase + i_t * d + i_d;
let i2 = i1 + hd;
let mut i_cs = i_t * hd + i_d;
if batched {
i_cs += b_i * (t * hd);
}
let (c, s) = (cos[i_cs], sin[i_cs]);
let (x1, x2) = (src[i1], src[i2]);
out[i1] = x1 * c - x2 * s;
out[i2] = x1 * s + x2 * c;
}
}
}
out
}
#[test]
fn qwen3_qk_norm_and_rope_match_cpu_at_decode_shapes() {
const EPS: f32 = 1e-6;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[qwen3-qknorm] no vulkan device ({e}); skipping");
return;
}
};
let genf = |seed: usize, i: usize| (((seed * 131 + i * 17) % 1000) as f32) / 500.0 - 1.0;
// (1) qk-norm rms_norm at the exact decode shapes: q = 32 heads x 128, k = 8 heads x 128.
for (rows, tag) in [(32usize, "q"), (8usize, "k")] {
const M: usize = 128;
let x: Vec<f32> = (0..rows * M).map(|i| genf(1, i)).collect();
let w: Vec<f32> = (0..M).map(|i| 0.5 + genf(2, i) * 0.1).collect();
let cpu = cpu_rms_norm(&x, &w, rows, M, EPS);
let xs = dev.upload_f32(&x).unwrap();
let ws = dev.upload_f32(&w).unwrap();
let got = xs
.rms_norm(&Layout::contiguous((rows, M)), &ws, &Layout::contiguous(M), EPS)
.unwrap()
.to_vec_f32()
.unwrap();
let maxerr = got
.iter()
.zip(&cpu)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max);
eprintln!("[qwen3-qknorm] rms_norm {tag} {rows}x{M} maxerr={maxerr:.3e}");
assert!(maxerr < 1e-4, "rms_norm {tag} {rows}x{M} diverged: {maxerr:.3e}");
}
// (2) NeoX rope at decode (t=1) and prefill (t=8) shapes, both 2D [t,d/2] and 3D [b,t,d/2].
const D: usize = 128;
const HD: usize = D / 2;
for (b, h, t) in [(1usize, 32usize, 1usize), (1, 8, 1), (1, 32, 8), (1, 8, 8)] {
let src: Vec<f32> = (0..b * h * t * D).map(|i| genf(3, i)).collect();
// realistic cos/sin: cos^2+sin^2=1 from an angle table
let mut cos2d = vec![0f32; t * HD];
let mut sin2d = vec![0f32; t * HD];
for it in 0..t {
for id in 0..HD {
let ang = (it as f32 + 1.0) / 10000f32.powf(2.0 * id as f32 / D as f32);
cos2d[it * HD + id] = ang.cos();
sin2d[it * HD + id] = ang.sin();
}
}
let srcs = dev.upload_f32(&src).unwrap();
// 2D cos/sin -> unbatched=0
let c2 = dev.upload_f32(&cos2d).unwrap();
let s2 = dev.upload_f32(&sin2d).unwrap();
let got2 = srcs
.rope(
&Layout::contiguous((b, h, t, D)),
&c2,
&Layout::contiguous((t, HD)),
&s2,
&Layout::contiguous((t, HD)),
)
.unwrap()
.to_vec_f32()
.unwrap();
let cpu2 = cpu_neox_rope(&src, &cos2d, &sin2d, b, h, t, D, false);
let e2 = got2
.iter()
.zip(&cpu2)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max);
eprintln!("[qwen3-qknorm] rope2D b{b}h{h}t{t} maxerr={e2:.3e}");
assert!(e2 < 1e-4, "rope 2D b{b}h{h}t{t} diverged: {e2:.3e}");
// 3D cos/sin [b,t,d/2] -> unbatched=1 (the decode gather shape)
let got3 = srcs
.rope(
&Layout::contiguous((b, h, t, D)),
&c2,
&Layout::contiguous((b, t, HD)),
&s2,
&Layout::contiguous((b, t, HD)),
)
.unwrap()
.to_vec_f32()
.unwrap();
let cpu3 = cpu_neox_rope(&src, &cos2d, &sin2d, b, h, t, D, true);
let e3 = got3
.iter()
.zip(&cpu3)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max);
eprintln!("[qwen3-qknorm] rope3D b{b}h{h}t{t} maxerr={e3:.3e}");
assert!(e3 < 1e-4, "rope 3D b{b}h{h}t{t} diverged: {e3:.3e}");
}
// (3) The chain: rms_norm(q) then rope(., cos, sin) at the decode shape, no intermediate sync
// (stresses buffer lifetime across the deferred batch, the qwen3 live path).
{
let (b, h, t) = (1usize, 32usize, 1usize);
let x: Vec<f32> = (0..b * h * t * D).map(|i| genf(5, i)).collect();
let w: Vec<f32> = (0..D).map(|i| 0.5 + genf(6, i) * 0.1).collect();
let mut cos2d = vec![0f32; t * HD];
let mut sin2d = vec![0f32; t * HD];
for id in 0..HD {
let ang = 3.0 / 10000f32.powf(2.0 * id as f32 / D as f32);
cos2d[id] = ang.cos();
sin2d[id] = ang.sin();
}
let normed = cpu_rms_norm(&x, &w, b * h * t, D, EPS);
let cpu = cpu_neox_rope(&normed, &cos2d, &sin2d, b, h, t, D, false);
let xs = dev.upload_f32(&x).unwrap();
let ws = dev.upload_f32(&w).unwrap();
let cs = dev.upload_f32(&cos2d).unwrap();
let ss = dev.upload_f32(&sin2d).unwrap();
let normed_gpu = xs
.rms_norm(&Layout::contiguous((b * h * t, D)), &ws, &Layout::contiguous(D), EPS)
.unwrap();
let got = normed_gpu
.rope(
&Layout::contiguous((b, h, t, D)),
&cs,
&Layout::contiguous((t, HD)),
&ss,
&Layout::contiguous((t, HD)),
)
.unwrap()
.to_vec_f32()
.unwrap();
let e = got
.iter()
.zip(&cpu)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max);
eprintln!("[qwen3-qknorm] rms_norm->rope chain b{b}h{h}t{t} maxerr={e:.3e}");
assert!(e < 1e-4, "rms_norm->rope chain diverged: {e:.3e}");
}
}
// Qwen3-4B decodes garbage on Vulkan while qwen2 (same backend) and qwen3-on-CPU are coherent;
// the root cause is the dense Q4_K decode matvec. This gates the shipped default (column dp4a,
// `mul_mat_q4k_dp4a`) bit-exact vs a CPU dequant matvec at the exact qwen3-4B projection shapes
// (k=2560 for q/k/gate/up, k=4096 for o) plus a qwen2 control (k=2048).
#[test]
fn dense_q4k_decode_matches_cpu_at_qwen3_shapes() {
use crate::quantized::k_quants::{BlockQ4K, GgmlType};
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[q4k-blk-dense] no vulkan device ({e}); skipping");
return;
}
};
let gen_w = |row: usize, i: usize| (((row * 13 + i * 7) % 1000) as f32) / 500.0 - 1.0;
let gen_x = |i: usize| (((i * 17 + 5) % 800) as f32) / 400.0 - 1.0;
// (nout, k): qwen3-4B q_proj(4096,2560) k/v_proj(1024,2560) o_proj(2560,4096); qwen2 ctrl(2048,2048).
for (nout, k) in [
(4096usize, 2560usize),
(1024, 2560),
(2560, 4096),
(2048, 2048),
] {
let nb = k / 256;
let mut blocks: Vec<BlockQ4K> =
(0..nout * nb).map(|_| unsafe { std::mem::zeroed() }).collect();
let mut wdeq = vec![0f32; nout * k];
for r in 0..nout {
let rowf: Vec<f32> = (0..k).map(|i| gen_w(r, i)).collect();
BlockQ4K::from_float(&rowf, &mut blocks[r * nb..(r + 1) * nb]);
BlockQ4K::to_float(&blocks[r * nb..(r + 1) * nb], &mut wdeq[r * k..(r + 1) * k]);
}
let u32s: &[u32] = unsafe {
std::slice::from_raw_parts(blocks.as_ptr() as *const u32, blocks.len() * 36)
};
let wq = dev.upload_u32(u32s).unwrap();
let x: Vec<f32> = (0..k).map(gen_x).collect();
let mut cpu = vec![0f32; nout];
for (r, c) in cpu.iter_mut().enumerate() {
*c = (0..k).map(|i| wdeq[r * k + i] * x[i]).sum();
}
let maxref = cpu.iter().fold(0f32, |m, &v| m.max(v.abs())).max(1e-30);
let rel = |got: &[f32]| {
got.iter()
.zip(&cpu)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max)
/ maxref
};
let got = dev.matvec_q4k(&wq, &x, nout, k).unwrap(); // production default = column dp4a
let rdef = rel(&got);
eprintln!("[q4k-decode] default nout={nout} k={k} rel={rdef:.3e}");
assert!(rdef < 2e-2, "default dense q4k decode diverged nout={nout} k={k}: {rdef:.3e}");
}
}
// Gates the shipped default (column dp4a) under the engine's real dispatch pattern: many dense
// matvecs at TWO different k (2560 for q/k, 4096 for o) interleaved into ONE deferred batch with
// NO intermediate sync (hold every out, read at the end) -- the pooled-buffer-lifetime stress the
// per-call test's to_vec flush hides. All bit-exact vs CPU == the batch keeps every op's operands.
#[test]
fn dense_q4k_decode_batched_two_k_no_sync() {
use crate::quantized::k_quants::{BlockQ4K, GgmlType};
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[q4k-blk-batch] no vulkan device ({e}); skipping");
return;
}
};
let gen_w = |seed: usize, row: usize, i: usize| {
(((seed * 5 + row * 13 + i * 7) % 1000) as f32) / 500.0 - 1.0
};
let gen_x = |seed: usize, i: usize| (((seed * 11 + i * 3) % 800) as f32) / 400.0 - 1.0;
// Build a (nout,k) packed Q4_K weight + its CPU dequant, upload it, upload x, return (wq, x, cpu).
let build = |seed: usize, nout: usize, k: usize| {
let nb = k / 256;
let mut blocks: Vec<BlockQ4K> =
(0..nout * nb).map(|_| unsafe { std::mem::zeroed() }).collect();
let mut wdeq = vec![0f32; nout * k];
for r in 0..nout {
let rowf: Vec<f32> = (0..k).map(|i| gen_w(seed, r, i)).collect();
BlockQ4K::from_float(&rowf, &mut blocks[r * nb..(r + 1) * nb]);
BlockQ4K::to_float(&blocks[r * nb..(r + 1) * nb], &mut wdeq[r * k..(r + 1) * k]);
}
let u32s: &[u32] = unsafe {
std::slice::from_raw_parts(blocks.as_ptr() as *const u32, blocks.len() * 36)
};
let wq = dev.upload_u32(u32s).unwrap();
let x: Vec<f32> = (0..k).map(|i| gen_x(seed, i)).collect();
let xs = dev.upload_f32(&x).unwrap();
let mut cpu = vec![0f32; nout];
for (r, c) in cpu.iter_mut().enumerate() {
*c = (0..k).map(|i| wdeq[r * k + i] * x[i]).sum();
}
(wq, xs, cpu)
};
// Two shapes at DIFFERENT k, mirroring qwen3-4B q_proj (k=2560) and o_proj (k=4096).
let (wq_a, xa, cpu_a) = build(1, 1024, 2560);
let (wq_b, xb, cpu_b) = build(2, 1024, 4096);
// Interleave block dispatches with NO read in between (all deferred into one batch). 80 rounds =
// 160 block dispatches -> 160 dropped pooled `meta` buffers, matching a ~40-layer decode forward
// (the engine allocates one fresh meta per q/k/v/o Q4_K matvec) -- stresses pool/meta churn.
let mut outs: Vec<(&str, VulkanStorage, &Vec<f32>)> = Vec::new();
for _ in 0..80 {
outs.push(("a", dev.matvec_q4k_gpu(&wq_a, &xa, 1024, 2560).unwrap(), &cpu_a));
outs.push(("b", dev.matvec_q4k_gpu(&wq_b, &xb, 1024, 4096).unwrap(), &cpu_b));
}
// Read now (first readback flushes+fences the whole batch).
let mut worst = 0f32;
for (tag, out, cpu) in &outs {
let got = out.to_vec_f32().unwrap();
let maxref = cpu.iter().fold(0f32, |m, &v| m.max(v.abs())).max(1e-30);
let rel = got.iter().zip(cpu.iter()).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max)
/ maxref;
if rel > 5e-3 {
eprintln!("[q4k-blk-batch] {tag} DIVERGED rel={rel:.3e}");
}
worst = worst.max(rel);
}
eprintln!("[q4k-decode-batch] 160 interleaved k=2560/4096 dispatches, no sync: worst_rel={worst:.3e}");
assert!(worst < 2e-2, "batched dense q4k decode diverged: {worst:.3e}");
}
// Gates the shipped default (column dp4a) when the activation is produced by rms_norm IN THE SAME
// BATCH (attn_norm -> quantize_act_q8 -> matvec), never pre-flushed -- the engine's real q_proj
// chain. Bit-exact vs a CPU rms_norm+matvec == the deferred RAW barrier between rms_norm and the
// matvec is correct.
#[test]
fn dense_q4k_decode_activation_produced_in_batch() {
use crate::quantized::k_quants::{BlockQ4K, GgmlType};
const NOUT: usize = 4096;
const K: usize = 2560;
const EPS: f32 = 1e-6;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[q4k-blk-inbatch] no vulkan device ({e}); skipping");
return;
}
};
let nb = K / 256;
let gen_w = |row: usize, i: usize| (((row * 13 + i * 7) % 1000) as f32) / 500.0 - 1.0;
let mut blocks: Vec<BlockQ4K> =
(0..NOUT * nb).map(|_| unsafe { std::mem::zeroed() }).collect();
let mut wdeq = vec![0f32; NOUT * K];
for r in 0..NOUT {
let rowf: Vec<f32> = (0..K).map(|i| gen_w(r, i)).collect();
BlockQ4K::from_float(&rowf, &mut blocks[r * nb..(r + 1) * nb]);
BlockQ4K::to_float(&blocks[r * nb..(r + 1) * nb], &mut wdeq[r * K..(r + 1) * K]);
}
let u32s: &[u32] =
unsafe { std::slice::from_raw_parts(blocks.as_ptr() as *const u32, blocks.len() * 36) };
let wq = dev.upload_u32(u32s).unwrap();
// Pre-norm input + rms weight; CPU computes act = rms_norm then q = W @ act.
let pre: Vec<f32> = (0..K).map(|i| (((i * 17 + 5) % 800) as f32) / 400.0 - 1.0).collect();
let nw: Vec<f32> = (0..K).map(|i| 0.9 + (((i * 3) % 100) as f32) / 500.0).collect();
let ss: f32 = pre.iter().map(|v| v * v).sum();
let denom = (ss / K as f32 + EPS).sqrt();
let act: Vec<f32> = (0..K).map(|i| pre[i] / denom * nw[i]).collect();
let cpu_q: Vec<f32> = (0..NOUT)
.map(|r| (0..K).map(|i| wdeq[r * K + i] * act[i]).sum())
.collect();
let maxref = cpu_q.iter().fold(0f32, |m, &v| m.max(v.abs())).max(1e-30);
// GPU: rms_norm produces the activation IN-BATCH; block matvec reads it with NO sync.
let pres = dev.upload_f32(&pre).unwrap();
let nws = dev.upload_f32(&nw).unwrap();
let actg = pres
.rms_norm(&Layout::contiguous((1, K)), &nws, &Layout::contiguous(K), EPS)
.unwrap();
let out = dev.matvec_q4k_gpu(&wq, &actg, NOUT, K).unwrap();
let got = out.to_vec_f32().unwrap();
let rel = got.iter().zip(&cpu_q).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max) / maxref;
eprintln!("[q4k-blk-inbatch] q_proj k={K} nout={NOUT} (act from in-batch rms_norm) rel={rel:.3e}");
assert!(rel < 2e-2, "block dp4a with in-batch activation diverged: {rel:.3e}");
}
// Regression for the f16lo_to_f32 SUBNORMAL bug that garbled qwen3 Vulkan decode: build a Q4_K
// weight where some super-blocks have a subnormal fp16 scale (a tiny weight range -> d = amax/15
// lands in the fp16 subnormal domain ~6e-8..6e-5) -- exactly the regime real trained weights hit
// and that BlockQ4K::from_float on uniform [-1,1] data never produces (so the earlier synthetic
// gates missed it). The block kernel's manual fp16 decode must match the CPU BlockQ4K::to_float
// dequant; the old normal-only decode diverged ~10% on subnormal-scale super-blocks and, amplified
// by the near-cancellation of the affine dp4a sum (sub-block partials hundreds of x the output),
// produced decode gibberish. Real qwen3-4B blk.0.attn_q was the field repro (block-vs-cpu 12.9%).
#[test]
fn dense_q4k_block_subnormal_f16_scales() {
use crate::quantized::k_quants::{BlockQ4K, GgmlType};
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[q4k-subnormal] no vulkan device ({e}); skipping");
return;
}
};
let (nout, k) = (512usize, 2560usize); // multi-pass; mixes normal- and subnormal-scale blocks
let nb = k / 256;
let gen = |r: usize, i: usize| (((r * 13 + i * 7) % 1000) as f32) / 500.0 - 1.0;
let mut blocks: Vec<BlockQ4K> =
(0..nout * nb).map(|_| unsafe { std::mem::zeroed() }).collect();
let mut wdeq = vec![0f32; nout * k];
for r in 0..nout {
let rowf: Vec<f32> = (0..k)
.map(|i| {
// every 3rd super-block: tiny range -> subnormal fp16 d/dmin.
let amp = if (i / 256) % 3 == 0 { 1.5e-5 } else { 0.4 };
gen(r, i) * amp
})
.collect();
BlockQ4K::from_float(&rowf, &mut blocks[r * nb..(r + 1) * nb]);
BlockQ4K::to_float(&blocks[r * nb..(r + 1) * nb], &mut wdeq[r * k..(r + 1) * k]);
}
let u32s: &[u32] =
unsafe { std::slice::from_raw_parts(blocks.as_ptr() as *const u32, blocks.len() * 36) };
let wq = dev.upload_u32(u32s).unwrap();
let x: Vec<f32> = (0..k).map(|i| (((i * 17 + 5) % 800) as f32) / 400.0 - 1.0).collect();
let mut cpu = vec![0f32; nout];
for (r, c) in cpu.iter_mut().enumerate() {
*c = (0..k).map(|i| wdeq[r * k + i] * x[i]).sum();
}
let maxref = cpu.iter().fold(0f32, |m, &v| m.max(v.abs())).max(1e-30);
// The coalesced CM kernel is the production default; this gate specifically guards the dp4a
// BLOCK kernel's manual f16 decode, so force it. The CM kernel's subnormal decode (via
// unpackHalf2x16) is gated separately by q4k_cm_decode_matches_oracle.
unsafe { std::env::set_var("VK_Q4K_CM_OFF", "1") };
let block = dev.matvec_q4k(&wq, &x, nout, k).unwrap();
unsafe { std::env::remove_var("VK_Q4K_CM_OFF") };
let rel = block.iter().zip(&cpu).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max) / maxref;
eprintln!("[q4k-subnormal] block kernel vs CPU, subnormal-scale super-blocks: rel={rel:.3e}");
assert!(rel < 2e-2, "block dp4a mis-decodes subnormal fp16 scales: {rel:.3e} (f16lo_to_f32 regression)");
}
// The production `add_rmsnorm` method dispatches the DSL block-per-row fused kernel
// (add_rmsnorm_blk.spv): one workgroup/row, coalesced reads + shared-mem reduce, emitting BOTH
// s = x + res (the new residual stream) and y = rms_norm(s) * alpha in one dispatch. Replaces the
// naive one-invocation-per-row add_rmsnorm.comp (uncoalesced, the pattern rms_norm proved ~10x
// slower). eps + ndim ride pooled SSBOs (cubecl has no push-constants); both outputs are tracked
// via dispatch_outs so a later in-batch reader of either gets its RAW barrier. Chaining 8 calls
// with no sync stresses the pooled scalars -- all bit-exact == no use-after-free.
#[test]
fn add_rmsnorm_production_path_is_dsl_kernel_bit_exact() {
const N: usize = 4096;
const ROWS: usize = 4096;
const EPS: f32 = 1e-6;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[add_rmsnorm-prod] no vulkan device ({e}); skipping");
return;
}
};
let gen_x = |row: usize, i: usize| (((row * 7 + i * 13) % 1000) as f32) / 500.0 - 1.0;
let gen_r = |row: usize, i: usize| (((row * 11 + i * 5) % 800) as f32) / 400.0 - 1.0;
let gen_a = |i: usize| (((i * 3) % 100) as f32) / 100.0 + 0.5;
let x: Vec<f32> = (0..ROWS * N).map(|idx| gen_x(idx / N, idx % N)).collect();
let res: Vec<f32> = (0..ROWS * N).map(|idx| gen_r(idx / N, idx % N)).collect();
let alpha: Vec<f32> = (0..N).map(gen_a).collect();
// CPU reference: s = x + res; y = s / sqrt(mean(s^2) + eps) * alpha.
let mut cpu_s = vec![0f32; ROWS * N];
let mut cpu_y = vec![0f32; ROWS * N];
for r in 0..ROWS {
let mut ss = 0f32;
for i in 0..N {
let v = x[r * N + i] + res[r * N + i];
cpu_s[r * N + i] = v;
ss += v * v;
}
let denom = (ss / N as f32 + EPS).sqrt();
for i in 0..N {
cpu_y[r * N + i] = cpu_s[r * N + i] / denom * alpha[i];
}
}
let xs = dev.upload_f32(&x).unwrap();
let rs = dev.upload_f32(&res).unwrap();
let as_ = dev.upload_f32(&alpha).unwrap();
let x_l = Layout::contiguous((ROWS, N));
let a_l = Layout::contiguous(N);
// Chain 8 production calls with NO intermediate sync (pooled eps/ndim stress; both outputs held).
let mut outs = Vec::new();
for _ in 0..8 {
outs.push(xs.add_rmsnorm(&x_l, &rs, &x_l, &as_, &a_l, EPS).unwrap());
}
let (mut worst_s, mut worst_y) = (0f32, 0f32);
for (s_out, y) in &outs {
let gs = s_out.to_vec_f32().unwrap();
let gy = y.to_vec_f32().unwrap();
worst_s = worst_s.max(
gs.iter()
.zip(&cpu_s)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max),
);
worst_y = worst_y.max(
gy.iter()
.zip(&cpu_y)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max),
);
}
eprintln!("[add_rmsnorm-prod] {ROWS}x{N} x8-batched s_maxerr={worst_s:.2e} y_maxerr={worst_y:.2e}");
assert!(
worst_s < 1e-3,
"production add_rmsnorm s diverged from CPU: {worst_s:.3e}"
);
assert!(
worst_y < 1e-3,
"production add_rmsnorm y diverged from CPU: {worst_y:.3e}"
);
// Method-loop bench (same harness runs on incumbent .comp and DSL kernel -- apples to apples).
let iters = 50;
let bytes = (4 * ROWS * N * 4) as f64; // read x+res, write s+y
for _ in 0..3 {
let _ = xs.add_rmsnorm(&x_l, &rs, &x_l, &as_, &a_l, EPS).unwrap();
}
dev.synchronize().unwrap();
let t = std::time::Instant::now();
for _ in 0..iters {
let _ = xs.add_rmsnorm(&x_l, &rs, &x_l, &as_, &a_l, EPS).unwrap();
}
dev.synchronize().unwrap();
let ms = t.elapsed().as_secs_f64() * 1e3 / iters as f64;
eprintln!(
"[add_rmsnorm-prod] {ROWS}x{N} method {ms:.3} ms ({:.0} GB/s)",
bytes / (ms * 1e6)
);
}
// The production `softmax_last_dim` method now dispatches the DSL block-per-row softmax
// (softmax_rows_blk.spv): one workgroup/row, coalesced reads + shared-mem max & sum reductions,
// replacing the naive one-invocation-per-row softmax_rows.comp (same uncoalesced access pattern
// rms_norm proved is ~10x slower). n rides a pooled SSBO (no push-constants), lifetime-safe across
// the deferred batch; chaining 8 calls with no sync stresses it -- all bit-exact == no use-after-free.
#[test]
fn softmax_production_path_is_dsl_kernel_bit_exact() {
const M: usize = 2048;
const ROWS: usize = 1024;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[softmax-prod] no vulkan device ({e}); skipping");
return;
}
};
let gen_x = |row: usize, i: usize| (((row * 7 + i * 13) % 1000) as f32) / 250.0 - 2.0;
let x: Vec<f32> = (0..ROWS * M).map(|idx| gen_x(idx / M, idx % M)).collect();
let mut cpu = vec![0f32; ROWS * M];
for r in 0..ROWS {
let mx = (0..M).map(|i| x[r * M + i]).fold(f32::MIN, f32::max);
let exps: Vec<f32> = (0..M).map(|i| (x[r * M + i] - mx).exp()).collect();
let sum: f32 = exps.iter().sum();
for i in 0..M {
cpu[r * M + i] = exps[i] / sum;
}
}
let xs = dev.upload_f32(&x).unwrap();
let x_l = Layout::contiguous((ROWS, M));
let mut outs = Vec::new();
for _ in 0..8 {
outs.push(xs.softmax_last_dim(&x_l).unwrap());
}
let mut worst = 0f32;
for out in &outs {
let got = out.to_vec_f32().unwrap();
let err = got
.iter()
.zip(&cpu)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max);
worst = worst.max(err);
}
eprintln!("[softmax-prod] {ROWS}x{M} x8-batched maxerr={worst:.2e} (DSL kernel via production path, pooled n, no sync crutch)");
assert!(
worst < 1e-4,
"production softmax (DSL kernel) diverged from CPU: {worst:.3e}"
);
let iters = 50;
let bytes = (2 * ROWS * M * 4) as f64; // read x + write out
let out = dev.alloc_f32(ROWS * M).unwrap();
let ndb = dev.upload_u32(&[M as u32]).unwrap();
let bufs = [xs.buffer, out.buffer, ndb.buffer];
let grid = (ROWS as u32, 1, 1);
dev.dispatch_out("softmax_rows", &bufs, 1, &[], grid)
.unwrap();
dev.synchronize().unwrap();
let t = std::time::Instant::now();
for _ in 0..iters {
dev.dispatch_out("softmax_rows", &bufs, 1, &[], grid)
.unwrap();
}
dev.synchronize().unwrap();
let ms = t.elapsed().as_secs_f64() * 1e3 / iters as f64;
eprintln!(
"[softmax-prod] {ROWS}x{M} kernel {ms:.3} ms ({:.0} GB/s) -- the DSL block kernel is now the production softmax_rows",
bytes / (ms * 1e6)
);
}
// The committed DSL block-reduced MoE .spv (`moe_matvec_q4k_blk_gu`, gate/up shape n=768 k=2048)
// dispatched through the PRODUCTION path: `quantize_q4k_split` de-interleaves a real packed Q4_K
// bank into the planar layout, `moe_matvec_blk_gpu` binds the 7 SSBOs and runs the .spv via ml's
// own VulkanDevice. Matches a CPU dequant(to_float)+gather+matvec within f32-reorder tolerance --
// proving the repack + registration + dispatch wiring is correct end-to-end (kernel math is
// separately bit-exact-verified upstream in hanzo-kernel). Scale-relative metric: the block
// reduction sums in a different (valid) order than the sequential CPU dot.
#[test]
fn moe_q4k_blk_dsl_runs_through_ml_vulkan() {
use crate::quantized::k_quants::{BlockQ4K, GgmlType};
const E: usize = 4;
const N: usize = 768;
const K: usize = 2048;
const NROWS: usize = 8;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[moe-q4k-blk] no vulkan device ({e}); skipping");
return;
}
};
let rows = E * N;
let nb = K / 256;
let gen_w = |row: usize, i: usize| (((row * 13 + i * 7) % 1000) as f32) / 500.0 - 1.0;
// Quantize each weight row to Q4_K, and dequantize it back for the CPU reference.
let mut blocks: Vec<BlockQ4K> = (0..rows * nb).map(|_| unsafe { std::mem::zeroed() }).collect();
let mut wdeq = vec![0f32; rows * K];
for r in 0..rows {
let rowf: Vec<f32> = (0..K).map(|i| gen_w(r, i)).collect();
BlockQ4K::from_float(&rowf, &mut blocks[r * nb..(r + 1) * nb]);
BlockQ4K::to_float(&blocks[r * nb..(r + 1) * nb], &mut wdeq[r * K..(r + 1) * K]);
}
let bytes: &[u8] = unsafe {
std::slice::from_raw_parts(
blocks.as_ptr() as *const u8,
blocks.len() * std::mem::size_of::<BlockQ4K>(),
)
};
let gen_x = |s: usize, i: usize| (((s * 17 + i * 3) % 800) as f32) / 400.0 - 1.0;
let x: Vec<f32> = (0..NROWS * K).map(|idx| gen_x(idx / K, idx % K)).collect();
let ids: Vec<u32> = (0..NROWS).map(|s| (s % E) as u32).collect();
let mut cpu = vec![0f32; NROWS * N];
for s in 0..NROWS {
let e = ids[s] as usize;
for r in 0..N {
let wrow = (e * N + r) * K;
let mut acc = 0f32;
for i in 0..K {
acc += wdeq[wrow + i] * x[s * K + i];
}
cpu[s * N + r] = acc;
}
}
let bank = dev.quantize_q4k_split(bytes, rows, K).unwrap();
let xs = dev.upload_f32(&x).unwrap();
let ids_buf = dev.upload_ids(&ids).unwrap();
let y = dev
.moe_matvec_blk_gpu("moe_matvec_q4k_blk_gu", &bank, &xs, &ids_buf, NROWS, N, K)
.unwrap();
let got = y.to_vec_f32().unwrap();
let maxref = cpu.iter().fold(0f32, |m, &v| m.max(v.abs()));
let maxerr = got
.iter()
.zip(&cpu)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max);
let rel = maxerr / maxref.max(1e-30);
eprintln!("[moe-q4k-blk] E{E} {NROWS}x{N}x{K} scale_rel={rel:.2e} (DSL blk spv via ml dispatch + split repack)");
assert!(rel < 1e-3, "moe q4k blk DSL diverged from CPU: scale_rel={rel:.3e}");
}
// Twin of the Q4_K proof for the committed `moe_matvec_q6k_blk_dn` .spv (down shape n=2048 k=768):
// `quantize_q6k_split` de-interleaves the packed Q6_K bank (ql/qh/i8-scales/d) into the planar
// arrays the DSL kernel binds, dispatched via ml's VulkanDevice, matched to CPU dequant+matvec.
#[test]
fn moe_q6k_blk_dsl_runs_through_ml_vulkan() {
use crate::quantized::k_quants::{BlockQ6K, GgmlType};
const E: usize = 4;
const N: usize = 2048;
const K: usize = 768;
const NROWS: usize = 8;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[moe-q6k-blk] no vulkan device ({e}); skipping");
return;
}
};
let rows = E * N;
let nb = K / 256;
let gen_w = |row: usize, i: usize| (((row * 11 + i * 5) % 900) as f32) / 450.0 - 1.0;
let mut blocks: Vec<BlockQ6K> = (0..rows * nb).map(|_| unsafe { std::mem::zeroed() }).collect();
let mut wdeq = vec![0f32; rows * K];
for r in 0..rows {
let rowf: Vec<f32> = (0..K).map(|i| gen_w(r, i)).collect();
BlockQ6K::from_float(&rowf, &mut blocks[r * nb..(r + 1) * nb]);
BlockQ6K::to_float(&blocks[r * nb..(r + 1) * nb], &mut wdeq[r * K..(r + 1) * K]);
}
let bytes: &[u8] = unsafe {
std::slice::from_raw_parts(
blocks.as_ptr() as *const u8,
blocks.len() * std::mem::size_of::<BlockQ6K>(),
)
};
let gen_x = |s: usize, i: usize| (((s * 19 + i * 7) % 700) as f32) / 350.0 - 1.0;
let x: Vec<f32> = (0..NROWS * K).map(|idx| gen_x(idx / K, idx % K)).collect();
let ids: Vec<u32> = (0..NROWS).map(|s| (s % E) as u32).collect();
let mut cpu = vec![0f32; NROWS * N];
for s in 0..NROWS {
let e = ids[s] as usize;
for r in 0..N {
let wrow = (e * N + r) * K;
let mut acc = 0f32;
for i in 0..K {
acc += wdeq[wrow + i] * x[s * K + i];
}
cpu[s * N + r] = acc;
}
}
let bank = dev.quantize_q6k_split(bytes, rows, K).unwrap();
let xs = dev.upload_f32(&x).unwrap();
let ids_buf = dev.upload_ids(&ids).unwrap();
let y = dev
.moe_matvec_blk_gpu("moe_matvec_q6k_blk_dn", &bank, &xs, &ids_buf, NROWS, N, K)
.unwrap();
let got = y.to_vec_f32().unwrap();
let maxref = cpu.iter().fold(0f32, |m, &v| m.max(v.abs()));
let maxerr = got
.iter()
.zip(&cpu)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max);
let rel = maxerr / maxref.max(1e-30);
eprintln!("[moe-q6k-blk] E{E} {NROWS}x{N}x{K} scale_rel={rel:.2e} (DSL blk spv via ml dispatch + split repack)");
assert!(rel < 1e-3, "moe q6k blk DSL diverged from CPU: scale_rel={rel:.3e}");
}
/// The coalesced multi-thread Q6_K decode matvec (`mul_mat_vec_q6k_cm`) agrees with a CPU f32 oracle
/// (BlockQ6K::to_float + dense matvec) across the live decode shapes: lm_head-like (few super-blocks
/// per row), ffn_down-like (k=11008), an odd `nout` (exercises the ROWS_PER_WG row guard) and
/// `nblocks < ITS` (k=256, exercises the block-slot guard). Both operands are f32 through the same
/// decode, so agreement is to f32 rounding (~1e-6); a structural decode/index bug lands orders out.
#[test]
fn q6k_cm_decode_matches_oracle() {
use crate::quantized::k_quants::{BlockQ6K, GgmlType};
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[q6k-cm] no vulkan device ({e}); skipping");
return;
}
};
for &(nout, k) in &[(256usize, 2048usize), (2048, 11008), (151, 512), (300, 256)] {
let nb = k / 256;
let gen_w = |row: usize, i: usize| (((row * 11 + i * 5) % 900) as f32) / 450.0 - 1.0;
let mut blocks: Vec<BlockQ6K> =
(0..nout * nb).map(|_| unsafe { std::mem::zeroed() }).collect();
let mut wdeq = vec![0f32; nout * k];
for r in 0..nout {
let rowf: Vec<f32> = (0..k).map(|i| gen_w(r, i)).collect();
BlockQ6K::from_float(&rowf, &mut blocks[r * nb..(r + 1) * nb]);
BlockQ6K::to_float(&blocks[r * nb..(r + 1) * nb], &mut wdeq[r * k..(r + 1) * k]);
}
let bytes: &[u8] = unsafe {
std::slice::from_raw_parts(
blocks.as_ptr() as *const u8,
blocks.len() * std::mem::size_of::<BlockQ6K>(),
)
};
let x: Vec<f32> = (0..k).map(|i| (((i * 19 + 3) % 700) as f32) / 350.0 - 1.0).collect();
let mut cpu = vec![0f32; nout];
for r in 0..nout {
let mut acc = 0f32;
for i in 0..k {
acc += wdeq[r * k + i] * x[i];
}
cpu[r] = acc;
}
let wq = dev.quantize_q6k(bytes, nout, k).unwrap();
let got = dev.matvec_q6k(&wq, &x, nout, k).unwrap();
let maxref = cpu.iter().fold(0f32, |m, &v| m.max(v.abs()));
let maxerr =
got.iter().zip(&cpu).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max);
let rel = maxerr / maxref.max(1e-30);
eprintln!("[q6k-cm] {nout}x{k} scale_rel={rel:.2e}");
assert!(rel < 1e-3, "q6k_cm diverged from CPU: {nout}x{k} scale_rel={rel:.3e}");
}
}
/// The coalesced multi-thread Q4_K decode matvec (`mul_mat_vec_q4k_cm`) agrees with a CPU f32 oracle
/// (BlockQ4K::to_float + dense matvec) across the live zen-eco/qwen2 decode shapes: attn q/o
/// (2048x2048), ffn gate/up (11008x2048), ffn down (2048x11008), kv (256x2048), an odd `nout`
/// (exercises the NR row guard) and nblocks < ITS (k=256, block-slot guard). One shape drives d/dmin
/// into the subnormal fp16 range (every 3rd super-block) to gate the unpackHalf2x16 decode. Both
/// operands are f32 through the same decode, so agreement is to f32 rounding; a structural
/// decode/index bug or a mis-decoded subnormal scale lands orders of magnitude out.
#[test]
fn q4k_cm_decode_matches_oracle() {
use crate::quantized::k_quants::{BlockQ4K, GgmlType};
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => { eprintln!("[q4k-cm] no vulkan device ({e}); skipping"); return; }
};
for &(nout, k, subnormal) in &[
(256usize, 2048usize, false),
(11008, 2048, false),
(2048, 11008, false),
(256, 2048, false),
(151, 512, false),
(300, 256, false),
(512, 2560, true),
] {
let nb = k / 256;
let gen_w = |row: usize, i: usize| {
let base = (((row * 11 + i * 5) % 900) as f32) / 450.0 - 1.0;
// every 3rd super-block: tiny range -> subnormal fp16 d/dmin.
let amp = if subnormal && (i / 256) % 3 == 0 { 1.5e-5 } else { 1.0 };
base * amp
};
let mut blocks: Vec<BlockQ4K> =
(0..nout * nb).map(|_| unsafe { std::mem::zeroed() }).collect();
let mut wdeq = vec![0f32; nout * k];
for r in 0..nout {
let rowf: Vec<f32> = (0..k).map(|i| gen_w(r, i)).collect();
BlockQ4K::from_float(&rowf, &mut blocks[r * nb..(r + 1) * nb]);
BlockQ4K::to_float(&blocks[r * nb..(r + 1) * nb], &mut wdeq[r * k..(r + 1) * k]);
}
let u32s: &[u32] = unsafe {
std::slice::from_raw_parts(blocks.as_ptr() as *const u32, blocks.len() * 36)
};
let wq = dev.upload_u32(u32s).unwrap();
let x: Vec<f32> = (0..k).map(|i| (((i * 19 + 3) % 700) as f32) / 350.0 - 1.0).collect();
let mut cpu = vec![0f32; nout];
for (r, c) in cpu.iter_mut().enumerate() {
*c = (0..k).map(|i| wdeq[r * k + i] * x[i]).sum();
}
let got = dev.matvec_q4k(&wq, &x, nout, k).unwrap(); // production default = CM kernel
let maxref = cpu.iter().fold(0f32, |m, &v| m.max(v.abs())).max(1e-30);
let maxerr = got.iter().zip(&cpu).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max);
let rel = maxerr / maxref;
eprintln!("[q4k-cm] {nout}x{k} subnormal={subnormal} scale_rel={rel:.2e}");
assert!(rel < 1e-3, "q4k_cm diverged from CPU: {nout}x{k} scale_rel={rel:.3e}");
}
}
// dp4a twin of the Q6_K proof: same split bank and shape, dispatched through
// `moe_matvec_blk_dp4a_gpu` with `with_xsum = false` (Q6_K derives its half-block activation sums
// in-register, so xsum is not a binding). This gates the GLUE — bank order + the no-xsum binding
// set — against CPU dequant+matvec; the kernel itself is gated bit-tight in hanzo-kernel's
// matvec-check. Tolerance is 1e-2 scale-relative: the q8 activation round-trip is the only
// approximation, and a binding/order bug lands orders of magnitude outside it.
#[test]
fn moe_q6k_dp4a_blk_dsl_runs_through_ml_vulkan() {
use crate::quantized::k_quants::{BlockQ6K, GgmlType};
const E: usize = 4;
const N: usize = 2048;
const K: usize = 768;
const NROWS: usize = 8;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[moe-q6k-dp4a] no vulkan device ({e}); skipping");
return;
}
};
if !dev.has_int_dot8() {
eprintln!("[moe-q6k-dp4a] device lacks integer dot-product; skipping");
return;
}
let rows = E * N;
let nb = K / 256;
let gen_w = |row: usize, i: usize| (((row * 11 + i * 5) % 900) as f32) / 450.0 - 1.0;
let mut blocks: Vec<BlockQ6K> = (0..rows * nb).map(|_| unsafe { std::mem::zeroed() }).collect();
let mut wdeq = vec![0f32; rows * K];
for r in 0..rows {
let rowf: Vec<f32> = (0..K).map(|i| gen_w(r, i)).collect();
BlockQ6K::from_float(&rowf, &mut blocks[r * nb..(r + 1) * nb]);
BlockQ6K::to_float(&blocks[r * nb..(r + 1) * nb], &mut wdeq[r * K..(r + 1) * K]);
}
let bytes: &[u8] = unsafe {
std::slice::from_raw_parts(
blocks.as_ptr() as *const u8,
blocks.len() * std::mem::size_of::<BlockQ6K>(),
)
};
let gen_x = |s: usize, i: usize| (((s * 19 + i * 7) % 700) as f32) / 350.0 - 1.0;
let x: Vec<f32> = (0..NROWS * K).map(|idx| gen_x(idx / K, idx % K)).collect();
let ids: Vec<u32> = (0..NROWS).map(|s| (s % E) as u32).collect();
let mut cpu = vec![0f32; NROWS * N];
for s in 0..NROWS {
let e = ids[s] as usize;
for r in 0..N {
let wrow = (e * N + r) * K;
let mut acc = 0f32;
for i in 0..K {
acc += wdeq[wrow + i] * x[s * K + i];
}
cpu[s * N + r] = acc;
}
}
let bank = dev.quantize_q6k_split(bytes, rows, K).unwrap();
let xs = dev.upload_f32(&x).unwrap();
let ids_buf = dev.upload_ids(&ids).unwrap();
let y = dev
.moe_matvec_blk_dp4a_gpu("moe_matvec_q6k_dp4a_blk_dn", false, &bank, &xs, &ids_buf, NROWS, N, K)
.unwrap();
let got = y.to_vec_f32().unwrap();
let maxref = cpu.iter().fold(0f32, |m, &v| m.max(v.abs()));
let maxerr = got
.iter()
.zip(&cpu)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max);
let rel = maxerr / maxref.max(1e-30);
eprintln!("[moe-q6k-dp4a] E{E} {NROWS}x{N}x{K} scale_rel={rel:.2e} (dp4a spv via ml dispatch, no-xsum binding set)");
assert!(rel < 1e-2, "moe q6k dp4a DSL diverged from CPU: scale_rel={rel:.3e}");
}
// The committed DSL flash-SDPA .spv (`sdpa_blk`, d=128 nt=64) dispatched through ml's own
// VulkanDevice at the decode shape (seq_q=1, GQA 32/8). Matches a CPU two-pass softmax attention
// within f32-reorder tolerance (scale-relative: attention outputs are softmax-weighted sums of ±V
// that cancel near zero, so the honest metric normalizes by the output scale, not per-element).
// Chains 8 calls with NO intermediate sync: the runtime-scalar scale/meta SSBOs drop each call but
// ride the BufPool `pending` list (reclaim is post-fence-only) -- all bit-exact == no use-after-free.
#[test]
fn sdpa_blk_dsl_runs_through_ml_vulkan() {
const H: usize = 32;
const KV: usize = 8;
const SQ: usize = 1;
const SK: usize = 2048;
const D: usize = 128;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[sdpa-blk] no vulkan device ({e}); skipping");
return;
}
};
let gen = |seed: usize, i: usize| (((seed * 13 + i * 7) % 2000) as f32) / 1000.0 - 1.0;
let q: Vec<f32> = (0..H * SQ * D).map(|idx| gen(1, idx)).collect();
let k: Vec<f32> = (0..KV * SK * D).map(|idx| gen(2, idx)).collect();
let v: Vec<f32> = (0..KV * SK * D).map(|idx| gen(3, idx)).collect();
// CPU reference: two-pass softmax(QKᵀ·scale)V, GQA (head h reads kv head h/(H/KV)), non-causal.
let scale = 1.0f32 / (D as f32).sqrt();
let groups = H / KV;
let mut cpu = vec![0f32; H * SQ * D];
for h in 0..H {
let kv = h / groups;
for qp in 0..SQ {
let qb = (h * SQ + qp) * D;
let sc: Vec<f32> = (0..SK)
.map(|kk| (0..D).map(|dd| q[qb + dd] * k[(kv * SK + kk) * D + dd]).sum::<f32>() * scale)
.collect();
let m = sc.iter().cloned().fold(f32::MIN, f32::max);
let ex: Vec<f32> = sc.iter().map(|s| (s - m).exp()).collect();
let sum: f32 = ex.iter().sum();
for dd in 0..D {
cpu[qb + dd] = (0..SK).map(|kk| ex[kk] / sum * v[(kv * SK + kk) * D + dd]).sum();
}
}
}
let qs = dev.upload_f32(&q).unwrap();
let ks = dev.upload_f32(&k).unwrap();
let vs = dev.upload_f32(&v).unwrap();
let mut outs = Vec::new();
for _ in 0..8 {
// Packed k/v: batch stride KV*SK*D, kv-head stride SK*D, key stride D.
outs.push(
dev.sdpa_blk_vk(&qs, &ks, &vs, 1, H, KV, SQ, SK, D, scale, false, KV * SK * D, SK * D, D)
.unwrap(),
);
}
let maxref = cpu.iter().fold(0f32, |m, &v| m.max(v.abs())).max(1e-30);
let mut worst = 0f32;
for out in &outs {
let got = out.to_vec_f32().unwrap();
let err = got.iter().zip(&cpu).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max);
worst = worst.max(err / maxref);
}
eprintln!("[sdpa-blk] h{H}kv{KV} q{SQ} k{SK} d{D} x8-batched scale_rel={worst:.2e} (DSL flash-SDPA spv via ml dispatch, pooled scale/meta, no sync crutch)");
assert!(worst < 1e-4, "sdpa_blk DSL diverged from CPU: scale_rel={worst:.3e}");
}
// Long-prefill ring-timeout guard. A 2k+ token prefill records a whole forward's dispatches into
// ONE queue submission whose on-GPU runtime (attention is O(seq²)) overruns the RADV/amdgpu ring
// lockup timeout -> "context is lost". The work bound (`work_cap`) splits that single submission
// into ring-timeout-safe pieces. This drives a 2048-query attention repeated for a forward's depth
// and asserts: cap-disabled submits the whole chain ONCE (the pre-fix hang shape), the bound splits
// it into MANY submissions, and splitting is BIT-IDENTICAL and matches a CPU softmax oracle. Small
// per-call context (SK) + a few checked query rows keep the oracle cheap; the split is exercised in
// full. The GPU-reset repro itself lives in the engine prefill bench, not this always-on unit test.
#[test]
fn long_prefill_work_cap_bounds_submissions_bit_exact() {
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[work-cap] no vulkan device ({e}); skipping");
return;
}
};
const H: usize = 8;
const KV: usize = 2;
const SQ: usize = 2048; // a 2048-token prefill chunk: one query row per token
const SK: usize = 256;
const D: usize = 128;
const LAYERS: usize = 32; // stand-in for a deep forward's attention dispatch count
const QCHK: usize = 8; // oracle only the first few query rows (all queries are uniform)
let scale = 1.0f32 / (D as f32).sqrt();
let groups = H / KV;
let gen = |seed: usize, i: usize| (((seed * 13 + i * 7) % 2000) as f32) / 1000.0 - 1.0;
let q: Vec<f32> = (0..H * SQ * D).map(|i| gen(1, i)).collect();
let k: Vec<f32> = (0..KV * SK * D).map(|i| gen(2, i)).collect();
let v: Vec<f32> = (0..KV * SK * D).map(|i| gen(3, i)).collect();
// CPU reference: two-pass softmax(QKᵀ·scale)V, GQA (head h -> kv head h/(H/KV)), non-causal,
// for the first QCHK query rows of every head.
let mut cpu = vec![0f32; H * QCHK * D];
for h in 0..H {
let kv = h / groups;
for qp in 0..QCHK {
let qb = (h * SQ + qp) * D;
let sc: Vec<f32> = (0..SK)
.map(|kk| (0..D).map(|dd| q[qb + dd] * k[(kv * SK + kk) * D + dd]).sum::<f32>() * scale)
.collect();
let m = sc.iter().cloned().fold(f32::MIN, f32::max);
let ex: Vec<f32> = sc.iter().map(|s| (s - m).exp()).collect();
let sum: f32 = ex.iter().sum();
let ob = (h * QCHK + qp) * D;
for dd in 0..D {
cpu[ob + dd] = (0..SK).map(|kk| ex[kk] / sum * v[(kv * SK + kk) * D + dd]).sum();
}
}
}
let qs = dev.upload_f32(&q).unwrap();
let ks = dev.upload_f32(&k).unwrap();
let vs = dev.upload_f32(&v).unwrap();
// One "forward": LAYERS independent sdpa dispatches recorded before the single readback flush.
let run = |dev: &VulkanDevice| -> (u64, Vec<f32>) {
let before = dev.submit_count();
let mut last = None;
for _ in 0..LAYERS {
last = Some(
dev.sdpa_blk_vk(&qs, &ks, &vs, 1, H, KV, SQ, SK, D, scale, false, KV * SK * D, SK * D, D)
.unwrap(),
);
}
let out = last.unwrap().to_vec_f32().unwrap();
(dev.submit_count() - before, out)
};
// GPU out is [H, SQ, D]; pull the first QCHK query rows of each head to match the oracle layout.
let extract = |out: &[f32]| -> Vec<f32> {
let mut r = vec![0f32; H * QCHK * D];
for h in 0..H {
for qp in 0..QCHK {
for dd in 0..D {
r[(h * QCHK + qp) * D + dd] = out[(h * SQ + qp) * D + dd];
}
}
}
r
};
let maxref = cpu.iter().fold(0f32, |m, &v| m.max(v.abs())).max(1e-30);
let relerr = |got: &[f32]| {
got.iter().zip(&cpu).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max) / maxref
};
// Pre-fix shape: the whole forward is ONE submission (at 2k+ tokens this overruns the ring timeout).
dev.set_work_cap(0);
let (uncapped_submits, out_uncapped) = run(&dev);
let got_uncapped = extract(&out_uncapped);
// Fixed: the work bound splits that forward into several ring-timeout-safe submissions.
let per_call = (H * SQ) as u64; // workgroups per sdpa dispatch
dev.set_work_cap(per_call * 4); // flush roughly every 4 dispatches
let (capped_submits, out_capped) = run(&dev);
let got_capped = extract(&out_capped);
eprintln!(
"[work-cap] SQ{SQ} SK{SK} H{H} x{LAYERS} submits uncapped={uncapped_submits} capped={capped_submits} \
relerr uncapped={:.2e} capped={:.2e}",
relerr(&got_uncapped),
relerr(&got_capped),
);
assert_eq!(uncapped_submits, 1, "cap disabled must submit the whole forward once");
assert!(capped_submits > 1, "work cap must split the forward into multiple submissions");
assert!(
capped_submits >= (LAYERS as u64) / 4 - 1,
"work cap under-split the forward: {capped_submits} submissions"
);
assert_eq!(got_capped, got_uncapped, "splitting the submission changed the result");
assert!(relerr(&got_capped) < 1e-4, "sdpa diverged from CPU: scale_rel={:.3e}", relerr(&got_capped));
}
/// Flash-decoding (`sdpa_decode_split_vk`) matches the same two-pass softmax CPU oracle as
/// `sdpa_blk_vk`, across GQA ratios, split counts, and a ragged seq_k (empty trailing splits).
/// Register-light wave64 subgroup + KV-split phase 1, flash-combine phase 2 -- numerically identical
/// to a single-pass online softmax up to f32 reduction order. This is the correctness gate for the
/// decode-attention occupancy fix; the perf A/B lives in CI benches, not here.
#[test]
fn sdpa_decode_split_matches_oracle() {
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[sdpa-split] no vulkan device ({e}); skipping");
return;
}
};
const D: usize = 128;
let gen = |seed: usize, i: usize| (((seed * 13 + i * 7) % 2000) as f32) / 1000.0 - 1.0;
// CPU reference: two-pass softmax(QKᵀ·scale)V, GQA (head h -> kv head h/(H/KV)), non-causal.
let cpu_ref = |h_n: usize, kv_n: usize, sk: usize, q: &[f32], k: &[f32], v: &[f32]| {
let scale = 1.0f32 / (D as f32).sqrt();
let groups = h_n / kv_n;
let mut cpu = vec![0f32; h_n * D];
for h in 0..h_n {
let kv = h / groups;
let qb = h * D;
let sc: Vec<f32> = (0..sk)
.map(|kk| (0..D).map(|dd| q[qb + dd] * k[(kv * sk + kk) * D + dd]).sum::<f32>() * scale)
.collect();
let m = sc.iter().cloned().fold(f32::MIN, f32::max);
let ex: Vec<f32> = sc.iter().map(|s| (s - m).exp()).collect();
let sum: f32 = ex.iter().sum();
for dd in 0..D {
cpu[qb + dd] = (0..sk).map(|kk| ex[kk] / sum * v[(kv * sk + kk) * D + dd]).sum();
}
}
cpu
};
// (H, KV, SK, n_split) cases: zen-eco decode (16/2), the 32/8 GQA shape, and a ragged seq_k
// (SK=13 with n_split=8 leaves trailing splits empty -> must contribute exactly zero).
let cases = [
(16usize, 2usize, 512usize, 1usize),
(16, 2, 512, 2),
(16, 2, 512, 4),
(16, 2, 512, 8),
(32, 8, 2048, 8),
(16, 2, 13, 8),
];
let scale = 1.0f32 / (D as f32).sqrt();
let mut worst = 0f32;
for (h_n, kv_n, sk, nsplit) in cases {
let q: Vec<f32> = (0..h_n * D).map(|idx| gen(1, idx)).collect();
let k: Vec<f32> = (0..kv_n * sk * D).map(|idx| gen(2, idx)).collect();
let v: Vec<f32> = (0..kv_n * sk * D).map(|idx| gen(3, idx)).collect();
let cpu = cpu_ref(h_n, kv_n, sk, &q, &k, &v);
let maxref = cpu.iter().fold(0f32, |m, &v| m.max(v.abs())).max(1e-30);
let qs = dev.upload_f32(&q).unwrap();
let ks = dev.upload_f32(&k).unwrap();
let vs = dev.upload_f32(&v).unwrap();
let out = dev
.sdpa_decode_split_vk(
&qs, &ks, &vs, 1, h_n, kv_n, 1, sk, D, scale, nsplit,
kv_n * sk * D, sk * D, D,
)
.unwrap();
let got = out.to_vec_f32().unwrap();
let err = got.iter().zip(&cpu).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max);
let rel = err / maxref;
eprintln!("[sdpa-split] h{h_n}kv{kv_n} k{sk} nsplit{nsplit} scale_rel={rel:.2e}");
worst = worst.max(rel);
}
assert!(worst < 1e-4, "sdpa_decode_split diverged from CPU: scale_rel={worst:.3e}");
}
// The committed DSL flash-attention coopmat .spv (`flash_attn_dsl`, d=128 plane=64 BR=BC=16)
// dispatched through ml's own VulkanDevice. Both matmuls (Q@Kᵀ, P@V) run on the f16 16x16x16
// cooperative-matrix path (OpCooperativeMatrixMulAddKHR, f16 A/B -> f32 acc, subgroup scope), so
// the gate is the f16-tensor-core tolerance (~1e-3..1e-2 scale-relative), NOT the scalar arm's
// ~1e-7. Scale-relative (max|Δ| / max|ref|) is the honest metric: attention outputs are
// softmax-weighted sums of ±V that cancel near zero, so per-element rel-error explodes there.
//
// The .spv bakes d=128 but seq_q/seq_k ride the runtime `meta`, so ONE artifact serves every
// shape below. The set is chosen to exercise the flagged coopmat failure modes on RADV
// (16x16 fragments, distinct from Metal's validated 8x8 arm): the aligned 512x512 causal shape
// proves the ColMajor-K -> Kᵀ fragment layout + the PV store stride; the RAGGED key counts
// (kv1/kv17/kv33) hit the partial-tile path where the staged K/V tile is zero-padded past seq_k
// (a wrong direct-slice load would read OOB); the RAGGED query count (sq40, a 16+16+8 tile grid)
// hits the partial query-tile at store + normalize; the decode GQA 32/8 shape is the production
// per-token attention. Every shape is a fresh dispatch (no cross-shape state).
#[test]
fn flash_dsl_coopmat_matches_oracle() {
const D: usize = 128;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[flash-dsl] no vulkan device ({e}); skipping");
return;
}
};
// Two-pass materialized softmax(QKᵀ·scale + causal_mask)V over PACKED k/v [n_kv, seq_k, d],
// GQA (head h reads kv head h/(n_heads/n_kv)). The normative reference the online-softmax
// coopmat flash must match. causal masks key kk > query qp (both 0-based within the call).
let flash_ref = |q: &[f32], k: &[f32], v: &[f32],
nh: usize, nkv: usize, sq: usize, sk: usize, causal: bool| -> Vec<f32> {
let scale = 1.0f32 / (D as f32).sqrt();
let groups = nh / nkv;
let mut out = vec![0f32; nh * sq * D];
for h in 0..nh {
let kv = h / groups;
for qp in 0..sq {
let qb = (h * sq + qp) * D;
let mut sc = vec![f32::NEG_INFINITY; sk];
for kk in 0..sk {
if causal && kk > qp {
continue;
}
sc[kk] = (0..D).map(|dd| q[qb + dd] * k[(kv * sk + kk) * D + dd]).sum::<f32>() * scale;
}
let m = sc.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
let ex: Vec<f32> = sc.iter().map(|s| (s - m).exp()).collect();
let sum: f32 = ex.iter().sum();
for dd in 0..D {
out[qb + dd] = (0..sk).map(|kk| ex[kk] / sum * v[(kv * sk + kk) * D + dd]).sum();
}
}
}
out
};
// One shape: gen deterministic q/k/v, run the coopmat flash through ml dispatch (packed KV:
// batch stride nkv*sk*D, head stride sk*D, key stride D), gate scale-relative vs `flash_ref`.
let run = |nh: usize, nkv: usize, sq: usize, sk: usize, causal: bool, tag: &str| {
let gen = |seed: usize, i: usize| (((seed * 13 + i * 7) % 2000) as f32) / 1000.0 - 1.0;
let q: Vec<f32> = (0..nh * sq * D).map(|i| gen(1, i)).collect();
let k: Vec<f32> = (0..nkv * sk * D).map(|i| gen(2, i)).collect();
let v: Vec<f32> = (0..nkv * sk * D).map(|i| gen(3, i)).collect();
let want = flash_ref(&q, &k, &v, nh, nkv, sq, sk, causal);
let scale = 1.0f32 / (D as f32).sqrt();
let qs = dev.upload_f32(&q).unwrap();
let ks = dev.upload_f32(&k).unwrap();
let vs = dev.upload_f32(&v).unwrap();
let out = dev
.flash_attn_dsl_vk(&qs, &ks, &vs, 1, nh, nkv, sq, sk, D, scale, causal, nkv * sk * D, sk * D, D)
.unwrap();
let got = out.to_vec_f32().unwrap();
let maxref = want.iter().fold(0f32, |m, &x| m.max(x.abs())).max(1e-30);
let maxd = got.iter().zip(&want).fold(0f32, |m, (a, b)| m.max((a - b).abs()));
let rel = maxd / maxref;
eprintln!("[flash-dsl {tag}] nh{nh}/nkv{nkv} sq{sq} sk{sk} d{D} causal{causal} scale_rel={rel:.2e}");
assert!(rel < 2e-2, "flash coopmat {tag}: scale_rel {rel:.3e} exceeds 2e-2 vs materialized ref");
};
// decode (seq_q=1, non-causal, the single query attends the whole cache), ragged key counts.
run(4, 2, 1, 1, false, "decode kv1");
run(4, 2, 1, 17, false, "decode kv17 (ragged key tile)");
run(4, 2, 1, 33, false, "decode kv33 (ragged key tile)");
run(4, 2, 1, 128, false, "decode kv128 (aligned)");
run(32, 8, 1, 2048, false, "decode kv2048 GQA4 (production)");
// prefill (causal): aligned single tile, aligned 3-tile, ragged query tile (16+16+8), 512x512.
run(4, 2, 16, 128, true, "prefill sq16 causal");
run(4, 2, 48, 48, true, "prefill sq48 causal (aligned 3-tile)");
run(4, 2, 40, 40, true, "prefill sq40 causal (ragged query tile)");
run(2, 1, 512, 512, true, "prefill 512x512 causal MHA");
}
// A/B: the fused coopmat flash (`flash_attn_dsl`) vs the block-flash base `sdpa_blk` in this crate.
// (Decode's live path is now `sdpa_decode_split_vk`, the occupancy split of `sdpa_blk`; this
// microbench A/Bs flash vs the `sdpa_blk` base -- the real prefill-wiring decision is the in-engine
// pp512 bench, not this kernel-level number.) Both kernels are measured identically: q/k/v uploaded
// once, out/scale/meta pre-allocated (no per-iter alloc), warmed, then a batch of N same-kernel
// dispatches timed with one flush. Run under `VK_PROFILE_GPU=1` for the per-op on-GPU `[VK_GPU]` avg
// us corroboration. This is a benchmark, not a gate; run explicitly (`--ignored`). Reports, never asserts speed.
#[test]
#[ignore]
fn flash_dsl_vs_sdpa_blk_ab() {
const D: usize = 128;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => { eprintln!("[flash-ab] no vulkan device ({e}); skipping"); return; }
};
let bench = |nh: usize, nkv: usize, sq: usize, sk: usize, causal: bool, iters: usize, tag: &str| {
let gen = |seed: usize, i: usize| (((seed * 13 + i * 7) % 2000) as f32) / 1000.0 - 1.0;
let q: Vec<f32> = (0..nh * sq * D).map(|i| gen(1, i)).collect();
let k: Vec<f32> = (0..nkv * sk * D).map(|i| gen(2, i)).collect();
let v: Vec<f32> = (0..nkv * sk * D).map(|i| gen(3, i)).collect();
let scale = 1.0f32 / (D as f32).sqrt();
let (kbs, khs, ks_) = (nkv * sk * D, sk * D, D); // packed KV strides
let qs = dev.upload_f32(&q).unwrap();
let ks = dev.upload_f32(&k).unwrap();
let vs = dev.upload_f32(&v).unwrap();
let out = dev.alloc_f32(nh * sq * D).unwrap();
let scl = dev.upload_f32(&[scale]).unwrap();
let m8: Vec<u32> = vec![sq as u32, sk as u32, nh as u32, nkv as u32, causal as u32, kbs as u32, khs as u32, ks_ as u32];
let mut m9 = m8.clone();
m9.push(0); // cube_base
let meta_blk = dev.upload_u32(&m8).unwrap();
let meta_flash = dev.upload_u32(&m9).unwrap();
let bufs_blk = [qs.buffer, ks.buffer, vs.buffer, out.buffer, scl.buffer, meta_blk.buffer];
let bufs_flash = [qs.buffer, ks.buffer, vs.buffer, out.buffer, scl.buffer, meta_flash.buffer];
let g_blk = ((nh * sq) as u32, 1u32, 1u32);
let g_flash = ((nh * sq.div_ceil(16)) as u32, 1u32, 1u32);
// Time N same-kernel dispatches in one batch, one flush. Pre-allocated buffers => kernel +
// record only. Both kernels pay the identical harness cost, so the ratio is honest.
let time_one = |name: &'static str, bufs: &[vk::Buffer], groups: (u32, u32, u32)| -> f64 {
for _ in 0..8 { dev.dispatch_outs(name, bufs, &[3], &[], groups).unwrap(); }
dev.flush().unwrap();
let t0 = std::time::Instant::now();
for _ in 0..iters { dev.dispatch_outs(name, bufs, &[3], &[], groups).unwrap(); }
dev.flush().unwrap();
t0.elapsed().as_secs_f64() * 1e3 / iters as f64
};
let ms_flash = time_one("flash_attn_dsl", &bufs_flash, g_flash);
let ms_blk = time_one("sdpa_blk", &bufs_blk, g_blk);
eprintln!(
"[flash-ab {tag}] nh{nh}/nkv{nkv} sq{sq} sk{sk} d{D} causal{causal} flash={ms_flash:.4}ms (wg={}) sdpa_blk={ms_blk:.4}ms (wg={}) flash/blk={:.2}x",
g_flash.0, g_blk.0, ms_flash / ms_blk,
);
};
// Decode (seq_q=1, the single query attends the whole cache): flash computes a 16-row tile for
// 1 real query (15/16 wasted) -- sdpa_blk is purpose-built here. Prefill (causal, seq_q=seq_k):
// flash's coopmat QK/PV amortize over 16 queries/tile -- the regime the tensor-core path targets.
bench(32, 8, 1, 2048, false, 200, "decode kv2048 GQA4 (production)");
bench(32, 8, 1, 512, false, 200, "decode kv512 GQA4");
bench(8, 2, 512, 512, true, 50, "prefill 512x512 causal GQA4");
bench(32, 8, 512, 512, true, 30, "prefill 512x512 causal GQA4 (32h)");
}
/// The affine Q4_K PREFILL MMQ .spv (mmq_q4k, coopmat/tensor-core) dispatches through ml's own
/// Vulkan path and matches a CPU affine-GEMM reference. Proves the codegen seam end-to-end for the
/// prefill kernel: the committed .spv's baked shape (n=2048,k=2048), the binding order
/// (xq,xs,xsum,wqs,wsc,wd,wdm,out), the LocalSize-512 dispatch, and the in-kernel Q4_K decode +
/// affine `- M*xsum` epilogue. Reference decodes the same packed layout (get_scale_min_k4) that
/// BlockQ4K::to_float uses. Scale-relative gate: a signed int8 sum cancels near zero.
#[test]
fn mmq_q4k_prefill_dsl_runs_through_ml_vulkan() {
const M: usize = 32;
const N: usize = 2048;
const K: usize = 2048;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[mmq-q4k] no vulkan device ({e}); skipping");
return;
}
};
let kb = K / 32;
let nsb = K / 256;
// Deterministic inputs (self-contained: hanzo-kernel is not a dep of hanzo-ml).
let mut s = 0x243F6A8885A308D3u64;
let mut next = || {
s ^= s << 13;
s ^= s >> 7;
s ^= s << 17;
s
};
let xq: Vec<i8> = (0..M * K).map(|_| ((next() % 255) as i64 - 127) as i8).collect();
let wqs: Vec<u32> = (0..N * nsb * 32).map(|_| next() as u32).collect();
let wsc: Vec<u32> = (0..N * nsb * 3).map(|_| next() as u32).collect();
let wd: Vec<f32> = (0..N * nsb).map(|_| (next() % 1000) as f32 / 20000.0 + 0.002).collect();
let wdm: Vec<f32> = (0..N * nsb).map(|_| (next() % 1000) as f32 / 40000.0).collect();
let xs: Vec<f32> = (0..M * kb).map(|_| (next() % 1000) as f32 / 50000.0 + 0.002).collect();
// xsum = xs*Sum(xq): the dequantized block sum quantize_act_q8 emits (so the offset term
// carries xs and the epilogue applies xs only to the dot).
let mut xsum = vec![0f32; M * kb];
for i in 0..M {
for b in 0..kb {
let mut acc = 0i32;
for l in 0..32 {
acc += xq[i * K + b * 32 + l] as i32;
}
xsum[i * kb + b] = xs[i * kb + b] * acc as f32;
}
}
// CPU reference: affine MMQ with the same in-place Q4_K decode.
let byte = |a: &[u32], base: usize, i: usize| (a[base + i / 4] >> (8 * (i % 4))) & 255;
let sc_of = |wsc: &[u32], sb: usize, j: usize| -> u32 {
if j < 4 { byte(wsc, sb, j) & 63 } else { (byte(wsc, sb, j + 4) & 15) | ((byte(wsc, sb, j - 4) >> 6) << 4) }
};
let m_of = |wsc: &[u32], sb: usize, j: usize| -> u32 {
if j < 4 { byte(wsc, sb, j + 4) & 63 } else { (byte(wsc, sb, j + 4) >> 4) | ((byte(wsc, sb, j) >> 6) << 4) }
};
let mut want = vec![0f32; M * N];
for i in 0..M {
for j in 0..N {
let mut acc = 0f32;
for b in 0..kb {
let is = b % 8;
let g = is / 2;
let blk = j * nsb + b / 8;
let mut isum = 0i32;
for qi in 0..32 {
let qbyte = byte(&wqs, blk * 32, g * 32 + qi);
let nib = ((qbyte >> (4 * (is % 2))) & 15) as i32;
isum += xq[i * K + b * 32 + qi] as i32 * nib;
}
let dd = wd[blk] * sc_of(&wsc, blk * 3, is) as f32;
let mm = wdm[blk] * m_of(&wsc, blk * 3, is) as f32;
acc += xs[i * kb + b] * dd * isum as f32 - mm * xsum[i * kb + b];
}
want[i * N + j] = acc;
}
}
// Upload + dispatch through ml. xq as raw i8 bytes; wqs/wsc as u32; the rest f32.
let xq_u8: Vec<u8> = xq.iter().map(|&b| b as u8).collect();
let xqh = dev.upload_qweight(&xq_u8).unwrap();
let xsh = dev.upload_f32(&xs).unwrap();
let xsumh = dev.upload_f32(&xsum).unwrap();
let u32_bytes = |v: &[u32]| -> Vec<u8> { v.iter().flat_map(|w| w.to_le_bytes()).collect() };
let bank = MoeBankSplit(vec![
dev.upload_qweight(&u32_bytes(&wqs)).unwrap(),
dev.upload_qweight(&u32_bytes(&wsc)).unwrap(),
dev.upload_f32(&wd).unwrap(),
dev.upload_f32(&wdm).unwrap(),
]);
let out = dev.mmq_q4k_gpu(&xqh, &xsh, &xsumh, &bank, M, N).unwrap();
let got = out.to_vec_f32().unwrap();
let maxref = want.iter().fold(0f32, |m, &v| m.max(v.abs())).max(1e-30);
let rel = got.iter().zip(&want).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max) / maxref;
eprintln!("[mmq-q4k] {M}x{N}x{K} scale_rel={rel:.2e} (affine Q4_K coopmat PREFILL spv via ml dispatch)");
assert!(rel < 1e-3, "mmq_q4k prefill DSL diverged from CPU: scale_rel={rel:.3e}");
}
/// The RUNTIME-DIMS twin (`mmq_q4k_rt`, m/n/k via meta SSBO) dispatches through ml at a shape the
/// baked-dims .spv can't serve -- multi-N-block AND both tails: M=40 (>32, tail), N=200 (3 N-blocks,
/// last partial), K=512. ONE .spv, arbitrary shape, tail guards clipping the partial tile. Same
/// affine oracle. This is the production seam that replaces mul_mm_q4k_tiled_dp4a for rows>1.
#[test]
fn mmq_q4k_rt_prefill_dsl_runs_through_ml_vulkan() {
const M: usize = 40;
const N: usize = 200;
const K: usize = 512;
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[mmq-q4k-rt] no vulkan device ({e}); skipping");
return;
}
};
let kb = K / 32;
let nsb = K / 256;
let mut s = 0x9E3779B97F4A7C15u64;
let mut next = || { s ^= s << 13; s ^= s >> 7; s ^= s << 17; s };
let xq: Vec<i8> = (0..M * K).map(|_| ((next() % 255) as i64 - 127) as i8).collect();
let wqs: Vec<u32> = (0..N * nsb * 32).map(|_| next() as u32).collect();
let wsc: Vec<u32> = (0..N * nsb * 3).map(|_| next() as u32).collect();
let wd: Vec<f32> = (0..N * nsb).map(|_| (next() % 1000) as f32 / 20000.0 + 0.002).collect();
let wdm: Vec<f32> = (0..N * nsb).map(|_| (next() % 1000) as f32 / 40000.0).collect();
let xs: Vec<f32> = (0..M * kb).map(|_| (next() % 1000) as f32 / 50000.0 + 0.002).collect();
let mut xsum = vec![0f32; M * kb];
for i in 0..M {
for b in 0..kb {
let mut acc = 0i32;
for l in 0..32 { acc += xq[i * K + b * 32 + l] as i32; }
xsum[i * kb + b] = xs[i * kb + b] * acc as f32;
}
}
let byte = |a: &[u32], base: usize, i: usize| (a[base + i / 4] >> (8 * (i % 4))) & 255;
let sc_of = |wsc: &[u32], sb: usize, j: usize| -> u32 {
if j < 4 { byte(wsc, sb, j) & 63 } else { (byte(wsc, sb, j + 4) & 15) | ((byte(wsc, sb, j - 4) >> 6) << 4) }
};
let m_of = |wsc: &[u32], sb: usize, j: usize| -> u32 {
if j < 4 { byte(wsc, sb, j + 4) & 63 } else { (byte(wsc, sb, j + 4) >> 4) | ((byte(wsc, sb, j) >> 6) << 4) }
};
let mut want = vec![0f32; M * N];
for i in 0..M {
for j in 0..N {
let mut acc = 0f32;
for b in 0..kb {
let is = b % 8;
let g = is / 2;
let blk = j * nsb + b / 8;
let mut isum = 0i32;
for qi in 0..32 {
let qbyte = byte(&wqs, blk * 32, g * 32 + qi);
let nib = ((qbyte >> (4 * (is % 2))) & 15) as i32;
isum += xq[i * K + b * 32 + qi] as i32 * nib;
}
let dd = wd[blk] * sc_of(&wsc, blk * 3, is) as f32;
let mm = wdm[blk] * m_of(&wsc, blk * 3, is) as f32;
acc += xs[i * kb + b] * dd * isum as f32 - mm * xsum[i * kb + b];
}
want[i * N + j] = acc;
}
}
let xq_u8: Vec<u8> = xq.iter().map(|&b| b as u8).collect();
let xqh = dev.upload_qweight(&xq_u8).unwrap();
let xsh = dev.upload_f32(&xs).unwrap();
let xsumh = dev.upload_f32(&xsum).unwrap();
let u32_bytes = |v: &[u32]| -> Vec<u8> { v.iter().flat_map(|w| w.to_le_bytes()).collect() };
let bank = MoeBankSplit(vec![
dev.upload_qweight(&u32_bytes(&wqs)).unwrap(),
dev.upload_qweight(&u32_bytes(&wsc)).unwrap(),
dev.upload_f32(&wd).unwrap(),
dev.upload_f32(&wdm).unwrap(),
]);
let out = dev.mmq_q4k_rt_gpu(&xqh, &xsh, &xsumh, &bank, M, N, K).unwrap();
let got = out.to_vec_f32().unwrap();
let maxref = want.iter().fold(0f32, |m, &v| m.max(v.abs())).max(1e-30);
let rel = got.iter().zip(&want).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max) / maxref;
eprintln!("[mmq-q4k-rt] {M}x{N}x{K} multi-block+tail scale_rel={rel:.2e} (runtime-dims coopmat MMQ via ml, one .spv)");
assert!(rel < 1e-3, "mmq_q4k_rt prefill DSL diverged from CPU: scale_rel={rel:.3e}");
}
/// A/B: coopmat runtime-dims MMQ (`mmq_q4k_rt`) vs the shipping dp4a-tiled prefill GEMM
/// (`mul_mm_q4k_tiled_dp4a`) at real prefill shapes, feeding BOTH the SAME logical Q4_K weight
/// (raw for dp4a, split for coopmat). Both submit through the same dispatch path, so per-submit
/// overhead cancels in the ratio -- this same-harness ratio is the go/no-go for routing coopmat
/// into matmul_q4k_gpu_off (a cache-warm ratio, NOT an absolute in-engine number). Gated on
/// HANZO_MMQ_AB=1 so it never runs in normal CI.
#[test]
fn mmq_q4k_coopmat_vs_dp4a_prefill_ab() {
if std::env::var_os("HANZO_MMQ_AB").is_none() {
return;
}
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => { eprintln!("[mmq-ab] no vulkan device ({e}); skipping"); return; }
};
let mut s = 0xD1B54A32D192ED03u64;
let mut next = || { s ^= s << 13; s ^= s >> 7; s ^= s << 17; s };
// (m, n, k): prefill batch m=512. First the real zen-eco-4b/qwen2 Q4_K projection shapes
// (all k=2048: ffn gate/up, attn q/o, attn kv), then larger llama/GLM shapes.
let shapes = [
(512usize, 11008usize, 2048usize),
(512, 2048, 2048),
(512, 256, 2048),
(512, 4096, 4096),
(512, 11008, 4096),
(512, 4096, 11008),
];
eprintln!("[mmq-ab] coopmat mmq_q4k_rt vs dp4a mul_mm_q4k_tiled_dp4a (same weight, same harness)");
for (m, n, k) in shapes {
let nb = k / 256;
let bytes: Vec<u8> = (0..n * nb * 144).map(|_| next() as u8).collect();
let wq = dev.upload_qweight(&bytes).unwrap();
let bank = dev.quantize_q4k_split(&bytes, n, k).unwrap();
let x: Vec<f32> = (0..m * k).map(|_| (next() % 1000) as f32 / 500.0 - 1.0).collect();
let xh = dev.upload_f32(&x).unwrap();
let (xq, xs, xsum) = dev.quantize_act_q8(&xh, m, k).unwrap();
let iters = 30;
// dp4a: force the mul_mm_q4k_tiled_dp4a tile (coopmat is now the default for aligned Q4_K).
unsafe { std::env::set_var("VK_Q4K_COOPMAT_OFF", "1") };
for _ in 0..3 { let _ = dev.matmul_q4k_gpu(&wq, &xh, m, n, k).unwrap(); }
dev.synchronize().unwrap();
let t = std::time::Instant::now();
for _ in 0..iters { let _ = dev.matmul_q4k_gpu(&wq, &xh, m, n, k).unwrap(); }
dev.synchronize().unwrap();
let dp4a_ms = t.elapsed().as_secs_f64() * 1e3 / iters as f64;
unsafe { std::env::remove_var("VK_Q4K_COOPMAT_OFF") };
// coopmat (candidate): runtime-dims MMQ over the split bank.
for _ in 0..3 { let _ = dev.mmq_q4k_rt_gpu(&xq, &xs, &xsum, &bank, m, n, k).unwrap(); }
dev.synchronize().unwrap();
let t = std::time::Instant::now();
for _ in 0..iters { let _ = dev.mmq_q4k_rt_gpu(&xq, &xs, &xsum, &bank, m, n, k).unwrap(); }
dev.synchronize().unwrap();
let coop_ms = t.elapsed().as_secs_f64() * 1e3 / iters as f64;
// f16 coopmat (llama-parity path): dequant Q4_K weight -> f16 LDS + coopMatMulAdd f16->f32.
// This is what llama's Vulkan (KHR_coopmat, mul_mm.comp COOPMAT) uses on RADV. Env-forced.
unsafe { std::env::set_var("VK_Q4K_COOPMAT", "1") };
for _ in 0..3 { let _ = dev.matmul_q4k_gpu(&wq, &xh, m, n, k).unwrap(); }
dev.synchronize().unwrap();
let t = std::time::Instant::now();
for _ in 0..iters { let _ = dev.matmul_q4k_gpu(&wq, &xh, m, n, k).unwrap(); }
dev.synchronize().unwrap();
let f16c_ms = t.elapsed().as_secs_f64() * 1e3 / iters as f64;
unsafe { std::env::remove_var("VK_Q4K_COOPMAT") };
let gf = |ms: f64| 2.0 * m as f64 * n as f64 * k as f64 / (ms * 1e6);
eprintln!(
"[mmq-ab] {m}x{n}x{k} dp4a {dp4a_ms:.3}ms ({:.0} GF) f16coop {f16c_ms:.3}ms ({:.0} GF, {:.2}x) i8coop {coop_ms:.3}ms ({:.0} GF, {:.2}x) best={}",
gf(dp4a_ms), gf(f16c_ms), dp4a_ms / f16c_ms, gf(coop_ms), dp4a_ms / coop_ms,
if f16c_ms < dp4a_ms && f16c_ms < coop_ms { "f16coop" }
else if dp4a_ms <= coop_ms { "dp4a" } else { "i8coop" }
);
}
}
/// The taller-BM dp4a tiles (BM=128/256, fewer cold-weight re-reads) must produce the same result
/// as the BM=64 default -- same int8 math, only the M-tiling and grid.y differ, so the tail guards
/// (mrow<mcount) at m not a multiple of 128/256 are the risk. Scale-relative (f32 add order differs).
#[test]
fn q4k_dp4a_bm_variants_match() {
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => { eprintln!("[q4k-bm] no vulkan device ({e}); skipping"); return; }
};
if !dev.inner.int_dot8 { eprintln!("[q4k-bm] no int_dot8; skipping"); return; }
let mut s = 0x1234_5678_9ABC_DEF0u64;
let mut next = || { s ^= s << 13; s ^= s >> 7; s ^= s << 17; s };
for (m, n, k) in [(200usize, 256usize, 512usize), (300, 128, 256), (512, 320, 768)] {
let nblk = n * (k / 256);
let wq_bytes: Vec<u8> = (0..nblk * 144).map(|_| next() as u8).collect(); // random Q4_K blocks
let wq = dev.upload_qweight(&wq_bytes).unwrap();
let x: Vec<f32> = (0..m * k).map(|_| (next() % 2000) as f32 / 1000.0 - 1.0).collect();
let xh = dev.upload_f32(&x).unwrap();
let base = dev.matmul_q4k_gpu(&wq, &xh, m, n, k).unwrap().to_vec_f32().unwrap();
let maxref = base.iter().fold(0f32, |a, &v| a.max(v.abs())).max(1e-30);
for bm in ["128", "256"] {
unsafe { std::env::set_var("VK_Q4K_BM", bm) };
let got = dev.matmul_q4k_gpu(&wq, &xh, m, n, k).unwrap().to_vec_f32().unwrap();
unsafe { std::env::remove_var("VK_Q4K_BM") };
let rel = got.iter().zip(&base).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max) / maxref;
eprintln!("[q4k-bm] {m}x{n}x{k} BM={bm} vs BM64 rel={rel:.2e}");
assert!(rel < 1e-5, "Q4_K dp4a BM={bm} {m}x{n}x{k} diverged from BM64: rel={rel:.3e}");
}
}
}
/// The f16 coopmat Q4_K prefill matmul (mul_mm_q4k_coopmat, VK_Q4K_COOPMAT) must agree with the
/// validated int8-dp4a path on identical Q4_K weights. Both decode the same 4.5-bit weight; coopmat
/// rounds weight+activation to f16 and dp4a rounds the activation to int8, so the gate is a loose
/// scale-relative bound (f16 mantissa ~ 1e-3 accumulated over k). Covers the real zen-eco-4b/qwen2
/// projection shapes (gate/up, attn q/o, kv, ffn_down) plus a small edge-tile case.
#[test]
fn q4k_coopmat_matches_dp4a() {
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => { eprintln!("[q4k-coop] no vulkan device ({e}); skipping"); return; }
};
if dev.coopmat_info().is_none() { eprintln!("[q4k-coop] no coopmat; skipping"); return; }
use crate::quantized::k_quants::{BlockQ4K, GgmlType};
let mut s = 0x0BADC0DE_CAFEF00Du64;
let mut next = || { s ^= s << 13; s ^= s >> 7; s ^= s << 17; s };
for (m, n, k) in [
(512usize, 11008usize, 2048usize), // ffn gate/up
(512, 2048, 2048), // attn q/o proj
(512, 256, 2048), // attn k/v proj
(512, 2048, 11008), // ffn_down
(64, 64, 512), // small
(128, 512, 768), // non-square edge tiles
] {
let nb = k / 256;
// Real Q4_K weights (valid f16 d/dmin) -- random bytes would seed Inf/NaN f16 scales.
let mut blocks: Vec<BlockQ4K> = (0..n * nb).map(|_| unsafe { std::mem::zeroed() }).collect();
for r in 0..n {
let rowf: Vec<f32> = (0..k).map(|_| (next() % 2000) as f32 / 1000.0 - 1.0).collect();
BlockQ4K::from_float(&rowf, &mut blocks[r * nb..(r + 1) * nb]);
}
let wq_bytes: &[u8] = unsafe {
std::slice::from_raw_parts(blocks.as_ptr() as *const u8,
blocks.len() * std::mem::size_of::<BlockQ4K>())
};
let wq = dev.upload_qweight(wq_bytes).unwrap();
let x: Vec<f32> = (0..m * k).map(|_| (next() % 2000) as f32 / 1000.0 - 1.0).collect();
let xh = dev.upload_f32(&x).unwrap();
unsafe { std::env::set_var("VK_Q4K_COOPMAT_OFF", "1") };
let base = dev.matmul_q4k_gpu(&wq, &xh, m, n, k).unwrap().to_vec_f32().unwrap();
unsafe { std::env::remove_var("VK_Q4K_COOPMAT_OFF") };
let maxref = base.iter().fold(0f32, |a, &v| a.max(v.abs())).max(1e-30);
let got = dev.matmul_q4k_gpu(&wq, &xh, m, n, k).unwrap().to_vec_f32().unwrap();
let rel = got.iter().zip(&base).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max) / maxref;
eprintln!("[q4k-coop] {m}x{n}x{k} coopmat vs dp4a scale_rel={rel:.2e}");
assert!(rel < 2e-2, "Q4_K coopmat {m}x{n}x{k} diverged from dp4a: scale_rel={rel:.3e}");
}
}
/// Controlled decode gate: a uniform Q6_K weight (every q6=1, scale=1, d=1) times an all-ones
/// activation must yield exactly k on every output. Isolates the 6-bit decode + scale + dp4a from
/// any data-dependence: q6=1 needs ql nibble 1 (byte 0x11), qh 2-bit 0b10 (byte 0xAA), scale 1, d 1.
#[test]
fn q6k_tiled_controlled() {
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => { eprintln!("[q6k-ctrl] no vulkan device ({e}); skipping"); return; }
};
if !dev.inner.int_dot8 { eprintln!("[q6k-ctrl] no int_dot8; skipping"); return; }
let (m, n, k) = (5usize, 3usize, 256usize);
let nblk = n * (k / 256);
let dbits = half::f16::from_f32(1.0).to_bits().to_le_bytes();
let mut blocks = vec![0u8; nblk * 210];
for blk in 0..nblk {
let b = &mut blocks[blk * 210..blk * 210 + 210];
for e in b[0..128].iter_mut() { *e = 0x11; } // ql: both nibbles = 1
for e in b[128..192].iter_mut() { *e = 0xAA; } // qh: all 2-bit fields = 0b10
for e in b[192..208].iter_mut() { *e = 0x01; } // scales = 1
b[208] = dbits[0]; b[209] = dbits[1]; // d = 1.0
}
let wq = dev.quantize_q6k(&blocks, n, k).unwrap();
let x = vec![1.0f32; m * k];
let xh = dev.upload_f32(&x).unwrap();
let got = dev.matmul_q6k_gpu(&wq, &xh, m, n, k).unwrap().to_vec_f32().unwrap();
let worst = got.iter().map(|&v| (v - k as f32).abs()).fold(0f32, f32::max);
eprintln!("[q6k-ctrl] {m}x{n}x{k} uniform q6=1: got[0..3]={:?} expect {k}, worst_abs_err={worst:.3}", &got[0..3.min(got.len())]);
assert!(worst < 0.5, "Q6_K tiled controlled: got {:?} expect {k} (worst {worst})", &got[0..got.len().min(6)]);
}
/// The tiled int8-dp4a Q6_K prefill matmul (mul_mm_q6k_tiled_dp4a) agrees with the trusted column
/// kernel (mul_mat_q6k) on identical Q6_K weight bytes -- so the in-kernel 6-bit decode is correct;
/// the only divergence is the q8 activation quantization the dp4a path adds. Also times both at real
/// prefill shapes when HANZO_MMQ_AB=1 (the win over the column kernel that dominates Q4_K_M prefill).
#[test]
fn mul_mm_q6k_coopmat_and_dp4a_match_column() {
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => { eprintln!("[q6k-tiled] no vulkan device ({e}); skipping"); return; }
};
if !dev.inner.int_dot8 {
eprintln!("[q6k-tiled] device lacks int_dot8; skipping");
return;
}
let mut s = 0x2545F4914F6CDD1Du64;
let mut next = || { s ^= s << 13; s ^= s >> 7; s ^= s << 17; s };
let ab = std::env::var_os("HANZO_MMQ_AB").is_some();
let shapes: &[(usize, usize, usize)] = if ab {
&[(37, 64, 256), (512, 2048, 768), (512, 768, 2048), (512, 4096, 4096)]
} else {
&[(37, 64, 256), (40, 200, 512), (100, 256, 768)]
};
for &(m, n, k) in shapes {
let nblk = n * (k / 256);
// Valid, sane 210-byte Q6_K blocks: random 6-bit codes (ql/qh), small signed scales, a modest
// d. quantize_q6k repacks to the 53-u32 padded layout both kernels read. Sane magnitudes so
// the tiled-vs-column delta is the q8 activation quant, not decode of pathological weights.
let dbits = half::f16::from_f32(0.1).to_bits().to_le_bytes();
let mut blocks = vec![0u8; nblk * 210];
for blk in 0..nblk {
let b = &mut blocks[blk * 210..blk * 210 + 210];
for e in b[0..192].iter_mut() { *e = next() as u8; } // ql[128] + qh[64]
for e in b[192..208].iter_mut() { *e = ((next() % 15) as i64 - 7) as u8; } // scales i8
b[208] = dbits[0];
b[209] = dbits[1];
}
let wq = dev.quantize_q6k(&blocks, n, k).unwrap();
let x: Vec<f32> = (0..m * k).map(|_| (next() % 2000) as f32 / 1000.0 - 1.0).collect();
let xh = dev.upload_f32(&x).unwrap();
// CPU oracle: decode Q6_K exactly (mirrors mul_mat_q6k) into dense f32, matmul with f32 x.
let nblocks = k / 256;
let mut wf = vec![0f32; n * k]; // [n, k]
for nn in 0..n {
for blk in 0..nblocks {
let src = &blocks[(nn * nblocks + blk) * 210..(nn * nblocks + blk) * 210 + 210];
let d = half::f16::from_le_bytes([src[208], src[209]]).to_f32();
for idx in 0..2usize {
let scoff = 192 + 8 * idx;
let qloff = 64 * idx;
let qhoff = 128 + 32 * idx;
for l in 0..32usize {
let is = l >> 4;
let qll = src[qloff + l] as u32;
let qlh = src[qloff + l + 32] as u32;
let qhv = src[qhoff + l] as u32;
let q1 = ((qll & 0xF) | ((qhv & 3) << 4)) as i32 - 32;
let q2 = ((qlh & 0xF) | (((qhv >> 2) & 3) << 4)) as i32 - 32;
let q3 = ((qll >> 4) | (((qhv >> 4) & 3) << 4)) as i32 - 32;
let q4 = ((qlh >> 4) | (((qhv >> 6) & 3) << 4)) as i32 - 32;
let sc = |b: usize| src[b] as i8 as i32;
let base = nn * k + blk * 256 + idx * 128;
wf[base + l] = d * sc(scoff + is) as f32 * q1 as f32;
wf[base + l + 32] = d * sc(scoff + is + 2) as f32 * q2 as f32;
wf[base + l + 64] = d * sc(scoff + is + 4) as f32 * q3 as f32;
wf[base + l + 96] = d * sc(scoff + is + 6) as f32 * q4 as f32;
}
}
}
}
let mut want = vec![0f32; m * n];
for mm in 0..m {
for nn in 0..n {
let mut acc = 0f32;
for kk in 0..k { acc += x[mm * k + kk] * wf[nn * k + kk]; }
want[mm * n + nn] = acc;
}
}
let relof = |g: &[f32]| -> f32 {
let maxref = want.iter().fold(0f32, |a, &v| a.max(v.abs())).max(1e-30);
g.iter().zip(&want).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max) / maxref
};
unsafe { std::env::set_var("VK_Q6K_LEGACY", "1") };
let col = dev.matmul_q6k_gpu(&wq, &xh, m, n, k).unwrap().to_vec_f32().unwrap();
unsafe { std::env::remove_var("VK_Q6K_LEGACY") };
// Default path is the f16 coopmat GEMM; the ragged m (37/40/100) also exercises its
// output-row padding. The int8 dp4a tile stays reachable as the A/B fallback.
let got_cm = dev.matmul_q6k_gpu(&wq, &xh, m, n, k).unwrap().to_vec_f32().unwrap();
unsafe { std::env::set_var("VK_Q6K_COOPMAT_OFF", "1") };
let got_dp4a = dev.matmul_q6k_gpu(&wq, &xh, m, n, k).unwrap().to_vec_f32().unwrap();
unsafe { std::env::remove_var("VK_Q6K_COOPMAT_OFF") };
let (rel_cm, rel_dp4a) = (relof(&got_cm), relof(&got_dp4a));
eprintln!("[q6k] {m}x{n}x{k} column_vs_cpu={:.2e} coopmat_vs_cpu={:.2e} dp4a_vs_cpu={:.2e}", relof(&col), rel_cm, rel_dp4a);
assert!(rel_cm < 2e-2, "Q6_K coopmat {m}x{n}x{k} diverged from CPU: rel={rel_cm:.3e}");
assert!(rel_dp4a < 2e-2, "Q6_K dp4a {m}x{n}x{k} diverged from CPU: rel={rel_dp4a:.3e}");
if ab {
let iters = 20;
unsafe { std::env::set_var("VK_Q6K_LEGACY", "1") };
for _ in 0..3 { let _ = dev.matmul_q6k_gpu(&wq, &xh, m, n, k).unwrap(); }
dev.synchronize().unwrap();
let t = std::time::Instant::now();
for _ in 0..iters { let _ = dev.matmul_q6k_gpu(&wq, &xh, m, n, k).unwrap(); }
dev.synchronize().unwrap();
let col_ms = t.elapsed().as_secs_f64() * 1e3 / iters as f64;
unsafe { std::env::remove_var("VK_Q6K_LEGACY") };
for _ in 0..3 { let _ = dev.matmul_q6k_gpu(&wq, &xh, m, n, k).unwrap(); }
dev.synchronize().unwrap();
let t = std::time::Instant::now();
for _ in 0..iters { let _ = dev.matmul_q6k_gpu(&wq, &xh, m, n, k).unwrap(); }
dev.synchronize().unwrap();
let til_ms = t.elapsed().as_secs_f64() * 1e3 / iters as f64;
eprintln!(
"[q6k-ab] {m}x{n}x{k} column {col_ms:.3}ms tiled {til_ms:.3}ms speedup={:.2}x",
col_ms / til_ms
);
}
}
}
/// Controlled decode gate for the COOPMAT path: a uniform Q6_K weight (every q6=1, scale=1, d=1)
/// times an all-ones activation must yield exactly k on every output. Aligned shape (m,nout % 16) so
/// matmul_q6k_gpu takes the coopmat arm; isolates the 6-bit decode + scale from any data-dependence.
/// q6=1 needs ql nibble 1 (byte 0x11), qh 2-bit 0b10 (byte 0xAA), scale 1, d 1 -- the same weight
/// bytes as q6k_tiled_controlled, but decoded into f16 LDS (1.0 is f16-exact => the f32 accumulator
/// sums to exactly k). This is the localizer for a wrong ql/qh byte base (cf. the dp4a QHB scar).
#[test]
fn q6k_coopmat_controlled() {
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => { eprintln!("[q6k-coop-ctrl] no vulkan device ({e}); skipping"); return; }
};
if dev.coopmat_info().is_none() { eprintln!("[q6k-coop-ctrl] no coopmat; skipping"); return; }
let (m, n, k) = (16usize, 16usize, 512usize); // m,n % 16 => coopmat arm; k spans 2 super-blocks
let nblk = n * (k / 256);
let dbits = half::f16::from_f32(1.0).to_bits().to_le_bytes();
let mut blocks = vec![0u8; nblk * 210];
for blk in 0..nblk {
let b = &mut blocks[blk * 210..blk * 210 + 210];
for e in b[0..128].iter_mut() { *e = 0x11; } // ql: both nibbles = 1
for e in b[128..192].iter_mut() { *e = 0xAA; } // qh: all 2-bit fields = 0b10
for e in b[192..208].iter_mut() { *e = 0x01; } // scales = 1
b[208] = dbits[0]; b[209] = dbits[1]; // d = 1.0
}
let wq = dev.quantize_q6k(&blocks, n, k).unwrap();
let x = vec![1.0f32; m * k];
let xh = dev.upload_f32(&x).unwrap();
let got = dev.matmul_q6k_gpu(&wq, &xh, m, n, k).unwrap().to_vec_f32().unwrap();
let worst = got.iter().map(|&v| (v - k as f32).abs()).fold(0f32, f32::max);
eprintln!("[q6k-coop-ctrl] {m}x{n}x{k} uniform q6=1: got[0..3]={:?} expect {k}, worst_abs_err={worst:.3}", &got[0..3.min(got.len())]);
assert!(worst < 0.5, "Q6_K coopmat controlled: got {:?} expect {k} (worst {worst})", &got[0..got.len().min(6)]);
}
/// The f16 coopmat Q6_K prefill matmul (mul_mm_q6k_coopmat) agrees with a CPU f32 oracle (decode
/// mirrors mul_mat_q6k) and the trusted column kernel on identical Q6_K weight bytes. The coopmat
/// path rounds weight+activation to f16, so the gate is a loose scale-relative bound (f16 mantissa
/// ~1e-3 accumulated over k). Covers the real zen-eco-4b projection shapes plus small/edge tiles;
/// all have m,nout multiples of 16 so matmul_q6k_gpu takes the coopmat arm.
#[test]
fn q6k_coopmat_matches_column() {
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => { eprintln!("[q6k-coop] no vulkan device ({e}); skipping"); return; }
};
if dev.coopmat_info().is_none() { eprintln!("[q6k-coop] no coopmat; skipping"); return; }
let mut s = 0x51C6C0DEF00DBEEFu64;
let mut next = || { s ^= s << 13; s ^= s >> 7; s ^= s << 17; s };
for (m, n, k) in [
(512usize, 2048usize, 768usize), // ffn gate/up-ish
(512, 768, 2048), // ffn_down (Q6_K in Q4_K_M)
(512, 4096, 4096), // square
(512, 256, 2048), // attn kv proj
(64, 64, 512), // small
(128, 512, 768), // non-square edge tiles
] {
let nblk = n * (k / 256);
let dbits = half::f16::from_f32(0.1).to_bits().to_le_bytes();
let mut blocks = vec![0u8; nblk * 210];
for blk in 0..nblk {
let b = &mut blocks[blk * 210..blk * 210 + 210];
for e in b[0..192].iter_mut() { *e = next() as u8; } // ql[128] + qh[64]
for e in b[192..208].iter_mut() { *e = ((next() % 15) as i64 - 7) as u8; } // scales i8
b[208] = dbits[0];
b[209] = dbits[1];
}
let wq = dev.quantize_q6k(&blocks, n, k).unwrap();
let x: Vec<f32> = (0..m * k).map(|_| (next() % 2000) as f32 / 1000.0 - 1.0).collect();
let xh = dev.upload_f32(&x).unwrap();
// CPU oracle: decode Q6_K exactly (mirrors mul_mat_q6k) into dense f32, matmul with f32 x.
let nblocks = k / 256;
let mut wf = vec![0f32; n * k];
for nn in 0..n {
for blk in 0..nblocks {
let src = &blocks[(nn * nblocks + blk) * 210..(nn * nblocks + blk) * 210 + 210];
let d = half::f16::from_le_bytes([src[208], src[209]]).to_f32();
for idx in 0..2usize {
let scoff = 192 + 8 * idx;
let qloff = 64 * idx;
let qhoff = 128 + 32 * idx;
for l in 0..32usize {
let is = l >> 4;
let qll = src[qloff + l] as u32;
let qlh = src[qloff + l + 32] as u32;
let qhv = src[qhoff + l] as u32;
let q1 = ((qll & 0xF) | ((qhv & 3) << 4)) as i32 - 32;
let q2 = ((qlh & 0xF) | (((qhv >> 2) & 3) << 4)) as i32 - 32;
let q3 = ((qll >> 4) | (((qhv >> 4) & 3) << 4)) as i32 - 32;
let q4 = ((qlh >> 4) | (((qhv >> 6) & 3) << 4)) as i32 - 32;
let sc = |b: usize| src[b] as i8 as i32;
let base = nn * k + blk * 256 + idx * 128;
wf[base + l] = d * sc(scoff + is) as f32 * q1 as f32;
wf[base + l + 32] = d * sc(scoff + is + 2) as f32 * q2 as f32;
wf[base + l + 64] = d * sc(scoff + is + 4) as f32 * q3 as f32;
wf[base + l + 96] = d * sc(scoff + is + 6) as f32 * q4 as f32;
}
}
}
}
let mut want = vec![0f32; m * n];
for mm in 0..m {
for nn in 0..n {
let mut acc = 0f32;
for kk in 0..k { acc += x[mm * k + kk] * wf[nn * k + kk]; }
want[mm * n + nn] = acc;
}
}
let relof = |g: &[f32]| -> f32 {
let maxref = want.iter().fold(0f32, |a, &v| a.max(v.abs())).max(1e-30);
g.iter().zip(&want).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max) / maxref
};
unsafe { std::env::set_var("VK_Q6K_LEGACY", "1") }; // column kernel reference
let col = dev.matmul_q6k_gpu(&wq, &xh, m, n, k).unwrap().to_vec_f32().unwrap();
unsafe { std::env::remove_var("VK_Q6K_LEGACY") };
let got = dev.matmul_q6k_gpu(&wq, &xh, m, n, k).unwrap().to_vec_f32().unwrap(); // coopmat default
let rel = relof(&got);
eprintln!("[q6k-coop] {m}x{n}x{k} coopmat_vs_cpu={rel:.2e} column_vs_cpu={:.2e}", relof(&col));
assert!(rel < 2e-2, "Q6_K coopmat {m}x{n}x{k} diverged from CPU: scale_rel={rel:.3e}");
}
}
// Proof that the Vulkan decode COMMAND-GRAPH (begin/end_graph_capture + VkGraph::replay), the
// BufPool capture pinning, and the device-offset copy2d_off together replay BIT-EXACTLY and, the
// crux, ADVANCE the KV-write slot per replay -- the exact stale-buffer hazard a record-once graph
// faces. Mirrors decode: a stable KV-like cache, a stable "new token" src, and a stable device
// position buffer. The captured graph (two dispatches, exercising the in-graph RAW barrier)
// appends src into cache[pos] via the device-offset copy, then mirrors the whole cache; every
// replay refreshes only src + pos in place. A frozen push-constant offset would leave all but one
// slot stale -- caught by the per-slot assert. Graph output must equal the eager per-token append
// byte-for-byte (the ship-criterion "graph-on == graph-off", proven at the ml layer).
#[test]
fn decode_command_graph_replays_bit_exact() {
const WIDTH: usize = 64; // KV row width (head_dim-like)
const N: usize = 6; // decode tokens
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[graph-proof] no vulkan device ({e}); skipping");
return;
}
};
if dev.inner.push_descriptor.is_none() {
eprintln!("[graph-proof] no VK_KHR_push_descriptor (capture path unsupported); skipping");
return;
}
// Distinct data per token so any stale slot is detectable.
let token: Vec<Vec<f32>> = (0..N)
.map(|t| (0..WIDTH).map(|i| (t * 1000 + i) as f32 + 0.5).collect())
.collect();
let zero = vec![0f32; N * WIDTH];
// --- EAGER reference: append each token into a fresh cache, one eager op per token ---
let eager_cache = dev.alloc_f32(N * WIDTH).unwrap();
unsafe {
dev.write_f32(
eager_cache.buffer,
eager_cache.memory,
eager_cache.host_visible,
&zero,
)
.unwrap();
}
for (t, td) in token.iter().enumerate() {
let src = dev.upload_f32(td).unwrap();
let pos = dev.upload_u32(&[t as u32]).unwrap();
// dst base = pos[0] * WIDTH; one row of WIDTH contiguous elems.
src.copy2d_off(&eager_cache, &pos, 1, WIDTH, WIDTH, WIDTH, 0, WIDTH)
.unwrap();
}
dev.flush().unwrap();
let eager = eager_cache.to_vec_f32().unwrap();
// --- GRAPH: capture once, replay per token refreshing only src + pos in place ---
let g_cache = dev.alloc_f32(N * WIDTH).unwrap();
let mut g_mirror = dev.alloc_f32(N * WIDTH).unwrap();
let g_src = dev.alloc_f32(WIDTH).unwrap();
let g_pos = dev.alloc_u32(1).unwrap();
unsafe {
dev.write_f32(g_cache.buffer, g_cache.memory, g_cache.host_visible, &zero)
.unwrap();
// Seed the stable inputs with token 0 so the capture records a well-formed forward.
dev.write_f32(g_src.buffer, g_src.memory, g_src.host_visible, &token[0])
.unwrap();
dev.write_u32(g_pos.buffer, g_pos.memory, g_pos.host_visible, &[0])
.unwrap();
}
dev.begin_graph_capture().unwrap();
// dispatch 1: device-offset append of the stable src into cache[pos].
g_src
.copy2d_off(&g_cache, &g_pos, 1, WIDTH, WIDTH, WIDTH, 0, WIDTH)
.unwrap();
// dispatch 2: mirror the WHOLE cache (RAW on cache from dispatch 1 -> in-graph barrier).
g_cache
.copy2d(&mut g_mirror, N, WIDTH, WIDTH, WIDTH, 0, 0)
.unwrap();
let graph = dev.end_graph_capture().unwrap();
eprintln!(
"[graph-proof] captured {} dispatches into the decode command-graph",
graph.n_dispatch()
);
for (t, td) in token.iter().enumerate() {
unsafe {
dev.write_f32(g_src.buffer, g_src.memory, g_src.host_visible, td)
.unwrap();
dev.write_u32(g_pos.buffer, g_pos.memory, g_pos.host_visible, &[t as u32])
.unwrap();
}
graph.replay().unwrap();
}
let got_cache = g_cache.to_vec_f32().unwrap();
let got_mirror = g_mirror.to_vec_f32().unwrap();
// Per-slot: each advancing slot holds ITS token (a frozen offset fails all but one).
for (t, td) in token.iter().enumerate() {
let slot = &got_cache[t * WIDTH..(t + 1) * WIDTH];
assert_eq!(
slot, &td[..],
"graph replay slot {t} stale/wrong -> KV-write offset did not advance (frozen-buffer bug)"
);
}
// Byte-for-byte identical to the eager per-token append (graph-on == graph-off).
assert_eq!(
got_cache, eager,
"graph decode cache diverged from eager append"
);
// The RAW-barriered second dispatch mirrored the final cache exactly.
assert_eq!(
got_mirror, eager,
"in-graph RAW-barriered mirror diverged (barrier not replayed correctly)"
);
eprintln!(
"[graph-proof] {N} replays bit-exact vs eager; every advancing KV slot correct; in-graph RAW barrier replayed. Command-graph mechanism VERIFIED."
);
}
// Proof that fused GQA flash SDPA replays correctly INSIDE a command graph while the attended KV
// span GROWS -- the decode attention piece. The captured graph binds the FULL fixed-shape KV cache
// and a caller-owned stable `meta` (sdpa_blk_vk_graph); each replay attends [0, seq_k) by
// refreshing only `meta[1]` in place. Output at every seq_k must match an eager CPU flash-softmax
// over that exact span (a frozen seq_k would attend the wrong -- warmup -- span).
#[test]
fn sdpa_in_command_graph_dynamic_seqk_bit_exact() {
const H: usize = 8;
const KV: usize = 2;
const D: usize = 128; // sdpa_blk .spv bakes d=128
const CAP: usize = 8; // KV cache capacity
let dev = match VulkanDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!("[graph-sdpa] no vulkan device ({e}); skipping");
return;
}
};
if dev.inner.push_descriptor.is_none() {
eprintln!("[graph-sdpa] no VK_KHR_push_descriptor; skipping");
return;
}
let f = |x: usize| ((x * 2654435761usize) & 0xffff) as f32 / 65535.0 - 0.5;
let q: Vec<f32> = (0..H * D).map(f).collect();
let kf: Vec<f32> = (0..KV * CAP * D).map(|i| f(i + 7)).collect();
let vf: Vec<f32> = (0..KV * CAP * D).map(|i| f(i + 13)).collect();
let scale = 1.0f32 / (D as f32).sqrt();
let groups = H / KV;
// CPU flash-softmax reference for a given attended span seq_k.
let cpu_ref = |seq_k: usize| -> Vec<f32> {
let mut out = vec![0f32; H * D];
for h in 0..H {
let kv = h / groups;
let qb = h * D;
let sc: Vec<f32> = (0..seq_k)
.map(|kk| {
(0..D).map(|dd| q[qb + dd] * kf[(kv * CAP + kk) * D + dd]).sum::<f32>() * scale
})
.collect();
let m = sc.iter().cloned().fold(f32::MIN, f32::max);
let ex: Vec<f32> = sc.iter().map(|s| (s - m).exp()).collect();
let sum: f32 = ex.iter().sum();
for dd in 0..D {
out[qb + dd] =
(0..seq_k).map(|kk| ex[kk] / sum * vf[(kv * CAP + kk) * D + dd]).sum();
}
}
out
};
let qs = dev.upload_f32(&q).unwrap();
let ks = dev.upload_f32(&kf).unwrap();
let vs = dev.upload_f32(&vf).unwrap();
let out = dev.alloc_f32(H * D).unwrap();
let scale_buf = dev.upload_f32(&[scale]).unwrap();
// Stable meta; full-cache strides constant, seq_k (field 1) refreshed per replay.
let meta = dev
.upload_u32(&[
1,
CAP as u32,
H as u32,
KV as u32,
0,
(KV * CAP * D) as u32,
(CAP * D) as u32,
D as u32,
])
.unwrap();
dev.begin_graph_capture().unwrap();
dev.sdpa_blk_vk_graph(&qs, &ks, &vs, &out, &scale_buf, &meta, 1, H, 1)
.unwrap();
let graph = dev.end_graph_capture().unwrap();
let maxref = (1..=CAP)
.flat_map(|sk| cpu_ref(sk))
.fold(0f32, |m, v| m.max(v.abs()))
.max(1e-30);
let mut worst = 0f32;
for &seq_k in &[1usize, 3, 5, 8] {
unsafe {
dev.write_u32(meta.buffer, meta.memory, meta.host_visible, &[1, seq_k as u32])
.unwrap();
}
graph.replay().unwrap();
let got = out.to_vec_f32().unwrap();
let refv = cpu_ref(seq_k);
let err = got
.iter()
.zip(&refv)
.map(|(a, b)| (a - b).abs())
.fold(0f32, f32::max);
worst = worst.max(err / maxref);
}
eprintln!("[graph-sdpa] sdpa replayed in-graph over growing seq_k in {{1,3,5,8}} scale_rel={worst:.2e}");
assert!(
worst < 1e-4,
"graph-replayed sdpa diverged from eager span attention: scale_rel={worst:.3e}"
);
}
}