ferrum-kernels 0.8.6

Unified compute kernels (CUDA/Metal/CPU) and model runner for Ferrum inference
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
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
//! Marlin INT4xFP16 fused GEMM kernel (IST Austria).
//!
//! Near-ideal 3.9x speedup over FP16 cuBLAS for INT4 quantized weights.
//! Weights must be in Marlin packed format (different from GPTQ).
//!
//! Constraints: K % 128 == 0, N % 256 == 0, SM >= 8.0 (Ampere+).

use cudarc::driver::{CudaSlice, CudaStream, DevicePtr};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock};

use crate::backend::native_status::StagedNativeStatus;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct CudaMarlinRuntimeConfig {
    profile: bool,
    skip_ws_zero: bool,
    trace_shapes: bool,
    trace_shapes_max: u64,
}

impl CudaMarlinRuntimeConfig {
    fn from_env() -> Self {
        Self::from_env_vars(std::env::vars())
    }

    fn from_env_vars<I, K, V>(vars: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<str>,
        V: AsRef<str>,
    {
        let mut config = Self {
            profile: false,
            skip_ws_zero: false,
            trace_shapes: false,
            trace_shapes_max: 256,
        };
        for (name, value) in vars {
            match name.as_ref() {
                "FERRUM_MARLIN_PROFILE" => config.profile = value.as_ref() == "1",
                "FERRUM_MARLIN_SKIP_WS_ZERO" => config.skip_ws_zero = value.as_ref() == "1",
                "FERRUM_MARLIN_TRACE_SHAPES" => config.trace_shapes = value.as_ref() == "1",
                "FERRUM_MARLIN_TRACE_SHAPES_MAX" => {
                    if let Ok(max) = value.as_ref().parse::<u64>() {
                        config.trace_shapes_max = max;
                    }
                }
                _ => {}
            }
        }
        config
    }
}

fn cuda_marlin_runtime_config() -> &'static CudaMarlinRuntimeConfig {
    static CONFIG: OnceLock<CudaMarlinRuntimeConfig> = OnceLock::new();
    CONFIG.get_or_init(CudaMarlinRuntimeConfig::from_env)
}

/// Cached `FERRUM_MARLIN_SKIP_WS_ZERO=1` flag. Read once on first
/// access, cheap for hot paths (called per Marlin GEMM dispatch).
fn skip_ws_zero() -> bool {
    cuda_marlin_runtime_config().skip_ws_zero
}

fn should_zero_workspace(config: &CudaMarlinRuntimeConfig) -> bool {
    !config.skip_ws_zero
}

/// Profile-only nested dense Marlin counters. They are intentionally not part
/// of normal model timings because callers already time the full projection.
pub static MARLIN_WS_ZERO_TIME_US: AtomicU64 = AtomicU64::new(0);
pub static MARLIN_WS_ZERO_CALLS: AtomicU64 = AtomicU64::new(0);
pub static MARLIN_GATHER_TIME_US: AtomicU64 = AtomicU64::new(0);
pub static MARLIN_GATHER_CALLS: AtomicU64 = AtomicU64::new(0);
pub static MARLIN_KERNEL_TIME_US: AtomicU64 = AtomicU64::new(0);
pub static MARLIN_KERNEL_CALLS: AtomicU64 = AtomicU64::new(0);
static MARLIN_TRACE_SHAPE_CALLS: AtomicU64 = AtomicU64::new(0);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MarlinProfileBucketStats {
    pub ws_zero_us: u64,
    pub ws_zero_calls: u64,
    pub gather_us: u64,
    pub gather_calls: u64,
    pub kernel_us: u64,
    pub kernel_calls: u64,
}

impl MarlinProfileBucketStats {
    pub const ZERO: Self = Self {
        ws_zero_us: 0,
        ws_zero_calls: 0,
        gather_us: 0,
        gather_calls: 0,
        kernel_us: 0,
        kernel_calls: 0,
    };

    fn record_ws_zero(&mut self, us: u64) {
        self.ws_zero_us += us;
        self.ws_zero_calls += 1;
    }

    fn record_gather(&mut self, us: u64) {
        self.gather_us += us;
        self.gather_calls += 1;
    }

