cortiq-engine 0.5.51

Portable inference runtime for the CMF model format, with no ML framework underneath: runs on CPU, and on GPU (Vulkan / Metal / DX12) with the `gpu` feature; tokenizer, chat templates and dynamic per-skill weight overlay.
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
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
22413
22414
22415
22416
22417
22418
22419
22420
22421
22422
22423
22424
22425
22426
22427
22428
22429
22430
22431
22432
22433
22434
22435
22436
22437
22438
22439
22440
22441
22442
22443
22444
22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
22461
22462
22463
22464
22465
22466
22467
22468
22469
22470
22471
22472
22473
22474
22475
22476
22477
22478
22479
22480
22481
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
22671
22672
22673
22674
22675
22676
22677
22678
22679
22680
22681
22682
22683
22684
22685
22686
22687
22688
22689
22690
22691
22692
22693
22694
22695
22696
22697
22698
22699
22700
22701
22702
22703
22704
22705
22706
22707
22708
22709
22710
22711
22712
22713
22714
22715
22716
22717
22718
22719
22720
22721
22722
22723
22724
22725
22726
22727
22728
22729
22730
22731
22732
22733
22734
22735
22736
22737
22738
22739
22740
22741
22742
22743
22744
22745
22746
22747
22748
22749
22750
22751
22752
22753
22754
22755
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769
22770
22771
22772
22773
22774
22775
22776
22777
22778
22779
22780
22781
22782
22783
22784
22785
22786
22787
22788
22789
22790
22791
22792
22793
22794
22795
22796
22797
22798
22799
22800
22801
22802
22803
22804
22805
22806
22807
22808
22809
22810
22811
22812
22813
22814
22815
22816
22817
22818
22819
22820
22821
22822
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834
22835
22836
22837
22838
22839
22840
22841
22842
22843
22844
22845
22846
22847
22848
22849
22850
22851
22852
22853
22854
22855
22856
22857
22858
22859
22860
22861
22862
22863
22864
22865
22866
22867
22868
22869
22870
22871
22872
22873
22874
22875
22876
22877
22878
22879
22880
22881
22882
22883
22884
22885
22886
22887
22888
22889
22890
22891
22892
22893
22894
22895
22896
22897
22898
22899
22900
22901
22902
22903
22904
22905
22906
22907
22908
22909
22910
22911
22912
22913
22914
22915
22916
22917
22918
22919
22920
22921
22922
22923
22924
22925
22926
22927
22928
22929
22930
22931
22932
22933
22934
22935
22936
22937
22938
22939
22940
22941
22942
22943
22944
22945
22946
22947
22948
22949
22950
22951
22952
22953
22954
22955
22956
22957
22958
22959
22960
22961
22962
22963
22964
22965
22966
22967
22968
22969
22970
22971
22972
22973
22974
22975
22976
22977
22978
22979
22980
22981
22982
22983
22984
22985
22986
22987
22988
22989
22990
22991
22992
22993
22994
22995
22996
22997
22998
22999
23000
23001
23002
23003
23004
23005
23006
23007
23008
23009
23010
23011
23012
23013
23014
23015
23016
23017
23018
23019
23020
23021
23022
23023
23024
23025
23026
23027
23028
23029
23030
23031
23032
23033
23034
23035
23036
23037
23038
23039
23040
23041
23042
23043
23044
23045
23046
23047
23048
23049
23050
23051
23052
23053
23054
23055
23056
23057
23058
23059
23060
23061
23062
23063
23064
23065
23066
23067
23068
23069
23070
23071
23072
23073
23074
23075
23076
23077
23078
23079
23080
23081
23082
23083
23084
23085
23086
23087
23088
23089
23090
23091
23092
23093
23094
23095
23096
23097
23098
23099
23100
23101
23102
23103
23104
23105
23106
23107
23108
23109
23110
23111
23112
23113
23114
23115
23116
23117
23118
23119
23120
23121
23122
23123
23124
23125
23126
23127
23128
23129
23130
23131
23132
23133
23134
23135
23136
23137
23138
23139
23140
23141
23142
23143
23144
23145
23146
23147
23148
23149
23150
23151
23152
23153
23154
23155
23156
23157
23158
23159
23160
23161
23162
23163
23164
23165
23166
23167
23168
23169
23170
23171
23172
23173
23174
23175
23176
23177
23178
23179
23180
23181
23182
23183
23184
23185
23186
23187
23188
23189
23190
23191
23192
23193
23194
23195
23196
23197
23198
23199
23200
23201
23202
23203
23204
23205
23206
23207
23208
23209
23210
23211
23212
23213
23214
23215
23216
23217
23218
23219
23220
23221
23222
23223
23224
23225
23226
23227
23228
23229
23230
23231
23232
23233
23234
23235
23236
23237
23238
23239
23240
23241
23242
23243
23244
23245
23246
23247
23248
23249
23250
23251
23252
23253
23254
23255
23256
23257
23258
23259
23260
23261
23262
23263
23264
23265
23266
23267
23268
23269
23270
23271
23272
23273
23274
23275
23276
23277
23278
23279
23280
23281
23282
23283
23284
23285
23286
23287
23288
23289
23290
23291
23292
23293
23294
23295
23296
23297
23298
23299
23300
23301
23302
23303
23304
23305
23306
23307
23308
23309
23310
23311
23312
23313
23314
23315
23316
23317
23318
23319
23320
23321
23322
23323
23324
23325
23326
23327
23328
23329
23330
23331
23332
23333
23334
23335
23336
23337
23338
23339
23340
23341
23342
23343
23344
23345
23346
23347
23348
23349
23350
23351
23352
23353
23354
23355
23356
23357
23358
23359
23360
23361
23362
23363
23364
23365
23366
23367
23368
23369
23370
23371
23372
23373
23374
23375
23376
23377
23378
23379
23380
23381
23382
23383
23384
23385
23386
23387
23388
23389
23390
23391
23392
23393
23394
23395
23396
23397
23398
23399
23400
23401
23402
23403
23404
23405
23406
23407
23408
23409
23410
23411
23412
23413
23414
23415
23416
23417
23418
23419
23420
23421
23422
23423
23424
23425
23426
23427
23428
23429
23430
23431
23432
23433
23434
23435
23436
23437
23438
23439
23440
23441
23442
23443
23444
23445
23446
23447
23448
23449
23450
23451
23452
23453
23454
23455
23456
23457
23458
23459
23460
23461
23462
23463
23464
23465
23466
23467
23468
23469
23470
23471
23472
23473
23474
23475
23476
23477
23478
23479
23480
23481
23482
23483
23484
23485
23486
23487
23488
23489
23490
23491
23492
23493
23494
23495
23496
23497
23498
23499
23500
23501
23502
23503
23504
23505
23506
23507
23508
23509
23510
23511
23512
23513
23514
23515
23516
23517
23518
23519
23520
23521
23522
23523
23524
23525
23526
23527
23528
23529
23530
23531
23532
23533
23534
23535
23536
23537
23538
23539
23540
23541
23542
23543
23544
23545
23546
23547
23548
23549
23550
23551
23552
23553
23554
23555
23556
23557
23558
23559
23560
23561
23562
23563
23564
23565
23566
23567
23568
23569
23570
23571
23572
23573
23574
23575
23576
23577
23578
23579
23580
23581
23582
23583
23584
23585
23586
23587
23588
23589
23590
23591
23592
23593
23594
23595
23596
23597
23598
23599
23600
23601
23602
23603
23604
23605
23606
23607
23608
23609
23610
23611
23612
23613
23614
23615
23616
23617
23618
23619
23620
23621
23622
23623
23624
23625
23626
23627
23628
23629
23630
23631
23632
23633
23634
23635
23636
23637
23638
23639
23640
23641
23642
23643
23644
23645
23646
23647
23648
23649
23650
23651
23652
23653
23654
23655
23656
23657
23658
23659
23660
23661
23662
23663
23664
23665
23666
23667
23668
23669
23670
23671
23672
23673
23674
23675
23676
23677
23678
23679
23680
23681
23682
23683
23684
23685
23686
23687
23688
23689
23690
23691
23692
23693
23694
23695
23696
23697
23698
23699
23700
23701
23702
23703
23704
23705
23706
23707
23708
23709
23710
23711
23712
23713
23714
23715
23716
23717
23718
23719
23720
23721
23722
23723
23724
23725
23726
23727
23728
23729
23730
23731
23732
23733
23734
23735
23736
23737
23738
23739
23740
23741
23742
23743
23744
23745
23746
23747
23748
23749
23750
23751
23752
23753
23754
23755
23756
23757
23758
23759
23760
23761
23762
23763
23764
23765
23766
23767
23768
23769
23770
23771
23772
23773
23774
23775
23776
23777
23778
23779
23780
23781
23782
23783
23784
23785
23786
23787
23788
23789
23790
23791
23792
23793
23794
23795
23796
23797
23798
23799
23800
23801
23802
23803
23804
23805
23806
23807
23808
23809
23810
23811
23812
23813
23814
23815
23816
23817
23818
23819
23820
23821
23822
23823
23824
23825
23826
23827
23828
23829
23830
23831
23832
23833
23834
23835
23836
23837
23838
23839
23840
23841
23842
23843
23844
23845
23846
23847
23848
23849
23850
23851
23852
23853
23854
23855
23856
23857
23858
23859
23860
23861
23862
23863
23864
23865
23866
23867
23868
23869
23870
23871
23872
23873
23874
23875
23876
23877
23878
23879
23880
23881
23882
23883
23884
23885
23886
23887
23888
23889
23890
23891
23892
23893
23894
23895
23896
23897
23898
23899
23900
23901
23902
23903
23904
23905
23906
23907
23908
23909
23910
23911
23912
23913
23914
23915
23916
23917
23918
23919
23920
23921
23922
23923
23924
23925
23926
23927
23928
23929
23930
23931
23932
23933
23934
23935
23936
23937
23938
23939
23940
23941
23942
23943
23944
23945
23946
23947
23948
23949
23950
23951
23952
23953
23954
23955
23956
23957
23958
23959
23960
23961
23962
23963
23964
23965
23966
23967
23968
23969
23970
23971
23972
23973
23974
23975
23976
23977
23978
23979
23980
23981
23982
23983
23984
23985
23986
23987
23988
23989
23990
23991
23992
23993
23994
23995
23996
23997
23998
23999
24000
24001
24002
24003
24004
24005
24006
24007
24008
24009
24010
24011
24012
24013
24014
24015
24016
24017
24018
24019
24020
24021
24022
24023
24024
24025
24026
24027
24028
24029
24030
24031
24032
24033
24034
24035
24036
24037
24038
24039
24040
24041
24042
24043
24044
24045
24046
24047
24048
24049
24050
24051
24052
24053
24054
24055
24056
24057
24058
24059
24060
24061
24062
24063
24064
24065
24066
24067
24068
24069
24070
24071
24072
24073
24074
24075
24076
24077
24078
24079
24080
24081
24082
24083
24084
24085
24086
24087
24088
24089
24090
24091
24092
24093
24094
24095
24096
24097
24098
24099
24100
24101
24102
24103
24104
24105
24106
24107
24108
24109
24110
24111
24112
24113
24114
24115
24116
24117
24118
24119
24120
24121
24122
24123
24124
24125
24126
24127
24128
24129
24130
24131
24132
24133
24134
24135
24136
24137
24138
24139
24140
24141
24142
24143
24144
24145
24146
24147
24148
24149
24150
24151
24152
24153
24154
24155
24156
24157
24158
24159
24160
24161
24162
24163
24164
24165
24166
24167
24168
24169
24170
24171
24172
24173
24174
24175
24176
24177
24178
24179
24180
24181
24182
24183
24184
24185
24186
24187
24188
24189
24190
24191
24192
24193
24194
24195
24196
24197
24198
24199
24200
24201
24202
24203
24204
24205
24206
24207
24208
24209
24210
24211
24212
24213
24214
24215
24216
24217
24218
24219
24220
24221
24222
24223
24224
24225
24226
24227
24228
24229
24230
24231
24232
24233
24234
24235
24236
24237
24238
24239
24240
24241
24242
24243
24244
24245
24246
24247
24248
24249
24250
24251
24252
24253
24254
24255
24256
24257
24258
24259
24260
24261
24262
24263
24264
24265
24266
24267
24268
24269
24270
24271
24272
24273
24274
24275
24276
24277
24278
24279
24280
24281
24282
24283
24284
24285
24286
24287
24288
24289
24290
24291
24292
24293
24294
24295
24296
24297
24298
24299
24300
24301
24302
24303
24304
24305
24306
24307
24308
24309
24310
24311
24312
24313
24314
24315
24316
24317
24318
24319
24320
24321
24322
24323
24324
24325
24326
24327
24328
24329
24330
24331
24332
24333
24334
24335
24336
24337
24338
24339
24340
24341
24342
24343
24344
24345
24346
24347
24348
24349
24350
24351
24352
24353
24354
24355
24356
24357
24358
24359
24360
24361
24362
24363
24364
24365
24366
24367
24368
24369
24370
24371
24372
24373
24374
24375
24376
24377
24378
24379
24380
24381
24382
24383
24384
24385
24386
24387
24388
24389
24390
24391
24392
24393
24394
24395
24396
24397
24398
24399
24400
24401
24402
24403
24404
24405
24406
24407
24408
24409
24410
24411
24412
24413
24414
24415
24416
24417
24418
24419
24420
24421
24422
24423
24424
24425
24426
24427
24428
24429
24430
24431
24432
24433
24434
24435
24436
24437
24438
24439
24440
24441
24442
24443
24444
24445
24446
24447
24448
24449
24450
24451
24452
24453
24454
24455
24456
24457
24458
24459
24460
24461
24462
24463
24464
24465
24466
24467
24468
24469
24470
24471
24472
24473
24474
24475
24476
24477
24478
24479
24480
24481
24482
24483
24484
24485
24486
24487
24488
24489
24490
24491
24492
24493
24494
24495
24496
24497
24498
24499
24500
24501
24502
24503
24504
24505
24506
24507
24508
24509
24510
24511
24512
24513
24514
24515
24516
24517
24518
24519
24520
24521
24522
24523
24524
24525
24526
24527
24528
24529
24530
24531
24532
24533
24534
24535
24536
24537
24538
24539
24540
24541
24542
24543
24544
24545
24546
24547
24548
24549
24550
24551
24552
24553
24554
24555
24556
24557
24558
24559
24560
24561
24562
24563
24564
24565
24566
24567
24568
24569
24570
24571
24572
24573
24574
24575
24576
24577
24578
24579
24580
24581
24582
24583
24584
24585
24586
24587
24588
24589
24590
24591
24592
24593
24594
24595
24596
24597
24598
24599
24600
24601
24602
24603
24604
24605
24606
24607
24608
24609
24610
24611
24612
24613
24614
24615
24616
24617
24618
24619
24620
24621
24622
24623
24624
24625
24626
24627
24628
24629
24630
24631
24632
24633
24634
24635
24636
24637
24638
24639
24640
24641
24642
24643
24644
24645
24646
24647
24648
24649
24650
24651
24652
24653
24654
24655
24656
24657
24658
24659
24660
24661
24662
24663
24664
24665
24666
24667
24668
24669
24670
24671
24672
24673
24674
24675
24676
24677
24678
24679
24680
24681
24682
24683
24684
24685
24686
24687
24688
24689
24690
24691
24692
24693
24694
24695
24696
24697
24698
24699
24700
24701
24702
24703
24704
24705
24706
24707
24708
24709
24710
24711
24712
24713
24714
24715
24716
24717
24718
24719
24720
24721
24722
24723
24724
24725
24726
24727
24728
24729
24730
24731
24732
24733
24734
24735
24736
24737
24738
24739
24740
24741
24742
24743
24744
24745
24746
24747
24748
24749
24750
24751
24752
24753
24754
24755
24756
24757
24758
24759
24760
24761
24762
24763
24764
24765
24766
24767
24768
24769
24770
24771
24772
24773
24774
24775
24776
24777
24778
24779
24780
24781
24782
24783
24784
24785
24786
24787
24788
24789
24790
24791
24792
24793
24794
24795
24796
24797
24798
24799
24800
24801
24802
24803
24804
24805
24806
24807
24808
24809
24810
24811
24812
24813
24814
24815
24816
24817
24818
24819
24820
24821
24822
24823
24824
24825
24826
24827
24828
24829
24830
24831
24832
24833
24834
24835
24836
24837
24838
24839
24840
24841
24842
24843
24844
24845
24846
24847
24848
24849
24850
24851
24852
24853
24854
24855
24856
24857
24858
24859
24860
24861
24862
24863
24864
24865
24866
24867
24868
24869
24870
24871
24872
24873
24874
24875
24876
24877
24878
24879
24880
24881
24882
24883
24884
24885
24886
24887
24888
24889
24890
24891
24892
24893
24894
24895
24896
24897
24898
24899
24900
24901
24902
24903
24904
24905
24906
24907
24908
24909
24910
24911
24912
24913
24914
24915
24916
24917
24918
24919
24920
24921
24922
24923
24924
24925
24926
24927
24928
24929
24930
24931
24932
24933
24934
24935
24936
24937
24938
24939
24940
24941
24942
24943
24944
24945
24946
24947
24948
24949
24950
24951
24952
24953
24954
24955
24956
24957
24958
24959
24960
24961
24962
24963
24964
24965
24966
24967
24968
24969
24970
24971
24972
24973
24974
24975
24976
24977
24978
24979
24980
24981
24982
24983
24984
24985
24986
24987
24988
24989
24990
24991
24992
24993
24994
24995
24996
24997
24998
24999
25000
25001
25002
25003
25004
25005
25006
25007
25008
25009
25010
25011
25012
25013
25014
25015
25016
25017
25018
25019
25020
25021
25022
25023
25024
25025
25026
25027
25028
25029
25030
25031
25032
25033
25034
25035
25036
25037
25038
25039
25040
25041
25042
25043
25044
25045
25046
25047
25048
25049
25050
25051
25052
25053
25054
25055
25056
25057
25058
25059
25060
25061
25062
25063
25064
25065
25066
25067
25068
25069
25070
25071
25072
25073
25074
25075
25076
25077
25078
25079
25080
25081
25082
25083
25084
25085
25086
25087
25088
25089
25090
25091
25092
25093
25094
25095
25096
25097
25098
25099
25100
25101
25102
25103
25104
25105
25106
25107
25108
25109
25110
25111
25112
25113
25114
25115
25116
25117
25118
25119
25120
25121
25122
25123
25124
25125
25126
25127
25128
25129
25130
25131
25132
25133
25134
25135
25136
25137
25138
25139
25140
25141
25142
25143
25144
25145
25146
25147
25148
25149
25150
25151
25152
25153
25154
25155
25156
25157
25158
25159
25160
25161
25162
25163
25164
25165
25166
25167
25168
25169
25170
25171
25172
25173
25174
25175
25176
25177
25178
25179
25180
25181
25182
25183
25184
25185
25186
25187
25188
25189
25190
25191
25192
25193
25194
25195
25196
25197
25198
25199
25200
25201
25202
25203
25204
25205
25206
25207
25208
25209
25210
25211
25212
25213
25214
25215
25216
25217
25218
25219
25220
25221
25222
25223
25224
25225
25226
25227
25228
25229
25230
25231
25232
25233
25234
25235
25236
25237
25238
25239
25240
25241
25242
25243
25244
25245
25246
25247
25248
25249
25250
25251
25252
25253
25254
25255
25256
25257
25258
25259
25260
25261
25262
25263
25264
25265
25266
25267
25268
25269
25270
25271
25272
25273
25274
25275
25276
25277
25278
25279
25280
25281
25282
25283
25284
25285
25286
25287
25288
25289
25290
25291
25292
25293
25294
25295
25296
25297
25298
25299
25300
25301
25302
25303
25304
25305
25306
25307
25308
25309
25310
25311
25312
25313
25314
25315
25316
25317
25318
25319
25320
25321
25322
25323
25324
25325
25326
25327
25328
25329
25330
25331
25332
25333
25334
25335
25336
25337
25338
25339
25340
25341
25342
25343
25344
25345
25346
25347
25348
25349
25350
25351
25352
25353
25354
25355
25356
25357
25358
25359
25360
25361
25362
25363
25364
25365
25366
25367
25368
25369
25370
25371
25372
25373
25374
25375
25376
25377
25378
25379
25380
25381
25382
25383
25384
25385
25386
25387
25388
25389
25390
25391
25392
25393
25394
25395
25396
25397
25398
25399
25400
25401
25402
25403
25404
25405
25406
25407
25408
25409
25410
25411
25412
25413
25414
25415
25416
25417
25418
25419
25420
25421
25422
25423
25424
25425
25426
25427
25428
25429
25430
25431
25432
25433
25434
25435
25436
25437
25438
25439
25440
25441
25442
25443
25444
25445
25446
25447
25448
25449
25450
25451
25452
25453
25454
25455
25456
25457
25458
25459
25460
25461
25462
25463
25464
25465
25466
25467
25468
25469
25470
25471
25472
25473
25474
25475
25476
25477
25478
25479
25480
25481
25482
25483
25484
25485
25486
25487
25488
25489
25490
25491
25492
25493
25494
25495
25496
25497
25498
25499
25500
25501
25502
25503
25504
25505
25506
25507
25508
25509
25510
25511
25512
25513
25514
25515
25516
25517
25518
25519
25520
25521
25522
25523
25524
25525
25526
25527
25528
25529
25530
25531
25532
25533
25534
25535
25536
25537
25538
25539
25540
25541
25542
25543
25544
25545
25546
25547
25548
25549
25550
25551
25552
25553
25554
25555
25556
25557
25558
25559
25560
25561
25562
25563
25564
25565
25566
25567
25568
25569
25570
25571
25572
25573
25574
25575
25576
25577
25578
25579
25580
25581
25582
25583
25584
25585
25586
25587
25588
25589
25590
25591
25592
25593
25594
25595
25596
25597
25598
25599
25600
25601
25602
25603
25604
25605
25606
25607
25608
25609
25610
25611
25612
25613
25614
25615
25616
25617
25618
25619
25620
25621
25622
25623
25624
25625
25626
25627
25628
25629
25630
25631
25632
25633
25634
25635
25636
25637
25638
25639
25640
25641
25642
25643
25644
25645
25646
25647
25648
25649
25650
25651
25652
25653
25654
25655
25656
25657
25658
25659
25660
25661
25662
25663
25664
25665
25666
25667
25668
25669
25670
25671
25672
25673
25674
25675
25676
25677
25678
25679
25680
25681
25682
25683
25684
25685
25686
25687
25688
25689
25690
25691
25692
25693
25694
25695
25696
25697
25698
25699
25700
25701
25702
25703
25704
25705
25706
25707
25708
25709
25710
25711
25712
25713
25714
25715
25716
25717
25718
25719
25720
25721
25722
25723
25724
25725
25726
25727
25728
25729
25730
25731
25732
25733
25734
25735
25736
25737
25738
25739
25740
25741
25742
25743
25744
25745
25746
25747
25748
25749
25750
25751
25752
25753
25754
25755
25756
25757
25758
25759
25760
25761
25762
25763
25764
25765
25766
25767
25768
25769
25770
25771
25772
25773
25774
25775
25776
25777
25778
25779
25780
25781
25782
25783
25784
25785
25786
25787
25788
25789
25790
25791
25792
25793
25794
25795
25796
25797
25798
25799
25800
25801
25802
25803
25804
25805
25806
25807
25808
25809
25810
25811
25812
25813
25814
25815
25816
25817
25818
25819
25820
25821
25822
25823
25824
25825
25826
25827
25828
25829
25830
25831
25832
25833
25834
25835
25836
25837
25838
25839
25840
25841
25842
25843
25844
25845
25846
25847
25848
25849
25850
25851
25852
25853
25854
25855
25856
25857
25858
25859
25860
25861
25862
25863
25864
25865
25866
25867
25868
25869
25870
25871
25872
25873
25874
25875
25876
25877
25878
25879
25880
25881
25882
25883
25884
25885
25886
25887
25888
25889
25890
25891
25892
25893
25894
25895
25896
25897
25898
25899
25900
25901
25902
25903
25904
25905
25906
25907
25908
25909
25910
25911
25912
25913
25914
25915
25916
25917
25918
25919
25920
25921
25922
25923
25924
25925
25926
25927
25928
25929
25930
25931
25932
25933
25934
25935
25936
25937
25938
25939
25940
25941
25942
25943
25944
25945
25946
25947
25948
25949
25950
25951
25952
25953
25954
25955
25956
25957
25958
25959
25960
25961
25962
25963
25964
25965
25966
25967
25968
25969
25970
25971
25972
25973
25974
25975
25976
25977
25978
25979
25980
25981
25982
25983
25984
25985
25986
25987
25988
25989
25990
25991
25992
25993
25994
25995
25996
25997
25998
25999
26000
26001
26002
26003
26004
26005
26006
26007
26008
26009
26010
26011
26012
26013
26014
26015
26016
26017
26018
26019
26020
26021
26022
26023
26024
26025
26026
26027
26028
26029
26030
26031
26032
26033
26034
26035
26036
26037
26038
26039
26040
26041
26042
26043
26044
26045
26046
26047
26048
26049
26050
26051
26052
26053
26054
26055
26056
26057
26058
26059
26060
26061
26062
26063
26064
26065
26066
26067
26068
26069
26070
26071
26072
26073
26074
26075
26076
26077
26078
26079
26080
26081
26082
26083
26084
26085
26086
26087
26088
26089
26090
26091
26092
26093
26094
26095
26096
26097
26098
26099
26100
26101
26102
26103
26104
26105
26106
26107
26108
26109
26110
26111
26112
26113
26114
26115
26116
26117
26118
26119
26120
26121
26122
26123
26124
26125
26126
26127
26128
26129
26130
26131
26132
26133
26134
26135
26136
26137
26138
26139
26140
26141
26142
26143
26144
26145
26146
26147
26148
26149
26150
26151
26152
26153
26154
26155
26156
26157
26158
26159
26160
26161
26162
26163
26164
26165
26166
26167
26168
26169
26170
26171
26172
26173
26174
26175
26176
26177
26178
26179
26180
26181
26182
26183
26184
26185
26186
26187
26188
26189
26190
26191
26192
26193
26194
26195
26196
26197
26198
26199
26200
26201
26202
26203
26204
26205
26206
26207
26208
26209
26210
26211
26212
26213
26214
26215
26216
26217
26218
26219
26220
26221
26222
26223
26224
26225
26226
26227
26228
26229
26230
26231
26232
26233
26234
26235
26236
26237
26238
26239
26240
26241
26242
26243
26244
26245
26246
26247
26248
26249
26250
26251
26252
26253
26254
26255
26256
26257
26258
26259
26260
26261
26262
26263
26264
26265
26266
26267
26268
26269
26270
26271
26272
26273
26274
26275
26276
26277
26278
26279
26280
26281
26282
26283
26284
26285
26286
26287
26288
26289
26290
26291
26292
26293
26294
26295
26296
26297
26298
26299
26300
26301
26302
26303
26304
26305
26306
26307
26308
26309
26310
26311
26312
26313
26314
26315
26316
26317
26318
26319
26320
26321
26322
26323
26324
26325
26326
26327
26328
26329
26330
26331
26332
26333
26334
26335
26336
26337
26338
26339
26340
26341
26342
26343
26344
26345
26346
26347
26348
26349
26350
26351
26352
26353
26354
26355
26356
26357
26358
26359
26360
26361
26362
26363
26364
26365
26366
26367
26368
26369
26370
26371
26372
26373
26374
26375
26376
26377
26378
26379
26380
26381
26382
26383
26384
26385
26386
26387
26388
26389
26390
26391
26392
26393
26394
26395
26396
26397
26398
26399
26400
26401
26402
26403
26404
26405
26406
26407
26408
26409
26410
26411
26412
26413
26414
26415
26416
26417
26418
26419
26420
26421
26422
26423
26424
26425
26426
26427
26428
26429
26430
26431
26432
26433
26434
26435
26436
26437
26438
26439
26440
26441
26442
26443
26444
26445
26446
26447
26448
26449
26450
26451
26452
26453
26454
26455
26456
26457
26458
26459
26460
26461
26462
26463
26464
26465
26466
26467
26468
26469
26470
26471
26472
26473
26474
26475
26476
26477
26478
26479
26480
26481
26482
26483
26484
26485
26486
26487
26488
26489
26490
26491
26492
26493
26494
26495
26496
26497
26498
26499
26500
26501
26502
26503
26504
26505
26506
26507
26508
26509
26510
26511
26512
26513
26514
26515
26516
26517
26518
26519
26520
26521
26522
26523
26524
26525
26526
26527
26528
26529
26530
26531
26532
26533
26534
26535
26536
26537
26538
26539
26540
26541
26542
26543
26544
26545
26546
26547
26548
26549
26550
26551
26552
26553
26554
26555
26556
26557
26558
26559
26560
26561
26562
26563
26564
26565
26566
26567
26568
26569
26570
26571
26572
26573
26574
26575
26576
26577
26578
26579
26580
26581
26582
26583
26584
26585
26586
26587
26588
26589
26590
26591
26592
26593
26594
26595
26596
26597
26598
26599
26600
26601
26602
26603
26604
26605
26606
26607
26608
26609
26610
26611
26612
26613
26614
26615
26616
26617
26618
26619
26620
26621
26622
26623
26624
26625
26626
26627
26628
26629
26630
26631
26632
26633
26634
26635
26636
26637
26638
26639
26640
26641
26642
26643
26644
26645
26646
26647
26648
26649
26650
26651
26652
26653
26654
26655
26656
26657
26658
26659
26660
26661
26662
26663
26664
26665
26666
26667
26668
26669
26670
26671
26672
26673
26674
26675
26676
26677
26678
26679
26680
26681
26682
26683
26684
26685
26686
26687
26688
26689
26690
26691
26692
26693
26694
26695
26696
26697
26698
26699
26700
26701
26702
26703
26704
26705
26706
26707
26708
26709
26710
26711
26712
26713
26714
26715
26716
26717
26718
26719
26720
26721
26722
26723
26724
26725
26726
26727
26728
26729
26730
26731
26732
26733
26734
26735
26736
26737
26738
26739
26740
26741
26742
26743
26744
26745
26746
26747
26748
26749
26750
26751
26752
26753
26754
26755
26756
26757
26758
26759
26760
26761
26762
26763
26764
26765
26766
26767
26768
26769
26770
26771
26772
26773
26774
26775
26776
26777
26778
26779
26780
26781
26782
26783
26784
26785
26786
26787
26788
26789
26790
26791
26792
26793
26794
26795
26796
26797
26798
26799
26800
26801
26802
26803
26804
26805
26806
26807
26808
26809
26810
26811
26812
26813
26814
26815
26816
26817
26818
26819
26820
26821
26822
26823
26824
26825
26826
26827
26828
26829
26830
26831
26832
26833
26834
26835
26836
26837
26838
26839
26840
26841
26842
26843
26844
26845
26846
26847
26848
26849
26850
26851
26852
26853
26854
26855
26856
26857
26858
26859
26860
26861
26862
26863
26864
26865
26866
26867
26868
26869
26870
26871
26872
26873
26874
26875
26876
26877
26878
26879
26880
26881
26882
26883
26884
26885
26886
26887
26888
26889
26890
26891
26892
26893
26894
26895
26896
26897
26898
26899
26900
26901
26902
26903
26904
26905
26906
26907
26908
26909
26910
26911
26912
26913
26914
26915
26916
26917
26918
26919
26920
26921
26922
26923
26924
26925
26926
26927
26928
26929
26930
26931
26932
26933
26934
26935
26936
26937
26938
26939
26940
26941
26942
26943
26944
26945
26946
26947
26948
26949
26950
26951
26952
26953
26954
26955
26956
26957
26958
26959
26960
26961
26962
26963
26964
26965
26966
26967
26968
26969
26970
26971
26972
26973
26974
26975
26976
26977
26978
26979
26980
26981
26982
26983
26984
26985
26986
26987
26988
26989
26990
26991
26992
26993
26994
26995
26996
26997
26998
26999
27000
27001
27002
27003
27004
27005
27006
27007
27008
27009
27010
27011
27012
27013
27014
27015
27016
27017
27018
27019
27020
27021
27022
27023
27024
27025
27026
27027
27028
27029
27030
27031
27032
27033
27034
27035
27036
27037
27038
27039
27040
27041
27042
27043
27044
27045
27046
27047
27048
27049
27050
27051
27052
27053
27054
27055
27056
27057
27058
27059
27060
27061
27062
27063
27064
27065
27066
27067
27068
27069
27070
27071
27072
27073
27074
27075
27076
27077
27078
27079
27080
27081
27082
27083
27084
27085
27086
27087
27088
27089
27090
27091
27092
27093
27094
27095
27096
27097
27098
27099
27100
27101
27102
27103
27104
27105
27106
27107
27108
27109
27110
27111
27112
27113
27114
27115
27116
27117
27118
27119
27120
27121
27122
27123
27124
27125
27126
27127
27128
27129
27130
27131
27132
27133
27134
27135
27136
27137
27138
27139
27140
27141
27142
27143
27144
27145
27146
27147
27148
27149
27150
27151
27152
27153
27154
27155
27156
27157
27158
27159
27160
27161
27162
27163
27164
27165
27166
27167
27168
27169
27170
27171
27172
27173
27174
27175
27176
27177
27178
27179
27180
27181
27182
27183
27184
27185
27186
27187
27188
27189
27190
27191
27192
27193
27194
27195
27196
27197
27198
27199
27200
27201
27202
27203
27204
27205
27206
27207
27208
27209
27210
27211
27212
27213
27214
27215
27216
27217
27218
27219
27220
27221
27222
27223
27224
27225
27226
27227
27228
27229
27230
27231
27232
27233
27234
27235
27236
27237
27238
27239
27240
27241
27242
27243
27244
27245
27246
27247
27248
27249
27250
27251
27252
27253
27254
27255
27256
27257
27258
27259
27260
27261
27262
27263
27264
27265
27266
27267
27268
27269
27270
27271
27272
27273
27274
27275
27276
27277
27278
27279
27280
27281
27282
27283
27284
27285
27286
27287
27288
27289
27290
27291
27292
27293
27294
27295
27296
27297
27298
27299
27300
27301
27302
27303
27304
27305
27306
27307
27308
27309
27310
27311
27312
27313
27314
27315
27316
27317
27318
27319
27320
27321
27322
27323
27324
27325
27326
27327
27328
27329
27330
27331
27332
27333
27334
27335
27336
27337
27338
27339
27340
27341
27342
27343
27344
27345
27346
27347
27348
27349
27350
27351
27352
27353
27354
27355
27356
27357
27358
27359
27360
27361
27362
27363
27364
27365
27366
27367
27368
27369
27370
27371
27372
27373
27374
27375
27376
27377
27378
27379
27380
27381
27382
27383
27384
27385
27386
27387
27388
27389
27390
27391
27392
27393
27394
27395
27396
27397
27398
27399
27400
27401
27402
27403
27404
27405
27406
27407
27408
27409
27410
27411
27412
27413
27414
27415
27416
27417
27418
27419
27420
27421
27422
27423
27424
27425
27426
27427
27428
27429
27430
27431
27432
27433
27434
27435
27436
27437
27438
27439
27440
27441
27442
27443
27444
27445
27446
27447
27448
27449
27450
27451
27452
27453
27454
27455
27456
27457
27458
27459
27460
27461
27462
27463
27464
27465
27466
27467
27468
27469
27470
27471
27472
27473
27474
27475
27476
27477
27478
27479
27480
27481
27482
27483
27484
27485
27486
27487
27488
27489
27490
27491
27492
27493
27494
27495
27496
27497
27498
27499
27500
27501
27502
27503
27504
27505
27506
27507
27508
27509
27510
27511
27512
27513
27514
27515
27516
27517
27518
27519
27520
27521
27522
27523
27524
27525
27526
27527
27528
27529
27530
27531
27532
27533
27534
27535
27536
27537
27538
27539
27540
27541
27542
27543
27544
27545
27546
27547
27548
27549
27550
27551
27552
27553
27554
27555
27556
27557
27558
27559
27560
27561
27562
27563
27564
27565
27566
27567
27568
27569
27570
27571
27572
27573
27574
27575
27576
27577
27578
27579
27580
27581
27582
27583
27584
27585
27586
27587
27588
27589
27590
27591
27592
27593
27594
27595
27596
27597
27598
27599
27600
27601
27602
27603
27604
27605
27606
27607
27608
27609
27610
27611
27612
27613
27614
27615
27616
27617
27618
27619
27620
27621
27622
27623
27624
27625
27626
27627
27628
27629
27630
27631
27632
27633
27634
27635
27636
27637
27638
27639
27640
27641
27642
27643
27644
27645
27646
27647
27648
27649
27650
27651
27652
27653
27654
27655
27656
27657
27658
27659
27660
27661
27662
27663
27664
27665
27666
27667
27668
27669
27670
27671
27672
27673
27674
27675
27676
27677
27678
27679
27680
27681
27682
27683
27684
27685
27686
27687
27688
27689
27690
27691
27692
27693
27694
27695
27696
27697
27698
27699
27700
27701
27702
27703
27704
27705
27706
27707
27708
27709
27710
27711
27712
27713
27714
27715
27716
27717
27718
27719
27720
27721
27722
27723
27724
27725
27726
27727
27728
27729
27730
27731
27732
27733
27734
27735
27736
27737
27738
27739
27740
27741
27742
27743
27744
27745
27746
27747
27748
27749
27750
27751
27752
27753
27754
27755
27756
27757
27758
27759
27760
27761
27762
27763
27764
27765
27766
27767
27768
27769
27770
27771
27772
27773
27774
27775
27776
27777
27778
27779
27780
27781
27782
27783
27784
27785
27786
27787
27788
27789
27790
27791
27792
27793
27794
27795
27796
27797
27798
27799
27800
27801
27802
27803
27804
27805
27806
27807
27808
27809
27810
27811
27812
27813
27814
27815
27816
27817
27818
27819
27820
27821
27822
27823
27824
27825
27826
27827
27828
27829
27830
27831
27832
27833
27834
27835
27836
27837
27838
27839
27840
27841
27842
27843
27844
27845
27846
27847
27848
27849
27850
27851
27852
27853
27854
27855
27856
27857
27858
27859
27860
27861
27862
27863
27864
27865
27866
27867
27868
27869
27870
27871
27872
27873
27874
27875
27876
27877
27878
27879
27880
27881
27882
27883
27884
27885
27886
27887
27888
27889
27890
27891
27892
27893
27894
27895
27896
27897
27898
27899
27900
27901
27902
27903
27904
27905
27906
27907
27908
27909
27910
27911
27912
27913
27914
27915
27916
27917
27918
27919
27920
27921
27922
27923
27924
27925
27926
27927
27928
27929
27930
27931
27932
27933
27934
27935
27936
27937
27938
27939
27940
27941
27942
27943
27944
27945
27946
27947
27948
27949
27950
27951
27952
27953
27954
27955
27956
27957
27958
27959
27960
27961
27962
27963
27964
27965
27966
27967
27968
27969
27970
27971
27972
27973
27974
27975
27976
27977
27978
27979
27980
27981
27982
27983
27984
27985
27986
27987
27988
27989
27990
27991
27992
27993
27994
27995
27996
27997
27998
27999
28000
28001
28002
28003
28004
28005
28006
28007
28008
28009
28010
28011
28012
28013
28014
28015
28016
28017
28018
28019
28020
28021
28022
28023
28024
28025
28026
28027
28028
28029
28030
28031
28032
28033
28034
28035
28036
28037
28038
28039
28040
28041
28042
28043
28044
28045
28046
28047
28048
28049
28050
28051
28052
28053
28054
28055
28056
28057
28058
28059
28060
28061
28062
28063
28064
28065
28066
28067
28068
28069
28070
28071
28072
28073
28074
28075
28076
28077
28078
28079
28080
28081
28082
28083
28084
28085
28086
28087
28088
28089
28090
28091
28092
28093
28094
28095
28096
28097
28098
28099
28100
28101
28102
28103
28104
28105
28106
28107
28108
28109
28110
28111
28112
28113
28114
28115
28116
28117
28118
28119
28120
28121
28122
28123
28124
28125
28126
28127
28128
28129
28130
28131
28132
28133
28134
28135
28136
28137
28138
28139
28140
28141
28142
28143
28144
28145
28146
28147
28148
28149
28150
28151
28152
28153
28154
28155
28156
28157
28158
28159
28160
28161
28162
28163
28164
28165
28166
28167
28168
28169
28170
28171
28172
28173
28174
28175
28176
28177
28178
28179
28180
28181
28182
28183
28184
28185
28186
28187
28188
28189
28190
28191
28192
28193
28194
28195
28196
28197
28198
28199
28200
28201
28202
28203
28204
28205
28206
28207
28208
28209
28210
28211
28212
28213
28214
28215
28216
28217
28218
28219
28220
28221
//! Cross-platform GPU backend (C1): wgpu → Vulkan / DX12 / Metal
//! (NVIDIA, AMD Radeon, Intel Arc, Apple). Implements the same contract as
//! `gpu_metal.rs`, behind the `gpu.rs` facade — runtime call-sites do not change.
//!
//! Difference from the Metal path: a discrete card has no unified memory, so
//! the quantized weights are LOADED into VRAM ONCE (residency cache keyed by
//! tensor index) — that is where the win lives (VRAM bandwidth ×5–10 vs CPU). The math
//! is identical to CPU/Metal: y[o] = row_scale[o]·Σ q[o,i]·xs[i], where xs is already
//! prescaled by the θ field (the two-field q8_2f folds into the input prescale).
//!
//! Enabling: `CMF_GPU=wgpu` (or `=1` on non-macOS, where wgpu is the only backend).
//! Any init/limit failure — `false` and an honest CPU path.

use crate::gpu::{BatchJob, MoeJob};
use cortiq_core::CmfModel;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};
use wgpu::util::DeviceExt;

/// Workgroup limit per dimension (WebGPU minimum; lm_head has more
/// rows — we use grid-stride in the shader).
const MAX_WG: u32 = 65_535;

/// Subgroup-accelerated MoE select: the top-k rounds ride subgroupMax /
/// subgroupMin (barrier-free within a subgroup) — two barriers per slot
/// against the tree version's eight. Lives in its OWN module: `enable
/// subgroups` fails validation on devices without the feature, and one
/// invalid function kills every entry point of a module (0.5.40's Metal
/// lesson, re-learned on Vulkan this afternoon).
const SELECT_SG_SRC: &str = r#"
struct MoeSelP { n_exp: u32, top_k: u32, norm: u32, pk: u32 };
@group(0) @binding(0) var<storage, read>       sg_logit : array<f32>;
@group(0) @binding(1) var<storage, read>       sg_slog  : array<f32>;
@group(0) @binding(2) var<storage, read_write> sg_sel   : array<u32>;
@group(0) @binding(3) var<storage, read_write> sg_w     : array<f32>;
@group(0) @binding(4) var<uniform>             sg_p     : MoeSelP;
@group(0) @binding(5) var<storage, read>       sg_sgw   : array<u32>;
@group(0) @binding(6) var<storage, read>       sg_x     : array<f32>;

var<workgroup> sgm_lg:  array<f32, 256>;
var<workgroup> sgm_red: array<f32, 256>;
var<workgroup> sgm_pv:  array<f32, 8>;
var<workgroup> sgm_pi:  array<u32, 8>;
var<workgroup> sgm_gate: f32;

@compute @workgroup_size(256)
fn moe_select_sg(@builtin(local_invocation_index) lid: u32,
                 @builtin(subgroup_invocation_id) sl: u32,
                 @builtin(subgroup_size) ssz: u32) {
    let sgid = lid / ssz;
    let n = sg_p.n_exp;
    let sg_kind = sg_p.pk & 0xFFu;
    let sg_hidden = sg_p.pk >> 8u;
    // shared-expert gate (same math as the tree kernel)
    if (sg_kind == 4u) {
        var d = 0.0;
        var i = lid;
        loop {
            if (i >= sg_hidden) { break; }
            d = d + bitcast<f32>(sg_sgw[i]) * sg_x[i];
            i = i + 256u;
        }
        sgm_red[lid] = d;
        workgroupBarrier();
        var st = 128u;
        loop {
            if (st == 0u) { break; }
            if (lid < st) { sgm_red[lid] = sgm_red[lid] + sgm_red[lid + st]; }
            workgroupBarrier();
            st = st >> 1u;
        }
        if (lid == 0u) { sgm_gate = sgm_red[0]; }
    } else {
        if (lid == 0u) { sgm_gate = sg_slog[0]; }
    }
    workgroupBarrier();
    var v = -3.0e38;
    if (lid < n) { v = sg_logit[lid]; }
    sgm_lg[lid] = v;
    // global max + softmax denom (subgroup sums, one barrier each)
    let m1 = subgroupMax(v);
    if (sl == 0u) { sgm_red[sgid] = m1; }
    workgroupBarrier();
    var mx = -3.0e38;
    if (lid < 8u) { mx = sgm_red[lid]; }
    mx = subgroupMax(mx);
    mx = subgroupBroadcast(mx, 0u);
    if (lid == 0u) { sgm_red[255] = mx; }
    workgroupBarrier();
    mx = sgm_red[255];
    let ev = select(0.0, exp(v - mx), lid < n);
    let s1 = subgroupAdd(ev);
    if (sl == 0u) { sgm_red[sgid] = s1; }
    workgroupBarrier();
    var denom = 0.0;
    if (lid < 8u) { denom = sgm_red[lid]; }
    denom = subgroupAdd(denom);
    denom = subgroupBroadcast(denom, 0u);
    if (lid == 0u) { sgm_red[254] = denom; }
    workgroupBarrier();
    denom = sgm_red[254];
    // top-k rounds: subgroup argmax (value then lowest index), then an
    // 8-wide final in subgroup 0.
    let k = sg_p.top_k;
    var wsum = 0.0;
    for (var slot = 0u; slot < k; slot = slot + 1u) {
        let lv = sgm_lg[lid];
        let sm = subgroupMax(lv);
        let cand = select(0xFFFFFFFFu, lid, lv == sm);
        let si = subgroupMin(cand);
        if (sl == 0u) {
            sgm_pv[sgid] = sm;
            sgm_pi[sgid] = si;
        }
        workgroupBarrier();
        if (sgid == 0u) {
            var pv = -3.0e38;
            var pi = 0xFFFFFFFFu;
            if (sl < 8u) {
                pv = sgm_pv[sl];
                pi = sgm_pi[sl];
            }
            let bm = subgroupMax(pv);
            let bc = select(0xFFFFFFFFu, pi, pv == bm);
            let bi = subgroupMin(bc);
            if (sl == 0u) {
                sgm_pv[0] = bm;
                sgm_pi[0] = bi;
            }
        }
        workgroupBarrier();
        let bi = sgm_pi[0];
        let w = exp(sgm_pv[0] - mx) / denom;
        if (lid == 0u) {
            sg_sel[slot] = bi;
            sg_w[slot] = w;
        }
        wsum = wsum + w;
        if (lid == bi) { sgm_lg[lid] = -3.0e38; }
        workgroupBarrier();
    }
    if (lid == 0u) {
        if (sg_p.norm != 0u) {
            for (var slot = 0u; slot < k; slot = slot + 1u) { sg_w[slot] = sg_w[slot] / wsum; }
        }
        sg_sel[k] = n;
        sg_w[k] = 1.0 / (1.0 + exp(-sgm_gate));
    }
}
"#;

const WGSL: &str = r#"
struct Params { cols4: u32, rows: u32, row0_words: u32, _pad: u32 };
@group(0) @binding(0) var<storage, read>       q  : array<u32>;   // 4×i8 packed into u32, row-major
@group(0) @binding(1) var<storage, read>       xs : array<f32>;   // cols, already prescaled by the θ field
@group(0) @binding(2) var<storage, read>       rs : array<f32>;   // row scales for the range
@group(0) @binding(3) var<storage, read_write> y  : array<f32>;   // output: rows
@group(0) @binding(4) var<uniform>             p  : Params;

var<workgroup> partial: array<f32, 64>;

// Exact unpack of 4 signed bytes from u32 (little-endian) — like char4→
// float4 on Metal, without snorm error.
fn i8x4(w: u32) -> vec4<f32> {
    let s = i32(w);
    let b0 = (s << 24u) >> 24u;
    let b1 = (s << 16u) >> 24u;
    let b2 = (s <<  8u) >> 24u;
    let b3 =  s          >> 24u;
    return vec4<f32>(f32(b0), f32(b1), f32(b2), f32(b3));
}

// Grid-stride over rows: the number of workgroups is capped at 65535/dimension,
// while rows (lm_head) number in the hundreds of thousands; one group processes rows
// wid.x, wid.x+nwg.x, … , reducing each with 64 threads.
@compute @workgroup_size(64)
fn q8_matvec(@builtin(workgroup_id) wid: vec3<u32>,
             @builtin(num_workgroups) nwg: vec3<u32>,
             @builtin(local_invocation_index) lid: u32) {
    var row = wid.x;
    loop {
        if (row >= p.rows) { break; }
        let base = p.row0_words + row * p.cols4;
        var acc = 0.0;
        var i = lid;
        loop {
            if (i >= p.cols4) { break; }
            let v = i8x4(q[base + i]);
            let xi = i * 4u;
            let xv = vec4<f32>(xs[xi], xs[xi + 1u], xs[xi + 2u], xs[xi + 3u]);
            acc = acc + dot(v, xv);
            i = i + 64u;
        }
        partial[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { partial[lid] = partial[lid] + partial[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) { y[row] = partial[0] * rs[row]; }
        workgroupBarrier(); // before partial is reused by the next row
        row = row + nwg.x;
    }
}

// GEMM of the prefill batch: y[bi, o] = rs[o]·Σ q[o,i]·xs[bi,i]. One workgroup
// per (row, position); the quant row stays hot in cache across bi.
struct MMParams { cols4: u32, rows: u32, nb: u32, _pad: u32 };
@group(0) @binding(0) var<storage, read>       qm  : array<u32>;
@group(0) @binding(1) var<storage, read>       xsm : array<f32>;  // [nb, cols] row-major
@group(0) @binding(2) var<storage, read>       rsm : array<f32>;  // [rows]
@group(0) @binding(3) var<storage, read_write> ym  : array<f32>;  // [nb, rows] row-major
@group(0) @binding(4) var<uniform>             pm  : MMParams;

var<workgroup> partial_mm: array<f32, 64>;

@compute @workgroup_size(64)
fn q8_matmat(@builtin(workgroup_id) wid: vec3<u32>,
             @builtin(num_workgroups) nwg: vec3<u32>,
             @builtin(local_invocation_index) lid: u32) {
    let bi = wid.y;
    if (bi >= pm.nb) { return; }
    let xb = bi * pm.cols4 * 4u;
    var row = wid.x;
    loop {
        if (row >= pm.rows) { break; }
        let qb = row * pm.cols4;
        var acc = 0.0;
        var i = lid;
        loop {
            if (i >= pm.cols4) { break; }
            let v = i8x4(qm[qb + i]);
            let xi = xb + i * 4u;
            let xv = vec4<f32>(xsm[xi], xsm[xi + 1u], xsm[xi + 2u], xsm[xi + 3u]);
            acc = acc + dot(v, xv);
            i = i + 64u;
        }
        partial_mm[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { partial_mm[lid] = partial_mm[lid] + partial_mm[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) { ym[bi * pm.rows + row] = partial_mm[0] * rsm[row]; }
        workgroupBarrier();
        row = row + nwg.x;
    }
}

// q1: 6-byte tiles [f16 scale][4B sign bits] per 32-group; gpr is even,
// so a row is whole 12-byte tile PAIRS = 3 u32 each (same layout walk
// as the Metal kernel). Bit set → +x; np = gpr/2 tile-pairs/row (64 cols each).
//
// FAST kernel (the FFN q1 matvecs are ~59% of a 27B decode token): one
// workgroup owns 16 output ROWS, 16 lanes/row (256 threads). Activations are
// staged into shared memory in 1024-col tiles and REUSED across the 16 rows
// (16× fewer activation loads). Sign unpack is a branchless XOR sign-flip
// (bit clear ⇒ flip the f32 sign bit) instead of 32 vec4 selects.
struct Q1Params { np: u32, rows: u32, _p0: u32, _p1: u32 };
@group(0) @binding(0) var<storage, read>       q1w : array<u32>;
@group(0) @binding(1) var<storage, read>       q1x : array<f32>;   // raw f32 activations
@group(0) @binding(2) var<storage, read_write> q1y : array<f32>;
@group(0) @binding(3) var<uniform>             q1p : Q1Params;
// The grouped projection's weight tiles, 16 bytes at a time — the tile
// base (row·gpr+g)·4 words is vec4-aligned by construction. Only the
// batch o_lora kernel binds this view.
@group(0) @binding(4) var<storage, read>       q1wv : array<vec4<u32>>;
@group(0) @binding(5) var<storage, read>       q1xv : array<vec4<f32>>;

// q4b_dot8's eight terms, the activations arriving as two vec4 registers.
fn q1_dot8v(w: u32, a: vec4<f32>, b: vec4<f32>) -> f32 {
    return (f32(w & 0xFu) - 8.0) * a.x
         + (f32((w >> 4u) & 0xFu) - 8.0) * a.y
         + (f32((w >> 8u) & 0xFu) - 8.0) * a.z
         + (f32((w >> 12u) & 0xFu) - 8.0) * a.w
         + (f32((w >> 16u) & 0xFu) - 8.0) * b.x
         + (f32((w >> 20u) & 0xFu) - 8.0) * b.y
         + (f32((w >> 24u) & 0xFu) - 8.0) * b.z
         + (f32((w >> 28u) & 0xFu) - 8.0) * b.w;
}

var<workgroup> partial_q1: array<f32, 256>;   // 16 rows × 16 lanes
// 1024-col activation tile, PADDED to 33 slots per 32-col group. The read
// pattern is lane*64 + j*4 (all 16 lanes share bank (j*4) mod 32 with a flat
// 1024 tile => 16-way bank conflict, ~8x LSU penalty on the dominant inner
// loop). Padding to stride-33 spreads the lanes across 16 distinct banks
// (66 mod 32 = 2). Same math/accumulation order => token-identical.
var<workgroup> q1xs: array<f32, 1056>;        // 32 groups × 33

// Sum of ±x over one 32-weight group; x read from the shared tile at xbase.
// bit=1 → +x, bit=0 → -x, done by XORing the f32 sign bit (no select chain).
fn q1_tile_sum(bits: u32, xbase: u32) -> f32 {
    var s = vec4<f32>(0.0);
    let pb = (xbase >> 5u) * 33u;   // xbase is a multiple of 32 => padded group base
    for (var j = 0u; j < 8u; j = j + 1u) {
        let nib = bits >> (j * 4u);
        let o = pb + j * 4u;         // j*4+{0..3} stays in [0,32) < 33: no group crossing
        let x = vec4<f32>(q1xs[o], q1xs[o + 1u], q1xs[o + 2u], q1xs[o + 3u]);
        let m = vec4<u32>(
            ((nib & 1u) ^ 1u) << 31u,
            (((nib >> 1u) & 1u) ^ 1u) << 31u,
            (((nib >> 2u) & 1u) ^ 1u) << 31u,
            (((nib >> 3u) & 1u) ^ 1u) << 31u);
        s = s + bitcast<vec4<f32>>(bitcast<vec4<u32>>(x) ^ m);
    }
    return s.x + s.y + s.z + s.w;
}

@compute @workgroup_size(128)
fn q1_matvec(@builtin(workgroup_id) wid: vec3<u32>,
             @builtin(num_workgroups) nwg: vec3<u32>,
             @builtin(local_invocation_index) lid: u32) {
    let cols = q1p.np * 64u;
    let r = lid / 16u;      // which of the 8 rows this thread serves
    let lane = lid % 16u;   // which tile-pair lane within a column tile
    var row0 = wid.x * 8u;
    loop {
        if (row0 >= q1p.rows) { break; }
        let row = row0 + r;
        var acc = 0.0;
        var ti = 0u;                       // column tile start, in tile-pairs
        loop {
            if (ti >= q1p.np) { break; }
            // Cooperatively stage 1024 activations (16 tile-pairs) into shared.
            let c0 = ti * 64u;
            var k = lid;
            loop {
                if (k >= 1024u) { break; }
                let c = c0 + k;
                q1xs[(k >> 5u) * 33u + (k & 31u)] = select(0.0, q1x[c], c < cols);
                k = k + 128u;
            }
            workgroupBarrier();
            let pi = ti + lane;            // this lane's tile-pair
            if (row < q1p.rows && pi < q1p.np) {
                let base = row * q1p.np * 3u + pi * 3u;
                let a0 = q1w[base]; let a1 = q1w[base + 1u]; let a2 = q1w[base + 2u];
                let s0 = unpack2x16float(a0).x;
                let s1 = unpack2x16float(a1).y;
                let bits0 = (a0 >> 16u) | (a1 << 16u);
                let xb = lane * 64u;       // local offset of this pair in q1xs
                acc = acc + s0 * q1_tile_sum(bits0, xb) + s1 * q1_tile_sum(a2, xb + 32u);
            }
            workgroupBarrier();
            ti = ti + 16u;
        }
        partial_q1[lid] = acc;
        workgroupBarrier();
        // reduce the 16 lanes of each row (blocks of 16 in partial_q1)
        if (lane < 8u) { partial_q1[lid] = partial_q1[lid] + partial_q1[lid + 8u]; }
        workgroupBarrier();
        if (lane < 4u) { partial_q1[lid] = partial_q1[lid] + partial_q1[lid + 4u]; }
        workgroupBarrier();
        if (lane < 2u) { partial_q1[lid] = partial_q1[lid] + partial_q1[lid + 2u]; }
        workgroupBarrier();
        if (lane < 1u) { partial_q1[lid] = partial_q1[lid] + partial_q1[lid + 1u]; }
        workgroupBarrier();
        if (lane == 0u && row < q1p.rows) { q1y[row] = partial_q1[lid]; }
        workgroupBarrier();
        row0 = row0 + nwg.x * 8u;
    }
}

// Tiled GEMM for wide prefill batches (the WGSL cousin of Metal's
// q8_mul_mm; WGSL has no subgroup matrices, so this is the classic
// register-blocked form): a 64(b)×64(rows) C-tile per 16×16 workgroup,
// each thread owning a 4×4 accumulator block; X and dequantized W stage
// through 8 KB of workgroup memory in K-steps of 16. The naive kernel
// above re-reads every W row per position — here W is read once per 64
// positions. Perf is hardware-dependent by design: the runtime probe
// decides per machine whether this beats the CPU, so a card where it
// loses simply keeps the CPU path.
var<workgroup> mm_at: array<f32, 64 * 16>;
var<workgroup> mm_wt: array<f32, 64 * 16>;

fn mm_store4(m: u32, n0: u32, v0: f32, v1: f32, v2: f32, v3: f32) {
    if (m >= pm.nb) { return; }
    let base = m * pm.rows + n0;
    if (n0 < pm.rows) { ymm[base] = v0; }
    if (n0 + 1u < pm.rows) { ymm[base + 1u] = v1; }
    if (n0 + 2u < pm.rows) { ymm[base + 2u] = v2; }
    if (n0 + 3u < pm.rows) { ymm[base + 3u] = v3; }
}

fn q8_store4(m: u32, n0: u32, v0: f32, v1: f32, v2: f32, v3: f32) {
    if (m >= pm.nb) { return; }
    let base = m * pm.rows + n0;
    if (n0 < pm.rows) { ym[base] = v0 * rsm[n0]; }
    if (n0 + 1u < pm.rows) { ym[base + 1u] = v1 * rsm[n0 + 1u]; }
    if (n0 + 2u < pm.rows) { ym[base + 2u] = v2 * rsm[n0 + 2u]; }
    if (n0 + 3u < pm.rows) { ym[base + 3u] = v3 * rsm[n0 + 3u]; }
}

fn q1m_store4(m: u32, n0: u32, v0: f32, v1: f32, v2: f32, v3: f32) {
    if (m >= pm.nb) { return; }
    let base = m * pm.rows + n0;
    if (n0 < pm.rows) { ym[base] = v0; }
    if (n0 + 1u < pm.rows) { ym[base + 1u] = v1; }
    if (n0 + 2u < pm.rows) { ym[base + 2u] = v2; }
    if (n0 + 3u < pm.rows) { ym[base + 3u] = v3; }
}

@compute @workgroup_size(16, 16)
fn q8_mul_mm(@builtin(workgroup_id) wid: vec3<u32>,
             @builtin(local_invocation_id) lid: vec3<u32>) {
    let cols = pm.cols4 * 4u;
    let m0 = wid.y * 64u;
    let n0 = wid.x * 64u;
    let tid = lid.y * 16u + lid.x;
    // Sixteen named scalars, not array<array<f32,4>,4> — see q4t_mul_mm.
    var a00 = 0.0; var a01 = 0.0; var a02 = 0.0; var a03 = 0.0;
    var a10 = 0.0; var a11 = 0.0; var a12 = 0.0; var a13 = 0.0;
    var a20 = 0.0; var a21 = 0.0; var a22 = 0.0; var a23 = 0.0;
    var a30 = 0.0; var a31 = 0.0; var a32 = 0.0; var a33 = 0.0;
    var k0 = 0u;
    loop {
        if (k0 >= cols) { break; }
        // Stage X tile [64×16] (4 f32 per thread) and W tile [64×16]
        // (one u32 = 4 quants per thread per round).
        for (var t = tid; t < 64u * 4u; t = t + 256u) {
            let m = t / 4u;
            let k4 = t % 4u;
            var xv = vec4<f32>(0.0);
            if (m0 + m < pm.nb && (k0 / 4u) + k4 < pm.cols4) {
                let xi = (m0 + m) * cols + k0 + k4 * 4u;
                xv = vec4<f32>(xsm[xi], xsm[xi + 1u], xsm[xi + 2u], xsm[xi + 3u]);
            }
            let dst = m * 16u + k4 * 4u;
            mm_at[dst] = xv.x;
            mm_at[dst + 1u] = xv.y;
            mm_at[dst + 2u] = xv.z;
            mm_at[dst + 3u] = xv.w;
        }
        for (var t = tid; t < 64u * 4u; t = t + 256u) {
            let n = t / 4u;
            let k4 = t % 4u;
            var wv = vec4<f32>(0.0);
            if (n0 + n < pm.rows && (k0 / 4u) + k4 < pm.cols4) {
                wv = i8x4(qm[(n0 + n) * pm.cols4 + (k0 / 4u) + k4]);
            }
            let dst = n * 16u + k4 * 4u;
            mm_wt[dst] = wv.x;
            mm_wt[dst + 1u] = wv.y;
            mm_wt[dst + 2u] = wv.z;
            mm_wt[dst + 3u] = wv.w;
        }
        workgroupBarrier();
        // 4×4 outer-product accumulation over the 16 staged K values.
        let ab = lid.y * 64u;
        let wb = lid.x * 64u;
        for (var k = 0u; k < 16u; k = k + 1u) {
            let x0 = mm_at[ab + k];
            let x1 = mm_at[ab + 16u + k];
            let x2 = mm_at[ab + 32u + k];
            let x3 = mm_at[ab + 48u + k];
            let y0 = mm_wt[wb + k];
            let y1 = mm_wt[wb + 16u + k];
            let y2 = mm_wt[wb + 32u + k];
            let y3 = mm_wt[wb + 48u + k];
            a00 = a00 + x0 * y0; a01 = a01 + x0 * y1;
            a02 = a02 + x0 * y2; a03 = a03 + x0 * y3;
            a10 = a10 + x1 * y0; a11 = a11 + x1 * y1;
            a12 = a12 + x1 * y2; a13 = a13 + x1 * y3;
            a20 = a20 + x2 * y0; a21 = a21 + x2 * y1;
            a22 = a22 + x2 * y2; a23 = a23 + x2 * y3;
            a30 = a30 + x3 * y0; a31 = a31 + x3 * y1;
            a32 = a32 + x3 * y2; a33 = a33 + x3 * y3;
        }
        workgroupBarrier();
        k0 = k0 + 16u;
    }
    let mb = m0 + lid.y * 4u;
    let nb2 = n0 + lid.x * 4u;
    q8_store4(mb, nb2, a00, a01, a02, a03);
    q8_store4(mb + 1u, nb2, a10, a11, a12, a13);
    q8_store4(mb + 2u, nb2, a20, a21, a22, a23);
    q8_store4(mb + 3u, nb2, a30, a31, a32, a33);
}

// Tiled q1 GEMM for wide batches (prefill / speculative K-token decode): the
// q1 twin of q8_mul_mm. Reuses the mul_mm bindings (rsm is unused — q1's scale
// is per-32-group and folded into the staged weight). Decode a 4-wide run of
// weights for one output row: 4 cols in one 32-group share a bit-word + scale;
// bit set → +scale, clear → −scale (XOR the sign bit). cols4 = cols/4, so the
// row has np = cols4/16 six-byte tile-pairs (64 cols each, 2 groups of 32).
fn q1_w4(n: u32, k: u32, np: u32) -> vec4<f32> {
    let pi = k / 64u;
    let off = k % 64u;                 // 4-aligned ⇒ never straddles a 32-group
    let base = n * np * 3u + pi * 3u;
    let a0 = qm[base]; let a1 = qm[base + 1u]; let a2 = qm[base + 2u];
    var bits: u32;
    var scale: f32;
    if (off < 32u) { bits = (a0 >> 16u) | (a1 << 16u); scale = unpack2x16float(a0).x; }
    else           { bits = a2;                        scale = unpack2x16float(a1).y; }
    let bo = off & 31u;
    let m = vec4<u32>(
        (((bits >> bo)        & 1u) ^ 1u) << 31u,
        (((bits >> (bo + 1u)) & 1u) ^ 1u) << 31u,
        (((bits >> (bo + 2u)) & 1u) ^ 1u) << 31u,
        (((bits >> (bo + 3u)) & 1u) ^ 1u) << 31u);
    let sv = vec4<f32>(scale, scale, scale, scale);
    return bitcast<vec4<f32>>(bitcast<vec4<u32>>(sv) ^ m);
}

@compute @workgroup_size(16, 16)
fn q1_mul_mm(@builtin(workgroup_id) wid: vec3<u32>,
             @builtin(local_invocation_id) lid: vec3<u32>) {
    let cols = pm.cols4 * 4u;
    let np = pm.cols4 / 16u;
    let m0 = wid.y * 64u;
    let n0 = wid.x * 64u;
    let tid = lid.y * 16u + lid.x;
    // Sixteen named scalars, not array<array<f32,4>,4> — see q4t_mul_mm.
    var a00 = 0.0; var a01 = 0.0; var a02 = 0.0; var a03 = 0.0;
    var a10 = 0.0; var a11 = 0.0; var a12 = 0.0; var a13 = 0.0;
    var a20 = 0.0; var a21 = 0.0; var a22 = 0.0; var a23 = 0.0;
    var a30 = 0.0; var a31 = 0.0; var a32 = 0.0; var a33 = 0.0;
    var k0 = 0u;
    loop {
        if (k0 >= cols) { break; }
        for (var t = tid; t < 64u * 4u; t = t + 256u) {
            let m = t / 4u;
            let k4 = t % 4u;
            var xv = vec4<f32>(0.0);
            if (m0 + m < pm.nb && (k0 / 4u) + k4 < pm.cols4) {
                let xi = (m0 + m) * cols + k0 + k4 * 4u;
                xv = vec4<f32>(xsm[xi], xsm[xi + 1u], xsm[xi + 2u], xsm[xi + 3u]);
            }
            let dst = m * 16u + k4 * 4u;
            mm_at[dst] = xv.x; mm_at[dst + 1u] = xv.y; mm_at[dst + 2u] = xv.z; mm_at[dst + 3u] = xv.w;
        }
        for (var t = tid; t < 64u * 4u; t = t + 256u) {
            let n = t / 4u;
            let k4 = t % 4u;
            var wv = vec4<f32>(0.0);
            if (n0 + n < pm.rows && (k0 / 4u) + k4 < pm.cols4) {
                wv = q1_w4(n0 + n, k0 + k4 * 4u, np);
            }
            let dst = n * 16u + k4 * 4u;
            mm_wt[dst] = wv.x; mm_wt[dst + 1u] = wv.y; mm_wt[dst + 2u] = wv.z; mm_wt[dst + 3u] = wv.w;
        }
        workgroupBarrier();
        let ab = lid.y * 64u;
        let wb = lid.x * 64u;
        for (var k = 0u; k < 16u; k = k + 1u) {
            let x0 = mm_at[ab + k];
            let x1 = mm_at[ab + 16u + k];
            let x2 = mm_at[ab + 32u + k];
            let x3 = mm_at[ab + 48u + k];
            let y0 = mm_wt[wb + k];
            let y1 = mm_wt[wb + 16u + k];
            let y2 = mm_wt[wb + 32u + k];
            let y3 = mm_wt[wb + 48u + k];
            a00 = a00 + x0 * y0; a01 = a01 + x0 * y1;
            a02 = a02 + x0 * y2; a03 = a03 + x0 * y3;
            a10 = a10 + x1 * y0; a11 = a11 + x1 * y1;
            a12 = a12 + x1 * y2; a13 = a13 + x1 * y3;
            a20 = a20 + x2 * y0; a21 = a21 + x2 * y1;
            a22 = a22 + x2 * y2; a23 = a23 + x2 * y3;
            a30 = a30 + x3 * y0; a31 = a31 + x3 * y1;
            a32 = a32 + x3 * y2; a33 = a33 + x3 * y3;
        }
        workgroupBarrier();
        k0 = k0 + 16u;
    }
    let mb = m0 + lid.y * 4u;
    let nb2 = n0 + lid.x * 4u;
    q1m_store4(mb, nb2, a00, a01, a02, a03);
    q1m_store4(mb + 1u, nb2, a10, a11, a12, a13);
    q1m_store4(mb + 2u, nb2, a20, a21, a22, a23);
    q1m_store4(mb + 3u, nb2, a30, a31, a32, a33);
}

// ── Element-wise kernels of the MoE block (silu·mul·col, axpy, zeroing) ──
struct N1 { n: u32, f: u32, lim: f32, _c: u32 };

@group(0) @binding(0) var<storage, read>       sg   : array<f32>;
@group(0) @binding(1) var<storage, read>       su   : array<f32>;
@group(0) @binding(2) var<storage, read>       scol : array<f32>;
@group(0) @binding(3) var<storage, read_write> sact : array<f32>;
@group(0) @binding(4) var<uniform>             snp  : N1;
@compute @workgroup_size(256)
fn silu_mul_pre(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    if (i >= snp.n) { return; }
    var gv = sg[i];
    var uv = su[i];
    // swiglu_limit, and its asymmetry is the reference's: `up` is clamped on
    // BOTH sides, `gate` only from above. A device that skips it diverges
    // from the CPU path exactly on the tokens that saturate.
    if (snp.lim > 0.0) {
        uv = clamp(uv, -snp.lim, snp.lim);
        gv = min(gv, snp.lim);
    }
    var v = (gv / (1.0 + exp(-gv))) * uv;
    if (snp.f == 1u) { v = v * scol[i]; }
    sact[i] = v;
}

// `set`: y = w·x[soff+i] rather than y += w·x[i]. Two callers wanted the
// assignment and were spending a whole zero-fill dispatch to get it, and one
// wanted a strided source and was spending a copy. `soff` is in floats.
// `asg`, not `set`: `set` is a WGSL reserved keyword, and a shader that
// fails to parse takes the WHOLE module with it — the context then does
// not come up and every op quietly walks the host.
struct AxpyP { w: f32, n: u32, asg: u32, soff: u32 };
@group(0) @binding(0) var<storage, read>       ad : array<f32>;
@group(0) @binding(1) var<storage, read_write> ay : array<f32>;
@group(0) @binding(2) var<uniform>             ap : AxpyP;
@compute @workgroup_size(256)
fn axpy(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    if (i >= ap.n) { return; }
    let v = ap.w * ad[ap.soff + i];
    if (ap.asg != 0u) { ay[i] = v; } else { ay[i] = ay[i] + v; }
}

// The same matvec with 256 threads and four rows to a workgroup.
//
// `f32_matvec` gives a row 64 threads and a workgroup, which for the
// hyper-connection mix — 24 rows over 16 384 columns — is 24 workgroups of
// 64 threads: fifteen hundred threads on a card that holds hundreds of
// thousands. The dispatch itself costs ~3 µs (measured), so the time was
// never the launch; it was the kernel using a rounding error of the machine.
struct F32WP { cols: u32, rows: u32, _a: u32, _b: u32 };
@group(0) @binding(0) var<storage, read>       fww : array<f32>;
@group(0) @binding(1) var<storage, read>       fwx : array<f32>;
@group(0) @binding(2) var<storage, read_write> fwy : array<f32>;
@group(0) @binding(3) var<uniform>             fwp : F32WP;
var<workgroup> fwpart: array<f32, 256>;
@compute @workgroup_size(256)
fn f32_matvec_w(@builtin(workgroup_id) wid: vec3<u32>,
                @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    if (row >= fwp.rows) { return; }
    let base = row * fwp.cols;
    var acc = 0.0;
    var i = lid;
    loop {
        if (i >= fwp.cols) { break; }
        acc = acc + fww[base + i] * fwx[i];
        i = i + 256u;
    }
    fwpart[lid] = acc;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { fwpart[lid] = fwpart[lid] + fwpart[lid + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    if (lid == 0u) { fwy[row] = fwpart[0]; }
}

// The same again with a thousand threads, for the shapes with FEW rows. The
// hyper-connection mix is 24 rows over 16 384 columns: at 256 threads that
// is 24 workgroups of 256, six thousand threads. Rows are what give this
// kernel its workgroups, so when rows are scarce the only width left is
// inside one.

@group(0) @binding(0) var<storage, read>       fxw : array<f32>;
@group(0) @binding(1) var<storage, read>       fxx : array<f32>;
@group(0) @binding(2) var<storage, read_write> fxy : array<f32>;
@group(0) @binding(3) var<uniform>             fxp : F32WP;
var<workgroup> fxpart: array<f32, 1024>;
@compute @workgroup_size(1024)
fn f32_matvec_x(@builtin(workgroup_id) wid: vec3<u32>,
                @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    if (row >= fxp.rows) { return; }
    let base = row * fxp.cols;
    var acc = 0.0;
    var i = lid;
    loop {
        if (i >= fxp.cols) { break; }
        acc = acc + fxw[base + i] * fxx[i];
        i = i + 1024u;
    }
    fxpart[lid] = acc;
    workgroupBarrier();
    var stride = 512u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { fxpart[lid] = fxpart[lid] + fxpart[lid + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    if (lid == 0u) { fxy[row] = fxpart[0]; }
}

// The f32 matvec split over COLUMNS, for the shapes with almost no rows.
//
// Rows are what give this kernel its workgroups, and the hyper-connection
// mix has 24 of them over 16 384 columns: even at 1024 threads that is
// twenty-four workgroups — six per cent of a card that holds hundreds of
// thousands of threads. Splitting the shared axis gives rows·splits
// workgroups and a cheap merge, the same trade the attention split makes.
struct F32SP { cols: u32, rows: u32, nsplit: u32, chunk: u32 };
@group(0) @binding(0) var<storage, read>       fmsw : array<f32>;
@group(0) @binding(1) var<storage, read>       fmsx : array<f32>;
@group(0) @binding(2) var<storage, read_write> fmsy : array<f32>;
@group(0) @binding(3) var<uniform>             fmsp : F32SP;
@group(0) @binding(4) var<storage, read_write> fmspart : array<f32>;  // rows*nsplit
var<workgroup> fmsred: array<f32, 256>;

@compute @workgroup_size(256)
fn f32_matvec_split(@builtin(workgroup_id) wid: vec3<u32>,
                    @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let sp = wid.y;
    if (row >= fmsp.rows || sp >= fmsp.nsplit) { return; }
    let lo = sp * fmsp.chunk;
    var hi = lo + fmsp.chunk;
    if (hi > fmsp.cols) { hi = fmsp.cols; }
    let base = row * fmsp.cols;
    var acc = 0.0;
    var i = lo + lid;
    loop {
        if (i >= hi) { break; }
        acc = acc + fmsw[base + i] * fmsx[i];
        i = i + 256u;
    }
    fmsred[lid] = acc;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { fmsred[lid] = fmsred[lid] + fmsred[lid + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    if (lid == 0u) { fmspart[row * fmsp.nsplit + sp] = fmsred[0]; }
    // Same reason as in the merge: this entry point never writes the output,
    // so without a mention its derived layout is one binding short of the
    // merge's and no single bind group can serve both. Never runs.
    if (fmsp.rows == 0xFFFFFFFFu) { fmsy[0] = 0.0; }
}

@compute @workgroup_size(64)
fn f32_matvec_merge(@builtin(workgroup_id) wid: vec3<u32>,
                    @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    if (row >= fmsp.rows) { return; }
    // The splits are summed IN ORDER by one lane: the partials are few and
    // a fixed order keeps the answer reproducible.
    if (lid == 0u) {
        var s = 0.0;
        for (var t = 0u; t < fmsp.nsplit; t = t + 1u) {
            s = s + fmspart[row * fmsp.nsplit + t];
        }
        fmsy[row] = s;
    }
    // A layout is derived from the bindings an ENTRY POINT uses, not from
    // the module's globals — so without this the merge's layout has fewer
    // bindings than the split's and one bind group cannot serve both.
    // The branch never runs.
    if (fmsp.rows == 0xFFFFFFFFu) { fmsy[0] = fmsw[0] + fmsx[0]; }
}

// Qwen3.5 output gate: attn_out *= sigmoid(gate), element-wise over nh·hd.
@group(0) @binding(0) var<storage, read>       gm_g : array<f32>;
@group(0) @binding(1) var<storage, read_write> gm_o : array<f32>;
@group(0) @binding(2) var<uniform>             gm_p : N1;
@compute @workgroup_size(256)
fn gate_mul(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    if (i >= gm_p.n) { return; }
    gm_o[i] = gm_o[i] * (1.0 / (1.0 + exp(-gm_g[i])));
}

@group(0) @binding(0) var<storage, read_write> zy  : array<f32>;
@group(0) @binding(1) var<uniform>             znp : N1;
@compute @workgroup_size(256)
fn fill_zero(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    if (i < znp.n) { zy[i] = 0.0; }
}

// silu(g)·u in place on g — the glue pass of the fused imagegen FFN
// (w1/w3/silu/w2 in one submission, one readback).
@group(0) @binding(0) var<storage, read_write> fsg : array<f32>;
@group(0) @binding(1) var<storage, read>       fsu : array<f32>;
@group(0) @binding(2) var<uniform>             fsp : N1;
@compute @workgroup_size(256)
fn ffn_silu_mul(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    if (i >= fsp.n) { return; }
    let g = fsg[i];
    fsg[i] = (g / (1.0 + exp(-g))) * fsu[i];
}

// Plain f32 matvec (for small unquantized projections like GDN in_proj_a/b):
// y[o] = Σ_i W[o,i]·x[i]. One workgroup per output row.
struct F32P { cols: u32, rows: u32, _a: u32, _b: u32 };
@group(0) @binding(0) var<storage, read>       f32w : array<f32>;
@group(0) @binding(1) var<storage, read>       f32x : array<f32>;
@group(0) @binding(2) var<storage, read_write> f32y : array<f32>;
@group(0) @binding(3) var<uniform>             f32p : F32P;
var<workgroup> f32part: array<f32, 64>;
@compute @workgroup_size(64)
fn f32_matvec(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    if (row >= f32p.rows) { return; }
    let base = row * f32p.cols;
    var acc = 0.0;
    var i = lid;
    loop {
        if (i >= f32p.cols) { break; }
        acc = acc + f32w[base + i] * f32x[i];
        i = i + 64u;
    }
    f32part[lid] = acc;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { f32part[lid] = f32part[lid] + f32part[lid + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    if (lid == 0u) { f32y[row] = f32part[0]; }
}

// ── Two independent projections of the SAME input, in ONE dispatch.
//
// A GDN layer projects its input four ways (qkv, z, a, b); a MoE layer
// projects it twice (router, shared gate). Those are independent, but
// dispatches inside a compute pass are serialized by wgpu's
// memory-visibility guarantee — so each costs a full launch (~29 µs on
// this Vulkan stack) for work the card could overlap. Measured on a
// 5090, the 40-layer decode spends 15.3 of its 16.4 ms in
// submit+readback across ~520 dispatches, so collapsing pairs is worth
// more than any arithmetic in the kernels.
//
// The two jobs are laid end-to-end in one row space: workgroup r < rows0
// is job 0, the rest is job 1. Whole workgroups branch together, so the
// per-kind branch never diverges within a workgroup. Kinds: 4 = f32,
// 6 = q4tp; the caller keeps the unfused path for anything else.
struct MvP2 {
    rows0: u32, cols0: u32, kind0: u32, _pa: u32,
    rows1: u32, cols1: u32, kind1: u32, _pb: u32,
};
@group(0) @binding(0) var<storage, read>       m2w0 : array<u32>;
@group(0) @binding(1) var<storage, read>       m2w1 : array<u32>;
@group(0) @binding(2) var<storage, read>       m2x  : array<f32>;
@group(0) @binding(3) var<storage, read_write> m2y0 : array<f32>;
@group(0) @binding(4) var<storage, read_write> m2y1 : array<f32>;
@group(0) @binding(5) var<uniform>             m2p  : MvP2;
var<workgroup> m2part: array<f32, 64>;
var<workgroup> m2lad:  array<f32, 32>;

fn m2_dot8(w: u32, xi: u32) -> f32 {
    return (f32(w & 0xFu) - 8.0) * m2x[xi]
         + (f32((w >> 4u) & 0xFu) - 8.0) * m2x[xi + 1u]
         + (f32((w >> 8u) & 0xFu) - 8.0) * m2x[xi + 2u]
         + (f32((w >> 12u) & 0xFu) - 8.0) * m2x[xi + 3u]
         + (f32((w >> 16u) & 0xFu) - 8.0) * m2x[xi + 4u]
         + (f32((w >> 20u) & 0xFu) - 8.0) * m2x[xi + 5u]
         + (f32((w >> 24u) & 0xFu) - 8.0) * m2x[xi + 6u]
         + (f32((w >> 28u) & 0xFu) - 8.0) * m2x[xi + 7u];
}

// One partial dot over job 0's weights. Split per buffer because WGSL has
// no way to pick a binding at runtime.
fn m2_part0(kind: u32, row: u32, cols: u32, lid: u32) -> f32 {
    var acc = 0.0;
    if (kind == 4u) {
        let base = row * cols;
        var i = lid;
        loop {
            if (i >= cols) { break; }
            acc = acc + bitcast<f32>(m2w0[base + i]) * m2x[i];
            i = i + 64u;
        }
        return acc;
    }
    let gpr = cols / 32u;
    let rows = m2p.rows0;
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    if (lid < 32u) {
        let pr = unpack2x16float(m2w0[params_w + row]);
        m2lad[lid] = exp2(pr.x + f32(lid) * pr.y);
    }
    workgroupBarrier();
    var g = lid;
    loop {
        if (g >= gpr) { break; }
        let bit = g * 5u;
        let cb = codes_b + row * cstride + (bit >> 3u);
        let sh = bit & 7u;
        var cv = (m2w0[cb >> 2u] >> ((cb & 3u) * 8u)) & 0xFFu;
        if (sh > 3u) {
            let cb1 = cb + 1u;
            cv = cv | (((m2w0[cb1 >> 2u] >> ((cb1 & 3u) * 8u)) & 0xFFu) << 8u);
        }
        let scale = m2lad[(cv >> sh) & 31u];
        let base = (row * gpr + g) * 4u;
        let xb = g * 32u;
        var gs = 0.0;
        for (var k = 0u; k < 4u; k = k + 1u) {
            gs = gs + m2_dot8(m2w0[base + k], xb + 8u * k);
        }
        acc = acc + scale * gs;
        g = g + 64u;
    }
    return acc;
}

fn m2_part1(kind: u32, row: u32, cols: u32, lid: u32) -> f32 {
    var acc = 0.0;
    if (kind == 4u) {
        let base = row * cols;
        var i = lid;
        loop {
            if (i >= cols) { break; }
            acc = acc + bitcast<f32>(m2w1[base + i]) * m2x[i];
            i = i + 64u;
        }
        return acc;
    }
    let gpr = cols / 32u;
    let rows = m2p.rows1;
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    if (lid < 32u) {
        let pr = unpack2x16float(m2w1[params_w + row]);
        m2lad[lid] = exp2(pr.x + f32(lid) * pr.y);
    }
    workgroupBarrier();
    var g = lid;
    loop {
        if (g >= gpr) { break; }
        let bit = g * 5u;
        let cb = codes_b + row * cstride + (bit >> 3u);
        let sh = bit & 7u;
        var cv = (m2w1[cb >> 2u] >> ((cb & 3u) * 8u)) & 0xFFu;
        if (sh > 3u) {
            let cb1 = cb + 1u;
            cv = cv | (((m2w1[cb1 >> 2u] >> ((cb1 & 3u) * 8u)) & 0xFFu) << 8u);
        }
        let scale = m2lad[(cv >> sh) & 31u];
        let base = (row * gpr + g) * 4u;
        let xb = g * 32u;
        var gs = 0.0;
        for (var k = 0u; k < 4u; k = k + 1u) {
            gs = gs + m2_dot8(m2w1[base + k], xb + 8u * k);
        }
        acc = acc + scale * gs;
        g = g + 64u;
    }
    return acc;
}

@compute @workgroup_size(64)
fn matvec_pair(@builtin(workgroup_id) wid: vec3<u32>,
               @builtin(num_workgroups) nwg: vec3<u32>,
               @builtin(local_invocation_index) lid: u32) {
    let total = m2p.rows0 + m2p.rows1;
    var flat = wid.x;
    loop {
        if (flat >= total) { break; }
        var acc = 0.0;
        let second = flat >= m2p.rows0;
        if (second) {
            acc = m2_part1(m2p.kind1, flat - m2p.rows0, m2p.cols1, lid);
        } else {
            acc = m2_part0(m2p.kind0, flat, m2p.cols0, lid);
        }
        m2part[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { m2part[lid] = m2part[lid] + m2part[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) {
            if (second) { m2y1[flat - m2p.rows0] = m2part[0]; }
            else { m2y0[flat] = m2part[0]; }
        }
        // The next iteration rewrites m2lad/m2part; make sure every thread
        // is done reading them first.
        workgroupBarrier();
        flat = flat + nwg.x;
    }
}

// f32 matvec with a token axis for the batch graph: wid.y = token, and
// the PER-ROW math is f32_matvec verbatim — same lane stride, same tree
// reduction — so the logits it produces are bit-identical to k separate
// dispatches of the single-token kernel. That equivalence is what lets
// the batch prefill's router and GDN a/b projections collapse from one
// dispatch per token per layer (~3200 a chunk) to one per layer.
struct F32BP { cols: u32, rows: u32, _a: u32, _b: u32 };
@group(0) @binding(0) var<storage, read>       fb_w : array<f32>;
@group(0) @binding(1) var<storage, read>       fb_x : array<f32>;
@group(0) @binding(2) var<storage, read_write> fb_y : array<f32>;
@group(0) @binding(3) var<uniform>             fb_p : F32BP;
var<workgroup> fb_part: array<f32, 64>;
@compute @workgroup_size(64)
fn f32_matvec_b(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let t = wid.y;
    if (row >= fb_p.rows) { return; }
    let base = row * fb_p.cols;
    let xoff = t * fb_p.cols;
    var acc = 0.0;
    var i = lid;
    loop {
        if (i >= fb_p.cols) { break; }
        acc = acc + fb_w[base + i] * fb_x[xoff + i];
        i = i + 64u;
    }
    fb_part[lid] = acc;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { fb_part[lid] = fb_part[lid] + fb_part[lid + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    if (lid == 0u) { fb_y[t * fb_p.rows + row] = fb_part[0]; }
}

// RMSNorm of one row (WGSL twin of Metal rmsnorm_k): o = x·rsqrt(mean(x²)+eps)·w',
// w' = w or (1+w) for gemma. One workgroup, 256-thread tree reduction — the
// building block that keeps the token graph's hidden resident across the norm.
struct RmsP { n: u32, gemma: u32, eps: f32, _p: u32 };
@group(0) @binding(0) var<storage, read>       rn_x : array<f32>;
@group(0) @binding(1) var<storage, read>       rn_w : array<f32>;
@group(0) @binding(2) var<storage, read_write> rn_o : array<f32>;
@group(0) @binding(3) var<uniform>             rn_p : RmsP;
// A THOUSAND threads, not 256. This kernel reduces, so it is one workgroup
// by construction — one SM of two hundred — and a dispatch costs 2.7 µs
// while this one takes ~23. There is nothing to hide the load latency
// behind except more threads on the same SM.
var<workgroup> rn_part: array<f32, 1024>;
@compute @workgroup_size(1024)
fn rmsnorm(@builtin(local_invocation_id) lid: vec3<u32>) {
    let tid = lid.x;
    let n = rn_p.n;
    var acc = 0.0;
    var i = tid;
    loop {
        if (i >= n) { break; }
        let v = rn_x[i];
        acc = acc + v * v;
        i = i + 1024u;
    }
    rn_part[tid] = acc;
    workgroupBarrier();
    var stride = 512u;
    loop {
        if (stride == 0u) { break; }
        if (tid < stride) { rn_part[tid] = rn_part[tid] + rn_part[tid + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let inv = inverseSqrt(rn_part[0] / f32(n) + rn_p.eps);
    i = tid;
    loop {
        if (i >= n) { break; }
        var wv = rn_w[i];
        if (rn_p.gemma == 1u) { wv = 1.0 + wv; }
        rn_o[i] = rn_x[i] * inv * wv;
        i = i + 1024u;
    }
}

// GDN depthwise causal conv + SiLU over the ring buffer of the last kk-1
// positions plus the current qkv, then shift the ring (drop oldest, append
// current). One thread per conv channel. WGSL twin of the Metal gdn_conv.
struct GcP { cdim: u32, kk: u32, xoff: u32, _b: u32 };
@group(0) @binding(0) var<storage, read>       gc_qkv  : array<f32>;
@group(0) @binding(1) var<storage, read>       gc_taps : array<f32>;
@group(0) @binding(2) var<storage, read_write> gc_ring : array<f32>;
@group(0) @binding(3) var<storage, read_write> gc_cq   : array<f32>;
@group(0) @binding(4) var<uniform>             gc_p    : GcP;
@compute @workgroup_size(256)
fn gdn_conv(@builtin(global_invocation_id) gid: vec3<u32>) {
    let c = gid.x;
    let cdim = gc_p.cdim;
    if (c >= cdim) { return; }
    let kk = gc_p.kk;
    let tb = c * kk;
    var acc = gc_qkv[gc_p.xoff + c] * gc_taps[tb + kk - 1u];
    for (var j = 0u; j + 1u < kk; j = j + 1u) {
        acc = acc + gc_ring[j * cdim + c] * gc_taps[tb + j];
    }
    gc_cq[c] = acc / (1.0 + exp(-acc));
    // ring shift (columns are independent per thread c)
    for (var j = 0u; j + 2u < kk; j = j + 1u) {
        gc_ring[j * cdim + c] = gc_ring[(j + 1u) * cdim + c];
    }
    if (kk > 1u) {
        gc_ring[(kk - 2u) * cdim + c] = gc_qkv[gc_p.xoff + c];
    }
}

// ── GDN (gated DeltaNet / linear attention) decode step ──────────────────
// One workgroup per v-head. From the conv output cq it l2-norms q/k, forms the
// decay g and gate β, runs the delta-rule state recurrence S ← g·S + kf⊗β(v −
// kfᵀS) with o = qfᵀS, then the gated RMSNorm o·norm·silu(z). S ([nv,dk,dv])
// persists across tokens (device state buffer). WGSL twin of the Metal GDN
// state-update kernel; dk,dv ≤ 256.
struct GdnP { nv: u32, dk: u32, dv: u32, kd: u32, rep: u32, cdim: u32, eps: f32, tok: u32 };
@group(0) @binding(0) var<storage, read>       gd_cq   : array<f32>;
@group(0) @binding(1) var<storage, read>       gd_z    : array<f32>;
@group(0) @binding(2) var<storage, read>       gd_a    : array<f32>;
@group(0) @binding(3) var<storage, read>       gd_b    : array<f32>;
@group(0) @binding(4) var<storage, read>       gd_alog : array<f32>;
@group(0) @binding(5) var<storage, read>       gd_dtb  : array<f32>;
@group(0) @binding(6) var<storage, read>       gd_norm : array<f32>;
@group(0) @binding(7) var<storage, read_write> gd_S    : array<f32>;
@group(0) @binding(8) var<storage, read_write> gd_o    : array<f32>;
// S and o again as vec4 (same-slot rule): a state row is dv-contiguous,
// so a lane's four columns are ONE 16-byte access, not four scattered.
@group(0) @binding(7) var<storage, read_write> gd_S4   : array<vec4<f32>>;
@group(0) @binding(8) var<storage, read_write> gd_o4   : array<vec4<f32>>;
@group(0) @binding(9) var<uniform>             gd_p    : GdnP;
var<workgroup> gd_kf: array<f32, 256>;
var<workgroup> gd_qf: array<f32, 256>;
var<workgroup> gd_ov: array<f32, 256>;
var<workgroup> gd_red: array<f32, 256>;
var<workgroup> gd_red2: array<f32, 256>;
var<workgroup> gd_red3: array<f32, 256>;
var<workgroup> gd_red4: array<f32, 256>;
fn gd_softplus(x: f32) -> f32 {
    if (x > 20.0) { return x; }
    return log(1.0 + exp(x));
}
fn gd_reduce(t: u32) -> f32 {
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (t < stride) { gd_red[t] = gd_red[t] + gd_red[t + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    return gd_red[0];
}
@compute @workgroup_size(256)
fn gdn_step(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let t = lid.x;
    if (h >= gd_p.nv) { return; }
    let dk = gd_p.dk;
    let dv = gd_p.dv;
    let ko = h / gd_p.rep;
    let qs = ko * dk;
    let ks = gd_p.kd + ko * dk;
    // l2-norm of q then k over dk
    gd_red[t] = select(0.0, gd_cq[qs + t] * gd_cq[qs + t], t < dk);
    workgroupBarrier();
    let nq = gd_reduce(t);
    workgroupBarrier();
    gd_red[t] = select(0.0, gd_cq[ks + t] * gd_cq[ks + t], t < dk);
    workgroupBarrier();
    let nkn = gd_reduce(t);
    workgroupBarrier();
    let invq = 1.0 / (sqrt(nq + 1e-6) * sqrt(f32(dk)));
    let invk = 1.0 / sqrt(nkn + 1e-6);
    if (t < dk) {
        gd_qf[t] = gd_cq[qs + t] * invq;
        gd_kf[t] = gd_cq[ks + t] * invk;
    }
    workgroupBarrier();
    let abo = gd_p.tok * gd_p.nv;
    let g = exp(-exp(gd_alog[h]) * gd_softplus(gd_a[abo + h] + gd_dtb[h]));
    let beta = 1.0 / (1.0 + exp(-gd_b[abo + h]));
    let sbase = h * dk * dv;
    if (t < dv) {
        let dj = t;
        let vt = gd_cq[2u * gd_p.kd + h * dv + dj];
        var kv = 0.0;
        for (var di = 0u; di < dk; di = di + 1u) { kv = kv + gd_S[sbase + di * dv + dj] * gd_kf[di]; }
        let delta = (vt - g * kv) * beta;
        var o = 0.0;
        for (var di = 0u; di < dk; di = di + 1u) {
            let idx = sbase + di * dv + dj;
            let cell = g * gd_S[idx] + gd_kf[di] * delta;
            gd_S[idx] = cell;
            o = o + gd_qf[di] * cell;
        }
        gd_ov[dj] = o;
    }
    workgroupBarrier();
    // gated RMSNorm over dv
    gd_red[t] = select(0.0, gd_ov[t] * gd_ov[t], t < dv);
    workgroupBarrier();
    let ss = gd_reduce(t);
    workgroupBarrier();
    let inv = 1.0 / sqrt(ss / f32(dv) + gd_p.eps);
    if (t < dv) {
        let zo = gd_p.tok * gd_p.nv * dv;
        let zz = gd_z[zo + h * dv + t];
        gd_o[zo + h * dv + t] = gd_ov[t] * inv * gd_norm[t] * (zz / (1.0 + exp(-zz)));
    }
}

// ── GDN, k-looped twins for the batched verify: the position recurrence
// stays INSIDE the kernel, so a layer is two dispatches with one barrier
// between them instead of 2k dispatches with 2k barrier drains — which
// were 7-8 ms of a 3-position verify. Columns are independent in the
// conv (no barriers at all); heads are independent in the step, so the
// loop lives inside the workgroup. When snap_stride > 0 each position's
// (ring, S) lands in the snapshot buffer as it is produced — the rows a
// partial acceptance restores from, at zero extra passes.
struct GcKP { cdim: u32, kk: u32, kb: u32, snap: u32, stride: u32, p0: u32, p1: u32, p2: u32 };
@group(0) @binding(0) var<storage, read>       gck_qkv : array<f32>;
@group(0) @binding(1) var<storage, read>       gck_taps: array<f32>;
@group(0) @binding(2) var<storage, read_write> gck_ring: array<f32>;
@group(0) @binding(3) var<storage, read_write> gck_cq  : array<f32>;
@group(0) @binding(4) var<uniform>             gck_p   : GcKP;
@group(0) @binding(5) var<storage, read_write> gck_snap: array<f32>;
@compute @workgroup_size(256)
fn gdn_conv_k(@builtin(global_invocation_id) gid: vec3<u32>) {
    let c = gid.x;
    let cdim = gck_p.cdim;
    if (c >= cdim) { return; }
    let kk = gck_p.kk;
    let tb = c * kk;
    for (var i = 0u; i < gck_p.kb; i = i + 1u) {
        let x = gck_qkv[i * cdim + c];
        var acc = x * gck_taps[tb + kk - 1u];
        for (var j = 0u; j + 1u < kk; j = j + 1u) {
            acc = acc + gck_ring[j * cdim + c] * gck_taps[tb + j];
        }
        gck_cq[i * cdim + c] = acc / (1.0 + exp(-acc));
        for (var j = 0u; j + 2u < kk; j = j + 1u) {
            gck_ring[j * cdim + c] = gck_ring[(j + 1u) * cdim + c];
        }
        if (kk > 1u) {
            gck_ring[(kk - 2u) * cdim + c] = x;
        }
        if (gck_p.snap != 0u) {
            let off = i * gck_p.stride;
            for (var j = 0u; j + 1u < kk; j = j + 1u) {
                gck_snap[off + j * cdim + c] = gck_ring[j * cdim + c];
            }
        }
    }
}

struct GdKP {
    nv: u32, dk: u32, dv: u32, kd: u32,
    rep: u32, cdim: u32, eps: f32, kb: u32,
    stride: u32, ring_els: u32, p0: u32, p1: u32,
};
@group(0) @binding(0) var<storage, read>       gdk_cq   : array<f32>;
@group(0) @binding(1) var<storage, read>       gdk_z    : array<f32>;
@group(0) @binding(2) var<storage, read>       gdk_a    : array<f32>;
@group(0) @binding(3) var<storage, read>       gdk_b    : array<f32>;
@group(0) @binding(4) var<storage, read>       gdk_alog : array<f32>;
@group(0) @binding(5) var<storage, read>       gdk_dtb  : array<f32>;
@group(0) @binding(6) var<storage, read>       gdk_norm : array<f32>;
@group(0) @binding(7) var<storage, read_write> gdk_S    : array<f32>;
@group(0) @binding(8) var<storage, read_write> gdk_o    : array<f32>;
@group(0) @binding(9) var<uniform>             gdk_p    : GdKP;
@group(0) @binding(10) var<storage, read_write> gdk_snap: array<f32>;
var<workgroup> gdk_kf: array<f32, 256>;
var<workgroup> gdk_qf: array<f32, 256>;
var<workgroup> gdk_ov: array<f32, 256>;
var<workgroup> gdk_red: array<f32, 256>;
fn gdk_reduce(t: u32) -> f32 {
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (t < stride) { gdk_red[t] = gdk_red[t] + gdk_red[t + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    return gdk_red[0];
}
@compute @workgroup_size(256)
fn gdn_step_k(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let t = lid.x;
    if (h >= gdk_p.nv) { return; }
    let dk = gdk_p.dk;
    let dv = gdk_p.dv;
    let ko = h / gdk_p.rep;
    let sbase = h * dk * dv;
    for (var i = 0u; i < gdk_p.kb; i = i + 1u) {
        let cq0 = i * gdk_p.cdim;
        let qs = cq0 + ko * dk;
        let ks = cq0 + gdk_p.kd + ko * dk;
        gdk_red[t] = select(0.0, gdk_cq[qs + t] * gdk_cq[qs + t], t < dk);
        workgroupBarrier();
        let nq = gdk_reduce(t);
        workgroupBarrier();
        gdk_red[t] = select(0.0, gdk_cq[ks + t] * gdk_cq[ks + t], t < dk);
        workgroupBarrier();
        let nkn = gdk_reduce(t);
        workgroupBarrier();
        let invq = 1.0 / (sqrt(nq + 1e-6) * sqrt(f32(dk)));
        let invk = 1.0 / sqrt(nkn + 1e-6);
        if (t < dk) {
            gdk_qf[t] = gdk_cq[qs + t] * invq;
            gdk_kf[t] = gdk_cq[ks + t] * invk;
        }
        workgroupBarrier();
        let abo = i * gdk_p.nv;
        let g = exp(-exp(gdk_alog[h]) * gd_softplus(gdk_a[abo + h] + gdk_dtb[h]));
        let beta = 1.0 / (1.0 + exp(-gdk_b[abo + h]));
        if (t < dv) {
            let dj = t;
            let vt = gdk_cq[cq0 + 2u * gdk_p.kd + h * dv + dj];
            var kv = 0.0;
            for (var di = 0u; di < dk; di = di + 1u) {
                kv = kv + gdk_S[sbase + di * dv + dj] * gdk_kf[di];
            }
            let delta = (vt - g * kv) * beta;
            var o = 0.0;
            let snap0 = i * gdk_p.stride + gdk_p.ring_els + sbase;
            for (var di = 0u; di < dk; di = di + 1u) {
                let idx = sbase + di * dv + dj;
                let cell = g * gdk_S[idx] + gdk_kf[di] * delta;
                gdk_S[idx] = cell;
                if (gdk_p.stride != 0u) {
                    gdk_snap[snap0 + di * dv + dj] = cell;
                }
                o = o + gdk_qf[di] * cell;
            }
            gdk_ov[dj] = o;
        }
        workgroupBarrier();
        gdk_red[t] = select(0.0, gdk_ov[t] * gdk_ov[t], t < dv);
        workgroupBarrier();
        let ss = gdk_reduce(t);
        workgroupBarrier();
        let inv = 1.0 / sqrt(ss / f32(dv) + gdk_p.eps);
        if (t < dv) {
            let zo = i * gdk_p.nv * dv;
            let zz = gdk_z[zo + h * dv + t];
            gdk_o[zo + h * dv + t] = gdk_ov[t] * inv * gdk_norm[t] * (zz / (1.0 + exp(-zz)));
        }
        workgroupBarrier();
    }
}

// ── GDN step, parallel edition: one WORKGROUP PER (head, column). The
// one-workgroup-per-head kernel put 32 workgroups on a 188-SM card — 8%
// occupancy, 3.7 ms/token of a 12.5 ms frame on the 2-bit 35B. Column j
// is independent under the delta rule, and both dk-loops become 128-lane
// tree reductions. Reduction order differs from the serial kernel, so
// bits differ within the documented GPU tie class; CMF_GDN_PAR=0 keeps
// the old kernel for A/B. The raw o lands in gd_o and gdn_step_norm
// applies the gated RMSNorm in place.
@compute @workgroup_size(128)
fn gdn_step_par(@builtin(workgroup_id) wid: vec3<u32>,
                @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let dj4 = wid.y;   // FOUR columns per workgroup, vec4 access
    let t = lid.x;
    let dk = gd_p.dk;
    let dv = gd_p.dv;
    if (h >= gd_p.nv || dj4 * 4u >= dv) { return; }
    let ko = h / gd_p.rep;
    let qs = ko * dk;
    let ks = gd_p.kd + ko * dk;
    // q/k l2 norms over dk (identical formulas, tree order)
    gd_red[t] = select(0.0, gd_cq[qs + t] * gd_cq[qs + t], t < dk);
    workgroupBarrier();
    var stride = 64u;
    loop {
        if (stride == 0u) { break; }
        if (t < stride) { gd_red[t] = gd_red[t] + gd_red[t + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let nq = gd_red[0];
    workgroupBarrier();
    gd_red[t] = select(0.0, gd_cq[ks + t] * gd_cq[ks + t], t < dk);
    workgroupBarrier();
    stride = 64u;
    loop {
        if (stride == 0u) { break; }
        if (t < stride) { gd_red[t] = gd_red[t] + gd_red[t + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let nkn = gd_red[0];
    workgroupBarrier();
    let invq = 1.0 / (sqrt(nq + 1e-6) * sqrt(f32(dk)));
    let invk = 1.0 / sqrt(nkn + 1e-6);
    let abo = gd_p.tok * gd_p.nv;
    let g = exp(-exp(gd_alog[h]) * gd_softplus(gd_a[abo + h] + gd_dtb[h]));
    let beta = 1.0 / (1.0 + exp(-gd_b[abo + h]));
    let s4base = (h * dk * dv) >> 2u;
    let dv4 = dv >> 2u;
    let vto = 2u * gd_p.kd + h * dv + dj4 * 4u;
    let vt = vec4<f32>(gd_cq[vto], gd_cq[vto + 1u], gd_cq[vto + 2u], gd_cq[vto + 3u]);
    let kf_t = select(0.0, gd_cq[ks + t] * invk, t < dk);
    let qf_t = select(0.0, gd_cq[qs + t] * invq, t < dk);
    // kv = kfᵀ S[:, j..j+3] — the four column reductions ride together
    var kv4 = vec4<f32>(0.0);
    if (t < dk) {
        kv4 = gd_S4[s4base + t * dv4 + dj4] * kf_t;
    }
    gd_red[t] = kv4.x;
    gd_red2[t] = kv4.y;
    gd_red3[t] = kv4.z;
    gd_red4[t] = kv4.w;
    workgroupBarrier();
    stride = 64u;
    loop {
        if (stride == 0u) { break; }
        if (t < stride) {
            gd_red[t] = gd_red[t] + gd_red[t + stride];
            gd_red2[t] = gd_red2[t] + gd_red2[t + stride];
            gd_red3[t] = gd_red3[t] + gd_red3[t + stride];
            gd_red4[t] = gd_red4[t] + gd_red4[t + stride];
        }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let kv = vec4<f32>(gd_red[0], gd_red2[0], gd_red3[0], gd_red4[0]);
    workgroupBarrier();
    let delta = (vt - g * kv) * beta;
    var contrib = vec4<f32>(0.0);
    if (t < dk) {
        let idx = s4base + t * dv4 + dj4;
        let cell = g * gd_S4[idx] + kf_t * delta;
        gd_S4[idx] = cell;
        contrib = qf_t * cell;
    }
    gd_red[t] = contrib.x;
    gd_red2[t] = contrib.y;
    gd_red3[t] = contrib.z;
    gd_red4[t] = contrib.w;
    workgroupBarrier();
    stride = 64u;
    loop {
        if (stride == 0u) { break; }
        if (t < stride) {
            gd_red[t] = gd_red[t] + gd_red[t + stride];
            gd_red2[t] = gd_red2[t] + gd_red2[t + stride];
            gd_red3[t] = gd_red3[t] + gd_red3[t + stride];
            gd_red4[t] = gd_red4[t] + gd_red4[t + stride];
        }
        workgroupBarrier();
        stride = stride / 2u;
    }
    if (t == 0u) {
        let zo4 = (gd_p.tok * gd_p.nv * dv) >> 2u;
        gd_o4[zo4 + h * dv4 + dj4] =
            vec4<f32>(gd_red[0], gd_red2[0], gd_red3[0], gd_red4[0]);
    }
}

// v2 of the parallel pair: the conv is INLINE (the same taps math, the
// same order, computed per element from the PRE-shift ring), so the par
// kernel no longer waits on a conv dispatch — and the ring shift rides
// the norm kernel, which was going to run anyway. One dependent hop
// fewer per GDN layer, thirty layers a frame.
struct GciP { kk: u32, xoff: u32, _a: u32, _b: u32 };
@group(0) @binding(10) var<storage, read>       gi_qkv  : array<f32>;
@group(0) @binding(11) var<storage, read_write> gi_ring : array<f32>;
@group(0) @binding(12) var<storage, read>       gi_taps : array<f32>;
@group(0) @binding(13) var<uniform>             gi_p    : GciP;

fn gi_cq(c: u32) -> f32 {
    let kk = gi_p.kk;
    let tb = c * kk;
    var acc = gi_qkv[gi_p.xoff + c] * gi_taps[tb + kk - 1u];
    for (var j = 0u; j + 1u < kk; j = j + 1u) {
        acc = acc + gi_ring[j * gd_p.cdim + c] * gi_taps[tb + j];
    }
    return acc / (1.0 + exp(-acc));
}

@compute @workgroup_size(128)
fn gdn_step_par2(@builtin(workgroup_id) wid: vec3<u32>,
                 @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let dj = wid.y;
    let t = lid.x;
    let dk = gd_p.dk;
    let dv = gd_p.dv;
    if (h >= gd_p.nv || dj >= dv) { return; }
    let ko = h / gd_p.rep;
    let qs = ko * dk;
    let ks = gd_p.kd + ko * dk;
    let cq_q = select(0.0, gi_cq(qs + t), t < dk);
    let cq_k = select(0.0, gi_cq(ks + t), t < dk);
    gd_red[t] = cq_q * cq_q;
    workgroupBarrier();
    var stride = 64u;
    loop {
        if (stride == 0u) { break; }
        if (t < stride) { gd_red[t] = gd_red[t] + gd_red[t + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let nq = gd_red[0];
    workgroupBarrier();
    gd_red[t] = cq_k * cq_k;
    workgroupBarrier();
    stride = 64u;
    loop {
        if (stride == 0u) { break; }
        if (t < stride) { gd_red[t] = gd_red[t] + gd_red[t + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let nkn = gd_red[0];
    workgroupBarrier();
    let invq = 1.0 / (sqrt(nq + 1e-6) * sqrt(f32(dk)));
    let invk = 1.0 / sqrt(nkn + 1e-6);
    let abo = gd_p.tok * gd_p.nv;
    let g = exp(-exp(gd_alog[h]) * gd_softplus(gd_a[abo + h] + gd_dtb[h]));
    let beta = 1.0 / (1.0 + exp(-gd_b[abo + h]));
    let sbase = h * dk * dv;
    let vt = gi_cq(2u * gd_p.kd + h * dv + dj);
    let kf_t = cq_k * invk;
    let qf_t = cq_q * invq;
    gd_red[t] = select(0.0, gd_S[sbase + t * dv + dj] * kf_t, t < dk);
    workgroupBarrier();
    stride = 64u;
    loop {
        if (stride == 0u) { break; }
        if (t < stride) { gd_red[t] = gd_red[t] + gd_red[t + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let kv = gd_red[0];
    workgroupBarrier();
    let delta = (vt - g * kv) * beta;
    var contrib = 0.0;
    if (t < dk) {
        let idx = sbase + t * dv + dj;
        let cell = g * gd_S[idx] + kf_t * delta;
        gd_S[idx] = cell;
        contrib = qf_t * cell;
    }
    gd_red[t] = contrib;
    workgroupBarrier();
    stride = 64u;
    loop {
        if (stride == 0u) { break; }
        if (t < stride) { gd_red[t] = gd_red[t] + gd_red[t + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    if (t == 0u) {
        let zo = gd_p.tok * gd_p.nv * dv;
        gd_o[zo + h * dv + dj] = gd_red[0];
    }
}

// norm v2: the gated RMSNorm PLUS the ring shift the conv kernel used to
// do — its writers (par2's gi_cq readers) are all upstream in the pass.
@compute @workgroup_size(256)
fn gdn_step_norm2(@builtin(workgroup_id) wid: vec3<u32>,
                  @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let t = lid.x;
    let dv = gd_p.dv;
    if (h >= gd_p.nv) { return; }
    let zo = gd_p.tok * gd_p.nv * dv;
    gd_red[t] = select(0.0, gd_o[zo + h * dv + t] * gd_o[zo + h * dv + t], t < dv);
    workgroupBarrier();
    let ss = gd_reduce(t);
    workgroupBarrier();
    let inv = 1.0 / sqrt(ss / f32(dv) + gd_p.eps);
    if (t < dv) {
        let zz = gd_z[zo + h * dv + t];
        gd_o[zo + h * dv + t] =
            gd_o[zo + h * dv + t] * inv * gd_norm[t] * (zz / (1.0 + exp(-zz)));
    }
    // ring shift, strided over cdim across all norm workgroups
    let kk = gi_p.kk;
    let cdim = gd_p.cdim;
    var c = wid.x * 256u + t;
    loop {
        if (c >= cdim) { break; }
        for (var j = 0u; j + 2u < kk; j = j + 1u) {
            gi_ring[j * cdim + c] = gi_ring[(j + 1u) * cdim + c];
        }
        if (kk > 1u) {
            gi_ring[(kk - 2u) * cdim + c] = gi_qkv[gi_p.xoff + c];
        }
        c = c + gd_p.nv * 256u;
    }
}

// Gated RMSNorm tail of the parallel GDN step: in place over gd_o.
@compute @workgroup_size(256)
fn gdn_step_norm(@builtin(workgroup_id) wid: vec3<u32>,
                 @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let t = lid.x;
    let dv = gd_p.dv;
    if (h >= gd_p.nv) { return; }
    let zo = gd_p.tok * gd_p.nv * dv;
    gd_red[t] = select(0.0, gd_o[zo + h * dv + t] * gd_o[zo + h * dv + t], t < dv);
    workgroupBarrier();
    let ss = gd_reduce(t);
    workgroupBarrier();
    let inv = 1.0 / sqrt(ss / f32(dv) + gd_p.eps);
    if (t < dv) {
        let zz = gd_z[zo + h * dv + t];
        gd_o[zo + h * dv + t] =
            gd_o[zo + h * dv + t] * inv * gd_norm[t] * (zz / (1.0 + exp(-zz)));
    }
}

// Fused residual-add + RMSNorm (WGSL twin of Metal add_rmsnorm_rows): h += d
// in place, then o = rms(h)·w. Collapses an axpy + an rmsnorm dispatch into
// one — cuts two launches per layer off the token graph.
struct ArP { n: u32, gemma: u32, eps: f32, _p: u32 };
@group(0) @binding(0) var<storage, read_write> ar_h : array<f32>;
@group(0) @binding(1) var<storage, read>       ar_d : array<f32>;
@group(0) @binding(2) var<storage, read>       ar_w : array<f32>;
@group(0) @binding(3) var<storage, read_write> ar_o : array<f32>;
@group(0) @binding(4) var<uniform>             ar_p : ArP;
var<workgroup> ar_part: array<f32, 256>;
@compute @workgroup_size(256)
fn add_rmsnorm(@builtin(local_invocation_id) lid: vec3<u32>) {
    let tid = lid.x;
    let n = ar_p.n;
    var acc = 0.0;
    var i = tid;
    loop {
        if (i >= n) { break; }
        let v = ar_h[i] + ar_d[i];
        ar_h[i] = v;
        acc = acc + v * v;
        i = i + 256u;
    }
    ar_part[tid] = acc;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (tid < stride) { ar_part[tid] = ar_part[tid] + ar_part[tid + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let inv = inverseSqrt(ar_part[0] / f32(n) + ar_p.eps);
    i = tid;
    loop {
        if (i >= n) { break; }
        var wv = ar_w[i];
        if (ar_p.gemma == 1u) { wv = 1.0 + wv; }
        ar_o[i] = ar_h[i] * inv * wv;
        i = i + 256u;
    }
}

// Batched RMSNorm for prefill: one workgroup per row (wid.x), row r reads/writes
// rn_x[r*n..] → rn_o[r*n..]; the weight rn_w[n] is shared. K prompt positions
// norm in one dispatch (twin of `rmsnorm`, strided by row).
@compute @workgroup_size(256)
fn rmsnorm_b(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let tid = lid.x;
    let n = rn_p.n;
    let base = wid.x * n;
    var acc = 0.0;
    var i = tid;
    loop { if (i >= n) { break; } let v = rn_x[base + i]; acc = acc + v * v; i = i + 256u; }
    rn_part[tid] = acc;
    workgroupBarrier();
    var stride = 128u;
    loop { if (stride == 0u) { break; } if (tid < stride) { rn_part[tid] = rn_part[tid] + rn_part[tid + stride]; } workgroupBarrier(); stride = stride / 2u; }
    let inv = inverseSqrt(rn_part[0] / f32(n) + rn_p.eps);
    i = tid;
    loop { if (i >= n) { break; } var wv = rn_w[i]; if (rn_p.gemma == 1u) { wv = 1.0 + wv; } rn_o[base + i] = rn_x[base + i] * inv * wv; i = i + 256u; }
}

// Batched fused residual-add + RMSNorm (one workgroup per row): ar_h[r] += ar_d[r]
// in place, then ar_o[r] = rms(ar_h[r])·w. Prefill twin of `add_rmsnorm`.
@compute @workgroup_size(256)
fn add_rmsnorm_b(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let tid = lid.x;
    let n = ar_p.n;
    let base = wid.x * n;
    var acc = 0.0;
    var i = tid;
    loop { if (i >= n) { break; } let v = ar_h[base + i] + ar_d[base + i]; ar_h[base + i] = v; acc = acc + v * v; i = i + 256u; }
    ar_part[tid] = acc;
    workgroupBarrier();
    var stride = 128u;
    loop { if (stride == 0u) { break; } if (tid < stride) { ar_part[tid] = ar_part[tid] + ar_part[tid + stride]; } workgroupBarrier(); stride = stride / 2u; }
    let inv = inverseSqrt(ar_part[0] / f32(n) + ar_p.eps);
    i = tid;
    loop { if (i >= n) { break; } var wv = ar_w[i]; if (ar_p.gemma == 1u) { wv = 1.0 + wv; } ar_o[base + i] = ar_h[base + i] * inv * wv; i = i + 256u; }
}

// RoPE + optional qk-norm + gate-split, one 32-thread workgroup per head
// (WGSL twin of Metal attn_rope_qkn; the qk-norm sum-of-squares reduces in
// workgroup memory — no subgroup ops, portable). Heads [0,nh)=Q (2·hd each
// when gated: q||gate), [nh,nh+nkv)=K. flags: 1=gate 2=qnorm 4=knorm 8=gemma.
struct RqP { nh: u32, nkv: u32, hd: u32, rd: u32, pos: u32, flags: u32, eps: f32, tok: u32 };
@group(0) @binding(0) var<storage, read>       rq_qraw : array<f32>;
@group(0) @binding(1) var<storage, read_write> rq_k    : array<f32>;
@group(0) @binding(2) var<storage, read_write> rq_qout : array<f32>;
@group(0) @binding(3) var<storage, read_write> rq_gout : array<f32>;
@group(0) @binding(4) var<storage, read>       rq_qnw  : array<f32>;
@group(0) @binding(5) var<storage, read>       rq_knw  : array<f32>;
@group(0) @binding(6) var<storage, read>       rq_invf : array<f32>;
@group(0) @binding(7) var<uniform>             rq_p    : RqP;
var<workgroup> rq_red: array<f32, 32>;
var<workgroup> rq_head: array<f32, 256>;
@compute @workgroup_size(32)
fn attn_rope_qkn(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let head = wid.x;
    let lane = lid.x;
    let nh = rq_p.nh;
    let hd = rq_p.hd;
    if (head >= nh + rq_p.nkv) { return; }
    let isq = head < nh;
    let gate = (rq_p.flags & 1u) != 0u;
    let src_base = select((head - nh) * hd, head * select(1u, 2u, gate) * hd, isq);
    // Batch-graph token offsets (0 in the token graph): q rows live in the
    // batched projection output, K is rotated IN PLACE in its batch slice.
    let qoff = rq_p.tok * nh * select(1u, 2u, gate) * hd;
    let koff = rq_p.tok * rq_p.nkv * hd;
    let nt = (hd + 31u) / 32u;  // ≤ 8 for head_dim ≤ 256 (Qwen3.5 uses 256)
    var xv: array<f32, 8>;
    var ss = 0.0;
    for (var t = 0u; t < nt; t = t + 1u) {
        let d = t * 32u + lane;
        var val = 0.0;
        if (d < hd) { val = select(rq_k[koff + src_base + d], rq_qraw[qoff + src_base + d], isq); }
        xv[t] = val;
        ss = ss + val * val;
    }
    rq_red[lane] = ss;
    workgroupBarrier();
    var stride = 16u;
    loop {
        if (stride == 0u) { break; }
        if (lane < stride) { rq_red[lane] = rq_red[lane] + rq_red[lane + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let normed = select((rq_p.flags & 4u) != 0u, (rq_p.flags & 2u) != 0u, isq);
    if (normed) {
        let inv = 1.0 / sqrt(rq_red[0] / f32(hd) + rq_p.eps);
        let gemma = (rq_p.flags & 8u) != 0u;
        for (var t = 0u; t < nt; t = t + 1u) {
            let d = t * 32u + lane;
            if (d < hd) {
                var wd = select(rq_knw[d], rq_qnw[d], isq);
                if (gemma) { wd = 1.0 + wd; }
                xv[t] = xv[t] * inv * wd;
            }
        }
    }
    // RoPE over the first rd dims, pairing dim i with dim i+hlf. Staged through
    // workgroup memory because the pair partner lands on a DIFFERENT lane when
    // hlf isn't a multiple of 32 (partial RoPE — Qwen3.5 rotates head_dim/4, so
    // hlf can be 16). The old register tiling (xv[t+toff], toff=hlf/32) silently
    // did nothing for hlf<32; here each lane ropes the pairs i=lane,lane+32,…
    for (var t = 0u; t < nt; t = t + 1u) {
        let d = t * 32u + lane;
        if (d < hd) { rq_head[d] = xv[t]; }
    }
    workgroupBarrier();
    let hlf = rq_p.rd / 2u;
    var ri = lane;
    loop {
        if (ri >= hlf) { break; }
        let angle = f32(rq_p.pos) * rq_invf[ri];
        let cc = cos(angle);
        let sfac = sin(angle);
        let x0 = rq_head[ri];
        let x1 = rq_head[ri + hlf];
        rq_head[ri] = x0 * cc - x1 * sfac;
        rq_head[ri + hlf] = x0 * sfac + x1 * cc;
        ri = ri + 32u;
    }
    workgroupBarrier();
    let dst_base = select((head - nh) * hd, head * hd, isq);
    for (var t = 0u; t < nt; t = t + 1u) {
        let d = t * 32u + lane;
        if (d < hd) {
            if (isq) { rq_qout[dst_base + d] = rq_head[d]; } else { rq_k[koff + dst_base + d] = rq_head[d]; }
        }
    }
    if (isq && gate) {
        let gbase = head * 2u * hd + hd;
        for (var t = 0u; t < nt; t = t + 1u) {
            let d = t * 32u + lane;
            if (d < hd) { rq_gout[head * hd + d] = rq_qraw[qoff + gbase + d]; }
        }
    }
}

// Append this position's K/V rows into the device cache mirror ([nkv,cap,hd]
// each) at row `stored`. WGSL twin of Metal kv_append.
struct KvP { nkv: u32, hd: u32, cap: u32, stored: u32 };
@group(0) @binding(0) var<storage, read>       kv_k  : array<f32>;
@group(0) @binding(1) var<storage, read>       kv_v  : array<f32>;
@group(0) @binding(2) var<storage, read_write> kv_kb : array<f32>;
@group(0) @binding(3) var<storage, read_write> kv_vb : array<f32>;
@group(0) @binding(4) var<uniform>             kv_p  : KvP;
// `stored` carries the batch token index in its high bits (pos | tok<<20):
// the batch graph appends straight from its batched K/V buffers, the token
// graph passes tok=0 and reads from offset zero as before. Positions stay
// under 2^20, far above any cap the cache allows.
@compute @workgroup_size(256)
fn kv_append(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    if (i >= kv_p.nkv * kv_p.hd) { return; }
    let stored = kv_p.stored & 0xFFFFFu;
    let toff = (kv_p.stored >> 20u) * kv_p.nkv * kv_p.hd;
    let h = i / kv_p.hd;
    let d = i % kv_p.hd;
    let dst = (h * kv_p.cap + stored) * kv_p.hd + d;
    kv_kb[dst] = kv_k[toff + i];
    kv_vb[dst] = kv_v[toff + i];
}

// Grouped decode attention, one 32-thread workgroup per Q-head. Dims sliced
// across lanes (dim d in lane d%32, slot d/32); online softmax over the n
// cached positions with the per-position q·k dot reduced in workgroup memory
// (portable — no subgroup ops). WGSL twin of Metal gqa_attend (output only;
// Born-importance is handled on the CPU side when eviction is active).
struct AtP { nh: u32, hpk: u32, hd: u32, cap: u32, n: u32, _a: u32, _b: u32, _c: u32 };
@group(0) @binding(0) var<storage, read>       at_q : array<vec4<f32>>;
@group(0) @binding(1) var<storage, read>       at_k : array<vec4<f32>>;
@group(0) @binding(2) var<storage, read>       at_v : array<vec4<f32>>;
@group(0) @binding(3) var<storage, read_write> at_o : array<f32>;
@group(0) @binding(4) var<uniform>             at_p : AtP;
// Flash-decoding: split the n cached positions across the 32 lanes. Each lane
// runs an INDEPENDENT online softmax over positions lane, lane+32, … with NO
// barrier in the loop (the old kernel barriered twice PER position — O(ctx)
// serial chain), then a 5-step 32-way log-sum-exp merge. Serial steps: n → n/32.
// K/V/Q are vec4 bindings (hd % 4 == 0, gated by the Rust callers): each lane
// reads a DIFFERENT cache row, so f32 loads were 4B-used-per-32B-sector — the
// depth wall of the decode graph (4090, 1.7B q1 @ctx512: attend dominated the
// 15 ms/token submit). vec4 quarters the wasted sectors. The workgroup
// accumulator stays SCALAR at stride 257 — (lane·257 + d) mod 32 is unique per
// lane, bank-conflict-free; a vec4 accumulator array cannot be (stride must be
// ≡1 mod 32 AND a multiple of 4 — impossible).
var<workgroup> at_acc: array<f32, 8224>; // [lane*257 + d], stride 257 dodges 32-bank conflicts, hd ≤ 256 (Qwen3.5=256)
var<workgroup> at_m: array<f32, 32>;
var<workgroup> at_l: array<f32, 32>;
// Decode-regime attend: 256 threads per head instead of one warp. Lanes
// are POSITIONS for the score pass (dot over hd each) and DIMENSIONS for
// the value pass (coalesced v reads, one output dim per lane, hd <= 256).
// Online softmax over 256-position chunks; per-chunk stats via one tree.
// The 32-lane kernel above kept a 257-stride accumulator per lane and a
// five-level 256-wide merge — 137 us per layer at fifty positions of
// context. This shape does the same math in the natural order.
var<workgroup> ad_sc: array<f32, 256>;
var<workgroup> ad_red: array<f32, 256>;

@compute @workgroup_size(256)
fn gqa_attend_dec(@builtin(workgroup_id) wid: vec3<u32>,
                  @builtin(local_invocation_index) lid: u32) {
    let h = wid.x;
    if (h >= at_p.nh) { return; }
    let hd = at_p.hd;
    let hd4 = hd / 4u;
    let n = at_p.n;
    let kbase = (h / at_p.hpk) * at_p.cap * hd4;
    let qbase = h * hd4;
    let scale = 1.0 / sqrt(f32(hd));
    var m = -1.0e30;
    var l = 0.0;
    var acc = 0.0;                      // this lane's output dim (lid < hd)
    var c0 = 0u;
    loop {
        if (c0 >= n) { break; }
        let cn = min(256u, n - c0);
        // scores: lane p of the chunk
        var sc = -1.0e30;
        if (lid < cn) {
            let krow = kbase + (c0 + lid) * hd4;
            var dot4 = vec4<f32>(0.0);
            for (var d = 0u; d < hd4; d = d + 1u) {
                dot4 = dot4 + at_q[qbase + d] * at_k[krow + d];
            }
            sc = (dot4.x + dot4.y + dot4.z + dot4.w) * scale;
        }
        ad_sc[lid] = sc;
        ad_red[lid] = sc;
        workgroupBarrier();
        var st = 128u;
        loop {
            if (st == 0u) { break; }
            if (lid < st) { ad_red[lid] = max(ad_red[lid], ad_red[lid + st]); }
            workgroupBarrier();
            st = st >> 1u;
        }
        let cm = ad_red[0];
        workgroupBarrier();
        let mp = max(m, cm);
        let f = exp(m - mp);
        // weights into shared, denom via tree
        let w = select(0.0, exp(ad_sc[lid] - mp), lid < cn);
        ad_sc[lid] = w;
        ad_red[lid] = w;
        workgroupBarrier();
        st = 128u;
        loop {
            if (st == 0u) { break; }
            if (lid < st) { ad_red[lid] = ad_red[lid] + ad_red[lid + st]; }
            workgroupBarrier();
            st = st >> 1u;
        }
        l = l * f + ad_red[0];
        workgroupBarrier();
        // value pass: lane = output dim, coalesced across lanes
        if (lid < hd) {
            acc = acc * f;
            let dw = lid >> 2u;
            let dc = lid & 3u;
            for (var p = 0u; p < cn; p = p + 1u) {
                acc = acc + ad_sc[p] * at_v[kbase + (c0 + p) * hd4 + dw][dc];
            }
        }
        m = mp;
        c0 = c0 + 256u;
        workgroupBarrier();
    }
    if (lid < hd) {
        at_o[h * hd + lid] = acc / l;
    }
}

@compute @workgroup_size(32)
fn gqa_attend(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let lane = lid.x;
    if (h >= at_p.nh) { return; }
    let hd = at_p.hd;
    let hd4 = hd / 4u;
    let n = at_p.n;
    let kbase = (h / at_p.hpk) * at_p.cap * hd4;
    let qbase = h * hd4;
    let scale = 1.0 / sqrt(f32(hd));
    let base = lane * 257u;
    for (var d = 0u; d < hd; d = d + 1u) { at_acc[base + d] = 0.0; }
    var m = -1e30;
    var l = 0.0;
    var p = lane;
    loop {
        if (p >= n) { break; }
        let krow = kbase + p * hd4;
        var dot4 = vec4<f32>(0.0);
        for (var d = 0u; d < hd4; d = d + 1u) { dot4 = dot4 + at_q[qbase + d] * at_k[krow + d]; }
        let dot = (dot4.x + dot4.y + dot4.z + dot4.w) * scale;
        let mp = max(m, dot);
        let f = exp(m - mp);
        let w = exp(dot - mp);
        l = l * f + w;
        for (var d = 0u; d < hd4; d = d + 1u) {
            let vv = at_v[krow + d] * w;
            let a = base + d * 4u;
            at_acc[a]      = at_acc[a]      * f + vv.x;
            at_acc[a + 1u] = at_acc[a + 1u] * f + vv.y;
            at_acc[a + 2u] = at_acc[a + 2u] * f + vv.z;
            at_acc[a + 3u] = at_acc[a + 3u] * f + vv.w;
        }
        m = mp;
        p = p + 32u;
    }
    at_m[lane] = m;
    at_l[lane] = l;
    workgroupBarrier();
    var stride = 16u;
    loop {
        if (stride == 0u) { break; }
        if (lane < stride) {
            let o = lane + stride;
            let m1 = at_m[lane];
            let m2 = at_m[o];
            let mm = max(m1, m2);
            let f1 = exp(m1 - mm);
            let f2 = exp(m2 - mm);
            at_l[lane] = at_l[lane] * f1 + at_l[o] * f2;
            let bo = o * 257u;
            for (var d = 0u; d < hd; d = d + 1u) {
                at_acc[base + d] = at_acc[base + d] * f1 + at_acc[bo + d] * f2;
            }
            at_m[lane] = mm;
        }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let invl = select(0.0, 1.0 / at_l[0], at_l[0] > 0.0);
    for (var d = lane; d < hd; d = d + 32u) {
        at_o[h * hd + d] = at_acc[d] * invl;
    }
}

// head_dim ≤ 256 on a 32 KB device: same stride 257, HALF the lanes.
//
// The 32-lane kernel above needs 32·257·4 = 32 896 B of workgroup memory
// and cannot be created where the limit is 32 768 — wgpu-Metal and mobile.
// The stride cannot shrink (it must exceed hd, and 257 is what dodges the
// 32-bank conflicts), so the lane count is the only free dimension:
// 16·257·4 = 16 448 B fits with room to spare.
//
// Without this, `hd_cap` on Apple was 128 and the whole-token graph
// silently declined for the ENTIRE Qwen3.5/3.6 family (head_dim 256) —
// every layer walked the host on a machine whose GPU could have run it.
// Halving the lanes halves the position parallelism, which this kernel
// can afford: it is bound by the vec4 K/V reads, not by lane occupancy.
var<workgroup> at_acc16: array<f32, 4112>; // [lane*257 + d], 16 lanes
var<workgroup> at_m16: array<f32, 16>;
var<workgroup> at_l16: array<f32, 16>;
@compute @workgroup_size(16)
fn gqa_attend_w16(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let lane = lid.x;
    if (h >= at_p.nh) { return; }
    let hd = at_p.hd;
    let hd4 = hd / 4u;
    let n = at_p.n;
    let kbase = (h / at_p.hpk) * at_p.cap * hd4;
    let qbase = h * hd4;
    let scale = 1.0 / sqrt(f32(hd));
    let base = lane * 257u;
    for (var d = 0u; d < hd; d = d + 1u) { at_acc16[base + d] = 0.0; }
    var m = -1e30;
    var l = 0.0;
    var p = lane;
    loop {
        if (p >= n) { break; }
        let krow = kbase + p * hd4;
        var dot4 = vec4<f32>(0.0);
        for (var d = 0u; d < hd4; d = d + 1u) { dot4 = dot4 + at_q[qbase + d] * at_k[krow + d]; }
        let dot = (dot4.x + dot4.y + dot4.z + dot4.w) * scale;
        let mp = max(m, dot);
        let f = exp(m - mp);
        let w = exp(dot - mp);
        l = l * f + w;
        for (var d = 0u; d < hd4; d = d + 1u) {
            let vv = at_v[krow + d] * w;
            let a = base + d * 4u;
            at_acc16[a]      = at_acc16[a]      * f + vv.x;
            at_acc16[a + 1u] = at_acc16[a + 1u] * f + vv.y;
            at_acc16[a + 2u] = at_acc16[a + 2u] * f + vv.z;
            at_acc16[a + 3u] = at_acc16[a + 3u] * f + vv.w;
        }
        m = mp;
        p = p + 16u;
    }
    at_m16[lane] = m;
    at_l16[lane] = l;
    workgroupBarrier();
    var stride = 8u;
    loop {
        if (stride == 0u) { break; }
        if (lane < stride) {
            let o = lane + stride;
            let m1 = at_m16[lane];
            let m2 = at_m16[o];
            let mm = max(m1, m2);
            let f1 = exp(m1 - mm);
            let f2 = exp(m2 - mm);
            at_l16[lane] = at_l16[lane] * f1 + at_l16[o] * f2;
            let bo = o * 257u;
            for (var d = 0u; d < hd; d = d + 1u) {
                at_acc16[base + d] = at_acc16[base + d] * f1 + at_acc16[bo + d] * f2;
            }
            at_m16[lane] = mm;
        }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let invl = select(0.0, 1.0 / at_l16[0], at_l16[0] > 0.0);
    for (var d = lane; d < hd; d = d + 16u) {
        at_o[h * hd + d] = at_acc16[d] * invl;
    }
}

// hd <= 128 twin of gqa_attend at stride 129 — 16.5 KB of workgroup
// memory instead of 33 KB. Mobile GPUs (Adreno/Mali) and wgpu-Metal cap
// maxComputeWorkgroupStorageSize at 32768 B, where the 257-stride kernel
// cannot even be created: the invalid pipeline turned every dispatch
// into a no-op and the graph decoded garbage on phones. (lane*129 + d)
// mod 32 == (lane + d) mod 32 — still bank-conflict-free.
var<workgroup> at_acc_s: array<f32, 4128>;
@compute @workgroup_size(32)
fn gqa_attend_s(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let lane = lid.x;
    if (h >= at_p.nh) { return; }
    let hd = at_p.hd;
    let hd4 = hd / 4u;
    let n = at_p.n;
    let kbase = (h / at_p.hpk) * at_p.cap * hd4;
    let qbase = h * hd4;
    let scale = 1.0 / sqrt(f32(hd));
    let base = lane * 129u;
    for (var d = 0u; d < hd; d = d + 1u) { at_acc_s[base + d] = 0.0; }
    var m = -1e30;
    var l = 0.0;
    var p = lane;
    loop {
        if (p >= n) { break; }
        let krow = kbase + p * hd4;
        var dot4 = vec4<f32>(0.0);
        for (var d = 0u; d < hd4; d = d + 1u) { dot4 = dot4 + at_q[qbase + d] * at_k[krow + d]; }
        let dot = (dot4.x + dot4.y + dot4.z + dot4.w) * scale;
        let mp = max(m, dot);
        let f = exp(m - mp);
        let w = exp(dot - mp);
        l = l * f + w;
        for (var d = 0u; d < hd4; d = d + 1u) {
            let vv = at_v[krow + d] * w;
            let a = base + d * 4u;
            at_acc_s[a]      = at_acc_s[a]      * f + vv.x;
            at_acc_s[a + 1u] = at_acc_s[a + 1u] * f + vv.y;
            at_acc_s[a + 2u] = at_acc_s[a + 2u] * f + vv.z;
            at_acc_s[a + 3u] = at_acc_s[a + 3u] * f + vv.w;
        }
        m = mp;
        p = p + 32u;
    }
    at_m[lane] = m;
    at_l[lane] = l;
    workgroupBarrier();
    var stride = 16u;
    loop {
        if (stride == 0u) { break; }
        if (lane < stride) {
            let o = lane + stride;
            let m1 = at_m[lane];
            let m2 = at_m[o];
            let mm = max(m1, m2);
            let f1 = exp(m1 - mm);
            let f2 = exp(m2 - mm);
            at_l[lane] = at_l[lane] * f1 + at_l[o] * f2;
            let bo = o * 129u;
            for (var d = 0u; d < hd; d = d + 1u) {
                at_acc_s[base + d] = at_acc_s[base + d] * f1 + at_acc_s[bo + d] * f2;
            }
            at_m[lane] = mm;
        }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let invl = select(0.0, 1.0 / at_l[0], at_l[0] > 0.0);
    for (var d = lane; d < hd; d = d + 32u) {
        at_o[h * hd + d] = at_acc_s[d] * invl;
    }
}

// q1t (ternary base-3) + q4_block matvec — reuse the q1 bindings (q1w/q1x/q1y/
// q1p) and its 4-slot layout. Weights arrive as array<u32>, so bytes come out
// with shift+mask (q1t_byte). q1p fields are reinterpreted: np=gpr, _p0=cols.
var<workgroup> partial_q1t: array<f32, 64>;
fn q1t_byte(off: u32) -> u32 {
    return (q1w[off >> 2u] >> ((off & 3u) * 8u)) & 0xFFu;
}
const Q1T_LUT: array<u32, 243> = array<u32, 243>(
    0u, 1u, 2u, 4u, 5u, 6u, 8u, 9u, 10u, 16u, 17u, 18u, 20u, 21u, 22u, 24u,
    25u, 26u, 32u, 33u, 34u, 36u, 37u, 38u, 40u, 41u, 42u, 64u, 65u, 66u, 68u, 69u,
    70u, 72u, 73u, 74u, 80u, 81u, 82u, 84u, 85u, 86u, 88u, 89u, 90u, 96u, 97u, 98u,
    100u, 101u, 102u, 104u, 105u, 106u, 128u, 129u, 130u, 132u, 133u, 134u, 136u, 137u, 138u, 144u,
    145u, 146u, 148u, 149u, 150u, 152u, 153u, 154u, 160u, 161u, 162u, 164u, 165u, 166u, 168u, 169u,
    170u, 256u, 257u, 258u, 260u, 261u, 262u, 264u, 265u, 266u, 272u, 273u, 274u, 276u, 277u, 278u,
    280u, 281u, 282u, 288u, 289u, 290u, 292u, 293u, 294u, 296u, 297u, 298u, 320u, 321u, 322u, 324u,
    325u, 326u, 328u, 329u, 330u, 336u, 337u, 338u, 340u, 341u, 342u, 344u, 345u, 346u, 352u, 353u,
    354u, 356u, 357u, 358u, 360u, 361u, 362u, 384u, 385u, 386u, 388u, 389u, 390u, 392u, 393u, 394u,
    400u, 401u, 402u, 404u, 405u, 406u, 408u, 409u, 410u, 416u, 417u, 418u, 420u, 421u, 422u, 424u,
    425u, 426u, 512u, 513u, 514u, 516u, 517u, 518u, 520u, 521u, 522u, 528u, 529u, 530u, 532u, 533u,
    534u, 536u, 537u, 538u, 544u, 545u, 546u, 548u, 549u, 550u, 552u, 553u, 554u, 576u, 577u, 578u,
    580u, 581u, 582u, 584u, 585u, 586u, 592u, 593u, 594u, 596u, 597u, 598u, 600u, 601u, 602u, 608u,
    609u, 610u, 612u, 613u, 614u, 616u, 617u, 618u, 640u, 641u, 642u, 644u, 645u, 646u, 648u, 649u,
    650u, 656u, 657u, 658u, 660u, 661u, 662u, 664u, 665u, 666u, 672u, 673u, 674u, 676u, 677u, 678u,
    680u, 681u, 682u
);

@compute @workgroup_size(64)
fn q1t_matvec(@builtin(workgroup_id) wid: vec3<u32>,
              @builtin(num_workgroups) nwg: vec3<u32>,
              @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let base_len = rows * gpr * 9u;
    let ent_off = base_len + (rows + 1u) * 4u;
    var row = wid.x;
    loop {
        if (row >= rows) { break; }
        var acc = 0.0;
        var g = lid;
        loop {
            if (g >= gpr) { break; }
            let toff = (row * gpr + g) * 9u;
            let sc16 = q1t_byte(toff) | (q1t_byte(toff + 1u) << 8u);
            let scale = unpack2x16float(sc16).x;
            let codes = toff + 2u;
            let xb = g * 32u;
            var gsum = 0.0;
            // One byte carries FIVE base-3 codes: read (and LUT) it once
            // and spend it on all five, instead of re-reading per weight —
            // 7 byte loads a group against 32. Same k order, same adds.
            var k = 0u;
            for (var bi = 0u; bi < 7u; bi = bi + 1u) {
                let p = Q1T_LUT[q1t_byte(codes + bi)];
                let n = min(5u, 32u - k);
                for (var j = 0u; j < n; j = j + 1u) {
                    let code = (p >> (j * 2u)) & 3u;
                    let sgn = select(0.0, 1.0, code == 1u) - select(0.0, 1.0, code == 2u);
                    gsum = gsum + sgn * q1x[xb + k + j];
                }
                k = k + n;
            }
            acc = acc + scale * gsum;
            g = g + 64u;
        }
        partial_q1t[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { partial_q1t[lid] = partial_q1t[lid] + partial_q1t[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) {
            var corr = 0.0;
            let rp0 = base_len + row * 4u;
            let c0 = q1t_byte(rp0) | (q1t_byte(rp0 + 1u) << 8u) | (q1t_byte(rp0 + 2u) << 16u) | (q1t_byte(rp0 + 3u) << 24u);
            let rp1 = base_len + (row + 1u) * 4u;
            let c1 = q1t_byte(rp1) | (q1t_byte(rp1 + 1u) << 8u) | (q1t_byte(rp1 + 2u) << 16u) | (q1t_byte(rp1 + 3u) << 24u);
            for (var p = c0; p < c1; p = p + 1u) {
                let e = ent_off + p * 4u;
                let col = q1t_byte(e) | (q1t_byte(e + 1u) << 8u);
                let val16 = q1t_byte(e + 2u) | (q1t_byte(e + 3u) << 8u);
                corr = corr + unpack2x16float(val16).x * q1x[col];
            }
            q1y[row] = partial_q1t[0] + corr;
        }
        workgroupBarrier();
        row = row + nwg.x;
    }
}

// 8 nibbles from one u32 word dot 8 activations (fully unrolled FMA chain).
fn q4b_dot8(w: u32, xi: u32) -> f32 {
    return (f32(w & 0xFu) - 8.0) * q1x[xi]
         + (f32((w >> 4u) & 0xFu) - 8.0) * q1x[xi + 1u]
         + (f32((w >> 8u) & 0xFu) - 8.0) * q1x[xi + 2u]
         + (f32((w >> 12u) & 0xFu) - 8.0) * q1x[xi + 3u]
         + (f32((w >> 16u) & 0xFu) - 8.0) * q1x[xi + 4u]
         + (f32((w >> 20u) & 0xFu) - 8.0) * q1x[xi + 5u]
         + (f32((w >> 24u) & 0xFu) - 8.0) * q1x[xi + 6u]
         + (f32((w >> 28u) & 0xFu) - 8.0) * q1x[xi + 7u];
}

// q4b, tall edition: 8 rows per 256-thread workgroup in pairs with vec4
// activations — the same recipe as q4tp_matvec4, on the split layout
// (nibbles and f16 scales in two distant planes). Per-row group order
// and add order match the one-row kernel, so parity carries.
var<workgroup> p8a_q4b: array<f32, 256>;
var<workgroup> p8b_q4b: array<f32, 256>;

@compute @workgroup_size(256)
fn q4b_matvec8(@builtin(workgroup_id) wid: vec3<u32>,
               @builtin(num_workgroups) nwg: vec3<u32>,
               @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let scales_off = rows * gpr * 16u;
    let sub = lid >> 6u;
    let l = lid & 63u;
    var base = wid.x * 8u;
    loop {
        if (base >= rows) { break; }
        let row_a = base + sub;
        let row_b = row_a + 4u;
        var acc_a = 0.0;
        var acc_b = 0.0;
        if (row_a < rows) {
            let live_b = row_b < rows;
            var g = l;
            loop {
                if (g >= gpr) { break; }
                let xq = g * 8u;
                let x0 = q4v_x[xq];      let x1 = q4v_x[xq + 1u];
                let x2 = q4v_x[xq + 2u]; let x3 = q4v_x[xq + 3u];
                let x4 = q4v_x[xq + 4u]; let x5 = q4v_x[xq + 5u];
                let x6 = q4v_x[xq + 6u]; let x7 = q4v_x[xq + 7u];
                let ga = row_a * gpr + g;
                let sab = scales_off + ga * 2u;
                let sa = unpack2x16float((q1w[sab >> 2u] >> ((sab & 3u) * 8u)) & 0xFFFFu).x;
                let va = q4v_w[ga];
                acc_a = acc_a + sa
                    * (q4v_dot8(va.x, x0, x1) + q4v_dot8(va.y, x2, x3)
                     + q4v_dot8(va.z, x4, x5) + q4v_dot8(va.w, x6, x7));
                if (live_b) {
                    let gb = row_b * gpr + g;
                    let sbb = scales_off + gb * 2u;
                    let sb = unpack2x16float((q1w[sbb >> 2u] >> ((sbb & 3u) * 8u)) & 0xFFFFu).x;
                    let vb = q4v_w[gb];
                    acc_b = acc_b + sb
                        * (q4v_dot8(vb.x, x0, x1) + q4v_dot8(vb.y, x2, x3)
                         + q4v_dot8(vb.z, x4, x5) + q4v_dot8(vb.w, x6, x7));
                }
                g = g + 64u;
            }
        }
        p8a_q4b[lid] = acc_a;
        p8b_q4b[lid] = acc_b;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (l < stride) {
                p8a_q4b[lid] = p8a_q4b[lid] + p8a_q4b[lid + stride];
                p8b_q4b[lid] = p8b_q4b[lid] + p8b_q4b[lid + stride];
            }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (l == 0u) {
            if (row_a < rows) { q1y[row_a] = p8a_q4b[sub << 6u]; }
            if (row_b < rows) { q1y[row_b] = p8b_q4b[sub << 6u]; }
        }
        workgroupBarrier();
        base = base + nwg.x * 8u;
    }
}

@compute @workgroup_size(64)
fn q4b_matvec(@builtin(workgroup_id) wid: vec3<u32>,
              @builtin(num_workgroups) nwg: vec3<u32>,
              @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let scales_off = rows * gpr * 16u;
    var row = wid.x;
    loop {
        if (row >= rows) { break; }
        var acc = 0.0;
        var g = lid;
        loop {
            if (g >= gpr) { break; }
            let gi = row * gpr + g;
            // Scale: one u32 read instead of two byte reads.
            let sc_byte = scales_off + gi * 2u;
            let sc16 = (q1w[sc_byte >> 2u] >> ((sc_byte & 3u) * 8u)) & 0xFFFFu;
            let scale = unpack2x16float(sc16).x;
            // 4 u32 reads = 16 bytes = 32 weights (4× fewer array accesses
            // than the per-byte path, ~40% fewer ALU per group).
            let pk4 = gi * 4u;
            let xb = g * 32u;
            let gsum = q4b_dot8(q1w[pk4], xb)
                     + q4b_dot8(q1w[pk4 + 1u], xb + 8u)
                     + q4b_dot8(q1w[pk4 + 2u], xb + 16u)
                     + q4b_dot8(q1w[pk4 + 3u], xb + 24u);
            acc = acc + scale * gsum;
            g = g + 64u;
        }
        partial_q1t[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { partial_q1t[lid] = partial_q1t[lid] + partial_q1t[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) { q1y[row] = partial_q1t[0]; }
        workgroupBarrier();
        row = row + nwg.x;
    }
}

// q4_tiled matvec: 18-byte interleaved tiles [f16 scale][16B nibbles] — ONE
// stream per row (the split q4b layout above reads nibbles and scales from
// two distant regions; feeding TILED bytes to it produced garbage — caught by
// an end-to-end answer check on real Vulkan). Tiles are 2-aligned, so words
// assemble from u16 halves of the u32 weight array.
fn q4t_u16(off16: u32) -> u32 {
    return (q1w[off16 >> 1u] >> ((off16 & 1u) * 16u)) & 0xFFFFu;
}
@compute @workgroup_size(64)
fn q4t_matvec(@builtin(workgroup_id) wid: vec3<u32>,
              @builtin(num_workgroups) nwg: vec3<u32>,
              @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    var row = wid.x;
    loop {
        if (row >= rows) { break; }
        var acc = 0.0;
        var g = lid;
        loop {
            if (g >= gpr) { break; }
            let t16 = (row * gpr + g) * 9u;
            let scale = unpack2x16float(q4t_u16(t16)).x;
            let xb = g * 32u;
            var gsum = 0.0;
            for (var k = 0u; k < 4u; k = k + 1u) {
                let w = q4t_u16(t16 + 1u + 2u * k) | (q4t_u16(t16 + 2u + 2u * k) << 16u);
                gsum = gsum + q4b_dot8(w, xb + 8u * k);
            }
            acc = acc + scale * gsum;
            g = g + 64u;
        }
        partial_q1t[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { partial_q1t[lid] = partial_q1t[lid] + partial_q1t[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) { q1y[row] = partial_q1t[0]; }
        workgroupBarrier();
        row = row + nwg.x;
    }
}

// q4t, tall edition: 8 rows per 256-thread workgroup in pairs, vec4
// activations. The 18-byte tile stride is 2-aligned, not 4, so the
// weights stay u16-assembled (that is the layout's own cost) — but the
// activation side vectorizes exactly as in q4tp, and every x vec4 feeds
// two rows. Per-row group order and add order are the one-row kernel's.
var<workgroup> lad_q4t8: array<f32, 8>;
var<workgroup> p8a_q4t: array<f32, 256>;
var<workgroup> p8b_q4t: array<f32, 256>;

fn q4t_dot8v(w: u32, a: vec4<f32>, b: vec4<f32>) -> f32 {
    return (f32(w & 0xFu) - 8.0) * a.x
         + (f32((w >> 4u) & 0xFu) - 8.0) * a.y
         + (f32((w >> 8u) & 0xFu) - 8.0) * a.z
         + (f32((w >> 12u) & 0xFu) - 8.0) * a.w
         + (f32((w >> 16u) & 0xFu) - 8.0) * b.x
         + (f32((w >> 20u) & 0xFu) - 8.0) * b.y
         + (f32((w >> 24u) & 0xFu) - 8.0) * b.z
         + (f32((w >> 28u) & 0xFu) - 8.0) * b.w;
}

@compute @workgroup_size(256)
fn q4t_matvec8(@builtin(workgroup_id) wid: vec3<u32>,
               @builtin(num_workgroups) nwg: vec3<u32>,
               @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let sub = lid >> 6u;
    let l = lid & 63u;
    var base = wid.x * 8u;
    loop {
        if (base >= rows) { break; }
        let row_a = base + sub;
        let row_b = row_a + 4u;
        var acc_a = 0.0;
        var acc_b = 0.0;
        if (row_a < rows) {
            let live_b = row_b < rows;
            var g = l;
            loop {
                if (g >= gpr) { break; }
                let xq = g * 8u;
                let x0 = q4v_x[xq];      let x1 = q4v_x[xq + 1u];
                let x2 = q4v_x[xq + 2u]; let x3 = q4v_x[xq + 3u];
                let x4 = q4v_x[xq + 4u]; let x5 = q4v_x[xq + 5u];
                let x6 = q4v_x[xq + 6u]; let x7 = q4v_x[xq + 7u];
                let ta = (row_a * gpr + g) * 9u;
                let sa = unpack2x16float(q4t_u16(ta)).x;
                let wa0 = q4t_u16(ta + 1u) | (q4t_u16(ta + 2u) << 16u);
                let wa1 = q4t_u16(ta + 3u) | (q4t_u16(ta + 4u) << 16u);
                let wa2 = q4t_u16(ta + 5u) | (q4t_u16(ta + 6u) << 16u);
                let wa3 = q4t_u16(ta + 7u) | (q4t_u16(ta + 8u) << 16u);
                acc_a = acc_a + sa
                    * (q4t_dot8v(wa0, x0, x1) + q4t_dot8v(wa1, x2, x3)
                     + q4t_dot8v(wa2, x4, x5) + q4t_dot8v(wa3, x6, x7));
                if (live_b) {
                    let tb = (row_b * gpr + g) * 9u;
                    let sb = unpack2x16float(q4t_u16(tb)).x;
                    let wb0 = q4t_u16(tb + 1u) | (q4t_u16(tb + 2u) << 16u);
                    let wb1 = q4t_u16(tb + 3u) | (q4t_u16(tb + 4u) << 16u);
                    let wb2 = q4t_u16(tb + 5u) | (q4t_u16(tb + 6u) << 16u);
                    let wb3 = q4t_u16(tb + 7u) | (q4t_u16(tb + 8u) << 16u);
                    acc_b = acc_b + sb
                        * (q4t_dot8v(wb0, x0, x1) + q4t_dot8v(wb1, x2, x3)
                         + q4t_dot8v(wb2, x4, x5) + q4t_dot8v(wb3, x6, x7));
                }
                g = g + 64u;
            }
        }
        p8a_q4t[lid] = acc_a;
        p8b_q4t[lid] = acc_b;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (l < stride) {
                p8a_q4t[lid] = p8a_q4t[lid] + p8a_q4t[lid + stride];
                p8b_q4t[lid] = p8b_q4t[lid] + p8b_q4t[lid + stride];
            }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (l == 0u) {
            if (row_a < rows) { q1y[row_a] = p8a_q4t[sub << 6u]; }
            if (row_b < rows) { q1y[row_b] = p8b_q4t[sub << 6u]; }
        }
        workgroupBarrier();
        base = base + nwg.x * 8u;
    }
}

// q4tp matvec: same nibble values as q4t, but the stride is a clean 16 B —
// so the words come straight off the u32 array instead of being assembled
// from u16 halves the way q4t's 2-aligned 18 B tiles force. The scale is a
// 5-bit rung on the row's ladder, kept in two planes after the nibbles.
//
// A workgroup owns one row at a time, so it expands that row's 32 rungs once
// into workgroup memory. Evaluating 2^(lo + code*step) per tile instead was
// measured on Metal to cost the model ~15% even though the kernel benchmarked
// faster standalone: the graph's dispatches serialize on each other, which
// exposes the dependent chain (code byte → exp2 → scale) that a free-running
// benchmark hides.
var<workgroup> lad_q4tp: array<f32, 32>;

fn q4tp_byte(off: u32) -> u32 {
    return (q1w[off >> 2u] >> ((off & 3u) * 8u)) & 0xFFu;
}

@compute @workgroup_size(64)
fn q4tp_matvec(@builtin(workgroup_id) wid: vec3<u32>,
               @builtin(num_workgroups) nwg: vec3<u32>,
               @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let params_w = rows * gpr * 4u;                  // u32 index of row params
    let codes_b = rows * gpr * 16u + rows * 4u;      // byte offset of the codes
    let cstride = (gpr * 5u + 7u) / 8u;
    var row = wid.x;
    loop {
        if (row >= rows) { break; }
        if (lid < 32u) {
            let pr = unpack2x16float(q1w[params_w + row]);
            lad_q4tp[lid] = exp2(pr.x + f32(lid) * pr.y);
        }
        workgroupBarrier();
        var acc = 0.0;
        var g = lid;
        loop {
            if (g >= gpr) { break; }
            let bit = g * 5u;
            let cb = codes_b + row * cstride + (bit >> 3u);
            let sh = bit & 7u;
            // The 5-bit field spills into the next byte past bit 3; the row's
            // stride always holds that byte when it does.
            var cv = q4tp_byte(cb);
            if (sh > 3u) { cv = cv | (q4tp_byte(cb + 1u) << 8u); }
            let scale = lad_q4tp[(cv >> sh) & 31u];
            let base = (row * gpr + g) * 4u;
            let xb = g * 32u;
            var gsum = 0.0;
            for (var k = 0u; k < 4u; k = k + 1u) {
                gsum = gsum + q4b_dot8(q1w[base + k], xb + 8u * k);
            }
            acc = acc + scale * gsum;
            g = g + 64u;
        }
        partial_q1t[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { partial_q1t[lid] = partial_q1t[lid] + partial_q1t[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) { q1y[row] = partial_q1t[0]; }
        workgroupBarrier();
        row = row + nwg.x;
    }
}

// Narrow-matrix edition: 16 rows per workgroup for gpr <= 64 shapes
// (cols <= 2048: the GDN projections, o/qkv projections, lm_head), where
// the 8-row kernel gives each lane exactly ONE group and nothing to
// amortize. Four rows per 64-lane sub-block share every activation vec4
// four ways. Per-row lane layout and add order match the one-row kernel.
var<workgroup> lad_q16: array<f32, 512>;
var<workgroup> p16_a: array<f32, 256>;
var<workgroup> p16_b: array<f32, 256>;
var<workgroup> p16_c: array<f32, 256>;
var<workgroup> p16_d: array<f32, 256>;

@compute @workgroup_size(256)
fn q4tp_matvec16(@builtin(workgroup_id) wid: vec3<u32>,
                 @builtin(num_workgroups) nwg: vec3<u32>,
                 @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    let sub = lid >> 6u;
    let l = lid & 63u;
    // `_p0`: how many activation vectors share this weight. One is a matvec;
    // more is a batch. The BATCH is the fast axis of the dispatch, so the
    // workgroups that read the same weight rows are neighbours and meet in
    // L2; walking the whole output space instead put them `rows/16` apart,
    // which streams the weight once per batch element and defeats the point.
    // Reuse is still L2's to give — this is not a register-blocked B kernel —
    // so the win is a measurement, not a claim.
    let nb = max(q1p._p0, 1u);
    let blocks = (rows + 15u) / 16u;
    var wb = wid.x;
    loop {
        if (wb >= blocks * nb) { break; }
        let bi = wb % nb;
        let base = (wb / nb) * 16u;
        let bofs = bi * rows;
        // 16 rows x 32 rungs: each thread stages two.
        for (var q = lid; q < 512u; q = q + 256u) {
            let r = base + (q >> 5u);
            if (r < rows) {
                let pr = unpack2x16float(q1w[params_w + r]);
                lad_q16[q] = exp2(pr.x + f32(q & 31u) * pr.y);
            }
        }
        workgroupBarrier();
        // w_* index the weight; r_* index the output, one batch apart.
        let w_a = base + sub * 4u;
        let w_b = w_a + 1u;
        let w_c = w_a + 2u;
        let w_d = w_a + 3u;
        let r_a = bofs + w_a;
        let r_b = bofs + w_b;
        let r_c = bofs + w_c;
        let r_d = bofs + w_d;
        // `_p1`: the low-rank group width, which slides the activation
        // window with the row. The FOUR rows this thread owns are
        // consecutive, so they share a window only when the width divides
        // the 16-row block — the caller checks that.
        var xblk = 0u;
        if (nb > 1u) { xblk = bi * gpr * 8u; }
        else if (q1p._p1 > 0u) { xblk = (w_a / q1p._p1) * gpr * 8u; }
        var aa = 0.0;
        var ab = 0.0;
        var ac = 0.0;
        var ad = 0.0;
        if (w_a < rows) {
            let all_live = w_d < rows;
            var g = l;
            loop {
                if (g >= gpr) { break; }
                let bit = g * 5u;
                let cbo = bit >> 3u;
                let sh = bit & 7u;
                let xq = xblk + g * 8u;
                let x0 = q4v_x[xq];      let x1 = q4v_x[xq + 1u];
                let x2 = q4v_x[xq + 2u]; let x3 = q4v_x[xq + 3u];
                let x4 = q4v_x[xq + 4u]; let x5 = q4v_x[xq + 5u];
                let x6 = q4v_x[xq + 6u]; let x7 = q4v_x[xq + 7u];
                let cra = codes_b + w_a * cstride + cbo;
                var cva = q4tp_byte(cra);
                if (sh > 3u) { cva = cva | (q4tp_byte(cra + 1u) << 8u); }
                let sa = lad_q16[(sub * 4u << 5u) + ((cva >> sh) & 31u)];
                let va = q4v_w[w_a * gpr + g];
                aa = aa + sa
                    * (q4v_dot8(va.x, x0, x1) + q4v_dot8(va.y, x2, x3)
                     + q4v_dot8(va.z, x4, x5) + q4v_dot8(va.w, x6, x7));
                if (all_live || w_b < rows) {
                    let crb = codes_b + w_b * cstride + cbo;
                    var cvb = q4tp_byte(crb);
                    if (sh > 3u) { cvb = cvb | (q4tp_byte(crb + 1u) << 8u); }
                    let sb = lad_q16[((sub * 4u + 1u) << 5u) + ((cvb >> sh) & 31u)];
                    let vb = q4v_w[w_b * gpr + g];
                    ab = ab + sb
                        * (q4v_dot8(vb.x, x0, x1) + q4v_dot8(vb.y, x2, x3)
                         + q4v_dot8(vb.z, x4, x5) + q4v_dot8(vb.w, x6, x7));
                }
                if (all_live || w_c < rows) {
                    let crc = codes_b + w_c * cstride + cbo;
                    var cvc = q4tp_byte(crc);
                    if (sh > 3u) { cvc = cvc | (q4tp_byte(crc + 1u) << 8u); }
                    let sc = lad_q16[((sub * 4u + 2u) << 5u) + ((cvc >> sh) & 31u)];
                    let vc = q4v_w[w_c * gpr + g];
                    ac = ac + sc
                        * (q4v_dot8(vc.x, x0, x1) + q4v_dot8(vc.y, x2, x3)
                         + q4v_dot8(vc.z, x4, x5) + q4v_dot8(vc.w, x6, x7));
                }
                if (all_live || w_d < rows) {
                    let crd = codes_b + w_d * cstride + cbo;
                    var cvd = q4tp_byte(crd);
                    if (sh > 3u) { cvd = cvd | (q4tp_byte(crd + 1u) << 8u); }
                    let sd = lad_q16[((sub * 4u + 3u) << 5u) + ((cvd >> sh) & 31u)];
                    let vd = q4v_w[w_d * gpr + g];
                    ad = ad + sd
                        * (q4v_dot8(vd.x, x0, x1) + q4v_dot8(vd.y, x2, x3)
                         + q4v_dot8(vd.z, x4, x5) + q4v_dot8(vd.w, x6, x7));
                }
                g = g + 64u;
            }
        }
        p16_a[lid] = aa;
        p16_b[lid] = ab;
        p16_c[lid] = ac;
        p16_d[lid] = ad;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (l < stride) {
                p16_a[lid] = p16_a[lid] + p16_a[lid + stride];
                p16_b[lid] = p16_b[lid] + p16_b[lid + stride];
                p16_c[lid] = p16_c[lid] + p16_c[lid + stride];
                p16_d[lid] = p16_d[lid] + p16_d[lid + stride];
            }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (l == 0u) {
            if (w_a < rows) { q1y[r_a] = p16_a[sub << 6u]; }
            if (w_b < rows) { q1y[r_b] = p16_b[sub << 6u]; }
            if (w_c < rows) { q1y[r_c] = p16_c[sub << 6u]; }
            if (w_d < rows) { q1y[r_d] = p16_d[sub << 6u]; }
        }
        workgroupBarrier();
        wb = wb + nwg.x;
    }
}

// q4tp matvec, tall edition: 4 rows per 256-thread workgroup, and the group's
// 16 B of nibbles arrive as ONE vec4<u32> load instead of four scalar loads.
// Written for dense-FFN shapes (17408x5120: the one-row kernel left a 27B
// dense model at ~5% of the card's bandwidth); the weight buffer is bound
// TWICE — the scalar u32 view for params and 5-bit codes (they live at
// unaligned offsets, and the buffer tail may not be 16 B-round, which a vec4
// view would silently clamp) and a vec4 view for the nibble tiles, whose
// region is 16 B-exact by construction. Each row's lane layout, add order and
// 64-slot reduction tree are byte-identical to q4tp_matvec, so the kernels
// are interchangeable under greedy parity.
@group(0) @binding(4) var<storage, read> q4v_w : array<vec4<u32>>;
// The activations again, as vec4: the scalar kernel issues 32 x-loads per
// 16 B of weights and is LSU-bound long before it is bandwidth-bound
// (measured 190 GB/s of 1.79 TB/s on the dense-FFN shapes). Components are
// consumed in the exact q4b_dot8 order.
@group(0) @binding(5) var<storage, read> q4v_x : array<vec4<f32>>;

var<workgroup> lad_q4v: array<f32, 256>;
var<workgroup> partial_q4v: array<f32, 256>;
var<workgroup> partial_q4vb: array<f32, 256>;

fn q4v_dot8(w: u32, a: vec4<f32>, b: vec4<f32>) -> f32 {
    return (f32(w & 0xFu) - 8.0) * a.x
         + (f32((w >> 4u) & 0xFu) - 8.0) * a.y
         + (f32((w >> 8u) & 0xFu) - 8.0) * a.z
         + (f32((w >> 12u) & 0xFu) - 8.0) * a.w
         + (f32((w >> 16u) & 0xFu) - 8.0) * b.x
         + (f32((w >> 20u) & 0xFu) - 8.0) * b.y
         + (f32((w >> 24u) & 0xFu) - 8.0) * b.z
         + (f32((w >> 28u) & 0xFu) - 8.0) * b.w;
}

@compute @workgroup_size(256)
fn q4tp_matvec4(@builtin(workgroup_id) wid: vec3<u32>,
                @builtin(num_workgroups) nwg: vec3<u32>,
                @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    let sub = lid >> 6u;
    let l = lid & 63u;
    // 8 rows per workgroup, register-blocked in pairs: sub-block `sub` owns
    // rows base+sub and base+sub+4, and every x vec4 fetched for a group
    // feeds BOTH rows' dot chains — the x side of the LSU load nearly
    // halves. Each row's group order and add order stay those of the
    // one-row kernel.
    // `_p0`: how many activation vectors share this weight. One is a matvec;
    // more is a batch. The BATCH is the fast axis of the dispatch, so the
    // workgroups that read the same weight rows are neighbours and meet in
    // L2; walking the whole output space instead put them `rows/16` apart,
    // which streams the weight once per batch element and defeats the point.
    // Reuse is still L2's to give — this is not a register-blocked B kernel —
    // so the win is a measurement, not a claim.
    let nb = max(q1p._p0, 1u);
    let blocks = (rows + 7u) / 8u;
    var wb = wid.x;
    loop {
        if (wb >= blocks * nb) { break; }
        let bi = wb % nb;
        let base = (wb / nb) * 8u;
        let bofs = bi * rows;
        {
            let r = base + (lid >> 5u);
            if (r < rows) {
                let pr = unpack2x16float(q1w[params_w + r]);
                lad_q4v[lid] = exp2(pr.x + f32(lid & 31u) * pr.y);
            }
        }
        workgroupBarrier();
        let wrow_a = base + sub;
        let wrow_b = base + sub + 4u;
        let row_a = bofs + wrow_a;
        let row_b = bofs + wrow_b;
        let live_a = wrow_a < rows;
        let live_b = wrow_b < rows;
        // In vec4 units: (row / lora) * gpr * 32 floats.
        var xblk = 0u;
        if (nb > 1u) { xblk = bi * gpr * 8u; }
        else if (q1p._p1 > 0u) { xblk = (wrow_a / q1p._p1) * gpr * 8u; }
        var acc_a = 0.0;
        var acc_b = 0.0;
        if (live_a) {
            let crow_a = codes_b + wrow_a * cstride;
            let crow_b = codes_b + wrow_b * cstride;
            var g = l;
            loop {
                if (g >= gpr) { break; }
                let bit = g * 5u;
                let cbo = bit >> 3u;
                let sh = bit & 7u;
                var cv_a = q4tp_byte(crow_a + cbo);
                if (sh > 3u) { cv_a = cv_a | (q4tp_byte(crow_a + cbo + 1u) << 8u); }
                let v_a = q4v_w[wrow_a * gpr + g];
                // `_p1` is the low-rank group width. Set, it slides the
                // activation window with the row — which is the ONLY thing
                // the grouped output projection does differently, and the
                // reason it had a kernel of its own reading 3.82 ms against
                // this one's 1.24 on comparable weights. Rows base..base+7
                // share a window whenever the width is a multiple of 8, and
                // the caller only takes this path then.
                let xq = xblk + g * 8u;
                let x0 = q4v_x[xq];      let x1 = q4v_x[xq + 1u];
                let x2 = q4v_x[xq + 2u]; let x3 = q4v_x[xq + 3u];
                let x4 = q4v_x[xq + 4u]; let x5 = q4v_x[xq + 5u];
                let x6 = q4v_x[xq + 6u]; let x7 = q4v_x[xq + 7u];
                let sa = lad_q4v[(sub << 5u) + ((cv_a >> sh) & 31u)];
                acc_a = acc_a + sa
                    * (q4v_dot8(v_a.x, x0, x1) + q4v_dot8(v_a.y, x2, x3)
                     + q4v_dot8(v_a.z, x4, x5) + q4v_dot8(v_a.w, x6, x7));
                if (live_b) {
                    var cv_b = q4tp_byte(crow_b + cbo);
                    if (sh > 3u) { cv_b = cv_b | (q4tp_byte(crow_b + cbo + 1u) << 8u); }
                    let v_b = q4v_w[wrow_b * gpr + g];
                    let sb = lad_q4v[128u + (sub << 5u) + ((cv_b >> sh) & 31u)];
                    acc_b = acc_b + sb
                        * (q4v_dot8(v_b.x, x0, x1) + q4v_dot8(v_b.y, x2, x3)
                         + q4v_dot8(v_b.z, x4, x5) + q4v_dot8(v_b.w, x6, x7));
                }
                g = g + 64u;
            }
        }
        partial_q4v[lid] = acc_a;
        partial_q4vb[lid] = acc_b;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (l < stride) {
                partial_q4v[lid] = partial_q4v[lid] + partial_q4v[lid + stride];
                partial_q4vb[lid] = partial_q4vb[lid] + partial_q4vb[lid + stride];
            }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (l == 0u && wrow_a < rows) { q1y[row_a] = partial_q4v[sub << 6u]; }
        if (l == 0u && wrow_b < rows) { q1y[row_b] = partial_q4vb[sub << 6u]; }
        workgroupBarrier();
        wb = wb + nwg.x;
    }
}

// ── GDN step, parallel edition, k-looped: a workgroup per (head,
// column-quad) with the position loop INSIDE — the occupancy of the
// parallel kernel (thousands of workgroups where the serial one raised
// nv) and none of the per-position dispatch drains. Every state slice
// is workgroup-local across positions, so the loop needs no cross-
// workgroup sync; the raw o lands per position and gdn_step_norm_k
// applies the gated RMSNorm after. Snapshots ride the update itself.
@group(0) @binding(7) var<storage, read_write> gdk_S4 : array<vec4<f32>>;
@group(0) @binding(8) var<storage, read_write> gdk_o4 : array<vec4<f32>>;
@group(0) @binding(10) var<storage, read_write> gdk_snap4 : array<vec4<f32>>;
var<workgroup> gdk_r2: array<f32, 256>;
var<workgroup> gdk_r3: array<f32, 256>;
var<workgroup> gdk_r4: array<f32, 256>;
@compute @workgroup_size(128)
fn gdn_step_par_k(@builtin(workgroup_id) wid: vec3<u32>,
                  @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let dj4 = wid.y;
    let t = lid.x;
    let dk = gdk_p.dk;
    let dv = gdk_p.dv;
    if (h >= gdk_p.nv || dj4 * 4u >= dv) { return; }
    let ko = h / gdk_p.rep;
    let dv4 = dv >> 2u;
    let s4base = (h * dk * dv) >> 2u;
    for (var i = 0u; i < gdk_p.kb; i = i + 1u) {
        let cq0 = i * gdk_p.cdim;
        let qs = cq0 + ko * dk;
        let ks = cq0 + gdk_p.kd + ko * dk;
        gdk_red[t] = select(0.0, gdk_cq[qs + t] * gdk_cq[qs + t], t < dk);
        workgroupBarrier();
        var stride = 64u;
        loop {
            if (stride == 0u) { break; }
            if (t < stride) { gdk_red[t] = gdk_red[t] + gdk_red[t + stride]; }
            workgroupBarrier();
            stride = stride / 2u;
        }
        let nq = gdk_red[0];
        workgroupBarrier();
        gdk_red[t] = select(0.0, gdk_cq[ks + t] * gdk_cq[ks + t], t < dk);
        workgroupBarrier();
        stride = 64u;
        loop {
            if (stride == 0u) { break; }
            if (t < stride) { gdk_red[t] = gdk_red[t] + gdk_red[t + stride]; }
            workgroupBarrier();
            stride = stride / 2u;
        }
        let nkn = gdk_red[0];
        workgroupBarrier();
        let invq = 1.0 / (sqrt(nq + 1e-6) * sqrt(f32(dk)));
        let invk = 1.0 / sqrt(nkn + 1e-6);
        let abo = i * gdk_p.nv;
        let g = exp(-exp(gdk_alog[h]) * gd_softplus(gdk_a[abo + h] + gdk_dtb[h]));
        let beta = 1.0 / (1.0 + exp(-gdk_b[abo + h]));
        let vto = cq0 + 2u * gdk_p.kd + h * dv + dj4 * 4u;
        let vt = vec4<f32>(gdk_cq[vto], gdk_cq[vto + 1u], gdk_cq[vto + 2u], gdk_cq[vto + 3u]);
        let kf_t = select(0.0, gdk_cq[ks + t] * invk, t < dk);
        let qf_t = select(0.0, gdk_cq[qs + t] * invq, t < dk);
        var kv4 = vec4<f32>(0.0);
        if (t < dk) {
            kv4 = gdk_S4[s4base + t * dv4 + dj4] * kf_t;
        }
        gdk_red[t] = kv4.x;
        gdk_r2[t] = kv4.y;
        gdk_r3[t] = kv4.z;
        gdk_r4[t] = kv4.w;
        workgroupBarrier();
        stride = 64u;
        loop {
            if (stride == 0u) { break; }
            if (t < stride) {
                gdk_red[t] = gdk_red[t] + gdk_red[t + stride];
                gdk_r2[t] = gdk_r2[t] + gdk_r2[t + stride];
                gdk_r3[t] = gdk_r3[t] + gdk_r3[t + stride];
                gdk_r4[t] = gdk_r4[t] + gdk_r4[t + stride];
            }
            workgroupBarrier();
            stride = stride / 2u;
        }
        let kv = vec4<f32>(gdk_red[0], gdk_r2[0], gdk_r3[0], gdk_r4[0]);
        workgroupBarrier();
        let delta = (vt - g * kv) * beta;
        var contrib = vec4<f32>(0.0);
        if (t < dk) {
            let idx = s4base + t * dv4 + dj4;
            let cell = g * gdk_S4[idx] + kf_t * delta;
            gdk_S4[idx] = cell;
            if (gdk_p.stride != 0u) {
                gdk_snap4[(i * gdk_p.stride + gdk_p.ring_els) / 4u + idx] = cell;
            }
            contrib = qf_t * cell;
        }
        gdk_red[t] = contrib.x;
        gdk_r2[t] = contrib.y;
        gdk_r3[t] = contrib.z;
        gdk_r4[t] = contrib.w;
        workgroupBarrier();
        stride = 64u;
        loop {
            if (stride == 0u) { break; }
            if (t < stride) {
                gdk_red[t] = gdk_red[t] + gdk_red[t + stride];
                gdk_r2[t] = gdk_r2[t] + gdk_r2[t + stride];
                gdk_r3[t] = gdk_r3[t] + gdk_r3[t + stride];
                gdk_r4[t] = gdk_r4[t] + gdk_r4[t + stride];
            }
            workgroupBarrier();
            stride = stride / 2u;
        }
        if (t == 0u) {
            gdk_o4[(i * gdk_p.nv * dv) / 4u + h * dv4 + dj4] =
                vec4<f32>(gdk_red[0], gdk_r2[0], gdk_r3[0], gdk_r4[0]);
        }
        workgroupBarrier();
    }
}

// Gated RMSNorm over the k raw o rows the parallel step left.
@compute @workgroup_size(256)
fn gdn_step_norm_k(@builtin(workgroup_id) wid: vec3<u32>,
                   @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let t = lid.x;
    let dv = gdk_p.dv;
    if (h >= gdk_p.nv) { return; }
    for (var i = 0u; i < gdk_p.kb; i = i + 1u) {
        let zo = i * gdk_p.nv * dv;
        gdk_red[t] = select(0.0, gdk_o[zo + h * dv + t] * gdk_o[zo + h * dv + t], t < dv);
        workgroupBarrier();
        let ss = gdk_reduce(t);
        workgroupBarrier();
        let inv = 1.0 / sqrt(ss / f32(dv) + gdk_p.eps);
        if (t < dv) {
            let zz = gdk_z[zo + h * dv + t];
            gdk_o[zo + h * dv + t] =
                gdk_o[zo + h * dv + t] * inv * gdk_norm[t] * (zz / (1.0 + exp(-zz)));
        }
        workgroupBarrier();
    }
}

// ── q4tp matvec, QUAD row blocking: sixteen rows a workgroup, each
// 64-lane sub-block owning FOUR rows 4 apart, so the eight x vec4 loads
// of a group feed four dot chains instead of two. The pair kernel's LSU
// is x-bound at large widths — 128 bytes of activations per group
// against ~20 of weights per row — and halving-again the x side is the
// same medicine the DSV4 down projection took (its 4-row twin). The
// accumulator is a NAMED vec4: constant component indexing only, or the
// registers spill to stack and the GEMV runs at a fraction of the card.
var<workgroup> lad_q4w: array<f32, 512>;
@compute @workgroup_size(256)
fn q4tp_matvec16w(@builtin(workgroup_id) wid: vec3<u32>,
                  @builtin(num_workgroups) nwg: vec3<u32>,
                  @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    let sub = lid >> 6u;
    let l = lid & 63u;
    let blocks = (rows + 15u) / 16u;
    var wb = wid.x;
    loop {
        if (wb >= blocks) { break; }
        let base = wb * 16u;
        for (var t = lid; t < 512u; t = t + 256u) {
            let r = base + (t >> 5u);
            if (r < rows) {
                let pr = unpack2x16float(q1w[params_w + r]);
                lad_q4w[t] = exp2(pr.x + f32(t & 31u) * pr.y);
            }
        }
        workgroupBarrier();
        let r0 = base + sub;
        let r1 = base + sub + 4u;
        let r2 = base + sub + 8u;
        let r3 = base + sub + 12u;
        var acc = vec4<f32>(0.0);
        if (r0 < rows) {
            let c0 = codes_b + r0 * cstride;
            let c1 = codes_b + r1 * cstride;
            let c2 = codes_b + r2 * cstride;
            let c3 = codes_b + r3 * cstride;
            let l1 = r1 < rows;
            let l2 = r2 < rows;
            let l3 = r3 < rows;
            var g = l;
            loop {
                if (g >= gpr) { break; }
                let bit = g * 5u;
                let cbo = bit >> 3u;
                let sh = bit & 7u;
                let x0 = g * 8u;
                let xa = q4v_x[x0];      let xb = q4v_x[x0 + 1u];
                let xc = q4v_x[x0 + 2u]; let xd = q4v_x[x0 + 3u];
                let xe = q4v_x[x0 + 4u]; let xf = q4v_x[x0 + 5u];
                let xg = q4v_x[x0 + 6u]; let xh = q4v_x[x0 + 7u];
                var cv = q4tp_byte(c0 + cbo);
                if (sh > 3u) { cv = cv | (q4tp_byte(c0 + cbo + 1u) << 8u); }
                var v = q4v_w[r0 * gpr + g];
                acc.x = acc.x + lad_q4w[(sub << 5u) + ((cv >> sh) & 31u)]
                    * (q4v_dot8(v.x, xa, xb) + q4v_dot8(v.y, xc, xd)
                     + q4v_dot8(v.z, xe, xf) + q4v_dot8(v.w, xg, xh));
                if (l1) {
                    cv = q4tp_byte(c1 + cbo);
                    if (sh > 3u) { cv = cv | (q4tp_byte(c1 + cbo + 1u) << 8u); }
                    v = q4v_w[r1 * gpr + g];
                    acc.y = acc.y + lad_q4w[128u + (sub << 5u) + ((cv >> sh) & 31u)]
                        * (q4v_dot8(v.x, xa, xb) + q4v_dot8(v.y, xc, xd)
                         + q4v_dot8(v.z, xe, xf) + q4v_dot8(v.w, xg, xh));
                }
                if (l2) {
                    cv = q4tp_byte(c2 + cbo);
                    if (sh > 3u) { cv = cv | (q4tp_byte(c2 + cbo + 1u) << 8u); }
                    v = q4v_w[r2 * gpr + g];
                    acc.z = acc.z + lad_q4w[256u + (sub << 5u) + ((cv >> sh) & 31u)]
                        * (q4v_dot8(v.x, xa, xb) + q4v_dot8(v.y, xc, xd)
                         + q4v_dot8(v.z, xe, xf) + q4v_dot8(v.w, xg, xh));
                }
                if (l3) {
                    cv = q4tp_byte(c3 + cbo);
                    if (sh > 3u) { cv = cv | (q4tp_byte(c3 + cbo + 1u) << 8u); }
                    v = q4v_w[r3 * gpr + g];
                    acc.w = acc.w + lad_q4w[384u + (sub << 5u) + ((cv >> sh) & 31u)]
                        * (q4v_dot8(v.x, xa, xb) + q4v_dot8(v.y, xc, xd)
                         + q4v_dot8(v.z, xe, xf) + q4v_dot8(v.w, xg, xh));
                }
                g = g + 64u;
            }
        }
        partial_q4k[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (l < stride) {
                partial_q4k[lid] = partial_q4k[lid] + partial_q4k[lid + stride];
            }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (l == 0u) {
            let r = partial_q4k[sub << 6u];
            if (r0 < rows) { q1y[r0] = r.x; }
            if (r1 < rows) { q1y[r1] = r.y; }
            if (r2 < rows) { q1y[r2] = r.z; }
            if (r3 < rows) { q1y[r3] = r.w; }
        }
        workgroupBarrier();
        wb = wb + nwg.x;
    }
}

// ── q4tp matvec, k-in-registers batch: ONE weight decode serves up to
// FOUR activation vectors. The nb-dispatch batch streams the whole weight
// once per element and hopes for L2; at 2-8 MB a layer there is nothing
// left to hope with, and a k=3 verify paid the weight bandwidth three
// times. Here the batch lives in a vec4 accumulator (named, constant
// -indexed — a dynamically indexed array would spill to stack and run at
// a fraction of the card, the register-spill lesson). Rows past kb read
// clamped garbage and are zeroed by the mask.
var<workgroup> partial_q4k: array<vec4<f32>, 256>;
var<workgroup> partial_q4kb: array<vec4<f32>, 256>;
@compute @workgroup_size(256)
fn q4tp_matvec4_k(@builtin(workgroup_id) wid: vec3<u32>,
                  @builtin(num_workgroups) nwg: vec3<u32>,
                  @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let kb = max(q1p._p0, 1u);
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    let sub = lid >> 6u;
    let l = lid & 63u;
    let xr = gpr * 8u;
    let mask = vec4<f32>(
        1.0,
        select(0.0, 1.0, kb > 1u),
        select(0.0, 1.0, kb > 2u),
        select(0.0, 1.0, kb > 3u),
    );
    let blocks = (rows + 7u) / 8u;
    var wb = wid.x;
    loop {
        if (wb >= blocks) { break; }
        let base = wb * 8u;
        {
            let r = base + (lid >> 5u);
            if (r < rows) {
                let pr = unpack2x16float(q1w[params_w + r]);
                lad_q4v[lid] = exp2(pr.x + f32(lid & 31u) * pr.y);
            }
        }
        workgroupBarrier();
        let wrow_a = base + sub;
        let wrow_b = base + sub + 4u;
        let live_b = wrow_b < rows;
        var acc_a = vec4<f32>(0.0);
        var acc_b = vec4<f32>(0.0);
        if (wrow_a < rows) {
            let crow_a = codes_b + wrow_a * cstride;
            let crow_b = codes_b + wrow_b * cstride;
            var g = l;
            loop {
                if (g >= gpr) { break; }
                let bit = g * 5u;
                let cbo = bit >> 3u;
                let sh = bit & 7u;
                var cv_a = q4tp_byte(crow_a + cbo);
                if (sh > 3u) { cv_a = cv_a | (q4tp_byte(crow_a + cbo + 1u) << 8u); }
                let v_a = q4v_w[wrow_a * gpr + g];
                let sa = lad_q4v[(sub << 5u) + ((cv_a >> sh) & 31u)];
                let x0 = g * 8u;
                let d_a = vec4<f32>(
                    q4v_dot8(v_a.x, q4v_x[x0], q4v_x[x0 + 1u])
                        + q4v_dot8(v_a.y, q4v_x[x0 + 2u], q4v_x[x0 + 3u])
                        + q4v_dot8(v_a.z, q4v_x[x0 + 4u], q4v_x[x0 + 5u])
                        + q4v_dot8(v_a.w, q4v_x[x0 + 6u], q4v_x[x0 + 7u]),
                    q4v_dot8(v_a.x, q4v_x[xr + x0], q4v_x[xr + x0 + 1u])
                        + q4v_dot8(v_a.y, q4v_x[xr + x0 + 2u], q4v_x[xr + x0 + 3u])
                        + q4v_dot8(v_a.z, q4v_x[xr + x0 + 4u], q4v_x[xr + x0 + 5u])
                        + q4v_dot8(v_a.w, q4v_x[xr + x0 + 6u], q4v_x[xr + x0 + 7u]),
                    q4v_dot8(v_a.x, q4v_x[2u * xr + x0], q4v_x[2u * xr + x0 + 1u])
                        + q4v_dot8(v_a.y, q4v_x[2u * xr + x0 + 2u], q4v_x[2u * xr + x0 + 3u])
                        + q4v_dot8(v_a.z, q4v_x[2u * xr + x0 + 4u], q4v_x[2u * xr + x0 + 5u])
                        + q4v_dot8(v_a.w, q4v_x[2u * xr + x0 + 6u], q4v_x[2u * xr + x0 + 7u]),
                    q4v_dot8(v_a.x, q4v_x[3u * xr + x0], q4v_x[3u * xr + x0 + 1u])
                        + q4v_dot8(v_a.y, q4v_x[3u * xr + x0 + 2u], q4v_x[3u * xr + x0 + 3u])
                        + q4v_dot8(v_a.z, q4v_x[3u * xr + x0 + 4u], q4v_x[3u * xr + x0 + 5u])
                        + q4v_dot8(v_a.w, q4v_x[3u * xr + x0 + 6u], q4v_x[3u * xr + x0 + 7u]),
                );
                acc_a = acc_a + sa * d_a * mask;
                if (live_b) {
                    var cv_b = q4tp_byte(crow_b + cbo);
                    if (sh > 3u) { cv_b = cv_b | (q4tp_byte(crow_b + cbo + 1u) << 8u); }
                    let v_b = q4v_w[wrow_b * gpr + g];
                    let sb = lad_q4v[128u + (sub << 5u) + ((cv_b >> sh) & 31u)];
                    let d_b = vec4<f32>(
                        q4v_dot8(v_b.x, q4v_x[x0], q4v_x[x0 + 1u])
                            + q4v_dot8(v_b.y, q4v_x[x0 + 2u], q4v_x[x0 + 3u])
                            + q4v_dot8(v_b.z, q4v_x[x0 + 4u], q4v_x[x0 + 5u])
                            + q4v_dot8(v_b.w, q4v_x[x0 + 6u], q4v_x[x0 + 7u]),
                        q4v_dot8(v_b.x, q4v_x[xr + x0], q4v_x[xr + x0 + 1u])
                            + q4v_dot8(v_b.y, q4v_x[xr + x0 + 2u], q4v_x[xr + x0 + 3u])
                            + q4v_dot8(v_b.z, q4v_x[xr + x0 + 4u], q4v_x[xr + x0 + 5u])
                            + q4v_dot8(v_b.w, q4v_x[xr + x0 + 6u], q4v_x[xr + x0 + 7u]),
                        q4v_dot8(v_b.x, q4v_x[2u * xr + x0], q4v_x[2u * xr + x0 + 1u])
                            + q4v_dot8(v_b.y, q4v_x[2u * xr + x0 + 2u], q4v_x[2u * xr + x0 + 3u])
                            + q4v_dot8(v_b.z, q4v_x[2u * xr + x0 + 4u], q4v_x[2u * xr + x0 + 5u])
                            + q4v_dot8(v_b.w, q4v_x[2u * xr + x0 + 6u], q4v_x[2u * xr + x0 + 7u]),
                        q4v_dot8(v_b.x, q4v_x[3u * xr + x0], q4v_x[3u * xr + x0 + 1u])
                            + q4v_dot8(v_b.y, q4v_x[3u * xr + x0 + 2u], q4v_x[3u * xr + x0 + 3u])
                            + q4v_dot8(v_b.z, q4v_x[3u * xr + x0 + 4u], q4v_x[3u * xr + x0 + 5u])
                            + q4v_dot8(v_b.w, q4v_x[3u * xr + x0 + 6u], q4v_x[3u * xr + x0 + 7u]),
                    );
                    acc_b = acc_b + sb * d_b * mask;
                }
                g = g + 64u;
            }
        }
        partial_q4k[lid] = acc_a;
        partial_q4kb[lid] = acc_b;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (l < stride) {
                partial_q4k[lid] = partial_q4k[lid] + partial_q4k[lid + stride];
                partial_q4kb[lid] = partial_q4kb[lid] + partial_q4kb[lid + stride];
            }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (l == 0u && wrow_a < rows) {
            let ra = partial_q4k[sub << 6u];
            q1y[wrow_a] = ra.x;
            if (kb > 1u) { q1y[rows + wrow_a] = ra.y; }
            if (kb > 2u) { q1y[2u * rows + wrow_a] = ra.z; }
            if (kb > 3u) { q1y[3u * rows + wrow_a] = ra.w; }
        }
        if (l == 0u && live_b) {
            let rb = partial_q4kb[sub << 6u];
            q1y[wrow_b] = rb.x;
            if (kb > 1u) { q1y[rows + wrow_b] = rb.y; }
            if (kb > 2u) { q1y[2u * rows + wrow_b] = rb.z; }
            if (kb > 3u) { q1y[3u * rows + wrow_b] = rb.w; }
        }
        workgroupBarrier();
        wb = wb + nwg.x;
    }
}

// ── Fold-select MoE twins: gu/down recompute the top-k FROM THE ROUTER
// LOGITS inside every workgroup — redundant arithmetic, but the serial
// select hop disappears and the layer chain loses one ~25 us dispatch
// latency. The comparator (max, lowest index on ties) is order-free, so
// every workgroup lands on the same experts; softmax summation order
// differs from the retired select kernel only in reduction shape.
// Slot 3 carries the LOGITS where the plain twins carry the selection.
@group(0) @binding(3) var<storage, read> mgf_logit : array<f32>;
struct MgfP { n_exp: u32, _a: u32, _b: u32, _c: u32 };
@group(0) @binding(7) var<uniform> mgf_p : MgfP;

var<workgroup> mgf_lg: array<f32, 256>;
var<workgroup> mgf_v:  array<f32, 64>;
var<workgroup> mgf_i:  array<u32, 64>;

// top-(slot+1) of n logits with 64 lanes; returns the slot'th expert id.
fn mgf_pick(slot: u32, n: u32, lid: u32) -> u32 {
    var chosen = 0u;
    for (var s = 0u; s <= slot; s = s + 1u) {
        var best = -3.0e38;
        var bi = 0xFFFFu;
        var i = lid;
        loop {
            if (i >= n) { break; }
            let v = mgf_lg[i];
            if (v > best || (v == best && i < bi)) { best = v; bi = i; }
            i = i + 64u;
        }
        mgf_v[lid] = best;
        mgf_i[lid] = bi;
        workgroupBarrier();
        var st = 32u;
        loop {
            if (st == 0u) { break; }
            if (lid < st) {
                let b = mgf_v[lid + st];
                let ib = mgf_i[lid + st];
                if (b > mgf_v[lid] || (b == mgf_v[lid] && ib < mgf_i[lid])) {
                    mgf_v[lid] = b;
                    mgf_i[lid] = ib;
                }
            }
            workgroupBarrier();
            st = st >> 1u;
        }
        chosen = mgf_i[0];
        workgroupBarrier();
        if (lid == 0u && s < slot) { mgf_lg[chosen] = -3.0e38; }
        workgroupBarrier();
    }
    return chosen;
}

@compute @workgroup_size(64)
fn moe_gate_up_q2tp_f(@builtin(workgroup_id) wid: vec3<u32>,
                      @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let slot = wid.y;
    let gpr = mg_p.gpr;
    let rows = mg_p.inter;
    let n = mgf_p.n_exp;
    let mat16 = mg_p.mat16;
    // Stage logits once (shared expert = last slot, id n).
    var i = lid;
    loop {
        if (i >= n) { break; }
        mgf_lg[i] = mgf_logit[i];
        i = i + 64u;
    }
    workgroupBarrier();
    var id = n;
    if (slot < mg_p.slots - 1u) {
        id = mgf_pick(slot, n, lid);
    }
    let base16 = id * mat16;
    let nib16 = base16 + row * gpr * 4u;
    let par16 = base16 + rows * gpr * 4u + row * 2u;
    let cst = (gpr * 5u + 7u) / 8u;
    let cod8 = (base16 + rows * gpr * 4u + rows * 2u) * 2u + row * cst;

    let gl = unpack2x16float(mg_g16(par16) | (mg_g16(par16 + 1u) << 16u));
    let ul = unpack2x16float(mg_u16f(par16) | (mg_u16f(par16 + 1u) << 16u));
    var ag = 0.0;
    var au = 0.0;
    for (var g = lid; g < gpr; g = g + 64u) {
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cg = mgp_gu8(cod8 + cb);
        var cu = mgp_uu8(cod8 + cb);
        if (shf > 3u) {
            cg = cg | (mgp_gu8(cod8 + cb + 1u) << 8u);
            cu = cu | (mgp_uu8(cod8 + cb + 1u) << 8u);
        }
        let cgv = (cg >> shf) & 31u;
        let cuv = (cu >> shf) & 31u;
        let sg = select(exp2(gl.x + f32(max(cgv, 1u) - 1u) * gl.y), 0.0, cgv == 0u);
        let su = select(exp2(ul.x + f32(max(cuv, 1u) - 1u) * ul.y), 0.0, cuv == 0u);
        let w32 = (nib16 + g * 4u) >> 1u;
        let xq = g * 8u;
        let x0 = mg_xv[xq];      let x1 = mg_xv[xq + 1u];
        let x2 = mg_xv[xq + 2u]; let x3 = mg_xv[xq + 3u];
        let x4 = mg_xv[xq + 4u]; let x5 = mg_xv[xq + 5u];
        let x6 = mg_xv[xq + 6u]; let x7 = mg_xv[xq + 7u];
        let dg = mg_dot16v(mg_gw[w32], x0, x1, x2, x3)
               + mg_dot16v(mg_gw[w32 + 1u], x4, x5, x6, x7);
        let du = mg_dot16v(mg_uw[w32], x0, x1, x2, x3)
               + mg_dot16v(mg_uw[w32 + 1u], x4, x5, x6, x7);
        ag = ag + sg * dg;
        au = au + su * du;
    }
    mg_pg[lid] = ag;
    mg_pu[lid] = au;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) {
            mg_pg[lid] = mg_pg[lid] + mg_pg[lid + stride];
            mg_pu[lid] = mg_pu[lid] + mg_pu[lid + stride];
        }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) {
        let g = mg_pg[0];
        var gg = g;
        var uu = mg_pu[0];
        if (mg_p.lim > 0.0) {
            uu = clamp(uu, -mg_p.lim, mg_p.lim);
            gg = min(gg, mg_p.lim);
        }
        mg_act[slot * mg_p.inter + row] = (gg / (1.0 + exp(-gg))) * uu;
    }
}

// down twin: recomputes ids AND weights (softmax over picked + shared
// sigmoid). Slot 2 = logits, slot 3 = shared-gate weight (f32 bits),
// slot 6 = the token's activations for the shared-gate dot.
@group(0) @binding(2) var<storage, read> mdf_logit : array<f32>;
@group(0) @binding(3) var<storage, read> mdf_sgw   : array<u32>;
@group(0) @binding(6) var<storage, read> mdf_x     : array<f32>;
struct MdfP { n_exp: u32, top_k: u32, norm: u32, pk: u32 };
@group(0) @binding(7) var<uniform> mdf_p : MdfP;

var<workgroup> mdf_lg: array<f32, 256>;
var<workgroup> mdf_v:  array<f32, 64>;
var<workgroup> mdf_i:  array<u32, 64>;
var<workgroup> mdf_sel: array<u32, 16>;
var<workgroup> mdf_wt:  array<f32, 16>;

@compute @workgroup_size(64)
fn moe_down_q4tp_f(@builtin(workgroup_id) wid: vec3<u32>,
                   @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let gpr = md_p.gpr;
    let rows = md_p.hidden;
    let n = mdf_p.n_exp;
    let kk = mdf_p.top_k;
    let sg_kind = mdf_p.pk & 0xFFu;
    let sg_hidden = mdf_p.pk >> 8u;
    // shared gate dot (same strided shape as the retired select kernel,
    // 64 lanes instead of 256)
    var sgv = 0.0;
    if (sg_kind == 4u) {
        var d = 0.0;
        var i = lid;
        loop {
            if (i >= sg_hidden) { break; }
            d = d + bitcast<f32>(mdf_sgw[i]) * mdf_x[i];
            i = i + 64u;
        }
        mdf_v[lid] = d;
        workgroupBarrier();
        var st = 32u;
        loop {
            if (st == 0u) { break; }
            if (lid < st) { mdf_v[lid] = mdf_v[lid] + mdf_v[lid + st]; }
            workgroupBarrier();
            st = st >> 1u;
        }
        sgv = mdf_v[0];
        workgroupBarrier();
    }
    // logits + max + denom with 64-lane reductions
    var i2 = lid;
    loop {
        if (i2 >= n) { break; }
        mdf_lg[i2] = mdf_logit[i2];
        i2 = i2 + 64u;
    }
    workgroupBarrier();
    var mbest = -3.0e38;
    var i3 = lid;
    loop {
        if (i3 >= n) { break; }
        mbest = max(mbest, mdf_lg[i3]);
        i3 = i3 + 64u;
    }
    mdf_v[lid] = mbest;
    workgroupBarrier();
    var st2 = 32u;
    loop {
        if (st2 == 0u) { break; }
        if (lid < st2) { mdf_v[lid] = max(mdf_v[lid], mdf_v[lid + st2]); }
        workgroupBarrier();
        st2 = st2 >> 1u;
    }
    let mx = mdf_v[0];
    workgroupBarrier();
    var dsum = 0.0;
    var i4 = lid;
    loop {
        if (i4 >= n) { break; }
        dsum = dsum + exp(mdf_lg[i4] - mx);
        i4 = i4 + 64u;
    }
    mdf_v[lid] = dsum;
    workgroupBarrier();
    st2 = 32u;
    loop {
        if (st2 == 0u) { break; }
        if (lid < st2) { mdf_v[lid] = mdf_v[lid] + mdf_v[lid + st2]; }
        workgroupBarrier();
        st2 = st2 >> 1u;
    }
    let denom = mdf_v[0];
    workgroupBarrier();
    // top-k, weights, optional renorm; shared expert last
    var wsum = 0.0;
    for (var s = 0u; s < kk; s = s + 1u) {
        var best = -3.0e38;
        var bi = 0xFFFFu;
        var i5 = lid;
        loop {
            if (i5 >= n) { break; }
            let v = mdf_lg[i5];
            if (v > best || (v == best && i5 < bi)) { best = v; bi = i5; }
            i5 = i5 + 64u;
        }
        mdf_v[lid] = best;
        mdf_i[lid] = bi;
        workgroupBarrier();
        var st3 = 32u;
        loop {
            if (st3 == 0u) { break; }
            if (lid < st3) {
                let b = mdf_v[lid + st3];
                let ib = mdf_i[lid + st3];
                if (b > mdf_v[lid] || (b == mdf_v[lid] && ib < mdf_i[lid])) {
                    mdf_v[lid] = b;
                    mdf_i[lid] = ib;
                }
            }
            workgroupBarrier();
            st3 = st3 >> 1u;
        }
        if (lid == 0u) {
            mdf_sel[s] = mdf_i[0];
            mdf_wt[s] = exp(mdf_v[0] - mx) / denom;
        }
        workgroupBarrier();
        wsum = wsum + exp(mdf_v[0] - mx) / denom;
        if (lid == 0u) { mdf_lg[mdf_i[0]] = -3.0e38; }
        workgroupBarrier();
    }
    if (lid == 0u) {
        if (mdf_p.norm != 0u) {
            for (var s = 0u; s < kk; s = s + 1u) { mdf_wt[s] = mdf_wt[s] / wsum; }
        }
        mdf_sel[kk] = n;
        mdf_wt[kk] = 1.0 / (1.0 + exp(-sgv));
    }
    workgroupBarrier();
    let cst = (gpr * 5u + 7u) / 8u;
    let total = md_p.slots * gpr;
    var acc = 0.0;
    for (var i6 = lid; i6 < total; i6 = i6 + 64u) {
        let slot = i6 / gpr;
        let g = i6 % gpr;
        let base16 = mdf_sel[slot] * md_p.mat16;
        let par16 = base16 + rows * gpr * 8u + row * 2u;
        let cod8 = (base16 + rows * gpr * 8u + rows * 2u) * 2u + row * cst;
        let pl = unpack2x16float(md_u16(par16) | (md_u16(par16 + 1u) << 16u));
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cv = mdp_u8(cod8 + cb);
        if (shf > 3u) { cv = cv | (mdp_u8(cod8 + cb + 1u) << 8u); }
        let scale = exp2(pl.x + f32((cv >> shf) & 31u) * pl.y);
        let t16 = base16 + (row * gpr + g) * 8u;
        let xb = (slot * gpr + g) * 32u;
        var d = 0.0;
        for (var k2 = 0u; k2 < 4u; k2 = k2 + 1u) {
            let w = md_u16(t16 + 2u * k2) | (md_u16(t16 + 1u + 2u * k2) << 16u);
            d = d + md_dot8(w, xb + 8u * k2);
        }
        acc = acc + mdf_wt[slot] * scale * d;
    }
    md_pt[lid] = acc;
    workgroupBarrier();
    var st4 = 32u;
    loop {
        if (st4 == 0u) { break; }
        if (lid < st4) { md_pt[lid] = md_pt[lid] + md_pt[lid + st4]; }
        workgroupBarrier();
        st4 = st4 >> 1u;
    }
    if (lid == 0u) { md_y[row] = md_pt[0]; }
}

// ── Multi-step greedy tail: argmax over the logits on the device, then
// re-embed the winner — k decode steps ride ONE submit and the CPU sees
// k token ids instead of k megabytes of logits. Ties pick an arbitrary
// maximal index (same class as the documented GPU float-order ties).
struct AmP { n: u32, parts: u32, st: u32, _p: u32 };
@group(0) @binding(0) var<storage, read>       am_x   : array<f32>;
@group(0) @binding(1) var<storage, read_write> am_pv  : array<f32>;
@group(0) @binding(2) var<storage, read_write> am_pi  : array<u32>;
@group(0) @binding(3) var<uniform>             am_p   : AmP;
var<workgroup> am_wv: array<f32, 256>;
var<workgroup> am_wi: array<u32, 256>;

@compute @workgroup_size(256)
fn argmax_part(@builtin(workgroup_id) wid: vec3<u32>,
               @builtin(local_invocation_index) lid: u32) {
    var best = -3.0e38;
    var bi = 0u;
    var i = wid.x * 256u + lid;
    let stride = am_p.parts * 256u;
    loop {
        if (i >= am_p.n) { break; }
        let v = am_x[i];
        if (v > best) { best = v; bi = i; }
        i = i + stride;
    }
    am_wv[lid] = best;
    am_wi[lid] = bi;
    workgroupBarrier();
    var s = 128u;
    loop {
        if (s == 0u) { break; }
        if (lid < s && am_wv[lid + s] > am_wv[lid]) {
            am_wv[lid] = am_wv[lid + s];
            am_wi[lid] = am_wi[lid + s];
        }
        workgroupBarrier();
        s = s >> 1u;
    }
    if (lid == 0u) {
        am_pv[wid.x] = am_wv[0];
        am_pi[wid.x] = am_wi[0];
    }
}

@group(0) @binding(0) var<storage, read>       af_pv : array<f32>;
@group(0) @binding(1) var<storage, read>       af_pi : array<u32>;
@group(0) @binding(2) var<storage, read_write> af_ids: array<u32>;
@group(0) @binding(3) var<uniform>             af_p  : AmP;
var<workgroup> af_wv: array<f32, 256>;
var<workgroup> af_wi: array<u32, 256>;

@compute @workgroup_size(256)
fn argmax_final(@builtin(local_invocation_index) lid: u32) {
    var best = -3.0e38;
    var bi = 0u;
    var i = lid;
    loop {
        if (i >= af_p.parts) { break; }
        if (af_pv[i] > best) { best = af_pv[i]; bi = af_pi[i]; }
        i = i + 256u;
    }
    af_wv[lid] = best;
    af_wi[lid] = bi;
    workgroupBarrier();
    var s = 128u;
    loop {
        if (s == 0u) { break; }
        if (lid < s && af_wv[lid + s] > af_wv[lid]) {
            af_wv[lid] = af_wv[lid + s];
            af_wi[lid] = af_wi[lid + s];
        }
        workgroupBarrier();
        s = s >> 1u;
    }
    if (lid == 0u) { af_ids[af_p.st] = af_wi[0]; }
}

// One thread = one hidden element of the winner's q4tp embedding row.
// `mult` carries the model's embed multiplier as f32 bits.
struct EgP { hidden: u32, gpr: u32, rows: u32, st: u32, mult: u32, _a: u32, _b: u32, _c: u32 };
@group(0) @binding(0) var<storage, read>       eg_w  : array<u32>;
@group(0) @binding(1) var<storage, read>       eg_ids: array<u32>;
@group(0) @binding(2) var<storage, read_write> eg_h  : array<f32>;
@group(0) @binding(3) var<uniform>             eg_p  : EgP;

fn eg_byte(off: u32) -> u32 {
    return (eg_w[off >> 2u] >> ((off & 3u) * 8u)) & 0xFFu;
}

@compute @workgroup_size(256)
fn embed_gather_q4tp(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    if (i >= eg_p.hidden) { return; }
    let r = eg_ids[eg_p.st];
    let gpr = eg_p.gpr;
    let g = i / 32u;
    let k = i % 32u;
    let params_w = eg_p.rows * gpr * 4u;
    let codes_b = eg_p.rows * gpr * 16u + eg_p.rows * 4u;
    let cst = (gpr * 5u + 7u) / 8u;
    let pr = unpack2x16float(eg_w[params_w + r]);
    let bit = g * 5u;
    let cb = codes_b + r * cst + (bit >> 3u);
    let sh = bit & 7u;
    var cv = eg_byte(cb);
    if (sh > 3u) { cv = cv | (eg_byte(cb + 1u) << 8u); }
    let sc = exp2(pr.x + f32((cv >> sh) & 31u) * pr.y);
    let nb = (r * gpr + g) * 16u + (k >> 1u);
    let byte = eg_byte(nb);
    let q = select(byte & 0xFu, (byte >> 4u) & 0xFu, (k & 1u) == 1u);
    eg_h[i] = (f32(q) - 8.0) * sc * bitcast<f32>(eg_p.mult);
}

// Fused SiLU(gate)·up → Q4Block down-proj matvec: eliminates the standalone
// silu dispatch (saves one inter-pass pipeline flush per layer).
@group(0) @binding(0) var<storage, read>       sd_w : array<u32>;
@group(0) @binding(1) var<storage, read>       sd_gate : array<f32>;
@group(0) @binding(2) var<storage, read>       sd_up : array<f32>;
@group(0) @binding(3) var<storage, read_write> sd_y : array<f32>;
@group(0) @binding(4) var<uniform>             sd_p : Q1Params;

var<workgroup> partial_sd: array<f32, 64>;

fn sd_dot8(w: u32, xi: u32) -> f32 {
    let g0 = sd_gate[xi];     let g1 = sd_gate[xi + 1u];
    let g2 = sd_gate[xi + 2u]; let g3 = sd_gate[xi + 3u];
    let g4 = sd_gate[xi + 4u]; let g5 = sd_gate[xi + 5u];
    let g6 = sd_gate[xi + 6u]; let g7 = sd_gate[xi + 7u];
    return (f32(w & 0xFu) - 8.0) * (g0 / (1.0 + exp(-g0)) * sd_up[xi])
         + (f32((w >> 4u) & 0xFu) - 8.0) * (g1 / (1.0 + exp(-g1)) * sd_up[xi + 1u])
         + (f32((w >> 8u) & 0xFu) - 8.0) * (g2 / (1.0 + exp(-g2)) * sd_up[xi + 2u])
         + (f32((w >> 12u) & 0xFu) - 8.0) * (g3 / (1.0 + exp(-g3)) * sd_up[xi + 3u])
         + (f32((w >> 16u) & 0xFu) - 8.0) * (g4 / (1.0 + exp(-g4)) * sd_up[xi + 4u])
         + (f32((w >> 20u) & 0xFu) - 8.0) * (g5 / (1.0 + exp(-g5)) * sd_up[xi + 5u])
         + (f32((w >> 24u) & 0xFu) - 8.0) * (g6 / (1.0 + exp(-g6)) * sd_up[xi + 6u])
         + (f32((w >> 28u) & 0xFu) - 8.0) * (g7 / (1.0 + exp(-g7)) * sd_up[xi + 7u]);
}

@compute @workgroup_size(64)
fn silu_down_matvec(@builtin(workgroup_id) wid: vec3<u32>,
                    @builtin(num_workgroups) nwg: vec3<u32>,
                    @builtin(local_invocation_index) lid: u32) {
    let gpr = sd_p.np;
    let rows = sd_p.rows;
    let scales_off = rows * gpr * 16u;
    var row = wid.x;
    loop {
        if (row >= rows) { break; }
        var acc = 0.0;
        var g = lid;
        loop {
            if (g >= gpr) { break; }
            let gi = row * gpr + g;
            let sc_byte = scales_off + gi * 2u;
            let sc16 = (sd_w[sc_byte >> 2u] >> ((sc_byte & 3u) * 8u)) & 0xFFFFu;
            let scale = unpack2x16float(sc16).x;
            let pk4 = gi * 4u;
            let xb = g * 32u;
            let gsum = sd_dot8(sd_w[pk4], xb)
                     + sd_dot8(sd_w[pk4 + 1u], xb + 8u)
                     + sd_dot8(sd_w[pk4 + 2u], xb + 16u)
                     + sd_dot8(sd_w[pk4 + 3u], xb + 24u);
            acc = acc + scale * gsum;
            g = g + 64u;
        }
        partial_sd[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { partial_sd[lid] = partial_sd[lid] + partial_sd[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) { sd_y[row] = partial_sd[0]; }
        workgroupBarrier();
        row = row + nwg.x;
    }
}

// q1t register-blocked GEMM (prefill) — the WGSL cousin of the Metal q1t_mul_mm
// and structurally identical to q8_mul_mm here; only the W staging decodes
// base-3 ternary × per-group f16 scale (no row_scale; scale folds into the
// staged weight). Own 4-slot bindings. The overlay is a second pass.
struct Q1tMmP { cols4: u32, rows: u32, nb: u32, _p: u32 };
@group(0) @binding(0) var<storage, read>       qmm : array<u32>;
@group(0) @binding(1) var<storage, read>       xmm : array<f32>;
// The same activations as vec4 (same-slot rule): the GEMM stages four
// consecutive floats per thread per K-step, and col0 is a multiple of 4,
// so that is one 16-byte load instead of four scalar ones.
@group(0) @binding(1) var<storage, read>       xmm4 : array<vec4<f32>>;
@group(0) @binding(2) var<storage, read_write> ymm : array<f32>;
@group(0) @binding(3) var<uniform>             pmm : Q1tMmP;

fn qmm_byte(off: u32) -> u32 {
    return (qmm[off >> 2u] >> ((off & 3u) * 8u)) & 0xFFu;
}
var<workgroup> q1t_at: array<f32, 64 * 16>;
var<workgroup> q1t_wt: array<f32, 64 * 16>;

@compute @workgroup_size(16, 16)
fn q1t_mul_mm(@builtin(workgroup_id) wid: vec3<u32>,
              @builtin(local_invocation_id) lid: vec3<u32>) {
    let cols = pmm.cols4 * 4u;
    let gpr = cols >> 5u;
    let m0 = wid.y * 64u;
    let n0 = wid.x * 64u;
    let tid = lid.y * 16u + lid.x;
    // Sixteen named scalars, not array<array<f32,4>,4> — see q4t_mul_mm.
    var a00 = 0.0; var a01 = 0.0; var a02 = 0.0; var a03 = 0.0;
    var a10 = 0.0; var a11 = 0.0; var a12 = 0.0; var a13 = 0.0;
    var a20 = 0.0; var a21 = 0.0; var a22 = 0.0; var a23 = 0.0;
    var a30 = 0.0; var a31 = 0.0; var a32 = 0.0; var a33 = 0.0;
    var k0 = 0u;
    loop {
        if (k0 >= cols) { break; }
        for (var t = tid; t < 64u * 4u; t = t + 256u) {
            let m = t / 4u;
            let k4 = t % 4u;
            var xv = vec4<f32>(0.0);
            let col0 = k0 + k4 * 4u;
            if (m0 + m < pmm.nb && col0 < cols) {
                // cols is a multiple of 32 and col0 of 4 — vec4-aligned.
                xv = xmm4[((m0 + m) * cols + col0) >> 2u];
            }
            let dst = m * 16u + k4 * 4u;
            q1t_at[dst] = xv.x; q1t_at[dst + 1u] = xv.y;
            q1t_at[dst + 2u] = xv.z; q1t_at[dst + 3u] = xv.w;
        }
        for (var t = tid; t < 64u * 4u; t = t + 256u) {
            let n = t / 4u;
            let k4 = t % 4u;
            var wv = vec4<f32>(0.0);
            let col0 = k0 + k4 * 4u;
            if (n0 + n < pmm.rows && col0 < cols) {
                let g = col0 >> 5u;
                let toff = ((n0 + n) * gpr + g) * 9u;
                let sc16 = qmm_byte(toff) | (qmm_byte(toff + 1u) << 8u);
                let scale = unpack2x16float(sc16).x;
                let codes = toff + 2u;
                for (var d = 0u; d < 4u; d = d + 1u) {
                    let p = (col0 + d) - g * 32u;
                    let b = qmm_byte(codes + p / 5u);
                    let code = (Q1T_LUT[b] >> ((p % 5u) * 2u)) & 3u;
                    var sgn = 0.0;
                    if (code == 1u) { sgn = 1.0; } else if (code == 2u) { sgn = -1.0; }
                    wv[d] = sgn * scale;
                }
            }
            let dst = n * 16u + k4 * 4u;
            q1t_wt[dst] = wv.x; q1t_wt[dst + 1u] = wv.y;
            q1t_wt[dst + 2u] = wv.z; q1t_wt[dst + 3u] = wv.w;
        }
        workgroupBarrier();
        let ab = lid.y * 64u;
        let wb = lid.x * 64u;
        for (var k = 0u; k < 16u; k = k + 1u) {
            let x0 = q1t_at[ab + k];
            let x1 = q1t_at[ab + 16u + k];
            let x2 = q1t_at[ab + 32u + k];
            let x3 = q1t_at[ab + 48u + k];
            let y0 = q1t_wt[wb + k];
            let y1 = q1t_wt[wb + 16u + k];
            let y2 = q1t_wt[wb + 32u + k];
            let y3 = q1t_wt[wb + 48u + k];
            a00 = a00 + x0 * y0; a01 = a01 + x0 * y1;
            a02 = a02 + x0 * y2; a03 = a03 + x0 * y3;
            a10 = a10 + x1 * y0; a11 = a11 + x1 * y1;
            a12 = a12 + x1 * y2; a13 = a13 + x1 * y3;
            a20 = a20 + x2 * y0; a21 = a21 + x2 * y1;
            a22 = a22 + x2 * y2; a23 = a23 + x2 * y3;
            a30 = a30 + x3 * y0; a31 = a31 + x3 * y1;
            a32 = a32 + x3 * y2; a33 = a33 + x3 * y3;
        }
        workgroupBarrier();
        k0 = k0 + 16u;
    }
    let mb = m0 + lid.y * 4u;
    let nb2 = n0 + lid.x * 4u;
    q4t_store4(mb, nb2, a00, a01, a02, a03);
    q4t_store4(mb + 1u, nb2, a10, a11, a12, a13);
    q4t_store4(mb + 2u, nb2, a20, a21, a22, a23);
    q4t_store4(mb + 3u, nb2, a30, a31, a32, a33);
}

// q4t register-blocked GEMM (imagegen DiT prefill / any wide q4t
// batch) — the WGSL cousin of the Metal q4t_mul_mm and structurally
// identical to q1t_mul_mm above; only the W staging decodes 18-byte
// q4t tiles (f16 scale + 16 nibble bytes per 32-weight group).
// Shares the 4-slot qmm/xmm/ymm/pmm bindings.
var<workgroup> q4t_at: array<f32, 64 * 16>;
var<workgroup> q4t_wt: array<f32, 64 * 16>;

fn q4t_store4(m: u32, n0: u32, v0: f32, v1: f32, v2: f32, v3: f32) {
    if (m >= pmm.nb) { return; }
    let base = m * pmm.rows + n0;
    if (n0 < pmm.rows) { ymm[base] = v0; }
    if (n0 + 1u < pmm.rows) { ymm[base + 1u] = v1; }
    if (n0 + 2u < pmm.rows) { ymm[base + 2u] = v2; }
    if (n0 + 3u < pmm.rows) { ymm[base + 3u] = v3; }
}

// q4tp register-blocked GEMM — the q4t kernel above with one block swapped:
// a 16 B nibble stride instead of the 18 B tile, and the scale off the row's
// ladder. Shares q4t_store4 and the q4t_at/q4t_wt staging arrays; only one
// entry point runs per dispatch, so the workgroup allocation is not doubled.
@compute @workgroup_size(16, 16)
fn q4tp_mul_mm(@builtin(workgroup_id) wid: vec3<u32>,
              @builtin(local_invocation_id) lid: vec3<u32>) {
    let cols = pmm.cols4 * 4u;
    let gpr = cols >> 5u;
    let m0 = wid.y * 64u;
    let n0 = wid.x * 64u;
    let tid = lid.y * 16u + lid.x;
    // The 4x4 register block is SIXTEEN NAMED SCALARS, not
    // array<array<f32,4>,4>: indexed by loop variables the array is a
    // private array, which this backend puts in stack memory — the
    // accumulators leave registers and the GEMM runs at a fraction of
    // the card (measured 373 GFLOP/s of an RTX 3090's ~35 TFLOP/s).
    var a00 = 0.0; var a01 = 0.0; var a02 = 0.0; var a03 = 0.0;
    var a10 = 0.0; var a11 = 0.0; var a12 = 0.0; var a13 = 0.0;
    var a20 = 0.0; var a21 = 0.0; var a22 = 0.0; var a23 = 0.0;
    var a30 = 0.0; var a31 = 0.0; var a32 = 0.0; var a33 = 0.0;
    var k0 = 0u;
    loop {
        if (k0 >= cols) { break; }
        for (var t = tid; t < 64u * 4u; t = t + 256u) {
            let m = t / 4u;
            let k4 = t % 4u;
            var xv = vec4<f32>(0.0);
            let col0 = k0 + k4 * 4u;
            if (m0 + m < pmm.nb && col0 < cols) {
                // cols is a multiple of 32 and col0 of 4 — vec4-aligned.
                xv = xmm4[((m0 + m) * cols + col0) >> 2u];
            }
            let dst = m * 16u + k4 * 4u;
            q4t_at[dst] = xv.x; q4t_at[dst + 1u] = xv.y;
            q4t_at[dst + 2u] = xv.z; q4t_at[dst + 3u] = xv.w;
        }
        for (var t = tid; t < 64u * 4u; t = t + 256u) {
            let n = t / 4u;
            let k4 = t % 4u;
            var wv = vec4<f32>(0.0);
            let col0 = k0 + k4 * 4u;
            if (n0 + n < pmm.rows && col0 < cols) {
                let g = col0 >> 5u;
                let wrow = n0 + n;
                let params_b = pmm.rows * gpr * 16u;
                let codes_b = params_b + pmm.rows * 4u;
                let cstride = (gpr * 5u + 7u) / 8u;
                let bit = g * 5u;
                let cb = codes_b + wrow * cstride + (bit >> 3u);
                let sh = bit & 7u;
                var cv = qmm_byte(cb);
                if (sh > 3u) { cv = cv | (qmm_byte(cb + 1u) << 8u); }
                // One exp2 per staged group of 4 — this thread stages exactly
                // one such group per K-step, and the GEMM's arithmetic hides
                // the chain that the matvec had to hoist out of its tile loop.
                let pr = unpack2x16float(qmm[(params_b >> 2u) + wrow]);
                let scale = exp2(pr.x + f32((cv >> sh) & 31u) * pr.y);
                // 4 consecutive weights = 2 nibble bytes (col0 is even).
                let toff = (wrow * gpr + g) * 16u;
                let p = col0 - g * 32u;
                // The two nibble bytes are adjacent: one u32 covers both
                // unless they straddle a word boundary (one case in four).
                let bo = toff + p / 2u;
                let w32 = qmm[bo >> 2u];
                let sh0 = (bo & 3u) * 8u;
                let b0 = (w32 >> sh0) & 0xFFu;
                var b1 = 0u;
                if ((bo & 3u) == 3u) {
                    b1 = qmm[(bo >> 2u) + 1u] & 0xFFu;
                } else {
                    b1 = (w32 >> (sh0 + 8u)) & 0xFFu;
                }
                wv[0u] = (f32(b0 & 0xFu) - 8.0) * scale;
                wv[1u] = (f32(b0 >> 4u) - 8.0) * scale;
                wv[2u] = (f32(b1 & 0xFu) - 8.0) * scale;
                wv[3u] = (f32(b1 >> 4u) - 8.0) * scale;
            }
            let dst = n * 16u + k4 * 4u;
            q4t_wt[dst] = wv.x; q4t_wt[dst + 1u] = wv.y;
            q4t_wt[dst + 2u] = wv.z; q4t_wt[dst + 3u] = wv.w;
        }
        workgroupBarrier();
        let ab = lid.y * 64u;
        let wb = lid.x * 64u;
        for (var k = 0u; k < 16u; k = k + 1u) {
            let x0 = q4t_at[ab + k];
            let x1 = q4t_at[ab + 16u + k];
            let x2 = q4t_at[ab + 32u + k];
            let x3 = q4t_at[ab + 48u + k];
            let y0 = q4t_wt[wb + k];
            let y1 = q4t_wt[wb + 16u + k];
            let y2 = q4t_wt[wb + 32u + k];
            let y3 = q4t_wt[wb + 48u + k];
            a00 = a00 + x0 * y0; a01 = a01 + x0 * y1;
            a02 = a02 + x0 * y2; a03 = a03 + x0 * y3;
            a10 = a10 + x1 * y0; a11 = a11 + x1 * y1;
            a12 = a12 + x1 * y2; a13 = a13 + x1 * y3;
            a20 = a20 + x2 * y0; a21 = a21 + x2 * y1;
            a22 = a22 + x2 * y2; a23 = a23 + x2 * y3;
            a30 = a30 + x3 * y0; a31 = a31 + x3 * y1;
            a32 = a32 + x3 * y2; a33 = a33 + x3 * y3;
        }
        workgroupBarrier();
        k0 = k0 + 16u;
    }
    let mb = m0 + lid.y * 4u;
    let nb2 = n0 + lid.x * 4u;
    q4t_store4(mb, nb2, a00, a01, a02, a03);
    q4t_store4(mb + 1u, nb2, a10, a11, a12, a13);
    q4t_store4(mb + 2u, nb2, a20, a21, a22, a23);
    q4t_store4(mb + 3u, nb2, a30, a31, a32, a33);
}

@compute @workgroup_size(16, 16)
fn q4t_mul_mm(@builtin(workgroup_id) wid: vec3<u32>,
              @builtin(local_invocation_id) lid: vec3<u32>) {
    let cols = pmm.cols4 * 4u;
    let gpr = cols >> 5u;
    let m0 = wid.y * 64u;
    let n0 = wid.x * 64u;
    let tid = lid.y * 16u + lid.x;
    // The 4x4 register block is SIXTEEN NAMED SCALARS, not
    // array<array<f32,4>,4>: indexed by loop variables the array is a
    // private array, which this backend puts in stack memory — the
    // accumulators leave registers and the GEMM runs at a fraction of
    // the card (measured 373 GFLOP/s of an RTX 3090's ~35 TFLOP/s).
    var a00 = 0.0; var a01 = 0.0; var a02 = 0.0; var a03 = 0.0;
    var a10 = 0.0; var a11 = 0.0; var a12 = 0.0; var a13 = 0.0;
    var a20 = 0.0; var a21 = 0.0; var a22 = 0.0; var a23 = 0.0;
    var a30 = 0.0; var a31 = 0.0; var a32 = 0.0; var a33 = 0.0;
    var k0 = 0u;
    loop {
        if (k0 >= cols) { break; }
        for (var t = tid; t < 64u * 4u; t = t + 256u) {
            let m = t / 4u;
            let k4 = t % 4u;
            var xv = vec4<f32>(0.0);
            let col0 = k0 + k4 * 4u;
            if (m0 + m < pmm.nb && col0 < cols) {
                // cols is a multiple of 32 and col0 of 4 — vec4-aligned.
                xv = xmm4[((m0 + m) * cols + col0) >> 2u];
            }
            let dst = m * 16u + k4 * 4u;
            q4t_at[dst] = xv.x; q4t_at[dst + 1u] = xv.y;
            q4t_at[dst + 2u] = xv.z; q4t_at[dst + 3u] = xv.w;
        }
        for (var t = tid; t < 64u * 4u; t = t + 256u) {
            let n = t / 4u;
            let k4 = t % 4u;
            var wv = vec4<f32>(0.0);
            let col0 = k0 + k4 * 4u;
            if (n0 + n < pmm.rows && col0 < cols) {
                let g = col0 >> 5u;
                let toff = ((n0 + n) * gpr + g) * 18u;
                let sc16 = qmm_byte(toff) | (qmm_byte(toff + 1u) << 8u);
                let scale = unpack2x16float(sc16).x;
                // 4 consecutive weights = 2 nibble bytes (col0 is even).
                let p = col0 - g * 32u;
                let b0 = qmm_byte(toff + 2u + p / 2u);
                let b1 = qmm_byte(toff + 3u + p / 2u);
                wv[0u] = (f32(b0 & 0xFu) - 8.0) * scale;
                wv[1u] = (f32(b0 >> 4u) - 8.0) * scale;
                wv[2u] = (f32(b1 & 0xFu) - 8.0) * scale;
                wv[3u] = (f32(b1 >> 4u) - 8.0) * scale;
            }
            let dst = n * 16u + k4 * 4u;
            q4t_wt[dst] = wv.x; q4t_wt[dst + 1u] = wv.y;
            q4t_wt[dst + 2u] = wv.z; q4t_wt[dst + 3u] = wv.w;
        }
        workgroupBarrier();
        let ab = lid.y * 64u;
        let wb = lid.x * 64u;
        for (var k = 0u; k < 16u; k = k + 1u) {
            let x0 = q4t_at[ab + k];
            let x1 = q4t_at[ab + 16u + k];
            let x2 = q4t_at[ab + 32u + k];
            let x3 = q4t_at[ab + 48u + k];
            let y0 = q4t_wt[wb + k];
            let y1 = q4t_wt[wb + 16u + k];
            let y2 = q4t_wt[wb + 32u + k];
            let y3 = q4t_wt[wb + 48u + k];
            a00 = a00 + x0 * y0; a01 = a01 + x0 * y1;
            a02 = a02 + x0 * y2; a03 = a03 + x0 * y3;
            a10 = a10 + x1 * y0; a11 = a11 + x1 * y1;
            a12 = a12 + x1 * y2; a13 = a13 + x1 * y3;
            a20 = a20 + x2 * y0; a21 = a21 + x2 * y1;
            a22 = a22 + x2 * y2; a23 = a23 + x2 * y3;
            a30 = a30 + x3 * y0; a31 = a31 + x3 * y1;
            a32 = a32 + x3 * y2; a33 = a33 + x3 * y3;
        }
        workgroupBarrier();
        k0 = k0 + 16u;
    }
    let mb = m0 + lid.y * 4u;
    let nb2 = n0 + lid.x * 4u;
    q4t_store4(mb, nb2, a00, a01, a02, a03);
    q4t_store4(mb + 1u, nb2, a10, a11, a12, a13);
    q4t_store4(mb + 2u, nb2, a20, a21, a22, a23);
    q4t_store4(mb + 3u, nb2, a30, a31, a32, a33);
}

@compute @workgroup_size(64)
fn q1t_overlay_mm(@builtin(global_invocation_id) gid: vec3<u32>) {
    let row = gid.x;
    if (row >= pmm.rows) { return; }
    let cols = pmm.cols4 * 4u;
    let gpr = cols >> 5u;
    let base_len = pmm.rows * gpr * 9u;
    let ent = base_len + (pmm.rows + 1u) * 4u;
    let rp0 = base_len + row * 4u;
    let c0 = qmm_byte(rp0) | (qmm_byte(rp0 + 1u) << 8u) | (qmm_byte(rp0 + 2u) << 16u) | (qmm_byte(rp0 + 3u) << 24u);
    let rp1 = base_len + (row + 1u) * 4u;
    let c1 = qmm_byte(rp1) | (qmm_byte(rp1 + 1u) << 8u) | (qmm_byte(rp1 + 2u) << 16u) | (qmm_byte(rp1 + 3u) << 24u);
    for (var p = c0; p < c1; p = p + 1u) {
        let e = ent + p * 4u;
        let col = qmm_byte(e) | (qmm_byte(e + 1u) << 8u);
        let val = unpack2x16float(qmm_byte(e + 2u) | (qmm_byte(e + 3u) << 8u)).x;
        for (var bi = 0u; bi < pmm.nb; bi = bi + 1u) {
            ymm[bi * pmm.rows + row] = ymm[bi * pmm.rows + row] + val * xmm[bi * cols + col];
        }
    }
}

// ── MoE inside the whole-token graph ────────────────────────────────────────
// Router logits/shared-gate logit arrive from ordinary matvecs; these three
// kernels keep the routing DECISION and every selected expert on-device, so
// a MoE layer costs one extra pass over a dense one instead of a CPU sync.
// Expert weights live in three per-layer concat buffers (q4t tiles, expert e
// at u16 offset e·mat16); the SHARED expert is the last block, pinned by the
// select kernel at slot top_k with a sigmoid weight.

// `sg_kind` = 4 means this kernel computes the shared-expert gate itself
// from `ms_sgw` · `ms_x` and the host skips that matvec entirely. It is a
// ONE-ROW projection: 2048 multiply-adds for a whole dispatch, and a
// dispatch costs ~23 us on this stack against ~5 us for a pass — measured
// by sweeping the layer count. Folding it into a kernel that already runs
// one workgroup is free. Any other dtype keeps the separate matvec and
// this kernel reads its result from `ms_slog`.
struct MoeSelP { n_exp: u32, top_k: u32, norm: u32, pk: u32 };
@group(0) @binding(0) var<storage, read>       ms_logit : array<f32>;
@group(0) @binding(1) var<storage, read>       ms_slog  : array<f32>;
@group(0) @binding(2) var<storage, read_write> ms_sel   : array<u32>;
@group(0) @binding(3) var<storage, read_write> ms_w     : array<f32>;
@group(0) @binding(4) var<uniform>             ms_p     : MoeSelP;
@group(0) @binding(5) var<storage, read>       ms_sgw   : array<u32>;
@group(0) @binding(6) var<storage, read>       ms_x     : array<f32>;
var<workgroup> ms_lg:  array<f32, 256>;
var<workgroup> ms_red: array<f32, 256>;
var<workgroup> ms_ri:  array<u32, 256>;
var<workgroup> ms_pick: u32;
var<workgroup> ms_sg:  f32;

// One workgroup, ALL-parallel: softmax reductions, then k rounds of an
// argmax reduce (the selected logit is neutralized between rounds). A
// serial one-thread top-k here measured ~270 ns per L2-latency-bound
// probe — 22 ms/token across 40 layers at k=8, the whole decode wall.
// Ties pick the LOWEST index, matching the CPU scan. n_exp ≤ 256.
@compute @workgroup_size(256)
fn moe_select(@builtin(local_invocation_index) lid: u32) {
    // Shared-expert gate first: the reduction scratch below is reused, so
    // this has to land in ms_sg before the router work starts.
    let sg_kind = ms_p.pk & 0xFFu;
    let sg_hidden = ms_p.pk >> 8u;
    if (sg_kind == 4u) {
        var d = 0.0;
        var i = lid;
        loop {
            if (i >= sg_hidden) { break; }
            d = d + bitcast<f32>(ms_sgw[i]) * ms_x[i];
            i = i + 256u;
        }
        ms_red[lid] = d;
        workgroupBarrier();
        var st = 128u;
        loop {
            if (st == 0u) { break; }
            if (lid < st) { ms_red[lid] = ms_red[lid] + ms_red[lid + st]; }
            workgroupBarrier();
            st = st >> 1u;
        }
        if (lid == 0u) { ms_sg = ms_red[0]; }
    } else {
        if (lid == 0u) { ms_sg = ms_slog[0]; }
    }
    workgroupBarrier();
    let n = ms_p.n_exp;
    var v = -3.0e38;
    if (lid < n) { v = ms_logit[lid]; }
    ms_lg[lid] = v;
    ms_red[lid] = v;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { ms_red[lid] = max(ms_red[lid], ms_red[lid + stride]); }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    let mx = ms_red[0];
    workgroupBarrier();
    ms_red[lid] = select(0.0, exp(v - mx), lid < n);
    workgroupBarrier();
    stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { ms_red[lid] = ms_red[lid] + ms_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    let denom = ms_red[0];
    workgroupBarrier();
    let k = ms_p.top_k;
    var wsum = 0.0;
    for (var slot = 0u; slot < k; slot = slot + 1u) {
        ms_red[lid] = ms_lg[lid];
        ms_ri[lid] = lid;
        workgroupBarrier();
        stride = 128u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) {
                let a = ms_red[lid];
                let b = ms_red[lid + stride];
                let ia = ms_ri[lid];
                let ib = ms_ri[lid + stride];
                if (b > a || (b == a && ib < ia)) {
                    ms_red[lid] = b;
                    ms_ri[lid] = ib;
                }
            }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) {
            let bi = ms_ri[0];
            ms_sel[slot] = bi;
            ms_w[slot] = exp(ms_red[0] - mx) / denom;
            ms_pick = bi;
        }
        workgroupBarrier();
        wsum = wsum + exp(ms_red[0] - mx) / denom;
        if (lid == ms_pick) { ms_lg[lid] = -3.0e38; }
        workgroupBarrier();
    }
    if (lid == 0u) {
        if (ms_p.norm != 0u) {
            for (var slot = 0u; slot < k; slot = slot + 1u) { ms_w[slot] = ms_w[slot] / wsum; }
        }
        ms_sel[k] = n;
        ms_w[k] = 1.0 / (1.0 + exp(-ms_sg));
    }
}

// gate+up+SiLU for every selected expert: workgroup (row, slot) does BOTH q4t
// row dots (they share the activation reads) and writes act = silu(g)·u.
struct MoeGuP { gpr: u32, inter: u32, slots: u32, mat16: u32 , lim: f32, _p0: u32, _p1: u32, _p2: u32 };
// Three scalars, not a vec3: a vec3<u32> aligns to 16 in uniform layout and
// pushes the struct to 48 bytes, while the buffer handed in is 32.
// `lim` is DeepSeek-V4's swiglu_limit: the up projection is clamped both
// ways, the gate only from above. Zero means no clamp, which is every other
// architecture that reaches these kernels.
@group(0) @binding(0) var<storage, read>       mg_gw  : array<u32>;
@group(0) @binding(1) var<storage, read>       mg_uw  : array<u32>;
@group(0) @binding(2) var<storage, read>       mg_x   : array<f32>;
@group(0) @binding(3) var<storage, read>       mg_sel : array<u32>;
@group(0) @binding(4) var<storage, read_write> mg_act : array<f32>;
@group(0) @binding(5) var<uniform>             mg_p   : MoeGuP;
var<workgroup> mg_pg: array<f32, 64>;
var<workgroup> mg_pu: array<f32, 64>;

// The same activations as `mg_x`, at the SAME SLOT, seen as vec4. The
// scalar view costs one load per weight, which left the dense FFN kernel
// at ~11% of the card's bandwidth until the vec4 rewrite (+2.7x there).
//
// A second global on slot 2 rather than a new slot 6, because an auto
// layout lists only the bindings its entry point actually USES: the q2tp
// kernel stopped touching `mg_x`, naga dropped slot 2, and the 7-entry
// bind group met a 6-entry layout. Same slot = the bind group is
// unchanged for every kernel here.
@group(0) @binding(2) var<storage, read> mg_xv : array<vec4<f32>>;

fn mg_g16(o: u32) -> u32 { return (mg_gw[o >> 1u] >> ((o & 1u) * 16u)) & 0xFFFFu; }
fn mg_u16f(o: u32) -> u32 { return (mg_uw[o >> 1u] >> ((o & 1u) * 16u)) & 0xFFFFu; }
fn mg_dot8(w: u32, xi: u32) -> f32 {
    return (f32(w & 0xFu) - 8.0) * mg_x[xi]
         + (f32((w >> 4u) & 0xFu) - 8.0) * mg_x[xi + 1u]
         + (f32((w >> 8u) & 0xFu) - 8.0) * mg_x[xi + 2u]
         + (f32((w >> 12u) & 0xFu) - 8.0) * mg_x[xi + 3u]
         + (f32((w >> 16u) & 0xFu) - 8.0) * mg_x[xi + 4u]
         + (f32((w >> 20u) & 0xFu) - 8.0) * mg_x[xi + 5u]
         + (f32((w >> 24u) & 0xFu) - 8.0) * mg_x[xi + 6u]
         + (f32((w >> 28u) & 0xFu) - 8.0) * mg_x[xi + 7u];
}

@compute @workgroup_size(64)
fn moe_gate_up(@builtin(workgroup_id) wid: vec3<u32>,
               @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let slot = wid.y;
    let gpr = mg_p.gpr;
    let base = mg_sel[slot] * mg_p.mat16 + row * gpr * 9u;
    var ag = 0.0;
    var au = 0.0;
    for (var g = lid; g < gpr; g = g + 64u) {
        let t16 = base + g * 9u;
        let sg = unpack2x16float(mg_g16(t16)).x;
        let su = unpack2x16float(mg_u16f(t16)).x;
        let xb = g * 32u;
        var dg = 0.0;
        var du = 0.0;
        for (var k = 0u; k < 4u; k = k + 1u) {
            let wg = mg_g16(t16 + 1u + 2u * k) | (mg_g16(t16 + 2u + 2u * k) << 16u);
            let wu = mg_u16f(t16 + 1u + 2u * k) | (mg_u16f(t16 + 2u + 2u * k) << 16u);
            dg = dg + mg_dot8(wg, xb + 8u * k);
            du = du + mg_dot8(wu, xb + 8u * k);
        }
        ag = ag + sg * dg;
        au = au + su * du;
    }
    mg_pg[lid] = ag;
    mg_pu[lid] = au;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) {
            mg_pg[lid] = mg_pg[lid] + mg_pg[lid + stride];
            mg_pu[lid] = mg_pu[lid] + mg_pu[lid + stride];
        }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) {
        let g = mg_pg[0];
        var gg = g;
        var uu = mg_pu[0];
        if (mg_p.lim > 0.0) {
            uu = clamp(uu, -mg_p.lim, mg_p.lim);
            gg = min(gg, mg_p.lim);
        }
        mg_act[slot * mg_p.inter + row] = (gg / (1.0 + exp(-gg))) * uu;
    }
}

// Weighted down-projection: one workgroup per hidden row accumulates
// Σ_slot w[slot]·(down[sel[slot]] row · act[slot]) over the flattened
// (slot, group) space, then overwrites y[row] (the graph's usual FFN
// output slot — the existing fused residual add consumes it).
struct MoeDnP { gpr: u32, hidden: u32, slots: u32, mat16: u32 };
@group(0) @binding(0) var<storage, read>       md_w   : array<u32>;
@group(0) @binding(1) var<storage, read>       md_act : array<f32>;
@group(0) @binding(2) var<storage, read>       md_sel : array<u32>;
@group(0) @binding(3) var<storage, read>       md_wt  : array<f32>;
@group(0) @binding(4) var<storage, read_write> md_y   : array<f32>;
@group(0) @binding(5) var<uniform>             md_p   : MoeDnP;
var<workgroup> md_pt: array<f32, 64>;

// The activations again as vec4, on the SAME slot — the scalar view costs
// one load per weight and put the down kernel at 33 us for ~5 MB of reads.
@group(0) @binding(1) var<storage, read> md_actv : array<vec4<f32>>;

fn md_u16(o: u32) -> u32 { return (md_w[o >> 1u] >> ((o & 1u) * 16u)) & 0xFFFFu; }
fn md_dot8v(w: u32, a: vec4<f32>, b: vec4<f32>) -> f32 {
    return (f32(w & 0xFu) - 8.0) * a.x
         + (f32((w >> 4u) & 0xFu) - 8.0) * a.y
         + (f32((w >> 8u) & 0xFu) - 8.0) * a.z
         + (f32((w >> 12u) & 0xFu) - 8.0) * a.w
         + (f32((w >> 16u) & 0xFu) - 8.0) * b.x
         + (f32((w >> 20u) & 0xFu) - 8.0) * b.y
         + (f32((w >> 24u) & 0xFu) - 8.0) * b.z
         + (f32((w >> 28u) & 0xFu) - 8.0) * b.w;
}
fn md_dot8(w: u32, xi: u32) -> f32 {
    return (f32(w & 0xFu) - 8.0) * md_act[xi]
         + (f32((w >> 4u) & 0xFu) - 8.0) * md_act[xi + 1u]
         + (f32((w >> 8u) & 0xFu) - 8.0) * md_act[xi + 2u]
         + (f32((w >> 12u) & 0xFu) - 8.0) * md_act[xi + 3u]
         + (f32((w >> 16u) & 0xFu) - 8.0) * md_act[xi + 4u]
         + (f32((w >> 20u) & 0xFu) - 8.0) * md_act[xi + 5u]
         + (f32((w >> 24u) & 0xFu) - 8.0) * md_act[xi + 6u]
         + (f32((w >> 28u) & 0xFu) - 8.0) * md_act[xi + 7u];
}

@compute @workgroup_size(64)
fn moe_down(@builtin(workgroup_id) wid: vec3<u32>,
            @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let gpr = md_p.gpr;
    let total = md_p.slots * gpr;
    var acc = 0.0;
    for (var i = lid; i < total; i = i + 64u) {
        let slot = i / gpr;
        let g = i % gpr;
        let t16 = md_sel[slot] * md_p.mat16 + (row * gpr + g) * 9u;
        let scale = unpack2x16float(md_u16(t16)).x;
        let xb = (slot * gpr + g) * 32u;
        var d = 0.0;
        for (var k = 0u; k < 4u; k = k + 1u) {
            let w = md_u16(t16 + 1u + 2u * k) | (md_u16(t16 + 2u + 2u * k) << 16u);
            d = d + md_dot8(w, xb + 8u * k);
        }
        acc = acc + md_wt[slot] * scale * d;
    }
    md_pt[lid] = acc;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { md_pt[lid] = md_pt[lid] + md_pt[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { md_y[row] = md_pt[0]; }
}

// ── q4tp twins of the two MoE kernels. Identical nibble math and
// identical bindings; only where the scale comes from differs. q4t
// carries an f16 scale inside each 18-byte tile, q4tp packs the nibbles
// 16-byte tight and puts a 5-bit rung index into a side plane, read
// against the row's geometric ladder `2^(lo + code·step)`. Per expert
// the blob is [nibbles | row params (f16 lo, f16 step) | 5-bit codes],
// so the two extra plane offsets fall out of rows/gpr.
fn mgp_gu8(o: u32) -> u32 { return (mg_gw[o >> 2u] >> ((o & 3u) * 8u)) & 0xFFu; }
fn mgp_uu8(o: u32) -> u32 { return (mg_uw[o >> 2u] >> ((o & 3u) * 8u)) & 0xFFu; }

@compute @workgroup_size(64)
fn moe_gate_up_q4tp(@builtin(workgroup_id) wid: vec3<u32>,
                    @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let slot = wid.y;
    let gpr = mg_p.gpr;
    let rows = mg_p.inter;
    let base16 = mg_sel[slot] * mg_p.mat16;
    let nib16 = base16 + row * gpr * 8u;
    let par16 = base16 + rows * gpr * 8u + row * 2u;
    let cst = (gpr * 5u + 7u) / 8u;
    let cod8 = (base16 + rows * gpr * 8u + rows * 2u) * 2u + row * cst;

    let gl = unpack2x16float(mg_g16(par16) | (mg_g16(par16 + 1u) << 16u));
    let ul = unpack2x16float(mg_u16f(par16) | (mg_u16f(par16 + 1u) << 16u));
    var ag = 0.0;
    var au = 0.0;
    for (var g = lid; g < gpr; g = g + 64u) {
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cg = mgp_gu8(cod8 + cb);
        var cu = mgp_uu8(cod8 + cb);
        // A 5-bit field starting past bit 3 spills into the next byte.
        if (shf > 3u) {
            cg = cg | (mgp_gu8(cod8 + cb + 1u) << 8u);
            cu = cu | (mgp_uu8(cod8 + cb + 1u) << 8u);
        }
        let sg = exp2(gl.x + f32((cg >> shf) & 31u) * gl.y);
        let su = exp2(ul.x + f32((cu >> shf) & 31u) * ul.y);
        let t16 = nib16 + g * 8u;
        let xb = g * 32u;
        var dg = 0.0;
        var du = 0.0;
        for (var k = 0u; k < 4u; k = k + 1u) {
            let wg = mg_g16(t16 + 2u * k) | (mg_g16(t16 + 1u + 2u * k) << 16u);
            let wu = mg_u16f(t16 + 2u * k) | (mg_u16f(t16 + 1u + 2u * k) << 16u);
            dg = dg + mg_dot8(wg, xb + 8u * k);
            du = du + mg_dot8(wu, xb + 8u * k);
        }
        ag = ag + sg * dg;
        au = au + su * du;
    }
    mg_pg[lid] = ag;
    mg_pu[lid] = au;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) {
            mg_pg[lid] = mg_pg[lid] + mg_pg[lid + stride];
            mg_pu[lid] = mg_pu[lid] + mg_pu[lid + stride];
        }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) {
        let g = mg_pg[0];
        var gg = g;
        var uu = mg_pu[0];
        if (mg_p.lim > 0.0) {
            uu = clamp(uu, -mg_p.lim, mg_p.lim);
            gg = min(gg, mg_p.lim);
        }
        mg_act[slot * mg_p.inter + row] = (gg / (1.0 + exp(-gg))) * uu;
    }
}

// q2tp gate/up: the q4tp kernel with a 2-bit weight plane. A group is
// 32 weights in 8 bytes (4 u16 units) instead of 16, and one u32 carries
// SIXTEEN weights, so the group is two words and two dot16s. The params
// and 5-bit code planes are byte-identical to q4tp — only the plane
// offsets move, since they sit behind a half-size weight plane.
// 16 two-bit weights against four staged vec4s — same add order as the
// scalar mg_dot16 below, which greedy parity depends on.
fn mg_dot16v(w: u32, a: vec4<f32>, b: vec4<f32>, c: vec4<f32>, d: vec4<f32>) -> f32 {
    return (f32(w & 3u) - 1.5) * a.x
         + (f32((w >> 2u) & 3u) - 1.5) * a.y
         + (f32((w >> 4u) & 3u) - 1.5) * a.z
         + (f32((w >> 6u) & 3u) - 1.5) * a.w
         + (f32((w >> 8u) & 3u) - 1.5) * b.x
         + (f32((w >> 10u) & 3u) - 1.5) * b.y
         + (f32((w >> 12u) & 3u) - 1.5) * b.z
         + (f32((w >> 14u) & 3u) - 1.5) * b.w
         + (f32((w >> 16u) & 3u) - 1.5) * c.x
         + (f32((w >> 18u) & 3u) - 1.5) * c.y
         + (f32((w >> 20u) & 3u) - 1.5) * c.z
         + (f32((w >> 22u) & 3u) - 1.5) * c.w
         + (f32((w >> 24u) & 3u) - 1.5) * d.x
         + (f32((w >> 26u) & 3u) - 1.5) * d.y
         + (f32((w >> 28u) & 3u) - 1.5) * d.z
         + (f32((w >> 30u) & 3u) - 1.5) * d.w;
}

fn mg_dot16(w: u32, xi: u32) -> f32 {
    return (f32(w & 3u) - 1.5) * mg_x[xi]
         + (f32((w >> 2u) & 3u) - 1.5) * mg_x[xi + 1u]
         + (f32((w >> 4u) & 3u) - 1.5) * mg_x[xi + 2u]
         + (f32((w >> 6u) & 3u) - 1.5) * mg_x[xi + 3u]
         + (f32((w >> 8u) & 3u) - 1.5) * mg_x[xi + 4u]
         + (f32((w >> 10u) & 3u) - 1.5) * mg_x[xi + 5u]
         + (f32((w >> 12u) & 3u) - 1.5) * mg_x[xi + 6u]
         + (f32((w >> 14u) & 3u) - 1.5) * mg_x[xi + 7u]
         + (f32((w >> 16u) & 3u) - 1.5) * mg_x[xi + 8u]
         + (f32((w >> 18u) & 3u) - 1.5) * mg_x[xi + 9u]
         + (f32((w >> 20u) & 3u) - 1.5) * mg_x[xi + 10u]
         + (f32((w >> 22u) & 3u) - 1.5) * mg_x[xi + 11u]
         + (f32((w >> 24u) & 3u) - 1.5) * mg_x[xi + 12u]
         + (f32((w >> 26u) & 3u) - 1.5) * mg_x[xi + 13u]
         + (f32((w >> 28u) & 3u) - 1.5) * mg_x[xi + 14u]
         + (f32((w >> 30u) & 3u) - 1.5) * mg_x[xi + 15u];
}

@compute @workgroup_size(64)
fn moe_gate_up_q2tp(@builtin(workgroup_id) wid: vec3<u32>,
                    @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let slot = wid.y;
    let gpr = mg_p.gpr;
    let rows = mg_p.inter;
    let base16 = mg_sel[slot] * mg_p.mat16;
    let nib16 = base16 + row * gpr * 4u;
    let par16 = base16 + rows * gpr * 4u + row * 2u;
    let cst = (gpr * 5u + 7u) / 8u;
    let cod8 = (base16 + rows * gpr * 4u + rows * 2u) * 2u + row * cst;

    let gl = unpack2x16float(mg_g16(par16) | (mg_g16(par16 + 1u) << 16u));
    let ul = unpack2x16float(mg_u16f(par16) | (mg_u16f(par16 + 1u) << 16u));
    var ag = 0.0;
    var au = 0.0;
    for (var g = lid; g < gpr; g = g + 64u) {
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cg = mgp_gu8(cod8 + cb);
        var cu = mgp_uu8(cod8 + cb);
        if (shf > 3u) {
            cg = cg | (mgp_gu8(cod8 + cb + 1u) << 8u);
            cu = cu | (mgp_uu8(cod8 + cb + 1u) << 8u);
        }
        // Rung 0 is the format's exact zero (the ±0.5/±1.5 grid has no
        // zero of its own); live rungs are the ladder shifted down one.
        let cgv = (cg >> shf) & 31u;
        let cuv = (cu >> shf) & 31u;
        let sg = select(exp2(gl.x + f32(max(cgv, 1u) - 1u) * gl.y), 0.0, cgv == 0u);
        let su = select(exp2(ul.x + f32(max(cuv, 1u) - 1u) * ul.y), 0.0, cuv == 0u);
        // Group base in u16 units is a multiple of 4, so the two 32-bit
        // words land on u32 lanes (nib16 >> 1) and (nib16 >> 1) + 1.
        let w32 = (nib16 + g * 4u) >> 1u;
        // One group = 32 activations = 8 vec4s, shared by gate and up.
        let xq = g * 8u;
        let x0 = mg_xv[xq];      let x1 = mg_xv[xq + 1u];
        let x2 = mg_xv[xq + 2u]; let x3 = mg_xv[xq + 3u];
        let x4 = mg_xv[xq + 4u]; let x5 = mg_xv[xq + 5u];
        let x6 = mg_xv[xq + 6u]; let x7 = mg_xv[xq + 7u];
        let dg = mg_dot16v(mg_gw[w32], x0, x1, x2, x3)
               + mg_dot16v(mg_gw[w32 + 1u], x4, x5, x6, x7);
        let du = mg_dot16v(mg_uw[w32], x0, x1, x2, x3)
               + mg_dot16v(mg_uw[w32 + 1u], x4, x5, x6, x7);
        ag = ag + sg * dg;
        au = au + su * du;
    }
    mg_pg[lid] = ag;
    mg_pu[lid] = au;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) {
            mg_pg[lid] = mg_pg[lid] + mg_pg[lid + stride];
            mg_pu[lid] = mg_pu[lid] + mg_pu[lid + stride];
        }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) {
        let g = mg_pg[0];
        var gg = g;
        var uu = mg_pu[0];
        if (mg_p.lim > 0.0) {
            uu = clamp(uu, -mg_p.lim, mg_p.lim);
            gg = min(gg, mg_p.lim);
        }
        mg_act[slot * mg_p.inter + row] = (gg / (1.0 + exp(-gg))) * uu;
    }
}

// FOUR output rows to a workgroup, 64 lanes each. `gpr` is 128 on the
// release, so a row has no use for more than 64 lanes — but four rows give
// the memory system four more independent streams to overlap, and the MoE
// is 5.6 ms of a 33 ms chain against a bandwidth floor near 1.8.
var<workgroup> mgm_pg: array<f32, 256>;
var<workgroup> mgm_pu: array<f32, 256>;
@compute @workgroup_size(256)
fn moe_gate_up_q2tp_m(@builtin(workgroup_id) wid: vec3<u32>,
                    @builtin(local_invocation_index) lid: u32) {
    let sub = lid / 64u;
    let lane = lid % 64u;
    let row = wid.x * 4u + sub;
    let slot = wid.y;
    if (row >= mg_p.inter) { return; }
    let gpr = mg_p.gpr;
    let rows = mg_p.inter;
    let base16 = mg_sel[slot] * mg_p.mat16;
    let nib16 = base16 + row * gpr * 4u;
    let par16 = base16 + rows * gpr * 4u + row * 2u;
    let cst = (gpr * 5u + 7u) / 8u;
    let cod8 = (base16 + rows * gpr * 4u + rows * 2u) * 2u + row * cst;

    let gl = unpack2x16float(mg_g16(par16) | (mg_g16(par16 + 1u) << 16u));
    let ul = unpack2x16float(mg_u16f(par16) | (mg_u16f(par16 + 1u) << 16u));
    var ag = 0.0;
    var au = 0.0;
    for (var g = lane; g < gpr; g = g + 64u) {
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cg = mgp_gu8(cod8 + cb);
        var cu = mgp_uu8(cod8 + cb);
        if (shf > 3u) {
            cg = cg | (mgp_gu8(cod8 + cb + 1u) << 8u);
            cu = cu | (mgp_uu8(cod8 + cb + 1u) << 8u);
        }
        // Rung 0 is the format's exact zero (the ±0.5/±1.5 grid has no
        // zero of its own); live rungs are the ladder shifted down one.
        let cgv = (cg >> shf) & 31u;
        let cuv = (cu >> shf) & 31u;
        let sg = select(exp2(gl.x + f32(max(cgv, 1u) - 1u) * gl.y), 0.0, cgv == 0u);
        let su = select(exp2(ul.x + f32(max(cuv, 1u) - 1u) * ul.y), 0.0, cuv == 0u);
        // Group base in u16 units is a multiple of 4, so the two 32-bit
        // words land on u32 lanes (nib16 >> 1) and (nib16 >> 1) + 1.
        let w32 = (nib16 + g * 4u) >> 1u;
        // One group = 32 activations = 8 vec4s, shared by gate and up.
        let xq = g * 8u;
        let x0 = mg_xv[xq];      let x1 = mg_xv[xq + 1u];
        let x2 = mg_xv[xq + 2u]; let x3 = mg_xv[xq + 3u];
        let x4 = mg_xv[xq + 4u]; let x5 = mg_xv[xq + 5u];
        let x6 = mg_xv[xq + 6u]; let x7 = mg_xv[xq + 7u];
        let dg = mg_dot16v(mg_gw[w32], x0, x1, x2, x3)
               + mg_dot16v(mg_gw[w32 + 1u], x4, x5, x6, x7);
        let du = mg_dot16v(mg_uw[w32], x0, x1, x2, x3)
               + mg_dot16v(mg_uw[w32 + 1u], x4, x5, x6, x7);
        ag = ag + sg * dg;
        au = au + su * du;
    }
    mgm_pg[lid] = ag;
    mgm_pu[lid] = au;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lane < stride) {
            mgm_pg[lid] = mgm_pg[lid] + mgm_pg[lid + stride];
            mgm_pu[lid] = mgm_pu[lid] + mgm_pu[lid + stride];
        }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lane == 0u) {
        let g = mgm_pg[sub * 64u];
        var gg = g;
        var uu = mgm_pu[sub * 64u];
        if (mg_p.lim > 0.0) {
            uu = clamp(uu, -mg_p.lim, mg_p.lim);
            gg = min(gg, mg_p.lim);
        }
        mg_act[slot * mg_p.inter + row] = (gg / (1.0 + exp(-gg))) * uu;
    }
}

fn mdp_u8(o: u32) -> u32 { return (md_w[o >> 2u] >> ((o & 3u) * 8u)) & 0xFFu; }

@compute @workgroup_size(64)
fn moe_down_q4tp(@builtin(workgroup_id) wid: vec3<u32>,
                 @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let gpr = md_p.gpr;
    let rows = md_p.hidden;
    let cst = (gpr * 5u + 7u) / 8u;
    let total = md_p.slots * gpr;
    var acc = 0.0;
    for (var i = lid; i < total; i = i + 64u) {
        let slot = i / gpr;
        let g = i % gpr;
        let base16 = md_sel[slot] * md_p.mat16;
        let par16 = base16 + rows * gpr * 8u + row * 2u;
        let cod8 = (base16 + rows * gpr * 8u + rows * 2u) * 2u + row * cst;
        let pl = unpack2x16float(md_u16(par16) | (md_u16(par16 + 1u) << 16u));
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cv = mdp_u8(cod8 + cb);
        if (shf > 3u) { cv = cv | (mdp_u8(cod8 + cb + 1u) << 8u); }
        let scale = exp2(pl.x + f32((cv >> shf) & 31u) * pl.y);
        let t16 = base16 + (row * gpr + g) * 8u;
        let xq = (slot * gpr + g) * 8u;
        let x0 = md_actv[xq];      let x1 = md_actv[xq + 1u];
        let x2 = md_actv[xq + 2u]; let x3 = md_actv[xq + 3u];
        let x4 = md_actv[xq + 4u]; let x5 = md_actv[xq + 5u];
        let x6 = md_actv[xq + 6u]; let x7 = md_actv[xq + 7u];
        let w0 = md_u16(t16) | (md_u16(t16 + 1u) << 16u);
        let w1 = md_u16(t16 + 2u) | (md_u16(t16 + 3u) << 16u);
        let w2 = md_u16(t16 + 4u) | (md_u16(t16 + 5u) << 16u);
        let w3 = md_u16(t16 + 6u) | (md_u16(t16 + 7u) << 16u);
        let d = md_dot8v(w0, x0, x1) + md_dot8v(w1, x2, x3)
              + md_dot8v(w2, x4, x5) + md_dot8v(w3, x6, x7);
        acc = acc + md_wt[slot] * scale * d;
    }
    md_pt[lid] = acc;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { md_pt[lid] = md_pt[lid] + md_pt[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { md_y[row] = md_pt[0]; }
}

// 256 threads. The loop walks slots·gpr = 576 terms on the release, which
// at 64 threads is nine iterations of dependent loads per thread; at 256 it
// is two and a bit, and the workgroup count (one per hidden row, 4096) was
// never the problem.
var<workgroup> mdm_pt: array<f32, 256>;
@compute @workgroup_size(256)
fn moe_down_q4tp_m(@builtin(workgroup_id) wid: vec3<u32>,
                 @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let gpr = md_p.gpr;
    let rows = md_p.hidden;
    let cst = (gpr * 5u + 7u) / 8u;
    let total = md_p.slots * gpr;
    var acc = 0.0;
    for (var i = lid; i < total; i = i + 256u) {
        let slot = i / gpr;
        let g = i % gpr;
        let base16 = md_sel[slot] * md_p.mat16;
        let par16 = base16 + rows * gpr * 8u + row * 2u;
        let cod8 = (base16 + rows * gpr * 8u + rows * 2u) * 2u + row * cst;
        let pl = unpack2x16float(md_u16(par16) | (md_u16(par16 + 1u) << 16u));
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cv = mdp_u8(cod8 + cb);
        if (shf > 3u) { cv = cv | (mdp_u8(cod8 + cb + 1u) << 8u); }
        let scale = exp2(pl.x + f32((cv >> shf) & 31u) * pl.y);
        let t16 = base16 + (row * gpr + g) * 8u;
        let xq = (slot * gpr + g) * 8u;
        let x0 = md_actv[xq];      let x1 = md_actv[xq + 1u];
        let x2 = md_actv[xq + 2u]; let x3 = md_actv[xq + 3u];
        let x4 = md_actv[xq + 4u]; let x5 = md_actv[xq + 5u];
        let x6 = md_actv[xq + 6u]; let x7 = md_actv[xq + 7u];
        let w0 = md_u16(t16) | (md_u16(t16 + 1u) << 16u);
        let w1 = md_u16(t16 + 2u) | (md_u16(t16 + 3u) << 16u);
        let w2 = md_u16(t16 + 4u) | (md_u16(t16 + 5u) << 16u);
        let w3 = md_u16(t16 + 6u) | (md_u16(t16 + 7u) << 16u);
        let d = md_dot8v(w0, x0, x1) + md_dot8v(w1, x2, x3)
              + md_dot8v(w2, x4, x5) + md_dot8v(w3, x6, x7);
        acc = acc + md_wt[slot] * scale * d;
    }
    mdm_pt[lid] = acc;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { mdm_pt[lid] = mdm_pt[lid] + mdm_pt[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { md_y[row] = mdm_pt[0]; }
}

// ── Batched MoE: the whole layer's routing and experts with a TOKEN
// dimension, for the batch (prefill) graph. The per-token encoding of
// this block cost ~7 commands per token per layer — at k=32 over 40
// layers that is ~9000 encoder commands a chunk, and the chunk clocked
// at the same 16 ms/token as the per-position path it was meant to
// beat. These three kernels replace all of it with THREE dispatches per
// layer and zero buffer-to-buffer copies.
//
// The router matvec lives INSIDE the select kernel: one thread = one
// expert row (n_exp <= 256 = workgroup size), reading x straight from
// the batch hidden at its token offset. No logits buffer, no row
// staging. f32 router weights only — the converter leaves the router
// unquantized; anything else falls back to the per-token path.
struct MoeSelBP { n_exp: u32, top_k: u32, norm: u32, pk: u32 };
@group(0) @binding(0) var<storage, read>       sb_lgin: array<f32>;
@group(0) @binding(1) var<storage, read>       sb_x   : array<f32>;
@group(0) @binding(2) var<storage, read_write> sb_sel : array<u32>;
@group(0) @binding(3) var<storage, read_write> sb_w   : array<f32>;
@group(0) @binding(4) var<uniform>             sb_p   : MoeSelBP;
@group(0) @binding(5) var<storage, read>       sb_sgw : array<u32>;
var<workgroup> sb_lg:  array<f32, 256>;
var<workgroup> sb_red: array<f32, 256>;
var<workgroup> sb_ri:  array<u32, 256>;
var<workgroup> sb_sg:  f32;

@compute @workgroup_size(256)
fn moe_select_b(@builtin(workgroup_id) wid: vec3<u32>,
                @builtin(local_invocation_index) lid: u32) {
    let t = wid.x;
    let n = sb_p.n_exp;
    let sg_kind = sb_p.pk & 0xFFu;
    let hidden = sb_p.pk >> 8u;
    let xb = t * hidden;
    // Logits arrive from the same f32 matvec kernel the parity-proven
    // path used, one slice per token — computing them here with a
    // different summation order shifted near-tied experts and broke
    // token parity with the CPU.
    var v = -3.0e38;
    if (lid < n) { v = sb_lgin[t * n + lid]; }
    // Shared-expert gate on this token's x (f32 weights, same fold as
    // the single-token kernel).
    if (sg_kind == 4u) {
        var d = 0.0;
        var i = lid;
        loop {
            if (i >= hidden) { break; }
            d = d + bitcast<f32>(sb_sgw[i]) * sb_x[xb + i];
            i = i + 256u;
        }
        sb_red[lid] = d;
        workgroupBarrier();
        var st = 128u;
        loop {
            if (st == 0u) { break; }
            if (lid < st) { sb_red[lid] = sb_red[lid] + sb_red[lid + st]; }
            workgroupBarrier();
            st = st >> 1u;
        }
        if (lid == 0u) { sb_sg = sb_red[0]; }
        workgroupBarrier();
    }
    sb_lg[lid] = v;
    sb_red[lid] = v;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { sb_red[lid] = max(sb_red[lid], sb_red[lid + stride]); }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    let mx = sb_red[0];
    workgroupBarrier();
    sb_red[lid] = select(0.0, exp(v - mx), lid < n);
    workgroupBarrier();
    stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { sb_red[lid] = sb_red[lid] + sb_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    let denom = sb_red[0];
    workgroupBarrier();
    let kk = sb_p.top_k;
    let ob = t * (kk + 1u);
    var wsum = 0.0;
    for (var slot = 0u; slot < kk; slot = slot + 1u) {
        sb_red[lid] = sb_lg[lid];
        sb_ri[lid] = lid;
        workgroupBarrier();
        stride = 128u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) {
                let a = sb_red[lid];
                let b = sb_red[lid + stride];
                let ia = sb_ri[lid];
                let ib = sb_ri[lid + stride];
                if (b > a || (b == a && ib < ia)) {
                    sb_red[lid] = b;
                    sb_ri[lid] = ib;
                }
            }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) {
            let bi = sb_ri[0];
            sb_sel[ob + slot] = bi;
            sb_w[ob + slot] = exp(sb_red[0] - mx) / denom;
        }
        workgroupBarrier();
        wsum = wsum + exp(sb_red[0] - mx) / denom;
        if (lid == sb_ri[0]) { sb_lg[lid] = -3.0e38; }
        workgroupBarrier();
    }
    if (lid == 0u) {
        if (sb_p.norm != 0u) {
            for (var slot = 0u; slot < kk; slot = slot + 1u) {
                sb_w[ob + slot] = sb_w[ob + slot] / wsum;
            }
        }
        sb_sel[ob + kk] = n;
        sb_w[ob + kk] = 1.0 / (1.0 + exp(-sb_sg));
    }
}

// gate+up+SiLU with a token axis: workgroup (row, slot, token).
struct MoeGuBP { gpr: u32, inter: u32, slots: u32, mat16: u32 , lim: f32, _p0: u32, _p1: u32, _p2: u32 };
// Three scalars, not a vec3: a vec3<u32> aligns to 16 in uniform layout and
// pushes the struct to 48 bytes, while the buffer handed in is 32.
// `lim` is DeepSeek-V4's swiglu_limit: the up projection is clamped both
// ways, the gate only from above. Zero means no clamp, which is every other
// architecture that reaches these kernels.
@group(0) @binding(0) var<storage, read>       gb_gw  : array<u32>;
@group(0) @binding(1) var<storage, read>       gb_uw  : array<u32>;
@group(0) @binding(2) var<storage, read>       gb_x   : array<f32>;
@group(0) @binding(3) var<storage, read>       gb_sel : array<u32>;
@group(0) @binding(4) var<storage, read_write> gb_act : array<f32>;
@group(0) @binding(5) var<uniform>             gb_p   : MoeGuBP;
var<workgroup> gb_pg: array<f32, 64>;
var<workgroup> gb_pu: array<f32, 64>;

fn gb_g16(o: u32) -> u32 { return (gb_gw[o >> 1u] >> ((o & 1u) * 16u)) & 0xFFFFu; }
fn gb_u16(o: u32) -> u32 { return (gb_uw[o >> 1u] >> ((o & 1u) * 16u)) & 0xFFFFu; }
fn gb_gu8(o: u32) -> u32 { return (gb_gw[o >> 2u] >> ((o & 3u) * 8u)) & 0xFFu; }
fn gb_uu8(o: u32) -> u32 { return (gb_uw[o >> 2u] >> ((o & 3u) * 8u)) & 0xFFu; }
fn gb_dot8(w: u32, xi: u32) -> f32 {
    return (f32(w & 0xFu) - 8.0) * gb_x[xi]
         + (f32((w >> 4u) & 0xFu) - 8.0) * gb_x[xi + 1u]
         + (f32((w >> 8u) & 0xFu) - 8.0) * gb_x[xi + 2u]
         + (f32((w >> 12u) & 0xFu) - 8.0) * gb_x[xi + 3u]
         + (f32((w >> 16u) & 0xFu) - 8.0) * gb_x[xi + 4u]
         + (f32((w >> 20u) & 0xFu) - 8.0) * gb_x[xi + 5u]
         + (f32((w >> 24u) & 0xFu) - 8.0) * gb_x[xi + 6u]
         + (f32((w >> 28u) & 0xFu) - 8.0) * gb_x[xi + 7u];
}

@compute @workgroup_size(64)
fn moe_gate_up_q4tp_b(@builtin(workgroup_id) wid: vec3<u32>,
                      @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let slot = wid.y;
    let t = wid.z;
    let gpr = gb_p.gpr;
    let rows = gb_p.inter;
    let hidden = gpr * 32u;
    let xoff = t * hidden;
    let base16 = gb_sel[t * gb_p.slots + slot] * gb_p.mat16;
    let nib16 = base16 + row * gpr * 8u;
    let par16 = base16 + rows * gpr * 8u + row * 2u;
    let cst = (gpr * 5u + 7u) / 8u;
    let cod8 = (base16 + rows * gpr * 8u + rows * 2u) * 2u + row * cst;
    let gl = unpack2x16float(gb_g16(par16) | (gb_g16(par16 + 1u) << 16u));
    let ul = unpack2x16float(gb_u16(par16) | (gb_u16(par16 + 1u) << 16u));
    var ag = 0.0;
    var au = 0.0;
    for (var g = lid; g < gpr; g = g + 64u) {
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cg = gb_gu8(cod8 + cb);
        var cu = gb_uu8(cod8 + cb);
        if (shf > 3u) {
            cg = cg | (gb_gu8(cod8 + cb + 1u) << 8u);
            cu = cu | (gb_uu8(cod8 + cb + 1u) << 8u);
        }
        let sg = exp2(gl.x + f32((cg >> shf) & 31u) * gl.y);
        let su = exp2(ul.x + f32((cu >> shf) & 31u) * ul.y);
        let t16 = nib16 + g * 8u;
        let xb = xoff + g * 32u;
        var dg = 0.0;
        var du = 0.0;
        for (var k = 0u; k < 4u; k = k + 1u) {
            let wg = gb_g16(t16 + 2u * k) | (gb_g16(t16 + 1u + 2u * k) << 16u);
            let wu = gb_u16(t16 + 2u * k) | (gb_u16(t16 + 1u + 2u * k) << 16u);
            dg = dg + gb_dot8(wg, xb + 8u * k);
            du = du + gb_dot8(wu, xb + 8u * k);
        }
        ag = ag + sg * dg;
        au = au + su * du;
    }
    gb_pg[lid] = ag;
    gb_pu[lid] = au;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) {
            gb_pg[lid] = gb_pg[lid] + gb_pg[lid + stride];
            gb_pu[lid] = gb_pu[lid] + gb_pu[lid + stride];
        }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) {
        let g = gb_pg[0];
        var gg = g;
        var uu = gb_pu[0];
        if (gb_p.lim > 0.0) {
            uu = clamp(uu, -gb_p.lim, gb_p.lim);
            gg = min(gg, gb_p.lim);
        }
        gb_act[(t * gb_p.slots + slot) * gb_p.inter + row] =
            (gg / (1.0 + exp(-gg))) * uu;
    }
}

// Weighted down-projection with a token axis; writes STRAIGHT into the
// batch FFN output at the token's row — no staging row, no copy back.
struct MoeDnBP { gpr: u32, hidden: u32, slots: u32, mat16: u32 };
@group(0) @binding(0) var<storage, read>       db_w   : array<u32>;
@group(0) @binding(1) var<storage, read>       db_act : array<f32>;
@group(0) @binding(2) var<storage, read>       db_sel : array<u32>;
@group(0) @binding(3) var<storage, read>       db_wt  : array<f32>;
@group(0) @binding(4) var<storage, read_write> db_y   : array<f32>;
@group(0) @binding(5) var<uniform>             db_p   : MoeDnBP;
// The same weight buffer, seen 16 bytes at a time: one q4tp group tile is
// exactly one vec4<u32>, and the scalar view above pays four transactions
// for it. Only the b4 kernel binds this view.
@group(0) @binding(6) var<storage, read>       db_wv  : array<vec4<u32>>;
// The activations too: the four-row kernel once staged them through a
// function-scope array, and dynamic indexing sent that array to local
// memory — every dot read paid a spill. Eight named vec4 registers do
// what the array was meant to.
@group(0) @binding(7) var<storage, read>       db_x4  : array<vec4<f32>>;

// Takes and returns the running sum so the fold order is EXACTLY the
// scalar kernel's left-to-right chain — grouping the eight terms first
// would round differently.
fn db_dotv(acc: f32, w: u32, a: vec4<f32>, b: vec4<f32>) -> f32 {
    return acc
         + (f32(w & 0xFu) - 8.0) * a.x
         + (f32((w >> 4u) & 0xFu) - 8.0) * a.y
         + (f32((w >> 8u) & 0xFu) - 8.0) * a.z
         + (f32((w >> 12u) & 0xFu) - 8.0) * a.w
         + (f32((w >> 16u) & 0xFu) - 8.0) * b.x
         + (f32((w >> 20u) & 0xFu) - 8.0) * b.y
         + (f32((w >> 24u) & 0xFu) - 8.0) * b.z
         + (f32((w >> 28u) & 0xFu) - 8.0) * b.w;
}
var<workgroup> db_pt: array<f32, 64>;

fn db_u16(o: u32) -> u32 { return (db_w[o >> 1u] >> ((o & 1u) * 16u)) & 0xFFFFu; }
fn db_u8(o: u32) -> u32 { return (db_w[o >> 2u] >> ((o & 3u) * 8u)) & 0xFFu; }
fn db_dot8(w: u32, xi: u32) -> f32 {
    return (f32(w & 0xFu) - 8.0) * db_act[xi]
         + (f32((w >> 4u) & 0xFu) - 8.0) * db_act[xi + 1u]
         + (f32((w >> 8u) & 0xFu) - 8.0) * db_act[xi + 2u]
         + (f32((w >> 12u) & 0xFu) - 8.0) * db_act[xi + 3u]
         + (f32((w >> 16u) & 0xFu) - 8.0) * db_act[xi + 4u]
         + (f32((w >> 20u) & 0xFu) - 8.0) * db_act[xi + 5u]
         + (f32((w >> 24u) & 0xFu) - 8.0) * db_act[xi + 6u]
         + (f32((w >> 28u) & 0xFu) - 8.0) * db_act[xi + 7u];
}

@compute @workgroup_size(64)
fn moe_down_q4tp_b(@builtin(workgroup_id) wid: vec3<u32>,
                   @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let t = wid.y;
    let gpr = db_p.gpr;
    let rows = db_p.hidden;
    let inter = gpr * 32u;
    let cst = (gpr * 5u + 7u) / 8u;
    let sb = t * db_p.slots;
    let ab = t * db_p.slots * inter;
    let total = db_p.slots * gpr;
    var acc = 0.0;
    for (var i = lid; i < total; i = i + 64u) {
        let slot = i / gpr;
        let g = i % gpr;
        let base16 = db_sel[sb + slot] * db_p.mat16;
        let par16 = base16 + rows * gpr * 8u + row * 2u;
        let cod8 = (base16 + rows * gpr * 8u + rows * 2u) * 2u + row * cst;
        let pl = unpack2x16float(db_u16(par16) | (db_u16(par16 + 1u) << 16u));
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cv = db_u8(cod8 + cb);
        if (shf > 3u) { cv = cv | (db_u8(cod8 + cb + 1u) << 8u); }
        let scale = exp2(pl.x + f32((cv >> shf) & 31u) * pl.y);
        let t16 = base16 + (row * gpr + g) * 8u;
        let xb = ab + (slot * gpr + g) * 32u;
        var d = 0.0;
        for (var k = 0u; k < 4u; k = k + 1u) {
            let w = db_u16(t16 + 2u * k) | (db_u16(t16 + 1u + 2u * k) << 16u);
            d = d + db_dot8(w, xb + 8u * k);
        }
        acc = acc + db_wt[sb + slot] * scale * d;
    }
    db_pt[lid] = acc;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { db_pt[lid] = db_pt[lid] + db_pt[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { db_y[t * rows + row] = db_pt[0]; }
}

// The down-projection split for parallelism: one workgroup per
// (row, slot, token) writes its weighted partial, a second dispatch sums
// the slots in ascending order. The single-dispatch kernel above gives
// each (row, token) all nine slots and stalls on latency — measured 0.71
// ms of the layer against gate/up's 0.22 for the same bytes. Round-off
// class: the partial's reduction tree differs from the fused loop's.
@compute @workgroup_size(64)
fn moe_down_q4tp_part(@builtin(workgroup_id) wid: vec3<u32>,
                      @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let slot = wid.y;
    let t = wid.z;
    let gpr = db_p.gpr;
    let rows = db_p.hidden;
    let cst = (gpr * 5u + 7u) / 8u;
    let sb = t * db_p.slots;
    let ab = t * db_p.slots * gpr * 32u;
    let base16 = db_sel[sb + slot] * db_p.mat16;
    let par16 = base16 + rows * gpr * 8u + row * 2u;
    let pl = unpack2x16float(db_u16(par16) | (db_u16(par16 + 1u) << 16u));
    let cod8 = (base16 + rows * gpr * 8u + rows * 2u) * 2u + row * cst;
    var acc = 0.0;
    for (var g = lid; g < gpr; g = g + 64u) {
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cv = db_u8(cod8 + cb);
        if (shf > 3u) { cv = cv | (db_u8(cod8 + cb + 1u) << 8u); }
        let scale = exp2(pl.x + f32((cv >> shf) & 31u) * pl.y);
        let t16 = base16 + (row * gpr + g) * 8u;
        let xb = ab + (slot * gpr + g) * 32u;
        var d = 0.0;
        for (var k = 0u; k < 4u; k = k + 1u) {
            let w = db_u16(t16 + 2u * k) | (db_u16(t16 + 1u + 2u * k) << 16u);
            d = d + db_dot8(w, xb + 8u * k);
        }
        acc = acc + scale * d;
    }
    db_pt[lid] = acc;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { db_pt[lid] = db_pt[lid] + db_pt[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) {
        db_y[(t * db_p.slots + slot) * rows + row] = db_wt[sb + slot] * db_pt[0];
    }
}

// …and the slot sum, one thread per (row, token), slots ascending.
@group(0) @binding(0) var<storage, read>       dr_part : array<f32>;
@group(0) @binding(1) var<storage, read_write> dr_y    : array<f32>;
@group(0) @binding(2) var<uniform>             dr_p    : MoeDnBP;
@compute @workgroup_size(256)
fn moe_down_q4tp_red(@builtin(workgroup_id) wid: vec3<u32>,
                     @builtin(local_invocation_index) lid: u32) {
    let row = wid.x * 256u + lid;
    let t = wid.y;
    if (row >= dr_p.hidden) { return; }
    var acc = 0.0;
    for (var s = 0u; s < dr_p.slots; s = s + 1u) {
        acc = acc + dr_part[(t * dr_p.slots + s) * dr_p.hidden + row];
    }
    dr_y[t * dr_p.hidden + row] = acc;
}

// The 2-bit down-projection for the DRAFT's experts: the same ladder
// and packing as gate/up (five-bit rung codes, sixteen weights a word),
// pointed the other way. Draft-only fidelity: nothing downstream needs
// this to match a walk bit for bit — a trunk pass verifies every token.
@compute @workgroup_size(64)
fn moe_down_q2tp_b(@builtin(workgroup_id) wid: vec3<u32>,
                   @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let t = wid.y;
    let gpr = db_p.gpr;
    let rows = db_p.hidden;
    let cst = (gpr * 5u + 7u) / 8u;
    let sb = t * db_p.slots;
    let ab4 = t * db_p.slots * gpr * 8u;
    let total = db_p.slots * gpr;
    var cur = 0xFFFFFFFFu;
    var base16 = 0u;
    var cod8 = 0u;
    var sw = 0.0;
    var pl = vec2<f32>(0.0, 0.0);
    var acc = 0.0;
    for (var i = lid; i < total; i = i + 64u) {
        let slot = i / gpr;
        let g = i % gpr;
        if (slot != cur) {
            cur = slot;
            base16 = db_sel[sb + slot] * db_p.mat16;
            let par16 = base16 + rows * gpr * 4u + row * 2u;
            pl = unpack2x16float(db_u16(par16) | (db_u16(par16 + 1u) << 16u));
            cod8 = (base16 + rows * gpr * 4u + rows * 2u) * 2u + row * cst;
            sw = db_wt[sb + slot];
        }
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cv = db_u8(cod8 + cb);
        if (shf > 3u) { cv = cv | (db_u8(cod8 + cb + 1u) << 8u); }
        let code = (cv >> shf) & 31u;
        let scale = select(exp2(pl.x + f32(max(code, 1u) - 1u) * pl.y), 0.0, code == 0u);
        let w32 = (base16 + (row * gpr + g) * 4u) >> 1u;
        let x4 = ab4 + (slot * gpr + g) * 8u;
        let d = mg_dot16v(db_w[w32], db_x4[x4], db_x4[x4 + 1u], db_x4[x4 + 2u], db_x4[x4 + 3u])
              + mg_dot16v(db_w[w32 + 1u], db_x4[x4 + 4u], db_x4[x4 + 5u], db_x4[x4 + 6u],
                          db_x4[x4 + 7u]);
        acc = acc + sw * scale * d;
    }
    db_pt[lid] = acc;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { db_pt[lid] = db_pt[lid] + db_pt[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { db_y[t * rows + row] = db_pt[0]; }
}

// Four rows per workgroup: the x span is loaded once per thread and
// feeds four weight tiles, cutting the L2 activation traffic that limits
// the one-row kernel four-fold. Each row's accumulation order equals the
// one-row kernel's, so per-row sums are bit-identical.
@compute @workgroup_size(64)
fn moe_down_q4tp_b4(@builtin(workgroup_id) wid: vec3<u32>,
                    @builtin(local_invocation_index) lid: u32) {
    let row0 = wid.x * 4u;
    let t = wid.y;
    let gpr = db_p.gpr;
    let rows = db_p.hidden;
    let cst = (gpr * 5u + 7u) / 8u;
    let sb = t * db_p.slots;
    let ab = t * db_p.slots * gpr * 32u;
    let total = db_p.slots * gpr;
    var cur = 0xFFFFFFFFu;
    var base16 = 0u;
    var codb = 0u;
    var sw = 0.0;
    var pl = vec2<f32>(0.0, 0.0);
    var pl1 = vec2<f32>(0.0, 0.0);
    var pl2 = vec2<f32>(0.0, 0.0);
    var pl3 = vec2<f32>(0.0, 0.0);
    var a0 = 0.0; var a1 = 0.0; var a2 = 0.0; var a3 = 0.0;
    for (var i = lid; i < total; i = i + 64u) {
        let slot = i / gpr;
        let g = i % gpr;
        if (slot != cur) {
            cur = slot;
            base16 = db_sel[sb + slot] * db_p.mat16;
            let par16 = base16 + rows * gpr * 8u + row0 * 2u;
            pl = unpack2x16float(db_u16(par16) | (db_u16(par16 + 1u) << 16u));
            pl1 = unpack2x16float(db_u16(par16 + 2u) | (db_u16(par16 + 3u) << 16u));
            pl2 = unpack2x16float(db_u16(par16 + 4u) | (db_u16(par16 + 5u) << 16u));
            pl3 = unpack2x16float(db_u16(par16 + 6u) | (db_u16(par16 + 7u) << 16u));
            codb = (base16 + rows * gpr * 8u + rows * 2u) * 2u;
            sw = db_wt[sb + slot];
        }
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        let x4 = (ab + (slot * gpr + g) * 32u) >> 2u;
        // The x span once, into eight NAMED vec4s — registers, not a
        // spilled array.
        let v0 = db_x4[x4];      let v1 = db_x4[x4 + 1u];
        let v2 = db_x4[x4 + 2u]; let v3 = db_x4[x4 + 3u];
        let v4 = db_x4[x4 + 4u]; let v5 = db_x4[x4 + 5u];
        let v6 = db_x4[x4 + 6u]; let v7 = db_x4[x4 + 7u];
        for (var r = 0u; r < 4u; r = r + 1u) {
            let row = row0 + r;
            if (row >= rows) { break; }
            let cod8 = codb + row * cst;
            var cv = db_u8(cod8 + cb);
            if (shf > 3u) { cv = cv | (db_u8(cod8 + cb + 1u) << 8u); }
            var plr = pl;
            if (r == 1u) { plr = pl1; }
            if (r == 2u) { plr = pl2; }
            if (r == 3u) { plr = pl3; }
            let scale = exp2(plr.x + f32((cv >> shf) & 31u) * plr.y);
            let wv = db_wv[(base16 + (row * gpr + g) * 8u) >> 3u];
            let d = db_dotv(
                db_dotv(db_dotv(db_dotv(0.0, wv.x, v0, v1), wv.y, v2, v3), wv.z, v4, v5),
                wv.w, v6, v7,
            );
            let v = sw * scale * d;
            if (r == 0u) { a0 = a0 + v; }
            if (r == 1u) { a1 = a1 + v; }
            if (r == 2u) { a2 = a2 + v; }
            if (r == 3u) { a3 = a3 + v; }
        }
    }
    // Four reductions through the same shared tree, one row at a time —
    // the tree per row equals the one-row kernel's.
    for (var r = 0u; r < 4u; r = r + 1u) {
        var acc = a0;
        if (r == 1u) { acc = a1; }
        if (r == 2u) { acc = a2; }
        if (r == 3u) { acc = a3; }
        db_pt[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { db_pt[lid] = db_pt[lid] + db_pt[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u && row0 + r < rows) { db_y[t * rows + row0 + r] = db_pt[0]; }
        workgroupBarrier();
    }
}

// The same weighted down-projection with the loads it always meant: the
// tile's four u32 words read directly (the u16 pair OR-ed together above
// is two loads of ONE word — legal only because the tile base is even,
// which the host checks), and the slot's params fetched once per slot
// instead of once per group. The per-thread accumulation order is the
// original loop's, so the sum is bit-identical.
@compute @workgroup_size(64)
fn moe_down_q4tp_b2(@builtin(workgroup_id) wid: vec3<u32>,
                    @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let t = wid.y;
    let gpr = db_p.gpr;
    let rows = db_p.hidden;
    let cst = (gpr * 5u + 7u) / 8u;
    let sb = t * db_p.slots;
    let ab = t * db_p.slots * gpr * 32u;
    let total = db_p.slots * gpr;
    var cur = 0xFFFFFFFFu;
    var base16 = 0u;
    var cod8 = 0u;
    var sw = 0.0;
    var pl = vec2<f32>(0.0, 0.0);
    var acc = 0.0;
    for (var i = lid; i < total; i = i + 64u) {
        let slot = i / gpr;
        let g = i % gpr;
        if (slot != cur) {
            cur = slot;
            base16 = db_sel[sb + slot] * db_p.mat16;
            let par16 = base16 + rows * gpr * 8u + row * 2u;
            pl = unpack2x16float(db_u16(par16) | (db_u16(par16 + 1u) << 16u));
            cod8 = (base16 + rows * gpr * 8u + rows * 2u) * 2u + row * cst;
            sw = db_wt[sb + slot];
        }
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cv = db_u8(cod8 + cb);
        if (shf > 3u) { cv = cv | (db_u8(cod8 + cb + 1u) << 8u); }
        let scale = exp2(pl.x + f32((cv >> shf) & 31u) * pl.y);
        let w32 = (base16 + (row * gpr + g) * 8u) >> 1u;
        let xb = ab + (slot * gpr + g) * 32u;
        var d = 0.0;
        for (var k = 0u; k < 4u; k = k + 1u) {
            d = d + db_dot8(db_w[w32 + k], xb + 8u * k);
        }
        acc = acc + sw * scale * d;
    }
    db_pt[lid] = acc;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { db_pt[lid] = db_pt[lid] + db_pt[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { db_y[t * rows + row] = db_pt[0]; }
}


// ── O(1) Nystrom attention on the graph (spec: nystrom.rs step/far_insert).
// State lives on the device after one upload per seal epoch: ring window,
// sinks, landmarks, and each head's flash-scaled far skeleton. Per o1
// layer per token: THREE dispatches replacing kv_append+attend, and the
// work is O(m + w) instead of O(ctx) — the graph's exact attend was
// 54 -> 37.8 tok/s from 4K to 16K while o1 holds flat by construction.
struct O1P { hpg: u32, m: u32, w: u32, nsrect: u32, d: u32, dv: u32, scale: f32, _p: u32 };

@group(0) @binding(0) var<storage, read_write> of_meta : array<u32>;
@group(0) @binding(1) var<storage, read>       of_rk   : array<f32>;
@group(0) @binding(2) var<storage, read>       of_rv   : array<f32>;
@group(0) @binding(3) var<storage, read>       of_qt   : array<f32>;
@group(0) @binding(4) var<storage, read_write> of_mz   : array<f32>;
@group(0) @binding(5) var<storage, read_write> of_th   : array<f32>;
@group(0) @binding(6) var<uniform>             of_p    : O1P;
var<workgroup> of_part: array<f32, 64>;
var<workgroup> of_rs: f32;
var<workgroup> of_e: f32;

// One workgroup per (group, head, landmark): absorb the evicted window
// slot into this head's far accumulators (nystrom.rs far_insert).
@compute @workgroup_size(64)
fn o1_far(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_index) lid: u32) {
    let hm = of_p.hpg * of_p.m;
    let g = wid.x / hm;
    let rr = wid.x % hm;
    let h = rr / of_p.m;
    let i = rr % of_p.m;
    let len = of_meta[g * 4u];
    if (len < of_p.w) { return; }
    let slot = of_meta[g * 4u + 1u];
    let d = of_p.d;
    let qb = ((g * of_p.hpg + h) * of_p.m + i) * d;
    let kb = (g * of_p.w + slot) * d;
    var acc = 0.0;
    var t = lid;
    loop {
        if (t >= d) { break; }
        acc = acc + of_qt[qb + t] * of_rk[kb + t];
        t = t + 64u;
    }
    of_part[lid] = acc;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { of_part[lid] = of_part[lid] + of_part[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    let mzb = (g * of_p.hpg + h) * 2u * of_p.m;
    if (lid == 0u) {
        let l = of_part[0] * of_p.scale;
        var mm = of_mz[mzb + i];
        var rs = 1.0;
        if (l > mm) {
            rs = exp(mm - l);
            of_mz[mzb + of_p.m + i] = of_mz[mzb + of_p.m + i] * rs;
            mm = l;
            of_mz[mzb + i] = l;
        }
        let e = exp(l - mm);
        of_mz[mzb + of_p.m + i] = of_mz[mzb + of_p.m + i] + e;
        of_rs = rs;
        of_e = e;
    }
    workgroupBarrier();
    let rs = of_rs;
    let e = of_e;
    let thb = ((g * of_p.hpg + h) * of_p.m + i) * of_p.dv;
    let vb = (g * of_p.w + slot) * of_p.dv;
    var u = lid;
    loop {
        if (u >= of_p.dv) { break; }
        of_th[thb + u] = of_th[thb + u] * rs + e * of_rv[vb + u];
        u = u + 64u;
    }
}

@group(0) @binding(0) var<storage, read_write> op_meta : array<u32>;
@group(0) @binding(1) var<storage, read>       op_k    : array<f32>;
@group(0) @binding(2) var<storage, read>       op_v    : array<f32>;
@group(0) @binding(3) var<storage, read_write> op_rk   : array<f32>;
@group(0) @binding(4) var<storage, read_write> op_rv   : array<f32>;
@group(0) @binding(5) var<uniform>             op_p    : O1P;

// One workgroup per group: push this token's rotated K and V into the
// window ring (after o1_far has read the slot being overwritten).
@compute @workgroup_size(256)
fn o1_push(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_index) lid: u32) {
    let g = wid.x;
    let len = op_meta[g * 4u];
    let head = op_meta[g * 4u + 1u];
    var slot = len;
    if (len == op_p.w) { slot = head; }
    let d = op_p.d;
    var t = lid;
    loop {
        if (t >= d) { break; }
        op_rk[(g * op_p.w + slot) * d + t] = op_k[g * d + t];
        t = t + 256u;
    }
    t = lid;
    loop {
        if (t >= op_p.dv) { break; }
        op_rv[(g * op_p.w + slot) * op_p.dv + t] = op_v[g * op_p.dv + t];
        t = t + 256u;
    }
    workgroupBarrier();
    if (lid == 0u) {
        if (len == op_p.w) {
            op_meta[g * 4u + 1u] = (head + 1u) % op_p.w;
            op_meta[g * 4u + 2u] = op_meta[g * 4u + 2u] + 1u;
        } else {
            op_meta[g * 4u] = len + 1u;
        }
    }
}

@group(0) @binding(0)  var<storage, read>       oa_meta : array<u32>;
@group(0) @binding(1)  var<storage, read>       oa_q    : array<f32>;
@group(0) @binding(2)  var<storage, read>       oa_rk   : array<f32>;
@group(0) @binding(3)  var<storage, read>       oa_rv   : array<f32>;
@group(0) @binding(4)  var<storage, read>       oa_sk   : array<f32>;
@group(0) @binding(5)  var<storage, read>       oa_sv   : array<f32>;
@group(0) @binding(6)  var<storage, read>       oa_kt   : array<f32>;
@group(0) @binding(7)  var<storage, read>       oa_mu   : array<f32>;
@group(0) @binding(8)  var<storage, read>       oa_mz   : array<f32>;
@group(0) @binding(9)  var<storage, read>       oa_th   : array<f32>;
@group(0) @binding(10) var<storage, read_write> oa_out  : array<f32>;
@group(0) @binding(11) var<uniform>             oa_p    : O1P;
var<workgroup> oa_qs:  array<f32, 256>;
var<workgroup> oa_scr: array<f32, 160>;
var<workgroup> oa_f:   array<f32, 32>;
var<workgroup> oa_u:   array<f32, 32>;
var<workgroup> oa_red: array<f32, 256>;
var<workgroup> oa_sc:  array<f32, 4>; // [c_all, far_den, den, have_far]

// One workgroup per (group, head): the whole Nystrom step output.
// Per-score dots run one THREAD per key/landmark (serial over d) — no
// barriers in the hot part, and the same product order as the CPU's
// scalar loop.
@compute @workgroup_size(256)
fn o1_attend(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_index) lid: u32) {
    let g = wid.x / oa_p.hpg;
    let h = wid.x % oa_p.hpg;
    let d = oa_p.d;
    let dv = oa_p.dv;
    let m = oa_p.m;
    let ns = oa_p.nsrect & 0xFFu;
    let rect_fm = (oa_p.nsrect >> 8u) != 0u;
    let len = oa_meta[g * 4u];
    let farl = oa_meta[g * 4u + 2u];
    let n = ns + len;
    let gh = g * oa_p.hpg + h;
    // q into shared
    var t = lid;
    loop {
        if (t >= d) { break; }
        oa_qs[t] = oa_q[gh * d + t];
        t = t + 256u;
    }
    workgroupBarrier();
    // near scores: thread s owns key s
    if (lid < n) {
        var acc = 0.0;
        if (lid < ns) {
            let kb = (g * ns + lid) * d;
            for (var j = 0u; j < d; j = j + 1u) { acc = acc + oa_qs[j] * oa_sk[kb + j]; }
        } else {
            let kb = (g * oa_p.w + (lid - ns)) * d;
            for (var j = 0u; j < d; j = j + 1u) { acc = acc + oa_qs[j] * oa_rk[kb + j]; }
        }
        oa_scr[lid] = acc * oa_p.scale;
    }
    // landmark scores: thread 200+a owns landmark a (disjoint from keys)
    if (lid >= 200u && lid < 200u + m && farl > 0u) {
        let a = lid - 200u;
        var acc = 0.0;
        let ktb = (g * m + a) * d;
        for (var j = 0u; j < d; j = j + 1u) {
            acc = acc + oa_qs[j] * oa_kt[ktb + j];
        }
        oa_f[a] = acc * oa_p.scale;
    }
    workgroupBarrier();
    // c = max near score (single thread — n <= 136, trivial)
    if (lid == 0u) {
        var c = -3.0e38;
        for (var sidx = 0u; sidx < n; sidx = sidx + 1u) { c = max(c, oa_scr[sidx]); }
        var c_all = c;
        var far_den = 0.0;
        var have_far = 0.0;
        if (farl > 0u) {
            var f = -3.0e38;
            for (var a = 0u; a < m; a = a + 1u) { f = max(f, oa_f[a]); }
            for (var a = 0u; a < m; a = a + 1u) { oa_f[a] = exp(oa_f[a] - f); }
            for (var b = 0u; b < m; b = b + 1u) {
                var uacc = 0.0;
                for (var a = 0u; a < m; a = a + 1u) {
                    uacc = uacc + oa_f[a] * oa_mu[(gh * m + a) * m + b];
                }
                if (rect_fm) { uacc = max(uacc, 0.0); }
                oa_u[b] = uacc;
            }
            let mzb = gh * 2u * m;
            for (var b = 0u; b < m; b = b + 1u) {
                c_all = max(c_all, f + oa_mz[mzb + b]);
            }
            for (var b = 0u; b < m; b = b + 1u) {
                let gain = oa_u[b] * exp(f + oa_mz[mzb + b] - c_all);
                oa_u[b] = gain;
                far_den = far_den + gain * oa_mz[mzb + m + b];
            }
            if (far_den >= 0.0) { have_far = 1.0; } else { far_den = 0.0; }
        }
        var den = far_den;
        for (var sidx = 0u; sidx < n; sidx = sidx + 1u) {
            let pv = exp(oa_scr[sidx] - c_all);
            oa_scr[sidx] = pv;
            den = den + pv;
        }
        oa_sc[0] = c_all;
        oa_sc[1] = far_den;
        oa_sc[2] = max(den, 1e-30);
        oa_sc[3] = have_far;
    }
    workgroupBarrier();
    let den = oa_sc[2];
    let have_far = oa_sc[3] > 0.5;
    t = lid;
    loop {
        if (t >= dv) { break; }
        var acc = 0.0;
        if (have_far) {
            for (var b = 0u; b < m; b = b + 1u) {
                acc = acc + oa_u[b] * oa_th[(gh * m + b) * dv + t];
            }
        }
        for (var sidx = 0u; sidx < ns; sidx = sidx + 1u) {
            acc = acc + oa_scr[sidx] * oa_sv[(g * ns + sidx) * dv + t];
        }
        for (var sidx = ns; sidx < n; sidx = sidx + 1u) {
            acc = acc + oa_scr[sidx] * oa_rv[(g * oa_p.w + (sidx - ns)) * dv + t];
        }
        oa_out[gh * dv + t] = acc / den;
        t = t + 256u;
    }
}

// ── DiT attention (imagegen): scores GEMM -> row softmax -> P·V.
// Same 64x64 tile / 4x4 named-scalar register block as the quantized
// GEMMs; the operands are plain f32 here, so the staging is a copy.
struct DitP { m: u32, k: u32, n: u32, scale: f32, s0: u32, causal: u32, _p0: u32, _p1: u32, };
@group(0) @binding(0) var<storage, read> da: array<f32>;
@group(0) @binding(1) var<storage, read> db: array<f32>;
@group(0) @binding(2) var<storage, read_write> dc: array<f32>;
@group(0) @binding(3) var<uniform> dp: DitP;
var<workgroup> dit_at: array<f32, 1024>;
var<workgroup> dit_bt: array<f32, 1024>;

fn dit_gemm(wid: vec3<u32>, lid: vec3<u32>, bt: bool) {
    let m0 = wid.y * 64u;
    let n0 = wid.x * 64u;
    let tid = lid.y * 16u + lid.x;
    var a00 = 0.0; var a01 = 0.0; var a02 = 0.0; var a03 = 0.0;
    var a10 = 0.0; var a11 = 0.0; var a12 = 0.0; var a13 = 0.0;
    var a20 = 0.0; var a21 = 0.0; var a22 = 0.0; var a23 = 0.0;
    var a30 = 0.0; var a31 = 0.0; var a32 = 0.0; var a33 = 0.0;
    var k0 = 0u;
    loop {
        if (k0 >= dp.k) { break; }
        for (var q = 0u; q < 4u; q = q + 1u) {
            let r = tid / 4u + q * 64u;
            if (r < 64u) {
                let c4 = (tid % 4u) * 4u;
                for (var e = 0u; e < 4u; e = e + 1u) {
                    let kk = k0 + c4 + e;
                    var va = 0.0;
                    if (m0 + r < dp.m && kk < dp.k) { va = da[(m0 + r) * dp.k + kk]; }
                    dit_at[r * 16u + c4 + e] = va;
                    var vb = 0.0;
                    if (n0 + r < dp.n && kk < dp.k) {
                        if (bt) { vb = db[(n0 + r) * dp.k + kk]; }
                        else { vb = db[kk * dp.n + n0 + r]; }
                    }
                    dit_bt[r * 16u + c4 + e] = vb;
                }
            }
        }
        workgroupBarrier();
        let ab = lid.y * 64u;
        let wb = lid.x * 64u;
        for (var k = 0u; k < 16u; k = k + 1u) {
            let x0 = dit_at[ab + k];
            let x1 = dit_at[ab + 16u + k];
            let x2 = dit_at[ab + 32u + k];
            let x3 = dit_at[ab + 48u + k];
            let y0 = dit_bt[wb + k];
            let y1 = dit_bt[wb + 16u + k];
            let y2 = dit_bt[wb + 32u + k];
            let y3 = dit_bt[wb + 48u + k];
            a00 = a00 + x0 * y0; a01 = a01 + x0 * y1;
            a02 = a02 + x0 * y2; a03 = a03 + x0 * y3;
            a10 = a10 + x1 * y0; a11 = a11 + x1 * y1;
            a12 = a12 + x1 * y2; a13 = a13 + x1 * y3;
            a20 = a20 + x2 * y0; a21 = a21 + x2 * y1;
            a22 = a22 + x2 * y2; a23 = a23 + x2 * y3;
            a30 = a30 + x3 * y0; a31 = a31 + x3 * y1;
            a32 = a32 + x3 * y2; a33 = a33 + x3 * y3;
        }
        workgroupBarrier();
        k0 = k0 + 16u;
    }
    let mb = m0 + lid.y * 4u;
    let nb2 = n0 + lid.x * 4u;
    dit_store4(mb, nb2, a00, a01, a02, a03);
    dit_store4(mb + 1u, nb2, a10, a11, a12, a13);
    dit_store4(mb + 2u, nb2, a20, a21, a22, a23);
    dit_store4(mb + 3u, nb2, a30, a31, a32, a33);
}

fn dit_store4(m: u32, n0: u32, v0: f32, v1: f32, v2: f32, v3: f32) {
    if (m >= dp.m) { return; }
    let base = m * dp.n + n0;
    if (n0 < dp.n) { dc[base] = v0 * dp.scale; }
    if (n0 + 1u < dp.n) { dc[base + 1u] = v1 * dp.scale; }
    if (n0 + 2u < dp.n) { dc[base + 2u] = v2 * dp.scale; }
    if (n0 + 3u < dp.n) { dc[base + 3u] = v3 * dp.scale; }
}

@compute @workgroup_size(16, 16)
fn dit_qk(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    dit_gemm(wid, lid, true);
}

@compute @workgroup_size(16, 16)
fn dit_pv(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    dit_gemm(wid, lid, false);
}

var<workgroup> dit_red: array<f32, 256>;

// Row softmax over dc, one workgroup per row of dp.n columns.
@compute @workgroup_size(256)
fn dit_softmax(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let row = wid.x * dp.n;
    let t = lid.x;
    // Causal bound: query `wid.x` may see keys 0..=s0+wid.x. Masked
    // entries are zeroed rather than set to -inf so the P·V GEMM that
    // follows reads a clean matrix.
    var lim = dp.n;
    if (dp.causal != 0u) { lim = min(dp.n, dp.s0 + wid.x + 1u); }
    var mx = -3.4e38;
    for (var j = t; j < lim; j = j + 256u) { mx = max(mx, dc[row + j]); }
    dit_red[t] = mx;
    workgroupBarrier();
    for (var s = 128u; s > 0u; s = s >> 1u) {
        if (t < s) { dit_red[t] = max(dit_red[t], dit_red[t + s]); }
        workgroupBarrier();
    }
    let m = dit_red[0];
    workgroupBarrier();
    var sum = 0.0;
    for (var j = t; j < lim; j = j + 256u) {
        let e = exp(dc[row + j] - m);
        dc[row + j] = e;
        sum = sum + e;
    }
    for (var j = lim + t; j < dp.n; j = j + 256u) { dc[row + j] = 0.0; }
    dit_red[t] = sum;
    workgroupBarrier();
    for (var s = 128u; s > 0u; s = s >> 1u) {
        if (t < s) { dit_red[t] = dit_red[t] + dit_red[t + s]; }
        workgroupBarrier();
    }
    let inv = 1.0 / dit_red[0];
    for (var j = t; j < lim; j = j + 256u) { dc[row + j] = dc[row + j] * inv; }
}

// [nh][n][hd] panel -> [n][nh*hd].
@compute @workgroup_size(256)
fn dit_unstack(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    let total = dp.m * dp.k * dp.n;   // nh * n * hd
    if (i >= total) { return; }
    let hd = dp.n;
    let n = dp.k;
    let h = i / (n * hd);
    let rest = i % (n * hd);
    let tok = rest / hd;
    let d = rest % hd;
    dc[tok * dp.m * hd + h * hd + d] = da[i];
}


// ── Per-head RMS and the rope tail (DeepSeek-V4) ────────────────────────────
//
// Three places need this and they differ only in flags: the queries take a
// second RMS on each head after wq_b and then a forward rotation; the shared
// KV vector takes a forward rotation alone; and attention's output takes the
// INVERSE rotation, a detail no naming convention would suggest.
//
// The rotation pairs ADJACENT coordinates — the reference builds its complex
// numbers with unflatten(-1, (2)) — and pairing halves instead agrees with it
// exactly at position 0 and nowhere else. That cost a night once.

struct RpP { nh: u32, hd: u32, rd: u32, flags: u32 };   // flags: 1 = rms, 2 = inverse
@group(0) @binding(0) var<storage, read_write> rp_x    : array<f32>;   // nh*hd
@group(0) @binding(1) var<storage, read>       rp_freq : array<f32>;   // rd/2
@group(0) @binding(2) var<uniform>             rp_p    : RpP;
@group(0) @binding(3) var<storage, read>       rp_pos  : array<f32>;   // [position, eps]

var<workgroup> rp_red: array<f32, 256>;

@compute @workgroup_size(256)
fn rope_heads(@builtin(workgroup_id) wid: vec3<u32>,
              @builtin(local_invocation_index) lid: u32) {
    let h = wid.x;
    if (h >= rp_p.nh) { return; }
    let hd = rp_p.hd;
    let rd = rp_p.rd;
    let b = h * hd;

    if ((rp_p.flags & 1u) != 0u) {
        var acc = 0.0;
        var i = lid;
        loop {
            if (i >= hd) { break; }
            let v = rp_x[b + i];
            acc = acc + v * v;
            i = i + 256u;
        }
        rp_red[lid] = acc;
        workgroupBarrier();
        var stride = 128u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { rp_red[lid] = rp_red[lid] + rp_red[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        let inv = inverseSqrt(rp_red[0] / f32(hd) + rp_pos[1]);
        workgroupBarrier();
        var j = lid;
        loop {
            if (j >= hd) { break; }
            rp_x[b + j] = rp_x[b + j] * inv;
            j = j + 256u;
        }
        workgroupBarrier();
    }

    // The tail only, adjacent pairs.
    let base = b + hd - rd;
    let pos = rp_pos[0];
    var t = lid;
    loop {
        if (t >= rd / 2u) { break; }
        let th = pos * rp_freq[t];
        var sn = sin(th);
        let cs = cos(th);
        if ((rp_p.flags & 2u) != 0u) { sn = -sn; }
        let a = rp_x[base + 2u * t];
        let c = rp_x[base + 2u * t + 1u];
        rp_x[base + 2u * t] = a * cs - c * sn;
        rp_x[base + 2u * t + 1u] = a * sn + c * cs;
        t = t + 256u;
    }
}

// ── Grouped low-rank output projection, stage A (DeepSeek-V4) ───────────────
//
// wo_a is block-diagonal wearing a dense disguise. It is stored as one
// [groups*lora, per_group] matrix, but row i multiplies ONLY the slice of the
// attention output that group i/lora owns — a matvec whose activation window
// slides with the row. One added term in the index buys the whole operator.
//
// It earns a kernel of its own by size: on the release checkpoint wo_a is 33M
// weights read once per layer per token, the largest single thing still on
// the CPU once the experts are away.
//
// q4tp layout; the per-row math is copied from q4tp_matvec unchanged, so the
// two agree bit for bit wherever they overlap — that is, at lora = rows,
// where the window stops sliding.

@compute @workgroup_size(64)
fn o_lora_a(@builtin(workgroup_id) wid: vec3<u32>,
            @builtin(num_workgroups) nwg: vec3<u32>,
            @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let lora = q1p._p0;
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    var row = wid.x;
    loop {
        if (row >= rows) { break; }
        if (lid < 32u) {
            let pr = unpack2x16float(q1w[params_w + row]);
            lad_q4tp[lid] = exp2(pr.x + f32(lid) * pr.y);
        }
        workgroupBarrier();
        // A row is exactly one group's width, so the slice offset needs no
        // parameter of its own: per_group = gpr * 32.
        let xoff = (row / lora) * gpr * 32u;
        var acc = 0.0;
        var g = lid;
        loop {
            if (g >= gpr) { break; }
            let bit = g * 5u;
            let cb = codes_b + row * cstride + (bit >> 3u);
            let sh = bit & 7u;
            var cv = q4tp_byte(cb);
            if (sh > 3u) { cv = cv | (q4tp_byte(cb + 1u) << 8u); }
            let scale = lad_q4tp[(cv >> sh) & 31u];
            let base = (row * gpr + g) * 4u;
            let xb = xoff + g * 32u;
            var gsum = 0.0;
            for (var k = 0u; k < 4u; k = k + 1u) {
                gsum = gsum + q4b_dot8(q1w[base + k], xb + 8u * k);
            }
            acc = acc + scale * gsum;
            g = g + 64u;
        }
        partial_q1t[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { partial_q1t[lid] = partial_q1t[lid] + partial_q1t[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) { q1y[row] = partial_q1t[0]; }
        workgroupBarrier();
        row = row + nwg.x;
    }
}

// The same grouped projection with 256 threads a row. `f32_matvec_w` showed
// what a 64-thread workgroup costs on this card; this kernel is the other
// one the chain leans on, and the output projection was 5.0 ms of a 45 ms
// chain. Its own partial array, because the 64-wide one is exactly 64 long.
var<workgroup> olw_part: array<f32, 256>;

@compute @workgroup_size(256)
fn o_lora_a_w(@builtin(workgroup_id) wid: vec3<u32>,
            @builtin(num_workgroups) nwg: vec3<u32>,
            @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let lora = q1p._p0;
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    var row = wid.x;
    loop {
        if (row >= rows) { break; }
        if (lid < 32u) {
            let pr = unpack2x16float(q1w[params_w + row]);
            lad_q4tp[lid] = exp2(pr.x + f32(lid) * pr.y);
        }
        workgroupBarrier();
        // A row is exactly one group's width, so the slice offset needs no
        // parameter of its own: per_group = gpr * 32.
        let xoff = (row / lora) * gpr * 32u;
        var acc = 0.0;
        var g = lid;
        loop {
            if (g >= gpr) { break; }
            let bit = g * 5u;
            let cb = codes_b + row * cstride + (bit >> 3u);
            let sh = bit & 7u;
            var cv = q4tp_byte(cb);
            if (sh > 3u) { cv = cv | (q4tp_byte(cb + 1u) << 8u); }
            let scale = lad_q4tp[(cv >> sh) & 31u];
            let base = (row * gpr + g) * 4u;
            let xb = xoff + g * 32u;
            var gsum = 0.0;
            for (var k = 0u; k < 4u; k = k + 1u) {
                gsum = gsum + q4b_dot8(q1w[base + k], xb + 8u * k);
            }
            acc = acc + scale * gsum;
            g = g + 256u;
        }
        olw_part[lid] = acc;
        workgroupBarrier();
        var stride = 128u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { olw_part[lid] = olw_part[lid] + olw_part[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u) { q1y[row] = olw_part[0]; }
        workgroupBarrier();
        row = row + nwg.x;
    }
}

var<workgroup> olm_part: array<f32, 256>;
var<workgroup> olm_lad: array<f32, 128>;

// FOUR rows at once, 64 lanes each. `gpr` is 128 on the release, so a row
// cannot use more than 64 lanes without half of them striding past the end —
// but four rows in one workgroup give the memory system four independent
// load streams to overlap, which is the same trick the 8-row q4tp kernel
// uses and the reason it beat the one-row one by 6.8 ms a token.
@compute @workgroup_size(256)
fn o_lora_a_m(@builtin(workgroup_id) wid: vec3<u32>,
              @builtin(num_workgroups) nwg: vec3<u32>,
              @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let lora = q1p._p0;
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    let sub = lid / 64u;          // which of the four rows
    let lane = lid % 64u;
    var row = wid.x * 4u + sub;
    loop {
        if (row >= rows) { break; }
        if (lane < 32u) {
            let pr = unpack2x16float(q1w[params_w + row]);
            olm_lad[sub * 32u + lane] = exp2(pr.x + f32(lane) * pr.y);
        }
        workgroupBarrier();
        let xoff = (row / lora) * gpr * 32u;
        var acc = 0.0;
        var g = lane;
        loop {
            if (g >= gpr) { break; }
            let bit = g * 5u;
            let cb = codes_b + row * cstride + (bit >> 3u);
            let sh = bit & 7u;
            var cv = q4tp_byte(cb);
            if (sh > 3u) { cv = cv | (q4tp_byte(cb + 1u) << 8u); }
            let scale = olm_lad[sub * 32u + ((cv >> sh) & 31u)];
            let base = (row * gpr + g) * 4u;
            let xb = xoff + g * 32u;
            var gsum = 0.0;
            for (var k = 0u; k < 4u; k = k + 1u) {
                gsum = gsum + q4b_dot8(q1w[base + k], xb + 8u * k);
            }
            acc = acc + scale * gsum;
            g = g + 64u;
        }
        olm_part[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lane < stride) {
                olm_part[lid] = olm_part[lid] + olm_part[lid + stride];
            }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lane == 0u) { q1y[row] = olm_part[sub * 64u]; }
        workgroupBarrier();
        row = row + nwg.x * 4u;
    }
}

// ── Sparse attention, split in two (DeepSeek-V4) ────────────────────────────
//
// The one-workgroup-per-head version leaves 64 workgroups on a card with
// 150-odd multiprocessors, and measured 0.54 ms a layer — the whole cost of
// the attention block once its encoding was cached away. Scores are cheap and
// genuinely per-head; the weighted sum is nh*hd independent outputs. So:
// scores in one dispatch of nh groups, the sum in another of nh*hd/256.

struct Sa2P { nh: u32, hd: u32, m: u32, scale: f32 };

@group(0) @binding(0) var<storage, read>       s2_q    : array<f32>;   // nh*hd
@group(0) @binding(1) var<storage, read>       s2_kv   : array<f32>;
@group(0) @binding(2) var<storage, read>       s2_idx  : array<u32>;   // m
@group(0) @binding(3) var<storage, read>       s2_sink : array<f32>;   // nh
@group(0) @binding(4) var<storage, read_write> s2_w    : array<f32>;   // nh*m
@group(0) @binding(5) var<uniform>             s2_p    : Sa2P;

var<workgroup> s2_red: array<f32, 256>;
var<workgroup> s2_sc:  array<f32, 1024>;
var<workgroup> s2_max: f32;
var<workgroup> s2_den: f32;

@compute @workgroup_size(256)
fn sa_scores(@builtin(workgroup_id) wid: vec3<u32>,
             @builtin(local_invocation_index) lid: u32) {
    let h = wid.x;
    if (h >= s2_p.nh) { return; }
    let hd = s2_p.hd;
    let m = s2_p.m;
    let qb = h * hd;

    var mx = s2_sink[h];
    var t = lid;
    loop {
        if (t >= m) { break; }
        let kb = s2_idx[t] * hd;
        var d = 0.0;
        for (var i = 0u; i < hd; i = i + 1u) { d = d + s2_q[qb + i] * s2_kv[kb + i]; }
        d = d * s2_p.scale;
        s2_sc[t] = d;
        mx = max(mx, d);
        t = t + 256u;
    }
    s2_red[lid] = mx;
    workgroupBarrier();
    var st = 128u;
    loop {
        if (st == 0u) { break; }
        if (lid < st) { s2_red[lid] = max(s2_red[lid], s2_red[lid + st]); }
        workgroupBarrier();
        st = st >> 1u;
    }
    if (lid == 0u) { s2_max = s2_red[0]; }
    workgroupBarrier();

    // The learned sink enters the denominator and NOT the numerator: that is
    // what lets a head attend to nothing at all.
    var acc = 0.0;
    var u = lid;
    loop {
        if (u >= m) { break; }
        let e = exp(s2_sc[u] - s2_max);
        s2_sc[u] = e;
        acc = acc + e;
        u = u + 256u;
    }
    s2_red[lid] = acc;
    workgroupBarrier();
    st = 128u;
    loop {
        if (st == 0u) { break; }
        if (lid < st) { s2_red[lid] = s2_red[lid] + s2_red[lid + st]; }
        workgroupBarrier();
        st = st >> 1u;
    }
    if (lid == 0u) { s2_den = s2_red[0] + exp(s2_sink[h] - s2_max); }
    workgroupBarrier();
    let inv = 1.0 / s2_den;
    var v = lid;
    loop {
        if (v >= m) { break; }
        s2_w[h * m + v] = s2_sc[v] * inv;
        v = v + 256u;
    }
}

@group(0) @binding(0) var<storage, read>       sy_w   : array<f32>;   // nh*m
@group(0) @binding(1) var<storage, read>       sy_kv  : array<f32>;
@group(0) @binding(2) var<storage, read>       sy_idx : array<u32>;
@group(0) @binding(3) var<storage, read_write> sy_out : array<f32>;   // nh*hd
@group(0) @binding(4) var<uniform>             sy_p   : Sa2P;

@compute @workgroup_size(256)
fn sa_apply(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    let hd = sy_p.hd;
    if (i >= sy_p.nh * hd) { return; }
    let h = i / hd;
    let d = i % hd;
    let m = sy_p.m;
    let wb = h * m;
    var acc = 0.0;
    for (var t = 0u; t < m; t = t + 1u) {
        acc = acc + sy_w[wb + t] * sy_kv[sy_idx[t] * hd + d];
    }
    sy_out[i] = acc;
}

// ── The KV compressor's pooling step (DeepSeek-V4) ──────────────────────────
//
// A softmax over the slot axis taken PER DIMENSION — not per token — then the
// weighted sum. Both compressors end here; they differ only in how the slots
// are gathered, so the gather lives inside the kernel and the graph never has
// to materialise the interleaved copy the CPU builds.
//
// Overlapping (ratio 4 in the release): each token contributes 2*width
// values, the first half belonging to the window that began half a stride
// earlier. Fold time pools 2*ratio slots — the previous window's taking their
// first half, the current window's taking their second. A missing previous
// window votes with -inf, which is also how a whole column of absent slots
// leaves the output at zero instead of dividing by nothing.

struct KpP { slots: u32, width: u32, ratio: u32, flags: u32 };
// flags: 1 = overlapping, 2 = a previous window exists, 4 = add the APE bias

@group(0) @binding(0) var<storage, read>       kp_pkv : array<f32>;
@group(0) @binding(1) var<storage, read>       kp_psc : array<f32>;
@group(0) @binding(2) var<storage, read>       kp_ckv : array<f32>;
@group(0) @binding(3) var<storage, read>       kp_csc : array<f32>;
@group(0) @binding(4) var<storage, read>       kp_ape : array<f32>;
@group(0) @binding(5) var<storage, read_write> kp_out : array<f32>;
@group(0) @binding(6) var<uniform>             kp_p   : KpP;

const KP_NINF: f32 = -3.0e38;

@compute @workgroup_size(256)
fn kv_pool(@builtin(global_invocation_id) gid: vec3<u32>) {
    let d = gid.x;
    let w = kp_p.width;
    if (d >= w) { return; }
    let slots = kp_p.slots;
    let r = kp_p.ratio;
    let overlap = (kp_p.flags & 1u) != 0u;
    let have_prev = (kp_p.flags & 2u) != 0u;
    let use_ape = (kp_p.flags & 4u) != 0u;

    // Pass one: the maximum, so the exponentials cannot overflow. A column
    // that is entirely absent stays at -inf and the slot is left at zero.
    var mx = KP_NINF;
    for (var t = 0u; t < slots; t = t + 1u) {
        var sc = KP_NINF;
        if (overlap) {
            if (t < r) {
                if (have_prev) { sc = kp_psc[t * 2u * w + d]; }
            } else {
                sc = kp_csc[(t - r) * 2u * w + w + d];
            }
        } else {
            sc = kp_csc[t * w + d];
            if (use_ape) { sc = sc + kp_ape[t * w + d]; }
        }
        mx = max(mx, sc);
    }
    if (mx <= KP_NINF) { kp_out[d] = 0.0; return; }

    var den = 0.0;
    var acc = 0.0;
    for (var t = 0u; t < slots; t = t + 1u) {
        var sc = KP_NINF;
        var kv = 0.0;
        if (overlap) {
            if (t < r) {
                if (have_prev) {
                    sc = kp_psc[t * 2u * w + d];
                    kv = kp_pkv[t * 2u * w + d];
                }
            } else {
                sc = kp_csc[(t - r) * 2u * w + w + d];
                kv = kp_ckv[(t - r) * 2u * w + w + d];
            }
        } else {
            sc = kp_csc[t * w + d];
            if (use_ape) { sc = sc + kp_ape[t * w + d]; }
            kv = kp_ckv[t * w + d];
        }
        if (sc > KP_NINF) {
            let e = exp(sc - mx);
            den = den + e;
            acc = acc + e * kv;
        }
    }
    if (den <= 0.0) { kp_out[d] = 0.0; return; }
    kp_out[d] = acc / den;
}

// ── The sparse indexer: scores, then the top-k (DeepSeek-V4) ────────────────
//
// The relu comes BEFORE the per-head weighting, so a head can vote for a
// position or abstain but never against it. Getting that order wrong produces
// scores that look reasonable and a top-k that is quietly different.

struct IxP { nh: u32, hd: u32, n_pos: u32, limit: u32 };

@group(0) @binding(0) var<storage, read>       ix_q   : array<f32>;   // nh*hd
@group(0) @binding(1) var<storage, read>       ix_kv  : array<f32>;   // n_pos*hd
@group(0) @binding(2) var<storage, read>       ix_w   : array<f32>;   // nh
@group(0) @binding(3) var<storage, read_write> ix_out : array<f32>;   // n_pos
@group(0) @binding(4) var<uniform>             ix_p   : IxP;

var<workgroup> ix_red: array<f32, 256>;

@compute @workgroup_size(256)
fn index_scores(@builtin(workgroup_id) wid: vec3<u32>,
                @builtin(local_invocation_index) lid: u32) {
    let t = wid.x;
    if (t >= ix_p.n_pos) { return; }
    if (t >= ix_p.limit) {
        if (lid == 0u) { ix_out[t] = KP_NINF; }
        return;
    }
    let hd = ix_p.hd;
    let kb = t * hd;
    // One lane per head: the relu makes the heads non-additive before their
    // weights, so a head's dot has to be finished by whoever owns it.
    var acc = 0.0;
    var h = lid;
    loop {
        if (h >= ix_p.nh) { break; }
        var dot = 0.0;
        let qb = h * hd;
        for (var i = 0u; i < hd; i = i + 1u) {
            dot = dot + ix_q[qb + i] * ix_kv[kb + i];
        }
        acc = acc + max(dot, 0.0) * ix_w[h];
        h = h + 256u;
    }
    ix_red[lid] = acc;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { ix_red[lid] = ix_red[lid] + ix_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { ix_out[t] = ix_red[0]; }
}

// Sparse attention, split over the ATTENDED POSITIONS.
//
// The one-workgroup-per-head kernel launches 64 workgroups on the release —
// 64 of the ~200 the card can hold, so it sits at a few percent occupancy and
// waits on memory latency rather than running out of arithmetic: the skip
// probe puts it at 18.1 ms of a 51.3 ms chain while doing 42M MACs a layer,
// which is about 1% of the machine.
//
// So each head's positions are cut into chunks, one workgroup a chunk, and
// each returns its own softmax frame — the running max, the running
// denominator, and an accumulator weighted against ITS max. The merge
// rescales the frames into a common max. Same flash-decoding shape as
// `gqa_attend_part`/`gqa_attend_merge` for the other architecture, and the
// same caveat: the sum happens in a different order, so this is a contract
// change, not an identity.

struct SapP { nh: u32, hd: u32, m: u32, nc: u32, ck: u32, _a: u32, _b: u32, scale: f32 };

@group(0) @binding(0) var<storage, read>       sp_q    : array<f32>;   // nh*hd
@group(0) @binding(1) var<storage, read>       sp_kv   : array<f32>;   // n*hd
@group(0) @binding(2) var<storage, read>       sp_idx  : array<u32>;   // m
@group(0) @binding(3) var<storage, read_write> sp_acc  : array<f32>;   // nh*nc*hd
@group(0) @binding(4) var<storage, read_write> sp_mx   : array<f32>;   // nh*nc
@group(0) @binding(5) var<storage, read_write> sp_ln   : array<f32>;   // nh*nc
@group(0) @binding(6) var<uniform>             sp_p    : SapP;

var<workgroup> sp_red: array<f32, 256>;
var<workgroup> sp_w:   array<f32, 256>;
var<workgroup> sp_mval: f32;
var<workgroup> sp_den: f32;

@compute @workgroup_size(256)
fn sparse_attend_part(@builtin(workgroup_id) wid: vec3<u32>,
                      @builtin(local_invocation_index) lid: u32) {
    let h = wid.x;
    let c = wid.y;
    if (h >= sp_p.nh || c >= sp_p.nc) { return; }
    let hd = sp_p.hd;
    let qb = h * hd;
    let lo = c * sp_p.ck;
    var hi = lo + sp_p.ck;
    if (hi > sp_p.m) { hi = sp_p.m; }
    let slot = h * sp_p.nc + c;
    if (lo >= hi) {
        if (lid == 0u) { sp_mx[slot] = -3.0e38; sp_ln[slot] = 0.0; }
        var z = lid;
        loop { if (z >= hd) { break; } sp_acc[slot * hd + z] = 0.0; z = z + 256u; }
        return;
    }
    let len = hi - lo;

    // 1. this chunk's scores.
    //
    // THIRTY-TWO THREADS TO A POSITION, not one. A thread that owns a whole
    // position reads its 512-float row by itself while its neighbour reads a
    // row two kilobytes away: every load is its own cache line and the
    // workgroup coalesces nothing. Splitting across k instead means the 32
    // threads of a group ask for 32 CONSECUTIVE floats at a time, which is
    // one transaction — and the head's positions are then done eight at a
    // time instead of 256, which costs a barrier per eight and is worth it.
    let lane = lid % 32u;
    let grp = lid / 32u;
    var t = grp;
    loop {
        if (t >= len) { break; }
        let p = sp_idx[lo + t];
        var d = 0.0;
        var k = lane;
        loop {
            if (k >= hd) { break; }
            d = d + sp_q[qb + k] * sp_kv[p * hd + k];
            k = k + 32u;
        }
        sp_red[lid] = d;
        workgroupBarrier();
        if (lane == 0u) {
            var sd = 0.0;
            for (var j = 0u; j < 32u; j = j + 1u) { sd = sd + sp_red[grp * 32u + j]; }
            sp_w[t] = sd * sp_p.scale;
        }
        workgroupBarrier();
        t = t + 8u;
    }
    var mx = -3.0e38;
    var t2 = lid;
    loop {
        if (t2 >= len) { break; }
        mx = max(mx, sp_w[t2]);
        t2 = t2 + 256u;
    }
    sp_red[lid] = mx;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { sp_red[lid] = max(sp_red[lid], sp_red[lid + stride]); }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { sp_mval = sp_red[0]; }
    workgroupBarrier();
    let mval = sp_mval;

    // 2. weights against THIS chunk's max, and its denominator
    var den = 0.0;
    t = lid;
    loop {
        if (t >= len) { break; }
        let e = exp(sp_w[t] - mval);
        sp_w[t] = e;
        den = den + e;
        t = t + 256u;
    }
    sp_red[lid] = den;
    workgroupBarrier();
    stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { sp_red[lid] = sp_red[lid] + sp_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) {
        sp_den = sp_red[0];
        sp_mx[slot] = mval;
        sp_ln[slot] = sp_red[0];
    }
    workgroupBarrier();

    // 3. the chunk's unnormalised accumulator, parallel over the output dim
    var k2 = lid;
    loop {
        if (k2 >= hd) { break; }
        var acc = 0.0;
        for (var i = 0u; i < len; i = i + 1u) {
            acc = acc + sp_w[i] * sp_kv[sp_idx[lo + i] * hd + k2];
        }
        sp_acc[slot * hd + k2] = acc;
        k2 = k2 + 256u;
    }
}

struct SamP { nh: u32, hd: u32, nc: u32, _a: u32 };

@group(0) @binding(0) var<storage, read>       sm_acc  : array<f32>;   // nh*nc*hd
@group(0) @binding(1) var<storage, read>       sm_mx   : array<f32>;   // nh*nc
@group(0) @binding(2) var<storage, read>       sm_ln   : array<f32>;   // nh*nc
@group(0) @binding(3) var<storage, read>       sm_sink : array<f32>;   // nh
@group(0) @binding(4) var<storage, read_write> sm_out  : array<f32>;   // nh*hd
@group(0) @binding(5) var<uniform>             sm_p    : SamP;

var<workgroup> sm_m: f32;
var<workgroup> sm_d: f32;

@compute @workgroup_size(256)
fn sparse_attend_merge(@builtin(workgroup_id) wid: vec3<u32>,
                       @builtin(local_invocation_index) lid: u32) {
    let h = wid.x;
    if (h >= sm_p.nh) { return; }
    let hd = sm_p.hd;
    let nc = sm_p.nc;
    // The sink enters the denominator and nothing else — which is what lets a
    // head attend to nothing at all.
    if (lid == 0u) {
        var mm = sm_sink[h];
        for (var c = 0u; c < nc; c = c + 1u) { mm = max(mm, sm_mx[h * nc + c]); }
        var dd = exp(sm_sink[h] - mm);
        for (var c = 0u; c < nc; c = c + 1u) {
            dd = dd + exp(sm_mx[h * nc + c] - mm) * sm_ln[h * nc + c];
        }
        sm_m = mm;
        sm_d = dd;
    }
    workgroupBarrier();
    let mm = sm_m;
    let inv = 1.0 / sm_d;
    var k = lid;
    loop {
        if (k >= hd) { break; }
        var y = 0.0;
        for (var c = 0u; c < nc; c = c + 1u) {
            y = y + exp(sm_mx[h * nc + c] - mm) * sm_acc[(h * nc + c) * hd + k];
        }
        sm_out[h * hd + k] = y * inv;
        k = k + 256u;
    }
}

// A copy, as a dispatch. `copy_buffer_to_buffer` cannot be recorded inside a
// compute pass, so every small copy in the prep path — this token's slot in
// the compressor window, the window shift, the closed window becoming the
// previous one — used to end a pass and start another. At ~30 passes a layer
// that bookkeeping was most of what a decode step cost, so the copies become
// dispatches and the whole layer becomes a handful of passes.

struct BlP { n: u32, soff: u32, doff: u32, _a: u32 };

@group(0) @binding(0) var<storage, read>       bl_src : array<f32>;
@group(0) @binding(1) var<storage, read_write> bl_dst : array<f32>;
@group(0) @binding(2) var<uniform>             bl_p   : BlP;

@compute @workgroup_size(256)
fn blit(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    if (i >= bl_p.n) { return; }
    bl_dst[bl_p.doff + i] = bl_src[bl_p.soff + i];
}

// Top-k without a sort. The CPU picks by repeated argmax, first maximum wins,
// and returns the winners in index order; the same set falls out of a rank —
// how many positions beat me, counting an equal score as a win only if it
// sits at a lower index — kept when it is below k. Two O(n^2) passes over one
// workgroup, which for the compressed axis is a few thousand comparisons and
// needs neither a scan nor an atomic to stay deterministic.

struct TkP { n: u32, k: u32, _a: u32, _b: u32 };

@group(0) @binding(0) var<storage, read>       tk_s   : array<f32>;   // n
@group(0) @binding(1) var<storage, read_write> tk_idx : array<u32>;   // k
@group(0) @binding(2) var<storage, read_write> tk_cnt : array<u32>;   // 1
@group(0) @binding(3) var<uniform>             tk_p   : TkP;

var<workgroup> tk_keep: array<u32, 4096>;

// A thousand threads. Both passes are O(n²) over the attended list and
// they run in ONE workgroup — at the release's 640 positions that is
// 410 000 comparisons on a single multiprocessor.
@compute @workgroup_size(1024)
fn top_k_index(@builtin(local_invocation_index) lid: u32) {
    let n = tk_p.n;
    var i = lid;
    loop {
        if (i >= n) { break; }
        let si = tk_s[i];
        var keep = 0u;
        if (si > KP_NINF) {
            var rank = 0u;
            for (var j = 0u; j < n; j = j + 1u) {
                let sj = tk_s[j];
                if (sj > KP_NINF) {
                    if (sj > si || (sj == si && j < i)) { rank = rank + 1u; }
                }
            }
            if (rank < tk_p.k) { keep = 1u; }
        }
        tk_keep[i] = keep;
        i = i + 1024u;
    }
    workgroupBarrier();
    // Position among the kept, by index — counted rather than scanned, which
    // costs one more pass and removes every ordering question.
    var m = lid;
    loop {
        if (m >= n) { break; }
        if (tk_keep[m] == 1u) {
            var before = 0u;
            for (var j = 0u; j < m; j = j + 1u) { before = before + tk_keep[j]; }
            tk_idx[before] = m;
        }
        m = m + 1024u;
    }
    workgroupBarrier();
    if (lid == 0u) {
        var total = 0u;
        for (var j = 0u; j < n; j = j + 1u) { total = total + tk_keep[j]; }
        tk_cnt[0] = total;
    }
}

// ── The attended-position list, assembled on the device ─────────────────────
//
// The sliding window first, in cache order, then the compressed positions the
// indexer picked — shifted by the window's CAPACITY, not by how much of it is
// in use, because that is where the compressed region starts in the layer's
// cache buffer.
//
// The length never has to come back from the card: it is `win_len + min(topk,
// finite positions)`, and the host knows both. Only the CONTENTS are a device
// secret, which is what lets the whole token stay in one submission.

struct IbP { win_len: u32, window: u32, k: u32, _p: u32 };
@group(0) @binding(0) var<storage, read>       ib_pick : array<u32>;
@group(0) @binding(1) var<storage, read_write> ib_out  : array<u32>;
@group(0) @binding(2) var<uniform>             ib_p    : IbP;

@compute @workgroup_size(256)
fn idx_build(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    if (i < ib_p.win_len) { ib_out[i] = i; return; }
    let j = i - ib_p.win_len;
    if (j < ib_p.k) { ib_out[i] = ib_p.window + ib_pick[j]; }
}

// ── MoE routing: sqrt-softplus, noaux_tc bias, top-k (DeepSeek-V4) ──────────
//
// The bias shifts the CHOICE and never the weight: the weight of a chosen
// expert is its pre-bias score. Swapping those two — an easy thing to do when
// the bias is right there — leaves a model that still speaks and routes
// slightly wrong forever.
//
// Ranking replaces the repeated argmax and gives selection order for free:
// rank i = how many experts beat it, an equal score counting only from a
// lower index, which is precisely what "first maximum wins" means. Ranks of
// the finite entries are dense from zero, so the count is just how many
// slots got filled.

struct RtP { n: u32, top_k: u32, flags: u32, scale: f32 };
// flags: 1 = bias present, 2 = mask present, 4 = indices forced (hash layers),
//        8 = pin the shared expert in slot top_k with weight 1,
//       16 = the packed set is a SUBSET: rt_map turns a global expert id into
//            a slot, or 0xFFFFFFFF when that expert did not fit on the card.
//
// A cold pick is not dropped and not substituted — it is handed back. The
// slot gets weight zero so the device contributes nothing for it, and the
// expert's global id and its real weight go into rt_cold for the host to
// finish. Routing therefore still ranges over every expert, which is the
// whole difference between this and a mask.
//
// With bit 8 the output is the `msel`/`mwt` pair the batched expert kernels
// read: top_k routed slots then the shared one, every slot written. Slots the
// router could not fill (a mask that closes too much) get weight ZERO rather
// than being left short — the kernels downstream take a fixed slot count, and
// a stale index with a live weight is how a token gets an expert nobody chose.

@group(0) @binding(0) var<storage, read>       rt_s      : array<f32>;   // n
@group(0) @binding(1) var<storage, read>       rt_bias   : array<f32>;   // n
@group(0) @binding(2) var<storage, read>       rt_mask   : array<u32>;   // n
@group(0) @binding(3) var<storage, read>       rt_forced : array<u32>;   // top_k
@group(0) @binding(4) var<storage, read_write> rt_idx    : array<u32>;   // top_k
@group(0) @binding(5) var<storage, read_write> rt_w      : array<f32>;   // top_k
@group(0) @binding(6) var<storage, read_write> rt_cnt    : array<u32>;   // 1
@group(0) @binding(7) var<uniform>             rt_p      : RtP;
@group(0) @binding(8) var<storage, read>       rt_map    : array<u32>;   // n
@group(0) @binding(9) var<storage, read_write> rt_cold   : array<u32>;   // 2*top_k

var<workgroup> rt_sc:   array<f32, 1024>;   // sqrt(softplus(score))
var<workgroup> rt_sh:   array<f32, 1024>;   // the same, biased and masked
var<workgroup> rt_used: array<u32, 64>;

// A THOUSAND threads. The ranking is O(n²) — for each expert it counts how
// many beat it — and with 256 routed experts that is 65 536 comparisons in
// ONE workgroup, which measured 1.65 ms of a 30.6 ms chain. Counting ranks
// is order-independent, so the tie-break (equal score, lower index wins) is
// untouched by how many threads do the counting.
@compute @workgroup_size(1024)
fn moe_route(@builtin(local_invocation_index) lid: u32) {
    let n = rt_p.n;
    let k = rt_p.top_k;
    let has_bias = (rt_p.flags & 1u) != 0u;
    let has_mask = (rt_p.flags & 2u) != 0u;
    let forced = (rt_p.flags & 4u) != 0u;

    // `shared` is a WGSL reserved word. Naming it that compiled here and
    // failed at pipeline creation, which took the whole context down and made
    // every GPU test pass by skipping.
    let pin_shared = (rt_p.flags & 8u) != 0u;
    let subset = (rt_p.flags & 16u) != 0u;
    if (lid < k) {
        rt_used[lid] = 0u;
        rt_idx[lid] = 0u;
        rt_w[lid] = 0.0;
        rt_cold[2u * lid] = 0xFFFFFFFFu;
        rt_cold[2u * lid + 1u] = 0u;
        // Diagnostic mirror, second half: every winner regardless of where it
        // lives. Nothing reads it but a human, so it cannot change an answer.
        rt_cold[2u * k + 2u * lid] = 0xFFFFFFFFu;
        rt_cold[2u * k + 2u * lid + 1u] = 0u;
    }
    // The shared expert sits LAST in the packing, which is `n` only when
    // every expert was packed. With a subset it is n_pack, carried in the
    // flags' upper bits — writing `n` there pointed the kernel past the end
    // of the buffer at whatever followed.
    let shared_slot = rt_p.flags >> 8u;
    if (pin_shared && lid == 0u) {
        rt_idx[k] = shared_slot;
        rt_w[k] = 1.0;
    }
    // Same reason: the zero-fill is a storage write that the ranking lanes
    // must not race with.
    storageBarrier();
    var i = lid;
    loop {
        if (i >= n) { break; }
        let v = rt_s[i];
        // softplus, guarded past 20 the way the reference's F.softplus is
        var sp = v;
        if (v <= 20.0) { sp = log(1.0 + exp(v)); }
        let sc = sqrt(sp);
        rt_sc[i] = sc;
        var sh = sc;
        if (has_bias) { sh = sh + rt_bias[i]; }
        if (has_mask && rt_mask[i] == 0u) { sh = KP_NINF; }
        rt_sh[i] = sh;
        i = i + 1024u;
    }
    workgroupBarrier();

    if (forced) {
        if (lid < k) {
            let e = rt_forced[lid];
            var w = 0.0;
            if (e < n) { w = rt_sc[e]; }
            if (subset && e < n) {
                let slot = rt_map[e];
                if (slot == 0xFFFFFFFFu) {
                    rt_idx[lid] = 0u;
                    rt_w[lid] = 0.0;
                    rt_cold[2u * lid] = e;
                    rt_cold[2u * lid + 1u] = bitcast<u32>(w);
                } else {
                    rt_idx[lid] = slot;
                    rt_w[lid] = w;
                }
            } else {
                rt_idx[lid] = e;
                rt_w[lid] = w;
            }
            rt_used[lid] = 1u;
        }
    } else {
        var m = lid;
        loop {
            if (m >= n) { break; }
            let si = rt_sh[m];
            if (si > KP_NINF) {
                var rank = 0u;
                for (var j = 0u; j < n; j = j + 1u) {
                    let sj = rt_sh[j];
                    if (sj > KP_NINF) {
                        if (sj > si || (sj == si && j < m)) { rank = rank + 1u; }
                    }
                }
                if (rank < k) {
                    rt_used[rank] = 1u;
                    rt_cold[2u * k + 2u * rank] = m;
                    rt_cold[2u * k + 2u * rank + 1u] = bitcast<u32>(rt_sh[m]);
                    // What the kernel believes it was handed, in the slots the
                    // winners do not use.
                    if (subset) {
                        let slot = rt_map[m];
                        if (slot == 0xFFFFFFFFu) {
                            // Cold: the device computes nothing for it and the
                            // host is told which expert and with what weight.
                            rt_idx[rank] = 0u;
                            rt_w[rank] = 0.0;
                            rt_cold[2u * rank] = m;
                            rt_cold[2u * rank + 1u] = bitcast<u32>(rt_sc[m]);
                        } else {
                            rt_idx[rank] = slot;
                            rt_w[rank] = rt_sc[m];
                        }
                    } else {
                        rt_idx[rank] = m;
                        rt_w[rank] = rt_sc[m];
                    }
                }
            }
            m = m + 1024u;
        }
    }
    // BOTH barriers. The ranking above writes rt_idx/rt_w, which are STORAGE
    // buffers, and the lane that normalises them below reads what every other
    // lane wrote. workgroupBarrier orders workgroup memory only; without the
    // storage barrier those writes need not be visible yet. With 8 experts it
    // happened to work, with 256 it did not — and the failure is a routing
    // weight quietly attached to the wrong expert.
    workgroupBarrier();
    storageBarrier();

    // Normalisation is a handful of terms; one lane keeps the add order fixed.
    if (lid == 0u) {

        var cnt = 0u;
        for (var j = 0u; j < k; j = j + 1u) {
            if (rt_used[j] == 1u) { cnt = cnt + 1u; }
        }
        rt_cnt[0] = cnt;
        // The sum runs over the chosen experts INCLUDING the cold ones — the
        // reference normalises across the whole top-k, and leaving them out
        // would inflate every surviving weight.
        var sum = 0.0;
        for (var j = 0u; j < cnt; j = j + 1u) {
            sum = sum + rt_w[j];
            if (rt_cold[2u * j] != 0xFFFFFFFFu) {
                sum = sum + bitcast<f32>(rt_cold[2u * j + 1u]);
            }
        }
        if (sum > 0.0) {
            let inv = rt_p.scale / sum;
            for (var j = 0u; j < cnt; j = j + 1u) {
                rt_w[j] = rt_w[j] * inv;
                if (rt_cold[2u * j] != 0xFFFFFFFFu) {
                    rt_cold[2u * j + 1u] =
                        bitcast<u32>(bitcast<f32>(rt_cold[2u * j + 1u]) * inv);
                }
            }
        }
        // The shared expert is not part of that normalisation — it rides at
        // weight 1 whatever the router decided.
    }
}

// ── Sparse attention over an index list (DeepSeek-V4) ───────────────────────
//
// Not the sliding-window attention the canonical graph encodes: the keys are
// named by an INDEX LIST — the window's positions followed by whichever
// compressed ones the indexer chose — and one KV vector of a single head's
// width serves all query heads. The learned sink enters the denominator and
// contributes nothing to the numerator, which is what lets a head attend to
// nothing at all.
//
// One workgroup per head. `hd` is 512 in the release, so the accumulator
// fits in workgroup storage and the whole head is one pass.

struct SaP { nh: u32, hd: u32, m: u32, scale: f32 };

@group(0) @binding(0) var<storage, read>       sa_q    : array<f32>;   // nh*hd
@group(0) @binding(1) var<storage, read>       sa_kv   : array<f32>;   // n*hd
@group(0) @binding(2) var<storage, read>       sa_idx  : array<u32>;   // m
@group(0) @binding(3) var<storage, read>       sa_sink : array<f32>;   // nh
@group(0) @binding(4) var<storage, read_write> sa_out  : array<f32>;   // nh*hd
@group(0) @binding(5) var<uniform>             sa_p    : SaP;

var<workgroup> sa_red: array<f32, 256>;
// Scores, then weights, for every attended position. Bounding it here bounds
// the index list: window (128) + index_topk (512) fits with room.
var<workgroup> sa_w: array<f32, 1024>;
var<workgroup> sa_max: f32;
var<workgroup> sa_den: f32;

@compute @workgroup_size(256)
fn sparse_attend(@builtin(workgroup_id) wid: vec3<u32>,
                 @builtin(local_invocation_index) lid: u32) {
    let h = wid.x;
    if (h >= sa_p.nh) { return; }
    let hd = sa_p.hd;
    let m = sa_p.m;
    let qb = h * hd;

    // 1. scores, kept — recomputing them per output dimension would cost
    //    hd times more, and computing them twice was the first draft's waste.
    var mx = sa_sink[h];
    var t = lid;
    loop {
        if (t >= m) { break; }
        let p = sa_idx[t];
        var d = 0.0;
        for (var k = 0u; k < hd; k = k + 1u) {
            d = d + sa_q[qb + k] * sa_kv[p * hd + k];
        }
        let sc = d * sa_p.scale;
        sa_w[t] = sc;
        mx = max(mx, sc);
        t = t + 256u;
    }
    sa_red[lid] = mx;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { sa_red[lid] = max(sa_red[lid], sa_red[lid + stride]); }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    let mval = sa_red[0];
    workgroupBarrier();

    // 2. weights and the denominator, the sink taking its share of the latter
    var den = 0.0;
    t = lid;
    loop {
        if (t >= m) { break; }
        let w = exp(sa_w[t] - mval);
        sa_w[t] = w;
        den = den + w;
        t = t + 256u;
    }
    sa_red[lid] = den;
    workgroupBarrier();
    stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { sa_red[lid] = sa_red[lid] + sa_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { sa_den = sa_red[0] + exp(sa_sink[h] - mval); }
    workgroupBarrier();
    let inv = 1.0 / sa_den;

    // 3. the weighted sum, parallel over the OUTPUT dimension. Splitting by
    //    position instead makes every thread accumulate into the same
    //    sa_acc[k] — a data race that the first draft called "serialised".
    var k = lid;
    loop {
        if (k >= hd) { break; }
        var acc = 0.0;
        for (var i = 0u; i < m; i = i + 1u) {
            acc = acc + sa_w[i] * sa_kv[sa_idx[i] * hd + k];
        }
        sa_out[qb + k] = acc * inv;
        k = k + 256u;
    }
}

// ── Hyper-connections on the device (DeepSeek-V4) ───────────────────────────
//
// The hidden state is `hc` copies of a `dim` vector, and a block folds them
// to one, runs, then expands back through a Sinkhorn-normalized mixing
// matrix. There is no ordinary residual, so this is not an add — it is the
// join between every pair of blocks, and leaving it on the CPU is what
// forces a round trip per layer.
//
// Sizes are small where it matters: hc is 4, mix_hc is 24, and only the fold
// runs over dim. One workgroup owns the whole thing.

struct HcP { hc: u32, dim: u32, iters: u32, eps: f32, nrm: u32, _a: u32, _b: u32, _c: u32 };

@group(0) @binding(0) var<storage, read>       hc_state : array<f32>;   // hc*dim
@group(0) @binding(1) var<storage, read>       hc_mix   : array<f32>;   // mix_hc, raw
@group(0) @binding(2) var<storage, read>       hc_sc    : array<f32>;   // 3
@group(0) @binding(3) var<storage, read>       hc_base  : array<f32>;   // mix_hc
@group(0) @binding(4) var<storage, read_write> hc_fold  : array<f32>;   // dim
@group(0) @binding(5) var<storage, read_write> hc_post  : array<f32>;   // hc
@group(0) @binding(6) var<storage, read_write> hc_comb  : array<f32>;   // hc*hc
@group(0) @binding(7) var<uniform>             hc_p     : HcP;
// The norm that ALWAYS follows the fold. Its own dispatch cost as much as
// the fold did and it reduces over the same vector this workgroup just
// wrote, so it is a phase, not a kernel. `hc_nrm != 0` turns it on.
@group(0) @binding(8) var<storage, read>       hc_nw    : array<f32>;   // dim
@group(0) @binding(9) var<storage, read_write> hc_out   : array<f32>;   // dim

// A thousand threads: this kernel reduces, so it is one workgroup by
// construction, and on one SM only more threads can hide the latency.
var<workgroup> hc_red: array<f32, 1024>;
var<workgroup> hc_pre_w: array<f32, 8>;
var<workgroup> hc_cmb_w: array<f32, 64>;
var<workgroup> hc_rsq: f32;

@compute @workgroup_size(1024)
fn hc_pre_fold(@builtin(local_invocation_index) lid: u32) {
    let hc = hc_p.hc;
    let dim = hc_p.dim;
    let n = hc * dim;

    // rsqrt(mean(state^2) + eps) — the reference scales the mixes by this,
    // and it is a mean over ALL copies, not per copy.
    var acc = 0.0;
    var i = lid;
    loop {
        if (i >= n) { break; }
        let v = hc_state[i];
        acc = acc + v * v;
        i = i + 1024u;
    }
    hc_red[lid] = acc;
    workgroupBarrier();
    var stride = 512u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { hc_red[lid] = hc_red[lid] + hc_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) {
        hc_rsq = inverseSqrt(hc_red[0] / f32(n) + hc_p.eps);
    }
    workgroupBarrier();
    let rsq = hc_rsq;

    // pre / post / comb. Thread 0 does it: hc is 4, and the Sinkhorn is a
    // sequential fixed point over a 4x4 — parallelising it would cost more
    // in barriers than it saves.
    if (lid == 0u) {
        for (var j = 0u; j < hc; j = j + 1u) {
            let m = hc_mix[j] * rsq * hc_sc[0] + hc_base[j];
            hc_pre_w[j] = 1.0 / (1.0 + exp(-m)) + hc_p.eps;
            let m2 = hc_mix[hc + j] * rsq * hc_sc[1] + hc_base[hc + j];
            hc_post[j] = 2.0 / (1.0 + exp(-m2));
        }
        // row softmax, then the alternating normalisation
        for (var j = 0u; j < hc; j = j + 1u) {
            var mx = -1e30;
            for (var k = 0u; k < hc; k = k + 1u) {
                let v = hc_mix[2u * hc + j * hc + k] * rsq * hc_sc[2]
                      + hc_base[2u * hc + j * hc + k];
                hc_cmb_w[j * hc + k] = v;
                mx = max(mx, v);
            }
            var sum = 0.0;
            for (var k = 0u; k < hc; k = k + 1u) {
                let e = exp(hc_cmb_w[j * hc + k] - mx);
                hc_cmb_w[j * hc + k] = e;
                sum = sum + e;
            }
            for (var k = 0u; k < hc; k = k + 1u) {
                hc_cmb_w[j * hc + k] = hc_cmb_w[j * hc + k] / sum + hc_p.eps;
            }
        }
        for (var k = 0u; k < hc; k = k + 1u) {          // first column pass
            var sum = 0.0;
            for (var j = 0u; j < hc; j = j + 1u) { sum = sum + hc_cmb_w[j * hc + k]; }
            for (var j = 0u; j < hc; j = j + 1u) {
                hc_cmb_w[j * hc + k] = hc_cmb_w[j * hc + k] / (sum + hc_p.eps);
            }
        }
        for (var it = 1u; it < hc_p.iters; it = it + 1u) {
            for (var j = 0u; j < hc; j = j + 1u) {
                var sum = 0.0;
                for (var k = 0u; k < hc; k = k + 1u) { sum = sum + hc_cmb_w[j * hc + k]; }
                for (var k = 0u; k < hc; k = k + 1u) {
                    hc_cmb_w[j * hc + k] = hc_cmb_w[j * hc + k] / (sum + hc_p.eps);
                }
            }
            for (var k = 0u; k < hc; k = k + 1u) {
                var sum = 0.0;
                for (var j = 0u; j < hc; j = j + 1u) { sum = sum + hc_cmb_w[j * hc + k]; }
                for (var j = 0u; j < hc; j = j + 1u) {
                    hc_cmb_w[j * hc + k] = hc_cmb_w[j * hc + k] / (sum + hc_p.eps);
                }
            }
        }
        for (var j = 0u; j < hc * hc; j = j + 1u) { hc_comb[j] = hc_cmb_w[j]; }
    }
    workgroupBarrier();

    // fold: y[d] = sum_j pre[j] * state[j*dim + d], and its sum of squares
    var acc3 = 0.0;
    var d = lid;
    loop {
        if (d >= dim) { break; }
        var y = 0.0;
        for (var j = 0u; j < hc; j = j + 1u) {
            y = y + hc_pre_w[j] * hc_state[j * dim + d];
        }
        hc_fold[d] = y;
        acc3 = acc3 + y * y;
        d = d + 1024u;
    }
    if (hc_p.nrm == 0u) { return; }
    hc_red[lid] = acc3;
    workgroupBarrier();
    var st2 = 512u;
    loop {
        if (st2 == 0u) { break; }
        if (lid < st2) { hc_red[lid] = hc_red[lid] + hc_red[lid + st2]; }
        workgroupBarrier();
        st2 = st2 >> 1u;
    }
    let inv = inverseSqrt(hc_red[0] / f32(dim) + hc_p.eps);
    var d2 = lid;
    loop {
        if (d2 >= dim) { break; }
        hc_out[d2] = hc_fold[d2] * inv * hc_nw[d2];
        d2 = d2 + 1024u;
    }
}

// The whole hyper-connection join in ONE dispatch.
//
// Expand, the mix projection, the fold with its Sinkhorn, and the norm are
// four dependent steps over a state of hc·dim floats — 16 384 on the
// release. As four dispatches they cost four kernel launches a piece and
// this happens TWICE a layer, which on 42 layers is 336 launches a token:
// the skip probe put "everything that is neither attention nor MoE" at
// 27.9 ms of a 51.3 ms chain, and this is the largest single item in it.
//
// One workgroup owns all four, with barriers where a dispatch boundary used
// to be. That is a real trade — 256 threads instead of the whole card — but
// the arithmetic is ~400k MACs and a launch is ~30 µs, so the trade is not
// close. `post`/`comb` are read by the expand and WRITTEN by the fold, in
// that order, which the barrier between them makes safe.

struct HbP { hc: u32, dim: u32, iters: u32, eps: f32, mix_hc: u32, _a: u32, _b: u32, _c: u32 };

@group(0) @binding(0)  var<storage, read>       hb_x     : array<f32>;   // dim
@group(0) @binding(1)  var<storage, read>       hb_res   : array<f32>;   // hc*dim
@group(0) @binding(2)  var<storage, read_write> hb_post  : array<f32>;   // hc
@group(0) @binding(3)  var<storage, read_write> hb_comb  : array<f32>;   // hc*hc
@group(0) @binding(4)  var<storage, read>       hb_mixw  : array<f32>;   // mix_hc*hc*dim
@group(0) @binding(5)  var<storage, read>       hb_sc    : array<f32>;   // 3
@group(0) @binding(6)  var<storage, read>       hb_base  : array<f32>;   // mix_hc
@group(0) @binding(7)  var<storage, read>       hb_nw    : array<f32>;   // dim
@group(0) @binding(8)  var<storage, read_write> hb_state : array<f32>;   // hc*dim
@group(0) @binding(9)  var<storage, read_write> hb_fold  : array<f32>;   // dim
@group(0) @binding(10) var<storage, read_write> hb_norm  : array<f32>;   // dim
@group(0) @binding(11) var<uniform>             hb_p     : HbP;

var<workgroup> hb_red: array<f32, 256>;
var<workgroup> hb_mix: array<f32, 64>;
var<workgroup> hb_pre: array<f32, 8>;
var<workgroup> hb_cmb: array<f32, 64>;
var<workgroup> hb_rsq: f32;

const HB_LANES: u32 = 8u;   // threads per mix output; 256/8 = 32 outputs max

@compute @workgroup_size(256)
fn hc_block(@builtin(local_invocation_index) lid: u32) {
    let hc = hb_p.hc;
    let dim = hb_p.dim;
    let n = hc * dim;
    let mh = hb_p.mix_hc;

    // ── 1. expand ─────────────────────────────────────────────────────────
    var i = lid;
    loop {
        if (i >= n) { break; }
        let j = i / dim;
        let d = i % dim;
        var y = hb_post[j] * hb_x[d];
        for (var k = 0u; k < hc; k = k + 1u) {
            y = y + hb_comb[k * hc + j] * hb_res[k * dim + d];
        }
        hb_state[i] = y;
        i = i + 256u;
    }
    workgroupBarrier();

    // ── 2. the mix projection, all outputs at once ────────────────────────
    // Eight threads to an output, striding the shared axis: one barrier for
    // the lot instead of one tree reduction per output.
    let o = lid / HB_LANES;
    let sub = lid % HB_LANES;
    var acc = 0.0;
    if (o < mh) {
        let base = o * n;
        var t = sub;
        loop {
            if (t >= n) { break; }
            acc = acc + hb_mixw[base + t] * hb_state[t];
            t = t + HB_LANES;
        }
    }
    hb_red[lid] = acc;
    workgroupBarrier();
    if (sub == 0u && o < mh) {
        var sm = 0.0;
        for (var t = 0u; t < HB_LANES; t = t + 1u) { sm = sm + hb_red[o * HB_LANES + t]; }
        hb_mix[o] = sm;
    }

    // ── 3. rsqrt(mean(state^2) + eps), over ALL copies ────────────────────
    var acc2 = 0.0;
    var i2 = lid;
    loop {
        if (i2 >= n) { break; }
        let v = hb_state[i2];
        acc2 = acc2 + v * v;
        i2 = i2 + 256u;
    }
    workgroupBarrier();           // hb_red is being reused
    hb_red[lid] = acc2;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { hb_red[lid] = hb_red[lid] + hb_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { hb_rsq = inverseSqrt(hb_red[0] / f32(n) + hb_p.eps); }
    workgroupBarrier();
    let rsq = hb_rsq;

    // ── 4. pre / post / comb, thread 0: hc is 4 and the Sinkhorn is a
    //       sequential fixed point over a 4x4.
    if (lid == 0u) {
        for (var j = 0u; j < hc; j = j + 1u) {
            let m = hb_mix[j] * rsq * hb_sc[0] + hb_base[j];
            hb_pre[j] = 1.0 / (1.0 + exp(-m)) + hb_p.eps;
            let m2 = hb_mix[hc + j] * rsq * hb_sc[1] + hb_base[hc + j];
            hb_post[j] = 2.0 / (1.0 + exp(-m2));
        }
        for (var j = 0u; j < hc; j = j + 1u) {
            var mx = -1e30;
            for (var k = 0u; k < hc; k = k + 1u) {
                let v = hb_mix[2u * hc + j * hc + k] * rsq * hb_sc[2]
                      + hb_base[2u * hc + j * hc + k];
                hb_cmb[j * hc + k] = v;
                mx = max(mx, v);
            }
            var sum = 0.0;
            for (var k = 0u; k < hc; k = k + 1u) {
                let e = exp(hb_cmb[j * hc + k] - mx);
                hb_cmb[j * hc + k] = e;
                sum = sum + e;
            }
            for (var k = 0u; k < hc; k = k + 1u) {
                hb_cmb[j * hc + k] = hb_cmb[j * hc + k] / sum + hb_p.eps;
            }
        }
        for (var k = 0u; k < hc; k = k + 1u) {
            var sum = 0.0;
            for (var j = 0u; j < hc; j = j + 1u) { sum = sum + hb_cmb[j * hc + k]; }
            for (var j = 0u; j < hc; j = j + 1u) {
                hb_cmb[j * hc + k] = hb_cmb[j * hc + k] / (sum + hb_p.eps);
            }
        }
        for (var it = 1u; it < hb_p.iters; it = it + 1u) {
            for (var j = 0u; j < hc; j = j + 1u) {
                var sum = 0.0;
                for (var k = 0u; k < hc; k = k + 1u) { sum = sum + hb_cmb[j * hc + k]; }
                for (var k = 0u; k < hc; k = k + 1u) {
                    hb_cmb[j * hc + k] = hb_cmb[j * hc + k] / (sum + hb_p.eps);
                }
            }
            for (var k = 0u; k < hc; k = k + 1u) {
                var sum = 0.0;
                for (var j = 0u; j < hc; j = j + 1u) { sum = sum + hb_cmb[j * hc + k]; }
                for (var j = 0u; j < hc; j = j + 1u) {
                    hb_cmb[j * hc + k] = hb_cmb[j * hc + k] / (sum + hb_p.eps);
                }
            }
        }
        for (var j = 0u; j < hc * hc; j = j + 1u) { hb_comb[j] = hb_cmb[j]; }
    }
    workgroupBarrier();

    // ── 5. fold, then the norm over it ────────────────────────────────────
    var acc3 = 0.0;
    var d = lid;
    loop {
        if (d >= dim) { break; }
        var y = 0.0;
        for (var j = 0u; j < hc; j = j + 1u) {
            y = y + hb_pre[j] * hb_state[j * dim + d];
        }
        hb_fold[d] = y;
        acc3 = acc3 + y * y;
        d = d + 256u;
    }
    hb_red[lid] = acc3;
    workgroupBarrier();
    stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { hb_red[lid] = hb_red[lid] + hb_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    let inv = inverseSqrt(hb_red[0] / f32(dim) + hb_p.eps);
    var d2 = lid;
    loop {
        if (d2 >= dim) { break; }
        hb_norm[d2] = hb_fold[d2] * inv * hb_nw[d2];
        d2 = d2 + 256u;
    }
}

// expand: state[j*dim+d] = post[j]*x[d] + sum_k comb[k*hc+j]*residual[k*dim+d]
// Summing over the FIRST index of comb, as the reference does — reading it
// the other way transposes the mixing and is not detectable by eye.
@group(0) @binding(0) var<storage, read>       he_x    : array<f32>;   // dim
@group(0) @binding(1) var<storage, read>       he_res  : array<f32>;   // hc*dim
@group(0) @binding(2) var<storage, read>       he_post : array<f32>;   // hc
@group(0) @binding(3) var<storage, read>       he_comb : array<f32>;   // hc*hc
@group(0) @binding(4) var<storage, read_write> he_out  : array<f32>;   // hc*dim
@group(0) @binding(5) var<uniform>             he_p    : HcP;
@compute @workgroup_size(256)
fn hc_post_expand(@builtin(global_invocation_id) gid: vec3<u32>) {
    let hc = he_p.hc;
    let dim = he_p.dim;
    let i = gid.x;
    if (i >= hc * dim) { return; }
    let j = i / dim;
    let d = i % dim;
    var y = he_post[j] * he_x[d];
    for (var k = 0u; k < hc; k = k + 1u) {
        y = y + he_comb[k * hc + j] * he_res[k * dim + d];
    }
    he_out[i] = y;
}

// ── Token-axis twins for the DeepSeek-V4 batched frame ─────────────────────
//
// The batch used to encode one FRAME per token: at B=5 that is five times the
// dispatches of a single token, and the chain is launch-latency-bound, so the
// batch was slower per token than the walk. These twins carry the token on a
// grid axis instead: per-token operands live in ONE buffer strided by the
// token, weights are read once per dispatch. Every kernel is an arithmetic
// copy of its single-token original — same add order, same tie-breaks — so
// greedy parity carries over term for term.

// rope_heads with grid (head, token): x is [b, nh*hd], the position comes
// from a per-token table instead of a uniform.
@group(0) @binding(0) var<storage, read_write> br_x    : array<f32>;
@group(0) @binding(1) var<storage, read>       br_freq : array<f32>;
@group(0) @binding(2) var<uniform>             br_p    : RpP;
@group(0) @binding(3) var<storage, read>       br_meta : array<vec2<f32>>;  // [pos, eps] per token
var<workgroup> br_red: array<f32, 256>;
@compute @workgroup_size(256)
fn bt_rope_heads(@builtin(workgroup_id) wid: vec3<u32>,
                 @builtin(local_invocation_index) lid: u32) {
    let h = wid.x;
    let t = wid.y;
    if (h >= br_p.nh) { return; }
    let hd = br_p.hd;
    let rd = br_p.rd;
    let b = t * br_p.nh * hd + h * hd;
    if ((br_p.flags & 1u) != 0u) {
        var acc = 0.0;
        var i = lid;
        loop {
            if (i >= hd) { break; }
            let v = br_x[b + i];
            acc = acc + v * v;
            i = i + 256u;
        }
        br_red[lid] = acc;
        workgroupBarrier();
        var stride = 128u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { br_red[lid] = br_red[lid] + br_red[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        let inv = inverseSqrt(br_red[0] / f32(hd) + br_meta[t].y);
        workgroupBarrier();
        var j = lid;
        loop {
            if (j >= hd) { break; }
            br_x[b + j] = br_x[b + j] * inv;
            j = j + 256u;
        }
        workgroupBarrier();
    }
    let base = b + hd - rd;
    let pos = br_meta[t].x;
    var tt = lid;
    loop {
        if (tt >= rd / 2u) { break; }
        let th = pos * br_freq[tt];
        var sn = sin(th);
        let cs = cos(th);
        if ((br_p.flags & 2u) != 0u) { sn = -sn; }
        let a = br_x[base + 2u * tt];
        let c = br_x[base + 2u * tt + 1u];
        br_x[base + 2u * tt] = a * cs - c * sn;
        br_x[base + 2u * tt + 1u] = a * sn + c * cs;
        tt = tt + 256u;
    }
}

// hc_pre_fold with grid (token): all per-token operands strided.
// `hc_p._a` carries mix_hc (the mixes stride).
@group(0) @binding(0) var<storage, read>       bhf_state : array<f32>;   // b, hc*dim
@group(0) @binding(1) var<storage, read>       bhf_mix   : array<f32>;   // b, mix_hc
@group(0) @binding(2) var<storage, read>       bhf_sc    : array<f32>;   // 3
@group(0) @binding(3) var<storage, read>       bhf_base  : array<f32>;   // mix_hc
@group(0) @binding(4) var<storage, read_write> bhf_fold  : array<f32>;   // b, dim
@group(0) @binding(5) var<storage, read_write> bhf_post  : array<f32>;   // b, hc
@group(0) @binding(6) var<storage, read_write> bhf_comb  : array<f32>;   // b, hc*hc
@group(0) @binding(7) var<uniform>             bhf_p     : HcP;
@group(0) @binding(8) var<storage, read>       bhf_nw    : array<f32>;   // dim
@group(0) @binding(9) var<storage, read_write> bhf_out   : array<f32>;   // b, dim
var<workgroup> bhf_red: array<f32, 1024>;
var<workgroup> bhf_pre_w: array<f32, 8>;
var<workgroup> bhf_cmb_w: array<f32, 64>;
var<workgroup> bhf_rsq: f32;
@compute @workgroup_size(1024)
fn bt_hc_pre_fold(@builtin(workgroup_id) wid: vec3<u32>,
                  @builtin(local_invocation_index) lid: u32) {
    let hc = bhf_p.hc;
    let dim = bhf_p.dim;
    let n = hc * dim;
    let t = wid.x;
    let sb = t * n;
    let mb = t * bhf_p._a;
    var acc = 0.0;
    var i = lid;
    loop {
        if (i >= n) { break; }
        let v = bhf_state[sb + i];
        acc = acc + v * v;
        i = i + 1024u;
    }
    bhf_red[lid] = acc;
    workgroupBarrier();
    var stride = 512u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { bhf_red[lid] = bhf_red[lid] + bhf_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) {
        bhf_rsq = inverseSqrt(bhf_red[0] / f32(n) + bhf_p.eps);
    }
    workgroupBarrier();
    let rsq = bhf_rsq;
    if (lid == 0u) {
        for (var j = 0u; j < hc; j = j + 1u) {
            let m = bhf_mix[mb + j] * rsq * bhf_sc[0] + bhf_base[j];
            bhf_pre_w[j] = 1.0 / (1.0 + exp(-m)) + bhf_p.eps;
            let m2 = bhf_mix[mb + hc + j] * rsq * bhf_sc[1] + bhf_base[hc + j];
            bhf_post[t * hc + j] = 2.0 / (1.0 + exp(-m2));
        }
        for (var j = 0u; j < hc; j = j + 1u) {
            var mx = -1e30;
            for (var k = 0u; k < hc; k = k + 1u) {
                let v = bhf_mix[mb + 2u * hc + j * hc + k] * rsq * bhf_sc[2]
                      + bhf_base[2u * hc + j * hc + k];
                bhf_cmb_w[j * hc + k] = v;
                mx = max(mx, v);
            }
            var sum = 0.0;
            for (var k = 0u; k < hc; k = k + 1u) {
                let e = exp(bhf_cmb_w[j * hc + k] - mx);
                bhf_cmb_w[j * hc + k] = e;
                sum = sum + e;
            }
            for (var k = 0u; k < hc; k = k + 1u) {
                bhf_cmb_w[j * hc + k] = bhf_cmb_w[j * hc + k] / sum + bhf_p.eps;
            }
        }
        for (var k = 0u; k < hc; k = k + 1u) {
            var sum = 0.0;
            for (var j = 0u; j < hc; j = j + 1u) { sum = sum + bhf_cmb_w[j * hc + k]; }
            for (var j = 0u; j < hc; j = j + 1u) {
                bhf_cmb_w[j * hc + k] = bhf_cmb_w[j * hc + k] / (sum + bhf_p.eps);
            }
        }
        for (var it = 1u; it < bhf_p.iters; it = it + 1u) {
            for (var j = 0u; j < hc; j = j + 1u) {
                var sum = 0.0;
                for (var k = 0u; k < hc; k = k + 1u) { sum = sum + bhf_cmb_w[j * hc + k]; }
                for (var k = 0u; k < hc; k = k + 1u) {
                    bhf_cmb_w[j * hc + k] = bhf_cmb_w[j * hc + k] / (sum + bhf_p.eps);
                }
            }
            for (var k = 0u; k < hc; k = k + 1u) {
                var sum = 0.0;
                for (var j = 0u; j < hc; j = j + 1u) { sum = sum + bhf_cmb_w[j * hc + k]; }
                for (var j = 0u; j < hc; j = j + 1u) {
                    bhf_cmb_w[j * hc + k] = bhf_cmb_w[j * hc + k] / (sum + bhf_p.eps);
                }
            }
        }
        for (var j = 0u; j < hc * hc; j = j + 1u) { bhf_comb[t * hc * hc + j] = bhf_cmb_w[j]; }
    }
    workgroupBarrier();
    var acc3 = 0.0;
    var d = lid;
    loop {
        if (d >= dim) { break; }
        var y = 0.0;
        for (var j = 0u; j < hc; j = j + 1u) {
            y = y + bhf_pre_w[j] * bhf_state[sb + j * dim + d];
        }
        bhf_fold[t * dim + d] = y;
        acc3 = acc3 + y * y;
        d = d + 1024u;
    }
    if (bhf_p.nrm == 0u) { return; }
    bhf_red[lid] = acc3;
    workgroupBarrier();
    var st2 = 512u;
    loop {
        if (st2 == 0u) { break; }
        if (lid < st2) { bhf_red[lid] = bhf_red[lid] + bhf_red[lid + st2]; }
        workgroupBarrier();
        st2 = st2 >> 1u;
    }
    let inv = inverseSqrt(bhf_red[0] / f32(dim) + bhf_p.eps);
    var d2 = lid;
    loop {
        if (d2 >= dim) { break; }
        bhf_out[t * dim + d2] = bhf_fold[t * dim + d2] * inv * bhf_nw[d2];
        d2 = d2 + 1024u;
    }
}

// hc_post_expand with grid (ceil(hc*dim/256), token).
@group(0) @binding(0) var<storage, read>       bhe_x    : array<f32>;   // b, dim
@group(0) @binding(1) var<storage, read>       bhe_res  : array<f32>;   // b, hc*dim
@group(0) @binding(2) var<storage, read>       bhe_post : array<f32>;   // b, hc
@group(0) @binding(3) var<storage, read>       bhe_comb : array<f32>;   // b, hc*hc
@group(0) @binding(4) var<storage, read_write> bhe_out  : array<f32>;   // b, hc*dim
@group(0) @binding(5) var<uniform>             bhe_p    : HcP;
@compute @workgroup_size(256)
fn bt_hc_post_expand(@builtin(workgroup_id) wid: vec3<u32>,
                     @builtin(local_invocation_id) lid: vec3<u32>) {
    let hc = bhe_p.hc;
    let dim = bhe_p.dim;
    let i = wid.x * 256u + lid.x;
    if (i >= hc * dim) { return; }
    let t = wid.y;
    let j = i / dim;
    let d = i % dim;
    var y = bhe_post[t * hc + j] * bhe_x[t * dim + d];
    for (var k = 0u; k < hc; k = k + 1u) {
        y = y + bhe_comb[t * hc * hc + k * hc + j] * bhe_res[t * hc * dim + k * dim + d];
    }
    bhe_out[t * hc * dim + i] = y;
}

// f32_matvec_w with grid (row, token): the weight is read for the whole
// batch, x and y stride by the token.
@group(0) @binding(0) var<storage, read>       bfw_w : array<f32>;
@group(0) @binding(1) var<storage, read>       bfw_x : array<f32>;   // b, cols
@group(0) @binding(2) var<storage, read_write> bfw_y : array<f32>;   // b, rows
@group(0) @binding(3) var<uniform>             bfw_p : F32WP;
var<workgroup> bfw_part: array<f32, 256>;
@compute @workgroup_size(256)
fn bt_f32_matvec_w(@builtin(workgroup_id) wid: vec3<u32>,
                   @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    if (row >= bfw_p.rows) { return; }
    let t = wid.y;
    let base = row * bfw_p.cols;
    let xb = t * bfw_p.cols;
    var acc = 0.0;
    var i = lid;
    loop {
        if (i >= bfw_p.cols) { break; }
        acc = acc + bfw_w[base + i] * bfw_x[xb + i];
        i = i + 256u;
    }
    bfw_part[lid] = acc;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { bfw_part[lid] = bfw_part[lid] + bfw_part[lid + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    if (lid == 0u) { bfw_y[t * bfw_p.rows + row] = bfw_part[0]; }
}

// f32_matvec_x with grid (row, token): the 1024-thread tree of the walk's
// few-row path, unchanged — the batch must reduce in the SAME order or a
// near-tied indexer top-k flips and the verified text is not the walked one.
@group(0) @binding(0) var<storage, read>       bfx_w : array<f32>;
@group(0) @binding(1) var<storage, read>       bfx_x : array<f32>;   // b, cols
@group(0) @binding(2) var<storage, read_write> bfx_y : array<f32>;   // b, rows
@group(0) @binding(3) var<uniform>             bfx_p : F32WP;
var<workgroup> bfx_part: array<f32, 1024>;
@compute @workgroup_size(1024)
fn bt_f32_matvec_x(@builtin(workgroup_id) wid: vec3<u32>,
                   @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    if (row >= bfx_p.rows) { return; }
    let t = wid.y;
    let base = row * bfx_p.cols;
    let xb = t * bfx_p.cols;
    var acc = 0.0;
    var i = lid;
    loop {
        if (i >= bfx_p.cols) { break; }
        acc = acc + bfx_w[base + i] * bfx_x[xb + i];
        i = i + 1024u;
    }
    bfx_part[lid] = acc;
    workgroupBarrier();
    var stride = 512u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { bfx_part[lid] = bfx_part[lid] + bfx_part[lid + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    if (lid == 0u) { bfx_y[t * bfx_p.rows + row] = bfx_part[0]; }
}

// moe_route with grid (token): logits, winners, weights, counts, forced rows
// and cold mirrors all stride by the token; the bias, mask and slot map are
// the layer's and shared.
@group(0) @binding(0) var<storage, read>       bt_s      : array<f32>;   // b, n
@group(0) @binding(1) var<storage, read>       bt_bias   : array<f32>;   // n
@group(0) @binding(2) var<storage, read>       bt_mask   : array<u32>;   // n
@group(0) @binding(3) var<storage, read>       bt_forced : array<u32>;   // b, top_k
@group(0) @binding(4) var<storage, read_write> bt_idx    : array<u32>;   // b, top_k+1
@group(0) @binding(5) var<storage, read_write> bt_w      : array<f32>;   // b, top_k+1
@group(0) @binding(6) var<storage, read_write> bt_cnt    : array<u32>;   // b
@group(0) @binding(7) var<uniform>             bt_p      : RtP;
@group(0) @binding(8) var<storage, read>       bt_map    : array<u32>;   // n
@group(0) @binding(9) var<storage, read_write> bt_cold   : array<u32>;   // b, 4*top_k
var<workgroup> btr_sc:   array<f32, 1024>;
var<workgroup> btr_sh:   array<f32, 1024>;
var<workgroup> btr_used: array<u32, 64>;
@compute @workgroup_size(1024)
fn bt_moe_route(@builtin(workgroup_id) wid: vec3<u32>,
                @builtin(local_invocation_index) lid: u32) {
    let n = bt_p.n;
    let k = bt_p.top_k;
    let t = wid.x;
    let lb = t * n;
    let ob = t * (k + 1u);
    let cb = t * 4u * k;
    let has_bias = (bt_p.flags & 1u) != 0u;
    let has_mask = (bt_p.flags & 2u) != 0u;
    let forced = (bt_p.flags & 4u) != 0u;
    let pin_shared = (bt_p.flags & 8u) != 0u;
    let subset = (bt_p.flags & 16u) != 0u;
    if (lid < k) {
        btr_used[lid] = 0u;
        bt_idx[ob + lid] = 0u;
        bt_w[ob + lid] = 0.0;
        bt_cold[cb + 2u * lid] = 0xFFFFFFFFu;
        bt_cold[cb + 2u * lid + 1u] = 0u;
        bt_cold[cb + 2u * k + 2u * lid] = 0xFFFFFFFFu;
        bt_cold[cb + 2u * k + 2u * lid + 1u] = 0u;
    }
    let shared_slot = bt_p.flags >> 8u;
    if (pin_shared && lid == 0u) {
        bt_idx[ob + k] = shared_slot;
        bt_w[ob + k] = 1.0;
    }
    storageBarrier();
    var i = lid;
    loop {
        if (i >= n) { break; }
        let v = bt_s[lb + i];
        var sp = v;
        if (v <= 20.0) { sp = log(1.0 + exp(v)); }
        let sc = sqrt(sp);
        btr_sc[i] = sc;
        var sh = sc;
        if (has_bias) { sh = sh + bt_bias[i]; }
        if (has_mask && bt_mask[i] == 0u) { sh = KP_NINF; }
        btr_sh[i] = sh;
        i = i + 1024u;
    }
    workgroupBarrier();
    if (forced) {
        if (lid < k) {
            let e = bt_forced[t * k + lid];
            var w = 0.0;
            if (e < n) { w = btr_sc[e]; }
            if (subset && e < n) {
                let slot = bt_map[e];
                if (slot == 0xFFFFFFFFu) {
                    bt_idx[ob + lid] = 0u;
                    bt_w[ob + lid] = 0.0;
                    bt_cold[cb + 2u * lid] = e;
                    bt_cold[cb + 2u * lid + 1u] = bitcast<u32>(w);
                } else {
                    bt_idx[ob + lid] = slot;
                    bt_w[ob + lid] = w;
                }
            } else {
                bt_idx[ob + lid] = e;
                bt_w[ob + lid] = w;
            }
            btr_used[lid] = 1u;
        }
    } else {
        var m = lid;
        loop {
            if (m >= n) { break; }
            let si = btr_sh[m];
            if (si > KP_NINF) {
                var rank = 0u;
                for (var j = 0u; j < n; j = j + 1u) {
                    let sj = btr_sh[j];
                    if (sj > KP_NINF) {
                        if (sj > si || (sj == si && j < m)) { rank = rank + 1u; }
                    }
                }
                if (rank < k) {
                    btr_used[rank] = 1u;
                    bt_cold[cb + 2u * k + 2u * rank] = m;
                    bt_cold[cb + 2u * k + 2u * rank + 1u] = bitcast<u32>(btr_sh[m]);
                    if (subset) {
                        let slot = bt_map[m];
                        if (slot == 0xFFFFFFFFu) {
                            bt_idx[ob + rank] = 0u;
                            bt_w[ob + rank] = 0.0;
                            bt_cold[cb + 2u * rank] = m;
                            bt_cold[cb + 2u * rank + 1u] = bitcast<u32>(btr_sc[m]);
                        } else {
                            bt_idx[ob + rank] = slot;
                            bt_w[ob + rank] = btr_sc[m];
                        }
                    } else {
                        bt_idx[ob + rank] = m;
                        bt_w[ob + rank] = btr_sc[m];
                    }
                }
            }
            m = m + 1024u;
        }
    }
    workgroupBarrier();
    storageBarrier();
    if (lid == 0u) {
        var cnt = 0u;
        for (var j = 0u; j < k; j = j + 1u) {
            if (btr_used[j] == 1u) { cnt = cnt + 1u; }
        }
        bt_cnt[t] = cnt;
        var sum = 0.0;
        for (var j = 0u; j < cnt; j = j + 1u) {
            sum = sum + bt_w[ob + j];
            if (bt_cold[cb + 2u * j] != 0xFFFFFFFFu) {
                sum = sum + bitcast<f32>(bt_cold[cb + 2u * j + 1u]);
            }
        }
        if (sum > 0.0) {
            let inv = bt_p.scale / sum;
            for (var j = 0u; j < cnt; j = j + 1u) {
                bt_w[ob + j] = bt_w[ob + j] * inv;
                if (bt_cold[cb + 2u * j] != 0xFFFFFFFFu) {
                    bt_cold[cb + 2u * j + 1u] =
                        bitcast<u32>(bitcast<f32>(bt_cold[cb + 2u * j + 1u]) * inv);
                }
            }
        }
    }
}

// moe_gate_up_q2tp with grid (row, slot, token).
var<workgroup> btg_pg: array<f32, 64>;
var<workgroup> btg_pu: array<f32, 64>;
@compute @workgroup_size(64)
fn bt_moe_gate_up_q2tp(@builtin(workgroup_id) wid: vec3<u32>,
                       @builtin(local_invocation_index) lid: u32) {
    let row = wid.x;
    let slot = wid.y;
    let t = wid.z;
    let gpr = mg_p.gpr;
    let rows = mg_p.inter;
    let base16 = mg_sel[t * mg_p.slots + slot] * mg_p.mat16;
    let nib16 = base16 + row * gpr * 4u;
    let par16 = base16 + rows * gpr * 4u + row * 2u;
    let cst = (gpr * 5u + 7u) / 8u;
    let cod8 = (base16 + rows * gpr * 4u + rows * 2u) * 2u + row * cst;
    let xb4 = t * gpr * 8u;
    let gl = unpack2x16float(mg_g16(par16) | (mg_g16(par16 + 1u) << 16u));
    let ul = unpack2x16float(mg_u16f(par16) | (mg_u16f(par16 + 1u) << 16u));
    var ag = 0.0;
    var au = 0.0;
    for (var g = lid; g < gpr; g = g + 64u) {
        let bit = g * 5u;
        let cb = bit >> 3u;
        let shf = bit & 7u;
        var cg = mgp_gu8(cod8 + cb);
        var cu = mgp_uu8(cod8 + cb);
        if (shf > 3u) {
            cg = cg | (mgp_gu8(cod8 + cb + 1u) << 8u);
            cu = cu | (mgp_uu8(cod8 + cb + 1u) << 8u);
        }
        let cgv = (cg >> shf) & 31u;
        let cuv = (cu >> shf) & 31u;
        let sg = select(exp2(gl.x + f32(max(cgv, 1u) - 1u) * gl.y), 0.0, cgv == 0u);
        let su = select(exp2(ul.x + f32(max(cuv, 1u) - 1u) * ul.y), 0.0, cuv == 0u);
        let w32 = (nib16 + g * 4u) >> 1u;
        let xq = xb4 + g * 8u;
        let x0 = mg_xv[xq];      let x1 = mg_xv[xq + 1u];
        let x2 = mg_xv[xq + 2u]; let x3 = mg_xv[xq + 3u];
        let x4 = mg_xv[xq + 4u]; let x5 = mg_xv[xq + 5u];
        let x6 = mg_xv[xq + 6u]; let x7 = mg_xv[xq + 7u];
        let dg = mg_dot16v(mg_gw[w32], x0, x1, x2, x3)
               + mg_dot16v(mg_gw[w32 + 1u], x4, x5, x6, x7);
        let du = mg_dot16v(mg_uw[w32], x0, x1, x2, x3)
               + mg_dot16v(mg_uw[w32 + 1u], x4, x5, x6, x7);
        ag = ag + sg * dg;
        au = au + su * du;
    }
    btg_pg[lid] = ag;
    btg_pu[lid] = au;
    workgroupBarrier();
    var stride = 32u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) {
            btg_pg[lid] = btg_pg[lid] + btg_pg[lid + stride];
            btg_pu[lid] = btg_pu[lid] + btg_pu[lid + stride];
        }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) {
        let g = btg_pg[0];
        var gg = g;
        var uu = btg_pu[0];
        if (mg_p.lim > 0.0) {
            uu = clamp(uu, -mg_p.lim, mg_p.lim);
            gg = min(gg, mg_p.lim);
        }
        mg_act[(t * mg_p.slots + slot) * mg_p.inter + row] = (gg / (1.0 + exp(-gg))) * uu;
    }
}

// Four rows of the SwiGLU inputs to one 64-thread workgroup: the x spans
// load once per group and feed all four rows' gate AND up tiles. Each
// row's group order, accumulation and tree are the one-row kernel's, so
// the activations are bit-identical.
var<workgroup> btg4_pg: array<f32, 64>;
var<workgroup> btg4_pu: array<f32, 64>;
@compute @workgroup_size(64)
fn bt_moe_gate_up_q2tp_r4(@builtin(workgroup_id) wid: vec3<u32>,
                          @builtin(local_invocation_index) lid: u32) {
    let row0 = wid.x * 4u;
    let slot = wid.y;
    let t = wid.z;
    let gpr = mg_p.gpr;
    let rows = mg_p.inter;
    let base16 = mg_sel[t * mg_p.slots + slot] * mg_p.mat16;
    let cst = (gpr * 5u + 7u) / 8u;
    let cod0 = (base16 + rows * gpr * 4u + rows * 2u) * 2u;
    let xb4 = t * gpr * 8u;
    var ag0 = 0.0; var au0 = 0.0;
    var ag1 = 0.0; var au1 = 0.0;
    var ag2 = 0.0; var au2 = 0.0;
    var ag3 = 0.0; var au3 = 0.0;
    for (var g = lid; g < gpr; g = g + 64u) {
        let bit = g * 5u;
        let cb0 = bit >> 3u;
        let shf = bit & 7u;
        let xq = xb4 + g * 8u;
        let x0 = mg_xv[xq];      let x1 = mg_xv[xq + 1u];
        let x2 = mg_xv[xq + 2u]; let x3 = mg_xv[xq + 3u];
        let x4 = mg_xv[xq + 4u]; let x5 = mg_xv[xq + 5u];
        let x6 = mg_xv[xq + 6u]; let x7 = mg_xv[xq + 7u];
        for (var r = 0u; r < 4u; r = r + 1u) {
            let row = row0 + r;
            if (row >= rows) { break; }
            let par16 = base16 + rows * gpr * 4u + row * 2u;
            let gl = unpack2x16float(mg_g16(par16) | (mg_g16(par16 + 1u) << 16u));
            let ul = unpack2x16float(mg_u16f(par16) | (mg_u16f(par16 + 1u) << 16u));
            let cod8 = cod0 + row * cst;
            var cg = mgp_gu8(cod8 + cb0);
            var cu = mgp_uu8(cod8 + cb0);
            if (shf > 3u) {
                cg = cg | (mgp_gu8(cod8 + cb0 + 1u) << 8u);
                cu = cu | (mgp_uu8(cod8 + cb0 + 1u) << 8u);
            }
            let cgv = (cg >> shf) & 31u;
            let cuv = (cu >> shf) & 31u;
            let sg = select(exp2(gl.x + f32(max(cgv, 1u) - 1u) * gl.y), 0.0, cgv == 0u);
            let su = select(exp2(ul.x + f32(max(cuv, 1u) - 1u) * ul.y), 0.0, cuv == 0u);
            let nib16 = base16 + row * gpr * 4u;
            let w32 = (nib16 + g * 4u) >> 1u;
            let dg = mg_dot16v(mg_gw[w32], x0, x1, x2, x3)
                   + mg_dot16v(mg_gw[w32 + 1u], x4, x5, x6, x7);
            let du = mg_dot16v(mg_uw[w32], x0, x1, x2, x3)
                   + mg_dot16v(mg_uw[w32 + 1u], x4, x5, x6, x7);
            if (r == 0u) { ag0 = ag0 + sg * dg; au0 = au0 + su * du; }
            if (r == 1u) { ag1 = ag1 + sg * dg; au1 = au1 + su * du; }
            if (r == 2u) { ag2 = ag2 + sg * dg; au2 = au2 + su * du; }
            if (r == 3u) { ag3 = ag3 + sg * dg; au3 = au3 + su * du; }
        }
    }
    for (var r = 0u; r < 4u; r = r + 1u) {
        var ag = ag0; var au = au0;
        if (r == 1u) { ag = ag1; au = au1; }
        if (r == 2u) { ag = ag2; au = au2; }
        if (r == 3u) { ag = ag3; au = au3; }
        btg4_pg[lid] = ag;
        btg4_pu[lid] = au;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) {
                btg4_pg[lid] = btg4_pg[lid] + btg4_pg[lid + stride];
                btg4_pu[lid] = btg4_pu[lid] + btg4_pu[lid + stride];
            }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u && row0 + r < rows) {
            var gg = btg4_pg[0];
            var uu = btg4_pu[0];
            if (mg_p.lim > 0.0) {
                uu = clamp(uu, -mg_p.lim, mg_p.lim);
                gg = min(gg, mg_p.lim);
            }
            mg_act[(t * mg_p.slots + slot) * mg_p.inter + row0 + r] =
                (gg / (1.0 + exp(-gg))) * uu;
        }
        workgroupBarrier();
    }
}

// sparse_attend with grid (head, token): q and out stride by nh*hd, the
// index list by `m` (the uniform carries the per-token STRIDE of the list
// buffer), the attended count comes from a per-token table. The KV cache is
// the layer's and shared — earlier tokens' appends are ordered by the pass.
@group(0) @binding(0) var<storage, read>       bsa_q    : array<f32>;   // b, nh*hd
@group(0) @binding(1) var<storage, read>       bsa_kv   : array<f32>;
@group(0) @binding(2) var<storage, read>       bsa_idx  : array<u32>;   // b, stride
@group(0) @binding(3) var<storage, read>       bsa_sink : array<f32>;   // nh
@group(0) @binding(4) var<storage, read_write> bsa_out  : array<f32>;   // b, nh*hd
@group(0) @binding(5) var<uniform>             bsa_p    : SaP;          // m = idx stride
@group(0) @binding(6) var<storage, read>       bsa_m    : array<u32>;   // b
var<workgroup> bsa_red: array<f32, 256>;
var<workgroup> bsa_w: array<f32, 1024>;
var<workgroup> bsa_den: f32;
@compute @workgroup_size(256)
fn bt_sparse_attend(@builtin(workgroup_id) wid: vec3<u32>,
                    @builtin(local_invocation_index) lid: u32) {
    let h = wid.x;
    if (h >= bsa_p.nh) { return; }
    let t = wid.y;
    let hd = bsa_p.hd;
    let m = bsa_m[t];
    let ib = t * bsa_p.m;
    let qb = t * bsa_p.nh * hd + h * hd;
    var mx = bsa_sink[h];
    var i = lid;
    loop {
        if (i >= m) { break; }
        let p = bsa_idx[ib + i];
        var d = 0.0;
        for (var k = 0u; k < hd; k = k + 1u) {
            d = d + bsa_q[qb + k] * bsa_kv[p * hd + k];
        }
        let sc = d * bsa_p.scale;
        bsa_w[i] = sc;
        mx = max(mx, sc);
        i = i + 256u;
    }
    bsa_red[lid] = mx;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { bsa_red[lid] = max(bsa_red[lid], bsa_red[lid + stride]); }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    let mval = bsa_red[0];
    workgroupBarrier();
    var den = 0.0;
    i = lid;
    loop {
        if (i >= m) { break; }
        let w = exp(bsa_w[i] - mval);
        bsa_w[i] = w;
        den = den + w;
        i = i + 256u;
    }
    bsa_red[lid] = den;
    workgroupBarrier();
    stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { bsa_red[lid] = bsa_red[lid] + bsa_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { bsa_den = bsa_red[0] + exp(bsa_sink[h] - mval); }
    workgroupBarrier();
    let inv = 1.0 / bsa_den;
    var k = lid;
    loop {
        if (k >= hd) { break; }
        var acc = 0.0;
        for (var j = 0u; j < m; j = j + 1u) {
            acc = acc + bsa_w[j] * bsa_kv[bsa_idx[ib + j] * hd + k];
        }
        bsa_out[qb + k] = acc * inv;
        k = k + 256u;
    }
}


// The whole hyper-connection join, fused, with the token on the grid: the
// four dependent dispatches per half become ONE link of the pass's critical
// path per half. The B=1 trade was negative (measured); at B=5 each link
// carries five tokens and the launch bookkeeping amortizes.
@group(0) @binding(0)  var<storage, read>       bhb_x     : array<f32>;   // b, dim
@group(0) @binding(1)  var<storage, read>       bhb_res   : array<f32>;   // b, hc*dim
@group(0) @binding(2)  var<storage, read_write> bhb_post  : array<f32>;   // b, hc
@group(0) @binding(3)  var<storage, read_write> bhb_comb  : array<f32>;   // b, hc*hc
@group(0) @binding(4)  var<storage, read>       bhb_mixw  : array<f32>;
@group(0) @binding(5)  var<storage, read>       bhb_sc    : array<f32>;
@group(0) @binding(6)  var<storage, read>       bhb_base  : array<f32>;
@group(0) @binding(7)  var<storage, read>       bhb_nw    : array<f32>;
@group(0) @binding(8)  var<storage, read_write> bhb_state : array<f32>;   // b, hc*dim
@group(0) @binding(9)  var<storage, read_write> bhb_fold  : array<f32>;   // b, dim
@group(0) @binding(10) var<storage, read_write> bhb_norm  : array<f32>;   // b, dim
@group(0) @binding(11) var<uniform>             bhb_p     : HbP;
var<workgroup> bhb_red: array<f32, 256>;
var<workgroup> bhb_mix: array<f32, 64>;
var<workgroup> bhb_pre: array<f32, 8>;
var<workgroup> bhb_cmb: array<f32, 64>;
var<workgroup> bhb_rsq: f32;
@compute @workgroup_size(256)
fn bt_hc_block(@builtin(workgroup_id) wid: vec3<u32>,
               @builtin(local_invocation_index) lid: u32) {
    let tok = wid.x;
    let xb = tok * bhb_p.dim;
    let rb = tok * bhb_p.hc * bhb_p.dim;
    let sb = rb;
    let fb = xb;
    let pb = tok * bhb_p.hc;
    let cb = tok * bhb_p.hc * bhb_p.hc;

    let hc = bhb_p.hc;
    let dim = bhb_p.dim;
    let n = hc * dim;
    let mh = bhb_p.mix_hc;

    // ── 1. expand ─────────────────────────────────────────────────────────
    var i = lid;
    loop {
        if (i >= n) { break; }
        let j = i / dim;
        let d = i % dim;
        var y = bhb_post[pb + j] * bhb_x[xb + d];
        for (var k = 0u; k < hc; k = k + 1u) {
            y = y + bhb_comb[cb + k * hc + j] * bhb_res[rb + k * dim + d];
        }
        bhb_state[sb + i] = y;
        i = i + 256u;
    }
    workgroupBarrier();

    // ── 2. the mix projection, all outputs at once ────────────────────────
    // Eight threads to an output, striding the shared axis: one barrier for
    // the lot instead of one tree reduction per output.
    let o = lid / HB_LANES;
    let sub = lid % HB_LANES;
    var acc = 0.0;
    if (o < mh) {
        let base = o * n;
        var t = sub;
        loop {
            if (t >= n) { break; }
            acc = acc + bhb_mixw[base + t] * bhb_state[sb + t];
            t = t + HB_LANES;
        }
    }
    bhb_red[lid] = acc;
    workgroupBarrier();
    if (sub == 0u && o < mh) {
        var sm = 0.0;
        for (var t = 0u; t < HB_LANES; t = t + 1u) { sm = sm + bhb_red[o * HB_LANES + t]; }
        bhb_mix[o] = sm;
    }

    // ── 3. rsqrt(mean(state^2) + eps), over ALL copies ────────────────────
    var acc2 = 0.0;
    var i2 = lid;
    loop {
        if (i2 >= n) { break; }
        let v = bhb_state[sb + i2];
        acc2 = acc2 + v * v;
        i2 = i2 + 256u;
    }
    workgroupBarrier();           // bhb_red is being reused
    bhb_red[lid] = acc2;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { bhb_red[lid] = bhb_red[lid] + bhb_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { bhb_rsq = inverseSqrt(bhb_red[0] / f32(n) + bhb_p.eps); }
    workgroupBarrier();
    let rsq = bhb_rsq;

    // ── 4. pre / post / comb, thread 0: hc is 4 and the Sinkhorn is a
    //       sequential fixed point over a 4x4.
    if (lid == 0u) {
        for (var j = 0u; j < hc; j = j + 1u) {
            let m = bhb_mix[j] * rsq * bhb_sc[0] + bhb_base[j];
            bhb_pre[j] = 1.0 / (1.0 + exp(-m)) + bhb_p.eps;
            let m2 = bhb_mix[hc + j] * rsq * bhb_sc[1] + bhb_base[hc + j];
            bhb_post[pb + j] = 2.0 / (1.0 + exp(-m2));
        }
        for (var j = 0u; j < hc; j = j + 1u) {
            var mx = -1e30;
            for (var k = 0u; k < hc; k = k + 1u) {
                let v = bhb_mix[2u * hc + j * hc + k] * rsq * bhb_sc[2]
                      + bhb_base[2u * hc + j * hc + k];
                bhb_cmb[j * hc + k] = v;
                mx = max(mx, v);
            }
            var sum = 0.0;
            for (var k = 0u; k < hc; k = k + 1u) {
                let e = exp(bhb_cmb[j * hc + k] - mx);
                bhb_cmb[j * hc + k] = e;
                sum = sum + e;
            }
            for (var k = 0u; k < hc; k = k + 1u) {
                bhb_cmb[j * hc + k] = bhb_cmb[j * hc + k] / sum + bhb_p.eps;
            }
        }
        for (var k = 0u; k < hc; k = k + 1u) {
            var sum = 0.0;
            for (var j = 0u; j < hc; j = j + 1u) { sum = sum + bhb_cmb[j * hc + k]; }
            for (var j = 0u; j < hc; j = j + 1u) {
                bhb_cmb[j * hc + k] = bhb_cmb[j * hc + k] / (sum + bhb_p.eps);
            }
        }
        for (var it = 1u; it < bhb_p.iters; it = it + 1u) {
            for (var j = 0u; j < hc; j = j + 1u) {
                var sum = 0.0;
                for (var k = 0u; k < hc; k = k + 1u) { sum = sum + bhb_cmb[j * hc + k]; }
                for (var k = 0u; k < hc; k = k + 1u) {
                    bhb_cmb[j * hc + k] = bhb_cmb[j * hc + k] / (sum + bhb_p.eps);
                }
            }
            for (var k = 0u; k < hc; k = k + 1u) {
                var sum = 0.0;
                for (var j = 0u; j < hc; j = j + 1u) { sum = sum + bhb_cmb[j * hc + k]; }
                for (var j = 0u; j < hc; j = j + 1u) {
                    bhb_cmb[j * hc + k] = bhb_cmb[j * hc + k] / (sum + bhb_p.eps);
                }
            }
        }
        for (var j = 0u; j < hc * hc; j = j + 1u) { bhb_comb[cb + j] = bhb_cmb[j]; }
    }
    workgroupBarrier();

    // ── 5. fold, then the norm over it ────────────────────────────────────
    var acc3 = 0.0;
    var d = lid;
    loop {
        if (d >= dim) { break; }
        var y = 0.0;
        for (var j = 0u; j < hc; j = j + 1u) {
            y = y + bhb_pre[j] * bhb_state[sb + j * dim + d];
        }
        bhb_fold[fb + d] = y;
        acc3 = acc3 + y * y;
        d = d + 256u;
    }
    bhb_red[lid] = acc3;
    workgroupBarrier();
    stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { bhb_red[lid] = bhb_red[lid] + bhb_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    let inv = inverseSqrt(bhb_red[0] / f32(dim) + bhb_p.eps);
    var d2 = lid;
    loop {
        if (d2 >= dim) { break; }
        bhb_norm[fb + d2] = bhb_fold[fb + d2] * inv * bhb_nw[d2];
        d2 = d2 + 256u;
    }
}


// The compressor's pending append for a RUN of tokens, one dispatch: every
// token in a fold-free segment writes a distinct slot of the pending
// stream, so the writes commute. The score picks up its in-window position
// bias here, exactly where the per-token step added it.
struct BcaP { width: u32, t0: u32, n: u32, flags: u32 }; // flags: 1 = overlap (add ape), bits 8.. = pos0 % ratio? no — slot base rides in t0's precomputed slots
@group(0) @binding(0) var<storage, read>       bca_ckv : array<f32>;   // b, width
@group(0) @binding(1) var<storage, read>       bca_csc : array<f32>;   // b, width
@group(0) @binding(2) var<storage, read>       bca_ape : array<f32>;
@group(0) @binding(3) var<storage, read_write> bca_pkv : array<f32>;   // ratio, width
@group(0) @binding(4) var<storage, read_write> bca_psc : array<f32>;
@group(0) @binding(5) var<uniform>             bca_p   : BcaP;
@group(0) @binding(6) var<storage, read>       bca_slot: array<u32>;   // b (slot per token)
@compute @workgroup_size(256)
fn bt_comp_append(@builtin(workgroup_id) wid: vec3<u32>,
                  @builtin(local_invocation_id) lid: vec3<u32>) {
    let i = wid.x * 256u + lid.x;
    if (i >= bca_p.width) { return; }
    let t = bca_p.t0 + wid.y;
    if (wid.y >= bca_p.n) { return; }
    let slot = bca_slot[t];
    let src = t * bca_p.width + i;
    bca_pkv[slot * bca_p.width + i] = bca_ckv[src];
    var sc = bca_csc[src];
    if ((bca_p.flags & 1u) != 0u) {
        sc = sc + bca_ape[slot * bca_p.width + i];
    }
    bca_psc[slot * bca_p.width + i] = sc;
}

// The whole fold-token compressor step as ONE dispatch: append the
// closing token's row, softmax-pool the window (kv_pool's expressions,
// element for element), norm through rmsnorm's exact 1024-thread tree,
// rotate the rope tail, land the entry in the cache and shift pending
// into previous. The per-token path spent seven dependent dispatches on
// this; the math here is the same operations in the same order, so the
// bits are too.
struct BcfP {
    width: u32, ratio: u32, rd: u32, flags: u32,
    dst_off: u32, pos_bits: u32, eps_bits: u32, trow: u32,
};
@group(0) @binding(0) var<storage, read>       bcf_ckv : array<f32>;
@group(0) @binding(1) var<storage, read>       bcf_csc : array<f32>;
@group(0) @binding(2) var<storage, read>       bcf_ape : array<f32>;
@group(0) @binding(3) var<storage, read_write> bcf_pkv : array<f32>;
@group(0) @binding(4) var<storage, read_write> bcf_psc : array<f32>;
@group(0) @binding(5) var<storage, read_write> bcf_qkv : array<f32>;
@group(0) @binding(6) var<storage, read_write> bcf_qsc : array<f32>;
@group(0) @binding(7) var<storage, read>       bcf_nw  : array<f32>;
@group(0) @binding(8) var<storage, read>       bcf_fr  : array<f32>;
@group(0) @binding(9) var<storage, read_write> bcf_dst : array<f32>;
@group(0) @binding(10) var<storage, read>      bcf_p   : BcfP;
var<workgroup> bcf_fold: array<f32, 512>;
var<workgroup> bcf_part: array<f32, 1024>;
@compute @workgroup_size(1024)
fn bt_comp_fold(@builtin(local_invocation_index) lid: u32) {
    let w = bcf_p.width;
    let r = bcf_p.ratio;
    let overlap = (bcf_p.flags & 1u) != 0u;
    let have_prev = (bcf_p.flags & 2u) != 0u;
    let ew = select(w, w / 2u, overlap);
    let slot = r - 1u;
    let trow = bcf_p.trow;
    // ── append the closing token ──
    var i = lid;
    loop {
        if (i >= w) { break; }
        bcf_pkv[slot * w + i] = bcf_ckv[trow * w + i];
        var sc = bcf_csc[trow * w + i];
        if (overlap) { sc = sc + bcf_ape[slot * w + i]; }
        bcf_psc[slot * w + i] = sc;
        i = i + 1024u;
    }
    storageBarrier();
    workgroupBarrier();
    // ── pool: kv_pool per element, w there is ew here ──
    let slots = select(r, 2u * r, overlap);
    var d = lid;
    loop {
        if (d >= ew) { break; }
        var mx = KP_NINF;
        for (var t = 0u; t < slots; t = t + 1u) {
            var sc = KP_NINF;
            if (overlap) {
                if (t < r) {
                    if (have_prev) { sc = bcf_qsc[t * 2u * ew + d]; }
                } else {
                    sc = bcf_psc[(t - r) * 2u * ew + ew + d];
                }
            } else {
                sc = bcf_psc[t * ew + d] + bcf_ape[t * ew + d];
            }
            mx = max(mx, sc);
        }
        var outv = 0.0;
        if (mx > KP_NINF) {
            var den = 0.0;
            var acc = 0.0;
            for (var t = 0u; t < slots; t = t + 1u) {
                var sc = KP_NINF;
                var kv = 0.0;
                if (overlap) {
                    if (t < r) {
                        if (have_prev) {
                            sc = bcf_qsc[t * 2u * ew + d];
                            kv = bcf_qkv[t * 2u * ew + d];
                        }
                    } else {
                        sc = bcf_psc[(t - r) * 2u * ew + ew + d];
                        kv = bcf_pkv[(t - r) * 2u * ew + ew + d];
                    }
                } else {
                    sc = bcf_psc[t * ew + d] + bcf_ape[t * ew + d];
                    kv = bcf_pkv[t * ew + d];
                }
                if (sc > KP_NINF) {
                    let e = exp(sc - mx);
                    den = den + e;
                    acc = acc + e * kv;
                }
            }
            if (den > 0.0) { outv = acc / den; }
        }
        bcf_fold[d] = outv;
        d = d + 1024u;
    }
    workgroupBarrier();
    // ── rmsnorm, the 1024-thread kernel's exact tree ──
    var acc2 = 0.0;
    var j = lid;
    loop {
        if (j >= ew) { break; }
        let v = bcf_fold[j];
        acc2 = acc2 + v * v;
        j = j + 1024u;
    }
    bcf_part[lid] = acc2;
    workgroupBarrier();
    var stride = 512u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { bcf_part[lid] = bcf_part[lid] + bcf_part[lid + stride]; }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let inv = inverseSqrt(bcf_part[0] / f32(ew) + bitcast<f32>(bcf_p.eps_bits));
    workgroupBarrier();
    var k2 = lid;
    loop {
        if (k2 >= ew) { break; }
        bcf_fold[k2] = bcf_fold[k2] * inv * bcf_nw[k2];
        k2 = k2 + 1024u;
    }
    workgroupBarrier();
    // ── rope tail, adjacent pairs at the window's FIRST position ──
    let rd = bcf_p.rd;
    let rbase = ew - rd;
    let pos = bitcast<f32>(bcf_p.pos_bits);
    var t2 = lid;
    loop {
        if (t2 >= rd / 2u) { break; }
        let th = pos * bcf_fr[t2];
        let sn = sin(th);
        let cs = cos(th);
        let a = bcf_fold[rbase + 2u * t2];
        let cc = bcf_fold[rbase + 2u * t2 + 1u];
        bcf_fold[rbase + 2u * t2] = a * cs - cc * sn;
        bcf_fold[rbase + 2u * t2 + 1u] = a * sn + cc * cs;
        t2 = t2 + 1024u;
    }
    workgroupBarrier();
    // ── land the entry, then pending becomes previous ──
    var d2 = lid;
    loop {
        if (d2 >= ew) { break; }
        bcf_dst[bcf_p.dst_off + d2] = bcf_fold[d2];
        d2 = d2 + 1024u;
    }
    if (overlap) {
        storageBarrier();
        workgroupBarrier();
        var m = lid;
        loop {
            if (m >= r * w) { break; }
            bcf_qkv[m] = bcf_pkv[m];
            bcf_qsc[m] = bcf_psc[m];
            m = m + 1024u;
        }
    }
}

// index_scores with grid (entry, token): the query heads and the output
// stride by the token; the weights arrive RAW with the fold factor in the
// uniform, multiplied per head exactly where the walk's axpy used to — same
// rounding, one dispatch fewer. Per-token entry limits ride in a table.
struct BixP { nh: u32, hd: u32, n_pos: u32, factor: f32 };
@group(0) @binding(0) var<storage, read>       bix_q    : array<f32>;   // b, nh*hd
@group(0) @binding(1) var<storage, read>       bix_kv   : array<f32>;
@group(0) @binding(2) var<storage, read>       bix_w    : array<f32>;   // b, nh (raw)
@group(0) @binding(3) var<storage, read_write> bix_out  : array<f32>;   // b, 4096
@group(0) @binding(4) var<uniform>             bix_p    : BixP;
@group(0) @binding(5) var<storage, read>       bix_lim  : array<u32>;   // b
var<workgroup> bix_red: array<f32, 256>;
@compute @workgroup_size(256)
fn bt_index_scores(@builtin(workgroup_id) wid: vec3<u32>,
                   @builtin(local_invocation_index) lid: u32) {
    let t = wid.x;
    let tok = wid.y;
    if (t >= bix_p.n_pos) { return; }
    let limit = bix_lim[tok];
    let ob = tok * 4096u;
    if (t >= limit) {
        if (lid == 0u) { bix_out[ob + t] = KP_NINF; }
        return;
    }
    let hd = bix_p.hd;
    let kb = t * hd;
    let qtb = tok * bix_p.nh * hd;
    let wtb = tok * bix_p.nh;
    var acc = 0.0;
    var h = lid;
    loop {
        if (h >= bix_p.nh) { break; }
        var dot = 0.0;
        let qb = qtb + h * hd;
        for (var i = 0u; i < hd; i = i + 1u) {
            dot = dot + bix_q[qb + i] * bix_kv[kb + i];
        }
        let hw = bix_w[wtb + h] * bix_p.factor;
        acc = acc + max(dot, 0.0) * hw;
        h = h + 256u;
    }
    bix_red[lid] = acc;
    workgroupBarrier();
    var stride = 128u;
    loop {
        if (stride == 0u) { break; }
        if (lid < stride) { bix_red[lid] = bix_red[lid] + bix_red[lid + stride]; }
        workgroupBarrier();
        stride = stride >> 1u;
    }
    if (lid == 0u) { bix_out[ob + t] = bix_red[0]; }
}

// top_k_index with grid (token): scores, picks and counts stride by the
// token; the entry count comes from the same limit table.
struct BtkP { kmax: u32, _a: u32, _b: u32, _c: u32 };
@group(0) @binding(0) var<storage, read>       btk_s   : array<f32>;   // b, 4096
@group(0) @binding(1) var<storage, read_write> btk_idx : array<u32>;   // b, kmax
@group(0) @binding(2) var<storage, read_write> btk_cnt : array<u32>;   // b
@group(0) @binding(3) var<uniform>             btk_p   : BtkP;
@group(0) @binding(4) var<storage, read>       btk_lim : array<u32>;   // b
var<workgroup> btk_keep: array<u32, 4096>;
@compute @workgroup_size(1024)
fn bt_top_k(@builtin(workgroup_id) wid: vec3<u32>,
            @builtin(local_invocation_index) lid: u32) {
    let tok = wid.x;
    let n = btk_lim[tok];
    let sb = tok * 4096u;
    var i = lid;
    loop {
        if (i >= n) { break; }
        let si = btk_s[sb + i];
        var keep = 0u;
        if (si > KP_NINF) {
            var rank = 0u;
            for (var j = 0u; j < n; j = j + 1u) {
                let sj = btk_s[sb + j];
                if (sj > KP_NINF) {
                    if (sj > si || (sj == si && j < i)) { rank = rank + 1u; }
                }
            }
            if (rank < btk_p.kmax) { keep = 1u; }
        }
        btk_keep[i] = keep;
        i = i + 1024u;
    }
    workgroupBarrier();
    var m = lid;
    loop {
        if (m >= n) { break; }
        if (btk_keep[m] == 1u) {
            var before = 0u;
            for (var j = 0u; j < m; j = j + 1u) { before = before + btk_keep[j]; }
            btk_idx[tok * btk_p.kmax + before] = m;
        }
        m = m + 1024u;
    }
    workgroupBarrier();
    if (lid == 0u) {
        var total = 0u;
        for (var j = 0u; j < n; j = j + 1u) { total = total + btk_keep[j]; }
        btk_cnt[tok] = total;
    }
}

// The staged attended-position list, per token: the visible tail of the
// UNSLID window first (the batch never slides mid-pass; its own rows sit in
// staging at the cache's tail), then this token's staged rows including its
// own, then the compressed positions — the indexer's picks, or all of them
// on the layers without one (seq flag).
struct BibP { window: u32, kmax: u32, seq: u32, srow0: u32 };
@group(0) @binding(0) var<storage, read>       bib_pick : array<u32>;   // b, kmax
@group(0) @binding(1) var<storage, read_write> bib_out  : array<u32>;   // b, 1024
@group(0) @binding(2) var<uniform>             bib_p    : BibP;
@group(0) @binding(3) var<storage, read>       bib_meta : array<vec4<u32>>; // b: [win_start, win_n, staged_n, k]
@compute @workgroup_size(256)
fn bt_idx_build_staged(@builtin(workgroup_id) wid: vec3<u32>,
                       @builtin(local_invocation_id) lid: vec3<u32>) {
    let tok = wid.y;
    let i = wid.x * 256u + lid.x;
    let m = bib_meta[tok];
    let ob = tok * 1024u;
    if (i < m.y) {
        bib_out[ob + i] = m.x + i;
        return;
    }
    let s = i - m.y;
    if (s < m.z) {
        bib_out[ob + i] = bib_p.srow0 + s;
        return;
    }
    let j = i - m.y - m.z;
    if (j < m.w) {
        if (bib_p.seq != 0u) {
            bib_out[ob + i] = bib_p.window + j;
        } else {
            bib_out[ob + i] = bib_p.window + bib_pick[tok * bib_p.kmax + j];
        }
    }
}

// The grouped low-rank output projection with grid (row-quad, token): the
// same four-rows-per-workgroup walk as o_lora_a_m, the activation window and
// the output sliding with the token.
var<workgroup> obt_part: array<f32, 256>;
var<workgroup> obt_lad: array<f32, 128>;
@compute @workgroup_size(256)
fn bt_o_lora_a(@builtin(workgroup_id) wid: vec3<u32>,
               @builtin(num_workgroups) nwg: vec3<u32>,
               @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let lora = q1p._p0;
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    let t = wid.y;
    let xtok = t * (rows / lora) * gpr * 32u;
    let sub = lid / 64u;
    let lane = lid % 64u;
    var row = wid.x * 4u + sub;
    loop {
        if (row >= rows) { break; }
        if (lane < 32u) {
            let pr = unpack2x16float(q1w[params_w + row]);
            obt_lad[sub * 32u + lane] = exp2(pr.x + f32(lane) * pr.y);
        }
        workgroupBarrier();
        let xoff = xtok + (row / lora) * gpr * 32u;
        var acc = 0.0;
        var g = lane;
        loop {
            if (g >= gpr) { break; }
            let bit = g * 5u;
            let cb = codes_b + row * cstride + (bit >> 3u);
            let sh = bit & 7u;
            var cv = q4tp_byte(cb);
            if (sh > 3u) { cv = cv | (q4tp_byte(cb + 1u) << 8u); }
            let scale = obt_lad[sub * 32u + ((cv >> sh) & 31u)];
            let wv = q1wv[row * gpr + g];
            let xb = xoff + g * 32u;
            var gsum = 0.0;
            gsum = gsum + q4b_dot8(wv.x, xb);
            gsum = gsum + q4b_dot8(wv.y, xb + 8u);
            gsum = gsum + q4b_dot8(wv.z, xb + 16u);
            gsum = gsum + q4b_dot8(wv.w, xb + 24u);
            acc = acc + scale * gsum;
            g = g + 64u;
        }
        obt_part[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lane < stride) { obt_part[lid] = obt_part[lid] + obt_part[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lane == 0u) { q1y[t * rows + row] = obt_part[sub * 64u]; }
        workgroupBarrier();
        row = row + nwg.x * 4u;
    }
}


// The grouped projection, four rows to a 64-thread workgroup: the x span
// loads once per group iteration and feeds all four rows from registers,
// and the scale comes straight from the row params — the shared ladder
// with its barriers is what made the original spend 0.4 ms on 30 MB.
// Each row keeps the original's lane assignment, group order and 64-wide
// tree, so its sum is bit-identical.
var<workgroup> obt_p4: array<f32, 64>;
@compute @workgroup_size(64)
fn bt_o_lora_a4(@builtin(workgroup_id) wid: vec3<u32>,
                @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let lora = q1p._p0;
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    let t = wid.y;
    let row0 = wid.x * 4u;
    let xtok = t * (rows / lora) * gpr * 32u;
    let xoff = xtok + (row0 / lora) * gpr * 32u;
    var a0 = 0.0; var a1 = 0.0; var a2 = 0.0; var a3 = 0.0;
    var g = lid;
    loop {
        if (g >= gpr) { break; }
        let xb = xoff + g * 32u;
        let v0 = q1xv[(xb >> 2u)];      let v1 = q1xv[(xb >> 2u) + 1u];
        let v2 = q1xv[(xb >> 2u) + 2u]; let v3 = q1xv[(xb >> 2u) + 3u];
        let v4 = q1xv[(xb >> 2u) + 4u]; let v5 = q1xv[(xb >> 2u) + 5u];
        let v6 = q1xv[(xb >> 2u) + 6u]; let v7 = q1xv[(xb >> 2u) + 7u];
        let bit = g * 5u;
        let cb0 = bit >> 3u;
        let sh = bit & 7u;
        for (var r = 0u; r < 4u; r = r + 1u) {
            let row = row0 + r;
            if (row >= rows) { break; }
            let pr = unpack2x16float(q1w[params_w + row]);
            let cb = codes_b + row * cstride + cb0;
            var cv = q4tp_byte(cb);
            if (sh > 3u) { cv = cv | (q4tp_byte(cb + 1u) << 8u); }
            let code = (cv >> sh) & 31u;
            let scale = exp2(pr.x + f32(code) * pr.y);
            let wv = q1wv[row * gpr + g];
            var gsum = 0.0;
            gsum = q1_dot8v(wv.x, v0, v1)
                 + q1_dot8v(wv.y, v2, v3)
                 + q1_dot8v(wv.z, v4, v5)
                 + q1_dot8v(wv.w, v6, v7);
            let vsc = scale * gsum;
            if (r == 0u) { a0 = a0 + vsc; }
            if (r == 1u) { a1 = a1 + vsc; }
            if (r == 2u) { a2 = a2 + vsc; }
            if (r == 3u) { a3 = a3 + vsc; }
        }
        g = g + 64u;
    }
    for (var r = 0u; r < 4u; r = r + 1u) {
        var acc = a0;
        if (r == 1u) { acc = a1; }
        if (r == 2u) { acc = a2; }
        if (r == 3u) { acc = a3; }
        obt_p4[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lid < stride) { obt_p4[lid] = obt_p4[lid] + obt_p4[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lid == 0u && row0 + r < rows) { q1y[t * rows + row0 + r] = obt_p4[0]; }
        workgroupBarrier();
    }
}

// The grouped projection with the group's x span staged through shared
// memory: all four sub-rows of a workgroup belong to one o-group and
// re-read the same span — the stage cuts that traffic four-fold. Values
// and per-lane order are the un-staged kernel's, so the sums are
// bit-identical. Created only when the device's workgroup storage fits
// the 4608-float span (18 KB + change); the host also refuses shapes
// that straddle groups.
var<workgroup> obt_xs: array<f32, 4608>;
@compute @workgroup_size(256)
fn bt_o_lora_a2(@builtin(workgroup_id) wid: vec3<u32>,
                @builtin(num_workgroups) nwg: vec3<u32>,
                @builtin(local_invocation_index) lid: u32) {
    let gpr = q1p.np;
    let rows = q1p.rows;
    let lora = q1p._p0;
    let params_w = rows * gpr * 4u;
    let codes_b = rows * gpr * 16u + rows * 4u;
    let cstride = (gpr * 5u + 7u) / 8u;
    let t = wid.y;
    let xtok = t * (rows / lora) * gpr * 32u;
    let sub = lid / 64u;
    let lane = lid % 64u;
    var row = wid.x * 4u + sub;
    loop {
        if (row >= rows) { break; }
        let span = gpr * 32u;
        let xoff = xtok + (row / lora) * span;
        for (var j = lid; j < span; j = j + 256u) { obt_xs[j] = q1x[xoff + j]; }
        if (lane < 32u) {
            let pr = unpack2x16float(q1w[params_w + row]);
            obt_lad[sub * 32u + lane] = exp2(pr.x + f32(lane) * pr.y);
        }
        workgroupBarrier();
        var acc = 0.0;
        var g = lane;
        loop {
            if (g >= gpr) { break; }
            let bit = g * 5u;
            let cb = codes_b + row * cstride + (bit >> 3u);
            let sh = bit & 7u;
            var cv = q4tp_byte(cb);
            if (sh > 3u) { cv = cv | (q4tp_byte(cb + 1u) << 8u); }
            let scale = obt_lad[sub * 32u + ((cv >> sh) & 31u)];
            let base = (row * gpr + g) * 4u;
            let xb = g * 32u;
            var gsum = 0.0;
            for (var k = 0u; k < 4u; k = k + 1u) {
                let w = q1w[base + k];
                let x8 = xb + 8u * k;
                gsum = gsum + (f32(w & 0xFu) - 8.0) * obt_xs[x8]
                     + (f32((w >> 4u) & 0xFu) - 8.0) * obt_xs[x8 + 1u]
                     + (f32((w >> 8u) & 0xFu) - 8.0) * obt_xs[x8 + 2u]
                     + (f32((w >> 12u) & 0xFu) - 8.0) * obt_xs[x8 + 3u]
                     + (f32((w >> 16u) & 0xFu) - 8.0) * obt_xs[x8 + 4u]
                     + (f32((w >> 20u) & 0xFu) - 8.0) * obt_xs[x8 + 5u]
                     + (f32((w >> 24u) & 0xFu) - 8.0) * obt_xs[x8 + 6u]
                     + (f32((w >> 28u) & 0xFu) - 8.0) * obt_xs[x8 + 7u];
            }
            acc = acc + scale * gsum;
            g = g + 64u;
        }
        obt_part[lid] = acc;
        workgroupBarrier();
        var stride = 32u;
        loop {
            if (stride == 0u) { break; }
            if (lane < stride) { obt_part[lid] = obt_part[lid] + obt_part[lid + stride]; }
            workgroupBarrier();
            stride = stride >> 1u;
        }
        if (lane == 0u) { q1y[t * rows + row] = obt_part[sub * 64u]; }
        workgroupBarrier();
        row = row + nwg.x * 4u;
    }
}
"#;

// Split-K decode attention (its own module: the main module's at_* binding
// slots are taken, and WGSL forbids two resource vars on one binding).
// `gqa_attend_part` runs the flash-decoding loop over ONE ck-position chunk
// per workgroup — grid (nh, nchunks) instead of nh, which left a discrete GPU
// at 16 resident workgroups and latency-bound at depth — and stores each
// chunk's unnormalized accumulator plus its (m, l) softmax frame.
// `gqa_attend_merge` (grid nh) rescales the chunk frames into the global max
// and normalizes. Same math as `gqa_attend` up to one extra merge rounding.
const ATTEND_SPLIT_SRC: &str = r#"
struct ApP { nh: u32, hpk: u32, hd: u32, cap: u32, n: u32, ck: u32, nc: u32, _p: u32 };
@group(0) @binding(0) var<storage, read>       ap_q  : array<vec4<f32>>;
@group(0) @binding(1) var<storage, read>       ap_k  : array<vec4<f32>>;
@group(0) @binding(2) var<storage, read>       ap_v  : array<vec4<f32>>;
@group(0) @binding(3) var<storage, read_write> ap_acc: array<f32>;
@group(0) @binding(4) var<storage, read_write> ap_ml : array<vec2<f32>>;
@group(0) @binding(5) var<uniform>             ap_p  : ApP;
@group(0) @binding(6) var<storage, read_write> ap_o  : array<f32>;
var<workgroup> app_acc: array<f32, 8224>;
var<workgroup> app_m: array<f32, 32>;
var<workgroup> app_l: array<f32, 32>;
@compute @workgroup_size(32)
fn gqa_attend_part(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let ch = wid.y;
    let lane = lid.x;
    if (h >= ap_p.nh) { return; }
    let hd = ap_p.hd;
    let hd4 = hd / 4u;
    let p0 = ch * ap_p.ck;
    let pend = min(ap_p.n, p0 + ap_p.ck);
    let kbase = (h / ap_p.hpk) * ap_p.cap * hd4;
    let qbase = h * hd4;
    let scale = 1.0 / sqrt(f32(hd));
    let base = lane * 257u;
    for (var d = 0u; d < hd; d = d + 1u) { app_acc[base + d] = 0.0; }
    var m = -1e30;
    var l = 0.0;
    var p = p0 + lane;
    loop {
        if (p >= pend) { break; }
        let krow = kbase + p * hd4;
        var dot4 = vec4<f32>(0.0);
        for (var d = 0u; d < hd4; d = d + 1u) { dot4 = dot4 + ap_q[qbase + d] * ap_k[krow + d]; }
        let dot = (dot4.x + dot4.y + dot4.z + dot4.w) * scale;
        let mp = max(m, dot);
        let f = exp(m - mp);
        let w = exp(dot - mp);
        l = l * f + w;
        for (var d = 0u; d < hd4; d = d + 1u) {
            let vv = ap_v[krow + d] * w;
            let a = base + d * 4u;
            app_acc[a]      = app_acc[a]      * f + vv.x;
            app_acc[a + 1u] = app_acc[a + 1u] * f + vv.y;
            app_acc[a + 2u] = app_acc[a + 2u] * f + vv.z;
            app_acc[a + 3u] = app_acc[a + 3u] * f + vv.w;
        }
        m = mp;
        p = p + 32u;
    }
    app_m[lane] = m;
    app_l[lane] = l;
    workgroupBarrier();
    var stride = 16u;
    loop {
        if (stride == 0u) { break; }
        if (lane < stride) {
            let o = lane + stride;
            let m1 = app_m[lane];
            let m2 = app_m[o];
            let mm = max(m1, m2);
            let f1 = exp(m1 - mm);
            let f2 = exp(m2 - mm);
            app_l[lane] = app_l[lane] * f1 + app_l[o] * f2;
            let bo = o * 257u;
            for (var d = 0u; d < hd; d = d + 1u) {
                app_acc[base + d] = app_acc[base + d] * f1 + app_acc[bo + d] * f2;
            }
            app_m[lane] = mm;
        }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let idx = h * ap_p.nc + ch;
    for (var d = lane; d < hd; d = d + 32u) {
        ap_acc[idx * hd + d] = app_acc[d];
    }
    if (lane == 0u) {
        ap_ml[idx] = vec2<f32>(app_m[0], app_l[0]);
    }
}
// hd <= 128 twin of gqa_attend_part at stride 129 (16.5 KB workgroup
// memory — fits the 32 KB mobile/Metal limit; see gqa_attend_s).
var<workgroup> app_acc_s: array<f32, 4128>;
@compute @workgroup_size(32)
fn gqa_attend_part_s(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let ch = wid.y;
    let lane = lid.x;
    if (h >= ap_p.nh) { return; }
    let hd = ap_p.hd;
    let hd4 = hd / 4u;
    let p0 = ch * ap_p.ck;
    let pend = min(ap_p.n, p0 + ap_p.ck);
    let kbase = (h / ap_p.hpk) * ap_p.cap * hd4;
    let qbase = h * hd4;
    let scale = 1.0 / sqrt(f32(hd));
    let base = lane * 129u;
    for (var d = 0u; d < hd; d = d + 1u) { app_acc_s[base + d] = 0.0; }
    var m = -1e30;
    var l = 0.0;
    var p = p0 + lane;
    loop {
        if (p >= pend) { break; }
        let krow = kbase + p * hd4;
        var dot4 = vec4<f32>(0.0);
        for (var d = 0u; d < hd4; d = d + 1u) { dot4 = dot4 + ap_q[qbase + d] * ap_k[krow + d]; }
        let dot = (dot4.x + dot4.y + dot4.z + dot4.w) * scale;
        let mp = max(m, dot);
        let f = exp(m - mp);
        let w = exp(dot - mp);
        l = l * f + w;
        for (var d = 0u; d < hd4; d = d + 1u) {
            let vv = ap_v[krow + d] * w;
            let a = base + d * 4u;
            app_acc_s[a]      = app_acc_s[a]      * f + vv.x;
            app_acc_s[a + 1u] = app_acc_s[a + 1u] * f + vv.y;
            app_acc_s[a + 2u] = app_acc_s[a + 2u] * f + vv.z;
            app_acc_s[a + 3u] = app_acc_s[a + 3u] * f + vv.w;
        }
        m = mp;
        p = p + 32u;
    }
    app_m[lane] = m;
    app_l[lane] = l;
    workgroupBarrier();
    var stride = 16u;
    loop {
        if (stride == 0u) { break; }
        if (lane < stride) {
            let o = lane + stride;
            let m1 = app_m[lane];
            let m2 = app_m[o];
            let mm = max(m1, m2);
            let f1 = exp(m1 - mm);
            let f2 = exp(m2 - mm);
            app_l[lane] = app_l[lane] * f1 + app_l[o] * f2;
            let bo = o * 129u;
            for (var d = 0u; d < hd; d = d + 1u) {
                app_acc_s[base + d] = app_acc_s[base + d] * f1 + app_acc_s[bo + d] * f2;
            }
            app_m[lane] = mm;
        }
        workgroupBarrier();
        stride = stride / 2u;
    }
    let idx = h * ap_p.nc + ch;
    for (var d = lane; d < hd; d = d + 32u) {
        ap_acc[idx * hd + d] = app_acc_s[d];
    }
    if (lane == 0u) {
        ap_ml[idx] = vec2<f32>(app_m[0], app_l[0]);
    }
}
@compute @workgroup_size(32)
fn gqa_attend_merge(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
    let h = wid.x;
    let lane = lid.x;
    if (h >= ap_p.nh) { return; }
    let hd = ap_p.hd;
    let nc = (ap_p.n + ap_p.ck - 1u) / ap_p.ck;
    var mg = -1e30;
    for (var ci = 0u; ci < nc; ci = ci + 1u) { mg = max(mg, ap_ml[h * ap_p.nc + ci].x); }
    var lg = 0.0;
    for (var ci = 0u; ci < nc; ci = ci + 1u) {
        let ml = ap_ml[h * ap_p.nc + ci];
        lg = lg + ml.y * exp(ml.x - mg);
    }
    let invl = select(0.0, 1.0 / lg, lg > 0.0);
    for (var d = lane; d < hd; d = d + 32u) {
        var a = 0.0;
        for (var ci = 0u; ci < nc; ci = ci + 1u) {
            let idx = h * ap_p.nc + ci;
            a = a + ap_acc[idx * hd + d] * exp(ap_ml[idx].x - mg);
        }
        ap_o[h * hd + d] = a * invl;
    }
}
"#;

/// Positions per split-K attend chunk; the split path engages past
/// `ATTEND_SPLIT_MIN` cached positions (below it the single-workgroup
/// kernel's one dispatch wins).
const ATTEND_CK: usize = 128;
const ATTEND_SPLIT_MIN: usize = 256;

struct Ctx {
    /// The instance and adapter the device came from, kept for exactly as
    /// long as the device — which Vulkan requires and we were not doing.
    /// They were locals in `init()`, so `VkInstance` was destroyed while a
    /// `VkDevice` made from it lived on in the static below. AddressSanitizer
    /// caught what that costs: a 48-byte block allocated by libEGL is freed
    /// on the way out of `init`, and freed a second time by the NVIDIA driver
    /// at process exit — the `double free or corruption` / `corrupted
    /// double-linked list` abort that has been landing after correct answers.
    _instance: wgpu::Instance,
    _adapter: wgpu::Adapter,
    device: wgpu::Device,
    queue: wgpu::Queue,
    matvec: wgpu::ComputePipeline,
    matmat: wgpu::ComputePipeline,
    mul_mm: wgpu::ComputePipeline,
    q1_mm: wgpu::ComputePipeline,
    silu: wgpu::ComputePipeline,
    axpy: wgpu::ComputePipeline,
    gate_mul: wgpu::ComputePipeline,
    zero: wgpu::ComputePipeline,
    q1: wgpu::ComputePipeline,
    q1t: wgpu::ComputePipeline,
    q4b: wgpu::ComputePipeline,
    q4t_mv: wgpu::ComputePipeline,
    /// Hyper-connection fold (with the Sinkhorn) and expand — the join
    /// between blocks in DeepSeek-V4, where an ordinary model has a residual.
    /// Attention over an index list with a learned sink — DeepSeek-V4's,
    /// not the canonical sliding window.
    /// Per-head RMS and the rope tail, forward or inverse.
    rope_heads: wgpu::ComputePipeline,
    o_lora_a: wgpu::ComputePipeline,
    kv_pool: wgpu::ComputePipeline,
    index_scores: wgpu::ComputePipeline,
    top_k_index: wgpu::ComputePipeline,
    hc_block: wgpu::ComputePipeline,
    f32_matvec_w: wgpu::ComputePipeline,
    o_lora_a_w: wgpu::ComputePipeline,
    f32_matvec_x: wgpu::ComputePipeline,
    f32_mv_split: wgpu::ComputePipeline,
    f32_mv_merge: wgpu::ComputePipeline,
    o_lora_a_m: wgpu::ComputePipeline,
    moe_gu_q2tp_m: wgpu::ComputePipeline,
    moe_dn_q4tp_m: wgpu::ComputePipeline,
    sa_part: wgpu::ComputePipeline,
    sa_merge: wgpu::ComputePipeline,
    blit: wgpu::ComputePipeline,
    idx_build: wgpu::ComputePipeline,
    moe_route: wgpu::ComputePipeline,
    sparse_attend: wgpu::ComputePipeline,
    sa_scores: wgpu::ComputePipeline,
    sa_apply: wgpu::ComputePipeline,
    hc_pre_fold: wgpu::ComputePipeline,
    hc_post_expand: wgpu::ComputePipeline,
    // Token-axis twins for the batched dsv4 frame.
    bt_rope_heads: wgpu::ComputePipeline,
    bt_hc_pre_fold: wgpu::ComputePipeline,
    bt_hc_block: wgpu::ComputePipeline,
    bt_comp_append: wgpu::ComputePipeline,
    bt_comp_fold: wgpu::ComputePipeline,
    bt_hc_post_expand: wgpu::ComputePipeline,
    bt_f32_matvec_w: wgpu::ComputePipeline,
    bt_f32_matvec_x: wgpu::ComputePipeline,
    bt_moe_route: wgpu::ComputePipeline,
    bt_moe_gate_up_q2tp: wgpu::ComputePipeline,
    bt_moe_gate_up_q2tp_r4: wgpu::ComputePipeline,
    bt_sparse_attend: wgpu::ComputePipeline,
    bt_index_scores: wgpu::ComputePipeline,
    bt_top_k: wgpu::ComputePipeline,
    bt_idx_build_staged: wgpu::ComputePipeline,
    bt_o_lora_a: wgpu::ComputePipeline,
    bt_o_lora_a4: wgpu::ComputePipeline,
    bt_o_lora_a2: Option<wgpu::ComputePipeline>,
    q4tp_mv: wgpu::ComputePipeline,
    /// Tall-matrix q4tp matvec (4 rows/workgroup, vec4 nibble loads); the
    /// per-row math is byte-identical to `q4tp_mv`. `CMF_MV4=0` reverts.
    q4tp_mv4: wgpu::ComputePipeline,
    use_mv4: bool,
    q4tp_mv16: wgpu::ComputePipeline,
    q4t_mv8: wgpu::ComputePipeline,
    q4b_mv8: wgpu::ComputePipeline,
    q4tp_mm: wgpu::ComputePipeline,
    argmax_part: wgpu::ComputePipeline,
    gdn_step_par: wgpu::ComputePipeline,
    gdn_step_norm: wgpu::ComputePipeline,
    gdn_par: bool,
    ts_query: Option<(wgpu::QuerySet, wgpu::Buffer, wgpu::Buffer)>,
    ts_period: f32,
    argmax_final: wgpu::ComputePipeline,
    embed_gather_q4tp: wgpu::ComputePipeline,
    silu_down: wgpu::ComputePipeline,
    q1t_mm: wgpu::ComputePipeline,
    q4t_mm: wgpu::ComputePipeline,
    dit_qk: wgpu::ComputePipeline,
    dit_pv: wgpu::ComputePipeline,
    dit_softmax: wgpu::ComputePipeline,
    dit_unstack: wgpu::ComputePipeline,
    ffn_silu: wgpu::ComputePipeline,
    q1t_ovmm: wgpu::ComputePipeline,
    rmsnorm: wgpu::ComputePipeline,
    add_rmsnorm: wgpu::ComputePipeline,
    rmsnorm_b: wgpu::ComputePipeline,
    add_rmsnorm_b: wgpu::ComputePipeline,
    attn_rope: wgpu::ComputePipeline,
    kv_append: wgpu::ComputePipeline,
    gqa_attend: wgpu::ComputePipeline,
    gqa_attend_s: wgpu::ComputePipeline,
    attend_part: wgpu::ComputePipeline,
    attend_part_s: wgpu::ComputePipeline,
    attend_merge: wgpu::ComputePipeline,
    /// Max head_dim the attend kernels can serve on this device: 256
    /// when 33 KB of workgroup storage fits (desktop), 128 on 32 KB
    /// devices (Adreno/Mali/wgpu-Metal) where only the stride-129
    /// kernels exist.
    hd_cap: usize,
    /// 32 KB+ of workgroup storage: the split-K attend parts need it.
    big_attend: bool,
    gdn_step: wgpu::ComputePipeline,
    gdn_conv: wgpu::ComputePipeline,
    f32_matvec: wgpu::ComputePipeline,
    f32_matvec_b: wgpu::ComputePipeline,
    layout_f32b: wgpu::BindGroupLayout,
    o1_far: wgpu::ComputePipeline,
    o1_push: wgpu::ComputePipeline,
    o1_attend: wgpu::ComputePipeline,
    layout_o1_far: wgpu::BindGroupLayout,
    layout_o1_push: wgpu::BindGroupLayout,
    layout_o1_attend: wgpu::BindGroupLayout,
    /// Device o1 state per (kv_id, layer); re-uploaded when the seal
    /// epoch changes (each generate seals fresh CPU state).
    o1m: Mutex<HashMap<(u64, usize), O1Dev>>,
    moe_select: wgpu::ComputePipeline,
    /// Two independent projections of one input in a single dispatch.
    matvec_pair: wgpu::ComputePipeline,
    layout_mv2: wgpu::BindGroupLayout,
    moe_gate_up: wgpu::ComputePipeline,
    moe_down: wgpu::ComputePipeline,
    /// q4tp twins — same bindings, ladder-plane scale decode.
    moe_gate_up_q4tp: wgpu::ComputePipeline,
    moe_down_q4tp: wgpu::ComputePipeline,
    /// Batched (token-axis) MoE for the batch graph; select_b carries the
    /// f32 router matvec inside itself.
    moe_select_b: wgpu::ComputePipeline,
    gdn_conv_k: wgpu::ComputePipeline,
    q4tp_mv_k: wgpu::ComputePipeline,
    q4tp_mv16w: wgpu::ComputePipeline,
    gdn_step_par_k: wgpu::ComputePipeline,
    gdn_step_norm_k: wgpu::ComputePipeline,
    gdn_step_k: wgpu::ComputePipeline,
    moe_gate_up_q4tp_b: wgpu::ComputePipeline,
    moe_down_q4tp_b: wgpu::ComputePipeline,
    moe_down_q4tp_b2: wgpu::ComputePipeline,
    moe_down_q4tp_part: wgpu::ComputePipeline,
    moe_down_q4tp_b4: wgpu::ComputePipeline,
    moe_down_q2tp_b: wgpu::ComputePipeline,
    moe_down_q4tp_red: wgpu::ComputePipeline,
    layout_moe_sel_b: wgpu::BindGroupLayout,
    layout_moe_gu_b: wgpu::BindGroupLayout,
    layout_moe_dn_b: wgpu::BindGroupLayout,
    layout: wgpu::BindGroupLayout,
    layout_mm: wgpu::BindGroupLayout,
    layout_mmm: wgpu::BindGroupLayout,
    layout_q1mm: wgpu::BindGroupLayout,
    layout_silu: wgpu::BindGroupLayout,
    layout_axpy: wgpu::BindGroupLayout,
    layout_gate_mul: wgpu::BindGroupLayout,
    layout_zero: wgpu::BindGroupLayout,
    layout_q1: wgpu::BindGroupLayout,
    layout_rmsnorm: wgpu::BindGroupLayout,
    layout_add_rmsnorm: wgpu::BindGroupLayout,
    layout_rmsnorm_b: wgpu::BindGroupLayout,
    layout_add_rmsnorm_b: wgpu::BindGroupLayout,
    layout_attn_rope: wgpu::BindGroupLayout,
    layout_kv: wgpu::BindGroupLayout,
    layout_attend: wgpu::BindGroupLayout,
    layout_attend_s: wgpu::BindGroupLayout,
    layout_attend_part: wgpu::BindGroupLayout,
    layout_attend_part_s: wgpu::BindGroupLayout,
    layout_attend_merge: wgpu::BindGroupLayout,
    layout_gdn: wgpu::BindGroupLayout,
    layout_gdn_conv: wgpu::BindGroupLayout,
    layout_f32: wgpu::BindGroupLayout,
    layout_silu_down: wgpu::BindGroupLayout,
    layout_moe_sel: wgpu::BindGroupLayout,
    layout_moe_gu: wgpu::BindGroupLayout,
    layout_moe_dn: wgpu::BindGroupLayout,
    /// wgpu treats an auto-derived layout as exclusive to the pipeline it
    /// came from, so the q4tp twins need their own even though the
    /// binding lists are identical.
    layout_moe_gu_q4tp: wgpu::BindGroupLayout,
    moe_gate_up_q2tp: wgpu::ComputePipeline,
    layout_moe_gu_q2tp: wgpu::BindGroupLayout,
    moe_gate_up_q2tp_f: wgpu::ComputePipeline,
    moe_down_q4tp_f: wgpu::ComputePipeline,
    gqa_attend_dec: wgpu::ComputePipeline,
    moe_select_sg: Option<wgpu::ComputePipeline>,
    gdn_step_par2: wgpu::ComputePipeline,
    gdn_step_norm2: wgpu::ComputePipeline,
    gdn_inline: bool,
    attend_dec: bool,
    foldsel: bool,
    layout_moe_dn_q4tp: wgpu::BindGroupLayout,
    /// Discrete card (PCIe VRAM) vs UMA — thresholds and budgets differ.
    discrete: bool,
    /// Weight-residency budget in bytes (CMF_GPU_VRAM_MB override). On a
    /// 24 GB card holding a 35 GB model, the first-touched tensors (=
    /// the first layers, decode touches them in order) stay resident and
    /// the rest honestly fall back to CPU — ngl-style offload without an
    /// explicit layer list, and no OOM.
    vram_budget: u64,
    /// Bytes currently resident in `weight_bufs`.
    resident: std::sync::atomic::AtomicU64,
    /// Pooled per-op scratch (grow-only): xs upload, y output, uniform
    /// params, readback staging. Every op used to CREATE all four (plus
    /// a bind group) and map_async-poll a fresh staging buffer — pure
    /// allocator traffic on the hot path. The lock is held across the
    /// whole op (encode → submit → poll): ops already serialize on the
    /// single queue.
    scratch: Mutex<Scratch>,
    /// Resident quant weights in VRAM — the WHOLE tensor is loaded once
    /// (key (base_ptr, idx)); ranges/batches address it by offset.
    ///
    /// Residency is DEMAND-DRIVEN and evicting: nothing is preloaded, the
    /// set grows as the model routes, and when the budget is full the
    /// least-valuable tensor makes room. Without eviction the first
    /// tensors to arrive owned the device for the process's life — a
    /// model that switched from prose to code kept the prose experts and
    /// ran the code ones on the CPU forever. The two working sets overlap
    /// at a Jaccard of 0.095, measured, so that is not a corner case.
    weight_bufs: Mutex<HashMap<(usize, usize), Resident>>,
    /// DeepSeek-V4's attention cache, per (kv id, layer), living on the card
    /// between tokens. Re-uploading it each token costs more than the frame
    /// saves: at 4K context it is megabytes per layer.
    dsv4_kv: Mutex<HashMap<(u64, usize), (wgpu::Buffer, usize)>>,
    /// The frame's working buffers, keyed by role and length. Their sizes do
    /// not change from token to token, and allocating ten of them per layer
    /// per token — 430 allocations a token on the release — is most of what a
    /// submission costs. Created once, written thereafter.
    dsv4_scratch: Mutex<HashMap<(u8, usize, usize), wgpu::Buffer>>,
    /// One compressor's streams, alive across tokens ON THE DEVICE:
    /// `[pending_kv, pending_score, prev_kv, prev_score]` keyed by
    /// (kind, kv_id, layer) — kind 0 is the attention compressor, 1 the
    /// indexer's own. Keeping these here is what lets a token advance the
    /// compressor without the host reading anything back: the streams are
    /// the only thing that carried state across the seam.
    dsv4_comp: Mutex<HashMap<(u8, u64, usize), [wgpu::Buffer; 4]>>,
    /// The indexer's compressed cache per layer, grown as the sequence is.
    dsv4_ixkv: Mutex<HashMap<(u64, usize), (wgpu::Buffer, usize)>>,
    /// Stable per-(tag, kv, layer) uniform slots for sequence-varying scalars.
    dsv4_uni: Mutex<HashMap<(u8, u64, usize), wgpu::Buffer>>,
    dsv4_store: Mutex<HashMap<(u8, u64, usize), wgpu::Buffer>>,
    /// CMF_DSV4_SLOT_CHECK: writes per slot key since the last submission.
    /// A slot may be written ONCE per submission — queue writes do not
    /// interleave with passes, so a second write reaches every dispatch of
    /// the first. That is the bug that made a run of layers route with the
    /// last layer's router bias, and a tag collision between two call sites
    /// looks exactly the same from here.
    slot_writes: Mutex<HashMap<(u8, u64, usize), u32>>,
    /// (epoch, bind groups) for the dsv4 frames — see `cached_bind`.
    dsv4_binds: Mutex<(u64, HashMap<(u8, u64, usize), wgpu::BindGroup>)>,
    /// Access clock for the aging above — one tick per weight lookup.
    res_clock: std::sync::atomic::AtomicU64,
    /// row_scale buffer per (idx, row0) — small, cached.
    rs_bufs: Mutex<HashMap<(usize, usize), wgpu::Buffer>>,
    /// Device K/V cache mirror per (kv_id, layer) for the token graph:
    /// [nkv, cap, hd] each, persists across decode tokens. `synced` counts
    /// the positions already resident (prefill sync + graph appends).
    attn_kv: Mutex<HashMap<(u64, usize), KvMirror>>,
    /// GDN recurrent state per (kv_id, layer): (conv ring, S), persists across
    /// decode tokens (created zeroed on first touch).
    gdn_state: Mutex<HashMap<(u64, usize), (wgpu::Buffer, wgpu::Buffer)>>,
    /// Speculative verify: per (kv_id, layer) snapshot buffer holding the
    /// GDN (ring, S) after every batch position, so a partial acceptance
    /// restores the recurrent state to the last position that was real.
    /// Value: (buffer, ring bytes, state bytes, slots).
    gdn_snap: Mutex<HashMap<(u64, usize), (wgpu::Buffer, u64, u64, usize)>>,
    /// Per-layer concatenated MoE expert weights (gate_all, up_all, down_all)
    /// keyed by (file base ptr, first gate idx) — every routed expert plus the
    /// shared one as the trailing block, uploaded once, addressed by expert id
    /// inside the kernels. Counted against `resident` like any weight.
    moe_expw: Mutex<HashMap<(usize, usize), (wgpu::Buffer, wgpu::Buffer, wgpu::Buffer)>>,
    /// Immutable [rows,cols,…] uniforms cached by content — the ~800 matvec
    /// param buffers per token are token-invariant, so uploading them once
    /// keeps them off the per-token encode critical path.
    uniforms: Mutex<HashMap<[u32; 4], wgpu::Buffer>>,
    uniforms8: Mutex<HashMap<[u32; 8], wgpu::Buffer>>,
    /// Immutable norm/small weight buffers cached by (data ptr, len) — the
    /// ~200 per-layer norm uploads per token are token-invariant. Sentinel
    /// key (0, n) holds shared zero buffers. Assumes stable weight pointers
    /// (mmap), same as `weight_bufs`.
    const_bufs: Mutex<HashMap<(usize, usize), wgpu::Buffer>>,
    /// Pooled graph scratch: eliminates per-token buffer allocations in the
    /// whole-token graph path (the dominant decode cost on Vulkan/DX12).
    graph_scratch: Mutex<GraphScratch>,
}

struct KvMirror {
    k: wgpu::Buffer,
    v: wgpu::Buffer,
    synced: usize,
}

#[derive(Default)]
struct Scratch {
    xs: Option<(wgpu::Buffer, u64)>,
    y: Option<(wgpu::Buffer, u64)>,
    stage: Option<(wgpu::Buffer, u64)>,
    /// The chain's paired readback (folded vector + hyper-connection state).
    /// Its own slot: `stage` is sized for one of them and sharing would make
    /// every token recreate whichever was asked for second.
    stage2: Option<(wgpu::Buffer, u64)>,
    params: Option<wgpu::Buffer>,
    /// Fused-FFN intermediates (gate / up panels).
    g: Option<(wgpu::Buffer, u64)>,
    u: Option<(wgpu::Buffer, u64)>,
    /// DiT attention: Q/K/V uploads, scores, panel, output, staging.
    dq: Option<(wgpu::Buffer, u64)>,
    dk: Option<(wgpu::Buffer, u64)>,
    dv: Option<(wgpu::Buffer, u64)>,
    dsc: Option<(wgpu::Buffer, u64)>,
    dpan: Option<(wgpu::Buffer, u64)>,
    dout: Option<(wgpu::Buffer, u64)>,
    dstage: Option<(wgpu::Buffer, u64)>,
    dpar: Option<wgpu::Buffer>,
}

impl Scratch {
    /// Grow-only slot: reuse when big enough, else recreate.
    fn ensure(
        dev: &wgpu::Device,
        slot: &mut Option<(wgpu::Buffer, u64)>,
        need: u64,
        usage: wgpu::BufferUsages,
        label: &str,
    ) -> wgpu::Buffer {
        match slot {
            Some((b, cap)) if *cap >= need => b.clone(),
            _ => {
                crate::gpu::probe_note_cold();
                let cap = need.next_power_of_two().max(4096);
                let b = dev.create_buffer(&wgpu::BufferDescriptor {
                    label: Some(label),
                    size: cap,
                    usage,
                    mapped_at_creation: false,
                });
                *slot = Some((b.clone(), cap));
                b
            }
        }
    }
}

/// Pooled scratch for the whole-token graph path. Grow-only: each slot is
/// allocated once (or grown) and reused across tokens — eliminates the ~20
/// Vulkan buffer allocations per token that dominated decode latency.
#[derive(Default)]
struct GraphScratch {
    h: Option<(wgpu::Buffer, u64)>,
    n1: Option<(wgpu::Buffer, u64)>,
    qraw: Option<(wgpu::Buffer, u64)>,
    kb: Option<(wgpu::Buffer, u64)>,
    vb: Option<(wgpu::Buffer, u64)>,
    qout: Option<(wgpu::Buffer, u64)>,
    gout: Option<(wgpu::Buffer, u64)>,
    attn: Option<(wgpu::Buffer, u64)>,
    ob: Option<(wgpu::Buffer, u64)>,
    gbuf: Option<(wgpu::Buffer, u64)>,
    ubuf: Option<(wgpu::Buffer, u64)>,
    abuf: Option<(wgpu::Buffer, u64)>,
    // GDN intermediates
    qkv_b: Option<(wgpu::Buffer, u64)>,
    cq_b: Option<(wgpu::Buffer, u64)>,
    z_b: Option<(wgpu::Buffer, u64)>,
    a_b: Option<(wgpu::Buffer, u64)>,
    b_b: Option<(wgpu::Buffer, u64)>,
    gdo_b: Option<(wgpu::Buffer, u64)>,
    // Split-K attend partials: [nh·nchunks·hd] accumulators + [nh·nchunks] (m,l)
    apacc: Option<(wgpu::Buffer, u64)>,
    apml: Option<(wgpu::Buffer, u64)>,
    // MoE routing intermediates: router logits, shared-gate logit, selected
    // expert ids + weights, per-slot activations
    m_logit: Option<(wgpu::Buffer, u64)>,
    m_slog: Option<(wgpu::Buffer, u64)>,
    m_sel: Option<(wgpu::Buffer, u64)>,
    m_wt: Option<(wgpu::Buffer, u64)>,
    m_act: Option<(wgpu::Buffer, u64)>,
    // Logits output + readback staging
    logits: Option<(wgpu::Buffer, u64)>,
    stage: Option<(wgpu::Buffer, u64)>,
    // Position-dependent uniforms (fixed size, write_buffer each token)
    kv_u: Option<wgpu::Buffer>,   // 16 bytes: [nkv, hd, cap, position]
    at_u: Option<wgpu::Buffer>,   // 32 bytes: [nh, nh/nkv, hd, cap, pos+1, 0, 0, 0]
    rope_u: Option<wgpu::Buffer>, // 32 bytes: [nh, nkv, hd, rd, pos, flags, eps, 0]
    // Multi-step slots: one uniform PER STEP with a stable identity, so the
    // attention bind groups survive across chunks (write_buffer runs at
    // submit — a single shared uniform would collapse every step to the
    // last position written).
    kv_us: Vec<wgpu::Buffer>,
    at_us: Vec<wgpu::Buffer>,
    rope_us: Vec<wgpu::Buffer>,
    ids: Option<(wgpu::Buffer, u64)>,
    ids_stage: Option<(wgpu::Buffer, u64)>,
    am_pv: Option<(wgpu::Buffer, u64)>,
    am_pi: Option<(wgpu::Buffer, u64)>,
}

impl GraphScratch {
    fn ensure(
        dev: &wgpu::Device,
        slot: &mut Option<(wgpu::Buffer, u64)>,
        need: u64,
        usage: wgpu::BufferUsages,
        label: &str,
    ) -> wgpu::Buffer {
        match slot {
            Some((b, cap)) if *cap >= need => b.clone(),
            _ => {
                let cap = need.next_power_of_two().max(256);
                let b = dev.create_buffer(&wgpu::BufferDescriptor {
                    label: Some(label),
                    size: cap,
                    usage,
                    mapped_at_creation: false,
                });
                *slot = Some((b.clone(), cap));
                b
            }
        }
    }
    /// Pooled uniform buffer of `size` bytes (created once, write_buffer'd each token).
    fn ensure_uniform(
        dev: &wgpu::Device,
        slot: &mut Option<wgpu::Buffer>,
        size: u64,
    ) -> wgpu::Buffer {
        match slot {
            Some(b) => b.clone(),
            None => {
                let b = dev.create_buffer(&wgpu::BufferDescriptor {
                    label: Some("g-unif"),
                    size,
                    usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                });
                *slot = Some(b.clone());
                b
            }
        }
    }
}

static CTX: OnceLock<Option<Ctx>> = OnceLock::new();

/// Whether the wgpu path is selected (the facade asks before `enabled()`):
/// `CMF_GPU=wgpu` — always; `CMF_GPU=1` (≠0) — only on non-macOS, where
/// there is no native Metal (on macOS `=1` goes to Metal). UNSET selects
/// the path by default on Linux/Windows when the feature is compiled —
/// init failure is a clean CPU fallback, so a box without a driver loses
/// nothing. `CMF_GPU=0` forces the CPU.
pub fn selected() -> bool {
    match std::env::var("CMF_GPU") {
        Ok(v) if v == "wgpu" => true,
        Ok(v) if v != "0" && v != "off" => !cfg!(target_os = "macos"),
        Ok(_) => false,
        Err(_) => {
            crate::pipeline::GLOBAL_USE_GPU.load(std::sync::atomic::Ordering::Relaxed)
                || cfg!(any(target_os = "linux", target_os = "windows"))
        }
    }
}

fn ctx() -> Option<&'static Ctx> {
    CTX.get_or_init(|| {
        if !selected() {
            return None;
        }
        match init() {
            Ok(c) => Some(c),
            Err(e) => {
                // Tests install no subscriber, so a tracing-only report makes
                // an init failure look exactly like "no GPU here".
                tracing::warn!("wgpu init failed — CPU fallback: {e}");
                if std::env::var("CMF_GPU_DEBUG").is_ok()
                    || std::env::var("CMF_DSV4_FRAME_DEBUG").is_ok()
                {
                    eprintln!("wgpu init не удался — откат на CPU: {e}");
                }
                None
            }
        }
    })
    .as_ref()
}

/// Total device-local memory of a Vulkan adapter, from the driver's own heap
/// report. None on other backends or when the query is unavailable. Only
/// platforms where wgpu carries the Vulkan backend at all; elsewhere the
/// stub answers None and the conservative default budget stands.
#[cfg(any(target_os = "linux", target_os = "windows", target_os = "android"))]
fn vulkan_vram_total(adapter: &wgpu::Adapter) -> Option<u64> {
    if adapter.get_info().backend != wgpu::Backend::Vulkan {
        return None;
    }
    unsafe {
        let hal = adapter.as_hal::<wgpu::hal::api::Vulkan>()?;
        let phd = hal.raw_physical_device();
        let mem = hal
            .shared_instance()
            .raw_instance()
            .get_physical_device_memory_properties(phd);
        let mut total = 0u64;
        for heap in mem.memory_heaps[..mem.memory_heap_count as usize].iter() {
            // DEVICE_LOCAL is bit 0 of VkMemoryHeapFlags. The LARGEST such
            // heap is the card's memory; summing would double-count the
            // small host-visible BAR window some drivers report separately.
            if heap.flags.as_raw() & 0x1 != 0 {
                total = total.max(heap.size);
            }
        }
        (total > 0).then_some(total)
    }
}

#[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "android")))]
fn vulkan_vram_total(_adapter: &wgpu::Adapter) -> Option<u64> {
    None
}

fn init() -> Result<Ctx, String> {
    // Backend selection is automatic (wgpu picks the platform's best:
    // DX12 on Windows, Vulkan on Linux, Metal on macOS), but the
    // standard WGPU_BACKEND env (vulkan|dx12|metal|gl) forces one.
    let backends = std::env::var("WGPU_BACKEND")
        .ok()
        .map(|v| match v.to_lowercase().as_str() {
            "vulkan" | "vk" => wgpu::Backends::VULKAN,
            "dx12" | "d3d12" => wgpu::Backends::DX12,
            "metal" | "mtl" => wgpu::Backends::METAL,
            "gl" | "gles" => wgpu::Backends::GL,
            _ => wgpu::Backends::all(),
        })
        .unwrap_or(wgpu::Backends::all());
    let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
        backends,
        flags: wgpu::InstanceFlags::default(),
        memory_budget_thresholds: Default::default(),
        backend_options: Default::default(),
        display: None,
    });
    let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
        power_preference: wgpu::PowerPreference::HighPerformance,
        force_fallback_adapter: false,
        compatible_surface: None,
        apply_limit_buckets: false,
    }))
    .map_err(|e| format!("no adapter: {e}"))?;

    // Take the card's maximum limits — large tensors (lm_head ≈ 254 MB
    // int8) require a raised storage buffer; a discrete card handles GB.
    let limits = adapter.limits();
    // 33 152 B = the stride-257 attend kernels' workgroup footprint.
    // Adreno/Mali/wgpu-Metal report 32 768 — there only the stride-129
    // (hd <= 128) kernels are created.
    let big_attend = limits.max_compute_workgroup_storage_size >= 33_152;
    let wg_storage = limits.max_compute_workgroup_storage_size;
    // GPU timestamps (CMF_GPU_TS=1): ask for the query features when the
    // adapter has them — the frame profiler below is the only consumer.
    // Two tiers, and conflating them cost the dsv4 profiler its clock on
    // Metal: a timestamp PAIR on a pass descriptor needs TIMESTAMP_QUERY and
    // nothing else, while write_timestamp inside an encoder or a pass needs
    // the other two. Demanding all three meant a device that offers the
    // first got no query set at all — which reads exactly like "the profiler
    // printed nothing", not like "this device cannot do the fine one".
    let ts_basic = wgpu::Features::TIMESTAMP_QUERY;
    let ts_fine_features = wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS
        | wgpu::Features::TIMESTAMP_QUERY_INSIDE_PASSES;
    let have_basic = adapter.features().contains(ts_basic);
    let have_fine = adapter.features().contains(ts_basic | ts_fine_features);
    let ts_features = if have_fine {
        ts_basic | ts_fine_features
    } else {
        ts_basic
    };
    let want_ts = have_basic;
    let want_sg = adapter.features().contains(wgpu::Features::SUBGROUP);
    let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
        label: Some("cortiq-wgpu"),
        required_limits: limits,
        required_features: if want_ts {
            ts_features
        } else {
            wgpu::Features::empty()
        } | if want_sg {
            wgpu::Features::SUBGROUP
        } else {
            wgpu::Features::empty()
        },
        ..Default::default()
    }))
    .map_err(|e| format!("request_device: {e}"))?;
    // Every shader-module and pipeline validation error below must fail
    // init: an invalid pipeline silently turns its dispatches into
    // no-ops and the graph decodes garbage (seen on phones before this
    // scope existed). Err here = clean CPU fallback.
    let vscope = device.push_error_scope(wgpu::ErrorFilter::Validation);

    let info = adapter.get_info();
    let discrete = info.device_type == wgpu::DeviceType::DiscreteGpu;
    let vram_budget = std::env::var("CMF_GPU_VRAM_MB")
        .ok()
        .and_then(|v| v.parse::<u64>().ok())
        .map(|mb| mb * 1024 * 1024)
        .unwrap_or(if discrete {
            // No knob: ask the driver. The Vulkan device-local heap is what
            // the card actually has; a margin of total/64 (1–2 GB) is left
            // to the driver's own bookkeeping. Backends that cannot answer
            // (DX12/GL) keep the conservative default — CMF_GPU_VRAM_MB
            // overrides either way.
            match vulkan_vram_total(&adapter) {
                Some(total) => {
                    let gib = 1024 * 1024 * 1024u64;
                    total - (total / 72).clamp(gib, 2 * gib).min(total / 2)
                }
                None => 8 * 1024 * 1024 * 1024,
            }
        } else {
            u64::MAX // UMA: the OS pages shared memory
        });
    tracing::info!(
        "wgpu GPU path: on ({} / {:?}, {}, weight budget {})",
        info.name,
        info.backend,
        if discrete { "discrete" } else { "uma" },
        if vram_budget == u64::MAX {
            "unlimited".to_string()
        } else {
            format!("{} MB", vram_budget / 1024 / 1024)
        },
    );

    let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("q8"),
        source: wgpu::ShaderSource::Wgsl(WGSL.into()),
    });
    // Auto layout: the bind group layout is inferred from the shader.
    let pipe = |ep: &str| {
        device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some(ep),
            layout: None, // auto: layout is inferred from the shader
            module: &module,
            entry_point: Some(ep),
            compilation_options: Default::default(),
            cache: None,
        })
    };
    let matvec = pipe("q8_matvec");
    let matmat = pipe("q8_matmat");
    let mul_mm = pipe("q8_mul_mm");
    let q1_mm = pipe("q1_mul_mm");
    let silu = pipe("silu_mul_pre");
    let axpy = pipe("axpy");
    let gate_mul = pipe("gate_mul");
    let zero = pipe("fill_zero");
    let q1 = pipe("q1_matvec");
    let q1t = pipe("q1t_matvec");
    let q4b = pipe("q4b_matvec");
    let q4t_mv = pipe("q4t_matvec");
    let rope_heads = pipe("rope_heads");
    let o_lora_a = pipe("o_lora_a");
    let kv_pool = pipe("kv_pool");
    let index_scores = pipe("index_scores");
    let top_k_index = pipe("top_k_index");
    let hc_block = pipe("hc_block");
    let f32_matvec_w = pipe("f32_matvec_w");
    let o_lora_a_w = pipe("o_lora_a_w");
    let f32_matvec_x = pipe("f32_matvec_x");
    let f32_mv_split = pipe("f32_matvec_split");
    let f32_mv_merge = pipe("f32_matvec_merge");
    let o_lora_a_m = pipe("o_lora_a_m");
    let moe_gu_q2tp_m = pipe("moe_gate_up_q2tp_m");
    let moe_dn_q4tp_m = pipe("moe_down_q4tp_m");
    let sa_part = pipe("sparse_attend_part");
    let sa_merge = pipe("sparse_attend_merge");
    let blit = pipe("blit");
    let idx_build = pipe("idx_build");
    let moe_route = pipe("moe_route");
    let sparse_attend = pipe("sparse_attend");
    let sa_scores = pipe("sa_scores");
    let sa_apply = pipe("sa_apply");
    let hc_pre_fold = pipe("hc_pre_fold");
    let hc_post_expand = pipe("hc_post_expand");
    let bt_rope_heads = pipe("bt_rope_heads");
    let bt_hc_pre_fold = pipe("bt_hc_pre_fold");
    let bt_hc_block = pipe("bt_hc_block");
    let bt_comp_append = pipe("bt_comp_append");
    let bt_comp_fold = pipe("bt_comp_fold");
    let bt_hc_post_expand = pipe("bt_hc_post_expand");
    let bt_f32_matvec_w = pipe("bt_f32_matvec_w");
    let bt_f32_matvec_x = pipe("bt_f32_matvec_x");
    let bt_moe_route = pipe("bt_moe_route");
    let bt_moe_gate_up_q2tp = pipe("bt_moe_gate_up_q2tp");
    let bt_moe_gate_up_q2tp_r4 = pipe("bt_moe_gate_up_q2tp_r4");
    let bt_sparse_attend = pipe("bt_sparse_attend");
    let bt_index_scores = pipe("bt_index_scores");
    let bt_top_k = pipe("bt_top_k");
    let bt_idx_build_staged = pipe("bt_idx_build_staged");
    let bt_o_lora_a = pipe("bt_o_lora_a");
    let bt_o_lora_a4 = pipe("bt_o_lora_a4");
    // The staged twin's 4608-float span needs 18 KB and change of
    // workgroup storage; a 16 KB device simply never creates it.
    let bt_o_lora_a2 = (wg_storage >= 19_500).then(|| pipe("bt_o_lora_a2"));
    let q4tp_mv = pipe("q4tp_matvec");
    let q4tp_mv4 = pipe("q4tp_matvec4");
    let use_mv4 = std::env::var("CMF_MV4").map(|v| v != "0").unwrap_or(true);
    let q4tp_mv16 = pipe("q4tp_matvec16");
    let q4t_mv8 = pipe("q4t_matvec8");
    let q4b_mv8 = pipe("q4b_matvec8");
    let q4tp_mm = pipe("q4tp_mul_mm");
    let argmax_part = pipe("argmax_part");
    let gdn_step_par = pipe("gdn_step_par");
    let gdn_step_par2 = pipe("gdn_step_par2");
    let gdn_step_norm2 = pipe("gdn_step_norm2");
    // Measured -1 tok/s on RTX PRO 6000: every dv-workgroup of a head
    // recomputes the conv reads, 128-fold traffic amplification against
    // one saved hop. Kept for narrow-dv models; CMF_GDN_INLINE=1 enables.
    let gdn_inline = std::env::var("CMF_GDN_INLINE").as_deref() == Ok("1");
    let gdn_step_norm = pipe("gdn_step_norm");
    let gdn_par = std::env::var("CMF_GDN_PAR")
        .map(|v| v != "0")
        .unwrap_or(true);
    // Frame profiler (CMF_GPU_TS=1): 256 timestamp slots + resolve/stage
    // buffers. Created only when the device carries the feature.
    let ts_query = if want_ts && matches!(std::env::var("CMF_GPU_TS").as_deref(), Ok("1") | Ok("2"))
    {
        let qs = device.create_query_set(&wgpu::QuerySetDescriptor {
            label: Some("g-ts"),
            ty: wgpu::QueryType::Timestamp,
            count: 256,
        });
        let resolve = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("g-ts-resolve"),
            size: 256 * 8,
            usage: wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });
        let stage = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("g-ts-stage"),
            size: 256 * 8,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        Some((qs, resolve, stage))
    } else {
        None
    };
    let ts_period = queue.get_timestamp_period();
    if std::env::var("CMF_TS_DEBUG").is_ok() {
        eprintln!(
            "[ts] базовый={have_basic} точный={have_fine} набор={} период={ts_period}",
            ts_query.is_some()
        );
    }
    let argmax_final = pipe("argmax_final");
    let embed_gather_q4tp = pipe("embed_gather_q4tp");
    let silu_down = pipe("silu_down_matvec");
    let q1t_mm = pipe("q1t_mul_mm");
    let q4t_mm = pipe("q4t_mul_mm");
    let dit_qk = pipe("dit_qk");
    let dit_pv = pipe("dit_pv");
    let dit_softmax = pipe("dit_softmax");
    let dit_unstack = pipe("dit_unstack");
    let ffn_silu = pipe("ffn_silu_mul");
    let q1t_ovmm = pipe("q1t_overlay_mm");
    let rmsnorm = pipe("rmsnorm");
    let add_rmsnorm = pipe("add_rmsnorm");
    let rmsnorm_b = pipe("rmsnorm_b");
    let add_rmsnorm_b = pipe("add_rmsnorm_b");
    let attn_rope = pipe("attn_rope_qkn");
    let kv_append = pipe("kv_append");
    let gqa_attend_s = pipe("gqa_attend_s");
    // 32 lanes where the workgroup budget allows it, 16 lanes where it does
    // not: both cover head_dim 256, so hd_cap is 256 everywhere now.
    let gqa_attend = if big_attend {
        pipe("gqa_attend")
    } else {
        pipe("gqa_attend_w16")
    };
    let gdn_step = pipe("gdn_step");
    let gdn_conv = pipe("gdn_conv");
    let f32_matvec = pipe("f32_matvec");
    let f32_matvec_b = pipe("f32_matvec_b");
    let layout_f32b = f32_matvec_b.get_bind_group_layout(0);
    let o1_far = pipe("o1_far");
    let o1_push = pipe("o1_push");
    let o1_attend = pipe("o1_attend");
    let layout_o1_far = o1_far.get_bind_group_layout(0);
    let layout_o1_push = o1_push.get_bind_group_layout(0);
    let layout_o1_attend = o1_attend.get_bind_group_layout(0);
    let matvec_pair = pipe("matvec_pair");
    let layout_mv2 = matvec_pair.get_bind_group_layout(0);
    let moe_select = pipe("moe_select");
    let moe_gate_up = pipe("moe_gate_up");
    let moe_down = pipe("moe_down");
    let moe_gate_up_q4tp = pipe("moe_gate_up_q4tp");
    let moe_down_q4tp = pipe("moe_down_q4tp");
    let moe_select_b = pipe("moe_select_b");
    let gdn_conv_k = pipe("gdn_conv_k");
    let q4tp_mv_k = pipe("q4tp_matvec4_k");
    let q4tp_mv16w = pipe("q4tp_matvec16w");
    let gdn_step_par_k = pipe("gdn_step_par_k");
    let gdn_step_norm_k = pipe("gdn_step_norm_k");
    let gdn_step_k = pipe("gdn_step_k");
    let moe_gate_up_q4tp_b = pipe("moe_gate_up_q4tp_b");
    let moe_down_q4tp_b = pipe("moe_down_q4tp_b");
    let moe_down_q4tp_b2 = pipe("moe_down_q4tp_b2");
    let moe_down_q4tp_part = pipe("moe_down_q4tp_part");
    let moe_down_q4tp_b4 = pipe("moe_down_q4tp_b4");
    let moe_down_q2tp_b = pipe("moe_down_q2tp_b");
    let moe_down_q4tp_red = pipe("moe_down_q4tp_red");
    let layout_moe_sel_b = moe_select_b.get_bind_group_layout(0);
    let layout_moe_gu_b = moe_gate_up_q4tp_b.get_bind_group_layout(0);
    let layout_moe_dn_b = moe_down_q4tp_b.get_bind_group_layout(0);
    let layout_moe_sel = moe_select.get_bind_group_layout(0);
    let layout_moe_gu = moe_gate_up.get_bind_group_layout(0);
    let layout_moe_dn = moe_down.get_bind_group_layout(0);
    let layout_moe_gu_q4tp = moe_gate_up_q4tp.get_bind_group_layout(0);
    let moe_gate_up_q2tp = pipe("moe_gate_up_q2tp");
    let moe_gate_up_q2tp_f = pipe("moe_gate_up_q2tp_f");
    let moe_down_q4tp_f = pipe("moe_down_q4tp_f");
    let gqa_attend_dec = pipe("gqa_attend_dec");
    let attend_dec = std::env::var("CMF_ATTEND_DEC")
        .map(|v| v != "0")
        .unwrap_or(true);
    // Measured NEGATIVE on RTX PRO 6000 (72.6 vs 79.0 tok/s): the redundant
    // per-workgroup top-k costs more than the retired select hop — in-pass
    // dispatches overlap more than the latency model assumed. Kept for
    // study; CMF_MOE_FOLDSEL=1 enables.
    let foldsel = std::env::var("CMF_MOE_FOLDSEL").as_deref() == Ok("1");
    let layout_moe_gu_q2tp = moe_gate_up_q2tp.get_bind_group_layout(0);
    let layout_moe_dn_q4tp = moe_down_q4tp.get_bind_group_layout(0);
    let layout = matvec.get_bind_group_layout(0);
    let layout_q1 = q1.get_bind_group_layout(0);
    let layout_rmsnorm = rmsnorm.get_bind_group_layout(0);
    let layout_add_rmsnorm = add_rmsnorm.get_bind_group_layout(0);
    let layout_rmsnorm_b = rmsnorm_b.get_bind_group_layout(0);
    let layout_add_rmsnorm_b = add_rmsnorm_b.get_bind_group_layout(0);
    let layout_attn_rope = attn_rope.get_bind_group_layout(0);
    let layout_kv = kv_append.get_bind_group_layout(0);
    let layout_attend = gqa_attend.get_bind_group_layout(0);
    let layout_attend_s = gqa_attend_s.get_bind_group_layout(0);
    let split_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("cmf-attend-split"),
        source: wgpu::ShaderSource::Wgsl(ATTEND_SPLIT_SRC.into()),
    });
    let pipe_split = |ep: &str| {
        device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some(ep),
            layout: None,
            module: &split_module,
            entry_point: Some(ep),
            compilation_options: Default::default(),
            cache: None,
        })
    };
    let attend_part_s = pipe_split("gqa_attend_part_s");
    let attend_part = if big_attend {
        pipe_split("gqa_attend_part")
    } else {
        attend_part_s.clone()
    };
    let attend_merge = pipe_split("gqa_attend_merge");
    // Subgroup select: its own module — `enable subgroups` must never
    // reach a device without the feature.
    let moe_select_sg = if want_sg && std::env::var("CMF_SELECT_SG").as_deref() != Ok("0") {
        let m = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("cmf-select-sg"),
            source: wgpu::ShaderSource::Wgsl(SELECT_SG_SRC.into()),
        });
        Some(
            device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("moe_select_sg"),
                layout: None,
                module: &m,
                entry_point: Some("moe_select_sg"),
                compilation_options: Default::default(),
                cache: None,
            }),
        )
    } else {
        None
    };
    let layout_attend_part = attend_part.get_bind_group_layout(0);
    let layout_attend_part_s = attend_part_s.get_bind_group_layout(0);
    let layout_attend_merge = attend_merge.get_bind_group_layout(0);
    let layout_gdn = gdn_step.get_bind_group_layout(0);
    let layout_gdn_conv = gdn_conv.get_bind_group_layout(0);
    let layout_f32 = f32_matvec.get_bind_group_layout(0);
    let layout_silu_down = silu_down.get_bind_group_layout(0);
    let layout_mm = matmat.get_bind_group_layout(0);
    let layout_mmm = mul_mm.get_bind_group_layout(0);
    let layout_q1mm = q1_mm.get_bind_group_layout(0);
    let layout_silu = silu.get_bind_group_layout(0);
    let layout_axpy = axpy.get_bind_group_layout(0);
    let layout_gate_mul = gate_mul.get_bind_group_layout(0);
    let layout_zero = zero.get_bind_group_layout(0);

    if let Some(e) = pollster::block_on(vscope.pop()) {
        return Err(format!("wgpu pipeline validation: {e}"));
    }

    Ok(Ctx {
        _instance: instance,
        _adapter: adapter,
        device,
        queue,
        matvec,
        matmat,
        mul_mm,
        q1_mm,
        silu,
        axpy,
        gate_mul,
        zero,
        q1,
        q1t,
        q4b,
        q4t_mv,
        rope_heads,
        o_lora_a,
        kv_pool,
        index_scores,
        top_k_index,
        hc_block,
        f32_matvec_w,
        o_lora_a_w,
        f32_matvec_x,
        f32_mv_split,
        f32_mv_merge,
        o_lora_a_m,
        moe_gu_q2tp_m,
        moe_dn_q4tp_m,
        sa_part,
        sa_merge,
        blit,
        idx_build,
        moe_route,
        sparse_attend,
        sa_scores,
        sa_apply,
        hc_pre_fold,
        hc_post_expand,
        bt_rope_heads,
        bt_hc_pre_fold,
        bt_hc_block,
        bt_comp_append,
        bt_comp_fold,
        bt_hc_post_expand,
        bt_f32_matvec_w,
        bt_f32_matvec_x,
        bt_moe_route,
        bt_moe_gate_up_q2tp,
        bt_moe_gate_up_q2tp_r4,
        bt_sparse_attend,
        bt_index_scores,
        bt_top_k,
        bt_idx_build_staged,
        bt_o_lora_a,
        bt_o_lora_a4,
        bt_o_lora_a2,
        q4tp_mv,
        q4tp_mv4,
        use_mv4,
        q4tp_mv16,
        q4t_mv8,
        q4b_mv8,
        q4tp_mm,
        argmax_part,
        gdn_step_par,
        gdn_step_par2,
        gdn_step_norm2,
        gdn_inline,
        gdn_step_norm,
        gdn_par,
        ts_query,
        ts_period,
        argmax_final,
        embed_gather_q4tp,
        silu_down,
        q1t_mm,
        q4t_mm,
        dit_qk,
        dit_pv,
        dit_softmax,
        dit_unstack,
        ffn_silu,
        q1t_ovmm,
        rmsnorm,
        add_rmsnorm,
        rmsnorm_b,
        add_rmsnorm_b,
        attn_rope,
        kv_append,
        gqa_attend,
        gqa_attend_s,
        attend_part,
        attend_part_s,
        attend_merge,
        hd_cap: 256,
        big_attend,
        gdn_step,
        gdn_conv,
        f32_matvec,
        f32_matvec_b,
        layout_f32b,
        o1_far,
        o1_push,
        o1_attend,
        layout_o1_far,
        layout_o1_push,
        layout_o1_attend,
        o1m: Mutex::new(HashMap::new()),
        matvec_pair,
        layout_mv2,
        moe_select,
        moe_gate_up,
        moe_down,
        moe_gate_up_q4tp,
        moe_down_q4tp,
        moe_select_b,
        gdn_conv_k,
        q4tp_mv_k,
        q4tp_mv16w,
        gdn_step_par_k,
        gdn_step_norm_k,
        gdn_step_k,
        moe_gate_up_q4tp_b,
        moe_down_q4tp_b,
        moe_down_q4tp_b2,
        moe_down_q4tp_part,
        moe_down_q4tp_b4,
        moe_down_q2tp_b,
        moe_down_q4tp_red,
        layout_moe_sel_b,
        layout_moe_gu_b,
        layout_moe_dn_b,
        layout,
        layout_mm,
        layout_mmm,
        layout_q1mm,
        layout_silu,
        layout_axpy,
        layout_gate_mul,
        layout_zero,
        layout_q1,
        layout_rmsnorm,
        layout_add_rmsnorm,
        layout_rmsnorm_b,
        layout_add_rmsnorm_b,
        layout_attn_rope,
        layout_kv,
        layout_attend,
        layout_attend_s,
        layout_attend_part,
        layout_attend_part_s,
        layout_attend_merge,
        layout_gdn,
        layout_gdn_conv,
        layout_f32,
        layout_silu_down,
        layout_moe_sel,
        layout_moe_gu,
        layout_moe_dn,
        layout_moe_gu_q4tp,
        moe_gate_up_q2tp,
        moe_gate_up_q2tp_f,
        moe_down_q4tp_f,
        gqa_attend_dec,
        moe_select_sg,
        attend_dec,
        foldsel,
        layout_moe_gu_q2tp,
        layout_moe_dn_q4tp,
        discrete,
        vram_budget,
        resident: std::sync::atomic::AtomicU64::new(0),
        scratch: Mutex::new(Scratch::default()),
        weight_bufs: Mutex::new(HashMap::new()),
        dsv4_kv: Mutex::new(HashMap::new()),
        dsv4_scratch: Mutex::new(HashMap::new()),
        dsv4_comp: Mutex::new(HashMap::new()),
        dsv4_ixkv: Mutex::new(HashMap::new()),
        dsv4_uni: Mutex::new(HashMap::new()),
        dsv4_store: Mutex::new(HashMap::new()),
        slot_writes: Mutex::new(HashMap::new()),
        dsv4_binds: Mutex::new((0, HashMap::new())),
        res_clock: std::sync::atomic::AtomicU64::new(0),
        uniforms: Mutex::new(HashMap::new()),
        uniforms8: Mutex::new(HashMap::new()),
        const_bufs: Mutex::new(HashMap::new()),
        rs_bufs: Mutex::new(HashMap::new()),
        attn_kv: Mutex::new(HashMap::new()),
        gdn_state: Mutex::new(HashMap::new()),
        gdn_snap: Mutex::new(HashMap::new()),
        moe_expw: Mutex::new(HashMap::new()),
        graph_scratch: Mutex::new(GraphScratch::default()),
    })
}

/// Is the active adapter a discrete card? (facade: threshold policy)
pub fn is_discrete() -> bool {
    ctx().map(|c| c.discrete).unwrap_or(false)
}

/// Resident quant weights of the WHOLE tensor in VRAM (loaded once per
/// (file, idx)), guarded by the VRAM budget: once the budget is spent,
/// new tensors return None and their ops run on the CPU. Decode touches
/// layers in order, so the resident set is deterministically the first
/// layers — ngl-style offload without configuration.
/// One tensor living on the device, with what the eviction policy needs.
struct Resident {
    buf: wgpu::Buffer,
    bytes: u64,
    /// Never evict. Set for the weights of layers the decode loop has
    /// committed to running on the card: their caches live there, so losing
    /// one mid-sequence is not a slower token but a refused one, and at a
    /// budget near the working set that happened every token.
    pinned: bool,
    /// Use count, aged lazily: the score at time `t` is `uses ·
    /// DECAY^(t − last)`. Plain frequency ossifies — an expert that was
    /// popular during the first prompt outranks one being used right now,
    /// forever.
    uses: f32,
    last: u64,
}

/// Per-tick multiplier for the aged use count. 0.999 halves a score over
/// ~700 lookups: long enough that a steady working set is never disturbed,
/// short enough that a change of task migrates within a prompt or two.
const RES_DECAY: f32 = 0.999;

/// A tensor touched within this many lookups is not evicted, whatever its
/// score. Without it a budget slightly smaller than the working set evicts
/// and re-uploads on every token, which is slower than never using the
/// device at all.
const RES_HYSTERESIS: u64 = 512;

fn res_score(e: &Resident, now: u64) -> f32 {
    e.uses * RES_DECAY.powi((now.saturating_sub(e.last)).min(4096) as i32)
}

/// Pin a layer's weights on the card for the rest of the sequence.
///
/// The residency cache evicts by score to make room, which is right while
/// the set is still being chosen and wrong the moment the decode loop has
/// committed to a set: an evicted layer drops off the card, its caches stay
/// there, and the loop refuses the whole fast path rather than read state
/// from two sides. On a budget near the working set that fired every token —
/// 125 times in a 48-token run on an emulated 24 GB card.
pub fn pin_weights(model: &Arc<CmfModel>, idxs: &[usize]) -> usize {
    let Some(c) = ctx() else { return 0 };
    let uid = model.uid() as usize;
    let mut map = c.weight_bufs.lock().unwrap();
    let mut n = 0;
    for &i in idxs {
        if let Some(e) = map.get_mut(&(uid, i)) {
            if !e.pinned {
                e.pinned = true;
                n += 1;
            }
        }
    }
    n
}

fn weight_buffer(c: &Ctx, key: (usize, usize), full_quant: &[u8]) -> Option<wgpu::Buffer> {
    use std::sync::atomic::Ordering;
    let now = c.res_clock.fetch_add(1, Ordering::Relaxed);
    let mut map = c.weight_bufs.lock().unwrap();
    if let Some(e) = map.get_mut(&key) {
        e.uses = res_score(e, now) + 1.0;
        e.last = now;
        return Some(e.buf.clone());
    }
    let len = full_quant.len() as u64;
    if len > c.vram_budget {
        return None; // one tensor larger than the whole budget
    }
    // Make room by evicting the least valuable, skipping anything touched
    // recently. Failing to free enough is not an error: the tensor stays on
    // the CPU, which is the pressure valve that keeps a too-small budget
    // from thrashing the bus.
    if c.resident.load(Ordering::Relaxed) + len > c.vram_budget {
        let mut cand: Vec<((usize, usize), f32, u64)> = map
            .iter()
            .filter(|(_, e)| !e.pinned && now.saturating_sub(e.last) > RES_HYSTERESIS)
            .map(|(k, e)| (*k, res_score(e, now), e.bytes))
            .collect();
        cand.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        let mut freed = 0u64;
        for (k, _, bytes) in cand {
            if c.resident.load(Ordering::Relaxed) - freed + len <= c.vram_budget {
                break;
            }
            map.remove(&k);
            freed += bytes;
        }
        if freed > 0 {
            c.resident.fetch_sub(freed, Ordering::Relaxed);
            crate::gpu::probe_note_cold();
        }
        if c.resident.load(Ordering::Relaxed) + len > c.vram_budget {
            return None; // still no room — honest CPU
        }
    }
    crate::gpu::probe_note_cold(); // first touch = upload, not a steady sample
    // DEVICE-LOCAL residency: create_buffer_init maps at creation → the buffer
    // lands in a HOST_VISIBLE heap and every matvec streams its weights over
    // PCIe (~25 GB/s) every token. A plain create_buffer + staged write_buffer
    // lets the allocator pick DEVICE_LOCAL VRAM (~1 TB/s on a 4090). This is
    // THE discrete-GPU decode fix; on UMA it's a wash.
    let buf = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("q1-weights"),
        // Rounded up: write_buffer refuses a size that is not a multiple of
        // four, and a small q4tp payload need not be one. This only ever
        // surfaced once the preparation's weights joined the preflight —
        // until then every tensor through here happened to be aligned.
        size: len.next_multiple_of(4),
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    let t_up = std::time::Instant::now();
    if upload_staged() {
        // A staging buffer we map ourselves, then one copy. `queue.write_buffer`
        // goes through wgpu's belt, which for a 92 GB expert stack measured
        // ~48 MB/s on Vulkan — half an hour before the first token. This path
        // memcpys straight into a mapped host-visible buffer and issues the
        // device copy itself.
        let n = len.next_multiple_of(4);
        let stg = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("weight-staging"),
            size: n,
            usage: wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: true,
        });
        {
            let Ok(mut view) = stg.slice(..).get_mapped_range_mut() else {
                return None;
            };
            // Write-only by construction: mapped upload memory may be
            // uncached, so wgpu hands out a slice you may write but not read.
            view.slice(..full_quant.len()).copy_from_slice(full_quant);
            if full_quant.len() < n as usize {
                view.slice(full_quant.len()..).fill(0);
            }
        }
        stg.unmap();
        let mut enc = c
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("wup") });
        enc.copy_buffer_to_buffer(&stg, 0, &buf, 0, n);
        submit(c, enc.finish());
        // The staging buffer must outlive the copy; polling here also keeps
        // the peak at one tensor rather than the whole stack.
        let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
    } else if full_quant.len() % 4 == 0 {
        c.queue.write_buffer(&buf, 0, full_quant);
    } else {
        let mut padded = full_quant.to_vec();
        padded.resize(padded.len().next_multiple_of(4), 0);
        c.queue.write_buffer(&buf, 0, &padded);
    }
    UPLOAD_NS.fetch_add(t_up.elapsed().as_nanos() as u64, Ordering::Relaxed);
    UPLOAD_BYTES.fetch_add(len, Ordering::Relaxed);
    c.resident.fetch_add(len, Ordering::Relaxed);
    map.insert(
        key,
        Resident {
            buf: buf.clone(),
            bytes: len,
            uses: 1.0,
            last: now,
            pinned: false,
        },
    );
    Some(buf)
}

/// One MoE layer's expert weights as three concatenated device buffers
/// (gate_all, up_all, down_all), q4t payloads back to back in `experts`
/// order (routed experts then the shared one) — the kernels address
/// expert e at u16 offset e·mat16. Uploaded once per layer (keyed by the
/// first gate idx), budget-guarded like every resident weight; the copy
/// walks the per-tensor directory, so no file-order contiguity is assumed.
/// One-shot reason the whole-token graph declined. Without it the fallback
/// to the per-op path is invisible, which is how a q4tp model looked
/// GPU-accelerated while every layer walked the host.
/// `CMF_GRAPH_SPLIT=N` — how many pieces the token graph's submission is
/// cut into (default 10, the sweep's plateau: 2→110.4, 6→119.9,
/// 10-13→122.3, 40→120.1 on the 35B against 97.9 unsplit; 0/1 = the
/// historical single submit).
fn graph_split_n() -> usize {
    static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *N.get_or_init(|| {
        std::env::var("CMF_GRAPH_SPLIT")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(10)
    })
}

fn graph_refused(why: &'static str) {
    use std::sync::atomic::{AtomicBool, Ordering};
    static SAID: AtomicBool = AtomicBool::new(false);
    if !SAID.swap(true, Ordering::Relaxed) {
        tracing::warn!("wgpu token graph declined: {why}");
    }
}

/// Device bytes one layer's expert stack wants (gate + up + down, all
/// experts). The builder asks BEFORE uploading to decide where the device
/// prefix ends; `moe_expert_bufs` uses the same arithmetic for its budget
/// refusal, so the two never disagree.
fn moe_pack_bytes(
    n_experts: usize,
    inter: usize,
    hidden: usize,
    q4tp: bool,
    gu_q2: bool,
    dn_q2: bool,
) -> Option<u64> {
    let plen = |rows: usize, cols: usize| -> Option<usize> {
        if q4tp {
            cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q4TiledP, &[rows, cols])
        } else {
            Some(rows * (cols / 32) * 18)
        }
    };
    let gu_len = if gu_q2 {
        cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q2TiledP, &[inter, hidden])?
    } else {
        plen(inter, hidden)?
    };
    let d_len = if dn_q2 {
        cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q2TiledP, &[hidden, inter])?
    } else {
        plen(hidden, inter)?
    };
    Some((n_experts * (2 * gu_len + d_len)) as u64)
}

fn moe_expert_bufs(
    c: &Ctx,
    model: &Arc<CmfModel>,
    experts: &[(usize, usize, usize)],
    inter: usize,
    hidden: usize,
    q4tp: bool,
    gu_q2: bool,
    dn_q2: bool,
) -> Option<(wgpu::Buffer, wgpu::Buffer, wgpu::Buffer)> {
    use std::sync::atomic::Ordering;
    if hidden % 32 != 0 || inter % 32 != 0 {
        graph_refused("moe_expert_bufs: hidden/inter not 32-aligned");
        return None;
    }
    let bytes = model.primary_bytes();
    let key = (model.uid() as usize, experts.first()?.0);
    if let Some(t) = c.moe_expw.lock().unwrap().get(&key) {
        return Some(t.clone());
    }
    // q4t is 18 B a group flat; q4tp is 16 B of nibbles plus the row
    // params and 5-bit code planes, which the format's own accessor sizes.
    let plen = |rows: usize, cols: usize| -> Option<usize> {
        if q4tp {
            cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q4TiledP, &[rows, cols])
        } else {
            Some(rows * (cols / 32) * 18)
        }
    };
    let gu_len = if gu_q2 {
        cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q2TiledP, &[inter, hidden])?
    } else {
        plen(inter, hidden)?
    };
    let d_len = if dn_q2 {
        cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q2TiledP, &[hidden, inter])?
    } else {
        plen(hidden, inter)?
    };
    let total = (experts.len() * (2 * gu_len + d_len)) as u64;
    if c.resident.load(Ordering::Relaxed) + total > c.vram_budget {
        // Over budget — the whole graph falls to CPU. Say so ONCE with the
        // numbers: the default budget is a conservative 8 GB on discrete
        // cards, so a 32 GB card running a big MoE lands here and every
        // expert quietly walks the host. That refusal used to be silent.
        use std::sync::atomic::AtomicBool;
        static SAID: AtomicBool = AtomicBool::new(false);
        if !SAID.swap(true, Ordering::Relaxed) {
            let mb = |b: u64| b / (1024 * 1024);
            tracing::warn!(
                "MoE experts need {} MB for this layer on top of {} MB resident, \
                 over the {} MB weight budget — the whole-token graph falls back to \
                 the CPU. Raise it with CMF_GPU_VRAM_MB (e.g. {} on this card).",
                mb(total),
                mb(c.resident.load(Ordering::Relaxed)),
                mb(c.vram_budget),
                mb(c.vram_budget) * 3,
            );
        }
        return None;
    }
    crate::gpu::probe_note_cold();
    // Every byte range this layer ships to the card, for the post-upload
    // evict below.
    let uploaded = std::cell::RefCell::new(Vec::<(usize, usize)>::new());
    let mk = |role: &dyn Fn(&(usize, usize, usize)) -> usize,
              rows: usize,
              cols: usize,
              plen: usize|
     -> Option<wgpu::Buffer> {
        // Check every expert BEFORE allocating: a shape mismatch found
        // halfway used to leave a gigabyte-scale buffer behind.
        let mut offs = Vec::with_capacity(experts.len());
        for t in experts {
            let e = model.tensors.get(role(t))?;
            if *e.shape.first()? as usize != rows
                || *e.shape.get(1)? as usize != cols
                || e.nbytes as usize != plen
            {
                return None;
            }
            let abs = model.entry_abs_offset(e)?;
            bytes.get(abs..abs + plen)?; // in range
            offs.push(abs);
        }
        let b = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("moe-experts"),
            size: (experts.len() * plen) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        // Straight from the mapping to the queue, expert by expert. Gathering
        // them into one Vec first meant a 2.2 GB allocation and a full extra
        // memcpy PER LAYER — 94 GB of pointless copying across the release,
        // on top of the 94 GB that has to move anyway.
        // Counted, like every other upload. The rate the profile printed —
        // 7850 MB/s — was measured over the SKELETON only, because the
        // expert stack is ninety per cent of the bytes and goes through
        // this loop rather than through `weight_buffer`.
        let t_up = std::time::Instant::now();
        for (i, &abs) in offs.iter().enumerate() {
            c.queue
                .write_buffer(&b, (i * plen) as u64, &bytes[abs..abs + plen]);
        }
        // Bound transient memory by ONE projection, not a whole expert
        // pack. Near the VRAM limit, keeping gate + up + down staging alive
        // together can OOM even though their final device buffers fit.
        c.queue.submit(std::iter::empty());
        let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
        UPLOAD_NS.fetch_add(t_up.elapsed().as_nanos() as u64, Ordering::Relaxed);
        UPLOAD_BYTES.fetch_add((offs.len() * plen) as u64, Ordering::Relaxed);
        uploaded.borrow_mut().extend(offs.iter().map(|&a| (a, plen)));
        Some(b)
    };
    let (g, u, d) = match (
        mk(&|t| t.0, inter, hidden, gu_len),
        mk(&|t| t.1, inter, hidden, gu_len),
        mk(&|t| t.2, hidden, inter, d_len),
    ) {
        (Some(g), Some(u), Some(d)) => (g, u, d),
        _ => {
            graph_refused("moe_expert_bufs: expert tensor shape/nbytes mismatch");
            return None;
        }
    };
    // Flush the write_buffer staging belt NOW: with 40 MoE layers the
    // pending uploads (~17 GB) would otherwise coexist with their device
    // copies until the graph's first submit — twice the expert weights in
    // memory = device OOM on discrete cards. One submit+wait per layer
    // bounds transient staging to this layer's three buffers.
    c.queue.submit(std::iter::empty());
    let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
    // The card holds these bytes now, so the host copy is dead weight: the
    // page cache otherwise keeps every resident expert a second time, and on
    // a 112 GB model that second copy IS the machine's RAM (measured: 172 of
    // 176 GB cached).
    //
    // Alternated off/on/off/on from a warmed file, 256 tokens each:
    //
    //   off  94 GB resident  1.0 tok/s      off  91 GB  0.5 tok/s
    //   on    5 GB resident  6.9 tok/s      on    5 GB  6.1 tok/s
    //
    // It is not a trade — holding a second copy of the weights is what was
    // costing the speed. At 94 GB resident the machine has nothing left for
    // the pages it does need, and reclaim churns; at 5 GB it does not.
    //
    // Pairs with `open()` skipping its whole-file `WillNeed` when this is on:
    // reading 104 GB ahead only to drop it behind the uploader had the kernel
    // fetching the same bytes twice. `CMF_UPLOAD_EVICT=0` opts out; discrete
    // only, as on UMA the mapping IS the working copy.
    if c.discrete
        && std::env::var("CMF_UPLOAD_EVICT")
            .map(|v| v != "0")
            .unwrap_or(true)
    {
        model.evict_ranges(&uploaded.borrow());
    }
    c.resident.fetch_add(total, Ordering::Relaxed);
    c.moe_expw
        .lock()
        .unwrap()
        .insert(key, (g.clone(), u.clone(), d.clone()));
    Some((g, u, d))
}

/// The draft pack's expert upload with gate/up requantized q4tp → q2tp on
/// the way through — via the encoder the binary registered. The down
/// projection stays q4tp (the q2tp down kernel does not exist). Cached
/// under the same first-gate key as every pack; only the draft reaches
/// these tensors, so the variant is unambiguous per process.
pub fn moe_expert_bufs_requant_gu(
    model: &Arc<CmfModel>,
    experts: &[(usize, usize, usize)],
    inter: usize,
    hidden: usize,
) -> Option<(wgpu::Buffer, wgpu::Buffer, wgpu::Buffer)> {
    use std::sync::atomic::Ordering;
    let c = ctx()?;
    let enc2 = *crate::dsv4::DSPARK_Q2TP_ENCODE.get()?;
    if hidden % 32 != 0 || inter % 32 != 0 {
        return None;
    }
    let bytes = model.primary_bytes();
    let key = (model.uid() as usize, experts.first()?.0);
    if let Some(t) = c.moe_expw.lock().unwrap().get(&key) {
        return Some(t.clone());
    }
    let gu_len =
        cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q2TiledP, &[inter, hidden])?;
    let d_len =
        cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q4TiledP, &[hidden, inter])?;
    let total = (experts.len() * (2 * gu_len + d_len)) as u64;
    if c.resident.load(Ordering::Relaxed) + total > c.vram_budget {
        return None;
    }
    let t0 = std::time::Instant::now();
    let mut vals = vec![0.0f32; inter * hidden];
    let mk_gu = |role: &dyn Fn(&(usize, usize, usize)) -> usize,
                 vals: &mut Vec<f32>|
     -> Option<wgpu::Buffer> {
        let b = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("dspark-experts-q2"),
            size: (experts.len() * gu_len) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        for (i, t) in experts.iter().enumerate() {
            let e = model.tensors.get(role(t))?;
            if e.dtype != cortiq_core::TensorDtype::Q4TiledP
                || e.shape != [inter, hidden]
            {
                return None;
            }
            let abs = model.entry_abs_offset(e)?;
            let src = bytes.get(abs..abs + e.nbytes as usize)?;
            cortiq_core::quant::dequant_q4tp(src, inter, hidden, vals);
            let q2 = enc2(vals, inter, hidden);
            if q2.len() != gu_len {
                return None;
            }
            c.queue.write_buffer(&b, (i * gu_len) as u64, &q2);
        }
        c.queue.submit(std::iter::empty());
        let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
        Some(b)
    };
    let mk_d = || -> Option<wgpu::Buffer> {
        let b = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("dspark-experts-dn"),
            size: (experts.len() * d_len) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        for (i, t) in experts.iter().enumerate() {
            let e = model.tensors.get(t.2)?;
            if e.dtype != cortiq_core::TensorDtype::Q4TiledP
                || e.nbytes as usize != d_len
            {
                return None;
            }
            let abs = model.entry_abs_offset(e)?;
            c.queue
                .write_buffer(&b, (i * d_len) as u64, bytes.get(abs..abs + d_len)?);
        }
        c.queue.submit(std::iter::empty());
        let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
        Some(b)
    };
    let (g, u, d) = match (mk_gu(&|t| t.0, &mut vals), mk_gu(&|t| t.1, &mut vals), mk_d()) {
        (Some(g), Some(u), Some(d)) => (g, u, d),
        _ => return None,
    };
    tracing::info!(
        "DSpark: реквант q4tp→q2tp {} экспертов за {:.1} с ({} МБ на карте)",
        experts.len(),
        t0.elapsed().as_secs_f64(),
        total / (1024 * 1024),
    );
    c.resident.fetch_add(total, Ordering::Relaxed);
    c.moe_expw
        .lock()
        .unwrap()
        .insert(key, (g.clone(), u.clone(), d.clone()));
    Some((g, u, d))
}

/// GPU enabled and initialized?
pub fn enabled() -> bool {
    ctx().is_some()
}

/// Probe helper: true — tensor `idx`'s weights are already resident;
/// false — not yet (with `may_upload`, the upload happens NOW within the
/// budget, without a dispatch, so the next touch is warm) or the tensor
/// can't be resolved.
pub fn q8_resident_or_upload(model: &Arc<CmfModel>, idx: usize, may_upload: bool) -> bool {
    let Some(c) = ctx() else { return false };
    let entry = &model.tensors[idx];
    let rows_total = entry.shape.first().copied().unwrap_or(0);
    let cols = entry.shape.get(1).copied().unwrap_or(0);
    if rows_total == 0 || cols == 0 {
        return false;
    }
    let Some(abs) = model.entry_abs_offset(entry) else {
        return false;
    };
    let bytes = model.primary_bytes();
    if abs + rows_total * cols > bytes.len() {
        return false;
    }
    let key = (model.uid() as usize, idx);
    if c.weight_bufs.lock().unwrap().contains_key(&key) {
        return true;
    }
    if may_upload {
        let _ = weight_buffer(c, key, &bytes[abs..abs + rows_total * cols]);
    }
    false
}

/// q8_row/q8_2f matvec on the GPU, rows [row0, row0+rows). `xs` are already
/// prescaled activations. false = could not (the caller falls back to CPU).
#[allow(clippy::too_many_arguments)]
pub fn q8_matvec_range(
    model: &Arc<CmfModel>,
    idx: usize,
    row0: usize,
    row_scale: &[f32],
    xs: &[f32],
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if cols % 4 != 0 || rows == 0 {
        return false;
    }
    let entry = &model.tensors[idx];
    let rows_total = entry.shape.first().copied().unwrap_or(0);
    if rows_total < row0 + rows {
        return false;
    }
    let Some(abs) = model.entry_abs_offset(entry) else {
        return false; // neighboring shard — different mapping; CPU
    };
    let bytes = model.primary_bytes();
    if abs + rows_total * cols > bytes.len() {
        return false;
    }
    let full_quant = &bytes[abs..abs + rows_total * cols];
    let key = (model.uid() as usize, idx);
    dispatch_matvec(
        c,
        Some(key),
        full_quant,
        row0,
        row_scale,
        xs,
        rows,
        cols,
        out,
    )
}

/// matvec kernel: resident weights of the WHOLE tensor + row0 offset, rs, xs,
/// dispatch, readback. `weight_key = None` — no cache (test).
#[allow(clippy::too_many_arguments)]
fn dispatch_matvec(
    c: &Ctx,
    weight_key: Option<(usize, usize)>,
    full_quant: &[u8],
    row0: usize,
    row_scale: &[f32],
    xs: &[f32],
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    if row_scale.len() < rows || xs.len() < cols || full_quant.len() < (row0 + rows) * cols {
        return false;
    }
    let q_buf = match weight_key {
        Some(k) => match weight_buffer(c, k, full_quant) {
            Some(b) => b,
            None => return false, // over VRAM budget — honest CPU path
        },
        None => c
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("q8-weights"),
                contents: full_quant,
                usage: wgpu::BufferUsages::STORAGE,
            }),
    };
    let make_rs = || {
        c.device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("q8-rs"),
                contents: bytemuck::cast_slice(&row_scale[..rows]),
                usage: wgpu::BufferUsages::STORAGE,
            })
    };
    let rs_buf = match weight_key {
        Some((base, idx)) => c
            .rs_bufs
            .lock()
            .unwrap()
            .entry((base ^ idx.wrapping_mul(1_000_003), row0))
            .or_insert_with(|| {
                crate::gpu::probe_note_cold();
                make_rs()
            })
            .clone(),
        None => make_rs(),
    };

    // Pooled scratch for the whole op (encode → submit → poll).
    let mut sc = c.scratch.lock().unwrap();
    let xs_buf = Scratch::ensure(
        &c.device,
        &mut sc.xs,
        (cols * 4) as u64,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        "q8-xs",
    );
    c.queue
        .write_buffer(&xs_buf, 0, bytemuck::cast_slice(&xs[..cols]));
    let y_size = (rows * 4) as u64;
    let y_buf = Scratch::ensure(
        &c.device,
        &mut sc.y,
        y_size,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        "q8-y",
    );
    let params = [
        (cols / 4) as u32,
        rows as u32,
        (row0 * cols / 4) as u32,
        0u32,
    ];
    let p_buf = match &sc.params {
        Some(b) => b.clone(),
        None => {
            let b = c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("q8-params"),
                size: 16,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            sc.params = Some(b.clone());
            b
        }
    };
    c.queue
        .write_buffer(&p_buf, 0, bytemuck::cast_slice(&params));
    let stage_buf = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        y_size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "q8-stage",
    );

    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("q8-bg"),
        layout: &c.layout,
        entries: &[
            bind_buf(0, &q_buf),
            bind_buf(1, &xs_buf),
            bind_buf(2, &rs_buf),
            bind_buf(3, &y_buf),
            bind_buf(4, &p_buf),
        ],
    });

    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("q8") });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("q8"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.matvec);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1); // grid-stride over rows
    }
    let ok = readback(c, enc, &y_buf, &stage_buf, y_size, &mut out[..rows]);
    drop(sc);
    ok
}

/// q1t (base+overlay) / q4_block matvec on wgpu — raw f32 x, scales embedded.
/// The kernel decodes bytes out of the u32 weight buffer; params carry
/// (gpr, rows, cols). Weights resident under the shared VRAM budget.
pub fn q1t_matvec(
    model: &Arc<CmfModel>,
    idx: usize,
    xs: &[f32],
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    q1t_like(model, idx, xs, rows, cols, out, false)
}

/// q4_block matvec on wgpu (nibbles + trailing scales, no overlay).
pub fn q4b_matvec(
    model: &Arc<CmfModel>,
    idx: usize,
    xs: &[f32],
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    q1t_like(model, idx, xs, rows, cols, out, true)
}

fn q1t_like(
    model: &Arc<CmfModel>,
    idx: usize,
    xs: &[f32],
    rows: usize,
    cols: usize,
    out: &mut [f32],
    q4: bool,
) -> bool {
    let Some(c) = ctx() else { return false };
    let gpr = cols / 32;
    if rows == 0 || cols % 32 != 0 || xs.len() < cols || out.len() < rows {
        return false;
    }
    let entry = &model.tensors[idx];
    if entry.shape.first().copied().unwrap_or(0) < rows {
        return false;
    }
    let Some(abs) = model.entry_abs_offset(entry) else {
        return false;
    };
    let bytes = model.primary_bytes();
    let plen = entry.nbytes as usize;
    // sanity: the base must at least fit (q1t base 9 B/group, q4b 18 B/group).
    let min_base = if q4 { rows * gpr * 18 } else { rows * gpr * 9 };
    if plen < min_base || abs + plen > bytes.len() {
        return false;
    }
    let pipeline = if q4 { &c.q4b } else { &c.q1t };
    dispatch_q1t(
        c,
        pipeline,
        Some((model.uid() as usize, idx)),
        &bytes[abs..abs + plen],
        xs,
        rows,
        cols,
        out,
    )
}

#[allow(clippy::too_many_arguments)]
fn dispatch_q1t(
    c: &Ctx,
    pipeline: &wgpu::ComputePipeline,
    weight_key: Option<(usize, usize)>,
    payload: &[u8],
    xs: &[f32],
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    let gpr = cols / 32;
    let q_buf = match weight_key {
        Some(k) => match weight_buffer(c, k, payload) {
            Some(b) => b,
            None => return false,
        },
        None => c
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("q1t-weights"),
                contents: payload,
                usage: wgpu::BufferUsages::STORAGE,
            }),
    };
    let mut sc = c.scratch.lock().unwrap();
    let xs_buf = Scratch::ensure(
        &c.device,
        &mut sc.xs,
        (cols * 4) as u64,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        "q1t-xs",
    );
    c.queue
        .write_buffer(&xs_buf, 0, bytemuck::cast_slice(&xs[..cols]));
    let y_size = (rows * 4) as u64;
    let y_buf = Scratch::ensure(
        &c.device,
        &mut sc.y,
        y_size,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        "q1t-y",
    );
    let params = [gpr as u32, rows as u32, cols as u32, 0u32];
    let p_buf = match &sc.params {
        Some(b) => b.clone(),
        None => {
            let b = c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("q1t-params"),
                size: 16,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            sc.params = Some(b.clone());
            b
        }
    };
    c.queue
        .write_buffer(&p_buf, 0, bytemuck::cast_slice(&params));
    let stage_buf = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        y_size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "q1t-stage",
    );
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("q1t-bg"),
        // Must be THIS pipeline's layout (wgpu treats each pipeline's layout as
        // distinct even when structurally identical to q1's).
        layout: &pipeline.get_bind_group_layout(0),
        entries: &[
            bind_buf(0, &q_buf),
            bind_buf(1, &xs_buf),
            bind_buf(2, &y_buf),
            bind_buf(3, &p_buf),
        ],
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("q1t") });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("q1t"),
            timestamp_writes: None,
        });
        pass.set_pipeline(pipeline);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1);
    }
    let ok = readback(c, enc, &y_buf, &stage_buf, y_size, &mut out[..rows]);
    drop(sc);
    ok
}

/// q1 matvec: raw f32 activations, tile-embedded scales (no rs buffer).
/// Weights resident under the same VRAM budget as q8; false = CPU path.
pub fn q1_matvec(
    model: &Arc<CmfModel>,
    idx: usize,
    xs: &[f32],
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let gpr = cols / 32;
    if rows == 0 || cols % 32 != 0 || gpr % 2 != 0 || xs.len() < cols || out.len() < rows {
        return false;
    }
    let entry = &model.tensors[idx];
    if entry.shape.first().copied().unwrap_or(0) < rows {
        return false;
    }
    let Some(abs) = model.entry_abs_offset(entry) else {
        return false;
    };
    let bytes = model.primary_bytes();
    let plen = rows * gpr * 6;
    if abs + plen > bytes.len() {
        return false;
    }
    dispatch_q1(
        c,
        Some((model.uid() as usize, idx)),
        &bytes[abs..abs + plen],
        xs,
        rows,
        cols,
        out,
    )
}

/// GPU RMSNorm of one row — the token-graph building block that keeps the
/// hidden state resident across the norm→matvec boundary. One workgroup,
/// direct buffers (no residency cache). Returns false without a GPU context.
pub fn rmsnorm_row(x: &[f32], w: &[f32], out: &mut [f32], gemma: bool, eps: f32) -> bool {
    let Some(c) = ctx() else { return false };
    let n = x.len();
    if n == 0 || w.len() < n || out.len() < n {
        return false;
    }
    let x_b = storage_bytes(c, bytemuck::cast_slice(x));
    let w_b = storage_bytes(c, bytemuck::cast_slice(&w[..n]));
    let o_b = rw_f32(c, n, true);
    let p_buf = uniform_u32x4(c, [n as u32, gemma as u32, eps.to_bits(), 0]);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("rms-bg"),
        layout: &c.layout_rmsnorm,
        entries: &[
            bind_buf(0, &x_b),
            bind_buf(1, &w_b),
            bind_buf(2, &o_b),
            bind_buf(3, &p_buf),
        ],
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("rms") });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("rms"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.rmsnorm);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(1, 1, 1);
    }
    let size = (n * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "rms-stage",
    );
    let ok = readback(c, enc, &o_b, &stage, size, &mut out[..n]);
    drop(sc);
    ok
}

/// GPU RoPE + qk-norm + gate-split building block (bring-up / parity). One
/// workgroup per head; writes qout[nh·hd], k in place[nkv·hd], gout[nh·hd].
/// qnw/knw must be hd-long (dummy ok if the norm flag is off), invf rd/2-long.
#[allow(clippy::too_many_arguments)]
pub fn attn_rope_qkn_gpu(
    qraw: &[f32],
    k_in: &[f32],
    qnw: &[f32],
    knw: &[f32],
    invf: &[f32],
    nh: usize,
    nkv: usize,
    hd: usize,
    rd: usize,
    pos: usize,
    flags: u32,
    eps: f32,
    qout: &mut [f32],
    k_out: &mut [f32],
    gout: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let qraw_b = storage_bytes(c, bytemuck::cast_slice(qraw));
    let k_b = c
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("rq-k"),
            contents: bytemuck::cast_slice(&k_in[..nkv * hd]),
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        });
    let qout_b = rw_f32(c, nh * hd, true);
    let gout_b = rw_f32(c, nh * hd, true);
    let qnw_b = storage_bytes(c, bytemuck::cast_slice(qnw));
    let knw_b = storage_bytes(c, bytemuck::cast_slice(knw));
    let invf_b = storage_bytes(c, bytemuck::cast_slice(invf));
    let p_data = [
        nh as u32,
        nkv as u32,
        hd as u32,
        rd as u32,
        pos as u32,
        flags,
        eps.to_bits(),
        0u32,
    ];
    let p_buf = c
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("rq-p"),
            contents: bytemuck::cast_slice(&p_data),
            usage: wgpu::BufferUsages::UNIFORM,
        });
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("rq-bg"),
        layout: &c.layout_attn_rope,
        entries: &[
            bind_buf(0, &qraw_b),
            bind_buf(1, &k_b),
            bind_buf(2, &qout_b),
            bind_buf(3, &gout_b),
            bind_buf(4, &qnw_b),
            bind_buf(5, &knw_b),
            bind_buf(6, &invf_b),
            bind_buf(7, &p_buf),
        ],
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("rq") });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("rq"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.attn_rope);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups((nh + nkv) as u32, 1, 1);
    }
    let mk_stage = |n: usize| {
        c.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("rq-stage"),
            size: (n * 4) as u64,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        })
    };
    let sq = mk_stage(nh * hd);
    let sk = mk_stage(nkv * hd);
    let sgt = mk_stage(nh * hd);
    enc.copy_buffer_to_buffer(&qout_b, 0, &sq, 0, (nh * hd * 4) as u64);
    enc.copy_buffer_to_buffer(&k_b, 0, &sk, 0, (nkv * hd * 4) as u64);
    enc.copy_buffer_to_buffer(&gout_b, 0, &sgt, 0, (nh * hd * 4) as u64);
    submit(c, enc.finish());
    for s in [&sq, &sk, &sgt] {
        s.slice(..).map_async(wgpu::MapMode::Read, |_| {});
    }
    if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
        return false;
    }
    let (Ok(dq), Ok(dk), Ok(dg)) = (
        sq.slice(..).get_mapped_range(),
        sk.slice(..).get_mapped_range(),
        sgt.slice(..).get_mapped_range(),
    ) else {
        return false;
    };
    qout[..nh * hd].copy_from_slice(bytemuck::cast_slice(&dq[..nh * hd * 4]));
    k_out[..nkv * hd].copy_from_slice(bytemuck::cast_slice(&dk[..nkv * hd * 4]));
    gout[..nh * hd].copy_from_slice(bytemuck::cast_slice(&dg[..nh * hd * 4]));
    true
}

/// GPU grouped decode attention (bring-up / parity). K/V caches are laid out
/// [nkv, cap, hd]; attends q[nh·hd] over the first `n` rows, writes out[nh·hd].
#[allow(clippy::too_many_arguments)]
pub fn gqa_attend_gpu(
    q: &[f32],
    kcache: &[f32],
    vcache: &[f32],
    nh: usize,
    hpk: usize,
    hd: usize,
    cap: usize,
    n: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if hd % 4 != 0 || hd > c.hd_cap {
        return false; // vec4 K/V reads; hd_cap = workgroup-storage limit
    }
    let q_b = storage_bytes(c, bytemuck::cast_slice(q));
    let k_b = storage_bytes(c, bytemuck::cast_slice(kcache));
    let v_b = storage_bytes(c, bytemuck::cast_slice(vcache));
    let o_b = rw_f32(c, nh * hd, true);
    let p_buf = c
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("at-p"),
            contents: bytemuck::cast_slice(&[
                nh as u32, hpk as u32, hd as u32, cap as u32, n as u32, 0u32, 0u32, 0u32,
            ]),
            usage: wgpu::BufferUsages::UNIFORM,
        });
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("at-bg"),
        layout: attend_pipes(c, hd).1,
        entries: &[
            bind_buf(0, &q_b),
            bind_buf(1, &k_b),
            bind_buf(2, &v_b),
            bind_buf(3, &o_b),
            bind_buf(4, &p_buf),
        ],
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("at") });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("at"),
            timestamp_writes: None,
        });
        pass.set_pipeline(attend_pipes(c, hd).0);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(nh as u32, 1, 1);
    }
    let size = (nh * hd * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "at-stage",
    );
    let ok = readback(c, enc, &o_b, &stage, size, &mut out[..nh * hd]);
    drop(sc);
    ok
}

/// Resident q1 weight for a model tensor (cached in VRAM by (ptr, idx)).
/// Returns (buffer, rows, cols). None on budget/shape refusal.
/// Is wgpu initialized on a DISCRETE adapter? Gates the whole-token
/// graph default (see gpu::wgpu_graph_default).
pub(crate) fn discrete_active() -> bool {
    ctx().map(|c| c.discrete).unwrap_or(false)
}

/// A wgpu context of any kind is up — the graph-default check for
/// desktop-class UMA (Apple silicon), where discreteness is the wrong
/// question.
pub(crate) fn adapter_up() -> bool {
    ctx().is_some()
}

fn q1_weight(c: &Ctx, model: &Arc<CmfModel>, idx: usize) -> Option<(wgpu::Buffer, usize, usize)> {
    let entry = model.tensors.get(idx)?;
    let rows = *entry.shape.first()? as usize;
    let cols = *entry.shape.get(1)? as usize;
    if cols % 32 != 0 {
        return None;
    }
    let abs = model.entry_abs_offset(entry)?;
    let bytes = model.primary_bytes();
    let plen = rows * (cols / 32) * 6;
    if abs + plen > bytes.len() {
        return None;
    }
    let buf = weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen])?;
    Some((buf, rows, cols))
}

/// A q4_tiled / q4tp weight as one device buffer — the whole tensor, since
/// both layouts keep their scales inside (q4t) or in trailing planes (q4tp)
/// and the kernels index them from the same base.
fn tile_weight(c: &Ctx, model: &Arc<CmfModel>, idx: usize) -> Option<(wgpu::Buffer, usize, usize)> {
    let entry = model.tensors.get(idx)?;
    let rows = *entry.shape.first()? as usize;
    let cols = *entry.shape.get(1)? as usize;
    if cols % 32 != 0 {
        return None;
    }
    let abs = model.entry_abs_offset(entry)?;
    let bytes = model.primary_bytes();
    let plen = entry.nbytes as usize;
    if abs + plen > bytes.len() {
        return None;
    }
    let buf = weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen])?;
    Some((buf, rows, cols))
}

/// Production drop-in for the attention sub-block on the token graph: takes
/// the already-normed hidden and returns the O-projection output (pre-
/// residual) — exactly where `qwen_attention` slots in. QKV/O weights are
/// resident (VRAM cache), the K/V cache is a persistent device mirror keyed
/// by (kv_id, layer) that is synced once from the CPU cache (prefill) then
/// appended to each token. Everything runs in ONE command encoder; only the
/// attention output reads back. false = refusal (caller keeps the CPU path).
#[allow(clippy::too_many_arguments)]
pub fn attn_dropin_gpu(
    model: &Arc<CmfModel>,
    kv_id: u64,
    layer: usize,
    normed: &[f32],
    wq_idx: usize,
    wk_idx: usize,
    wv_idx: usize,
    wo_idx: usize,
    q_norm: Option<&[f32]>,
    k_norm: Option<&[f32]>,
    invf: &[f32],
    nh: usize,
    nkv: usize,
    hd: usize,
    rd: usize,
    hidden: usize,
    pos: usize,
    cap: usize,
    gemma: bool,
    eps: f32,
    cpu_k: &[Vec<f32>],
    cpu_v: &[Vec<f32>],
    attn_out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if pos >= cap || hd % 4 != 0 || hd > c.hd_cap {
        return false; // vec4 K/V reads; hd_cap = workgroup-storage limit
    }
    let (wq, rq, cq) = q1_weight(c, model, wq_idx).unwrap_or((
        c.device.create_buffer(&wgpu::BufferDescriptor {
            label: None,
            size: 4,
            usage: wgpu::BufferUsages::STORAGE,
            mapped_at_creation: false,
        }),
        0,
        0,
    ));
    if rq != nh * hd || cq != hidden {
        return false; // gated arch (e.g. output_gate doubles rows) → CPU path
    }
    let Some((wk, _, _)) = q1_weight(c, model, wk_idx) else {
        return false;
    };
    let Some((wv, _, _)) = q1_weight(c, model, wv_idx) else {
        return false;
    };
    let Some((wo, ro, co)) = q1_weight(c, model, wo_idx) else {
        return false;
    };
    if ro != hidden || co != nh * hd {
        return false;
    }
    // Device K/V mirror (persist across tokens).
    let mut kvm = c.attn_kv.lock().unwrap();
    let entry = kvm.entry((kv_id, layer)).or_insert_with(|| {
        let sz = (nkv * cap * hd * 4) as u64;
        let mk = || {
            c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("kv-mirror"),
                size: sz,
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_DST
                    | wgpu::BufferUsages::COPY_SRC,
                mapped_at_creation: false,
            })
        };
        KvMirror {
            k: mk(),
            v: mk(),
            synced: 0,
        }
    });
    // Sync prefill history 0..pos from the CPU cache (once).
    if entry.synced < pos {
        for h in 0..nkv {
            let src_k = &cpu_k[h];
            let src_v = &cpu_v[h];
            let from = entry.synced;
            let take = pos.min(src_k.len() / hd);
            if take > from {
                let off = ((h * cap + from) * hd * 4) as u64;
                c.queue.write_buffer(
                    &entry.k,
                    off,
                    bytemuck::cast_slice(&src_k[from * hd..take * hd]),
                );
                c.queue.write_buffer(
                    &entry.v,
                    off,
                    bytemuck::cast_slice(&src_v[from * hd..take * hd]),
                );
            }
        }
        entry.synced = pos;
    }
    let kbuf = entry.k.clone();
    let vbuf = entry.v.clone();
    drop(kvm);

    let stor = |data: &[u8]| {
        c.device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: None,
                contents: data,
                usage: wgpu::BufferUsages::STORAGE,
            })
    };
    let dummy = vec![0f32; hd];
    let qnw_b = stor(bytemuck::cast_slice(q_norm.unwrap_or(&dummy)));
    let knw_b = stor(bytemuck::cast_slice(k_norm.unwrap_or(&dummy)));
    let invf_b = stor(bytemuck::cast_slice(invf));
    let normed_b = stor(bytemuck::cast_slice(&normed[..hidden]));
    let qraw_b = rw_f32(c, nh * hd, false);
    let k_b = rw_f32(c, nkv * hd, false);
    let v_b = rw_f32(c, nkv * hd, false);
    let qout_b = rw_f32(c, nh * hd, false);
    let gout_b = rw_f32(c, nh * hd, false);
    let attn_b = rw_f32(c, nh * hd, false);
    let o_b = rw_f32(c, hidden, true);
    let flags = if q_norm.is_some() { 2u32 } else { 0 }
        | if k_norm.is_some() { 4 } else { 0 }
        | if gemma { 8 } else { 0 };
    let unif = |d: &[u32]| {
        c.device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: None,
                contents: bytemuck::cast_slice(d),
                usage: wgpu::BufferUsages::UNIFORM,
            })
    };
    let bg = |layout: &wgpu::BindGroupLayout, bufs: &[&wgpu::Buffer]| {
        let e: Vec<_> = bufs
            .iter()
            .enumerate()
            .map(|(i, b)| bind_buf(i as u32, b))
            .collect();
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout,
            entries: &e,
        })
    };
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("attn-dropin"),
        });
    let go =
        |enc: &mut wgpu::CommandEncoder, p: &wgpu::ComputePipeline, b: &wgpu::BindGroup, g: u32| {
            let mut pass = begin_pass(enc);
            pass.set_pipeline(p);
            pass.set_bind_group(0, b, &[]);
            pass.dispatch_workgroups(g, 1, 1);
        };
    encode_matvec_q1(c, &mut enc, &wq, &normed_b, &qraw_b, nh * hd, hidden);
    encode_matvec_q1(c, &mut enc, &wk, &normed_b, &k_b, nkv * hd, hidden);
    encode_matvec_q1(c, &mut enc, &wv, &normed_b, &v_b, nkv * hd, hidden);
    let rq_p = unif(&[
        nh as u32,
        nkv as u32,
        hd as u32,
        rd as u32,
        pos as u32,
        flags,
        eps.to_bits(),
        0,
    ]);
    go(
        &mut enc,
        &c.attn_rope,
        &bg(
            &c.layout_attn_rope,
            &[
                &qraw_b, &k_b, &qout_b, &gout_b, &qnw_b, &knw_b, &invf_b, &rq_p,
            ],
        ),
        (nh + nkv) as u32,
    );
    let kv_p = unif(&[nkv as u32, hd as u32, cap as u32, pos as u32]);
    go(
        &mut enc,
        &c.kv_append,
        &bg(&c.layout_kv, &[&k_b, &v_b, &kbuf, &vbuf, &kv_p]),
        ((nkv * hd) as u32).div_ceil(256),
    );
    let at_p = unif(&[
        nh as u32,
        (nh / nkv) as u32,
        hd as u32,
        cap as u32,
        (pos + 1) as u32,
        0,
        0,
        0,
    ]);
    {
        let (ap, al) = attend_pipes(c, hd);
        go(
            &mut enc,
            ap,
            &bg(al, &[&qout_b, &kbuf, &vbuf, &attn_b, &at_p]),
            nh as u32,
        );
    }
    encode_matvec_q1(c, &mut enc, &wo, &attn_b, &o_b, hidden, nh * hd);
    let size = (hidden * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dropin-stage",
    );
    let ok = readback(c, enc, &o_b, &stage, size, &mut attn_out[..hidden]);
    drop(sc);
    if ok {
        c.attn_kv
            .lock()
            .unwrap()
            .get_mut(&(kv_id, layer))
            .map(|m| m.synced = pos + 1);
    }
    ok
}

/// WHOLE-TOKEN decode graph: the entire layer stack (rmsnorm → attention →
/// residual → rmsnorm → SiLU-FFN → residual, every layer) encoded into ONE
/// command buffer with the hidden RESIDENT on the GPU — only the final hidden
/// reads back (one submit/token instead of ~2 per layer). This is what lifts
/// the submit-latency wall. Returns false on any refusal (caller keeps CPU).
#[allow(clippy::too_many_arguments)]
pub fn forward_token_graph(
    model: &Arc<CmfModel>,
    kv_id: u64,
    layers: &[crate::gpu::GraphLayer],
    o1: &[Option<Vec<crate::nystrom::O1DeviceView<'_>>>],
    o1_epoch: u64,
    invf: &[f32],
    h: &mut [f32],
    nh: usize,
    nkv: usize,
    hd: usize,
    rd: usize,
    hidden: usize,
    inter: usize,
    position: usize,
    cap: usize,
    gemma: bool,
    eps: f32,
    // Optional final-norm + lm_head fold: (weight, rows). When Some and the
    // weight resolves, the graph rides the final RMSNorm and lm_head in the
    // same submit and reads back `logits` (rows) instead of the hidden — one
    // fewer op + sync per token, and the lm_head stays on-device.
    lm_head: Option<(&crate::gpu::GraphW, usize)>,
    final_norm: &[f32],
    logits: &mut Vec<f32>,
    loop_norm_at: &[usize],
    // Multi-step greedy: encode `steps` whole frames in THIS submit, argmax
    // and re-embed on the device, and return the k winner ids instead of
    // logits. Needs `embed` = (q4tp embedding weight, vocab rows, multiplier).
    steps: usize,
    embed: Option<(&crate::gpu::GraphW, usize, f32)>,
    ids_out: Option<&mut Vec<u32>>,
    // Reports how many leading layers the graph executed. Equal to
    // `layers.len()` on a full run; smaller when the expert budget ended
    // the device prefix early — then `h` holds the boundary hidden, no
    // logits were produced, and the caller owns the remaining layers.
    mut layers_run: Option<&mut usize>,
) -> bool {
    let Some(c) = ctx() else {
        graph_refused("no ctx");
        return false;
    };
    if position >= cap || hd % 4 != 0 || hd > c.hd_cap {
        {
            use std::sync::atomic::{AtomicBool, Ordering};
            static SAID: AtomicBool = AtomicBool::new(false);
            if !SAID.swap(true, Ordering::Relaxed) {
                tracing::warn!(
                    "wgpu token graph declined: position {position} >= cap {cap}, or head_dim \
                     {hd} (must be %4 and <= hd_cap {})",
                    c.hd_cap
                );
            }
        }
        return false; // vec4 K/V reads; hd_cap = workgroup-storage limit
    }
    let t_start = std::time::Instant::now();
    // A resolved matvec weight: the device-local buffer, (q8 only) its row
    // scales, and the codec kind (0=q8_row 1=q1 2=q4_block 3=q1t 4=f32 5=q4_tiled).
    struct GMat {
        buf: wgpu::Buffer,
        rs: Option<wgpu::Buffer>,
        kind: u8,
    }
    enum LAttn {
        Full {
            wq: GMat,
            wk: GMat,
            wv: GMat,
            wo: GMat,
        },
        Gdn {
            qkv: GMat,
            z: GMat,
            a: GMat,
            b: GMat,
            out: GMat,
            nv: usize,
            nk: usize,
            dk: usize,
            dv: usize,
            kk: usize,
            cdim: usize,
        },
    }
    enum LFfn {
        Dense {
            gate: GMat,
            up: GMat,
            down: GMat,
        },
        Moe {
            router: GMat,
            sgate: GMat,
            gate_all: wgpu::Buffer,
            up_all: wgpu::Buffer,
            down_all: wgpu::Buffer,
            n_exp: usize,
            top_k: usize,
            inter: usize,
            norm_topk: bool,
            q4tp: bool,
            gu_q2: bool,
        },
    }
    struct LW {
        attn: LAttn,
        ffn: LFfn,
    }
    // Resolve + cache every layer's weights (q8_row or q1) up front; bail (CPU)
    // on any refusal (budget/shape/dtype).
    let resolve = |gw: &crate::gpu::GraphW, rows: usize, cols: usize| -> Option<GMat> {
        match gw.kind {
            0 => {
                // q8_row: weight bytes = rows*cols, plus per-row scales.
                if gw.row_scale.len() < rows {
                    return None;
                }
                let b = tensor_weight(c, model, gw.idx, rows, cols)?; // device-local
                // Row scales are token-invariant — cache by (ptr,rows).
                let key = (gw.row_scale.as_ptr() as usize, rows);
                let mut cb = c.const_bufs.lock().unwrap();
                let rsb = if let Some(x) = cb.get(&key) {
                    x.clone()
                } else {
                    let x = c.device.create_buffer(&wgpu::BufferDescriptor {
                        label: Some("g-rs"),
                        size: (rows * 4) as u64,
                        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
                        mapped_at_creation: false,
                    });
                    c.queue
                        .write_buffer(&x, 0, bytemuck::cast_slice(&gw.row_scale[..rows]));
                    cb.insert(key, x.clone());
                    x
                };
                Some(GMat {
                    buf: b,
                    rs: Some(rsb),
                    kind: 0,
                })
            }
            1 => {
                let (b, r, cc) = q1_weight(c, model, gw.idx)?;
                if r != rows || cc != cols {
                    return None;
                }
                Some(GMat {
                    buf: b,
                    rs: None,
                    kind: 1,
                })
            }
            2 | 3 | 5 | 6 => {
                // q4_block / q1t / q4_tiled / q4tp: the tensor carries its own byte
                // length (tiles + q1t's sparse overlay) — fetch whole,
                // device-local.
                let entry = model.tensors.get(gw.idx)?;
                if *entry.shape.first()? as usize != rows || *entry.shape.get(1)? as usize != cols {
                    return None;
                }
                let abs = model.entry_abs_offset(entry)?;
                let plen = entry.nbytes as usize;
                let bytes = model.primary_bytes();
                if abs + plen > bytes.len() {
                    return None;
                }
                let b = weight_buffer(
                    c,
                    (model.uid() as usize, gw.idx),
                    &bytes[abs..abs + plen],
                )?;
                Some(GMat {
                    buf: b,
                    rs: None,
                    kind: gw.kind,
                })
            }
            4 => {
                // f32 weight (small unquantized projection, e.g. GDN a/b) —
                // token-invariant: cache device-local by (ptr, rows*cols)
                // instead of re-uploading it every token.
                if gw.data.len() < rows * cols {
                    return None;
                }
                let key = (gw.data.as_ptr() as usize, rows * cols);
                let mut cb = c.const_bufs.lock().unwrap();
                let b = if let Some(x) = cb.get(&key) {
                    x.clone()
                } else {
                    let x = c.device.create_buffer(&wgpu::BufferDescriptor {
                        label: Some("g-f32w"),
                        size: (rows * cols * 4) as u64,
                        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
                        mapped_at_creation: false,
                    });
                    c.queue
                        .write_buffer(&x, 0, bytemuck::cast_slice(&gw.data[..rows * cols]));
                    cb.insert(key, x.clone());
                    x
                };
                Some(GMat {
                    buf: b,
                    rs: None,
                    kind: 4,
                })
            }
            _ => None,
        }
    };
    let mut lws = Vec::with_capacity(layers.len());
    let mut gdn_dims: Option<(usize, usize, usize, usize, usize, usize)> = None; // nv,nk,dk,dv,kk,cdim
    // Budget-driven device prefix: the first MoE layer whose experts no
    // longer fit ends the prefix instead of sinking the whole graph — the
    // caller finishes the remaining layers on the host from the boundary
    // hidden. One sync per token at the boundary, not per layer.
    let mut prefix = false;
    for l in layers {
        let attn = match &l.attn {
            crate::gpu::GraphAttn::Full {
                wq,
                wk,
                wv,
                wo,
                output_gate,
                ..
            } => {
                // Gated attention: wq packs q||gate per head → 2·nh·hd rows.
                let qrows = nh * hd * (1 + *output_gate as usize);
                let (Some(wq), Some(wk), Some(wv), Some(wo)) = (
                    resolve(wq, qrows, hidden),
                    resolve(wk, nkv * hd, hidden),
                    resolve(wv, nkv * hd, hidden),
                    resolve(wo, hidden, nh * hd),
                ) else {
                    return false;
                };
                LAttn::Full { wq, wk, wv, wo }
            }
            crate::gpu::GraphAttn::Gdn {
                qkv,
                z,
                a,
                b,
                out,
                nv,
                nk,
                dk,
                dv,
                kk,
                ..
            } => {
                let cdim = 2 * nk * dk + nv * dv;
                gdn_dims = Some((*nv, *nk, *dk, *dv, *kk, cdim));
                let (Some(qkv), Some(z), Some(a), Some(b), Some(out)) = (
                    resolve(qkv, cdim, hidden),
                    resolve(z, nv * dv, hidden),
                    resolve(a, *nv, hidden),
                    resolve(b, *nv, hidden),
                    resolve(out, hidden, nv * dv),
                ) else {
                    return false;
                };
                LAttn::Gdn {
                    qkv,
                    z,
                    a,
                    b,
                    out,
                    nv: *nv,
                    nk: *nk,
                    dk: *dk,
                    dv: *dv,
                    kk: *kk,
                    cdim,
                }
            }
        };
        let ffn = match &l.ffn {
            crate::gpu::GraphFfn::Dense { gate, up, down } => {
                let (Some(gate), Some(up), Some(down)) = (
                    resolve(gate, inter, hidden),
                    resolve(up, inter, hidden),
                    resolve(down, hidden, inter),
                ) else {
                    return false;
                };
                LFfn::Dense { gate, up, down }
            }
            crate::gpu::GraphFfn::Moe {
                router,
                shared_gate,
                experts,
                n_exp,
                top_k,
                inter: mi,
                norm_topk,
                q4tp,
                gu_q2,
            } => {
                // Select kernel: logits live in a 256-slot workgroup array;
                // slot top_k+1 holds the shared expert.
                if *top_k >= 16 || *n_exp > 256 || experts.len() != n_exp + 1 {
                    return false;
                }
                let (Some(router), Some(sgate)) = (
                    resolve(router, *n_exp, hidden),
                    resolve(shared_gate, 1, hidden),
                ) else {
                    return false;
                };
                // Over budget with layers already built: end the prefix
                // here. Layer zero over budget keeps the historical loud
                // whole-graph refusal inside moe_expert_bufs. A layer whose
                // buffers are ALREADY on the card is never the one to stop
                // at — its bytes are inside `resident`, so re-running the
                // check against them would shrink the prefix to one layer
                // on the second token (measured: 25 tok/s where 60 belongs).
                let cached = experts.first().is_some_and(|e| {
                    c.moe_expw
                        .lock()
                        .unwrap()
                        .contains_key(&(model.uid() as usize, e.0))
                });
                let need = moe_pack_bytes(experts.len(), *mi, hidden, *q4tp, *gu_q2, false)
                    .unwrap_or(u64::MAX);
                if !cached
                    && !lws.is_empty()
                    && c.resident.load(std::sync::atomic::Ordering::Relaxed) + need
                        > c.vram_budget
                {
                    prefix = true;
                    break;
                }
                let Some((gate_all, up_all, down_all)) =
                    moe_expert_bufs(c, model, experts, *mi, hidden, *q4tp, *gu_q2, false)
                else {
                    return false;
                };
                LFfn::Moe {
                    router,
                    sgate,
                    gate_all,
                    up_all,
                    down_all,
                    n_exp: *n_exp,
                    top_k: *top_k,
                    inter: *mi,
                    norm_topk: *norm_topk,
                    q4tp: *q4tp,
                    gu_q2: *gu_q2,
                }
            }
        };
        lws.push(LW { attn, ffn });
    }
    if prefix {
        // The multi-step tail (on-device argmax + re-embed) needs the head,
        // which a prefix does not reach — those callers fall back whole.
        if steps > 1 || ids_out.is_some() {
            graph_refused("device prefix cannot serve the multi-step tail");
            return false;
        }
        use std::sync::atomic::{AtomicBool, Ordering};
        static SAID: AtomicBool = AtomicBool::new(false);
        if !SAID.swap(true, Ordering::Relaxed) {
            tracing::info!(
                "wgpu token graph: device prefix {} of {} layers (expert budget), \
                 the tail runs on the host from the boundary hidden",
                lws.len(),
                layers.len()
            );
        }
    }
    if let Some(n) = layers_run.as_deref_mut() {
        *n = lws.len();
    }
    let layers = &layers[..lws.len()];
    // DEVICE-LOCAL + content-cached: create_buffer + write_buffer keeps norm
    // weights in VRAM (not the HOST_VISIBLE heap create_buffer_init forces);
    // caching by (ptr,len) uploads each token-invariant norm buffer once.
    let stor = |data: &[u8]| {
        let key = (data.as_ptr() as usize, data.len());
        let mut cb = c.const_bufs.lock().unwrap();
        if let Some(b) = cb.get(&key) {
            return b.clone();
        }
        let b = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: None,
            size: data.len() as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        c.queue.write_buffer(&b, 0, data);
        cb.insert(key, b.clone());
        b
    };
    // Shared zero buffer of `n` f32 (sentinel key (0,n)) — for absent q/k-norms
    // and the silu bias slot, so no per-token zero Vec is allocated/uploaded.
    let zeros = |n: usize| -> wgpu::Buffer {
        let key = (0usize, n * 4);
        let mut cb = c.const_bufs.lock().unwrap();
        if let Some(b) = cb.get(&key) {
            return b.clone();
        }
        let b = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("g-zero"),
            size: (n * 4) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        c.queue.write_buffer(&b, 0, &vec![0u8; n * 4]);
        cb.insert(key, b.clone());
        b
    };
    let unif = |d: &[u32]| {
        c.device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: None,
                contents: bytemuck::cast_slice(d),
                usage: wgpu::BufferUsages::UNIFORM,
            })
    };
    let bg = |layout: &wgpu::BindGroupLayout, bufs: &[&wgpu::Buffer]| {
        let e: Vec<_> = bufs
            .iter()
            .enumerate()
            .map(|(i, b)| bind_buf(i as u32, b))
            .collect();
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout,
            entries: &e,
        })
    };
    // ── Pooled scratch: all intermediate buffers are reused across tokens ──
    let mut gs = c.graph_scratch.lock().unwrap();
    let st = wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC; // COPY_SRC: debug taps (CMF_O1_TRACE)
    let h_buf = GraphScratch::ensure(
        &c.device,
        &mut gs.h,
        (hidden * 4) as u64,
        st | wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST,
        "g-h",
    );
    c.queue
        .write_buffer(&h_buf, 0, bytemuck::cast_slice(&h[..hidden]));
    let n1 = GraphScratch::ensure(
        &c.device,
        &mut gs.n1,
        (hidden * 4) as u64,
        st | wgpu::BufferUsages::COPY_SRC,
        "g-n1",
    );
    // Gated attention (Qwen3.5) makes wq emit 2·nh·hd (q||gate per head), so the
    // raw-QKV scratch must hold the widened q output for any gated layer.
    let any_gate = layers.iter().any(|l| {
        matches!(
            &l.attn,
            crate::gpu::GraphAttn::Full {
                output_gate: true,
                ..
            }
        )
    });
    let qraw = GraphScratch::ensure(
        &c.device,
        &mut gs.qraw,
        (nh * hd * (1 + any_gate as usize) * 4) as u64,
        st,
        "g-qraw",
    );
    let kb = GraphScratch::ensure(&c.device, &mut gs.kb, (nkv * hd * 4) as u64, st, "g-kb");
    let vb = GraphScratch::ensure(&c.device, &mut gs.vb, (nkv * hd * 4) as u64, st, "g-vb");
    let qout = GraphScratch::ensure(&c.device, &mut gs.qout, (nh * hd * 4) as u64, st, "g-qout");
    let gout = GraphScratch::ensure(&c.device, &mut gs.gout, (nh * hd * 4) as u64, st, "g-gout");
    let attn = GraphScratch::ensure(&c.device, &mut gs.attn, (nh * hd * 4) as u64, st, "g-attn");
    let ob = GraphScratch::ensure(&c.device, &mut gs.ob, (hidden * 4) as u64, st, "g-ob");
    let gbuf = GraphScratch::ensure(&c.device, &mut gs.gbuf, (inter * 4) as u64, st, "g-gbuf");
    let ubuf = GraphScratch::ensure(&c.device, &mut gs.ubuf, (inter * 4) as u64, st, "g-ubuf");
    let abuf = GraphScratch::ensure(&c.device, &mut gs.abuf, (inter * 4) as u64, st, "g-abuf");
    // MoE routing scratch, sized to the largest MoE layer (absent → skipped).
    let moe_geom = lws
        .iter()
        .filter_map(|lw| match &lw.ffn {
            LFfn::Moe {
                n_exp,
                top_k,
                inter,
                ..
            } => Some((*n_exp, *top_k + 1, *inter)),
            _ => None,
        })
        .reduce(|a, b| (a.0.max(b.0), a.1.max(b.1), a.2.max(b.2)));
    let moe_bufs = moe_geom.map(|(mn, ms, mi)| {
        (
            GraphScratch::ensure(&c.device, &mut gs.m_logit, (mn * 4) as u64, st, "g-mlogit"),
            GraphScratch::ensure(&c.device, &mut gs.m_slog, 4, st, "g-mslog"),
            GraphScratch::ensure(&c.device, &mut gs.m_sel, (ms * 4) as u64, st, "g-msel"),
            GraphScratch::ensure(&c.device, &mut gs.m_wt, (ms * 4) as u64, st, "g-mwt"),
            GraphScratch::ensure(&c.device, &mut gs.m_act, (ms * mi * 4) as u64, st, "g-mact"),
        )
    });
    let invf_b = stor(bytemuck::cast_slice(invf));
    let dummy_hd = zeros(hd);
    // GDN intermediates (sized to the model's GDN geometry; 1 if no GDN layer).
    let (gnv, _gnk, gdk, gdv, _gkk, gcdim) = gdn_dims.unwrap_or((1, 1, 1, 1, 1, 1));
    let qkv_b = GraphScratch::ensure(&c.device, &mut gs.qkv_b, (gcdim * 4) as u64, st, "g-qkv");
    let cq_b = GraphScratch::ensure(&c.device, &mut gs.cq_b, (gcdim * 4) as u64, st, "g-cq");
    let z_b = GraphScratch::ensure(&c.device, &mut gs.z_b, (gnv * gdv * 4) as u64, st, "g-z");
    let a_b = GraphScratch::ensure(&c.device, &mut gs.a_b, (gnv * 4) as u64, st, "g-a");
    let b_b = GraphScratch::ensure(&c.device, &mut gs.b_b, (gnv * 4) as u64, st, "g-b");
    let gdo_b = GraphScratch::ensure(
        &c.device,
        &mut gs.gdo_b,
        (gnv * gdv * 4) as u64,
        st,
        "g-gdo",
    );
    // Sync each Full layer's device K/V mirror from the CPU cache (once);
    // GDN layers carry a persistent (ring, S) recurrent state instead.
    let mut kvbufs: Vec<Option<(wgpu::Buffer, wgpu::Buffer)>> = Vec::with_capacity(layers.len());
    let mut gdnbufs: Vec<Option<(wgpu::Buffer, wgpu::Buffer)>> = Vec::with_capacity(layers.len());
    {
        let mut kvm = c.attn_kv.lock().unwrap();
        let mut gsm = c.gdn_state.lock().unwrap();
        for (li, l) in layers.iter().enumerate() {
            match &l.attn {
                crate::gpu::GraphAttn::Full { cpu_k, cpu_v, .. } => {
                    if o1.get(li).is_some_and(|v| v.is_some()) {
                        // o1 replaces this layer's KV attention outright —
                        // no mirror, and no prefill K/V upload (16K of it
                        // at long context) that nothing would read.
                        kvbufs.push(None);
                        gdnbufs.push(None);
                        continue;
                    }
                    let e = kvm.entry((kv_id, li)).or_insert_with(|| {
                        let sz = (nkv * cap * hd * 4) as u64;
                        let mk = || {
                            c.device.create_buffer(&wgpu::BufferDescriptor {
                                label: Some("kv"),
                                size: sz,
                                usage: wgpu::BufferUsages::STORAGE
                                    | wgpu::BufferUsages::COPY_DST
                                    | wgpu::BufferUsages::COPY_SRC,
                                mapped_at_creation: false,
                            })
                        };
                        KvMirror {
                            k: mk(),
                            v: mk(),
                            synced: 0,
                        }
                    });
                    if e.synced < position {
                        for hh in 0..nkv {
                            let take = position.min(cpu_k[hh].len() / hd);
                            if take > e.synced {
                                let off = ((hh * cap + e.synced) * hd * 4) as u64;
                                c.queue.write_buffer(
                                    &e.k,
                                    off,
                                    bytemuck::cast_slice(&cpu_k[hh][e.synced * hd..take * hd]),
                                );
                                c.queue.write_buffer(
                                    &e.v,
                                    off,
                                    bytemuck::cast_slice(&cpu_v[hh][e.synced * hd..take * hd]),
                                );
                            }
                        }
                        e.synced = position;
                    }
                    kvbufs.push(Some((e.k.clone(), e.v.clone())));
                    gdnbufs.push(None);
                }
                crate::gpu::GraphAttn::Gdn { cpu_state, .. } => {
                    let e = gsm.entry((kv_id, li)).or_insert_with(|| {
                        let ring_sz = ((gcdim * (_gkk.max(1).saturating_sub(1))) * 4) as u64;
                        let s_sz = (gnv * gdk * gdv * 4) as u64;
                        let mk = |sz: u64| {
                            let bf = c.device.create_buffer(&wgpu::BufferDescriptor {
                                label: Some("gdn-state"),
                                size: sz.max(4),
                                usage: wgpu::BufferUsages::STORAGE
                                    | wgpu::BufferUsages::COPY_DST
                                    | wgpu::BufferUsages::COPY_SRC,
                                mapped_at_creation: false,
                            });
                            c.queue.write_buffer(&bf, 0, &vec![0u8; sz.max(4) as usize]);
                            bf
                        };
                        let (ring, sbuf) = (mk(ring_sz), mk(s_sz));
                        // Seed from the CPU recurrence when the host ran the
                        // prefill (o1 collection, CPU fallback). A fresh entry
                        // with an EMPTY cpu_state is the graph-prefill flow —
                        // the graph builds the state itself from position 0.
                        // Zero-initialized device state at decode is the
                        // "coherent but contextless" failure this closes.
                        let want = (ring_sz + s_sz) as usize / 4;
                        if cpu_state.len() == want && want > 0 {
                            let ring_n = ring_sz as usize / 4;
                            c.queue.write_buffer(
                                &ring,
                                0,
                                bytemuck::cast_slice(&cpu_state[..ring_n]),
                            );
                            c.queue.write_buffer(
                                &sbuf,
                                0,
                                bytemuck::cast_slice(&cpu_state[ring_n..]),
                            );
                        }
                        (ring, sbuf)
                    });
                    gdnbufs.push(Some((e.0.clone(), e.1.clone())));
                    kvbufs.push(None);
                }
            }
        }
    }
    let prof = std::env::var("CMF_GRAPH_PROF").is_ok();
    // Group mutually-independent projections (that all read the same normed
    // hidden) into ONE compute pass — the GPU can overlap them, cutting the
    // per-pass barrier bubbles that dominate single-token decode. Default on
    // (measured +5-8% token-identical across q1/q8/GDN); CMF_GPU_GROUP=0 off.
    let group = std::env::var("CMF_GPU_GROUP")
        .map(|v| v != "0")
        .unwrap_or(true);
    // Hand strictly-serial single-dispatch stages to the NEXT pass instead of
    // opening a pass for each. Dispatch ORDER is unchanged, so the answer is
    // unchanged; only pass boundaries move. CMF_PASSFUSE=0 reverts.
    let passfuse = std::env::var("CMF_PASSFUSE")
        .map(|v| v != "0")
        .unwrap_or(true);
    // CMF_SKIP_PROBE=moe|gdn — TIMING ONLY, the answer is garbage. Drops a
    // whole stage's dispatches while leaving every buffer, pass and shape
    // in place, so the delta is that stage's real share of the frame. The
    // arithmetic-only probe (CMF_TOPK_PROBE) says MoE math is ~2 ms of 17;
    // this one says where the rest actually goes, which neither dispatch
    // counting nor pass counting predicted correctly.
    let skip = std::env::var("CMF_SKIP_PROBE").unwrap_or_default();
    let (skip_moe, skip_gdn) = (skip.contains("moe"), skip.contains("gdn"));
    // Skeleton pieces, so the 7.5 ms that is neither MoE nor GDN can be
    // attributed instead of guessed at: the four GDN input projections,
    // the GDN output projection, the fused residual+norm, the router, and
    // the whole full-attention chain.
    let skip_proj = skip.contains("proj");
    let skip_outp = skip.contains("outp");
    let skip_norm = skip.contains("norm");
    let skip_router = skip.contains("router");
    let skip_attn = skip.contains("attn");
    // CMF_LAYERS_PROBE=N — TIMING ONLY, the answer is garbage. Encodes just
    // the first N layers, leaving the final norm + lm_head + readback in
    // place. Decode time against N is a straight line whose slope is the
    // per-layer cost and whose intercept is everything that happens once a
    // token: the submit, the ~1 MB logits readback and the lm_head. Neither
    // dispatch counting nor pass counting predicted the frame correctly, so
    // this splits it by measurement instead.
    let layer_cap = std::env::var("CMF_LAYERS_PROBE")
        .ok()
        .and_then(|v| v.parse::<usize>().ok())
        .unwrap_or(usize::MAX);
    let t_enc0 = std::time::Instant::now();
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("token-graph"),
        });
    let go =
        |enc: &mut wgpu::CommandEncoder, p: &wgpu::ComputePipeline, b: &wgpu::BindGroup, g: u32| {
            let mut pass = begin_pass(enc);
            pass.set_pipeline(p);
            pass.set_bind_group(0, b, &[]);
            pass.dispatch_workgroups(g, 1, 1);
        };
    let flags = |qn: bool, kn: bool| {
        (if qn { 2u32 } else { 0 }) | (if kn { 4 } else { 0 }) | (if gemma { 8 } else { 0 })
    };
    // Constant uniforms for the whole token (position is fixed for this call).
    // Token-invariant ones use the content-keyed cache; position-dependent ones
    // use pooled buffers updated via write_buffer (no allocation after first token).
    let g = if gemma { 1u32 } else { 0 };
    let rms_u = uniform_u32x4(c, [hidden as u32, g, eps.to_bits(), 0]);
    let ax_u = uniform_u32x4(c, [1.0f32.to_bits(), hidden as u32, 0, 0]);
    let silu_u = uniform_u32x4(c, [inter as u32, 0, 0, 0]);
    let steps = steps.max(1);
    // One uniform PER STEP, with stable identities: write_buffer lands at
    // submit, so a single shared buffer would collapse every step to the
    // last position written. Slot 0 is the plain single-step path.
    let mku = |v: &mut Vec<wgpu::Buffer>, size: u64| {
        while v.len() < steps {
            v.push(c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("g-step-u"),
                size,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            }));
        }
    };
    mku(&mut gs.kv_us, 16);
    mku(&mut gs.at_us, 32);
    mku(&mut gs.rope_us, 32);
    for st in 0..steps {
        let p = position + st;
        c.queue.write_buffer(
            &gs.kv_us[st],
            0,
            bytemuck::cast_slice(&[nkv as u32, hd as u32, cap as u32, p as u32]),
        );
        c.queue.write_buffer(
            &gs.at_us[st],
            0,
            bytemuck::cast_slice(&[
                nh as u32,
                (nh / nkv) as u32,
                hd as u32,
                cap as u32,
                (p + 1) as u32,
                0,
                0,
                0,
            ]),
        );
    }
    let kv_us = std::mem::take(&mut gs.kv_us);
    let at_us = std::mem::take(&mut gs.at_us);
    let rope_us = std::mem::take(&mut gs.rope_us);
    // Encode one matvec, dtype-dispatched: q8_row (encode_matvec + row scales)
    // or q1 (encode_matvec_q1). Each is its own pass — pass-grouping measured
    // as a no-op (the wall is per-dispatch, not per-barrier).
    let emat = |enc: &mut wgpu::CommandEncoder,
                m: &GMat,
                xs: &wgpu::Buffer,
                y: &wgpu::Buffer,
                rows: usize,
                cols: usize| {
        match m.kind {
            0 => encode_matvec(c, enc, &m.buf, xs, m.rs.as_ref().unwrap(), y, rows, cols),
            1 => encode_matvec_q1(c, enc, &m.buf, xs, y, rows, cols),
            2 => encode_q1t_like(c, enc, &c.q4b, &m.buf, xs, y, rows, cols),
            3 => encode_q1t_like(c, enc, &c.q1t, &m.buf, xs, y, rows, cols),
            5 => {
                if c.use_mv4 {
                    let gpr = cols / 32;
                    let p_buf = uniform_u32x4(c, [gpr as u32, rows as u32, cols as u32, 0]);
                    let layout = c.q4t_mv8.get_bind_group_layout(0);
                    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                        label: None,
                        layout: &layout,
                        entries: &[
                            bind_buf(0, &m.buf),
                            bind_buf(2, y),
                            bind_buf(3, &p_buf),
                            bind_buf(5, xs),
                        ],
                    });
                    let mut pass = begin_pass(enc);
                    pass.set_pipeline(&c.q4t_mv8);
                    pass.set_bind_group(0, &bind, &[]);
                    pass.dispatch_workgroups((rows as u32).div_ceil(8).min(MAX_WG), 1, 1);
                } else {
                    encode_q1t_like(c, enc, &c.q4t_mv, &m.buf, xs, y, rows, cols)
                }
            }
            6 => {
                if c.use_mv4 {
                    encode_q4tp_mv4(c, enc, &m.buf, xs, y, rows, cols)
                } else {
                    encode_q1t_like(c, enc, &c.q4tp_mv, &m.buf, xs, y, rows, cols)
                }
            }
            _ => encode_f32matvec(c, enc, &m.buf, xs, y, rows, cols),
        }
    };
    // Prep a matvec (pipeline, bind group, workgroups) WITHOUT opening a pass —
    // so several independent ones can share a pass. None = a dtype we don't
    // group (q4t/q1t) → caller falls back to per-op emat. The bind group keeps
    // its uniform buffer alive, so returning it alone is enough.
    let prep = |m: &GMat,
                xs: &wgpu::Buffer,
                y: &wgpu::Buffer,
                rows: usize,
                cols: usize|
     -> Option<(&wgpu::ComputePipeline, wgpu::BindGroup, u32)> {
        match m.kind {
            0 => {
                let p_buf = uniform_u32x4(c, [(cols / 4) as u32, rows as u32, 0, 0]);
                let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.layout,
                    entries: &[
                        bind_buf(0, &m.buf),
                        bind_buf(1, xs),
                        bind_buf(2, m.rs.as_ref().unwrap()),
                        bind_buf(3, y),
                        bind_buf(4, &p_buf),
                    ],
                });
                Some((&c.matvec, bind, (rows as u32).min(MAX_WG)))
            }
            1 => {
                let gpr = cols / 32;
                let p_buf = uniform_u32x4(c, [(gpr / 2) as u32, rows as u32, 0, 0]);
                let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.layout_q1,
                    entries: &[
                        bind_buf(0, &m.buf),
                        bind_buf(1, xs),
                        bind_buf(2, y),
                        bind_buf(3, &p_buf),
                    ],
                });
                Some((&c.q1, bind, (rows as u32).div_ceil(8).min(MAX_WG)))
            }
            4 => {
                let p_buf = uniform_u32x4(c, [cols as u32, rows as u32, 0, 0]);
                let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.layout_f32,
                    entries: &[
                        bind_buf(0, &m.buf),
                        bind_buf(1, xs),
                        bind_buf(2, y),
                        bind_buf(3, &p_buf),
                    ],
                });
                Some((&c.f32_matvec, bind, (rows as u32).min(MAX_WG)))
            }
            // These arms must exist: without them `prep` returned None for
            // every q4t/q4tp projection, `group_mats` fell back to one
            // compute pass PER matvec and the MoE layer took its per-op
            // branch — a pass costs ~60 us on this Vulkan stack against
            // ~2 ms of arithmetic for the whole MoE block.
            2 | 5 | 6 => {
                let gpr = cols / 32;
                let p_buf = uniform_u32x4(c, [gpr as u32, rows as u32, cols as u32, 0]);
                if m.kind == 2 && c.use_mv4 {
                    let layout = c.q4b_mv8.get_bind_group_layout(0);
                    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                        label: None,
                        layout: &layout,
                        entries: &[
                            bind_buf(0, &m.buf),
                            bind_buf(2, y),
                            bind_buf(3, &p_buf),
                            bind_buf(4, &m.buf),
                            bind_buf(5, xs),
                        ],
                    });
                    return Some((&c.q4b_mv8, bind, (rows as u32).div_ceil(8).min(MAX_WG)));
                }
                if m.kind == 5 && c.use_mv4 {
                    // q4t's twin takes the same five bindings as q4tp's.
                    let layout = c.q4t_mv8.get_bind_group_layout(0);
                    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                        label: None,
                        layout: &layout,
                        // NO slot 4: q4t assembles weights from u16 halves
                        // (18 B tiles are 2-aligned), so the entry point
                        // never reads the vec4 weight view and its auto
                        // layout does not carry that binding.
                        entries: &[
                            bind_buf(0, &m.buf),
                            bind_buf(2, y),
                            bind_buf(3, &p_buf),
                            bind_buf(5, xs),
                        ],
                    });
                    return Some((&c.q4t_mv8, bind, (rows as u32).div_ceil(8).min(MAX_WG)));
                }
                if m.kind == 6 && c.use_mv4 {
                    // Wide rows: the quad-row kernel, four dot chains per
                    // x fetch (the pair kernel was x-LSU-bound at the
                    // dense FFN widths).
                    let (pipe6, per_wg) = if gpr <= 64 {
                        (&c.q4tp_mv16, 16u32)
                    } else {
                        (&c.q4tp_mv16w, 16u32)
                    };
                    // Word 2 is the batch count for THIS pair and `cols` for
                    // everything else bound to the same struct — the shared
                    // uniform above cannot serve both.
                    let p6 = q4tp_mv_params(c, gpr, rows, 1);
                    let layout = pipe6.get_bind_group_layout(0);
                    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                        label: None,
                        layout: &layout,
                        entries: &[
                            bind_buf(0, &m.buf),
                            bind_buf(2, y),
                            bind_buf(3, &p6),
                            bind_buf(4, &m.buf),
                            bind_buf(5, xs),
                        ],
                    });
                    return Some((pipe6, bind, (rows as u32).div_ceil(per_wg).min(MAX_WG)));
                }
                let pl = match m.kind {
                    5 => &c.q4t_mv,
                    6 => &c.q4tp_mv,
                    _ => &c.q4b,
                };
                let layout = pl.get_bind_group_layout(0);
                let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &layout,
                    entries: &[
                        bind_buf(0, &m.buf),
                        bind_buf(1, xs),
                        bind_buf(2, y),
                        bind_buf(3, &p_buf),
                    ],
                });
                Some((pl, bind, (rows as u32).min(MAX_WG)))
            }
            3 => {
                let gpr = cols / 32;
                let p_buf = uniform_u32x4(c, [gpr as u32, rows as u32, cols as u32, 0]);
                let layout = c.q1t.get_bind_group_layout(0);
                let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &layout,
                    entries: &[
                        bind_buf(0, &m.buf),
                        bind_buf(1, xs),
                        bind_buf(2, y),
                        bind_buf(3, &p_buf),
                    ],
                });
                Some((&c.q1t, bind, (rows as u32).min(MAX_WG)))
            }
            _ => None,
        }
    };
    // Emit a set of mutually-INDEPENDENT matvecs. When grouping is on and every
    // one preps, they share a single compute pass (no barrier between them);
    // otherwise each goes through emat as its own pass. Correctness rests on the
    // caller passing only matvecs with no read-after-write among them.
    let group_mats =
        |enc: &mut wgpu::CommandEncoder,
         mats: &[(&GMat, &wgpu::Buffer, &wgpu::Buffer, usize, usize)]| {
            if group {
                let prepped: Vec<_> = mats
                    .iter()
                    .filter_map(|(m, xs, y, r, cc)| prep(m, xs, y, *r, *cc))
                    .collect();
                if prepped.len() == mats.len() {
                    let mut pass = begin_pass(enc);
                    for (p, b, g) in &prepped {
                        pass.set_pipeline(p);
                        pass.set_bind_group(0, b, &[]);
                        pass.dispatch_workgroups(*g, 1, 1);
                    }
                    return;
                }
            }
            for (m, xs, y, r, cc) in mats {
                emat(enc, m, xs, y, *r, *cc);
            }
        };
    // Two projections of the same input under ONE dispatch. `false` = a
    // kind the paired kernel does not cover, caller keeps `group_mats`.
    let pair_mats = |enc: &mut wgpu::CommandEncoder,
                     a: (&GMat, &wgpu::Buffer, usize, usize),
                     b: (&GMat, &wgpu::Buffer, usize, usize),
                     xs: &wgpu::Buffer|
     -> bool {
        let ok = |k: u8| k == 4 || k == 6;
        if !ok(a.0.kind) || !ok(b.0.kind) {
            return false;
        }
        let p = unif(&[
            a.2 as u32,
            a.3 as u32,
            a.0.kind as u32,
            0,
            b.2 as u32,
            b.3 as u32,
            b.0.kind as u32,
            0,
        ]);
        let bind = bg(&c.layout_mv2, &[&a.0.buf, &b.0.buf, xs, a.1, b.1, &p]);
        go(enc, &c.matvec_pair, &bind, ((a.2 + b.2) as u32).min(MAX_WG));
        true
    };
    let mut o1_dbg: Vec<(usize, wgpu::Buffer)> = Vec::new();
    // ── Frame profiler (CMF_GPU_TS=1, single-step): GPU timestamps at pass
    // granularity, aggregated per (stage, layer-kind). The microscope that
    // replaces cost-model guessing.
    let mut ts_n: u32 = 0;
    let mut ts_lbl: Vec<(u8, u8)> = Vec::new();
    let ts_fine = std::env::var("CMF_GPU_TS").as_deref() == Ok("2");
    macro_rules! ts {
        ($enc:expr, $stage:expr, $kind:expr) => {
            if steps == 1 {
                if let Some((qs, _, _)) = &c.ts_query {
                    if ts_n < 255 {
                        $enc.write_timestamp(qs, ts_n);
                        ts_lbl.push(($stage, $kind));
                        ts_n += 1;
                    }
                }
            }
        };
    }
    // Per-dispatch stamps INSIDE a pass (CMF_GPU_TS=2), for the first layer
    // of each kind only — 256 slots cannot carry every dispatch of a frame.
    macro_rules! tsp {
        ($pass:expr, $on:expr, $stage:expr) => {
            if ts_fine && $on && steps == 1 {
                if let Some((qs, _, _)) = &c.ts_query {
                    if ts_n < 255 {
                        $pass.write_timestamp(qs, ts_n);
                        ts_lbl.push(($stage, 9));
                        ts_n += 1;
                    }
                }
            }
        };
    }
    // ── Multi-step prerequisites: the lm_head fold and a q4tp embedding,
    // both resolved up front. Anything missing refuses the WHOLE call so
    // the pipeline can fall back to single-step.
    // "multi" really means: the DEVICE picks the token(s) and the CPU
    // reads ids, not logits. k=1 rides it too — a 4-byte readback against
    // a megabyte of logits.
    let multi = ids_out.is_some();
    let lm_pre = lm_head.and_then(|(gw, rows)| resolve(gw, rows, hidden).map(|m| (m, rows)));
    let emb_pre =
        embed.and_then(|(gw, rows, mult)| resolve(gw, rows, hidden).map(|m| (m, rows, mult)));
    if multi {
        let embed_ok = matches!(&emb_pre, Some((m, _, _)) if m.kind == 6);
        if lm_pre.is_none() || !embed_ok {
            graph_refused("multi-step needs the lm_head fold and a q4tp embedding");
            return false;
        }
    }
    const AM_PARTS: u32 = 512;
    let lm_rows_pre = lm_pre.as_ref().map(|(_, r)| *r).unwrap_or(0);
    let (lbuf_pre, am_pv, am_pi, ids_buf, ids_stage) = if multi {
        let lsize = (lm_rows_pre * 4) as u64;
        (
            Some(GraphScratch::ensure(
                &c.device,
                &mut gs.logits,
                lsize,
                wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
                "g-logits",
            )),
            Some(GraphScratch::ensure(
                &c.device,
                &mut gs.am_pv,
                (AM_PARTS * 4) as u64,
                wgpu::BufferUsages::STORAGE,
                "g-am-pv",
            )),
            Some(GraphScratch::ensure(
                &c.device,
                &mut gs.am_pi,
                (AM_PARTS * 4) as u64,
                wgpu::BufferUsages::STORAGE,
                "g-am-pi",
            )),
            Some(GraphScratch::ensure(
                &c.device,
                &mut gs.ids,
                (steps * 4) as u64,
                wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
                "g-ids",
            )),
            Some(GraphScratch::ensure(
                &c.device,
                &mut gs.ids_stage,
                (steps * 4) as u64,
                wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
                "g-ids-stage",
            )),
        )
    } else {
        (None, None, None, None, None)
    };
    for stp in 0..steps {
        let kv_u = kv_us[stp].clone();
        let at_u = at_us[stp].clone();
        let rope_u = rope_us[stp].clone();
        let position = position + stp;
        // Bootstrap the first layer's attention norm; thereafter each residual is
        // fused with the following norm (add_rmsnorm), saving two dispatches/layer.
        let inw0 = stor(bytemuck::cast_slice(layers[0].input_norm));
        ts!(enc, 0, 0);
        go(
            &mut enc,
            &c.rmsnorm,
            &bg(&c.layout_rmsnorm, &[&h_buf, &inw0, &n1, &rms_u]),
            1,
        );
        // Split the submission mid-stack (single-step decode only): the card
        // starts the first layers while the host still encodes the rest —
        // the DSV4 chain-split trick. Measured +12.8% on the 35B (97.9 ->
        // 110.4): it hides the encode AND the driver's submit latency.
        // Same queue, same order; nothing about the computation changes.
        // `CMF_GRAPH_SPLIT=N` pieces; 0/1 = historical single submit.
        let split_n = graph_split_n();
        let chunk = if steps == 1 && split_n > 1 {
            // Never below four layers a piece: a short device prefix cut
            // into per-layer submits pays more in submissions than it
            // hides in encode.
            lws.len().div_ceil(split_n).max(4)
        } else {
            usize::MAX
        };
        for (li, l) in layers.iter().enumerate() {
            if li >= layer_cap {
                break;
            }
            if li > 0 && chunk != usize::MAX && li % chunk == 0 {
                let full = std::mem::replace(
                    &mut enc,
                    c.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
                        label: Some("token-graph"),
                    }),
                );
                c.queue.submit([full.finish()]);
            }
            let lw = &lws[li];
            let lkind: u8 = if matches!(lw.attn, LAttn::Full { .. }) {
                1
            } else {
                0
            };
            let pnw = stor(bytemuck::cast_slice(l.post_norm));
            // ── token mixing (attention or GDN) → ob ──
            match (&lw.attn, &l.attn) {
                (
                    LAttn::Full { wq, wk, wv, wo },
                    crate::gpu::GraphAttn::Full {
                        q_norm,
                        k_norm,
                        bias,
                        output_gate,
                        ..
                    },
                ) => {
                    let o1_here = o1.get(li).and_then(|v| v.as_ref());
                    // true = the fused short-context arm already ran the output
                    // gate and the O projection inside its pass.
                    let mut attn_done = false;
                    let qnw = q_norm
                        .map(|q| stor(bytemuck::cast_slice(q)))
                        .unwrap_or_else(|| zeros(hd));
                    let knw = k_norm
                        .map(|k| stor(bytemuck::cast_slice(k)))
                        .unwrap_or_else(|| zeros(hd));
                    let gate_flag = if *output_gate { 1u32 } else { 0 };
                    c.queue.write_buffer(
                        &rope_u,
                        0,
                        bytemuck::cast_slice(&[
                            nh as u32,
                            nkv as u32,
                            hd as u32,
                            rd as u32,
                            position as u32,
                            flags(q_norm.is_some(), k_norm.is_some()) | gate_flag,
                            eps.to_bits(),
                            0,
                        ]),
                    );
                    // Gated wq emits 2·nh·hd (q||gate interleaved per head); the rope
                    // kernel splits it, roping q and passing gate through to `gout`.
                    let qrows = nh * hd * (1 + *output_gate as usize);
                    group_mats(
                        &mut enc,
                        &[
                            (wq, &n1, &qraw, qrows, hidden),
                            (wk, &n1, &kb, nkv * hd, hidden),
                            (wv, &n1, &vb, nkv * hd, hidden),
                        ],
                    );
                    if let Some((bq, bk, bv)) = bias {
                        let (bqb, bkb, bvb) = (
                            stor(bytemuck::cast_slice(bq)),
                            stor(bytemuck::cast_slice(bk)),
                            stor(bytemuck::cast_slice(bv)),
                        );
                        let axq = uniform_u32x4(c, [1.0f32.to_bits(), (nh * hd) as u32, 0, 0]);
                        let axkv = uniform_u32x4(c, [1.0f32.to_bits(), (nkv * hd) as u32, 0, 0]);
                        go(
                            &mut enc,
                            &c.axpy,
                            &bg(&c.layout_axpy, &[&bqb, &qraw, &axq]),
                            ((nh * hd) as u32).div_ceil(256),
                        );
                        go(
                            &mut enc,
                            &c.axpy,
                            &bg(&c.layout_axpy, &[&bkb, &kb, &axkv]),
                            ((nkv * hd) as u32).div_ceil(256),
                        );
                        go(
                            &mut enc,
                            &c.axpy,
                            &bg(&c.layout_axpy, &[&bvb, &vb, &axkv]),
                            ((nkv * hd) as u32).div_ceil(256),
                        );
                    }
                    if let Some(views) = o1_here {
                        // O(1) attention: rope as usual, then the three o1
                        // kernels replace kv_append + attend. State mirrors on
                        // the device once per seal epoch; kv mirrors are not
                        // touched for this layer at all.
                        if o1_ensure(c, kv_id, li, views, o1_epoch).is_none() {
                            graph_refused("o1 state not portable");
                            return false;
                        }
                        let (
                            dmeta,
                            drk,
                            drv,
                            dsk,
                            dsv,
                            dkt,
                            dqt,
                            dmu,
                            dmz,
                            dth,
                            gg,
                            hh_,
                            mm,
                            ww,
                            nns,
                            sc,
                        ) = {
                            let map = c.o1m.lock().unwrap();
                            let d = map.get(&(kv_id, li)).unwrap();
                            (
                                d.meta.clone(),
                                d.ring_k.clone(),
                                d.ring_v.clone(),
                                d.sink_k.clone(),
                                d.sink_v.clone(),
                                d.k_tilde.clone(),
                                d.qt.clone(),
                                d.mu.clone(),
                                d.mz.clone(),
                                d.that.clone(),
                                d.g,
                                d.h,
                                d.m,
                                d.w,
                                d.ns,
                                d.scale,
                            )
                        };
                        let rect_fm = views
                            .first()
                            .and_then(|v| v.heads.first())
                            .is_some_and(|h| h.rect_fm);
                        let o1_u = uniform_u32x8(
                            c,
                            [
                                hh_ as u32,
                                mm as u32,
                                ww as u32,
                                (nns as u32) | (u32::from(rect_fm) << 8),
                                hd as u32,
                                hd as u32,
                                sc.to_bits(),
                                0,
                            ],
                        );
                        let bg_rope = bg(
                            &c.layout_attn_rope,
                            &[&qraw, &kb, &qout, &gout, &qnw, &knw, &invf_b, &rope_u],
                        );
                        let bg_far = bg(
                            &c.layout_o1_far,
                            &[&dmeta, &drk, &drv, &dqt, &dmz, &dth, &o1_u],
                        );
                        let bg_push = bg(&c.layout_o1_push, &[&dmeta, &kb, &vb, &drk, &drv, &o1_u]);
                        let bg_att = bg(
                            &c.layout_o1_attend,
                            &[
                                &dmeta, &qout, &drk, &drv, &dsk, &dsv, &dkt, &dmu, &dmz, &dth,
                                &attn, &o1_u,
                            ],
                        );
                        let mut pass = begin_pass(&mut enc);
                        pass.set_pipeline(&c.attn_rope);
                        pass.set_bind_group(0, &bg_rope, &[]);
                        pass.dispatch_workgroups((nh + nkv) as u32, 1, 1);
                        pass.set_pipeline(&c.o1_far);
                        pass.set_bind_group(0, &bg_far, &[]);
                        pass.dispatch_workgroups((gg * hh_ * mm) as u32, 1, 1);
                        pass.set_pipeline(&c.o1_push);
                        pass.set_bind_group(0, &bg_push, &[]);
                        pass.dispatch_workgroups(gg as u32, 1, 1);
                        pass.set_pipeline(&c.o1_attend);
                        pass.set_bind_group(0, &bg_att, &[]);
                        pass.dispatch_workgroups((gg * hh_) as u32, 1, 1);
                        drop(pass);
                        if std::env::var("CMF_O1_TRACE").is_ok() {
                            // Debug-only: stage this layer's o1 attention output
                            // for a post-submit dump (the attn buffer itself is
                            // reused by every later layer).
                            let dbgb = c.device.create_buffer(&wgpu::BufferDescriptor {
                                label: Some("o1-dbg"),
                                size: (nh * hd * 4) as u64,
                                usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
                                mapped_at_creation: false,
                            });
                            enc.copy_buffer_to_buffer(&attn, 0, &dbgb, 0, (nh * hd * 4) as u64);
                            o1_dbg.push((li, dbgb));
                            let dbgq = c.device.create_buffer(&wgpu::BufferDescriptor {
                                label: Some("o1-dbg-q"),
                                size: 64,
                                usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
                                mapped_at_creation: false,
                            });
                            enc.copy_buffer_to_buffer(&qout, 0, &dbgq, 0, 16);
                            enc.copy_buffer_to_buffer(&kb, 0, &dbgq, 16, 16);
                            enc.copy_buffer_to_buffer(&vb, 0, &dbgq, 32, 16);
                            o1_dbg.push((li + 10_000, dbgq));
                        }
                    } else {
                        let (kbuf, vbuf) = kvbufs[li].as_ref().unwrap();
                        // rope + kv_append are independent (both read kb, neither
                        // writes it) — share ONE compute pass to avoid the inter-pass
                        // pipeline flush (~78 μs on NVIDIA Vulkan).
                        let n_ctx = position + 1;
                        // Short context (the decode regime): attend + output gate +
                        // O-projection ride the SAME pass as rope/kv when they prep —
                        // five passes become one, and dispatch order is unchanged.
                        let short_ctx = !(n_ctx > ATTEND_SPLIT_MIN && (hd <= 128 || c.big_attend));
                        if passfuse && short_ctx && !skip_attn {
                            attn_done = true;
                            let (mut ap, al) = attend_pipes(c, hd);
                            let dec_l;
                            let bg_att = if c.attend_dec && hd <= 256 {
                                ap = &c.gqa_attend_dec;
                                // Auto layouts are pipeline-exclusive — the twin's
                                // binding SET matches, its layout object does not.
                                dec_l = c.gqa_attend_dec.get_bind_group_layout(0);
                                bg(&dec_l, &[&qout, kbuf, vbuf, &attn, &at_u])
                            } else {
                                bg(al, &[&qout, kbuf, vbuf, &attn, &at_u])
                            };
                            let gm = if *output_gate {
                                let gm_u = uniform_u32x4(c, [(nh * hd) as u32, 0, 0, 0]);
                                Some(bg(&c.layout_gate_mul, &[&gout, &attn, &gm_u]))
                            } else {
                                None
                            };
                            let wo_prep = prep(wo, &attn, &ob, hidden, nh * hd);
                            {
                                let bg_rope = bg(
                                    &c.layout_attn_rope,
                                    &[&qraw, &kb, &qout, &gout, &qnw, &knw, &invf_b, &rope_u],
                                );
                                let bg_kv = bg(&c.layout_kv, &[&kb, &vb, kbuf, vbuf, &kv_u]);
                                let mut pass =
                                    begin_pass(&mut enc);
                                let fine = li < 4;
                                tsp!(pass, fine, 20); // pass start (after qkv projections)
                                pass.set_pipeline(&c.attn_rope);
                                pass.set_bind_group(0, &bg_rope, &[]);
                                pass.dispatch_workgroups((nh + nkv) as u32, 1, 1);
                                tsp!(pass, fine, 21); // rope
                                pass.set_pipeline(&c.kv_append);
                                pass.set_bind_group(0, &bg_kv, &[]);
                                pass.dispatch_workgroups(((nkv * hd) as u32).div_ceil(256), 1, 1);
                                tsp!(pass, fine, 22); // kv append
                                pass.set_pipeline(ap);
                                pass.set_bind_group(0, &bg_att, &[]);
                                pass.dispatch_workgroups(nh as u32, 1, 1);
                                tsp!(pass, fine, 23); // attend
                                if let Some(bg_gm) = &gm {
                                    pass.set_pipeline(&c.gate_mul);
                                    pass.set_bind_group(0, bg_gm, &[]);
                                    pass.dispatch_workgroups(
                                        ((nh * hd) as u32).div_ceil(256),
                                        1,
                                        1,
                                    );
                                }
                                tsp!(pass, fine, 24); // gate
                                if let Some((wp, wb, ww)) = &wo_prep {
                                    pass.set_pipeline(wp);
                                    pass.set_bind_group(0, wb, &[]);
                                    pass.dispatch_workgroups(*ww, 1, 1);
                                }
                                tsp!(pass, fine, 25); // o-proj
                            }
                            if wo_prep.is_none() {
                                emat(&mut enc, wo, &attn, &ob, hidden, nh * hd);
                            }
                        } else {
                            {
                                let bg_rope = bg(
                                    &c.layout_attn_rope,
                                    &[&qraw, &kb, &qout, &gout, &qnw, &knw, &invf_b, &rope_u],
                                );
                                let bg_kv = bg(&c.layout_kv, &[&kb, &vb, kbuf, vbuf, &kv_u]);
                                let mut pass =
                                    begin_pass(&mut enc);
                                pass.set_pipeline(&c.attn_rope);
                                pass.set_bind_group(0, &bg_rope, &[]);
                                pass.dispatch_workgroups((nh + nkv) as u32, 1, 1);
                                pass.set_pipeline(&c.kv_append);
                                pass.set_bind_group(0, &bg_kv, &[]);
                                pass.dispatch_workgroups(((nkv * hd) as u32).div_ceil(256), 1, 1);
                            }
                            if n_ctx > ATTEND_SPLIT_MIN && (hd <= 128 || c.big_attend) {
                                // Split-K attend: (nh × chunks) part workgroups + a
                                // per-head merge, both in ONE pass (WebGPU orders
                                // dispatches within a pass, so the merge sees the
                                // partials without an inter-pass flush).
                                let nc = cap.div_ceil(ATTEND_CK);
                                let nc_used = n_ctx.div_ceil(ATTEND_CK);
                                let pacc = GraphScratch::ensure(
                                    &c.device,
                                    &mut gs.apacc,
                                    (nh * nc * hd * 4) as u64,
                                    st,
                                    "g-apacc",
                                );
                                let pml = GraphScratch::ensure(
                                    &c.device,
                                    &mut gs.apml,
                                    (nh * nc * 8) as u64,
                                    st,
                                    "g-apml",
                                );
                                let ap_u = unif(&[
                                    nh as u32,
                                    (nh / nkv) as u32,
                                    hd as u32,
                                    cap as u32,
                                    n_ctx as u32,
                                    ATTEND_CK as u32,
                                    nc as u32,
                                    0,
                                ]);
                                let (pp, pl) = attend_part_pipes(c, hd);
                                let bg_part = bg(pl, &[&qout, kbuf, vbuf, &pacc, &pml, &ap_u]);
                                let bg_merge =
                                    c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                                        label: None,
                                        layout: &c.layout_attend_merge,
                                        entries: &[
                                            bind_buf(3, &pacc),
                                            bind_buf(4, &pml),
                                            bind_buf(5, &ap_u),
                                            bind_buf(6, &attn),
                                        ],
                                    });
                                let mut pass =
                                    begin_pass(&mut enc);
                                pass.set_pipeline(pp);
                                pass.set_bind_group(0, &bg_part, &[]);
                                pass.dispatch_workgroups(nh as u32, nc_used as u32, 1);
                                pass.set_pipeline(&c.attend_merge);
                                pass.set_bind_group(0, &bg_merge, &[]);
                                pass.dispatch_workgroups(nh as u32, 1, 1);
                            } else if !skip_attn {
                                let (ap, al) = attend_pipes(c, hd);
                                go(
                                    &mut enc,
                                    ap,
                                    &bg(al, &[&qout, kbuf, vbuf, &attn, &at_u]),
                                    nh as u32,
                                );
                            }
                        } // fused-vs-split attend arms
                        // attn_out *= sigmoid(gate) before the O projection.
                    }
                    if *output_gate && !attn_done {
                        let gm_u = uniform_u32x4(c, [(nh * hd) as u32, 0, 0, 0]);
                        go(
                            &mut enc,
                            &c.gate_mul,
                            &bg(&c.layout_gate_mul, &[&gout, &attn, &gm_u]),
                            ((nh * hd) as u32).div_ceil(256),
                        );
                    }
                    if !attn_done {
                        emat(&mut enc, wo, &attn, &ob, hidden, nh * hd);
                    }
                }
                (
                    LAttn::Gdn {
                        qkv,
                        z,
                        a,
                        b,
                        out,
                        nv,
                        nk,
                        dk,
                        dv,
                        kk,
                        cdim,
                    },
                    crate::gpu::GraphAttn::Gdn {
                        conv1d,
                        a_log,
                        dt_bias,
                        norm,
                        ..
                    },
                ) => {
                    let (ring, s) = gdnbufs[li].as_ref().unwrap();
                    let taps = stor(bytemuck::cast_slice(conv1d));
                    let alog = stor(bytemuck::cast_slice(a_log));
                    let dtb = stor(bytemuck::cast_slice(dt_bias));
                    let gnorm = stor(bytemuck::cast_slice(norm));
                    let gc_p = uniform_u32x4(c, [*cdim as u32, *kk as u32, 0, 0]);
                    let gd_p = unif(&[
                        *nv as u32,
                        *dk as u32,
                        *dv as u32,
                        (nk * dk) as u32,
                        (nv / nk) as u32,
                        *cdim as u32,
                        eps.to_bits(),
                        0,
                    ]);
                    let bg_conv = bg(&c.layout_gdn_conv, &[&qkv_b, &taps, ring, &cq_b, &gc_p]);
                    let bg_step = bg(
                        &c.layout_gdn,
                        &[
                            &cq_b, &z_b, &a_b, &b_b, &alog, &dtb, &gnorm, s, &gdo_b, &gd_p,
                        ],
                    );
                    // The parallel step/norm entries use SUBSETS of the gdn
                    // binding set, and an auto layout lists only what its entry
                    // reads — each gets its own bind group (lesson of the day).
                    let bg_par = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                        label: None,
                        layout: &c.gdn_step_par.get_bind_group_layout(0),
                        entries: &[
                            bind_buf(0, &cq_b),
                            bind_buf(2, &a_b),
                            bind_buf(3, &b_b),
                            bind_buf(4, &alog),
                            bind_buf(5, &dtb),
                            bind_buf(7, s),
                            bind_buf(8, &gdo_b),
                            bind_buf(9, &gd_p),
                        ],
                    });
                    let bg_snorm = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                        label: None,
                        layout: &c.gdn_step_norm.get_bind_group_layout(0),
                        entries: &[
                            bind_buf(1, &z_b),
                            bind_buf(6, &gnorm),
                            bind_buf(8, &gdo_b),
                            bind_buf(9, &gd_p),
                        ],
                    });
                    let gi_u = uniform_u32x4(c, [*kk as u32, 0, 0, 0]);
                    let (bg_par2, bg_snorm2) = if c.gdn_inline {
                        (
                            Some(c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                                label: None,
                                layout: &c.gdn_step_par2.get_bind_group_layout(0),
                                entries: &[
                                    bind_buf(2, &a_b),
                                    bind_buf(3, &b_b),
                                    bind_buf(4, &alog),
                                    bind_buf(5, &dtb),
                                    bind_buf(7, s),
                                    bind_buf(8, &gdo_b),
                                    bind_buf(9, &gd_p),
                                    bind_buf(10, &qkv_b),
                                    bind_buf(11, ring),
                                    bind_buf(12, &taps),
                                    bind_buf(13, &gi_u),
                                ],
                            })),
                            Some(c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                                label: None,
                                layout: &c.gdn_step_norm2.get_bind_group_layout(0),
                                entries: &[
                                    bind_buf(1, &z_b),
                                    bind_buf(6, &gnorm),
                                    bind_buf(8, &gdo_b),
                                    bind_buf(9, &gd_p),
                                    bind_buf(10, &qkv_b),
                                    bind_buf(11, ring),
                                    bind_buf(13, &gi_u),
                                ],
                            })),
                        )
                    } else {
                        (None, None)
                    };
                    // The whole GDN chain in ONE compute pass: projections →
                    // conv → step → out_proj. Each stage reads the previous
                    // stage's output, which is exactly what a pass guarantees
                    // (dispatches inside it are ordered, with memory visible
                    // between them — the same rule the fused SiLU FFN relies on).
                    //
                    // A pass, not a dispatch, is the unit that costs here:
                    // teaching `prep` the q4tp kind collapsed this layer's four
                    // projection passes into one and bought 1.46 ms a token
                    // across 30 layers — ~16 us per pass. Four passes become one.
                    let projs = [
                        prep(qkv, &n1, &qkv_b, *cdim, hidden),
                        prep(z, &n1, &z_b, nv * dv, hidden),
                        prep(a, &n1, &a_b, *nv, hidden),
                        prep(b, &n1, &b_b, *nv, hidden),
                    ];
                    let outp = prep(out, &gdo_b, &ob, hidden, nv * dv);
                    if projs.iter().all(|p| p.is_some()) && outp.is_some() {
                        let _ = (skip_proj, skip_outp);
                        let mut pass = begin_pass(&mut enc);
                        if !skip_proj {
                            for p in projs.iter().flatten() {
                                pass.set_pipeline(p.0);
                                pass.set_bind_group(0, &p.1, &[]);
                                pass.dispatch_workgroups(p.2, 1, 1);
                            }
                        }
                        let fine = li == 0;
                        tsp!(pass, fine, 10); // after projections
                        if !skip_gdn {
                            if c.gdn_par && c.gdn_inline {
                                pass.set_pipeline(&c.gdn_step_par2);
                                pass.set_bind_group(0, bg_par2.as_ref().unwrap(), &[]);
                                pass.dispatch_workgroups(*nv as u32, *dv as u32, 1);
                                tsp!(pass, fine, 12); // step_par (conv inline)
                                pass.set_pipeline(&c.gdn_step_norm2);
                                pass.set_bind_group(0, bg_snorm2.as_ref().unwrap(), &[]);
                                pass.dispatch_workgroups(*nv as u32, 1, 1);
                                tsp!(pass, fine, 13); // step_norm (+ring shift)
                            } else {
                                pass.set_pipeline(&c.gdn_conv);
                                pass.set_bind_group(0, &bg_conv, &[]);
                                pass.dispatch_workgroups((*cdim as u32).div_ceil(256), 1, 1);
                                tsp!(pass, fine, 11); // conv
                                if c.gdn_par {
                                    pass.set_pipeline(&c.gdn_step_par);
                                    pass.set_bind_group(0, &bg_par, &[]);
                                    pass.dispatch_workgroups(
                                        *nv as u32,
                                        (*dv as u32).div_ceil(4),
                                        1,
                                    );
                                    tsp!(pass, fine, 12); // step_par
                                    pass.set_pipeline(&c.gdn_step_norm);
                                    pass.set_bind_group(0, &bg_snorm, &[]);
                                    pass.dispatch_workgroups(*nv as u32, 1, 1);
                                    tsp!(pass, fine, 13); // step_norm
                                } else {
                                    pass.set_pipeline(&c.gdn_step);
                                    pass.set_bind_group(0, &bg_step, &[]);
                                    pass.dispatch_workgroups(*nv as u32, 1, 1);
                                    tsp!(pass, fine, 12);
                                }
                            } // gdn_inline arms
                        }
                        if !skip_outp {
                            let o = outp.as_ref().unwrap();
                            pass.set_pipeline(o.0);
                            pass.set_bind_group(0, &o.1, &[]);
                            pass.dispatch_workgroups(o.2, 1, 1);
                            tsp!(pass, fine, 14); // out-proj
                        }
                    } else {
                        group_mats(
                            &mut enc,
                            &[
                                (qkv, &n1, &qkv_b, *cdim, hidden),
                                (z, &n1, &z_b, nv * dv, hidden),
                                (a, &n1, &a_b, *nv, hidden),
                                (b, &n1, &b_b, *nv, hidden),
                            ],
                        );
                        go(
                            &mut enc,
                            &c.gdn_conv,
                            &bg_conv,
                            (*cdim as u32).div_ceil(256),
                        );
                        if c.gdn_par {
                            {
                                let mut pass =
                                    begin_pass(&mut enc);
                                pass.set_pipeline(&c.gdn_step_par);
                                pass.set_bind_group(0, &bg_par, &[]);
                                pass.dispatch_workgroups(*nv as u32, (*dv as u32).div_ceil(4), 1);
                                pass.set_pipeline(&c.gdn_step_norm);
                                pass.set_bind_group(0, &bg_snorm, &[]);
                                pass.dispatch_workgroups(*nv as u32, 1, 1);
                            }
                        } else {
                            go(&mut enc, &c.gdn_step, &bg_step, *nv as u32);
                        }
                        emat(&mut enc, out, &gdo_b, &ob, hidden, nv * dv);
                    }
                }
                _ => return false,
            }
            ts!(enc, 1, lkind);
            // token-mix residual + FFN-norm fused: h += ob, n1 = rms(h, post_norm).
            // It used to open its own compute pass. On this Vulkan stack a PASS
            // BOUNDARY is the expensive part (the MoE block is built entirely
            // around that fact), and this one sits between two passes that are
            // strictly serial anyway — so hand it to the FFN pass as a prologue
            // and let within-pass serialization do the same job for free.
            // CMF_PASSFUSE=0 puts it back in its own pass.
            let mut ffn_pre: Option<(&wgpu::ComputePipeline, wgpu::BindGroup, u32)> = None;
            if !skip_norm {
                let nbg = bg(&c.layout_add_rmsnorm, &[&h_buf, &ob, &pnw, &n1, &rms_u]);
                if passfuse {
                    ffn_pre = Some((&c.add_rmsnorm, nbg, 1));
                } else {
                    go(&mut enc, &c.add_rmsnorm, &nbg, 1);
                }
            }
            // …and the layer's TAIL (FFN residual + the next layer's input norm)
            // rides out on the same pass. It reads `ob`, which that pass's last
            // dispatch writes — the same within-pass ordering the block above
            // relies on. With both ends folded in, a layer is TWO passes
            // (token-mix, then FFN) instead of four.
            let simple_tail = passfuse && !loop_norm_at.contains(&li);
            let mut ffn_post: Option<(&wgpu::ComputePipeline, wgpu::BindGroup, u32)> = None;
            let mut tail_done = false;
            if simple_tail {
                ffn_post = Some(if li + 1 < layers.len() {
                    let inw_next = stor(bytemuck::cast_slice(layers[li + 1].input_norm));
                    (
                        &c.add_rmsnorm,
                        bg(
                            &c.layout_add_rmsnorm,
                            &[&h_buf, &ob, &inw_next, &n1, &rms_u],
                        ),
                        1,
                    )
                } else {
                    (
                        &c.axpy,
                        bg(&c.layout_axpy, &[&ob, &h_buf, &ax_u]),
                        (hidden as u32).div_ceil(256),
                    )
                });
            }
            // SiLU FFN: gate+up matvecs + silu fused in ONE compute pass
            // (dispatches within a pass are serialized — silu safely reads gate/up output).
            match &lw.ffn {
                LFfn::Dense { gate, up, down } => {
                    let pg = prep(gate, &n1, &gbuf, inter, hidden);
                    let pu = prep(up, &n1, &ubuf, inter, hidden);
                    if let (Some((pgp, bg_g, wg)), Some((pup, bg_u, wu))) = (pg, pu) {
                        let bg_silu =
                            bg(&c.layout_silu, &[&gbuf, &ubuf, &dummy_hd, &abuf, &silu_u]);
                        let mut pass = begin_pass(&mut enc);
                        if let Some((p, b, w)) = &ffn_pre {
                            pass.set_pipeline(p);
                            pass.set_bind_group(0, b, &[]);
                            pass.dispatch_workgroups(*w, 1, 1);
                        }
                        pass.set_pipeline(pgp);
                        pass.set_bind_group(0, &bg_g, &[]);
                        pass.dispatch_workgroups(wg, 1, 1);
                        pass.set_pipeline(pup);
                        pass.set_bind_group(0, &bg_u, &[]);
                        pass.dispatch_workgroups(wu, 1, 1);
                        pass.set_pipeline(&c.silu);
                        pass.set_bind_group(0, &bg_silu, &[]);
                        pass.dispatch_workgroups((inter as u32).div_ceil(256), 1, 1);
                        // NOTE: the dense arm still emits `down` outside this pass
                        // (see emat below), so the tail cannot ride here.
                    } else {
                        group_mats(
                            &mut enc,
                            &[
                                (gate, &n1, &gbuf, inter, hidden),
                                (up, &n1, &ubuf, inter, hidden),
                            ],
                        );
                        go(
                            &mut enc,
                            &c.silu,
                            &bg(&c.layout_silu, &[&gbuf, &ubuf, &dummy_hd, &abuf, &silu_u]),
                            (inter as u32).div_ceil(256),
                        );
                    }
                    emat(&mut enc, down, &abuf, &ob, hidden, inter);
                }
                LFfn::Moe {
                    router,
                    sgate,
                    gate_all,
                    up_all,
                    down_all,
                    n_exp,
                    top_k,
                    inter: mi,
                    norm_topk,
                    q4tp,
                    gu_q2,
                } => {
                    // The WHOLE MoE FFN — router + shared-gate matvecs, top-k
                    // select, fused gate+up+SiLU over the selected experts, and
                    // the weighted down accumulation into ob — rides in ONE
                    // compute pass: dispatches within a pass serialize with
                    // memory visibility (same guarantee the dense fused FFN
                    // uses), and the inter-pass pipeline flush (~78 µs on
                    // NVIDIA Vulkan) is what dominates a 40-layer decode.
                    let (mlogit, mslog, msel, mwt, mact) = moe_bufs.as_ref().unwrap();
                    let slots = *top_k + 1;
                    // sg_kind = 4 tells the select kernel to compute the shared
                    // gate itself; then the sgate matvec below is not encoded.
                    let sg_fold = sgate.kind == 4;
                    // Cached uniform: `unif` mints a fresh buffer per call, and one
                    // per MoE layer per token exhausted the device. hidden and the
                    // fold flag share the spare word.
                    let sel_u = uniform_u32x4(
                        c,
                        [
                            *n_exp as u32,
                            *top_k as u32,
                            *norm_topk as u32,
                            (hidden as u32) << 8 | u32::from(sg_fold) * 4,
                        ],
                    );
                    // Per-expert stride in u16 units: q4t is 9 per group flat,
                    // q4tp adds the row params and code planes on top of 8.
                    let mat16 = |rows: usize, cols: usize| -> u32 {
                        let n = if *q4tp {
                            cortiq_core::quant::expected_nbytes(
                                cortiq_core::TensorDtype::Q4TiledP,
                                &[rows, cols],
                            )
                            .unwrap_or(0)
                        } else {
                            rows * (cols / 32) * 18
                        };
                        (n / 2) as u32
                    };
                    // gate/up may be a HALF-WIDTH plane (q2tp experts against a
                    // q4tp down), so its per-expert stride is its own.
                    let gu_mat16 = if *gu_q2 {
                        (cortiq_core::quant::expected_nbytes(
                            cortiq_core::TensorDtype::Q2TiledP,
                            &[*mi, hidden],
                        )
                        .unwrap_or(0)
                            / 2) as u32
                    } else {
                        mat16(*mi, hidden)
                    };
                    // Eight words now: the fifth is the swiglu limit, zero
                    // for every architecture but DeepSeek-V4.
                    let gu_u = uniform_u32x8(
                        c,
                        [(hidden / 32) as u32, *mi as u32, slots as u32, gu_mat16, 0, 0, 0, 0],
                    );
                    let dn_u = uniform_u32x4(
                        c,
                        [
                            (*mi / 32) as u32,
                            hidden as u32,
                            slots as u32,
                            mat16(hidden, *mi),
                        ],
                    );
                    let (p_gu, p_dn, l_gu, l_dn) = if *gu_q2 {
                        (
                            &c.moe_gate_up_q2tp,
                            &c.moe_down_q4tp,
                            &c.layout_moe_gu_q2tp,
                            &c.layout_moe_dn_q4tp,
                        )
                    } else if *q4tp {
                        (
                            &c.moe_gate_up_q4tp,
                            &c.moe_down_q4tp,
                            &c.layout_moe_gu_q4tp,
                            &c.layout_moe_dn_q4tp,
                        )
                    } else {
                        (
                            &c.moe_gate_up,
                            &c.moe_down,
                            &c.layout_moe_gu,
                            &c.layout_moe_dn,
                        )
                    };
                    let bg_sel = bg(
                        &c.layout_moe_sel,
                        &[mlogit, mslog, msel, mwt, &sel_u, &sgate.buf, &n1],
                    );
                    let bg_sel_sg = c.moe_select_sg.as_ref().map(|p| {
                        let l = p.get_bind_group_layout(0);
                        bg(&l, &[mlogit, mslog, msel, mwt, &sel_u, &sgate.buf, &n1])
                    });
                    let bg_gu = bg(l_gu, &[gate_all, up_all, &n1, msel, mact, &gu_u]);
                    let bg_dn = bg(l_dn, &[down_all, mact, msel, mwt, &ob, &dn_u]);
                    let pr = prep(router, &n1, mlogit, *n_exp, hidden);
                    let ps = prep(sgate, &n1, mslog, 1, hidden);
                    let mut continue_moe_std = true;
                    // Fold-select (q2tp + folded shared gate): router feeds the
                    // gu/down twins DIRECTLY — the select hop and the sgate
                    // matvec disappear from the layer's dependency chain.
                    let fold = c.foldsel && *gu_q2 && sg_fold && !skip_router;
                    if fold {
                        if let Some((prp, bgr, wr)) = prep(router, &n1, mlogit, *n_exp, hidden) {
                            let mgf_u = uniform_u32x4(c, [*n_exp as u32, 0, 0, 0]);
                            let l_guf = c.moe_gate_up_q2tp_f.get_bind_group_layout(0);
                            let bg_guf = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                                label: None,
                                layout: &l_guf,
                                entries: &[
                                    bind_buf(0, gate_all),
                                    bind_buf(1, up_all),
                                    bind_buf(2, &n1),
                                    bind_buf(3, mlogit),
                                    bind_buf(4, mact),
                                    bind_buf(5, &gu_u),
                                    bind_buf(7, &mgf_u),
                                ],
                            });
                            let mdf_u = uniform_u32x4(
                                c,
                                [
                                    *n_exp as u32,
                                    *top_k as u32,
                                    *norm_topk as u32,
                                    (hidden as u32) << 8 | 4,
                                ],
                            );
                            let l_dnf = c.moe_down_q4tp_f.get_bind_group_layout(0);
                            let bg_dnf = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                                label: None,
                                layout: &l_dnf,
                                entries: &[
                                    bind_buf(0, down_all),
                                    bind_buf(1, mact),
                                    bind_buf(2, mlogit),
                                    bind_buf(3, &sgate.buf),
                                    bind_buf(4, &ob),
                                    bind_buf(5, &dn_u),
                                    bind_buf(6, &n1),
                                    bind_buf(7, &mdf_u),
                                ],
                            });
                            let mut pass = begin_pass(&mut enc);
                            if let Some((p, b, w)) = &ffn_pre {
                                pass.set_pipeline(p);
                                pass.set_bind_group(0, b, &[]);
                                pass.dispatch_workgroups(*w, 1, 1);
                            }
                            pass.set_pipeline(prp);
                            pass.set_bind_group(0, &bgr, &[]);
                            pass.dispatch_workgroups(wr, 1, 1);
                            if !skip_moe {
                                pass.set_pipeline(&c.moe_gate_up_q2tp_f);
                                pass.set_bind_group(0, &bg_guf, &[]);
                                pass.dispatch_workgroups(*mi as u32, slots as u32, 1);
                                pass.set_pipeline(&c.moe_down_q4tp_f);
                                pass.set_bind_group(0, &bg_dnf, &[]);
                                pass.dispatch_workgroups(hidden as u32, 1, 1);
                                if let Some((p, b, w)) = &ffn_post {
                                    pass.set_pipeline(p);
                                    pass.set_bind_group(0, b, &[]);
                                    pass.dispatch_workgroups(*w, 1, 1);
                                    tail_done = true;
                                }
                            }
                            drop(pass);
                            enc.copy_buffer_to_buffer(&n1, 0, &h_buf, 0, 0);
                            // (zero-length copy: keeps the borrow checker shape
                            // identical to the non-fold arm; no-op on device)
                            continue_moe_std = false;
                        }
                    }
                    if continue_moe_std {
                        if let (Some((prp, bgr, wr)), Some((psp, bgs, ws))) = (pr, ps) {
                            let mut pass = begin_pass(&mut enc);
                            if let Some((p, b, w)) = &ffn_pre {
                                pass.set_pipeline(p);
                                pass.set_bind_group(0, b, &[]);
                                pass.dispatch_workgroups(*w, 1, 1);
                            }
                            let fine = li == 0;
                            tsp!(pass, fine, 30); // pass start (after prologue norm)
                            if !skip_router {
                                pass.set_pipeline(prp);
                                pass.set_bind_group(0, &bgr, &[]);
                                pass.dispatch_workgroups(wr, 1, 1);
                            }
                            tsp!(pass, fine, 31); // router
                            if !sg_fold {
                                pass.set_pipeline(psp);
                                pass.set_bind_group(0, &bgs, &[]);
                                pass.dispatch_workgroups(ws, 1, 1);
                            }
                            if let Some(sgp) = &c.moe_select_sg {
                                // Same binding ORDER as the tree kernel's bg_sel —
                                // but its OWN layout (auto layouts are exclusive).
                                pass.set_pipeline(sgp);
                                pass.set_bind_group(0, bg_sel_sg.as_ref().unwrap(), &[]);
                                pass.dispatch_workgroups(1, 1, 1);
                            } else {
                                pass.set_pipeline(&c.moe_select);
                                pass.set_bind_group(0, &bg_sel, &[]);
                                pass.dispatch_workgroups(1, 1, 1);
                            }
                            tsp!(pass, fine, 32); // select
                            if !skip_moe {
                                pass.set_pipeline(p_gu);
                                pass.set_bind_group(0, &bg_gu, &[]);
                                pass.dispatch_workgroups(*mi as u32, slots as u32, 1);
                                tsp!(pass, fine, 33); // gate/up experts
                                pass.set_pipeline(p_dn);
                                pass.set_bind_group(0, &bg_dn, &[]);
                                pass.dispatch_workgroups(hidden as u32, 1, 1);
                                tsp!(pass, fine, 34); // down experts
                                if let Some((p, b, w)) = &ffn_post {
                                    pass.set_pipeline(p);
                                    pass.set_bind_group(0, b, &[]);
                                    pass.dispatch_workgroups(*w, 1, 1);
                                    tail_done = true;
                                }
                            }
                        } else {
                            // Un-preppable router dtype: per-op passes (correct, rare).
                            group_mats(
                                &mut enc,
                                &[
                                    (router, &n1, mlogit, *n_exp, hidden),
                                    (sgate, &n1, mslog, 1, hidden),
                                ],
                            );
                            go(&mut enc, &c.moe_select, &bg_sel, 1);
                            {
                                let mut pass =
                                    begin_pass(&mut enc);
                                pass.set_pipeline(p_gu);
                                pass.set_bind_group(0, &bg_gu, &[]);
                                pass.dispatch_workgroups(*mi as u32, slots as u32, 1);
                            }
                            go(&mut enc, p_dn, &bg_dn, hidden as u32);
                        }
                    } // continue_moe_std
                }
            }
            // FFN-residual + next layer's attn-norm fused (plain residual on the last).
            // At loop boundaries (Looped Transformer), insert final_norm between the
            // residual and the next iteration's input norm.
            ts!(enc, 2, lkind);
            if tail_done {
                // already emitted at the end of the FFN pass
            } else if li + 1 < layers.len() {
                if loop_norm_at.contains(&li) {
                    // h += ob; n1 = rms(h, final_norm); copy n1→h; n1 = rms(h, next_input_norm)
                    let fnw = stor(bytemuck::cast_slice(final_norm));
                    let inw_next = stor(bytemuck::cast_slice(layers[li + 1].input_norm));
                    go(
                        &mut enc,
                        &c.add_rmsnorm,
                        &bg(&c.layout_add_rmsnorm, &[&h_buf, &ob, &fnw, &n1, &rms_u]),
                        1,
                    );
                    enc.copy_buffer_to_buffer(&n1, 0, &h_buf, 0, (hidden * 4) as u64);
                    go(
                        &mut enc,
                        &c.rmsnorm,
                        &bg(&c.layout_rmsnorm, &[&h_buf, &inw_next, &n1, &rms_u]),
                        1,
                    );
                } else {
                    let inw_next = stor(bytemuck::cast_slice(layers[li + 1].input_norm));
                    go(
                        &mut enc,
                        &c.add_rmsnorm,
                        &bg(
                            &c.layout_add_rmsnorm,
                            &[&h_buf, &ob, &inw_next, &n1, &rms_u],
                        ),
                        1,
                    );
                }
            } else {
                go(
                    &mut enc,
                    &c.axpy,
                    &bg(&c.layout_axpy, &[&ob, &h_buf, &ax_u]),
                    (hidden as u32).div_ceil(256),
                );
            }
        }
        // ── Multi-step tail: final norm + lm_head + on-device argmax; the
        // winner's embedding becomes the next step's h. All inside the SAME
        // encoder — one submit carries every step.
        if multi {
            let (lm, lrows) = lm_pre.as_ref().unwrap();
            let lrows = *lrows;
            let lbuf = lbuf_pre.as_ref().unwrap();
            let fnw = stor(bytemuck::cast_slice(final_norm));
            go(
                &mut enc,
                &c.rmsnorm,
                &bg(&c.layout_rmsnorm, &[&h_buf, &fnw, &n1, &rms_u]),
                1,
            );
            emat(&mut enc, lm, &n1, lbuf, lrows, hidden);
            let am_u = uniform_u32x4(c, [lrows as u32, AM_PARTS, stp as u32, 0]);
            let l_ap = c.argmax_part.get_bind_group_layout(0);
            let bg_ap = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &l_ap,
                entries: &[
                    bind_buf(0, lbuf),
                    bind_buf(1, am_pv.as_ref().unwrap()),
                    bind_buf(2, am_pi.as_ref().unwrap()),
                    bind_buf(3, &am_u),
                ],
            });
            go(&mut enc, &c.argmax_part, &bg_ap, AM_PARTS);
            let l_af = c.argmax_final.get_bind_group_layout(0);
            let bg_af = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &l_af,
                entries: &[
                    bind_buf(0, am_pv.as_ref().unwrap()),
                    bind_buf(1, am_pi.as_ref().unwrap()),
                    bind_buf(2, ids_buf.as_ref().unwrap()),
                    bind_buf(3, &am_u),
                ],
            });
            go(&mut enc, &c.argmax_final, &bg_af, 1);
            if stp + 1 < steps {
                let (em, e_rows, mult) = emb_pre.as_ref().unwrap();
                let eg_u = uniform_u32x8(
                    c,
                    [
                        hidden as u32,
                        (hidden / 32) as u32,
                        *e_rows as u32,
                        stp as u32,
                        mult.to_bits(),
                        0,
                        0,
                        0,
                    ],
                );
                let l_eg = c.embed_gather_q4tp.get_bind_group_layout(0);
                let bg_eg = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &l_eg,
                    entries: &[
                        bind_buf(0, &em.buf),
                        bind_buf(1, ids_buf.as_ref().unwrap()),
                        bind_buf(2, &h_buf),
                        bind_buf(3, &eg_u),
                    ],
                });
                go(
                    &mut enc,
                    &c.embed_gather_q4tp,
                    &bg_eg,
                    (hidden as u32).div_ceil(256),
                );
            }
        }
    } // for stp (multi-step frames)
    let t_enc = t_enc0.elapsed().as_secs_f64() * 1000.0;
    let t_sub0 = std::time::Instant::now();
    // Return the step-slot uniforms to the scratch pool.
    gs.kv_us = kv_us;
    gs.at_us = at_us;
    gs.rope_us = rope_us;
    // ── Multi-step exit: one submit, one k×u32 readback, no logits. ──
    if multi {
        let ids_b = ids_buf.as_ref().unwrap();
        let stage = ids_stage.as_ref().unwrap();
        let sz = (steps * 4) as u64;
        enc.copy_buffer_to_buffer(ids_b, 0, stage, 0, sz);
        submit(c, enc.finish());
        let (tx, rx) = std::sync::mpsc::channel();
        stage.map_async(wgpu::MapMode::Read, ..sz, move |r| {
            let _ = tx.send(r);
        });
        let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
        let ok = rx.recv().map(|r| r.is_ok()).unwrap_or(false);
        if ok {
            let raw = stage.get_mapped_range(..sz).unwrap();
            let ids: &[u32] = bytemuck::cast_slice(&raw);
            if let Some(out) = ids_out {
                out.clear();
                out.extend_from_slice(ids);
            }
            drop(raw);
        }
        stage.unmap();
        if ok {
            let mut kvm = c.attn_kv.lock().unwrap();
            for li in 0..layers.len() {
                if let Some(m) = kvm.get_mut(&(kv_id, li)) {
                    m.synced = position + steps;
                }
            }
            let mut gsm = c.gdn_state.lock().unwrap();
            let _ = &mut gsm; // states advanced on-device; nothing to sync
        }
        drop(gs);
        if prof {
            let setup = t_enc0.duration_since(t_start).as_secs_f64() * 1000.0;
            eprintln!(
                "token-graph[x{steps}]: setup {setup:.2} ms | encode {t_enc:.2} ms | submit+ids {:.2} ms",
                t_sub0.elapsed().as_secs_f64() * 1000.0
            );
        }
        return ok;
    }
    // h_buf now holds the final hidden. Either ride final-norm + lm_head and
    // read back logits, or (no lm / unresolved weight / device prefix) read
    // back the hidden — a prefix boundary is mid-stack, so no final norm.
    let lm_resolved = if prefix {
        None
    } else {
        lm_head.and_then(|(gw, rows)| resolve(gw, rows, hidden).map(|m| (m, rows)))
    };
    let ok = if let Some((lm, lrows)) = lm_resolved {
        let fnw = stor(bytemuck::cast_slice(final_norm));
        go(
            &mut enc,
            &c.rmsnorm,
            &bg(&c.layout_rmsnorm, &[&h_buf, &fnw, &n1, &rms_u]),
            1,
        );
        let lsize = (lrows * 4) as u64;
        let lbuf = GraphScratch::ensure(
            &c.device,
            &mut gs.logits,
            lsize,
            wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            "g-logits",
        );
        emat(&mut enc, &lm, &n1, &lbuf, lrows, hidden);
        ts!(enc, 3, 0);
        if let Some((qs, resolve, tstage)) = &c.ts_query {
            if steps == 1 && ts_n > 0 {
                enc.resolve_query_set(qs, 0..ts_n, resolve, 0);
                enc.copy_buffer_to_buffer(resolve, 0, tstage, 0, ts_n as u64 * 8);
            }
        }
        logits.resize(lrows, 0.0);
        let stage = GraphScratch::ensure(
            &c.device,
            &mut gs.stage,
            lsize,
            wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            "g-stage",
        );
        let r = readback(c, enc, &lbuf, &stage, lsize, &mut logits[..lrows]);
        drop(gs);
        r
    } else {
        let size = (hidden * 4) as u64;
        let stage = GraphScratch::ensure(
            &c.device,
            &mut gs.stage,
            size,
            wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            "g-stage",
        );
        let r = readback(c, enc, &h_buf, &stage, size, &mut h[..hidden]);
        drop(gs);
        r
    };
    if ok && ts_n > 1 {
        if let Some((_, _, tstage)) = &c.ts_query {
            let (tx, rx) = std::sync::mpsc::channel();
            tstage.map_async(wgpu::MapMode::Read, ..(ts_n as u64 * 8), move |r| {
                let _ = tx.send(r);
            });
            let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
            if rx.recv().map(|r| r.is_ok()).unwrap_or(false) {
                let raw = tstage.get_mapped_range(..(ts_n as u64 * 8)).unwrap();
                let t: Vec<u64> = bytemuck::cast_slice::<u8, u64>(&raw).to_vec();
                drop(raw);
                // Attribute each delta to the LATER stamp's (stage, kind).
                let mut agg = std::collections::BTreeMap::<(u8, u8), (f64, u32)>::new();
                for i in 1..ts_n as usize {
                    let dt = t[i].saturating_sub(t[i - 1]) as f64 * c.ts_period as f64 / 1000.0;
                    let e = agg.entry(ts_lbl[i]).or_insert((0.0, 0));
                    e.0 += dt;
                    e.1 += 1;
                }
                let name = |k: (u8, u8)| match k {
                    (1, 0) => "gdn-mix",
                    (1, 1) => "attn-mix",
                    (2, 0) => "ffn@gdn",
                    (2, 1) => "ffn@attn",
                    (3, _) => "tail(norm+lm)",
                    (10, _) => "|gdn:proj",
                    (11, _) => "|gdn:conv",
                    (12, _) => "|gdn:step",
                    (13, _) => "|gdn:snorm",
                    (14, _) => "|gdn:outp",
                    (20, _) => "|attn:qkv",
                    (21, _) => "|attn:rope",
                    (22, _) => "|attn:kv",
                    (23, _) => "|attn:attend",
                    (24, _) => "|attn:gate",
                    (25, _) => "|attn:wo",
                    (30, _) => "|moe:pre",
                    (31, _) => "|moe:router",
                    (32, _) => "|moe:select",
                    (33, _) => "|moe:gu",
                    (34, _) => "|moe:dn",
                    _ => "start",
                };
                let mut line = String::from("gpu-ts:");
                let total: f64 = agg.values().map(|v| v.0).sum();
                for (k, (us, n)) in &agg {
                    line.push_str(&format!(" {}={:.0}us/{}", name(*k), us, n));
                }
                line.push_str(&format!(" | total {:.2} ms", total / 1000.0));
                eprintln!("{line}");
            }
            tstage.unmap();
        }
    }
    if ok {
        for (li, b) in &o1_dbg {
            let (tx, rx) = std::sync::mpsc::channel();
            b.map_async(wgpu::MapMode::Read, .., move |r| {
                let _ = tx.send(r);
            });
            let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
            if rx.recv().map(|r| r.is_ok()).unwrap_or(false) {
                let raw = b.get_mapped_range(..).unwrap();
                let all: &[f32] = bytemuck::cast_slice(&raw);
                let v: Vec<f32> = all[..all.len().min(16)].to_vec();
                drop(raw);
                if *li >= 10_000 {
                    eprintln!(
                        "o1-trace L{} gpu q[..4]={:?} k[..4]={:?} v[..4]={:?}",
                        li - 10_000,
                        &v[..4],
                        &v[4..8],
                        &v[8..12]
                    );
                } else {
                    eprintln!("o1-trace L{li} gpu attn[..8] = {v:?}");
                }
            }
        }
        // The append at `position` is now durable — advance each mirror.
        let mut kvm = c.attn_kv.lock().unwrap();
        for li in 0..layers.len() {
            if let Some(m) = kvm.get_mut(&(kv_id, li)) {
                m.synced = position + 1;
            }
        }
    }
    if prof {
        let setup = t_enc0.duration_since(t_start).as_secs_f64() * 1000.0;
        eprintln!(
            "token-graph: setup {setup:.2} ms | encode {t_enc:.2} ms | submit+readback {:.2} ms",
            t_sub0.elapsed().as_secs_f64() * 1000.0
        );
    }
    ok
}

/// Batched prefill: K prompt positions through the whole layer stack in ONE
/// submit. Projections & FFN run as resident GEMMs (each weight read once per K
/// columns instead of once per position); attention and GDN loop the existing
/// per-position kernels over scratch slices (KV mirror / recurrent S persist).
/// Cuts graph prefill from N whole-graph submits to N/K. Returns false on any
/// unsupported case (bias, q4t/q1t projections) → caller keeps the per-position
/// graph. positions[i] = absolute sequence position of batch row i (contiguous
/// causal run starting at positions[0]); `h` is [k·hidden] in/out.
#[allow(clippy::too_many_arguments)]
pub fn forward_batch_graph(
    model: &Arc<CmfModel>,
    kv_id: u64,
    layers: &[crate::gpu::GraphLayer],
    invf: &[f32],
    h: &mut [f32],
    nh: usize,
    nkv: usize,
    hd: usize,
    rd: usize,
    hidden: usize,
    inter: usize,
    positions: &[usize],
    cap: usize,
    gemma: bool,
    eps: f32,
    k: usize,
    // Speculative verify: fold final-norm + lm_head over every position and
    // read the k logit rows back beside the hiddens, snapshotting the GDN
    // state after each position so a partial acceptance can restore it
    // (`gdn_spec_restore`). None = the plain batched prefill.
    mut spec: Option<crate::gpu::SpecTail<'_>>,
) -> bool {
    let t_bfn = std::time::Instant::now();
    let Some(c) = ctx() else {
        bgraph_refused("no ctx");
        return false;
    };
    if k == 0 || positions.len() != k {
        bgraph_refused("k/positions mismatch");
        return false;
    }
    let pos0 = positions[0];
    if pos0 + k > cap || hd % 4 != 0 || hd > c.hd_cap {
        bgraph_refused("pos+k past cap, or head_dim not %4 / over hd_cap");
        return false; // vec4 K/V reads; hd_cap = workgroup-storage limit
    }
    struct GMat {
        buf: wgpu::Buffer,
        rs: Option<wgpu::Buffer>,
        kind: u8,
    }
    enum LAttn {
        Full {
            wq: GMat,
            wk: GMat,
            wv: GMat,
            wo: GMat,
        },
        Gdn {
            qkv: GMat,
            z: GMat,
            a: GMat,
            b: GMat,
            out: GMat,
            nv: usize,
            nk: usize,
            dk: usize,
            dv: usize,
            kk: usize,
            cdim: usize,
        },
    }
    /// Батчевый FFN слоя. MoE маршрутизируется ПО ТОКЕНАМ, поэтому его
    /// эксперты кодируются в цикле внутри того же submit'а, тогда как
    /// attention и проекции остаются батчевыми GEMM'ами. Раньше здесь
    /// допускался только Dense, и любая MoE-модель уходила на путь
    /// «одна позиция за submit»: префилл 33 tok/s против 54 на декоде,
    /// то есть промпт обрабатывался медленнее, чем генерация.
    enum BFfn {
        Dense {
            gate: GMat,
            up: GMat,
            down: GMat,
        },
        Moe {
            router: GMat,
            sgate: GMat,
            gate_all: wgpu::Buffer,
            up_all: wgpu::Buffer,
            down_all: wgpu::Buffer,
            n_exp: usize,
            top_k: usize,
            inter: usize,
            norm_topk: bool,
            q4tp: bool,
        },
    }
    struct LW {
        attn: LAttn,
        ffn: BFfn,
    }
    let resolve = |gw: &crate::gpu::GraphW, rows: usize, cols: usize| -> Option<GMat> {
        match gw.kind {
            0 => {
                if gw.row_scale.len() < rows {
                    return None;
                }
                let b = tensor_weight(c, model, gw.idx, rows, cols)?;
                let rsb = c.device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some("bg-rs"),
                    size: (rows * 4) as u64,
                    usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                });
                c.queue
                    .write_buffer(&rsb, 0, bytemuck::cast_slice(&gw.row_scale[..rows]));
                Some(GMat {
                    buf: b,
                    rs: Some(rsb),
                    kind: 0,
                })
            }
            1 => {
                let (b, r, cc) = q1_weight(c, model, gw.idx)?;
                if r != rows || cc != cols {
                    return None;
                }
                Some(GMat {
                    buf: b,
                    rs: None,
                    kind: 1,
                })
            }
            4 => {
                if gw.data.len() < rows * cols {
                    return None;
                }
                let b = c.device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some("bg-f32w"),
                    size: (rows * cols * 4) as u64,
                    usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                });
                c.queue
                    .write_buffer(&b, 0, bytemuck::cast_slice(&gw.data[..rows * cols]));
                Some(GMat {
                    buf: b,
                    rs: None,
                    kind: 4,
                })
            }
            // q4_tiled and q4tp: same buffer shape, the kernel differs.
            // Leaving these out is what kept every q4t/q4tp model off the
            // batched path — including its GDN projections, which is where
            // the refusal actually landed.
            k @ (5 | 6) => {
                let (b, r, cc) = tile_weight(c, model, gw.idx)?;
                if r != rows || cc != cols {
                    return None;
                }
                Some(GMat {
                    buf: b,
                    rs: None,
                    kind: k,
                })
            }
            _ => None, // q1t not batched here → CPU/per-position path
        }
    };
    // GEMM-able projection? (q8_row/q1). f32 (a/b) is per-position; anything else bails.
    // kinds 5/6 (q4_tiled, q4tp) have tile GEMMs too — admitting only 0/1
    // is what kept every q4tp model off the batched path.
    let gemmable = |m: &GMat| matches!(m.kind, 0 | 1 | 5 | 6);
    let mut lws = Vec::with_capacity(layers.len());
    let mut gdn_dims: Option<(usize, usize, usize, usize, usize, usize)> = None;
    for l in layers {
        let attn = match &l.attn {
            crate::gpu::GraphAttn::Full {
                wq,
                wk,
                wv,
                wo,
                output_gate,
                bias,
                ..
            } => {
                if bias.is_some() {
                    bgraph_refused("site:5889");
                    return false;
                } // batched bias axpy not wired
                let qrows = nh * hd * (1 + *output_gate as usize);
                let (Some(wq), Some(wk), Some(wv), Some(wo)) = (
                    resolve(wq, qrows, hidden),
                    resolve(wk, nkv * hd, hidden),
                    resolve(wv, nkv * hd, hidden),
                    resolve(wo, hidden, nh * hd),
                ) else {
                    bgraph_refused("site:5898");
                    return false;
                };
                if !(gemmable(&wq) && gemmable(&wk) && gemmable(&wv) && gemmable(&wo)) {
                    bgraph_refused("attention weights not gemmable");
                    return false;
                }
                LAttn::Full { wq, wk, wv, wo }
            }
            crate::gpu::GraphAttn::Gdn {
                qkv,
                z,
                a,
                b,
                out,
                nv,
                nk,
                dk,
                dv,
                kk,
                ..
            } => {
                let cdim = 2 * nk * dk + nv * dv;
                gdn_dims = Some((*nv, *nk, *dk, *dv, *kk, cdim));
                let (Some(qkv), Some(z), Some(a), Some(b), Some(out)) = (
                    resolve(qkv, cdim, hidden),
                    resolve(z, nv * dv, hidden),
                    resolve(a, *nv, hidden),
                    resolve(b, *nv, hidden),
                    resolve(out, hidden, nv * dv),
                ) else {
                    bgraph_refused("site:5928");
                    return false;
                };
                if !(gemmable(&qkv) && gemmable(&z) && gemmable(&out) && a.kind == 4 && b.kind == 4)
                {
                    bgraph_refused("site:5932");
                    return false;
                }
                LAttn::Gdn {
                    qkv,
                    z,
                    a,
                    b,
                    out,
                    nv: *nv,
                    nk: *nk,
                    dk: *dk,
                    dv: *dv,
                    kk: *kk,
                    cdim,
                }
            }
        };
        let bffn = match &l.ffn {
            crate::gpu::GraphFfn::Dense {
                gate: lg,
                up: lu,
                down: ld,
            } => {
                let (Some(gate), Some(up), Some(down)) = (
                    resolve(lg, inter, hidden),
                    resolve(lu, inter, hidden),
                    resolve(ld, hidden, inter),
                ) else {
                    bgraph_refused("site:5960");
                    return false;
                };
                if !(gemmable(&gate) && gemmable(&up) && gemmable(&down)) {
                    bgraph_refused("dense FFN not gemmable");
                    return false;
                }
                BFfn::Dense { gate, up, down }
            }
            crate::gpu::GraphFfn::Moe {
                router,
                shared_gate,
                experts,
                n_exp,
                top_k,
                inter: mi,
                norm_topk,
                q4tp,
                gu_q2,
            } => {
                if *top_k >= 16 || *n_exp > 256 || experts.len() != n_exp + 1 {
                    bgraph_refused("site:5979");
                    return false;
                }
                let (Some(router), Some(sgate)) = (
                    resolve(router, *n_exp, hidden),
                    resolve(shared_gate, 1, hidden),
                ) else {
                    bgraph_refused("site:5985");
                    return false;
                };
                let Some((gate_all, up_all, down_all)) =
                    moe_expert_bufs(c, model, experts, *mi, hidden, *q4tp, false, false)
                else {
                    bgraph_refused("site:5990");
                    return false;
                };
                BFfn::Moe {
                    router,
                    sgate,
                    gate_all,
                    up_all,
                    down_all,
                    n_exp: *n_exp,
                    top_k: *top_k,
                    inter: *mi,
                    norm_topk: *norm_topk,
                    q4tp: *q4tp,
                }
            }
        };
        lws.push(LW { attn, ffn: bffn });
    }
    let stor = |data: &[u8]| {
        let b = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: None,
            size: data.len() as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        c.queue.write_buffer(&b, 0, data);
        b
    };
    let unif = |d: &[u32]| {
        c.device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: None,
                contents: bytemuck::cast_slice(d),
                usage: wgpu::BufferUsages::UNIFORM,
            })
    };
    let bg = |layout: &wgpu::BindGroupLayout, bufs: &[&wgpu::Buffer]| {
        let e: Vec<_> = bufs
            .iter()
            .enumerate()
            .map(|(i, b)| bind_buf(i as u32, b))
            .collect();
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout,
            entries: &e,
        })
    };
    // Buffers usable both as compute storage and copy src/dst (K-loop slicing).
    let rwc = |n: usize| {
        c.device.create_buffer(&wgpu::BufferDescriptor {
            label: None,
            size: (n.max(1) * 4) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_SRC
                | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        })
    };
    let h_buf = rwc(k * hidden);
    c.queue
        .write_buffer(&h_buf, 0, bytemuck::cast_slice(&h[..k * hidden]));
    let n1 = rwc(k * hidden);
    let any_gate = layers.iter().any(|l| {
        matches!(
            &l.attn,
            crate::gpu::GraphAttn::Full {
                output_gate: true,
                ..
            }
        )
    });
    let qdim = nh * hd * (1 + any_gate as usize);
    let (gnv, _gnk, gdk, gdv, _gkk, gcdim) = gdn_dims.unwrap_or((1, 1, 1, 1, 1, 1));
    // batched GEMM outputs
    let qraw_b = rwc(k * qdim);
    let kb_b = rwc(k * nkv * hd);
    let vb_b = rwc(k * nkv * hd);
    let attn_bb = rwc(k * nh * hd);
    let qkv_b = rwc(k * gcdim);
    let z_b = rwc(k * gnv * gdv);
    let gdo_b = rwc(k * gnv * gdv);
    let ob = rwc(k * hidden);
    let gbuf = rwc(k * inter);
    let ubuf = rwc(k * inter);
    let abuf = rwc(k * inter);
    // per-position scratch
    let n1_s = rwc(hidden);
    let qraw_s = rwc(qdim);
    let kb_s = rwc(nkv * hd);
    let vb_s = rwc(nkv * hd);
    let qout_s = rwc(nh * hd);
    let gout_s = rwc(nh * hd);
    let attn_s = rwc(nh * hd);
    let qkv_s = rwc(gcdim);
    // k rows for the k-looped conv/step twins — row i at i*cdim.
    let cq_s = rwc(k * gcdim);
    let z_s = rwc(gnv * gdv);
    let a_s = rwc(gnv);
    let b_s = rwc(gnv);
    // Whole-batch a/b planes: one token-axis matvec per layer fills them,
    // and gdn_step reads its token's row via GdnP.tok.
    let a_bb = rwc(k * gnv);
    let b_bb = rwc(k * gnv);
    let gdo_s = rwc(gnv * gdv);
    let invf_b = stor(bytemuck::cast_slice(invf));
    let dummy_hd = stor(bytemuck::cast_slice(&vec![0f32; hd]));
    // KV mirror + GDN state (fresh; batch appends positions pos0..pos0+k).
    let mut kvbufs: Vec<Option<(wgpu::Buffer, wgpu::Buffer)>> = Vec::with_capacity(layers.len());
    let mut gdnbufs: Vec<Option<(wgpu::Buffer, wgpu::Buffer)>> = Vec::with_capacity(layers.len());
    {
        let mut kvm = c.attn_kv.lock().unwrap();
        let mut gsm = c.gdn_state.lock().unwrap();
        for (li, l) in layers.iter().enumerate() {
            match &l.attn {
                crate::gpu::GraphAttn::Full { .. } => {
                    let e = kvm.entry((kv_id, li)).or_insert_with(|| {
                        let sz = (nkv * cap * hd * 4) as u64;
                        let mk = || {
                            c.device.create_buffer(&wgpu::BufferDescriptor {
                                label: Some("kv"),
                                size: sz,
                                usage: wgpu::BufferUsages::STORAGE
                                    | wgpu::BufferUsages::COPY_DST
                                    | wgpu::BufferUsages::COPY_SRC,
                                mapped_at_creation: false,
                            })
                        };
                        KvMirror {
                            k: mk(),
                            v: mk(),
                            synced: 0,
                        }
                    });
                    kvbufs.push(Some((e.k.clone(), e.v.clone())));
                    gdnbufs.push(None);
                }
                crate::gpu::GraphAttn::Gdn { .. } => {
                    let e = gsm.entry((kv_id, li)).or_insert_with(|| {
                        let ring_sz = (gcdim * (_gkk.max(1).saturating_sub(1)) * 4) as u64;
                        let s_sz = (gnv * gdk * gdv * 4) as u64;
                        let mk = |sz: u64| {
                            let bf = c.device.create_buffer(&wgpu::BufferDescriptor {
                                label: Some("gdn-state"),
                                size: sz.max(4),
                                usage: wgpu::BufferUsages::STORAGE
                                    | wgpu::BufferUsages::COPY_DST
                                    | wgpu::BufferUsages::COPY_SRC,
                                mapped_at_creation: false,
                            });
                            c.queue.write_buffer(&bf, 0, &vec![0u8; sz.max(4) as usize]);
                            bf
                        };
                        (mk(ring_sz), mk(s_sz))
                    });
                    gdnbufs.push(Some((e.0.clone(), e.1.clone())));
                    kvbufs.push(None);
                }
            }
        }
    }
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("batch-graph"),
        });
    let go =
        |enc: &mut wgpu::CommandEncoder, p: &wgpu::ComputePipeline, b: &wgpu::BindGroup, g: u32| {
            let mut pass = begin_pass(enc);
            pass.set_pipeline(p);
            pass.set_bind_group(0, b, &[]);
            pass.dispatch_workgroups(g, 1, 1);
        };
    let flags = |qn: bool, kn: bool| {
        (if qn { 2u32 } else { 0 }) | (if kn { 4 } else { 0 }) | (if gemma { 8 } else { 0 })
    };
    let rms_u = unif(&[hidden as u32, if gemma { 1 } else { 0 }, eps.to_bits(), 0]);
    let silu_u = unif(&[(k * inter) as u32, 0, 0, 0]);
    // Batched GEMM matvec (q8_row / q1) into a [k·rows] output.
    let ematb = |enc: &mut wgpu::CommandEncoder,
                 m: &GMat,
                 xs: &wgpu::Buffer,
                 y: &wgpu::Buffer,
                 rows: usize,
                 cols: usize| {
        match m.kind {
            0 => encode_q8_mm(c, enc, &m.buf, m.rs.as_ref().unwrap(), xs, y, rows, cols, k),
            5 => encode_q4_tile_mm(c, enc, &c.q4t_mm, &m.buf, xs, y, rows, cols, k),
            6 => {
                // The 64-wide GEMM tile wastes a small batch (a k=3 verify
                // keeps 3 rows of 64 busy). The batched matvec kernel
                // streams the weight once per batch element with the batch
                // as the fast dispatch axis — its other callers proved it;
                // the tile GEMM keeps the big-chunk prefill.
                if k <= 4 && cols / 32 <= 64 && c.use_mv4 {
                    // Narrow rows only: with eight x vec4 fetches PER BATCH
                    // ELEMENT in flight the register file overflows at the
                    // dense 17408-wide shapes and the amortized weight read
                    // buys nothing back (measured 470 us/layer against the
                    // pair kernel's 3x167). MoE-width experts keep the win.
                    let gpr = cols / 32;
                    let p_buf = q4tp_mv_params(c, gpr, rows, k);
                    let layout = c.q4tp_mv_k.get_bind_group_layout(0);
                    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                        label: None,
                        layout: &layout,
                        entries: &[
                            bind_buf(0, &m.buf),
                            bind_buf(2, y),
                            bind_buf(3, &p_buf),
                            bind_buf(4, &m.buf),
                            bind_buf(5, xs),
                        ],
                    });
                    let mut pass = begin_pass(enc);
                    pass.set_pipeline(&c.q4tp_mv_k);
                    pass.set_bind_group(0, &bind, &[]);
                    pass.dispatch_workgroups((rows as u32).div_ceil(8).min(MAX_WG), 1, 1);
                } else if k <= 16 && c.use_mv4 {
                    let _ = encode_q4tp_mv4_b(c, enc, &m.buf, xs, y, rows, cols, k);
                } else {
                    encode_q4_tile_mm(c, enc, &c.q4tp_mm, &m.buf, xs, y, rows, cols, k)
                }
            }
            _ => encode_q1_mm(c, enc, &m.buf, xs, y, rows, cols, k),
        }
    };
    // SINGLE-row matvec for the per-token stretches inside the batch (the MoE
    // router/gate run once per token). `ematb` bakes nb=k into the GEMM: fed a
    // one-row buffer it reads k rows past the end and writes k rows into a
    // one-row output — and it has no f32 arm at all, so a kind-4 router fell
    // into the q1 decoder. Both were enough to turn the answer into noise.
    let emat1 = |enc: &mut wgpu::CommandEncoder,
                 m: &GMat,
                 xs: &wgpu::Buffer,
                 y: &wgpu::Buffer,
                 rows: usize,
                 cols: usize| {
        match m.kind {
            0 => encode_matvec(c, enc, &m.buf, xs, m.rs.as_ref().unwrap(), y, rows, cols),
            1 => encode_matvec_q1(c, enc, &m.buf, xs, y, rows, cols),
            5 => {
                if c.use_mv4 {
                    let gpr = cols / 32;
                    let p_buf = uniform_u32x4(c, [gpr as u32, rows as u32, cols as u32, 0]);
                    let layout = c.q4t_mv8.get_bind_group_layout(0);
                    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                        label: None,
                        layout: &layout,
                        entries: &[
                            bind_buf(0, &m.buf),
                            bind_buf(2, y),
                            bind_buf(3, &p_buf),
                            bind_buf(4, &m.buf),
                            bind_buf(5, xs),
                        ],
                    });
                    let mut pass = begin_pass(enc);
                    pass.set_pipeline(&c.q4t_mv8);
                    pass.set_bind_group(0, &bind, &[]);
                    pass.dispatch_workgroups((rows as u32).div_ceil(8).min(MAX_WG), 1, 1);
                } else {
                    encode_q1t_like(c, enc, &c.q4t_mv, &m.buf, xs, y, rows, cols)
                }
            }
            6 => {
                if c.use_mv4 {
                    encode_q4tp_mv4(c, enc, &m.buf, xs, y, rows, cols)
                } else {
                    encode_q1t_like(c, enc, &c.q4tp_mv, &m.buf, xs, y, rows, cols)
                }
            }
            _ => encode_f32matvec(c, enc, &m.buf, xs, y, rows, cols),
        }
    };
    let cp =
        |enc: &mut wgpu::CommandEncoder,
         src: &wgpu::Buffer,
         so: usize,
         dst: &wgpu::Buffer,
         n: usize| enc.copy_buffer_to_buffer(src, (so * 4) as u64, dst, 0, (n * 4) as u64);
    // Однострочные срезы батча для MoE: его ядра написаны на ОДИН токен,
    // поэтому i-я строка копируется сюда, считается и уезжает обратно.
    let row_in = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("bg-row-in"),
        size: (hidden * 4) as u64,
        usage: wgpu::BufferUsages::STORAGE
            | wgpu::BufferUsages::COPY_DST
            | wgpu::BufferUsages::COPY_SRC,
        mapped_at_creation: false,
    });
    let row_out = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("bg-row-out"),
        size: (hidden * 4) as u64,
        usage: wgpu::BufferUsages::STORAGE
            | wgpu::BufferUsages::COPY_DST
            | wgpu::BufferUsages::COPY_SRC,
        mapped_at_creation: false,
    });
    let moe_bufs = lws.iter().find_map(|w| match &w.ffn {
        BFfn::Moe {
            n_exp,
            top_k,
            inter: mi,
            ..
        } => Some((*n_exp, *top_k + 1, *mi)),
        _ => None,
    });
    let moe_bufs = moe_bufs.map(|(mn, ms, mi)| {
        let mk = |n: usize, label: &str| {
            c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some(label),
                size: (n * 4).max(4) as u64,
                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            })
        };
        (
            mk(k * mn, "bg-mlogit"),
            mk(1, "bg-mslog"),
            mk(k * ms, "bg-msel"),
            mk(k * ms, "bg-mwt"),
            mk(k * ms * mi, "bg-mact"),
        )
    });
    let cpo =
        |enc: &mut wgpu::CommandEncoder,
         src: &wgpu::Buffer,
         dst: &wgpu::Buffer,
         dof: usize,
         n: usize| enc.copy_buffer_to_buffer(src, 0, dst, (dof * 4) as u64, (n * 4) as u64);
    // Bootstrap first layer's input norm over all k rows.
    let inw0 = stor(bytemuck::cast_slice(layers[0].input_norm));
    go(
        &mut enc,
        &c.rmsnorm_b,
        &bg(&c.layout_rmsnorm_b, &[&h_buf, &inw0, &n1, &rms_u]),
        k as u32,
    );
    // `CMF_BATCH_TS=1`: coarse GPU stage stamps over the batch — the
    // k-independent fixed cost lives somewhere in here and host timers
    // cannot see past the submit boundary.
    let bts_on = std::env::var("CMF_BATCH_TS").is_ok() && c.ts_query.is_some();
    let mut bts_lbl: Vec<u8> = Vec::new();
    macro_rules! bts {
        ($enc:expr, $lbl:expr) => {
            if bts_on {
                if let Some((qs, _, _)) = c.ts_query.as_ref() {
                    let n = bts_lbl.len() as u32;
                    if n < 250 {
                        $enc.write_timestamp(qs, n);
                        bts_lbl.push($lbl);
                    }
                }
            }
        };
    }
    bts!(enc, 0);
    // The token graph's pipelined submission, ported: the card starts the
    // first layers of the verify/prefill batch while the host still
    // encodes the rest. Timestamps must stay inside ONE submission-window
    // accounting, so the stamps simply ride whichever encoder is current.
    let bchunk = if graph_split_n() > 1 {
        layers.len().div_ceil(graph_split_n()).max(4)
    } else {
        usize::MAX
    };
    for (li, l) in layers.iter().enumerate() {
        if li > 0 && bchunk != usize::MAX && li % bchunk == 0 {
            let full = std::mem::replace(
                &mut enc,
                c.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
                    label: Some("batch-graph"),
                }),
            );
            c.queue.submit([full.finish()]);
        }
        let lw = &lws[li];
        let pnw = stor(bytemuck::cast_slice(l.post_norm));
        match (&lw.attn, &l.attn) {
            (
                LAttn::Full { wq, wk, wv, wo },
                crate::gpu::GraphAttn::Full {
                    q_norm,
                    k_norm,
                    output_gate,
                    ..
                },
            ) => {
                let (kbuf, vbuf) = kvbufs[li].as_ref().unwrap();
                let qnw = stor(bytemuck::cast_slice(q_norm.unwrap_or(&vec![0f32; hd])));
                let knw = stor(bytemuck::cast_slice(k_norm.unwrap_or(&vec![0f32; hd])));
                let qrows = nh * hd * (1 + *output_gate as usize);
                ematb(&mut enc, wq, &n1, &qraw_b, qrows, hidden);
                ematb(&mut enc, wk, &n1, &kb_b, nkv * hd, hidden);
                ematb(&mut enc, wv, &n1, &vb_b, nkv * hd, hidden);
                {
                    // ONE compute pass for every position: the loop's four
                    // dispatches per token each carried their own pass, and
                    // pass boundaries — not the math — were 4.3 of this
                    // stage's 5.4 ms. In-pass dispatch ordering already
                    // guarantees each sees the previous one's writes.
                    let mut pass = begin_pass(&mut enc);
                    for i in 0..k {
                        let p = positions[i];
                        let gate_flag = if *output_gate { 1u32 } else { 0 };
                        let rope_u = uniform_u32x8(
                            c,
                            [
                                nh as u32,
                                nkv as u32,
                                hd as u32,
                                rd as u32,
                                p as u32,
                                flags(q_norm.is_some(), k_norm.is_some()) | gate_flag,
                                eps.to_bits(),
                                i as u32,
                            ],
                        );
                        let kv_u = uniform_u32x4(
                            c,
                            [nkv as u32, hd as u32, cap as u32, (p | (i << 20)) as u32],
                        );
                        let at_u = unif(&[
                            nh as u32,
                            (nh / nkv) as u32,
                            hd as u32,
                            cap as u32,
                            (p + 1) as u32,
                            0,
                            0,
                            0,
                        ]);
                        pass.set_pipeline(&c.attn_rope);
                        pass.set_bind_group(
                            0,
                            &bg(
                                &c.layout_attn_rope,
                                &[
                                    &qraw_b, &kb_b, &qout_s, &gout_s, &qnw, &knw, &invf_b, &rope_u,
                                ],
                            ),
                            &[],
                        );
                        pass.dispatch_workgroups((nh + nkv) as u32, 1, 1);
                        pass.set_pipeline(&c.kv_append);
                        pass.set_bind_group(0, &bg(&c.layout_kv, &[&kb_b, &vb_b, kbuf, vbuf, &kv_u]), &[]);
                        pass.dispatch_workgroups(((nkv * hd) as u32).div_ceil(256), 1, 1);
                        let (ap, al) = attend_pipes(c, hd);
                        pass.set_pipeline(ap);
                        pass.set_bind_group(0, &bg(al, &[&qout_s, kbuf, vbuf, &attn_s, &at_u]), &[]);
                        pass.dispatch_workgroups(nh as u32, 1, 1);
                        if *output_gate {
                            let gm_u = unif(&[(nh * hd) as u32, 0, 0, 0]);
                            pass.set_pipeline(&c.gate_mul);
                            pass.set_bind_group(0, &bg(&c.layout_gate_mul, &[&gout_s, &attn_s, &gm_u]), &[]);
                            pass.dispatch_workgroups(((nh * hd) as u32).div_ceil(256), 1, 1);
                        }
                        encode_blit_p(&mut pass, c, &attn_s, &attn_bb, nh * hd, 0, i * nh * hd, None);
                    }
                }
                ematb(&mut enc, wo, &attn_bb, &ob, hidden, nh * hd);
                bts!(enc, 1);
            }
            (
                LAttn::Gdn {
                    qkv,
                    z,
                    a,
                    b,
                    out,
                    nv,
                    nk,
                    dk,
                    dv,
                    kk,
                    cdim,
                },
                crate::gpu::GraphAttn::Gdn {
                    conv1d,
                    a_log,
                    dt_bias,
                    norm,
                    ..
                },
            ) => {
                let (ring, s) = gdnbufs[li].as_ref().unwrap();
                // Speculative rounds: a snapshot slot per position, taken
                // right after this position's state advance. The buffer
                // lives per (kv_id, layer) and regrows if k does.
                let snap = if spec.is_some() && std::env::var("CMF_SPEC_NOSNAP").is_err() {
                    let ring_sz = (cdim * kk.saturating_sub(1) * 4) as u64;
                    let s_sz = (nv * dk * dv * 4) as u64;
                    let mut m = c.gdn_snap.lock().unwrap();
                    let e = m.entry((kv_id, li)).or_insert_with(|| {
                        let b = c.device.create_buffer(&wgpu::BufferDescriptor {
                            label: Some("gdn-snap"),
                            size: (k as u64 * (ring_sz + s_sz)).max(4),
                            usage: wgpu::BufferUsages::STORAGE
                                | wgpu::BufferUsages::COPY_DST
                                | wgpu::BufferUsages::COPY_SRC,
                            mapped_at_creation: false,
                        });
                        (b, ring_sz, s_sz, k)
                    });
                    if e.3 < k {
                        e.0 = c.device.create_buffer(&wgpu::BufferDescriptor {
                            label: Some("gdn-snap"),
                            size: (k as u64 * (ring_sz + s_sz)).max(4),
                            usage: wgpu::BufferUsages::STORAGE
                                | wgpu::BufferUsages::COPY_DST
                                | wgpu::BufferUsages::COPY_SRC,
                            mapped_at_creation: false,
                        });
                        e.3 = k;
                    }
                    Some((e.0.clone(), ring_sz, s_sz))
                } else {
                    None
                };
                let taps = stor(bytemuck::cast_slice(conv1d));
                let alog = stor(bytemuck::cast_slice(a_log));
                let dtb = stor(bytemuck::cast_slice(dt_bias));
                let gnorm = stor(bytemuck::cast_slice(norm));
                bts!(enc, 6);
                ematb(&mut enc, qkv, &n1, &qkv_b, *cdim, hidden);
                ematb(&mut enc, z, &n1, &z_b, nv * dv, hidden);
                let gc_p = unif(&[*cdim as u32, *kk as u32, 0, 0]);
                let gd_p = unif(&[
                    *nv as u32,
                    *dk as u32,
                    *dv as u32,
                    (nk * dk) as u32,
                    (nv / nk) as u32,
                    *cdim as u32,
                    eps.to_bits(),
                    0,
                ]);
                // Token offsets ride in the kernels' spare uniform words:
                // conv reads its token's qkv slice, step reads/writes its
                // token's z/output rows in the BATCH buffers. The staging
                // copies this replaces were 4 of the 8 commands per token
                // per GDN layer of a chunk.
                // a/b for EVERY token in one dispatch each — the per-token
                // matvecs were 1920 of the chunk's ~4800 remaining commands.
                let fb_u = uniform_u32x4(c, [hidden as u32, *nv as u32, 0, 0]);
                {
                    let mut pass = begin_pass(&mut enc);
                    for (w, y) in [(&a.buf, &a_bb), (&b.buf, &b_bb)] {
                        pass.set_pipeline(&c.f32_matvec_b);
                        pass.set_bind_group(0, &bg(&c.layout_f32b, &[w, &n1, y, &fb_u]), &[]);
                        pass.dispatch_workgroups((*nv as u32).min(MAX_WG), k as u32, 1);
                    }
                }
                bts!(enc, 7);
                {
                    // The position recurrence lives INSIDE two k-looped
                    // kernels: one dispatch of conv (columns independent —
                    // no barriers at all), one of step (heads independent),
                    // with each position's (ring, S) written straight into
                    // the snapshot buffer by the kernels themselves. The
                    // per-dispatch chain this replaces spent 7-8 ms of a
                    // 3-position verify on barrier drains.
                    let (snap_stride, ring_els, snap_buf) = match snap.as_ref() {
                        Some((b, ring_sz, s_sz)) => (
                            ((*ring_sz + *s_sz) / 4) as u32,
                            (*ring_sz / 4) as u32,
                            b.clone(),
                        ),
                        None => (0u32, 0u32, row_in.clone()),
                    };
                    let gc_pt = unif(&[
                        *cdim as u32,
                        *kk as u32,
                        k as u32,
                        snap_stride.min(1),
                        snap_stride,
                        0,
                        0,
                        0,
                    ]);
                    let gd_pt = unif(&[
                        *nv as u32,
                        *dk as u32,
                        *dv as u32,
                        (nk * dk) as u32,
                        (nv / nk) as u32,
                        *cdim as u32,
                        eps.to_bits(),
                        k as u32,
                        snap_stride,
                        ring_els,
                        0,
                        0,
                    ]);
                    let mut pass = begin_pass(&mut enc);
                    pass.set_pipeline(&c.gdn_conv_k);
                    pass.set_bind_group(
                        0,
                        &{
                            let l = c.gdn_conv_k.get_bind_group_layout(0);
                            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                                label: None,
                                layout: &l,
                                entries: &[
                                    bind_buf(0, &qkv_b),
                                    bind_buf(1, &taps),
                                    bind_buf(2, ring),
                                    bind_buf(3, &cq_s),
                                    bind_buf(4, &gc_pt),
                                    bind_buf(5, &snap_buf),
                                ],
                            })
                        },
                        &[],
                    );
                    pass.dispatch_workgroups((*cdim as u32).div_ceil(256), 1, 1);
                    pass.set_pipeline(&c.gdn_step_par_k);
                    pass.set_bind_group(
                        0,
                        &{
                            let l = c.gdn_step_par_k.get_bind_group_layout(0);
                            // The auto layout keeps only what the entry
                            // point touches: no z, no norm weight here.
                            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                                label: None,
                                layout: &l,
                                entries: &[
                                    bind_buf(0, &cq_s),
                                    bind_buf(2, &a_bb),
                                    bind_buf(3, &b_bb),
                                    bind_buf(4, &alog),
                                    bind_buf(5, &dtb),
                                    bind_buf(7, s),
                                    bind_buf(8, &gdo_b),
                                    bind_buf(9, &gd_pt),
                                    bind_buf(10, &snap_buf),
                                ],
                            })
                        },
                        &[],
                    );
                    pass.dispatch_workgroups(*nv as u32, (*dv as u32).div_ceil(4), 1);
                    pass.set_pipeline(&c.gdn_step_norm_k);
                    pass.set_bind_group(
                        0,
                        &{
                            let l = c.gdn_step_norm_k.get_bind_group_layout(0);
                            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                                label: None,
                                layout: &l,
                                entries: &[
                                    bind_buf(1, &z_b),
                                    bind_buf(6, &gnorm),
                                    bind_buf(8, &gdo_b),
                                    bind_buf(9, &gd_pt),
                                ],
                            })
                        },
                        &[],
                    );
                    pass.dispatch_workgroups(*nv as u32, 1, 1);
                }
                bts!(enc, 5);
                ematb(&mut enc, out, &gdo_b, &ob, hidden, nv * dv);
                bts!(enc, 2);
            }
            _ => return false,
        }
        go(
            &mut enc,
            &c.add_rmsnorm_b,
            &bg(&c.layout_add_rmsnorm_b, &[&h_buf, &ob, &pnw, &n1, &rms_u]),
            k as u32,
        );
        match &lw.ffn {
            BFfn::Dense { gate, up, down } => {
                ematb(&mut enc, gate, &n1, &gbuf, inter, hidden);
                ematb(&mut enc, up, &n1, &ubuf, inter, hidden);
                go(
                    &mut enc,
                    &c.silu,
                    &bg(&c.layout_silu, &[&gbuf, &ubuf, &dummy_hd, &abuf, &silu_u]),
                    ((k * inter) as u32).div_ceil(256),
                );
                ematb(&mut enc, down, &abuf, &ob, hidden, inter);
            }
            // Routing is per token, so the experts run token by token —
            // but inside THIS submit, next to the batched attention and
            // projections. Same four kernels the token graph uses, fed a
            // one-row slice of the batch and writing one row back.
            BFfn::Moe {
                router,
                sgate,
                gate_all,
                up_all,
                down_all,
                n_exp,
                top_k,
                inter: mi,
                norm_topk,
                q4tp,
            } => {
                let (mlogit, mslog, msel, mwt, mact) = moe_bufs.as_ref().unwrap();
                let mut continue_ffn = true;
                let slots = *top_k + 1;
                let mat16 = |rows: usize, cols: usize| -> u32 {
                    let n = if *q4tp {
                        cortiq_core::quant::expected_nbytes(
                            cortiq_core::TensorDtype::Q4TiledP,
                            &[rows, cols],
                        )
                        .unwrap_or(0)
                    } else {
                        rows * (cols / 32) * 18
                    };
                    (n / 2) as u32
                };
                let sg_fold = sgate.kind == 4;
                let sel_u = uniform_u32x4(
                    c,
                    [
                        *n_exp as u32,
                        *top_k as u32,
                        *norm_topk as u32,
                        (hidden as u32) << 8 | u32::from(sg_fold) * 4,
                    ],
                );
                let gu_u = uniform_u32x8(
                    c,
                    [(hidden / 32) as u32, *mi as u32, slots as u32, mat16(*mi, hidden), 0, 0, 0, 0],
                );
                let dn_u = uniform_u32x4(
                    c,
                    [
                        (*mi / 32) as u32,
                        hidden as u32,
                        slots as u32,
                        mat16(hidden, *mi),
                    ],
                );
                let (p_gu, p_dn, l_gu, l_dn) = if *q4tp {
                    (
                        &c.moe_gate_up_q4tp,
                        &c.moe_down_q4tp,
                        &c.layout_moe_gu_q4tp,
                        &c.layout_moe_dn_q4tp,
                    )
                } else {
                    (
                        &c.moe_gate_up,
                        &c.moe_down,
                        &c.layout_moe_gu,
                        &c.layout_moe_dn,
                    )
                };
                {
                    use std::sync::atomic::{AtomicBool, Ordering};
                    static SAID: AtomicBool = AtomicBool::new(false);
                    if !SAID.swap(true, Ordering::Relaxed)
                        && std::env::var("CMF_GRAPH_SPEC_TIME").is_ok()
                    {
                        eprintln!(
                            "batch-moe path: q4tp={} router.kind={} sgate.kind={} n_exp={}",
                            q4tp, router.kind, sgate.kind, n_exp
                        );
                    }
                }
                if *q4tp && router.kind == 4 && sgate.kind == 4 && *n_exp <= 256 {
                    // Uniform q4tp experts + f32 router/gate: k router
                    // matvecs (offset bindings, no staging rows) plus THREE
                    // token-axis dispatches for select/experts/down. The
                    // loop below is ~7 commands per token per layer and
                    // clocks the chunk at per-position speed.
                    // Router for every token in ONE dispatch; per-row math is
                    // f32_matvec verbatim, so the logits stay bit-identical.
                    let fr_u = uniform_u32x4(c, [hidden as u32, *n_exp as u32, 0, 0]);
                    {
                        let mut pass = begin_pass(&mut enc);
                        pass.set_pipeline(&c.f32_matvec_b);
                        pass.set_bind_group(
                            0,
                            &bg(&c.layout_f32b, &[&router.buf, &n1, mlogit, &fr_u]),
                            &[],
                        );
                        pass.dispatch_workgroups((*n_exp as u32).min(MAX_WG), k as u32, 1);
                    }
                    let bg_sel = bg(
                        &c.layout_moe_sel_b,
                        &[mlogit, &n1, msel, mwt, &sel_u, &sgate.buf],
                    );
                    let bg_gu = bg(
                        &c.layout_moe_gu_b,
                        &[gate_all, up_all, &n1, msel, mact, &gu_u],
                    );
                    let bg_dn = bg(&c.layout_moe_dn_b, &[down_all, mact, msel, mwt, &ob, &dn_u]);
                    let mut pass = begin_pass(&mut enc);
                    pass.set_pipeline(&c.moe_select_b);
                    pass.set_bind_group(0, &bg_sel, &[]);
                    pass.dispatch_workgroups(k as u32, 1, 1);
                    pass.set_pipeline(&c.moe_gate_up_q4tp_b);
                    pass.set_bind_group(0, &bg_gu, &[]);
                    pass.dispatch_workgroups(*mi as u32, slots as u32, k as u32);
                    pass.set_pipeline(&c.moe_down_q4tp_b);
                    pass.set_bind_group(0, &bg_dn, &[]);
                    pass.dispatch_workgroups(hidden as u32, k as u32, 1);
                    drop(pass);
                    continue_ffn = false;
                }
                if continue_ffn {
                    for i in 0..k {
                        cp(&mut enc, &n1, i * hidden, &row_in, hidden);
                        let bg_sel = bg(
                            &c.layout_moe_sel,
                            &[mlogit, mslog, msel, mwt, &sel_u, &sgate.buf, &row_in],
                        );
                        let bg_gu = bg(l_gu, &[gate_all, up_all, &row_in, msel, mact, &gu_u]);
                        let bg_dn = bg(l_dn, &[down_all, mact, msel, mwt, &row_out, &dn_u]);
                        emat1(&mut enc, router, &row_in, mlogit, *n_exp, hidden);
                        if !sg_fold {
                            emat1(&mut enc, sgate, &row_in, mslog, 1, hidden);
                        }
                        let mut pass = begin_pass(&mut enc);
                        pass.set_pipeline(&c.moe_select);
                        pass.set_bind_group(0, &bg_sel, &[]);
                        pass.dispatch_workgroups(1, 1, 1);
                        pass.set_pipeline(p_gu);
                        pass.set_bind_group(0, &bg_gu, &[]);
                        pass.dispatch_workgroups(*mi as u32, slots as u32, 1);
                        pass.set_pipeline(p_dn);
                        pass.set_bind_group(0, &bg_dn, &[]);
                        pass.dispatch_workgroups(hidden as u32, 1, 1);
                        drop(pass);
                        cpo(&mut enc, &row_out, &ob, i * hidden, hidden);
                    }
                }
            }
        }
        bts!(enc, 3);
        if li + 1 < layers.len() {
            let inw_next = stor(bytemuck::cast_slice(layers[li + 1].input_norm));
            go(
                &mut enc,
                &c.add_rmsnorm_b,
                &bg(
                    &c.layout_add_rmsnorm_b,
                    &[&h_buf, &ob, &inw_next, &n1, &rms_u],
                ),
                k as u32,
            );
        } else {
            let ax_u = unif(&[1.0f32.to_bits(), (k * hidden) as u32, 0, 0]);
            go(
                &mut enc,
                &c.axpy,
                &bg(&c.layout_axpy, &[&ob, &h_buf, &ax_u]),
                ((k * hidden) as u32).div_ceil(256),
            );
        }
        bts!(enc, 4);
    }
    let size = (k * hidden * 4) as u64;
    let stage = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("bg-stage"),
        size,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    let t_enc_done = std::time::Instant::now();
    let ok = if let Some(sp) = spec.as_mut() {
        // Speculative tail: final-norm each row, one batched lm_head GEMM,
        // read every position's logits back beside the hiddens. The CPU
        // argmaxes them — a verify wants the last accepted row's WHOLE
        // logits anyway (the sampler's contract at the loop top).
        let Some(lm) = resolve(&sp.lm, sp.lm_rows, hidden) else {
            bgraph_refused("spec tail: lm head weight did not resolve");
            return false;
        };
        let fnw = stor(bytemuck::cast_slice(sp.final_norm));
        let n1b = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("spec-n1"),
            size,
            usage: wgpu::BufferUsages::STORAGE,
            mapped_at_creation: false,
        });
        go(
            &mut enc,
            &c.rmsnorm_b,
            &bg(&c.layout_rmsnorm_b, &[&h_buf, &fnw, &n1b, &rms_u]),
            k as u32,
        );
        let lsize = (k * sp.lm_rows * 4) as u64;
        let lbuf = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("spec-logits"),
            size: lsize,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });
        ematb(&mut enc, &lm, &n1b, &lbuf, sp.lm_rows, hidden);
        bts!(enc, 5);
        if bts_on && bts_lbl.len() > 1 {
            if let Some((qs, resolve, tstage)) = c.ts_query.as_ref() {
                enc.resolve_query_set(qs, 0..bts_lbl.len() as u32, resolve, 0);
                enc.copy_buffer_to_buffer(resolve, 0, tstage, 0, bts_lbl.len() as u64 * 8);
            }
        }
        sp.logits_out.resize(k * sp.lm_rows, 0.0);
        readback2(
            c,
            enc,
            (&h_buf, &mut h[..k * hidden]),
            (&lbuf, &mut sp.logits_out[..]),
        )
    } else {
        if bts_on && bts_lbl.len() > 1 {
            if let Some((qs, resolve, tstage)) = c.ts_query.as_ref() {
                enc.resolve_query_set(qs, 0..bts_lbl.len() as u32, resolve, 0);
                enc.copy_buffer_to_buffer(resolve, 0, tstage, 0, bts_lbl.len() as u64 * 8);
            }
        }
        readback(c, enc, &h_buf, &stage, size, &mut h[..k * hidden])
    };
    if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
        let post = t_enc_done.elapsed().as_secs_f64() * 1e3;
        eprintln!(
            "batch-graph: encode {:.1} ms | gpu+readback {:.1} ms",
            t_bfn.elapsed().as_secs_f64() * 1e3 - post,
            post,
        );
    }
    if ok && bts_on && bts_lbl.len() > 1 {
        if let Some((_, _, tstage)) = c.ts_query.as_ref() {
            let bytes = bts_lbl.len() as u64 * 8;
            let (tx, rx) = std::sync::mpsc::channel();
            tstage.map_async(wgpu::MapMode::Read, ..bytes, move |r| {
                let _ = tx.send(r);
            });
            let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
            if rx.recv().map(|r| r.is_ok()).unwrap_or(false) {
                if let Ok(raw) = tstage.get_mapped_range(..bytes) {
                    let t: Vec<u64> = bytemuck::cast_slice::<u8, u64>(&raw).to_vec();
                    drop(raw);
                    let mut agg = [(0f64, 0u32); 8];
                    for i in 1..bts_lbl.len() {
                        let d = t[i].saturating_sub(t[i - 1]) as f64 * c.ts_period as f64 / 1e6;
                        let e = &mut agg[bts_lbl[i] as usize];
                        e.0 += d;
                        e.1 += 1;
                    }
                    let names = ["start", "attn", "gdn-out", "ffn", "norm", "recur", "gdn-in", "gdn-proj"];
                    let line: Vec<String> = agg
                        .iter()
                        .enumerate()
                        .filter(|(_, e)| e.1 > 0)
                        .map(|(i, e)| format!("{}={:.2}ms/{}", names[i], e.0, e.1))
                        .collect();
                    eprintln!("batch-ts: {}", line.join(" "));
                }
            }
            tstage.unmap();
        }
    }
    if ok {
        let mut kvm = c.attn_kv.lock().unwrap();
        for li in 0..layers.len() {
            if let Some(m) = kvm.get_mut(&(kv_id, li)) {
                m.synced = pos0 + k;
            }
        }
    }
    ok
}

/// After a partial speculative acceptance: put every GDN layer's (ring, S)
/// back to the snapshot taken after batch position `slot` — the last
/// position whose input token was real.
pub fn gdn_spec_restore(kv_id: u64, slot: usize) -> bool {
    let Some(c) = ctx() else { return false };
    let snaps = c.gdn_snap.lock().unwrap();
    let states = c.gdn_state.lock().unwrap();
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("gdn-restore") });
    let mut any = false;
    for ((id, li), (snap, ring_sz, s_sz, slots)) in snaps.iter() {
        if *id != kv_id || slot >= *slots {
            continue;
        }
        let Some((ring, s)) = states.get(&(*id, *li)) else {
            continue;
        };
        let off = slot as u64 * (ring_sz + s_sz);
        if *ring_sz > 0 {
            enc.copy_buffer_to_buffer(snap, off, ring, 0, *ring_sz);
        }
        enc.copy_buffer_to_buffer(snap, off + ring_sz, s, 0, *s_sz);
        any = true;
    }
    c.queue.submit([enc.finish()]);
    any
}

/// Drop the device K/V mirror for a pipeline (called on cache clear).
pub fn kv_mirror_reset(kv_id: u64) {
    if let Some(c) = ctx() {
        c.attn_kv.lock().unwrap().retain(|(id, _), _| *id != kv_id);
        c.gdn_state
            .lock()
            .unwrap()
            .retain(|(id, _), _| *id != kv_id);
    }
}

/// GDN depthwise conv step (bring-up / parity): updates cq [cdim] and shifts
/// the ring [(kk-1)·cdim] in place.
pub fn gdn_conv_gpu(
    qkv: &[f32],
    taps: &[f32],
    ring: &mut [f32],
    cdim: usize,
    kk: usize,
    cq: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let qb = storage_bytes(c, bytemuck::cast_slice(qkv));
    let tb = storage_bytes(c, bytemuck::cast_slice(taps));
    let rb = c
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: None,
            contents: bytemuck::cast_slice(ring),
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        });
    let cb = rw_f32(c, cdim, true);
    let p = uniform_u32x4(c, [cdim as u32, kk as u32, 0, 0]);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.layout_gdn_conv,
        entries: &[
            bind_buf(0, &qb),
            bind_buf(1, &tb),
            bind_buf(2, &rb),
            bind_buf(3, &cb),
            bind_buf(4, &p),
        ],
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
    {
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.gdn_conv);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups((cdim as u32).div_ceil(256), 1, 1);
    }
    let rsz = (ring.len() * 4) as u64;
    let csz = (cdim * 4) as u64;
    let sr = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: rsz,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    let scq = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: csz,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    enc.copy_buffer_to_buffer(&rb, 0, &sr, 0, rsz);
    enc.copy_buffer_to_buffer(&cb, 0, &scq, 0, csz);
    submit(c, enc.finish());
    sr.slice(..).map_async(wgpu::MapMode::Read, |_| {});
    scq.slice(..).map_async(wgpu::MapMode::Read, |_| {});
    if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
        return false;
    }
    let (Ok(dr), Ok(dc)) = (
        sr.slice(..).get_mapped_range(),
        scq.slice(..).get_mapped_range(),
    ) else {
        return false;
    };
    ring.copy_from_slice(bytemuck::cast_slice(&dr[..ring.len() * 4]));
    cq[..cdim].copy_from_slice(bytemuck::cast_slice(&dc[..cdim * 4]));
    true
}

/// GDN decode step (bring-up / parity): one workgroup per v-head. `s` is the
/// [nv·dk·dv] recurrent state, updated in place; writes `o` [nv·dv].
#[allow(clippy::too_many_arguments)]
pub fn gdn_step_gpu(
    cq: &[f32],
    z: &[f32],
    a: &[f32],
    b: &[f32],
    alog: &[f32],
    dtb: &[f32],
    norm: &[f32],
    s: &mut [f32],
    nv: usize,
    dk: usize,
    dv: usize,
    kd: usize,
    rep: usize,
    cdim: usize,
    eps: f32,
    o: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let sb = c
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("gdn-s"),
            contents: bytemuck::cast_slice(s),
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        });
    let ob = rw_f32(c, nv * dv, true);
    let p = c
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("gdn-p"),
            contents: bytemuck::cast_slice(&[
                nv as u32,
                dk as u32,
                dv as u32,
                kd as u32,
                rep as u32,
                cdim as u32,
                eps.to_bits(),
                0u32,
            ]),
            usage: wgpu::BufferUsages::UNIFORM,
        });
    let sbuf = |d: &[f32]| storage_bytes(c, bytemuck::cast_slice(d));
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("gdn-bg"),
        layout: &c.layout_gdn,
        entries: &[
            bind_buf(0, &sbuf(cq)),
            bind_buf(1, &sbuf(z)),
            bind_buf(2, &sbuf(a)),
            bind_buf(3, &sbuf(b)),
            bind_buf(4, &sbuf(alog)),
            bind_buf(5, &sbuf(dtb)),
            bind_buf(6, &sbuf(norm)),
            bind_buf(7, &sb),
            bind_buf(8, &ob),
            bind_buf(9, &p),
        ],
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("gdn") });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("gdn"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.gdn_step);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(nv as u32, 1, 1);
    }
    // read back updated S and o
    let ssz = (s.len() * 4) as u64;
    let osz = (nv * dv * 4) as u64;
    let stage_s = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: ssz,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    let stage_o = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: osz,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    enc.copy_buffer_to_buffer(&sb, 0, &stage_s, 0, ssz);
    enc.copy_buffer_to_buffer(&ob, 0, &stage_o, 0, osz);
    submit(c, enc.finish());
    stage_s.slice(..).map_async(wgpu::MapMode::Read, |_| {});
    stage_o.slice(..).map_async(wgpu::MapMode::Read, |_| {});
    if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
        return false;
    }
    let (Ok(ds), Ok(dobuf)) = (
        stage_s.slice(..).get_mapped_range(),
        stage_o.slice(..).get_mapped_range(),
    ) else {
        return false;
    };
    s.copy_from_slice(bytemuck::cast_slice(&ds[..s.len() * 4]));
    o[..nv * dv].copy_from_slice(bytemuck::cast_slice(&dobuf[..nv * dv * 4]));
    true
}

/// One full attention sub-block resident on the GPU in a SINGLE command
/// encoder: rmsnorm → QKV (q1) → rope/qk-norm → kv_append → attend → O (q1)
/// → residual. The K/V cache lives on the device ([nkv,cap,hd]) and persists
/// across tokens; only the updated hidden is read back. This is the token
/// graph's attention half — it collapses ~6 per-op submits into one.
/// `flags` follows attn_rope_qkn (2=qnorm 4=knorm 8=gemma; gate unsupported
/// here). Weights are raw q1 payloads (bring-up path; production keys the
/// resident VRAM cache). Returns false without a GPU context.
#[allow(clippy::too_many_arguments)]
pub fn attn_block_gpu(
    h_in: &[f32],
    attn_norm_w: &[f32],
    wq: &[u8],
    wk: &[u8],
    wv: &[u8],
    wo: &[u8],
    qnw: &[f32],
    knw: &[f32],
    invf: &[f32],
    kbuf: &wgpu::Buffer,
    vbuf: &wgpu::Buffer,
    nh: usize,
    nkv: usize,
    hd: usize,
    rd: usize,
    hidden: usize,
    cap: usize,
    stored: usize,
    flags: u32,
    eps: f32,
    h_out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let unif = |data: &[u32]| {
        c.device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("blk-u"),
                contents: bytemuck::cast_slice(data),
                usage: wgpu::BufferUsages::UNIFORM,
            })
    };
    let stor = |data: &[u8]| {
        c.device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("blk-w"),
                contents: data,
                usage: wgpu::BufferUsages::STORAGE,
            })
    };
    // Resident buffers.
    let h_buf = c
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("blk-h"),
            contents: bytemuck::cast_slice(&h_in[..hidden]),
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        });
    let normw_b = stor(bytemuck::cast_slice(&attn_norm_w[..hidden]));
    let normed_b = rw_f32(c, hidden, false);
    let wq_b = stor(wq);
    let wk_b = stor(wk);
    let wv_b = stor(wv);
    let wo_b = stor(wo);
    let qraw_b = rw_f32(c, nh * hd, false);
    let k_b = rw_f32(c, nkv * hd, false);
    let v_b = rw_f32(c, nkv * hd, false);
    let qout_b = rw_f32(c, nh * hd, false);
    let gout_b = rw_f32(c, nh * hd, false);
    let qnw_b = stor(bytemuck::cast_slice(qnw));
    let knw_b = stor(bytemuck::cast_slice(knw));
    let invf_b = stor(bytemuck::cast_slice(invf));
    let attn_b = rw_f32(c, nh * hd, false);
    let o_b = rw_f32(c, hidden, false);
    let bg = |layout: &wgpu::BindGroupLayout, bufs: &[&wgpu::Buffer]| {
        let entries: Vec<wgpu::BindGroupEntry> = bufs
            .iter()
            .enumerate()
            .map(|(i, b)| bind_buf(i as u32, b))
            .collect();
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout,
            entries: &entries,
        })
    };
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("attn-block"),
        });
    let dispatch = |enc: &mut wgpu::CommandEncoder,
                    pipe: &wgpu::ComputePipeline,
                    bind: &wgpu::BindGroup,
                    groups: u32| {
        let mut pass = begin_pass(enc);
        pass.set_pipeline(pipe);
        pass.set_bind_group(0, bind, &[]);
        pass.dispatch_workgroups(groups, 1, 1);
    };
    // 1. rmsnorm(h) -> normed
    let rms_p = unif(&[hidden as u32, 0, eps.to_bits(), 0]);
    dispatch(
        &mut enc,
        &c.rmsnorm,
        &bg(&c.layout_rmsnorm, &[&h_buf, &normw_b, &normed_b, &rms_p]),
        1,
    );
    // 2. QKV (q1) from normed
    encode_matvec_q1(c, &mut enc, &wq_b, &normed_b, &qraw_b, nh * hd, hidden);
    encode_matvec_q1(c, &mut enc, &wk_b, &normed_b, &k_b, nkv * hd, hidden);
    encode_matvec_q1(c, &mut enc, &wv_b, &normed_b, &v_b, nkv * hd, hidden);
    // 3. rope + qk-norm
    let rq_p = unif(&[
        nh as u32,
        nkv as u32,
        hd as u32,
        rd as u32,
        stored as u32,
        flags,
        eps.to_bits(),
        0,
    ]);
    dispatch(
        &mut enc,
        &c.attn_rope,
        &bg(
            &c.layout_attn_rope,
            &[
                &qraw_b, &k_b, &qout_b, &gout_b, &qnw_b, &knw_b, &invf_b, &rq_p,
            ],
        ),
        (nh + nkv) as u32,
    );
    // 4. kv_append
    let kv_p = unif(&[nkv as u32, hd as u32, cap as u32, stored as u32]);
    let kv_groups = ((nkv * hd) as u32).div_ceil(256);
    dispatch(
        &mut enc,
        &c.kv_append,
        &bg(&c.layout_kv, &[&k_b, &v_b, kbuf, vbuf, &kv_p]),
        kv_groups,
    );
    // 5. attend
    let at_p = unif(&[
        nh as u32,
        (nh / nkv) as u32,
        hd as u32,
        cap as u32,
        (stored + 1) as u32,
        0,
        0,
        0,
    ]);
    {
        let (ap, al) = attend_pipes(c, hd);
        dispatch(
            &mut enc,
            ap,
            &bg(al, &[&qout_b, kbuf, vbuf, &attn_b, &at_p]),
            nh as u32,
        );
    }
    // 6. O (q1)
    encode_matvec_q1(c, &mut enc, &wo_b, &attn_b, &o_b, hidden, nh * hd);
    // 7. residual h += o
    let ax_p = unif(&[1.0f32.to_bits(), hidden as u32, 0, 0]);
    dispatch(
        &mut enc,
        &c.axpy,
        &bg(&c.layout_axpy, &[&o_b, &h_buf, &ax_p]),
        (hidden as u32).div_ceil(256),
    );
    // readback updated hidden
    let size = (hidden * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "blk-stage",
    );
    let ok = readback(c, enc, &h_buf, &stage, size, &mut h_out[..hidden]);
    drop(sc);
    ok
}

/// q1 kernel body (weight_key = None — no residency cache; test path).
fn dispatch_q1(
    c: &Ctx,
    weight_key: Option<(usize, usize)>,
    payload: &[u8],
    xs: &[f32],
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    let gpr = cols / 32;
    let q_buf = match weight_key {
        Some(k) => match weight_buffer(c, k, payload) {
            Some(b) => b,
            None => return false, // over VRAM budget — honest CPU path
        },
        None => c
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("q1-weights"),
                contents: payload,
                usage: wgpu::BufferUsages::STORAGE,
            }),
    };
    let mut sc = c.scratch.lock().unwrap();
    let xs_buf = Scratch::ensure(
        &c.device,
        &mut sc.xs,
        (cols * 4) as u64,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        "q1-xs",
    );
    c.queue
        .write_buffer(&xs_buf, 0, bytemuck::cast_slice(&xs[..cols]));
    let y_size = (rows * 4) as u64;
    let y_buf = Scratch::ensure(
        &c.device,
        &mut sc.y,
        y_size,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        "q1-y",
    );
    let params = [(gpr / 2) as u32, rows as u32, 0u32, 0u32];
    let p_buf = match &sc.params {
        Some(b) => b.clone(),
        None => {
            let b = c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("q1-params"),
                size: 16,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            sc.params = Some(b.clone());
            b
        }
    };
    c.queue
        .write_buffer(&p_buf, 0, bytemuck::cast_slice(&params));
    let stage_buf = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        y_size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "q1-stage",
    );
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("q1-bg"),
        layout: &c.layout_q1,
        entries: &[
            bind_buf(0, &q_buf),
            bind_buf(1, &xs_buf),
            bind_buf(2, &y_buf),
            bind_buf(3, &p_buf),
        ],
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("q1") });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("q1"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.q1);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups((rows as u32).div_ceil(8).min(MAX_WG), 1, 1);
    }
    let ok = readback(c, enc, &y_buf, &stage_buf, y_size, &mut out[..rows]);
    drop(sc);
    ok
}

/// GEMM of the prefill batch: `pre` are prescaled inputs row-major [b, cols],
/// out — row-major [b, rows]. Weights are resident in VRAM. false = CPU path.
#[allow(clippy::too_many_arguments)]
pub fn q8_matmat(
    model: &Arc<CmfModel>,
    idx: usize,
    row_scale: &[f32],
    pre: &[f32],
    b: usize,
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if cols % 4 != 0 || rows == 0 || b == 0 {
        return false;
    }
    let entry = &model.tensors[idx];
    if entry.shape.first().copied().unwrap_or(0) < rows {
        return false;
    }
    let Some(abs) = model.entry_abs_offset(entry) else {
        return false;
    };
    let bytes = model.primary_bytes();
    if abs + rows * cols > bytes.len()
        || row_scale.len() < rows
        || pre.len() < b * cols
        || out.len() < b * rows
    {
        return false;
    }
    let full_quant = &bytes[abs..abs + rows * cols];
    dispatch_matmat(
        c,
        Some((model.uid() as usize, idx)),
        full_quant,
        row_scale,
        pre,
        b,
        rows,
        cols,
        out,
    )
}

/// Batched q1 GEMM (prefill): resident 1-bit weight, batch of raw-f32 inputs,
/// one 2D dispatch of q1_mul_mm, one readback. cols must be a 64-multiple (the
/// q1 format packs whole tile-pairs). Weights resident + cached; x through the
/// pooled scratch.
pub fn q1_matmat(
    model: &Arc<CmfModel>,
    idx: usize,
    pre: &[f32],
    b: usize,
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if cols % 64 != 0 || rows == 0 || b == 0 || pre.len() < b * cols || out.len() < b * rows {
        return false;
    }
    let entry = &model.tensors[idx];
    if entry.shape.first().copied().unwrap_or(0) < rows {
        return false;
    }
    let Some(abs) = model.entry_abs_offset(entry) else {
        return false;
    };
    let bytes = model.primary_bytes();
    let plen = entry.nbytes as usize;
    if abs + plen > bytes.len() {
        return false;
    }
    let Some(w) = weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen]) else {
        return false; // over VRAM budget → CPU path
    };
    let mut sc = c.scratch.lock().unwrap();
    let xs_buf = Scratch::ensure(
        &c.device,
        &mut sc.xs,
        (b * cols * 4) as u64,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        "q1mm-xs",
    );
    c.queue
        .write_buffer(&xs_buf, 0, bytemuck::cast_slice(&pre[..b * cols]));
    let y_size = (b * rows * 4) as u64;
    let y_buf = Scratch::ensure(
        &c.device,
        &mut sc.y,
        y_size,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        "q1mm-y",
    );
    let p_buf = uniform_u32x4(c, [(cols / 4) as u32, rows as u32, b as u32, 0]);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("q1mm-bg"),
        layout: &c.layout_q1mm, // q1_mul_mm omits binding 2 (no row-scale)
        entries: &[
            bind_buf(0, &w),
            bind_buf(1, &xs_buf),
            bind_buf(3, &y_buf),
            bind_buf(4, &p_buf),
        ],
    });
    let stage_buf = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        y_size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "q1mm-stage",
    );
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("q1mm"),
        });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("q1mm"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.q1_mm);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(
            (rows as u32).div_ceil(64).min(MAX_WG),
            (b as u32).div_ceil(64),
            1,
        );
    }
    let ok = readback(c, enc, &y_buf, &stage_buf, y_size, &mut out[..b * rows]);
    drop(sc);
    ok
}

/// matmat kernel: resident weights + rs + batch of inputs, 2D dispatch, readback.
#[allow(clippy::too_many_arguments)]
fn dispatch_matmat(
    c: &Ctx,
    weight_key: Option<(usize, usize)>,
    full_quant: &[u8],
    row_scale: &[f32],
    pre: &[f32],
    b: usize,
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    if full_quant.len() < rows * cols
        || row_scale.len() < rows
        || pre.len() < b * cols
        || out.len() < b * rows
    {
        return false;
    }
    let q_buf = match weight_key {
        Some(k) => match weight_buffer(c, k, full_quant) {
            Some(b) => b,
            None => return false, // over VRAM budget — honest CPU path
        },
        None => c
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("mm-weights"),
                contents: full_quant,
                usage: wgpu::BufferUsages::STORAGE,
            }),
    };
    // rs cached per tensor (row0 sentinel = full-tensor scales).
    let rs_buf = match weight_key {
        Some((base, idx)) => c
            .rs_bufs
            .lock()
            .unwrap()
            .entry((base ^ idx.wrapping_mul(1_000_003), usize::MAX))
            .or_insert_with(|| {
                crate::gpu::probe_note_cold();
                c.device
                    .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                        label: Some("mm-rs"),
                        contents: bytemuck::cast_slice(&row_scale[..rows]),
                        usage: wgpu::BufferUsages::STORAGE,
                    })
            })
            .clone(),
        None => c
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("mm-rs"),
                contents: bytemuck::cast_slice(&row_scale[..rows]),
                usage: wgpu::BufferUsages::STORAGE,
            }),
    };
    // Pooled scratch for the whole op (encode → submit → poll).
    let mut sc = c.scratch.lock().unwrap();
    let xs_buf = Scratch::ensure(
        &c.device,
        &mut sc.xs,
        (b * cols * 4) as u64,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        "mm-xs",
    );
    c.queue
        .write_buffer(&xs_buf, 0, bytemuck::cast_slice(&pre[..b * cols]));
    let y_size = (b * rows * 4) as u64;
    let y_buf = Scratch::ensure(
        &c.device,
        &mut sc.y,
        y_size,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        "mm-y",
    );
    let params = [(cols / 4) as u32, rows as u32, b as u32, 0u32];
    let p_buf = match &sc.params {
        Some(bf) => bf.clone(),
        None => {
            let bf = c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("mm-params"),
                size: 16,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            sc.params = Some(bf.clone());
            bf
        }
    };
    c.queue
        .write_buffer(&p_buf, 0, bytemuck::cast_slice(&params));
    let stage_buf = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        y_size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "mm-stage",
    );
    let use_mm = b >= 32;
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("mm-bg"),
        // Auto bind-group layouts are pipeline-exclusive in wgpu — pick
        // the layout of the pipeline this dispatch actually uses.
        layout: if use_mm { &c.layout_mmm } else { &c.layout_mm },
        entries: &[
            bind_buf(0, &q_buf),
            bind_buf(1, &xs_buf),
            bind_buf(2, &rs_buf),
            bind_buf(3, &y_buf),
            bind_buf(4, &p_buf),
        ],
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("mm") });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("mm"),
            timestamp_writes: None,
        });
        if use_mm {
            pass.set_pipeline(&c.mul_mm);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(
                (rows as u32).div_ceil(64).min(MAX_WG),
                (b as u32).div_ceil(64),
                1,
            );
        } else {
            pass.set_pipeline(&c.matmat);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups((rows as u32).min(MAX_WG), b as u32, 1);
        }
    }
    let ok = readback(c, enc, &y_buf, &stage_buf, y_size, &mut out[..b * rows]);
    drop(sc);
    ok
}

/// q1t batched GEMM (prefill) on wgpu — register-blocked base GEMM then the
/// sparse overlay, two passes in one encoder. Raw f32 x, scales in the tiles.
/// Batched q4t GEMM (imagegen DiT prefill shapes) — the wgpu twin of
/// the Metal q4t_matmat: one q4t_mul_mm dispatch reading the 18-byte
/// tiles from the cached weight buffer. The CPU/GPU probe arbitrates
/// per process exactly as on Metal.
/// q4tp twin of `q4t_matmat` — the batched GEMM the wide-batch arm of
/// `QTensor::matmat` reaches for (DiT prefill, MoE experts, dense FFN
/// batches). Without it a q4tp model kept that arm on the CPU while q4t
/// went to the device.
pub fn q4tp_matmat(
    model: &Arc<CmfModel>,
    idx: usize,
    xs: &[f32],
    b: usize,
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let gpr = cols / 32;
    if cols % 32 != 0 || rows == 0 || b == 0 {
        return false;
    }
    let entry = &model.tensors[idx];
    if entry.shape.first().copied().unwrap_or(0) < rows {
        return false;
    }
    let Some(abs) = model.entry_abs_offset(entry) else {
        return false;
    };
    let bytes = model.primary_bytes();
    let plen = entry.nbytes as usize;
    let Some(need) =
        cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q4TiledP, &[rows, cols])
    else {
        return false;
    };
    if plen < need || abs + plen > bytes.len() || xs.len() < b * cols || out.len() < b * rows {
        return false;
    }
    let q_buf = match weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen]) {
        Some(bf) => bf,
        None => return false,
    };
    let mut sc = c.scratch.lock().unwrap();
    let xs_buf = Scratch::ensure(
        &c.device,
        &mut sc.xs,
        (b * cols * 4) as u64,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        "q4tpmm-xs",
    );
    c.queue
        .write_buffer(&xs_buf, 0, bytemuck::cast_slice(&xs[..b * cols]));
    let y_size = (b * rows * 4) as u64;
    let y_buf = Scratch::ensure(
        &c.device,
        &mut sc.y,
        y_size,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        "q4tpmm-y",
    );
    let params = [(cols / 4) as u32, rows as u32, b as u32, 0u32];
    let p_buf = match &sc.params {
        Some(bf) => bf.clone(),
        None => {
            let bf = c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("q4tpmm-params"),
                size: 16,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            sc.params = Some(bf.clone());
            bf
        }
    };
    c.queue
        .write_buffer(&p_buf, 0, bytemuck::cast_slice(&params));
    let stage_buf = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        y_size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "q4tpmm-stage",
    );
    let entries = [
        bind_buf(0, &q_buf),
        bind_buf(1, &xs_buf),
        bind_buf(2, &y_buf),
        bind_buf(3, &p_buf),
    ];
    let bind_mm = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("q4tpmm-bg"),
        layout: &c.q4tp_mm.get_bind_group_layout(0),
        entries: &entries,
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("q4tpmm"),
        });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("q4tpmm"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.q4tp_mm);
        pass.set_bind_group(0, &bind_mm, &[]);
        pass.dispatch_workgroups(
            (rows as u32).div_ceil(64).min(MAX_WG),
            (b as u32).div_ceil(64),
            1,
        );
    }
    readback(c, enc, &y_buf, &stage_buf, y_size, &mut out[..b * rows])
}

pub fn q4t_matmat(
    model: &Arc<CmfModel>,
    idx: usize,
    xs: &[f32],
    b: usize,
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let gpr = cols / 32;
    if cols % 32 != 0 || rows == 0 || b == 0 {
        return false;
    }
    let entry = &model.tensors[idx];
    if entry.shape.first().copied().unwrap_or(0) < rows {
        return false;
    }
    let Some(abs) = model.entry_abs_offset(entry) else {
        return false;
    };
    let bytes = model.primary_bytes();
    let plen = entry.nbytes as usize;
    if plen < rows * gpr * 18
        || abs + plen > bytes.len()
        || xs.len() < b * cols
        || out.len() < b * rows
    {
        return false;
    }
    let q_buf = match weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen]) {
        Some(bf) => bf,
        None => return false,
    };
    let mut sc = c.scratch.lock().unwrap();
    let xs_buf = Scratch::ensure(
        &c.device,
        &mut sc.xs,
        (b * cols * 4) as u64,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        "q4tmm-xs",
    );
    c.queue
        .write_buffer(&xs_buf, 0, bytemuck::cast_slice(&xs[..b * cols]));
    let y_size = (b * rows * 4) as u64;
    let y_buf = Scratch::ensure(
        &c.device,
        &mut sc.y,
        y_size,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        "q4tmm-y",
    );
    let params = [(cols / 4) as u32, rows as u32, b as u32, 0u32];
    let p_buf = match &sc.params {
        Some(bf) => bf.clone(),
        None => {
            let bf = c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("q4tmm-params"),
                size: 16,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            sc.params = Some(bf.clone());
            bf
        }
    };
    c.queue
        .write_buffer(&p_buf, 0, bytemuck::cast_slice(&params));
    let stage_buf = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        y_size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "q4tmm-stage",
    );
    let entries = [
        bind_buf(0, &q_buf),
        bind_buf(1, &xs_buf),
        bind_buf(2, &y_buf),
        bind_buf(3, &p_buf),
    ];
    let bind_mm = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("q4tmm-bg"),
        layout: &c.q4t_mm.get_bind_group_layout(0),
        entries: &entries,
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("q4tmm"),
        });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("q4tmm"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.q4t_mm);
        pass.set_bind_group(0, &bind_mm, &[]);
        pass.dispatch_workgroups(
            (rows as u32).div_ceil(64).min(MAX_WG),
            (b as u32).div_ceil(64),
            1,
        );
    }
    readback(c, enc, &y_buf, &stage_buf, y_size, &mut out[..b * rows])
}

/// One q4tp matvec through the WGSL kernel, weight fetched from the model.
/// Exists so the shader can be pinned to `dequant_q4tp` in a test: the token
/// graph is the only other caller, and a wrong kernel there still produces
/// fluent text.
#[doc(hidden)]
/// Single-token q4tp matvec — the lm_head class — through the DEDICATED
/// matvec kernel rather than the batched GEMM at b=1. The GEMM measured
/// 11.73 ms against the host's 9.51 on a 129280x4096 head, which is how a
/// route that should have been a rout ended up losing its own probe.
pub fn q4tp_matvec(
    model: &Arc<CmfModel>,
    idx: usize,
    xs: &[f32],
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    q4tp_matvec_for_test(model, idx, xs, rows, cols, out)
}

pub fn q4tp_matvec_for_test(
    model: &Arc<CmfModel>,
    idx: usize,
    xs: &[f32],
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    q4tp_matvec_batch_for_test(model, idx, xs, 1, rows, cols, out)
}

/// `batch` activation vectors end to end against one weight, `batch * rows`
/// outputs. Exposed so the dequant-pinned test can hold the batched form to
/// the same definition as the single one — the batch dimension shares the
/// kernel with the matvec, so a bug in it is a bug in decode too.
#[allow(clippy::too_many_arguments)]
pub fn q4tp_matvec_batch_for_test(
    model: &Arc<CmfModel>,
    idx: usize,
    xs: &[f32],
    batch: usize,
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if cols % 32 != 0
        || rows == 0
        || batch == 0
        || xs.len() < batch * cols
        || out.len() < batch * rows
    {
        return false;
    }
    let entry = &model.tensors[idx];
    if entry.dtype != cortiq_core::TensorDtype::Q4TiledP {
        return false;
    }
    let Some(abs) = model.entry_abs_offset(entry) else {
        return false;
    };
    let bytes = model.primary_bytes();
    let plen = entry.nbytes as usize;
    let Some(need) =
        cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q4TiledP, &[rows, cols])
    else {
        return false;
    };
    if plen < need || abs + plen > bytes.len() {
        return false;
    }
    let Some(q_buf) = weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen])
    else {
        return false;
    };
    let mut sc = c.scratch.lock().unwrap();
    let xs_buf = Scratch::ensure(
        &c.device,
        &mut sc.xs,
        (batch * cols * 4) as u64,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        "q4tp-xs",
    );
    c.queue
        .write_buffer(&xs_buf, 0, bytemuck::cast_slice(&xs[..batch * cols]));
    let y_size = (batch * rows * 4) as u64;
    let y_buf = Scratch::ensure(
        &c.device,
        &mut sc.y,
        y_size,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        "q4tp-y",
    );
    let stage_buf = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        y_size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "q4tp-stage",
    );
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("q4tp-mv"),
        });
    // The hook follows the runtime's kernel choice, so the dequant-pinned
    // test exercises whichever variant real decodes will use.
    if c.use_mv4 {
        if !encode_q4tp_mv4_b(c, &mut enc, &q_buf, &xs_buf, &y_buf, rows, cols, batch) {
            return false;
        }
    } else if batch == 1 {
        encode_q1t_like(c, &mut enc, &c.q4tp_mv, &q_buf, &xs_buf, &y_buf, rows, cols);
    } else {
        return false;
    }
    readback(c, enc, &y_buf, &stage_buf, y_size, &mut out[..batch * rows])
}

/// DiT attention on wgpu: per head, scores = scale·Q·Kᵀ → row softmax →
/// P·V, then one unstack of the [nh][n][hd] panel into [n][nh·hd]. All
/// of it in ONE submission with the scores and the panel resident on the
/// device — the CPU only ships Q/K/V in and the result out. Head-major
/// inputs, matching `gpu_metal::dit_attention`.
#[allow(clippy::too_many_arguments)]
pub fn dit_attention(
    qh: &[f32],
    kh: &[f32],
    vh: &[f32],
    nh: usize,
    nkv: usize,
    n: usize,
    hd: usize,
    scale: f32,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if nh == 0 || nkv == 0 || n == 0 || hd == 0 || nh % nkv != 0 {
        return false;
    }
    if qh.len() < nh * n * hd || kh.len() < nkv * n * hd || vh.len() < nkv * n * hd {
        return false;
    }
    if out.len() < n * nh * hd {
        return false;
    }
    let dev = &c.device;
    // Grow-only slots, not fresh allocations per call: a render calls
    // this 26 times per forward and the driver's allocator is not free.
    // `Scratch::ensure` also flags the cold call so the contention
    // tripwire does not read a one-off buffer creation as a busy device.
    let mut sc = c.scratch.lock().unwrap();
    let st = wgpu::BufferUsages::STORAGE;
    let up = |slot: &mut Option<(wgpu::Buffer, u64)>, data: &[f32], label: &str| -> wgpu::Buffer {
        let b = Scratch::ensure(
            dev,
            slot,
            (data.len() * 4) as u64,
            st | wgpu::BufferUsages::COPY_DST,
            label,
        );
        c.queue.write_buffer(&b, 0, bytemuck::cast_slice(data));
        b
    };
    let qb = up(&mut sc.dq, &qh[..nh * n * hd], "dit-q");
    let kb = up(&mut sc.dk, &kh[..nkv * n * hd], "dit-k");
    let vb = up(&mut sc.dv, &vh[..nkv * n * hd], "dit-v");
    let scb = Scratch::ensure(dev, &mut sc.dsc, (n * n * 4) as u64, st, "dit-scores");
    let pb = Scratch::ensure(dev, &mut sc.dpan, (nh * n * hd * 4) as u64, st, "dit-panel");
    let ab = Scratch::ensure(
        dev,
        &mut sc.dout,
        (n * nh * hd * 4) as u64,
        st | wgpu::BufferUsages::COPY_SRC,
        "dit-out",
    );
    let stage = Scratch::ensure(
        dev,
        &mut sc.dstage,
        (n * nh * hd * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dit-stage",
    );
    drop(sc);

    // One uniform per distinct (m, k, n, scale) shape; the head offset
    // rides in the bound slice, not the params.
    let params = |m: u32, k: u32, nn: u32, sc: f32| -> wgpu::Buffer {
        let raw = [m, k, nn, sc.to_bits(), 0u32, 0u32, 0u32, 0u32];
        let b = dev.create_buffer(&wgpu::BufferDescriptor {
            label: Some("dit-params"),
            size: 32,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        c.queue.write_buffer(&b, 0, bytemuck::cast_slice(&raw));
        b
    };
    let p_qk = params(n as u32, hd as u32, n as u32, scale);
    let p_sm = params(n as u32, hd as u32, n as u32, 1.0);
    let p_pv = params(n as u32, n as u32, hd as u32, 1.0);
    let p_un = params(nh as u32, n as u32, hd as u32, 1.0);

    let bind = |pipe: &wgpu::ComputePipeline,
                a: &wgpu::Buffer,
                ao: u64,
                al: u64,
                b: &wgpu::Buffer,
                bo: u64,
                bl: u64,
                cc: &wgpu::Buffer,
                co: u64,
                cl: u64,
                pp: &wgpu::Buffer|
     -> wgpu::BindGroup {
        dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("dit-bg"),
            layout: &pipe.get_bind_group_layout(0),
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                        buffer: a,
                        offset: ao,
                        size: std::num::NonZeroU64::new(al),
                    }),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                        buffer: b,
                        offset: bo,
                        size: std::num::NonZeroU64::new(bl),
                    }),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                        buffer: cc,
                        offset: co,
                        size: std::num::NonZeroU64::new(cl),
                    }),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: pp.as_entire_binding(),
                },
            ],
        })
    };

    let hpk = nh / nkv;
    let head = (n * hd * 4) as u64;
    let sc_len = (n * n * 4) as u64;
    let mut enc = dev.create_command_encoder(&wgpu::CommandEncoderDescriptor {
        label: Some("dit-attn"),
    });
    for h in 0..nh {
        let kv = (h / hpk) as u64;
        let bg_qk = bind(
            &c.dit_qk,
            &qb,
            h as u64 * head,
            head,
            &kb,
            kv * head,
            head,
            &scb,
            0,
            sc_len,
            &p_qk,
        );
        // Naga derives each pipeline's layout from the bindings it
        // actually uses: softmax touches only the scores and the params,
        // so its group has two entries, not four.
        let bg_sm = dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("dit-sm-bg"),
            layout: &c.dit_softmax.get_bind_group_layout(0),
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: scb.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: p_sm.as_entire_binding(),
                },
            ],
        });
        let bg_pv = bind(
            &c.dit_pv,
            &scb,
            0,
            sc_len,
            &vb,
            kv * head,
            head,
            &pb,
            h as u64 * head,
            head,
            &p_pv,
        );
        // Each stage reads what the previous one wrote to the SAME
        // scores buffer, so each gets its own pass: wgpu inserts the
        // memory barrier at pass boundaries, and three dispatches inside
        // one pass raced (max pixel error 38/255 against the CPU path).
        {
            let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("dit-qk"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&c.dit_qk);
            pass.set_bind_group(0, &bg_qk, &[]);
            pass.dispatch_workgroups((n as u32).div_ceil(64), (n as u32).div_ceil(64), 1);
        }
        {
            let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("dit-sm"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&c.dit_softmax);
            pass.set_bind_group(0, &bg_sm, &[]);
            pass.dispatch_workgroups(n as u32, 1, 1);
        }
        {
            let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("dit-pv"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&c.dit_pv);
            pass.set_bind_group(0, &bg_pv, &[]);
            pass.dispatch_workgroups((hd as u32).div_ceil(64), (n as u32).div_ceil(64), 1);
        }
    }
    {
        let total = (nh * n * hd) as u32;
        // unstack reads the panel (0) and writes the output (2).
        let bg_un = dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("dit-un-bg"),
            layout: &c.dit_unstack.get_bind_group_layout(0),
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: pb.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: ab.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: p_un.as_entire_binding(),
                },
            ],
        });
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("dit-unstack"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.dit_unstack);
        pass.set_bind_group(0, &bg_un, &[]);
        pass.dispatch_workgroups(total.div_ceil(256), 1, 1);
    }
    readback(
        c,
        enc,
        &ab,
        &stage,
        (n * nh * hd * 4) as u64,
        &mut out[..n * nh * hd],
    )
}

/// Causal chunk attention on wgpu: `b` new queries against `s0 + b`
/// cached keys, per head, with the causal bound applied in the softmax.
/// Same three kernels as the DiT path, rectangular this time.
///
/// This is the prefill attention the CPU path only has on aarch64 — its
/// batched attend needs Accelerate or the NEON micro-GEMM, so x86 fell
/// back to a per-position scalar loop. Measured on a 256-core EPYC that
/// loop was 30% of a 512-token prefill and 46% of a 1024-token one.
///
/// `q` is head-major [nh][b][hd] (post-RoPE); `k`/`v` are per-kv-head
/// contiguous [s0+b][hd] — the cache's own layout. `out` is
/// [b][nh·hd].
#[allow(clippy::too_many_arguments)]
pub fn chunk_attend(
    q: &[f32],
    k: &[&[f32]],
    v: &[&[f32]],
    b: usize,
    s0: usize,
    nh: usize,
    nkv: usize,
    hd: usize,
    scale: f32,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let n = s0 + b;
    if nh == 0 || nkv == 0 || b == 0 || hd == 0 || nh % nkv != 0 || n == 0 {
        return false;
    }
    if q.len() < nh * b * hd || k.len() != nkv || v.len() != nkv {
        return false;
    }
    for h in 0..nkv {
        if k[h].len() < n * hd || v[h].len() < n * hd {
            return false;
        }
    }
    if out.len() < b * nh * hd {
        return false;
    }
    let dev = &c.device;
    let mut sc = c.scratch.lock().unwrap();
    let st = wgpu::BufferUsages::STORAGE;
    let qb = Scratch::ensure(
        dev,
        &mut sc.dq,
        (nh * b * hd * 4) as u64,
        st | wgpu::BufferUsages::COPY_DST,
        "ca-q",
    );
    c.queue
        .write_buffer(&qb, 0, bytemuck::cast_slice(&q[..nh * b * hd]));
    // K/V are per-head slices of the CPU cache: pack them back to back
    // so one buffer serves every head at a known stride.
    let kvsz = (nkv * n * hd * 4) as u64;
    let kb = Scratch::ensure(
        dev,
        &mut sc.dk,
        kvsz,
        st | wgpu::BufferUsages::COPY_DST,
        "ca-k",
    );
    let vb = Scratch::ensure(
        dev,
        &mut sc.dv,
        kvsz,
        st | wgpu::BufferUsages::COPY_DST,
        "ca-v",
    );
    for h in 0..nkv {
        let off = (h * n * hd * 4) as u64;
        c.queue
            .write_buffer(&kb, off, bytemuck::cast_slice(&k[h][..n * hd]));
        c.queue
            .write_buffer(&vb, off, bytemuck::cast_slice(&v[h][..n * hd]));
    }
    let scb = Scratch::ensure(dev, &mut sc.dsc, (b * n * 4) as u64, st, "ca-scores");
    let pb = Scratch::ensure(dev, &mut sc.dpan, (nh * b * hd * 4) as u64, st, "ca-panel");
    let ab = Scratch::ensure(
        dev,
        &mut sc.dout,
        (b * nh * hd * 4) as u64,
        st | wgpu::BufferUsages::COPY_SRC,
        "ca-out",
    );
    let stage = Scratch::ensure(
        dev,
        &mut sc.dstage,
        (b * nh * hd * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "ca-stage",
    );
    drop(sc);

    let params = |m: u32, kk: u32, nn: u32, s: f32, s0v: u32, caus: u32| -> wgpu::Buffer {
        let raw = [m, kk, nn, s.to_bits(), s0v, caus, 0u32, 0u32];
        let bf = dev.create_buffer(&wgpu::BufferDescriptor {
            label: Some("ca-params"),
            size: 32,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        c.queue.write_buffer(&bf, 0, bytemuck::cast_slice(&raw));
        bf
    };
    let p_qk = params(b as u32, hd as u32, n as u32, scale, s0 as u32, 0);
    let p_sm = params(b as u32, hd as u32, n as u32, 1.0, s0 as u32, 1);
    let p_pv = params(b as u32, n as u32, hd as u32, 1.0, s0 as u32, 0);
    let p_un = params(nh as u32, b as u32, hd as u32, 1.0, 0, 0);

    fn slot(bf: &wgpu::Buffer, off: u64, len: u64, bind: u32) -> wgpu::BindGroupEntry<'_> {
        wgpu::BindGroupEntry {
            binding: bind,
            resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                buffer: bf,
                offset: off,
                size: std::num::NonZeroU64::new(len),
            }),
        }
    }
    let qhead = (b * hd * 4) as u64;
    let khead = (n * hd * 4) as u64;
    let sc_len = (b * n * 4) as u64;
    let hpk = nh / nkv;
    let mut enc = dev.create_command_encoder(&wgpu::CommandEncoderDescriptor {
        label: Some("chunk-attend"),
    });
    for h in 0..nh {
        let kv = (h / hpk) as u64;
        let bg_qk = dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("ca-qk"),
            layout: &c.dit_qk.get_bind_group_layout(0),
            entries: &[
                slot(&qb, h as u64 * qhead, qhead, 0),
                slot(&kb, kv * khead, khead, 1),
                slot(&scb, 0, sc_len, 2),
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: p_qk.as_entire_binding(),
                },
            ],
        });
        let bg_sm = dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("ca-sm"),
            layout: &c.dit_softmax.get_bind_group_layout(0),
            entries: &[
                slot(&scb, 0, sc_len, 2),
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: p_sm.as_entire_binding(),
                },
            ],
        });
        let bg_pv = dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("ca-pv"),
            layout: &c.dit_pv.get_bind_group_layout(0),
            entries: &[
                slot(&scb, 0, sc_len, 0),
                slot(&vb, kv * khead, khead, 1),
                slot(&pb, h as u64 * qhead, qhead, 2),
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: p_pv.as_entire_binding(),
                },
            ],
        });
        // One pass per stage: each reads what the previous wrote to the
        // same scores buffer, and dispatches inside one pass do not
        // order against each other.
        {
            let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("ca-qk"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&c.dit_qk);
            pass.set_bind_group(0, &bg_qk, &[]);
            pass.dispatch_workgroups((n as u32).div_ceil(64), (b as u32).div_ceil(64), 1);
        }
        {
            let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("ca-sm"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&c.dit_softmax);
            pass.set_bind_group(0, &bg_sm, &[]);
            pass.dispatch_workgroups(b as u32, 1, 1);
        }
        {
            let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("ca-pv"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&c.dit_pv);
            pass.set_bind_group(0, &bg_pv, &[]);
            pass.dispatch_workgroups((hd as u32).div_ceil(64), (b as u32).div_ceil(64), 1);
        }
    }
    {
        let bg_un = dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("ca-un"),
            layout: &c.dit_unstack.get_bind_group_layout(0),
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: pb.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: ab.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: p_un.as_entire_binding(),
                },
            ],
        });
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("ca-un"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.dit_unstack);
        pass.set_bind_group(0, &bg_un, &[]);
        pass.dispatch_workgroups(((nh * b * hd) as u32).div_ceil(256), 1, 1);
    }
    readback(
        c,
        enc,
        &ab,
        &stage,
        (b * nh * hd * 4) as u64,
        &mut out[..b * nh * hd],
    )
}

/// Fused QKV on wgpu: one upload of the normed chunk, three GEMMs, one
/// readback of Q|K|V laid out back to back. The unfused route pays three
/// submits and three uploads of the same X — at a 512-token chunk that
/// is the same 6 MB shipped three times, 44 times per prefill.
/// Weights stay cached in VRAM. `out` receives q (b·rq), then k (b·rk),
/// then v (b·rv).
#[allow(clippy::too_many_arguments)]
pub fn q4t_qkv(
    model: &Arc<CmfModel>,
    wq: usize,
    wk: usize,
    wv: usize,
    xs: &[f32],
    b: usize,
    cols: usize,
    rq: usize,
    rk: usize,
    rv: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if cols % 32 != 0 || b == 0 {
        return false;
    }
    let need = b * (rq + rk + rv);
    if xs.len() < b * cols || out.len() < need {
        return false;
    }
    let bytes = model.primary_bytes();
    let gpr = cols / 32;
    let wbuf = |idx: usize, rows: usize| -> Option<wgpu::Buffer> {
        let entry = &model.tensors[idx];
        if entry.shape.first().copied().unwrap_or(0) < rows {
            return None;
        }
        let abs = model.entry_abs_offset(entry)?;
        let plen = entry.nbytes as usize;
        if plen < rows * gpr * 18 || abs + plen > bytes.len() {
            return None;
        }
        weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen])
    };
    let (Some(bq), Some(bk), Some(bv)) = (wbuf(wq, rq), wbuf(wk, rk), wbuf(wv, rv)) else {
        return false;
    };

    let dev = &c.device;
    let mut sc = c.scratch.lock().unwrap();
    let st = wgpu::BufferUsages::STORAGE;
    let xs_buf = Scratch::ensure(
        dev,
        &mut sc.xs,
        (b * cols * 4) as u64,
        st | wgpu::BufferUsages::COPY_DST,
        "qkv-xs",
    );
    c.queue
        .write_buffer(&xs_buf, 0, bytemuck::cast_slice(&xs[..b * cols]));
    let y_size = (need * 4) as u64;
    let y_buf = Scratch::ensure(
        dev,
        &mut sc.y,
        y_size,
        st | wgpu::BufferUsages::COPY_SRC,
        "qkv-y",
    );
    let stage = Scratch::ensure(
        dev,
        &mut sc.stage,
        y_size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "qkv-stage",
    );
    drop(sc);

    let params = |rows: usize| -> wgpu::Buffer {
        let raw = [(cols / 4) as u32, rows as u32, b as u32, 0u32];
        let bf = dev.create_buffer(&wgpu::BufferDescriptor {
            label: Some("qkv-params"),
            size: 16,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        c.queue.write_buffer(&bf, 0, bytemuck::cast_slice(&raw));
        bf
    };
    let layout = c.q4t_mm.get_bind_group_layout(0);
    let mut enc =
        dev.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("qkv") });
    let mut off = 0u64;
    for (wbf, rows) in [(&bq, rq), (&bk, rk), (&bv, rv)] {
        let pbf = params(rows);
        let bg = dev.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("qkv-bg"),
            layout: &layout,
            entries: &[
                bind_buf(0, wbf),
                bind_buf(1, &xs_buf),
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                        buffer: &y_buf,
                        offset: off,
                        size: std::num::NonZeroU64::new((b * rows * 4) as u64),
                    }),
                },
                bind_buf(3, &pbf),
            ],
        });
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("qkv-mm"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.q4t_mm);
        pass.set_bind_group(0, &bg, &[]);
        pass.dispatch_workgroups(
            (rows as u32).div_ceil(64).min(MAX_WG),
            (b as u32).div_ceil(64),
            1,
        );
        drop(pass);
        off += (b * rows * 4) as u64;
    }
    readback(c, enc, &y_buf, &stage, y_size, &mut out[..need])
}

/// Fused DiT SwiGLU FFN on wgpu: g=X·W1ᵀ, u=X·W3ᵀ, silu(g)·u,
/// y=·W2ᵀ — four passes, ONE submission, one readback. The unfused
/// per-op route pays 3 submits and ships the [b, inter] intermediates
/// across PCIe twice; on discrete cards that overhead dominates the
/// GEMM itself. Weights stay cached in VRAM.
#[allow(clippy::too_many_arguments)]
pub fn q4t_ffn(
    model: &Arc<CmfModel>,
    w1: usize,
    w3: usize,
    w2: usize,
    xs: &[f32],
    b: usize,
    hidden: usize,
    inter: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if hidden % 32 != 0 || inter % 32 != 0 || b == 0 {
        return false;
    }
    let bytes = model.primary_bytes();
    let wbuf = |idx: usize, rows: usize, cols: usize| -> Option<wgpu::Buffer> {
        let entry = &model.tensors[idx];
        let abs = model.entry_abs_offset(entry)?;
        let plen = entry.nbytes as usize;
        if plen < rows * (cols / 32) * 18 || abs + plen > bytes.len() {
            return None;
        }
        weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen])
    };
    let (Some(q1), Some(q3), Some(q2)) = (
        wbuf(w1, inter, hidden),
        wbuf(w3, inter, hidden),
        wbuf(w2, hidden, inter),
    ) else {
        return false;
    };
    if xs.len() < b * hidden || out.len() < b * hidden {
        return false;
    }
    let mut sc = c.scratch.lock().unwrap();
    let xs_buf = Scratch::ensure(
        &c.device,
        &mut sc.xs,
        (b * hidden * 4) as u64,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        "q4tffn-xs",
    );
    c.queue
        .write_buffer(&xs_buf, 0, bytemuck::cast_slice(&xs[..b * hidden]));
    let panel = (b * inter * 4) as u64;
    let g_buf = Scratch::ensure(
        &c.device,
        &mut sc.g,
        panel,
        wgpu::BufferUsages::STORAGE,
        "q4tffn-g",
    );
    let u_buf = Scratch::ensure(
        &c.device,
        &mut sc.u,
        panel,
        wgpu::BufferUsages::STORAGE,
        "q4tffn-u",
    );
    let y_size = (b * hidden * 4) as u64;
    let y_buf = Scratch::ensure(
        &c.device,
        &mut sc.y,
        y_size,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        "q4tffn-y",
    );
    let stage_buf = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        y_size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "q4tffn-stage",
    );
    // Content-keyed uniforms: three shapes live in one submission, so
    // one rewritable buffer cannot serve them.
    let p13 = uniform_u32x4(c, [(hidden / 4) as u32, inter as u32, b as u32, 0]);
    let p2 = uniform_u32x4(c, [(inter / 4) as u32, hidden as u32, b as u32, 0]);
    let psilu = uniform_u32x4(c, [(b * inter) as u32, 0, 0, 0]);
    let mm_layout = c.q4t_mm.get_bind_group_layout(0);
    let bind_mm = |q: &wgpu::Buffer, x: &wgpu::Buffer, y: &wgpu::Buffer, p: &wgpu::Buffer| {
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("q4tffn-bg"),
            layout: &mm_layout,
            entries: &[
                bind_buf(0, q),
                bind_buf(1, x),
                bind_buf(2, y),
                bind_buf(3, p),
            ],
        })
    };
    let bg1 = bind_mm(&q1, &xs_buf, &g_buf, &p13);
    let bg3 = bind_mm(&q3, &xs_buf, &u_buf, &p13);
    let bg2 = bind_mm(&q2, &g_buf, &y_buf, &p2);
    let bg_silu = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("q4tffn-silu-bg"),
        layout: &c.ffn_silu.get_bind_group_layout(0),
        entries: &[
            bind_buf(0, &g_buf),
            bind_buf(1, &u_buf),
            bind_buf(2, &psilu),
        ],
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("q4tffn"),
        });
    let mm_pass = |enc: &mut wgpu::CommandEncoder, bg: &wgpu::BindGroup, rows: usize| {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("q4tffn-mm"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.q4t_mm);
        pass.set_bind_group(0, bg, &[]);
        pass.dispatch_workgroups(
            (rows as u32).div_ceil(64).min(MAX_WG),
            (b as u32).div_ceil(64),
            1,
        );
    };
    mm_pass(&mut enc, &bg1, inter);
    mm_pass(&mut enc, &bg3, inter);
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("q4tffn-silu"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.ffn_silu);
        pass.set_bind_group(0, &bg_silu, &[]);
        pass.dispatch_workgroups(((b * inter) as u32).div_ceil(256).min(MAX_WG), 1, 1);
    }
    mm_pass(&mut enc, &bg2, hidden);
    readback(c, enc, &y_buf, &stage_buf, y_size, &mut out[..b * hidden])
}

pub fn q1t_matmat(
    model: &Arc<CmfModel>,
    idx: usize,
    xs: &[f32],
    b: usize,
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let gpr = cols / 32;
    if cols % 32 != 0 || rows == 0 || b == 0 {
        return false;
    }
    let entry = &model.tensors[idx];
    if entry.shape.first().copied().unwrap_or(0) < rows {
        return false;
    }
    let Some(abs) = model.entry_abs_offset(entry) else {
        return false;
    };
    let bytes = model.primary_bytes();
    let plen = entry.nbytes as usize;
    if plen < rows * gpr * 9
        || abs + plen > bytes.len()
        || xs.len() < b * cols
        || out.len() < b * rows
    {
        return false;
    }
    dispatch_q1t_mm(
        c,
        Some((model.uid() as usize, idx)),
        &bytes[abs..abs + plen],
        xs,
        b,
        rows,
        cols,
        out,
    )
}

#[allow(clippy::too_many_arguments)]
fn dispatch_q1t_mm(
    c: &Ctx,
    weight_key: Option<(usize, usize)>,
    payload: &[u8],
    xs: &[f32],
    b: usize,
    rows: usize,
    cols: usize,
    out: &mut [f32],
) -> bool {
    let q_buf = match weight_key {
        Some(k) => match weight_buffer(c, k, payload) {
            Some(bf) => bf,
            None => return false,
        },
        None => c
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("q1tmm-weights"),
                contents: payload,
                usage: wgpu::BufferUsages::STORAGE,
            }),
    };
    let mut sc = c.scratch.lock().unwrap();
    let xs_buf = Scratch::ensure(
        &c.device,
        &mut sc.xs,
        (b * cols * 4) as u64,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        "q1tmm-xs",
    );
    c.queue
        .write_buffer(&xs_buf, 0, bytemuck::cast_slice(&xs[..b * cols]));
    let y_size = (b * rows * 4) as u64;
    let y_buf = Scratch::ensure(
        &c.device,
        &mut sc.y,
        y_size,
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        "q1tmm-y",
    );
    let params = [(cols / 4) as u32, rows as u32, b as u32, 0u32];
    let p_buf = match &sc.params {
        Some(bf) => bf.clone(),
        None => {
            let bf = c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("q1tmm-params"),
                size: 16,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            sc.params = Some(bf.clone());
            bf
        }
    };
    c.queue
        .write_buffer(&p_buf, 0, bytemuck::cast_slice(&params));
    let stage_buf = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        y_size,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "q1tmm-stage",
    );
    let entries = [
        bind_buf(0, &q_buf),
        bind_buf(1, &xs_buf),
        bind_buf(2, &y_buf),
        bind_buf(3, &p_buf),
    ];
    let bind_mm = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("q1tmm-bg"),
        layout: &c.q1t_mm.get_bind_group_layout(0),
        entries: &entries,
    });
    let bind_ov = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("q1tov-bg"),
        layout: &c.q1t_ovmm.get_bind_group_layout(0),
        entries: &entries,
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("q1tmm"),
        });
    {
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("q1tmm"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.q1t_mm);
        pass.set_bind_group(0, &bind_mm, &[]);
        pass.dispatch_workgroups(
            (rows as u32).div_ceil(64).min(MAX_WG),
            (b as u32).div_ceil(64),
            1,
        );
    }
    {
        // Separate pass = a barrier, so the overlay reads the finished base.
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("q1tov"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&c.q1t_ovmm);
        pass.set_bind_group(0, &bind_ov, &[]);
        pass.dispatch_workgroups((rows as u32).div_ceil(64).min(MAX_WG), 1, 1);
    }
    let ok = readback(c, enc, &y_buf, &stage_buf, y_size, &mut out[..b * rows]);
    drop(sc);
    ok
}

/// Copy the output buffer GPU→staging→CPU (map+poll). Single readback path
/// for matvec/matmat.
/// Spin briefly for a submission to land instead of sleeping on it.
fn spin_wait() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_GPU_SPIN").map(|v| v != "0").unwrap_or(true))
}

/// Submissions to the device, all sites. A round trip costs a fence
/// whatever it carries, so "how many a token" is the number that decides
/// whether a decode step is compute-bound or latency-bound — and it is not
/// derivable from anything else the profile prints.
pub static SUBMITS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// Weight residency: how much went to the card and how long it took. The
/// first token pays all of it, and on a 92 GB expert stack that was half an
/// hour — worth knowing as a rate rather than as "the bench is slow to start".
pub static UPLOAD_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
pub static UPLOAD_BYTES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// Staged upload (default): map a staging buffer and issue the copy here,
/// instead of handing the bytes to `queue.write_buffer`. `CMF_GPU_UPLOAD=map`
/// restores the historical write_buffer path.
fn upload_staged() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_GPU_UPLOAD").map(|v| v == "staged").unwrap_or(true))
}

#[inline]
fn submit(c: &Ctx, buf: wgpu::CommandBuffer) {
    SUBMITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    if slot_check() {
        c.slot_writes.lock().unwrap().clear();
    }
    c.queue.submit(Some(buf));
}

/// `CMF_DSV4_SLOT_CHECK=1`: panic if a per-layer slot is written twice
/// before its submission. Off by default — it costs a lock per slot write —
/// but the toy gate runs with it on, because the invariant it guards is a
/// convention and conventions are what the 50.280 was made of.
fn slot_check() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_SLOT_CHECK").is_ok_and(|v| v != "0"))
}

fn note_slot_write(c: &Ctx, kind: &str, key: (u8, u64, usize)) {
    if !slot_check() {
        return;
    }
    let mut m = c.slot_writes.lock().unwrap();
    let n = m.entry(key).or_insert(0);
    *n += 1;
    assert!(
        *n == 1,
        "{kind}-слот {key:?} записан {n} раз до одной отправки — \
         последняя запись достанется ВСЕМ проходам, которые его читают \
         (столкновение тегов или два слоя на один ключ)",
    );
}

/// Two device buffers, one staging buffer, one fence. The chain used to
/// read its folded vector and then call `dsv4_state_read` for the
/// hyper-connection state — two submissions and two map-waits for a token
/// that is otherwise a single submission.
/// Compute passes opened, all sites. On a decode step the dsv4 chain opens
/// about thirty a layer, and a pass costs the driver a fixed amount whatever
/// it dispatches — which is why a 32-layer TOY with 128-wide tensors still
/// waits 35 ms a token. Reported per token next to the submissions.
pub static PASSES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

#[inline]
fn begin_pass(enc: &mut wgpu::CommandEncoder) -> wgpu::ComputePass<'_> {
    PASSES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    // A stage label set by the BT frame turns this pass into a timestamped
    // one; everything else stays on the cold path of one atomic load.
    let which = BT_TS_STAGE.load(std::sync::atomic::Ordering::Relaxed);
    let timestamp_writes = if which != 0 {
        ctx().and_then(|c| ts_pair(c, which))
    } else {
        None
    };
    enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
        label: None,
        timestamp_writes,
    })
}

fn readback2(
    c: &Ctx,
    mut enc: wgpu::CommandEncoder,
    a: (&wgpu::Buffer, &mut [f32]),
    b: (&wgpu::Buffer, &mut [f32]),
) -> bool {
    let (a_buf, a_out) = a;
    let (b_buf, b_out) = b;
    let a_bytes = (a_out.len() * 4) as u64;
    let b_bytes = (b_out.len() * 4) as u64;
    // COPY_BUFFER_ALIGNMENT is 4; the second slice starts on a 16-byte
    // boundary so the map range stays comfortably aligned on every backend.
    let off = a_bytes.div_ceil(16) * 16;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage2,
        off + b_bytes,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dsv4-pair-stage",
    );
    enc.copy_buffer_to_buffer(a_buf, 0, &stage, 0, a_bytes);
    enc.copy_buffer_to_buffer(b_buf, 0, &stage, off, b_bytes);
    submit(c, enc.finish());
    let slice = stage.slice(..off + b_bytes);
    let done = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
    let d2 = done.clone();
    slice.map_async(wgpu::MapMode::Read, move |_| {
        d2.store(true, std::sync::atomic::Ordering::Release);
    });
    if spin_wait() {
        let t0 = std::time::Instant::now();
        loop {
            let _ = c.device.poll(wgpu::PollType::Poll);
            if done.load(std::sync::atomic::Ordering::Acquire) {
                break;
            }
            if t0.elapsed() > std::time::Duration::from_millis(2) {
                if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
                    return false;
                }
                break;
            }
            std::hint::spin_loop();
        }
    } else if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
        return false;
    }
    {
        let Ok(data) = slice.get_mapped_range() else {
            return false;
        };
        a_out.copy_from_slice(bytemuck::cast_slice(&data[..a_bytes as usize]));
        b_out.copy_from_slice(bytemuck::cast_slice(
            &data[off as usize..(off + b_bytes) as usize],
        ));
    }
    stage.unmap();
    true
}

fn readback(
    c: &Ctx,
    mut enc: wgpu::CommandEncoder,
    y_buf: &wgpu::Buffer,
    staging: &wgpu::Buffer,
    y_size: u64,
    out: &mut [f32],
) -> bool {
    enc.copy_buffer_to_buffer(y_buf, 0, staging, 0, y_size);
    submit(c, enc.finish());
    let slice = staging.slice(..y_size);
    let done = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
    let d2 = done.clone();
    slice.map_async(wgpu::MapMode::Read, move |_| {
        d2.store(true, std::sync::atomic::Ordering::Release);
    });
    // A blocking wait hands the thread to the OS scheduler, and getting it
    // back costs more than the work did: these submissions finish in tens of
    // microseconds and there are 86 of them a token. Spin on the queue for a
    // short while first, then block — a decode that stalls for a real reason
    // must not burn a core forever. CMF_GPU_SPIN=0 reverts.
    if spin_wait() {
        let t0 = std::time::Instant::now();
        loop {
            let _ = c.device.poll(wgpu::PollType::Poll);
            if done.load(std::sync::atomic::Ordering::Acquire) {
                break;
            }
            if t0.elapsed() > std::time::Duration::from_millis(2) {
                if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
                    return false;
                }
                break;
            }
            std::hint::spin_loop();
        }
    } else if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
        return false;
    }
    {
        let Ok(data) = slice.get_mapped_range() else {
            return false;
        };
        out.copy_from_slice(bytemuck::cast_slice(&data[..out.len() * 4]));
    }
    staging.unmap();
    true
}

fn bind_buf(binding: u32, buf: &wgpu::Buffer) -> wgpu::BindGroupEntry<'_> {
    wgpu::BindGroupEntry {
        binding,
        resource: buf.as_entire_binding(),
    }
}

/// A window into a strided per-token buffer: how the batched frame hands a
/// single-token kernel one token's slice without changing the kernel. The
/// offset must respect `min_storage_buffer_offset_alignment` (256 on every
/// card this runs on), which every per-token stride here does.
fn bind_buf_off(binding: u32, buf: &wgpu::Buffer, off: u64, size: u64) -> wgpu::BindGroupEntry<'_> {
    wgpu::BindGroupEntry {
        binding,
        resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
            buffer: buf,
            offset: off,
            size: std::num::NonZeroU64::new(size),
        }),
    }
}

fn storage_bytes(c: &Ctx, data: &[u8]) -> wgpu::Buffer {
    c.device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: None,
            contents: data,
            usage: wgpu::BufferUsages::STORAGE,
        })
}

fn uniform_u32x4(c: &Ctx, v: [u32; 4]) -> wgpu::Buffer {
    // Content-keyed cache: these params (rows/cols/flags) repeat every token,
    // so build each once and clone the handle thereafter.
    let mut u = c.uniforms.lock().unwrap();
    if let Some(b) = u.get(&v) {
        return b.clone();
    }
    let b = c
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: None,
            contents: bytemuck::cast_slice(&v),
            usage: wgpu::BufferUsages::UNIFORM,
        });
    u.insert(v, b.clone());
    b
}

/// Three words and a float, in one uniform. The cache is keyed on the bits,
/// which is exactly right: two params differ iff their bytes differ.
fn uniform_mixed(c: &Ctx, v: [u32; 3], f: f32) -> wgpu::Buffer {
    uniform_u32x4(c, [v[0], v[1], v[2], f.to_bits()])
}

fn rw_f32(c: &Ctx, n: usize, copy_src: bool) -> wgpu::Buffer {
    let usage = if copy_src {
        wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC
    } else {
        wgpu::BufferUsages::STORAGE
    };
    c.device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: (n * 4) as u64,
        usage,
        mapped_at_creation: false,
    })
}

/// Resident quant weights of tensor `idx` (the whole tensor, cached by (file,idx)).
fn tensor_weight(
    c: &Ctx,
    model: &Arc<CmfModel>,
    idx: usize,
    rows: usize,
    cols: usize,
) -> Option<wgpu::Buffer> {
    let entry = &model.tensors[idx];
    if entry.shape.first().copied().unwrap_or(0) < rows {
        return None;
    }
    let abs = model.entry_abs_offset(entry)?;
    let bytes = model.primary_bytes();
    if abs + rows * cols > bytes.len() {
        return None;
    }
    weight_buffer(
        c,
        (model.uid() as usize, idx),
        &bytes[abs..abs + rows * cols],
    )
}

/// `tensor_weight` for tile-packed dtypes whose payload length differs
/// from rows·cols (q4_tiled: 18 B per 32-weight group).
fn tensor_weight_sized(
    c: &Ctx,
    model: &Arc<CmfModel>,
    idx: usize,
    rows: usize,
    payload: usize,
) -> Option<wgpu::Buffer> {
    let entry = &model.tensors[idx];
    if entry.shape.first().copied().unwrap_or(0) < rows {
        return None;
    }
    let abs = model.entry_abs_offset(entry)?;
    let bytes = model.primary_bytes();
    if abs + payload > bytes.len() {
        return None;
    }
    weight_buffer(
        c,
        (model.uid() as usize, idx),
        &bytes[abs..abs + payload],
    )
}

/// Encodes q8-matvec (row0=0) into the given encoder, writes to `y`. The bind
/// group and uniform are ref-counted by the command buffer until submit.
fn encode_matvec(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    rs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
) {
    let p_buf = uniform_u32x4(c, [(cols / 4) as u32, rows as u32, 0, 0]);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.layout,
        entries: &[
            bind_buf(0, weight),
            bind_buf(1, xs),
            bind_buf(2, rs),
            bind_buf(3, y),
            bind_buf(4, &p_buf),
        ],
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(&c.matvec);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1);
}

/// q1 cousin of `encode_matvec`: the q1 pipeline + `layout_q1` (4 bindings,
/// no row-scale — q1 carries its scales inside the tiles). params = the
/// `dispatch_q1` layout `[gpr/2, rows, 0, 0]`. Lets q1 QKV share one encoder.
fn encode_matvec_q1(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
) {
    let gpr = cols / 32;
    let p_buf = uniform_u32x4(c, [(gpr / 2) as u32, rows as u32, 0, 0]);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.layout_q1,
        entries: &[
            bind_buf(0, weight),
            bind_buf(1, xs),
            bind_buf(2, y),
            bind_buf(3, &p_buf),
        ],
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(&c.q1);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((rows as u32).div_ceil(8).min(MAX_WG), 1, 1);
}

/// Encode a resident q1 GEMM (batched prefill): Y[k,rows] = X[k,cols] @ Wᵀ, all
/// buffers already on the device. q1_mul_mm omits binding 2 (no row scale).
#[allow(dead_code)] // wired by forward_batch_graph (batched prefill, in progress)
/// Batched q4_tiled / q4tp GEMM into `enc` — the tile GEMMs the imagegen
/// path already used, wired for the graph.
///
/// `ematb` matched kind 0 and sent EVERYTHING else to the q1 kernel, which
/// for a q4tp weight is simply the wrong decoder. Together with `gemmable`
/// admitting only kinds 0 and 1, that shut batched prefill out of every
/// q4t and q4tp file — the same shape of bug as the `prep()` hole, and the
/// reason prefill ran one position at a time at 33 tok/s against 54 on
/// decode.
/// One-shot reason the BATCHED graph declined. Three silent fallbacks in a
/// row this session cost hours; a refusal that says nothing is the most
/// expensive kind of bug in this file.
fn bgraph_refused(why: &'static str) {
    use std::sync::atomic::{AtomicBool, Ordering};
    static SAID: AtomicBool = AtomicBool::new(false);
    if !SAID.swap(true, Ordering::Relaxed) {
        tracing::warn!("batch graph declined: {why}");
    }
}

/// Device mirror of one layer's sealed o1 state.
struct O1Dev {
    epoch: u64,
    meta: wgpu::Buffer,
    ring_k: wgpu::Buffer,
    ring_v: wgpu::Buffer,
    sink_k: wgpu::Buffer,
    sink_v: wgpu::Buffer,
    k_tilde: wgpu::Buffer,
    qt: wgpu::Buffer,
    mu: wgpu::Buffer,
    mz: wgpu::Buffer,
    that: wgpu::Buffer,
    g: usize,
    h: usize,
    m: usize,
    w: usize,
    ns: usize,
    scale: f32,
}

/// Upload (or reuse) a layer's o1 state. One upload per seal epoch: the
/// window ring and far skeleton then live and MUTATE on the device, and
/// the CPU copy is stale by design — the same one-way discipline as the
/// KV mirror.
fn o1_ensure(
    c: &Ctx,
    kv_id: u64,
    li: usize,
    views: &[crate::nystrom::O1DeviceView<'_>],
    epoch: u64,
) -> Option<()> {
    {
        let m = c.o1m.lock().unwrap();
        if let Some(d) = m.get(&(kv_id, li)) {
            if d.epoch == epoch {
                return Some(());
            }
        }
    }
    tracing::info!("o1_ensure: UPLOADING layer {li} (epoch {epoch})");
    let g0 = views.first()?;
    let (gcnt, hcnt, m, w, ns) = (views.len(), g0.heads.len(), g0.m_eff, g0.w, g0.sink_len);
    // Landmark threads park at lane 200+ in the attend kernel.
    if ns + w > 196 || m > 32 || g0.d > 256 || g0.dv > 256 {
        return None;
    }
    for v in views {
        if v.m_eff != m || v.w != w || v.sink_len != ns || v.heads.len() != hcnt {
            return None;
        }
    }
    let (d, dv) = (g0.d, g0.dv);
    let stor_f = |data: &[f32], label: &str| -> wgpu::Buffer {
        let b = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some(label),
            size: ((data.len() * 4).max(4)) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        c.queue.write_buffer(&b, 0, bytemuck::cast_slice(data));
        b
    };
    let mut meta = Vec::with_capacity(gcnt * 4);
    let (mut rk, mut rv, mut sk, mut sv, mut kt) = (vec![], vec![], vec![], vec![], vec![]);
    let (mut qt, mut mu, mut mz, mut th) = (vec![], vec![], vec![], vec![]);
    for v in views {
        meta.extend_from_slice(&[v.win_len as u32, v.win_head as u32, v.far_len as u32, 0]);
        // Ring buffers are cap-sized already (cap = w in skeleton mode).
        rk.extend_from_slice(v.win_k);
        rk.resize(rk.len() + (w * d - v.win_k.len().min(w * d)), 0.0);
        rv.extend_from_slice(v.win_v);
        rv.resize(rv.len() + (w * dv - v.win_v.len().min(w * dv)), 0.0);
        sk.extend_from_slice(v.sink_k);
        sv.extend_from_slice(v.sink_v);
        kt.extend_from_slice(v.k_tilde);
        for hh in &v.heads {
            qt.extend_from_slice(hh.q_tilde);
            mu.extend_from_slice(hh.mu);
            mz.extend_from_slice(hh.m_max);
            mz.extend_from_slice(hh.z_hat);
            th.extend_from_slice(hh.t_hat);
        }
    }
    let meta_b = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("o1-meta"),
        size: (meta.len() * 4) as u64,
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    c.queue
        .write_buffer(&meta_b, 0, bytemuck::cast_slice(&meta));
    let dev = O1Dev {
        epoch,
        meta: meta_b,
        ring_k: stor_f(&rk, "o1-rk"),
        ring_v: stor_f(&rv, "o1-rv"),
        sink_k: stor_f(&sk, "o1-sk"),
        sink_v: stor_f(&sv, "o1-sv"),
        k_tilde: stor_f(&kt, "o1-kt"),
        qt: stor_f(&qt, "o1-qt"),
        mu: stor_f(&mu, "o1-mu"),
        mz: stor_f(&mz, "o1-mz"),
        that: stor_f(&th, "o1-th"),
        g: gcnt,
        h: hcnt,
        m,
        w,
        ns,
        scale: g0.scale,
    };
    c.o1m.lock().unwrap().insert((kv_id, li), dev);
    Some(())
}

fn encode_q4_tile_mm(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    pipeline: &wgpu::ComputePipeline,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
    k: usize,
) {
    let p_buf = uniform_u32x4(c, [(cols / 4) as u32, rows as u32, k as u32, 0]);
    let layout = pipeline.get_bind_group_layout(0);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &layout,
        entries: &[
            bind_buf(0, weight),
            bind_buf(1, xs),
            bind_buf(2, y),
            bind_buf(3, &p_buf),
        ],
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(pipeline);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(
        (rows as u32).div_ceil(64).min(MAX_WG),
        (k as u32).div_ceil(64),
        1,
    );
}

fn encode_q1_mm(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
    k: usize,
) {
    let p_buf = uniform_u32x4(c, [(cols / 4) as u32, rows as u32, k as u32, 0]);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.layout_q1mm,
        entries: &[
            bind_buf(0, weight),
            bind_buf(1, xs),
            bind_buf(3, y),
            bind_buf(4, &p_buf),
        ],
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(&c.q1_mm);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(
        (rows as u32).div_ceil(64).min(MAX_WG),
        (k as u32).div_ceil(64),
        1,
    );
}

/// Encode a resident q8 GEMM (int8 weight + per-row f32 scale) into `enc`.
#[allow(dead_code)] // wired by forward_batch_graph (batched prefill, in progress)
fn encode_q8_mm(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    weight: &wgpu::Buffer,
    rs: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
    k: usize,
) {
    let p_buf = uniform_u32x4(c, [(cols / 4) as u32, rows as u32, k as u32, 0]);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.layout_mmm,
        entries: &[
            bind_buf(0, weight),
            bind_buf(1, xs),
            bind_buf(2, rs),
            bind_buf(3, y),
            bind_buf(4, &p_buf),
        ],
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(&c.mul_mm);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(
        (rows as u32).div_ceil(64).min(MAX_WG),
        (k as u32).div_ceil(64),
        1,
    );
}

/// Encode a plain f32 matvec (small unquantized projections) into `enc`.
/// The keyed twin of `encode_f32matvec`: every buffer at the call site is
/// stable for the layer's lifetime, so the group is built once per
/// (tag, sequence, layer) and reused — fresh groups here were a measurable
/// share of the chain's host encoding.
#[allow(clippy::too_many_arguments)]
fn encode_f32matvec_k(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
    bkey: (u8, u64, usize),
) {
    let mut pass = begin_pass(enc);
    encode_f32matvec_k_p(&mut pass, c, weight, xs, y, rows, cols, bkey);
}

#[allow(clippy::too_many_arguments)]
/// The chain's f32 matvec: 256 threads a row instead of 64.
#[allow(clippy::too_many_arguments)]
fn encode_f32matvec_w_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
    bkey: (u8, u64, usize),
) {
    // Rows are what give this kernel its workgroups. With very few of them,
    // split the shared axis instead: rows·splits workgroups and one cheap
    // merge pass. CMF_DSV4_F32SPLIT=0 reverts.
    // 512, not 4096: the stands' hc·dim is 512, and a threshold that no toy
    // can reach is a path no gate can check — this one reached the release
    // unexercised and failed there on the first dispatch.
    if rows <= 32 && cols >= 512 && f32_split() {
        let nsplit = 8usize;
        let chunk = cols.div_ceil(nsplit);
        // The caller's ROLE tag has to be in the key. Both hyper-connection
        // mixes of a layer arrive here with the same (sequence, layer) and
        // different roles, and sharing one slot means the second one's
        // parameters reach the first one's dispatch — queue writes all land
        // before the submission. CMF_DSV4_SLOT_CHECK=1 named this exactly:
        // "slot (186, 1, 0) written 2 times".
        let key = bkey.2 * 256 + bkey.0 as usize;
        // The partials buffer likewise: two roles in one submission would
        // otherwise write the same scratch.
        let part = frame_buf(c, 115 + (bkey.0 & 1), rows * nsplit * 4, false);
        let p = uni_slot(c, 186, bkey.1, key,
            [cols as u32, rows as u32, nsplit as u32, chunk as u32]);
        let b1 = cached_bind(c, (188, bkey.1, key), || {
            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &c.f32_mv_split.get_bind_group_layout(0),
                entries: &[
                    bind_buf(0, weight),
                    bind_buf(1, xs),
                    bind_buf(2, y),
                    bind_buf(3, &p),
                    bind_buf(4, &part),
                ],
            })
        });
        pass.set_pipeline(&c.f32_mv_split);
        pass.set_bind_group(0, &b1, &[]);
        pass.dispatch_workgroups(rows as u32, nsplit as u32, 1);
        let b2 = cached_bind(c, (190, bkey.1, key), || {
            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &c.f32_mv_merge.get_bind_group_layout(0),
                entries: &[
                    bind_buf(0, weight),
                    bind_buf(1, xs),
                    bind_buf(2, y),
                    bind_buf(3, &p),
                    bind_buf(4, &part),
                ],
            })
        });
        pass.set_pipeline(&c.f32_mv_merge);
        pass.set_bind_group(0, &b2, &[]);
        pass.dispatch_workgroups(rows as u32, 1, 1);
        return;
    }
    let pipe = if rows < 64 { &c.f32_matvec_x } else { &c.f32_matvec_w };
    let bind = cached_bind(c, bkey, || {
        let p = uniform_u32x4(c, [cols as u32, rows as u32, 0, 0]);
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &pipe.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, weight),
                bind_buf(1, xs),
                bind_buf(2, y),
                bind_buf(3, &p),
            ],
        })
    });
    pass.set_pipeline(pipe);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1);
}

fn encode_f32matvec_k_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
    bkey: (u8, u64, usize),
) {
    let bind = cached_bind(c, bkey, || {
        let p_buf = uniform_u32x4(c, [cols as u32, rows as u32, 0, 0]);
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.layout_f32,
            entries: &[
                bind_buf(0, weight),
                bind_buf(1, xs),
                bind_buf(2, y),
                bind_buf(3, &p_buf),
            ],
        })
    });
    pass.set_pipeline(&c.f32_matvec);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1);
}

fn encode_f32matvec(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
) {
    let p_buf = uniform_u32x4(c, [cols as u32, rows as u32, 0, 0]);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.layout_f32,
        entries: &[
            bind_buf(0, weight),
            bind_buf(1, xs),
            bind_buf(2, y),
            bind_buf(3, &p_buf),
        ],
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(&c.f32_matvec);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1);
}

/// `encode_f32matvec` with byte offsets into `xs` and `y` — the batched MoE
/// router runs the SAME f32 kernel per token (bit-for-bit the logits the
/// parity-proven path produced) but reads its token's row of the batch
/// hidden and writes its token's slice of the logit plane directly. Both
/// offsets land on 256-byte boundaries (t·hidden·4 and t·n_exp·4 with
/// hidden=2048, n_exp≤256), which is all wgpu asks of a buffer binding.
#[allow(clippy::too_many_arguments)]
/// Content-keyed cache for 8-word uniforms — the per-token GDN params of a
/// batched chunk repeat every chunk, and `unif` mints a fresh buffer per
/// call (the OOM lesson of the folded-gate work).
fn uniform_u32x8(c: &Ctx, v: [u32; 8]) -> wgpu::Buffer {
    let mut u = c.uniforms8.lock().unwrap();
    if let Some(b) = u.get(&v) {
        return b.clone();
    }
    let b = c
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: None,
            contents: bytemuck::cast_slice(&v),
            usage: wgpu::BufferUsages::UNIFORM,
        });
    u.insert(v, b.clone());
    b
}

fn encode_f32matvec_off(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    xs_off: u64,
    y: &wgpu::Buffer,
    y_off: u64,
    y_len: u64,
    rows: usize,
    cols: usize,
) {
    let p_buf = uniform_u32x4(c, [cols as u32, rows as u32, 0, 0]);
    let entries = [
        wgpu::BindGroupEntry {
            binding: 0,
            resource: weight.as_entire_binding(),
        },
        wgpu::BindGroupEntry {
            binding: 1,
            resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                buffer: xs,
                offset: xs_off,
                size: wgpu::BufferSize::new((cols * 4) as u64),
            }),
        },
        wgpu::BindGroupEntry {
            binding: 2,
            resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                buffer: y,
                offset: y_off,
                size: wgpu::BufferSize::new(y_len),
            }),
        },
        wgpu::BindGroupEntry {
            binding: 3,
            resource: p_buf.as_entire_binding(),
        },
    ];
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.layout_f32,
        entries: &entries,
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(&c.f32_matvec);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1);
}

/// Encode a q4_tiled or q1t matvec into `enc` (same 4-slot layout as q1, but
/// params are [gpr, rows, cols]; q1t reads its sparse overlay from the tail of
/// the same buffer). `pipeline` is c.q4b or c.q1t.
/// Attend kernel flavor by head_dim: stride-129 (16.5 KB of workgroup
/// memory, exists on every device) for hd <= 128, stride-257 for larger
/// heads (desktop-only — see Ctx::hd_cap).
fn attend_pipes(c: &Ctx, hd: usize) -> (&wgpu::ComputePipeline, &wgpu::BindGroupLayout) {
    if hd <= 128 {
        (&c.gqa_attend_s, &c.layout_attend_s)
    } else {
        (&c.gqa_attend, &c.layout_attend)
    }
}

fn attend_part_pipes(c: &Ctx, hd: usize) -> (&wgpu::ComputePipeline, &wgpu::BindGroupLayout) {
    if hd <= 128 {
        (&c.attend_part_s, &c.layout_attend_part_s)
    } else {
        (&c.attend_part, &c.layout_attend_part)
    }
}

/// `q4tp_matvec4`: the q1t-like binding set plus the weight buffer AGAIN at
/// slot 4 as the kernel's vec4 nibble view.
/// Parameters for the q4tp matvec PAIR (`q4tp_matvec16` / `q4tp_matvec4`).
///
/// Word 2 is the batch count for these two kernels and `cols` for everything
/// else bound to `Q1Params`. Building it by hand at each call site put `cols`
/// into the batch slot of the chain's busiest projection encoder — the
/// kernel then ran every row block four thousand times and decoding fell
/// from 27 tok/s to 0.35. One constructor, so the mistake has nowhere to
/// live.
fn q4tp_mv_params(c: &Ctx, gpr: usize, rows: usize, batch: usize) -> wgpu::Buffer {
    q4tp_mv_params_w(c, gpr, rows, batch, 0)
}

/// The same, with word 3 — the grouped projection's row window, which slides
/// the activation with the row. Batch and window are mutually exclusive
/// modes; the kernel reads the batch first.
fn q4tp_mv_params_w(
    c: &Ctx,
    gpr: usize,
    rows: usize,
    batch: usize,
    window: usize,
) -> wgpu::Buffer {
    debug_assert!((1..=64).contains(&batch), "batch {batch} out of range");
    debug_assert!(
        batch == 1 || window == 0,
        "batch {batch} with window {window}: the kernel honours one or the other"
    );
    uniform_u32x4(c, [gpr as u32, rows as u32, batch as u32, window as u32])
}

fn encode_q4tp_mv4(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
) {
    encode_q4tp_mv4_b(c, enc, weight, xs, y, rows, cols, 1);
}

/// The same projection against `batch` activation vectors laid end to end,
/// writing `batch * rows` outputs. The weight is read once for all of them,
/// which is the point: a prompt chunk and a speculative verify both need
/// exactly this and nothing else.
///
/// Always encodes: the row blocking sits inside one batch element by
/// construction, so no shape is refused.
#[allow(clippy::too_many_arguments)]
fn encode_q4tp_mv4_b(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
    batch: usize,
) -> bool {
    let gpr = cols / 32;
    // Narrow shapes (one group per lane in the 8-row kernel) go 16-rows.
    // With a batch the divisibility decides first: a block that straddled
    // two tokens would read one token's weights against the other's x.
    let (pipe, per_wg) = if gpr <= 64 {
        (&c.q4tp_mv16, 16u32)
    } else if batch == 1 {
        (&c.q4tp_mv16w, 16u32)
    } else {
        (&c.q4tp_mv4, 8u32)
    };
    let p_buf = q4tp_mv_params(c, gpr, rows, batch);
    let layout = pipe.get_bind_group_layout(0);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &layout,
        entries: &[
            bind_buf(0, weight),
            bind_buf(2, y),
            bind_buf(3, &p_buf),
            bind_buf(4, weight),
            bind_buf(5, xs),
        ],
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(pipe);
    pass.set_bind_group(0, &bind, &[]);
    // One workgroup per (row block, batch element); the batch is the fast
    // axis inside the kernel, so a row block is never split across two.
    pass.dispatch_workgroups(
        ((rows as u32).div_ceil(per_wg) * batch as u32).min(MAX_WG),
        1,
        1,
    );
    true
}

/// The one-row q4tp kernel, which is the one `gpu_q4tp_parity` blesses.
/// `encode_q4tp_mv4` picks a wider variant by shape; when a frame has to
/// agree with the CPU to the last bit, agreement beats throughput.
#[allow(clippy::too_many_arguments)]
fn encode_q4tp_mv1(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
    bkey: (u8, u64, usize),
) {
    let mut pass = begin_pass(enc);
    encode_q4tp_mv1_p(&mut pass, c, weight, xs, y, rows, cols, bkey);
}

#[allow(clippy::too_many_arguments)]
fn encode_q4tp_mv1_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
    bkey: (u8, u64, usize),
) {
    let bind = cached_bind(c, bkey, || {
        let p_buf = uniform_u32x4(c, [(cols / 32) as u32, rows as u32, cols as u32, 0]);
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.q4tp_mv.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, weight),
                bind_buf(1, xs),
                bind_buf(2, y),
                bind_buf(3, &p_buf),
            ],
        })
    });
    pass.set_pipeline(&c.q4tp_mv);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1);
}

/// The wide q4tp matvec, as a dispatch inside a caller's pass.
///
/// The chain used the one-row kernel throughout — one workgroup a row — while
/// `encode_q4tp_mv4` picks an 8- or 16-row variant by shape and is the
/// default everywhere else, including the head that measured 0.32 ms against
/// the host's 9.43. The projections inside a chained layer are the same
/// shapes; there is no reason for them to take the narrow kernel.
///
/// It sums the same products in a different lane order, so it is a contract
/// change and carries `CMF_DSV4_MV4=0`.
#[allow(clippy::too_many_arguments)]
fn encode_q4tp_mvw_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
    bkey: (u8, u64, usize),
) {
    if !chain_mv4() {
        encode_q4tp_mv1_p(pass, c, weight, xs, y, rows, cols, bkey);
        return;
    }
    let gpr = cols / 32;
    let (pipe, per_wg) = if gpr <= 64 {
        (&c.q4tp_mv16, 16u32)
    } else {
        (&c.q4tp_mv4, 8u32)
    };
    let bind = cached_bind(c, bkey, || {
        let p_buf = q4tp_mv_params(c, gpr, rows, 1);
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &pipe.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, weight),
                bind_buf(2, y),
                bind_buf(3, &p_buf),
                bind_buf(4, weight),
                bind_buf(5, xs),
            ],
        })
    });
    pass.set_pipeline(pipe);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((rows as u32).div_ceil(per_wg).min(MAX_WG), 1, 1);
}

/// The grouped low-rank projection through the EIGHT-ROW q4tp kernel.
///
/// Its weights are ordinary q4tp — the one-row kernel it had was a copy of
/// q4tp_matvec with one added term in the activation index — so the only
/// thing standing between it and the register-blocked kernel was that
/// sliding window, which `_p1` now carries. Measured 3.82 ms against
/// wo_b's 1.24 on comparable weights through the fast one.
///
/// Only when the group width is a multiple of 8: the 8 rows a workgroup
/// owns must share one window, or the pair-blocked x fetch is wrong.
#[allow(clippy::too_many_arguments)]
fn encode_o_lora_mv4_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
    lora: usize,
    bkey: (u8, u64, usize),
) -> bool {
    if lora == 0 || cols % 32 != 0 {
        return false;
    }
    let gpr = cols / 32;
    let (pipe, per_wg) = if gpr <= 64 {
        (&c.q4tp_mv16, 16u32)
    } else {
        (&c.q4tp_mv4, 8u32)
    };
    // The rows a workgroup owns must share one activation window, and a
    // workgroup owns `per_wg` consecutive rows starting at a multiple of
    // `per_wg`. The 16-row kernel therefore needs a width divisible by 16,
    // not by 8 — which is exactly the case the stands hit (their gpr is 2,
    // so they take the 16-row path) and exactly why this read 133.433
    // against the host's 133.396.
    if lora % per_wg as usize != 0 {
        return false;
    }
    let bind = cached_bind(c, bkey, || {
        let p = q4tp_mv_params_w(c, gpr, rows, 1, lora);
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &pipe.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, weight),
                bind_buf(2, y),
                bind_buf(3, &p),
                bind_buf(4, weight),
                bind_buf(5, xs),
            ],
        })
    });
    pass.set_pipeline(pipe);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((rows as u32).div_ceil(per_wg).min(MAX_WG), 1, 1);
    true
}

/// `CMF_DSV4_MOE4=0` puts the q2tp experts back on one row a workgroup.
/// `CMF_DSV4_F32SPLIT=1` splits the few-row f32 matvec over its columns.
///
/// OFF: it measured 27.1 tok/s against the one-dispatch path's 27.2. The
/// commit that claimed to turn it off did not: its patch made two edits and
/// the second failed an assertion, so the file was never written and only
/// the message changed. A review reading the source rather than the log
/// caught it.
fn f32_split() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_F32SPLIT").is_ok_and(|v| v != "0"))
}

/// `CMF_DSV4_OLORA_MV4=0` keeps the grouped projection on its own kernel.
///
/// It is 3.82 ms of a 30.6 ms chain while wo_b — comparable weights through
/// the register-blocked q4tp kernel — is 1.24, and the only difference is
/// the activation window that slides with the row. `_p1` carries it now, in
/// BOTH the 8-row and the 16-row kernel: teaching only the 8-row one is
/// what made this read 133.433 against the host's 133.396, because the
/// stands have gpr = 2 and take the 16-row path. The window was simply
/// missing there, and the rows kept reading the first group's activations.
fn olora_mv4() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_OLORA_MV4").map(|v| v != "0").unwrap_or(true))
}

/// OFF by default: the A/B put four rows a workgroup at 24.9 tok/s against
/// 25.7 without, with the chain's wait identical to within 0.12 ms — so the
/// difference is host noise and the change buys nothing measurable. Kept
/// switchable; the default takes the path that has been measured longer.
fn moe4() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_MOE4").is_ok_and(|v| v != "0"))
}

/// `CMF_DSV4_OLORA=1|2|3` pins the grouped projection to the one-row,
/// 256-thread or four-row kernel, for the A/B that decides which.
fn olora_pick() -> Option<u32> {
    static P: std::sync::OnceLock<Option<u32>> = std::sync::OnceLock::new();
    *P.get_or_init(|| std::env::var("CMF_DSV4_OLORA").ok().and_then(|v| v.parse().ok()))
}

/// `CMF_DSV4_MV4=0` puts the chain back on the one-row q4tp kernel.
fn chain_mv4() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_MV4").map(|v| v != "0").unwrap_or(true))
}

fn encode_q1t_like(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    pipeline: &wgpu::ComputePipeline,
    weight: &wgpu::Buffer,
    xs: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
) {
    let gpr = cols / 32;
    let p_buf = uniform_u32x4(c, [gpr as u32, rows as u32, cols as u32, 0]);
    let layout = pipeline.get_bind_group_layout(0);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &layout,
        entries: &[
            bind_buf(0, weight),
            bind_buf(1, xs),
            bind_buf(2, y),
            bind_buf(3, &p_buf),
        ],
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(pipeline);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1);
}

/// Fused SiLU(gate)·up → Q4Block down-proj: one dispatch instead of silu + matvec.
fn encode_silu_down(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    weight: &wgpu::Buffer,
    gate: &wgpu::Buffer,
    up: &wgpu::Buffer,
    y: &wgpu::Buffer,
    rows: usize,
    cols: usize,
) {
    let gpr = cols / 32;
    let p_buf = uniform_u32x4(c, [gpr as u32, rows as u32, cols as u32, 0]);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.layout_silu_down,
        entries: &[
            bind_buf(0, weight),
            bind_buf(1, gate),
            bind_buf(2, up),
            bind_buf(3, y),
            bind_buf(4, &p_buf),
        ],
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(&c.silu_down);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1);
}

/// q1 batched matvec: N q1 projections (e.g. QKV) in ONE submit + one
/// readback — the chain-fusion that `matvec_batch` does for q8, now for
/// 1-bit weights. Bails to `false` (→ CPU) on any budget/shape refusal so
/// the caller's fallback stays intact.
fn matvec_batch_q1(model: &Arc<CmfModel>, jobs: &[BatchJob], out: &mut [&mut [f32]]) -> bool {
    let Some(c) = ctx() else { return false };
    let bytes = model.primary_bytes();
    // Resident weight per job (VRAM cache; over-budget/oob → honest CPU).
    let mut weights = Vec::with_capacity(jobs.len());
    for j in jobs {
        let gpr = j.cols / 32;
        if j.rows == 0 || j.cols % 32 != 0 || gpr % 2 != 0 || j.xs.len() < j.cols {
            return false;
        }
        let entry = &model.tensors[j.idx];
        if entry.shape.first().copied().unwrap_or(0) < j.rows {
            return false;
        }
        let Some(abs) = model.entry_abs_offset(entry) else {
            return false;
        };
        let plen = j.rows * gpr * 6;
        if abs + plen > bytes.len() {
            return false;
        }
        let Some(w) = weight_buffer(c, (model.uid() as usize, j.idx), &bytes[abs..abs + plen])
        else {
            return false;
        };
        weights.push(w);
    }
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("q1-batch"),
        });
    let mut y_bufs = Vec::with_capacity(jobs.len());
    for (j, w) in jobs.iter().zip(&weights) {
        let xs_b = storage_bytes(c, bytemuck::cast_slice(&j.xs[..j.cols]));
        let y_b = rw_f32(c, j.rows, true);
        encode_matvec_q1(c, &mut enc, w, &xs_b, &y_b, j.rows, j.cols);
        y_bufs.push(y_b);
    }
    // ONE pooled staging buffer for all outputs, one map (mirror the q8 path).
    let total: u64 = jobs.iter().map(|j| (j.rows * 4) as u64).sum();
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        total,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "q1-batch-stage",
    );
    let mut off = 0u64;
    for (y_b, j) in y_bufs.iter().zip(jobs) {
        enc.copy_buffer_to_buffer(y_b, 0, &stage, off, (j.rows * 4) as u64);
        off += (j.rows * 4) as u64;
    }
    submit(c, enc.finish());
    stage.slice(..total).map_async(wgpu::MapMode::Read, |_| {});
    if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
        return false;
    }
    {
        let Ok(data) = stage.slice(..total).get_mapped_range() else {
            return false;
        };
        let mut off = 0usize;
        for (j, o) in jobs.iter().zip(out.iter_mut()) {
            o[..j.rows].copy_from_slice(bytemuck::cast_slice(&data[off..off + j.rows * 4]));
            off += j.rows * 4;
        }
    }
    stage.unmap();
    drop(sc);
    true
}

/// Layer MoE-FFN in a single submission: for each expert gate/up-matvec →
/// silu·mul·col_down → down-matvec → y += w·d. Intermediate buffers are
/// GPU-resident, one sync per layer.
pub fn moe_block(model: &Arc<CmfModel>, jobs: &[MoeJob], out: &mut [f32]) -> bool {
    if jobs.iter().any(|j| j.q1) {
        return false; // q1 WGSL kernel not implemented yet — honest CPU
    }
    let Some(c) = ctx() else { return false };
    if jobs.is_empty() {
        return false;
    }
    let q4t = jobs[0].q4t;
    let q4tp = jobs[0].q4tp;
    if jobs.iter().any(|j| j.q4t != q4t || j.q4tp != q4tp) {
        return false; // mixed job kinds — honest CPU
    }
    if q4t && q4tp {
        return false; // a trio is one layout or the other
    }
    let inter = jobs[0].gate.1;
    let hidden = jobs[0].down.1;
    if out.len() != hidden {
        return false;
    }
    // Resident weights of all triples — validate first (fail → CPU entirely).
    let fetch = |idx: usize, rows: usize, cols: usize| -> Option<wgpu::Buffer> {
        if q4tp {
            // Three planes, not a flat tile: nibbles, then the per-row
            // (lo, step) pair, then the 5-bit rung codes. Only the layout
            // owner knows the total, so ask it rather than re-deriving.
            let n = cortiq_core::quant::expected_nbytes(
                cortiq_core::TensorDtype::Q4TiledP,
                &[rows, cols],
            )?;
            tensor_weight_sized(c, model, idx, rows, n)
        } else if q4t {
            tensor_weight_sized(c, model, idx, rows, rows * (cols / 32) * 18)
        } else {
            tensor_weight(c, model, idx, rows, cols)
        }
    };
    let mut w3 = Vec::with_capacity(jobs.len());
    for j in jobs {
        let (gi, gr, gc, _) = j.gate;
        let (ui, ur, uc, _) = j.up;
        let (di, dr, dc, _) = j.down;
        let align = if q4t || q4tp { 32 } else { 4 };
        if gc % align != 0 || uc % align != 0 || dc % align != 0 {
            return false;
        }
        let (Some(gw), Some(uw), Some(dw)) =
            (fetch(gi, gr, gc), fetch(ui, ur, uc), fetch(di, dr, dc))
        else {
            return false;
        };
        w3.push((gw, uw, dw));
    }

    let g_buf = rw_f32(c, inter, false);
    let u_buf = rw_f32(c, inter, false);
    let a_buf = rw_f32(c, inter, false);
    let d_buf = rw_f32(c, hidden, false);
    let y_buf = rw_f32(c, hidden, true);

    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("moe") });

    // y = 0
    {
        let np = uniform_u32x4(c, [hidden as u32, 0, 0, 0]);
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.layout_zero,
            entries: &[bind_buf(0, &y_buf), bind_buf(1, &np)],
        });
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.zero);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups((hidden as u32).div_ceil(256), 1, 1);
    }

    for (j, (gw, uw, dw)) in jobs.iter().zip(&w3) {
        let (_, gr, gc, grs) = &j.gate;
        let (_, ur, uc, urs) = &j.up;
        let (_, dr, dc, drs) = &j.down;
        // Per-tensor scale/col buffers are stable across tokens — cache
        // them like the matvec row-scales instead of re-uploading.
        let mut rs_map = c.rs_bufs.lock().unwrap();
        let mut cached = |tag: usize, idx: usize, data: &[f32]| -> wgpu::Buffer {
            rs_map
                .entry((idx.wrapping_mul(1_000_003) ^ tag, usize::MAX - 1))
                .or_insert_with(|| {
                    crate::gpu::probe_note_cold();
                    storage_bytes(c, bytemuck::cast_slice(data))
                })
                .clone()
        };
        let grs_b = cached(1, j.gate.0, grs);
        let urs_b = cached(2, j.up.0, urs);
        let drs_b = cached(3, j.down.0, drs);
        let has_col = !j.down_col.is_empty();
        let col_b = if has_col {
            cached(4, j.down.0, j.down_col)
        } else {
            cached(5, usize::MAX, &[0f32]) // dummy, gated by f=0
        };
        drop(rs_map);
        let xsg = storage_bytes(c, bytemuck::cast_slice(&j.xs_gate));
        let xsu = storage_bytes(c, bytemuck::cast_slice(&j.xs_up));

        if q4tp {
            encode_q1t_like(c, &mut enc, &c.q4tp_mv, gw, &xsg, &g_buf, *gr, *gc);
            encode_q1t_like(c, &mut enc, &c.q4tp_mv, uw, &xsu, &u_buf, *ur, *uc);
        } else if q4t {
            encode_q1t_like(c, &mut enc, &c.q4t_mv, gw, &xsg, &g_buf, *gr, *gc);
            encode_q1t_like(c, &mut enc, &c.q4t_mv, uw, &xsu, &u_buf, *ur, *uc);
        } else {
            encode_matvec(c, &mut enc, gw, &xsg, &grs_b, &g_buf, *gr, *gc);
            encode_matvec(c, &mut enc, uw, &xsu, &urs_b, &u_buf, *ur, *uc);
        }
        // act = silu(g)·u·col_down
        {
            let np = uniform_u32x4(c, [inter as u32, has_col as u32, j.swiglu_limit.to_bits(), 0]);
            let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &c.layout_silu,
                entries: &[
                    bind_buf(0, &g_buf),
                    bind_buf(1, &u_buf),
                    bind_buf(2, &col_b),
                    bind_buf(3, &a_buf),
                    bind_buf(4, &np),
                ],
            });
            let mut pass = begin_pass(&mut enc);
            pass.set_pipeline(&c.silu);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups((inter as u32).div_ceil(256), 1, 1);
        }
        if q4tp {
            encode_q1t_like(c, &mut enc, &c.q4tp_mv, dw, &a_buf, &d_buf, *dr, *dc);
        } else if q4t {
            encode_q1t_like(c, &mut enc, &c.q4t_mv, dw, &a_buf, &d_buf, *dr, *dc);
        } else {
            encode_matvec(c, &mut enc, dw, &a_buf, &drs_b, &d_buf, *dr, *dc);
        }
        // y += w·d
        {
            let wp = uniform_u32x4(c, [j.w.to_bits(), hidden as u32, 0, 0]);
            let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &c.layout_axpy,
                entries: &[bind_buf(0, &d_buf), bind_buf(1, &y_buf), bind_buf(2, &wp)],
            });
            let mut pass = begin_pass(&mut enc);
            pass.set_pipeline(&c.axpy);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups((hidden as u32).div_ceil(256), 1, 1);
        }
    }
    // Hold the scratch lock across the readback: with concurrent server
    // slots two ops must not share the staging buffer mid-flight.
    let mut sc = c.scratch.lock().unwrap();
    let stage_buf = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        (hidden * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "moe-stage",
    );
    let ok = readback(c, enc, &y_buf, &stage_buf, (hidden * 4) as u64, out);
    drop(sc);
    ok
}

/// N independent q8-matvec (GDN projections of one input) in a single submission.
pub fn matvec_batch(model: &Arc<CmfModel>, jobs: &[BatchJob], out: &mut [&mut [f32]]) -> bool {
    let Some(c) = ctx() else { return false };
    if jobs.is_empty() || jobs.len() != out.len() {
        return false;
    }
    // q1 jobs carry tile-embedded scales (empty row_scale) and need the q1
    // pipeline — route the whole batch to the q1 encoder. Mixed batches
    // (shouldn't happen: QKV share a dtype) fall to the CPU path.
    // wgpu has a q1 batched kernel and no q4t/q4tp twin, so those layouts
    // keep the CPU path here rather than being fed to the wrong kernel.
    if jobs.iter().any(|j| {
        matches!(
            j.layout,
            crate::gpu::BatchLayout::Q4t | crate::gpu::BatchLayout::Q4tp
        )
    }) {
        return false;
    }
    let n_q1 = jobs
        .iter()
        .filter(|j| j.layout == crate::gpu::BatchLayout::Q1)
        .count();
    if n_q1 == jobs.len() {
        return matvec_batch_q1(model, jobs, out);
    }
    if n_q1 != 0 {
        return false;
    }
    let mut weights = Vec::with_capacity(jobs.len());
    for j in jobs {
        if j.cols % 4 != 0 {
            return false;
        }
        let Some(w) = tensor_weight(c, model, j.idx, j.rows, j.cols) else {
            return false;
        };
        weights.push(w);
    }
    let mut y_bufs = Vec::with_capacity(jobs.len());
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("batch"),
        });
    for (j, w) in jobs.iter().zip(&weights) {
        let rs_b = storage_bytes(c, bytemuck::cast_slice(j.row_scale));
        let xs_b = storage_bytes(c, bytemuck::cast_slice(&j.xs));
        let y_b = rw_f32(c, j.rows, true);
        encode_matvec(c, &mut enc, w, &xs_b, &rs_b, &y_b, j.rows, j.cols);
        y_bufs.push(y_b);
    }
    // ONE pooled staging buffer for all outputs (per-job offsets),
    // one map — instead of N fresh MAP_READ allocations per call.
    let total: u64 = jobs.iter().map(|j| (j.rows * 4) as u64).sum();
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        total,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "batch-stage",
    );
    let mut off = 0u64;
    for (y_b, j) in y_bufs.iter().zip(jobs) {
        enc.copy_buffer_to_buffer(y_b, 0, &stage, off, (j.rows * 4) as u64);
        off += (j.rows * 4) as u64;
    }
    submit(c, enc.finish());
    stage.slice(..total).map_async(wgpu::MapMode::Read, |_| {});
    if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
        return false;
    }
    {
        let Ok(data) = stage.slice(..total).get_mapped_range() else {
            return false;
        };
        let mut off = 0usize;
        for (j, o) in jobs.iter().zip(out.iter_mut()) {
            o[..j.rows].copy_from_slice(bytemuck::cast_slice(&data[off..off + j.rows * 4]));
            off += j.rows * 4;
        }
    }
    stage.unmap();
    drop(sc);
    true
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn wgpu_q8_matvec_matches_cpu_reference() {
        // Force the wgpu path on (Metal-via-wgpu locally; Vulkan on the server).
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping parity test");
            return;
        };
        let (rows, cols) = (256usize, 64usize); // cols % 4 == 0
        // Synthetic int8 weights + row scales + pre-scaled activations.
        let mut q = vec![0i8; rows * cols];
        for (i, v) in q.iter_mut().enumerate() {
            *v = (((i * 37 + 11) % 255) as i32 - 127) as i8;
        }
        let rs: Vec<f32> = (0..rows).map(|r| 0.01 + (r % 7) as f32 * 0.003).collect();
        let xs: Vec<f32> = (0..cols).map(|i| ((i % 13) as f32 - 6.0) * 0.1).collect();

        // CPU reference: y[o] = rs[o] * Σ q[o,i]·xs[i].
        let mut want = vec![0f32; rows];
        for o in 0..rows {
            let mut acc = 0f32;
            for i in 0..cols {
                acc += q[o * cols + i] as f32 * xs[i];
            }
            want[o] = acc * rs[o];
        }

        let qbytes: &[u8] = bytemuck::cast_slice(&q);
        let mut got = vec![0f32; rows];
        assert!(dispatch_matvec(
            c, None, qbytes, 0, &rs, &xs, rows, cols, &mut got
        ));

        let max_d = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(max_d < 1e-3, "wgpu q8_matvec ≠ CPU: max|Δ| = {max_d}");

        // Also check the row0 offset: the range [rows/2, rows) of the full
        // tensor must match the tail of the reference.
        let r0 = rows / 2;
        let mut got2 = vec![0f32; rows - r0];
        assert!(dispatch_matvec(
            c,
            None,
            qbytes,
            r0,
            &rs[r0..],
            &xs,
            rows - r0,
            cols,
            &mut got2
        ));
        let max_d2 = want[r0..]
            .iter()
            .zip(&got2)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(max_d2 < 1e-3, "wgpu row0 offset ≠ CPU: max|Δ| = {max_d2}");
    }

    /// Quantifies the whole-token-graph ceiling on THIS device: K chained
    /// matvecs run as K separate submit+readback ops (today's per-op path)
    /// vs the same K dispatches in ONE command buffer with a single readback
    /// (intermediates stay on the GPU — what the graph does). The ratio is how
    /// much the submit/PCIe-readback wall is costing per token.
    /// Run: `CMF_GPU=wgpu cargo test -p cortiq-engine --release --features gpu
    ///       --test-threads 1 wgpu_chain_probe -- --ignored --nocapture`
    #[test]
    #[ignore]
    fn wgpu_chain_probe() {
        use std::time::Instant;
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping");
            return;
        };
        let n: usize = std::env::var("CMF_CHAIN_N")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(896);
        let k: usize = std::env::var("CMF_CHAIN_K")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(100);
        assert!(n % 4 == 0);
        // Resident n×n q8 weights + row scales (values irrelevant — timing only).
        let q = vec![1i8; n * n];
        let w = c
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("probe-w"),
                contents: bytemuck::cast_slice(&q),
                usage: wgpu::BufferUsages::STORAGE,
            });
        let rs = c
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("probe-rs"),
                contents: bytemuck::cast_slice(&vec![1f32; n]),
                usage: wgpu::BufferUsages::STORAGE,
            });
        let p = c
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("probe-p"),
                contents: bytemuck::cast_slice(&[(n / 4) as u32, n as u32, 0u32, 0u32]),
                usage: wgpu::BufferUsages::UNIFORM,
            });
        let mkbuf = |lbl| {
            c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some(lbl),
                size: (n * 4) as u64,
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_SRC
                    | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            })
        };
        let a = mkbuf("probe-a");
        let b = mkbuf("probe-b");
        c.queue
            .write_buffer(&a, 0, bytemuck::cast_slice(&vec![0.01f32; n]));
        let stage = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("probe-stage"),
            size: (n * 4) as u64,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        let bg = |xs: &wgpu::Buffer, y: &wgpu::Buffer| {
            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("probe-bg"),
                layout: &c.layout,
                entries: &[
                    bind_buf(0, &w),
                    bind_buf(1, xs),
                    bind_buf(2, &rs),
                    bind_buf(3, y),
                    bind_buf(4, &p),
                ],
            })
        };
        let bg_ab = bg(&a, &b);
        let bg_ba = bg(&b, &a);
        let wg = (n as u32).min(MAX_WG);
        let readback = |buf: &wgpu::Buffer, enc: wgpu::CommandEncoder| {
            let mut enc = enc;
            enc.copy_buffer_to_buffer(buf, 0, &stage, 0, (n * 4) as u64);
            submit(c, enc.finish());
            stage.slice(..).map_async(wgpu::MapMode::Read, |_| {});
            let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
            let _ = stage.slice(..).get_mapped_range();
            stage.unmap();
        };
        let dispatch = |enc: &mut wgpu::CommandEncoder, even: bool| {
            let mut pass = begin_pass(enc);
            pass.set_pipeline(&c.matvec);
            pass.set_bind_group(0, if even { &bg_ab } else { &bg_ba }, &[]);
            pass.dispatch_workgroups(wg, 1, 1);
        };
        // Warm.
        for _ in 0..3 {
            let mut e = c
                .device
                .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
            dispatch(&mut e, true);
            readback(&b, e);
        }
        // Per-op: K submits + K readbacks.
        let t = Instant::now();
        for i in 0..k {
            let mut e = c
                .device
                .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
            dispatch(&mut e, i % 2 == 0);
            readback(if i % 2 == 0 { &b } else { &a }, e);
        }
        let per_op = t.elapsed().as_secs_f64();
        // Fused: K dispatches, ONE submit + ONE readback.
        let t = Instant::now();
        let mut e = c
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
        for i in 0..k {
            dispatch(&mut e, i % 2 == 0);
        }
        readback(if (k - 1) % 2 == 0 { &b } else { &a }, e);
        let fused = t.elapsed().as_secs_f64();
        eprintln!(
            "CHAIN PROBE n={n} k={k}: per-op {:.2} ms ({:.3} ms/op) | fused {:.2} ms | speedup {:.2}× | submit+readback wall ≈ {:.3} ms/op",
            per_op * 1e3,
            per_op * 1e3 / k as f64,
            fused * 1e3,
            per_op / fused,
            (per_op - fused) * 1e3 / (k - 1) as f64,
        );
    }

    #[test]
    fn wgpu_q1_matvec_matches_cpu_reference() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping q1 parity test");
            return;
        };
        let (rows, cols) = (33usize, 256usize); // gpr = 8 (even), odd rows
        let gpr = cols / 32;
        let mut payload = Vec::new();
        for t in 0..rows * gpr {
            let sc = 0.005 + (t % 9) as f32 * 0.004;
            payload.extend_from_slice(&cortiq_core::quant::f32_to_f16(sc).to_le_bytes());
            for j in 0..4 {
                payload.push(((t * 41 + j * 71 + 13) % 253) as u8);
            }
        }
        let xs: Vec<f32> = (0..cols)
            .map(|i| ((i * 7 + 3) % 29) as f32 / 29.0 - 0.5)
            .collect();
        let mut w = vec![0f32; rows * cols];
        cortiq_core::quant::dequant_q1(&payload, &mut w);
        let mut want = vec![0f32; rows];
        for o in 0..rows {
            want[o] = (0..cols).map(|i| w[o * cols + i] * xs[i]).sum();
        }
        let mut got = vec![0f32; rows];
        assert!(dispatch_q1(c, None, &payload, &xs, rows, cols, &mut got));
        let max_d = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(max_d < 1e-3, "wgpu q1_matvec ≠ CPU: max|Δ| = {max_d}");
    }

    #[test]
    fn wgpu_rmsnorm_matches_cpu() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        if ctx().is_none() {
            eprintln!("no wgpu adapter — skipping rmsnorm parity test");
            return;
        }
        let n = 896usize;
        let eps = 1e-6f32;
        let x: Vec<f32> = (0..n)
            .map(|i| ((i * 13 + 7) % 101) as f32 / 101.0 - 0.5)
            .collect();
        let w: Vec<f32> = (0..n)
            .map(|i| 0.5 + ((i * 5 + 1) % 17) as f32 / 17.0)
            .collect();
        let ss: f32 = x.iter().map(|v| v * v).sum();
        let inv = 1.0 / (ss / n as f32 + eps).sqrt();
        // plain RMSNorm
        let want: Vec<f32> = (0..n).map(|i| x[i] * inv * w[i]).collect();
        let mut got = vec![0f32; n];
        assert!(rmsnorm_row(&x, &w, &mut got, false, eps));
        let md = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(md < 1e-4, "wgpu rmsnorm ≠ CPU: max|Δ| = {md}");
        // gemma variant: w' = 1 + w
        let wantg: Vec<f32> = (0..n).map(|i| x[i] * inv * (1.0 + w[i])).collect();
        let mut gotg = vec![0f32; n];
        assert!(rmsnorm_row(&x, &w, &mut gotg, true, eps));
        let mdg = wantg
            .iter()
            .zip(&gotg)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(mdg < 1e-4, "wgpu rmsnorm(gemma) ≠ CPU: max|Δ| = {mdg}");
    }

    #[test]
    fn wgpu_add_rmsnorm_matches_cpu() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping");
            return;
        };
        let n = 896usize;
        let eps = 1e-6f32;
        let h: Vec<f32> = (0..n)
            .map(|i| ((i * 13 + 7) % 101) as f32 / 101.0 - 0.5)
            .collect();
        let d: Vec<f32> = (0..n)
            .map(|i| ((i * 7 + 3) % 61) as f32 / 61.0 - 0.5)
            .collect();
        let w: Vec<f32> = (0..n)
            .map(|i| 0.5 + ((i * 5 + 1) % 17) as f32 / 17.0)
            .collect();
        // CPU reference: h += d, then rmsnorm(h, w)
        let hd: Vec<f32> = (0..n).map(|i| h[i] + d[i]).collect();
        let ss: f32 = hd.iter().map(|x| x * x).sum();
        let inv = 1.0 / (ss / n as f32 + eps).sqrt();
        let want: Vec<f32> = (0..n).map(|i| hd[i] * inv * w[i]).collect();
        // GPU add_rmsnorm
        let hb = c
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: None,
                contents: bytemuck::cast_slice(&h),
                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            });
        let db = storage_bytes(c, bytemuck::cast_slice(&d));
        let wb = storage_bytes(c, bytemuck::cast_slice(&w));
        let ob = rw_f32(c, n, true);
        let pb = uniform_u32x4(c, [n as u32, 0, eps.to_bits(), 0]);
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.layout_add_rmsnorm,
            entries: &[
                bind_buf(0, &hb),
                bind_buf(1, &db),
                bind_buf(2, &wb),
                bind_buf(3, &ob),
                bind_buf(4, &pb),
            ],
        });
        let mut enc = c
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
        {
            let mut p = begin_pass(&mut enc);
            p.set_pipeline(&c.add_rmsnorm);
            p.set_bind_group(0, &bind, &[]);
            p.dispatch_workgroups(1, 1, 1);
        }
        let mut got = vec![0f32; n];
        let sz = (n * 4) as u64;
        let mut sc = c.scratch.lock().unwrap();
        let stage = Scratch::ensure(
            &c.device,
            &mut sc.stage,
            sz,
            wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            "arn-stage",
        );
        assert!(readback(c, enc, &ob, &stage, sz, &mut got));
        drop(sc);
        let md = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(md < 1e-4, "wgpu add_rmsnorm ≠ CPU: max|Δ| = {md}");
    }

    #[test]
    fn wgpu_attn_rope_qkn_matches_cpu() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        if ctx().is_none() {
            eprintln!("no wgpu adapter — skipping attn_rope parity test");
            return;
        }
        // head_dim 256 with partial RoPE (rd=64) — the Qwen3.5 geometry: nt=8
        // (>4-slot xv) and hlf=32 exercise the paths that broke the graph.
        let (nh, nkv, hd, rd, pos) = (4usize, 2usize, 256usize, 64usize, 5usize);
        let eps = 1e-6f32;
        let flags = 1u32 | 2u32 | 4u32; // gate + qnorm + knorm, non-gemma
        let jitter = |a: usize, b: usize| ((a * 31 + b * 17 + 7) % 97) as f32 / 97.0 - 0.5;
        // qraw: nh heads × 2·hd (q part || gate part); k: nkv × hd
        let qraw: Vec<f32> = (0..nh * 2 * hd).map(|i| jitter(i, 1)).collect();
        let k_in: Vec<f32> = (0..nkv * hd).map(|i| jitter(i, 2)).collect();
        let qnw: Vec<f32> = (0..hd).map(|d| 0.7 + jitter(d, 3)).collect();
        let knw: Vec<f32> = (0..hd).map(|d| 0.7 + jitter(d, 4)).collect();
        let invf: Vec<f32> = (0..rd / 2)
            .map(|i| 1.0 / (10000f32).powf(2.0 * i as f32 / rd as f32))
            .collect();
        // CPU reference: qk-norm then half-split partial RoPE.
        let norm_rope = |v: &mut [f32], w: &[f32]| {
            let ss: f32 = v.iter().map(|x| x * x).sum();
            let inv = 1.0 / (ss / hd as f32 + eps).sqrt();
            for d in 0..hd {
                v[d] = v[d] * inv * w[d];
            }
            let hlf = rd / 2;
            for i in 0..hlf {
                let ang = pos as f32 * invf[i];
                let (c, s) = (ang.cos(), ang.sin());
                let (x0, x1) = (v[i], v[i + hlf]);
                v[i] = x0 * c - x1 * s;
                v[i + hlf] = x0 * s + x1 * c;
            }
        };
        let mut want_q = vec![0f32; nh * hd];
        let mut want_g = vec![0f32; nh * hd];
        for h in 0..nh {
            let mut q: Vec<f32> = qraw[h * 2 * hd..h * 2 * hd + hd].to_vec();
            norm_rope(&mut q, &qnw);
            want_q[h * hd..(h + 1) * hd].copy_from_slice(&q);
            want_g[h * hd..(h + 1) * hd]
                .copy_from_slice(&qraw[h * 2 * hd + hd..h * 2 * hd + 2 * hd]);
        }
        let mut want_k = k_in.clone();
        for kh in 0..nkv {
            let mut kk = want_k[kh * hd..(kh + 1) * hd].to_vec();
            norm_rope(&mut kk, &knw);
            want_k[kh * hd..(kh + 1) * hd].copy_from_slice(&kk);
        }
        let mut got_q = vec![0f32; nh * hd];
        let mut got_k = vec![0f32; nkv * hd];
        let mut got_g = vec![0f32; nh * hd];
        assert!(attn_rope_qkn_gpu(
            &qraw, &k_in, &qnw, &knw, &invf, nh, nkv, hd, rd, pos, flags, eps, &mut got_q,
            &mut got_k, &mut got_g,
        ));
        let md = |a: &[f32], b: &[f32]| {
            a.iter()
                .zip(b)
                .map(|(x, y)| (x - y).abs())
                .fold(0.0f32, f32::max)
        };
        assert!(
            md(&want_q, &got_q) < 1e-4,
            "q mismatch: {}",
            md(&want_q, &got_q)
        );
        assert!(
            md(&want_k, &got_k) < 1e-4,
            "k mismatch: {}",
            md(&want_k, &got_k)
        );
        assert!(
            md(&want_g, &got_g) < 1e-4,
            "gate mismatch: {}",
            md(&want_g, &got_g)
        );
    }

    #[test]
    fn wgpu_gqa_attend_matches_cpu() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping gqa_attend parity test");
            return;
        };
        // hd=128 exercises the stride-129 kernel (exists everywhere);
        // hd=256 exercises stride-257 where the device's workgroup
        // storage allows it (32 KB devices — Adreno/Mali/wgpu-Metal —
        // honestly refuse: hd_cap gates them to the small kernel).
        attend_case(128);
        if c.hd_cap >= 256 {
            attend_case(256);
        } else {
            eprintln!("hd_cap {} — skipping hd=256 attend case", c.hd_cap);
        }
    }

    fn attend_case(hd: usize) {
        let (nh, hpk, cap, n) = (4usize, 2usize, 16usize, 5usize);
        let nkv = nh / hpk;
        let jit = |a: usize, b: usize| ((a * 29 + b * 13 + 5) % 89) as f32 / 89.0 - 0.5;
        let q: Vec<f32> = (0..nh * hd).map(|i| jit(i, 1)).collect();
        // caches laid out [nkv, cap, hd]; only first n rows are valid.
        let mut kc = vec![0f32; nkv * cap * hd];
        let mut vc = vec![0f32; nkv * cap * hd];
        for kh in 0..nkv {
            for p in 0..n {
                for d in 0..hd {
                    kc[(kh * cap + p) * hd + d] = jit(kh * 1000 + p * 10 + d, 2);
                    vc[(kh * cap + p) * hd + d] = jit(kh * 1000 + p * 10 + d, 3);
                }
            }
        }
        // CPU reference: scaled softmax attention per head.
        let scale = 1.0 / (hd as f32).sqrt();
        let mut want = vec![0f32; nh * hd];
        for h in 0..nh {
            let kh = h / hpk;
            let mut sc: Vec<f32> = (0..n)
                .map(|p| {
                    (0..hd)
                        .map(|d| q[h * hd + d] * kc[(kh * cap + p) * hd + d])
                        .sum::<f32>()
                        * scale
                })
                .collect();
            let mx = sc.iter().cloned().fold(f32::MIN, f32::max);
            let mut den = 0.0;
            for s in sc.iter_mut() {
                *s = (*s - mx).exp();
                den += *s;
            }
            for d in 0..hd {
                want[h * hd + d] = (0..n)
                    .map(|p| sc[p] * vc[(kh * cap + p) * hd + d])
                    .sum::<f32>()
                    / den;
            }
        }
        let mut got = vec![0f32; nh * hd];
        assert!(gqa_attend_gpu(&q, &kc, &vc, nh, hpk, hd, cap, n, &mut got));
        let md = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(md < 1e-4, "wgpu gqa_attend hd={hd} ≠ CPU: max|Δ| = {md}");
    }

    #[test]
    fn wgpu_o1_step_matches_cpu() {
        // End-to-end: the REAL NystromState is the reference — prefill a
        // group, clone it, advance the clone one token on the CPU, and
        // require the device mirror (upload + o1_far/o1_push/o1_attend)
        // to produce the same attention output from the same state.
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping o1 test");
            return;
        };
        // Production geometry (Qwen3.6): the small-dim version passed while
        // the real model garbled, so the test runs BOTH.
        for (d, dv, m, w, sink, hpg, t) in [
            (8usize, 8usize, 4usize, 8usize, 2usize, 2usize, 40usize),
            (256, 256, 32, 128, 4, 8, 430),
        ] {
            let jit = |a: usize, b: usize| ((a * 37 + b * 13 + 3) % 83) as f32 / 83.0 - 0.5;
            let ks: Vec<f32> = (0..t * d).map(|i| jit(i, 1)).collect();
            let vs: Vec<f32> = (0..t * dv).map(|i| jit(i, 2)).collect();
            let qs_own: Vec<Vec<f32>> = (0..hpg)
                .map(|h| (0..t * d).map(|i| jit(i, 3 + h)).collect())
                .collect();
            let qs_refs: Vec<&[f32]> = qs_own.iter().map(|v| v.as_slice()).collect();
            let mut st = crate::nystrom::NystromState::new_group(m, w, sink, hpg);
            st.prefill_group(&qs_refs, &ks, &vs, t, d, dv);
            // CPU ground truth for the next token (built below per group).
            // TWO groups — the production model has nkv=2, and the group
            // concatenation in the upload plus every g-offset in the kernels
            // is exactly what a single-group test cannot catch.
            let mut st2 = crate::nystrom::NystromState::new_group(m, w, sink, hpg);
            let qs2_own: Vec<Vec<f32>> = (0..hpg)
                .map(|h| (0..t * d).map(|i| jit(i, 23 + h)).collect())
                .collect();
            let qs2_refs: Vec<&[f32]> = qs2_own.iter().map(|v| v.as_slice()).collect();
            let ks2: Vec<f32> = (0..t * d).map(|i| jit(i, 21)).collect();
            let vs2: Vec<f32> = (0..t * dv).map(|i| jit(i, 22)).collect();
            st2.prefill_group(&qs2_refs, &ks2, &vs2, t, d, dv);
            let gcnt = 2usize;
            let q_new: Vec<f32> = (0..gcnt * hpg * d).map(|i| jit(i, 5)).collect();
            let k_new: Vec<f32> = (0..gcnt * d).map(|i| jit(i, 6)).collect();
            let v_new: Vec<f32> = (0..gcnt * dv).map(|i| jit(i, 7)).collect();
            let mut want = vec![0f32; gcnt * hpg * dv];
            let mut cpu1 = st.clone();
            let mut cpu2 = st2.clone();
            cpu1.step_group(
                &q_new[..hpg * d],
                &k_new[..d],
                &v_new[..dv],
                &mut want[..hpg * dv],
            );
            cpu2.step_group(
                &q_new[hpg * d..],
                &k_new[d..],
                &v_new[dv..],
                &mut want[hpg * dv..],
            );
            // Device: upload the PRE-step states, run the three kernels.
            let views = vec![st.device_view(), st2.device_view()];
            assert!(!views[0].exact_only, "t must exceed w+8 for this test");
            let mv = views[0].m_eff;
            o1_ensure(c, u64::MAX, usize::MAX, &views, 1).expect("o1 upload");
            let dev_bufs = {
                let map = c.o1m.lock().unwrap();
                let dref = map.get(&(u64::MAX, usize::MAX)).unwrap();
                (
                    dref.meta.clone(),
                    dref.ring_k.clone(),
                    dref.ring_v.clone(),
                    dref.sink_k.clone(),
                    dref.sink_v.clone(),
                    dref.k_tilde.clone(),
                    dref.qt.clone(),
                    dref.mu.clone(),
                    dref.mz.clone(),
                    dref.that.clone(),
                    dref.scale,
                )
            };
            let (dmeta, drk, drv, dsk, dsv, dkt, dqt, dmu, dmz, dth, sc) = dev_bufs;
            let rect_fm = views[0].heads[0].rect_fm;
            let stor = |data: &[f32]| {
                use wgpu::util::DeviceExt;
                c.device
                    .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                        label: None,
                        contents: bytemuck::cast_slice(data),
                        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
                    })
            };
            let qb = stor(&q_new);
            let kb = stor(&k_new);
            let vb = stor(&v_new);
            let ob = c.device.create_buffer(&wgpu::BufferDescriptor {
                label: None,
                size: (gcnt * hpg * dv * 4) as u64,
                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
                mapped_at_creation: false,
            });
            let o1_u = uniform_u32x8(
                c,
                [
                    hpg as u32,
                    mv as u32,
                    w as u32,
                    (sink as u32) | (u32::from(rect_fm) << 8),
                    d as u32,
                    dv as u32,
                    sc.to_bits(),
                    0,
                ],
            );
            let bgf = |layout: &wgpu::BindGroupLayout, bufs: &[&wgpu::Buffer]| {
                let entries: Vec<_> = bufs
                    .iter()
                    .enumerate()
                    .map(|(i, b)| bind_buf(i as u32, b))
                    .collect();
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout,
                    entries: &entries,
                })
            };
            let bg_far = bgf(
                &c.layout_o1_far,
                &[&dmeta, &drk, &drv, &dqt, &dmz, &dth, &o1_u],
            );
            let bg_push = bgf(&c.layout_o1_push, &[&dmeta, &kb, &vb, &drk, &drv, &o1_u]);
            let bg_att = bgf(
                &c.layout_o1_attend,
                &[
                    &dmeta, &qb, &drk, &drv, &dsk, &dsv, &dkt, &dmu, &dmz, &dth, &ob, &o1_u,
                ],
            );
            let mut enc = c
                .device
                .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
            {
                let mut pass = begin_pass(&mut enc);
                pass.set_pipeline(&c.o1_far);
                pass.set_bind_group(0, &bg_far, &[]);
                pass.dispatch_workgroups((gcnt * hpg * mv) as u32, 1, 1);
                pass.set_pipeline(&c.o1_push);
                pass.set_bind_group(0, &bg_push, &[]);
                pass.dispatch_workgroups(gcnt as u32, 1, 1);
                pass.set_pipeline(&c.o1_attend);
                pass.set_bind_group(0, &bg_att, &[]);
                pass.dispatch_workgroups((gcnt * hpg) as u32, 1, 1);
            }
            let stage = c.device.create_buffer(&wgpu::BufferDescriptor {
                label: None,
                size: (gcnt * hpg * dv * 4) as u64,
                usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });
            enc.copy_buffer_to_buffer(&ob, 0, &stage, 0, (gcnt * hpg * dv * 4) as u64);
            submit(c, enc.finish());
            let (tx, rx) = std::sync::mpsc::channel();
            stage.map_async(wgpu::MapMode::Read, .., move |r| tx.send(r).unwrap());
            let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
            rx.recv().unwrap().unwrap();
            let got: Vec<f32> = bytemuck::cast_slice(&stage.get_mapped_range(..).unwrap()).to_vec();
            c.o1m.lock().unwrap().remove(&(u64::MAX, usize::MAX));
            let md = want
                .iter()
                .zip(&got)
                .map(|(a, b)| (a - b).abs())
                .fold(0.0f32, f32::max);
            assert!(
                md < 1e-3,
                "wgpu o1 step ≠ CPU (d={d} m={m} w={w} hpg={hpg}): max|Δ| = {md}"
            );
        }
    }

    #[test]
    fn wgpu_gdn_step_matches_cpu() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        if ctx().is_none() {
            eprintln!("no wgpu adapter — skipping gdn_step test");
            return;
        }
        let (nv, nk, dk, dv) = (4usize, 2usize, 8usize, 8usize);
        let kd = nk * dk;
        let rep = nv / nk;
        let cdim = 2 * kd + nv * dv;
        let eps = 1e-6f32;
        let jit = |a: usize, b: usize| ((a * 23 + b * 11 + 5) % 71) as f32 / 71.0 - 0.5;
        let cq: Vec<f32> = (0..cdim).map(|i| jit(i, 1)).collect();
        let z: Vec<f32> = (0..nv * dv).map(|i| jit(i, 2)).collect();
        let a: Vec<f32> = (0..nv).map(|i| jit(i, 3)).collect();
        let b: Vec<f32> = (0..nv).map(|i| jit(i, 4)).collect();
        let alog: Vec<f32> = (0..nv).map(|i| jit(i, 5) - 0.5).collect();
        let dtb: Vec<f32> = (0..nv).map(|i| jit(i, 6)).collect();
        let norm: Vec<f32> = (0..dv).map(|i| 0.8 + jit(i, 7)).collect();
        let s0: Vec<f32> = (0..nv * dk * dv).map(|i| jit(i, 8) * 0.3).collect();
        // CPU reference (mirrors linear_core::gdn_step).
        let sp = |x: f32| if x > 20.0 { x } else { (1.0 + x.exp()).ln() };
        let sig = |x: f32| 1.0 / (1.0 + (-x).exp());
        let silu = |x: f32| x / (1.0 + (-x).exp());
        let mut sc = s0.clone();
        let mut want = vec![0f32; nv * dv];
        for h in 0..nv {
            let ko = h / rep;
            let (qs, ks) = (ko * dk, kd + ko * dk);
            let nq: f32 = (0..dk).map(|d| cq[qs + d] * cq[qs + d]).sum();
            let nkn: f32 = (0..dk).map(|d| cq[ks + d] * cq[ks + d]).sum();
            let invq = 1.0 / ((nq + 1e-6).sqrt() * (dk as f32).sqrt());
            let invk = 1.0 / (nkn + 1e-6).sqrt();
            let qf: Vec<f32> = (0..dk).map(|d| cq[qs + d] * invq).collect();
            let kf: Vec<f32> = (0..dk).map(|d| cq[ks + d] * invk).collect();
            let g = (-(alog[h].exp()) * sp(a[h] + dtb[h])).exp();
            let beta = sig(b[h]);
            let sbase = h * dk * dv;
            let mut o = vec![0f32; dv];
            for dj in 0..dv {
                let vt = cq[2 * kd + h * dv + dj];
                let mut kv = 0.0;
                for di in 0..dk {
                    kv += sc[sbase + di * dv + dj] * kf[di];
                }
                let delta = (vt - g * kv) * beta;
                for di in 0..dk {
                    let idx = sbase + di * dv + dj;
                    let cell = g * sc[idx] + kf[di] * delta;
                    sc[idx] = cell;
                    o[dj] += qf[di] * cell;
                }
            }
            let ss: f32 = o.iter().map(|v| v * v).sum();
            let inv = 1.0 / (ss / dv as f32 + eps).sqrt();
            for dj in 0..dv {
                want[h * dv + dj] = o[dj] * inv * norm[dj] * silu(z[h * dv + dj]);
            }
        }
        // GPU
        let mut sg = s0.clone();
        let mut got = vec![0f32; nv * dv];
        assert!(gdn_step_gpu(
            &cq, &z, &a, &b, &alog, &dtb, &norm, &mut sg, nv, dk, dv, kd, rep, cdim, eps, &mut got
        ));
        let mo = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        let msd = sc
            .iter()
            .zip(&sg)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(mo < 2e-3, "wgpu gdn_step o ≠ CPU: max|Δ| = {mo}");
        assert!(msd < 2e-3, "wgpu gdn_step S ≠ CPU: max|Δ| = {msd}");
    }

    #[test]
    fn wgpu_gdn_conv_matches_cpu() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        if ctx().is_none() {
            eprintln!("no wgpu adapter — skipping gdn_conv test");
            return;
        }
        let (cdim, kk) = (48usize, 4usize);
        let jit = |a: usize, b: usize| ((a * 19 + b * 7 + 3) % 61) as f32 / 61.0 - 0.5;
        let qkv: Vec<f32> = (0..cdim).map(|i| jit(i, 1)).collect();
        let taps: Vec<f32> = (0..cdim * kk).map(|i| jit(i, 2)).collect();
        let ring0: Vec<f32> = (0..(kk - 1) * cdim).map(|i| jit(i, 3)).collect();
        let silu = |x: f32| x / (1.0 + (-x).exp());
        // CPU reference
        let mut rc = ring0.clone();
        let mut want_cq = vec![0f32; cdim];
        for c in 0..cdim {
            let t = &taps[c * kk..(c + 1) * kk];
            let mut acc = qkv[c] * t[kk - 1];
            for j in 0..kk - 1 {
                acc += rc[j * cdim + c] * t[j];
            }
            want_cq[c] = silu(acc);
        }
        rc.copy_within(cdim.., 0);
        let tail = (kk - 2) * cdim;
        rc[tail..tail + cdim].copy_from_slice(&qkv[..cdim]);
        // GPU
        let mut rg = ring0.clone();
        let mut got_cq = vec![0f32; cdim];
        assert!(gdn_conv_gpu(&qkv, &taps, &mut rg, cdim, kk, &mut got_cq));
        let mc = want_cq
            .iter()
            .zip(&got_cq)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        let mr = rc
            .iter()
            .zip(&rg)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(mc < 1e-5, "wgpu gdn_conv cq ≠ CPU: {mc}");
        assert!(mr < 1e-6, "wgpu gdn_conv ring ≠ CPU: {mr}");
    }

    // Build a deterministic q1 payload for a [rows, cols] weight + its dequant.
    #[cfg(test)]
    fn mk_q1(rows: usize, cols: usize, seed: usize) -> (Vec<u8>, Vec<f32>) {
        let gpr = cols / 32;
        let mut payload = Vec::new();
        for t in 0..rows * gpr {
            let sc = 0.004 + ((t + seed) % 9) as f32 * 0.003;
            payload.extend_from_slice(&cortiq_core::quant::f32_to_f16(sc).to_le_bytes());
            for j in 0..4 {
                payload.push(((t * 37 + j * 53 + seed * 7 + 11) % 251) as u8);
            }
        }
        let mut w = vec![0f32; rows * cols];
        cortiq_core::quant::dequant_q1(&payload, &mut w);
        (payload, w)
    }

    #[test]
    fn wgpu_attn_block_matches_cpu() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping attn_block test");
            return;
        };
        let (nh, nkv, hd, rd, hidden, cap, stored) =
            (4usize, 2usize, 64usize, 64usize, 128usize, 8usize, 2usize);
        let hpk = nh / nkv;
        let eps = 1e-6f32;
        let flags = 2u32 | 4u32; // qnorm + knorm, no gate
        let jit = |a: usize, b: usize| ((a * 31 + b * 17 + 3) % 83) as f32 / 83.0 - 0.5;
        let h_in: Vec<f32> = (0..hidden).map(|i| jit(i, 1)).collect();
        let norm_w: Vec<f32> = (0..hidden).map(|i| 0.8 + jit(i, 2)).collect();
        let (wq_p, wq) = mk_q1(nh * hd, hidden, 1);
        let (wk_p, wk) = mk_q1(nkv * hd, hidden, 2);
        let (wv_p, wv) = mk_q1(nkv * hd, hidden, 3);
        let (wo_p, wo) = mk_q1(hidden, nh * hd, 4);
        let qnw: Vec<f32> = (0..hd).map(|d| 0.7 + jit(d, 5)).collect();
        let knw: Vec<f32> = (0..hd).map(|d| 0.7 + jit(d, 6)).collect();
        let invf: Vec<f32> = (0..rd / 2)
            .map(|i| 1.0 / (10000f32).powf(2.0 * i as f32 / rd as f32))
            .collect();
        // Pre-filled device K/V caches [nkv, cap, hd] (first `stored` rows valid).
        let mut kc = vec![0f32; nkv * cap * hd];
        let mut vc = vec![0f32; nkv * cap * hd];
        for kh in 0..nkv {
            for p in 0..stored {
                for d in 0..hd {
                    kc[(kh * cap + p) * hd + d] = jit(kh * 900 + p * 30 + d, 7);
                    vc[(kh * cap + p) * hd + d] = jit(kh * 900 + p * 30 + d, 8);
                }
            }
        }
        let mkcache = |data: &[f32]| {
            c.device
                .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                    label: Some("cache"),
                    contents: bytemuck::cast_slice(data),
                    usage: wgpu::BufferUsages::STORAGE
                        | wgpu::BufferUsages::COPY_SRC
                        | wgpu::BufferUsages::COPY_DST,
                })
        };
        let kbuf = mkcache(&kc);
        let vbuf = mkcache(&vc);
        // ---- CPU reference ----
        let ss: f32 = h_in.iter().map(|x| x * x).sum();
        let rinv = 1.0 / (ss / hidden as f32 + eps).sqrt();
        let normed: Vec<f32> = (0..hidden).map(|i| h_in[i] * rinv * norm_w[i]).collect();
        let matvec = |w: &[f32], rows: usize, cols: usize, x: &[f32]| -> Vec<f32> {
            (0..rows)
                .map(|o| (0..cols).map(|i| w[o * cols + i] * x[i]).sum())
                .collect()
        };
        let qraw = matvec(&wq, nh * hd, hidden, &normed);
        let kv_k = matvec(&wk, nkv * hd, hidden, &normed);
        let kv_v = matvec(&wv, nkv * hd, hidden, &normed);
        let norm_rope = |v: &mut [f32], w: &[f32]| {
            let s: f32 = v.iter().map(|x| x * x).sum();
            let inv = 1.0 / (s / hd as f32 + eps).sqrt();
            for d in 0..hd {
                v[d] = v[d] * inv * w[d];
            }
            for i in 0..rd / 2 {
                let ang = stored as f32 * invf[i];
                let (co, si) = (ang.cos(), ang.sin());
                let (x0, x1) = (v[i], v[i + rd / 2]);
                v[i] = x0 * co - x1 * si;
                v[i + rd / 2] = x0 * si + x1 * co;
            }
        };
        let mut qout = vec![0f32; nh * hd];
        for h in 0..nh {
            let mut q = qraw[h * hd..(h + 1) * hd].to_vec();
            norm_rope(&mut q, &qnw);
            qout[h * hd..(h + 1) * hd].copy_from_slice(&q);
        }
        for kh in 0..nkv {
            let mut kk = kv_k[kh * hd..(kh + 1) * hd].to_vec();
            norm_rope(&mut kk, &knw);
            kc[(kh * cap + stored) * hd..(kh * cap + stored) * hd + hd].copy_from_slice(&kk);
            vc[(kh * cap + stored) * hd..(kh * cap + stored) * hd + hd]
                .copy_from_slice(&kv_v[kh * hd..(kh + 1) * hd]);
        }
        let n = stored + 1;
        let scale = 1.0 / (hd as f32).sqrt();
        let mut attn = vec![0f32; nh * hd];
        for h in 0..nh {
            let kh = h / hpk;
            let mut sc: Vec<f32> = (0..n)
                .map(|p| {
                    (0..hd)
                        .map(|d| qout[h * hd + d] * kc[(kh * cap + p) * hd + d])
                        .sum::<f32>()
                        * scale
                })
                .collect();
            let mx = sc.iter().cloned().fold(f32::MIN, f32::max);
            let mut den = 0.0;
            for s in sc.iter_mut() {
                *s = (*s - mx).exp();
                den += *s;
            }
            for d in 0..hd {
                attn[h * hd + d] = (0..n)
                    .map(|p| sc[p] * vc[(kh * cap + p) * hd + d])
                    .sum::<f32>()
                    / den;
            }
        }
        let o = matvec(&wo, hidden, nh * hd, &attn);
        let want: Vec<f32> = (0..hidden).map(|i| h_in[i] + o[i]).collect();
        // ---- GPU block ----
        let mut got = vec![0f32; hidden];
        assert!(attn_block_gpu(
            &h_in, &norm_w, &wq_p, &wk_p, &wv_p, &wo_p, &qnw, &knw, &invf, &kbuf, &vbuf, nh, nkv,
            hd, rd, hidden, cap, stored, flags, eps, &mut got,
        ));
        let md = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(md < 2e-3, "wgpu attn_block ≠ CPU: max|Δ| = {md}");
    }

    // Payoff microbench: the resident attention block (ONE submit) vs the same
    // steps as separate submit+readback ops (today's per-op decode). Run with
    //   cargo test -p cortiq-engine --release --features gpu attn_block_timing -- --ignored --nocapture
    #[test]
    #[ignore]
    fn wgpu_attn_block_timing() {
        use std::time::Instant;
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping");
            return;
        };
        // 1.7B-ish attention geometry.
        let (nh, nkv, hd, rd, hidden, cap, stored) = (
            16usize, 8usize, 128usize, 128usize, 2048usize, 256usize, 128usize,
        );
        let hpk = nh / nkv;
        let eps = 1e-6f32;
        let flags = 2u32 | 4u32;
        let h_in = vec![0.01f32; hidden];
        let norm_w = vec![1.0f32; hidden];
        let (wq_p, _) = mk_q1(nh * hd, hidden, 1);
        let (wk_p, _) = mk_q1(nkv * hd, hidden, 2);
        let (wv_p, _) = mk_q1(nkv * hd, hidden, 3);
        let (wo_p, _) = mk_q1(hidden, nh * hd, 4);
        let qnw = vec![1.0f32; hd];
        let knw = vec![1.0f32; hd];
        let invf: Vec<f32> = (0..rd / 2)
            .map(|i| 1.0 / (10000f32).powf(2.0 * i as f32 / rd as f32))
            .collect();
        let kc = vec![0.01f32; nkv * cap * hd];
        let vc = vec![0.01f32; nkv * cap * hd];
        let mkc = |d: &[f32]| {
            c.device
                .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                    label: None,
                    contents: bytemuck::cast_slice(d),
                    usage: wgpu::BufferUsages::STORAGE
                        | wgpu::BufferUsages::COPY_SRC
                        | wgpu::BufferUsages::COPY_DST,
                })
        };
        let (kbuf, vbuf) = (mkc(&kc), mkc(&vc));
        let iters = 200;
        let mut hout = vec![0f32; hidden];
        // FUSED: the resident block, one submit + one readback per call.
        for _ in 0..20 {
            attn_block_gpu(
                &h_in, &norm_w, &wq_p, &wk_p, &wv_p, &wo_p, &qnw, &knw, &invf, &kbuf, &vbuf, nh,
                nkv, hd, rd, hidden, cap, stored, flags, eps, &mut hout,
            );
        }
        let t0 = Instant::now();
        for _ in 0..iters {
            attn_block_gpu(
                &h_in, &norm_w, &wq_p, &wk_p, &wv_p, &wo_p, &qnw, &knw, &invf, &kbuf, &vbuf, nh,
                nkv, hd, rd, hidden, cap, stored, flags, eps, &mut hout,
            );
        }
        let fused = t0.elapsed().as_secs_f64() * 1000.0 / iters as f64;
        // UNFUSED: each step its own submit+readback (rmsnorm, QKV×3, rope, attend, O).
        let mut normed = vec![0f32; hidden];
        let mut qraw = vec![0f32; nh * hd];
        let mut kk = vec![0f32; nkv * hd];
        let mut vv = vec![0f32; nkv * hd];
        let mut qout = vec![0f32; nh * hd];
        let mut kout = vec![0f32; nkv * hd];
        let mut gout = vec![0f32; nh * hd];
        let mut attn = vec![0f32; nh * hd];
        let mut oout = vec![0f32; hidden];
        let unfused_once = |normed: &mut [f32],
                            qraw: &mut [f32],
                            kk: &mut [f32],
                            vv: &mut [f32],
                            qout: &mut [f32],
                            kout: &mut [f32],
                            gout: &mut [f32],
                            attn: &mut [f32],
                            oout: &mut [f32]| {
            rmsnorm_row(&h_in, &norm_w, normed, false, eps);
            dispatch_q1(c, None, &wq_p, normed, nh * hd, hidden, qraw);
            dispatch_q1(c, None, &wk_p, normed, nkv * hd, hidden, kk);
            dispatch_q1(c, None, &wv_p, normed, nkv * hd, hidden, vv);
            attn_rope_qkn_gpu(
                qraw, kk, &qnw, &knw, &invf, nh, nkv, hd, rd, stored, flags, eps, qout, kout, gout,
            );
            gqa_attend_gpu(qout, &kc, &vc, nh, hpk, hd, cap, stored + 1, attn);
            dispatch_q1(c, None, &wo_p, attn, hidden, nh * hd, oout);
        };
        for _ in 0..20 {
            unfused_once(
                &mut normed,
                &mut qraw,
                &mut kk,
                &mut vv,
                &mut qout,
                &mut kout,
                &mut gout,
                &mut attn,
                &mut oout,
            );
        }
        let t1 = Instant::now();
        for _ in 0..iters {
            unfused_once(
                &mut normed,
                &mut qraw,
                &mut kk,
                &mut vv,
                &mut qout,
                &mut kout,
                &mut gout,
                &mut attn,
                &mut oout,
            );
        }
        let unfused = t1.elapsed().as_secs_f64() * 1000.0 / iters as f64;
        eprintln!(
            "ATTN BLOCK 1.7B-dims: fused(1 submit) {fused:.3} ms/layer | unfused(per-op) {unfused:.3} ms/layer | speedup {:.2}×",
            unfused / fused
        );
    }

    #[test]
    fn wgpu_q1t_matvec_matches_cpu_reference() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping q1t parity test");
            return;
        };
        use cortiq_core::quant::{GROUP_SIZE, f32_to_f16, q1t_pack};
        let (rows, cols) = (33usize, 256usize);
        let gpr = cols / GROUP_SIZE;
        let outliers: [(usize, f32); 3] = [(5, 3.0), (300, -2.0), (600, 1.5)]; // sorted
        let is_out = |flat: usize| outliers.iter().any(|&(i, _)| i == flat);
        let mut payload = Vec::new();
        for r in 0..rows {
            for g in 0..gpr {
                let s = 0.02 + ((r + g) % 7) as f32 * 0.01;
                payload.extend_from_slice(&f32_to_f16(s).to_le_bytes());
                let mut cc = [0u8; 7];
                for k in 0..GROUP_SIZE {
                    let code = if is_out(r * cols + g * GROUP_SIZE + k) {
                        0
                    } else {
                        ((k * 7 + r + g) % 3) as u8
                    };
                    q1t_pack(&mut cc, k, code);
                }
                payload.extend_from_slice(&cc);
            }
        }
        let mut row_ptr = vec![0u32; rows + 1];
        for &(idx, _) in &outliers {
            row_ptr[idx / cols + 1] += 1;
        }
        for r in 0..rows {
            row_ptr[r + 1] += row_ptr[r];
        }
        for &p in &row_ptr {
            payload.extend_from_slice(&p.to_le_bytes());
        }
        for &(idx, v) in &outliers {
            payload.extend_from_slice(&((idx % cols) as u16).to_le_bytes());
            payload.extend_from_slice(&f32_to_f16(v).to_le_bytes());
        }
        let xs: Vec<f32> = (0..cols)
            .map(|i| ((i * 7 + 3) % 29) as f32 / 29.0 - 0.5)
            .collect();
        let mut w = vec![0f32; rows * cols];
        cortiq_core::quant::dequant_q1t(&payload, rows, cols, &mut w);
        let mut want = vec![0f32; rows];
        for o in 0..rows {
            want[o] = (0..cols).map(|i| w[o * cols + i] * xs[i]).sum();
        }
        let mut got = vec![0f32; rows];
        assert!(dispatch_q1t(
            c, &c.q1t, None, &payload, &xs, rows, cols, &mut got
        ));
        let max_d = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0f32, f32::max);
        assert!(max_d < 1e-2, "wgpu q1t_matvec ≠ CPU: max|Δ| = {max_d}");
    }

    #[test]
    fn wgpu_q4b_matvec_matches_cpu_reference() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping q4b parity test");
            return;
        };
        use cortiq_core::quant::{GROUP_SIZE, f32_to_f16};
        let (rows, cols) = (33usize, 256usize);
        let n_groups = rows * (cols / GROUP_SIZE);
        let mut payload = vec![0u8; n_groups * 16]; // packed nibbles
        for g in 0..n_groups {
            for k in 0..16 {
                let lo = ((g * 3 + k) % 16) as u8;
                let hi = ((g * 5 + k * 2) % 16) as u8;
                payload[g * 16 + k] = lo | (hi << 4);
            }
        }
        for g in 0..n_groups {
            let s = 0.02 + (g % 7) as f32 * 0.01;
            payload.extend_from_slice(&f32_to_f16(s).to_le_bytes());
        }
        let xs: Vec<f32> = (0..cols)
            .map(|i| ((i * 7 + 3) % 29) as f32 / 29.0 - 0.5)
            .collect();
        let mut w = vec![0f32; rows * cols];
        cortiq_core::quant::dequant_q4_block(&payload, &mut w);
        let mut want = vec![0f32; rows];
        for o in 0..rows {
            want[o] = (0..cols).map(|i| w[o * cols + i] * xs[i]).sum();
        }
        let mut got = vec![0f32; rows];
        assert!(dispatch_q1t(
            c, &c.q4b, None, &payload, &xs, rows, cols, &mut got
        ));
        let max_d = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0f32, f32::max);
        assert!(max_d < 1e-2, "wgpu q4b_matvec ≠ CPU: max|Δ| = {max_d}");
    }

    #[test]
    fn wgpu_q1t_matmat_matches_cpu_reference() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping q1t GEMM parity test");
            return;
        };
        use cortiq_core::quant::{GROUP_SIZE, f32_to_f16, q1t_pack};
        let (b, rows, cols) = (40usize, 64usize, 256usize);
        let gpr = cols / GROUP_SIZE;
        let outliers: [(usize, f32); 4] = [(5, 3.0), (300, -2.0), (600, 1.5), (2000, -1.0)];
        let is_out = |flat: usize| outliers.iter().any(|&(i, _)| i == flat);
        let mut payload = Vec::new();
        for r in 0..rows {
            for g in 0..gpr {
                let s = 0.02 + ((r + g) % 7) as f32 * 0.01;
                payload.extend_from_slice(&f32_to_f16(s).to_le_bytes());
                let mut cc = [0u8; 7];
                for k in 0..GROUP_SIZE {
                    let code = if is_out(r * cols + g * GROUP_SIZE + k) {
                        0
                    } else {
                        ((k * 7 + r + g) % 3) as u8
                    };
                    q1t_pack(&mut cc, k, code);
                }
                payload.extend_from_slice(&cc);
            }
        }
        let mut row_ptr = vec![0u32; rows + 1];
        for &(idx, _) in &outliers {
            row_ptr[idx / cols + 1] += 1;
        }
        for r in 0..rows {
            row_ptr[r + 1] += row_ptr[r];
        }
        for &p in &row_ptr {
            payload.extend_from_slice(&p.to_le_bytes());
        }
        for &(idx, v) in &outliers {
            payload.extend_from_slice(&((idx % cols) as u16).to_le_bytes());
            payload.extend_from_slice(&f32_to_f16(v).to_le_bytes());
        }
        let xs: Vec<f32> = (0..b * cols)
            .map(|i| ((i * 13 + 7) % 31) as f32 / 31.0 - 0.5)
            .collect();
        let mut w = vec![0f32; rows * cols];
        cortiq_core::quant::dequant_q1t(&payload, rows, cols, &mut w);
        let mut want = vec![0f32; b * rows];
        for bi in 0..b {
            for o in 0..rows {
                want[bi * rows + o] = (0..cols).map(|i| w[o * cols + i] * xs[bi * cols + i]).sum();
            }
        }
        let mut got = vec![0f32; b * rows];
        assert!(dispatch_q1t_mm(
            c, None, &payload, &xs, b, rows, cols, &mut got
        ));
        let max_d = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0f32, f32::max);
        assert!(max_d < 2e-2, "wgpu q1t_mul_mm ≠ CPU: max|Δ| = {max_d}");
    }

    #[test]
    fn wgpu_q8_matmat_matches_cpu_reference() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping matmat test");
            return;
        };
        let (rows, cols, b) = (128usize, 64usize, 5usize);
        let mut q = vec![0i8; rows * cols];
        for (i, v) in q.iter_mut().enumerate() {
            *v = (((i * 53 + 3) % 255) as i32 - 127) as i8;
        }
        let rs: Vec<f32> = (0..rows).map(|r| 0.01 + (r % 5) as f32 * 0.004).collect();
        let pre: Vec<f32> = (0..b * cols)
            .map(|i| ((i % 17) as f32 - 8.0) * 0.05)
            .collect();
        // CPU ref: out[bi, o] = rs[o]·Σ q[o,i]·pre[bi,i].
        let mut want = vec![0f32; b * rows];
        for bi in 0..b {
            for o in 0..rows {
                let mut acc = 0f32;
                for i in 0..cols {
                    acc += q[o * cols + i] as f32 * pre[bi * cols + i];
                }
                want[bi * rows + o] = acc * rs[o];
            }
        }
        let qbytes: &[u8] = bytemuck::cast_slice(&q);
        let mut got = vec![0f32; b * rows];
        assert!(dispatch_matmat(
            c, None, qbytes, &rs, &pre, b, rows, cols, &mut got
        ));
        let max_d = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(max_d < 1e-3, "wgpu q8_matmat ≠ CPU: max|Δ| = {max_d}");
    }

    /// The tiled kernel (b ≥ 32) on deliberately awkward shapes: rows
    /// not a multiple of the 64-tile, cols not a multiple of the K-step
    /// — every edge guard fires.
    #[test]
    fn wgpu_q8_mul_mm_matches_cpu_reference() {
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping mul_mm test");
            return;
        };
        let (rows, cols, b) = (100usize, 52usize, 70usize);
        let mut q = vec![0i8; rows * cols];
        for (i, v) in q.iter_mut().enumerate() {
            *v = (((i * 31 + 7) % 255) as i32 - 127) as i8;
        }
        let rs: Vec<f32> = (0..rows).map(|r| 0.01 + (r % 7) as f32 * 0.003).collect();
        let pre: Vec<f32> = (0..b * cols)
            .map(|i| ((i % 19) as f32 - 9.0) * 0.04)
            .collect();
        let mut want = vec![0f32; b * rows];
        for bi in 0..b {
            for o in 0..rows {
                let mut acc = 0f32;
                for i in 0..cols {
                    acc += q[o * cols + i] as f32 * pre[bi * cols + i];
                }
                want[bi * rows + o] = acc * rs[o];
            }
        }
        let qbytes: &[u8] = bytemuck::cast_slice(&q);
        let mut got = vec![0f32; b * rows];
        assert!(dispatch_matmat(
            c, None, qbytes, &rs, &pre, b, rows, cols, &mut got
        ));
        let max_d = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(max_d < 1e-3, "wgpu q8_mul_mm ≠ CPU: max|Δ| = {max_d}");
    }

    // Tiled q1 GEMM on an awkward shape (rows/batch not 64-multiples, cols a
    // 64-multiple as the format requires): the prefill / speculative-batch path.
    #[test]
    fn wgpu_q1_mul_mm_matches_cpu_reference() {
        use cortiq_core::quant::{f16_to_f32, f32_to_f16};
        unsafe { std::env::set_var("CMF_GPU", "wgpu") };
        let Some(c) = ctx() else {
            eprintln!("no wgpu adapter — skipping q1_mul_mm test");
            return;
        };
        let (rows, cols, b) = (100usize, 128usize, 70usize); // cols % 64 == 0
        let np = cols / 64;
        let jit = |a: usize| ((a * 2654435761usize) >> 13) as u32; // cheap hash → bits
        // Build the q1 weight blob + a decoded f32 reference weight in lock-step.
        let mut q1w = vec![0u32; rows * np * 3];
        let mut wref = vec![0f32; rows * cols];
        for o in 0..rows {
            for pi in 0..np {
                let s0 = 0.02 + ((o * 7 + pi) % 11) as f32 * 0.005;
                let s1 = 0.03 + ((o * 3 + pi * 5) % 9) as f32 * 0.004;
                let (h0, h1) = (f32_to_f16(s0), f32_to_f16(s1));
                let (sf0, sf1) = (f16_to_f32(h0), f16_to_f32(h1));
                let bits0 = jit(o * 131 + pi * 17 + 1);
                let bits1 = jit(o * 131 + pi * 17 + 2);
                let base = o * np * 3 + pi * 3;
                q1w[base] = (h0 as u32) | ((bits0 & 0xFFFF) << 16);
                q1w[base + 1] = (bits0 >> 16) | ((h1 as u32) << 16);
                q1w[base + 2] = bits1;
                for j in 0..32usize {
                    let sgn0 = if (bits0 >> j) & 1 != 0 { sf0 } else { -sf0 };
                    let sgn1 = if (bits1 >> j) & 1 != 0 { sf1 } else { -sf1 };
                    wref[o * cols + pi * 64 + j] = sgn0;
                    wref[o * cols + pi * 64 + 32 + j] = sgn1;
                }
            }
        }
        let x: Vec<f32> = (0..b * cols)
            .map(|i| ((i % 23) as f32 - 11.0) * 0.03)
            .collect();
        let mut want = vec![0f32; b * rows];
        for bi in 0..b {
            for o in 0..rows {
                let mut acc = 0f32;
                for i in 0..cols {
                    acc += wref[o * cols + i] * x[bi * cols + i];
                }
                want[bi * rows + o] = acc;
            }
        }
        // GPU dispatch (inline — q1_mm is not yet wired into a public entry).
        let qbuf = storage_bytes(c, bytemuck::cast_slice(&q1w));
        let xbuf = storage_bytes(c, bytemuck::cast_slice(&x));
        let ybuf = rw_f32(c, b * rows, true);
        let pbuf = uniform_u32x4(c, [(cols / 4) as u32, rows as u32, b as u32, 0]);
        // q1_mul_mm never reads rs → its auto layout omits binding 2.
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.layout_q1mm,
            entries: &[
                bind_buf(0, &qbuf),
                bind_buf(1, &xbuf),
                bind_buf(3, &ybuf),
                bind_buf(4, &pbuf),
            ],
        });
        let mut enc = c
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
        {
            let mut pass = begin_pass(&mut enc);
            pass.set_pipeline(&c.q1_mm);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups((rows as u32).div_ceil(64), (b as u32).div_ceil(64), 1);
        }
        let mut sc = c.scratch.lock().unwrap();
        let stage = Scratch::ensure(
            &c.device,
            &mut sc.stage,
            (b * rows * 4) as u64,
            wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            "q1mm-stage",
        );
        let mut got = vec![0f32; b * rows];
        assert!(readback(
            c,
            enc,
            &ybuf,
            &stage,
            (b * rows * 4) as u64,
            &mut got
        ));
        drop(sc);
        let max_d = want
            .iter()
            .zip(&got)
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(max_d < 1e-3, "wgpu q1_mul_mm ≠ CPU: max|Δ| = {max_d}");
    }
}

/// Was the wgpu backend ASKED for, and did it come up? The two halves must
/// be told apart: a machine nobody pointed at wgpu is a legitimate skip, a
/// machine that was pointed at it and produced no context is a failure. A
/// reserved word in one shader once took the whole context down and every
/// GPU test reported success by skipping.
pub fn selected_and_up() -> Option<bool> {
    // "Asked" means an EXPLICIT request. The wgpu path also self-selects
    // by default on Linux/Windows, but a default selection on a box with
    // no device is the designed CPU fallback, not a failure — parity
    // tests skip there (what headless CI looks like) instead of dying.
    if std::env::var("CMF_GPU").is_err() || !selected() {
        return None; // nobody asked
    }
    Some(ctx().is_some())
}

/// Cheap device probe for `gpu::backend_available`: can wgpu bring an
/// adapter up here at all? One instance, no device/queue, no caching —
/// the caller caches.
/// Every adapter wgpu can see, and which one would be chosen. Three times in
/// one night the question "is the GPU actually visible?" was answered by
/// inference from a missing log line; this answers it directly.
/// Per-head RMS and the rope tail on the device — `rms` for the queries'
/// second normalisation, `inverse` for attention's output.
#[allow(clippy::too_many_arguments)]
pub fn rope_heads_for_test(
    x: &mut [f32],
    inv_freq: &[f32],
    nh: usize,
    hd: usize,
    rd: usize,
    pos: usize,
    eps: f32,
    rms: bool,
    inverse: bool,
) -> bool {
    let Some(c) = ctx() else { return false };
    if x.len() != nh * hd || rd > hd || rd % 2 != 0 || inv_freq.len() * 2 < rd {
        return false;
    }
    // In place: seeded from the host and read back afterwards, so the buffer
    // needs both directions. rw_f32 gives STORAGE|COPY_SRC and refuses the
    // write; storage_bytes gives STORAGE and refuses the read.
    let xb = c
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("rope-x"),
            contents: bytemuck::cast_slice(x),
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        });
    let fb = storage_bytes(c, bytemuck::cast_slice(&inv_freq[..rd / 2]));
    let pb = storage_bytes(c, bytemuck::cast_slice(&[pos as f32, eps]));
    let flags = (rms as u32) | ((inverse as u32) << 1);
    let p = uniform_u32x4(c, [nh as u32, hd as u32, rd as u32, flags]);
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("rope") });
    {
        let layout = c.rope_heads.get_bind_group_layout(0);
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &layout,
            entries: &[
                bind_buf(0, &xb),
                bind_buf(1, &fb),
                bind_buf(2, &p),
                bind_buf(3, &pb),
            ],
        });
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.rope_heads);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(nh as u32, 1, 1);
    }
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        (nh * hd * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "rope-stage",
    );
    let ok = readback(c, enc, &xb, &stage, (nh * hd * 4) as u64, x);
    drop(sc);
    ok
}

/// Stage A of the grouped low-rank output projection on the device.
///
/// `lora` rows share each group's slice of `attn`; `rows` is `groups * lora`.
/// q4tp only — the release stores wo_a that way in both published variants,
/// and guessing at a layout is how a kernel returns plausible nonsense.
pub fn o_lora_a_for_test(
    model: &Arc<CmfModel>,
    idx: usize,
    attn: &[f32],
    rows: usize,
    lora: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if rows == 0 || lora == 0 || rows % lora != 0 || out.len() < rows {
        return false;
    }
    let entry = &model.tensors[idx];
    if entry.dtype != cortiq_core::TensorDtype::Q4TiledP || entry.shape.len() != 2 {
        return false;
    }
    let (trows, cols) = (entry.shape[0], entry.shape[1]);
    let groups = rows / lora;
    if trows < rows || cols % 32 != 0 || attn.len() < groups * cols {
        return false;
    }
    let Some(abs) = model.entry_abs_offset(entry) else {
        return false;
    };
    let bytes = model.primary_bytes();
    let plen = entry.nbytes as usize;
    if abs + plen > bytes.len() {
        return false;
    }
    let Some(w) = weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen]) else {
        return false; // over budget → the caller keeps it on the CPU
    };
    let xb = storage_bytes(c, bytemuck::cast_slice(&attn[..groups * cols]));
    let yb = rw_f32(c, rows, true);
    let p = uniform_u32x4(c, [(cols / 32) as u32, rows as u32, lora as u32, 0]);
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("o-lora-a"),
        });
    {
        let layout = c.o_lora_a.get_bind_group_layout(0);
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &layout,
            entries: &[
                bind_buf(0, &w),
                bind_buf(1, &xb),
                bind_buf(2, &yb),
                bind_buf(3, &p),
            ],
        });
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.o_lora_a);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1);
    }
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        (rows * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "o-lora-stage",
    );
    let ok = readback(c, enc, &yb, &stage, (rows * 4) as u64, &mut out[..rows]);
    drop(sc);
    ok
}

/// The compressor's pooling step on the device — `overlap` picks the folding
/// the release uses at ratio 4, `ape` the positional bias the plain one adds.
#[allow(clippy::too_many_arguments)]
pub fn kv_pool_for_test(
    prev_kv: &[f32],
    prev_score: &[f32],
    cur_kv: &[f32],
    cur_score: &[f32],
    ape: Option<&[f32]>,
    ratio: usize,
    width: usize,
    overlap: bool,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if width == 0 || ratio == 0 || out.len() < width {
        return false;
    }
    let slots = if overlap { 2 * ratio } else { ratio };
    let stride = if overlap { 2 * width } else { width };
    if cur_kv.len() < ratio * stride || cur_score.len() < ratio * stride {
        return false;
    }
    let have_prev = overlap && prev_kv.len() >= ratio * stride && prev_score.len() >= ratio * stride;
    // Unused bindings still have to point somewhere; the current window is as
    // good a placeholder as an empty buffer and costs no allocation.
    let ckv = storage_bytes(c, bytemuck::cast_slice(cur_kv));
    let csc = storage_bytes(c, bytemuck::cast_slice(cur_score));
    let pkv = if have_prev {
        storage_bytes(c, bytemuck::cast_slice(prev_kv))
    } else {
        ckv.clone()
    };
    let psc = if have_prev {
        storage_bytes(c, bytemuck::cast_slice(prev_score))
    } else {
        csc.clone()
    };
    let use_ape = ape.is_some_and(|a| a.len() >= ratio * width) && !overlap;
    let apb = if use_ape {
        storage_bytes(c, bytemuck::cast_slice(ape.unwrap()))
    } else {
        csc.clone()
    };
    let yb = rw_f32(c, width, true);
    let flags = (overlap as u32) | ((have_prev as u32) << 1) | ((use_ape as u32) << 2);
    let p = uniform_u32x4(c, [slots as u32, width as u32, ratio as u32, flags]);
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("kv-pool") });
    {
        let layout = c.kv_pool.get_bind_group_layout(0);
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &layout,
            entries: &[
                bind_buf(0, &pkv),
                bind_buf(1, &psc),
                bind_buf(2, &ckv),
                bind_buf(3, &csc),
                bind_buf(4, &apb),
                bind_buf(5, &yb),
                bind_buf(6, &p),
            ],
        });
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.kv_pool);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups((width as u32).div_ceil(256), 1, 1);
    }
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        (width * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "kv-pool-stage",
    );
    let ok = readback(c, enc, &yb, &stage, (width * 4) as u64, &mut out[..width]);
    drop(sc);
    ok
}

/// The indexer's scoring pass on the device.
#[allow(clippy::too_many_arguments)]
pub fn index_scores_for_test(
    q: &[f32],
    kv: &[f32],
    hw: &[f32],
    nh: usize,
    hd: usize,
    n_pos: usize,
    limit: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if q.len() < nh * hd || kv.len() < n_pos * hd || hw.len() < nh || out.len() < n_pos {
        return false;
    }
    if n_pos == 0 {
        return true;
    }
    let qb = storage_bytes(c, bytemuck::cast_slice(&q[..nh * hd]));
    let kb = storage_bytes(c, bytemuck::cast_slice(&kv[..n_pos * hd]));
    let wb = storage_bytes(c, bytemuck::cast_slice(&hw[..nh]));
    let yb = rw_f32(c, n_pos, true);
    let p = uniform_u32x4(c, [nh as u32, hd as u32, n_pos as u32, limit as u32]);
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("ix") });
    {
        let layout = c.index_scores.get_bind_group_layout(0);
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &layout,
            entries: &[
                bind_buf(0, &qb),
                bind_buf(1, &kb),
                bind_buf(2, &wb),
                bind_buf(3, &yb),
                bind_buf(4, &p),
            ],
        });
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.index_scores);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups((n_pos as u32).min(MAX_WG), 1, 1);
    }
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        (n_pos * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "ix-stage",
    );
    let ok = readback(c, enc, &yb, &stage, (n_pos * 4) as u64, &mut out[..n_pos]);
    drop(sc);
    ok
}

/// Top-k positions on the device, in index order — the list `sparse_attend`
/// consumes. Bounded by the kernel's workgroup array; beyond it the caller
/// keeps the CPU's version rather than getting a truncated answer.
/// `y += w·x` (or `y = w·x` when `set`) on the device, for the parity test.
/// This kernel had no test at all, and its uniform was written in a
/// different field order than the shader reads — so it silently did nothing.
pub fn axpy_for_test(x: &[f32], y: &mut [f32], w: f32, set: bool, soff: usize) -> bool {
    let Some(c) = ctx() else { return false };
    let n = y.len();
    if n == 0 || x.len() < soff + n {
        return false;
    }
    let xb = storage_bytes(c, bytemuck::cast_slice(x));
    // COPY_DST as well: this one is written from the host before the
    // dispatch, and `rw_f32` only asks for STORAGE | COPY_SRC.
    let yb = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("axpy-y"),
        size: (n * 4) as u64,
        usage: wgpu::BufferUsages::STORAGE
            | wgpu::BufferUsages::COPY_SRC
            | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    c.queue.write_buffer(&yb, 0, bytemuck::cast_slice(y));
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("axpy") });
    {
        let mut pass = begin_pass(&mut enc);
        encode_axpy_full_p(&mut pass, c, &xb, &yb, w, n, set, soff, None);
    }
    let stage = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: (n * 4) as u64,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    readback(c, enc, &yb, &stage, (n * 4) as u64, y)
}

pub fn top_k_for_test(scores: &[f32], k: usize, out: &mut Vec<u32>) -> bool {
    let Some(c) = ctx() else { return false };
    let n = scores.len();
    if n == 0 || n > 4096 || k == 0 {
        out.clear();
        return n == 0;
    }
    let kk = k.min(n);
    let sb = storage_bytes(c, bytemuck::cast_slice(scores));
    let ib = rw_f32(c, kk, true);
    let cb = rw_f32(c, 1, true);
    let p = uniform_u32x4(c, [n as u32, k as u32, 0, 0]);
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("topk") });
    {
        let layout = c.top_k_index.get_bind_group_layout(0);
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &layout,
            entries: &[
                bind_buf(0, &sb),
                bind_buf(1, &ib),
                bind_buf(2, &cb),
                bind_buf(3, &p),
            ],
        });
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.top_k_index);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(1, 1, 1);
    }
    let bytes = ((kk + 1) * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        bytes,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "topk-stage",
    );
    enc.copy_buffer_to_buffer(&ib, 0, &stage, 0, (kk * 4) as u64);
    enc.copy_buffer_to_buffer(&cb, 0, &stage, (kk * 4) as u64, 4);
    submit(c, enc.finish());
    let slice = stage.slice(..bytes);
    slice.map_async(wgpu::MapMode::Read, |_| {});
    if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
        return false;
    }
    let mut ok = false;
    if let Ok(data) = slice.get_mapped_range() {
        let words: &[u32] = bytemuck::cast_slice(&data[..bytes as usize]);
        let cnt = (words[kk] as usize).min(kk);
        out.clear();
        out.extend_from_slice(&words[..cnt]);
        ok = true;
    }
    stage.unmap();
    drop(sc);
    ok
}

#[allow(clippy::too_many_arguments)]
fn encode_hc_fold_k(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    state: &wgpu::Buffer,
    mixes: &wgpu::Buffer,
    sc: &wgpu::Buffer,
    base: &wgpu::Buffer,
    fold: &wgpu::Buffer,
    post: &wgpu::Buffer,
    comb: &wgpu::Buffer,
    p: &wgpu::Buffer,
    bkey: (u8, u64, usize),
) {
    let mut pass = begin_pass(enc);
    encode_hc_fold_k_p(&mut pass, c, state, mixes, sc, base, fold, post, comb, p, None, bkey);
}

#[allow(clippy::too_many_arguments)]
fn encode_hc_fold_k_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    state: &wgpu::Buffer,
    mixes: &wgpu::Buffer,
    sc: &wgpu::Buffer,
    base: &wgpu::Buffer,
    fold: &wgpu::Buffer,
    post: &wgpu::Buffer,
    comb: &wgpu::Buffer,
    p: &wgpu::Buffer,
    // The norm that always follows: same workgroup, same reduction machinery,
    // one dispatch instead of two.
    nrm: Option<(&wgpu::Buffer, &wgpu::Buffer)>,
    bkey: (u8, u64, usize),
) {
    // The norm's weight and output must be BOUND either way — a bind group
    // has to satisfy the layout — so the absent case binds the fold buffer
    // twice and the kernel skips the phase.
    let (nw, out) = nrm.unwrap_or((fold, fold));
    let bind = cached_bind(c, bkey, || c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.hc_pre_fold.get_bind_group_layout(0),
        entries: &[
            bind_buf(0, state),
            bind_buf(1, mixes),
            bind_buf(2, sc),
            bind_buf(3, base),
            bind_buf(4, fold),
            bind_buf(5, post),
            bind_buf(6, comb),
            bind_buf(7, p),
            bind_buf(8, nw),
            bind_buf(9, out),
        ],
    }));
    pass.set_pipeline(&c.hc_pre_fold);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(1, 1, 1);
}

fn encode_hc_fold(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    state: &wgpu::Buffer,
    mixes: &wgpu::Buffer,
    sc: &wgpu::Buffer,
    base: &wgpu::Buffer,
    fold: &wgpu::Buffer,
    post: &wgpu::Buffer,
    comb: &wgpu::Buffer,
    p: &wgpu::Buffer,
) {
    let norm_out = frame_buf(c, 123, fold.size() as usize, false);
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.hc_pre_fold.get_bind_group_layout(0),
        entries: &[
            bind_buf(0, state),
            bind_buf(1, mixes),
            bind_buf(2, sc),
            bind_buf(3, base),
            bind_buf(4, fold),
            bind_buf(5, post),
            bind_buf(6, comb),
            bind_buf(7, p),
            // The fused-normalisation phase is disabled in this legacy
            // wrapper (`HcP.nrm == 0`) and runs as the next dispatch, but the
            // pipeline layout still requires every declared binding.
            bind_buf(8, base),
            bind_buf(9, &norm_out),
        ],
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(&c.hc_pre_fold);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(1, 1, 1);
}

#[allow(clippy::too_many_arguments)]
fn encode_hc_expand_k(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    res: &wgpu::Buffer,
    post: &wgpu::Buffer,
    comb: &wgpu::Buffer,
    out: &wgpu::Buffer,
    p: &wgpu::Buffer,
    hc: usize,
    dim: usize,
    bkey: (u8, u64, usize),
) {
    let mut pass = begin_pass(enc);
    encode_hc_expand_k_p(&mut pass, c, x, res, post, comb, out, p, hc, dim, bkey);
}

#[allow(clippy::too_many_arguments)]
fn encode_hc_expand_k_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    x: &wgpu::Buffer,
    res: &wgpu::Buffer,
    post: &wgpu::Buffer,
    comb: &wgpu::Buffer,
    out: &wgpu::Buffer,
    p: &wgpu::Buffer,
    hc: usize,
    dim: usize,
    bkey: (u8, u64, usize),
) {
    let bind = cached_bind(c, bkey, || c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.hc_post_expand.get_bind_group_layout(0),
        entries: &[
            bind_buf(0, x),
            bind_buf(1, res),
            bind_buf(2, post),
            bind_buf(3, comb),
            bind_buf(4, out),
            bind_buf(5, p),
        ],
    }));
    pass.set_pipeline(&c.hc_post_expand);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(((hc * dim) as u32).div_ceil(256), 1, 1);
}

fn encode_hc_expand(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    res: &wgpu::Buffer,
    post: &wgpu::Buffer,
    comb: &wgpu::Buffer,
    out: &wgpu::Buffer,
    p: &wgpu::Buffer,
    hc: usize,
    dim: usize,
) {
    let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.hc_post_expand.get_bind_group_layout(0),
        entries: &[
            bind_buf(0, x),
            bind_buf(1, res),
            bind_buf(2, post),
            bind_buf(3, comb),
            bind_buf(4, out),
            bind_buf(5, p),
        ],
    });
    let mut pass = begin_pass(enc);
    pass.set_pipeline(&c.hc_post_expand);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(((hc * dim) as u32).div_ceil(256), 1, 1);
}

/// The attention chain from an already-normed LoRA vector to the block output.
#[allow(clippy::too_many_arguments)]
fn encode_attn_chain(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    wb: &[wgpu::Buffer],
    qn: &wgpu::Buffer,
    q: &wgpu::Buffer,
    attn: &wgpu::Buffer,
    mid: &wgpu::Buffer,
    out: &wgpu::Buffer,
    cache: &wgpu::Buffer,
    ixb: &wgpu::Buffer,
    sink: &wgpu::Buffer,
    freq: &wgpu::Buffer,
    posb: &wgpu::Buffer,
    g: Dsv4AttnGeom,
    kv_id: u64,
    li: usize,
    m: usize,
    q_ready: bool,
) {
    let rows = g.o_groups * g.o_lora;
    let cols = g.nh * g.hd / g.o_groups;
    // By SHAPE, not by flag. The 256-thread twin measured 21.3 tok/s against
    // the 64-thread kernel's 21.8: this projection's columns are
    // nh·hd/o_groups, so `gpr` is 128 on the release and half of 256 threads
    // would stride past the end. Wide only where there is width to use.
    // By shape: 256 threads only where a row has 256 groups to give them;
    // otherwise four rows at once, which buys overlap instead of width.
    // CMF_DSV4_OLORA picks explicitly for the A/B.
    let o_pipe = match olora_pick() {
        Some(1) => &c.o_lora_a,
        Some(2) => &c.o_lora_a_w,
        Some(3) => &c.o_lora_a_m,
        _ if !chain_mv4() => &c.o_lora_a,
        _ if cols / 32 >= 256 => &c.o_lora_a_w,
        _ => &c.o_lora_a_m,
    };
    let o_rows_per_wg = if std::ptr::eq(o_pipe, &c.o_lora_a_m) { 4u32 } else { 1u32 };
    let o_bind = cached_bind(c, (63, kv_id, li), || {
        let p = uniform_u32x4(c, [(cols / 32) as u32, rows as u32, g.o_lora as u32, 0]);
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &o_pipe.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, &wb[2]),
                bind_buf(1, attn),
                bind_buf(2, mid),
                bind_buf(3, &p),
            ],
        })
    });
    // Six dependent dispatches, no copy between them: one pass. Split back
    // into six only when the timestamp query set exists, because a per-stage
    // timing needs a pass boundary to be written at — and that is the whole
    // reason attention had six in the first place.
    if c.ts_query.is_none() && !sa_split() {
        let sa_bind = sa_bind_single(c, q, cache, ixb, sink, attn, g.nh, g.hd, m, g.scale,
            Some((kv_id, li)));
        let mut pass = begin_pass(enc);
        if !q_ready && !dsv4_skip("qproj") {
            encode_q4tp_mvw_p(&mut pass, c, &wb[1], qn, q, g.nh * g.hd, g.q_lora,
                (60, kv_id, li));
        }
        encode_rope_heads_p(&mut pass, c, q, freq, posb, g.nh, g.hd, g.rd, true, false,
            (61, kv_id, li));
        if !dsv4_skip("sa") {
            if sa_split_k() {
                encode_sa_split_p(&mut pass, c, q, cache, ixb, sink, attn, g.nh, g.hd, m,
                    g.scale, kv_id, li);
            } else {
                pass.set_pipeline(&c.sparse_attend);
                pass.set_bind_group(0, &sa_bind, &[]);
                pass.dispatch_workgroups(g.nh as u32, 1, 1);
            }
        }
        encode_rope_heads_p(&mut pass, c, attn, freq, posb, g.nh, g.hd, g.rd, false, true,
            (62, kv_id, li));
        // Split apart: the pair measured 5.0 ms of a 30.6 ms chain against a
        // bandwidth floor near 0.7, and they are different kernels reading
        // differently-shaped weights.
        if !dsv4_skip("olora") && !dsv4_skip("oproj")
            && !(olora_mv4()
                && encode_o_lora_mv4_p(&mut pass, c, &wb[2], attn, mid, rows, cols,
                    g.o_lora, (65, kv_id, li)))
        {
            pass.set_pipeline(o_pipe);
            pass.set_bind_group(0, &o_bind, &[]);
            pass.dispatch_workgroups(
                (rows as u32).div_ceil(o_rows_per_wg).min(MAX_WG), 1, 1);
        }
        if !dsv4_skip("wob") && !dsv4_skip("oproj") {
            encode_q4tp_mvw_p(&mut pass, c, &wb[3], mid, out, g.dim, g.o_groups * g.o_lora,
                (64, kv_id, li));
        }
        return;
    }
    if !q_ready {
        encode_q4tp_mv1(c, enc, &wb[1], qn, q, g.nh * g.hd, g.q_lora, (60, kv_id, li));
    }
    encode_rope_heads(c, enc, q, freq, posb, g.nh, g.hd, g.rd, true, false, (61, kv_id, li));
    encode_sparse_attend2(c, enc, q, cache, ixb, sink, attn, g.nh, g.hd, m, g.scale,
        Some((kv_id, li)));
    encode_rope_heads(c, enc, attn, freq, posb, g.nh, g.hd, g.rd, false, true, (62, kv_id, li));
    {
        let mut pass = begin_pass(enc);
        pass.set_pipeline(o_pipe);
        pass.set_bind_group(0, &o_bind, &[]);
        pass.dispatch_workgroups((rows as u32).div_ceil(o_rows_per_wg).min(MAX_WG), 1, 1);
    }
    encode_q4tp_mv1(
        c,
        enc,
        &wb[3],
        mid,
        out,
        g.dim,
        g.o_groups * g.o_lora,
        (64, kv_id, li),
    );
}

/// Route, then the chosen experts and the shared one.
#[allow(clippy::too_many_arguments)]
fn encode_moe_chain(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    logits: &wgpu::Buffer,
    x: &wgpu::Buffer,
    msel: &wgpu::Buffer,
    mwt: &wgpu::Buffer,
    mcnt: &wgpu::Buffer,
    mact: &wgpu::Buffer,
    out: &wgpu::Buffer,
    gate_all: &wgpu::Buffer,
    up_all: &wgpu::Buffer,
    down_all: &wgpu::Buffer,
    w: &Dsv4LayerW,
    g: Dsv4MoeGeom,
    n_pack: usize,
    slots: usize,
    bkey: (u64, usize),
) {
    let mut pass = begin_pass(enc);
    encode_moe_chain_p(&mut pass, c, logits, x, msel, mwt, mcnt, mact, out, gate_all, up_all, down_all, w, g, n_pack, slots, bkey);
}

#[allow(clippy::too_many_arguments)]
fn encode_moe_chain_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    logits: &wgpu::Buffer,
    x: &wgpu::Buffer,
    msel: &wgpu::Buffer,
    mwt: &wgpu::Buffer,
    mcnt: &wgpu::Buffer,
    mact: &wgpu::Buffer,
    out: &wgpu::Buffer,
    gate_all: &wgpu::Buffer,
    up_all: &wgpu::Buffer,
    down_all: &wgpu::Buffer,
    w: &Dsv4LayerW,
    g: Dsv4MoeGeom,
    n_pack: usize,
    slots: usize,
    bkey: (u64, usize),
) {
    // The bias now lives in the PACK, whose address is stable for the life
    // of the process — so the const cache is sound for it, and each layer
    // gets its own device buffer. The per-call pool here was the many-layer
    // clobber: every queue write lands before the run's single submit.
    let bs = match w.moe.bias {
        Some(b) if b.len() >= n_pack => const_buf(c, bytemuck::cast_slice(&b[..n_pack])),
        _ => logits.clone(),
    };
    let mk = frame_buf(c, 17, n_pack * 4, true);
    // Per-layer, not pooled: a run holding two hash layers wrote both lists
    // into one buffer before the single submit, and both routed with the
    // second one's experts.
    let fc = match w.moe.forced {
        Some(f) if f.len() >= g.top_k => {
            let v: Vec<u32> = f[..g.top_k].iter().map(|&i| i as u32).collect();
            store_slot(c, 18, bkey.0, bkey.1, bytemuck::cast_slice(&v))
        }
        _ => store_slot(c, 18, bkey.0, bkey.1, &vec![0u8; g.top_k * 4]),
    };
    let rflags = (w.moe.bias.is_some_and(|b| b.len() >= n_pack) as u32)
        | ((w.moe.forced.is_some_and(|f| f.len() >= g.top_k) as u32) << 2)
        | 8
        | ((n_pack as u32) << 8);
    let rp = uniform_mixed(c, [n_pack as u32, g.top_k as u32, rflags], g.route_scale);
    let stride16 = |rows: usize, cols: usize, q2: bool| -> u32 {
        let dt = if q2 {
            cortiq_core::TensorDtype::Q2TiledP
        } else {
            cortiq_core::TensorDtype::Q4TiledP
        };
        (cortiq_core::quant::expected_nbytes(dt, &[rows, cols]).unwrap_or(0) / 2) as u32
    };
    let gu_u = uniform_u32x8(
        c,
        [
            (g.hidden / 32) as u32,
            g.inter as u32,
            slots as u32,
            stride16(g.inter, g.hidden, g.gu_q2),
            g.swiglu_limit.to_bits(),
            0,
            0,
            0,
        ],
    );
    let dn_u = uniform_u32x4(
        c,
        [
            (g.inter / 32) as u32,
            g.hidden as u32,
            slots as u32,
            stride16(g.hidden, g.inter, false),
        ],
    );
    // Four rows to a workgroup where the layout allows it: the columns give
    // gpr = 128, so a row cannot use more than 64 lanes, and the only width
    // left is overlap between rows. CMF_DSV4_MOE4=0 reverts.
    let (p_gu, p_dn, l_gu, l_dn) = if g.gu_q2 {
        (
            if moe4() { &c.moe_gu_q2tp_m } else { &c.moe_gate_up_q2tp },
            if moe4() { &c.moe_dn_q4tp_m } else { &c.moe_down_q4tp },
            &c.layout_moe_gu_q2tp,
            &c.layout_moe_dn_q4tp,
        )
    } else {
        (
            &c.moe_gate_up_q4tp_b,
            &c.moe_down_q4tp_b,
            &c.layout_moe_gu_b,
            &c.layout_moe_dn_b,
        )
    };
    let bind_r = cached_bind(c, (127, bkey.0, bkey.1), || c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.moe_route.get_bind_group_layout(0),
        entries: &[
            bind_buf(0, logits),
            bind_buf(1, &bs),
            bind_buf(2, &mk),
            bind_buf(3, &fc),
            bind_buf(4, msel),
            bind_buf(5, mwt),
            bind_buf(6, mcnt),
            bind_buf(7, &rp),
            bind_buf(8, &frame_buf(c, 26, n_pack.max(1) * 4, true)),
            bind_buf(9, &frame_buf(c, 27, 2 * g.top_k * 4, false)),
        ],
    }));
    // The layout comes from the PIPELINE, not the cached one: wgpu treats an
    // auto-derived layout as exclusive to the pipeline that produced it, so a
    // group built against a twin's layout is rejected at dispatch — with a
    // message about "exclusive pipelines", not about layouts.
    let bg_gu = cached_bind(c, (128, bkey.0, bkey.1), || c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &p_gu.get_bind_group_layout(0),
        entries: &[
            bind_buf(0, gate_all),
            bind_buf(1, up_all),
            bind_buf(2, x),
            bind_buf(3, msel),
            bind_buf(4, mact),
            bind_buf(5, &gu_u),
        ],
    }));
    let bg_dn = cached_bind(c, (129, bkey.0, bkey.1), || c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &p_dn.get_bind_group_layout(0),
        entries: &[
            bind_buf(0, down_all),
            bind_buf(1, mact),
            bind_buf(2, msel),
            bind_buf(3, mwt),
            bind_buf(4, out),
            bind_buf(5, &dn_u),
        ],
    }));
    // Three separately-skippable parts: the router ranks 256 experts in one
    // workgroup, the gate/up pair streams the experts' weights, and the down
    // projection streams them again. The MoE measured 5.5 ms of a 30.6 ms
    // chain against a weight-bandwidth floor near 2.5, and which of the
    // three owns that gap is not something to reason about.
    if !dsv4_skip("route") {
        pass.set_pipeline(&c.moe_route);
        pass.set_bind_group(0, &bind_r, &[]);
        pass.dispatch_workgroups(1, 1, 1);
    }
    if !dsv4_skip("gu") {
        pass.set_pipeline(p_gu);
        pass.set_bind_group(0, &bg_gu, &[]);
        let gu_per_wg = if g.gu_q2 && moe4() { 4u32 } else { 1u32 };
        pass.dispatch_workgroups((g.inter as u32).div_ceil(gu_per_wg), slots as u32, 1);
    }
    if !dsv4_skip("dn") {
        pass.set_pipeline(p_dn);
        pass.set_bind_group(0, &bg_dn, &[]);
        pass.dispatch_workgroups(g.hidden as u32, 1, 1);
    }
}

/// Everything one DeepSeek-V4 layer does, in ONE submission.
///
/// The two frames before this cost two barriers a layer — 30 ms of a 76 ms
/// token, spent waiting rather than computing. Fusing them means the
/// hyper-connection glue between the halves has to run on the device too,
/// which is what `hc_pre_fold` (mixes, Sinkhorn and the fold in one kernel)
/// and `hc_post_expand` were built for.
///
/// The frame is shifted by one on purpose: it ENDS by folding and norming the
/// state for the NEXT layer's attention half and projecting its LoRA vector,
/// then reads back that normed hidden. The host needs exactly that one vector
/// — for the kv projection, the compressor and the indexer, which still live
/// there — and nothing else. Layer zero's opening fold is done on the host
/// once, which costs nothing at all.
#[derive(Clone)]
pub struct Dsv4LayerW<'a> {
    pub attn: Dsv4AttnW<'a>,
    pub moe: Dsv4MoeW<'a>,
    /// Hyper-connection projection of the FFN half: `[mix_hc, hc*dim]` f32.
    pub hc_ffn_fn: &'a [f32],
    pub hc_ffn_scale: &'a [f32; 3],
    pub hc_ffn_base: &'a [f32],
    /// The same for the NEXT layer's attention half — absent on the last.
    pub hc_next_fn: Option<&'a [f32]>,
    pub hc_next_scale: &'a [f32; 3],
    pub hc_next_base: &'a [f32],
    pub ffn_norm: &'a [f32],
    /// The next layer's input norm and its q_norm, for the tail that
    /// prepares the following frame.
    pub next_norm: &'a [f32],
    pub next_q_norm: &'a [f32],
    /// The next layer's wq_a, by directory index.
    pub next_wq_a: Option<usize>,
    /// Router logits weight, f32 `[n_exp, dim]`.
    pub router: &'a [f32],
}

#[derive(Clone, Copy)]
pub struct Dsv4LayerGeom {
    pub attn: Dsv4AttnGeom,
    pub moe: Dsv4MoeGeom,
    pub hc: usize,
    pub hc_eps: f32,
    pub sinkhorn_iters: usize,
}

/// Returns the next layer's normed hidden in `folded_next` (or this layer's
/// state contribution when there is no next layer).
#[allow(clippy::too_many_arguments)]
/// Encode one layer's whole preparation — window, compressor, the indexer's
/// compressor, the indexer — into the caller's encoder, and return the
/// attended list's length.
///
/// The order matters and matches the host's: the compressors see the token
/// BEFORE the window append changes what `filled` means, and the indexer
/// scores against the cache its own compressor has just extended.
#[allow(clippy::too_many_arguments)]
pub fn dsv4_encode_prep(
    model: &Arc<CmfModel>,
    p: &Dsv4Prep,
    kv_id: u64,
    li: usize,
    hidden: &wgpu::Buffer,
    qn: &wgpu::Buffer,
    cache: &wgpu::Buffer,
    idx_out: &wgpu::Buffer,
    hd: usize,
    window: usize,
    dim: usize,
    rope_dim: usize,
    eps: f32,
    pos: usize,
    inv_freq: &[f32],
    enc: &mut wgpu::CommandEncoder,
) -> Option<usize> {
    let c = ctx()?;
    // The compressors first: both read this token's hidden state, and the
    // attention one appends into the same cache buffer the window lives in,
    // past the window's capacity.
    let mut n_comp = p.n_comp;
    if let Some((cw, cg)) = &p.comp {
        if !dsv4_skip("comp") && dsv4_compressor_frame(
            model, cw, *cg, 0, kv_id, li, hidden, pos, inv_freq, cache, p.comp_dst_off, enc,
        )
        .is_some()
        {
            n_comp += 1;
        }
    }
    let mut n_ix = p.n_ix;
    let mut m = None;
    if let Some((iw, ig, ixw, ixg)) = &p.ix {
        let ixkv = dsv4_index_cache(kv_id, li, ixg.idim * (n_ix + 1))?;
        if !dsv4_skip("comp") && dsv4_compressor_frame(
            model, iw, *ig, 1, kv_id, li, hidden, pos, inv_freq, &ixkv, p.ix_dst_off, enc,
        )
        .is_some()
        {
            n_ix += 1;
        }
        // Nothing compressed yet means nothing to score: attention sees the
        // window alone, which is what the host does at the start of a
        // sequence too.
        if n_comp > 0 && n_ix > 0 {
            m = if dsv4_skip("ix") { None } else { dsv4_indexer_frame(
                model, ixw, *ixg, kv_id, li, hidden, qn, &ixkv, n_ix, n_comp,
                // The window AFTER this token's append, which is what the
                // host reads off its own vector: clamp the incremented
                // count, not the count. `filled.min(window) + 1` is right
                // until the window fills and then hands attention one
                // position more than the window holds, for the rest of the
                // sequence.
                (p.filled + 1).min(window), pos, inv_freq, idx_out, enc,
            ) };
        }
    }
    if dsv4_skip("win") {
        return Some(m.unwrap_or(p.filled.min(window)));
    }
    let filled = dsv4_window_append(
        model, p.wkv, p.kv_norm, hidden, cache, hd, window, p.filled, dim, rope_dim, eps,
        pos, inv_freq, kv_id, li, enc,
    )?;
    match m {
        Some(m) => Some(m),
        // No indexer, or nothing to score: the list is the window, and every
        // compressed position after it when there is no indexer to choose.
        None => {
            let k = if p.ix.is_none() { n_comp } else { 0 };
            let pick: Vec<u32> = (0..k as u32).collect();
            let pb = if pick.is_empty() {
                frame_buf(c, 111, 4, true)
            } else {
                frame_up(c, 111, bytemuck::cast_slice(&pick))
            };
            encode_idx_build(c, enc, &pb, idx_out, filled, window, k, None);
            Some(filled + k)
        }
    }
}

/// The indexer's own compressed cache, on the device and grown as the
/// sequence does — the attention cache's twin, kept apart because the two
/// have different widths.
fn dsv4_index_cache(kv_id: u64, li: usize, floats: usize) -> Option<wgpu::Buffer> {
    let c = ctx()?;
    let cap = floats.next_power_of_two().max(1024);
    let mut m = c.dsv4_ixkv.lock().unwrap();
    // Growing CARRIES the contents. This cache accumulates one compressed
    // entry every `ratio` tokens for the whole sequence, and dropping the old
    // buffer meant the indexer scored every earlier entry against zeros from
    // the next power of two onward — silently, and only on contexts long
    // enough to cross one. The KV cache beside it has always copied; this one
    // did not.
    let carry = match m.get(&(kv_id, li)) {
        Some((old, have)) if *have < cap => Some((old.clone(), *have)),
        _ => None,
    };
    if let Some((old, have)) = carry {
        let bigger = c.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("dsv4-index-kv"),
            size: (cap * 4) as u64,
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_SRC
                | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        let mut enc = c
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("ixkv-grow"),
            });
        enc.copy_buffer_to_buffer(&old, 0, &bigger, 0, (have * 4) as u64);
        submit(c, enc.finish());
        m.insert((kv_id, li), (bigger, cap));
        // The same epoch the KV cache bumps: a cached bind group outlives
        // the buffer it points at otherwise.
        GREW.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    }
    let (b, _) = m.entry((kv_id, li)).or_insert_with(|| {
        (
            c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("dsv4-index-kv"),
                size: (cap * 4) as u64,
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_SRC
                    | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            }),
            cap,
        )
    });
    Some(b.clone())
}

/// Everything a layer needs to build its OWN attention inputs on the card:
/// the window append, the compressor (and the indexer's, and the indexer) —
/// the three producers that used to run on the host between submissions.
///
/// With this present a layer frame needs nothing from the host but the
/// position, so its result never has to come back: `folded_next` stays on
/// the device and the NEXT layer reads it there. That is the whole of step
/// four — the readback at the end of this function was there for the host's
/// prep and for nothing else.
#[derive(Clone)]
pub struct Dsv4Prep<'a> {
    /// The KV projection that feeds the sliding window, and its norm.
    pub wkv: usize,
    pub kv_norm: &'a [f32],
    /// The attention compressor; `None` on the pure sliding-window layers.
    pub comp: Option<(Dsv4CompW<'a>, Dsv4CompGeom)>,
    /// The indexer: its own compressor, then the scoring half.
    pub ix: Option<(Dsv4CompW<'a>, Dsv4CompGeom, Dsv4IxW, Dsv4IxGeom)>,
    /// Cache bookkeeping the host keeps (all of it is derivable from the
    /// position, so none of it costs a readback): how much of the window is
    /// filled BEFORE this token, how many compressed entries each cache
    /// holds, and where the next one goes.
    pub filled: usize,
    /// The window's CAPACITY in slots — where the compressed region starts.
    pub window: usize,
    pub n_comp: usize,
    pub n_ix: usize,
    pub comp_dst_off: usize,
    pub ix_dst_off: usize,
    /// The most positions attention can be asked to read: window capacity
    /// plus the indexer's budget (or every compressed entry, without one).
    /// The list buffer is sized by THIS — sizing it by the host list, which
    /// is empty when the device builds its own, cut the release's 640-entry
    /// list to 64: the writes past that are silently clamped and the reads
    /// return zero, so attention quietly stares at position zero.
    pub idx_cap: usize,
}

#[allow(clippy::too_many_arguments)]
/// One layer, encoded into the CALLER'S encoder and never submitted here.
///
/// `prep` present means the layer builds its own attention inputs on the
/// card and `idxs` is ignored; absent means the host supplied the list, as
/// before. Either way nothing comes back: the folded state for the next
/// layer is left in the frame's own buffer, which is where the next call
/// looks for it. That is what lets a whole token be one submission.
#[allow(clippy::too_many_arguments)]
/// `CMF_DSV4_SKIP=attn|moe|prep|hc` — TIMING ONLY, the answer is garbage.
/// Drops a stage's dispatches while leaving every buffer, pass and shape in
/// place, so the delta in tok/s is that stage's real share of the token.
/// Neither dispatch counting nor pass counting predicted the frame
/// correctly on the qwen graph; there is no reason to trust them here.
/// `CMF_DSV4_HCFUSE=1` runs the fused hyper-connection join — four steps and
/// a Sinkhorn in ONE workgroup instead of four dispatches.
///
/// OFF by default, because it was MEASURED and it loses. The arithmetic
/// argument was right and the conclusion was wrong: 336 launches a token is
/// real, but so is the mix projection — 24 outputs over hc·dim = 16 384
/// inputs, 1.5 MB of weights — and putting that through one workgroup uses
/// one SM of two hundred. On the release the chain's wait went 51.6 → 84.5
/// ms and decode 16.5 → 10.6 tok/s. Kept, and kept switchable, because the
/// same kernel wins wherever launches dominate and the mix is small.
fn hc_fuse() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_HCFUSE").is_ok_and(|v| v != "0"))
}

fn dsv4_skip(what: &str) -> bool {
    static S: std::sync::OnceLock<String> = std::sync::OnceLock::new();
    S.get_or_init(|| std::env::var("CMF_DSV4_SKIP").unwrap_or_default())
        .contains(what)
}

/// How many tokens of one batch a layer's keys can hold apart. Generous:
/// the keys are only cache discriminators, and collisions between layers
/// would be far more expensive to debug than a sparse map is to keep.
const FRAME_TOK_STRIDE: usize = 64;

thread_local! {
    // A batch records several calls before one submit. Every pooled upload,
    // mutable uniform and cached bind group inside a frame must therefore
    // have a token identity; otherwise the final queue.write_buffer wins for
    // all of them. Persistent model/cache buffers do NOT use this salt.
    static DSV4_FRAME_SALT: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

struct Dsv4FrameSalt(usize);

impl Dsv4FrameSalt {
    fn enter(salt: usize) -> Self {
        let old = DSV4_FRAME_SALT.with(|s| s.replace(salt));
        Self(old)
    }
}

impl Drop for Dsv4FrameSalt {
    fn drop(&mut self) {
        DSV4_FRAME_SALT.with(|s| s.set(self.0));
    }
}

#[inline]
fn dsv4_frame_salt() -> usize {
    DSV4_FRAME_SALT.with(std::cell::Cell::get)
}

#[inline]
fn dsv4_salted_li(li: usize) -> usize {
    let salt = dsv4_frame_salt();
    if salt == 0 { li } else { li + salt * 1_000_000 }
}

fn dsv4_layer_frame_enc(
    model: &Arc<CmfModel>,
    w: &Dsv4LayerW,
    g: Dsv4LayerGeom,
    kv_id: u64,
    li: usize,
    // Which token of the batch this frame encodes. The KV cache is the
    // layer's, so it keys on `li`; everything else — uniform slots, cached
    // bind groups, the scratch that carries this token's state between
    // dispatches — has to be the TOKEN's, or two tokens encoded into one
    // submission share a slot and the last write decides for both. That is
    // silent and it is wrong: their rope positions differ.
    tok: usize,
    batch_frame: bool,
    q_ready: bool,
    defer_next_q: bool,
    qn: Option<&[f32]>,
    idxs: &[u32],
    prep: Option<&Dsv4Prep>,
    inv_freq: &[f32],
    pos: usize,
    enc: &mut wgpu::CommandEncoder,
) -> Option<wgpu::Buffer> {
    // Salt 0 preserves every single-token cache key. Batch token zero must
    // still differ from it, hence the one-based value.
    let _frame_salt = Dsv4FrameSalt::enter(if batch_frame { tok + 1 } else { 0 });
    macro_rules! no {
        ($($t:tt)*) => {{
            if std::env::var("CMF_DSV4_FRAME_DEBUG").is_ok() {
                eprintln!("кадр слоя отклонён: {}", format_args!($($t)*));
            }
            return None;
        }};
    }
    let Some(c) = ctx() else { no!("нет контекста wgpu") };
    // Keys are per (layer, token); the cache lookup below stays per layer.
    let lk = li * FRAME_TOK_STRIDE + tok;
    let a = g.attn;
    let m = g.moe;
    let (hc, dim) = (g.hc, a.dim);
    let mix_hc = (2 + hc) * hc;
    if qn.is_some_and(|v| v.len() < a.q_lora)
        || (prep.is_none() && (idxs.is_empty() || idxs.len() > 1024))
    {
        no!("формы: idx {}", idxs.len());
    }
    if w.hc_ffn_fn.len() < mix_hc * hc * dim || w.router.len() < m.hidden {
        no!("гипер-связи или роутер не той формы");
    }

    // ── weights ──
    let bytes = model.primary_bytes();
    let mut wb = Vec::with_capacity(5);
    for &idx in &[
        w.attn.wq_a,
        w.attn.wq_b,
        w.attn.wo_a,
        w.attn.wo_b,
        w.next_wq_a.unwrap_or(w.attn.wq_a),
    ] {
        let Some(e) = model.tensors.get(idx) else {
            no!("тензора {idx} нет");
        };
        if e.dtype != cortiq_core::TensorDtype::Q4TiledP {
            no!("{} не q4tp", e.name);
        }
        let (Some(abs), plen) = (model.entry_abs_offset(e), e.nbytes as usize) else {
            no!("{} без смещения", e.name);
        };
        let Some(b) = weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen])
        else {
            no!("{} не влез в VRAM", e.name);
        };
        wb.push(b);
    }
    let Some((gate_all, up_all, down_all)) = moe_expert_bufs(
        c,
        model,
        w.moe.experts,
        m.inter,
        m.hidden,
        true,
        m.gu_q2,
        false,
    ) else {
        no!("эксперты не влезли в VRAM");
    };
    let cache = {
        let map = c.dsv4_kv.lock().unwrap();
        match map.get(&(kv_id, li)) {
            Some((b, _)) => b.clone(),
            None => no!("кеш ({kv_id}, {li}) не заведён"),
        }
    };
    // The hyper-connection state lives on the card for the whole token; the
    // host seeds it once at layer zero.
    let state = frame_buf_t(c, 40, tok, hc * dim * 4, true);

    // ── constants (model-owned, address keying is sound) ──
    let qnw = const_buf(c, bytemuck::cast_slice(&w.attn.q_norm[..a.q_lora]));
    let sink = const_buf(c, bytemuck::cast_slice(&w.attn.sink[..a.nh]));
    let freq = const_buf(c, bytemuck::cast_slice(&inv_freq[..a.rd / 2]));
    let ffn_fn = const_buf(c, bytemuck::cast_slice(w.hc_ffn_fn));
    let ffn_sc = const_buf(c, bytemuck::cast_slice(w.hc_ffn_scale));
    let ffn_bs = const_buf(c, bytemuck::cast_slice(&w.hc_ffn_base[..mix_hc]));
    let ffn_nw = const_buf(c, bytemuck::cast_slice(&w.ffn_norm[..dim]));
    let n_exp = w.moe.experts.len().saturating_sub(1);
    if w.router.len() < m.hidden * n_exp {
        no!("роутер короче {} × {}", n_exp, m.hidden);
    }
    let router = const_buf(c, bytemuck::cast_slice(&w.router[..m.hidden * n_exp]));
    let next_nw = const_buf(c, bytemuck::cast_slice(&w.next_norm[..dim]));
    let next_qn = const_buf(c, bytemuck::cast_slice(&w.next_q_norm[..a.q_lora]));

    // ── per-call uploads ──
    let posb = frame_up_pos_t(c, 1, tok, pos, a.eps);
    let ixb = {
        // Flat 1024 — the attention list's hard ceiling (the m > 1024
        // guard). 4 KB buys a PERMANENT identity: frame_buf keys on
        // (tag, len), so a growing capacity would mint a new buffer while
        // every cached group kept the old one, with no GREW bump to save it.
        let b = frame_buf_t(c, 2, tok, 1024 * 4, true);
        if !idxs.is_empty() {
            c.queue.write_buffer(&b, 0, bytemuck::cast_slice(idxs));
        }
        b
    };
    let qnb = match qn {
        Some(v) => frame_up(c, 4, bytemuck::cast_slice(&v[..a.q_lora])),
        None => frame_buf_t(c, 4, tok, a.q_lora * 4, true),
    };

    // ── working buffers ──
    let n_pack = n_exp;
    let slots = m.top_k + 1;
    let q = frame_buf(c, 5, a.nh * a.hd * 4, false);
    let attn = frame_buf(c, 6, a.nh * a.hd * 4, false);
    let mid = frame_buf(c, 7, a.o_groups * a.o_lora * 4, false);
    let ao = frame_buf(c, 8, dim * 4, false);
    let mixes = frame_buf(c, 41, mix_hc * 4, false);
    // `true`: the tail copies the next layer's input INTO this one, so it is
    // a copy destination as well as a kernel output. Without the flag the
    // layer-frame path fails validation at the first token — which is how
    // the release run caught it, and a unit test could not have.
    let folded = frame_buf_t(c, 42, tok, dim * 4, true);
    let hpost = frame_buf_t(c, 43, tok, hc * 4, true);
    let hcomb = frame_buf_t(c, 44, tok, hc * hc * 4, true);
    let x2 = frame_buf_t(c, 45, tok, dim * 4, true);
    let state2 = frame_buf(c, 46, hc * dim * 4, false);
    let logit_b = frame_buf(c, 47, n_pack * 4, false);
    let msel = frame_buf(c, 19, slots * 4, false);
    let mwt = frame_buf(c, 20, slots * 4, false);
    let mcnt = frame_buf(c, 21, 4, false);
    let mact = frame_buf(c, 22, slots * m.inter * 4, false);
    let mo = frame_buf(c, 24, dim * 4, false);
    let qr2 = frame_buf(c, 48, a.q_lora * 4, false);
    let qn2 = frame_buf(c, 49, a.q_lora * 4, false);

    // Eight words now: the fifth says whether the fold also norms.
    let hcp = uniform_u32x8(
        c,
        [hc as u32, dim as u32, g.sinkhorn_iters as u32, g.hc_eps.to_bits(), 0, 0, 0, 0],
    );
    let hcp_n = uniform_u32x8(
        c,
        [hc as u32, dim as u32, g.sinkhorn_iters as u32, g.hc_eps.to_bits(), 1, 0, 0, 0],
    );

    // ── the layer's own preparation, when it owns it ──
    // `folded` is the previous frame's output and this token's hidden state;
    // for layer zero the caller seeded it.
    let m_attend = match prep {
        None => idxs.len(),
        Some(p) => {
            if dsv4_skip("prep") { return None; }
            let Some(n) = dsv4_encode_prep(
                // `x2` and NOT `folded`: the previous frame's tail leaves
                // the next layer's NORMED input there, which is exactly what
                // the host used to hand in. `folded` is a mid-frame scratch
                // and holds nothing yet at this point.
                model, p, kv_id, li, &x2, &qnb, &cache, &ixb, a.hd, p.window,
                dim, a.rd, a.eps, pos, inv_freq, enc,
            ) else {
                no!("подготовка слоя не собралась");
            };
            if n == 0 || n > 1024 {
                no!("список позиций длиной {n}");
            }
            n
        }
    };

    // ── attention half (the fold for it was prepared by the previous frame) ──
    if !dsv4_skip("attn") {
        encode_attn_chain(c, enc, &wb, &qnb, &q, &attn, &mid, &ao, &cache, &ixb,
                          &sink, &freq, &posb, a, kv_id, li, m_attend, q_ready);
    }

    // ── glue: expand, then the FFN half's fold and norm ──
    // Everything from the attention output to the next layer's input is one
    // chain of dependent dispatches with no copy in the middle, so it is ONE
    // pass. Twelve passes' worth of driver bookkeeping a layer went here,
    // and on a small layer that bookkeeping IS the token.
    let mut copy_qn = false;
    {
        let mut pass = begin_pass(enc);
        // Four dispatches — expand, mix, fold, norm — in one. `hc_fuse()`
        // reverts to the four for a bisect.
        if hc_fuse() {
            encode_hc_block_p(&mut pass, c, &ao, &state, &hpost, &hcomb, &ffn_fn, &ffn_sc,
                &ffn_bs, &ffn_nw, &state2, &folded, &x2, hc, dim, mix_hc, g.sinkhorn_iters,
                a.eps, (172, kv_id, lk));
        } else {
        if !dsv4_skip("hc") {
        if !dsv4_skip("hcexp") {
        encode_hc_expand_k_p(&mut pass, c, &ao, &state, &hpost, &hcomb, &state2, &hcp, hc, dim,
            (120, kv_id, lk));
        }
        if !dsv4_skip("hcmix") {
        encode_f32matvec_w_p(&mut pass, c, &ffn_fn, &state2, &mixes, mix_hc, hc * dim,
            (121, kv_id, lk));
        }
        // The fold norms too: one dispatch, not two.
        if !dsv4_skip("hcfold") {
        encode_hc_fold_k_p(&mut pass, c, &state2, &mixes, &ffn_sc, &ffn_bs, &folded, &hpost,
            &hcomb, &hcp_n, Some((&ffn_nw, &x2)), (122, kv_id, lk));
        }
        }
        }

        // ── MoE half ──
        encode_f32matvec_w_p(&mut pass, c, &router, &x2, &logit_b, n_pack, m.hidden,
            (123, kv_id, lk));
        if !dsv4_skip("moe") {
            encode_moe_chain_p(&mut pass, c, &logit_b, &x2, &msel, &mwt, &mcnt, &mact, &mo,
                               &gate_all, &up_all, &down_all, w, m, n_pack, slots,
                               (kv_id, li));
        }

        // ── expand, then prepare the NEXT layer ──
        let fused_next = hc_fuse() && w.hc_next_fn.is_some();
        if !fused_next {
            encode_hc_expand_k_p(&mut pass, c, &mo, &state2, &hpost, &hcomb, &state, &hcp,
                hc, dim, (124, kv_id, lk));
        }
        if let Some(nf) = w.hc_next_fn {
            let nfn = const_buf(c, bytemuck::cast_slice(nf));
            let nsc = const_buf(c, bytemuck::cast_slice(w.hc_next_scale));
            let nbs = const_buf(c, bytemuck::cast_slice(&w.hc_next_base[..mix_hc]));
            if fused_next {
                // The same four steps as the FFN half, for the next layer's
                // attention: expand the MoE output into the state, mix, fold,
                // norm. The expand above is folded in here, which is why it
                // is skipped when this branch runs.
                encode_hc_block_p(&mut pass, c, &mo, &state2, &hpost, &hcomb, &nfn, &nsc,
                    &nbs, &next_nw, &state, &folded, &x2, hc, dim, mix_hc, g.sinkhorn_iters,
                    a.eps, (174, kv_id, lk));
            } else {
            encode_f32matvec_w_p(&mut pass, c, &nfn, &state, &mixes, mix_hc, hc * dim,
                (125, kv_id, lk));
            encode_hc_fold_k_p(&mut pass, c, &state, &mixes, &nsc, &nbs, &folded, &hpost,
                &hcomb, &hcp_n, Some((&next_nw, &x2)), (126, kv_id, lk));
            }
            // The next layer's LoRA vector, but only when the host is not
            // going to hand it over anyway — the indexer needs `qr` there, so
            // today it projects it regardless and computing it twice is waste.
            if qn.is_none() && !defer_next_q && !dsv4_skip("nextq") {
                encode_q4tp_mvw_p(&mut pass, c, &wb[4], &x2, &qr2, a.q_lora, dim,
                    (52, kv_id, lk));
                encode_rmsnorm_p(&mut pass, c, &qr2, &next_qn, &qn2, a.q_lora, a.eps,
                    (53, kv_id, lk));
                copy_qn = true;
            }
        }
    }
    // Outside the pass: a buffer-to-buffer copy cannot be recorded inside one.
    if copy_qn {
        enc.copy_buffer_to_buffer(&qn2, 0, &qnb, 0, (a.q_lora * 4) as u64);
    }

    // The next layer's input. It stays here: `folded` is where the next
    // frame reads its hidden state from, so a chain of layers needs one
    // copy and no round trip. The wrapper below reads it when a caller
    // still wants the value on the host.
    let src = if w.hc_next_fn.is_some() { &x2 } else { &ao };
    enc.copy_buffer_to_buffer(src, 0, &folded, 0, (dim * 4) as u64);
    Some(folded)
}

/// Make sure a layer's cache exists and is at least `cap` floats.
///
/// The host path got this for free: it rewrote the whole cache every token
/// through `dsv4_cache_write`, which creates and grows. A chained layer
/// never calls that — it appends on the card — so without this its buffer is
/// whatever some earlier token happened to size it to, and the compressed
/// region walks off the end as the sequence lengthens.
pub fn dsv4_cache_ensure(kv_id: u64, li: usize, cap: usize) -> bool {
    let Some(c) = ctx() else { return false };
    if (cap * 4) as u64 > c.device.limits().max_storage_buffer_binding_size as u64 {
        return false;
    }
    let mut map = c.dsv4_kv.lock().unwrap();
    // Growing must CARRY the contents. The host path rewrites the whole
    // cache every token, so drop-and-recreate cost it nothing; the chain
    // owns the contents on the device, and dropping the buffer there wipes
    // the window and every compressed entry the sequence has accumulated —
    // a drift that grows with length and never fails loudly.
    if let Some((old, have)) = map.get(&(kv_id, li)).map(|(b, h)| (b.clone(), *h)) {
        if have < cap {
            let bigger = c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("dsv4-kv"),
                size: (cap * 4) as u64,
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_DST
                    | wgpu::BufferUsages::COPY_SRC,
                mapped_at_creation: false,
            });
            let mut enc = c
                .device
                .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                    label: Some("dsv4-kv-grow"),
                });
            enc.copy_buffer_to_buffer(&old, 0, &bigger, 0, (have * 4) as u64);
            submit(c, enc.finish());
            map.insert((kv_id, li), (bigger, cap));
            GREW.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        }
        return true;
    }
    map.insert(
        (kv_id, li),
        (
            c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("dsv4-kv"),
                size: (cap * 4) as u64,
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_DST
                    | wgpu::BufferUsages::COPY_SRC,
                mapped_at_creation: false,
            }),
            cap,
        ),
    );
    true
}

/// Seed the buffer a chain's FIRST layer reads its hidden state from.
///
/// Every later layer finds it there because the previous frame's tail wrote
/// it. Layer zero has no previous frame — the same hole that once put
/// garbage into `post`/`comb` and cost a perplexity of 1470. Seed it, or the
/// chain starts on whatever the last token left behind.
/// Seed only the fold: the device's qn is already this layer's.
pub fn dsv4_chain_seed_fold(x: &[f32]) -> bool {
    let Some(c) = ctx() else { return false };
    let b = frame_buf(c, 45, x.len() * 4, true);
    c.queue.write_buffer(&b, 0, bytemuck::cast_slice(x));
    true
}

/// Seed one token of a batch: its opening fold and its LoRA vector.
pub fn dsv4_chain_seed_t(x: &[f32], qn: &[f32], tok: usize) -> bool {
    let Some(c) = ctx() else { return false };
    let b = frame_buf_t(c, 45, tok, x.len() * 4, true);
    c.queue.write_buffer(&b, 0, bytemuck::cast_slice(x));
    let q = frame_buf_t(c, 4, tok, qn.len() * 4, true);
    c.queue.write_buffer(&q, 0, bytemuck::cast_slice(qn));
    true
}

pub fn dsv4_chain_seed(x: &[f32], qn: &[f32]) -> bool {
    let Some(c) = ctx() else { return false };
    let b = frame_buf(c, 45, x.len() * 4, true);
    c.queue.write_buffer(&b, 0, bytemuck::cast_slice(x));
    // The LoRA vector too: every frame leaves the NEXT layer's there, and
    // layer zero has no frame before it. Passing it as `qn` instead would
    // stop that frame computing the one after — the tail is guarded on
    // `qn.is_none()`.
    let q = frame_buf(c, 4, qn.len() * 4, true);
    c.queue.write_buffer(&q, 0, bytemuck::cast_slice(qn));
    true
}

/// Encode a run of consecutive layers into ONE encoder and submit it once,
/// returning the last layer's folded output on the host.
///
/// This is the point of the whole exercise: 43 layers used to cost 86
/// submissions and 43 round trips, because the host had to see each layer's
/// output to prepare the next one's attention inputs. It does not any more.
#[allow(clippy::too_many_arguments)]
/// A whole BATCH of known tokens through a run of layers, in one submission.
///
/// The layer is the outer loop and the token the inner one, which is the
/// only ordering that works: token t's attention has to see the window and
/// the compressed entries that tokens before it in the same batch just
/// wrote, and a compute pass orders its dispatches, so encoding them in that
/// order is enough — no fence, no readback between them.
///
/// Nothing here is coupled through CONTENT. A token's prep is the layer's
/// static weights plus counts, and the counts follow from the position: the
/// window fills by one a token to its capacity, and a compressed entry
/// appears every `ratio` tokens. So the whole batch's preps are known before
/// the first dispatch is encoded — which is what lets this be one
/// submission rather than B of them.
///
/// `folded_out` takes `batch * dim`: each token's fold for the head, in
/// order. The head is the caller's business — it is one q4tp matvec with a
/// batch, which the pair already does.
#[allow(clippy::too_many_arguments)]
pub fn dsv4_chain_batch(
    model: &Arc<CmfModel>,
    layers: &[(Dsv4LayerW<'_>, Dsv4LayerGeom, Dsv4Prep<'_>)],
    kv_id: u64,
    first_li: usize,
    inv_freq: &[&[f32]],
    pos: usize,
    batch: usize,
    // Hash layers force their expert list from the TOKEN's id, so the layer
    // description itself varies across a batch — not just the prep. One row
    // per token, each as long as `layers`; None where the layer does not hash.
    forced: Option<&[Vec<Option<Vec<usize>>>]>,
    folded_out: &mut [f32],
    // Optional hyper-connection state for every token, laid out
    // [batch, hc, dim]. A device prefix followed by a host layer needs it;
    // bringing it home beside the folds still costs one fence.
    state_out: Option<&mut [f32]>,
) -> bool {
    let Some(c) = ctx() else { return false };
    let Some((_, g0, _)) = layers.first() else {
        return false;
    };
    let dim = g0.attn.dim;
    let state_len = batch * g0.hc * dim;
    if batch == 0
        || batch > FRAME_TOK_STRIDE
        || folded_out.len() < batch * dim
        || state_out.as_ref().is_some_and(|s| s.len() < state_len)
        || inv_freq.len() != layers.len()
    {
        return false;
    }
    if bt_frame_on() {
        return dsv4_chain_batch_bt(
            model, layers, kv_id, first_li, inv_freq, pos, batch, forced, folded_out,
            state_out,
        );
    }
    // Grow every index cache to its FINAL batch size before recording the
    // encoder. Growing half way through would copy only the pre-batch data:
    // earlier tokens' appends still live in the unsubmitted encoder and the
    // later tokens would bind a fresh buffer that cannot contain them.
    for (i, (_, _, p)) in layers.iter().enumerate() {
        if let Some((_, cg, _, ixg)) = p.ix.as_ref() {
            let extra = batch.div_ceil(cg.ratio.max(1));
            if dsv4_index_cache(
                kv_id,
                first_li + i,
                ixg.idim * (p.n_ix + extra + 1),
            )
            .is_none()
            {
                return false;
            }
        }
    }
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-chain-batch"),
        });
    let gather = frame_buf(c, 117, batch * dim * 4, false);
    let gather_state = state_out
        .as_ref()
        .map(|_| frame_buf(c, 118, state_len * 4, false));
    let t_enc = std::time::Instant::now();
    for (i, (w, g, p)) in layers.iter().enumerate() {
        // The query projection is independent across the known tokens. Pack
        // their normalized LoRA vectors, run the actual B-axis q4tp kernel,
        // then scatter the heads back to the per-token frames. The previous
        // implementation merely recorded B matvecs in one encoder and was
        // slower than the walk despite calling itself batched.
        let bytes = model.primary_bytes();
        let Some(qe) = model.tensors.get(w.attn.wq_b) else {
            return false;
        };
        let (Some(qabs), qlen) = (model.entry_abs_offset(qe), qe.nbytes as usize) else {
            return false;
        };
        let Some(qw) = weight_buffer(
            c,
            (model.uid() as usize, w.attn.wq_b),
            &bytes[qabs..qabs + qlen],
        ) else {
            return false;
        };
        let qn_pack = frame_buf(c, 119, batch * g.attn.q_lora * 4, false);
        let q_pack = frame_buf(c, 120, batch * g.attn.nh * g.attn.hd * 4, false);
        {
            let mut pass = begin_pass(&mut enc);
            for t in 0..batch {
                let qn = frame_buf_t(c, 4, t, g.attn.q_lora * 4, true);
                encode_blit_p(
                    &mut pass,
                    c,
                    &qn,
                    &qn_pack,
                    g.attn.q_lora,
                    0,
                    t * g.attn.q_lora,
                    None,
                );
            }
        }
        if !encode_q4tp_mv4_b(
            c,
            &mut enc,
            &qw,
            &qn_pack,
            &q_pack,
            g.attn.nh * g.attn.hd,
            g.attn.q_lora,
            batch,
        ) {
            return false;
        }
        {
            let mut pass = begin_pass(&mut enc);
            for t in 0..batch {
                let q = frame_buf_t(
                    c,
                    5,
                    FRAME_TOK_STRIDE + t + 1,
                    g.attn.nh * g.attn.hd * 4,
                    false,
                );
                encode_blit_p(
                    &mut pass,
                    c,
                    &q_pack,
                    &q,
                    g.attn.nh * g.attn.hd,
                    t * g.attn.nh * g.attn.hd,
                    0,
                    None,
                );
            }
        }
        for t in 0..batch {
            // The counts as of this token: everything the prep carries that
            // is not a weight.
            let mut pt = p.clone();
            pt.filled = (p.filled + t).min(p.window);
            let advanced = |ratio: usize| -> usize {
                if ratio == 0 {
                    return 0;
                }
                (0..t).filter(|k| (pos + k + 1) % ratio == 0).count()
            };
            if let Some((_, cg)) = p.comp.as_ref() {
                let ew = if cg.overlap { cg.width / 2 } else { cg.width };
                pt.n_comp = p.n_comp + advanced(cg.ratio);
                pt.comp_dst_off = p.comp_dst_off + (pt.n_comp - p.n_comp) * ew;
            }
            if let Some((_, cg, _, _)) = p.ix.as_ref() {
                let ew = if cg.overlap { cg.width / 2 } else { cg.width };
                pt.n_ix = p.n_ix + advanced(cg.ratio);
                pt.ix_dst_off = p.ix_dst_off + (pt.n_ix - p.n_ix) * ew;
            }
            // The layer as this token sees it: identical but for the row a
            // hash layer forces from the token's id.
            let mut wt;
            let w = match forced.and_then(|f| f.get(t)).and_then(|r| r.get(i)) {
                Some(row) => {
                    wt = w.clone();
                    wt.moe.forced = row.as_deref();
                    &wt
                }
                None => w,
            };
            let Some(b) = dsv4_layer_frame_enc(
                model,
                w,
                *g,
                kv_id,
                first_li + i,
                t,
                true,
                true,
                true,
                None,
                &[],
                Some(&pt),
                inv_freq[i],
                pos + t,
                &mut enc,
            ) else {
                return false;
            };
            // The last layer's fold is what the head reads; gather the batch
            // into one buffer so a single readback brings all of it home.
            if i + 1 == layers.len() {
                let mut pass = begin_pass(&mut enc);
                encode_blit_p(&mut pass, c, &b, &gather, dim, 0, t * dim, None);
                if let Some(gs) = gather_state.as_ref() {
                    let sb = frame_buf_t(c, 40, t, g0.hc * dim * 4, true);
                    encode_blit_p(
                        &mut pass,
                        c,
                        &sb,
                        gs,
                        g0.hc * dim,
                        0,
                        t * g0.hc * dim,
                        None,
                    );
                }
            }
        }
        // Likewise, prepare the next device layer's shared LoRA vector once
        // for the batch. A host tail recomputes its own projection, so the
        // final device layer deliberately does not pay for an unused next-q.
        if i + 1 < layers.len() && !dsv4_skip("nextq") {
            let Some(next_idx) = w.next_wq_a else {
                return false;
            };
            let Some(ne) = model.tensors.get(next_idx) else {
                return false;
            };
            let (Some(nabs), nlen) = (model.entry_abs_offset(ne), ne.nbytes as usize) else {
                return false;
            };
            let Some(nw) = weight_buffer(
                c,
                (model.uid() as usize, next_idx),
                &bytes[nabs..nabs + nlen],
            ) else {
                return false;
            };
            let x_pack = frame_buf(c, 121, batch * dim * 4, false);
            let qr_pack = frame_buf(c, 122, batch * g.attn.q_lora * 4, false);
            {
                let mut pass = begin_pass(&mut enc);
                for t in 0..batch {
                    let x = frame_buf_t(c, 45, t, dim * 4, true);
                    encode_blit_p(
                        &mut pass,
                        c,
                        &x,
                        &x_pack,
                        dim,
                        0,
                        t * dim,
                        None,
                    );
                }
            }
            if !encode_q4tp_mv4_b(
                c,
                &mut enc,
                &nw,
                &x_pack,
                &qr_pack,
                g.attn.q_lora,
                dim,
                batch,
            ) {
                return false;
            }
            let norm = const_buf(c, bytemuck::cast_slice(&w.next_q_norm[..g.attn.q_lora]));
            let mut pass = begin_pass(&mut enc);
            for t in 0..batch {
                let qr = frame_buf_t(
                    c,
                    48,
                    FRAME_TOK_STRIDE + t + 1,
                    g.attn.q_lora * 4,
                    false,
                );
                let qn = frame_buf_t(c, 4, t, g.attn.q_lora * 4, true);
                encode_blit_p(
                    &mut pass,
                    c,
                    &qr_pack,
                    &qr,
                    g.attn.q_lora,
                    t * g.attn.q_lora,
                    0,
                    None,
                );
                encode_rmsnorm_p(
                    &mut pass,
                    c,
                    &qr,
                    &norm,
                    &qn,
                    g.attn.q_lora,
                    g.attn.eps,
                    (53, kv_id, 10_000_000 + (first_li + i) * FRAME_TOK_STRIDE + t),
                );
            }
        }
    }
    CHAIN_ENC_NS.fetch_add(
        t_enc.elapsed().as_nanos() as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    match (gather_state.as_ref(), state_out) {
        (Some(gs), Some(states)) => readback2(
            c,
            enc,
            (&gather, &mut folded_out[..batch * dim]),
            (gs, &mut states[..state_len]),
        ),
        _ => {
            let bytes = (batch * dim * 4) as u64;
            let mut sc = c.scratch.lock().unwrap();
            let stage = Scratch::ensure(
                &c.device,
                &mut sc.stage,
                bytes,
                wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
                "dsv4-batch-stage",
            );
            let ok = readback(c, enc, &gather, &stage, bytes, &mut folded_out[..batch * dim]);
            drop(sc);
            ok
        }
    }
}

/// The token-axis batch: one encoder, one frame per LAYER, every dispatch
/// covering all B tokens. The historical path encoded one frame per token
/// and was launch-bound — 204 ms for a 5-wide pass against 37.8 for one
/// token; the token axis exists to put the batch back at one token's
/// dispatch count.
#[allow(clippy::too_many_arguments)]
fn dsv4_chain_batch_bt(
    model: &Arc<CmfModel>,
    layers: &[(Dsv4LayerW<'_>, Dsv4LayerGeom, Dsv4Prep<'_>)],
    kv_id: u64,
    first_li: usize,
    inv_freq: &[&[f32]],
    pos: usize,
    batch: usize,
    forced: Option<&[Vec<Option<Vec<usize>>>]>,
    folded_out: &mut [f32],
    state_out: Option<&mut [f32]>,
) -> bool {
    let Some(c) = ctx() else { return false };
    let Some((_, g0, _)) = layers.first() else {
        return false;
    };
    let dim = g0.attn.dim;
    let state_len = batch * g0.hc * dim;
    // Index caches grow to their final batch size before recording — growing
    // mid-encode would strand the unsubmitted appends (see the per-token
    // path's comment).
    for (i, (_, _, p)) in layers.iter().enumerate() {
        if let Some((_, cg, _, ixg)) = p.ix.as_ref() {
            let extra = batch.div_ceil(cg.ratio.max(1));
            if dsv4_index_cache(kv_id, first_li + i, ixg.idim * (p.n_ix + extra + 1)).is_none()
            {
                return false;
            }
        }
    }
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-chain-batch-bt"),
        });
    // Fresh slots for this chain's sampled passes: the counter rotates
    // globally, and a wrap inside one chain would pair unrelated stamps.
    if c.ts_query.is_some() && !bt_ts_lis().is_empty() {
        TS_SLOT.store(0, std::sync::atomic::Ordering::Relaxed);
        TS_PAIRS.lock().unwrap().clear();
    }
    let t_enc = std::time::Instant::now();
    // `CMF_DSV4_CHAIN_SPLIT=N` (default 4): submit the chain in N pieces so
    // the card starts the first layers while the host still encodes the
    // rest. Same queue, same order — the split changes when work is handed
    // over, never what it computes. N=1 restores the single submission.
    let split_n = {
        static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
        *N.get_or_init(|| {
            std::env::var("CMF_DSV4_CHAIN_SPLIT")
                .ok()
                .and_then(|v| v.parse().ok())
                .filter(|&n| n >= 1)
                .unwrap_or(4)
        })
    };
    let chunk = layers.len().div_ceil(split_n).max(1);
    let mut last: Option<(wgpu::Buffer, wgpu::Buffer)> = None;
    for (i, (w, g, p)) in layers.iter().enumerate() {
        if i > 0 && i % chunk == 0 {
            let full = std::mem::replace(
                &mut enc,
                c.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
                    label: Some("dsv4-chain-batch-bt"),
                }),
            );
            c.queue.submit([full.finish()]);
        }
        // Per-token counts follow from the position alone (window fills by
        // one, a compressed entry appears every `ratio`), so the whole
        // batch's preps are known before anything is encoded.
        let mut preps = Vec::with_capacity(batch);
        for t in 0..batch {
            let mut pt = p.clone();
            pt.filled = (p.filled + t).min(p.window);
            let advanced = |ratio: usize| -> usize {
                if ratio == 0 {
                    return 0;
                }
                (0..t).filter(|k| (pos + k + 1) % ratio == 0).count()
            };
            if let Some((_, cg)) = p.comp.as_ref() {
                let ew = if cg.overlap { cg.width / 2 } else { cg.width };
                pt.n_comp = p.n_comp + advanced(cg.ratio);
                pt.comp_dst_off = p.comp_dst_off + (pt.n_comp - p.n_comp) * ew;
            }
            if let Some((_, cg, _, _)) = p.ix.as_ref() {
                let ew = if cg.overlap { cg.width / 2 } else { cg.width };
                pt.n_ix = p.n_ix + advanced(cg.ratio);
                pt.ix_dst_off = p.ix_dst_off + (pt.n_ix - p.n_ix) * ew;
            }
            preps.push(pt);
        }
        let rows: Option<Vec<Option<Vec<usize>>>> = forced.map(|f| {
            (0..batch)
                .map(|t| f.get(t).and_then(|r| r.get(i)).cloned().flatten())
                .collect()
        });
        // Speculative verify: photograph every layer's per-token hidden
        // input so a partial acceptance can replay the accepted tokens'
        // state appends without recomputing the pass.
        let (retain_n, caps) = SPEC_RETAIN.with(|v| v.borrow().clone());
        if retain_n > 0 && i < retain_n {
            let x2_bt = frame_buf_t(c, BT_X2, 0, batch * dim * 4, true);
            let retain = frame_buf_t(c, BT_RETAIN, 0, retain_n * batch * dim * 4, false);
            let mut pass = begin_pass(&mut enc);
            encode_blit_p(
                &mut pass,
                c,
                &x2_bt,
                &retain,
                batch * dim,
                0,
                i * batch * dim,
                None,
            );
        }
        let Some(out) = dsv4_layer_frame_bt_enc(
            model,
            w,
            *g,
            kv_id,
            first_li + i,
            batch,
            &preps,
            rows.as_deref(),
            inv_freq[i],
            pos,
            &mut enc,
        ) else {
            return false;
        };
        // The draft's capture: photograph the post-layer state of the armed
        // target layers, every token, for the host to read after acceptance.
        if let Some(slot) = caps.iter().position(|&t| t == first_li + i) {
            let hcd = g0.hc * dim;
            let cap = frame_buf_t(c, BT_CAP, 0, caps.len() * batch * hcd * 4, false);
            let mut pass = begin_pass(&mut enc);
            encode_blit_p(
                &mut pass,
                c,
                &out.1,
                &cap,
                batch * hcd,
                0,
                slot * batch * hcd,
                None,
            );
        }
        last = Some(out);
    }
    let Some((folds, states)) = last else {
        return false;
    };
    // Blit, not copy_buffer_to_buffer: the pooled gather buffers carry no
    // COPY_DST (the per-token path fills them with the same kernel).
    let gather = frame_buf(c, 117, batch * dim * 4, false);
    let gather_state = state_out
        .as_ref()
        .map(|_| frame_buf(c, 118, state_len * 4, false));
    {
        let mut pass = begin_pass(&mut enc);
        encode_blit_p(&mut pass, c, &folds, &gather, batch * dim, 0, 0, None);
        if let Some(gs) = gather_state.as_ref() {
            encode_blit_p(&mut pass, c, &states, gs, state_len, 0, 0, None);
        }
    }
    CHAIN_ENC_NS.fetch_add(
        t_enc.elapsed().as_nanos() as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    // Resolve the sampled layers' pass stamps into the staging buffer; the
    // frame's own fence below pays for the round trip.
    let ts_pairs: Vec<(usize, u32)> = if !bt_ts_lis().is_empty() {
        std::mem::take(&mut TS_PAIRS.lock().unwrap())
    } else {
        Vec::new()
    };
    if let (false, Some((qs, resolve, tstage))) = (ts_pairs.is_empty(), c.ts_query.as_ref()) {
        for (n, (_, slot)) in ts_pairs.iter().enumerate() {
            if (n as u64 + 1) * 16 > 256 * 8 {
                break;
            }
            enc.resolve_query_set(qs, *slot..*slot + 2, resolve, 0);
            enc.copy_buffer_to_buffer(resolve, 0, tstage, (n as u64) * 16, 16);
        }
    }
    let ok = match (gather_state.as_ref(), state_out) {
        (Some(gs), Some(states_out)) => readback2(
            c,
            enc,
            (&gather, &mut folded_out[..batch * dim]),
            (gs, &mut states_out[..state_len]),
        ),
        _ => {
            let bytes = (batch * dim * 4) as u64;
            let mut sc = c.scratch.lock().unwrap();
            let stage = Scratch::ensure(
                &c.device,
                &mut sc.stage,
                bytes,
                wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
                "dsv4-batch-stage",
            );
            let ok = readback(c, enc, &gather, &stage, bytes, &mut folded_out[..batch * dim]);
            drop(sc);
            ok
        }
    };
    if ok && !ts_pairs.is_empty() {
        if let Some((_, _, tstage)) = &c.ts_query {
            let n_read = ts_pairs.len().min(128);
            let bytes = (n_read as u64) * 16;
            let (tx, rx) = std::sync::mpsc::channel();
            tstage.map_async(wgpu::MapMode::Read, ..bytes, move |r| {
                let _ = tx.send(r);
            });
            let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
            if rx.recv().map(|r| r.is_ok()).unwrap_or(false) {
                if let Ok(raw) = tstage.get_mapped_range(..bytes) {
                    let t: &[u64] = bytemuck::cast_slice(&raw);
                    let mut agg = [(0f64, 0u32); BT_TS_NAMES.len()];
                    for (n, (which, _)) in ts_pairs.iter().enumerate().take(n_read) {
                        let d = t[2 * n + 1].saturating_sub(t[2 * n]);
                        let ms = d as f64 * c.ts_period as f64 / 1e6;
                        if let Some(e) = agg.get_mut(*which) {
                            e.0 += ms;
                            e.1 += 1;
                        }
                    }
                    drop(raw);
                    let n_lis = bt_ts_lis().len().max(1) as f64;
                    let line: Vec<String> = agg
                        .iter()
                        .enumerate()
                        .filter(|(_, e)| e.1 > 0)
                        .map(|(i, e)| {
                            format!("{} {:.2}({})", BT_TS_NAMES[i], e.0 / n_lis, e.1)
                        })
                        .collect();
                    eprintln!(
                        "[bt-ts] на слой ({} слоёв): {}",
                        bt_ts_lis().len(),
                        line.join(" | ")
                    );
                }
            }
            tstage.unmap();
        }
    }
    ok
}

/// `CMF_DSV4_BT_HCSPLIT=0`: the batch frame joins the halves with the
/// one-workgroup fused block instead of the three-dispatch
/// expand→mix→fold sequence. The fuse saves two links but serializes the
/// mix's 1.5 MB of weights through one SM per token — measured on the
/// release at B=5: fused 2.98 ms of the layer, split 1.15; the chain went
/// 187 → 110 ms. Split is the default; the fuse stays for a bisect.
/// `CMF_DSV4_OLORA_A4=0`: the grouped projection back on the shared-ladder
/// kernel. The four-row register twin computes the same sums in the same
/// order without the ladder's barriers.
fn ol_a4() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_OLORA_A4").map(|v| v != "0").unwrap_or(true))
}

fn bt_hc_split() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_BT_HCSPLIT").map(|v| v != "0").unwrap_or(true))
}

/// `CMF_DSV4_DN=b|b2|split|b4` picks the batch frame's down-projection
/// kernel. b4 (default) gives each workgroup four rows so the x span
/// loads once for four weight tiles — the one-row kernel is L2-bound on
/// exactly that traffic. split keeps per-slot partials + an ascending
/// sum (round-off class); b/b2 are the one-row originals for a bisect.
fn bt_dn_mode() -> u8 {
    static M: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
    *M.get_or_init(|| match std::env::var("CMF_DSV4_DN").as_deref() {
        Ok("b") => 0,
        Ok("b2") => 1,
        Ok("split") => 2,
        _ => 3,
    })
}

/// `CMF_DSV4_COMPFOLD=1`: the fold token as ONE fused dispatch instead of
/// the seven-dispatch per-token step. Bit-exact on every toy and on
/// chunked-prefill perplexity, −4 ms off the verify chain — and OFF by
/// default, because on the release bench the DRAFT's proposals diverge
/// two rounds after an indexer fold inside a verify pass (trace: fed
/// differs at spec@97 while both argmaxes agree) and acceptance pays 5
/// points, more than the chain saves. The verify-only difference is not
/// found yet; the trace and the suspects live in the campaign notes.
fn bt_comp_fold_on() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_COMPFOLD").is_ok_and(|v| v != "0"))
}

/// Whether the batch uses the token-axis frame (default) or the historical
/// one-frame-per-token encoding (`CMF_DSV4_BT=0`, kept for a bisect).
fn bt_frame_on() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_BT").map(|v| v != "0").unwrap_or(true))
}

// ── the batched frame's buffer tags (frame_buf, tok 0) ──
// Working buffers are sized batch*len, so a different batch width lands in a
// different pool entry by length; bind groups additionally salt their key
// with the batch. Registry: frame_buf 180–202, cached_bind 180–199.
const BT_STATE: u8 = 180;
const BT_X2: u8 = 181;
const BT_FOLD: u8 = 182;
const BT_QN: u8 = 183;
const BT_Q: u8 = 184;
const BT_ATTN: u8 = 185;
const BT_MID: u8 = 186;
const BT_AO: u8 = 187;
const BT_MIXES: u8 = 188;
const BT_HPOST: u8 = 189;
const BT_HCOMB: u8 = 190;
const BT_STATE2: u8 = 191;
const BT_LOGIT: u8 = 192;
const BT_MSEL: u8 = 193;
const BT_MWT: u8 = 194;
const BT_MCNT: u8 = 195;
const BT_MACT: u8 = 196;
const BT_MO: u8 = 197;
const BT_QR: u8 = 198;
const BT_ROPE_META: u8 = 199;
const BT_SA_M: u8 = 200;
const BT_IDX: u8 = 201;
const BT_FORCED: u8 = 202;

/// Seed one token of the batched frame: its hyper-connection state, the
/// attention half's post/comb (computed on the host for layer zero), its
/// opening fold and its LoRA vector. The strided twin of
/// `dsv4_state_write_t` + `dsv4_hc_write_t` + `dsv4_chain_seed_t`.
pub fn dsv4_chain_seed_bt(
    tok: usize,
    batch: usize,
    state: &[f32],
    post: &[f32],
    comb: &[f32],
    fold: &[f32],
    qn: &[f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let up = |tag: u8, len: usize, off: usize, data: &[f32]| {
        let b = frame_buf_t(c, tag, 0, batch * len * 4, true);
        c.queue
            .write_buffer(&b, (off * len * 4) as u64, bytemuck::cast_slice(data));
    };
    up(BT_STATE, state.len(), tok, state);
    up(BT_HPOST, post.len(), tok, post);
    up(BT_HCOMB, comb.len(), tok, comb);
    up(BT_X2, fold.len(), tok, fold);
    up(BT_QN, qn.len(), tok, qn);
    true
}

const BT_RETAIN: u8 = 204;
const BT_SPEC_SCRATCH: u8 = 205;
const BT_DSPARK_IDX: u8 = 207;
const BT_DSPARK_META: u8 = 208;
const BT_DSPARK_KV: u8 = 209;

/// One DSpark stage as the graph consumes it: directory indices for the
/// quantized weights, host slices for the small f32 pieces (all living in
/// the pack or the layer — address-stable, so `const_buf` keying is sound).
pub struct DsparkStageW<'a> {
    pub wq_a: usize,
    pub wq_b: usize,
    pub wo_a: usize,
    pub wo_b: usize,
    pub wkv: usize,
    pub q_norm: &'a [f32],
    pub kv_norm: &'a [f32],
    pub attn_norm: &'a [f32],
    pub ffn_norm: &'a [f32],
    pub sink: &'a [f32],
    pub hc_attn_fn: &'a [f32],
    pub hc_attn_scale: &'a [f32],
    pub hc_attn_base: &'a [f32],
    pub hc_ffn_fn: &'a [f32],
    pub hc_ffn_scale: &'a [f32],
    pub hc_ffn_base: &'a [f32],
    pub router: &'a [f32],
    pub bias: Option<&'a [f32]>,
    pub experts: &'a [(usize, usize, usize)],
    /// Resident bitmap over ALL experts (the router still ranks them all).
    pub mask_u32: &'a [u32],
    /// Global id → pack slot, u32, 0xFFFFFFFF where cold (never chosen —
    /// the mask forbids it).
    pub map_u32: &'a [u32],
}

#[derive(Clone, Copy)]
pub struct DsparkGeom {
    pub dim: usize,
    pub hc: usize,
    pub nh: usize,
    pub hd: usize,
    pub rd: usize,
    pub q_lora: usize,
    pub o_lora: usize,
    pub o_groups: usize,
    pub inter: usize,
    pub n_experts: usize,
    pub top_k: usize,
    pub window: usize,
    pub eps: f32,
    pub hc_eps: f32,
    pub sinkhorn_iters: usize,
    pub route_scale: f32,
    pub swiglu_limit: f32,
    pub scale: f32,
    pub gu_q2: bool,
    pub dn_q2: bool,
}

/// The five-position DSpark block, whole, in ONE submission.
///
/// Every dispatch carries the block on a grid axis; the stage rings live on
/// the device under `(kv_id, 1000 + stage)`; the block's own keys occupy
/// rows `[window, window+block)` of the same buffer and are rewritten
/// every call. The draft does not owe the walk bit-exactness — a worse draft
/// costs acceptance, never correctness — so every kernel here is the fast
/// batched twin. Returns the last stage's states, `block * hc * dim`.
#[allow(clippy::too_many_arguments)]
pub fn dspark_graph(
    model: &Arc<CmfModel>,
    stages: &[DsparkStageW<'_>],
    g: DsparkGeom,
    kv_id: u64,
    main_proj: usize,
    main_norm: &[f32],
    mh: &[f32],
    states0: &[f32],
    pos: usize,
    filled: usize,
    inv_freq: &[f32],
    block: usize,
    states_out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let (hc, dim) = (g.hc, g.dim);
    let mix_hc = (2 + hc) * hc;
    if states0.len() < block * hc * dim || states_out.len() < block * hc * dim {
        return false;
    }
    let bytes = model.primary_bytes();
    let wbuf = |idx: usize| -> Option<wgpu::Buffer> {
        let e = model.tensors.get(idx)?;
        let abs = model.entry_abs_offset(e)?;
        weight_buffer(
            c,
            (model.uid() as usize, idx),
            &bytes[abs..abs + e.nbytes as usize],
        )
    };
    let dk = |si: usize| 100_000 + si; // the graph's li-namespace
    let fb = |tag: u8, len: usize, upload: bool| frame_buf_t(c, tag, 0, block * len * 4, upload);
    let state_bt = fb(BT_STATE, hc * dim, true);
    let x2_bt = fb(BT_X2, dim, true);
    let fold_bt = fb(BT_FOLD, dim, false);
    let mixes_bt = fb(BT_MIXES, mix_hc, false);
    let hpost_bt = fb(BT_HPOST, hc, true);
    let hcomb_bt = fb(BT_HCOMB, hc * hc, true);
    let state2_bt = fb(BT_STATE2, hc * dim, false);
    let q_bt = fb(BT_Q, g.nh * g.hd, false);
    let attn_bt = fb(BT_ATTN, g.nh * g.hd, false);
    let mid_bt = fb(BT_MID, g.o_groups * g.o_lora, false);
    let ao_bt = fb(BT_AO, dim, false);
    let qr_bt = fb(BT_QR, g.q_lora, false);
    let logit_bt = fb(BT_LOGIT, g.n_experts, false);
    let slots = g.top_k + 1;
    let msel_bt = fb(BT_MSEL, slots, false);
    let mwt_bt = fb(BT_MWT, slots, false);
    let mcnt_bt = fb(BT_MCNT, 1, false);
    let mact_bt = fb(BT_MACT, slots * g.inter, false);
    let mo_bt = fb(BT_MO, dim, false);
    let cold_bt = fb(203, 4 * g.top_k, false);
    // The stage rings must exist and hold window + block rows.
    for si in 0..stages.len() {
        if !dsv4_cache_ensure(kv_id, dk(si), (g.window + block) * g.hd) {
            return false;
        }
    }
    // Whole-call uploads: initial states, captures, the attended list and
    // the per-position rope table. One submission per draft call means one
    // write each — no write-before-submit aliasing.
    c.queue
        .write_buffer(&state_bt, 0, bytemuck::cast_slice(&states0[..block * hc * dim]));
    let mh_buf = frame_buf_t(c, BT_DSPARK_KV, 0, mh.len() * 4, true);
    c.queue.write_buffer(&mh_buf, 0, bytemuck::cast_slice(mh));
    let m_attend = filled + block;
    if m_attend > 1024 {
        return false;
    }
    let idx: Vec<u32> = (0..filled as u32)
        .chain(g.window as u32..(g.window + block) as u32)
        .collect();
    let idx_b = frame_buf_t(c, BT_DSPARK_IDX, 0, 1024 * 4, true);
    c.queue.write_buffer(&idx_b, 0, bytemuck::cast_slice(&idx));
    let ms = vec![m_attend as u32; block];
    let sa_m = frame_buf_t(c, BT_DSPARK_META, 0, block * 4, true);
    c.queue.write_buffer(&sa_m, 0, bytemuck::cast_slice(&ms));
    let metas: Vec<f32> = (0..block)
        .flat_map(|i| [(pos + 1 + i) as f32, g.eps])
        .collect();
    let rope_meta = fb(BT_ROPE_META, 2, true);
    c.queue
        .write_buffer(&rope_meta, 0, bytemuck::cast_slice(&metas));
    let tip_meta = frame_up_pos_t(c, 1, FRAME_TOK_STRIDE - 1, pos, g.eps);

    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dspark-graph"),
        });
    let freq = const_buf(c, bytemuck::cast_slice(&inv_freq[..g.rd / 2]));
    let hcp = uniform_u32x8(
        c,
        [hc as u32, dim as u32, g.sinkhorn_iters as u32, g.hc_eps.to_bits(), 0, 0, 0, 0],
    );
    let hcp_n = uniform_u32x8(
        c,
        [hc as u32, dim as u32, g.sinkhorn_iters as u32, g.hc_eps.to_bits(), 1, mix_hc as u32, 0, 0],
    );

    // ── the tip's ring entry: main_x = main_norm(main_proj(captures)),
    //    then each stage's kv row at slot pos % window. ──
    let Some(mp_w) = wbuf(main_proj) else { return false };
    let main_raw = frame_buf_t(c, BT_DSPARK_KV, 9, dim * 4, false);
    let main_x = frame_buf_t(c, BT_DSPARK_KV, 10, dim * 4, false);
    {
        let mnw = const_buf(c, bytemuck::cast_slice(main_norm));
        let mut pass = begin_pass(&mut enc);
        encode_q4tp_mvw_p(&mut pass, c, &mp_w, &mh_buf, &main_raw, dim, mh.len(), (231, kv_id, 0));
        encode_rmsnorm_p(&mut pass, c, &main_raw, &mnw, &main_x, dim, g.eps, (232, kv_id, 0));
    }
    for (si, s) in stages.iter().enumerate() {
        let Some(wkv_w) = wbuf(s.wkv) else { return false };
        let cache = {
            let map = c.dsv4_kv.lock().unwrap();
            match map.get(&(kv_id, dk(si))) {
                Some((b, _)) => b.clone(),
                None => return false,
            }
        };
        let kvw = {
            let Some(e) = model.tensors.get(s.wkv) else { return false };
            e.shape[0]
        };
        let kv_raw = frame_buf_t(c, BT_DSPARK_KV, 11, kvw * 4, false);
        let kv_row = frame_buf_t(c, BT_DSPARK_KV, 12 + si, kvw * 4, false);
        let knw = const_buf(c, bytemuck::cast_slice(s.kv_norm));
        let mut pass = begin_pass(&mut enc);
        encode_q4tp_mvw_p(&mut pass, c, &wkv_w, &main_x, &kv_raw, kvw, dim, (233, kv_id, dk(si)));
        encode_rmsnorm_p(&mut pass, c, &kv_raw, &knw, &kv_row, kvw, g.eps, (234, kv_id, dk(si)));
        encode_rope_heads_p(&mut pass, c, &kv_row, &freq, &tip_meta, 1, kvw, g.rd, false, false,
            (235, kv_id, dk(si)));
        encode_blit_p(&mut pass, c, &kv_row, &cache, g.hd, kvw - g.hd,
            (pos % g.window) * g.hd, None);
    }

    // ── the block through the three stages ──
    for (si, s) in stages.iter().enumerate() {
        let li = dk(si);
        let bk = |tag: u8| (tag, kv_id, li);
        let (Some(wq_a), Some(wq_b), Some(wo_a), Some(wo_b), Some(wkv_w)) =
            (wbuf(s.wq_a), wbuf(s.wq_b), wbuf(s.wo_a), wbuf(s.wo_b), wbuf(s.wkv))
        else {
            return false;
        };
        let Some((gate_all, up_all, down_all)) = (if g.gu_q2 {
            moe_expert_bufs_requant_gu(model, s.experts, g.inter, dim)
        } else {
            moe_expert_bufs(c, model, s.experts, g.inter, dim, true, false, false)
        }) else {
            return false;
        };
        let cache = {
            let map = c.dsv4_kv.lock().unwrap();
            match map.get(&(kv_id, li)) {
                Some((b, _)) => b.clone(),
                None => return false,
            }
        };
        let kvw = {
            let Some(e) = model.tensors.get(s.wkv) else { return false };
            e.shape[0]
        };
        let a_fn = const_buf(c, bytemuck::cast_slice(s.hc_attn_fn));
        let a_sc = const_buf(c, bytemuck::cast_slice(s.hc_attn_scale));
        let a_bs = const_buf(c, bytemuck::cast_slice(s.hc_attn_base));
        let a_nw = const_buf(c, bytemuck::cast_slice(s.attn_norm));
        let f_fn = const_buf(c, bytemuck::cast_slice(s.hc_ffn_fn));
        let f_sc = const_buf(c, bytemuck::cast_slice(s.hc_ffn_scale));
        let f_bs = const_buf(c, bytemuck::cast_slice(s.hc_ffn_base));
        let f_nw = const_buf(c, bytemuck::cast_slice(s.ffn_norm));
        let qnw = const_buf(c, bytemuck::cast_slice(s.q_norm));
        let knw = const_buf(c, bytemuck::cast_slice(s.kv_norm));
        let sink = const_buf(c, bytemuck::cast_slice(s.sink));
        let router = const_buf(c, bytemuck::cast_slice(s.router));
        let mask_b = const_buf(c, bytemuck::cast_slice(s.mask_u32));
        let map_b = const_buf(c, bytemuck::cast_slice(s.map_u32));
        let n_res = s.experts.len() - 1;

        let mixfold = |pass: &mut wgpu::ComputePass<'_>, t1: u8, t2: u8, st: &wgpu::Buffer,
                       fnw: &wgpu::Buffer, sc: &wgpu::Buffer, bs: &wgpu::Buffer,
                       nw: &wgpu::Buffer| {
            let bind = cached_bind(c, bk(t1), || {
                let p = uniform_u32x4(c, [(hc * dim) as u32, mix_hc as u32, 0, 0]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_f32_matvec_x.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, fnw),
                        bind_buf(1, st),
                        bind_buf(2, &mixes_bt),
                        bind_buf(3, &p),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_f32_matvec_x);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(mix_hc as u32, block as u32, 1);
            let bind = cached_bind(c, bk(t2), || {
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_hc_pre_fold.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, st),
                        bind_buf(1, &mixes_bt),
                        bind_buf(2, sc),
                        bind_buf(3, bs),
                        bind_buf(4, &fold_bt),
                        bind_buf(5, &hpost_bt),
                        bind_buf(6, &hcomb_bt),
                        bind_buf(7, &hcp_n),
                        bind_buf(8, nw),
                        bind_buf(9, &x2_bt),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_hc_pre_fold);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(block as u32, 1, 1);
        };
        let expand = |pass: &mut wgpu::ComputePass<'_>, tag: u8, x: &wgpu::Buffer,
                      res: &wgpu::Buffer, out: &wgpu::Buffer| {
            let bind = cached_bind(c, bk(tag), || {
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_hc_post_expand.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, x),
                        bind_buf(1, res),
                        bind_buf(2, &hpost_bt),
                        bind_buf(3, &hcomb_bt),
                        bind_buf(4, out),
                        bind_buf(5, &hcp),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_hc_post_expand);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(((hc * dim) as u32).div_ceil(256), block as u32, 1);
        };
        let rms_b = |pass: &mut wgpu::ComputePass<'_>, tag: u8, x: &wgpu::Buffer,
                     w: &wgpu::Buffer, o: &wgpu::Buffer, n: usize| {
            let bind = cached_bind(c, bk(tag), || {
                let p = uniform_u32x4(c, [n as u32, 0, g.eps.to_bits(), 0]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.rmsnorm_b.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, x),
                        bind_buf(1, w),
                        bind_buf(2, o),
                        bind_buf(3, &p),
                    ],
                })
            });
            pass.set_pipeline(&c.rmsnorm_b);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(block as u32, 1, 1);
        };
        let rope_bt = |pass: &mut wgpu::ComputePass<'_>, tag: u8, x: &wgpu::Buffer,
                       nh: usize, hd: usize, flags: u32| {
            let bind = cached_bind(c, bk(tag), || {
                let p = uniform_u32x4(c, [nh as u32, hd as u32, g.rd as u32, flags]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_rope_heads.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, x),
                        bind_buf(1, &freq),
                        bind_buf(2, &p),
                        bind_buf(3, &rope_meta),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_rope_heads);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(nh as u32, block as u32, 1);
        };

        // attention half's fold
        {
            let mut pass = begin_pass(&mut enc);
            mixfold(&mut pass, 236, 237, &state_bt, &a_fn, &a_sc, &a_bs, &a_nw);
        }
        // the block's own keys into rows [window, window+block)
        let kvfull = frame_buf_t(c, BT_DSPARK_KV, 8, block * kvw * 4, false);
        let kvnorm = frame_buf_t(c, BT_DSPARK_KV, 16, block * kvw * 4, false);
        if !encode_q4tp_mv4_b(c, &mut enc, &wkv_w, &x2_bt, &kvfull, kvw, dim, block) {
            return false;
        }
        {
            let mut pass = begin_pass(&mut enc);
            rms_b(&mut pass, 238, &kvfull, &knw, &kvnorm, kvw);
            rope_bt(&mut pass, 239, &kvnorm, 1, kvw, 0);
            for i in 0..block {
                encode_blit_p(&mut pass, c, &kvnorm, &cache, g.hd, i * kvw + kvw - g.hd,
                    (g.window + i) * g.hd, None);
            }
        }
        // q
        // upload=true: the trunk's batch shares this (tag, len) entry and
        // seeds it with write_buffer — first creation decides the usage.
        let qn_bt = fb(BT_QN, g.q_lora, true);
        if !encode_q4tp_mv4_b(c, &mut enc, &wq_a, &x2_bt, &qr_bt, g.q_lora, dim, block) {
            return false;
        }
        {
            let mut pass = begin_pass(&mut enc);
            rms_b(&mut pass, 240, &qr_bt, &qnw, &qn_bt, g.q_lora);
        }
        if !encode_q4tp_mv4_b(c, &mut enc, &wq_b, &qn_bt, &q_bt, g.nh * g.hd, g.q_lora, block) {
            return false;
        }
        {
            let mut pass = begin_pass(&mut enc);
            rope_bt(&mut pass, 241, &q_bt, g.nh, g.hd, 1);
            // attend: one shared list (stride 0), every position sees the
            // ring and the whole block.
            let bind = cached_bind(c, bk(242), || {
                let p = uniform_mixed(c, [g.nh as u32, g.hd as u32, 0], g.scale);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_sparse_attend.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &q_bt),
                        bind_buf(1, &cache),
                        bind_buf(2, &idx_b),
                        bind_buf(3, &sink),
                        bind_buf(4, &attn_bt),
                        bind_buf(5, &p),
                        bind_buf(6, &sa_m),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_sparse_attend);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(g.nh as u32, block as u32, 1);
            rope_bt(&mut pass, 243, &attn_bt, g.nh, g.hd, 2);
            // o_project
            let o_rows = g.o_groups * g.o_lora;
            let o_cols = g.nh * g.hd / g.o_groups;
            let p_ol = if g.o_lora % 4 == 0 && ol_a4() {
                &c.bt_o_lora_a4
            } else {
                &c.bt_o_lora_a
            };
            let a4 = std::ptr::eq(
                p_ol as *const wgpu::ComputePipeline,
                &c.bt_o_lora_a4 as *const _,
            );
            let bind = cached_bind(c, bk(244), || {
                let p = uniform_u32x4(c, [(o_cols / 32) as u32, o_rows as u32, g.o_lora as u32, 0]);
                let mut entries = vec![bind_buf(0, &wo_a)];
                if !a4 {
                    entries.push(bind_buf(1, &attn_bt));
                }
                entries.push(bind_buf(2, &mid_bt));
                entries.push(bind_buf(3, &p));
                entries.push(bind_buf(4, &wo_a));
                if a4 {
                    entries.push(bind_buf(5, &attn_bt));
                }
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &p_ol.get_bind_group_layout(0),
                    entries: &entries,
                })
            });
            pass.set_pipeline(p_ol);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups((o_rows as u32).div_ceil(4), block as u32, 1);
        }
        if !encode_q4tp_mv4_b(c, &mut enc, &wo_b, &mid_bt, &ao_bt, dim,
            g.o_groups * g.o_lora, block)
        {
            return false;
        }
        // glue + MoE
        {
            let mut pass = begin_pass(&mut enc);
            expand(&mut pass, 245, &ao_bt, &state_bt, &state2_bt);
            mixfold(&mut pass, 246, 247, &state2_bt, &f_fn, &f_sc, &f_bs, &f_nw);
            let pipe = if g.n_experts < 64 {
                &c.bt_f32_matvec_x
            } else {
                &c.bt_f32_matvec_w
            };
            let bind = cached_bind(c, bk(248), || {
                let p = uniform_u32x4(c, [dim as u32, g.n_experts as u32, 0, 0]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &pipe.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &router),
                        bind_buf(1, &x2_bt),
                        bind_buf(2, &logit_bt),
                        bind_buf(3, &p),
                    ],
                })
            });
            pass.set_pipeline(pipe);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(g.n_experts as u32, block as u32, 1);
            let bias_b = match s.bias {
                Some(b) => const_buf(c, bytemuck::cast_slice(b)),
                None => logit_bt.clone(),
            };
            let rflags = (s.bias.is_some() as u32)
                | 2  // mask: only resident experts are selectable
                | 8
                | 16 // subset: winners arrive as pack slots via the map
                | ((n_res as u32) << 8);
            let forced_dummy = frame_buf_t(c, BT_FORCED, dk(si), block * g.top_k * 4, true);
            let bind = cached_bind(c, bk(249), || {
                let rp = uniform_mixed(
                    c,
                    [g.n_experts as u32, g.top_k as u32, rflags],
                    g.route_scale,
                );
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_moe_route.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &logit_bt),
                        bind_buf(1, &bias_b),
                        bind_buf(2, &mask_b),
                        bind_buf(3, &forced_dummy),
                        bind_buf(4, &msel_bt),
                        bind_buf(5, &mwt_bt),
                        bind_buf(6, &mcnt_bt),
                        bind_buf(7, &rp),
                        bind_buf(8, &map_b),
                        bind_buf(9, &cold_bt),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_moe_route);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(block as u32, 1, 1);
            let stride16 = |rows: usize, cols: usize, q2: bool| -> u32 {
                let dt = if q2 {
                    cortiq_core::TensorDtype::Q2TiledP
                } else {
                    cortiq_core::TensorDtype::Q4TiledP
                };
                (cortiq_core::quant::expected_nbytes(dt, &[rows, cols]).unwrap_or(0) / 2) as u32
            };
            let gu_u = uniform_u32x8(
                c,
                [
                    (dim / 32) as u32,
                    g.inter as u32,
                    slots as u32,
                    stride16(g.inter, dim, g.gu_q2),
                    g.swiglu_limit.to_bits(),
                    0,
                    0,
                    0,
                ],
            );
            let dn_u = uniform_u32x4(
                c,
                [(g.inter / 32) as u32, dim as u32, slots as u32,
                 stride16(dim, g.inter, g.dn_q2)],
            );
            let p_gu = if g.gu_q2 {
                if g.inter % 4 == 0 { &c.bt_moe_gate_up_q2tp_r4 } else { &c.bt_moe_gate_up_q2tp }
            } else {
                &c.moe_gate_up_q4tp_b
            };
            let bind = cached_bind(c, bk(250), || {
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &p_gu.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &gate_all),
                        bind_buf(1, &up_all),
                        bind_buf(2, &x2_bt),
                        bind_buf(3, &msel_bt),
                        bind_buf(4, &mact_bt),
                        bind_buf(5, &gu_u),
                    ],
                })
            });
            pass.set_pipeline(p_gu);
            pass.set_bind_group(0, &bind, &[]);
            let gx = if g.gu_q2 && g.inter % 4 == 0 {
                (g.inter as u32).div_ceil(4)
            } else {
                g.inter as u32
            };
            pass.dispatch_workgroups(gx, slots as u32, block as u32);
            let p_dn = if g.dn_q2 { &c.moe_down_q2tp_b } else { &c.moe_down_q4tp_b };
            let bind = cached_bind(c, bk(251), || {
                // The 2-bit kernel reads x through the vec4 view and never
                // touches the scalar activation binding; the layouts differ.
                let mut entries = vec![
                    bind_buf(0, &down_all),
                    bind_buf(2, &msel_bt),
                    bind_buf(3, &mwt_bt),
                    bind_buf(4, &mo_bt),
                    bind_buf(5, &dn_u),
                ];
                if g.dn_q2 {
                    entries.push(bind_buf(7, &mact_bt));
                } else {
                    entries.insert(1, bind_buf(1, &mact_bt));
                }
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &p_dn.get_bind_group_layout(0),
                    entries: &entries,
                })
            });
            pass.set_pipeline(p_dn);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(dim as u32, block as u32, 1);
            expand(&mut pass, 252, &mo_bt, &state2_bt, &state_bt);
        }
    }
    // ── home: the final states, one fence ──
    let bytes_out = (block * hc * dim * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        bytes_out,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dspark-stage",
    );
    let ok = readback(c, enc, &state_bt, &stage, bytes_out,
        &mut states_out[..block * hc * dim]);
    drop(sc);
    ok
}

thread_local! {
    /// Armed by the speculative verify: how many layers' per-token hidden
    /// inputs the batch should retain on the device (0 = off), and which
    /// layers' post-layer hyper-connection states to photograph for the
    /// draft's capture. The retained hiddens feed the state replay after a
    /// partial acceptance; the captures feed the next draft.
    static SPEC_RETAIN: std::cell::RefCell<(usize, Vec<usize>)> =
        const { std::cell::RefCell::new((0, Vec::new())) };
}

/// Read a pooled frame buffer back, for parity debugging only: the tag
/// registry names what lives where.
/// Debug window into a layer's INDEX cache: `n` floats from float offset
/// `off`. The fold-in-verify investigation reads the entry a fused fold
/// just landed and compares it against the per-token path's.
pub fn dsv4_dbg_read_ix(kv_id: u64, li: usize, off: usize, n: usize) -> Option<Vec<f32>> {
    let c = ctx()?;
    let b = {
        let m = c.dsv4_ixkv.lock().unwrap();
        m.get(&(kv_id, li)).map(|(b, _)| b.clone())?
    };
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
    let stage = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("dbg-ix-stage"),
        size: (n * 4) as u64,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    enc.copy_buffer_to_buffer(&b, (off * 4) as u64, &stage, 0, (n * 4) as u64);
    submit(c, enc.finish());
    let slice = stage.slice(..);
    slice.map_async(wgpu::MapMode::Read, |_| {});
    c.device.poll(wgpu::PollType::wait_indefinitely()).ok()?;
    let data = slice.get_mapped_range().ok()?;
    let v: Vec<f32> = bytemuck::cast_slice(&data).to_vec();
    drop(data);
    Some(v)
}

pub fn dsv4_dbg_read_tag(tag: u8, tok: usize, n: usize) -> Option<Vec<f32>> {
    let c = ctx()?;
    let b = {
        let m = c.dsv4_scratch.lock().unwrap();
        // The pool keys on (tag, tok, len); a debug reader doesn't know the
        // len, so take the first entry matching (tag, tok).
        m.iter()
            .find(|((t, tk, len), _)| *t == tag && *tk == tok && *len == n * 4)
            .or_else(|| m.iter().find(|((t, tk, _), _)| *t == tag && *tk == tok))
            .map(|(_, b)| b.clone())?
    };
    let bytes = ((n * 4) as u64).min(b.size());
    let mut out = vec![0.0f32; (bytes / 4) as usize];
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        bytes,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dbg-stage",
    );
    let enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("dbg") });
    let ok = readback(c, enc, &b, &stage, bytes, &mut out);
    drop(sc);
    ok.then_some(out)
}

/// Arm (n_layers > 0) or disarm (0) hidden-state retention for the next
/// batched chain call on this thread. `caps` lists the layer ordinals whose
/// post-layer state the draft's capture needs.
pub fn dsv4_spec_retain_arm(n_layers: usize, caps: &[usize]) {
    SPEC_RETAIN.with(|c| *c.borrow_mut() = (n_layers, caps.to_vec()));
}

const BT_CAP: u8 = 206;

/// Read the WHOLE capture photograph back in one fence:
/// `[n_caps, batch, hc*dim]`, host-sliced by the caller. The per-slot
/// variant cost a fence per (slot, token) — five of them a pass.
pub fn dsv4_spec_cap_read_all(
    batch: usize,
    n_caps: usize,
    hc_dim: usize,
    out: &mut [f32],
) -> bool {
    if n_caps == 0 {
        return true;
    }
    let Some(c) = ctx() else { return false };
    let cap = frame_buf_t(c, BT_CAP, 0, n_caps * batch * hc_dim * 4, false);
    let bytes = (n_caps * batch * hc_dim * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        bytes,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dsv4-cap-stage",
    );
    let enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-cap-read-all"),
        });
    let ok = readback(c, enc, &cap, &stage, bytes, &mut out[..n_caps * batch * hc_dim]);
    drop(sc);
    ok
}

/// Read one photographed capture back: `slot` indexes the armed `caps`
/// list, `tok` the batch position. `out` takes hc*dim floats.
pub fn dsv4_spec_cap_read(
    slot: usize,
    tok: usize,
    batch: usize,
    n_caps: usize,
    hc_dim: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let cap = frame_buf_t(c, BT_CAP, 0, n_caps * batch * hc_dim * 4, false);
    let bytes = (hc_dim * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        bytes,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dsv4-cap-stage",
    );
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-cap-read"),
        });
    enc.copy_buffer_to_buffer(
        &cap,
        ((slot * batch + tok) * hc_dim * 4) as u64,
        &stage,
        0,
        bytes,
    );
    let ok = {
        submit(c, enc.finish());
        let slice = stage.slice(..bytes);
        let done = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let d2 = done.clone();
        slice.map_async(wgpu::MapMode::Read, move |_| {
            d2.store(true, std::sync::atomic::Ordering::Release);
        });
        if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
            false
        } else if let Ok(data) = slice.get_mapped_range() {
            out[..hc_dim].copy_from_slice(bytemuck::cast_slice(&data[..hc_dim * 4]));
            drop(data);
            stage.unmap();
            true
        } else {
            false
        }
    };
    drop(sc);
    ok
}

/// The device state a speculative pass damages and how to put it back.
///
/// Append-only regions (the compressed tail inside the KV cache, the
/// indexer's cache) roll back by COUNT and are not copied. What is copied:
/// the window rows the B appends will shift out (at most B per layer), and
/// the compressor streams (pending/previous, both kinds), which mutate on
/// every token.
pub struct Dsv4SpecShadow {
    kv_id: u64,
    batch: usize,
    layers: Vec<SpecLayerShadow>,
}
struct SpecLayerShadow {
    li: usize,
    hd: usize,
    window: usize,
    filled: usize,
    /// The first `dk` window rows, photographed before the pass.
    dk: usize,
    head: Option<wgpu::Buffer>,
    comp: Vec<(u8, [wgpu::Buffer; 4])>,
}

/// Photograph what the B-token pass will destroy. One submission.
pub fn dsv4_spec_shadow(
    kv_id: u64,
    metas: &[(usize, usize, usize, usize)], // (li, hd, window, filled)
    batch: usize,
) -> Option<Dsv4SpecShadow> {
    let c = ctx()?;
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-spec-shadow"),
        });
    let mut layers = Vec::with_capacity(metas.len());
    for &(li, hd, window, filled) in metas {
        // Staged batches freeze the window until commit, so there is
        // nothing to photograph there any more; only the compressor
        // streams still mutate mid-pass.
        let dk = 0usize.min(filled.min((filled + batch).saturating_sub(window)));
        let head = if dk > 0 {
            let cache = {
                let map = c.dsv4_kv.lock().unwrap();
                map.get(&(kv_id, li)).map(|(b, _)| b.clone())?
            };
            let b = c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("dsv4-spec-head"),
                size: (dk * hd * 4) as u64,
                usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC,
                mapped_at_creation: false,
            });
            enc.copy_buffer_to_buffer(&cache, 0, &b, 0, (dk * hd * 4) as u64);
            Some(b)
        } else {
            None
        };
        let mut comp = Vec::new();
        for kind in [0u8, 1u8] {
            let live = {
                let map = c.dsv4_comp.lock().unwrap();
                map.get(&(kind, kv_id, li)).cloned()
            };
            if let Some(bufs) = live {
                let clones: [wgpu::Buffer; 4] = std::array::from_fn(|i| {
                    let b = c.device.create_buffer(&wgpu::BufferDescriptor {
                        label: Some("dsv4-spec-comp"),
                        size: bufs[i].size(),
                        usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC,
                        mapped_at_creation: false,
                    });
                    enc.copy_buffer_to_buffer(&bufs[i], 0, &b, 0, bufs[i].size());
                    b
                });
                comp.push((kind, clones));
            }
        }
        layers.push(SpecLayerShadow {
            li,
            hd,
            window,
            filled,
            dk,
            head,
            comp,
        });
    }
    submit(c, enc.finish());
    Some(Dsv4SpecShadow {
        kv_id,
        batch,
        layers,
    })
}

/// Put the device back to the snapshot (as if NO speculative token ran):
/// drop the B appended window rows, un-shift, restore the shadowed head
/// rows, copy the compressor streams back. Counts are the host's business.
pub fn dsv4_spec_restore(sh: &Dsv4SpecShadow) -> bool {
    let Some(c) = ctx() else { return false };
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-spec-restore"),
        });
    for l in &sh.layers {
        if l.dk > 0 {
            let cache = {
                let map = c.dsv4_kv.lock().unwrap();
                match map.get(&(sh.kv_id, l.li)) {
                    Some((b, _)) => b.clone(),
                    None => return false,
                }
            };
            // Surviving original rows sit at [0, keep); slide them right by
            // dk and put the photographed head back in front.
            let keep = l.filled - l.dk;
            if keep > 0 {
                let scratch = frame_buf(c, BT_SPEC_SCRATCH, l.window * l.hd * 4, true);
                enc.copy_buffer_to_buffer(&cache, 0, &scratch, 0, (keep * l.hd * 4) as u64);
                enc.copy_buffer_to_buffer(
                    &scratch,
                    0,
                    &cache,
                    (l.dk * l.hd * 4) as u64,
                    (keep * l.hd * 4) as u64,
                );
            }
            if let Some(head) = &l.head {
                enc.copy_buffer_to_buffer(head, 0, &cache, 0, (l.dk * l.hd * 4) as u64);
            }
        }
        for (kind, clones) in &l.comp {
            let live = {
                let map = c.dsv4_comp.lock().unwrap();
                map.get(&(*kind, sh.kv_id, l.li)).cloned()
            };
            let Some(live) = live else { return false };
            for i in 0..4 {
                let n = live[i].size().min(clones[i].size());
                enc.copy_buffer_to_buffer(&clones[i], 0, &live[i], 0, n);
            }
        }
    }
    submit(c, enc.finish());
    true
}

/// Land the accepted prefix's staged rows in every device layer's window —
/// the staged batch's deferred slide. Safe for any k in 0..=batch.
pub fn dsv4_spec_commit_windows(
    kv_id: u64,
    metas: &[(usize, usize, usize, usize)], // (li, filled, window, hd)
    batch: usize,
    k: usize,
) -> bool {
    let Some(c) = ctx() else { return false };
    if k == 0 {
        return true;
    }
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-spec-commit"),
        });
    for &(li, filled, window, hd) in metas {
        let (cache, cap) = {
            let map = c.dsv4_kv.lock().unwrap();
            match map.get(&(kv_id, li)) {
                Some((b, cap)) => (b.clone(), *cap),
                None => return false,
            }
        };
        let srow0 = (cap / hd).saturating_sub(batch + 1);
        encode_staged_commit(c, &mut enc, &cache, filled, window, hd, srow0, k);
    }
    submit(c, enc.finish());
    true
}

/// Re-append the state of the ACCEPTED tokens after a restore: window rows,
/// compressor streams and folds, the indexer's compressor — from the hidden
/// inputs the batch retained per (layer, token). No attention, no scoring:
/// state only, exactly what a sequential walk of those k tokens would have
/// written.
#[allow(clippy::too_many_arguments)]
pub fn dsv4_spec_replay(
    model: &Arc<CmfModel>,
    layers: &[(usize, Dsv4Prep<'_>)], // (li, prep with counts AS OF the snapshot)
    kv_id: u64,
    pos0: usize,
    batch: usize,
    k: usize,
    inv_freq: &[&[f32]],
    hd: usize,
    dim: usize,
    rope_dim: usize,
    eps: f32,
    // The staged batch commits window rows by blit; the replay then owes
    // only the compressor streams and folds.
    skip_window: bool,
) -> bool {
    if k == 0 {
        return true;
    }
    macro_rules! rfail {
        ($($t:tt)*) => {{
            if std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok() {
                eprintln!("spec_replay: {}", format_args!($($t)*));
            }
            return false;
        }};
    }
    let Some(c) = ctx() else { rfail!("нет контекста") };
    let retain = frame_buf_t(c, BT_RETAIN, 0, layers.len() * batch * dim * 4, false);
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-spec-replay"),
        });
    for (i, (li, p)) in layers.iter().enumerate() {
        for t in 0..k {
            let _salt = Dsv4FrameSalt::enter(t + 1);
            let x2_t = frame_buf_t(c, 45, t, dim * 4, true);
            {
                let mut pass = begin_pass(&mut enc);
                encode_blit_p(
                    &mut pass,
                    c,
                    &retain,
                    &x2_t,
                    dim,
                    (i * batch + t) * dim,
                    0,
                    None,
                );
            }
            let advanced = |ratio: usize| -> usize {
                if ratio == 0 {
                    return 0;
                }
                (0..t).filter(|j| (pos0 + j + 1) % ratio == 0).count()
            };
            let cache = {
                let map = c.dsv4_kv.lock().unwrap();
                match map.get(&(kv_id, *li)) {
                    Some((b, _)) => b.clone(),
                    None => rfail!("нет кеша слоя {li}"),
                }
            };
            if let Some((cw, cg)) = &p.comp {
                let ew = if cg.overlap { cg.width / 2 } else { cg.width };
                let n_comp = p.n_comp + advanced(cg.ratio);
                let off = p.comp_dst_off + (n_comp - p.n_comp) * ew;
                if dsv4_compressor_frame(
                    model, cw, *cg, 0, kv_id, *li, &x2_t, pos0 + t, inv_freq[i], &cache, off,
                    &mut enc,
                )
                .is_none()
                {
                    // None means "no fold this token" — the pending append
                    // is encoded either way; the prep path reads it the
                    // same. A real refusal (missing weight) also lands here
                    // and surfaces as a wrong answer downstream, which the
                    // sequential-parity test is what catches.
                }
            }
            if let Some((iw, ig, _, ixg)) = &p.ix {
                let ew = if ig.overlap { ig.width / 2 } else { ig.width };
                let n_ix = p.n_ix + advanced(ig.ratio);
                let off = p.ix_dst_off + (n_ix - p.n_ix) * ew;
                let Some(ixkv) = dsv4_index_cache(kv_id, *li, ixg.idim * (n_ix + 2)) else {
                    rfail!("ix-кеш слоя {li}");
                };
                if dsv4_compressor_frame(
                    model, iw, *ig, 1, kv_id, *li, &x2_t, pos0 + t, inv_freq[i], &ixkv, off,
                    &mut enc,
                )
                .is_none()
                {
                    // Same contract as above: None is "no fold".
                }
            }
            let filled = (p.filled + t).min(p.window);
            if !skip_window
                && dsv4_window_append(
                    model, p.wkv, p.kv_norm, &x2_t, &cache, hd, p.window, filled, dim,
                    rope_dim, eps, pos0 + t, inv_freq[i], kv_id, *li, &mut enc,
                )
                .is_none()
            {
                rfail!("окно слоя {li} токена {t}");
            }
        }
    }
    submit(c, enc.finish());
    true
}

/// Slide the window by what a run of `k` appends would have slid it, and
/// land the first `k` staged rows as its newest — through scratch, because
/// neither a copy nor a blit may alias its own buffer. Three copies when it
/// slides, two when it does not.
#[allow(clippy::too_many_arguments)]
fn encode_staged_commit(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    cache: &wgpu::Buffer,
    filled: usize,
    window: usize,
    hd: usize,
    srow0: usize,
    k: usize,
) {
    if k == 0 {
        return;
    }
    let shift = (filled + k).saturating_sub(window);
    let keep = filled - shift.min(filled);
    let scratch = frame_buf(c, BT_SPEC_SCRATCH, window * hd * 4, true);
    if shift > 0 {
        if keep > 0 {
            enc.copy_buffer_to_buffer(cache, (shift * hd * 4) as u64, &scratch, 0,
                (keep * hd * 4) as u64);
        }
        enc.copy_buffer_to_buffer(cache, (srow0 * hd * 4) as u64, &scratch,
            (keep * hd * 4) as u64, (k * hd * 4) as u64);
        enc.copy_buffer_to_buffer(&scratch, 0, cache, 0, ((keep + k) * hd * 4) as u64);
    } else {
        enc.copy_buffer_to_buffer(cache, (srow0 * hd * 4) as u64, &scratch, 0,
            (k * hd * 4) as u64);
        enc.copy_buffer_to_buffer(&scratch, 0, cache, (filled * hd * 4) as u64,
            (k * hd * 4) as u64);
    }
}

/// One layer of a whole BATCH, with the token on a grid axis: every
/// dispatch covers all B tokens, so the layer costs the dispatch count of a
/// single token. The arithmetic per token is the single frame's, term for
/// term; only the prep (window append, compressors, indexer) still encodes
/// per token — its writes are ordered by the pass, which is what lets token
/// t attend to the entries token t-1 just appended.
#[allow(clippy::too_many_arguments)]
fn dsv4_layer_frame_bt_enc(
    model: &Arc<CmfModel>,
    w: &Dsv4LayerW,
    g: Dsv4LayerGeom,
    kv_id: u64,
    li: usize,
    batch: usize,
    preps: &[Dsv4Prep],
    forced_rows: Option<&[Option<Vec<usize>>]>,
    inv_freq: &[f32],
    pos0: usize,
    enc: &mut wgpu::CommandEncoder,
) -> Option<(wgpu::Buffer, wgpu::Buffer)> {
    macro_rules! no {
        ($($t:tt)*) => {{
            if std::env::var("CMF_DSV4_FRAME_DEBUG").is_ok() {
                eprintln!("пакетный кадр слоя отклонён: {}", format_args!($($t)*));
            }
            return None;
        }};
    }
    let Some(c) = ctx() else { no!("нет контекста wgpu") };
    let a = g.attn;
    let m = g.moe;
    let (hc, dim) = (g.hc, a.dim);
    let mix_hc = (2 + hc) * hc;
    if preps.len() != batch {
        no!("подготовок {} на пакет {batch}", preps.len());
    }
    // The bind-group key: the layer, salted by the batch width so a 3-wide
    // chunk never reuses a 5-wide chunk's groups (their buffers differ by
    // length and therefore identity).
    let bk = |tag: u8| (tag, kv_id, li * FRAME_TOK_STRIDE + batch);

    // ── weights (same set and order as the single frame) ──
    let bytes = model.primary_bytes();
    let mut wb = Vec::with_capacity(5);
    for &idx in &[
        w.attn.wq_a,
        w.attn.wq_b,
        w.attn.wo_a,
        w.attn.wo_b,
        w.next_wq_a.unwrap_or(w.attn.wq_a),
    ] {
        let Some(e) = model.tensors.get(idx) else {
            no!("тензора {idx} нет");
        };
        if e.dtype != cortiq_core::TensorDtype::Q4TiledP {
            no!("{} не q4tp", e.name);
        }
        let (Some(abs), plen) = (model.entry_abs_offset(e), e.nbytes as usize) else {
            no!("{} без смещения", e.name);
        };
        let Some(b) = weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen])
        else {
            no!("{} не влез в VRAM", e.name);
        };
        wb.push(b);
    }
    let Some((gate_all, up_all, down_all)) =
        moe_expert_bufs(c, model, w.moe.experts, m.inter, m.hidden, true, m.gu_q2, false)
    else {
        no!("эксперты не влезли в VRAM");
    };
    let cache = {
        let map = c.dsv4_kv.lock().unwrap();
        match map.get(&(kv_id, li)) {
            Some((b, _)) => b.clone(),
            None => no!("кеш ({kv_id}, {li}) не заведён"),
        }
    };

    // ── constants ──
    let sink = const_buf(c, bytemuck::cast_slice(&w.attn.sink[..a.nh]));
    let freq = const_buf(c, bytemuck::cast_slice(&inv_freq[..a.rd / 2]));
    let ffn_fn = const_buf(c, bytemuck::cast_slice(w.hc_ffn_fn));
    let ffn_sc = const_buf(c, bytemuck::cast_slice(w.hc_ffn_scale));
    let ffn_bs = const_buf(c, bytemuck::cast_slice(&w.hc_ffn_base[..mix_hc]));
    let ffn_nw = const_buf(c, bytemuck::cast_slice(&w.ffn_norm[..dim]));
    let n_exp = w.moe.experts.len().saturating_sub(1);
    if w.router.len() < m.hidden * n_exp {
        no!("роутер короче {} × {}", n_exp, m.hidden);
    }
    let router = const_buf(c, bytemuck::cast_slice(&w.router[..m.hidden * n_exp]));
    let next_nw = const_buf(c, bytemuck::cast_slice(&w.next_norm[..dim]));
    let next_qn = const_buf(c, bytemuck::cast_slice(&w.next_q_norm[..a.q_lora]));

    // ── strided working buffers ──
    let n_pack = n_exp;
    let slots = m.top_k + 1;
    let fb = |tag: u8, len: usize, upload: bool| frame_buf_t(c, tag, 0, batch * len * 4, upload);
    let state_bt = fb(BT_STATE, hc * dim, true);
    let x2_bt = fb(BT_X2, dim, true);
    let fold_bt = fb(BT_FOLD, dim, false);
    let qn_bt = fb(BT_QN, a.q_lora, true);
    let q_bt = fb(BT_Q, a.nh * a.hd, false);
    let attn_bt = fb(BT_ATTN, a.nh * a.hd, false);
    let mid_bt = fb(BT_MID, a.o_groups * a.o_lora, false);
    let ao_bt = fb(BT_AO, dim, false);
    let mixes_bt = fb(BT_MIXES, mix_hc, false);
    let hpost_bt = fb(BT_HPOST, hc, true);
    let hcomb_bt = fb(BT_HCOMB, hc * hc, true);
    let state2_bt = fb(BT_STATE2, hc * dim, false);
    let logit_bt = fb(BT_LOGIT, n_pack, false);
    let msel_bt = fb(BT_MSEL, slots, false);
    let mwt_bt = fb(BT_MWT, slots, false);
    let mcnt_bt = fb(BT_MCNT, 1, false);
    let mact_bt = fb(BT_MACT, slots * m.inter, false);
    let mo_bt = fb(BT_MO, dim, false);
    let qr_bt = fb(BT_QR, a.q_lora, false);
    let rope_meta = fb(BT_ROPE_META, 2, true);
    // PER LAYER, not pooled: these are filled with `queue.write_buffer`,
    // and every such write lands before the run's single submit — a shared
    // buffer would hand every layer the LAST layer's contents. The same
    // trap once made two hash layers route with one list.
    let sa_m = frame_buf_t(c, BT_SA_M, li, batch * 4, true);
    let idx_bt = fb(BT_IDX, 1024, true);
    let forced_bt = frame_buf_t(c, BT_FORCED, li, batch * m.top_k * 4, true);

    let _ = (&sa_m, &idx_bt);

    // ── STAGED batch: the window never slides mid-pass. Every token's new
    //    key row lands in staging at the cache's tail; the attends read the
    //    frozen window plus each token's staged prefix through per-token
    //    index lists; the slide happens ONCE at commit. This is what lets
    //    the attends (and the indexer) run batched instead of interleaved —
    //    the interleave was the dispatch count, and the dispatch count was
    //    the pass. ──
    let metas: Vec<f32> = (0..batch)
        .flat_map(|t| [(pos0 + t) as f32, a.eps])
        .collect();
    c.queue
        .write_buffer(&rope_meta, 0, bytemuck::cast_slice(&metas));
    let p0 = &preps[0];
    let (cache_cap, kvw) = {
        let map = c.dsv4_kv.lock().unwrap();
        let cap = match map.get(&(kv_id, li)) {
            Some((_, cap)) => *cap,
            None => no!("кеш ({kv_id}, {li}) не заведён"),
        };
        let Some(e) = model.tensors.get(p0.wkv) else {
            no!("wkv без записи");
        };
        (cap, e.shape[0])
    };
    let srow0 = (cache_cap / a.hd).saturating_sub(batch + 1);
    {
        // Staging must sit past everything the pass appends.
        let ew_c = p0.comp.as_ref().map_or(0, |(_, cg)| {
            if cg.overlap { cg.width / 2 } else { cg.width }
        });
        let comp_top = p0.window * a.hd
            + (p0.n_comp + batch.div_ceil(4).max(1) + 2) * ew_c.max(1);
        if srow0 * a.hd < comp_top {
            no!("нет места под staging: srow0 {srow0}, comp_top {comp_top}");
        }
    }

    // Sampled-layer GPU profile: every pass opened between two marks lands
    // under the earlier mark's stage.
    let ts_armed = c.ts_query.is_some() && bt_ts_lis().contains(&li);
    let mark = |s: usize| {
        if ts_armed {
            bt_ts(s);
        }
    };
    mark(1);
    // q for the whole batch — touches no cache.
    if !encode_q4tp_mv4_b(c, enc, &wb[1], &qn_bt, &q_bt, a.nh * a.hd, a.q_lora, batch) {
        no!("q-проекция пакетом не закодировалась");
    }

    // Compressor streams: the two projections batch over the tokens (they
    // read only each token's hidden), the state step stays per token in
    // causal order — its pending/prev shuffle is order-dependent. What it
    // appends goes PAST the logical extents, so the batched attends below
    // stay safe.
    let mut comp_jobs: Vec<(u8, Dsv4CompW, Dsv4CompGeom)> = Vec::new();
    if let Some((cw, cg)) = &p0.comp {
        comp_jobs.push((0, cw.clone(), *cg));
    }
    if let Some((iw, ig, _, ixg0)) = &p0.ix {
        if dsv4_index_cache(kv_id, li, ixg0.idim * (p0.n_ix + batch + 2)).is_none() {
            no!("ix-кеш слоя {li}");
        }
        comp_jobs.push((1, iw.clone(), *ig));
    }
    mark(2);
    if !dsv4_skip("comp") {
        for (kind, cw, cg) in &comp_jobs {
            let ckv_bt = frame_buf_t(c, BT_DSPARK_KV, 26 + *kind as usize, batch * cg.width * 4, false);
            let csc_bt = frame_buf_t(c, BT_DSPARK_KV, 28 + *kind as usize, batch * cg.width * 4, false);
            for (wi, out) in [(cw.wkv, &ckv_bt), (cw.wgate, &csc_bt)] {
                let Some(e) = model.tensors.get(wi) else { no!("компрессор без тензора") };
                let (Some(abs), plen) = (model.entry_abs_offset(e), e.nbytes as usize) else {
                    no!("компрессор без смещения");
                };
                let Some(wbuf) = weight_buffer(c, (model.uid() as usize, wi), &bytes[abs..abs + plen])
                else {
                    no!("компрессор не влез");
                };
                if !encode_q4tp_mv4_b(c, enc, &wbuf, &x2_bt, out, cg.width, cg.hidden, batch) {
                    no!("проекция компрессора пакетом");
                }
            }
            let dst = if *kind == 0 {
                cache.clone()
            } else {
                let m2 = c.dsv4_ixkv.lock().unwrap();
                match m2.get(&(kv_id, li)) {
                    Some((b, _)) => b.clone(),
                    None => no!("ix-кеш ({kv_id},{li}) не заведён"),
                }
            };
            // Fold-free tokens append in ONE dispatch per segment (their
            // slots are all distinct between folds, so the writes commute);
            // only the token that CLOSES a window walks the full per-token
            // state step, folds included.
            let streams = {
                let mut m = c.dsv4_comp.lock().unwrap();
                m.entry((*kind, kv_id, li))
                    .or_insert_with(|| {
                        let span = cg.ratio * cg.width;
                        let mk = || {
                            c.device.create_buffer(&wgpu::BufferDescriptor {
                                label: Some("dsv4-comp-stream"),
                                size: (span * 4).max(4) as u64,
                                usage: wgpu::BufferUsages::STORAGE
                                    | wgpu::BufferUsages::COPY_SRC
                                    | wgpu::BufferUsages::COPY_DST,
                                mapped_at_creation: false,
                            })
                        };
                        [mk(), mk(), mk(), mk()]
                    })
                    .clone()
            };
            let ape_all = const_buf(c, bytemuck::cast_slice(cw.ape));
            let slots: Vec<u32> = (0..batch).map(|t| ((pos0 + t) % cg.ratio) as u32).collect();
            let slot_b = frame_buf_t(c, BT_DSPARK_META, 2 * FRAME_TOK_STRIDE + li * 2 + *kind as usize, batch * 4, true);
            c.queue.write_buffer(&slot_b, 0, bytemuck::cast_slice(&slots));
            let append_seg = |enc: &mut wgpu::CommandEncoder, t0: usize, n: usize| {
                if n == 0 {
                    return;
                }
                let tag = (231 + kind) as u8;
                // The uniform carries (t0, n); the fold position slides with
                // pos0, so the SAME t0 recurs with a DIFFERENT n — n must be
                // in the key or a stale shorter uniform silently drops the
                // tail tokens' appends.
                let bkey = ((li * FRAME_TOK_STRIDE + batch) * FRAME_TOK_STRIDE + t0)
                    * FRAME_TOK_STRIDE
                    + n;
                let bind = cached_bind(c, (tag, kv_id, bkey), || {
                    let flags = cg.overlap as u32;
                    let pu = uniform_u32x4(c, [cg.width as u32, t0 as u32, n as u32, flags]);
                    c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                        label: None,
                        layout: &c.bt_comp_append.get_bind_group_layout(0),
                        entries: &[
                            bind_buf(0, &ckv_bt),
                            bind_buf(1, &csc_bt),
                            bind_buf(2, &ape_all),
                            bind_buf(3, &streams[0]),
                            bind_buf(4, &streams[1]),
                            bind_buf(5, &pu),
                            bind_buf(6, &slot_b),
                        ],
                    })
                });
                let mut pass = begin_pass(enc);
                pass.set_pipeline(&c.bt_comp_append);
                pass.set_bind_group(0, &bind, &[]);
                pass.dispatch_workgroups((cg.width as u32).div_ceil(256), n as u32, 1);
            };
            // Which kinds batch their appends. Both are walk-exact now that
            // the uniform's (t0, n) pair is part of the bind key — the old
            // kind-0 "divergence" was a stale shorter n from a previous
            // pass's cached uniform dropping tail appends. CMF_DSV4_SEGAPP
            // overrides for a bisect.
            let seg_on = std::env::var("CMF_DSV4_SEGAPP").unwrap_or_else(|_| "01".into());
            let seg_this = seg_on.contains(char::from(b'0' + *kind));
            let mut seg0 = 0usize;
            for (t, p) in preps.iter().enumerate() {
                let folds = (pos0 + t + 1) % cg.ratio == 0;
                if !folds && seg_this {
                    continue;
                }
                if seg_this {
                    append_seg(enc, seg0, t - seg0);
                }
                seg0 = t + 1;
                let ew_f = if cg.overlap { cg.width / 2 } else { cg.width };
                if bt_comp_fold_on() && ew_f <= 512 {
                    // Append + pool + norm + rope + land + shift, one link.
                    let off = if *kind == 0 { p.comp_dst_off } else { p.ix_dst_off };
                    let pos = pos0 + t;
                    let have_prev = pos + 1 > cg.ratio;
                    let flags = (cg.overlap as u32) | ((have_prev as u32) << 1);
                    let posb = (pos + 1 - cg.ratio) as f32;
                    // PER (layer, kind, token): two folds can share one
                    // pass at B=5, and every queue.write_buffer lands
                    // before the submit — a shared buffer would hand the
                    // first fold the second fold's uniform.
                    let pu = frame_buf_t(
                        c,
                        BT_DSPARK_META,
                        8 * FRAME_TOK_STRIDE + (li * FRAME_TOK_STRIDE + t) * 2 + *kind as usize,
                        32,
                        true,
                    );
                    c.queue.write_buffer(&pu, 0, bytemuck::cast_slice(&[
                        cg.width as u32, cg.ratio as u32, cg.rope_dim as u32, flags,
                        off as u32, posb.to_bits(), cg.eps.to_bits(), t as u32,
                    ]));
                    let nw_f = const_buf(c, bytemuck::cast_slice(&cw.norm[..ew_f]));
                    let fr_f = const_buf(c, bytemuck::cast_slice(&inv_freq[..cg.rope_dim / 2]));
                    let bind = cached_bind(c, ((253 + *kind) as u8, kv_id, li * FRAME_TOK_STRIDE + t), || {
                        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                            label: None,
                            layout: &c.bt_comp_fold.get_bind_group_layout(0),
                            entries: &[
                                bind_buf(0, &ckv_bt),
                                bind_buf(1, &csc_bt),
                                bind_buf(2, &ape_all),
                                bind_buf(3, &streams[0]),
                                bind_buf(4, &streams[1]),
                                bind_buf(5, &streams[2]),
                                bind_buf(6, &streams[3]),
                                bind_buf(7, &nw_f),
                                bind_buf(8, &fr_f),
                                bind_buf(9, &dst),
                                bind_buf(10, &pu),
                            ],
                        })
                    });
                    let mut pass = begin_pass(enc);
                    pass.set_pipeline(&c.bt_comp_fold);
                    pass.set_bind_group(0, &bind, &[]);
                    pass.dispatch_workgroups(1, 1, 1);
                } else {
                    let _salt = Dsv4FrameSalt::enter(t + 1);
                    let ckv_t = frame_buf(c, 70 + kind, cg.width * 4, false);
                    let csc_t = frame_buf(c, 72 + kind, cg.width * 4, false);
                    {
                        let mut pass = begin_pass(enc);
                        encode_blit_p(&mut pass, c, &ckv_bt, &ckv_t, cg.width, t * cg.width, 0, None);
                        encode_blit_p(&mut pass, c, &csc_bt, &csc_t, cg.width, t * cg.width, 0, None);
                    }
                    let off = if *kind == 0 { p.comp_dst_off } else { p.ix_dst_off };
                    let _ = comp_state_step(
                        c, cw, *cg, *kind, kv_id, li, &ckv_t, &csc_t, pos0 + t, inv_freq, &dst,
                        off, enc, None,
                    );
                }
            }
            if seg_this {
                append_seg(enc, seg0, batch - seg0);
            }
        }
    }

    mark(3);
    // The staged key rows, batched: project, norm, rotate, park at the tail.
    let kvfull = frame_buf_t(c, BT_DSPARK_KV, 20, batch * kvw * 4, false);
    let kvnorm = frame_buf_t(c, BT_DSPARK_KV, 21, batch * kvw * 4, false);
    if !dsv4_skip("win") {
        let Some(e) = model.tensors.get(p0.wkv) else {
            no!("wkv без записи");
        };
        let (Some(abs), plen) = (model.entry_abs_offset(e), e.nbytes as usize) else {
            no!("wkv без смещения");
        };
        let Some(wkv_w) = weight_buffer(c, (model.uid() as usize, p0.wkv), &bytes[abs..abs + plen])
        else {
            no!("wkv не влез");
        };
        if !encode_q4tp_mv4_b(c, enc, &wkv_w, &x2_bt, &kvfull, kvw, dim, batch) {
            no!("wkv пакетом не закодировался");
        }
        let knw = const_buf(c, bytemuck::cast_slice(&p0.kv_norm[..kvw]));
        let mut pass = begin_pass(enc);
        {
            let bind = cached_bind(c, bk(231), || {
                let pu = uniform_u32x4(c, [kvw as u32, 0, a.eps.to_bits(), 0]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.rmsnorm_b.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &kvfull),
                        bind_buf(1, &knw),
                        bind_buf(2, &kvnorm),
                        bind_buf(3, &pu),
                    ],
                })
            });
            pass.set_pipeline(&c.rmsnorm_b);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(batch as u32, 1, 1);
        }
        {
            let bind = cached_bind(c, bk(232), || {
                let pu = uniform_u32x4(c, [1, kvw as u32, a.rd as u32, 0]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_rope_heads.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &kvnorm),
                        bind_buf(1, &freq),
                        bind_buf(2, &pu),
                        bind_buf(3, &rope_meta),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_rope_heads);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(1, batch as u32, 1);
        }
        for t in 0..batch {
            encode_blit_p(&mut pass, c, &kvnorm, &cache, a.hd, t * kvw + kvw - a.hd,
                (srow0 + t) * a.hd, None);
        }
    }

    // The indexer, batched: queries and head weights for every token in two
    // B-axis projections, then scores, top-k and the staged lists.
    let kmax = p0.idx_cap.saturating_sub(p0.window).max(1);
    let pick_bt = fb(203, kmax, false);
    let lim_bt = frame_buf_t(c, BT_SA_M, FRAME_TOK_STRIDE + li, batch * 4, true);
    let meta_bt = frame_buf_t(c, BT_FORCED, FRAME_TOK_STRIDE + li, batch * 4 * 4, true);
    let is_ix = p0.ix.is_some();
    let mut ms = vec![0u32; batch];
    let mut metas_ix = vec![0u32; batch * 4];
    let mut lims = vec![0u32; batch];
    for (t, p) in preps.iter().enumerate() {
        let adv_c = p.comp.as_ref().map_or(0, |(_, cg)| {
            usize::from(cg.ratio > 0 && (pos0 + t + 1) % cg.ratio.max(1) == 0)
        });
        let n_comp_t = p.n_comp + adv_c;
        let adv_i = p.ix.as_ref().map_or(0, |(_, cg, _, _)| {
            usize::from(cg.ratio > 0 && (pos0 + t + 1) % cg.ratio.max(1) == 0)
        });
        let n_ix_t = p.n_ix + adv_i;
        let old_vis = p0.filled.min(p0.window.saturating_sub(t + 1));
        let win_start = p0.filled - old_vis;
        let staged_n = t + 1;
        let k_t = if is_ix {
            let limit = n_ix_t.min(n_comp_t);
            lims[t] = limit as u32;
            if limit == 0 { 0 } else { p0.idx_cap.saturating_sub(p0.window).min(4096).min({
                let Some((_, _, _, ixg)) = p.ix.as_ref() else { unreachable!() };
                ixg.top_k
            }).min(limit) }
        } else {
            n_comp_t
        };
        metas_ix[t * 4] = win_start as u32;
        metas_ix[t * 4 + 1] = old_vis as u32;
        metas_ix[t * 4 + 2] = staged_n as u32;
        metas_ix[t * 4 + 3] = k_t as u32;
        ms[t] = (old_vis + staged_n + k_t) as u32;
        if ms[t] > 1024 {
            no!("список токена {t} длиной {}", ms[t]);
        }
    }
    c.queue.write_buffer(&meta_bt, 0, bytemuck::cast_slice(&metas_ix));
    c.queue.write_buffer(&lim_bt, 0, bytemuck::cast_slice(&lims));
    let sa_m_li = frame_buf_t(c, BT_SA_M, li, batch * 4, true);
    c.queue.write_buffer(&sa_m_li, 0, bytemuck::cast_slice(&ms));
    if is_ix && !dsv4_skip("ix") {
        let Some((_, _, ixw, ixg)) = p0.ix.as_ref() else { unreachable!() };
        let mut ixb = Vec::with_capacity(2);
        for &idx in &[ixw.wq_b, ixw.weights_proj] {
            let Some(e) = model.tensors.get(idx) else { no!("индексер без тензора") };
            let (Some(abs), plen) = (model.entry_abs_offset(e), e.nbytes as usize) else {
                no!("индексер без смещения");
            };
            let Some(b) = weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen])
            else {
                no!("индексер не влез");
            };
            ixb.push(b);
        }
        let ixkv = {
            let m2 = c.dsv4_ixkv.lock().unwrap();
            match m2.get(&(kv_id, li)) {
                Some((b, _)) => b.clone(),
                None => no!("ix-кеш ({kv_id},{li}) не заведён"),
            }
        };
        let qi_bt = frame_buf_t(c, BT_DSPARK_KV, 22, batch * ixg.ih * ixg.idim * 4, false);
        let hw_bt = frame_buf_t(c, BT_DSPARK_KV, 23, batch * ixg.ih * 4, false);
        let sc_bt = frame_buf_t(c, BT_DSPARK_KV, 24, batch * 4096 * 4, false);
        let cnt_bt = frame_buf_t(c, BT_DSPARK_KV, 25, batch * 4, false);
        mark(4);
        if !encode_q4tp_mv4_b(c, enc, &ixb[0], &qn_bt, &qi_bt, ixg.ih * ixg.idim, ixg.q_lora, batch) {
            no!("индексер q пакетом");
        }
        if !encode_q4tp_mv4_b(c, enc, &ixb[1], &x2_bt, &hw_bt, ixg.ih, ixg.hidden, batch) {
            no!("индексер веса пакетом");
        }
        mark(5);
        let sc_factor = (ixg.idim as f32).powf(-0.5) * (ixg.ih as f32).powf(-0.5);
        let n_pos = lims.iter().copied().max().unwrap_or(0) as usize;
        let mut pass = begin_pass(enc);
        {
            let bind = cached_bind(c, bk(233), || {
                let pu = uniform_u32x4(c, [ixg.ih as u32, ixg.idim as u32, ixg.rope_dim as u32, 0]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_rope_heads.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &qi_bt),
                        bind_buf(1, &freq),
                        bind_buf(2, &pu),
                        bind_buf(3, &rope_meta),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_rope_heads);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(ixg.ih as u32, batch as u32, 1);
        }
        if n_pos > 0 {
            let bind = cached_bind(c, bk(234), || {
                let pu = uniform_mixed(c, [ixg.ih as u32, ixg.idim as u32, 4096], sc_factor);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_index_scores.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &qi_bt),
                        bind_buf(1, &ixkv),
                        bind_buf(2, &hw_bt),
                        bind_buf(3, &sc_bt),
                        bind_buf(4, &pu),
                        bind_buf(5, &lim_bt),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_index_scores);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(n_pos as u32, batch as u32, 1);
            let bind = cached_bind(c, bk(235), || {
                let pu = uniform_u32x4(c, [kmax as u32, 0, 0, 0]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_top_k.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &sc_bt),
                        bind_buf(1, &pick_bt),
                        bind_buf(2, &cnt_bt),
                        bind_buf(3, &pu),
                        bind_buf(4, &lim_bt),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_top_k);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(batch as u32, 1, 1);
        }
    }
    mark(6);
    {
        let mut pass = begin_pass(enc);
        let bind = cached_bind(c, bk(236), || {
            let pu = uniform_u32x4(c, [
                p0.window as u32,
                kmax as u32,
                (!is_ix) as u32,
                srow0 as u32,
            ]);
            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &c.bt_idx_build_staged.get_bind_group_layout(0),
                entries: &[
                    bind_buf(0, &pick_bt),
                    bind_buf(1, &idx_bt),
                    bind_buf(2, &pu),
                    bind_buf(3, &meta_bt),
                ],
            })
        });
        pass.set_pipeline(&c.bt_idx_build_staged);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(4, batch as u32, 1);
        // The attends, all tokens at once, over the frozen cache.
        if !dsv4_skip("attn") && !dsv4_skip("sa") {
            let bind = cached_bind(c, bk(228), || {
                let pu = uniform_u32x4(c, [a.nh as u32, a.hd as u32, a.rd as u32, 1]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_rope_heads.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &q_bt),
                        bind_buf(1, &freq),
                        bind_buf(2, &pu),
                        bind_buf(3, &rope_meta),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_rope_heads);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(a.nh as u32, batch as u32, 1);
            let bind = cached_bind(c, bk(229), || {
                let pu = uniform_mixed(c, [a.nh as u32, a.hd as u32, 1024], a.scale);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_sparse_attend.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &q_bt),
                        bind_buf(1, &cache),
                        bind_buf(2, &idx_bt),
                        bind_buf(3, &sink),
                        bind_buf(4, &attn_bt),
                        bind_buf(5, &pu),
                        bind_buf(6, &sa_m_li),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_sparse_attend);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(a.nh as u32, batch as u32, 1);
            let bind = cached_bind(c, bk(230), || {
                let pu = uniform_u32x4(c, [a.nh as u32, a.hd as u32, a.rd as u32, 2]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_rope_heads.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &attn_bt),
                        bind_buf(1, &freq),
                        bind_buf(2, &pu),
                        bind_buf(3, &rope_meta),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_rope_heads);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(a.nh as u32, batch as u32, 1);
        }
    }
    {
        let _ = ();
        // Grouped output projection, one dispatch for the whole batch. The
        // per-token walk-exact loop cost five dependent links of the pass's
        // critical path per layer; the twin reduces in a different tree,
        // which the speculative mode's round-off contract already covers.
        if !dsv4_skip("olora") && !dsv4_skip("oproj") {
            let o_rows = a.o_groups * a.o_lora;
            let o_cols = a.nh * a.hd / a.o_groups;
            if ts_armed {
                // Attribution probe: an empty pass between the attends and
                // this dispatch absorbs the prior pass's drain into its own
                // window, so `olora` reads the dispatch alone.
                bt_ts(10);
                let _p = begin_pass(enc);
            }
            mark(7);
            // The staged twin shares one o-group's x span across its four
            // sub-rows; shapes that straddle a group keep the direct kernel.
            // Staging the span bought nothing on the release (0.41→0.45:
            // the weights, not x, are what this dispatch waits on) — the
            // twin stays for the next investigation, off by default.
            let ol_stage = {
                static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
                *ON.get_or_init(|| std::env::var("CMF_DSV4_OLORA_STAGE").is_ok_and(|v| v != "0"))
            };
            let p_ol = match c.bt_o_lora_a2.as_ref() {
                Some(p2) if ol_stage && o_cols <= 4608 && a.o_lora % 4 == 0 => p2,
                _ if a.o_lora % 4 == 0 && ol_a4() => &c.bt_o_lora_a4,
                _ => &c.bt_o_lora_a,
            };
            let bind = cached_bind(c, bk(213), || {
                let p = uniform_u32x4(c, [(o_cols / 32) as u32, o_rows as u32, a.o_lora as u32, 0]);
                // Entries follow the chosen pipeline's auto layout: the a4
                // twin reads x only through the vec4 view and drops the
                // scalar binding; the staged twin has neither vec4 view.
                let a4 = std::ptr::eq(
                    p_ol as *const wgpu::ComputePipeline,
                    &c.bt_o_lora_a4 as *const _,
                );
                let a2 = std::ptr::eq(
                    p_ol as *const wgpu::ComputePipeline,
                    c.bt_o_lora_a2.as_ref().map_or(std::ptr::null(), |x| x as *const _),
                );
                let mut entries = vec![bind_buf(0, &wb[2])];
                if !a4 {
                    entries.push(bind_buf(1, &attn_bt));
                }
                entries.push(bind_buf(2, &mid_bt));
                entries.push(bind_buf(3, &p));
                if !a2 {
                    entries.push(bind_buf(4, &wb[2]));
                }
                if a4 {
                    entries.push(bind_buf(5, &attn_bt));
                }
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &p_ol.get_bind_group_layout(0),
                    entries: &entries,
                })
            });
            let mut pass = begin_pass(enc);
            pass.set_pipeline(p_ol);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups((o_rows as u32).div_ceil(4), batch as u32, 1);
        }
    }
    mark(15);
    if !dsv4_skip("wob") && !dsv4_skip("oproj")
        && !encode_q4tp_mv4_b(c, enc, &wb[3], &mid_bt, &ao_bt, dim, a.o_groups * a.o_lora, batch)
    {
        no!("wo_b пакетом не закодировался");
    }

    mark(8);
    // ── glue and MoE, one pass ──
    let hcp = uniform_u32x8(
        c,
        [hc as u32, dim as u32, g.sinkhorn_iters as u32, g.hc_eps.to_bits(), 0, 0, 0, 0],
    );
    let hcp_n = uniform_u32x8(
        c,
        [hc as u32, dim as u32, g.sinkhorn_iters as u32, g.hc_eps.to_bits(), 1, mix_hc as u32, 0, 0],
    );
    // The routing description: per-token forced rows when the layer hashes.
    let has_forced = forced_rows.is_some_and(|f| f.iter().any(|r| r.is_some()));
    if has_forced {
        let mut rows = vec![0u32; batch * m.top_k];
        for (t, r) in forced_rows.unwrap().iter().enumerate() {
            let Some(r) = r else {
                no!("хэш-слой без строки токена {t}");
            };
            if r.len() < m.top_k {
                no!("хэш-строка токена {t} короче top_k");
            }
            for (i, &e) in r[..m.top_k].iter().enumerate() {
                rows[t * m.top_k + i] = e as u32;
            }
        }
        c.queue
            .write_buffer(&forced_bt, 0, bytemuck::cast_slice(&rows));
    }
    {
        let mut pass = begin_pass(enc);
        let expand = |pass: &mut wgpu::ComputePass<'_>, tag: u8, x: &wgpu::Buffer,
                      res: &wgpu::Buffer, out: &wgpu::Buffer| {
            let bind = cached_bind(c, bk(tag), || {
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_hc_post_expand.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, x),
                        bind_buf(1, res),
                        bind_buf(2, &hpost_bt),
                        bind_buf(3, &hcomb_bt),
                        bind_buf(4, out),
                        bind_buf(5, &hcp),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_hc_post_expand);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(((hc * dim) as u32).div_ceil(256), batch as u32, 1);
        };
        let mix = |pass: &mut wgpu::ComputePass<'_>, tag: u8, fnw: &wgpu::Buffer,
                   state: &wgpu::Buffer| {
            // The walk picks the 1024-thread kernel below 64 rows; the twin
            // must reduce in the same tree.
            let pipe = if mix_hc < 64 {
                &c.bt_f32_matvec_x
            } else {
                &c.bt_f32_matvec_w
            };
            let bind = cached_bind(c, bk(tag), || {
                let p = uniform_u32x4(c, [(hc * dim) as u32, mix_hc as u32, 0, 0]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &pipe.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, fnw),
                        bind_buf(1, state),
                        bind_buf(2, &mixes_bt),
                        bind_buf(3, &p),
                    ],
                })
            });
            pass.set_pipeline(pipe);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(mix_hc as u32, batch as u32, 1);
        };
        let fold = |pass: &mut wgpu::ComputePass<'_>, tag: u8, state: &wgpu::Buffer,
                    sc: &wgpu::Buffer, bs: &wgpu::Buffer, nw: &wgpu::Buffer| {
            let bind = cached_bind(c, bk(tag), || {
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_hc_pre_fold.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, state),
                        bind_buf(1, &mixes_bt),
                        bind_buf(2, sc),
                        bind_buf(3, bs),
                        bind_buf(4, &fold_bt),
                        bind_buf(5, &hpost_bt),
                        bind_buf(6, &hcomb_bt),
                        bind_buf(7, &hcp_n),
                        bind_buf(8, nw),
                        bind_buf(9, &x2_bt),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_hc_pre_fold);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(batch as u32, 1, 1);
        };
        // The whole FFN-half join — expand, mix, Sinkhorn fold, norm — as
        // ONE link of the critical path.
        let fused = |pass: &mut wgpu::ComputePass<'_>, tag: u8, x: &wgpu::Buffer,
                     res: &wgpu::Buffer, state_out: &wgpu::Buffer, mixw: &wgpu::Buffer,
                     sc: &wgpu::Buffer, bs: &wgpu::Buffer, nw: &wgpu::Buffer| {
            let bind = cached_bind(c, bk(tag), || {
                let p = uniform_u32x8(
                    c,
                    [hc as u32, dim as u32, g.sinkhorn_iters as u32, g.hc_eps.to_bits(),
                     mix_hc as u32, 0, 0, 0],
                );
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_hc_block.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, x),
                        bind_buf(1, res),
                        bind_buf(2, &hpost_bt),
                        bind_buf(3, &hcomb_bt),
                        bind_buf(4, mixw),
                        bind_buf(5, sc),
                        bind_buf(6, bs),
                        bind_buf(7, nw),
                        bind_buf(8, state_out),
                        bind_buf(9, &fold_bt),
                        bind_buf(10, &x2_bt),
                        bind_buf(11, &p),
                    ],
                })
            });
            pass.set_pipeline(&c.bt_hc_block);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(batch as u32, 1, 1);
        };
        if bt_hc_split() {
            expand(&mut pass, 240, &ao_bt, &state_bt, &state2_bt);
            mix(&mut pass, 241, &ffn_fn, &state2_bt);
            fold(&mut pass, 242, &state2_bt, &ffn_sc, &ffn_bs, &ffn_nw);
        } else {
            fused(&mut pass, 214, &ao_bt, &state_bt, &state2_bt, &ffn_fn, &ffn_sc, &ffn_bs, &ffn_nw);
        }
        // ...router logits over the normed fold...
        if ts_armed {
            drop(pass);
            bt_ts(11);
            pass = begin_pass(enc);
        }
        {
            let pipe = if n_pack < 64 {
                &c.bt_f32_matvec_x
            } else {
                &c.bt_f32_matvec_w
            };
            let bind = cached_bind(c, bk(217), || {
                let p = uniform_u32x4(c, [m.hidden as u32, n_pack as u32, 0, 0]);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &pipe.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &router),
                        bind_buf(1, &x2_bt),
                        bind_buf(2, &logit_bt),
                        bind_buf(3, &p),
                    ],
                })
            });
            pass.set_pipeline(pipe);
            pass.set_bind_group(0, &bind, &[]);
            pass.dispatch_workgroups(n_pack as u32, batch as u32, 1);
        }
        // ...route, gate/up, down...
        let bs = match w.moe.bias {
            Some(b) if b.len() >= n_pack => const_buf(c, bytemuck::cast_slice(&b[..n_pack])),
            _ => logit_bt.clone(),
        };
        let rflags = (w.moe.bias.is_some_and(|b| b.len() >= n_pack) as u32)
            | ((has_forced as u32) << 2)
            | 8
            | ((n_pack as u32) << 8);
        let mk = frame_buf(c, 17, n_pack * 4, true);
        let rmap = frame_buf(c, 26, n_pack.max(1) * 4, true);
        let cold = fb(203, 4 * m.top_k, false);
        {
            let bind = cached_bind(c, bk(218), || {
                let rp = uniform_mixed(c, [n_pack as u32, m.top_k as u32, rflags], m.route_scale);
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.bt_moe_route.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &logit_bt),
                        bind_buf(1, &bs),
                        bind_buf(2, &mk),
                        bind_buf(3, &forced_bt),
                        bind_buf(4, &msel_bt),
                        bind_buf(5, &mwt_bt),
                        bind_buf(6, &mcnt_bt),
                        bind_buf(7, &rp),
                        bind_buf(8, &rmap),
                        bind_buf(9, &cold),
                    ],
                })
            });
            if !dsv4_skip("route") && !dsv4_skip("moe") {
                pass.set_pipeline(&c.bt_moe_route);
                pass.set_bind_group(0, &bind, &[]);
                pass.dispatch_workgroups(batch as u32, 1, 1);
            }
        }
        let stride16 = |rows: usize, cols: usize, q2: bool| -> u32 {
            let dt = if q2 {
                cortiq_core::TensorDtype::Q2TiledP
            } else {
                cortiq_core::TensorDtype::Q4TiledP
            };
            (cortiq_core::quant::expected_nbytes(dt, &[rows, cols]).unwrap_or(0) / 2) as u32
        };
        let gu_u = uniform_u32x8(
            c,
            [
                (m.hidden / 32) as u32,
                m.inter as u32,
                slots as u32,
                stride16(m.inter, m.hidden, m.gu_q2),
                m.swiglu_limit.to_bits(),
                0,
                0,
                0,
            ],
        );
        let dn_u = uniform_u32x4(
            c,
            [
                (m.inter / 32) as u32,
                m.hidden as u32,
                slots as u32,
                stride16(m.hidden, m.inter, false),
            ],
        );
        let p_gu = if m.gu_q2 {
            if m.inter % 4 == 0 { &c.bt_moe_gate_up_q2tp_r4 } else { &c.bt_moe_gate_up_q2tp }
        } else {
            &c.moe_gate_up_q4tp_b
        };
        if ts_armed {
            drop(pass);
            bt_ts(12);
            pass = begin_pass(enc);
        }
        let bind = cached_bind(c, bk(219), || {
            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &p_gu.get_bind_group_layout(0),
                entries: &[
                    bind_buf(0, &gate_all),
                    bind_buf(1, &up_all),
                    bind_buf(2, &x2_bt),
                    bind_buf(3, &msel_bt),
                    bind_buf(4, &mact_bt),
                    bind_buf(5, &gu_u),
                ],
            })
        });
        if !dsv4_skip("gu") && !dsv4_skip("moe") {
            pass.set_pipeline(p_gu);
            pass.set_bind_group(0, &bind, &[]);
            let gx = if m.gu_q2 && m.inter % 4 == 0 {
                (m.inter as u32).div_ceil(4)
            } else {
                m.inter as u32
            };
            pass.dispatch_workgroups(gx, slots as u32, batch as u32);
        }
        if ts_armed {
            drop(pass);
            bt_ts(13);
            pass = begin_pass(enc);
        }
        if bt_dn_mode() == 3 && stride16(m.hidden, m.inter, false) % 8 == 0 {
            let bind = cached_bind(c, bk(248), || {
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.moe_down_q4tp_b4.get_bind_group_layout(0),
                    // No entry for binding 1: the register-vec4 kernel
                    // reads x only through the vec4 view, and the auto
                    // layout drops what the entry point never touches.
                    entries: &[
                        bind_buf(0, &down_all),
                        bind_buf(2, &msel_bt),
                        bind_buf(3, &mwt_bt),
                        bind_buf(4, &mo_bt),
                        bind_buf(5, &dn_u),
                        bind_buf(6, &down_all),
                        bind_buf(7, &mact_bt),
                    ],
                })
            });
            if !dsv4_skip("dn") && !dsv4_skip("moe") {
                pass.set_pipeline(&c.moe_down_q4tp_b4);
                pass.set_bind_group(0, &bind, &[]);
                pass.dispatch_workgroups((m.hidden as u32).div_ceil(4), batch as u32, 1);
            }
        } else if bt_dn_mode() == 2 {
            // Per-slot partials, then the ascending-slot sum.
            let dpart = frame_buf_t(c, BT_DSPARK_KV, 30, batch * slots * m.hidden * 4, false);
            let bindp = cached_bind(c, bk(246), || {
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.moe_down_q4tp_part.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &down_all),
                        bind_buf(1, &mact_bt),
                        bind_buf(2, &msel_bt),
                        bind_buf(3, &mwt_bt),
                        bind_buf(4, &dpart),
                        bind_buf(5, &dn_u),
                    ],
                })
            });
            let bindr = cached_bind(c, bk(247), || {
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.moe_down_q4tp_red.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &dpart),
                        bind_buf(1, &mo_bt),
                        bind_buf(2, &dn_u),
                    ],
                })
            });
            if !dsv4_skip("dn") && !dsv4_skip("moe") {
                pass.set_pipeline(&c.moe_down_q4tp_part);
                pass.set_bind_group(0, &bindp, &[]);
                pass.dispatch_workgroups(m.hidden as u32, slots as u32, batch as u32);
                pass.set_pipeline(&c.moe_down_q4tp_red);
                pass.set_bind_group(0, &bindr, &[]);
                pass.dispatch_workgroups((m.hidden as u32).div_ceil(256), batch as u32, 1);
            }
        } else {
            // The direct-load twin needs an even u16 stride so a tile's four
            // words ARE four words; every published layout satisfies it, the
            // check keeps an exotic one honest.
            let p_dn = if bt_dn_mode() == 1 && stride16(m.hidden, m.inter, false) % 2 == 0 {
                &c.moe_down_q4tp_b2
            } else {
                &c.moe_down_q4tp_b
            };
            let bind = cached_bind(c, bk(220), || {
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &p_dn.get_bind_group_layout(0),
                    entries: &[
                        bind_buf(0, &down_all),
                        bind_buf(1, &mact_bt),
                        bind_buf(2, &msel_bt),
                        bind_buf(3, &mwt_bt),
                        bind_buf(4, &mo_bt),
                        bind_buf(5, &dn_u),
                    ],
                })
            });
            if !dsv4_skip("dn") && !dsv4_skip("moe") {
                pass.set_pipeline(p_dn);
                pass.set_bind_group(0, &bind, &[]);
                pass.dispatch_workgroups(m.hidden as u32, batch as u32, 1);
            }
        }
        // ...the MoE half's join and the NEXT layer's opening, fused too.
        if ts_armed {
            drop(pass);
            bt_ts(14);
            pass = begin_pass(enc);
        }
        if let Some(nf) = w.hc_next_fn {
            let nfn = const_buf(c, bytemuck::cast_slice(nf));
            let nsc = const_buf(c, bytemuck::cast_slice(w.hc_next_scale));
            let nbs = const_buf(c, bytemuck::cast_slice(&w.hc_next_base[..mix_hc]));
            if bt_hc_split() {
                expand(&mut pass, 243, &mo_bt, &state2_bt, &state_bt);
                mix(&mut pass, 244, &nfn, &state_bt);
                fold(&mut pass, 245, &state_bt, &nsc, &nbs, &next_nw);
            } else {
                fused(&mut pass, 221, &mo_bt, &state2_bt, &state_bt, &nfn, &nsc, &nbs, &next_nw);
            }
        } else {
            expand(&mut pass, 221, &mo_bt, &state2_bt, &state_bt);
        }
    }
    mark(9);
    if w.hc_next_fn.is_some() && !dsv4_skip("nextq") {
        if !encode_q4tp_mv4_b(c, enc, &wb[4], &x2_bt, &qr_bt, a.q_lora, dim, batch) {
            no!("next-q пакетом не закодировался");
        }
        // One row-per-workgroup dispatch for every token's LoRA norm: the
        // per-token walk tree cost five dependent links; round-off class.
        let mut pass = begin_pass(enc);
        let bind = cached_bind(c, bk(227), || {
            let p = uniform_u32x4(c, [a.q_lora as u32, 0, a.eps.to_bits(), 0]);
            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &c.rmsnorm_b.get_bind_group_layout(0),
                entries: &[
                    bind_buf(0, &qr_bt),
                    bind_buf(1, &next_qn),
                    bind_buf(2, &qn_bt),
                    bind_buf(3, &p),
                ],
            })
        });
        pass.set_pipeline(&c.rmsnorm_b);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(batch as u32, 1, 1);
    }
    // ── the slide, once: outside a speculative verify the batch commits
    //    wholesale — the staged rows become the window's newest, the oldest
    //    leave, exactly what B walked appends would have left. A verify
    //    (retention armed) defers this to dsv4_spec_finish, which commits
    //    only the accepted prefix. ──
    mark(10);
    if SPEC_RETAIN.with(|v| v.borrow().0) == 0 && !dsv4_skip("win") {
        encode_staged_commit(c, enc, &cache, p0.filled, p0.window, a.hd, srow0, batch);
    }
    let src = if w.hc_next_fn.is_some() {
        x2_bt.clone()
    } else {
        ao_bt.clone()
    };
    if ts_armed {
        bt_ts(0);
    }
    Some((src, state_bt))
}

pub fn dsv4_layer_chain(
    model: &Arc<CmfModel>,
    layers: &[(Dsv4LayerW<'_>, Dsv4LayerGeom, Dsv4Prep<'_>)],
    kv_id: u64,
    first_li: usize,
    inv_freq: &[&[f32]],
    pos: usize,
    folded_out: &mut [f32],
    // When present, the hyper-connection state comes back in the SAME
    // submission — the caller then skips `dsv4_state_read` entirely.
    state_out: Option<&mut [f32]>,
) -> bool {
    let Some(c) = ctx() else { return false };
    let Some((_, g0, _)) = layers.first() else {
        return false;
    };
    let dim = g0.attn.dim;
    if folded_out.len() < dim || inv_freq.len() != layers.len() {
        return false;
    }
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-chain"),
        });
    let mut last = None;
    let t_enc = std::time::Instant::now();
    for (i, (w, g, p)) in layers.iter().enumerate() {
        // `qn = None` throughout: the first layer's LoRA vector was left on
        // the card by the caller's seed, every later one by the frame before.
        let Some(b) = dsv4_layer_frame_enc(
            model, w, *g, kv_id, first_li + i, 0, false, false, false, None, &[], Some(p), inv_freq[i], pos, &mut enc,
        ) else {
            // Nothing has been submitted, so the token can still be run the
            // old way — but the caches this chain advanced have NOT been
            // touched either, because every write went into this encoder.
            return false;
        };
        last = Some(b);
    }
    let Some(src) = last else { return false };
    // The whole run's HOST encode time, before the fence: the number that
    // says whether the chain is submit-bound or encode-bound.
    CHAIN_ENC_NS.fetch_add(
        t_enc.elapsed().as_nanos() as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    CHAIN_LAYERS.fetch_add(layers.len() as u64, std::sync::atomic::Ordering::Relaxed);
    CHAIN_RUNS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let t_wait = std::time::Instant::now();
    let ok = match state_out {
        // The state comes back in the SAME submission as the folded vector.
        // Asking for it afterwards cost a second fence on a token that is
        // otherwise one submission — a third of the round trips, for a copy
        // of a few kilobytes.
        Some(st) => {
            let sb = frame_buf(c, 40, st.len() * 4, true);
            readback2(c, enc, (&src, &mut folded_out[..dim]), (&sb, st))
        }
        None => {
            let mut sc = c.scratch.lock().unwrap();
            let stage = Scratch::ensure(
                &c.device,
                &mut sc.stage,
                (dim * 4) as u64,
                wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
                "dsv4-chain-stage",
            );
            let ok = readback(c, enc, &src, &stage, (dim * 4) as u64, &mut folded_out[..dim]);
            drop(sc);
            ok
        }
    };
    CHAIN_WAIT_NS.fetch_add(
        t_wait.elapsed().as_nanos() as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    ok
}

/// Host encode and fence-wait of the chain, per run, with the layer count —
/// the split that decides where its 12.5 tok/s goes.
pub static CHAIN_ENC_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
pub static CHAIN_WAIT_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
pub static CHAIN_LAYERS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// Submissions per token — one fence each. The number that says whether a
/// layer kind is still breaking the chain into pieces.
pub static CHAIN_RUNS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// One layer, submitted on its own and read back — the shape the two-frame
/// path and the current layer loop use.
#[allow(clippy::too_many_arguments)]
pub fn dsv4_layer_frame(
    model: &Arc<CmfModel>,
    w: &Dsv4LayerW,
    g: Dsv4LayerGeom,
    kv_id: u64,
    li: usize,
    qn: Option<&[f32]>,
    idxs: &[u32],
    inv_freq: &[f32],
    pos: usize,
    folded_next: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    let dim = g.attn.dim;
    if folded_next.len() < dim {
        return false;
    }
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-layer"),
        });
    let Some(folded) = dsv4_layer_frame_enc(
        model, w, g, kv_id, li, 0, false, false, false, qn, idxs, None, inv_freq, pos, &mut enc,
    ) else {
        return false;
    };
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        (dim * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dsv4-layer-stage",
    );
    let ok = readback(c, enc, &folded, &stage, (dim * 4) as u64, &mut folded_next[..dim]);
    drop(sc);
    ok
}

/// Is this tensor resident, or can it be made so? Uploads it if it can.
pub fn dsv4_weight_ready(model: &Arc<CmfModel>, idx: usize) -> bool {
    let Some(c) = ctx() else { return false };
    let Some(e) = model.tensors.get(idx) else {
        return false;
    };
    let Some(abs) = model.entry_abs_offset(e) else {
        return false;
    };
    let bytes = model.primary_bytes();
    let plen = e.nbytes as usize;
    if abs + plen > bytes.len() {
        return false;
    }
    weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen]).is_some()
}

/// How many experts of this shape still fit on the card. The caller packs
/// that many and leaves the rest to the host — per EXPERT, so no layer ever
/// has to leave the device wholesale.
pub fn dsv4_experts_fit(inter: usize, hidden: usize, gu_q2: bool, dn_q2: bool) -> usize {
    use std::sync::atomic::Ordering;
    let Some(c) = ctx() else { return 0 };
    let gu = if gu_q2 {
        cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q2TiledP, &[inter, hidden])
    } else {
        cortiq_core::quant::expected_nbytes(cortiq_core::TensorDtype::Q4TiledP, &[inter, hidden])
    }
    .unwrap_or(0);
    let dn = cortiq_core::quant::expected_nbytes(
        if dn_q2 {
            cortiq_core::TensorDtype::Q2TiledP
        } else {
            cortiq_core::TensorDtype::Q4TiledP
        },
        &[hidden, inter],
    )
    .unwrap_or(0);
    let per = (2 * gu + dn) as u64;
    if per == 0 {
        return 0;
    }
    let used = c.resident.load(Ordering::Relaxed);
    // A weight budget is not the physical allocation ceiling: command
    // staging, KV/state buffers and the driver's own bookkeeping live beside
    // it. Keep a small geometry-independent reserve that scales down on
    // small cards and caps out on large ones. The value is intentionally a
    // function of the configured budget, never a checkpoint/layer cutoff.
    let mib = 1024 * 1024u64;
    let reserve = std::env::var("CMF_GPU_WORKSPACE_MB")
        .ok()
        .and_then(|v| v.parse::<u64>().ok())
        .map(|v| v.saturating_mul(mib))
        .unwrap_or_else(|| {
            // The base covers command staging, KV growth and driver
            // bookkeeping; the draft term carves out what the speculative
            // draft's own resident pack will take, so the trunk's greedy
            // packing stops before the draft has nowhere to live. Packing
            // against the physical ceiling fails NONDETERMINISTICALLY as
            // the KV grows mid-run, so the base rides twice when a draft
            // is coming.
            let base = (c.vram_budget / 384).clamp(256 * mib, 512 * mib);
            let draft = DRAFT_RESERVE.load(Ordering::Relaxed);
            if draft > 0 { 2 * base + draft } else { base }
        });
    let usable = c.vram_budget.saturating_sub(reserve);
    ((usable.saturating_sub(used)) / per) as usize
}

/// What the speculative draft's device pack will need, set at load when the
/// file carries an MTP stack and speculation is not disabled. Zero means no
/// draft is coming and the trunk may pack into the whole budget.
pub static DRAFT_RESERVE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// The configured weight budget, initializing the device on first ask.
/// None when no GPU path is selected or init failed.
pub fn dsv4_vram_budget() -> Option<u64> {
    ctx().map(|c| c.vram_budget)
}

/// `dsv4_experts_fit` with the draft's own carve-out handed back: the draft
/// pack builder must see the room that was reserved FOR it, not the room
/// that remains after its own reservation.
pub fn dsv4_draft_fit(inter: usize, hidden: usize, gu_q2: bool, dn_q2: bool) -> usize {
    let base = dsv4_experts_fit(inter, hidden, gu_q2, dn_q2);
    if std::env::var("CMF_GPU_WORKSPACE_MB").is_ok() {
        // An explicit workspace is the operator's own split; the draft
        // competes inside it exactly as before the reservation existed.
        return base;
    }
    let gu = cortiq_core::quant::expected_nbytes(
        if gu_q2 {
            cortiq_core::TensorDtype::Q2TiledP
        } else {
            cortiq_core::TensorDtype::Q4TiledP
        },
        &[inter, hidden],
    )
    .unwrap_or(0);
    let dn = cortiq_core::quant::expected_nbytes(
        if dn_q2 {
            cortiq_core::TensorDtype::Q2TiledP
        } else {
            cortiq_core::TensorDtype::Q4TiledP
        },
        &[hidden, inter],
    )
    .unwrap_or(0);
    let per = (2 * gu + dn) as u64;
    if per == 0 {
        return base;
    }
    base + (DRAFT_RESERVE.load(std::sync::atomic::Ordering::Relaxed) / per) as usize
}

/// Can this layer's experts live on the card? Uploads them if they can, so a
/// caller that pre-flights every layer has also paid the upload before it
/// commits to the device path.
/// Ask for the attention weights BEFORE the experts, or the experts take the
/// card and the skeleton — two orders of magnitude smaller — has nowhere left.
pub fn dsv4_experts_ready(
    model: &Arc<CmfModel>,
    experts: &[(usize, usize, usize)],
    inter: usize,
    hidden: usize,
    gu_q2: bool,
    dn_q2: bool,
) -> bool {
    let Some(c) = ctx() else { return false };
    moe_expert_bufs(c, model, experts, inter, hidden, true, gu_q2, dn_q2).is_some()
}

/// Seed the attention half's `post`/`comb` from the host. The frame's opening
/// expand reads what the PREVIOUS frame's tail left there; layer zero has no
/// previous frame, and neither does the layer after one that ran on the host.
/// Without this both read whatever was in the buffer — perplexity 1470.
/// Seed one token's hyper-connection mix for the run's FIRST layer.
///
/// Every later layer computes its own on the card, but the first takes the
/// host's. With one slot the batch's tokens would all get whichever seed was
/// written last — so this, like the state, is per token.
pub fn dsv4_hc_write_t(post: &[f32], comb: &[f32], tok: usize) -> bool {
    let Some(c) = ctx() else { return false };
    let pb = frame_buf_t(c, 43, tok, post.len() * 4, true);
    let cb = frame_buf_t(c, 44, tok, comb.len() * 4, true);
    c.queue.write_buffer(&pb, 0, bytemuck::cast_slice(post));
    c.queue.write_buffer(&cb, 0, bytemuck::cast_slice(comb));
    true
}

pub fn dsv4_hc_write(post: &[f32], comb: &[f32]) -> bool {
    let Some(c) = ctx() else { return false };
    let pb = frame_buf(c, 43, post.len() * 4, true);
    let cb = frame_buf(c, 44, comb.len() * 4, true);
    c.queue.write_buffer(&pb, 0, bytemuck::cast_slice(post));
    c.queue.write_buffer(&cb, 0, bytemuck::cast_slice(comb));
    true
}

/// Seed the layer-frame's hyper-connection state from the host (layer zero).
/// Seed one token's hyper-connection state on the card.
///
/// The batch needs one of these per token before the chain runs: each token
/// enters at its own embedding, and after the frame's buffers went per token
/// the single-slot writer can only reach token zero.
pub fn dsv4_state_write_t(state: &[f32], tok: usize) -> bool {
    let Some(c) = ctx() else { return false };
    let b = frame_buf_t(c, 40, tok, state.len() * 4, true);
    c.queue.write_buffer(&b, 0, bytemuck::cast_slice(state));
    true
}

pub fn dsv4_state_write(state: &[f32]) -> bool {
    let Some(c) = ctx() else { return false };
    let b = frame_buf(c, 40, state.len() * 4, true);
    c.queue.write_buffer(&b, 0, bytemuck::cast_slice(state));
    true
}

/// Read the hyper-connection state back (end of token, for the head).
pub fn dsv4_state_read(state: &mut [f32]) -> bool {
    let Some(c) = ctx() else { return false };
    let b = frame_buf(c, 40, state.len() * 4, true);
    let bytes = (state.len() * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        bytes,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dsv4-state-stage",
    );
    let enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("st") });
    let ok = readback(c, enc, &b, &stage, bytes, state);
    drop(sc);
    ok
}

/// Add the experts that did not fit in VRAM to the device-owned
/// hyper-connection state and bring the corrected state home.
///
/// The MoE frame has already expanded the resident experts as
/// `post[j] * resident + comb * residual`. Hyper-connections are linear in
/// the block output, so the exact correction is another expansion with an
/// identity `comb`: `state[j] += post[j] * cold`. Doing it here keeps the
/// policy independent of a layer number or a particular VRAM size.
pub fn dsv4_state_add_cold(cold: &[f32], hc: usize, state_out: &mut [f32]) -> bool {
    let Some(c) = ctx() else { return false };
    if hc == 0 || cold.is_empty() || state_out.len() != hc * cold.len() {
        return false;
    }
    let dim = cold.len();
    let state = frame_buf(c, 40, state_out.len() * 4, true);
    let corrected = frame_buf(c, 46, state_out.len() * 4, true);
    // `post` was produced by the FFN half's opening fold and is still live in
    // the canonical slot after the MoE frame returns its cold winners.
    let post = frame_buf(c, 43, hc * 4, true);
    let cold_buf = frame_up(c, 121, bytemuck::cast_slice(cold));
    let mut identity = vec![0.0f32; hc * hc];
    for j in 0..hc {
        identity[j * hc + j] = 1.0;
    }
    let comb = frame_up(c, 122, bytemuck::cast_slice(&identity));
    let p = uniform_u32x8(
        c,
        [hc as u32, dim as u32, 0, 0, 0, 0, 0, 0],
    );
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-cold-state-fix"),
        });
    encode_hc_expand(
        c,
        &mut enc,
        &cold_buf,
        &state,
        &post,
        &comb,
        &corrected,
        &p,
        hc,
        dim,
    );
    enc.copy_buffer_to_buffer(
        &corrected,
        0,
        &state,
        0,
        (state_out.len() * 4) as u64,
    );
    let bytes = (state_out.len() * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        bytes,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dsv4-cold-state-stage",
    );
    let ok = readback(c, enc, &state, &stage, bytes, state_out);
    drop(sc);
    ok
}

/// MoE routing on the device: scores in, chosen experts and their normalised
/// weights out, in selection order. `forced` is the hash layers' table row.
#[allow(clippy::too_many_arguments)]
/// The token-axis router against the same contract, `b` tokens at once:
/// `scores` is `[b, n]`, outputs are `[b, top_k+1]`. Exercises exactly the
/// kernel the batched frame dispatches.
#[allow(clippy::too_many_arguments)]
pub fn bt_moe_route_for_test(
    scores: &[f32],
    b: usize,
    bias: Option<&[f32]>,
    forced: Option<&[Vec<usize>]>,
    top_k: usize,
    route_scale: f32,
    idx_out: &mut Vec<usize>,
    w_out: &mut Vec<f32>,
) -> bool {
    let Some(c) = ctx() else { return false };
    let n = scores.len() / b.max(1);
    if n == 0 || n > 1024 || top_k == 0 || top_k > 64 || b == 0 {
        return false;
    }
    let slots = top_k + 1;
    let sb = storage_bytes(c, bytemuck::cast_slice(scores));
    let bb = match bias {
        Some(v) if v.len() >= n => storage_bytes(c, bytemuck::cast_slice(&v[..n])),
        _ => sb.clone(),
    };
    let mb = storage_bytes(c, bytemuck::cast_slice(&vec![1u32; n]));
    let fbuf = match forced {
        Some(rows) if rows.len() >= b => {
            let mut v = vec![0u32; b * top_k];
            for (t, r) in rows.iter().enumerate() {
                for (i, &e) in r.iter().take(top_k).enumerate() {
                    v[t * top_k + i] = e as u32;
                }
            }
            storage_bytes(c, bytemuck::cast_slice(&v))
        }
        _ => storage_bytes(c, bytemuck::cast_slice(&vec![0u32; b * top_k])),
    };
    let ib = rw_f32(c, b * slots, true);
    let wb = rw_f32(c, b * slots, true);
    let cnt = rw_f32(c, b, true);
    let rmb = storage_bytes(c, bytemuck::cast_slice(&vec![0u32; n]));
    let coldb = rw_f32(c, b * 4 * top_k, false);
    let flags = (bias.is_some_and(|v| v.len() >= n) as u32)
        | ((forced.is_some() as u32) << 2)
        | 8
        | ((n as u32) << 8);
    let p = uniform_mixed(c, [n as u32, top_k as u32, flags], route_scale);
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("bt-route-test"),
        });
    {
        let layout = c.bt_moe_route.get_bind_group_layout(0);
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &layout,
            entries: &[
                bind_buf(0, &sb),
                bind_buf(1, &bb),
                bind_buf(2, &mb),
                bind_buf(3, &fbuf),
                bind_buf(4, &ib),
                bind_buf(5, &wb),
                bind_buf(6, &cnt),
                bind_buf(7, &p),
                bind_buf(8, &rmb),
                bind_buf(9, &coldb),
            ],
        });
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.bt_moe_route);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(b as u32, 1, 1);
    }
    let ib_bytes = (b * slots * 4) as u64;
    let mut iv = vec![0.0f32; b * slots];
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        ib_bytes,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "bt-route-stage",
    );
    if !readback(c, enc, &ib, &stage, ib_bytes, &mut iv) {
        return false;
    }
    let iu: Vec<u32> = bytemuck::cast_slice(&iv).to_vec();
    idx_out.clear();
    idx_out.extend(iu.iter().map(|&x| x as usize));
    let mut enc2 = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("bt-route-test-w"),
        });
    let _ = &mut enc2;
    let mut wv = vec![0.0f32; b * slots];
    if !readback(c, enc2, &wb, &stage, ib_bytes, &mut wv) {
        return false;
    }
    w_out.clear();
    w_out.extend_from_slice(&wv);
    drop(sc);
    true
}

pub fn moe_route_for_test(
    scores: &[f32],
    bias: Option<&[f32]>,
    mask: Option<&[bool]>,
    forced: Option<&[usize]>,
    top_k: usize,
    route_scale: f32,
    // Pin the shared expert in slot `top_k` at weight 1, and write every slot
    // — the `msel`/`mwt` pair the batched expert kernels take. Off returns
    // just what the router chose.
    shared_slot: bool,
    idx_out: &mut Vec<usize>,
    w_out: &mut Vec<f32>,
) -> bool {
    let Some(c) = ctx() else { return false };
    let n = scores.len();
    if n == 0 || n > 1024 || top_k == 0 || top_k > 64 {
        return false;
    }
    let sb = storage_bytes(c, bytemuck::cast_slice(scores));
    let bb = match bias {
        Some(b) if b.len() >= n => storage_bytes(c, bytemuck::cast_slice(&b[..n])),
        _ => sb.clone(),
    };
    let mb = match mask {
        Some(m) if m.len() >= n => {
            let v: Vec<u32> = m[..n].iter().map(|&x| x as u32).collect();
            storage_bytes(c, bytemuck::cast_slice(&v))
        }
        _ => storage_bytes(c, bytemuck::cast_slice(&vec![1u32; n])),
    };
    let fb = match forced {
        Some(f) if f.len() >= top_k => {
            let v: Vec<u32> = f[..top_k].iter().map(|&x| x as u32).collect();
            storage_bytes(c, bytemuck::cast_slice(&v))
        }
        _ => storage_bytes(c, bytemuck::cast_slice(&vec![0u32; top_k])),
    };
    let slots = top_k + shared_slot as usize;
    let ib = rw_f32(c, slots, true);
    let wb = rw_f32(c, slots, true);
    let cb = rw_f32(c, 1, true);
    let rmb = storage_bytes(c, bytemuck::cast_slice(&vec![0u32; n]));
    let coldb = rw_f32(c, 4 * top_k, false);
    let flags = (bias.is_some_and(|b| b.len() >= n) as u32)
        | ((mask.is_some_and(|m| m.len() >= n) as u32) << 1)
        | ((forced.is_some_and(|f| f.len() >= top_k) as u32) << 2)
        | ((shared_slot as u32) << 3)
        // Nothing is packed away here, so the shared expert sits at n — the
        // same slot the frame computes from its packing.
        | ((n as u32) << 8);
    let p = uniform_mixed(c, [n as u32, top_k as u32, flags], route_scale);
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("route") });
    {
        let layout = c.moe_route.get_bind_group_layout(0);
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &layout,
            entries: &[
                bind_buf(0, &sb),
                bind_buf(1, &bb),
                bind_buf(2, &mb),
                bind_buf(3, &fb),
                bind_buf(4, &ib),
                bind_buf(5, &wb),
                bind_buf(6, &cb),
                bind_buf(7, &p),
                // The router grew a remap and a winners buffer; a standalone
                // caller that skips them is a validation error, not a
                // silently different answer.
                bind_buf(8, &rmb),
                bind_buf(9, &coldb),
            ],
        });
        // The card's own clock around the whole MoE block. All three kernels
        // share one pass — splitting it to time them apart would add two
        // pass boundaries a layer and measure the split instead — so this is
        // the block's total, which is the number that says whether the card
        // is busy or waiting.
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.moe_route);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(1, 1, 1);
    }
    // idx | weights | count, one map.
    let bytes = ((2 * slots + 1) * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        bytes,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "route-stage",
    );
    enc.copy_buffer_to_buffer(&ib, 0, &stage, 0, (slots * 4) as u64);
    enc.copy_buffer_to_buffer(&wb, 0, &stage, (slots * 4) as u64, (slots * 4) as u64);
    enc.copy_buffer_to_buffer(&cb, 0, &stage, (2 * slots * 4) as u64, 4);
    submit(c, enc.finish());
    let slice = stage.slice(..bytes);
    slice.map_async(wgpu::MapMode::Read, |_| {});
    if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
        return false;
    }
    let mut ok = false;
    if let Ok(data) = slice.get_mapped_range() {
        let words: &[u32] = bytemuck::cast_slice(&data[..bytes as usize]);
        let ws: &[f32] = bytemuck::cast_slice(&data[slots * 4..2 * slots * 4]);
        // With a shared slot the caller wants every slot, filled or not; the
        // count is what the kernels use to skip nothing.
        let take = if shared_slot {
            slots
        } else {
            (words[2 * slots] as usize).min(top_k)
        };
        idx_out.clear();
        w_out.clear();
        idx_out.extend(words[..take].iter().map(|&x| x as usize));
        w_out.extend_from_slice(&ws[..take]);
        ok = true;
    }
    stage.unmap();
    drop(sc);
    ok
}

fn encode_rmsnorm(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    w: &wgpu::Buffer,
    o: &wgpu::Buffer,
    n: usize,
    eps: f32,
    bkey: (u8, u64, usize),
) {
    let mut pass = begin_pass(enc);
    encode_rmsnorm_p(&mut pass, c, x, w, o, n, eps, bkey);
}

#[allow(clippy::too_many_arguments)]
fn encode_rmsnorm_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    x: &wgpu::Buffer,
    w: &wgpu::Buffer,
    o: &wgpu::Buffer,
    n: usize,
    eps: f32,
    bkey: (u8, u64, usize),
) {
    let bind = cached_bind(c, bkey, || {
        let p = uniform_u32x4(c, [n as u32, 0, eps.to_bits(), 0]);
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.rmsnorm.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, x),
                bind_buf(1, w),
                bind_buf(2, o),
                bind_buf(3, &p),
            ],
        })
    });
    pass.set_pipeline(&c.rmsnorm);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(1, 1, 1);
}

#[allow(clippy::too_many_arguments)]
fn encode_rope_heads(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    x: &wgpu::Buffer,
    freq: &wgpu::Buffer,
    posb: &wgpu::Buffer,
    nh: usize,
    hd: usize,
    rd: usize,
    rms: bool,
    inverse: bool,
    bkey: (u8, u64, usize),
) {
    let mut pass = begin_pass(enc);
    encode_rope_heads_p(&mut pass, c, x, freq, posb, nh, hd, rd, rms, inverse, bkey);
}

#[allow(clippy::too_many_arguments)]
fn encode_rope_heads_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    x: &wgpu::Buffer,
    freq: &wgpu::Buffer,
    posb: &wgpu::Buffer,
    nh: usize,
    hd: usize,
    rd: usize,
    rms: bool,
    inverse: bool,
    bkey: (u8, u64, usize),
) {
    let bind = cached_bind(c, bkey, || {
        let flags = (rms as u32) | ((inverse as u32) << 1);
        let p = uniform_u32x4(c, [nh as u32, hd as u32, rd as u32, flags]);
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.rope_heads.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, x),
                bind_buf(1, freq),
                bind_buf(2, &p),
                bind_buf(3, posb),
            ],
        })
    });
    pass.set_pipeline(&c.rope_heads);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(nh as u32, 1, 1);
}

/// A constant vector (a norm weight, the sinks, the frequency table) parked
/// on the card and keyed on its host address — the same bytes arrive every
/// token, and re-uploading them 43 times a token is pure waste.
fn const_buf(c: &Ctx, data: &[u8]) -> wgpu::Buffer {
    let key = (data.as_ptr() as usize, data.len());
    let mut m = c.const_bufs.lock().unwrap();
    if let Some(b) = m.get(&key) {
        return b.clone();
    }
    let b = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("dsv4-const"),
        size: data.len().max(4) as u64,
        // COPY_SRC too: the compressor frame slices one window's worth of
        // `ape` out of the parked table with a device-to-device copy,
        // which beats re-uploading that slice every token.
        usage: wgpu::BufferUsages::STORAGE
            | wgpu::BufferUsages::COPY_DST
            | wgpu::BufferUsages::COPY_SRC,
        mapped_at_creation: false,
    });
    c.queue.write_buffer(&b, 0, data);
    m.insert(key, b.clone());
    b
}

/// A frame working buffer, created on first use and reused for good. `tag`
/// separates roles that happen to share a length — two buffers of the same
/// size are not interchangeable when both are live in one encoder.
/// A pooled scratch buffer, keyed by (role, size).
///
/// GROW-ONLY BY CONTRACT: an entry is never evicted or recreated, so the
/// handle a caller got last token is the same one it gets this token. The
/// bind-group cache depends on that — it holds groups built from these
/// buffers and only the GREW epoch invalidates them, so adding eviction
/// here would leave every cached group pointing at a dead buffer with
/// nothing to notice.
fn frame_buf(c: &Ctx, tag: u8, len_bytes: usize, upload: bool) -> wgpu::Buffer {
    let salt = dsv4_frame_salt();
    let tok = if salt == 0 {
        0
    } else {
        // Explicit token carriers occupy 0..FRAME_TOK_STRIDE. Keep implicit
        // scratch in a disjoint namespace so token 1's position buffer can
        // never alias token 0's temporary buffer with the same tag.
        FRAME_TOK_STRIDE + salt
    };
    frame_buf_t(c, tag, tok, len_bytes, upload)
}

/// The same pool, one buffer per token of the batch.
///
/// Scratch that is written and consumed inside a single layer can stay
/// shared: tokens are encoded one after another and a compute pass orders
/// its dispatches. What cannot be shared is anything that CARRIES a token
/// from one layer to the next — the hyper-connection state above all — since
/// the second token would overwrite the first one's before it is read.
fn frame_buf_t(c: &Ctx, tag: u8, tok: usize, len_bytes: usize, upload: bool) -> wgpu::Buffer {
    let mut m = c.dsv4_scratch.lock().unwrap();
    if let Some(b) = m.get(&(tag, tok, len_bytes)) {
        return b.clone();
    }
    let mut usage = wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC;
    if upload {
        usage |= wgpu::BufferUsages::COPY_DST;
    }
    let b = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("dsv4-frame"),
        size: len_bytes.max(4) as u64,
        usage,
        mapped_at_creation: false,
    });
    m.insert((tag, tok, len_bytes), b.clone());
    b
}

/// Upload into a reused buffer instead of minting one per call.
/// The position uniform, written once per TOKEN: its contents are the same
/// for every layer, and the per-layer `frame_up` was 43 redundant queue
/// writes a token.
/// The position uniform for one token of a batch.
///
/// It cannot share a slot: several `write_buffer` calls land before the
/// submission, so every bind group would read the LAST position written and
/// the whole batch would attend at one place. The write-skip cache is only
/// safe for the single-token path, so the batch always writes.
fn frame_up_pos_t(c: &Ctx, tag: u8, tok: usize, pos: usize, eps: f32) -> wgpu::Buffer {
    if tok == 0 {
        return frame_up_pos(c, tag, pos, eps);
    }
    let b = frame_buf_t(c, tag, tok, 8, true);
    c.queue
        .write_buffer(&b, 0, bytemuck::cast_slice(&[pos as f32, eps]));
    b
}

fn frame_up_pos(c: &Ctx, tag: u8, pos: usize, eps: f32) -> wgpu::Buffer {
    use std::sync::atomic::{AtomicU64, Ordering};
    static LAST: [AtomicU64; 256] = [const { AtomicU64::new(u64::MAX) }; 256];
    let stamp = ((pos as u64) << 32) | eps.to_bits() as u64;
    let b = frame_buf(c, tag, 8, true);
    if LAST[tag as usize].swap(stamp, Ordering::Relaxed) != stamp {
        c.queue
            .write_buffer(&b, 0, bytemuck::cast_slice(&[pos as f32, eps]));
    }
    b
}

fn frame_up(c: &Ctx, tag: u8, data: &[u8]) -> wgpu::Buffer {
    let b = frame_buf(c, tag, data.len(), true);
    c.queue.write_buffer(&b, 0, data);
    b
}

fn sa_split() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_SA_SPLIT").is_ok_and(|v| v != "0"))
}

/// The two-dispatch sparse attention: scores per head, then the weighted sum
/// over nh*hd independent outputs. Same numbers as the one-workgroup-per-head
/// kernel, spread across the card instead of 64 groups of it — which measured
/// 0.54 ms a layer and was the whole cost of the block once its encoding was
/// cached away.
#[allow(clippy::too_many_arguments)]
/// Take the next slot pair and record it under `which`, so the frame that
/// owns the encoder can resolve every pass it wrote.
fn ts_pair(c: &Ctx, which: usize) -> Option<wgpu::ComputePassTimestampWrites<'_>> {
    let (qs, _, _) = c.ts_query.as_ref()?;
    let slot = (TS_SLOT.fetch_add(2, std::sync::atomic::Ordering::Relaxed) % 254) as u32;
    TS_PAIRS.lock().unwrap().push((which, slot));
    Some(wgpu::ComputePassTimestampWrites {
        query_set: qs,
        beginning_of_pass_write_index: Some(slot),
        end_of_pass_write_index: Some(slot + 1),
    })
}

/// The one-kernel attention's bind group, shared by the timestamped path
/// (a pass of its own, so the query set has boundaries to write) and the
/// fused one (a dispatch inside the layer's pass).
#[allow(clippy::too_many_arguments)]
/// What a dispatch actually costs, and WHY.
///
/// Every fusion this session was bought or rejected on an assumed ~30 µs a
/// dispatch, and that number came from dividing a frame by its dispatch
/// count — which cannot tell a kernel LAUNCH from the memory barrier wgpu
/// inserts when two dispatches touch the same buffer. The two have opposite
/// remedies: launches want fewer dispatches, barriers want independent ones
/// grouped together. So measure both.
///
/// N trivial dispatches into ONE buffer (every pair a hazard) against N into
/// eight buffers round-robin (no hazard within a group of eight).
pub fn dispatch_bench() -> Vec<String> {
    let mut out = Vec::new();
    let Some(c) = ctx() else {
        out.push("нет устройства".into());
        return out;
    };
    const N: usize = 2000;
    let bufs: Vec<wgpu::Buffer> = (0..8)
        .map(|_| {
            c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("dbench"),
                size: 4096,
                usage: wgpu::BufferUsages::STORAGE,
                mapped_at_creation: false,
            })
        })
        .collect();
    let p = uniform_u32x4(c, [1024, 0, 0, 0]);
    let binds: Vec<wgpu::BindGroup> = bufs
        .iter()
        .map(|b| {
            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &c.zero.get_bind_group_layout(0),
                entries: &[bind_buf(0, b), bind_buf(1, &p)],
            })
        })
        .collect();
    for (name, stride) in [("зависимые (один буфер)", 0usize), ("независимые (8 буферов)", 1)] {
        // Warm, then time.
        for round in 0..2 {
            let t0 = std::time::Instant::now();
            let mut enc = c
                .device
                .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
            {
                let mut pass = begin_pass(&mut enc);
                pass.set_pipeline(&c.zero);
                for i in 0..N {
                    let b = if stride == 0 { 0 } else { i % 8 };
                    pass.set_bind_group(0, &binds[b], &[]);
                    pass.dispatch_workgroups(4, 1, 1);
                }
            }
            submit(c, enc.finish());
            let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
            if round == 1 {
                let us = t0.elapsed().as_secs_f64() * 1e6 / N as f64;
                out.push(format!("{name}: {us:.2} мкс на диспатч"));
            }
        }
    }
    out
}

/// How many position-chunks to cut a head into. One workgroup a chunk, so
/// this is the occupancy knob: 64 heads alone left the card at a few percent.
fn sa_chunks(m: usize) -> usize {
    if m == 0 {
        return 1;
    }
    // CMF_DSV4_SA_CHUNKS forces the count. The toys attend to fewer than 128
    // positions, so they would take one chunk and never exercise the merge
    // at all — the flag is what lets the known-good stands test it.
    static F: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    let forced = *F.get_or_init(|| {
        std::env::var("CMF_DSV4_SA_CHUNKS")
            .ok()
            .and_then(|v| v.parse::<usize>().ok())
            .unwrap_or(0)
    });
    if forced > 0 {
        return forced.min(m).min(SA_MAX_CHUNKS);
    }
    // MEASURED, not reasoned: on the release, 2 chunks gave 22.9 tok/s, 4
    // gave 23.6 and 8 gave 24.5 — monotone, because each chunk is a
    // workgroup and 64 heads alone leave the card idle. ~16 positions a
    // chunk, so a short list still splits.
    m.div_ceil(16).clamp(1, SA_MAX_CHUNKS)
}

/// The scratch is nh·SA_MAX_CHUNKS·hd floats — 4 MB at the release's shape.
const SA_MAX_CHUNKS: usize = 32;

/// `CMF_DSV4_SA_SPLIT=0` reverts attention to the one-workgroup-per-head
/// kernel. The split changes the order the softmax sums in, so it is a
/// contract change and wants a flag.
fn sa_split_k() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_SA_SPLIT").map(|v| v != "0").unwrap_or(true))
}

/// Sparse attention as two dispatches: a per-chunk pass and a merge.
#[allow(clippy::too_many_arguments)]
fn encode_sa_split_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    q: &wgpu::Buffer,
    kv: &wgpu::Buffer,
    ixb: &wgpu::Buffer,
    sink: &wgpu::Buffer,
    out: &wgpu::Buffer,
    nh: usize,
    hd: usize,
    m: usize,
    scale: f32,
    kv_id: u64,
    li: usize,
) {
    let nc = sa_chunks(m);
    let ck = m.div_ceil(nc).max(1);
    let acc = frame_buf(c, 112, nh * SA_MAX_CHUNKS * hd * 4, false);
    let mxb = frame_buf(c, 113, nh * SA_MAX_CHUNKS * 4, false);
    let lnb = frame_buf(c, 114, nh * SA_MAX_CHUNKS * 4, false);
    let pp = uni_slot8(
        c,
        176,
        kv_id,
        li,
        [
            nh as u32,
            hd as u32,
            m as u32,
            nc as u32,
            ck as u32,
            0,
            0,
            scale.to_bits(),
        ],
    );
    let bind = cached_bind(c, (180, kv_id, li), || {
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.sa_part.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, q),
                bind_buf(1, kv),
                bind_buf(2, ixb),
                bind_buf(3, &acc),
                bind_buf(4, &mxb),
                bind_buf(5, &lnb),
                bind_buf(6, &pp),
            ],
        })
    });
    pass.set_pipeline(&c.sa_part);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(nh as u32, nc as u32, 1);

    let mp = uni_slot(c, 182, kv_id, li, [nh as u32, hd as u32, nc as u32, 0]);
    let bind2 = cached_bind(c, (184, kv_id, li), || {
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.sa_merge.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, &acc),
                bind_buf(1, &mxb),
                bind_buf(2, &lnb),
                bind_buf(3, sink),
                bind_buf(4, out),
                bind_buf(5, &mp),
            ],
        })
    });
    pass.set_pipeline(&c.sa_merge);
    pass.set_bind_group(0, &bind2, &[]);
    pass.dispatch_workgroups(nh as u32, 1, 1);
}

/// The fused hyper-connection join: expand, mix, fold, norm — one dispatch.
#[allow(clippy::too_many_arguments)]
fn encode_hc_block_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    x: &wgpu::Buffer,
    res: &wgpu::Buffer,
    post: &wgpu::Buffer,
    comb: &wgpu::Buffer,
    mixw: &wgpu::Buffer,
    sc: &wgpu::Buffer,
    base: &wgpu::Buffer,
    nw: &wgpu::Buffer,
    state: &wgpu::Buffer,
    fold: &wgpu::Buffer,
    norm: &wgpu::Buffer,
    hc: usize,
    dim: usize,
    mix_hc: usize,
    iters: usize,
    eps: f32,
    bkey: (u8, u64, usize),
) {
    let bind = cached_bind(c, bkey, || {
        let p = uniform_u32x8(
            c,
            [
                hc as u32,
                dim as u32,
                iters as u32,
                eps.to_bits(),
                mix_hc as u32,
                0,
                0,
                0,
            ],
        );
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.hc_block.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, x),
                bind_buf(1, res),
                bind_buf(2, post),
                bind_buf(3, comb),
                bind_buf(4, mixw),
                bind_buf(5, sc),
                bind_buf(6, base),
                bind_buf(7, nw),
                bind_buf(8, state),
                bind_buf(9, fold),
                bind_buf(10, norm),
                bind_buf(11, &p),
            ],
        })
    });
    pass.set_pipeline(&c.hc_block);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(1, 1, 1);
}

/// A device-to-device copy of `n` floats, as a dispatch — so it can sit
/// inside a compute pass instead of ending one.
#[allow(clippy::too_many_arguments)]
fn encode_blit_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    src: &wgpu::Buffer,
    dst: &wgpu::Buffer,
    n: usize,
    soff: usize,
    doff: usize,
    // Some: cache the group. The offsets move with the sequence, so they ride
    // a per-layer uniform slot that is rewritten rather than a content-keyed
    // buffer.
    bkey: Option<(u8, u64, usize)>,
) {
    let p = match bkey {
        Some((tag, kv, li)) => uni_slot(c, tag, kv, li, [n as u32, soff as u32, doff as u32, 0]),
        None => uniform_u32x4(c, [n as u32, soff as u32, doff as u32, 0]),
    };
    let mk = || c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.blit.get_bind_group_layout(0),
        entries: &[bind_buf(0, src), bind_buf(1, dst), bind_buf(2, &p)],
    });
    let bind = match bkey {
        Some((tag, kv, li)) => cached_bind(c, (tag.wrapping_add(1), kv, li), mk),
        None => mk(),
    };
    pass.set_pipeline(&c.blit);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((n as u32).div_ceil(256), 1, 1);
}

fn sa_bind_single(
    c: &Ctx,
    q: &wgpu::Buffer,
    kv: &wgpu::Buffer,
    ixb: &wgpu::Buffer,
    sink: &wgpu::Buffer,
    out: &wgpu::Buffer,
    nh: usize,
    hd: usize,
    m: usize,
    scale: f32,
    bkey: Option<(u64, usize)>,
) -> wgpu::BindGroup {
    let p = match bkey {
        Some((kv_id, li)) => uni_slot(c, 140, kv_id, li,
            [nh as u32, hd as u32, m as u32, scale.to_bits()]),
        None => uniform_mixed(c, [nh as u32, hd as u32, m as u32], scale),
    };
    let mk = || c.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &c.sparse_attend.get_bind_group_layout(0),
        entries: &[
            bind_buf(0, q),
            bind_buf(1, kv),
            bind_buf(2, ixb),
            bind_buf(3, sink),
            bind_buf(4, out),
            bind_buf(5, &p),
        ],
    });
    match bkey {
        Some((kv_id, li)) => cached_bind(c, (141, kv_id, li), mk),
        None => mk(),
    }
}

fn encode_sparse_attend2(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    q: &wgpu::Buffer,
    kv: &wgpu::Buffer,
    ixb: &wgpu::Buffer,
    sink: &wgpu::Buffer,
    out: &wgpu::Buffer,
    nh: usize,
    hd: usize,
    m: usize,
    scale: f32,
    // Some: cache the bind group under this (sequence, layer). The attended
    // count walks with the sequence, so the uniform is a per-layer slot that
    // is rewritten rather than a content-keyed buffer — same trade as the
    // prep encoders. None: build fresh (the standalone frames, whose buffers
    // are not the chain's).
    bkey: Option<(u64, usize)>,
) {
    // ONE workgroup per head after all. The split into scores + apply spread
    // the work across the card and bought 0.54 -> 0.49 ms a layer — nothing —
    // while moving the model's perplexity by 0.7% through a different
    // accumulation order. Faster would have justified that; a wash does not.
    // CMF_SA_SPLIT=1 runs the split pair for anyone who wants to retry it on
    // a part where occupancy actually bites.
    if !sa_split() {
        let bind = sa_bind_single(c, q, kv, ixb, sink, out, nh, hd, m, scale, bkey);
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: None,
            timestamp_writes: ts_pair(c, 0),
        });
        pass.set_pipeline(&c.sparse_attend);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(nh as u32, 1, 1);
        return;
    }
    let wbuf = frame_buf(c, 9, nh * m.max(1) * 4, false);
    let p = uniform_mixed(c, [nh as u32, hd as u32, m as u32], scale);
    {
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.sa_scores.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, q),
                bind_buf(1, kv),
                bind_buf(2, ixb),
                bind_buf(3, sink),
                bind_buf(4, &wbuf),
                bind_buf(5, &p),
            ],
        });
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: None,
            timestamp_writes: ts_pair(c, 1),
        });
        pass.set_pipeline(&c.sa_scores);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(nh as u32, 1, 1);
    }
    {
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.sa_apply.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, &wbuf),
                bind_buf(1, kv),
                bind_buf(2, ixb),
                bind_buf(3, out),
                bind_buf(4, &p),
            ],
        });
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: None,
            timestamp_writes: ts_pair(c, 2),
        });
        pass.set_pipeline(&c.sa_apply);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(((nh * hd) as u32).div_ceil(256), 1, 1);
    }
}

/// `y += w·d`, both device-side, `n` floats.
fn encode_axpy(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    d: &wgpu::Buffer,
    y: &wgpu::Buffer,
    w: f32,
    n: usize,
    bkey: (u8, u64, usize),
) {
    let mut pass = begin_pass(enc);
    encode_axpy_p(&mut pass, c, d, y, w, n, bkey);
}

#[allow(clippy::too_many_arguments)]
fn encode_axpy_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    d: &wgpu::Buffer,
    y: &wgpu::Buffer,
    w: f32,
    n: usize,
    bkey: (u8, u64, usize),
) {
    encode_axpy_full_p(pass, c, d, y, w, n, false, 0, Some(bkey));
}

/// `y = w·x[soff..]` when `set`, else `y += w·x[soff..]`.
///
/// The uniform is written in the order the SHADER declares — `w` first, then
/// `n`. It used to be written `[n, 0, 0, w]` against a `{ w: f32, n: u32 }`
/// struct, so the kernel read `n` as zero and every invocation returned at
/// the bounds check: the whole op was a no-op wherever the device ran it.
#[allow(clippy::too_many_arguments)]
fn encode_axpy_full_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    d: &wgpu::Buffer,
    y: &wgpu::Buffer,
    w: f32,
    n: usize,
    set: bool,
    soff: usize,
    bkey: Option<(u8, u64, usize)>,
) {
    let vals = [w.to_bits(), n as u32, set as u32, soff as u32];
    let mk = || {
        let p = uniform_u32x4(c, vals);
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.axpy.get_bind_group_layout(0),
            entries: &[bind_buf(0, d), bind_buf(1, y), bind_buf(2, &p)],
        })
    };
    let bind = match bkey {
        // `soff` walks with the sequence on the compressor's bias add, so the
        // cached form takes a per-layer slot it can rewrite.
        Some((tag, kv, li)) => {
            let p = uni_slot(c, tag, kv, li, vals);
            cached_bind(c, (tag, kv, li), || {
                c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                    label: None,
                    layout: &c.axpy.get_bind_group_layout(0),
                    entries: &[bind_buf(0, d), bind_buf(1, y), bind_buf(2, &p)],
                })
            })
        }
        None => mk(),
    };
    pass.set_pipeline(&c.axpy);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((n as u32).div_ceil(256), 1, 1);
}

/// The compressor's fold: a softmax over the slot axis PER DIMENSION, then
/// the weighted sum. `width` here is the OUTPUT width — half the projection
/// when the windows overlap, which is also the stride the kernel assumes.
#[allow(clippy::too_many_arguments)]
fn encode_kv_pool(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    prev_kv: &wgpu::Buffer,
    prev_sc: &wgpu::Buffer,
    cur_kv: &wgpu::Buffer,
    cur_sc: &wgpu::Buffer,
    ape: &wgpu::Buffer,
    out: &wgpu::Buffer,
    g: Dsv4CompGeom,
    have_prev: bool,
    kind: u8,
    kvid: u64,
    li: usize,
) {
    let mut pass = begin_pass(enc);
    encode_kv_pool_p(&mut pass, c, prev_kv, prev_sc, cur_kv, cur_sc, ape, out, g, have_prev, kind, kvid, li);
}

#[allow(clippy::too_many_arguments)]
fn encode_kv_pool_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    prev_kv: &wgpu::Buffer,
    prev_sc: &wgpu::Buffer,
    cur_kv: &wgpu::Buffer,
    cur_sc: &wgpu::Buffer,
    ape: &wgpu::Buffer,
    out: &wgpu::Buffer,
    g: Dsv4CompGeom,
    have_prev: bool,
    kind: u8,
    kvid: u64,
    li: usize,
) {
    let ew = if g.overlap { g.width / 2 } else { g.width };
    let slots = if g.overlap { 2 * g.ratio } else { g.ratio };
    // The bias is folded in on arrival when the windows overlap, so the
    // kernel must not add it a second time — it does so only for the flat
    // compressor, which has nowhere else to put it.
    let use_ape = !g.overlap;
    // `have_prev` flips once early in the sequence, so the uniform is a
    // rewritten slot rather than a content-keyed buffer.
    let flags = (g.overlap as u32) | ((have_prev as u32) << 1) | ((use_ape as u32) << 2);
    let p = uni_slot(c, 130 + kind, kvid, li, [slots as u32, ew as u32, g.ratio as u32, flags]);
    let bind = cached_bind(c, (132 + kind, kvid, li), || {
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.kv_pool.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, prev_kv),
                bind_buf(1, prev_sc),
                bind_buf(2, cur_kv),
                bind_buf(3, cur_sc),
                bind_buf(4, ape),
                bind_buf(5, out),
                bind_buf(6, &p),
            ],
        })
    });
    pass.set_pipeline(&c.kv_pool);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((ew as u32).div_ceil(256), 1, 1);
}

/// What one compressor needs from the model, by directory index (the two
/// projections are q4tp) and by value (the small f32 pieces).
#[derive(Clone)]
pub struct Dsv4CompW<'a> {
    pub wkv: usize,
    pub wgate: usize,
    pub norm: &'a [f32],
    /// `[ratio, width]` — the in-window position bias.
    pub ape: &'a [f32],
}

#[derive(Clone, Copy)]
pub struct Dsv4CompGeom {
    /// `wkv.rows()`; the folded entry is half this when the windows overlap.
    pub width: usize,
    pub hidden: usize,
    pub ratio: usize,
    pub overlap: bool,
    pub rope_dim: usize,
    pub eps: f32,
}

/// Advance ONE compressor by one token, entirely on the device, and fold the
/// window when it closes.
///
/// The host's only inputs are the position and the hidden state that is
/// already there; the pending and previous streams live in `dsv4_comp` and
/// are never read back. That is the whole point: the compressor was the
/// reason every layer had to come back to the CPU mid-token.
///
/// Returns `Some(offset)` — in floats, into the layer's cache buffer — when
/// this token closed a window and an entry was appended there, `Some` with
/// no write is impossible, and `None` when the window is still filling or
/// the frame declined. `n_comp` is how many entries the cache already holds.
#[allow(clippy::too_many_arguments)]
pub fn dsv4_compressor_frame(
    model: &Arc<CmfModel>,
    w: &Dsv4CompW,
    g: Dsv4CompGeom,
    kind: u8,
    kv_id: u64,
    li: usize,
    hidden: &wgpu::Buffer,
    pos: usize,
    inv_freq: &[f32],
    // Where a folded entry goes: the layer's cache and the float offset of
    // the first free compressed slot.
    dst: &wgpu::Buffer,
    dst_off: usize,
    enc: &mut wgpu::CommandEncoder,
) -> Option<usize> {
    let c = ctx()?;
    let ew = if g.overlap { g.width / 2 } else { g.width };
    if g.width == 0 || g.ratio == 0 || g.hidden % 32 != 0 {
        return None;
    }
    let bytes = model.primary_bytes();
    let mut wb = Vec::with_capacity(2);
    for &idx in &[w.wkv, w.wgate] {
        let e = model.tensors.get(idx)?;
        if e.dtype != cortiq_core::TensorDtype::Q4TiledP || e.shape.len() != 2 {
            return None;
        }
        let abs = model.entry_abs_offset(e)?;
        let plen = e.nbytes as usize;
        bytes.get(abs..abs + plen)?;
        wb.push(weight_buffer(
            c,
            (model.uid() as usize, idx),
            &bytes[abs..abs + plen],
        )?);
    }

    let ckv = frame_buf(c, 70 + kind, g.width * 4, false);
    let csc = frame_buf(c, 72 + kind, g.width * 4, false);
    // The two projections and everything the state step does are one chain
    // of dependent dispatches: one pass for the whole compressor.
    comp_state_step(c, w, g, kind, kv_id, li, &ckv, &csc, pos, inv_freq, dst, dst_off, enc,
        Some((&wb[0], &wb[1], hidden)))
}

/// Everything after the two projections: the slot bookkeeping, the fold when
/// the window closes, and the shuffle of pending into previous. Split out
/// because this — not the matvecs, which have their own parity tests — is
/// what is new here, and it can be driven from a test with the projections
/// handed in.
#[allow(clippy::too_many_arguments)]
fn comp_state_step(
    c: &Ctx,
    w: &Dsv4CompW,
    g: Dsv4CompGeom,
    kind: u8,
    kv_id: u64,
    li: usize,
    ckv: &wgpu::Buffer,
    csc: &wgpu::Buffer,
    pos: usize,
    inv_freq: &[f32],
    dst: &wgpu::Buffer,
    dst_off: usize,
    enc: &mut wgpu::CommandEncoder,
    // Some: the two projections that produce `ckv`/`csc`, encoded into this
    // step's own pass instead of two of their own. None: the caller already
    // filled them (the parity test hands them in).
    proj: Option<(&wgpu::Buffer, &wgpu::Buffer, &wgpu::Buffer)>,
) -> Option<usize> {
    let ew = if g.overlap { g.width / 2 } else { g.width };
    let span = g.ratio * g.width;
    let st = {
        let mut m = c.dsv4_comp.lock().unwrap();
        m.entry((kind, kv_id, li))
            .or_insert_with(|| {
                let mk = || {
                    c.device.create_buffer(&wgpu::BufferDescriptor {
                        label: Some("dsv4-comp-stream"),
                        size: (span * 4).max(4) as u64,
                        usage: wgpu::BufferUsages::STORAGE
                            | wgpu::BufferUsages::COPY_SRC
                            | wgpu::BufferUsages::COPY_DST,
                        mapped_at_creation: false,
                    })
                };
                [mk(), mk(), mk(), mk()]
            })
            .clone()
    };
    let (pend_kv, pend_sc, prev_kv, prev_sc) = (&st[0], &st[1], &st[2], &st[3]);
    // This token's slot in the window, and whether it closes it. Both are a
    // function of the position alone, so no counter has to live on the card.
    let slot = pos % g.ratio;
    let folds = slot + 1 == g.ratio;
    // A previous window exists once one has closed.
    let have_prev = pos + 1 > g.ratio;

    // One pass for the whole compressor step: the copies are dispatches now
    // (`blit`), which is what let them stop cutting the layer into pieces.
    let ape_all = const_buf(c, bytemuck::cast_slice(w.ape));
    let ape_slot = frame_buf(c, 74 + kind, g.width * 4, true);
    let folded = frame_buf(c, 76 + kind, ew * 4, false);
    let normed = frame_buf(c, 78 + kind, ew * 4, false);
    let nw = const_buf(c, bytemuck::cast_slice(&w.norm[..ew]));
    {
        let mut pass = begin_pass(enc);
        if let Some((wkv, wgate, hidden)) = proj {
            encode_q4tp_mvw_p(&mut pass, c, wkv, hidden, ckv, g.width, g.hidden,
                (80 + kind, kv_id, li));
            encode_q4tp_mvw_p(&mut pass, c, wgate, hidden, csc, g.width, g.hidden,
                (82 + kind, kv_id, li));
        }
        if g.overlap {
            // The reference biases the score as the token ARRIVES and keeps
            // it biased across the shift, so `ape` is added once, here, and
            // the pooling kernel is told not to add it again.
            // The strided source reads this token's slot of `ape` directly:
            // the copy into a scratch buffer existed only because axpy could
            // not offset its input.
            encode_axpy_full_p(&mut pass, c, &ape_all, &csc, 1.0, g.width, false,
                slot * g.width, Some((84 + kind, kv_id, li)));
        }
        encode_blit_p(&mut pass, c, &ckv, pend_kv, g.width, 0, slot * g.width,
            Some((146 + kind * 2, kv_id, li)));
        encode_blit_p(&mut pass, c, &csc, pend_sc, g.width, 0, slot * g.width,
            Some((150 + kind * 2, kv_id, li)));
        if !folds {
            return None;
        }
        encode_kv_pool_p(&mut pass, c, prev_kv, prev_sc, pend_kv, pend_sc, &ape_all,
            &folded, g, have_prev, kind, kv_id, li);
        encode_rmsnorm_p(&mut pass, c, &folded, &nw, &normed, ew, g.eps, (86 + kind, kv_id, li));
    // The entry carries a window key's rope tail, at the position of the
    // window's FIRST token — not this one.
        let freq = const_buf(c, bytemuck::cast_slice(&inv_freq[..g.rope_dim / 2]));
        let posb = frame_up_pos(c, 79 + kind, pos + 1 - g.ratio, g.eps);
        encode_rope_heads_p(&mut pass, c, &normed, &freq, &posb, 1, ew, g.rope_dim, false,
            false, (88 + kind, kv_id, li));
        encode_blit_p(&mut pass, c, &normed, dst, ew, 0, dst_off,
            Some((154 + kind * 2, kv_id, li)));
        if g.overlap {
            // The window that just closed becomes the previous one — the fold
            // reads half its dimensions from that stride.
            encode_blit_p(&mut pass, c, pend_kv, prev_kv, span, 0, 0,
                Some((158 + kind * 2, kv_id, li)));
            encode_blit_p(&mut pass, c, pend_sc, prev_sc, span, 0, 0,
                Some((162 + kind * 2, kv_id, li)));
        }
    }
    Some(dst_off)
}

/// Append this token's key to the sliding window, on the device: the KV
/// projection, its norm, its rope tail, and the shift that keeps the window
/// at capacity.
///
/// The last producer the host still owned. The reference keeps a ring; the
/// engine keeps the last N in order, which is the same SET but makes the
/// position list plain `0..win_len` — so the shift has to be a real shift.
/// At 128×512 floats that is a quarter of a megabyte of device-local copy a
/// layer, which is microseconds, and it buys the host's exit from the loop.
#[allow(clippy::too_many_arguments)]
pub fn dsv4_window_append(
    model: &Arc<CmfModel>,
    wkv: usize,
    kv_norm: &[f32],
    hidden: &wgpu::Buffer,
    cache: &wgpu::Buffer,
    // head_dim, the window's capacity in slots, how many are filled BEFORE
    // this token, the model's hidden width and the rope tail.
    hd: usize,
    window: usize,
    filled: usize,
    dim: usize,
    rope_dim: usize,
    eps: f32,
    pos: usize,
    inv_freq: &[f32],
    kv_id: u64,
    li: usize,
    enc: &mut wgpu::CommandEncoder,
) -> Option<usize> {
    let c = ctx()?;
    let bytes = model.primary_bytes();
    let e = model.tensors.get(wkv)?;
    if e.dtype != cortiq_core::TensorDtype::Q4TiledP {
        return None;
    }
    let abs = model.entry_abs_offset(e)?;
    let plen = e.nbytes as usize;
    bytes.get(abs..abs + plen)?;
    let wb = weight_buffer(c, (model.uid() as usize, wkv), &bytes[abs..abs + plen])?;

    let raw = frame_buf(c, 104, hd * 4, false);
    let kv = frame_buf(c, 105, hd * 4, false);
    window_place(c, enc, &raw, kv_norm, hd, window, filled, rope_dim, eps, pos,
        inv_freq, cache, kv_id, li, Some((&wb, hidden, dim)));
    Some((filled + 1).min(window))
}

/// The half of the window append that is NOT a matvec: the norm, the rope
/// tail and the slide that keeps the window at capacity. Split out so a test
/// can drive it with the projection handed in — the q4tp matvec has its own
/// parity test, these three did not have one at all.
#[allow(clippy::too_many_arguments)]
fn window_place(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    raw: &wgpu::Buffer,
    kv_norm: &[f32],
    hd: usize,
    window: usize,
    filled: usize,
    rope_dim: usize,
    eps: f32,
    pos: usize,
    inv_freq: &[f32],
    cache: &wgpu::Buffer,
    kv_id: u64,
    li: usize,
    // Some: the KV projection that fills `raw`, encoded into this step's own
    // pass. None: the caller filled it (the parity test hands it in).
    proj: Option<(&wgpu::Buffer, &wgpu::Buffer, usize)>,
) {
    let kv = frame_buf(c, 105, hd * 4, false);
    let nw = const_buf(c, bytemuck::cast_slice(&kv_norm[..hd]));
    let freq = const_buf(c, bytemuck::cast_slice(&inv_freq[..rope_dim / 2]));
    let posb = frame_up_pos(c, 108, pos, eps);
    let tmp = frame_buf(c, 110, (window.max(1) - 1).max(1) * hd * 4, true);
    let mut pass = begin_pass(enc);
    if let Some((wb, hidden, dim)) = proj {
        encode_q4tp_mvw_p(&mut pass, c, wb, hidden, raw, hd, dim, (106, kv_id, li));
    }
    encode_rmsnorm_p(&mut pass, c, raw, &nw, &kv, hd, eps, (107, kv_id, li));
    encode_rope_heads_p(&mut pass, c, &kv, &freq, &posb, 1, hd, rope_dim, false, false,
        (109, kv_id, li));

    // Where it lands, and whether the window has to slide first.
    let slot = if filled < window {
        filled
    } else {
        // Drop the oldest: everything moves down one slot. A copy whose
        // source and destination overlap is not allowed, so it goes through
        // a scratch buffer — still device-local, still no host.
        let n = (window - 1) * hd;
        encode_blit_p(&mut pass, c, cache, &tmp, n, hd, 0, Some((166, kv_id, li)));
        encode_blit_p(&mut pass, c, &tmp, cache, n, 0, 0, Some((168, kv_id, li)));
        window - 1
    };
    encode_blit_p(&mut pass, c, &kv, cache, hd, 0, slot * hd, Some((170, kv_id, li)));
}

/// Drive the norm, the rope tail and the slide from a test with the
/// projection handed in, and give back the whole window as attention would
/// read it.
#[allow(clippy::too_many_arguments)]
pub fn dsv4_window_place_for_test(
    raw: &[f32],
    kv_norm: &[f32],
    inv_freq: &[f32],
    seed: &[f32],
    hd: usize,
    window: usize,
    filled: usize,
    rope_dim: usize,
    eps: f32,
    pos: usize,
    out: &mut Vec<f32>,
) -> bool {
    let Some(c) = ctx() else { return false };
    // A POOLED buffer, not a fresh one: `encode_rmsnorm` caches its bind
    // group by key, so a new buffer every call would leave the cache
    // pointing at the first one. Production hands it `frame_buf` for the
    // same reason.
    let rb = frame_up(c, 112, bytemuck::cast_slice(&raw[..hd]));
    // Pooled for the SAME reason as `rb` above, and the reason is not
    // decorative: the final blit caches its bind group by key, so a fresh
    // buffer each call leaves that group pointing at the first one. The new
    // entry then lands in a buffer nobody reads and the slot stays zero —
    // which is what this test reported for two months as a kernel fault.
    let cache = frame_buf(c, 113, window * hd * 4, true);
    c.queue.write_buffer(&cache, 0, bytemuck::cast_slice(seed));
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
    window_place(c, &mut enc, &rb, kv_norm, hd, window, filled, rope_dim, eps, pos,
        inv_freq, &cache, 909, 0, None);
    out.clear();
    out.resize(window * hd, 0.0);
    let bytes = (window * hd * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        bytes,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "win-test-stage",
    );
    let ok = readback(c, enc, &cache, &stage, bytes, out);
    drop(sc);
    ok
}

/// What the indexer needs from the model: two q4tp projections by directory
/// index. Its own compressor goes through `dsv4_compressor_frame` as kind 1.
#[derive(Clone)]
pub struct Dsv4IxW {
    /// `[ih*idim, q_lora]` — reads the SHARED LoRA output, not attention's
    /// queries.
    pub wq_b: usize,
    /// `[ih, hidden]`.
    pub weights_proj: usize,
}

#[derive(Clone, Copy)]
pub struct Dsv4IxGeom {
    pub ih: usize,
    pub idim: usize,
    pub q_lora: usize,
    pub hidden: usize,
    pub rope_dim: usize,
    pub eps: f32,
    pub top_k: usize,
    /// The cache's window CAPACITY — where the compressed region starts.
    pub window: usize,
}

/// Score the compressed positions, take the top-k, and assemble the attended
/// list — all on the device, into `out_idx`.
///
/// Returns the list's length, which the host computes rather than reads back:
/// the window in use plus however many of the top-k there were positions for.
/// That is the whole trick — the CONTENTS are a device secret, the LENGTH
/// never was, so nothing has to come home mid-token.
#[allow(clippy::too_many_arguments)]
pub fn dsv4_indexer_frame(
    model: &Arc<CmfModel>,
    w: &Dsv4IxW,
    g: Dsv4IxGeom,
    kv_id: u64,
    li: usize,
    hidden: &wgpu::Buffer,
    qn: &wgpu::Buffer,
    index_kv: &wgpu::Buffer,
    // How many entries the indexer's own cache holds, and how many compressed
    // positions attention has: the reference scores the smaller of the two.
    n_ix: usize,
    n_comp: usize,
    win_len: usize,
    pos: usize,
    inv_freq: &[f32],
    out_idx: &wgpu::Buffer,
    enc: &mut wgpu::CommandEncoder,
) -> Option<usize> {
    let c = ctx()?;
    let limit = n_ix.min(n_comp);
    if g.ih == 0 || g.idim == 0 || limit == 0 || limit > 4096 {
        return None;
    }
    let bytes = model.primary_bytes();
    let mut wb = Vec::with_capacity(2);
    for &idx in &[w.wq_b, w.weights_proj] {
        let e = model.tensors.get(idx)?;
        if e.dtype != cortiq_core::TensorDtype::Q4TiledP || e.shape.len() != 2 {
            return None;
        }
        let abs = model.entry_abs_offset(e)?;
        let plen = e.nbytes as usize;
        bytes.get(abs..abs + plen)?;
        wb.push(weight_buffer(
            c,
            (model.uid() as usize, idx),
            &bytes[abs..abs + plen],
        )?);
    }

    let qi = frame_buf(c, 90, g.ih * g.idim * 4, false);
    let hw_raw = frame_buf(c, 91, g.ih * 4, false);
    let hw = frame_buf(c, 92, g.ih * 4, false);
    // Fixed capacities, not `limit`-sized: frame_buf keys on (tag, len), so
    // a length that walks with the sequence would change the buffer's
    // identity under the cached bind groups below. `limit` is capped at
    // 4096 on entry.
    let scores = frame_buf(c, 93, 4096 * 4, false);
    let pick = frame_buf(c, 94, g.top_k.max(1) * 4, false);
    let cnt = frame_buf(c, 95, 4, false);

    let freq = const_buf(c, bytemuck::cast_slice(&inv_freq[..g.rope_dim / 2]));
    let posb = frame_up_pos(c, 97, pos, g.eps);
    // The reference folds head_dim^-0.5 · n_heads^-0.5 into weights_proj's
    // output. A uniform positive factor cannot change which positions win,
    // but the scores are the kernel's contract, not just a ranking key.
    let sc_factor = (g.idim as f32).powf(-0.5) * (g.ih as f32).powf(-0.5);
    let k_actual = g.top_k.min(limit);
    // EIGHT dispatches, ONE pass. Each is a step of the one before it, and
    // dispatches inside a compute pass already run in order with the writes
    // of the previous one visible — so the eight passes this used to open
    // bought nothing but eight lots of driver bookkeeping, which on a small
    // layer is most of what the token costs.
    {
        let mut pass = begin_pass(enc);
        encode_q4tp_mvw_p(&mut pass, c, &wb[0], qn, &qi, g.ih * g.idim, g.q_lora,
            (96, kv_id, li));
        encode_rope_heads_p(&mut pass, c, &qi, &freq, &posb, g.ih, g.idim, g.rope_dim,
            false, false, (98, kv_id, li));
        encode_q4tp_mvw_p(&mut pass, c, &wb[1], hidden, &hw_raw, g.ih, g.hidden,
            (99, kv_id, li));
        // `set`: hw = sc_factor·hw_raw. This was a whole-buffer zero-fill
        // followed by an accumulate — two dispatches to express an
        // assignment.
        encode_axpy_full_p(&mut pass, c, &hw_raw, &hw, sc_factor, g.ih, true, 0,
            Some((101, kv_id, li)));
        encode_index_scores_p(&mut pass, c, &qi, index_kv, &hw, &scores, g.ih, g.idim,
            limit, (kv_id, li));
        encode_top_k_p(&mut pass, c, &scores, &pick, &cnt, limit, g.top_k, (kv_id, li));
        encode_idx_build_p(&mut pass, c, &pick, out_idx, win_len, g.window, k_actual,
            Some((kv_id, li)));
    }
    Some(win_len + k_actual)
}

fn encode_fill_zero(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    y: &wgpu::Buffer,
    n: usize,
    bkey: (u8, u64, usize),
) {
    let mut pass = begin_pass(enc);
    encode_fill_zero_p(&mut pass, c, y, n, bkey);
}

#[allow(clippy::too_many_arguments)]
fn encode_fill_zero_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    y: &wgpu::Buffer,
    n: usize,
    bkey: (u8, u64, usize),
) {
    let bind = cached_bind(c, bkey, || {
        let p = uniform_u32x4(c, [n as u32, 0, 0, 0]);
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.zero.get_bind_group_layout(0),
            entries: &[bind_buf(0, y), bind_buf(1, &p)],
        })
    });
    pass.set_pipeline(&c.zero);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((n as u32).div_ceil(256), 1, 1);
}

#[allow(clippy::too_many_arguments)]
fn encode_index_scores(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    q: &wgpu::Buffer,
    kv: &wgpu::Buffer,
    hw: &wgpu::Buffer,
    out: &wgpu::Buffer,
    nh: usize,
    hd: usize,
    n_pos: usize,
    bkey: (u64, usize),
) {
    let mut pass = begin_pass(enc);
    encode_index_scores_p(&mut pass, c, q, kv, hw, out, nh, hd, n_pos, bkey);
}

#[allow(clippy::too_many_arguments)]
fn encode_index_scores_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    q: &wgpu::Buffer,
    kv: &wgpu::Buffer,
    hw: &wgpu::Buffer,
    out: &wgpu::Buffer,
    nh: usize,
    hd: usize,
    n_pos: usize,
    bkey: (u64, usize),
) {
    // n_pos walks with the sequence, so the uniform lives in a per-layer
    // slot whose contents are rewritten each call; the bind group can then
    // survive across tokens.
    let p = uni_slot(c, 134, bkey.0, bkey.1, [nh as u32, hd as u32, n_pos as u32, n_pos as u32]);
    let bind = cached_bind(c, (135, bkey.0, bkey.1), || {
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.index_scores.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, q),
                bind_buf(1, kv),
                bind_buf(2, hw),
                bind_buf(3, out),
                bind_buf(4, &p),
            ],
        })
    });
    pass.set_pipeline(&c.index_scores);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((n_pos as u32).min(MAX_WG), 1, 1);
}

fn encode_top_k(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    scores: &wgpu::Buffer,
    pick: &wgpu::Buffer,
    cnt: &wgpu::Buffer,
    n: usize,
    k: usize,
    bkey: (u64, usize),
) {
    let mut pass = begin_pass(enc);
    encode_top_k_p(&mut pass, c, scores, pick, cnt, n, k, bkey);
}

#[allow(clippy::too_many_arguments)]
fn encode_top_k_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    scores: &wgpu::Buffer,
    pick: &wgpu::Buffer,
    cnt: &wgpu::Buffer,
    n: usize,
    k: usize,
    bkey: (u64, usize),
) {
    let p = uni_slot(c, 136, bkey.0, bkey.1, [n as u32, k as u32, 0, 0]);
    let bind = cached_bind(c, (137, bkey.0, bkey.1), || {
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.top_k_index.get_bind_group_layout(0),
            entries: &[
                bind_buf(0, scores),
                bind_buf(1, pick),
                bind_buf(2, cnt),
                bind_buf(3, &p),
            ],
        })
    });
    pass.set_pipeline(&c.top_k_index);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups(1, 1, 1);
}

/// NOT cached, and this one bites: `win_len` grows with the sequence and `k`
/// changes with it, so the uniform — which `uniform_u32x4` keys on its
/// CONTENTS — becomes a different buffer while a cached bind group would
/// still point at the previous token's. The first call would be right and
/// every one after it would read a stale window length.
fn encode_idx_build(
    c: &Ctx,
    enc: &mut wgpu::CommandEncoder,
    pick: &wgpu::Buffer,
    out: &wgpu::Buffer,
    win_len: usize,
    window: usize,
    k: usize,
    // The indexer path binds stable buffers and can keep its group; the
    // prep fallback's pick buffer changes identity with its length, so it
    // passes None and builds a fresh group each call.
    bkey: Option<(u64, usize)>,
) {
    let mut pass = begin_pass(enc);
    encode_idx_build_p(&mut pass, c, pick, out, win_len, window, k, bkey);
}

#[allow(clippy::too_many_arguments)]
fn encode_idx_build_p(
    pass: &mut wgpu::ComputePass<'_>,
    c: &Ctx,
    pick: &wgpu::Buffer,
    out: &wgpu::Buffer,
    win_len: usize,
    window: usize,
    k: usize,
    // The indexer path binds stable buffers and can keep its group; the
    // prep fallback's pick buffer changes identity with its length, so it
    // passes None and builds a fresh group each call.
    bkey: Option<(u64, usize)>,
) {
    let n = win_len + k;
    let mk = |p: &wgpu::Buffer| {
        c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &c.idx_build.get_bind_group_layout(0),
            entries: &[bind_buf(0, pick), bind_buf(1, out), bind_buf(2, p)],
        })
    };
    let bind = match bkey {
        Some((kvid, li)) => {
            let p = uni_slot(c, 138, kvid, li, [win_len as u32, window as u32, k as u32, 0]);
            cached_bind(c, (139, kvid, li), || mk(&p))
        }
        None => mk(&uniform_u32x4(c, [win_len as u32, window as u32, k as u32, 0])),
    };
    pass.set_pipeline(&c.idx_build);
    pass.set_bind_group(0, &bind, &[]);
    pass.dispatch_workgroups((n as u32).div_ceil(256), 1, 1);
}

/// Drive the device-side compressor state with the projections handed in,
/// one token per call, and return the folded entry on the token that closes
/// a window. The frame does exactly this after its two matvecs; giving the
/// projections from the host is what lets a test compare the STATE — the
/// slots, the fold timing, the previous-window shuffle, the rope position —
/// against `dsv4::compressor_step` without a model file.
#[allow(clippy::too_many_arguments)]
pub fn dsv4_comp_state_for_test(
    ckv: &[f32],
    csc: &[f32],
    norm: &[f32],
    ape: &[f32],
    inv_freq: &[f32],
    width: usize,
    ratio: usize,
    overlap: bool,
    rope_dim: usize,
    eps: f32,
    pos: usize,
    kv_id: u64,
    out: &mut Vec<f32>,
) -> Option<bool> {
    let c = ctx()?;
    let g = Dsv4CompGeom {
        width,
        hidden: 0,
        ratio,
        overlap,
        rope_dim,
        eps,
    };
    let ew = if overlap { width / 2 } else { width };
    let w = Dsv4CompW {
        wkv: 0,
        wgate: 0,
        norm,
        ape,
    };
    // POOLED, all three. The steps inside cache their bind groups by key, so
    // a buffer freshly created per call leaves those groups bound to the
    // first call's memory: writes land where nothing reads them and the
    // result reads as a broken kernel. Production hands these in from the
    // frame pool for exactly this reason.
    let kb = frame_up(c, 114, bytemuck::cast_slice(&ckv[..width]));
    let sb = frame_up(c, 115, bytemuck::cast_slice(&csc[..width]));
    let dst = frame_buf(c, 116, ew * 4, true);
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
    let folded = comp_state_step(
        c, &w, g, 9, kv_id, 0, &kb, &sb, pos, inv_freq, &dst, 0, &mut enc, None,
    )
    .is_some();
    if !folded {
        submit(c, enc.finish());
        let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
        return Some(false);
    }
    out.clear();
    out.resize(ew, 0.0);
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        (ew * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "comp-test-stage",
    );
    let ok = readback(c, enc, &dst, &stage, (ew * 4) as u64, out);
    drop(sc);
    ok.then_some(true)
}

/// The position-list assembly alone, driven from a test: the picks come in
/// as they would from top-k, and what comes back is what `sparse_attend`
/// would read. The shift by the window's CAPACITY (not its fill) is the part
/// worth pinning — get it wrong and attention reads the wrong keys while
/// every shape still checks out.
pub fn dsv4_idx_build_for_test(
    pick: &[u32],
    win_len: usize,
    window: usize,
    out: &mut Vec<u32>,
) -> bool {
    let Some(c) = ctx() else { return false };
    let n = win_len + pick.len();
    // A zero-length binding is a validation error, and "the indexer picked
    // nothing" is a real state — pad rather than refuse.
    let padded: Vec<u32> = if pick.is_empty() { vec![0] } else { pick.to_vec() };
    let pb = storage_bytes(c, bytemuck::cast_slice(&padded));
    let ob = c.device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: (n.max(1) * 4) as u64,
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        mapped_at_creation: false,
    });
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
    encode_idx_build(c, &mut enc, &pb, &ob, win_len, window, pick.len(), None);
    let bytes = (n * 4) as u64;
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        bytes,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "idx-build-stage",
    );
    enc.copy_buffer_to_buffer(&ob, 0, &stage, 0, bytes);
    submit(c, enc.finish());
    let slice = stage.slice(..bytes);
    slice.map_async(wgpu::MapMode::Read, |_| {});
    if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
        return false;
    }
    let mut ok = false;
    if let Ok(data) = slice.get_mapped_range() {
        out.clear();
        out.extend_from_slice(bytemuck::cast_slice(&data[..bytes as usize]));
        ok = true;
    }
    stage.unmap();
    drop(sc);
    ok
}

/// Drop a sequence's compressor streams (called with the rest of its state).
pub fn dsv4_compressor_forget(kv_id: u64) {
    if let Some(c) = ctx() {
        c.dsv4_comp.lock().unwrap().retain(|k, _| k.1 != kv_id);
    }
}

/// The one thing that has to survive between tokens. `off` and `data` are in
/// floats; the buffer is created on first use at `cap` and never shrinks.
pub fn dsv4_cache_write(kv_id: u64, li: usize, off: usize, data: &[f32], cap: usize) -> bool {
    let dbg = std::env::var("CMF_DSV4_FRAME_DEBUG").is_ok();
    let Some(c) = ctx() else {
        if dbg {
            eprintln!("кеш dsv4: нет контекста wgpu");
        }
        return false;
    };
    if off + data.len() > cap {
        if dbg {
            eprintln!("кеш dsv4: {off}+{} не влезает в {cap}", data.len());
        }
        return false;
    }
    // Storage buffers have a size ceiling of their own, well under VRAM, and
    // silently refusing at it reads as "no device" from the caller's side.
    if (cap * 4) as u64 > c.device.limits().max_storage_buffer_binding_size as u64 {
        tracing::warn!(
            "кеш dsv4: {} МБ превышает предел одного буфера {} МБ — слой остаётся на CPU",
            cap * 4 / (1 << 20),
            c.device.limits().max_storage_buffer_binding_size / (1 << 20)
        );
        return false;
    }
    let mut map = c.dsv4_kv.lock().unwrap();
    // Grow rather than refuse: the compressed axis lengthens as the sequence
    // does, and a buffer sized for token 100 is not a reason to fall off the
    // device at token 1000. The caller rewrites both regions each token, so
    // losing the old contents costs nothing.
    if map.get(&(kv_id, li)).is_some_and(|(_, have)| *have < cap) {
        map.remove(&(kv_id, li));
        GREW.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    }
    let e = map.entry((kv_id, li)).or_insert_with(|| {
        (
            c.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("dsv4-kv"),
                size: (cap * 4) as u64,
                // COPY_SRC as well: the window slide reads this buffer to
                // write it one slot down, so the cache is its own source.
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_DST
                    | wgpu::BufferUsages::COPY_SRC,
                mapped_at_creation: false,
            }),
            cap,
        )
    });
    if e.1 < off + data.len() {
        return false; // a longer context than the cache was built for
    }
    if !data.is_empty() {
        c.queue
            .write_buffer(&e.0, (off * 4) as u64, bytemuck::cast_slice(data));
    }
    true
}

/// ── the u8 tag registry ────────────────────────────────────────────────
///
/// Four separate maps key on a `u8` tag, and only the tag tells two call
/// sites apart. A collision does not crash: it hands one site the other's
/// bind group, pointing at the wrong buffers, and the model quietly gets
/// worse. The numbers in use, so the next one can be picked without reading
/// the file:
///
/// * `frame_buf` (tag, len)   — 1, 2, 9, 17, 26, 27, 40, 43, 44, 70–79,
///                              90–95, 104, 105, 108, 110, 111
/// * `frame_up_pos` (tag)     — 1, 79–80, 97, 108
/// * `uni_slot` (tag, kv, li) — 84–85, 101, 130–131, 134, 136, 138, 140,
///                              142–164 (blit, even), 166, 168, 170
/// * `cached_bind` (tag,kv,li)— 30–33, 36–42, 50–64, 80–101, 120–129,
///                              132–141, 143–165 (blit, odd), 167, 169, 171
///
/// `CMF_DSV4_SLOT_CHECK=1` turns a collision between two `uni_slot` or
/// `store_slot` sites into a panic instead of a silent wrong answer: a slot
/// written twice before one submission is either a collision or the
/// last-write-wins bug that made a run of layers route with the last
/// layer's router bias.
///
/// Bind groups for the dsv4 frames, keyed by role and layer. Their buffers
/// are pooled and stable between tokens, so building 11 of them per layer per
/// token — 473 a token on the release — was pure host overhead. The epoch
/// invalidates the lot whenever a pooled buffer is rebuilt underneath them.
fn cached_bind<F>(c: &Ctx, key: (u8, u64, usize), build: F) -> wgpu::BindGroup
where
    F: FnOnce() -> wgpu::BindGroup,
{
    use std::sync::atomic::Ordering;
    let key = (key.0, key.1, dsv4_salted_li(key.2));
    let epoch = GREW.load(Ordering::Relaxed);
    let mut m = c.dsv4_binds.lock().unwrap();
    if m.0 != epoch {
        m.0 = epoch;
        m.1.clear();
    }
    if let Some(b) = m.1.get(&key) {
        return b.clone();
    }
    let b = build();
    m.1.insert(key, b.clone());
    b
}

/// A per-(tag, sequence, layer) UNIFORM buffer, 16 bytes, written every
/// call: how a sequence-varying scalar coexists with a CACHED bind group.
/// The buffer's identity never changes, only its contents — the opposite
/// trade from the content-keyed uniform pool, whose identity IS its
/// contents.
/// `uni_slot`'s eight-word twin, for the kernels whose params do not fit in
/// four. Same contract: one write per key per submission.
fn uni_slot8(c: &Ctx, tag: u8, kv: u64, li: usize, vals: [u32; 8]) -> wgpu::Buffer {
    let li = dsv4_salted_li(li);
    let b = {
        let mut m = c.dsv4_uni.lock().unwrap();
        m.entry((tag, kv, li))
            .or_insert_with(|| {
                c.device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some("dsv4-uni-slot8"),
                    size: 32,
                    usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                })
            })
            .clone()
    };
    note_slot_write(c, "uni8", (tag, kv, li));
    c.queue.write_buffer(&b, 0, bytemuck::cast_slice(&vals));
    b
}

fn uni_slot(c: &Ctx, tag: u8, kv: u64, li: usize, vals: [u32; 4]) -> wgpu::Buffer {
    let li = dsv4_salted_li(li);
    let b = {
        let mut m = c.dsv4_uni.lock().unwrap();
        m.entry((tag, kv, li))
            .or_insert_with(|| {
                c.device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some("dsv4-uni-slot"),
                    size: 16,
                    usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                })
            })
            .clone()
    };
    note_slot_write(c, "uni", (tag, kv, li));
    c.queue.write_buffer(&b, 0, bytemuck::cast_slice(&vals));
    b
}

/// `uni_slot`'s storage twin: a per-(tag, sequence, layer) STORAGE buffer
/// rewritten every call. What it buys is the hash layers — their forced
/// expert list changes per token and used to go through the (tag, len) pool,
/// where every layer of a run shared one buffer and the last write won. With
/// a buffer per layer they route with their own table row and can share a
/// submission with everyone else.
fn store_slot(c: &Ctx, tag: u8, kv: u64, li: usize, data: &[u8]) -> wgpu::Buffer {
    let li = dsv4_salted_li(li);
    let size = (data.len().max(4) as u64).div_ceil(16) * 16;
    let b = {
        let mut m = c.dsv4_store.lock().unwrap();
        let e = m.entry((tag, kv, li));
        match e {
            std::collections::hash_map::Entry::Occupied(o) if o.get().size() >= size => {
                o.get().clone()
            }
            other => {
                let b = c.device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some("dsv4-store-slot"),
                    size,
                    usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                });
                match other {
                    std::collections::hash_map::Entry::Occupied(mut o) => {
                        o.insert(b.clone());
                        // A cached bind group would otherwise outlive the
                        // buffer it names.
                        GREW.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                    }
                    std::collections::hash_map::Entry::Vacant(v) => {
                        v.insert(b.clone());
                    }
                }
                b
            }
        }
    };
    if !data.is_empty() {
        note_slot_write(c, "store", (tag, kv, li));
        c.queue.write_buffer(&b, 0, data);
    }
    b
}

/// Bumped whenever a cache buffer is reallocated. A caller that writes only
/// the tail has to notice, because the new buffer holds nothing.
pub static GREW: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// Drop a conversation's caches (a new sequence, or the pipeline resetting).
pub fn dsv4_cache_clear(kv_id: u64) {
    if let Some(c) = ctx() {
        c.dsv4_kv.lock().unwrap().retain(|k, _| k.0 != kv_id);
    }
}

/// The quantized tensors one DeepSeek-V4 attention block reads, by directory
/// index, plus the two small f32 vectors it needs whole.
#[derive(Clone)]
pub struct Dsv4AttnW<'a> {
    pub wq_a: usize,
    pub wq_b: usize,
    pub wo_a: usize,
    pub wo_b: usize,
    pub q_norm: &'a [f32],
    pub sink: &'a [f32],
}

/// Shapes for one block. Separate from the weights so the caller can build it
/// once per layer and keep it.
#[derive(Clone, Copy)]
pub struct Dsv4AttnGeom {
    pub dim: usize,
    pub nh: usize,
    pub hd: usize,
    pub rd: usize,
    pub q_lora: usize,
    pub o_lora: usize,
    pub o_groups: usize,
    pub eps: f32,
    pub scale: f32,
}

/// DeepSeek-V4's attention block, start to finish, in ONE submission.
///
/// Eight operations that were eight round trips: the query LoRA and its two
/// norms, the rope tail, attention over the index list, the inverse rope, and
/// the grouped output projection. Nothing between them touches the host — the
/// intermediate vectors never leave the card, and the KV cache is already
/// there.
///
/// The kv vector itself stays on the CPU deliberately: the compressor's
/// pending windows are host state, and pulling one 512-wide vector back is
/// cheaper than moving that state too. That is the next frame to fuse, not a
/// thing forgotten here.
#[allow(clippy::too_many_arguments)]
/// The hyper-connection work that sits between a layer's two halves, so the
/// frame can do it instead of the host.
///
/// Measured: these are 19 ms of a 57 ms token on the CPU and hundredths of a
/// millisecond on the card. Nothing about them needs the host — they were
/// only there because the frames handed their output back.
pub struct Dsv4HcTail<'a> {
    /// The FFN half's projection, scales and base.
    pub fn_: &'a [f32],
    pub scale: &'a [f32; 3],
    pub base: &'a [f32],
    /// The norm applied to the fold that feeds the MoE half.
    pub norm: &'a [f32],
    pub hc: usize,
    pub sinkhorn_iters: usize,
    pub hc_eps: f32,
    /// RMS epsilon for the norm that follows the fold.
    pub eps: f32,
}

#[allow(clippy::too_many_arguments)]
pub fn dsv4_attn_frame(
    model: &Arc<CmfModel>,
    w: &Dsv4AttnW,
    g: Dsv4AttnGeom,
    hidden: &[f32],
    // The layer's own `q_norm(wq_a(x))`, when the caller already has it — the
    // indexer needs that vector on the host anyway, and computing it twice is
    // worse than uploading 1536 floats. `None` puts both ops in the frame.
    qn_in: Option<&[f32]>,
    kv_id: u64,
    li: usize,
    idxs: &[u32],
    inv_freq: &[f32],
    pos: usize,
    // When present the frame also expands its output into the layer state,
    // folds the FFN half and norms it, and hands back THAT — the MoE half's
    // input — instead of the attention output.
    hc: Option<&Dsv4HcTail>,
    out: &mut [f32],
) -> bool {
    // A refusal used to be a silent `false`, and three of them in a row cost
    // an evening of guessing which guard had fired.
    macro_rules! no {
        ($($t:tt)*) => {{
            tracing::debug!("кадр dsv4 отклонён: {}", format_args!($($t)*));
            if std::env::var("CMF_DSV4_FRAME_DEBUG").is_ok() {
                eprintln!("кадр dsv4 отклонён: {}", format_args!($($t)*));
            }
            return false;
        }};
    }
    let Some(c) = ctx() else { no!("нет контекста wgpu") };
    // `hidden` is only read when the frame has to build the LoRA vector
    // itself; demanding it regardless refused every caller that had one.
    if (qn_in.is_none() && hidden.len() < g.dim)
        // Empty is a CONTRACT, not a mistake: "leave the result on the
        // card". The layer-frame refactor hit this exact guard-versus-branch
        // ordering and documented it; this is the second instance.
        || (!out.is_empty() && out.len() < g.dim)
        || w.sink.len() < g.nh
        || w.q_norm.len() < g.q_lora
        || idxs.is_empty()
        || idxs.len() > 1024
        || inv_freq.len() * 2 < g.rd
    {
        no!(
            "формы: hidden {} dim {} out {} sink {} nh {} q_norm {} q_lora {} idx {} freq {} rd {}",
            hidden.len(), g.dim, out.len(), w.sink.len(), g.nh,
            w.q_norm.len(), g.q_lora, idxs.len(), inv_freq.len(), g.rd
        );
    }
    let bytes = model.primary_bytes();
    // Every weight q4tp, or the frame declines: a mixed layer would need the
    // per-op branches back and this is not the place to guess a layout.
    let mut wb = Vec::with_capacity(4);
    for &idx in &[w.wq_a, w.wq_b, w.wo_a, w.wo_b] {
        let Some(e) = model.tensors.get(idx) else {
            no!("тензора {idx} нет в каталоге");
        };
        if e.dtype != cortiq_core::TensorDtype::Q4TiledP || e.shape.len() != 2 {
            no!("{} не q4tp ({:?}, {:?})", e.name, e.dtype, e.shape);
        }
        let Some(abs) = model.entry_abs_offset(e) else {
            no!("{} без абсолютного смещения", e.name);
        };
        let plen = e.nbytes as usize;
        if abs + plen > bytes.len() {
            no!("{} выходит за файл", e.name);
        }
        let Some(b) = weight_buffer(c, (model.uid() as usize, idx), &bytes[abs..abs + plen])
        else {
            no!("{} не поместился в бюджет VRAM", e.name);
        };
        wb.push(b);
    }
    let cache = {
        let map = c.dsv4_kv.lock().unwrap();
        match map.get(&(kv_id, li)) {
            Some((b, _)) => b.clone(),
            None => no!("кеш ({kv_id}, {li}) не заведён"),
        }
    };

    if let Some(v) = qn_in {
        if v.len() < g.q_lora {
            no!("готовый qn короче q_lora: {} < {}", v.len(), g.q_lora);
        }
    }
    // Constants (q_norm, sink, inv_freq) go through the const cache keyed on
    // their address — they are the same bytes every token. Everything else is
    // a reused buffer written in place.
    let hb = match qn_in {
        None => frame_up(c, 0, bytemuck::cast_slice(&hidden[..g.dim])),
        Some(_) => frame_buf(c, 0, 4, true),
    };
    // These three ARE model-owned and outlive the run, so address keying is
    // sound for them — unlike anything built per call.
    let qnw = const_buf(c, bytemuck::cast_slice(&w.q_norm[..g.q_lora]));
    let sink = const_buf(c, bytemuck::cast_slice(&w.sink[..g.nh]));
    let freq = const_buf(c, bytemuck::cast_slice(&inv_freq[..g.rd / 2]));
    let posb = frame_up_pos(c, 1, pos, g.eps);
    // The list length changes token to token; round the buffer up so it is
    // not reallocated on every step, and pass the true count in the uniform.
    let ixb = {
        let cap = idxs.len().next_power_of_two().max(64);
        let b = frame_buf(c, 2, cap * 4, true);
        c.queue.write_buffer(&b, 0, bytemuck::cast_slice(idxs));
        b
    };

    // Readable, all of them: `CMF_DSV4_FRAME_TAP` reads back an intermediate
    // instead of the output. Eight verified kernels can still be wired wrong,
    // and a single number at the end says only that they were.
    let qr = frame_buf(c, 3, g.q_lora * 4, false);
    let qn = match qn_in {
        Some(v) => frame_up(c, 4, bytemuck::cast_slice(&v[..g.q_lora])),
        None => frame_buf(c, 4, g.q_lora * 4, true),
    };
    let q = frame_buf(c, 5, g.nh * g.hd * 4, false);
    let attn = frame_buf(c, 6, g.nh * g.hd * 4, false);
    let mid = frame_buf(c, 7, g.o_groups * g.o_lora * 4, false);
    let yb = frame_buf(c, 8, g.dim * 4, false);

    let t_all = std::time::Instant::now();
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-attn"),
        });
    if qn_in.is_none() {
        encode_q4tp_mv1(c, &mut enc, &wb[0], &hb, &qr, g.q_lora, g.dim, (30, kv_id, li));
        encode_rmsnorm(c, &mut enc, &qr, &qnw, &qn, g.q_lora, g.eps, (31, kv_id, li));
    }
    encode_q4tp_mv1(c, &mut enc, &wb[1], &qn, &q, g.nh * g.hd, g.q_lora, (32, kv_id, li));
    encode_rope_heads(
        c, &mut enc, &q, &freq, &posb, g.nh, g.hd, g.rd, true, false, (33, kv_id, li),
    );
    encode_sparse_attend2(
        c, &mut enc, &q, &cache, &ixb, &sink, &attn, g.nh, g.hd, idxs.len(), g.scale, None,
    );
    encode_rope_heads(
        c, &mut enc, &attn, &freq, &posb, g.nh, g.hd, g.rd, false, true, (34, kv_id, li),
    );
    {
        let rows = g.o_groups * g.o_lora;
        let cols = g.nh * g.hd / g.o_groups;
        let bind = cached_bind(c, (36, kv_id, li), || {
            let p = uniform_u32x4(c, [(cols / 32) as u32, rows as u32, g.o_lora as u32, 0]);
            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &c.o_lora_a.get_bind_group_layout(0),
                entries: &[
                    bind_buf(0, &wb[2]),
                    bind_buf(1, &attn),
                    bind_buf(2, &mid),
                    bind_buf(3, &p),
                ],
            })
        });
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.o_lora_a);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups((rows as u32).min(MAX_WG), 1, 1);
    }
    encode_q4tp_mv1(
        c,
        &mut enc,
        &wb[3],
        &mid,
        &yb,
        g.dim,
        g.o_groups * g.o_lora,
        (35, kv_id, li),
    );

    // ── the hyper-connections, when the caller handed them over ──
    // The same order the host's hc_block keeps: expand this half's output
    // into the layer state, mix, fold with the FFN half's parameters, norm.
    // What comes out is the MoE half's INPUT, so the host has nothing left
    // to do between the two frames — which is 19 ms of a 57 ms token.
    let hc_out = hc.map(|h| {
        let mix_hc = (2 + h.hc) * h.hc;
        let state = frame_buf(c, 40, h.hc * g.dim * 4, true);
        let state2 = frame_buf(c, 46, h.hc * g.dim * 4, true);
        let hpost = frame_buf(c, 43, h.hc * 4, true);
        let hcomb = frame_buf(c, 44, h.hc * h.hc * 4, true);
        let mixes = frame_buf(c, 41, mix_hc * 4, true);
        let folded = frame_buf(c, 42, g.dim * 4, true);
        let x2 = frame_buf(c, 45, g.dim * 4, true);
        let hcp = uniform_u32x8(
            c,
            [h.hc as u32, g.dim as u32, h.sinkhorn_iters as u32, h.hc_eps.to_bits(), 0, 0, 0, 0],
        );
        let ffn_fn = const_buf(c, bytemuck::cast_slice(h.fn_));
        let ffn_sc = const_buf(c, bytemuck::cast_slice(h.scale));
        let ffn_bs = const_buf(c, bytemuck::cast_slice(&h.base[..mix_hc]));
        let ffn_nw = const_buf(c, bytemuck::cast_slice(&h.norm[..g.dim]));
        encode_hc_expand(c, &mut enc, &yb, &state, &hpost, &hcomb, &state2, &hcp, h.hc, g.dim);
        encode_f32matvec(c, &mut enc, &ffn_fn, &state2, &mixes, mix_hc, h.hc * g.dim);
        encode_hc_fold(
            c, &mut enc, &state2, &mixes, &ffn_sc, &ffn_bs, &folded, &hpost, &hcomb, &hcp,
        );
        encode_rmsnorm(c, &mut enc, &folded, &ffn_nw, &x2, g.dim, h.eps, (54, kv_id, li));
        x2
    });

    let tap = std::env::var("CMF_DSV4_FRAME_TAP").unwrap_or_default();
    let (src, n) = match tap.as_str() {
        "qr" => (&qr, g.q_lora),
        "qn" => (&qn, g.q_lora),
        "q" => (&q, g.nh * g.hd),
        "attn" => (&attn, g.nh * g.hd),
        "mid" => (&mid, g.o_groups * g.o_lora),
        _ => (hc_out.as_ref().unwrap_or(&yb), g.dim),
    };
    // An EMPTY `out` means the caller wants the result left where it is: the
    // MoE frame reads it from the same buffer, so the token does not stop
    // here at all.
    if out.is_empty() {
        submit(c, enc.finish());
        ATT_ENC_NS.fetch_add(
            t_all.elapsed().as_nanos() as u64,
            std::sync::atomic::Ordering::Relaxed,
        );
        return true;
    }
    if out.len() < n {
        no!("отвод {tap} нуждается в {n} значениях, дано {}", out.len());
    }
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        (n * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dsv4-attn-stage",
    );
    // Resolve every pair the passes above took, into the staging buffer
    // back to back — one copy per pass, all inside this frame's encoder.
    let pairs: Vec<(usize, u32)> = std::mem::take(&mut TS_PAIRS.lock().unwrap());
    if let Some((qs, resolve, tstage)) = &c.ts_query {
        for (n, (_, slot)) in pairs.iter().enumerate() {
            enc.resolve_query_set(qs, *slot..*slot + 2, resolve, 0);
            enc.copy_buffer_to_buffer(resolve, 0, tstage, (n as u64) * 16, 16);
        }
    }
    let t_enc = std::time::Instant::now();
    let ok = readback(c, enc, src, &stage, (n * 4) as u64, &mut out[..n]);
    // The card's clock, read after the frame's own fence.
    if ok && !pairs.is_empty() {
        if let Some((_, _, tstage)) = &c.ts_query {
            let bytes = (pairs.len() as u64) * 16;
            let (tx, rx) = std::sync::mpsc::channel();
            tstage.map_async(wgpu::MapMode::Read, ..bytes, move |r| {
                let _ = tx.send(r);
            });
            let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
            if rx.recv().map(|r| r.is_ok()).unwrap_or(false) {
                if let Ok(raw) = tstage.get_mapped_range(..bytes) {
                    let t: &[u64] = bytemuck::cast_slice(&raw);
                    for (n, (which, _)) in pairs.iter().enumerate() {
                        let d = t[2 * n + 1].saturating_sub(t[2 * n]);
                        let ns = (d as f64 * c.ts_period as f64) as u64;
                        ATT_GPU_NS[*which].fetch_add(ns, std::sync::atomic::Ordering::Relaxed);
                    }
                    drop(raw);
                    ATT_GPU_N.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                }
            }
            tstage.unmap();
        }
    }
    drop(sc);
    ATT_ENC_NS.fetch_add(
        t_enc.duration_since(t_all).as_nanos() as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    ATT_WAIT_NS.fetch_add(
        t_enc.elapsed().as_nanos() as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    ok
}

/// Time inside `dsv4_attn_frame`, split at the submit — the same question the
/// MoE frame already answers, asked of the block that now costs more.
pub static ATT_ENC_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
pub static ATT_WAIT_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// One DeepSeek-V4 MoE block on the device: route, run the chosen experts and
/// the shared one, sum. One submission.
///
/// The experts arrive PACKED — a subset chosen by the host (the hot set a
/// mask keeps), shared expert last — and the router's logits arrive already
/// renumbered into that packing. That removes the whole global-to-slot remap
/// the obvious design needs, and it makes the mask implicit: an expert not in
/// the packing has no logit and cannot be chosen.
#[derive(Clone)]
pub struct Dsv4MoeW<'a> {
    /// The gate as dense f32 `[n_exp, hidden]`. Used when `logits` is empty,
    /// which is what a device-resident input forces: the host cannot score
    /// a vector it does not have.
    pub router: &'a [f32],
    /// `(gate, up, down)` directory indices per packed expert, shared LAST.
    pub experts: &'a [(usize, usize, usize)],
    /// Router logits over the packed routed experts (shared excluded).
    pub logits: &'a [f32],
    /// noaux_tc selection bias, same numbering. Absent on the hash layers.
    pub bias: Option<&'a [f32]>,
    /// Hash-layer row, already in packed numbering.
    pub forced: Option<&'a [usize]>,
    /// global expert id -> packed slot, `u32::MAX` where the expert did not
    /// fit. When present the router ranges over ALL experts and hands the
    /// cold picks back instead of avoiding them.
    pub remap: Option<&'a [u32]>,
}

#[derive(Clone, Copy)]
pub struct Dsv4MoeGeom {
    pub hidden: usize,
    pub inter: usize,
    pub top_k: usize,
    pub route_scale: f32,
    pub swiglu_limit: f32,
    /// gate/up are q2tp against a q4tp down — the mixed 2-bit profile.
    pub gu_q2: bool,
}

pub fn dsv4_moe_frame(
    model: &Arc<CmfModel>,
    w: &Dsv4MoeW,
    g: Dsv4MoeGeom,
    // EMPTY means the attention frame left this half's input on the card in
    // its own buffer, which is the whole point: with the hyper-connections
    // done there too, the host has nothing to carry between the halves.
    x: &[f32],
    // `(expert, weight)` pairs the device left for the host, empty when the
    // whole packing was resident.
    cold_out: &mut Vec<(usize, f32)>,
    // The normalized FFN input the cold experts consume. When `x` above is
    // empty that vector exists only in the device frame; return it beside the
    // cold IDs so disk-backed CPU completion has real activations.
    cold_x_out: &mut Vec<f32>,
    // The state handover, split the way the layer frame splits it and for
    // the same reason: the EXPANSION of this half's output into the state is
    // unconditional whenever the device owns the state — the last layer has
    // no next fold, but its MoE half still has to enter the state the head
    // reads, and making the whole tail conditional is exactly how it did
    // not. `hc_cur` drives the expand; `hc_next` the next layer's fold.
    hc_cur: Option<&Dsv4HcTail>,
    hc_next: Option<(&Dsv4HcTail, &[f32])>,
    out: &mut [f32],
) -> bool {
    macro_rules! no {
        ($($t:tt)*) => {{
            tracing::debug!("кадр MoE отклонён: {}", format_args!($($t)*));
            if std::env::var("CMF_DSV4_FRAME_DEBUG").is_ok() {
                eprintln!("кадр MoE отклонён: {}", format_args!($($t)*));
            }
            return false;
        }};
    }
    let t_all = std::time::Instant::now();
    let Some(c) = ctx() else { no!("нет контекста wgpu") };
    let n_pack = w.experts.len().saturating_sub(1); // routed; shared is last
    let slots = g.top_k + 1;
    let n_all = if w.logits.is_empty() {
        w.router.len() / g.hidden.max(1)
    } else {
        w.logits.len()
    };
    let subset = w.remap.is_some_and(|r| r.len() >= n_all) && n_all > 0;
    // ONE width for the whole routing side: the scores, the bias and the
    // uniform must agree, and they did not. The bias went in n_pack long
    // while the kernel ranked over n_all, so every index past the packing
    // boundary read the LAST bias entry — WGSL clamps an out-of-bounds read
    // rather than faulting, so it looked like a plausible number and the
    // router quietly preferred the packed experts.
    let n_route = if subset { n_all } else { n_pack };
    if n_pack == 0
        || (!w.logits.is_empty() && w.logits.len() < n_route)
        || n_pack > 1024
        || g.top_k == 0
        || g.top_k > 63
        || (!x.is_empty() && x.len() < g.hidden)
        || out.len() < g.hidden
        || g.hidden % 32 != 0
        || g.inter % 32 != 0
    {
        no!(
            "формы: упаковано {n_pack} логитов {} top_k {} hidden {} inter {}",
            w.logits.len(),
            g.top_k,
            g.hidden,
            g.inter
        );
    }
    let t_bufs = std::time::Instant::now();
    let Some((gate_all, up_all, down_all)) =
        moe_expert_bufs(c, model, w.experts, g.inter, g.hidden, true, g.gu_q2, false)
    else {
        no!("эксперты не поместились в бюджет VRAM");
    };
    MOE_BUFS_NS.fetch_add(
        t_bufs.elapsed().as_nanos() as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    let t_up = std::time::Instant::now();

    // ── routing, on the device, straight into the msel/mwt the kernels read ──
    let lg = if w.logits.is_empty() {
        // Score on the card, from the input that is already there.
        let rb = const_buf(c, bytemuck::cast_slice(&w.router[..n_route * g.hidden]));
        // upload=true even on the device-scored arm: the pool keys buffers
        // by (tag, tok, len) and the OTHER arm of this `if` fills the same
        // slot with `write_buffer`. Whichever arm ran first used to fix the
        // usage flags for the whole process — on a card small enough to
        // route some layers on the host and some on the device, the second
        // pattern died on a COPY_DST validation. An unused COPY_DST is free.
        let lb = frame_buf(c, 16, n_route * 4, true);
        let mut e0 = c
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
        let xin = frame_buf(c, 45, g.hidden * 4, true);
        encode_f32matvec(c, &mut e0, &rb, &xin, &lb, n_route, g.hidden);
        submit(c, e0.finish());
        lb
    } else {
        frame_up(c, 16, bytemuck::cast_slice(&w.logits[..n_route]))
    };
    // NOT const_buf: that cache is keyed on the host ADDRESS, which is only
    // meaningful for model weights that outlive the process. The bias arrives
    // in a Vec built per layer, and the allocator hands back the same address
    // layer after layer — so every layer was routed with layer zero's bias.
    // The toys never caught it because they carry no expert_bias at all.
    let bs = match w.bias {
        Some(b) if b.len() >= n_route => {
            frame_up(c, 25, bytemuck::cast_slice(&b[..n_route]))
        }
        _ => lg.clone(),
    };
    let rmb = match w.remap {
        Some(r) if subset => frame_up(c, 26, bytemuck::cast_slice(&r[..n_all])),
        _ => frame_buf(c, 26, n_all.max(1) * 4, true),
    };
    let coldb = frame_buf(c, 27, 4 * g.top_k * 4, false);
    let mk = frame_buf(c, 17, n_pack * 4, true);
    let fc = match w.forced {
        Some(f) if f.len() >= g.top_k => {
            let v: Vec<u32> = f[..g.top_k].iter().map(|&i| i as u32).collect();
            frame_up(c, 18, bytemuck::cast_slice(&v))
        }
        _ => frame_buf(c, 18, g.top_k * 4, true),
    };
    let msel = frame_buf(c, 19, slots * 4, false);
    let mwt = frame_buf(c, 20, slots * 4, false);
    let mcnt = frame_buf(c, 21, 4, false);
    let mact = frame_buf(c, 22, slots * g.inter * 4, false);
    let xb = if x.is_empty() {
        frame_buf(c, 45, g.hidden * 4, true)
    } else {
        frame_up(c, 23, bytemuck::cast_slice(&x[..g.hidden]))
    };
    let ob = frame_buf(c, 24, g.hidden * 4, false);

    let rflags = (w.bias.is_some_and(|b| b.len() >= n_route) as u32)
        | ((w.forced.is_some_and(|f| f.len() >= g.top_k) as u32) << 2)
        | 8 // always pin the shared slot: these kernels take a fixed count
        | ((subset as u32) << 4)
        | ((n_pack as u32) << 8); // where the shared expert actually sits
    if std::env::var("CMF_DSV4_MOE_CHECK").is_ok() {
        eprintln!(
            "[маршрут] n_all={n_all} n_pack={n_pack} subset={subset} flags={rflags} \
             remap[0..12]={:?}",
            w.remap.map(|r| &r[..12.min(r.len())])
        );
    }
    // Ranking ranges over EVERY expert when the packing is a subset — that is
    // the whole point. Passing n_pack here silently turned it back into a
    // mask that also indexed the packed buffer with global ids.
    let rp = uniform_mixed(
        c,
        [n_route as u32, g.top_k as u32, rflags],
        g.route_scale,
    );

    let stride16 = |rows: usize, cols: usize, q2: bool| -> u32 {
        let dt = if q2 {
            cortiq_core::TensorDtype::Q2TiledP
        } else {
            cortiq_core::TensorDtype::Q4TiledP
        };
        (cortiq_core::quant::expected_nbytes(dt, &[rows, cols]).unwrap_or(0) / 2) as u32
    };
    let gu_u = uniform_u32x8(
        c,
        [
            (g.hidden / 32) as u32,
            g.inter as u32,
            slots as u32,
            stride16(g.inter, g.hidden, g.gu_q2),
            g.swiglu_limit.to_bits(),
            0,
            0,
            0,
        ],
    );
    let dn_u = uniform_u32x4(
        c,
        [
            (g.inter / 32) as u32,
            g.hidden as u32,
            slots as u32,
            stride16(g.hidden, g.inter, false),
        ],
    );
    let (p_gu, p_dn, l_gu, l_dn) = if g.gu_q2 {
        (
            &c.moe_gate_up_q2tp,
            &c.moe_down_q4tp,
            &c.layout_moe_gu_q2tp,
            &c.layout_moe_dn_q4tp,
        )
    } else {
        (
            &c.moe_gate_up_q4tp_b,
            &c.moe_down_q4tp_b,
            &c.layout_moe_gu_b,
            &c.layout_moe_dn_b,
        )
    };

    MOE_UP_NS.fetch_add(
        t_up.elapsed().as_nanos() as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    let t_pass = std::time::Instant::now();
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("dsv4-moe"),
        });
    // The layer's identity for the bind cache: its first expert's directory
    // index, which is unique per layer and already at hand.
    let lkey = w.experts.first().map(|e| e.0).unwrap_or(0);
    if w.logits.is_empty() {
        let rb = const_buf(c, bytemuck::cast_slice(&w.router[..n_route * g.hidden]));
        let xin = frame_buf(c, 45, g.hidden * 4, true);
        encode_f32matvec(c, &mut enc, &rb, &xin, &lg, n_route, g.hidden);
    }
    {
        // NOT cached. This group holds `rp`, a CONTENT-keyed uniform: change a
        // flag and the uniform becomes a different buffer while the cached
        // group keeps pointing at the old one — the layer then routes with
        // yesterday's flags forever. Encoding it costs 0.01 ms a layer; being
        // wrong costs a model.
        let _ = lkey;
        let bind = {
            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: &c.moe_route.get_bind_group_layout(0),
                entries: &[
                    bind_buf(0, &lg),
                    bind_buf(1, &bs),
                    bind_buf(2, &mk),
                    bind_buf(3, &fc),
                    bind_buf(4, &msel),
                    bind_buf(5, &mwt),
                    bind_buf(6, &mcnt),
                    bind_buf(7, &rp),
                    bind_buf(8, &rmb),
                    bind_buf(9, &coldb),
                ],
            })
        };
        // The card's own clock around the whole MoE block. All three kernels
        // share this pass; splitting it to time them apart would add two
        // pass boundaries a layer and measure the split.
        //
        // A FRESH pair of slots: rewriting the same two on every frame of
        // every token leaves the queries unreset between submissions, and an
        // unreset timestamp query reads back as zero.
        let slot = (TS_SLOT.fetch_add(2, std::sync::atomic::Ordering::Relaxed) % 254) as u32;
        TS_LAST.store(slot, std::sync::atomic::Ordering::Relaxed);
        let tsw = c.ts_query.as_ref().map(|(qs, _, _)| wgpu::ComputePassTimestampWrites {
            query_set: qs,
            beginning_of_pass_write_index: Some(slot),
            end_of_pass_write_index: Some(slot + 1),
        });
        let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: None,
            timestamp_writes: tsw,
        });
        pass.set_pipeline(&c.moe_route);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(1, 1, 1);

        let bg_gu = cached_bind(c, (41, 0, lkey), || {
            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: l_gu,
                entries: &[
                    bind_buf(0, &gate_all),
                    bind_buf(1, &up_all),
                    bind_buf(2, &xb),
                    bind_buf(3, &msel),
                    bind_buf(4, &mact),
                    bind_buf(5, &gu_u),
                ],
            })
        });
        pass.set_pipeline(p_gu);
        pass.set_bind_group(0, &bg_gu, &[]);
        pass.dispatch_workgroups(g.inter as u32, slots as u32, 1);

        let bg_dn = cached_bind(c, (42, 0, lkey), || {
            c.device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: None,
                layout: l_dn,
                entries: &[
                    bind_buf(0, &down_all),
                    bind_buf(1, &mact),
                    bind_buf(2, &msel),
                    bind_buf(3, &mwt),
                    bind_buf(4, &ob),
                    bind_buf(5, &dn_u),
                ],
            })
        });
        pass.set_pipeline(p_dn);
        pass.set_bind_group(0, &bg_dn, &[]);
        pass.dispatch_workgroups(g.hidden as u32, 1, 1);
    }
    // ── the state handover, on the card ──
    if let Some(h) = hc_cur {
        let state = frame_buf(c, 40, h.hc * g.hidden * 4, true);
        let state2 = frame_buf(c, 46, h.hc * g.hidden * 4, true);
        let hpost = frame_buf(c, 43, h.hc * 4, true);
        let hcomb = frame_buf(c, 44, h.hc * h.hc * 4, true);
        let hcp = uniform_u32x8(
            c,
            [h.hc as u32, g.hidden as u32, h.sinkhorn_iters as u32, h.hc_eps.to_bits(), 0, 0, 0, 0],
        );
        encode_hc_expand(c, &mut enc, &ob, &state2, &hpost, &hcomb, &state, &hcp, h.hc, g.hidden);
    }
    let hc_out = hc_next.map(|(h, next_norm)| {
        let mix_hc = (2 + h.hc) * h.hc;
        let state = frame_buf(c, 40, h.hc * g.hidden * 4, true);
        let state2 = frame_buf(c, 46, h.hc * g.hidden * 4, true);
        let hpost = frame_buf(c, 43, h.hc * 4, true);
        let hcomb = frame_buf(c, 44, h.hc * h.hc * 4, true);
        let mixes = frame_buf(c, 41, mix_hc * 4, true);
        let folded = frame_buf(c, 42, g.hidden * 4, true);
        let x2 = frame_buf(c, 45, g.hidden * 4, true);
        let hcp = uniform_u32x8(
            c,
            [h.hc as u32, g.hidden as u32, h.sinkhorn_iters as u32, h.hc_eps.to_bits(), 0, 0, 0, 0],
        );
        let nfn = const_buf(c, bytemuck::cast_slice(h.fn_));
        let nsc = const_buf(c, bytemuck::cast_slice(h.scale));
        let nbs = const_buf(c, bytemuck::cast_slice(&h.base[..mix_hc]));
        let nnw = const_buf(c, bytemuck::cast_slice(&next_norm[..g.hidden]));
        encode_f32matvec(c, &mut enc, &nfn, &state, &mixes, mix_hc, h.hc * g.hidden);
        encode_hc_fold(
            c, &mut enc, &state, &mixes, &nsc, &nbs, &folded, &hpost, &hcomb, &hcp,
        );
        encode_rmsnorm(c, &mut enc, &folded, &nnw, &x2, g.hidden, h.eps, (55, 0, lkey));
        x2
    });

    if let Some((qs, resolve, tstage)) = &c.ts_query {
        let slot = TS_LAST.load(std::sync::atomic::Ordering::Relaxed);
        // An UNCONDITIONAL value in the staging buffer before the copy. If it
        // comes back, the copy never landed; if it comes back zeroed, the
        // copy landed and the queries themselves are empty. Two answers, one
        // run — the same trick that separated a kernel from its readback in
        // the cold-expert path.
        if std::env::var("CMF_TS_DEBUG").is_ok() {
            let mark: [u64; 2] = [0xDEAD_BEEF_1111, 0xDEAD_BEEF_2222];
            c.queue.write_buffer(tstage, 0, bytemuck::cast_slice(&mark));
        }
        // Offset ZERO, always: a query resolve's destination offset has to be
        // 256-byte aligned, and slot*8 is not for any slot but the first
        // thirty-two. The pair still comes from the rotating slots; only
        // where it lands is fixed.
        enc.resolve_query_set(qs, slot..slot + 2, resolve, 0);
        enc.copy_buffer_to_buffer(resolve, 0, tstage, 0, 16);
    }
    let t_enc = std::time::Instant::now();
    MOE_PASS_NS.fetch_add(
        t_pass.elapsed().as_nanos() as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    let mut sc = c.scratch.lock().unwrap();
    // The cold list rides the SAME staging buffer and the SAME fence, so the
    // host learns which picks it owes without paying a second barrier.
    //
    // This block went missing once — `cold_out` was declared, threaded all
    // the way down and never filled — and the empty list read exactly like a
    // router that had chosen no cold experts. An unconditional probe written
    // from the kernel is what proved otherwise.
    let cold_bytes = (4 * g.top_k * 4) as u64;
    let x_off = (g.hidden * 4) as u64 + cold_bytes;
    let total = x_off + (g.hidden * 4) as u64;
    // ONE ensure for the whole readback. A first call sized to the hidden
    // state alone used to run before this one, on the same slot: it built a
    // buffer that the next line immediately outgrew and replaced.
    let stage2 = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        total,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "dsv4-moe-stage",
    );
    enc.copy_buffer_to_buffer(
        hc_out.as_ref().unwrap_or(&ob),
        0,
        &stage2,
        0,
        (g.hidden * 4) as u64,
    );
    enc.copy_buffer_to_buffer(&coldb, 0, &stage2, (g.hidden * 4) as u64, cold_bytes);
    enc.copy_buffer_to_buffer(&xb, 0, &stage2, x_off, (g.hidden * 4) as u64);
    submit(c, enc.finish());
    let slice = stage2.slice(..total);
    slice.map_async(wgpu::MapMode::Read, |_| {});
    if c.device.poll(wgpu::PollType::wait_indefinitely()).is_err() {
        return false;
    }
    let mut ok = false;
    if let Ok(data) = slice.get_mapped_range() {
        out[..g.hidden].copy_from_slice(bytemuck::cast_slice(&data[..g.hidden * 4]));
        let tail: &[u32] = bytemuck::cast_slice(&data[g.hidden * 4..total as usize]);
        cold_out.clear();
        for t in 0..g.top_k {
            if tail[2 * t] != u32::MAX {
                cold_out.push((tail[2 * t] as usize, f32::from_bits(tail[2 * t + 1])));
            }
        }
        cold_x_out.clear();
        cold_x_out.extend_from_slice(bytemuck::cast_slice(
            &data[x_off as usize..total as usize],
        ));
        if std::env::var("CMF_DSV4_MOE_CHECK").is_ok() {
            let picks: Vec<(u32, f32)> = (0..g.top_k)
                .map(|t| {
                    (
                        tail[2 * g.top_k + 2 * t],
                        f32::from_bits(tail[2 * g.top_k + 2 * t + 1]),
                    )
                })
                .collect();
            eprintln!("[победители карты] {picks:?}");
        }
        ok = true;
    }
    stage2.unmap();
    // The card's clock, read after the frame's own wait — free, because the
    // fence has already been paid for.
    if let Some((_, _, tstage)) = &c.ts_query {
        let (tx, rx) = std::sync::mpsc::channel();
        tstage.map_async(wgpu::MapMode::Read, ..16, move |r| {
            let _ = tx.send(r);
        });
        let _ = c.device.poll(wgpu::PollType::wait_indefinitely());
        if rx.recv().map(|r| r.is_ok()).unwrap_or(false) {
            if let Ok(raw) = tstage.get_mapped_range(..16) {
                let t: &[u64] = bytemuck::cast_slice(&raw);
                if std::env::var("CMF_TS_DEBUG").is_ok() {
                    eprintln!("[ts] t0={} t1={} period={}", t[0], t[1], c.ts_period);
                }
                let ns = (t[1].saturating_sub(t[0]) as f64 * c.ts_period as f64) as u64;
                drop(raw);
                MOE_GPU_NS[0].fetch_add(ns, std::sync::atomic::Ordering::Relaxed);
                MOE_GPU_N.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            }
        }
        tstage.unmap();
    }
    drop(sc);
    // Encoding and waiting are different problems with different fixes, and
    // the layer total cannot tell them apart. Costs one Instant per layer.
    MOE_ENC_NS.fetch_add(
        t_enc.duration_since(t_all).as_nanos() as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    MOE_WAIT_NS.fetch_add(
        t_enc.elapsed().as_nanos() as u64,
        std::sync::atomic::Ordering::Relaxed,
    );
    ok
}

/// Time inside `dsv4_moe_frame`, split at the submit. Read by the dsv4
/// profile so a slow block can be blamed on the right half.
pub static MOE_ENC_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
pub static MOE_WAIT_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// The encode half again, split three ways — "encoding" turned out to be the
/// whole token's cost and "which part of it" is not guessable: the expert
/// buffer lookup, the per-call uploads, and the passes themselves.
/// GPU time inside the MoE frame, per kernel: routing, gate/up, down.
/// The host counters above measure the CPU's share of a frame; these are the
/// only thing that says what the CARD spends, and dsv4 had no equivalent —
/// `CMF_GPU_TS` instruments the general token graph, which this arch does
/// not use, so the profiler simply printed nothing.
pub static MOE_GPU_NS: [std::sync::atomic::AtomicU64; 3] = [
    std::sync::atomic::AtomicU64::new(0),
    std::sync::atomic::AtomicU64::new(0),
    std::sync::atomic::AtomicU64::new(0),
];
pub static MOE_GPU_N: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// Card time of the attention frame's passes: 0 = the single-kernel sparse
/// attend, 1 = its scores half, 2 = its apply half.
pub static ATT_GPU_NS: [std::sync::atomic::AtomicU64; 3] = [
    std::sync::atomic::AtomicU64::new(0),
    std::sync::atomic::AtomicU64::new(0),
    std::sync::atomic::AtomicU64::new(0),
];
pub static ATT_GPU_N: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// (which counter, slot) for every pass the current encoder stamped.
static TS_PAIRS: Mutex<Vec<(usize, u32)>> = Mutex::new(Vec::new());
/// Rotating timestamp slot, so no two frames in flight share a query.
static TS_SLOT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
/// The pair the last encoded pass took, for the frame that resolves it.
static TS_LAST: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);

/// Per-stage GPU time inside the BT chain frame: `CMF_GPU_TS=1` plus
/// `CMF_BT_TS=li[,li…]` pick the sampled layers; `begin_pass` stamps every
/// pass opened while a stage label is set, and the chain's own fence pays
/// for the readback. Labels index `BT_TS_NAMES`.
static BT_TS_STAGE: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
const BT_TS_NAMES: [&str; 16] = [
    "", "q", "comp", "окно", "ix-mv", "ix-score", "attend", "olora", "glue1", "nextq", "commit",
    "route", "gu", "dn", "glue2", "wo_b",
];

fn bt_ts_lis() -> &'static [usize] {
    static L: std::sync::OnceLock<Vec<usize>> = std::sync::OnceLock::new();
    L.get_or_init(|| {
        std::env::var("CMF_BT_TS")
            .map(|v| v.split(',').filter_map(|s| s.trim().parse().ok()).collect())
            .unwrap_or_default()
    })
}

fn bt_ts(stage: usize) {
    BT_TS_STAGE.store(stage, std::sync::atomic::Ordering::Relaxed);
}
pub static MOE_BUFS_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
pub static MOE_UP_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
pub static MOE_PASS_NS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// Attention over an index list with the learned sink, on the device.
///
/// Step two of the whole-token graph. Verified against `dsv4::sparse_attend`
/// before anything depends on it: a sink that contributes to the numerator,
/// or a denominator missing its share, changes every head's output by a
/// factor that no generated text would reveal.
#[allow(clippy::too_many_arguments)]
pub fn sparse_attend_for_test(
    q: &[f32],
    kv: &[f32],
    idxs: &[u32],
    sink: &[f32],
    scale: f32,
    nh: usize,
    hd: usize,
    out: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if q.len() != nh * hd || out.len() != nh * hd || sink.len() != nh || idxs.len() > 1024 {
        return false;
    }
    let qb = storage_bytes(c, bytemuck::cast_slice(q));
    let kvb = storage_bytes(c, bytemuck::cast_slice(kv));
    let ib = storage_bytes(c, bytemuck::cast_slice(idxs));
    let sb = storage_bytes(c, bytemuck::cast_slice(sink));
    let ob = rw_f32(c, nh * hd, true);
    let p = uniform_u32x4(
        c,
        [nh as u32, hd as u32, idxs.len() as u32, scale.to_bits()],
    );
    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("sa") });
    let _ = &p; // the split path builds its own params
    encode_sparse_attend2(
        c, &mut enc, &qb, &kvb, &ib, &sb, &ob, nh, hd, idxs.len(), scale, None,
    );
    let mut sc = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc.stage,
        (nh * hd * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "sa-stage",
    );
    let ok = readback(c, enc, &ob, &stage, (nh * hd * 4) as u64, out);
    drop(sc);
    ok
}

/// One hyper-connection join on the device: fold the copies (with the
/// Sinkhorn) and expand them back around a block output computed elsewhere.
///
/// Step one of the whole-token graph, and deliberately useless on its own —
/// it costs a submission to save none. It exists so the join can be checked
/// against the CPU before anything is built on top of it, because a
/// transposed mixing matrix or a Sinkhorn off by one iteration produces
/// output that looks entirely reasonable.
#[allow(clippy::too_many_arguments)]
pub fn hc_join_for_test(
    state: &[f32],
    mixes: &[f32],
    scale: &[f32; 3],
    base: &[f32],
    block_out: &[f32],
    hc: usize,
    dim: usize,
    iters: u32,
    eps: f32,
    folded: &mut [f32],
    expanded: &mut [f32],
) -> bool {
    let Some(c) = ctx() else { return false };
    if state.len() != hc * dim || folded.len() != dim || expanded.len() != hc * dim {
        return false;
    }
    let st = storage_bytes(c, bytemuck::cast_slice(state));
    let mx = storage_bytes(c, bytemuck::cast_slice(mixes));
    let sc = storage_bytes(c, bytemuck::cast_slice(&scale[..]));
    let bs = storage_bytes(c, bytemuck::cast_slice(base));
    let fo = rw_f32(c, dim, true);
    let po = rw_f32(c, hc, false);
    let cb = rw_f32(c, hc * hc, false);
    // HcP grew a fifth field, `nrm`, when the fold learned to emit the
    // normed vector — so the uniform is 32 bytes now, and a 16-byte one is
    // rejected outright. Zero: this hook wants the raw fold, not the norm.
    let params = uniform_u32x8(
        c,
        [hc as u32, dim as u32, iters, eps.to_bits(), 0, 0, 0, 0],
    );

    let mut enc = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("hc") });
    {
        let ones = storage_bytes(c, bytemuck::cast_slice(&vec![1.0f32; dim]));
        let normed_out = storage_bytes(c, bytemuck::cast_slice(&vec![0.0f32; dim]));
        let layout = c.hc_pre_fold.get_bind_group_layout(0);
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &layout,
            entries: &[
                bind_buf(0, &st),
                bind_buf(1, &mx),
                bind_buf(2, &sc),
                bind_buf(3, &bs),
                bind_buf(4, &fo),
                bind_buf(5, &po),
                bind_buf(6, &cb),
                bind_buf(7, &params),
                // 8 and 9 arrived when the fold learned to emit the NORMED
                // vector alongside the raw one. The layout takes every
                // binding the entry point touches, so leaving them out is a
                // validation error, not a smaller bind group — this hook had
                // been failing on it. The test reads binding 4, the raw
                // fold; a unit norm and a scratch output satisfy the rest.
                bind_buf(8, &ones),
                bind_buf(9, &normed_out),
            ],
        });
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.hc_pre_fold);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(1, 1, 1);
    }
    let bo = storage_bytes(c, bytemuck::cast_slice(block_out));
    let ex = rw_f32(c, hc * dim, true);
    {
        let layout = c.hc_post_expand.get_bind_group_layout(0);
        let bind = c.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &layout,
            entries: &[
                bind_buf(0, &bo),
                bind_buf(1, &st),
                bind_buf(2, &po),
                bind_buf(3, &cb),
                bind_buf(4, &ex),
                bind_buf(5, &params),
            ],
        });
        let mut pass = begin_pass(&mut enc);
        pass.set_pipeline(&c.hc_post_expand);
        pass.set_bind_group(0, &bind, &[]);
        pass.dispatch_workgroups(((hc * dim) as u32).div_ceil(256), 1, 1);
    }
    // Two readbacks because the two results have different lengths; this is
    // a check, not a hot path.
    let mut sc_lock = c.scratch.lock().unwrap();
    let stage = Scratch::ensure(
        &c.device,
        &mut sc_lock.stage,
        ((hc * dim).max(dim) * 4) as u64,
        wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        "hc-stage",
    );
    if !readback(c, enc, &fo, &stage, (dim * 4) as u64, folded) {
        return false;
    }
    let enc2 = c
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("hc2") });
    if !readback(c, enc2, &ex, &stage, ((hc * dim) * 4) as u64, expanded) {
        return false;
    }
    drop(sc_lock);
    true
}

pub fn adapter_report() -> Vec<String> {
    let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
        backends: wgpu::Backends::all(),
        flags: wgpu::InstanceFlags::default(),
        memory_budget_thresholds: Default::default(),
        backend_options: Default::default(),
        display: None,
    });
    let mut out: Vec<String> =
        pollster::block_on(instance.enumerate_adapters(wgpu::Backends::all()))
            .iter()
            .map(|a| {
                let i = a.get_info();
                let l = a.limits();
                format!(
                    "{:?} | {} | {:?} | буфер до {:.1} ГБ | рабочая группа {}",
                    i.backend,
                    i.name,
                    i.device_type,
                    l.max_buffer_size as f64 / 1e9,
                    l.max_compute_workgroup_size_x
                )
            })
            .collect();
    if out.is_empty() {
        out.push("адаптеров не найдено".into());
    }
    match pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
        power_preference: wgpu::PowerPreference::HighPerformance,
        force_fallback_adapter: false,
        compatible_surface: None,
        apply_limit_buckets: false,
    })) {
        Ok(a) => out.push(format!("выбран: {}", a.get_info().name)),
        Err(e) => out.push(format!("выбрать не удалось: {e}")),
    }
    out
}

pub fn adapter_probe() -> bool {
    let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
        backends: wgpu::Backends::all(),
        flags: wgpu::InstanceFlags::default(),
        memory_budget_thresholds: Default::default(),
        backend_options: Default::default(),
        display: None,
    });
    pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
        power_preference: wgpu::PowerPreference::HighPerformance,
        force_fallback_adapter: false,
        compatible_surface: None,
        apply_limit_buckets: false,
    }))
    .is_ok()
}