    fn record_kernel(&mut self, us: u64) {
        self.kernel_us += us;
        self.kernel_calls += 1;
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MarlinProfileByProjection {
    pub qkv: MarlinProfileBucketStats,
    pub o_proj: MarlinProfileBucketStats,
    pub gate_up: MarlinProfileBucketStats,
    pub down: MarlinProfileBucketStats,
    pub lm_head: MarlinProfileBucketStats,
    pub other: MarlinProfileBucketStats,
}

impl MarlinProfileByProjection {
    pub const ZERO: Self = Self {
        qkv: MarlinProfileBucketStats::ZERO,
        o_proj: MarlinProfileBucketStats::ZERO,
        gate_up: MarlinProfileBucketStats::ZERO,
        down: MarlinProfileBucketStats::ZERO,
        lm_head: MarlinProfileBucketStats::ZERO,
        other: MarlinProfileBucketStats::ZERO,
    };
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MarlinProfileBucket {
    Qkv,
    OProj,
    GateUp,
    Down,
    LmHead,
    Other,
}

static MARLIN_PROFILE_BY_PROJECTION: Mutex<MarlinProfileByProjection> =
    Mutex::new(MarlinProfileByProjection::ZERO);

struct CudaMarlinEventTimer {
    start: cudarc::driver::sys::CUevent,
    end: cudarc::driver::sys::CUevent,
}

impl CudaMarlinEventTimer {
    fn start(raw_stream: cudarc::driver::sys::CUstream) -> Option<Self> {
        use cudarc::driver::sys as cu;
        let mut start: cu::CUevent = std::ptr::null_mut();
        let mut end: cu::CUevent = std::ptr::null_mut();
        unsafe {
            let _ = cu::cuEventCreate(&mut start, 0);
            let _ = cu::cuEventCreate(&mut end, 0);
        }
        if start.is_null() || end.is_null() {
            unsafe {
                if !start.is_null() {
                    let _ = cu::cuEventDestroy_v2(start);
                }
                if !end.is_null() {
                    let _ = cu::cuEventDestroy_v2(end);
                }
            }
            return None;
        }
        let timer = Self { start, end };
        timer.record_start(raw_stream);
        Some(timer)
    }

    fn record_start(&self, raw_stream: cudarc::driver::sys::CUstream) {
        unsafe {
            let _ = cudarc::driver::sys::cuEventRecord(self.start, raw_stream);
        }
    }

    fn finish_us(&self, raw_stream: cudarc::driver::sys::CUstream) -> u64 {
        unsafe {
            let _ = cudarc::driver::sys::cuEventRecord(self.end, raw_stream);
            let _ = cudarc::driver::sys::cuEventSynchronize(self.end);
        }
        (unsafe { cudarc::driver::result::event::elapsed(self.start, self.end) }
            .ok()
            .unwrap_or(0.0) as f64
            * 1000.0) as u64
    }
}

impl Drop for CudaMarlinEventTimer {
    fn drop(&mut self) {
        unsafe {
            let _ = cudarc::driver::sys::cuEventDestroy_v2(self.start);
            let _ = cudarc::driver::sys::cuEventDestroy_v2(self.end);
        }
    }
}

fn marlin_profile_bucket_from_label(label: &str) -> MarlinProfileBucket {
    if label.contains("qkv_proj") {
        MarlinProfileBucket::Qkv
    } else if label.contains("o_proj") {
        MarlinProfileBucket::OProj
    } else if label.contains("gate_up_proj") {
        MarlinProfileBucket::GateUp
    } else if label.contains("down_proj") {
        MarlinProfileBucket::Down
    } else if label.contains("lm_head") {
        MarlinProfileBucket::LmHead
    } else {
        MarlinProfileBucket::Other
    }
}

fn current_marlin_profile_bucket() -> MarlinProfileBucket {
    marlin_profile_bucket_from_label(&super::current_cuda_alloc_label())
}

fn marlin_profile_bucket_mut(
    stats: &mut MarlinProfileByProjection,
    bucket: MarlinProfileBucket,
) -> &mut MarlinProfileBucketStats {
    match bucket {
        MarlinProfileBucket::Qkv => &mut stats.qkv,
        MarlinProfileBucket::OProj => &mut stats.o_proj,
        MarlinProfileBucket::GateUp => &mut stats.gate_up,
        MarlinProfileBucket::Down => &mut stats.down,
        MarlinProfileBucket::LmHead => &mut stats.lm_head,
        MarlinProfileBucket::Other => &mut stats.other,
    }
}

fn with_marlin_profile_bucket_stats(
    bucket: MarlinProfileBucket,
    f: impl FnOnce(&mut MarlinProfileBucketStats),
) {
    let mut stats = MARLIN_PROFILE_BY_PROJECTION
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    f(marlin_profile_bucket_mut(&mut stats, bucket));
}

fn record_marlin_ws_zero(bucket: MarlinProfileBucket, us: u64) {
    with_marlin_profile_bucket_stats(bucket, |stats| stats.record_ws_zero(us));
}

fn record_marlin_gather(bucket: MarlinProfileBucket, us: u64) {
    with_marlin_profile_bucket_stats(bucket, |stats| stats.record_gather(us));
}

fn record_marlin_kernel(bucket: MarlinProfileBucket, us: u64) {
    with_marlin_profile_bucket_stats(bucket, |stats| stats.record_kernel(us));
}

pub fn record_marlin_gather_for_current_label(us: u64) {
    MARLIN_GATHER_TIME_US.fetch_add(us, Ordering::Relaxed);
    MARLIN_GATHER_CALLS.fetch_add(1, Ordering::Relaxed);
    record_marlin_gather(current_marlin_profile_bucket(), us);
}

pub fn drain_marlin_profile_by_projection() -> MarlinProfileByProjection {
    let mut stats = MARLIN_PROFILE_BY_PROJECTION
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    let snapshot = *stats;
    *stats = MarlinProfileByProjection::ZERO;
    snapshot
}

pub fn profile_marlin() -> bool {
    cuda_marlin_runtime_config().profile
}

fn trace_marlin_shapes() -> bool {
    cuda_marlin_runtime_config().trace_shapes
}

fn marlin_shape_trace_max() -> u64 {
    cuda_marlin_runtime_config().trace_shapes_max
}

// FFI declaration for the Marlin CUDA kernel.
// Only linked when the "marlin" feature is enabled (requires nvcc + SM >= 8.0).
#[cfg(feature = "marlin")]
extern "C" {
    fn marlin_cuda(
        A: *const std::ffi::c_void,
        B: *const std::ffi::c_void,
        C: *mut std::ffi::c_void,
        s: *const std::ffi::c_void,
        prob_m: i32,
        prob_n: i32,
        prob_k: i32,
        workspace: *mut std::ffi::c_void,
        groupsize: i32,
        dev: i32,
        stream: cudarc::driver::sys::CUstream,
        thread_k: i32,
        thread_n: i32,
        sms: i32,
        max_par: i32,
        // -1 ⇒ same as prob_n. For offset GEMM into a stacked B/s
        // buffer, pass total_n so b_gl_stride and s_gl_stride see the
        // full N width while iteration covers only the expert subset.
        prob_n_full: i32,
    ) -> i32;

    // Stage 11: fused MoE Marlin. ONE launch processes all experts in a
    // bucket. Caller pre-buckets experts by their thread_m_blocks need
    // (prob_m here = 16 * thread_m_blocks). gridDim.y = expert_count.
    fn marlin_cuda_moe(
        A: *const std::ffi::c_void,
        B: *const std::ffi::c_void,
        C: *mut std::ffi::c_void,
        s: *const std::ffi::c_void,
        prob_m: i32,
        prob_n: i32,
        prob_k: i32,
        workspace: *mut std::ffi::c_void,
        a_row_offsets: *const i32, // device [E_global] cumulative row offsets in A
        tokens_per_expert: *const i32, // device [E_global]
        active_expert_ids: *const i32, // device [expert_count] (or null for identity)
        expert_count: i32,
        b_int4_per_expert: i32,
        s_int4_per_expert: i32,
        locks_i32_per_expert: i32,
        groupsize: i32,
        dev: i32,
        stream: cudarc::driver::sys::CUstream,
        thread_k: i32,
        thread_n: i32,
        sms: i32,
        prob_n_full: i32,
    ) -> i32;
}

// vLLM marlin_moe_wna16 port (Stage 14). Supplied by the versioned native
// operator artifact set. Single fused
// (sorted_token_ids, expert_ids) launch — eliminates the m=16 padding
// waste of our Stage 12.1 path. Linked statically only when the
// `vllm-moe-marlin` feature is built in.
#[cfg(feature = "vllm-moe-marlin")]
extern "C" {
    fn ferrum_vllm_marlin_moe_set_profile_config(
        path: *const std::ffi::c_char,
        commit_sha: *const std::ffi::c_char,
        env_hash: *const std::ffi::c_char,
        model: *const std::ffi::c_char,
        concurrency: i32,
        runtime_flags_json: *const std::ffi::c_char,
    );

    fn ferrum_vllm_marlin_moe_clear_profile_config();

    fn ferrum_vllm_marlin_moe_f16(
        a: *const std::ffi::c_void,        // [size_m, size_k] fp16
        b: *const std::ffi::c_void,        // [num_experts, k/16, n*pack/16] i32 marlin-packed
        c: *mut std::ffi::c_void,          // [size_m * top_k, size_n] fp16
        c_tmp: *mut std::ffi::c_void,      // fp32 scratch (or null)
        b_scales: *const std::ffi::c_void, // [num_experts, num_groups, size_n] fp16
        b_zeros: *const std::ffi::c_void,  // [num_experts, num_groups, size_n/8] i32 or null
        workspace: *mut std::ffi::c_void,  // [N/128 * sms * 4] i32
        sorted_token_ids: *const i32,
        expert_ids: *const i32,
        num_tokens_past_padded: *const i32,
        topk_weights: *const f32, // (or null when mul_topk_weights=0)
        moe_block_size: i32,      // 8 / 16 / 32 / 48 / 64
        top_k: i32,
        mul_topk_weights: i32, // 0 or 1
        is_ep: i32,            // 0 or 1
        prob_m: i32,
        prob_n: i32,
        prob_k: i32,
        group_size: i32, // 128 typically
        has_zp: i32,     // 0 symmetric kU4B8, 1 asymmetric kU4 + b_zeros
        dev: i32,
        stream: cudarc::driver::sys::CUstream,
        use_atomic_add: i32,
        use_fp32_reduce: i32,
    ) -> i32;

    fn ferrum_vllm_marlin_moe_fp8_f16(
        a: *const std::ffi::c_void,        // [size_m, size_k] fp16
        b: *const std::ffi::c_void,        // [num_experts, ...] E4M3 Marlin-packed
        c: *mut std::ffi::c_void,          // [size_m * top_k, size_n] fp16
        c_tmp: *mut std::ffi::c_void,      // fp32 scratch (or null)
        b_scales: *const std::ffi::c_void, // [num_experts, 1, size_n] fp16
        b_zeros: *const std::ffi::c_void,  // always null for E4M3
        workspace: *mut std::ffi::c_void,
        sorted_token_ids: *const i32,
        expert_ids: *const i32,
        num_tokens_past_padded: *const i32,
        topk_weights: *const f32,
        moe_block_size: i32,
        top_k: i32,
        mul_topk_weights: i32,
        is_ep: i32,
        prob_m: i32,
        prob_n: i32,
        prob_k: i32,
        group_size: i32, // -1 for channelwise or 128 for block-grouped E4M3
        has_zp: i32,     // always 0 for E4M3
        dev: i32,
        stream: cudarc::driver::sys::CUstream,
        use_atomic_add: i32,
        use_fp32_reduce: i32,
    ) -> i32;

    fn ferrum_vllm_marlin_moe_mxfp4_bf16(
        a: *const std::ffi::c_void,        // [size_m, size_k] bf16
        b: *const std::ffi::c_void,        // [num_experts, ...] E2M1 Marlin-packed nibbles
        c: *mut std::ffi::c_void,          // [size_m * top_k, size_n] bf16
        c_tmp: *mut std::ffi::c_void,      // fp32 scratch (or null)
        b_bias: *const std::ffi::c_void,   // [num_experts, size_n] bf16
        b_scales: *const std::ffi::c_void, // [num_experts, size_k / 32, size_n] E8M0 bytes
        workspace: *mut std::ffi::c_void,
        sorted_token_ids: *const i32,
        expert_ids: *const i32,
        num_tokens_past_padded: *const i32,
        topk_weights: *const f32,
        moe_block_size: i32,
        top_k: i32,
        mul_topk_weights: i32,
        is_ep: i32,
        prob_m: i32,
        prob_n: i32,
        prob_k: i32,
        group_size: i32, // exactly 32 for E2M1 + E8M0 MXFP4
        dev: i32,
        stream: cudarc::driver::sys::CUstream,
        use_atomic_add: i32,
        use_fp32_reduce: i32,
    ) -> i32;
}

#[cfg(feature = "vllm-moe-marlin")]
pub fn configure_vllm_moe_profile_sink(
    config: &ferrum_bench_core::ProfileSinkConfig,
) -> std::io::Result<()> {
    use std::ffi::CString;

    let Some(path) = &config.jsonl_path else {
        unsafe { ferrum_vllm_marlin_moe_clear_profile_config() };
        return Ok(());
    };

    let path = CString::new(path.as_os_str().to_string_lossy().into_owned()).map_err(|err| {
        std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            format!("profile path contains NUL byte: {err}"),
        )
    })?;
    let commit_sha = CString::new(
        config
            .metadata
            .commit_sha
            .as_deref()
            .unwrap_or_default()
            .to_string(),
    )
    .map_err(profile_cstring_error("profile commit_sha"))?;
    let env_hash = CString::new(config.metadata.env_hash.clone())
        .map_err(profile_cstring_error("env_hash"))?;
    let model =
        CString::new(config.metadata.model.clone()).map_err(profile_cstring_error("model"))?;
    let runtime_flags_json =
        serde_json::to_string(&config.metadata.runtime_flags).unwrap_or_else(|_| "{}".to_string());
    let runtime_flags_json =
        CString::new(runtime_flags_json).map_err(profile_cstring_error("runtime_flags_json"))?;

    unsafe {
        ferrum_vllm_marlin_moe_set_profile_config(
            path.as_ptr(),
            commit_sha.as_ptr(),
            env_hash.as_ptr(),
            model.as_ptr(),
            config.metadata.concurrency.min(i32::MAX as u32) as i32,
            runtime_flags_json.as_ptr(),
        );
    }
    Ok(())
}

#[cfg(feature = "vllm-moe-marlin")]
fn profile_cstring_error(field: &'static str) -> impl FnOnce(std::ffi::NulError) -> std::io::Error {
    move |err| {
        std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            format!("{field} contains NUL byte: {err}"),
        )
    }
}

/// Check if Marlin kernel is available at compile time.
pub fn is_available() -> bool {
    cfg!(feature = "marlin")
}

/// Marlin-format quantized weight for one linear layer.
pub struct MarlinWeight {
    /// Repacked INT4 weights in Marlin tile format: varies by K, N
    pub qweight: CudaSlice<i32>,
    /// Per-group FP16 scales (permuted for Marlin access pattern)
    pub scales: CudaSlice<half::f16>,
    /// Optional per-group GPTQ zero-points for vLLM Marlin-MoE asymmetric
    /// INT4. Stored packed as actual zero-point codes, not AutoGPTQ's
    /// on-disk `qzeros = zero - 1`.
    pub qzeros: Option<CudaSlice<i32>>,
    /// Workspace for Marlin kernel: [N/128 * max_par] int32, zeroed
    pub workspace: CudaSlice<i32>,
    pub k: usize,
    pub n: usize,
    pub group_size: i32,
    /// True when `qweight` is in vLLM Marlin-MoE tile layout. Such stacks
    /// must be dispatched through `marlin_gemm_moe_vllm`, not bucketed
    /// IST-DASLab offset GEMMs.
    pub vllm_moe: bool,
    /// Activation gather permutation for desc_act=true (act-order) GPTQ.
    /// `perm[i]` = original column index that should appear at position i
    /// after gather. Computed at load time as `argsort(g_idx_disk)`.
    /// `qweight` rows have already been permuted by this; runtime gathers
    /// input columns by the same perm so the standard Marlin kernel
    /// produces the un-permuted GEMM result. None for desc_act=false.
    pub perm: Option<CudaSlice<i32>>,
}

/// Run Marlin INT4xFP16 fused GEMM.
///
/// Computes: C[m, n] = A[m, k] @ dequant(B[k, n])
/// where B is in Marlin packed INT4 format.
///
/// Only available when compiled with `--features marlin`.
#[cfg(feature = "marlin")]
pub fn marlin_gemm(
    stream: &Arc<CudaStream>,
    input: &CudaSlice<half::f16>,
    weight: &MarlinWeight,
    output: &mut CudaSlice<half::f16>,
    m: i32,
) -> candle_core::Result<()> {
    let n = weight.n as i32;
    let k = weight.k as i32;

    // Layers with n % 256 != 0 (e.g. a quantized 2048x128 MoE router or a
    // 151936-wide lm_head from repacked repos) only have valid kernel
    // instantiations for the small-m 128x128 tile (THREAD_M_BLOCKS == 1,
    // i.e. m <= 16); the m > 16 auto-config picks 64x256 tiles and the
    // kernel rejects the shape. Split such GEMMs into m <= 16 chunks —
    // a handful of extra ~5us launches on at most two tiny/tall layers.
    if n % 256 != 0 && m > 16 {
        let mut row = 0usize;
        while row < m as usize {
            let chunk = (m as usize - row).min(16);
            let a_view = input.slice(row * weight.k..(row + chunk) * weight.k);
            let mut c_view = output.slice_mut(row * weight.n..(row + chunk) * weight.n);
            marlin_gemm_chunk(stream, &a_view, weight, &mut c_view, chunk as i32)?;
            row += chunk;
        }
        return Ok(());
    }
    marlin_gemm_chunk(
        stream,
        &input.slice(..),
        weight,
        &mut output.slice_mut(..),
        m,
    )
}

fn marlin_gemm_chunk(
    stream: &Arc<CudaStream>,
    input: &cudarc::driver::CudaView<'_, half::f16>,
    weight: &MarlinWeight,
    output: &mut cudarc::driver::CudaViewMut<'_, half::f16>,
    m: i32,
) -> candle_core::Result<()> {
    let n = weight.n as i32;
    let k = weight.k as i32;

    let raw_stream = stream.cu_stream();
    let profile = profile_marlin();
    let profile_bucket = profile.then(current_marlin_profile_bucket);

    // Zero workspace on the runner's stream — Marlin uses it as mutex locks.
    // All operations (memset + kernel) on same stream → naturally ordered.
    if should_zero_workspace(cuda_marlin_runtime_config()) {
        let timer = profile
            .then(|| CudaMarlinEventTimer::start(raw_stream))
            .flatten();
        let (ws_ptr, _guard) = weight.workspace.device_ptr(stream);
        unsafe {
            cudarc::driver::sys::cuMemsetD32Async(ws_ptr, 0, weight.workspace.len(), raw_stream);
        }
        if let Some(timer) = timer {
            let elapsed_us = timer.finish_us(raw_stream);
            MARLIN_WS_ZERO_TIME_US.fetch_add(elapsed_us, Ordering::Relaxed);
            MARLIN_WS_ZERO_CALLS.fetch_add(1, Ordering::Relaxed);
            if let Some(bucket) = profile_bucket {
                record_marlin_ws_zero(bucket, elapsed_us);
            }
        }
    }

    // Get raw device pointers
    let (a_ptr, _a_guard) = input.device_ptr(stream);
    let (b_ptr, _b_guard) = weight.qweight.device_ptr(stream);
    let (c_ptr, _c_guard) = output.device_ptr(stream);
    let (s_ptr, _s_guard) = weight.scales.device_ptr(stream);
    let (ws_ptr, _ws_guard) = weight.workspace.device_ptr(stream);

    if trace_marlin_shapes() {
        let call = MARLIN_TRACE_SHAPE_CALLS.fetch_add(1, Ordering::Relaxed);
        if call < marlin_shape_trace_max() {
            let label = super::current_cuda_alloc_label();
            let bucket = marlin_profile_bucket_from_label(&label);
            eprintln!(
                "[marlin-shape-trace] call={} label={} bucket={:?} m={} n={} k={} gs={} qweight_len={} scales_len={} workspace_len={} a=0x{:x} b=0x{:x} c=0x{:x} s=0x{:x} ws=0x{:x}",
                call,
                label,
                bucket,
                m,
                n,
                k,
                weight.group_size,
                weight.qweight.len(),
                weight.scales.len(),
                weight.workspace.len(),
                a_ptr,
                b_ptr,
                c_ptr,
                s_ptr,
                ws_ptr,
            );
        }
    }

    let timer = profile
        .then(|| CudaMarlinEventTimer::start(raw_stream))
        .flatten();
    let ret = unsafe {
        marlin_cuda(
            a_ptr as *const _,
            b_ptr as *const _,
            c_ptr as *mut _,
            s_ptr as *const _,
            m,
            n,
            k,
            ws_ptr as *mut _,
            weight.group_size,
            0, // dev
            raw_stream,
            -1, // auto thread_k
            -1, // auto thread_n
            -1, // auto sms
            16, // max_par
            -1, // prob_n_full = prob_n (non-stacked)
        )
    };
    if let Some(timer) = timer {
        let elapsed_us = timer.finish_us(raw_stream);
        MARLIN_KERNEL_TIME_US.fetch_add(elapsed_us, Ordering::Relaxed);
        MARLIN_KERNEL_CALLS.fetch_add(1, Ordering::Relaxed);
        if let Some(bucket) = profile_bucket {
            record_marlin_kernel(bucket, elapsed_us);
        }
    }

    if ret != 0 {
        return Err(candle_core::Error::Msg(format!(
            "marlin_cuda failed: ret={ret} (m={m}, n={n}, k={k}, gs={})",
            weight.group_size
        )));
    }

    // No per-call sync needed — all operations (memset + kernel) are on the
    // runner's stream. decode_step syncs once at the end before returning logits.
    Ok(())
}

/// Stub when Marlin feature is not enabled.
#[cfg(not(feature = "marlin"))]
pub fn marlin_gemm(
    _stream: &Arc<CudaStream>,
    _input: &CudaSlice<half::f16>,
    _weight: &MarlinWeight,
    _output: &mut CudaSlice<half::f16>,
    _m: i32,
) -> candle_core::Result<()> {
    Err(candle_core::Error::Msg(
        "Marlin kernel not available (compile with --features marlin)".into(),
    ))
}

/// Marlin GEMM on a column-slice of a stacked weight (used for MoE
/// expert dispatch). The stacked `weight` holds num_experts × n_per_expert
/// columns concatenated along N; this call processes columns
/// `[expert_offset .. expert_offset + expert_n)` only.
///
/// `expert_offset` and `expert_n` MUST be multiples of Marlin's `tile_n`
/// (typically 64). The repack laid out the whole N contiguously so a
/// pointer offset lands on a tile boundary.
///
/// Workspace: shares the parent stacked workspace; we offset its pointer
/// by `expert_offset / 128` ints so each expert uses its own mutex slot
/// range.
#[cfg(feature = "marlin")]
pub fn marlin_gemm_with_offset(
    stream: &Arc<CudaStream>,
    input: &CudaSlice<half::f16>,
    weight: &MarlinWeight,
    output: &mut CudaSlice<half::f16>,
    m: i32,
    expert_offset: i32,
    expert_n: i32,
) -> candle_core::Result<()> {
    use cudarc::driver::DevicePtr;
    let n = expert_n;
    let k = weight.k as i32;
    if expert_offset < 0 || expert_n <= 0 || expert_offset + expert_n > weight.n as i32 {
        return Err(candle_core::Error::Msg(format!(
            "marlin offset out of range: offset={expert_offset} n={expert_n} stacked_n={}",
            weight.n
        )));
    }
    let raw_stream = stream.cu_stream();

    // PER-EXPERT CONTIGUOUS LAYOUT (built by load_gptq_stacked):
    // Each expert's packed bytes are CONTIGUOUS in the buffer.
    // Buffer = [exp0_marlin_tile | exp1_marlin_tile | ...].
    // expert_idx is implicit: expert_offset / expert_n.
    //
    // qweight: per-expert tile = (n_per_expert * k / 8) i32. Offset
    //          by expert_idx × that_size i32.
    // scales:  per-expert tile = (k/group_size * n_per_expert) f16.
    //          Offset by expert_idx × that_size f16.
    // workspace: per-expert range = (n_per_expert/128) * MAX_PAR i32.
    //          Offset by expert_idx × that_size i32.
    //
    // Marlin sees a regular N=expert_n tile per call. prob_n =
    // prob_n_full = expert_n (no stride decoupling needed).
    let expert_idx = (expert_offset / expert_n) as usize;
    let n_per = expert_n as usize;
    let k_us = k as usize;

    const MAX_PAR: usize = 16;
    let ws_per_expert = (n_per / 128).max(1) * MAX_PAR;
    let ws_offset_bytes = expert_idx * ws_per_expert * std::mem::size_of::<i32>();
    if should_zero_workspace(cuda_marlin_runtime_config()) {
        let (ws_ptr, _g) = weight.workspace.device_ptr(stream);
        unsafe {
            cudarc::driver::sys::cuMemsetD32Async(
                ws_ptr + ws_offset_bytes as u64,
                0,
                ws_per_expert,
                raw_stream,
            );
        }
    }

    let qw_per_expert_i32 = (n_per * k_us) / 8;
    let qw_offset_bytes = expert_idx * qw_per_expert_i32 * std::mem::size_of::<i32>();

    let num_groups = k_us / weight.group_size as usize;
    let sc_per_expert_f16 = num_groups * n_per;
    let scales_offset_bytes = expert_idx * sc_per_expert_f16 * std::mem::size_of::<half::f16>();

    let (a_ptr, _a_guard) = input.device_ptr(stream);
    let (b_ptr_full, _b_guard) = weight.qweight.device_ptr(stream);
    let (c_ptr, _c_guard) = output.device_ptr(stream);
    let (s_ptr_full, _s_guard) = weight.scales.device_ptr(stream);
    let (ws_ptr_full, _ws_guard) = weight.workspace.device_ptr(stream);
    let b_ptr = b_ptr_full + qw_offset_bytes as u64;
    let s_ptr = s_ptr_full + scales_offset_bytes as u64;
    let ws_ptr = ws_ptr_full + ws_offset_bytes as u64;

    let ret = unsafe {
        marlin_cuda(
            a_ptr as *const _,
            b_ptr as *const _,
            c_ptr as *mut _,
            s_ptr as *const _,
            m,
            n,
            k,
            ws_ptr as *mut _,
            weight.group_size,
            0,
            raw_stream,
            -1,
            -1,
            -1,
            16,
            // Per-expert contiguous: stride == iteration width.
            -1,
        )
    };
    if ret != 0 {
        return Err(candle_core::Error::Msg(format!(
            "marlin_cuda (offset) failed ret={ret} m={m} n={n} k={k} offset={expert_offset}"
        )));
    }
    Ok(())
}

#[cfg(not(feature = "marlin"))]
pub fn marlin_gemm_with_offset(
    _stream: &Arc<CudaStream>,
    _input: &CudaSlice<half::f16>,
    _weight: &MarlinWeight,
    _output: &mut CudaSlice<half::f16>,
    _m: i32,
    _expert_offset: i32,
    _expert_n: i32,
) -> candle_core::Result<()> {
    Err(candle_core::Error::Msg(
        "Marlin kernel not available (compile with --features marlin)".into(),
    ))
}

/// Same as [`marlin_gemm_with_offset`] but also strides the input and
/// output buffers by row offsets. Used by the bucketed MoE dispatcher
/// to run a single expert's column-slice GEMM against a sub-range of
/// the packed input/output buffer without needing a buffer-view type.
///
/// `in_row_offset` rows of `K` f16 elements at the start of `input`
/// are skipped; `out_row_offset` rows of `expert_n` f16 elements at
/// the start of `output` are skipped.
#[cfg(feature = "marlin")]
#[allow(clippy::too_many_arguments)]
pub fn marlin_gemm_with_offset_strided(
    stream: &Arc<CudaStream>,
    input: &CudaSlice<half::f16>,
    in_row_offset: i32,
    weight: &MarlinWeight,
    output: &mut CudaSlice<half::f16>,
    out_row_offset: i32,
    m: i32,
    expert_offset: i32,
    expert_n: i32,
) -> candle_core::Result<()> {
    use cudarc::driver::DevicePtr;
    let n = expert_n;
    let k = weight.k as i32;
    if expert_offset < 0 || expert_n <= 0 || expert_offset + expert_n > weight.n as i32 {
        return Err(candle_core::Error::Msg(format!(
            "marlin offset out of range: offset={expert_offset} n={expert_n} stacked_n={}",
            weight.n
        )));
    }
    let raw_stream = stream.cu_stream();

    // Per-expert contiguous layout, same offset arithmetic as
    // marlin_gemm_with_offset.
    let expert_idx = (expert_offset / expert_n) as usize;
    let n_per = expert_n as usize;
    let k_us = k as usize;

    const MAX_PAR: usize = 16;
    let ws_per_expert = (n_per / 128).max(1) * MAX_PAR;
    let ws_offset_bytes = expert_idx * ws_per_expert * std::mem::size_of::<i32>();
    // Skip per-call workspace zeroing if env says so. Caller is then
    // responsible for bulk-zeroing the workspace before the batch
    // (saves N-1 cuMemsetD32Async launches per phase). Cached on
    // first access — std::env::var is too slow for the hot path.
    if !skip_ws_zero() {
        let (ws_ptr, _g) = weight.workspace.device_ptr(stream);
        unsafe {
            cudarc::driver::sys::cuMemsetD32Async(
                ws_ptr + ws_offset_bytes as u64,
                0,
                ws_per_expert,
                raw_stream,
            );
        }
    }

    let qw_per_expert_i32 = (n_per * k_us) / 8;
    let qw_offset_bytes = expert_idx * qw_per_expert_i32 * std::mem::size_of::<i32>();

    let num_groups = k_us / weight.group_size as usize;
    let sc_per_expert_f16 = num_groups * n_per;
    let scales_offset_bytes = expert_idx * sc_per_expert_f16 * std::mem::size_of::<half::f16>();

    let in_offset_bytes = in_row_offset as usize * (k as usize) * std::mem::size_of::<half::f16>();
    let out_offset_bytes =
        out_row_offset as usize * (n as usize) * std::mem::size_of::<half::f16>();

    let (a_ptr, _a_guard) = input.device_ptr(stream);
    let (b_ptr_full, _b_guard) = weight.qweight.device_ptr(stream);
    let (c_ptr, _c_guard) = output.device_ptr(stream);
    let (s_ptr_full, _s_guard) = weight.scales.device_ptr(stream);
    let (ws_ptr_full, _ws_guard) = weight.workspace.device_ptr(stream);
    let a_ptr_off = a_ptr + in_offset_bytes as u64;
    let b_ptr = b_ptr_full + qw_offset_bytes as u64;
    let c_ptr_off = c_ptr + out_offset_bytes as u64;
    let s_ptr = s_ptr_full + scales_offset_bytes as u64;
    let ws_ptr = ws_ptr_full + ws_offset_bytes as u64;

    let ret = unsafe {
        marlin_cuda(
            a_ptr_off as *const _,
            b_ptr as *const _,
            c_ptr_off as *mut _,
            s_ptr as *const _,
            m,
            n,
            k,
            ws_ptr as *mut _,
            weight.group_size,
            0,
            raw_stream,
            -1,
            -1,
            -1,
            16,
            // Per-expert contiguous: stride == iteration.
            -1,
        )
    };
    if ret != 0 {
        return Err(candle_core::Error::Msg(format!(
            "marlin_cuda (offset_strided) failed ret={ret} m={m} n={n} k={k} \
             expert_offset={expert_offset} in_row_offset={in_row_offset} \
             out_row_offset={out_row_offset}"
        )));
    }
    Ok(())
}

#[cfg(not(feature = "marlin"))]
#[allow(clippy::too_many_arguments)]
pub fn marlin_gemm_with_offset_strided(
    _stream: &Arc<CudaStream>,
    _input: &CudaSlice<half::f16>,
    _in_row_offset: i32,
    _weight: &MarlinWeight,
    _output: &mut CudaSlice<half::f16>,
    _out_row_offset: i32,
    _m: i32,
    _expert_offset: i32,
    _expert_n: i32,
) -> candle_core::Result<()> {
    Err(candle_core::Error::Msg(
        "Marlin kernel not available (compile with --features marlin)".into(),
    ))
}

/// Stage 11 — fused MoE Marlin: ONE launch processes all experts in
/// `active_expert_ids` (len = `expert_count`) by reading `expert_id =
/// active_expert_ids[blockIdx.y]`, applying pointer offsets to the
/// stacked B / s / workspace, and reading per-expert (m, A_row_offset)
/// from the per-layer `tokens_per_expert` / `a_row_offsets` arrays.
///
/// `prob_m` is the bucket-wide max-m: every active expert MUST have
/// `tokens_per_expert[e] ≤ prob_m`, and `prob_m` MUST be a multiple of
/// 16. The kernel selects `thread_m_blocks = prob_m / 16` (1..=4); for
/// experts with fewer tokens the kernel pads with zeros.
///
/// Caller is responsible for:
///   - bucketing active experts by max-m (prob_m ∈ {16, 32, 48, 64})
///   - pre-zeroing the bucketed workspace slots (or relying on
///     `marlin_zero_stacked_workspace` having been called this iter)
///   - ensuring all active experts share the same `prob_n`, `prob_k`,
///     `group_size` (true for MoE — every expert in a layer has the
///     same shape)
#[cfg(feature = "marlin")]
#[allow(clippy::too_many_arguments)]
pub fn marlin_gemm_moe(
    stream: &Arc<CudaStream>,
    input: &CudaSlice<half::f16>,
    weight: &MarlinWeight,
    output: &mut CudaSlice<half::f16>,
    a_row_offsets: &CudaSlice<i32>,
    tokens_per_expert: &CudaSlice<i32>,
    active_expert_ids: Option<&CudaSlice<i32>>,
    expert_count: i32,
    prob_m: i32,
    n_per_expert: i32,
    num_experts_global: i32,
) -> candle_core::Result<()> {
    use cudarc::driver::DevicePtr;
    if expert_count <= 0 {
        return Ok(());
    }
    if prob_m <= 0 || prob_m > 64 || prob_m % 16 != 0 {
        return Err(candle_core::Error::Msg(format!(
            "marlin_gemm_moe: prob_m must be in {{16, 32, 48, 64}}, got {prob_m}"
        )));
    }
    let n = n_per_expert;
    let k = weight.k as i32;
    let n_per = n as usize;
    let k_us = k as usize;
    if n_per == 0 || (weight.n as i32) < num_experts_global * n {
        return Err(candle_core::Error::Msg(format!(
            "marlin_gemm_moe: stacked weight N={} too small for E_global={num_experts_global} × n_per={n}",
            weight.n
        )));
    }

    // Stacked-tile strides (int4 elems = 16 bytes each).
    // qweight per expert = (n_per * k) / 8 i32 = (n_per * k) / 32 int4
    // scales per expert  = (k/group_size * n_per) f16 = (...)/8 int4
    // workspace per expert = (n_per/128) * MAX_PAR i32
    const MAX_PAR: usize = 16;
    let b_int4_per_expert = ((n_per * k_us) / 32) as i32;
    let groups = k_us / weight.group_size as usize;
    let s_int4_per_expert = ((groups * n_per) / 8) as i32;
    let locks_i32_per_expert = (((n_per / 128).max(1)) * MAX_PAR) as i32;

    let raw_stream = stream.cu_stream();
    let (a_ptr, _ag) = input.device_ptr(stream);
    let (b_ptr, _bg) = weight.qweight.device_ptr(stream);
    let (c_ptr, _cg) = output.device_ptr(stream);
    let (s_ptr, _sg) = weight.scales.device_ptr(stream);
    let (ws_ptr, _wg) = weight.workspace.device_ptr(stream);
    let (off_ptr, _og) = a_row_offsets.device_ptr(stream);
    let (tok_ptr, _tg) = tokens_per_expert.device_ptr(stream);
    let act_ptr_opt = active_expert_ids.map(|s| s.device_ptr(stream));
    let act_raw: u64 = match &act_ptr_opt {
        Some((p, _)) => *p,
        None => 0,
    };

    let ret = unsafe {
        marlin_cuda_moe(
            a_ptr as *const _,
            b_ptr as *const _,
            c_ptr as *mut _,
            s_ptr as *const _,
            prob_m,
            n,
            k,
            ws_ptr as *mut _,
            off_ptr as *const _,
            tok_ptr as *const _,
            act_raw as *const _,
            expert_count,
            b_int4_per_expert,
            s_int4_per_expert,
            locks_i32_per_expert,
            weight.group_size,
            0, // dev
            raw_stream,
            -1,
            -1,
            -1,
            n, // prob_n_full = prob_n (per-expert contiguous stacking)
        )
    };

    if ret != 0 {
        return Err(candle_core::Error::Msg(format!(
            "marlin_cuda_moe failed: ret={ret} (prob_m={prob_m}, n={n}, k={k}, \
             experts={expert_count}, gs={})",
            weight.group_size
        )));
    }
    Ok(())
}

#[cfg(not(feature = "marlin"))]
#[allow(clippy::too_many_arguments)]
pub fn marlin_gemm_moe(
    _stream: &Arc<CudaStream>,
    _input: &CudaSlice<half::f16>,
    _weight: &MarlinWeight,
    _output: &mut CudaSlice<half::f16>,
    _a_row_offsets: &CudaSlice<i32>,
    _tokens_per_expert: &CudaSlice<i32>,
    _active_expert_ids: Option<&CudaSlice<i32>>,
    _expert_count: i32,
    _prob_m: i32,
    _n_per_expert: i32,
    _num_experts_global: i32,
) -> candle_core::Result<()> {
    Err(candle_core::Error::Msg(
        "Marlin kernel not available (compile with --features marlin)".into(),
    ))
}

// ===================== Stage 14: vLLM marlin_moe_wna16 port =====================

fn marlin_moe_ffi_status(ret: i32) -> (&'static str, u32) {
    let status =
        StagedNativeStatus::decode(ret).expect("Marlin-MoE status decoder requires a failure");
    let stage = match status.stage() {
        1 => "sm-count",
        2 => "max-shared-memory",
        3 => "act-order-launch",
        4 => "blocks-per-sm",
        5 => "function-attribute",
        6 => "kernel-launch",
        _ => "unknown",
    };
    (stage, u32::from(status.native_status()))
}

/// Raw, allocation-agnostic arguments for the vLLM Marlin-MoE launch.
///
/// The owning caller must retain every allocation until work enqueued on
/// `stream` has completed. Optional pointers deliberately retain their
/// corresponding mode flags so this boundary can reject inconsistent FFI
/// states before the native C++ implementation reaches `TORCH_CHECK`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum MarlinMoeF16WeightType {
    U4B8,
    E4M3,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct MarlinMoeRawLaunchArgs {
    pub(crate) weight_type: MarlinMoeF16WeightType,
    pub(crate) a: cudarc::driver::sys::CUdeviceptr,
    pub(crate) b: cudarc::driver::sys::CUdeviceptr,
    pub(crate) c: cudarc::driver::sys::CUdeviceptr,
    pub(crate) c_tmp: Option<cudarc::driver::sys::CUdeviceptr>,
    pub(crate) scales: cudarc::driver::sys::CUdeviceptr,
    pub(crate) zero_points: Option<cudarc::driver::sys::CUdeviceptr>,
    pub(crate) workspace: cudarc::driver::sys::CUdeviceptr,
    pub(crate) sorted_token_ids: cudarc::driver::sys::CUdeviceptr,
    pub(crate) expert_ids: cudarc::driver::sys::CUdeviceptr,
    pub(crate) num_tokens_past_padded: cudarc::driver::sys::CUdeviceptr,
    pub(crate) topk_weights: Option<cudarc::driver::sys::CUdeviceptr>,
    pub(crate) moe_block_size: i32,
    pub(crate) top_k: i32,
    pub(crate) mul_topk_weights: bool,
    pub(crate) is_ep: bool,
    pub(crate) prob_m: i32,
    pub(crate) prob_n: i32,
    pub(crate) prob_k: i32,
    pub(crate) group_size: i32,
    pub(crate) has_zero_points: bool,
    pub(crate) device_ordinal: i32,
    pub(crate) use_atomic_add: bool,
    pub(crate) use_fp32_reduce: bool,
}

impl MarlinMoeRawLaunchArgs {
    fn validate(&self) -> candle_core::Result<()> {
        validate_marlin_moe_pointer("a", self.a, 16)?;
        validate_marlin_moe_pointer("b", self.b, 16)?;
        validate_marlin_moe_pointer("c", self.c, 16)?;
        validate_marlin_moe_pointer("scales", self.scales, 16)?;
        validate_marlin_moe_pointer("workspace", self.workspace, 4)?;
        validate_marlin_moe_pointer("sorted_token_ids", self.sorted_token_ids, 4)?;
        validate_marlin_moe_pointer("expert_ids", self.expert_ids, 4)?;
        validate_marlin_moe_pointer("num_tokens_past_padded", self.num_tokens_past_padded, 4)?;
        if let Some(pointer) = self.c_tmp {
            validate_marlin_moe_pointer("c_tmp", pointer, 16)?;
        }
        if let Some(pointer) = self.zero_points {
            validate_marlin_moe_pointer("zero_points", pointer, 16)?;
        }
        if let Some(pointer) = self.topk_weights {
            validate_marlin_moe_pointer("topk_weights", pointer, 4)?;
        }

        if self.prob_m <= 0 || self.prob_n <= 0 || self.prob_k <= 0 {
            return Err(invalid_marlin_moe_args(format!(
                "prob_m, prob_n, and prob_k must be positive, got [{}, {}, {}]",
                self.prob_m, self.prob_n, self.prob_k
            )));
        }
        if !matches!(self.moe_block_size, 8 | 16 | 32 | 48 | 64) {
            return Err(invalid_marlin_moe_args(format!(
                "unsupported moe_block_size {}; expected one of 8, 16, 32, 48, 64",
                self.moe_block_size
            )));
        }
        if self.top_k <= 0 {
            return Err(invalid_marlin_moe_args(format!(
                "top_k must be positive, got {}",
                self.top_k
            )));
        }
        if self.prob_m.checked_mul(self.top_k).is_none() {
            return Err(invalid_marlin_moe_args(
                "prob_m * top_k overflows the kernel's i32 output-row domain",
            ));
        }
        if self.prob_n % 64 != 0 {
            return Err(invalid_marlin_moe_args(format!(
                "prob_n {} must be divisible by the Marlin minimum thread width 64",
                self.prob_n
            )));
        }
        if self.prob_k % 64 != 0 {
            return Err(invalid_marlin_moe_args(format!(
                "prob_k {} must be divisible by the Marlin minimum thread width 64",
                self.prob_k
            )));
        }
        match self.weight_type {
            MarlinMoeF16WeightType::U4B8 => {
                if self.group_size != -1 {
                    if self.group_size <= 0 || self.group_size % 16 != 0 {
                        return Err(invalid_marlin_moe_args(format!(
                            "group_size must be -1 or a positive multiple of 16, got {}",
                            self.group_size
                        )));
                    }
                    if self.prob_k % self.group_size != 0 {
                        return Err(invalid_marlin_moe_args(format!(
                            "prob_k {} must be divisible by group_size {}",
                            self.prob_k, self.group_size
                        )));
                    }
                }
            }
            MarlinMoeF16WeightType::E4M3 => {
                if !matches!(self.group_size, -1 | 128) {
                    return Err(invalid_marlin_moe_args(format!(
                        "E4M3 group_size must be -1 or 128, got {}",
                        self.group_size
                    )));
                }
                if self.group_size == 128 && self.prob_k % 128 != 0 {
                    return Err(invalid_marlin_moe_args(format!(
                        "E4M3 prob_k {} must be divisible by group_size 128",
                        self.prob_k
                    )));
                }
            }
        }
        if self.device_ordinal < 0 {
            return Err(invalid_marlin_moe_args(format!(
                "device_ordinal must be non-negative, got {}",
                self.device_ordinal
            )));
        }
        if self.has_zero_points != self.zero_points.is_some() {
            return Err(invalid_marlin_moe_args(
                "has_zero_points must exactly match the zero_points pointer",
            ));
        }
        if self.weight_type == MarlinMoeF16WeightType::E4M3
            && (self.has_zero_points || self.zero_points.is_some())
        {
            return Err(invalid_marlin_moe_args("E4M3 forbids zero points"));
        }
        if self.mul_topk_weights && self.topk_weights.is_none() {
            return Err(invalid_marlin_moe_args(
                "mul_topk_weights requires a non-null topk_weights pointer",
            ));
        }
        if self.use_atomic_add == self.use_fp32_reduce {
            return Err(invalid_marlin_moe_args(
                "exactly one of use_atomic_add and use_fp32_reduce must be enabled",
            ));
        }
        if self.use_fp32_reduce != self.c_tmp.is_some() {
            return Err(invalid_marlin_moe_args(
                "use_fp32_reduce must exactly match the c_tmp pointer",
            ));
        }
        Ok(())
    }
}

/// Native MXFP4 encoding accepted by the BF16 Marlin-MoE entrypoint.
///
/// The packed weight contains two E2M1 values per byte and uses one E8M0
/// scale byte for every 32 values along K. Keeping this separate from
/// `MarlinMoeF16WeightType` prevents the existing FP16 U4/E4M3 ABI from
/// accidentally selecting the BF16-only entrypoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum MarlinMoeMxfp4WeightType {
    E2M1E8M0,
}

/// Raw, allocation-agnostic arguments for BF16 x MXFP4 Marlin-MoE.
///
/// `a`, `c`, and `bias` must point to BF16 data. `b` is Marlin-packed E2M1
/// nibble data and `scales` is the corresponding group-32 E8M0 byte data.
/// The owning provider must retain every allocation until work enqueued on
/// `stream` has completed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct MarlinMoeMxfp4Bf16LaunchArgs {
    pub(crate) weight_type: MarlinMoeMxfp4WeightType,
    pub(crate) expert_count: i32,
    pub(crate) a: cudarc::driver::sys::CUdeviceptr,
    pub(crate) b: cudarc::driver::sys::CUdeviceptr,
    pub(crate) c: cudarc::driver::sys::CUdeviceptr,
    pub(crate) c_tmp: Option<cudarc::driver::sys::CUdeviceptr>,
    pub(crate) bias: cudarc::driver::sys::CUdeviceptr,
    pub(crate) scales: cudarc::driver::sys::CUdeviceptr,
    pub(crate) workspace: cudarc::driver::sys::CUdeviceptr,
    pub(crate) sorted_token_ids: cudarc::driver::sys::CUdeviceptr,
    pub(crate) expert_ids: cudarc::driver::sys::CUdeviceptr,
    pub(crate) num_tokens_past_padded: cudarc::driver::sys::CUdeviceptr,
    pub(crate) topk_weights: Option<cudarc::driver::sys::CUdeviceptr>,
    pub(crate) moe_block_size: i32,
    pub(crate) top_k: i32,
    pub(crate) mul_topk_weights: bool,
    pub(crate) is_ep: bool,
    pub(crate) prob_m: i32,
    pub(crate) prob_n: i32,
    pub(crate) prob_k: i32,
    pub(crate) group_size: i32,
    pub(crate) device_ordinal: i32,
    pub(crate) use_atomic_add: bool,
    pub(crate) use_fp32_reduce: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct MarlinMoeMxfp4Bf16RequiredBytes {
    a: u64,
    b: u64,
    c: u64,
    c_tmp: Option<u64>,
    bias: u64,
    scales: u64,
    topk_weights: Option<u64>,
}

impl MarlinMoeMxfp4Bf16LaunchArgs {
    fn validate(&self) -> candle_core::Result<MarlinMoeMxfp4Bf16RequiredBytes> {
        validate_marlin_moe_pointer("a", self.a, 16)?;
        validate_marlin_moe_pointer("b", self.b, 16)?;
        validate_marlin_moe_pointer("c", self.c, 16)?;
        validate_marlin_moe_pointer("bias", self.bias, 16)?;
        validate_marlin_moe_pointer("scales", self.scales, 16)?;
        validate_marlin_moe_pointer("workspace", self.workspace, 4)?;
        validate_marlin_moe_pointer("sorted_token_ids", self.sorted_token_ids, 4)?;
        validate_marlin_moe_pointer("expert_ids", self.expert_ids, 4)?;
        validate_marlin_moe_pointer("num_tokens_past_padded", self.num_tokens_past_padded, 4)?;
        if let Some(pointer) = self.c_tmp {
            validate_marlin_moe_pointer("c_tmp", pointer, 16)?;
        }
        if let Some(pointer) = self.topk_weights {
            validate_marlin_moe_pointer("topk_weights", pointer, 4)?;
        }

        if self.expert_count <= 0 {
            return Err(invalid_marlin_moe_args(format!(
                "MXFP4 expert_count must be positive, got {}",
                self.expert_count
            )));
        }
        if self.prob_m <= 0 || self.prob_n <= 0 || self.prob_k <= 0 {
            return Err(invalid_marlin_moe_args(format!(
                "MXFP4 BF16 prob_m, prob_n, and prob_k must be positive, got [{}, {}, {}]",
                self.prob_m, self.prob_n, self.prob_k
            )));
        }
        if !matches!(self.moe_block_size, 8 | 16 | 32 | 48 | 64) {
            return Err(invalid_marlin_moe_args(format!(
                "unsupported moe_block_size {}; expected one of 8, 16, 32, 48, 64",
                self.moe_block_size
            )));
        }
        if self.top_k <= 0 || self.top_k > self.expert_count {
            return Err(invalid_marlin_moe_args(format!(
                "MXFP4 top_k must be in 1..=expert_count, got top_k={} expert_count={}",
                self.top_k, self.expert_count
            )));
        }
        let output_rows = self.prob_m.checked_mul(self.top_k).ok_or_else(|| {
            invalid_marlin_moe_args(
                "MXFP4 prob_m * top_k overflows the kernel's i32 output-row domain",
            )
        })?;
        if self.prob_n % 64 != 0 {
            return Err(invalid_marlin_moe_args(format!(
                "MXFP4 prob_n {} must be divisible by the Marlin minimum thread width 64",
                self.prob_n
            )));
        }
        let expected_group_size = match self.weight_type {
            MarlinMoeMxfp4WeightType::E2M1E8M0 => 32,
        };
        if self.group_size != expected_group_size {
            return Err(invalid_marlin_moe_args(format!(
                "MXFP4 E2M1/E8M0 group_size must be exactly {expected_group_size}, got {}",
                self.group_size,
            )));
        }
        if self.prob_k % expected_group_size != 0 {
            return Err(invalid_marlin_moe_args(format!(
                "MXFP4 prob_k {} must be divisible by group_size {expected_group_size}",
                self.prob_k,
            )));
        }
        if self.device_ordinal < 0 {
            return Err(invalid_marlin_moe_args(format!(
                "device_ordinal must be non-negative, got {}",
                self.device_ordinal
            )));
        }
        if self.mul_topk_weights && self.topk_weights.is_none() {
            return Err(invalid_marlin_moe_args(
                "mul_topk_weights requires a non-null topk_weights pointer",
            ));
        }
        if self.use_atomic_add == self.use_fp32_reduce {
            return Err(invalid_marlin_moe_args(
                "exactly one of use_atomic_add and use_fp32_reduce must be enabled",
            ));
        }
        if self.use_fp32_reduce != self.c_tmp.is_some() {
            return Err(invalid_marlin_moe_args(
                "use_fp32_reduce must exactly match the c_tmp pointer",
            ));
        }

        let experts = self.expert_count as u64;
        let m = self.prob_m as u64;
        let n = self.prob_n as u64;
        let k = self.prob_k as u64;
        let output_rows = output_rows as u64;
        let required = MarlinMoeMxfp4Bf16RequiredBytes {
            a: checked_marlin_moe_bytes("MXFP4 BF16 input [M,K]", &[m, k, 2])?,
            b: checked_marlin_moe_bytes(
                "MXFP4 E2M1 packed weight [E,N,K/2]",
                &[experts, n, k / 2],
            )?,
            c: checked_marlin_moe_bytes("MXFP4 BF16 output [M*top_k,N]", &[output_rows, n, 2])?,
            c_tmp: self
                .c_tmp
                .map(|_| {
                    checked_marlin_moe_bytes(
                        "MXFP4 FP32 reduction scratch [M*top_k,N]",
                        &[output_rows, n, 4],
                    )
                })
                .transpose()?,
            bias: checked_marlin_moe_bytes("MXFP4 BF16 bias [E,N]", &[experts, n, 2])?,
            scales: checked_marlin_moe_bytes(
                "MXFP4 E8M0 scales [E,K/32,N]",
                &[experts, k / 32, n],
            )?,
            topk_weights: self
                .topk_weights
                .map(|_| {
                    checked_marlin_moe_bytes("MXFP4 top-k weights [M,top_k]", &[output_rows, 4])
                })
                .transpose()?,
        };

        validate_marlin_moe_span("a", self.a, required.a)?;
        validate_marlin_moe_span("b", self.b, required.b)?;
        validate_marlin_moe_span("c", self.c, required.c)?;
        validate_marlin_moe_span("bias", self.bias, required.bias)?;
        validate_marlin_moe_span("scales", self.scales, required.scales)?;
        if let (Some(pointer), Some(bytes)) = (self.c_tmp, required.c_tmp) {
            validate_marlin_moe_span("c_tmp", pointer, bytes)?;
        }
        if let (Some(pointer), Some(bytes)) = (self.topk_weights, required.topk_weights) {
            validate_marlin_moe_span("topk_weights", pointer, bytes)?;
        }
        Ok(required)
    }
}

fn checked_marlin_moe_bytes(label: &str, factors: &[u64]) -> candle_core::Result<u64> {
    factors.iter().try_fold(1_u64, |bytes, factor| {
        bytes.checked_mul(*factor).ok_or_else(|| {
            invalid_marlin_moe_args(format!("{label} byte size overflows the u64 device domain"))
        })
    })
}

fn validate_marlin_moe_span(
    name: &str,
    pointer: cudarc::driver::sys::CUdeviceptr,
    bytes: u64,
) -> candle_core::Result<()> {
    debug_assert!(bytes > 0);
    if pointer.checked_add(bytes - 1).is_none() {
        return Err(invalid_marlin_moe_args(format!(
            "{name} pointer range overflows the u64 device domain"
        )));
    }
    Ok(())
}

fn validate_marlin_moe_pointer(
    name: &str,
    pointer: cudarc::driver::sys::CUdeviceptr,
    alignment: u64,
) -> candle_core::Result<()> {
    if pointer == 0 {
        return Err(invalid_marlin_moe_args(format!(
            "{name} pointer must be non-null"
        )));
    }
    if pointer % alignment != 0 {
        return Err(invalid_marlin_moe_args(format!(
            "{name} pointer 0x{pointer:x} must be aligned to {alignment} bytes"
        )));
    }
    Ok(())
}

fn invalid_marlin_moe_args(reason: impl std::fmt::Display) -> candle_core::Error {
    candle_core::Error::Msg(format!("invalid vLLM Marlin-MoE launch: {reason}"))
}

#[cfg(feature = "vllm-moe-marlin")]
pub(crate) fn launch_marlin_moe_vllm_raw(
    stream: &CudaStream,
    args: MarlinMoeRawLaunchArgs,
) -> candle_core::Result<()> {
    args.validate()?;
    let (entrypoint, ret) = unsafe {
        match args.weight_type {
            MarlinMoeF16WeightType::U4B8 => (
                "ferrum_vllm_marlin_moe_f16",
                ferrum_vllm_marlin_moe_f16(
                    args.a as *const _,
                    args.b as *const _,
                    args.c as *mut _,
                    args.c_tmp.unwrap_or_default() as *mut _,
                    args.scales as *const _,
                    args.zero_points.unwrap_or_default() as *const _,
                    args.workspace as *mut _,
                    args.sorted_token_ids as *const i32,
                    args.expert_ids as *const i32,
                    args.num_tokens_past_padded as *const i32,
                    args.topk_weights.unwrap_or_default() as *const f32,
                    args.moe_block_size,
                    args.top_k,
                    i32::from(args.mul_topk_weights),
                    i32::from(args.is_ep),
                    args.prob_m,
                    args.prob_n,
                    args.prob_k,
                    args.group_size,
                    i32::from(args.has_zero_points),
                    args.device_ordinal,
                    stream.cu_stream(),
                    i32::from(args.use_atomic_add),
                    i32::from(args.use_fp32_reduce),
                ),
            ),
            MarlinMoeF16WeightType::E4M3 => (
                "ferrum_vllm_marlin_moe_fp8_f16",
                ferrum_vllm_marlin_moe_fp8_f16(
                    args.a as *const _,
                    args.b as *const _,
                    args.c as *mut _,
                    args.c_tmp.unwrap_or_default() as *mut _,
                    args.scales as *const _,
                    std::ptr::null(),
                    args.workspace as *mut _,
                    args.sorted_token_ids as *const i32,
                    args.expert_ids as *const i32,
                    args.num_tokens_past_padded as *const i32,
                    args.topk_weights.unwrap_or_default() as *const f32,
                    args.moe_block_size,
                    args.top_k,
                    i32::from(args.mul_topk_weights),
                    i32::from(args.is_ep),
                    args.prob_m,
                    args.prob_n,
                    args.prob_k,
                    args.group_size,
                    0,
                    args.device_ordinal,
                    stream.cu_stream(),
                    i32::from(args.use_atomic_add),
                    i32::from(args.use_fp32_reduce),
                ),
            ),
        }
    };
    if ret != 0 {
        let (stage, cuda_status) = marlin_moe_ffi_status(ret);
        return Err(candle_core::Error::Msg(format!(
            "{entrypoint} failed at {stage}: \
             cuda_status={cuda_status}, ret={ret} (m={}, n={}, k={})",
            args.prob_m, args.prob_n, args.prob_k
        )));
    }
    Ok(())
}

/// Validate and enqueue the BF16 x MXFP4 Marlin-MoE native entrypoint.
#[cfg(feature = "vllm-moe-marlin")]
pub(crate) fn launch_marlin_moe_mxfp4_bf16(
    stream: &CudaStream,
    args: MarlinMoeMxfp4Bf16LaunchArgs,
) -> candle_core::Result<()> {
    let _required = args.validate()?;
    let ret = unsafe {
        ferrum_vllm_marlin_moe_mxfp4_bf16(
            args.a as *const _,
            args.b as *const _,
            args.c as *mut _,
            args.c_tmp.unwrap_or_default() as *mut _,
            args.bias as *const _,
            args.scales as *const _,
            args.workspace as *mut _,
            args.sorted_token_ids as *const i32,
            args.expert_ids as *const i32,
            args.num_tokens_past_padded as *const i32,
            args.topk_weights.unwrap_or_default() as *const f32,
            args.moe_block_size,
            args.top_k,
            i32::from(args.mul_topk_weights),
            i32::from(args.is_ep),
            args.prob_m,
            args.prob_n,
            args.prob_k,
            args.group_size,
            args.device_ordinal,
            stream.cu_stream(),
            i32::from(args.use_atomic_add),
            i32::from(args.use_fp32_reduce),
        )
    };
    if ret != 0 {
        let (stage, cuda_status) = marlin_moe_ffi_status(ret);
        return Err(candle_core::Error::Msg(format!(
            "ferrum_vllm_marlin_moe_mxfp4_bf16 failed at {stage}: \
             cuda_status={cuda_status}, ret={ret} (m={}, n={}, k={}, experts={})",
            args.prob_m, args.prob_n, args.prob_k, args.expert_count
        )));
    }
    Ok(())
}

#[cfg(not(feature = "vllm-moe-marlin"))]
pub(crate) fn launch_marlin_moe_mxfp4_bf16(
    _stream: &CudaStream,
    _args: MarlinMoeMxfp4Bf16LaunchArgs,
) -> candle_core::Result<()> {
    Err(candle_core::Error::Msg(
        "vLLM MXFP4 BF16 Marlin-MoE not built — compile with --features vllm-moe-marlin".into(),
    ))
}

#[cfg(not(feature = "vllm-moe-marlin"))]
pub(crate) fn launch_marlin_moe_vllm_raw(
    _stream: &CudaStream,
    _args: MarlinMoeRawLaunchArgs,
) -> candle_core::Result<()> {
    Err(candle_core::Error::Msg(
        "vLLM marlin_moe_wna16 not built — compile with --features vllm-moe-marlin".into(),
    ))
}

/// Stage 14 - fused MoE Marlin via the vLLM marlin_moe_wna16 native artifact
/// kernel. Replaces our Stage 12.1 bucketed `marlin_gemm_moe` with a
/// single launch that processes ALL `(token, expert)` pairs of a layer
/// in one go using vLLM's `(sorted_token_ids, expert_ids)` indirection.
///
/// vLLM's design eliminates the m=16 padding waste of our Stage 12.1
/// path: each output tile reads its expert id from the per-tile
/// `expert_ids[block_idx]` array, gathers its 16 input rows via
/// `sorted_token_ids[block_idx*moe_block_size .. ]`, and accumulates
/// directly. Inactive (sentinel) rows are masked out without compute.
///
/// Caller must:
/// - Run `B::moe_align_block_size` first to build sorted_token_ids,
///   expert_ids, num_tokens_past_padded.
/// - Allocate output `c[size_m * top_k, size_n]` fp16.
/// - Provide a stacked Marlin-packed weight tile (the same one our
///   per-expert `marlin_gemm_with_offset` consumes).
/// - Pre-zero the workspace (or rely on `marlin_zero_stacked_workspace`).
///
/// `prob_m = size_m` (number of original input tokens), `prob_n` =
/// per-expert n, `prob_k` = k. Inputs are flat across all experts; the
/// kernel routes per-tile via expert_ids.
///
/// Only available with `--features vllm-moe-marlin`.
#[cfg(feature = "vllm-moe-marlin")]
#[allow(clippy::too_many_arguments)]
pub fn marlin_gemm_moe_vllm(
    stream: &Arc<CudaStream>,
    input: &CudaSlice<half::f16>,
    weight: &MarlinWeight,
    output: &mut CudaSlice<half::f16>,
    c_tmp: Option<&mut CudaSlice<f32>>,
    sorted_token_ids: &CudaSlice<i32>,
    expert_ids: &CudaSlice<i32>,
    num_tokens_past_padded: &CudaSlice<i32>,
    topk_weights: Option<&CudaSlice<f32>>,
    moe_block_size: i32,
    top_k: i32,
    mul_topk_weights: bool,
    is_ep: bool,
    prob_m: i32,
    prob_n: i32,
    prob_k: i32,
) -> candle_core::Result<()> {
    use cudarc::driver::DevicePtr;
    let raw_stream = stream.cu_stream();
    let profile = profile_marlin();
    let profile_bucket = profile.then(current_marlin_profile_bucket);

    let (a_ptr, _ag) = input.device_ptr(stream);
    let (b_ptr, _bg) = weight.qweight.device_ptr(stream);
    let (c_ptr, _cg) = output.device_ptr(stream);
    let (s_ptr, _sg) = weight.scales.device_ptr(stream);
    let z_ptr = match weight.qzeros.as_ref() {
        Some(z) => Some(z.device_ptr(stream).0),
        None => None,
    };
    let (ws_ptr, _wg) = weight.workspace.device_ptr(stream);
    let (st_ptr, _stg) = sorted_token_ids.device_ptr(stream);
    let (eid_ptr, _eidg) = expert_ids.device_ptr(stream);
    let (npp_ptr, _nppg) = num_tokens_past_padded.device_ptr(stream);

    let c_tmp_ptr = match c_tmp.as_ref() {
        Some(c) => Some(c.device_ptr(stream).0),
        None => None,
    };
    let topk_w_ptr = match topk_weights {
        Some(w) => Some(w.device_ptr(stream).0),
        None => None,
    };

    let timer = profile
        .then(|| CudaMarlinEventTimer::start(raw_stream))
        .flatten();
    let result = launch_marlin_moe_vllm_raw(
        stream,
        MarlinMoeRawLaunchArgs {
            weight_type: MarlinMoeF16WeightType::U4B8,
            a: a_ptr,
            b: b_ptr,
            c: c_ptr,
            c_tmp: c_tmp_ptr,
            scales: s_ptr,
            zero_points: z_ptr,
            workspace: ws_ptr,
            sorted_token_ids: st_ptr,
            expert_ids: eid_ptr,
            num_tokens_past_padded: npp_ptr,
            topk_weights: topk_w_ptr,
            moe_block_size,
            top_k,
            mul_topk_weights,
            is_ep,
            prob_m,
            prob_n,
            prob_k,
            group_size: weight.group_size,
            has_zero_points: weight.qzeros.is_some(),
            device_ordinal: 0,
            use_atomic_add: c_tmp_ptr.is_none(),
            use_fp32_reduce: c_tmp_ptr.is_some(),
        },
    );
    if let Some(timer) = timer {
        let elapsed_us = timer.finish_us(raw_stream);
        MARLIN_KERNEL_TIME_US.fetch_add(elapsed_us, Ordering::Relaxed);
        MARLIN_KERNEL_CALLS.fetch_add(1, Ordering::Relaxed);
        if let Some(bucket) = profile_bucket {
            record_marlin_kernel(bucket, elapsed_us);
        }
    }
    result
}

#[cfg(not(feature = "vllm-moe-marlin"))]
#[allow(clippy::too_many_arguments)]
pub fn marlin_gemm_moe_vllm(
    _stream: &Arc<CudaStream>,
    _input: &CudaSlice<half::f16>,
    _weight: &MarlinWeight,
    _output: &mut CudaSlice<half::f16>,
    _c_tmp: Option<&mut CudaSlice<f32>>,
    _sorted_token_ids: &CudaSlice<i32>,
    _expert_ids: &CudaSlice<i32>,
    _num_tokens_past_padded: &CudaSlice<i32>,
    _topk_weights: Option<&CudaSlice<f32>>,
    _moe_block_size: i32,
    _top_k: i32,
    _mul_topk_weights: bool,
    _is_ep: bool,
    _prob_m: i32,
    _prob_n: i32,
    _prob_k: i32,
) -> candle_core::Result<()> {
    Err(candle_core::Error::Msg(
        "vLLM marlin_moe_wna16 not built — compile with --features vllm-moe-marlin".into(),
    ))
}

pub use crate::marlin_repack::{
    permute_gptq_qweight_rows, repack_gptq_to_marlin, repack_scales_to_marlin,
};

#[cfg(test)]
mod tests {
    #[cfg(feature = "vllm-moe-marlin")]
    use super::{
        configure_vllm_moe_profile_sink, launch_marlin_moe_mxfp4_bf16, launch_marlin_moe_vllm_raw,
    };
    use super::{
        marlin_moe_ffi_status, marlin_profile_bucket_from_label, should_zero_workspace,
        CudaMarlinRuntimeConfig, MarlinMoeF16WeightType, MarlinMoeMxfp4Bf16LaunchArgs,
        MarlinMoeMxfp4WeightType, MarlinMoeRawLaunchArgs, MarlinProfileBucket,
        MarlinProfileBucketStats,
    };
    #[cfg(feature = "vllm-moe-marlin")]
    use crate::marlin_repack::{prepare_block_fp8_weight_for_fp8_marlin, repack_gptq_to_marlin};
    #[cfg(feature = "vllm-moe-marlin")]
    use crate::mxfp4_marlin_materializer::{
        permute_mxfp4_marlin_bias_bf16, prepare_mxfp4_expert_scales_for_marlin,
        transpose_mxfp4_expert_blocks_to_gptq_words,
    };
    #[cfg(feature = "vllm-moe-marlin")]
    use cudarc::driver::sys::CUdevice_attribute::CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT;
    #[cfg(feature = "vllm-moe-marlin")]
    use cudarc::driver::{
        CudaContext, CudaSlice, CudaStream, DevicePtr, DevicePtrMut, LaunchConfig, PushKernelArg,
    };
    #[cfg(feature = "vllm-moe-marlin")]
    use cudarc::nvrtc::Ptx;
    #[cfg(feature = "vllm-moe-marlin")]
    use ferrum_bench_core::{ProfileMetadata, ProfileSinkConfig};
    #[cfg(feature = "vllm-moe-marlin")]
    use half::{bf16, f16};
    #[cfg(feature = "vllm-moe-marlin")]
    use sha2::{Digest, Sha256};
    #[cfg(feature = "vllm-moe-marlin")]
    use std::sync::Arc;

    fn valid_marlin_moe_raw_args() -> MarlinMoeRawLaunchArgs {
        MarlinMoeRawLaunchArgs {
            weight_type: MarlinMoeF16WeightType::U4B8,
            a: 0x1000,
            b: 0x2000,
            c: 0x3000,
            c_tmp: None,
            scales: 0x4000,
            zero_points: None,
            workspace: 0x5000,
            sorted_token_ids: 0x6000,
            expert_ids: 0x7000,
            num_tokens_past_padded: 0x8000,
            topk_weights: None,
            moe_block_size: 16,
            top_k: 8,
            mul_topk_weights: false,
            is_ep: false,
            prob_m: 4,
            prob_n: 1024,
            prob_k: 2048,
            group_size: 128,
            has_zero_points: false,
            device_ordinal: 0,
            use_atomic_add: true,
            use_fp32_reduce: false,
        }
    }

    #[cfg(feature = "vllm-moe-marlin")]
    fn prepare_mxfp4_marlin_bias(bias: &[bf16], expert_count: usize, n: usize) -> Vec<bf16> {
        let source_bytes = bias
            .iter()
            .flat_map(|value| value.to_bits().to_le_bytes())
            .collect::<Vec<_>>();
        let prepared = permute_mxfp4_marlin_bias_bf16(&source_bytes, expert_count, n)
            .expect("prepare MXFP4 Marlin P32 bias");
        assert_eq!(prepared.len(), source_bytes.len());
        prepared
            .chunks_exact(2)
            .map(|bytes| bf16::from_bits(u16::from_le_bytes([bytes[0], bytes[1]])))
            .collect()
    }

    #[cfg(feature = "vllm-moe-marlin")]
    fn assert_gpt_oss_mxfp4_two_expert_source_reference(
        context: &Arc<CudaContext>,
        stream: &Arc<CudaStream>,
        input_device: &CudaSlice<bf16>,
        input: &[bf16],
        n: usize,
        logical_k: usize,
        physical_k: usize,
    ) -> f64 {
        const EXPERTS: usize = 2;
        const ROWS: usize = 4;
        const MOE_BLOCK_SIZE: usize = 16;
        const E2M1: [f32; 8] = [0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0];

        assert_eq!(input.len(), ROWS * physical_k);
        let packed_bytes = n * physical_k / 2;
        let scale_bytes = n * (physical_k / 32);
        let logical_groups = logical_k / 32;
        let physical_groups = physical_k / 32;
        let mut blocks = vec![0_u8; EXPERTS * packed_bytes];
        let mut scales = vec![0_u8; EXPERTS * scale_bytes];
        let mut bias = vec![bf16::ZERO; EXPERTS * n];

        for expert in 0..EXPERTS {
            for output in 0..n {
                bias[expert * n + output] = bf16::from_f32(
                    (i32::try_from((expert * 5 + output) % 13).unwrap() - 6) as f32 / 64.0,
                );
                for group in 0..logical_groups {
                    scales[expert * scale_bytes + output * physical_groups + group] =
                        125 + ((expert * 3 + output + group * 2) % 5) as u8;
                    let feature = group * 32 + (expert * 11 + output * 3 + group * 5) % 32;
                    let mut nibble = 1 + ((expert + output + group) % 7) as u8;
                    if (expert + output + group).is_multiple_of(3) {
                        nibble |= 0x08;
                    }
                    let byte = &mut blocks
                        [expert * packed_bytes + output * (physical_k / 2) + feature / 2];
                    if feature.is_multiple_of(2) {
                        *byte |= nibble;
                    } else {
                        *byte |= nibble << 4;
                    }
                }
            }
        }
        assert!(scales.chunks_exact(physical_groups).all(|row| {
            row[..logical_groups]
                .windows(2)
                .any(|pair| pair[0] != pair[1])
                && row[logical_groups..].iter().all(|scale| *scale == 0)
        }));

        let mut reference = vec![0.0_f32; ROWS * n];
        for row in 0..ROWS {
            let expert = row % EXPERTS;
            for output in 0..n {
                let mut sum = bias[expert * n + output].to_f32();
                for group in 0..logical_groups {
                    let feature = group * 32 + (expert * 11 + output * 3 + group * 5) % 32;
                    let packed =
                        blocks[expert * packed_bytes + output * (physical_k / 2) + feature / 2];
                    let nibble = if feature.is_multiple_of(2) {
                        packed & 0x0f
                    } else {
                        packed >> 4
                    };
                    let mut weight = E2M1[usize::from(nibble & 0x07)];
                    if nibble & 0x08 != 0 {
                        weight = -weight;
                    }
                    let exponent =
                        i32::from(scales[expert * scale_bytes + output * physical_groups + group])
                            - 127;
                    sum += input[row * physical_k + feature].to_f32()
                        * weight
                        * 2.0_f32.powi(exponent);
                }
                reference[row * n + output] = sum;
            }
        }

        let mut packed_weight = Vec::with_capacity(EXPERTS * packed_bytes / 4);
        let mut packed_scales = Vec::with_capacity(EXPERTS * scale_bytes);
        for expert in 0..EXPERTS {
            let raw = &blocks[expert * packed_bytes..(expert + 1) * packed_bytes];
            let words = transpose_mxfp4_expert_blocks_to_gptq_words(raw, n, physical_k)
                .expect("transpose source MXFP4 expert");
            packed_weight.extend(repack_gptq_to_marlin(&words, physical_k, n));
            packed_scales.extend(
                prepare_mxfp4_expert_scales_for_marlin(
                    &scales[expert * scale_bytes..(expert + 1) * scale_bytes],
                    n,
                    physical_k,
                )
                .expect("prepare source E8M0 expert scales"),
            );
        }
        assert_eq!(packed_weight.len() * 4, EXPERTS * packed_bytes);
        assert_eq!(packed_scales.len(), EXPERTS * scale_bytes);

        let weight_device: CudaSlice<i32> = stream.clone_htod(&packed_weight).unwrap();
        let scales_device: CudaSlice<u8> = stream.clone_htod(&packed_scales).unwrap();
        let prepared_bias = prepare_mxfp4_marlin_bias(&bias, EXPERTS, n);
        let bias_device: CudaSlice<bf16> = stream.clone_htod(&prepared_bias).unwrap();
        let mut output_device: CudaSlice<bf16> = stream.alloc_zeros(ROWS * n).unwrap();
        let sms = usize::try_from(
            context
                .attribute(CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT)
                .unwrap(),
        )
        .unwrap();
        let mut reduce_device: CudaSlice<f32> =
            stream.alloc_zeros(sms * 4 * MOE_BLOCK_SIZE * 256).unwrap();
        let mut sorted = vec![ROWS as i32; EXPERTS * MOE_BLOCK_SIZE];
        sorted[0] = 0;
        sorted[1] = 2;
        sorted[MOE_BLOCK_SIZE] = 1;
        sorted[MOE_BLOCK_SIZE + 1] = 3;
        let sorted_device: CudaSlice<i32> = stream.clone_htod(&sorted).unwrap();
        let expert_device: CudaSlice<i32> = stream.clone_htod(&[0, 1]).unwrap();
        let padded_device: CudaSlice<i32> = stream
            .clone_htod(&[(EXPERTS * MOE_BLOCK_SIZE) as i32])
            .unwrap();
        let workspace: CudaSlice<i32> = stream.alloc_zeros(n.div_ceil(128) * sms * 4).unwrap();

        {
            let (a, _a_guard) = input_device.device_ptr(stream);
            let (b, _b_guard) = weight_device.device_ptr(stream);
            let (c, _c_guard) = output_device.device_ptr_mut(stream);
            let (c_tmp, _c_tmp_guard) = reduce_device.device_ptr_mut(stream);
            let (bias, _bias_guard) = bias_device.device_ptr(stream);
            let (scales, _scales_guard) = scales_device.device_ptr(stream);
            let (workspace, _workspace_guard) = workspace.device_ptr(stream);
            let (sorted, _sorted_guard) = sorted_device.device_ptr(stream);
            let (experts, _experts_guard) = expert_device.device_ptr(stream);
            let (padded, _padded_guard) = padded_device.device_ptr(stream);
            launch_marlin_moe_mxfp4_bf16(
                stream,
                MarlinMoeMxfp4Bf16LaunchArgs {
                    weight_type: MarlinMoeMxfp4WeightType::E2M1E8M0,
                    expert_count: EXPERTS as i32,
                    a,
                    b,
                    c,
                    c_tmp: Some(c_tmp),
                    bias,
                    scales,
                    workspace,
                    sorted_token_ids: sorted,
                    expert_ids: experts,
                    num_tokens_past_padded: padded,
                    topk_weights: None,
                    moe_block_size: MOE_BLOCK_SIZE as i32,
                    top_k: 1,
                    mul_topk_weights: false,
                    is_ep: false,
                    prob_m: ROWS as i32,
                    prob_n: n as i32,
                    prob_k: physical_k as i32,
                    group_size: 32,
                    device_ordinal: 0,
                    use_atomic_add: false,
                    use_fp32_reduce: true,
                },
            )
            .unwrap();
            stream.synchronize().unwrap();
        }

        let actual = stream.clone_dtoh(&output_device).unwrap();
        let mut maximum_relative_l2 = 0.0_f64;
        for row in 0..ROWS {
            let expected = &reference[row * n..(row + 1) * n];
            let observed = &actual[row * n..(row + 1) * n];
            let reference_l2 = expected
                .iter()
                .map(|value| f64::from(*value).powi(2))
                .sum::<f64>();
            let error_l2 = observed
                .iter()
                .zip(expected)
                .map(|(actual, expected)| f64::from(actual.to_f32() - expected).powi(2))
                .sum::<f64>();
            assert!(observed.iter().all(|value| value.to_f32().is_finite()));
            let relative_l2 = error_l2.sqrt() / reference_l2.sqrt().max(1.0e-6);
            assert!(relative_l2 <= 0.05, "row={row} relL2={relative_l2:.8}");
            maximum_relative_l2 = maximum_relative_l2.max(relative_l2);
        }
        maximum_relative_l2
    }

    fn valid_marlin_moe_mxfp4_bf16_args() -> MarlinMoeMxfp4Bf16LaunchArgs {
        MarlinMoeMxfp4Bf16LaunchArgs {
            weight_type: MarlinMoeMxfp4WeightType::E2M1E8M0,
            expert_count: 4,
            a: 0x1000,
            b: 0x2000,
            c: 0x3000,
            c_tmp: None,
            bias: 0x4000,
            scales: 0x5000,
            workspace: 0x6000,
            sorted_token_ids: 0x7000,
            expert_ids: 0x8000,
            num_tokens_past_padded: 0x9000,
            topk_weights: None,
            moe_block_size: 16,
            top_k: 2,
            mul_topk_weights: false,
            is_ep: false,
            prob_m: 4,
            prob_n: 512,
            prob_k: 256,
            group_size: 32,
            device_ordinal: 0,
            use_atomic_add: true,
            use_fp32_reduce: false,
        }
    }

    fn assert_invalid_marlin_moe_mxfp4_bf16_args(
        args: MarlinMoeMxfp4Bf16LaunchArgs,
        expected: &str,
    ) {
        let error = args.validate().expect_err("launch arguments must fail");
        assert!(
            error.to_string().contains(expected),
            "expected error containing {expected:?}, got {error}"
        );
    }

    #[test]
    fn marlin_moe_mxfp4_bf16_args_compute_required_buffer_shapes() {
        let required = valid_marlin_moe_mxfp4_bf16_args().validate().unwrap();
        assert_eq!(required.a, 2_048);
        assert_eq!(required.b, 262_144);
        assert_eq!(required.c, 8_192);
        assert_eq!(required.c_tmp, None);
        assert_eq!(required.bias, 4_096);
        assert_eq!(required.scales, 16_384);
        assert_eq!(required.topk_weights, None);

        let mut fp32_reduce = valid_marlin_moe_mxfp4_bf16_args();
        fp32_reduce.c_tmp = Some(0xa000);
        fp32_reduce.topk_weights = Some(0xb000);
        fp32_reduce.mul_topk_weights = true;
        fp32_reduce.use_atomic_add = false;
        fp32_reduce.use_fp32_reduce = true;
        let required = fp32_reduce.validate().unwrap();
        assert_eq!(required.c_tmp, Some(16_384));
        assert_eq!(required.topk_weights, Some(32));
    }

    #[test]
    fn marlin_moe_mxfp4_bf16_args_reject_invalid_pointers() {
        let mut args = valid_marlin_moe_mxfp4_bf16_args();
        args.bias = 0;
        assert_invalid_marlin_moe_mxfp4_bf16_args(args, "bias pointer must be non-null");

        let mut args = valid_marlin_moe_mxfp4_bf16_args();
        args.scales += 2;
        assert_invalid_marlin_moe_mxfp4_bf16_args(args, "scales pointer");

        let mut args = valid_marlin_moe_mxfp4_bf16_args();
        args.b = u64::MAX - 15;
        assert_invalid_marlin_moe_mxfp4_bf16_args(args, "b pointer range overflows");
    }

    #[test]
    fn marlin_moe_mxfp4_bf16_args_reject_invalid_shapes_and_overflow() {
        let mut args = valid_marlin_moe_mxfp4_bf16_args();
        args.group_size = 128;
        assert_invalid_marlin_moe_mxfp4_bf16_args(args, "group_size must be exactly 32");

        let mut args = valid_marlin_moe_mxfp4_bf16_args();
        args.prob_k = 240;
        assert_invalid_marlin_moe_mxfp4_bf16_args(args, "must be divisible by group_size 32");

        let mut args = valid_marlin_moe_mxfp4_bf16_args();
        args.prob_n = 96;
        assert_invalid_marlin_moe_mxfp4_bf16_args(args, "must be divisible");

        let mut args = valid_marlin_moe_mxfp4_bf16_args();
        args.top_k = 5;
        assert_invalid_marlin_moe_mxfp4_bf16_args(args, "1..=expert_count");

        let mut args = valid_marlin_moe_mxfp4_bf16_args();
        args.prob_m = i32::MAX;
        assert_invalid_marlin_moe_mxfp4_bf16_args(args, "prob_m * top_k overflows");

        let mut args = valid_marlin_moe_mxfp4_bf16_args();
        args.expert_count = i32::MAX;
        args.top_k = 1;
        args.prob_m = 1;
        args.prob_n = i32::MAX - i32::MAX.rem_euclid(64);
        args.prob_k = i32::MAX - i32::MAX.rem_euclid(32);
        assert_invalid_marlin_moe_mxfp4_bf16_args(args, "packed weight");
    }

    #[cfg(feature = "vllm-moe-marlin")]
    #[test]
    fn marlin_moe_mxfp4_bf16_extern_abi_matches_locked_export() {
        type ExpectedAbi = unsafe extern "C" fn(
            *const std::ffi::c_void,
            *const std::ffi::c_void,
            *mut std::ffi::c_void,
            *mut std::ffi::c_void,
            *const std::ffi::c_void,
            *const std::ffi::c_void,
            *mut std::ffi::c_void,
            *const i32,
            *const i32,
            *const i32,
            *const f32,
            i32,
            i32,
            i32,
            i32,
            i32,
            i32,
            i32,
            i32,
            i32,
            cudarc::driver::sys::CUstream,
            i32,
            i32,
        ) -> i32;

        let _: ExpectedAbi = super::ferrum_vllm_marlin_moe_mxfp4_bf16;
    }

    #[test]
    fn marlin_moe_ffi_status_preserves_failure_stage_and_cuda_status() {
        assert_eq!(marlin_moe_ffi_status((1 << 16) | 10), ("sm-count", 10));
        assert_eq!(
            marlin_moe_ffi_status((5 << 16) | 9),
            ("function-attribute", 9)
        );
        assert_eq!(
            marlin_moe_ffi_status((6 << 16) | 701),
            ("kernel-launch", 701)
        );
        assert_eq!(marlin_moe_ffi_status(17), ("unknown", 17));
    }

    fn assert_invalid_marlin_moe_args(args: MarlinMoeRawLaunchArgs, expected: &str) {
        let error = args.validate().expect_err("launch arguments must fail");
        assert!(
            error.to_string().contains(expected),
            "expected error containing {expected:?}, got {error}"
        );
    }

    #[test]
    fn marlin_moe_raw_args_accept_supported_modes() {
        valid_marlin_moe_raw_args().validate().unwrap();

        let mut fp32_reduce = valid_marlin_moe_raw_args();
        fp32_reduce.c_tmp = Some(0x9000);
        fp32_reduce.zero_points = Some(0xa000);
        fp32_reduce.topk_weights = Some(0xb000);
        fp32_reduce.has_zero_points = true;
        fp32_reduce.mul_topk_weights = true;
        fp32_reduce.use_atomic_add = false;
        fp32_reduce.use_fp32_reduce = true;
        fp32_reduce.validate().unwrap();

        let mut per_channel = valid_marlin_moe_raw_args();
        per_channel.group_size = -1;
        per_channel.validate().unwrap();

        let mut fp8 = valid_marlin_moe_raw_args();
        fp8.weight_type = MarlinMoeF16WeightType::E4M3;
        fp8.group_size = -1;
        fp8.validate().unwrap();

        let mut fp8_group128 = valid_marlin_moe_raw_args();
        fp8_group128.weight_type = MarlinMoeF16WeightType::E4M3;
        fp8_group128.group_size = 128;
        fp8_group128.validate().unwrap();
    }

    #[test]
    fn marlin_moe_raw_args_reject_invalid_pointers() {
        let mut args = valid_marlin_moe_raw_args();
        args.a = 0;
        assert_invalid_marlin_moe_args(args, "a pointer must be non-null");

        let mut args = valid_marlin_moe_raw_args();
        args.scales += 2;
        assert_invalid_marlin_moe_args(args, "scales pointer");

        let mut args = valid_marlin_moe_raw_args();
        args.topk_weights = Some(0xb002);
        assert_invalid_marlin_moe_args(args, "topk_weights pointer");
    }

    #[test]
    fn marlin_moe_raw_args_reject_invalid_shapes_and_config() {
        let mut args = valid_marlin_moe_raw_args();
        args.prob_m = 0;
        assert_invalid_marlin_moe_args(args, "must be positive");

        let mut args = valid_marlin_moe_raw_args();
        args.moe_block_size = 24;
        assert_invalid_marlin_moe_args(args, "unsupported moe_block_size");

        let mut args = valid_marlin_moe_raw_args();
        args.top_k = 0;
        assert_invalid_marlin_moe_args(args, "top_k must be positive");

        let mut args = valid_marlin_moe_raw_args();
        args.prob_m = i32::MAX;
        assert_invalid_marlin_moe_args(args, "prob_m * top_k overflows");

        let mut args = valid_marlin_moe_raw_args();
        args.prob_n = 96;
        assert_invalid_marlin_moe_args(args, "prob_n 96 must be divisible");

        let mut args = valid_marlin_moe_raw_args();
        args.prob_k = 96;
        args.group_size = -1;
        assert_invalid_marlin_moe_args(args, "prob_k 96 must be divisible");

        let mut args = valid_marlin_moe_raw_args();
        args.group_size = 0;
        assert_invalid_marlin_moe_args(args, "group_size must be -1");

        let mut args = valid_marlin_moe_raw_args();
        args.group_size = 96;
        assert_invalid_marlin_moe_args(args, "must be divisible by group_size");

        let mut args = valid_marlin_moe_raw_args();
        args.device_ordinal = -1;
        assert_invalid_marlin_moe_args(args, "device_ordinal must be non-negative");
    }

    #[test]
    fn marlin_moe_raw_args_reject_inconsistent_optional_modes() {
        let mut args = valid_marlin_moe_raw_args();
        args.has_zero_points = true;
        assert_invalid_marlin_moe_args(args, "has_zero_points must exactly match");

        let mut args = valid_marlin_moe_raw_args();
        args.mul_topk_weights = true;
        assert_invalid_marlin_moe_args(args, "requires a non-null topk_weights");

        let mut args = valid_marlin_moe_raw_args();
        args.use_fp32_reduce = true;
        assert_invalid_marlin_moe_args(args, "exactly one");

        let mut args = valid_marlin_moe_raw_args();
        args.use_atomic_add = false;
        assert_invalid_marlin_moe_args(args, "exactly one");

        let mut args = valid_marlin_moe_raw_args();
        args.c_tmp = Some(0x9000);
        assert_invalid_marlin_moe_args(args, "use_fp32_reduce must exactly match");

        for group_size in [0, 16, 64, 256] {
            let mut args = valid_marlin_moe_raw_args();
            args.weight_type = MarlinMoeF16WeightType::E4M3;
            args.group_size = group_size;
            assert_invalid_marlin_moe_args(args, "E4M3 group_size must be -1 or 128");
        }

        let mut args = valid_marlin_moe_raw_args();
        args.weight_type = MarlinMoeF16WeightType::E4M3;
        args.prob_k = 192;
        assert_invalid_marlin_moe_args(args, "must be divisible by group_size 128");

        let mut args = valid_marlin_moe_raw_args();
        args.weight_type = MarlinMoeF16WeightType::E4M3;
        args.group_size = -1;
        args.zero_points = Some(0xa000);
        args.has_zero_points = true;
        assert_invalid_marlin_moe_args(args, "E4M3 forbids zero points");
    }

    #[test]
    #[ignore = "requires an sm89 CUDA host and the GPT-OSS MXFP4 Marlin-MoE native artifact"]
    #[cfg(feature = "vllm-moe-marlin")]
    fn gpt_oss_mxfp4_marlin_bias_p32_two_experts_matches_logical_source() {
        const EXPERTS: usize = 2;
        const ROWS: usize = 2;
        const N: usize = 64;
        const K: usize = 128;
        const MOE_BLOCK_SIZE: usize = 16;

        let context = CudaContext::new(0).expect("CUDA context");
        let stream = context.default_stream();
        let sms = usize::try_from(
            context
                .attribute(CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT)
                .expect("query CUDA SM count"),
        )
        .expect("CUDA SM count must be positive");
        let input_device: CudaSlice<bf16> = stream
            .alloc_zeros(ROWS * K)
            .expect("allocate zero MXFP4 input");
        let weight_device: CudaSlice<i32> = stream
            .alloc_zeros(EXPERTS * N * K / 8)
            .expect("allocate zero MXFP4 weights");
        let neutral_scales = vec![127_u8; EXPERTS * N * (K / 32)];
        let scales_device: CudaSlice<u8> = stream
            .clone_htod(&neutral_scales)
            .expect("upload neutral MXFP4 scales");
        let logical_bias = (0..EXPERTS)
            .flat_map(|expert| {
                (0..N).map(move |output| {
                    let magnitude = (output + 1) as f32;
                    bf16::from_f32(if expert == 0 { -magnitude } else { magnitude })
                })
            })
            .collect::<Vec<_>>();
        let prepared_bias = prepare_mxfp4_marlin_bias(&logical_bias, EXPERTS, N);
        let bias_device: CudaSlice<bf16> = stream
            .clone_htod(&prepared_bias)
            .expect("upload P32 MXFP4 bias");
        let mut output_device: CudaSlice<bf16> =
            stream.alloc_zeros(ROWS * N).expect("allocate MXFP4 output");
        let mut reduce_device: CudaSlice<f32> = stream
            .alloc_zeros(sms * 4 * MOE_BLOCK_SIZE * 256)
            .expect("allocate MXFP4 reduction scratch");
        let mut sorted = vec![ROWS as i32; EXPERTS * MOE_BLOCK_SIZE];
        sorted[0] = 0;
        sorted[MOE_BLOCK_SIZE] = 1;
        let sorted_device: CudaSlice<i32> = stream.clone_htod(&sorted).unwrap();
        let expert_device: CudaSlice<i32> = stream.clone_htod(&[0, 1]).unwrap();
        let padded_device: CudaSlice<i32> = stream
            .clone_htod(&[(EXPERTS * MOE_BLOCK_SIZE) as i32])
            .unwrap();
        let workspace: CudaSlice<i32> = stream.alloc_zeros(sms * 4).unwrap();

        {
            let (a, _a_guard) = input_device.device_ptr(&stream);
            let (b, _b_guard) = weight_device.device_ptr(&stream);
            let (c, _c_guard) = output_device.device_ptr_mut(&stream);
            let (c_tmp, _c_tmp_guard) = reduce_device.device_ptr_mut(&stream);
            let (bias, _bias_guard) = bias_device.device_ptr(&stream);
            let (scales, _scales_guard) = scales_device.device_ptr(&stream);
            let (workspace, _workspace_guard) = workspace.device_ptr(&stream);
            let (sorted, _sorted_guard) = sorted_device.device_ptr(&stream);
            let (experts, _experts_guard) = expert_device.device_ptr(&stream);
            let (padded, _padded_guard) = padded_device.device_ptr(&stream);
            launch_marlin_moe_mxfp4_bf16(
                &stream,
                MarlinMoeMxfp4Bf16LaunchArgs {
                    weight_type: MarlinMoeMxfp4WeightType::E2M1E8M0,
                    expert_count: EXPERTS as i32,
                    a,
                    b,
                    c,
                    c_tmp: Some(c_tmp),
                    bias,
                    scales,
                    workspace,
                    sorted_token_ids: sorted,
                    expert_ids: experts,
                    num_tokens_past_padded: padded,
                    topk_weights: None,
                    moe_block_size: MOE_BLOCK_SIZE as i32,
                    top_k: 1,
                    mul_topk_weights: false,
                    is_ep: false,
                    prob_m: ROWS as i32,
                    prob_n: N as i32,
                    prob_k: K as i32,
                    group_size: 32,
                    device_ordinal: 0,
                    use_atomic_add: false,
                    use_fp32_reduce: true,
                },
            )
            .expect("launch MXFP4 bias-only Marlin-MoE");
            stream.synchronize().expect("synchronize bias-only launch");
        }

        let actual = stream
            .clone_dtoh(&output_device)
            .expect("download bias-only output");
        for row in 0..ROWS {
            for output in 0..N {
                assert_eq!(
                    actual[row * N + output].to_bits(),
                    logical_bias[row * N + output].to_bits(),
                    "row={row} expert={row} output={output}"
                );
            }
        }
        eprintln!("FERRUM GPTOSS MXFP4 MARLIN BIAS P32 E2 M2 PASS: N={N} K={K}");
    }

    #[test]
    #[ignore = "requires an sm89 CUDA host and the GPT-OSS MXFP4 Marlin-MoE native artifact"]
    #[cfg(feature = "vllm-moe-marlin")]
    fn gpt_oss_mxfp4_marlin_moe_bf16_ffi_matches_source_reference_for_four_cases() {
        const MOE_BLOCK_SIZE: usize = 16;
        const LCG_MULTIPLIER: u64 = 0x5851_f42d_4c95_7f2d;
        const LCG_INCREMENT: u64 = 0x1405_7b7e_f767_814f;
        const ROOT_SEED: u64 = 0x4750_544f_5353_4d58;
        const SHAPE_SEED_XOR: u64 = 0x9e37_79b9_7f4a_7c15;
        const WEIGHT_SEED_XOR: u64 = 0x5745_4947_4854_5f31;
        const SCALE_SEED_XOR: u64 = 0x5343_414c_455f_5f31;
        const ACTIVATION_SEED_XOR: u64 = 0x4143_5449_5641_5445;
        const QUALITY_VECTOR_DEFINITION: &str = concat!(
            "gpt-oss-mxfp4-marlin-moe-v1;",
            "cases=gate-up-512x256-b1,b4|down-256x512-b1,b4;",
            "root_seed=0x4750544f53534d58;",
            "lcg=0x5851f42d4c957f2d+0x14057b7ef767814f;",
            "stream_xors=0x9e3779b97f4a7c15,0x5745494748545f31,",
            "0x5343414c455f5f31,0x4143544956415445;",
            "activation=bf16[-1,1];source=e2m1-low-nibble-first/e8m0-group32;",
            "decode=e2m1-table[0,0.5,1,1.5,2,3,4,6]*2^(e8m0-127);",
            "bias=bf16;accumulator=f32;output=bf16;",
            "reference=source-decoded-matmul-plus-bias;",
            "relative_l2_max=0.05;nan=0;inf=0",
        );
        const QUALITY_VECTOR_DIGEST: &str =
            "7b8d4908cbee9c68aa4ff4c47c5e883bf76788250cfbb334603dbbd746218b21";
        const CASES: [(&str, usize, usize, usize, usize); 4] = [
            ("gate-up-512x256-batch-1", 0, 512, 256, 1),
            ("gate-up-512x256-batch-4", 0, 512, 256, 4),
            ("down-256x512-batch-1", 1, 256, 512, 1),
            ("down-256x512-batch-4", 1, 256, 512, 4),
        ];

        fn next(state: &mut u64) -> u64 {
            *state = state
                .wrapping_mul(LCG_MULTIPLIER)
                .wrapping_add(LCG_INCREMENT);
            *state
        }

        fn stream_seed(shape_index: usize, stream_xor: u64) -> u64 {
            let mut state = ROOT_SEED
                ^ (u64::try_from(shape_index + 1)
                    .expect("shape index fits u64")
                    .wrapping_mul(SHAPE_SEED_XOR))
                ^ stream_xor;
            next(&mut state)
        }

        fn decode_e2m1(bits: u8) -> f32 {
            const MAGNITUDES: [f32; 8] = [0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0];
            let magnitude = MAGNITUDES[usize::from(bits & 0x07)];
            if bits & 0x08 == 0 {
                magnitude
            } else {
                -magnitude
            }
        }

        let context = CudaContext::new(0).expect("CUDA context");
        let stream = context.default_stream();
        assert_eq!(
            format!("{:x}", Sha256::digest(QUALITY_VECTOR_DEFINITION.as_bytes())),
            QUALITY_VECTOR_DIGEST,
            "GPT-OSS MXFP4 quality-vector definition changed without a digest update"
        );
        let sms = usize::try_from(
            context
                .attribute(CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT)
                .expect("query CUDA SM count"),
        )
        .expect("CUDA SM count must be positive");

        for (case_id, shape_index, n, k, batch) in CASES {
            let mut activation_state = stream_seed(shape_index, ACTIVATION_SEED_XOR);
            let input = (0..batch * k)
                .map(|_| {
                    let signed = i32::try_from(next(&mut activation_state) % 129)
                        .expect("activation residue fits i32")
                        - 64;
                    bf16::from_f32(signed as f32 / 64.0)
                })
                .collect::<Vec<_>>();

            let mut weight_state = stream_seed(shape_index, WEIGHT_SEED_XOR);
            let source_blocks = (0..n * k / 2)
                .map(|_| {
                    let even =
                        u8::try_from(next(&mut weight_state) & 0x0f).expect("E2M1 nibble fits u8");
                    let odd =
                        u8::try_from(next(&mut weight_state) & 0x0f).expect("E2M1 nibble fits u8");
                    even | (odd << 4)
                })
                .collect::<Vec<_>>();

            let groups_per_row = k / 32;
            let mut scale_state = stream_seed(shape_index, SCALE_SEED_XOR);
            let source_scales = (0..n * groups_per_row)
                .map(|_| {
                    125_u8
                        + u8::try_from(next(&mut scale_state) % 5).expect("E8M0 scale tier fits u8")
                })
                .collect::<Vec<_>>();
            let bias = (0..n)
                .map(|output| bf16::from_f32((output as i32 % 7 - 3) as f32 / 32.0))
                .collect::<Vec<_>>();

            let mut reference = vec![0.0_f32; batch * n];
            for row in 0..batch {
                for output in 0..n {
                    let mut sum = bias[output].to_f32();
                    for input_feature in 0..k {
                        let packed = source_blocks[output * (k / 2) + input_feature / 2];
                        let nibble = if input_feature.is_multiple_of(2) {
                            packed & 0x0f
                        } else {
                            packed >> 4
                        };
                        let exponent =
                            i32::from(source_scales[output * groups_per_row + input_feature / 32])
                                - 127;
                        let decoded_weight = decode_e2m1(nibble) * 2.0_f32.powi(exponent);
                        sum += input[row * k + input_feature].to_f32() * decoded_weight;
                    }
                    reference[row * n + output] = sum;
                }
            }

            let gptq_words = transpose_mxfp4_expert_blocks_to_gptq_words(&source_blocks, n, k)
                .expect("transpose source MXFP4 nibbles");
            let packed_weight = repack_gptq_to_marlin(&gptq_words, k, n);
            let packed_scales = prepare_mxfp4_expert_scales_for_marlin(&source_scales, n, k)
                .expect("prepare source E8M0 scales");

            let input_device: CudaSlice<bf16> = stream.clone_htod(&input).expect("upload input");
            let weight_device: CudaSlice<i32> = stream
                .clone_htod(&packed_weight)
                .expect("upload packed MXFP4 weight");
            let scales_device: CudaSlice<u8> = stream
                .clone_htod(&packed_scales)
                .expect("upload packed E8M0 scales");
            let prepared_bias = prepare_mxfp4_marlin_bias(&bias, 1, n);
            let bias_device: CudaSlice<bf16> =
                stream.clone_htod(&prepared_bias).expect("upload P32 bias");
            let mut output_device: CudaSlice<bf16> = stream
                .alloc_zeros(batch * n)
                .expect("allocate MXFP4 output");
            // Match the production MoE workspace bound: every resident SM may
            // own four 16-row, 256-column FP32 reduction tiles. The native
            // kernel indexes this fixed arena, not merely the logical [M,N]
            // output extent.
            let mut reduce_device: CudaSlice<f32> = stream
                .alloc_zeros(sms * 4 * MOE_BLOCK_SIZE * 256)
                .expect("allocate MXFP4 reduction scratch");

            let mut sorted_token_ids = vec![i32::try_from(batch).unwrap(); MOE_BLOCK_SIZE];
            for (token, sorted) in sorted_token_ids.iter_mut().take(batch).enumerate() {
                *sorted = i32::try_from(token).expect("token index fits i32");
            }
            let sorted_token_ids_device: CudaSlice<i32> = stream
                .clone_htod(&sorted_token_ids)
                .expect("upload padded sorted token ids");
            let expert_ids_device: CudaSlice<i32> =
                stream.clone_htod(&[0]).expect("upload expert block id");
            let num_tokens_past_padded_device: CudaSlice<i32> = stream
                .clone_htod(&[i32::try_from(MOE_BLOCK_SIZE).unwrap()])
                .expect("upload padded token count");
            let workspace: CudaSlice<i32> = stream
                .alloc_zeros(n.div_ceil(128) * sms * 4)
                .expect("allocate Marlin-MoE workspace");

            {
                let (input_pointer, _input_guard) = input_device.device_ptr(&stream);
                let (weight_pointer, _weight_guard) = weight_device.device_ptr(&stream);
                let (output_pointer, _output_guard) = output_device.device_ptr_mut(&stream);
                let (reduce_pointer, _reduce_guard) = reduce_device.device_ptr_mut(&stream);
                let (bias_pointer, _bias_guard) = bias_device.device_ptr(&stream);
                let (scales_pointer, _scales_guard) = scales_device.device_ptr(&stream);
                let (workspace_pointer, _workspace_guard) = workspace.device_ptr(&stream);
                let (sorted_pointer, _sorted_guard) = sorted_token_ids_device.device_ptr(&stream);
                let (expert_pointer, _expert_guard) = expert_ids_device.device_ptr(&stream);
                let (padded_pointer, _padded_guard) =
                    num_tokens_past_padded_device.device_ptr(&stream);

                launch_marlin_moe_mxfp4_bf16(
                    &stream,
                    MarlinMoeMxfp4Bf16LaunchArgs {
                        weight_type: MarlinMoeMxfp4WeightType::E2M1E8M0,
                        expert_count: 1,
                        a: input_pointer,
                        b: weight_pointer,
                        c: output_pointer,
                        c_tmp: Some(reduce_pointer),
                        bias: bias_pointer,
                        scales: scales_pointer,
                        workspace: workspace_pointer,
                        sorted_token_ids: sorted_pointer,
                        expert_ids: expert_pointer,
                        num_tokens_past_padded: padded_pointer,
                        topk_weights: None,
                        moe_block_size: i32::try_from(MOE_BLOCK_SIZE).unwrap(),
                        top_k: 1,
                        mul_topk_weights: false,
                        is_ep: false,
                        prob_m: i32::try_from(batch).unwrap(),
                        prob_n: i32::try_from(n).unwrap(),
                        prob_k: i32::try_from(k).unwrap(),
                        group_size: 32,
                        device_ordinal: 0,
                        use_atomic_add: false,
                        use_fp32_reduce: true,
                    },
                )
                .expect("launch GPT-OSS MXFP4 Marlin-MoE");
                stream
                    .synchronize()
                    .expect("synchronize GPT-OSS MXFP4 Marlin-MoE");
            }

            let actual = stream
                .clone_dtoh(&output_device)
                .expect("download GPT-OSS MXFP4 output");
            let mut reference_squared = 0.0_f64;
            let mut error_squared = 0.0_f64;
            let mut nan_count = 0_usize;
            let mut infinity_count = 0_usize;
            for (actual, expected) in actual.iter().zip(reference.iter().copied()) {
                let actual = actual.to_f32();
                reference_squared += f64::from(expected) * f64::from(expected);
                if actual.is_nan() {
                    nan_count += 1;
                } else if actual.is_infinite() {
                    infinity_count += 1;
                } else {
                    let error = f64::from(actual - expected);
                    error_squared += error * error;
                }
            }
            let relative_l2 = if nan_count == 0 && infinity_count == 0 {
                error_squared.sqrt() / reference_squared.sqrt().max(1.0e-6)
            } else {
                f64::INFINITY
            };

            eprintln!(
                "GPT_OSS_MXFP4_MARLIN_MOE_FFI_FIXTURE name={case_id} \
                 quality_vector_digest={QUALITY_VECTOR_DIGEST} \
                 rel_err={relative_l2:.8} nan_count={nan_count} \
                 infinity_count={infinity_count}"
            );
            assert_eq!(nan_count, 0, "{case_id} emitted NaN");
            assert_eq!(infinity_count, 0, "{case_id} emitted Inf");
            assert!(
                relative_l2 <= 0.05,
                "{case_id} rel_err={relative_l2:.8} exceeds 0.05"
            );
        }
    }

    #[test]
    #[ignore = "requires an sm89 CUDA host and the GPT-OSS MXFP4 Marlin-MoE native artifact"]
    #[cfg(feature = "vllm-moe-marlin")]
    fn gpt_oss_mxfp4_marlin_moe_executes_official_down_with_physical_k_padding() {
        const N: usize = 2880;
        const LOGICAL_K: usize = 2880;
        const PHYSICAL_K: usize = 2944;
        const BATCH: usize = 1;
        const MOE_BLOCK_SIZE: usize = 16;
        const ACTIVE_FEATURES: [usize; 8] = [0, 127, 128, 511, 1024, 1537, 2048, 2815];

        fn decode_e2m1(bits: u8) -> f32 {
            const MAGNITUDES: [f32; 8] = [0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0];
            let magnitude = MAGNITUDES[usize::from(bits & 0x07)];
            if bits & 0x08 == 0 {
                magnitude
            } else {
                -magnitude
            }
        }

        let context = CudaContext::new(0).expect("CUDA context");
        let stream = context.default_stream();
        let sms = usize::try_from(
            context
                .attribute(CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT)
                .expect("query CUDA SM count"),
        )
        .expect("CUDA SM count must be positive");

        // The model contract remains [N=2880,K=2880]. Only the execution K is
        // padded to 2944 so the native 128x64 tile is legal. Cross-tile
        // nonzero values make this a numerical pipeline check instead of a
        // zero-output geometry check that could hide a stuck K pipeline.
        let input = (0..PHYSICAL_K)
            .map(|feature| {
                if feature < LOGICAL_K {
                    bf16::from_f32((i32::try_from(feature % 17).unwrap() - 8) as f32 / 8.0)
                } else {
                    bf16::from_f32(0.0)
                }
            })
            .collect::<Vec<_>>();
        let mut source_blocks = vec![0_u8; N * PHYSICAL_K / 2];
        let groups_per_row = PHYSICAL_K / 32;
        let logical_groups_per_row = LOGICAL_K / 32;
        let mut source_scales = vec![0_u8; N * groups_per_row];
        let bias = (0..N)
            .map(|output| bf16::from_f32((output as i32 % 7 - 3) as f32 / 32.0))
            .collect::<Vec<_>>();
        let mut reference = vec![0.0_f32; N];
        for output in 0..N {
            source_scales
                [output * groups_per_row..output * groups_per_row + logical_groups_per_row]
                .fill(127);
            let mut sum = bias[output].to_f32();
            for (ordinal, feature) in ACTIVE_FEATURES.iter().copied().enumerate() {
                let magnitude = 1 + ((output + ordinal) % 7) as u8;
                let nibble = if (output + ordinal).is_multiple_of(3) {
                    magnitude | 0x08
                } else {
                    magnitude
                };
                let byte = &mut source_blocks[output * (PHYSICAL_K / 2) + feature / 2];
                if feature.is_multiple_of(2) {
                    *byte = (*byte & 0xf0) | nibble;
                } else {
                    *byte = (*byte & 0x0f) | (nibble << 4);
                }
                sum += input[feature].to_f32() * decode_e2m1(nibble);
            }
            reference[output] = sum;
        }
        assert!(input[LOGICAL_K..].iter().all(|value| value.to_f32() == 0.0));
        assert!(source_scales
            .chunks_exact(groups_per_row)
            .all(|row| row[logical_groups_per_row..]
                .iter()
                .all(|scale| *scale == 0)));

        let gptq_words = transpose_mxfp4_expert_blocks_to_gptq_words(&source_blocks, N, PHYSICAL_K)
            .expect("transpose padded official-shape MXFP4 nibbles");
        let packed_weight = repack_gptq_to_marlin(&gptq_words, PHYSICAL_K, N);
        let packed_scales = prepare_mxfp4_expert_scales_for_marlin(&source_scales, N, PHYSICAL_K)
            .expect("prepare padded official-shape E8M0 scales");

        let input_device: CudaSlice<bf16> = stream.clone_htod(&input).expect("upload input");
        let weight_device: CudaSlice<i32> = stream
            .clone_htod(&packed_weight)
            .expect("upload official-shape MXFP4 weight");
        let scales_device: CudaSlice<u8> = stream
            .clone_htod(&packed_scales)
            .expect("upload official-shape E8M0 scales");
        let prepared_bias = prepare_mxfp4_marlin_bias(&bias, 1, N);
        let bias_device: CudaSlice<bf16> =
            stream.clone_htod(&prepared_bias).expect("upload P32 bias");
        let mut output_device: CudaSlice<bf16> = stream
            .alloc_zeros(BATCH * N)
            .expect("allocate official-shape output");
        let mut reduce_device: CudaSlice<f32> = stream
            .alloc_zeros(sms * 4 * MOE_BLOCK_SIZE * 256)
            .expect("allocate official-shape reduction scratch");

        let mut sorted_token_ids = vec![BATCH as i32; MOE_BLOCK_SIZE];
        sorted_token_ids[0] = 0;
        let sorted_token_ids_device: CudaSlice<i32> = stream
            .clone_htod(&sorted_token_ids)
            .expect("upload padded sorted token ids");
        let expert_ids_device: CudaSlice<i32> =
            stream.clone_htod(&[0]).expect("upload expert block id");
        let num_tokens_past_padded_device: CudaSlice<i32> = stream
            .clone_htod(&[MOE_BLOCK_SIZE as i32])
            .expect("upload padded token count");
        let workspace: CudaSlice<i32> = stream
            .alloc_zeros(N.div_ceil(128) * sms * 4)
            .expect("allocate official-shape Marlin-MoE workspace");

        {
            let (input_pointer, _input_guard) = input_device.device_ptr(&stream);
            let (weight_pointer, _weight_guard) = weight_device.device_ptr(&stream);
            let (output_pointer, _output_guard) = output_device.device_ptr_mut(&stream);
            let (reduce_pointer, _reduce_guard) = reduce_device.device_ptr_mut(&stream);
            let (bias_pointer, _bias_guard) = bias_device.device_ptr(&stream);
            let (scales_pointer, _scales_guard) = scales_device.device_ptr(&stream);
            let (workspace_pointer, _workspace_guard) = workspace.device_ptr(&stream);
            let (sorted_pointer, _sorted_guard) = sorted_token_ids_device.device_ptr(&stream);
            let (expert_pointer, _expert_guard) = expert_ids_device.device_ptr(&stream);
            let (padded_pointer, _padded_guard) = num_tokens_past_padded_device.device_ptr(&stream);

            launch_marlin_moe_mxfp4_bf16(
                &stream,
                MarlinMoeMxfp4Bf16LaunchArgs {
                    weight_type: MarlinMoeMxfp4WeightType::E2M1E8M0,
                    expert_count: 1,
                    a: input_pointer,
                    b: weight_pointer,
                    c: output_pointer,
                    c_tmp: Some(reduce_pointer),
                    bias: bias_pointer,
                    scales: scales_pointer,
                    workspace: workspace_pointer,
                    sorted_token_ids: sorted_pointer,
                    expert_ids: expert_pointer,
                    num_tokens_past_padded: padded_pointer,
                    topk_weights: None,
                    moe_block_size: MOE_BLOCK_SIZE as i32,
                    top_k: 1,
                    mul_topk_weights: false,
                    is_ep: false,
                    prob_m: BATCH as i32,
                    prob_n: N as i32,
                    prob_k: PHYSICAL_K as i32,
                    group_size: 32,
                    device_ordinal: 0,
                    use_atomic_add: false,
                    use_fp32_reduce: true,
                },
            )
            .expect("launch official GPT-OSS 20B down geometry");
            stream
                .synchronize()
                .expect("synchronize official GPT-OSS 20B down geometry");
        }

        let actual = stream
            .clone_dtoh(&output_device)
            .expect("download official-shape output");
        let mut reference_squared = 0.0_f64;
        let mut error_squared = 0.0_f64;
        let mut non_finite = 0_usize;
        for (actual, expected) in actual.iter().zip(reference.iter().copied()) {
            let actual = actual.to_f32();
            reference_squared += f64::from(expected) * f64::from(expected);
            if actual.is_finite() {
                let error = f64::from(actual - expected);
                error_squared += error * error;
            } else {
                non_finite += 1;
            }
        }
        let relative_l2 = error_squared.sqrt() / reference_squared.sqrt().max(1.0e-6);
        assert_eq!(
            non_finite, 0,
            "padded official down geometry emitted NaN/Inf"
        );
        assert!(
            relative_l2 <= 0.05,
            "padded official down geometry relative L2 {relative_l2:.8} exceeds 0.05"
        );
        eprintln!(
            "FERRUM GPTOSS MXFP4 OFFICIAL GEOMETRY PASS: M={BATCH} N={N} logical_K={LOGICAL_K} physical_K={PHYSICAL_K} rel_err={relative_l2:.8}"
        );
    }

    #[test]
    #[ignore = "requires an sm89 CUDA host and the GPT-OSS MXFP4 Marlin-MoE A6 artifact"]
    #[cfg(feature = "vllm-moe-marlin")]
    fn gpt_oss_mxfp4_official_down_two_experts_four_rows_uses_128x64_and_matches_source() {
        const N: usize = 2880;
        const LOGICAL_K: usize = 2880;
        const PHYSICAL_K: usize = 2944;
        const ROWS: usize = 4;

        std::env::set_var("FERRUM_VLLM_MOE_LOG_CONFIG", "1");
        for name in [
            "FERRUM_VLLM_MOE_LOG_CONFIG_MIN_PAIRS",
            "FERRUM_VLLM_MOE_LOG_CONFIG_MAX_PAIRS",
            "FERRUM_VLLM_MOE_THREAD_K",
            "FERRUM_VLLM_MOE_THREAD_N",
        ] {
            std::env::remove_var(name);
        }
        let profile_path = std::env::temp_dir().join(format!(
            "ferrum-gptoss-down-{}-config.jsonl",
            std::process::id()
        ));
        let _ = std::fs::remove_file(&profile_path);
        configure_vllm_moe_profile_sink(&ProfileSinkConfig::enabled(
            profile_path.clone(),
            ProfileMetadata::default(),
        ))
        .unwrap();

        let context = CudaContext::new(0).unwrap();
        let stream = context.default_stream();
        let gate_up = (0..ROWS * LOGICAL_K * 2)
            .map(|index| bf16::from_f32((index as i32 % 23 - 11) as f32 / 16.0))
            .collect::<Vec<_>>();
        let gate_up_device: CudaSlice<bf16> = stream.clone_htod(&gate_up).unwrap();
        let mut input_device: CudaSlice<bf16> = stream.alloc_zeros(ROWS * PHYSICAL_K).unwrap();
        let module = context
            .load_module(Ptx::from_src(crate::ptx::GPT_OSS_MOE.to_owned()))
            .unwrap();
        let function = module
            .load_function("gpt_oss_clamped_swiglu_interleaved_bf16")
            .unwrap();
        {
            let (gate_up, _gate_up_guard) = gate_up_device.device_ptr(&stream);
            let (input, _input_guard) = input_device.device_ptr_mut(&stream);
            let logical_k = LOGICAL_K as i32;
            let physical_k = PHYSICAL_K as i32;
            let elements = (ROWS * PHYSICAL_K) as i64;
            let limit = 7.0_f32;
            let mut launch = stream.launch_builder(&function);
            launch.arg(&gate_up);
            launch.arg(&input);
            launch.arg(&logical_k);
            launch.arg(&physical_k);
            launch.arg(&elements);
            launch.arg(&limit);
            unsafe {
                launch.launch(LaunchConfig {
                    grid_dim: ((elements as u32).div_ceil(256), 1, 1),
                    block_dim: (256, 1, 1),
                    shared_mem_bytes: 0,
                })
            }
            .unwrap();
            stream.synchronize().unwrap();
        }
        let input = stream.clone_dtoh(&input_device).unwrap();
        assert!(input
            .chunks_exact(PHYSICAL_K)
            .all(|row| row[LOGICAL_K..].iter().all(|value| value.to_f32() == 0.0)));
        let relative_l2 = assert_gpt_oss_mxfp4_two_expert_source_reference(
            &context,
            &stream,
            &input_device,
            &input,
            N,
            LOGICAL_K,
            PHYSICAL_K,
        );

        let profile = std::fs::read_to_string(&profile_path).unwrap();
        let selected = profile
            .lines()
            .map(|line| serde_json::from_str::<serde_json::Value>(line).unwrap())
            .find(|event| {
                event["event"] == "vllm_moe_config"
                    && event["shape"]["prob_m"] == ROWS as i64
                    && event["shape"]["prob_n"] == N as i64
                    && event["shape"]["prob_k"] == PHYSICAL_K as i64
            })
            .expect("native event for official padded down shape");
        assert_eq!(selected["shape"]["thread_k"], 128);
        assert_eq!(selected["shape"]["thread_n"], 64);
        configure_vllm_moe_profile_sink(&ProfileSinkConfig::disabled()).unwrap();
        let _ = std::fs::remove_file(profile_path);
        eprintln!(
            "FERRUM GPTOSS MXFP4 DOWN E2 M4 PASS: N={N} logical_K={LOGICAL_K} physical_K={PHYSICAL_K} rel_err={relative_l2:.8}"
        );
    }

    #[test]
    #[ignore = "requires an sm89 CUDA host and the GPT-OSS MXFP4 Marlin-MoE artifact"]
    #[cfg(feature = "vllm-moe-marlin")]
    fn gpt_oss_mxfp4_official_gate_up_two_experts_four_rows_matches_source() {
        const N: usize = 5760;
        const K: usize = 2880;
        const ROWS: usize = 4;

        let context = CudaContext::new(0).unwrap();
        let stream = context.default_stream();
        let input = (0..ROWS * K)
            .map(|index| bf16::from_f32((index as i32 % 29 - 14) as f32 / 16.0))
            .collect::<Vec<_>>();
        let input_device: CudaSlice<bf16> = stream.clone_htod(&input).unwrap();
        let relative_l2 = assert_gpt_oss_mxfp4_two_expert_source_reference(
            &context,
            &stream,
            &input_device,
            &input,
            N,
            K,
            K,
        );
        eprintln!("FERRUM GPTOSS MXFP4 GATE_UP E2 M4 PASS: N={N} K={K} rel_err={relative_l2:.8}");
    }

    #[test]
    #[ignore = "requires an sm89 CUDA host and the FP8 Marlin-MoE native artifact"]
    #[cfg(feature = "vllm-moe-marlin")]
    fn qwen36_a3_fp8_marlin_moe_ffi_matches_cpu_reference_for_four_cases() {
        const BLOCK_SHAPE: [usize; 2] = [128, 128];
        const MOE_BLOCK_SIZE: usize = 16;
        const LCG_MULTIPLIER: u64 = 0x5851_f42d_4c95_7f2d;
        const LCG_INCREMENT: u64 = 0x1405_7b7e_f767_814f;
        const ROOT_SEED: u64 = 0x5147_454e_3338_4650;
        const SHAPE_SEED_XOR: u64 = 0x9e37_79b9_7f4a_7c15;
        const WEIGHT_SEED_XOR: u64 = 0x5745_4947_4854_5f31;
        const SCALE_SEED_XOR: u64 = 0x5343_414c_455f_5f31;
        const ACTIVATION_SEED_XOR: u64 = 0x4143_5449_5641_5445;
        const CASES: [(&str, usize, usize, usize, usize); 4] = [
            ("weight-256x128-batch-1", 0, 256, 128, 1),
            ("weight-256x128-batch-4", 0, 256, 128, 4),
            ("weight-256x256-batch-1", 1, 256, 256, 1),
            ("weight-256x256-batch-4", 1, 256, 256, 4),
        ];

        fn next(state: &mut u64) -> u64 {
            *state = state
                .wrapping_mul(LCG_MULTIPLIER)
                .wrapping_add(LCG_INCREMENT);
            *state
        }

        fn stream_seed(shape_index: usize, stream_xor: u64) -> u64 {
            let mut state = ROOT_SEED
                ^ (u64::try_from(shape_index + 1)
                    .expect("shape index fits u64")
                    .wrapping_mul(SHAPE_SEED_XOR))
                ^ stream_xor;
            next(&mut state)
        }

        let context = CudaContext::new(0).expect("CUDA context");
        let stream = context.default_stream();
        let sms = usize::try_from(
            context
                .attribute(CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT)
                .expect("query CUDA SM count"),
        )
        .expect("CUDA SM count must be positive");

        for (case_id, shape_index, n, k, batch) in CASES {
            let mut activation_state = stream_seed(shape_index, ACTIVATION_SEED_XOR);
            let input = (0..batch * k)
                .map(|_| {
                    let signed = i32::try_from(next(&mut activation_state) % 129)
                        .expect("activation residue fits i32")
                        - 64;
                    f16::from_f32(signed as f32 / 64.0)
                })
                .collect::<Vec<_>>();

            let scale_rows = n.div_ceil(BLOCK_SHAPE[0]);
            let scale_columns = k.div_ceil(BLOCK_SHAPE[1]);
            let mut scale_state = stream_seed(shape_index, SCALE_SEED_XOR);
            let inverse_scales = (0..scale_rows * scale_columns)
                .map(|_| {
                    bf16::from_bits(
                        0x3b80
                            + 0x20
                                * u16::try_from(next(&mut scale_state) % 5)
                                    .expect("scale residue fits u16"),
                    )
                })
                .collect::<Vec<_>>();
            let inverse_scale_bytes = inverse_scales
                .iter()
                .flat_map(|scale| scale.to_le_bytes())
                .collect::<Vec<_>>();

            let mut weight_state = stream_seed(shape_index, WEIGHT_SEED_XOR);
            let source_values = (0..n * k)
                .map(|index| {
                    let word = next(&mut weight_state);
                    if word & 0x0f == 0 {
                        0
                    } else {
                        let output_channel = index / k;
                        let exponent_tier =
                            u8::try_from((output_channel % 8) / 2).expect("exponent tier fits u8");
                        let magnitude = 0x20 + exponent_tier * 8 + ((word >> 9) & 0x07) as u8;
                        let sign = ((word >> 31) & 0x80) as u8;
                        magnitude | sign
                    }
                })
                .collect::<Vec<_>>();

            let mut reference = vec![0.0_f32; batch * n];
            for row in 0..batch {
                for output in 0..n {
                    let mut sum = 0.0_f32;
                    for input_feature in 0..k {
                        let source =
                            float8::F8E4M3::from_bits(source_values[output * k + input_feature])
                                .to_f32();
                        let scale_index = (output / BLOCK_SHAPE[0]) * scale_columns
                            + input_feature / BLOCK_SHAPE[1];
                        let decoded_weight = source * inverse_scales[scale_index].to_f32();
                        sum += input[row * k + input_feature].to_f32() * decoded_weight;
                    }
                    reference[row * n + output] = sum;
                }
            }

            let prepared = prepare_block_fp8_weight_for_fp8_marlin(
                &source_values,
                &inverse_scale_bytes,
                n,
                k,
                BLOCK_SHAPE,
            )
            .expect("prepare block-FP8 Marlin-MoE weight");
            let (packed_weight, packed_scales) = prepared.into_parts();

            let input_device: CudaSlice<f16> = stream.clone_htod(&input).expect("upload input");
            let weight_device: CudaSlice<u8> = stream
                .clone_htod(&packed_weight)
                .expect("upload packed FP8 weight");
            let scales_device: CudaSlice<f16> = stream
                .clone_htod(&packed_scales)
                .expect("upload packed FP8 scales");
            let mut output_device: CudaSlice<f16> = stream
                .alloc_zeros(batch * n)
                .expect("allocate Marlin-MoE output");

            // One active expert owns one 16-row block. Actual token ids occupy
            // the prefix and every remaining row uses the vLLM sentinel
            // `prob_m * top_k` so the kernel skips padded rows.
            let mut sorted_token_ids = vec![i32::try_from(batch).unwrap(); MOE_BLOCK_SIZE];
            for (token, sorted) in sorted_token_ids.iter_mut().take(batch).enumerate() {
                *sorted = i32::try_from(token).expect("token index fits i32");
            }
            let sorted_token_ids_device: CudaSlice<i32> = stream
                .clone_htod(&sorted_token_ids)
                .expect("upload padded sorted token ids");
            let expert_ids_device: CudaSlice<i32> =
                stream.clone_htod(&[0]).expect("upload expert block id");
            let num_tokens_past_padded_device: CudaSlice<i32> = stream
                .clone_htod(&[i32::try_from(MOE_BLOCK_SIZE).unwrap()])
                .expect("upload padded token count");
            let workspace: CudaSlice<i32> = stream
                .alloc_zeros(n.div_ceil(128) * sms * 4)
                .expect("allocate Marlin-MoE workspace");

            {
                let (input_pointer, _input_guard) = input_device.device_ptr(&stream);
                let (weight_pointer, _weight_guard) = weight_device.device_ptr(&stream);
                let (output_pointer, _output_guard) = output_device.device_ptr_mut(&stream);
                let (scales_pointer, _scales_guard) = scales_device.device_ptr(&stream);
                let (workspace_pointer, _workspace_guard) = workspace.device_ptr(&stream);
                let (sorted_pointer, _sorted_guard) = sorted_token_ids_device.device_ptr(&stream);
                let (expert_pointer, _expert_guard) = expert_ids_device.device_ptr(&stream);
                let (padded_pointer, _padded_guard) =
                    num_tokens_past_padded_device.device_ptr(&stream);

                launch_marlin_moe_vllm_raw(
                    &stream,
                    MarlinMoeRawLaunchArgs {
                        weight_type: MarlinMoeF16WeightType::E4M3,
                        a: input_pointer,
                        b: weight_pointer,
                        c: output_pointer,
                        c_tmp: None,
                        scales: scales_pointer,
                        zero_points: None,
                        workspace: workspace_pointer,
                        sorted_token_ids: sorted_pointer,
                        expert_ids: expert_pointer,
                        num_tokens_past_padded: padded_pointer,
                        topk_weights: None,
                        moe_block_size: i32::try_from(MOE_BLOCK_SIZE).unwrap(),
                        top_k: 1,
                        mul_topk_weights: false,
                        is_ep: false,
                        prob_m: i32::try_from(batch).unwrap(),
                        prob_n: i32::try_from(n).unwrap(),
                        prob_k: i32::try_from(k).unwrap(),
                        group_size: -1,
                        has_zero_points: false,
                        device_ordinal: 0,
                        use_atomic_add: true,
                        use_fp32_reduce: false,
                    },
                )
                .expect("launch ferrum_vllm_marlin_moe_fp8_f16");
                stream.synchronize().expect("synchronize FP8 Marlin-MoE");
            }

            let actual = stream
                .clone_dtoh(&output_device)
                .expect("download FP8 Marlin-MoE output");
            let mut reference_squared = 0.0_f64;
            let mut error_squared = 0.0_f64;
            let mut nan_count = 0_usize;
            let mut infinity_count = 0_usize;
            for (actual, expected) in actual.iter().zip(reference.iter().copied()) {
                let actual = actual.to_f32();
                reference_squared += f64::from(expected) * f64::from(expected);
                if actual.is_nan() {
                    nan_count += 1;
                } else if actual.is_infinite() {
                    infinity_count += 1;
                } else {
                    let error = f64::from(actual - expected);
                    error_squared += error * error;
                }
            }
            let relative_l2 = if nan_count == 0 && infinity_count == 0 {
                error_squared.sqrt() / reference_squared.sqrt().max(1.0e-6)
            } else {
                f64::INFINITY
            };

            eprintln!(
                "QWEN36_A3_FP8_MARLIN_MOE_FFI_FIXTURE name={case_id} \
                 rel_err={relative_l2:.8} nan_count={nan_count} \
                 infinity_count={infinity_count}"
            );
            assert_eq!(nan_count, 0, "{case_id} emitted NaN");
            assert_eq!(infinity_count, 0, "{case_id} emitted Inf");
            assert!(
                relative_l2 <= 0.05,
                "{case_id} rel_err={relative_l2:.8} exceeds 0.05"
            );
        }
    }

    #[test]
    fn cuda_marlin_runtime_config_parses_skip_ws_zero() {
        let config = CudaMarlinRuntimeConfig::from_env_vars([
            ("FERRUM_MARLIN_PROFILE", "1"),
            ("FERRUM_MARLIN_SKIP_WS_ZERO", "1"),
            ("FERRUM_MARLIN_TRACE_SHAPES", "1"),
            ("FERRUM_MARLIN_TRACE_SHAPES_MAX", "17"),
        ]);
        assert!(config.profile);
        assert!(config.skip_ws_zero);
        assert!(config.trace_shapes);
        assert_eq!(config.trace_shapes_max, 17);
    }

    #[test]
    fn cuda_marlin_runtime_config_defaults_to_zero_workspace() {
        let config = CudaMarlinRuntimeConfig::from_env_vars([
            ("FERRUM_MARLIN_PROFILE", "true"),
            ("FERRUM_MARLIN_SKIP_WS_ZERO", "true"),
            ("FERRUM_MARLIN_TRACE_SHAPES", "true"),
            ("FERRUM_MARLIN_TRACE_SHAPES_MAX", "not-a-number"),
        ]);
        assert!(!config.profile);
        assert!(!config.skip_ws_zero);
        assert!(!config.trace_shapes);
        assert_eq!(config.trace_shapes_max, 256);
    }

    #[test]
    fn marlin_workspace_zeroing_follows_runtime_config() {
        let default_config = CudaMarlinRuntimeConfig::from_env_vars(Vec::<(&str, &str)>::new());
        assert!(should_zero_workspace(&default_config));

        let skip_config =
            CudaMarlinRuntimeConfig::from_env_vars([("FERRUM_MARLIN_SKIP_WS_ZERO", "1")]);
        assert!(!should_zero_workspace(&skip_config));
    }

    #[test]
    fn marlin_profile_bucket_labels_match_projection_names() {
        assert_eq!(
            marlin_profile_bucket_from_label("label=llama.batched_layer.qkv_proj"),
            MarlinProfileBucket::Qkv
        );
        assert_eq!(
            marlin_profile_bucket_from_label("label=llama.forward_layer.o_proj"),
            MarlinProfileBucket::OProj
        );
        assert_eq!(
            marlin_profile_bucket_from_label("label=llama.forward_layer.gate_up_proj"),
            MarlinProfileBucket::GateUp
        );
        assert_eq!(
            marlin_profile_bucket_from_label("label=llama.forward_layer.down_proj"),
            MarlinProfileBucket::Down
        );
        assert_eq!(
            marlin_profile_bucket_from_label("label=llama.batched.lm_head"),
            MarlinProfileBucket::LmHead
        );
        assert_eq!(
            marlin_profile_bucket_from_label("label=<none>"),
            MarlinProfileBucket::Other
        );
    }

    #[test]
    fn marlin_profile_bucket_stats_record_all_profile_phases() {
        let mut stats = MarlinProfileBucketStats::ZERO;

        stats.record_ws_zero(3);
        stats.record_gather(5);
        stats.record_kernel(7);

        assert_eq!(stats.ws_zero_us, 3);
        assert_eq!(stats.ws_zero_calls, 1);
        assert_eq!(stats.gather_us, 5);
        assert_eq!(stats.gather_calls, 1);
        assert_eq!(stats.kernel_us, 7);
        assert_eq!(stats.kernel_calls, 1);
    }
}