inky-engine 0.1.0

A hand-fused Metal inference engine for Thinking Machines' Inkling 2 Mini, for Apple Silicon M5 Macs (Metal 4)
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
//! Inkling 2 Mini decode engine: weight binding, GPU memory layout and the per-token dispatch schedule.

use crate::gguf::*;
use crate::metal::*;
use objc2_metal::{MTLBuffer, MTLCommandBuffer};
use std::cell::{Cell, RefCell};
use std::os::unix::fs::FileExt;

pub const E: usize = 2304;
pub const NH: usize = 24;
pub const NKV: usize = 8;
pub const HD: usize = 128;
pub const NREL: usize = 16;
pub const EXT: usize = 1024;
pub const NEXP: usize = 128;
pub const TOPK: usize = 8;
pub const FF: usize = 1152;
pub const NCH: usize = 2 * NKV * HD + 2 * E; // conv-state channels per slot
pub const N_PHYS: usize = 24;
pub const LOOP_START: usize = 6;
pub const LOOP_END: usize = 18;
pub const N_PASS: usize = 3;
pub const N_SLOT: usize = N_PHYS + (N_PASS - 1) * (LOOP_END - LOOP_START);
pub const BLOCK: usize = 6;
pub const KPS_CHUNK: usize = 32;
/// keys in front of the prefill K/V scratch that the 64-key attention chunks may read (always masked)
const KGUARD: usize = 64;
/// most spans one step can carry (each has a logits row in the batch arena)
pub const MAX_SPANS: usize = 128;
/// process-wide default for `Engine::load`: keep only part of the (exact, fp16) K/V cache in memory while generating and
/// stream the older part from a temporary file on every token (`--kv-disk`)
pub static KV_DISK: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// one position of one slot in the K/V file: K then V, 8 groups of 128 fp16 each
const REC: usize = 2 * NKV * HD * 2;

/// The disk tier of the K/V cache (`--kv-disk`). While generating, the two cache buffers are rings holding only the newest
/// positions ("hot"); every older position a layer still attends to is in `file` and is read into `stg` for each token.
struct Tier {
    file: std::fs::File,
    on: Cell<bool>,
    /// per kind [sliding-window, full]: oldest position the hot ring holds / positions below this are in the file
    lo: [Cell<usize>; 2], cold: [Cell<usize>; 2],
    /// positions below this were written to the file earlier in this session (cached K/V never change, so a later tiering
    /// after a prefill only writes what is new)
    filed: [Cell<usize>; 2],
    /// position at which the sizes are reviewed again (needs an idle GPU)
    next: Cell<usize>,
    /// smallest context that is tiered
    min_n: usize,
    /// staging for the file reads, in two halves: the GPU works on one while the next reads land in the other
    stg: RefCell<Buf>, half: Cell<usize>, pending: RefCell<[Option<CmdBuf>; 2]>,
    /// this step's file reads in execution order, and the round (staging half fill) that is loaded
    plan: RefCell<Vec<Chunk>>, execs: RefCell<Vec<ExecPlan>>, loaded: Cell<usize>,
    pub read_bytes: Cell<u64>, pub read_time: Cell<std::time::Duration>, unaligned: Cell<bool>,
}
#[derive(Clone, Copy)]
struct Chunk { exec: usize, kind: usize, i: usize, c0: usize, c1: usize, round: usize, off: usize, sp0: usize, ns: usize, kps: usize }
/// how one layer execution's keys are divided: file chunks cover the splits before `b`, the hot ring the `ns` splits from `sp0`
#[derive(Clone, Copy)]
struct ExecPlan { b: usize, kps: usize, nsplit: usize, sp0: usize, ns: usize }

/// the encoder a decode step is being written to; with the disk tier a step spans several command buffers
pub struct Cx { e: Enc, cb: Option<CmdBuf> }

/// process-wide default for `Engine::load`: the opt-in 8-bit K/V cache (`--kv8`)
pub static KV8: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// initial capacity (positions) of the K/V buffers; they grow with the context
const KV_CAP0: usize = 64;
/// MoE tile classes of the fp16 prefill path: (kernel, max tokens per tile, threads per threadgroup)
pub const NTC: usize = 2;
const MOE_CLS: [(&str, usize, usize); NTC] = [("k_moe_sg8", 128, 256), ("k_moe_sg4", 64, 128)];
pub const MAX_SPLIT: usize = 256;

/// kernel classes, for skip-mask profiling
pub const CLS_NAMES: &[&str] = &["embed", "mix_attn", "qkv", "attn", "combine", "wo", "mix_ffn", "router", "moe_gu", "moe_down",
                                 "ctrl_down", "ctrl_apply", "out_mix", "lm_head", "argmax",
                                 "kvprep", "wo_post", "gather", "moe_comb", "ffn_post", "ctrl_norm", "topk_route"];
const C_KVPREP: u32 = 15; const C_WOPOST: u32 = 16; const C_GATHER: u32 = 17; const C_COMB2: u32 = 18; const C_FFNPOST: u32 = 19;
const C_CTRLN: u32 = 20; const C_TOPK: u32 = 21;
const C_EMBED: u32 = 0; const C_MIXA: u32 = 1; const C_QKV: u32 = 2; const C_ATTN: u32 = 3; const C_COMB: u32 = 4; const C_WO: u32 = 5;
const C_MIXF: u32 = 6; const C_ROUTER: u32 = 7; const C_GU: u32 = 8; const C_DOWN: u32 = 9; const C_CTRLD: u32 = 10; const C_CTRLA: u32 = 11;
const C_OMIX: u32 = 12; const C_LMHEAD: u32 = 13; const C_ARGMAX: u32 = 14;

pub const KERNELS: &[&str] = &[
    "k_embed", "k_mix", "k_mv", "k_mvb", "k_epi", "k_ctrl_down", "k_ctrl_apply", "k_attn", "k_attn_combine",
    "k_moe_gu", "k_moe_down", "k_argmax1", "k_argmax2",
    "k_topk", "k_norm",
    "k_gemm", "k_moe_gemm", "k_gemm_t", "k_moe_gemm_t", "k_topk_b", "k_moe_route", "k_moe_comb_b", "k_postconv_b", "k_state_upd", "k_state_keep", "k_kvprep_b", "k_attn_b", "k_attn_bt", "k_ctrl_apply_b", "k_tohalf", "k_gemm_sg8", "k_gemm_sg4", "k_moe_sg8", "k_moe_sg4", "k_ctrl_gf", "k_moe_route3", "k_moe_comb_h", "k_attn_h", "k_kvcopy", "k_zero", "k_mix_b_1_1", "k_mix_b_2_1", "k_mix_b_3_1", "k_mix_b_4_1", "k_mix_b_5_1", "k_mix_b_6_1", "k_mix_b_1_3", "k_mix_b_2_3", "k_mix_b_3_3", "k_mix_b_4_3", "k_mix_b_5_3", "k_mix_b_6_3", "k_mix_b_1_4", "k_mix_b_2_4", "k_mix_b_3_4", "k_mix_b_4_4", "k_mix_b_5_4", "k_mix_b_6_4", "k_mix_b_id",
    "k_touch",
];

// ----------------------------------------------------------------------------- kernel arg structs (mirror kernels.metal)
#[repr(C)] #[derive(Clone, Copy, Default)]
struct EmbedArgs { emb_lo: u32, emb_hi: u32, row_bytes: u32, norm_off: u32, out0: u32, out1: u32, tok_index: u32, eps: f32 }

#[repr(C)] #[derive(Clone, Copy, Default)]
struct MixJob { n_s: u32, n_q: u32, s_off: [u32; 6], q_off: i32, out_off: [u32; 4], norm_off: [i32; 4], eps: f32, tstride: u32, ncopy: [[i32; 3]; 4], ntok: u32,
                 hcopy: [[i32; 3]; 4], hout: [i32; 4], hstride: u32, nof32: u32 }

#[repr(C)] #[derive(Clone, Copy, Default)]
struct Seg {
    w_lo: u32, w_hi: u32, row_bytes: u32, wtype: u32,
    nrows: u32, k: u32, tg_start: u32, in_off: u32,
    in2_off: i32, norm_off: i32, ss_off: i32, ss_n: i32,
    out_off: u32, epi: u32, res_off: i32, cs_off: u32,
    convw_off: u32, pad: [u32; 3],
}
#[repr(C)] #[derive(Clone, Copy, Default)]
struct MvArgs { nseg: u32, eps: f32, post: u32, ctr: u32, bias_off: u32, sel_off: u32, wscale: f32, pad: u32, seg: [Seg; 12] }

#[repr(C)] #[derive(Clone, Copy, Default)]
struct CtrlJob { u_off: u32, m_off: u32, d_off: u32, out_off: u32, gain_off: u32, forget_off: u32, res_out: u32, ss_off: u32, norm_w: i32, ctr: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct CtrlArgs { n_c: u32, tstride: u32, pad: [u32; 2], job: [CtrlJob; 4] }

#[repr(C)] #[derive(Clone, Copy, Default)]
struct AttnArgs {
    pos: u32, nkeys: u32, first_pos: u32, ring: u32, cap: u32, k_base: u32, v_base: u32, cs_off: u32,
    q_off: u32, k_off: u32, v_off: u32, r_off: u32, sink_off: u32, kdyn_off: i32, vdyn_off: i32, part_off: u32,
    kconv_w: u32, vconv_w: u32, qclip_w: u32, relclip_w: u32, relbias_w: u32, tau: f32, kps: u32, nsplit: u32,
    ctr: u32, gate_off: u32, out_off: u32, pad: u32,
    sp0: u32, noown: u32, gstr: u32, istr: u32, idx0: u32, lastbias: f32,
}
#[repr(C)] #[derive(Clone, Copy, Default)]
struct DevCopyArgs { src: u32, dst: u32, n: u32, pad: u32 }
/// dev (key eviction experiments): the far keys of the full-attention slots replaced by a selection (`rows` per group,
/// the last one a summary of the evicted keys with score bias `lastbias`); keys from position `r0` on are untouched
pub struct Prune { bag: Buf, rows: usize, r0: usize, lastbias: f32, use_sum: bool }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct CombineArgs { part_off: u32, nsplit: u32, gate_off: u32, out_off: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct EpiArgs { out_off: u32, res_off: i32, cs_off: u32, convw_off: u32, n: u32, pad: [u32; 3] }

#[repr(C)] #[derive(Clone, Copy, Default)]
struct MoeArgs {
    logits_off: u32, bias_off: u32, sel_off: u32, x_off: u32,
    norm_off: i32, ss_off: i32, ss_n: i32, eps: f32,
    gu_lo: u32, gu_hi: u32, gu_expert_bytes: u32, gu_row_bytes: u32,
    act_off: u32, wscale: f32, beta: f32, lbeta: f32,
    dn_lo: u32, dn_hi: u32, dn_expert_bytes: u32, dn_row_bytes: u32,
    dn_type: u32, out_off: u32, res_off: i32, cs_off: u32,
    convw_off: u32, pad: [u32; 3],
}

// ---- prefill arg structs (mirror prefill.metal)
#[repr(C)] #[derive(Clone, Copy, Default)]
struct GSeg { w_lo: u32, w_hi: u32, row_bytes: u32, wtype: u32, nrows: u32, k: u32, tile_start: u32, in_off: u32, out_off: u32, ldo: u32, wsrc: u32, in2: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct ToHalfArgs { src_off: u32, dst_off: u32, k: u32, rows: u32, perm_off: u32, gather: u32, src_stride: u32, dst_stride: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct HSeg { w_lo: u32, w_hi: u32, row_bytes: u32, wtype: u32, nrows: u32, k: u32, tile_start: u32, xh_off: u32, out_off: u32, ldo: u32, wsrc: u32, hout: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct GemmHArgs { nseg: u32, t: u32, pad: [u32; 2], seg: [HSeg; 10] }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct MoeGemmHArgs {
    w_lo: u32, w_hi: u32, expert_bytes: u32, row_bytes: u32, wtype: u32, nrows: u32, k: u32, xh_off: u32,
    tiles_off: u32, ntiles_off: u32, out_off: u32, ldo: u32, situ: u32, aw_off: u32, beta: f32, lbeta: f32,
}
#[repr(C)] #[derive(Clone, Copy, Default)]
struct CtrlBJob { u_off: u32, m_off: u32, g_off: u32, f_off: u32, out_off: u32, norm_w: u32, h_off: i32, nof32: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct CtrlGfJob { gain_w: u32, forget_w: u32, gh_off: u32, u_off: u32, m_off: u32, raw_off: u32, pad: [u32; 2] }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct CtrlGfArgs { t: u32, njobs: u32, pad: [u32; 2], job: [CtrlGfJob; 4] }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct CtrlBArgs { n: u32, pad: [u32; 3], job: [CtrlBJob; 4] }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct GemmArgs { nseg: u32, t: u32, pad: [u32; 2], seg: [GSeg; 10] }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct MoeGemmArgs {
    w_lo: u32, w_hi: u32, expert_bytes: u32, row_bytes: u32, wtype: u32, nrows: u32, k: u32, x_off: u32,
    gather: u32, perm_off: u32, tiles_off: u32, ntiles_off: u32, out_off: u32, ldo: u32, situ: u32, pad: u32,
    beta: f32, lbeta: f32, pad2: [f32; 2],
}
#[repr(C)] #[derive(Clone, Copy, Default)]
struct TopkBArgs { logits_off: u32, bias_off: u32, sel_off: u32, t: u32, wscale: f32, pad: [u32; 3] }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct RouteArgs { sel_off: u32, t: u32, perm_off: u32, inv_off: u32, tiles_off: u32, ntiles_off: u32, tile: u32, pad: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct Route3Args { sel_off: u32, t: u32, perm_off: u32, inv_off: u32, aw_off: u32, ind_off: u32, gu_y: u32, dn_y: u32, tiles_off: [u32; NTC], pad: [u32; 2] }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct MoeCombArgs { yd_off: u32, inv_off: u32, sel_off: u32, out_off: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct ConvBArgs { y_off: u32, ldy: u32, res_off: u32, out_off: u32, cs_off: u32, convw_off: u32, has_res: u32, pad: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct StateArgs { x_off: u32, ldx: u32, cs_off: u32, nch: u32, t: u32, pad: [u32; 3] }
#[repr(C)] #[derive(Clone, Copy)]
struct StateKeepArgs { x_off: u32, ldx: u32, cs_off: u32, nch: u32, t: u32, snap_off: u32, stride: u32, pad: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct KvPrepArgs {
    t: u32, p0: u32, ring: u32, cap: u32, k_base: u32, v_base: u32, cs_off: u32, q_off: u32,
    k_off: u32, v_off: u32, r_off: u32, kdyn_off: i32, vdyn_off: i32, qp_off: u32, rp_off: u32, qhalf: u32,
    kconv_w: u32, vconv_w: u32, qclip_w: u32, relclip_w: u32, log_alpha: f32, log_floor: f32, is_full: u32, kpos0: i32,
}
#[repr(C)] #[derive(Clone, Copy, Default)]
struct AttnBArgs { t: u32, p0: u32, ring: u32, cap: u32, k_base: u32, v_base: u32, window: u32, qp_off: u32,
                   rp_off: u32, sink_off: u32, gate_off: u32, out_off: u32, relbias_w: u32, ghalf: u32, kpos0: i32, pad: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
struct KvCopyArgs { n: u32, pos0: u32, ring_k: u32, ring_v: u32, cap: u32, sk_off: u32, sv_off: u32, scap: u32, kpos0: i32, to_ring: u32, pad: [u32; 2] }

/// batch (prefill) arena: per-token activation arrays [T][dim], f32 offsets into the same arena buffer
#[derive(Clone, Copy, Default, Debug)]
pub struct BArena {
    /// logits in `b.lg`: one vocabulary row per span for a prefill (`lg_rows` of them, at most `MAX_SPANS`).
    /// A verify span uses `1 + k` of those rows, one per position. This is not one row per token.
    lg: u32,
    lg_rows: u32,
    vec0: u32, q: u32, gate: u32, k: u32, v: u32, rel: u32, sink: u32, kdyn: u32, vdyn: u32, qp: u32, rp: u32, attn_g: u32,
    inp: [u32; 3], msg: [u32; 4], cin: [u32; 3], fin: u32, finc: u32, ctrlg: [u32; 4],
    cat_u: [u32; 4], cat_m: [u32; 4], gain_o: [u32; 4], forget_o: [u32; 4],
    xh: [u32; 3], agh: u32, xgh: u32, acth: u32, cath: [u32; 4], gh: [u32; 4],
    tl: [u32; NTC], ind: u32, aw: u32, fh: u32, sk: u32, sv: u32,
    wo_y: u32, ffn_y: u32, rlog: u32, sel: u32, perm: u32, inv: u32, tiles: u32, ntiles: u32, act: u32, yd: u32, end: u32,
    /// tokens this layout was sized for (0: no batch arena allocated)
    cap: usize,
}
impl BArena {
    /// offsets for a batch of up to `t` tokens. `lean`: the fp16 path, which never touches the f32 intermediates of the
    /// fallback kernels and keeps q / gate / the per-assignment outputs as half.
    fn new(start: u32, t: usize, lean: bool, kvb: usize, vocab_pad: usize) -> BArena {
        let mut o = start;
        let mut take = |n: usize| { let r = o; o += ((n + 63) / 64 * 64) as u32; r };
        let fb = |n: usize| if lean { 0 } else { n };      // only the fallback paths use it
        let hf = |n: usize| if lean { n / 2 } else { n };  // half in the fp16 path
        let mut b = BArena { cap: t, ..Default::default() };
        b.vec0 = take(B_NVEC * t * E);
        b.q = take(hf(t * NH * HD)); b.gate = take(hf(t * NH * HD)); b.k = take(t * NKV * HD); b.v = take(t * NKV * HD);
        b.rel = take(t * NH * NREL); b.sink = take(t * NH); b.kdyn = take(t * 32); b.vdyn = take(t * 32);
        b.qp = take(hf(t * NH * HD)); b.rp = take(t * NH * NREL); b.attn_g = take(fb(t * NH * HD));
        for i in 0..3 { b.inp[i] = take(t * E); }
        for i in 0..4 { b.msg[i] = take(t * E); }
        for i in 0..3 { b.cin[i] = take(fb(t * E)); }
        b.fin = take(t * E); b.finc = take(fb(t * E));
        for i in 0..4 { b.cat_u[i] = take(fb(t * E)); b.cat_m[i] = take(fb(t * E)); b.gain_o[i] = take(t * E); b.forget_o[i] = take(fb(t * E)); }
        for i in 0..3 { b.xh[i] = take(t * E / 2); }
        b.agh = take(t * NH * HD / 2);
        b.xgh = take(t * TOPK * E / 2);
        b.acth = take(t * TOPK * FF / 2);
        for i in 0..4 { b.cath[i] = take(t * 2 * E / 2); b.gh[i] = take(t * 64 / 2); }
        for i in 0..4 { b.ctrlg[i] = take(t * 64); }
        b.wo_y = take(t * E); b.ffn_y = take(t * E);
        b.rlog = take(t * NEXP); b.sel = take(t * 16);
        b.tl = [take((t * TOPK / 128 + NEXP + 1) * 4), take((NEXP + 1) * 4)];
        b.ind = take(64); b.aw = take(t * TOPK); b.fh = take(t * E / 2);
        // sliding-window K/V scratch of the batch: [guard | window history | batch keys | guard] per group, half
        b.sk = take(NKV * (EXT + t + 2 * KGUARD) * HD * kvb / 4); b.sv = take(NKV * (EXT + t + 2 * KGUARD) * HD * kvb / 4);
        // Not `t` rows. At chunk 4096 that would be 32× this buffer (128 rows today). A verify span reuses them.
        b.lg_rows = t.min(MAX_SPANS) as u32;
        b.lg = take(b.lg_rows as usize * vocab_pad);
        b.perm = take(t * TOPK); b.inv = take(t * TOPK); b.tiles = take(fb((t * TOPK / 32 + NEXP + 1) * 4)); b.ntiles = take(64);
        b.act = take(fb(t * TOPK * FF)); b.yd = take(hf(t * TOPK * E));
        b.end = o;
        b
    }
    fn vec(&self, i: usize) -> u32 { self.vec0 + (i * self.cap * E) as u32 }
}
#[repr(C)] #[derive(Clone, Copy, Default)]
struct ArgmaxArgs { logits_off: u32, n: u32, part_off: u32, nparts: u32, out_index: u32, pad: [u32; 3] }

// ----------------------------------------------------------------------------- weights
#[derive(Clone, Copy, Default, Debug)]
pub struct QW { pub off: u64, pub row_bytes: u32, pub ty: u32, pub rows: u32, pub k: u32, pub expert_bytes: u64 }

fn qw(g: &Gguf, name: &str) -> QW {
    let t = g.tensor(name);
    let rb = t.row_bytes();
    QW { off: t.file_off, row_bytes: rb as u32, ty: t.ty, rows: t.ne[1] as u32, k: t.ne[0] as u32, expert_bytes: rb * t.ne[1] }
}

pub struct Layer {
    pub swa: bool,
    pub wq: QW, pub wgate: QW, pub wk: QW, pub wv: QW, pub wo: QW, pub wrel: QW, pub wsink: QW,
    pub kconvp: Option<QW>, pub vconvp: Option<QW>,
    pub attn_norm: i32, pub q_inp_norm: i32, pub k_inp_norm: i32, pub v_inp_norm: i32,
    pub attn_res_query: i32, pub n_attn_q: u32,
    pub ffn_res_query: u32, pub ffn_norm: u32,
    pub q_clip: u32, pub rel_clip: u32, pub rel_bias: u32, pub k_conv: u32, pub v_conv: u32,
    pub attn_post_conv: u32, pub ffn_post_conv: u32,
    pub router: u32, pub exp_bias: u32,
    pub gu: QW, pub dn: QW,
}

pub struct LoopW { pub msg_query: u32, pub down: [u32; 4], pub gain: [u32; 4], pub forget: [u32; 4] }

struct Pb { data: Vec<u8> }
impl Pb {
    fn align(&mut self) { while self.data.len() % 256 != 0 { self.data.push(0); } }
    fn f32(&mut self, g: &Gguf, name: &str) -> u32 {
        self.align();
        let off = self.data.len() as u32;
        let t = g.tensor(name);
        assert_eq!(t.ty, T_F32, "{name}");
        self.data.extend_from_slice(g.bytes(t));
        off
    }
    /// F32 -> BF16 (round to nearest even). Lossless for this model's bf16-origin matrices.
    fn bf16(&mut self, g: &Gguf, name: &str) -> u32 {
        self.align();
        let off = self.data.len() as u32;
        let t = g.tensor(name);
        assert_eq!(t.ty, T_F32, "{name}");
        for c in g.bytes(t).chunks_exact(4) {
            let u = u32::from_le_bytes(c.try_into().unwrap());
            let r = u.wrapping_add(0x7FFF + ((u >> 16) & 1)) >> 16;
            self.data.extend_from_slice(&(r as u16).to_le_bytes());
        }
        off
    }
}

// ----------------------------------------------------------------------------- activation arena (f32 offsets)
#[derive(Clone, Copy)]
#[derive(Debug)]
pub struct Arena {
    pub q: u32, pub gate: u32, pub k: u32, pub v: u32, pub rel: u32, pub sink: u32, pub kdyn: u32, pub vdyn: u32,
    pub attn_g: u32, pub inp: [u32; 3], pub msg: [u32; 4], pub cin: [u32; 3], pub ctrlg: [u32; 4], pub ssp: [u32; 4],
    pub cat: [u32; 4], pub fin: u32, pub finc: u32, pub fout: u32, pub rlog: u32, pub sel: u32, pub sel_log: u32, pub act: u32, pub part: u32, pub logits: u32, pub amax: u32, pub total: u32,
}
const N_VEC: usize = 16;
/// hidden-state vectors of the batch arena: at most 4 block states + the current one + 3 pinned + the one being written
const B_NVEC: usize = 10;

impl Arena {
    fn new(max_split: usize, vocab_pad: usize) -> Arena {
        let mut o = (N_VEC * E) as u32;
        let mut take = |n: usize| { let r = o; o += ((n + 63) / 64 * 64) as u32; r };
        let q = take(NH * HD); let gate = take(NH * HD); let k = take(NKV * HD); let v = take(NKV * HD);
        let rel = take(NH * NREL); let sink = take(NH); let kdyn = take(32); let vdyn = take(32);
        let attn_g = take(NH * HD);
        let inp = [take(E), take(E), take(E)];
        let msg = [take(E), take(E), take(E), take(E)];
        let cin = [take(E), take(E), take(E)];
        let ctrlg = [take(64), take(64), take(64), take(64)];
        let ssp = [take(128), take(128), take(128), take(128)];
        let cat = [take(2 * E), take(2 * E), take(2 * E), take(2 * E)];
        let fin = take(E); let finc = take(E); let fout = take(E);
        let rlog = take(NEXP); let sel = take(2 * TOPK); let sel_log = take(64 * 16); let act = take(TOPK * FF);
        let part = take(NH * max_split * (HD + 2));
        let logits = take(vocab_pad);
        let amax = take(2 * 256);
        Arena { q, gate, k, v, rel, sink, kdyn, vdyn, attn_g, inp, msg, cin, ctrlg, ssp, cat, fin, finc, fout, rlog, sel, sel_log, act, part, logits, amax, total: o }
    }
    pub fn vec(i: usize) -> u32 { (i * E) as u32 }
    /// the same layout starting `b` floats further into the activation buffer (a lane's own copy in a decode batch)
    fn shift(&self, b: u32) -> Arena {
        let mut a = *self;
        for f in [&mut a.q, &mut a.gate, &mut a.k, &mut a.v, &mut a.rel, &mut a.sink, &mut a.kdyn, &mut a.vdyn, &mut a.attn_g, &mut a.fin, &mut a.finc,
                  &mut a.fout, &mut a.rlog, &mut a.sel, &mut a.sel_log, &mut a.act, &mut a.part, &mut a.logits, &mut a.amax] { *f += b; }
        for f in a.inp.iter_mut().chain(a.msg.iter_mut()).chain(a.cin.iter_mut()).chain(a.ctrlg.iter_mut()).chain(a.ssp.iter_mut()).chain(a.cat.iter_mut()) { *f += b; }
        a
    }
}

// round-robin vector allocator that never hands out a live vector
#[derive(Clone)]
struct VecAlloc { next: usize, n: usize }
impl VecAlloc {
    fn alloc(&mut self, live: &[usize]) -> usize {
        for _ in 0..self.n {
            let c = self.next;
            self.next = (self.next + 1) % self.n;
            if c != 0 && !live.contains(&c) { return c; }
        }
        panic!("vector pool exhausted");
    }
}

pub struct Engine {
    pub g: Gguf,
    pub gpu: Gpu,
    w: Buf, pub pb: Buf, ctr: Buf,
    /// activations: the small decode arena, followed by the batch (prefill) arena while a prefill needs one
    act: std::cell::RefCell<Buf>,
    /// per slot: (is sliding-window, index among the slots of its kind)
    slot_kind: Vec<(bool, usize)>,
    /// dev: capture of the full-attention layers' prepared queries and sinks during a prefill (per slot: n x 24 x 128 fp16, n x 24 f32)
    qcap: RefCell<Option<(Buf, usize)>>,
    pub layers: Vec<Layer>,
    loops: Vec<LoopW>,
    tok_embd: QW, tok_norm: u32, output: QW, output_norm: u32, output_res_query: u32,
    pub ar: Arena,
    /// where the decode path's arena starts in the activation buffer: 0, or a lane's copy during a decode batch
    abase: Cell<u32>,
    /// first float of the decode batch's lane arenas (see `ensure_dbatch`)
    dstart: Cell<u32>,
    pub kv8: bool, kvb: usize, ba: std::cell::Cell<BArena>, pub chunk: usize, pub tensor: bool, pub hgemm: bool, sep_comb: bool, sep_topk: bool,
    /// last-layer tail mode of the prefill (dev tools switch it off to profile full layers)
    pub tail: std::cell::Cell<bool>,
    pub skip: std::cell::Cell<u32>,
    pub stop: std::cell::Cell<usize>, done: std::cell::Cell<usize>, pub last_out: std::cell::Cell<usize>, pub last_mid: std::cell::Cell<usize>,
    pub max_ctx: usize,
    pub n_vocab: usize,
    eps: f32, log_alpha: f32, log_floor: f32, situ_beta: f32, situ_lbeta: f32, wscale: f32, pub softcap: f32,
    /// Convolution images for the verify span in flight, one per position, plus the K/V bytes that span overwrote.
    /// Not `cs_at_prompt`: a rejected tail must not become the next turn's resume point.
    verify_keep: RefCell<Option<VerifyKeep>>,
    /// Set only while `encode_verify`'s layers run, so a later prefill does not write into the images.
    verify_record: Cell<bool>,
}

/// One stream's state: everything a token's computation reads or writes besides the weights and the scratch arenas.
/// Two lanes on one engine are independent streams; the engine works on one lane at a time (`Engine::on`).
pub struct Lane {
    /// K/V cache (fp16). Sliding-window layers keep exactly their window (a ring of EXT keys per slot), full-attention
    /// layers every key. Both buffers are sized to the context and grow with it.
    kv_swa: RefCell<Buf>, kv_full: RefCell<Buf>, swa_cap: Cell<usize>, full_cap: Cell<usize>,
    /// causal-convolution rows of every slot (the recurrent state)
    cs: Buf,
    /// the stream's tokens by position (greedy decoding writes each step's argmax here, GPU to GPU)
    pub toks: Buf,
    tier: Option<Tier>,
    pub prune: RefCell<Option<Prune>>,
}

/// The engine working on one lane. Methods that touch a stream's state are on `Run`; the rest are the engine's, which
/// `Run` reaches through Deref.
#[derive(Clone, Copy)]
pub struct Run<'a> { pub eng: &'a Engine, pub lane: &'a Lane }
impl std::ops::Deref for Run<'_> { type Target = Engine; fn deref(&self) -> &Engine { self.eng } }

/// One lane's part of a step: positions [p0, p0 + t) of that lane.
#[derive(Clone, Copy)]
pub struct Span<'a> { pub lane: &'a Lane, pub p0: usize, pub t: usize }

/// K/V rows one verify position overwrote, copied before the span ran.
#[derive(Clone)]
struct KvUndo { pos: usize, rows: Vec<u8> }
struct VerifySpan {
    lane: *const Lane,
    p0: usize,
    kv: Vec<KvUndo>,
    /// `p0 + n_keep` after rollback. Absent until then. Not a seal of the span's end, and not `cs_at_prompt`.
    accepted_end: Option<usize>,
}
struct VerifyKeep {
    /// `spans * (1+k)` convolution images, span-major, each `cs`'s layout. Transient.
    cs: Buf,
    stride: usize,
    t: usize,
    spans: Vec<VerifySpan>,
}

static LANE_SEQ: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);

impl Lane {
    /// A fresh, empty stream for `eng`: a small cache that grows with the context, zeroed convolution rows.
    pub fn new(eng: &Engine) -> Lane {
        let n_swa = eng.slot_kind.iter().filter(|k| k.0).count();
        let n_full = N_SLOT - n_swa;
        let cap0 = KV_CAP0.min(eng.max_ctx);
        let gpu = &eng.gpu;
        let tier = if KV_DISK.load(std::sync::atomic::Ordering::Relaxed) {
            assert!(!eng.kv8, "--kv-disk keeps the exact fp16 cache; it cannot be combined with --kv8");
            let path = std::env::temp_dir().join(format!("inky-kv-{}-{}.bin", std::process::id(), LANE_SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed)));
            let file = std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(true).open(&path).expect("create the K/V file");
            // uncached I/O: the point of the file is that its contents are NOT in memory
            unsafe { libc::fcntl(std::os::fd::AsRawFd::as_raw_fd(&file), libc::F_NOCACHE, 1); }
            let _ = std::fs::remove_file(&path);       // it lives as long as the lane
            Some(Tier { file, on: Cell::new(false), lo: Default::default(), cold: Default::default(), filed: Default::default(), next: Cell::new(0),
                        min_n: std::env::var("INK_TIER_MIN").ok().and_then(|v| v.parse().ok()).unwrap_or(512),
                        stg: RefCell::new(gpu.buffer(16384)), half: Cell::new(0), pending: RefCell::new([None, None]),
                        plan: RefCell::new(Vec::new()), execs: RefCell::new(Vec::new()), loaded: Cell::new(usize::MAX),
                        read_bytes: Cell::new(0), read_time: Cell::new(Default::default()), unaligned: Cell::new(false) })
        } else { None };
        Lane {
            kv_swa: RefCell::new(gpu.buffer(n_swa * 2 * NKV * cap0 * HD * eng.kvb)),
            kv_full: RefCell::new(gpu.buffer(n_full * 2 * NKV * cap0 * HD * eng.kvb)),
            swa_cap: Cell::new(cap0), full_cap: Cell::new(cap0),
            cs: gpu.buffer(N_SLOT * NCH * 3 * 4),
            toks: gpu.buffer((eng.max_ctx + 8) * 4),
            tier, prune: RefCell::new(None),
        }
    }
}

fn slot_of(pass: usize, ip: usize) -> usize {
    if pass == 0 || !(LOOP_START..LOOP_END).contains(&ip) { ip } else { N_PHYS + (pass - 1) * (LOOP_END - LOOP_START) + (ip - LOOP_START) }
}

impl Engine {
    /// the vocabulary as the arena stores it (rounded up to 64)
    fn vocab_pad(&self) -> usize { (self.ar.amax - self.ar.logits) as usize }
    /// One convolution image: `N_SLOT × NCH × 3` floats. The page's 3.66 MiB.
    pub fn cs_image_bytes() -> usize { N_SLOT * NCH * 3 * 4 }
    /// dev: a hash of a lane's cache (both buffers) and convolution rows, to compare two runs bit for bit
    pub fn lane_hash(&self, lane: &Lane) -> u64 {
        let mut h = 0xcbf29ce484222325u64;
        let mut eat = |b: &Buf| { let p = b.contents().as_ptr() as *const u64; for i in 0..b.length() / 8 { h = (h ^ unsafe { *p.add(i) }).wrapping_mul(0x100000001b3); } };
        eat(&lane.kv_swa.borrow()); eat(&lane.kv_full.borrow()); eat(&lane.cs);
        h
    }
    /// dev: make `dst` a copy of `src` (cache and convolution rows); `dst` must have been reserved to the same capacity
    pub fn copy_lane(&self, src: &Lane, dst: &Lane) {
        let cp = |a: &Buf, b: &Buf| { assert_eq!(a.length(), b.length()); unsafe { std::ptr::copy_nonoverlapping(a.contents().as_ptr() as *const u8, b.contents().as_ptr() as *mut u8, a.length()); } };
        cp(&src.kv_swa.borrow(), &dst.kv_swa.borrow()); cp(&src.kv_full.borrow(), &dst.kv_full.borrow()); cp(&src.cs, &dst.cs);
    }
    /// dev: make `dst` hold `src`'s first `n` positions (cache rows of every slot, convolution rows); the two may have
    /// different capacities
    pub fn copy_lane_prefix(&self, src: &Lane, dst: &Lane, n: usize) {
        for (sb, db, sc, dc, nk) in [(&src.kv_swa, &dst.kv_swa, src.swa_cap.get(), dst.swa_cap.get(), n.min(EXT)), (&src.kv_full, &dst.kv_full, src.full_cap.get(), dst.full_cap.get(), n)] {
            assert!(nk <= sc && nk <= dc);
            let (sp, dp) = (sb.borrow().contents().as_ptr() as *const u8, db.borrow().contents().as_ptr() as *mut u8);
            let rows = sb.borrow().length() / (sc * HD * self.kvb);
            assert_eq!(rows, db.borrow().length() / (dc * HD * self.kvb));
            for r in 0..rows { unsafe { std::ptr::copy_nonoverlapping(sp.add(r * sc * HD * self.kvb), dp.add(r * dc * HD * self.kvb), nk * HD * self.kvb); } }
        }
        unsafe { std::ptr::copy_nonoverlapping(src.cs.contents().as_ptr() as *const u8, dst.cs.contents().as_ptr() as *mut u8, src.cs.length()); }
    }
    /// GPU memory the device has allocated (bytes) and the working set it recommends
    pub fn gpu_mem(&self) -> (usize, usize) {
        use objc2_metal::MTLDevice;
        (self.gpu.dev.currentAllocatedSize(), self.gpu.dev.recommendedMaxWorkingSetSize() as usize)
    }
    /// Vocabulary row `i` in `b.lg` from the last step that wrote it. A prefill with several spans stores one
    /// row per span, so `i` is the span. A verify span stores `1 + k` rows, so `i` is the position in that span
    /// (span `s`, position `r`, at `s * (1 + k) + r`).
    pub fn span_logits(&self, i: usize) -> &[f32] {
        let b = self.ba.get();
        assert!(i < b.lg_rows as usize, "span_logits: row {i} but b.lg has {} rows", b.lg_rows);
        let off = b.lg as usize + i * self.vocab_pad();
        unsafe { std::slice::from_raw_parts((self.act.borrow().contents().as_ptr() as *const f32).add(off), self.n_vocab) }
    }
    /// the decode path's arena at the current base
    fn ar_at(&self) -> Arena { self.ar.shift(self.abase.get()) }
    /// offset of residual vector `i` of the decode path at the current base
    fn v(&self, i: usize) -> u32 { self.abase.get() + Arena::vec(i) as u32 }
    /// Room for a decode batch of `n` lanes: a copy of the decode arena per lane, after everything else in the activation
    /// buffer. Call after `ensure_batch` (a later batch arena may overlap it). Nothing may be in flight on the GPU.
    pub fn ensure_dbatch(&self, n: usize) {
        let start = (self.ar.total).max(self.ba.get().end);
        let need = (start as usize + n * self.ar.total as usize) * 4;
        self.dstart.set(start);
        if self.act.borrow().length() < need {
            let nb = self.gpu.buffer(need);
            unsafe { std::ptr::copy_nonoverlapping(self.act.borrow().contents().as_ptr() as *const u8, nb.contents().as_ptr() as *mut u8, self.act.borrow().length()); }
            *self.act.borrow_mut() = nb;
        }
    }
    /// the logits lane `j` of the last decode batch produced
    pub fn dbatch_logits(&self, j: usize) -> &[f32] {
        let off = (self.dstart.get() + j as u32 * self.ar.total + self.ar.logits) as usize;
        unsafe { std::slice::from_raw_parts((self.act.borrow().contents().as_ptr() as *const f32).add(off), self.n_vocab) }
    }
    /// the engine working on `lane`
    pub fn on<'a>(&'a self, lane: &'a Lane) -> Run<'a> { Run { eng: self, lane } }
    pub fn load(path: &str, max_ctx: usize) -> Engine { Engine::load_with(path, max_ctx, KV8.load(std::sync::atomic::Ordering::Relaxed)) }
    /// `kv8`: keep the K/V cache as 8-bit integers with a per-vector scale (half the memory, not exact) instead of fp16
    pub fn load_with(path: &str, max_ctx: usize, kv8: bool) -> Engine {
        let t0 = std::time::Instant::now();
        let g = Gguf::open(path).expect("open gguf");
        let arch = match &g.kv["general.architecture"] { Value::S(s) => s.clone(), _ => String::new() };
        assert_eq!(arch, "inkling2_loop", "unsupported architecture {arch}");
        let a = |k: &str| format!("inkling2_loop.{k}");
        assert_eq!(g.u(&a("embedding_length")) as usize, E);
        assert_eq!(g.u(&a("block_count")) as usize, N_PHYS);
        assert_eq!(g.u(&a("expert_count")) as usize, NEXP);
        assert_eq!(g.u(&a("expert_used_count")) as usize, TOPK);
        assert_eq!(g.u(&a("loop.start")) as usize, LOOP_START);
        assert_eq!(g.u(&a("loop.end")) as usize, LOOP_END);
        assert_eq!(g.u(&a("loop.count")) as usize, N_PASS);
        assert_eq!(g.u(&a("attn_res.block_size")) as usize, BLOCK);
        assert_eq!(g.u(&a("attention.sliding_window")) as usize, EXT);
        assert_eq!(g.u(&a("attention.relative_extent")) as usize, EXT);
        let swa_pat = match &g.kv[&a("attention.sliding_window_pattern")] { Value::AB(v) => v.clone(), _ => panic!("swa pattern") };

        let mut pb = Pb { data: Vec::new() };
        let mut layers = Vec::new();
        for i in 0..N_PHYS {
            let n = |s: &str| format!("blk.{i}.{s}");
            let swa = swa_pat[i];
            let opt_f32 = |pb: &mut Pb, s: &str| -> i32 { if g.opt(&n(s)).is_some() { pb.f32(&g, &n(s)) as i32 } else { -1 } };
            let attn_norm = opt_f32(&mut pb, "attn_norm.weight");
            let q_inp_norm = opt_f32(&mut pb, "attn_q_inp_norm.weight");
            let k_inp_norm = opt_f32(&mut pb, "attn_k_inp_norm.weight");
            let v_inp_norm = opt_f32(&mut pb, "attn_v_inp_norm.weight");
            let attn_res_query = opt_f32(&mut pb, "attn_res_query.weight");
            let n_attn_q = g.opt(&n("attn_res_query.weight")).map(|t| if t.ne.len() > 1 { t.ne[1] as u32 } else { 1 }).unwrap_or(0);
            let layer = Layer {
                swa,
                wq: qw(&g, &n("attn_q.weight")), wgate: qw(&g, &n("attn_gate.weight")), wk: qw(&g, &n("attn_k.weight")),
                wv: qw(&g, &n("attn_v.weight")), wo: qw(&g, &n("attn_output.weight")), wrel: qw(&g, &n("attn_rel.weight")),
                wsink: qw(&g, &n("attn_sink_proj.weight")),
                kconvp: g.opt(&n("attn_k_conv_proj.weight")).map(|_| qw(&g, &n("attn_k_conv_proj.weight"))),
                vconvp: g.opt(&n("attn_v_conv_proj.weight")).map(|_| qw(&g, &n("attn_v_conv_proj.weight"))),
                attn_norm, q_inp_norm, k_inp_norm, v_inp_norm, attn_res_query, n_attn_q,
                ffn_res_query: pb.f32(&g, &n("ffn_res_query.weight")),
                ffn_norm: pb.f32(&g, &n("ffn_norm.weight")),
                q_clip: pb.f32(&g, &n("attn_q_clip.weight")),
                rel_clip: pb.f32(&g, &n("attn_rel_clip.weight")),
                rel_bias: pb.f32(&g, &n("attn_rel_bias.weight")),
                k_conv: pb.f32(&g, &n("attn_k_conv.weight")),
                v_conv: pb.f32(&g, &n("attn_v_conv.weight")),
                attn_post_conv: pb.f32(&g, &n("attn_post_conv.weight")),
                ffn_post_conv: pb.f32(&g, &n("ffn_post_conv.weight")),
                router: pb.bf16(&g, &n("ffn_gate_inp.weight")),
                exp_bias: pb.f32(&g, &n("exp_probs_b.bias")),
                gu: qw(&g, &n("ffn_gate_up_exps.weight")),
                dn: qw(&g, &n("ffn_down_exps.weight")),
            };
            assert!(layer.swa == (layer.attn_norm >= 0));
            assert_eq!(layer.gu.ty, T_Q4_K);
            assert!(layer.dn.ty == T_Q8_0 || layer.dn.ty == T_Q5_0, "down type {}", type_name(layer.dn.ty));
            for w in [&layer.wq, &layer.wgate, &layer.wk, &layer.wo, &layer.wrel, &layer.wsink] { assert_eq!(w.ty, T_Q4_K); }
            assert!(layer.wv.ty == T_Q4_K || layer.wv.ty == T_Q6_K);
            layers.push(layer);
        }
        let mut loops = Vec::new();
        for i in LOOP_START..LOOP_END {
            let n = |s: &str| format!("blk.{i}.{s}");
            let names = ["q", "k", "v", "ffn"];
            let mut lw = LoopW { msg_query: pb.f32(&g, &n("loop_msg_query.weight")), down: [0; 4], gain: [0; 4], forget: [0; 4] };
            for c in 0..4 {
                lw.down[c] = pb.bf16(&g, &n(&format!("loop_{}_ctrl_down.weight", names[c])));
                lw.gain[c] = pb.bf16(&g, &n(&format!("loop_{}_ctrl_gain.weight", names[c])));
                lw.forget[c] = pb.bf16(&g, &n(&format!("loop_{}_ctrl_forget.weight", names[c])));
            }
            loops.push(lw);
        }
        let tok_norm = pb.f32(&g, "token_embd_norm.weight");
        let output_norm = pb.f32(&g, "output_norm.weight");
        let output_res_query = pb.f32(&g, "output_res_query.weight");
        let tok_embd = qw(&g, "token_embd.weight");
        let output = qw(&g, "output.weight");
        assert_eq!(tok_embd.ty, T_Q4_K);
        assert_eq!(output.ty, T_Q6_K);
        let n_vocab = g.u(&a("vocab_size_unpadded")) as usize;
        let n_vocab_pad = output.rows as usize;

        let src = format!("{}{}\n{}\n{}", if kv8 { "#define KV8 1\n" } else { "" }, include_str!("kernels.metal"), include_str!("prefill.metal"), include_str!("bench_kernels.metal"));
        let kvb = if kv8 { 1 } else { 2 };
        let gpu = Gpu::new(&src, KERNELS);
        let w = gpu.buffer_nocopy(g.map, g.map_len);
        pb.data.extend(std::iter::repeat(0u8).take(128 * NREL * 4));   // k_attn_h reads up to 95 rows past a relative-bias table
        let pbuf = gpu.buffer_with(&pb.data);

        let max_split = MAX_SPLIT;
        let ar = Arena::new(max_split, n_vocab_pad);
        let chunk: usize = std::env::var("INK_CHUNK").ok().and_then(|v| v.parse().ok()).unwrap_or(4096).min(max_ctx);
        assert!(chunk % 32 == 0 && chunk <= 4096);
        assert!(max_ctx % 64 == 0, "context size must be a multiple of 64");
        // the batch arena is allocated when a prefill needs it and released when generation starts
        let act = gpu.buffer(ar.total as usize * 4);
        // K/V cache layout: per slot [K: NKV x cap x HD][V: NKV x cap x HD] halfs, sliding-window and full slots in separate
        // buffers that both grow with the context (Metal commits a buffer when it is created, so capacity is real memory)
        let mut slot_kind = Vec::new();
        let (mut n_swa, mut n_full) = (0usize, 0usize);
        for s in 0..N_SLOT {
            let phys = if s < N_PHYS { s } else { LOOP_START + (s - N_PHYS) % (LOOP_END - LOOP_START) };
            if layers[phys].swa { slot_kind.push((true, n_swa)); n_swa += 1; } else { slot_kind.push((false, n_full)); n_full += 1; }
        }
        assert!(n_full * 2 * NKV * max_ctx * HD < u32::MAX as usize, "KV cache too large for 32-bit offsets");
        let ctr = gpu.buffer(4096 * 4);
        eprintln!("[inky] loaded {} ({:.1} GiB mapped, {:.1} MiB params) in {:.2?}",
            path.rsplit('/').next().unwrap_or(path), g.file_len as f64 / (1u64 << 30) as f64, pb.data.len() as f64 / (1 << 20) as f64, t0.elapsed());
        let eng = Engine {
            eps: g.f(&a("attention.layer_norm_rms_epsilon")) as f32,
            log_alpha: g.f(&a("attention.log_scale_alpha")) as f32,
            log_floor: g.f(&a("attention.log_scale_floor")) as f32,
            situ_beta: g.f(&a("activation.situ_beta")) as f32,
            situ_lbeta: g.f(&a("activation.situ_linear_beta")) as f32,
            wscale: g.f(&a("expert_weights_scale")) as f32,
            softcap: g.f(&a("final_logit_softcapping")) as f32,
            g, gpu, w, pb: pbuf, act: std::cell::RefCell::new(act),
            slot_kind, qcap: RefCell::new(None), ctr, layers, loops, tok_embd, tok_norm, output, output_norm, output_res_query,
            ar, abase: Cell::new(0), dstart: Cell::new(0), kv8, kvb, ba: Default::default(), chunk, tensor: std::env::var("INK_NO_TENSOR").is_err(),
            hgemm: std::env::var("INK_NO_HGEMM").is_err() && std::env::var("INK_NO_TENSOR").is_err(),
            sep_comb: std::env::var("INK_FUSE_COMB").is_err(), sep_topk: std::env::var("INK_SEP_TOPK").is_ok(),
            tail: std::cell::Cell::new(std::env::var("INK_NO_TAIL").is_err()),
            skip: Default::default(), stop: std::cell::Cell::new(usize::MAX), done: std::cell::Cell::new(0),
            last_out: Default::default(), last_mid: Default::default(),
            max_ctx, n_vocab,
            verify_keep: RefCell::new(None), verify_record: Cell::new(false),
        };
        let mut eng = eng;
        eng.ensure_batch(64);
        let ok = !eng.hgemm || eng.layout_ok();
        eng.release_batch();
        if !ok {
            eprintln!("[inky] unexpected tensor accumulator layout on this GPU/OS: using the slower prefill path");
            eng.hgemm = false;
        }
        if std::env::var("INK_NO_WARM").is_err() { eng.warm(); }
        eng
    }

    /// Allocate (or enlarge) the batch arena for a prefill of `t` tokens. Nothing may be in flight on the GPU.
    pub fn ensure_batch(&self, t: usize) {
        let t = t.div_ceil(64) * 64;
        if t <= self.ba.get().cap { return; }
        let nb = BArena::new(self.ar.total, t, self.hgemm, self.kvb, self.vocab_pad());
        self.realloc_act(nb.end as usize * 4);
        self.ba.set(nb);
    }
    /// Give the batch arena back (the decode arena and the last logits stay). Nothing may be in flight on the GPU.
    pub fn release_batch(&self) {
        if self.ba.get().cap == 0 { return; }
        self.realloc_act(self.ar.total as usize * 4);
        self.ba.set(BArena::default());
    }
    fn realloc_act(&self, bytes: usize) {
        let nb = self.gpu.buffer(bytes);
        unsafe { std::ptr::copy_nonoverlapping(self.act.borrow().contents().as_ptr() as *const u8, nb.contents().as_ptr() as *mut u8, self.ar.total as usize * 4); }
        *self.act.borrow_mut() = nb;
    }
    // ======================================================================== dev: key eviction experiments
    pub fn qcap_alloc(&self, n: usize) {
        let nf = self.slot_kind.iter().filter(|k| !k.0).count();
        *self.qcap.borrow_mut() = Some((self.gpu.buffer(nf * n * (NH * HD / 2 + NH) * 4), n));
    }
    pub fn arena(&self, off: u32, n: usize) -> Vec<f32> {
        unsafe { std::slice::from_raw_parts((self.act.borrow().contents().as_ptr() as *const f32).add(off as usize), n).to_vec() }
    }
    /// expert ids chosen by each of the 48 layer executions of the last decode step
    pub fn routes(&self) -> Vec<[u8; TOPK]> {
        let v = self.arena(self.ar.sel_log, 48 * 16);
        (0..48).map(|l| { let mut r = [0u8; TOPK]; for k in 0..TOPK { r[k] = v[l * 16 + k] as u8; } r }).collect()
    }
    /// dev: MoE tiles of the last prefilled layer: per width class (128, 64, 32) the per-tile token counts
    pub fn moe_tiles(&self) -> [Vec<u32>; NTC] {
        let a = self.act.borrow().contents().as_ptr() as *const u32;
        let cls = |c: usize| unsafe {
            let n = *a.add(self.ba.get().ind as usize + c * 4) as usize;
            (0..n).map(|i| *a.add(self.ba.get().tl[c] as usize + i * 4 + 2)).collect::<Vec<u32>>()
        };
        [cls(0), cls(1)]
    }
    /// The register epilogues of the fp16 kernels assume how the tensor ops spread their accumulators over threads
    /// (see the comment above k_gemm_sg in prefill.metal).
    fn layout_ok(&self) -> bool {
        {
            let out_off = self.ba.get().yd;
            let cb = self.gpu.cmd();
            let e = Enc::new(&cb);
            self.bind_eng(&e);
            e.pso(&self.gpu.pso("k_layout_sg")); e.bytes(&out_off, 5);
            e.dispatch((1, 1, 1), 32);
            e.end(); cb.commit(); cb.waitUntilCompleted();
            let o = unsafe { std::slice::from_raw_parts((self.act.borrow().contents().as_ptr() as *const i32).add(out_off as usize), 32 * 64) };
            for t in 0..32 {
                if o[t * 64 + 63] != 32 { return false; }
                let rt = |i: usize| (o[t * 64 + i] >> 16, o[t * 64 + i] & 0xffff);
                for q in 0..8 {
                    let (r0, t0) = rt(4 * q);
                    if o[t * 64 + 4 * q] < 0 || r0 % 4 != 0 || t0 >= 16 { return false; }
                    for e in 1..4 { if rt(4 * q + e) != (r0 + e as i32, t0) { return false; } }
                    if q < 4 && (r0 >= 32 || rt(4 * q + 16) != (r0 + 32, t0)) { return false; }
                }
            }
        }
        // attention P.V accumulators: 16 per thread, quads of 4 consecutive dims of one query row
        {
            let out_off = self.ba.get().yd;
            let cb = self.gpu.cmd();
            let e = Enc::new(&cb);
            self.bind_eng(&e);
            e.pso(&self.gpu.pso("k_layout_att")); e.bytes(&out_off, 5);
            e.dispatch((1, 1, 1), 256);
            e.end(); cb.commit(); cb.waitUntilCompleted();
            let o = unsafe { std::slice::from_raw_parts((self.act.borrow().contents().as_ptr() as *const i32).add(out_off as usize), 256 * 64) };
            for t in 0..256 {
                if o[t * 64 + 63] != 16 { return false; }
                for q in 0..4 {
                    let (d0, r0) = (o[t * 64 + 4 * q] >> 16, o[t * 64 + 4 * q] & 0xffff);
                    if o[t * 64 + 4 * q] < 0 || d0 % 4 != 0 { return false; }
                    for e in 1..4 { if o[t * 64 + 4 * q + e] != ((d0 + e as i32) << 16 | r0) { return false; } }
                }
            }
        }
        true
    }
    pub fn logits(&self) -> &[f32] {
        unsafe { std::slice::from_raw_parts((self.act.borrow().contents().as_ptr() as *const f32).add(self.ar.logits as usize), self.n_vocab) }
    }

    /// micro-benchmark a Q4_K matvec variant over the gate_up expert weights of every layer (no cache reuse)
    pub fn kbench(&self, name: &str, nr: usize, nsg: usize) -> f64 {
        #[repr(C)] #[derive(Clone, Copy)]
        struct BenchArgs { w_lo: u32, w_hi: u32, row_bytes: u32, nrows: u32, k: u32, x_off: u32, out_off: u32, pad: u32 }
        let rows = 8 * E; // 8 experts' worth of gate_up rows
        let mut best = f64::MAX;
        for rep in 0..4 {
            let cb = self.gpu.cmd();
            let e = Enc::new(&cb);
            self.bind_eng(&e);
            e.pso(&self.gpu.pso(name));
            let mut bytes = 0usize;
            for l in 0..N_PHYS {
                for chunk in 0..4 {
                    let gu = &self.layers[l].gu;
                    let off = gu.off + ((rep * 4 + chunk) % 16) as u64 * 8 * gu.expert_bytes;
                    let a = BenchArgs { w_lo: off as u32, w_hi: (off >> 32) as u32, row_bytes: gu.row_bytes, nrows: rows as u32, k: E as u32,
                                        x_off: Arena::vec(1), out_off: self.ar.logits, pad: 0 };
                    e.bytes(&a, 5);
                    e.dispatch((rows / (nr * nsg), 1, 1), 32 * nsg);
                    e.barrier();
                    bytes += rows * gu.row_bytes as usize;
                }
            }
            e.end();
            cb.commit();
            cb.waitUntilCompleted();
            let t = cb.GPUEndTime() - cb.GPUStartTime();
            if rep > 0 { best = best.min(bytes as f64 / t / 1e9); }
        }
        best
    }

    /// micro-benchmark a MoE-down variant over all layers' down experts (random-ish expert picks)
    pub fn kbench_down(&self, name: &str, rows_per_tg: usize, tg_threads: usize, only_type: u32) -> f64 {
        let ar = self.ar;
        // synthetic act + selection
        let a = self.act.borrow().contents().as_ptr() as *mut f32;
        unsafe {
            for i in 0..TOPK * FF { *a.add(ar.act as usize + i) = ((i * 7919) % 1000) as f32 / 1000.0 - 0.5; }
            for k in 0..TOPK { *a.add(ar.sel as usize + TOPK + k) = 0.125; }
        }
        let mut best = f64::MAX;
        for rep in 0..4 {
            let cb = self.gpu.cmd();
            let e = Enc::new(&cb);
            self.bind_eng(&e);
            let mut bytes = 0usize;
            let mut sels = Vec::new();
            for l in 0..N_PHYS {
                if only_type != 0 && self.layers[l].dn.ty != only_type { continue; }
                for it in 0..4 {
                    let dn = &self.layers[l].dn;
                    let ma = MoeArgs { sel_off: ar.logits + 100000 + ((l * 4 + it) as u32 % 32) * 16, act_off: ar.act, out_off: Arena::vec(2), res_off: Arena::vec(3) as i32,
                                       dn_lo: dn.off as u32, dn_hi: (dn.off >> 32) as u32, dn_expert_bytes: dn.expert_bytes as u32,
                                       dn_row_bytes: dn.row_bytes, dn_type: dn.ty, ..Default::default() };
                    sels.push(ma.sel_off);
                    e.pso(&self.gpu.pso(name));
                    e.bytes(&ma, 5);
                    e.dispatch((E / rows_per_tg, 1, 1), tg_threads);
                    e.barrier();
                    bytes += TOPK * E * dn.row_bytes as usize;
                }
            }
            unsafe {
                for (j, &so) in sels.iter().enumerate() {
                    for k in 0..TOPK { *a.add(so as usize + k) = (((j * 37 + k * 13 + rep * 5) % 128) as f32).max(0.0); *a.add(so as usize + TOPK + k) = 0.125; }
                }
            }
            e.end();
            cb.commit();
            cb.waitUntilCompleted();
            let t = cb.GPUEndTime() - cb.GPUStartTime();
            if rep > 0 { best = best.min(bytes as f64 / t / 1e9); }
        }
        best
    }

    /// touch every page of the mapped model from the GPU once (wires the GPU page mappings: avoids first-use
    /// page-fault stalls when a rarely used expert is hit for the first time mid-generation)
    pub fn warm(&self) {
        #[repr(C)] #[derive(Clone, Copy)]
        struct TouchArgs { lo: u32, hi: u32, n16: u32, pad: u32 }
        let t0 = std::time::Instant::now();
        let chunk = 1usize << 30;
        let mut off = 0usize;
        let cb = self.gpu.cmd();
        let e = Enc::new(&cb); self.bind_eng(&e);
        e.pso(&self.gpu.pso("k_touch"));
        while off < self.g.file_len {
            let n = chunk.min(self.g.file_len - off) / 16;
            let ta = TouchArgs { lo: off as u32, hi: (off >> 32) as u32, n16: n as u32, pad: 0 };
            e.bytes(&ta, 5); e.dispatch((4096, 1, 1), 256);
            off += chunk;
        }
        e.end(); cb.commit(); cb.waitUntilCompleted();
        eprintln!("[inky] warmed {:.1} GiB of weights in {:.0?}", self.g.file_len as f64 / (1u64 << 30) as f64, t0.elapsed());
    }

    /// raw GPU read bandwidth over the mapped weights
    pub fn bw_weights(&self) {
        #[repr(C)] #[derive(Clone, Copy)]
        struct TouchArgs { lo: u32, hi: u32, n16: u32, pad: u32 }
        for (gb, tgs) in [(1usize, 1024usize), (2, 2048), (4, 4096), (8, 8192)] {
            let mut best = 0.0f64;
            for rep in 0..3 {
                let off = (1u64 << 30) + rep as u64 * (1u64 << 28);
                let cb = self.gpu.cmd();
                let e = Enc::new(&cb); self.bind_eng(&e);
                let ta = TouchArgs { lo: off as u32, hi: (off >> 32) as u32, n16: ((gb << 30) / 16) as u32, pad: 0 };
                e.pso(&self.gpu.pso("k_touch")); e.bytes(&ta, 5); e.dispatch((tgs, 1, 1), 256);
                e.end(); cb.commit(); cb.waitUntilCompleted();
                best = best.max((gb << 30) as f64 / (cb.GPUEndTime() - cb.GPUStartTime()) / 1e9);
            }
            println!("weights read {gb} GB: {best:.1} GB/s");
        }
    }

    /// benchmark the production MoE kernels over every layer with random expert picks (GB/s of expert weights)
    pub fn kbench_moe(&self, which: &str, grid: (usize, usize), tgs: usize) -> f64 {
        let ar = self.ar;
        let a = self.act.borrow().contents().as_ptr() as *mut f32;
        let mut best = 0.0f64;
        for rep in 0..5 {
            let cb = self.gpu.cmd();
            let e = Enc::new(&cb);
            self.bind_eng(&e);
            let mut bytes = 0usize;
            for l in 0..N_PHYS {
                for it in 0..2 {
                    let sel = ar.logits + 100000 + ((l * 2 + it) as u32) * 16;
                    unsafe { for k in 0..TOPK {
                        *a.add(sel as usize + k) = (((l * 2 + it) * 53 + k * 17 + rep * 29) % 128) as f32;
                        *a.add(sel as usize + TOPK + k) = 0.125; } }
                    let ld = &self.layers[l];
                    let ma = MoeArgs { sel_off: sel, x_off: Arena::vec(1), act_off: ar.act, out_off: Arena::vec(2), res_off: Arena::vec(3) as i32,
                        gu_lo: ld.gu.off as u32, gu_hi: (ld.gu.off >> 32) as u32, gu_expert_bytes: ld.gu.expert_bytes as u32, gu_row_bytes: ld.gu.row_bytes,
                        dn_lo: ld.dn.off as u32, dn_hi: (ld.dn.off >> 32) as u32, dn_expert_bytes: ld.dn.expert_bytes as u32, dn_row_bytes: ld.dn.row_bytes,
                        dn_type: ld.dn.ty, beta: 4.0, lbeta: 25.0, cs_off: 0, convw_off: ld.ffn_post_conv, ..Default::default() };
                    e.pso(&self.gpu.pso(which));
                    e.bytes(&ma, 5);
                    e.dispatch((grid.0, grid.1, 1), tgs);
                    e.barrier();
                    bytes += TOPK * if which.contains("gu") { ld.gu.expert_bytes as usize } else { ld.dn.expert_bytes as usize };
                }
            }
            e.end();
            cb.commit();
            cb.waitUntilCompleted();
            if rep > 0 { best = best.max(bytes as f64 / (cb.GPUEndTime() - cb.GPUStartTime()) / 1e9); }
        }
        best
    }

    /// dense GEMM tiling benchmark: QKV-sized Q4_K matrices of all layers, T tokens of random activations
    pub fn kbench_gemm(&self, name: &str, bm: usize, bn: usize, t: usize) -> f64 {
        self.ensure_batch(self.chunk);
        let b = self.ba.get();
        let a = self.act.borrow().contents().as_ptr() as *mut f32;
        let half_x = name.ends_with("_3") || name.starts_with("kgb") || name.starts_with("kgs");
        let nsg: usize = if name.starts_with("kgb") || name.starts_with("kgs") { name.rsplit('_').next().unwrap().parse().unwrap() } else { 4 };
        let swap = name.starts_with("kgs");
        unsafe {
            for i in 0..t * E {
                let v = ((i * 7919) % 1000) as f32 / 500.0 - 1.0;
                if half_x {
                    // f32 -> f16 bits (simple round-to-nearest for normal range)
                    let bits = v.to_bits(); let sign = (bits >> 16) & 0x8000; let exp = ((bits >> 23) & 0xff) as i32 - 127 + 15;
                    let h: u16 = if v == 0.0 || exp <= 0 { sign as u16 } else { (sign | ((exp as u32) << 10) | ((bits >> 13) & 0x3ff)) as u16 };
                    *(a.add(b.inp[0] as usize) as *mut u16).add(i) = h;
                } else { *a.add(b.inp[0] as usize + i) = v; }
            }
        }
        let mut best = 0.0f64;
        for _ in 0..3 {
            let cb = self.gpu.cmd();
            let e = Enc::new(&cb);
            self.bind_eng(&e);
            let tt = t as u32;
            let mut flops = 0.0;
            for l in &self.layers {
                let w = &l.wq;
                let sg = GSeg { w_lo: w.off as u32, w_hi: (w.off >> 32) as u32, row_bytes: w.row_bytes, wtype: w.ty, nrows: w.rows, k: w.k,
                                in_off: b.inp[0], out_off: b.gain_o[0], ldo: w.rows, ..Default::default() };
                e.pso(&self.gpu.pso(name)); e.bytes(&sg, 5); e.bytes(&tt, 9);
                {
                if swap { e.dispatch((t.div_ceil(bn), (w.rows as usize).div_ceil(bm), 1), 32 * nsg); }
                else { e.dispatch(((w.rows as usize).div_ceil(bm), t.div_ceil(bn), 1), 32 * nsg); }
                e.barrier();
                flops += 2.0 * t as f64 * w.rows as f64 * w.k as f64;
                }
            }
            e.end(); cb.commit(); cb.waitUntilCompleted();
            best = best.max(flops / (cb.GPUEndTime() - cb.GPUStartTime()) / 1e12);
        }
        best
    }

    /// dev: after a prefill of `t` tokens, re-run only the MoE GEMM kernels of every layer on the buffers of the last layer
    /// (real routing, gathered inputs already in place): (gate/up TFLOPs, down TFLOPs)
    pub fn kbench_moe_pf(&self, t: usize) -> (f64, f64) {
        let b = self.ba.get();
        let mut res = [0.0f64; 2];
        for which in 0..2 {
            for _ in 0..3 {
                let cb = self.gpu.cmd();
                let e = Enc::new(&cb);
                self.bind_eng(&e);
                for l in &self.layers {
                    for (c, (name, _, tg)) in MOE_CLS.iter().enumerate() {
                        let g = if which == 0 { MoeGemmHArgs {
                            w_lo: l.gu.off as u32, w_hi: (l.gu.off >> 32) as u32, expert_bytes: l.gu.expert_bytes as u32, row_bytes: l.gu.row_bytes,
                            wtype: l.gu.ty, nrows: (2 * FF) as u32, k: E as u32, xh_off: b.xgh, tiles_off: b.tl[c], ntiles_off: b.ind + (c * 4) as u32,
                            out_off: b.acth, ldo: FF as u32, situ: 1, aw_off: b.aw, beta: self.situ_beta, lbeta: self.situ_lbeta,
                        } } else { MoeGemmHArgs {
                            w_lo: l.dn.off as u32, w_hi: (l.dn.off >> 32) as u32, expert_bytes: l.dn.expert_bytes as u32, row_bytes: l.dn.row_bytes,
                            wtype: l.dn.ty, nrows: E as u32, k: FF as u32, xh_off: b.acth, tiles_off: b.tl[c], ntiles_off: b.ind + (c * 4) as u32,
                            out_off: b.yd, ldo: E as u32, situ: 0, aw_off: 0, beta: 0.0, lbeta: 0.0,
                        } };
                        e.pso(&self.gpu.pso(name)); e.bytes(&g, 5);
                        e.dispatch_indirect(&self.act.borrow(), (b.ind as usize + which * 4 * NTC + c * 4) * 4, *tg);
                    }
                    e.barrier();
                }
                e.end(); cb.commit(); cb.waitUntilCompleted();
                let flops = self.layers.len() as f64 * (t * TOPK) as f64 * 2.0 * (E * FF) as f64 * if which == 0 { 2.0 } else { 1.0 };
                res[which] = res[which].max(flops / (cb.GPUEndTime() - cb.GPUStartTime()) / 1e12);
            }
        }
        (res[0], res[1])
    }

    /// per-simdgroup tile benchmark: `nact` of `nsg` simdgroups per tile do the matmul. Returns TFLOPs over the ACTIVE tokens.
    /// tensor-unit ceiling probe (fp16 x fp16 from device memory, no weight staging): TFLOPs
    pub fn kbench_peak(&self, name: &str, nsg: usize, t: usize) -> f64 {
        self.ensure_batch(self.chunk);
        let b = self.ba.get();
        let h = self.act.borrow().contents().as_ptr() as *mut u16;
        unsafe { for i in 0..t * E { *h.add(b.inp[0] as usize * 2 + i) = 0x3800 + ((i * 7919) % 512) as u16; } }
        let rows = 3072usize;
        let mut best = 0.0f64;
        for _ in 0..3 {
            let cb = self.gpu.cmd();
            let e = Enc::new(&cb);
            self.bind_eng(&e);
            let tt = t as u32;
            for _ in 0..8 {
                let sg = GSeg { k: E as u32, in_off: b.inp[0], out_off: b.gain_o[0], ldo: rows as u32, nrows: rows as u32, ..Default::default() };
                e.pso(&self.gpu.pso(name)); e.bytes(&sg, 5); e.bytes(&tt, 9);
                e.dispatch((t / (16 * nsg), rows / 64, 1), 32 * nsg);
                e.barrier();
            }
            e.end(); cb.commit(); cb.waitUntilCompleted();
            best = best.max(8.0 * 2.0 * (t * rows * E) as f64 / (cb.GPUEndTime() - cb.GPUStartTime()) / 1e12);
        }
        best
    }
    /// precision-mode probe: TFLOPs of a tile-op variant and its error against the exact-accumulation variant (layer 0 wq, realistic x)
    pub fn kbench_mode(&self, name: &str, t: usize) -> (f64, f64, f64) {
        self.ensure_batch(self.chunk);
        let b = self.ba.get();
        let a = self.act.borrow().contents().as_ptr() as *mut f32;
        let h = self.act.borrow().contents().as_ptr() as *mut u16;
        // x ~ unit-variance pseudo-normal values, as fp16
        let f16 = |v: f32| -> u16 { let bits = v.to_bits(); let sign = (bits >> 16) & 0x8000; let exp = ((bits >> 23) & 0xff) as i32 - 127 + 15;
            if v == 0.0 || exp <= 0 { sign as u16 } else { (sign | ((exp as u32) << 10) | ((bits >> 13) & 0x3ff)) as u16 } };
        let mut st = 0x9E3779B97F4A7C15u64;
        let mut rnd = || { st ^= st << 13; st ^= st >> 7; st ^= st << 17; (st >> 40) as f32 / (1u64 << 24) as f32 };
        unsafe { for i in 0..t * E { let v = rnd() + rnd() + rnd() + rnd() - 2.0; *h.add(b.inp[0] as usize * 2 + i) = f16(v * 1.7); } }
        let w = &self.layers[0].wq;
        let run = |nm: &str, out: u32| -> f64 {
            let mut best = 0.0f64;
            for _ in 0..3 {
                let cb = self.gpu.cmd();
                let e = Enc::new(&cb);
                self.bind_eng(&e);
                let tt = t as u32;
                for _ in 0..8 {
                    let sg = GSeg { w_lo: w.off as u32, w_hi: (w.off >> 32) as u32, row_bytes: w.row_bytes, wtype: w.ty, nrows: w.rows, k: w.k,
                                    in_off: b.inp[0], out_off: out, ldo: w.rows, ..Default::default() };
                    e.pso(&self.gpu.pso(nm)); e.bytes(&sg, 5); e.bytes(&tt, 9);
                    e.dispatch((t / 128, w.rows as usize / 64, 1), 256);
                    e.barrier();
                }
                e.end(); cb.commit(); cb.waitUntilCompleted();
                best = best.max(8.0 * 2.0 * t as f64 * w.rows as f64 * w.k as f64 / (cb.GPUEndTime() - cb.GPUStartTime()) / 1e12);
            }
            best
        };
        run("kgmode_0_0", b.q);
        let tf = run(name, b.gate);
        let n = t * w.rows as usize;
        let (mut se, mut sr, mut mx) = (0.0f64, 0.0f64, 0.0f64);
        unsafe { for i in 0..n { let (x, y) = (*a.add(b.gate as usize + i) as f64, *a.add(b.q as usize + i) as f64); se += (x - y) * (x - y); sr += y * y; mx = mx.max((x - y).abs()); } }
        (tf, (se / sr).sqrt(), mx / (sr / n as f64).sqrt())
    }
    pub fn kbench_sgn(&self, name: &str, nsg: usize, nact: usize, t: usize) -> f64 {
        self.ensure_batch(self.chunk);
        let b = self.ba.get();
        let a = self.act.borrow().contents().as_ptr() as *mut u16;
        unsafe { for i in 0..t * E { *a.add(b.inp[0] as usize * 2 + i) = 0x3800 + ((i * 7919) % 512) as u16; } }
        let mut best = 0.0f64;
        for _ in 0..3 {
            let cb = self.gpu.cmd();
            let e = Enc::new(&cb);
            self.bind_eng(&e);
            let tt = (t | nact << 16) as u32;
            let mut flops = 0.0;
            for l in &self.layers {
                let w = &l.wq;
                let sg = GSeg { w_lo: w.off as u32, w_hi: (w.off >> 32) as u32, row_bytes: w.row_bytes, wtype: w.ty, nrows: w.rows, k: w.k,
                                in_off: b.inp[0], out_off: b.gain_o[0], ldo: w.rows, ..Default::default() };
                e.pso(&self.gpu.pso(name)); e.bytes(&sg, 5); e.bytes(&tt, 9);
                e.dispatch((t / (16 * nsg), w.rows as usize / 64, 1), 32 * nsg);
                e.barrier();
                flops += 2.0 * (t * nact / nsg) as f64 * w.rows as f64 * w.k as f64;
            }
            e.end(); cb.commit(); cb.waitUntilCompleted();
            best = best.max(flops / (cb.GPUEndTime() - cb.GPUStartTime()) / 1e12);
        }
        best
    }

    /// dev: overwrite the scratch arenas (decode and batch) with NaN in both f32 and fp16 readings, so a result that still
    /// depends on what a previous step left there shows. The counters are not touched: their kernels leave them at zero.
    pub fn arena_map(&self) -> String { format!("{:?}\nbatch {:?}", self.ar, self.ba.get()) }
    pub fn poison_scratch(&self) {
        let a = self.act.borrow();
        let p = a.contents().as_ptr() as *mut u32;
        // dev: INK_POISON_RANGE=lo,hi poisons only words [lo, hi), to find a region that is read before it is written
        let (lo, hi) = std::env::var("INK_POISON_RANGE").ok().and_then(|v| v.split_once(',').map(|(x, y)| (x.parse().unwrap(), y.parse().unwrap())))
            .unwrap_or((0usize, usize::MAX));
        for i in lo..hi.min(a.length() / 4) { unsafe { *p.add(i) = 0x7fc0_7fc0; } }
    }
    /// bind a lane's cache (sliding-window buffer), convolution rows and tokens
    fn bind_lane(&self, e: &Enc, lane: &Lane) {
        e.buf(&lane.kv_swa.borrow(), 0, 3); e.buf(&lane.cs, 0, 4); e.buf(&lane.toks, 0, 6);
    }
    /// bind the engine's buffers: weights, parameters, scratch arenas, counters
    fn bind_eng(&self, e: &Enc) {
        e.buf(&self.w, 0, 0); e.buf(&self.pb, 0, 1); e.buf(&self.act.borrow(), 0, 2); e.buf(&self.ctr, 0, 7);
    }

    fn seg(w: &QW, in_off: u32, out_off: u32) -> Seg {
        debug_assert!(w.ty == T_Q4_K || w.ty == T_Q6_K);
        Seg { w_lo: w.off as u32, w_hi: (w.off >> 32) as u32, row_bytes: w.row_bytes, wtype: w.ty, nrows: w.rows, k: w.k,
              in_off, in2_off: -1, norm_off: -1, ss_off: -1, ss_n: 0, out_off, ..Default::default() }
    }
    fn seg_pb(off: u32, rows: u32, k: u32, in_off: u32, out_off: u32) -> Seg {
        Seg { w_lo: off, w_hi: 0, row_bytes: k * 2, wtype: T_BF16, nrows: rows, k, in_off, in2_off: -1, norm_off: -1, ss_off: -1, ss_n: 0, out_off, ..Default::default() }
    }
    #[inline]
    /// `go` without the barrier: for dispatches that do not depend on each other (one per lane), followed by one barrier
    fn go_nb<T>(&self, e: &Enc, cls: u32, pso: &str, args: &T, grid: (usize, usize, usize), tg: usize) {
        if self.skip.get() & (1 << cls) != 0 { return; }
        e.pso(&self.gpu.pso(pso));
        e.bytes(args, 5);
        e.dispatch(grid, tg);
    }
    fn go<T>(&self, e: &Enc, cls: u32, pso: &str, args: &T, grid: (usize, usize, usize), tg: usize) {
        if self.skip.get() & (1 << cls) != 0 { return; }
        e.pso(&self.gpu.pso(pso));
        e.bytes(args, 5);
        e.dispatch(grid, tg);
        e.barrier();
    }
    fn mv(&self, e: &Enc, cls: u32, segs: &[Seg]) { self.mv_post(e, cls, segs, MvArgs::default()); }
    fn mv_post(&self, e: &Enc, cls: u32, segs: &[Seg], post: MvArgs) {
        let mut a = MvArgs { nseg: segs.len() as u32, eps: self.eps, ..post };
        let mut tg = 0u32;
        for (i, s) in segs.iter().enumerate() {
            a.seg[i] = *s;
            a.seg[i].tg_start = tg;
            tg += s.nrows.div_ceil(8);
        }
        self.go(e, cls, "k_mv", &a, (tg as usize, 1, 1), 128);
    }
    /// k_mv for the `n` lanes of a decode batch (segments at lane 0's offsets; lane j's are `j * ar.total` further)
    fn mvb(&self, e: &Enc, cls: u32, segs: &[Seg], post: MvArgs, n: usize) {
        if self.skip.get() & (1 << cls) != 0 { return; }
        let mut a = MvArgs { nseg: segs.len() as u32, eps: self.eps, ..post };
        let mut tg = 0u32;
        for (i, s) in segs.iter().enumerate() { assert!(s.epi == 0); a.seg[i] = *s; a.seg[i].tg_start = tg; tg += s.nrows.div_ceil(8); }
        e.pso(&self.gpu.pso("k_mvb"));
        e.bytes(&a, 5);
        e.bytes(&[n as u32, self.ar.total], 8);
        e.dispatch((tg as usize, 1, 1), 128);
        e.barrier();
    }
    /// decode: one token, the decode arena's layout
    fn mix(&self, e: &Enc, cls: u32, jobs: &[MixJob]) { self.mix_impl(e, cls, jobs, 1, false); }
    /// prefill: `t` tokens in the batch arena's layout. A batch of one token is still a batch: its jobs carry the batch
    /// layout (token stride, fp16 outputs), which the decode kernel does not read.
    fn mix_t(&self, e: &Enc, cls: u32, jobs: &[MixJob], t: usize) { self.mix_impl(e, cls, jobs, t, true); }
    fn mix_impl(&self, e: &Enc, cls: u32, jobs: &[MixJob], t: usize, batch: bool) {
        if self.skip.get() & (1 << cls) != 0 { return; }
        if batch && std::env::var("INK_OLD_MIXB").is_err() {
            // independent jobs run concurrently (no barrier between them)
            for j in jobs {
                let j = MixJob { ntok: t as u32, ..*j };
                let name = if j.q_off < 0 { "k_mix_b_id".to_string() } else { format!("k_mix_b_{}_{}", j.n_s, j.n_q) };
                e.pso(&self.gpu.pso(&name));
                e.bytes(&j, 5);
                e.dispatch((t.div_ceil(4), 1, 1), 128);
            }
            e.barrier();
            return;
        }
        e.pso(&self.gpu.pso("k_mix"));
        e.bytes_slice(jobs, 5);
        e.dispatch((jobs.len(), t, 1), 768);
        e.barrier();
    }
    fn mix_job(&self, states: &[usize], q_off: i32, n_q: u32, outs: &[u32], norms: &[i32]) -> MixJob {
        let offs: Vec<u32> = states.iter().map(|&s| self.v(s)).collect();
        self.mix_job_off(&offs, q_off, n_q, outs, norms, 0)
    }
    fn mix_job_off(&self, states: &[u32], q_off: i32, n_q: u32, outs: &[u32], norms: &[i32], tstride: u32) -> MixJob {
        let mut j = MixJob { n_s: states.len() as u32, n_q, q_off, eps: self.eps, tstride, ..Default::default() };
        assert!(states.len() <= 6);
        for (i, &s) in states.iter().enumerate() { j.s_off[i] = s; }
        for i in 0..4 { j.norm_off[i] = -1; j.ncopy[i] = [-1; 3]; j.hcopy[i] = [-1; 3]; j.hout[i] = -1; }
        j.hstride = (2 * E) as u32;
        for (i, &o) in outs.iter().enumerate() { j.out_off[i] = o; }
        for (i, &n) in norms.iter().enumerate() { j.norm_off[i] = n; }
        j
    }
    /// loop controllers, then RMSNorm of each controlled vector (norm weight w, written to out)
    /// decode loop controllers: g = D.cat[j] (bf16 matvec on the mix kernel's normalized copies), then gain/forget apply
    /// with the RMSNorm fused into the last threadgroup of each controller
    fn ctrl_dec(&self, e: &Enc, jobs: &[CtrlJob]) {
        let ar = self.ar_at();
        let segs: Vec<Seg> = jobs.iter().map(|j| {
            let c = ar.ctrlg.iter().position(|&o| o == j.out_off).unwrap();
            Self::seg_pb(j.d_off, 64, (2 * E) as u32, ar.cat[c], j.out_off)
        }).collect();
        self.mv(e, C_CTRLD, &segs);
        let mut a = CtrlArgs { n_c: jobs.len() as u32, ..Default::default() };
        for (i, j) in jobs.iter().enumerate() { a.job[i] = *j; }
        self.go(e, C_CTRLA, "k_ctrl_apply", &a, (E / 32, jobs.len(), 1), 256);
    }

    fn gemm(&self, e: &Enc, cls: u32, segs: &[GSeg], t: usize) {
        let mut a = GemmArgs { nseg: segs.len() as u32, t: t as u32, ..Default::default() };
        let mut tiles = 0u32;
        for (i, sg) in segs.iter().enumerate() { a.seg[i] = *sg; a.seg[i].tile_start = tiles; tiles += sg.nrows.div_ceil(64); }
        self.go(e, cls, if self.tensor { "k_gemm_t" } else { "k_gemm" }, &a, (tiles as usize, t.div_ceil(32), 1), 128);
    }
    /// batched loop controllers as GEMMs: g = D.[u^;m^] (split-input GEMM over the mix kernel's normalized copies),
    /// gain/forget pre-activations = G.g, F.g (K=64 GEMM), then a fused apply + RMSNorm per token
    /// jobs: (controller index c, loop weights, u_off, m_off, out_off, norm weight)
    /// fp16 path: the mix kernels already wrote [u^ | m^] as half into cath; `hout[i]` receives job i's output as half
    /// (`nof32[i]`: only as half)
    fn ctrl_b(&self, e: &Enc, lw: &LoopW, jobs: &[(usize, u32, u32, u32, u32)], t: usize, hout: &[(u32, bool)]) {
        let b = self.ba.get();
        let pb = |off: u32, rows: u32, k: u32, in_off: u32, in2: u32, out: u32| GSeg {
            w_lo: off, w_hi: 0, row_bytes: k * 2, wtype: T_BF16, nrows: rows, k, in_off, out_off: out, ldo: rows, wsrc: 1, in2, ..Default::default() };
        if self.hgemm {
            let down: Vec<HSeg> = jobs.iter().map(|&(c, _, _, _, _)| Self::hseg_pb(lw.down[c], 64, (2 * E) as u32, b.cath[c], b.ctrlg[c])).collect();
            self.gemm_h(e, C_CTRLD, &down, t);
            for &(c, _, _, _, _) in jobs { self.tohalf(e, C_CTRLA, b.ctrlg[c], 64, b.gh[c], 64, 64, t, None); }
            e.barrier();
            // gain/forget GEMM with the apply fused into its epilogue: raw (un-normalized) outputs land in gain_o
            let mut ga = CtrlGfArgs { t: t as u32, njobs: jobs.len() as u32, ..Default::default() };
            for (i, &(c, u, m, _, _)) in jobs.iter().enumerate() {
                ga.job[i] = CtrlGfJob { gain_w: lw.gain[c], forget_w: lw.forget[c], gh_off: b.gh[c], u_off: u, m_off: m, raw_off: b.gain_o[c], pad: [0; 2] };
            }
            self.go(e, C_CTRLA, "k_ctrl_gf", &ga, (t.div_ceil(128), jobs.len() * (E / 32), 1), 256);
        } else {
            let down: Vec<GSeg> = jobs.iter().map(|&(c, _, _, _, _)| pb(lw.down[c], 64, (2 * E) as u32, b.cat_u[c], b.cat_m[c], b.ctrlg[c])).collect();
            self.gemm(e, C_CTRLD, &down, t);
            let mut gf = Vec::new();
            for &(c, _, _, _, _) in jobs {
                gf.push(pb(lw.gain[c], E as u32, 64, b.ctrlg[c], 0, b.gain_o[c]));
                gf.push(pb(lw.forget[c], E as u32, 64, b.ctrlg[c], 0, b.forget_o[c]));
            }
            self.gemm(e, C_CTRLA, &gf, t);
        }
        let mut a = CtrlBArgs { n: jobs.len() as u32, ..Default::default() };
        for (i, &(c, u, m, out, nw)) in jobs.iter().enumerate() {
            let (h, nof) = hout.get(i).map(|&(h, n)| (h as i32, n as u32)).unwrap_or((-1, 0));
            a.job[i] = if self.hgemm { CtrlBJob { u_off: b.gain_o[c], m_off: u32::MAX, g_off: 0, f_off: 0, out_off: out, norm_w: nw, h_off: h, nof32: nof } }
                       else { CtrlBJob { u_off: u, m_off: m, g_off: b.gain_o[c], f_off: b.forget_o[c], out_off: out, norm_w: nw, h_off: h, nof32: nof } };
        }
        self.go(e, C_CTRLN, "k_ctrl_apply_b", &a, (t, jobs.len(), 1), 256);
    }

    /// f32 [rows][k] (row stride src_stride, optional row gather) -> half [rows][dst_stride] at dst
    #[allow(clippy::too_many_arguments)]
    fn tohalf(&self, e: &Enc, cls: u32, src: u32, src_stride: usize, dst: u32, dst_stride: usize, k: usize, rows: usize, perm: Option<u32>) {
        self.tohalf_x(e, cls, src, src_stride, dst, dst_stride, k, rows, perm, false)
    }
    /// `half_src`: the source is already half (row stride in halves): a plain (gathered) copy
    #[allow(clippy::too_many_arguments)]
    fn tohalf_x(&self, e: &Enc, cls: u32, src: u32, src_stride: usize, dst: u32, dst_stride: usize, k: usize, rows: usize, perm: Option<u32>, half_src: bool) {
        let a = ToHalfArgs { src_off: src, dst_off: dst, k: k as u32, rows: rows as u32, perm_off: perm.unwrap_or(0), gather: perm.is_some() as u32 | (half_src as u32) << 1,
                             src_stride: src_stride as u32, dst_stride: dst_stride as u32 };
        if self.skip.get() & (1 << cls) != 0 { return; }
        e.pso(&self.gpu.pso("k_tohalf")); e.bytes(&a, 5); e.dispatch((k.div_ceil(1024), rows, 1), 256);
    }
    /// fp16-activation tensor GEMM over segments (weight, wsrc, half input, output, ldo); barrier after
    fn gemm_h(&self, e: &Enc, cls: u32, segs: &[HSeg], t: usize) {
        let mut a = GemmHArgs { nseg: segs.len() as u32, t: t as u32, ..Default::default() };
        let mut tiles = 0u32;
        for (i, sg) in segs.iter().enumerate() { a.seg[i] = *sg; a.seg[i].tile_start = tiles; tiles += sg.nrows.div_ceil(64); }
        if t > 64 { self.go(e, cls, "k_gemm_sg8", &a, (t.div_ceil(128), tiles as usize, 1), 256); }
        else { self.go(e, cls, "k_gemm_sg4", &a, (1, tiles as usize, 1), 128); }
    }
    fn hseg(w: &QW, xh: u32, out: u32) -> HSeg {
        HSeg { w_lo: w.off as u32, w_hi: (w.off >> 32) as u32, row_bytes: w.row_bytes, wtype: w.ty, nrows: w.rows, k: w.k, xh_off: xh, out_off: out, ldo: w.rows, ..Default::default() }
    }
    fn hseg_pb(off: u32, rows: u32, k: u32, xh: u32, out: u32) -> HSeg {
        HSeg { w_lo: off, w_hi: 0, row_bytes: k * 2, wtype: T_BF16, nrows: rows, k, xh_off: xh, out_off: out, ldo: rows, wsrc: 1, ..Default::default() }
    }

    fn gseg(w: &QW, in_off: u32, out_off: u32) -> GSeg {
        GSeg { w_lo: w.off as u32, w_hi: (w.off >> 32) as u32, row_bytes: w.row_bytes, wtype: w.ty, nrows: w.rows, k: w.k,
               in_off, out_off, ldo: w.rows, ..Default::default() }
    }


    /// Encode one decode step for each of `lanes` (lane, position of the token it decodes) in one command buffer, through
    /// the decode path: every lane runs today's single-stream kernels on its own copy of the decode arena with its own
    /// buffers bound, layer by layer, so each lane's numbers are exactly what it gets decoding alone. Logits for each lane
    /// land in its arena (`dbatch_logits`). Needs `ensure_dbatch(lanes.len())`; no disk tier, no pruning.
    /// This is the decode-path reference the `dbatch` harness checks (`INK_DB_OLD`), not the serving path:
    /// `inky serve` decodes through `encode_decode`.
    pub fn encode_dbatch(&self, cb: &CmdBuf, lanes: &[(&Lane, usize)], logits: bool) {
        let n = lanes.len();
        let (start, tot) = (self.dstart.get(), self.ar.total);
        assert!(n >= 1 && start >= self.ar.total && start >= self.ba.get().end && self.act.borrow().length() >= (start + n as u32 * tot) as usize * 4,
                "decode batch arenas: call ensure_dbatch() after ensure_batch()");
        for (l, pos) in lanes { assert!(l.tier.is_none() && l.prune.borrow().is_none() && *pos < self.on(l).kv_capacity(), "K/V capacity: call reserve() first"); }
        let base = |j: usize| start + j as u32 * tot;
        let mut cx = Cx { e: Enc::new(cb), cb: None };
        self.bind_eng(&cx.e);
        for (j, (l, pos)) in lanes.iter().enumerate() {
            self.abase.set(base(j));
            self.bind_lane(&cx.e, l);
            let ea = EmbedArgs { emb_lo: self.tok_embd.off as u32, emb_hi: (self.tok_embd.off >> 32) as u32, row_bytes: self.tok_embd.row_bytes,
                                 norm_off: self.tok_norm, out0: self.v(0), out1: self.v(0), tok_index: *pos as u32, eps: self.eps };
            self.go(&cx.e, C_EMBED, "k_embed", &ea, (1, 1, 1), 256);
        }
        // the residual bookkeeping is the same for every lane: run it once per layer, and every lane's layer from it
        let (mut cur, mut blocks, mut va, mut exec) = (0usize, Vec::<usize>::new(), VecAlloc { next: 1, n: N_VEC }, 0usize);
        let stage_a = std::env::var("INK_DBATCH_A").is_ok();
        let mut layer_all = |cx: &mut Cx, cur: &mut usize, blocks: &mut Vec<usize>, va: &mut VecAlloc, slot: usize, ip: usize, msg: Option<(usize, usize)>, pinned: &[usize]| {
            if !stage_a {
                *cur = self.layer_db(&cx.e, lanes, start, exec, slot, ip, *cur, blocks, msg, va, pinned);
                exec += 1;
                return;
            }
            let mut after = None;
            for (j, (l, pos)) in lanes.iter().enumerate() {
                self.abase.set(base(j));
                self.bind_lane(&cx.e, l);
                self.done.set(exec);
                let (mut b2, mut v2) = (blocks.clone(), va.clone());
                let c2 = self.on(l).layer(cx, *pos, slot, ip, *cur, &mut b2, msg, &mut v2, pinned);
                after = Some((c2, b2, v2));
            }
            let (c2, b2, v2) = after.unwrap();
            *cur = c2; *blocks = b2; *va = v2; exec += 1;
        };
        for ip in 0..LOOP_START { layer_all(&mut cx, &mut cur, &mut blocks, &mut va, ip, ip, None, &[]); }
        let prelude = cur;
        let prelude_blocks = blocks.clone();
        let mut bank = (0usize, 0usize);
        for pass in 0..N_PASS {
            if pass > 0 { cur = prelude; blocks = prelude_blocks.clone(); }
            let msg = if pass > 0 { Some(bank) } else { None };
            let pinned: Vec<usize> = { let mut p = vec![prelude]; if pass > 0 { p.push(bank.0); p.push(bank.1); } p };
            for ip in LOOP_START..LOOP_END { layer_all(&mut cx, &mut cur, &mut blocks, &mut va, slot_of(pass, ip), ip, msg, &pinned); }
            bank = (*blocks.get(prelude_blocks.len() + 1).unwrap_or(&0), cur);
        }
        for ip in LOOP_END..N_PHYS { layer_all(&mut cx, &mut cur, &mut blocks, &mut va, ip, ip, None, &[]); }
        if logits {
            self.abase.set(base(0));
            let ar = self.ar_at();
            let mut st = blocks.clone(); st.push(cur);
            let mut jb = self.mix_job(&st, self.output_res_query as i32, 1, &[ar.fout], &[self.output_norm as i32]);
            jb.tstride = tot;
            self.mix_impl(&cx.e, C_OMIX, &[jb], n, false);
            self.mvb(&cx.e, C_LMHEAD, &[Engine::seg(&self.output, ar.fout, ar.logits)], MvArgs::default(), n);
        }
        self.abase.set(0);
        cx.e.end();
    }

    /// One layer execution of a decode batch: the decode path's layer for every lane, with the row-wise work (mixes,
    /// projections, output projection, router) run once over all lanes and the rest per lane. Each lane's arithmetic is
    /// the decode path's, so its numbers are exactly what it gets decoding alone.
    #[allow(clippy::too_many_arguments)]
    fn layer_db(&self, e: &Enc, lanes: &[(&Lane, usize)], start: u32, exec: usize, slot: usize, ip: usize, cur: usize,
                blocks: &mut Vec<usize>, msg: Option<(usize, usize)>, va: &mut VecAlloc, pinned: &[usize]) -> usize {
        let n = lanes.len();
        let tot = self.ar.total;
        let at = |j: usize| self.abase.set(start + j as u32 * tot);
        let l = &self.layers[ip];
        let ctl = msg.map(|_| &self.loops[ip - LOOP_START]);
        let cs_slot = (slot * NCH * 3) as u32;
        let ts = |mut j: MixJob| { j.tstride = tot; j };
        at(0);
        let ar = self.ar_at();
        // ---- attention inputs: depth mix (+ loop control) -> normalized q/k/v inputs
        let norms: [i32; 3] = if l.swa { [l.attn_norm; 3] } else { [l.q_inp_norm, l.k_inp_norm, l.v_inp_norm] };
        let mut st = blocks.clone(); st.push(cur);
        let nq = if l.attn_res_query >= 0 { l.n_attn_q } else { 1 };
        let xin: [u32; 3];
        if let Some(lw) = ctl {
            let (b0, b1) = msg.unwrap();
            let mut ja = self.mix_job(&st, l.attn_res_query, nq, &ar.inp[..nq as usize], &[]);
            if l.swa { ja.ncopy[0] = [ar.cat[0] as i32, ar.cat[1] as i32, ar.cat[2] as i32]; }
            else { for j in 0..3 { ja.ncopy[j][0] = ar.cat[j] as i32; } }
            let mut jm = self.mix_job(&[b0, b1], lw.msg_query as i32, 4, &ar.msg, &[]);
            for j in 0..4 { jm.ncopy[j][0] = (ar.cat[j] + E as u32) as i32; }
            self.mix_impl(e, C_MIXA, &[ts(ja), ts(jm)], n, false);
            for jl in 0..n {
                at(jl);
                let ar = self.ar_at();
                let mut cj = Vec::new();
                for j in 0..3 {
                    let u = if l.swa { ar.inp[0] } else { ar.inp[j] };
                    cj.push(CtrlJob { u_off: u, m_off: ar.msg[j], d_off: lw.down[j], out_off: ar.ctrlg[j], gain_off: lw.gain[j],
                                      forget_off: lw.forget[j], res_out: ar.cin[j], ss_off: ar.ssp[j], norm_w: norms[j], ctr: 8 + j as u32 });
                }
                self.ctrl_dec(e, &cj);
            }
            at(0);
            xin = ar.cin;
        } else {
            let nn: Vec<i32> = if l.swa { vec![l.attn_norm] } else { norms.to_vec() };
            self.mix_impl(e, C_MIXA, &[ts(self.mix_job(&st, l.attn_res_query, nq, &ar.inp[..nq as usize], &nn))], n, false);
            xin = if l.swa { [ar.inp[0]; 3] } else { [ar.inp[0], ar.inp[1], ar.inp[2]] };
        }
        // ---- fused projections, once for every lane
        let mut segs = vec![Engine::seg(&l.wq, xin[0], ar.q), Engine::seg(&l.wgate, xin[0], ar.gate), Engine::seg(&l.wrel, xin[0], ar.rel),
                            Engine::seg(&l.wsink, xin[0], ar.sink), Engine::seg(&l.wk, xin[1], ar.k), Engine::seg(&l.wv, xin[2], ar.v)];
        if let (Some(kp), Some(vp)) = (&l.kconvp, &l.vconvp) { segs.push(Engine::seg(kp, xin[1], ar.kdyn)); segs.push(Engine::seg(vp, xin[2], ar.vdyn)); }
        self.mvb(e, C_QKV, &segs, MvArgs::default(), n);
        // ---- attention (split-K, fused K/V conv and cache write) per lane, on the lane's cache and state
        for (jl, (lane, pos)) in lanes.iter().enumerate() {
            at(jl);
            let ar = self.ar_at();
            let run = self.on(lane);
            self.bind_lane(e, lane);
            let pos = *pos;
            let (kb, vb, kcap) = run.kv_slot(slot);
            let tau = if l.swa { 1.0 } else { 1.0 + self.log_alpha * (((pos + 1) as f32 / self.log_floor).max(1.0)).ln() };
            let base = AttnArgs {
                pos: pos as u32, cap: kcap, k_base: kb, v_base: vb, cs_off: cs_slot,
                q_off: ar.q, k_off: ar.k, v_off: ar.v, r_off: ar.rel, sink_off: ar.sink,
                kdyn_off: if l.swa { -1 } else { ar.kdyn as i32 }, vdyn_off: if l.swa { -1 } else { ar.vdyn as i32 }, part_off: ar.part,
                kconv_w: l.k_conv, vconv_w: l.v_conv, qclip_w: l.q_clip, relclip_w: l.rel_clip, relbias_w: l.rel_bias, tau,
                ctr: 0, gate_off: ar.gate, out_off: ar.attn_g, gstr: kcap * HD as u32, istr: HD as u32, ..Default::default()
            };
            run.bind_kv(e, l.swa);
            let nkeys = if l.swa { (pos + 1).min(EXT) } else { pos + 1 };
            let kps = Run::split_kps(nkeys);
            let nsplit = nkeys.div_ceil(kps);
            let aa = AttnArgs { nkeys: nkeys as u32, first_pos: (pos + 1 - nkeys) as u32, ring: l.swa as u32, kps: kps as u32, nsplit: nsplit as u32,
                                pad: self.sep_comb as u32, ..base };
            self.go(e, C_ATTN, "k_attn", &aa, (nsplit, NKV, 1), 128);
            if self.sep_comb {
                let ca = CombineArgs { part_off: ar.part, nsplit: nsplit as u32, gate_off: ar.gate, out_off: ar.attn_g };
                self.go(e, C_COMB, "k_attn_combine", &ca, (NH, 1, 1), 128);
            }
        }
        at(0);
        // ---- output projection once for every lane, then each lane's post-conv (+ residual) on its own state
        let block_start = ip % BLOCK == 0;
        let mut live: Vec<usize> = blocks.clone(); live.push(cur); live.extend_from_slice(pinned);
        let mid = va.alloc(&live);
        self.mvb(e, C_WO, &[Engine::seg(&l.wo, ar.attn_g, self.v(mid))], MvArgs::default(), n);
        for (jl, (lane, _)) in lanes.iter().enumerate() {
            at(jl);
            self.bind_lane(e, lane);
            let ea = EpiArgs { out_off: self.v(mid), res_off: if block_start { -1 } else { self.v(cur) as i32 }, cs_off: cs_slot + (2 * NKV * HD * 3) as u32,
                               convw_off: l.attn_post_conv, n: E as u32, pad: [0; 3] };
            self.go(e, C_WO, "k_epi", &ea, (E.div_ceil(256), 1, 1), 256);
        }
        at(0);
        if block_start { blocks.push(cur); }
        // ---- FFN input: depth mix (+ control) -> normalized
        let mut st = blocks.clone(); st.push(mid);
        let x_off = if let Some(lw) = ctl {
            let mut jf = self.mix_job(&st, l.ffn_res_query as i32, 1, &[ar.fin], &[]);
            jf.ncopy[0][0] = ar.cat[3] as i32;
            self.mix_impl(e, C_MIXF, &[ts(jf)], n, false);
            for jl in 0..n {
                at(jl);
                let ar = self.ar_at();
                self.ctrl_dec(e, &[CtrlJob { u_off: ar.fin, m_off: ar.msg[3], d_off: lw.down[3], out_off: ar.ctrlg[3], gain_off: lw.gain[3],
                                             forget_off: lw.forget[3], res_out: ar.finc, ss_off: ar.ssp[3], norm_w: l.ffn_norm as i32, ctr: 11 }]);
            }
            at(0);
            ar.finc
        } else {
            self.mix_impl(e, C_MIXF, &[ts(self.mix_job(&st, l.ffn_res_query as i32, 1, &[ar.fin], &[l.ffn_norm as i32]))], n, false);
            ar.fin
        };
        // ---- router with fused top-k, once for every lane
        assert!(!self.sep_topk);
        let sel = ar.sel_log + (exec * 16) as u32;
        let rpost = MvArgs { post: 1, ctr: 12, bias_off: l.exp_bias, sel_off: sel, wscale: self.wscale, ..Default::default() };
        self.mvb(e, C_ROUTER, &[Engine::seg_pb(l.router, NEXP as u32, E as u32, x_off, ar.rlog)], rpost, n);
        let mut live: Vec<usize> = blocks.clone(); live.push(mid); live.extend_from_slice(pinned);
        let out = va.alloc(&live);
        // ---- experts per lane (the down projection's epilogue updates the lane's state)
        for (jl, (lane, _)) in lanes.iter().enumerate() {
            at(jl);
            let ar = self.ar_at();
            self.bind_lane(e, lane);
            let sel = ar.sel_log + (exec * 16) as u32;
            let x = if ctl.is_some() { ar.finc } else { ar.fin };
            let ma = MoeArgs {
                logits_off: ar.rlog, bias_off: l.exp_bias, sel_off: sel, x_off: x,
                norm_off: -1, ss_off: -1, ss_n: 0, eps: self.eps,
                gu_lo: l.gu.off as u32, gu_hi: (l.gu.off >> 32) as u32, gu_expert_bytes: l.gu.expert_bytes as u32, gu_row_bytes: l.gu.row_bytes,
                act_off: ar.act, wscale: self.wscale, beta: self.situ_beta, lbeta: self.situ_lbeta,
                dn_lo: l.dn.off as u32, dn_hi: (l.dn.off >> 32) as u32, dn_expert_bytes: l.dn.expert_bytes as u32, dn_row_bytes: l.dn.row_bytes,
                dn_type: l.dn.ty, out_off: self.v(out), res_off: self.v(mid) as i32, cs_off: cs_slot + ((2 * NKV * HD + E) * 3) as u32,
                convw_off: l.ffn_post_conv, pad: [0; 3],
            };
            self.go(e, C_GU, "k_moe_gu", &ma, (FF / 4, TOPK, 1), 128);
            self.go(e, C_DOWN, "k_moe_down", &ma, (E / 4, 1, 1), 64);
        }
        at(0);
        out
    }

    /// Encode one step over `spans`, each lane's positions [p0, p0 + t), as one batch: row-wise work (mixes, controllers,
    /// projections, router, experts) runs once over every span's rows; what couples a row to its lane (embedding, K/V
    /// preparation and cache writes, attention, the convolutions and their state) runs span by span with that lane's
    /// buffers bound. Optionally computes each span's logits (and argmax, written to the lane's tokens) for its last row:
    /// in the decode arena for a single span (`logits()`), in the batch arena for several (`span_logits`).
    pub fn encode_spans(&self, cb: &CmdBuf, spans: &[Span], logits: bool, argmax: bool) { self.encode_spans_x(cb, spans, logits, argmax, false, None); }
    /// Verify step. Each span has length `1 + k` (the last committed token again, then `k` drafts). `k` is the
    /// argument, not a fixed 8. Writes one vocabulary row per position into `b.lg`; `span_logits` names the row.
    /// Argmax is off: that flag writes `toks[p0 + t]`, where the host has just placed the draft tokens.
    /// Long prefills stay on `encode_spans` (one row per span, the last). This does not grow `lg` to one row per token.
    pub fn encode_verify(&self, cb: &CmdBuf, spans: &[Span], k: usize) {
        assert!(!spans.is_empty() && spans.iter().all(|s| s.t == 1 + k), "a verify span has length 1 + k");
        // A --kv-disk lane stages cache from the single-token step path. This span does not, and it must not run there.
        assert!(spans.iter().all(|s| s.lane.tier.is_none()), "verify does not run on a --kv-disk lane");
        let t = 1 + k;
        let stride = N_SLOT * NCH * 3;
        let cs = self.gpu.buffer(spans.len() * t * stride * 4);
        unsafe { std::ptr::write_bytes(cs.contents().as_ptr() as *mut u8, 0, spans.len() * t * stride * 4); }
        let kept = spans.iter().map(|sp| VerifySpan {
            lane: sp.lane as *const Lane,
            p0: sp.p0,
            kv: self.snapshot_kv_positions(sp.lane, sp.p0, sp.t),
            accepted_end: None,
        }).collect();
        *self.verify_keep.borrow_mut() = Some(VerifyKeep { cs, stride, t, spans: kept });
        self.encode_spans_x(cb, spans, true, false, false, Some(k));
    }
    /// Put span `si` of the last `encode_verify` back to its first `n_keep` positions (1..=1+k).
    /// The convolution image at that position replaces `cs`. K/V rows past it are put back to the bytes from before
    /// the span, so a rejected draft and anything after it do not stay. The recorded length is `p0 + n_keep`, never
    /// a seal of the span's end. The images live on `verify_keep`, not in serve's `cs_at_prompt`. Nothing may be in
    /// flight on the GPU. The correction token is not written here: its K and V land when that token is encoded.
    pub fn rollback_verify(&self, lane: &Lane, si: usize, n_keep: usize) {
        assert!(lane.tier.is_none(), "verify does not run on a --kv-disk lane");
        let tail = {
            let mut slot = self.verify_keep.borrow_mut();
            let g = slot.as_mut().expect("rollback_verify without encode_verify");
            assert!(si < g.spans.len(), "verify span {si} is out of range");
            let t = g.t;
            let stride = g.stride;
            let cs = g.cs.clone();
            assert!(n_keep >= 1 && n_keep <= t, "n_keep {n_keep} is outside the verify span of {t}");
            let rec = &mut g.spans[si];
            assert!(std::ptr::eq(rec.lane, lane), "rollback lane is not the lane encode_verify ran");
            let end = rec.p0 + n_keep;
            // Layer 2 has no block table. The only length this step records is the accepted prefix.
            rec.accepted_end = Some(end);
            assert!(end <= rec.p0 + t);
            if n_keep < t { assert!(end < rec.p0 + t, "a rejected tail is not sealed"); }
            let bytes = stride * 4;
            assert_eq!(bytes, lane.cs.length());
            unsafe {
                let src = (cs.contents().as_ptr() as *const u8).add((si * t + (n_keep - 1)) * bytes);
                std::ptr::copy_nonoverlapping(src, lane.cs.contents().as_ptr() as *mut u8, bytes);
            }
            rec.kv.iter().skip(n_keep).cloned().collect::<Vec<_>>()
        };
        for undo in &tail { self.restore_kv_pos(lane, undo); }
    }
    /// Bytes of every slot's K and V row at positions [p0, p0+t), before the span writes them.
    fn snapshot_kv_positions(&self, lane: &Lane, p0: usize, t: usize) -> Vec<KvUndo> {
        let eb = self.kvb;
        let row = HD * eb;
        let mut out = Vec::with_capacity(t);
        for d in 0..t {
            let pos = p0 + d;
            let mut rows = Vec::new();
            for slot in 0..N_SLOT {
                let (swa, i) = self.slot_kind[slot];
                let cap = if swa { lane.swa_cap.get() } else { lane.full_cap.get() };
                assert!(swa || pos < cap, "full-attention row {pos} is past the cache");
                let idx = if swa { pos % cap } else { pos };
                let buf = if swa { lane.kv_swa.borrow() } else { lane.kv_full.borrow() };
                let base = buf.contents().as_ptr() as *const u8;
                let k0 = i * 2 * NKV * cap * HD * eb;
                for which in 0..2 {
                    let b0 = k0 + which * NKV * cap * HD * eb;
                    for g in 0..NKV {
                        let off = b0 + (g * cap + idx) * row;
                        rows.extend_from_slice(unsafe { std::slice::from_raw_parts(base.add(off), row) });
                    }
                }
            }
            out.push(KvUndo { pos, rows });
        }
        out
    }
    fn restore_kv_pos(&self, lane: &Lane, undo: &KvUndo) {
        let eb = self.kvb;
        let row = HD * eb;
        let pos = undo.pos;
        let mut at = 0usize;
        for slot in 0..N_SLOT {
            let (swa, i) = self.slot_kind[slot];
            let cap = if swa { lane.swa_cap.get() } else { lane.full_cap.get() };
            let idx = if swa { pos % cap } else { pos };
            let buf = if swa { lane.kv_swa.borrow() } else { lane.kv_full.borrow() };
            let base = buf.contents().as_ptr() as *mut u8;
            let k0 = i * 2 * NKV * cap * HD * eb;
            for which in 0..2 {
                let b0 = k0 + which * NKV * cap * HD * eb;
                for g in 0..NKV {
                    let off = b0 + (g * cap + idx) * row;
                    unsafe { std::ptr::copy_nonoverlapping(undo.rows.as_ptr().add(at), base.add(off), row); }
                    at += row;
                }
            }
        }
        assert_eq!(at, undo.rows.len());
    }
    /// Write this conv site's state after every prefix of the span. Runs only while a verify span is recording,
    /// and only before `k_state_upd` overwrites `cs` with the state at the end.
    fn keep_state(&self, e: &Enc, si: usize, su: &StateArgs, cls: u32, grid: (usize, usize, usize)) {
        if !self.verify_record.get() { return; }
        let (sk, buf) = {
            let slot = self.verify_keep.borrow();
            let g = slot.as_ref().expect("verify recording without convolution images");
            let sk = StateKeepArgs {
                x_off: su.x_off, ldx: su.ldx, cs_off: su.cs_off, nch: su.nch, t: su.t,
                snap_off: (si * g.t * g.stride) as u32, stride: g.stride as u32, pad: 0,
            };
            (sk, g.cs.clone())
        };
        e.buf(&buf, 0, 9);
        self.go(e, cls, "k_state_keep", &sk, grid, 256);
    }
    /// Encode one decode step: one token for each lane in `spans` (every span has t = 1). Row-wise work runs through the
    /// batched tensor path as in `encode_spans`; each lane's attention is the decode path's fused kernel (K/V conv, cache
    /// write, split-K attention over its own cache). A decode step's arithmetic is its own: a token's numbers depend
    /// neither on the other lanes in the step nor on how many there are, but differ from a prefill row's, so cache
    /// blocks written by decode steps are never shared (docs/DESIGN.md).
    pub fn encode_decode(&self, cb: &CmdBuf, spans: &[Span], logits: bool, argmax: bool) {
        assert!(spans.iter().all(|s| s.t == 1));
        self.encode_spans_x(cb, spans, logits, argmax, true, None);
    }
    fn encode_spans_x(&self, cb: &CmdBuf, spans: &[Span], logits: bool, argmax: bool, dec: bool, verify_k: Option<usize>) {
        assert!(!spans.is_empty() && spans.len() <= MAX_SPANS);
        let t: usize = spans.iter().map(|s| s.t).sum();
        for sp in spans { assert!(sp.t >= 1 && sp.p0 + sp.t <= self.on(sp.lane).kv_capacity(), "K/V capacity: call reserve() first"); }
        assert!(spans.len() == 1 || self.hgemm, "several spans need the fp16 path");
        assert!(!dec || (self.hgemm && 2 * spans.len() <= self.ba.get().cap), "a decode step needs the fp16 path and room for f32 queries");
        assert!(!dec || (self.dstart.get() >= self.ba.get().end && self.act.borrow().length() >= (self.dstart.get() as usize + spans.len() * self.ar.total as usize) * 4),
                "a decode step keeps each lane's split partials in the decode-batch region: call ensure_dbatch(n) after ensure_batch()");
        assert!(t <= self.chunk && t <= self.ba.get().cap, "batch arena: call ensure_batch() first");
        if let Some(k) = verify_k {
            assert!(!dec && !argmax, "verify is not a decode step, and argmax would overwrite toks[p0 + t]");
            let need = spans.len() * (1 + k);
            assert!(need <= self.ba.get().lg_rows as usize,
                    "verify needs {need} vocabulary rows and this batch has {}; lg is not grown to one row per token",
                    self.ba.get().lg_rows);
        }
        let mut row0 = Vec::with_capacity(spans.len());
        { let mut r = 0; for sp in spans { row0.push(r); r += sp.t; } }
        let e = Enc::new(cb);
        self.bind_eng(&e);
        self.bind_lane(&e, spans[0].lane);
        // the window K/V scratch (sk, then sv) starts every prefill at zero: see k_zero (a decode step does not use it)
        if !dec {
            let bz = self.ba.get();
            let zr = [bz.sk, bz.perm - bz.sk];
            e.pso(&self.gpu.pso("k_zero")); e.bytes(&zr, 5); e.dispatch(((zr[1] as usize).div_ceil(1024), 1, 1), 256);
            e.barrier();
        }
        let b = self.ba.get();
        for (si, sp) in spans.iter().enumerate() {
            let r0 = (row0[si] * E) as u32;
            let ea = EmbedArgs { emb_lo: self.tok_embd.off as u32, emb_hi: (self.tok_embd.off >> 32) as u32, row_bytes: self.tok_embd.row_bytes,
                                 norm_off: self.tok_norm, out0: b.vec(0) + r0, out1: b.vec(0) + r0, tok_index: sp.p0 as u32, eps: self.eps };
            e.buf(&sp.lane.toks, 0, 6);
            self.go_nb(&e, C_EMBED, "k_embed", &ea, (sp.t, 1, 1), 256);
        }
        e.barrier();
        // Last layer: nothing after the batch reads its outputs except the final token (logits) and the conv states (last 3
        // rows of the two branch outputs). It is a full-attention layer, so K/V still cover every token, but queries, attention,
        // output projection and FFN only run from row t-8: the conv/residual chain makes rows >= t-2 of the result exact.
        let mut trim = [0usize; N_PHYS];
        // Verify needs every row's logits, so it does not take the last-layer tail trim (single span, t > 16).
        if verify_k.is_none() && spans.len() == 1 && self.hgemm && self.tail.get() && t > 16 { trim[N_PHYS - 1] = t - 8; }
        let mut va = VecAlloc { next: 1, n: B_NVEC };
        let mut blocks: Vec<usize> = Vec::new();
        let mut cur = 0usize;
        if verify_k.is_some() { self.verify_record.set(true); }
        for ip in 0..LOOP_START { cur = self.layer_b(&e, spans, &row0, t, dec, ip, ip, cur, &mut blocks, None, &mut va, &[], trim[ip]); }
        let prelude = cur;
        let prelude_blocks = blocks.clone();
        let mut bank = (0usize, 0usize);
        for pass in 0..N_PASS {
            if pass > 0 { cur = prelude; blocks = prelude_blocks.clone(); }
            let msg = if pass > 0 { Some(bank) } else { None };
            let pinned: Vec<usize> = { let mut p = vec![prelude]; if pass > 0 { p.push(bank.0); p.push(bank.1); } p };
            for ip in LOOP_START..LOOP_END { cur = self.layer_b(&e, spans, &row0, t, dec, slot_of(pass, ip), ip, cur, &mut blocks, msg, &mut va, &pinned, 0); }
            bank = (blocks[prelude_blocks.len() + 1], cur);
        }
        for ip in LOOP_END..N_PHYS { cur = self.layer_b(&e, spans, &row0, t, dec, ip, ip, cur, &mut blocks, None, &mut va, &[], trim[ip]); }
        self.verify_record.set(false);
        // Three logit destinations stay as they are. A single-span prefill reads ar.logits. A multi-span prefill
        // reads b.lg, one row per span. Decode writes gemm_h into b.lg with ldo = vocab_pad. Verify is a fourth
        // use of b.lg, for that span only: 1+k rows, one per position, argmax off.
        if let Some(k) = verify_k {
            let ar = self.ar;
            let rows = 1 + k;
            let vp = self.vocab_pad();
            for (si, sp) in spans.iter().enumerate() {
                for r in 0..sp.t {
                    let row = ((row0[si] + r) * E) as u32;
                    let lg = b.lg + ((si * rows + r) * vp) as u32;
                    let mut st: Vec<u32> = blocks.iter().map(|&v| b.vec(v) + row).collect();
                    st.push(b.vec(cur) + row);
                    let j = self.mix_job_off(&st, self.output_res_query as i32, 1, &[ar.fout], &[self.output_norm as i32], 0);
                    self.mix(&e, C_OMIX, &[j]);
                    self.mv(&e, C_LMHEAD, &[Engine::seg(&self.output, ar.fout, lg)]);
                }
            }
        } else if logits && dec {
            // a decode step: every lane's last row through the output mix and one tensor GEMM over the output matrix (read
            // once for all lanes, whatever their number); logits in the batch arena (`span_logits`)
            let ar = self.ar;
            let n = spans.len();
            let st: Vec<u32> = blocks.iter().map(|&v| b.vec(v)).chain(std::iter::once(b.vec(cur))).collect();
            let mut j = self.mix_job_off(&st, self.output_res_query as i32, 1, &[b.fin], &[self.output_norm as i32], E as u32);
            j.hout[0] = b.xh[0] as i32; j.nof32 = 1;
            self.mix_t(&e, C_OMIX, &[j], n);
            let mut hs = Engine::hseg(&self.output, b.xh[0], b.lg);
            hs.ldo = self.vocab_pad() as u32;
            self.gemm_h(&e, C_LMHEAD, &[hs], n);
            if argmax {
                for (si, sp) in spans.iter().enumerate() {
                    let nparts = 128u32;
                    let am = ArgmaxArgs { logits_off: b.lg + (si * self.vocab_pad()) as u32, n: self.n_vocab as u32, part_off: ar.amax, nparts, out_index: (sp.p0 + 1) as u32, pad: [0; 3] };
                    e.buf(&sp.lane.toks, 0, 6);
                    self.go(&e, C_ARGMAX, "k_argmax1", &am, (nparts as usize, 1, 1), 1024);
                    self.go(&e, C_ARGMAX, "k_argmax2", &am, (1, 1, 1), 32);
                }
            }
        } else if logits {
            let ar = self.ar;
            for (si, sp) in spans.iter().enumerate() {
                let last = ((row0[si] + sp.t - 1) * E) as u32;
                let lg = if spans.len() == 1 { ar.logits } else { b.lg + (si * self.vocab_pad()) as u32 };
                let mut st: Vec<u32> = blocks.iter().map(|&v| b.vec(v) + last).collect();
                st.push(b.vec(cur) + last);
                let j = self.mix_job_off(&st, self.output_res_query as i32, 1, &[ar.fout], &[self.output_norm as i32], 0);
                self.mix(&e, C_OMIX, &[j]);
                self.mv(&e, C_LMHEAD, &[Engine::seg(&self.output, ar.fout, lg)]);
                if argmax {
                    let nparts = 128u32;
                    let am = ArgmaxArgs { logits_off: lg, n: self.n_vocab as u32, part_off: ar.amax, nparts, out_index: (sp.p0 + sp.t) as u32, pad: [0; 3] };
                    e.buf(&sp.lane.toks, 0, 6);
                    self.go(&e, C_ARGMAX, "k_argmax1", &am, (nparts as usize, 1, 1), 1024);
                    self.go(&e, C_ARGMAX, "k_argmax2", &am, (1, 1, 1), 32);
                }
            }
        }
        e.end();
    }

    #[allow(clippy::too_many_arguments)]
    fn layer_b(&self, e: &Enc, spans: &[Span], row0: &[usize], t: usize, dec: bool, slot: usize, ip: usize, cur: usize, blocks: &mut Vec<usize>,
               msg: Option<(usize, usize)>, va: &mut VecAlloc, pinned: &[usize], ts: usize) -> usize {
        let l = &self.layers[ip];
        let b = self.ba.get();
        let vo = |i: usize| b.vec(i);
        let ctl = msg.map(|_| &self.loops[ip - LOOP_START]);
        let cs_slot = (slot * NCH * 3) as u32;
        let tt = t as u32;
        let es = E as u32;
        let hatt = self.hgemm;      // fp16 path: k_attn_h, half q / gate, router on the fp16 FFN input
        let (qgh, rth) = (hatt, hatt);
        // the query side (queries, attention, output projection, FFN) covers rows [ts, t); the key side always all rows
        assert!(ts == 0 || (ctl.is_none() && hatt));
        let n = t - ts;
        let nn_ = n as u32;
        let sh = |off: u32, dim: usize| off + (ts * dim) as u32;
        // ---- attention inputs
        let norms: [i32; 3] = if l.swa { [l.attn_norm; 3] } else { [l.q_inp_norm, l.k_inp_norm, l.v_inp_norm] };
        let mut st: Vec<u32> = blocks.iter().map(|&v| vo(v)).collect(); st.push(vo(cur));
        let nq = if l.attn_res_query >= 0 { l.n_attn_q } else { 1 };
        let xin: [u32; 3];
        if let Some(lw) = ctl {
            let (b0, b1) = msg.unwrap();
            let mut ja = self.mix_job_off(&st, l.attn_res_query, nq, &b.inp[..nq as usize], &[], es);
            let mut jm = self.mix_job_off(&[vo(b0), vo(b1)], lw.msg_query as i32, 4, &b.msg, &[], es);
            if self.hgemm {
                // normalized copies straight into the controllers' fp16 inputs [u^ | m^]
                if l.swa { ja.hcopy[0] = [b.cath[0] as i32, b.cath[1] as i32, b.cath[2] as i32]; }
                else { for j in 0..3 { ja.hcopy[j][0] = b.cath[j] as i32; } }
                for j in 0..4 { jm.hcopy[j][0] = (b.cath[j] + (E / 2) as u32) as i32; }
            } else {
                if l.swa { ja.ncopy[0] = [b.cat_u[0] as i32, b.cat_u[1] as i32, b.cat_u[2] as i32]; }
                else { for j in 0..3 { ja.ncopy[j][0] = b.cat_u[j] as i32; } }
                for j in 0..4 { jm.ncopy[j][0] = b.cat_m[j] as i32; }
            }
            self.mix_t(e, C_MIXA, &[ja, jm], t);
            let jobs: Vec<(usize, u32, u32, u32, u32)> = (0..3).map(|j| (j, if l.swa { b.inp[0] } else { b.inp[j] }, b.msg[j], b.cin[j], norms[j] as u32)).collect();
            let ho: Vec<(u32, bool)> = if self.hgemm { (0..3).map(|j| (b.xh[j], true)).collect() } else { vec![] };
            self.ctrl_b(e, lw, &jobs, t, &ho);
            xin = b.cin;
        } else {
            let nn: Vec<i32> = if l.swa { vec![l.attn_norm] } else { norms.to_vec() };
            let mut ja = self.mix_job_off(&st, l.attn_res_query, nq, &b.inp[..nq as usize], &nn, es);
            if self.hgemm { for q in 0..nq as usize { ja.hout[q] = b.xh[q] as i32; } ja.nof32 = 0xF; }
            self.mix_t(e, C_MIXA, &[ja], t);
            xin = if l.swa { [b.inp[0]; 3] } else { [b.inp[0], b.inp[1], b.inp[2]] };
        }
        // ---- projections (one batched GEMM over all segments)
        let mut segs = vec![Engine::gseg(&l.wq, xin[0], b.q), Engine::gseg(&l.wgate, xin[0], b.gate), Engine::gseg(&l.wrel, xin[0], b.rel),
                            Engine::gseg(&l.wsink, xin[0], b.sink), Engine::gseg(&l.wk, xin[1], b.k), Engine::gseg(&l.wv, xin[2], b.v)];
        if let (Some(kp), Some(vp)) = (&l.kconvp, &l.vconvp) { segs.push(Engine::gseg(kp, xin[1], b.kdyn)); segs.push(Engine::gseg(vp, xin[2], b.vdyn)); }
        if self.hgemm {
            // the fp16 inputs b.xh[..] were written by the mix / controller kernels: one shared input in an uncontrolled
            // sliding-window layer, separate q / k / v inputs otherwise
            let xi: [usize; 3] = if ctl.is_some() || !l.swa { [0, 1, 2] } else { [0, 0, 0] };
            let mut hq = vec![Engine::hseg(&l.wq, b.xh[xi[0]], b.q), Engine::hseg(&l.wgate, b.xh[xi[0]], b.gate), Engine::hseg(&l.wrel, b.xh[xi[0]], b.rel),
                              Engine::hseg(&l.wsink, b.xh[xi[0]], b.sink)];
            // q and the output gate are only read once more (K/V prep, attention epilogue): write them as half
            if hatt && qgh && !dec { hq[0].hout = 1; hq[1].hout = 1; }     // a decode step keeps them f32 for k_attn
            let mut hs = vec![Engine::hseg(&l.wk, b.xh[xi[1]], b.k), Engine::hseg(&l.wv, b.xh[xi[2]], b.v)];
            if let (Some(kp), Some(vp)) = (&l.kconvp, &l.vconvp) { hs.push(Engine::hseg(kp, b.xh[xi[1]], b.kdyn)); hs.push(Engine::hseg(vp, b.xh[xi[2]], b.vdyn)); }
            if ts == 0 { hq.extend_from_slice(&hs); self.gemm_h(e, C_QKV, &hq, t); }
            else {
                // query-side projections for the tail rows only (the two GEMMs are independent)
                for sg in hq.iter_mut() { sg.xh_off = sh(sg.xh_off, E / 2); sg.out_off = sh(sg.out_off, sg.nrows as usize / (1 + sg.hout as usize)); }
                self.gemm_h(e, C_QKV, &hs, t);
                self.gemm_h(e, C_QKV, &hq, n);
            }
        } else {
            self.gemm(e, C_QKV, &segs, t);
        }
        // ---- K/V conv + cache write, Q/R prep, attention: span by span, each with its lane's cache and convolution rows.
        // Sliding-window layers: a span attends through a linear scratch [window history | span keys] in the arena (reused by
        // the next span: every dispatch ends with a barrier), and the ring (exactly one window) only receives the last window
        // of keys. Full layers address their cache directly.
        let scap = (EXT + b.cap + 2 * KGUARD) as u32;
        if dec {
            // a decode step: each lane's token through the decode path's fused attention (K/V conv from the lane's state,
            // cache write, split-K attention over the lane's cache, combine + output gate), into qp as f32 rows. The lanes'
            // attention dispatches are independent (own cache, state, rows and split partials): no barrier between them.
            let mut combs: Vec<CombineArgs> = Vec::with_capacity(spans.len());
            for (si, sp) in spans.iter().enumerate() {
                let run = self.on(sp.lane);
                let (pos, r0) = (sp.p0, row0[si]);
                self.bind_lane(e, sp.lane);
                let (kb, vb, kcap) = run.kv_slot(slot);
                let tau = if l.swa { 1.0 } else { 1.0 + self.log_alpha * (((pos + 1) as f32 / self.log_floor).max(1.0)).ln() };
                let ar = self.ar;
                let base = AttnArgs {
                    pos: pos as u32, cap: kcap, k_base: kb, v_base: vb, cs_off: cs_slot,
                    q_off: b.q + (r0 * NH * HD) as u32, k_off: b.k + (r0 * NKV * HD) as u32, v_off: b.v + (r0 * NKV * HD) as u32,
                    r_off: b.rel + (r0 * NH * NREL) as u32, sink_off: b.sink + (r0 * NH) as u32,
                    kdyn_off: if l.swa { -1 } else { (b.kdyn + (r0 * 32) as u32) as i32 }, vdyn_off: if l.swa { -1 } else { (b.vdyn + (r0 * 32) as u32) as i32 },
                    part_off: self.dstart.get() + si as u32 * ar.total + ar.part, kconv_w: l.k_conv, vconv_w: l.v_conv, qclip_w: l.q_clip, relclip_w: l.rel_clip, relbias_w: l.rel_bias, tau,
                    ctr: 0, gate_off: b.gate + (r0 * NH * HD) as u32, out_off: b.qp + (r0 * NH * HD) as u32, gstr: kcap * HD as u32, istr: HD as u32,
                    ..Default::default()
                };
                run.bind_kv(e, l.swa);
                let nkeys = if l.swa { (pos + 1).min(EXT) } else { pos + 1 };
                let kps = Run::split_kps(nkeys);
                let nsplit = nkeys.div_ceil(kps);
                let aa = AttnArgs { nkeys: nkeys as u32, first_pos: (pos + 1 - nkeys) as u32, ring: l.swa as u32, kps: kps as u32, nsplit: nsplit as u32,
                                    pad: self.sep_comb as u32, ..base };
                assert!(self.sep_comb, "a decode step combines the splits in a separate pass");
                self.go_nb(e, C_ATTN, "k_attn", &aa, (nsplit, NKV, 1), 128);
                combs.push(CombineArgs { part_off: aa.part_off, nsplit: nsplit as u32, gate_off: aa.gate_off, out_off: aa.out_off });
            }
            e.barrier();
            for ca in &combs { self.go_nb(e, C_COMB, "k_attn_combine", ca, (NH, 1, 1), 128); }
            e.barrier();
            e.buf(&self.act.borrow(), 0, 3);
            // f32 attention outputs -> the half rows the output projection reads
            self.tohalf(e, C_WO, b.qp, NH * HD, b.agh, NH * HD, NH * HD, t, None);
            e.barrier();
        }
        for (si, sp) in spans.iter().enumerate() {
        if dec { break; }
        let run = self.on(sp.lane);
        let (p0, t, r0) = (sp.p0, sp.t, row0[si]);
        let tt = t as u32;
        // a row-major region shifted to this span's first row
        let rs = |off: u32, dim: usize| off + (r0 * dim) as u32;
        let (n, nn_) = (t - ts, (t - ts) as u32);
        let shs = |off: u32, dim: usize| off + ((r0 + ts) * dim) as u32;
        e.buf(&sp.lane.cs, 0, 4);
        let (kb, vb, kcap) = run.kv_slot(slot);
        let (pk, pv, cap, kpos0) = if l.swa { (b.sk * (4 / self.kvb) as u32, b.sv * (4 / self.kvb) as u32, scap, p0 as i32 - (EXT + KGUARD) as i32) } else { (kb, vb, kcap, 0) };
        let kcopy = |n: usize, pos0: usize, to_ring: u32| {
            let a = KvCopyArgs { n: n as u32, pos0: pos0 as u32, ring_k: kb, ring_v: vb, cap: kcap, sk_off: b.sk, sv_off: b.sv, scap, kpos0, to_ring, pad: [0; 2] };
            e.buf(&sp.lane.kv_swa.borrow(), 0, 3);
            self.go(e, C_KVPREP, "k_kvcopy", &a, (n, NKV, 1), 32);
            e.buf(&self.act.borrow(), 0, 3);
        };
        if l.swa { if p0 > 0 { kcopy(p0.min(EXT), p0 - p0.min(EXT), 0); } e.buf(&self.act.borrow(), 0, 3); } else { e.buf(&sp.lane.kv_full.borrow(), 0, 3); }
        let (qd, gd) = (if hatt && qgh { NH * HD / 2 } else { NH * HD }, if qgh { NH * HD / 2 } else { NH * HD });
        let qpd = if hatt { NH * HD / 2 } else { NH * HD };
        let ka = KvPrepArgs {
            t: tt, p0: p0 as u32, ring: 0, cap, k_base: pk, v_base: pv, cs_off: cs_slot, q_off: rs(b.q, qd),
            k_off: rs(b.k, NKV * HD), v_off: rs(b.v, NKV * HD), r_off: rs(b.rel, NH * NREL),
            kdyn_off: if l.swa { -1 } else { rs(b.kdyn, 32) as i32 }, vdyn_off: if l.swa { -1 } else { rs(b.vdyn, 32) as i32 },
            qp_off: rs(b.qp, qpd), rp_off: rs(b.rp, NH * NREL), qhalf: hatt as u32 | ((hatt && qgh) as u32) << 1, kconv_w: l.k_conv, vconv_w: l.v_conv, qclip_w: l.q_clip, relclip_w: l.rel_clip,
            log_alpha: self.log_alpha, log_floor: self.log_floor, is_full: (!l.swa) as u32, kpos0,
        };
        self.go(e, C_KVPREP, "k_kvprep_b", &ka, (t, NKV, 1), 128);
        if let (false, Some((qc, ncap))) = (l.swa, self.qcap.borrow().as_ref()) {
            assert!(spans.len() == 1 && hatt && ts == 0 && p0 + t <= *ncap, "query capture needs one span, the fp16 attention path, no tail trimming and room");
            let (fi, per) = (self.slot_kind[slot].1, ncap * (NH * HD / 2 + NH));
            e.buf(qc, 0, 3);
            let a = DevCopyArgs { src: b.qp, dst: (fi * per + p0 * (NH * HD / 2)) as u32, n: (t * NH * HD / 2) as u32, pad: 0 };
            self.go(e, C_KVPREP, "k_devcopy", &a, ((t * NH * HD / 2).div_ceil(1024), 1, 1), 1024);
            let a = DevCopyArgs { src: b.sink, dst: (fi * per + ncap * (NH * HD / 2) + p0 * NH) as u32, n: (t * NH) as u32, pad: 0 };
            self.go(e, C_KVPREP, "k_devcopy", &a, ((t * NH).div_ceil(1024), 1, 1), 1024);
            e.buf(&sp.lane.kv_full.borrow(), 0, 3);
        }
        if l.swa { kcopy(t.min(EXT), p0 + t - t.min(EXT), 1); }
        let su = StateArgs { x_off: rs(b.k, NKV * HD), ldx: (NKV * HD) as u32, cs_off: cs_slot, nch: (NKV * HD) as u32, t: tt, pad: [0; 3] };
        self.keep_state(e, si, &su, C_KVPREP, (NKV * HD / 256, 1, 1));
        self.go(e, C_KVPREP, "k_state_upd", &su, (NKV * HD / 256, 1, 1), 256);
        let su = StateArgs { x_off: rs(b.v, NKV * HD), ldx: (NKV * HD) as u32, cs_off: cs_slot + (NKV * HD * 3) as u32, nch: (NKV * HD) as u32, t: tt, pad: [0; 3] };
        self.keep_state(e, si, &su, C_KVPREP, (NKV * HD / 256, 1, 1));
        self.go(e, C_KVPREP, "k_state_upd", &su, (NKV * HD / 256, 1, 1), 256);
        // ---- attention (causal, query-tiled)
        let aa = AttnBArgs { t: tt, p0: p0 as u32, ring: 0, cap, k_base: pk, v_base: pv,
                             window: if l.swa { EXT as u32 } else { 0 }, qp_off: rs(b.qp, qpd), rp_off: rs(b.rp, NH * NREL), sink_off: rs(b.sink, NH), gate_off: rs(b.gate, gd),
                             out_off: rs(b.attn_g, NH * HD), relbias_w: l.rel_bias, ghalf: 0, kpos0, pad: 0 };
        if hatt {
            let ah = AttnBArgs { t: nn_, p0: (p0 + ts) as u32, qp_off: shs(b.qp, NH * HD / 2), rp_off: shs(b.rp, NH * NREL), sink_off: shs(b.sink, NH),
                                 gate_off: shs(b.gate, gd), out_off: shs(b.agh, NH * HD / 2), ghalf: qgh as u32, ..aa };
            self.go(e, C_ATTN, "k_attn_h", &ah, (n.div_ceil(32), NH, 1), 256);
        }
        else if self.tensor { self.go(e, C_ATTN, "k_attn_bt", &aa, (t.div_ceil(32), NH, 1), 256); }
        else { self.go(e, C_ATTN, "k_attn_b", &aa, (t.div_ceil(8), NKV, 1), 128); }
        }
        // ---- output projection + post-conv (+ residual)
        if self.hgemm {
            if !hatt { self.tohalf(e, C_WO, b.attn_g, NH * HD, b.agh, NH * HD, NH * HD, t, None); e.barrier(); }
            self.gemm_h(e, C_WO, &[Engine::hseg(&l.wo, sh(b.agh, NH * HD / 2), sh(b.wo_y, E))], n);
        } else {
            self.gemm(e, C_WO, &[Engine::gseg(&l.wo, b.attn_g, b.wo_y)], t);
        }
        let block_start = ip % BLOCK == 0;
        let mut live: Vec<usize> = blocks.clone(); live.push(cur); live.extend_from_slice(pinned);
        let mid = va.alloc(&live);
        // (tail mode: the first 3 tail rows see the conv state instead of their real predecessors; they are never used)
        // post-conv and its state: span by span (the convolution runs along each lane's own rows, from its own state)
        // (each span's convolution reads its lane's state before the state update overwrites it; spans are independent)
        for (si, sp) in spans.iter().enumerate() {
            let (q0, qn) = (((row0[si] + ts) * E) as u32, sp.t - ts);
            e.buf(&sp.lane.cs, 0, 4);
            let cv = ConvBArgs { y_off: b.wo_y + q0, ldy: es, res_off: vo(cur) + q0, out_off: vo(mid) + q0, cs_off: cs_slot + (2 * NKV * HD * 3) as u32,
                                 convw_off: l.attn_post_conv, has_res: (!block_start) as u32, pad: 0 };
            self.go_nb(e, C_WOPOST, "k_postconv_b", &cv, (E / 256, qn, 1), 256);
        }
        e.barrier();
        for (si, sp) in spans.iter().enumerate() {
            e.buf(&sp.lane.cs, 0, 4);
            let su = StateArgs { x_off: b.wo_y + (row0[si] * E) as u32, ldx: es, cs_off: cs_slot + (2 * NKV * HD * 3) as u32, nch: es, t: sp.t as u32, pad: [0; 3] };
            self.keep_state(e, si, &su, C_WOPOST, (E / 256, 1, 1));
            self.go_nb(e, C_WOPOST, "k_state_upd", &su, (E / 256, 1, 1), 256);
        }
        e.barrier();
        if block_start { blocks.push(cur); }
        // ---- FFN input
        let mut st: Vec<u32> = blocks.iter().map(|&v| sh(vo(v), E)).collect(); st.push(sh(vo(mid), E));
        let x = if let Some(lw) = ctl {
            let mut jf = self.mix_job_off(&st, l.ffn_res_query as i32, 1, &[b.fin], &[], es);
            if self.hgemm { jf.hcopy[0][0] = b.cath[3] as i32; } else { jf.ncopy[0][0] = b.cat_u[3] as i32; }
            self.mix_t(e, C_MIXF, &[jf], t);
            let ho: Vec<(u32, bool)> = if self.hgemm { vec![(b.fh, rth)] } else { vec![] };
            self.ctrl_b(e, lw, &[(3, b.fin, b.msg[3], b.finc, l.ffn_norm)], t, &ho);
            b.finc
        } else {
            let mut jf = self.mix_job_off(&st, l.ffn_res_query as i32, 1, &[sh(b.fin, E)], &[l.ffn_norm as i32], es);
            if self.hgemm { jf.hout[0] = sh(b.fh, E / 2) as i32; jf.nof32 = rth as u32; }
            self.mix_t(e, C_MIXF, &[jf], n);
            b.fin
        };
        // ---- router + top-k + expert grouping
        let rs = GSeg { w_lo: l.router, w_hi: 0, row_bytes: es * 2, wtype: T_BF16, nrows: NEXP as u32, k: es, in_off: sh(x, E), out_off: b.rlog,
                        ldo: NEXP as u32, wsrc: 1, ..Default::default() };
        if rth { self.gemm_h(e, C_ROUTER, &[Engine::hseg_pb(l.router, NEXP as u32, es, sh(b.fh, E / 2), b.rlog)], n); }
        else { self.gemm(e, C_ROUTER, &[rs], n); }
        let ta = TopkBArgs { logits_off: b.rlog, bias_off: l.exp_bias, sel_off: b.sel, t: nn_, wscale: self.wscale, pad: [0; 3] };
        self.go(e, C_TOPK, "k_topk_b", &ta, (n, 1, 1), 32);
        let tile = 32usize;
        if self.hgemm {
            let ra = Route3Args { sel_off: b.sel, t: nn_, perm_off: b.perm, inv_off: b.inv, aw_off: b.aw, ind_off: b.ind, gu_y: (FF / 32) as u32,
                                  dn_y: (E / 64) as u32, tiles_off: b.tl, pad: [0; 2] };
            self.go(e, C_TOPK, "k_moe_route3", &ra, (1, 1, 1), 1024);
        } else {
            let ra = RouteArgs { sel_off: b.sel, t: tt, perm_off: b.perm, inv_off: b.inv, tiles_off: b.tiles, ntiles_off: b.ntiles, tile: tile as u32, pad: 0 };
            self.go(e, C_ROUTER, "k_moe_route", &ra, (1, 1, 1), 1024);
        }
        let max_tiles = (t * TOPK).div_ceil(tile) + NEXP;
        let gu = MoeGemmArgs {
            w_lo: l.gu.off as u32, w_hi: (l.gu.off >> 32) as u32, expert_bytes: l.gu.expert_bytes as u32, row_bytes: l.gu.row_bytes,
            wtype: l.gu.ty, nrows: (2 * FF) as u32, k: es, x_off: x, gather: 1, perm_off: b.perm, tiles_off: b.tiles, ntiles_off: b.ntiles,
            out_off: b.act, ldo: FF as u32, situ: 1, pad: 0, beta: self.situ_beta, lbeta: self.situ_lbeta, pad2: [0.0; 2],
        };
        let mg = if self.tensor { "k_moe_gemm_t" } else { "k_moe_gemm" };
        if self.hgemm {
            // gather the FFN input rows of every (token, expert) assignment into contiguous half rows
            // gather the FFN input rows of every (token, expert) assignment into contiguous half rows
            self.tohalf_x(e, C_GATHER, sh(b.fh, E / 2), E, b.xgh, E, E, n * TOPK, Some(b.perm), true);
            e.barrier();
            // tiles in four widths, sized on the GPU by the routing kernel (indirect dispatch); classes run concurrently
            if self.skip.get() & (1 << C_GU) == 0 {
                for (c, (name, _, tg)) in MOE_CLS.iter().enumerate() {
                    let g = MoeGemmHArgs {
                        w_lo: l.gu.off as u32, w_hi: (l.gu.off >> 32) as u32, expert_bytes: l.gu.expert_bytes as u32, row_bytes: l.gu.row_bytes,
                        wtype: l.gu.ty, nrows: (2 * FF) as u32, k: es, xh_off: b.xgh, tiles_off: b.tl[c], ntiles_off: b.ind + (c * 4) as u32,
                        out_off: b.acth, ldo: FF as u32, situ: 1, aw_off: b.aw, beta: self.situ_beta, lbeta: self.situ_lbeta,
                    };
                    e.pso(&self.gpu.pso(name)); e.bytes(&g, 5);
                    e.dispatch_indirect(&self.act.borrow(), (b.ind as usize + c * 4) * 4, *tg);
                }
                e.barrier();
            }
            if self.skip.get() & (1 << C_DOWN) == 0 {
                for (c, (name, _, tg)) in MOE_CLS.iter().enumerate() {
                    let d = MoeGemmHArgs {
                        w_lo: l.dn.off as u32, w_hi: (l.dn.off >> 32) as u32, expert_bytes: l.dn.expert_bytes as u32, row_bytes: l.dn.row_bytes,
                        wtype: l.dn.ty, nrows: es, k: FF as u32, xh_off: b.acth, tiles_off: b.tl[c], ntiles_off: b.ind + (c * 4) as u32,
                        out_off: b.yd, ldo: es, situ: 0, aw_off: 0, beta: 0.0, lbeta: 0.0,
                    };
                    e.pso(&self.gpu.pso(name)); e.bytes(&d, 5);
                    e.dispatch_indirect(&self.act.borrow(), (b.ind as usize + 4 * NTC + c * 4) * 4, *tg);
                }
                e.barrier();
            }
        } else {
        self.go(e, C_GU, mg, &gu, (FF / 32, max_tiles, 1), 128);
        let dn = MoeGemmArgs {
            w_lo: l.dn.off as u32, w_hi: (l.dn.off >> 32) as u32, expert_bytes: l.dn.expert_bytes as u32, row_bytes: l.dn.row_bytes,
            wtype: l.dn.ty, nrows: es, k: FF as u32, x_off: b.act, gather: 0, perm_off: b.perm, tiles_off: b.tiles, ntiles_off: b.ntiles,
            out_off: b.yd, ldo: es, situ: 0, pad: 0, beta: 0.0, lbeta: 0.0, pad2: [0.0; 2],
        };
        self.go(e, C_DOWN, mg, &dn, (E / 64, max_tiles, 1), 128);
        }
        let ca = MoeCombArgs { yd_off: b.yd, inv_off: b.inv, sel_off: b.sel, out_off: sh(b.ffn_y, E) };
        if self.hgemm { self.go(e, C_COMB2, "k_moe_comb_h", &ca, (n, E.div_ceil(1024), 1), 256); }
        else { self.go(e, C_DOWN, "k_moe_comb_b", &ca, (E / 256, t, 1), 256); }
        let mut live: Vec<usize> = blocks.clone(); live.push(mid); live.extend_from_slice(pinned);
        let out = va.alloc(&live);
        for (si, sp) in spans.iter().enumerate() {
            let (q0, qn) = (((row0[si] + ts) * E) as u32, sp.t - ts);
            e.buf(&sp.lane.cs, 0, 4);
            let cv = ConvBArgs { y_off: b.ffn_y + q0, ldy: es, res_off: vo(mid) + q0, out_off: vo(out) + q0, cs_off: cs_slot + ((2 * NKV * HD + E) * 3) as u32,
                                 convw_off: l.ffn_post_conv, has_res: 1, pad: 0 };
            self.go_nb(e, C_FFNPOST, "k_postconv_b", &cv, (E / 256, qn, 1), 256);
        }
        e.barrier();
        for (si, sp) in spans.iter().enumerate() {
            e.buf(&sp.lane.cs, 0, 4);
            let su = StateArgs { x_off: b.ffn_y + (row0[si] * E) as u32, ldx: es, cs_off: cs_slot + ((2 * NKV * HD + E) * 3) as u32, nch: es, t: sp.t as u32, pad: [0; 3] };
            self.keep_state(e, si, &su, C_FFNPOST, (E / 256, 1, 1));
            self.go_nb(e, C_FFNPOST, "k_state_upd", &su, (E / 256, 1, 1), 256);
        }
        e.barrier();
        out
    }

}

/// The engine working on one lane: every method that reads or writes a stream's state (its K/V cache, convolution
/// rows, tokens, disk tier) lives here; the weights, kernels and scratch arenas are the engine's, reached through Deref.
impl<'a> Run<'a> {
    /// bind everything a kernel may read: the engine's buffers and this lane's cache, convolution rows and tokens
    fn bind(&self, e: &Enc) {
        self.bind_eng(e);
        e.buf(&self.lane.kv_swa.borrow(), 0, 3); e.buf(&self.lane.cs, 0, 4); e.buf(&self.lane.toks, 0, 6);
    }
    /// (k_base, v_base, cap) of a slot's K/V inside its buffer (offsets in cache elements: fp16, or bytes with --kv8)
    fn kv_slot(&self, slot: usize) -> (u32, u32, u32) {
        let (swa, i) = self.slot_kind[slot];
        let cap = if swa { self.lane.swa_cap.get() } else { self.lane.full_cap.get() };
        ((i * 2 * NKV * cap * HD) as u32, ((i * 2 + 1) * NKV * cap * HD) as u32, cap as u32)
    }
    /// bind the K/V buffer a layer's kernels address through buffer 3
    fn bind_kv(&self, e: &Enc, swa: bool) { if swa { e.buf(&self.lane.kv_swa.borrow(), 0, 3); } else { e.buf(&self.lane.kv_full.borrow(), 0, 3); } }
    /// positions the K/V cache can hold right now
    pub fn kv_capacity(&self) -> usize { if self.lane.swa_cap.get() >= EXT { self.lane.full_cap.get() } else { self.lane.swa_cap.get().min(self.lane.full_cap.get()) } }
    /// first position a generation loop must call `gen_reserve` for (with an idle GPU) before it may run that step
    pub fn gen_capacity(&self) -> usize {
        match &self.lane.tier { Some(t) if t.on.get() => t.next.get(), Some(t) => self.kv_capacity().min(t.min_n), None => self.kv_capacity() }
    }
    /// bytes of K/V cache held in memory (with the disk tier: the hot rings and the read staging)
    pub fn kv_bytes(&self) -> usize {
        self.lane.kv_swa.borrow().length() + self.lane.kv_full.borrow().length() + self.lane.tier.as_ref().map_or(0, |t| if t.on.get() { t.stg.borrow().length() } else { 0 })
    }
    /// Make room for `n` positions in the K/V cache (no-op if they fit). Nothing may be in flight on the GPU.
    /// Sliding-window layers keep one window (their ring is linear until the context reaches it, so it can grow like the
    /// full-attention rows); `headroom` adds slack for the tokens about to be generated so growth stays rare.
    pub fn reserve(&self, n: usize, headroom: bool) {
        assert!(n <= self.max_ctx, "context full ({} tokens)", self.max_ctx);
        let grow = |buf: &std::cell::RefCell<Buf>, capc: &std::cell::Cell<usize>, need: usize, limit: usize, nslots: usize| {
            let old = capc.get();
            if need <= old { return; }
            // slack for the tokens about to be generated: INK_HEADROOM=a,b -> need/a (at least b positions)
            let (hd, hm) = std::env::var("INK_HEADROOM").ok().and_then(|v| v.split_once(',').map(|(a, b)| (a.parse::<usize>().unwrap(), b.parse::<usize>().unwrap()))).unwrap_or((16, 0));
            let want = if headroom { need + (need / hd).max(hm) } else { need };
            let cap = (want.div_ceil(64) * 64).min(limit);
            let eb = self.kvb;
            let nb = self.gpu.buffer(nslots * 2 * NKV * cap * HD * eb);
            // every (slot, K|V, group) row block moves to its place in the larger layout
            unsafe {
                let (src, dst) = (buf.borrow().contents().as_ptr() as *const u8, nb.contents().as_ptr() as *mut u8);
                for blk in 0..nslots * 2 * NKV { std::ptr::copy_nonoverlapping(src.add(blk * old * HD * eb), dst.add(blk * cap * HD * eb), old * HD * eb); }
            }
            *buf.borrow_mut() = nb;
            capc.set(cap);
        };
        let n_swa = self.slot_kind.iter().filter(|k| k.0).count();
        grow(&self.lane.kv_swa, &self.lane.swa_cap, n.min(EXT), EXT, n_swa);
        grow(&self.lane.kv_full, &self.lane.full_cap, n, self.max_ctx, N_SLOT - n_swa);
    }
    /// Allocate everything a prefill of `t` tokens ending at context length `n` needs and let the GPU map it now.
    /// The first use of a new buffer costs ~20 ms per GiB; a caller that knows its prompt before it starts the clock
    /// (one-shot runs: while the model loads) pays that here instead of inside the prefill.
    pub fn prepare(&self, n: usize, t: usize) {
        self.reserve(n + 1, true);
        self.ensure_batch(t.min(self.chunk));
        let cb = self.gpu.cmd();
        let e = Enc::new(&cb);
        self.bind(&e);
        // one trivial dispatch per buffer (a window-history copy of key 0 into the scratch guard area) makes the driver wire them
        let b = self.ba.get();
        let a = KvCopyArgs { n: 1, pos0: 0, ring_k: 0, ring_v: 0, cap: 64, sk_off: b.sk, sv_off: b.sv, scap: (EXT + b.cap + 2 * KGUARD) as u32,
                             kpos0: 0, to_ring: 0, pad: [0; 2] };
        e.pso(&self.gpu.pso("k_kvcopy")); e.bytes(&a, 5);
        self.bind_kv(&e, true); e.dispatch((1, 1, 1), 32);
        e.barrier();
        self.bind_kv(&e, false); e.dispatch((1, 1, 1), 32);
        e.end(); cb.commit(); cb.waitUntilCompleted();
    }
    /// bytes of GPU working memory besides the weights: (K/V cache, activations incl. the batch arena if allocated)
    pub fn mem(&self) -> (usize, usize) { (self.kv_bytes(), self.act.borrow().length()) }

    // ======================================================================== disk tier of the K/V cache (--kv-disk)
    pub fn tier_on(&self) -> bool { self.lane.tier.as_ref().is_some_and(|t| t.on.get()) }
    fn kind_bufs(&self, kind: usize) -> (&RefCell<Buf>, &Cell<usize>, usize) {
        let n_swa = self.slot_kind.iter().filter(|k| k.0).count();
        if kind == 0 { (&self.lane.kv_swa, &self.lane.swa_cap, n_swa) } else { (&self.lane.kv_full, &self.lane.full_cap, N_SLOT - n_swa) }
    }
    /// what the cache would hold in memory for `n` positions without the tier, bytes
    pub fn kv_exact_bytes(&self, n: usize) -> usize {
        let n_swa = self.slot_kind.iter().filter(|k| k.0).count();
        (n_swa * n.min(EXT) + (N_SLOT - n_swa) * n) * REC
    }
    /// bytes of the cache that are on disk only (read back for every token)
    pub fn kv_disk_bytes(&self, n: usize) -> usize {
        let Some(t) = &self.lane.tier else { return 0 };
        if !t.on.get() { return 0; }
        let n_swa = self.slot_kind.iter().filter(|k| k.0).count();
        let cold = |kind: usize, w0: usize| t.lo[kind].get().saturating_sub(w0);
        (n_swa * cold(0, n.saturating_sub(EXT)) + (N_SLOT - n_swa) * cold(1, 0)) * REC
    }
    /// (bytes read from the K/V file, time spent reading) since the engine was loaded
    pub fn tier_io(&self) -> (u64, std::time::Duration) { self.lane.tier.as_ref().map_or((0, Default::default()), |t| (t.read_bytes.get(), t.read_time.get())) }
    /// hot ring sizes (positions) and staging bytes for a context of `n` positions: together at most half of what the
    /// cache would take in memory
    fn tier_targets(&self, n: usize) -> (usize, usize, usize) {
        // dev: INK_TIER_HOT=positions forces small hot rings so short tests exercise the file
        if let Some(h) = std::env::var("INK_TIER_HOT").ok().and_then(|v| v.parse::<usize>().ok()) { return (h, h, 2 << 20); }
        let c = self.kv_exact_bytes(n);
        // staging: two halves, each at least one split of the longest attention (see `step`)
        let s = (((c / 16) >> 20).clamp(2, 32) << 20).max(2 * Self::split_kps(n) * REC);
        let hot = (c / 2).saturating_sub(s);
        ((n.min(EXT) * hot / c / 64 * 64).max(64), (n * hot / c / 64 * 64).max(64), s)
    }
    fn tier_foff(&self, kind: usize, i: usize, p: usize) -> u64 {
        let n_swa = self.slot_kind.iter().filter(|k| k.0).count();
        ((if kind == 0 { i * EXT + p % EXT } else { n_swa * EXT + i * self.max_ctx + p }) * REC) as u64
    }
    fn split_target() -> usize { std::env::var("INK_SPLITS").ok().and_then(|v| v.parse().ok()).unwrap_or(8usize) }
    /// keys per split of a decode attention over `nk` keys (the same division with and without the disk tier, so both sum
    /// the same keys in the same order)
    fn split_kps(nk: usize) -> usize { KPS_CHUNK * nk.div_ceil(KPS_CHUNK * Self::split_target()).max(nk.div_ceil(KPS_CHUNK * MAX_SPLIT)) }
    /// file pieces (offset, positions) that hold positions [a, b) of a slot, in position order (a sliding-window slot's
    /// file is a ring of one window)
    fn tier_pieces(&self, kind: usize, i: usize, a: usize, b: usize) -> Vec<(u64, usize)> {
        let mut v = Vec::new();
        let mut p = a;
        while p < b { let e = if kind == 0 { b.min((p / EXT + 1) * EXT) } else { b }; v.push((self.tier_foff(kind, i, p), e - p)); p = e; }
        v
    }
    /// copy one position (16 rows of 128 fp16) between two cache buffers / between a cache buffer and a file record
    unsafe fn rows_copy(src: *const u8, i: usize, cap_s: usize, idx_s: usize, dst: *mut u8, cap_d: usize, idx_d: usize) {
        for r in 0..2 * NKV { std::ptr::copy_nonoverlapping(src.add(((i * 2 * NKV + r) * cap_s + idx_s) * HD * 2), dst.add(((i * 2 * NKV + r) * cap_d + idx_d) * HD * 2), HD * 2); }
    }
    unsafe fn rows_to_rec(src: *const u8, i: usize, cap: usize, idx: usize, rec: *mut u8) {
        for r in 0..2 * NKV { std::ptr::copy_nonoverlapping(src.add(((i * 2 * NKV + r) * cap + idx) * HD * 2), rec.add(r * HD * 2), HD * 2); }
    }
    unsafe fn rec_to_rows(rec: *const u8, dst: *mut u8, i: usize, cap: usize, idx: usize) {
        for r in 0..2 * NKV { std::ptr::copy_nonoverlapping(rec.add(r * HD * 2), dst.add(((i * 2 * NKV + r) * cap + idx) * HD * 2), HD * 2); }
    }
    /// write positions [a, b) of every slot of a kind to the file; `idx(p)` is where position p sits in the kind's buffer now
    fn tier_write(&self, kind: usize, a: usize, b: usize, idx: impl Fn(usize) -> usize) {
        if a >= b { return; }
        let t = self.lane.tier.as_ref().unwrap();
        let (buf, capc, nsl) = self.kind_bufs(kind);
        let (src, cap) = (buf.borrow().contents().as_ptr() as *const u8, capc.get());
        let mut recs = vec![0u8; (b - a).min(2048) * REC];
        for i in 0..nsl {
            let mut p = a;
            while p < b {
                let e = b.min(p + 2048);
                for q in p..e { unsafe { Self::rows_to_rec(src, i, cap, idx(q), recs.as_mut_ptr().add((q - p) * REC)); } }
                let mut o = 0;
                for (off, n) in self.tier_pieces(kind, i, p, e) { t.file.write_all_at(&recs[o..o + n * REC], off).expect("write the K/V file"); o += n * REC; }
                p = e;
            }
        }
    }
    /// read file pieces (offset, bytes, destination) into memory, several at a time (uncached reads; large pieces are split)
    fn tier_read(&self, jobs: &[(u64, usize, *mut u8)]) {
        let t = self.lane.tier.as_ref().unwrap();
        let t0 = std::time::Instant::now();
        let piece: usize = std::env::var("INK_TIER_PIECE").ok().and_then(|v| v.parse::<usize>().ok()).unwrap_or(4) << 20;
        let nthreads: usize = std::env::var("INK_TIER_THREADS").ok().and_then(|v| v.parse().ok()).unwrap_or(4);
        let mut parts: Vec<(u64, usize, usize)> = Vec::new();        // (file offset, bytes, destination address)
        for &(off, bytes, dst) in jobs {
            let mut o = 0;
            while o < bytes { let n = (bytes - o).min(piece); parts.push((off + o as u64, n, dst as usize + o)); o += n; }
        }
        let total: usize = parts.iter().map(|p| p.1).sum();
        let file = &t.file;
        let read = |&(off, n, dst): &(u64, usize, usize)| {
            let sl = unsafe { std::slice::from_raw_parts_mut(dst as *mut u8, n) };
            file.read_exact_at(sl, off).expect("read the K/V file");
        };
        if parts.len() <= 1 { parts.iter().for_each(read); }
        else {
            let next = std::sync::atomic::AtomicUsize::new(0);
            let (parts, next, read) = (&parts, &next, &read);
            std::thread::scope(|sc| {
                for _ in 0..parts.len().min(nthreads) {
                    sc.spawn(move || loop {
                        let k = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                        if k >= parts.len() { break; }
                        read(&parts[k]);
                    });
                }
            });
        }
        t.read_bytes.set(t.read_bytes.get() + total as u64);
        t.read_time.set(t.read_time.get() + t0.elapsed());
    }
    fn tier_wait(&self) {
        let t = self.lane.tier.as_ref().unwrap();
        for p in t.pending.borrow_mut().iter_mut() { if let Some(cb) = p.take() { cb.waitUntilCompleted(); } }
    }
    /// Start keeping only the newest positions in memory: `n` positions are cached, in the normal layout. GPU idle.
    fn tier_enter(&self, n: usize) {
        let t = self.lane.tier.as_ref().unwrap();
        if t.on.get() || n < t.min_n { return; }
        let (hs, hf, s) = self.tier_targets(n);
        for (kind, h) in [(0usize, hs), (1usize, hf)] {
            let (buf, capc, nsl) = self.kind_bufs(kind);
            let old_cap = capc.get();
            let h = h.min(n);
            let (first, lo) = (if kind == 0 { n.saturating_sub(EXT) } else { 0 }, n - h.min(n));
            let idx_old = |p: usize| if kind == 0 { p % old_cap } else { p };
            // the file runs one split ahead of the hot ring's start, so a split boundary always lies where both have the keys
            let cold = (lo + Self::split_kps(n + 1 - first) + 64).min(n);
            self.tier_write(kind, first.max(t.filed[kind].get()), cold, idx_old);
            t.filed[kind].set(cold.max(t.filed[kind].get()));
            let nb = self.gpu.buffer(nsl * 2 * NKV * h * HD * 2);
            unsafe {
                let (src, dst) = (buf.borrow().contents().as_ptr() as *const u8, nb.contents().as_ptr() as *mut u8);
                for i in 0..nsl { for p in lo.max(first)..n { Self::rows_copy(src, i, old_cap, idx_old(p), dst, h, p % h); } }
            }
            *buf.borrow_mut() = nb;
            capc.set(h);
            t.lo[kind].set(lo); t.cold[kind].set(cold);
        }
        *t.stg.borrow_mut() = self.gpu.buffer(s);
        t.half.set(0); t.loaded.set(usize::MAX);
        t.on.set(true);
        t.next.set(n + 64);
    }
    /// Bring the whole cache back into memory in the normal layout (a prefill attends through it directly). GPU idle.
    pub fn tier_leave(&self, n: usize) {
        let Some(t) = &self.lane.tier else { return };
        if !t.on.get() { return; }
        self.tier_wait();
        for kind in 0..2 {
            let (buf, capc, nsl) = self.kind_bufs(kind);
            let (h, lo) = (capc.get(), t.lo[kind].get());
            let first = if kind == 0 { n.saturating_sub(EXT) } else { 0 };
            let cap = if kind == 0 { (n.min(EXT).div_ceil(64) * 64).clamp(KV_CAP0, EXT) } else { (n.div_ceil(64) * 64).clamp(KV_CAP0, self.max_ctx) };
            let idx_new = |p: usize| if kind == 0 { p % cap } else { p };
            let nb = self.gpu.buffer(nsl * 2 * NKV * cap * HD * 2);
            let (src, dst) = (buf.borrow().contents().as_ptr() as *const u8, nb.contents().as_ptr() as *mut u8);
            let mut recs = vec![0u8; 2048 * REC];
            let rp = recs.as_mut_ptr();
            let cold_end = lo.max(first).min(n);
            for i in 0..nsl {
                let mut p = first;
                while p < cold_end {
                    let e = cold_end.min(p + 2048);
                    let mut o = 0;
                    let mut jobs: Vec<(u64, usize, *mut u8)> = Vec::new();
                    for (off, k) in self.tier_pieces(kind, i, p, e) { jobs.push((off, k * REC, unsafe { rp.add(o) })); o += k * REC; }
                    self.tier_read(&jobs);
                    for q in p..e { unsafe { Self::rec_to_rows(rp.add((q - p) * REC), dst, i, cap, idx_new(q)); } }
                    p = e;
                }
                for q in cold_end..n { unsafe { Self::rows_copy(src, i, h, q % h, dst, cap, idx_new(q)); } }
            }
            drop(recs);
            *buf.borrow_mut() = nb;
            capc.set(cap);
        }
        *t.stg.borrow_mut() = self.gpu.buffer(16384);
        t.on.set(false);
    }
    /// forget the cache (a new session)
    fn tier_reset(&self) {
        let Some(t) = &self.lane.tier else { return };
        t.filed[0].set(0); t.filed[1].set(0);
        if !t.on.get() { return; }
        self.tier_wait();
        for kind in 0..2 {
            let (buf, capc, nsl) = self.kind_bufs(kind);
            *buf.borrow_mut() = self.gpu.buffer(nsl * 2 * NKV * KV_CAP0 * HD * 2);
            capc.set(KV_CAP0);
        }
        *t.stg.borrow_mut() = self.gpu.buffer(16384);
        t.on.set(false);
    }
    /// Make room for the step at `pos` (positions 0..pos are cached). Nothing may be in flight on the GPU.
    pub fn gen_reserve(&self, pos: usize) {
        let Some(t) = &self.lane.tier else { self.reserve(pos + 1, true); return };
        if !t.on.get() { self.tier_enter(pos); }
        if !t.on.get() { self.reserve(pos + 1, true); return; }
        // the hot rings follow the context so memory stays at half of the cache
        self.tier_wait();
        let (hs, hf, s) = self.tier_targets(pos + 1);
        for (kind, h) in [(0usize, hs), (1usize, hf)] {
            let (buf, capc, nsl) = self.kind_bufs(kind);
            let cap = capc.get();
            if h < cap + 64 { continue; }
            let nb = self.gpu.buffer(nsl * 2 * NKV * h * HD * 2);
            unsafe {
                let (src, dst) = (buf.borrow().contents().as_ptr() as *const u8, nb.contents().as_ptr() as *mut u8);
                for i in 0..nsl { for p in t.lo[kind].get()..pos { Self::rows_copy(src, i, cap, p % cap, dst, h, p % h); } }
            }
            *buf.borrow_mut() = nb;
            capc.set(h);
        }
        if s > t.stg.borrow().length() { *t.stg.borrow_mut() = self.gpu.buffer(s); t.half.set(0); }
        t.next.set(pos + 64);
    }
    /// One decode step, committed: returns the command buffer whose completion means the step is done.
    pub fn step(&self, pos: usize, logits: bool, argmax: bool) -> CmdBuf {
        let cb = self.gpu.cmd();
        if !self.tier_on() { self.encode_step(&cb, pos, logits, argmax); cb.commit(); return cb; }
        let t = self.lane.tier.as_ref().unwrap();
        // Rows this step overwrites go to the file first - in fact one split earlier than that (and in batches of 16), so the
        // keys around the hot ring's start are in both places. Rows older than 32 steps are final (2 steps are in flight).
        for kind in 0..2 {
            let (_, capc, _) = self.kind_bufs(kind);
            let cap = capc.get();
            let need = (pos + 1).saturating_sub(cap);
            let lo = t.lo[kind].get().max(need);
            let done = pos.saturating_sub(32);
            let want = (lo + Self::split_kps(if kind == 0 { (pos + 1).min(EXT) } else { pos + 1 })).min(done).max(need);
            if t.cold[kind].get() < want {
                let upto = (want + 15).min(done).max(want);
                self.tier_write(kind, t.cold[kind].get().max(t.filed[kind].get()), upto, |p| p % cap);
                t.cold[kind].set(upto);
                t.filed[kind].set(upto.max(t.filed[kind].get()));
            }
            t.lo[kind].set(lo);
        }
        // Plan this step's file reads: per layer execution the splits older than its hot ring, packed into staging halves.
        // The layer's keys are divided into splits exactly as without the tier (same kps from the window start); the file
        // supplies whole splits up to a boundary b that the hot ring also reaches, so every split sums the same keys in the
        // same order as the in-memory path and the results are bit-identical.
        let half_recs = t.stg.borrow().length() / 2 / REC;
        let (mut plan, mut execs) = (Vec::new(), Vec::new());
        let (mut round, mut room) = (0usize, half_recs);
        let mut order: Vec<usize> = (0..LOOP_START).collect();
        for pass in 0..N_PASS { for ip in LOOP_START..LOOP_END { order.push(slot_of(pass, ip)); } }
        order.extend(LOOP_END..N_PHYS);
        for (exec, &slot) in order.iter().enumerate() {
            let (swa, i) = self.slot_kind[slot];
            let kind = if swa { 0 } else { 1 };
            let w0 = if swa { (pos + 1).saturating_sub(EXT) } else { 0 };
            let lo = t.lo[kind].get().max(w0);
            let nk = pos + 1 - w0;
            let kps = Self::split_kps(nk);
            let b = w0 + (lo - w0).div_ceil(kps) * kps;
            if b <= pos && b <= t.cold[kind].get().max(w0) && kps <= half_recs {
                let (mut c, mut sp) = (w0, 0usize);
                while c < b {
                    if room < kps { round += 1; room = half_recs; }
                    let ns = (room / kps).min((b - c) / kps);
                    plan.push(Chunk { exec, kind, i, c0: c, c1: c + ns * kps, round, off: (half_recs - room) * REC, sp0: sp, ns, kps });
                    c += ns * kps; sp += ns; room -= ns * kps;
                }
                let nsplit = nk.div_ceil(kps);
                execs.push(ExecPlan { b, kps, nsplit, sp0: sp, ns: nsplit - sp });
            } else {
                // (tiny hot rings in tests) no common boundary: divide the file part and the hot part separately - the same
                // keys, summed in a different grouping
                if lo > w0 && !t.unaligned.replace(true) { eprintln!("[inky] --kv-disk: hot ring shorter than a split, sums are grouped differently from the in-memory path"); }
                let spl = |nk: usize| { let kps = KPS_CHUNK * nk.div_ceil(KPS_CHUNK * Self::split_target()); (kps, nk.div_ceil(kps)) };
                let (mut c, mut sp) = (w0, 0usize);
                while c < lo {
                    if room < (lo - c).min(64) { round += 1; room = half_recs; }
                    let n = room.min(lo - c);
                    let (kps, ns) = spl(n);
                    plan.push(Chunk { exec, kind, i, c0: c, c1: c + n, round, off: (half_recs - room) * REC, sp0: sp, ns, kps });
                    c += n; sp += ns; room -= n;
                }
                let (kps, ns) = spl(pos + 1 - lo);
                assert!(sp + ns <= MAX_SPLIT, "too many K/V file chunks for one layer");
                execs.push(ExecPlan { b: lo, kps, nsplit: sp + ns, sp0: sp, ns });
            }
        }
        *t.execs.borrow_mut() = execs;
        *t.plan.borrow_mut() = plan;
        t.loaded.set(usize::MAX);
        let mut cx = Cx { e: Enc::new(&cb), cb: Some(cb) };
        self.encode_step_cx(&mut cx, pos, logits, argmax);
        let Cx { e, cb } = cx;
        e.end();
        let cb = cb.unwrap();
        cb.commit();
        // the next step starts in the other staging half
        let h = t.half.get();
        t.pending.borrow_mut()[h] = Some(cb.clone());
        t.half.set(h ^ 1);
        cb
    }
    /// make the staging round of chunk `k` of the plan available: switch halves if it is a new one and read it from the file
    fn tier_load(&self, cx: &mut Cx, k: usize) {
        let t = self.lane.tier.as_ref().unwrap();
        let plan = t.plan.borrow();
        let round = plan[k].round;
        if t.loaded.get() == round { return; }
        if round > 0 {
            // hand the commands written so far to the GPU; they keep reading the half we are leaving
            let ncb = self.gpu.cmd();
            let oe = std::mem::replace(&mut cx.e, Enc::new(&ncb));
            oe.end();
            let ocb = cx.cb.replace(ncb).unwrap();
            ocb.commit();
            let h = t.half.get();
            t.pending.borrow_mut()[h] = Some(ocb);
            t.half.set(h ^ 1);
            self.bind(&cx.e);
        }
        // whatever last used this half must be finished before it is overwritten
        let h = t.half.get();
        let prev = t.pending.borrow_mut()[h].take();
        if let Some(cb) = prev { cb.waitUntilCompleted(); }
        let stg = t.stg.borrow();
        let base = unsafe { (stg.contents().as_ptr() as *mut u8).add(h * (stg.length() / 2)) };
        let mut jobs = Vec::new();
        for c in plan.iter().filter(|c| c.round == round) {
            let mut o = c.off;
            for (off, n) in self.tier_pieces(c.kind, c.i, c.c0, c.c1) { jobs.push((off, n * REC, unsafe { base.add(o) })); o += n * REC; }
        }
        self.tier_read(&jobs);
        t.loaded.set(round);
    }

    /// conv state and sliding-window rings, to rerun the same decode steps
    pub fn snapshot(&self) -> (Vec<u8>, Vec<u8>) {
        let g = |b: &Buf| unsafe { std::slice::from_raw_parts(b.contents().as_ptr() as *const u8, b.length()).to_vec() };
        (g(&self.lane.cs), g(&self.lane.kv_swa.borrow()))
    }
    pub fn restore(&self, s: &(Vec<u8>, Vec<u8>)) {
        unsafe {
            std::ptr::copy_nonoverlapping(s.0.as_ptr(), self.lane.cs.contents().as_ptr() as *mut u8, s.0.len());
            std::ptr::copy_nonoverlapping(s.1.as_ptr(), self.lane.kv_swa.borrow().contents().as_ptr() as *mut u8, s.1.len());
        }
    }
    /// Score every far key (older than one window at context `n`) of the full-attention slots by the attention it received
    /// from a sample of the prompt's own queries, keep the best `keep` share per (slot, group) and optionally one summary
    /// key (mean of the evicted keys and values, score bias ln(count)). Returns (far keys, kept per group).
    pub fn prune_build(&self, n: usize, keep: f64, use_sum: bool, nq: usize, mode: &str) -> (usize, usize) {
        assert!(!self.kv8 && !self.tier_on() && n > EXT);
        let qc = self.qcap.borrow();
        let (qbuf, ncap) = qc.as_ref().expect("qcap_alloc + prefill first");
        let r0 = n - EXT;
        let f = ((r0 as f64 * keep).round() as usize).min(r0);
        let rows = f + 1;
        let nf = self.slot_kind.iter().filter(|k| !k.0).count();
        let bag = self.gpu.buffer(nf * 2 * NKV * rows * HD * 2);
        let lut: Vec<f32> = (0..65536u32).map(|h| { let sg = if h & 0x8000 != 0 { -1.0f32 } else { 1.0 }; let e = ((h >> 10) & 31) as i32; let m = (h & 1023) as f32;
            if e == 0 { sg * m * 2f32.powi(-24) } else if e == 31 { 0.0 } else { sg * (1.0 + m / 1024.0) * 2f32.powi(e - 15) } }).collect();
        let f2h = |v: f32| -> u16 { let bits = v.to_bits(); let sign = ((bits >> 16) & 0x8000) as u16; let exp = ((bits >> 23) & 0xff) as i32 - 127 + 15;
            if v == 0.0 || exp <= 0 { sign } else { let mant = (bits >> 13) & 0x3ff; let rem = bits & 0x1fff;
                let mut h = ((exp as u32) << 10 | mant) + if rem > 0x1000 || (rem == 0x1000 && mant & 1 == 1) { 1 } else { 0 }; if h >= 0x7c00 { h = 0x7bff; } sign | h as u16 } };
        let cap = self.lane.full_cap.get();
        let (kvp, qp, bp) = (self.lane.kv_full.borrow().contents().as_ptr() as usize, qbuf.contents().as_ptr() as usize, bag.contents().as_ptr() as usize);
        let per = ncap * (NH * HD / 2 + NH) * 4;
        // sampled query positions: spread over the context, denser at the end
        let mut qpos: Vec<usize> = (0..nq).map(|i| (n - 1) - i * (n - 1) / nq.max(1)).collect();
        qpos.dedup();
        let tasks: Vec<(usize, usize)> = (0..nf).flat_map(|fi| (0..NKV).map(move |g| (fi, g))).collect();
        let next = std::sync::atomic::AtomicUsize::new(0);
        let (lut, qpos, tasks, f2h) = (&lut, &qpos, &tasks, &f2h);
        std::thread::scope(|sc| {
            for _ in 0..8 { sc.spawn(|| loop {
                let k = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                if k >= tasks.len() { break; }
                let (fi, g) = tasks[k];
                let row = |kv: usize, j: usize| unsafe { std::slice::from_raw_parts((kvp as *const u16).add(((fi * 2 + kv) * NKV + g) * cap * HD + j * HD), HD) };
                let keys: Vec<f32> = (0..n).flat_map(|j| row(0, j).iter().map(|&h| lut[h as usize])).collect();
                let mut imp = vec![0f32; n];
                let mut sc_ = vec![0f32; n];
                for &t in qpos.iter() { for hh in 0..3 {
                    let h = g * 3 + hh;
                    let q: Vec<f32> = unsafe { std::slice::from_raw_parts((qp + fi * per) as *const u16, ncap * NH * HD) }[(t * NH + h) * HD..(t * NH + h + 1) * HD].iter().map(|&x| lut[x as usize]).collect();
                    let sink = unsafe { *((qp + fi * per + ncap * NH * HD * 2) as *const f32).add(t * NH + h) };
                    let mut m = sink;
                    for j in 0..=t {
                        let kr = &keys[j * HD..(j + 1) * HD];
                        let mut a = [0f32; 8];
                        for c in (0..HD).step_by(8) { for u in 0..8 { a[u] += q[c + u] * kr[c + u]; } }
                        let s = a.iter().sum::<f32>();
                        sc_[j] = s; if s > m { m = s; }
                    }
                    let mut z = (sink - m).exp();
                    for j in 0..=t { sc_[j] = (sc_[j] - m).exp(); z += sc_[j]; }
                    match mode {
                        "max" => for j in 0..=t { let p = sc_[j] / z; if p > imp[j] { imp[j] = p; } },
                        // only what a key received while it was inside one window of the query / only from further away
                        "near" => for j in t.saturating_sub(EXT - 1)..=t { imp[j] += sc_[j] / z; },
                        "far" => for j in 0..=t.saturating_sub(EXT) { imp[j] += sc_[j] / z; },
                        _ => for j in 0..=t { imp[j] += sc_[j] / z; },
                    }
                } }
                if mode == "rand" { for (j, x) in imp.iter_mut().enumerate() { *x = ((j as u64 * 0x9E3779B97F4A7C15 + (k as u64) * 0xD1B54A32D192ED03) >> 40) as f32; } }
                let mut order: Vec<usize> = (0..r0).collect();
                order.sort_by(|&a, &b| imp[b].partial_cmp(&imp[a]).unwrap());
                let mut kept: Vec<usize> = order[..f].to_vec();
                kept.sort();
                let out = |kv: usize, r: usize| unsafe { std::slice::from_raw_parts_mut((bp as *mut u16).add(((fi * 2 + kv) * NKV + g) * rows * HD + r * HD), HD) };
                for kv in 0..2 {
                    for (r, &j) in kept.iter().enumerate() { out(kv, r).copy_from_slice(row(kv, j)); }
                    let mut mean = vec![0f64; HD];
                    for &j in &order[f..] { for (mm, &h) in mean.iter_mut().zip(row(kv, j)) { *mm += lut[h as usize] as f64; } }
                    let cnt = (r0 - f).max(1) as f64;
                    for (o, mm) in out(kv, f).iter_mut().zip(&mean) { *o = f2h((*mm / cnt) as f32); }
                }
            }); }
        });
        drop(qc);
        *self.lane.prune.borrow_mut() = Some(Prune { bag, rows, r0, lastbias: ((r0 - f).max(1) as f32).ln(), use_sum: use_sum && r0 > f });
        (r0, f)
    }
    /// dev: how peaked is full attention? After a prefill of `n` tokens (tail mode off) the arena still holds the last layer's
    /// prepared queries. For the last `nq` query tokens and every head: how many of its keys score within `margin` logits of
    /// the best one (keys further down contribute less than e^-margin of it). Relative bias is ignored (it only exists within
    /// 1024 tokens). Returns per margin the mean fraction of keys that are within it, and the worst head's fraction.
    pub fn kv_screen(&self, n: usize, nq: usize, margins: &[f32]) -> Vec<(f32, f64, f64)> {
        assert!(!self.kv8, "this dev tool reads the fp16 cache");
        let b = self.ba.get();
        let slot = N_PHYS - 1;
        assert!(!self.slot_kind[slot].0, "last layer is expected to be a full-attention layer");
        let (kb, _, cap) = self.kv_slot(slot);
        let h2f = |h: u16| -> f32 { let sg = if h & 0x8000 != 0 { -1.0f32 } else { 1.0 }; let e = ((h >> 10) & 31) as i32; let m = (h & 1023) as f32;
            if e == 0 { sg * m * 2f32.powi(-24) } else { sg * (1.0 + m / 1024.0) * 2f32.powi(e - 15) } };
        let kv = self.lane.kv_full.borrow();
        let kp = kv.contents().as_ptr() as *const u16;
        let act = self.act.borrow();
        let qp = unsafe { (act.contents().as_ptr() as *const u16).add(b.qp as usize * 2) };
        let sink = unsafe { (act.contents().as_ptr() as *const f32).add(b.sink as usize) };
        let mut within = vec![(0.0f64, 0.0f64); margins.len()];
        let mut cnt = 0usize;
        for g in 0..NKV {
            // keys of this group as f32
            let keys: Vec<f32> = (0..n * HD).map(|i| h2f(unsafe { *kp.add(kb as usize + g * cap as usize * HD + i) })).collect();
            for t in n - nq..n { for hh in 0..3 {
                let h = g * 3 + hh;
                let q: Vec<f32> = (0..HD).map(|d| h2f(unsafe { *qp.add((t * NH + h) * HD + d) })).collect();
                let sc: Vec<f32> = (0..=t).map(|j| { let k = &keys[j * HD..(j + 1) * HD]; q.iter().zip(k).map(|(a, c)| a * c).sum::<f32>() }).collect();
                let m = sc.iter().cloned().fold(unsafe { *sink.add(t * NH + h) }, f32::max);
                for (i, &mg) in margins.iter().enumerate() {
                    let f = sc.iter().filter(|&&x| x > m - mg).count() as f64 / sc.len() as f64;
                    within[i].0 += f; within[i].1 = within[i].1.max(f);
                }
                cnt += 1;
            } }
        }
        margins.iter().zip(within).map(|(&m, (s, w))| (m, s / cnt as f64, w)).collect()
    }
    /// dev: what would storing the cache at `bits` per value cost in accuracy? Rounds the keys and values of positions
    /// [from, to) in place to a per-vector linear grid (the storage stays fp16; nothing gets smaller). kbits / vbits = 0: untouched.
    pub fn kv_quant_sim(&self, from: usize, to: usize, kbits: u32, vbits: u32) {
        assert!(!self.kv8, "this dev tool reads the fp16 cache");
        let h2f = |h: u16| -> f32 { let sg = if h & 0x8000 != 0 { -1.0f32 } else { 1.0 }; let e = ((h >> 10) & 31) as i32; let m = (h & 1023) as f32;
            if e == 0 { sg * m * 2f32.powi(-24) } else { sg * (1.0 + m / 1024.0) * 2f32.powi(e - 15) } };
        let f2h = |v: f32| -> u16 { let bits = v.to_bits(); let sign = ((bits >> 16) & 0x8000) as u16; let exp = ((bits >> 23) & 0xff) as i32 - 127 + 15;
            if v == 0.0 || exp <= 0 { sign } else {
                let mant = (bits >> 13) & 0x3ff; let rem = bits & 0x1fff;
                let mut h = ((exp as u32) << 10 | mant) + if rem > 0x1000 || (rem == 0x1000 && mant & 1 == 1) { 1 } else { 0 };
                if h >= 0x7c00 { h = 0x7bff; }
                sign | h as u16 } };
        for slot in 0..N_SLOT {
            let (is_swa, _) = self.slot_kind[slot];
            let (kb, vb, cap) = self.kv_slot(slot);
            let buf = if is_swa { self.lane.kv_swa.borrow() } else { self.lane.kv_full.borrow() };
            let base = buf.contents().as_ptr() as *mut u16;
            for (b, bits) in [(kb, kbits), (vb, vbits)] {
                if bits == 0 { continue; }
                let lv = ((1u32 << (bits - 1)) - 1) as f32;
                for g in 0..NKV { for pos in from.max(if is_swa { to.saturating_sub(EXT) } else { 0 })..to {
                    let idx = if is_swa { pos % cap as usize } else { pos };
                    let row = unsafe { std::slice::from_raw_parts_mut(base.add(b as usize + (g * cap as usize + idx) * HD), HD) };
                    let v: Vec<f32> = row.iter().map(|&h| h2f(h)).collect();
                    if bits >= 100 {
                        // floating formats: 100 + m = round every value to m mantissa bits; 200 + m = the same with a 3-bit exponent
                        // (binades [0.25, 32), fixed step 2^-(m+2) below)
                        let (m, lim) = ((bits % 100) as i32, bits >= 200);
                        for (r, &x) in row.iter_mut().zip(&v) {
                            if x == 0.0 { continue; }
                            let mut e = x.abs().log2().floor() as i32;
                            if lim { e = e.clamp(-2, 4); }
                            let q = 2f32.powi(e - m);
                            let mut y = (x / q).round() * q;
                            if lim { y = y.clamp(-(2f32.powi(5) - q), 2f32.powi(5) - q); }
                            *r = f2h(y);
                        }
                        continue;
                    }
                    let mx = v.iter().fold(0f32, |a, x| a.max(x.abs()));
                    if mx == 0.0 { continue; }
                    for (r, x) in row.iter_mut().zip(&v) { *r = f2h((x / mx * lv).round() * mx / lv); }
                } }
            }
        }
    }
    /// dev: raw bytes of the K/V cache holding real keys after `n` positions: (sliding-window rings, full-attention rows)
    pub fn kv_dump(&self, n: usize) -> (Vec<u8>, Vec<u8>) {
        assert!(!self.kv8, "this dev tool reads the fp16 cache");
        let (mut swa, mut full) = (Vec::new(), Vec::new());
        for slot in 0..N_SLOT {
            let (is_swa, _) = self.slot_kind[slot];
            let (kb, vb, cap) = self.kv_slot(slot);
            let buf = if is_swa { self.lane.kv_swa.borrow() } else { self.lane.kv_full.borrow() };
            let base = buf.contents().as_ptr() as *const u8;
            let rows = if is_swa { n.min(EXT) } else { n };
            for b in [kb, vb] { for g in 0..NKV {
                let sl = unsafe { std::slice::from_raw_parts(base.add((b as usize + g * cap as usize * HD) * 2), rows * HD * 2) };
                if is_swa { swa.extend_from_slice(sl); } else { full.extend_from_slice(sl); }
            } }
        }
        (swa, full)
    }

    /// a copy of this lane's convolution rows (its recurrent state at the current position)
    pub fn get_cs(&self) -> Vec<u8> { unsafe { std::slice::from_raw_parts(self.lane.cs.contents().as_ptr() as *const u8, self.lane.cs.length()).to_vec() } }
    /// put back convolution rows `get_cs` copied. Nothing may be in flight on the GPU.
    pub fn set_cs(&self, v: &[u8]) { assert_eq!(v.len(), self.lane.cs.length()); unsafe { std::ptr::copy_nonoverlapping(v.as_ptr(), self.lane.cs.contents().as_ptr() as *mut u8, v.len()); } }
    pub fn reset(&self) {
        self.tier_reset();
        unsafe { std::ptr::write_bytes(self.lane.cs.contents().as_ptr() as *mut u8, 0, self.lane.cs.length()); }
    }

    pub fn set_tokens(&self, start: usize, toks: &[u32]) {
        let p = self.lane.toks.contents().as_ptr() as *mut i32;
        for (i, &t) in toks.iter().enumerate() { unsafe { *p.add(start + i) = t as i32; } }
    }
    pub fn token_at(&self, i: usize) -> u32 { unsafe { *(self.lane.toks.contents().as_ptr() as *const i32).add(i) as u32 } }
    /// Encode one full decode step for token at `pos` (input token read from toks[pos]).
    /// If `logits` is set, also computes lm_head and writes argmax to toks[pos+1].
    pub fn encode_step(&self, cb: &CmdBuf, pos: usize, logits: bool, argmax: bool) {
        assert!(!self.tier_on(), "with the disk tier a step spans several command buffers: use step()");
        assert!(pos < self.kv_capacity(), "K/V capacity: call reserve() first");
        let mut cx = Cx { e: Enc::new(cb), cb: None };
        self.encode_step_cx(&mut cx, pos, logits, argmax);
        cx.e.end();
    }
    fn encode_step_cx(&self, cx: &mut Cx, pos: usize, logits: bool, argmax: bool) {
        let e = &cx.e;
        self.bind(e);
        let ar = self.ar_at();
        // embedding -> vec 0 (x0)
        let ea = EmbedArgs { emb_lo: self.tok_embd.off as u32, emb_hi: (self.tok_embd.off >> 32) as u32, row_bytes: self.tok_embd.row_bytes,
                             norm_off: self.tok_norm, out0: self.v(0), out1: self.v(0), tok_index: pos as u32, eps: self.eps };
        self.go(e, C_EMBED, "k_embed", &ea, (1, 1, 1), 256);

        self.done.set(0);
        let mut va = VecAlloc { next: 1, n: N_VEC };
        let mut blocks: Vec<usize> = Vec::new();
        let mut cur = 0usize;
        for ip in 0..LOOP_START { cur = self.layer(cx, pos, ip, ip, cur, &mut blocks, None, &mut va, &[]); }
        let prelude = cur;
        let prelude_blocks = blocks.clone();
        let mut bank = (0usize, 0usize);
        for pass in 0..N_PASS {
            if pass > 0 { cur = prelude; blocks = prelude_blocks.clone(); }
            let msg = if pass > 0 { Some(bank) } else { None };
            let pinned: Vec<usize> = { let mut p = vec![prelude]; if pass > 0 { p.push(bank.0); p.push(bank.1); } p };
            for ip in LOOP_START..LOOP_END {
                cur = self.layer(cx, pos, slot_of(pass, ip), ip, cur, &mut blocks, msg, &mut va, &pinned);
            }
            bank = (*blocks.get(prelude_blocks.len() + 1).unwrap_or(&0), cur);
        }
        for ip in LOOP_END..N_PHYS { cur = self.layer(cx, pos, ip, ip, cur, &mut blocks, None, &mut va, &[]); }

        let e = &cx.e;
        if logits {
            let mut st = blocks.clone(); st.push(cur);
            let j = self.mix_job(&st, self.output_res_query as i32, 1, &[ar.fout], &[self.output_norm as i32]);
            self.mix(e, C_OMIX, &[j]);
            self.mv(e, C_LMHEAD, &[Engine::seg(&self.output, ar.fout, ar.logits)]);
        }
        if logits && argmax {
            let nparts = 128u32;
            let am = ArgmaxArgs { logits_off: ar.logits, n: self.n_vocab as u32, part_off: ar.amax, nparts, out_index: pos as u32 + 1, pad: [0; 3] };
            self.go(e, C_ARGMAX, "k_argmax1", &am, (nparts as usize, 1, 1), 1024);
            self.go(e, C_ARGMAX, "k_argmax2", &am, (1, 1, 1), 32);
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn layer(&self, cx: &mut Cx, pos: usize, slot: usize, ip: usize, cur: usize, blocks: &mut Vec<usize>,
             msg: Option<(usize, usize)>, va: &mut VecAlloc, pinned: &[usize]) -> usize {
        if self.done.get() >= self.stop.get() { return cur; }
        self.done.set(self.done.get() + 1);
        let e = &cx.e;
        let l = &self.layers[ip];
        let ar = self.ar_at();
        let ctl = msg.map(|_| &self.loops[ip - LOOP_START]);
        let cs_slot = (slot * NCH * 3) as u32;

        // ---- attention inputs: depth mix (+ loop control) -> normalized q/k/v inputs
        let norms: [i32; 3] = if l.swa { [l.attn_norm; 3] } else { [l.q_inp_norm, l.k_inp_norm, l.v_inp_norm] };
        let mut st = blocks.clone(); st.push(cur);
        let nq = if l.attn_res_query >= 0 { l.n_attn_q } else { 1 };
        let xin: [u32; 3];
        if let Some(lw) = ctl {
            let (b0, b1) = msg.unwrap();
            // raw mixed inputs u_j -> inp[j]; RMSNorm'd copies -> cat[j][0:E]; messages m_j -> msg[j], normalized -> cat[j][E:2E]
            let mut ja = self.mix_job(&st, l.attn_res_query, nq, &ar.inp[..nq as usize], &[]);
            if l.swa { ja.ncopy[0] = [ar.cat[0] as i32, ar.cat[1] as i32, ar.cat[2] as i32]; }
            else { for j in 0..3 { ja.ncopy[j][0] = ar.cat[j] as i32; } }
            let mut jm = self.mix_job(&[b0, b1], lw.msg_query as i32, 4, &ar.msg, &[]);
            for j in 0..4 { jm.ncopy[j][0] = (ar.cat[j] + E as u32) as i32; }
            self.mix(e, C_MIXA, &[ja, jm]);
            let mut cj = Vec::new();
            for j in 0..3 {
                let u = if l.swa { ar.inp[0] } else { ar.inp[j] };
                cj.push(CtrlJob { u_off: u, m_off: ar.msg[j], d_off: lw.down[j], out_off: ar.ctrlg[j], gain_off: lw.gain[j],
                                  forget_off: lw.forget[j], res_out: ar.cin[j], ss_off: ar.ssp[j], norm_w: norms[j], ctr: 8 + j as u32 });
            }
            self.ctrl_dec(e, &cj);
            xin = ar.cin;
        } else {
            let nn: Vec<i32> = if l.swa { vec![l.attn_norm] } else { norms.to_vec() };
            self.mix(e, C_MIXA, &[self.mix_job(&st, l.attn_res_query, nq, &ar.inp[..nq as usize], &nn)]);
            xin = if l.swa { [ar.inp[0]; 3] } else { [ar.inp[0], ar.inp[1], ar.inp[2]] };
        }
        // ---- fused projections (one dispatch)
        let mut segs = vec![Engine::seg(&l.wq, xin[0], ar.q), Engine::seg(&l.wgate, xin[0], ar.gate), Engine::seg(&l.wrel, xin[0], ar.rel),
                            Engine::seg(&l.wsink, xin[0], ar.sink), Engine::seg(&l.wk, xin[1], ar.k), Engine::seg(&l.wv, xin[2], ar.v)];
        if let (Some(kp), Some(vp)) = (&l.kconvp, &l.vconvp) { segs.push(Engine::seg(kp, xin[1], ar.kdyn)); segs.push(Engine::seg(vp, xin[2], ar.vdyn)); }
        self.mv(e, C_QKV, &segs);
        // ---- attention (split-K) with the split combine + output gate fused into the last split per kv group
        let (kb, vb, kcap) = self.kv_slot(slot);
        let tau = if l.swa { 1.0 } else { 1.0 + self.log_alpha * (((pos + 1) as f32 / self.log_floor).max(1.0)).ln() };
        let base = AttnArgs {
            pos: pos as u32, cap: kcap, k_base: kb, v_base: vb, cs_off: cs_slot,
            q_off: ar.q, k_off: ar.k, v_off: ar.v, r_off: ar.rel, sink_off: ar.sink,
            kdyn_off: if l.swa { -1 } else { ar.kdyn as i32 }, vdyn_off: if l.swa { -1 } else { ar.vdyn as i32 }, part_off: ar.part,
            kconv_w: l.k_conv, vconv_w: l.v_conv, qclip_w: l.q_clip, relclip_w: l.rel_clip, relbias_w: l.rel_bias, tau,
            ctr: 0, gate_off: ar.gate, out_off: ar.attn_g, gstr: kcap * HD as u32, istr: HD as u32, ..Default::default()
        };
        let nsplit;
        if self.tier_on() {
            // disk tier: the keys older than the hot ring come from the file (staged in chunks, each its own dispatch writing
            // its share of the split partials); the hot ring is one more dispatch and also stores this token's K/V
            let t = self.lane.tier.as_ref().unwrap();
            let exec = self.done.get() - 1;
            let x = t.execs.borrow()[exec];
            let chunks: Vec<(usize, Chunk)> = t.plan.borrow().iter().cloned().enumerate().filter(|(_, c)| c.exec == exec).collect();
            for (k, c) in chunks {
                self.tier_load(cx, k);
                let e = &cx.e;
                let off = ((t.half.get() * (t.stg.borrow().length() / 2) + c.off) / 2) as u32;       // in fp16 elements
                let a = AttnArgs { nkeys: (c.c1 - c.c0) as u32, first_pos: c.c0 as u32, ring: 0, idx0: c.c0 as u32, k_base: off, v_base: off + (NKV * HD) as u32,
                                   gstr: HD as u32, istr: (2 * NKV * HD) as u32, kps: c.kps as u32, nsplit: x.nsplit as u32, sp0: c.sp0 as u32, noown: 1, pad: 1, ..base };
                e.buf(&t.stg.borrow(), 0, 3);
                self.go(e, C_ATTN, "k_attn", &a, (c.ns, NKV, 1), 128);
            }
            let e = &cx.e;
            let a = AttnArgs { nkeys: (pos + 1 - x.b) as u32, first_pos: x.b as u32, ring: 1, kps: x.kps as u32, nsplit: x.nsplit as u32, sp0: x.sp0 as u32, pad: 1, ..base };
            self.bind_kv(e, l.swa);
            self.go(e, C_ATTN, "k_attn", &a, (x.ns, NKV, 1), 128);
            let total = x.nsplit;
            nsplit = total;
        } else if let (false, Some(pr)) = (l.swa, self.lane.prune.borrow().as_ref()) {
            // dev: far keys from the selection, recent keys from the cache
            let fi = self.slot_kind[slot].1;
            let spl = |nk: usize| { let kps = KPS_CHUNK * nk.div_ceil(KPS_CHUNK * Self::split_target()); (kps, nk.div_ceil(kps)) };
            let nb = if pr.use_sum { pr.rows } else { pr.rows - 1 };
            let ((k1, n1), (k2, n2)) = (if nb > 0 { spl(nb) } else { (32, 0) }, spl(pos + 1 - pr.r0));
            if nb > 0 {
                let kb2 = (fi * 2 * NKV * pr.rows * HD) as u32;
                let a = AttnArgs { nkeys: nb as u32, first_pos: 0, ring: 0, cap: pr.rows as u32, k_base: kb2, v_base: kb2 + (NKV * pr.rows * HD) as u32,
                                   gstr: (pr.rows * HD) as u32, kps: k1 as u32, nsplit: (n1 + n2) as u32, sp0: 0, noown: 1, pad: 1,
                                   lastbias: if pr.use_sum { pr.lastbias } else { 0.0 }, ..base };
                e.buf(&pr.bag, 0, 3);
                self.go(e, C_ATTN, "k_attn", &a, (n1, NKV, 1), 128);
            }
            let a = AttnArgs { nkeys: (pos + 1 - pr.r0) as u32, first_pos: pr.r0 as u32, ring: 0, kps: k2 as u32, nsplit: (n1 + n2) as u32, sp0: n1 as u32, pad: 1, ..base };
            self.bind_kv(e, false);
            self.go(e, C_ATTN, "k_attn", &a, (n2, NKV, 1), 128);
            nsplit = n1 + n2;
        } else {
            // ---- attention (split-K) with the split combine + output gate fused into the last split per kv group
            self.bind_kv(e, l.swa);
            let nkeys = if l.swa { (pos + 1).min(EXT) } else { pos + 1 };
            let kps = Self::split_kps(nkeys);
            nsplit = nkeys.div_ceil(kps);
            let aa = AttnArgs { nkeys: nkeys as u32, first_pos: (pos + 1 - nkeys) as u32, ring: l.swa as u32, kps: kps as u32, nsplit: nsplit as u32,
                                pad: self.sep_comb as u32, ..base };
            self.go(e, C_ATTN, "k_attn", &aa, (nsplit, NKV, 1), 128);
        }
        let e = &cx.e;
        if self.sep_comb || self.tier_on() || self.lane.prune.borrow().is_some() {
            let ca = CombineArgs { part_off: ar.part, nsplit: nsplit as u32, gate_off: ar.gate, out_off: ar.attn_g };
            self.go(e, C_COMB, "k_attn_combine", &ca, (NH, 1, 1), 128);
        }
        // ---- output projection + post-conv (+ residual unless this layer starts a new block)
        let block_start = ip % BLOCK == 0;
        let mut live: Vec<usize> = blocks.clone(); live.push(cur); live.extend_from_slice(pinned);
        let mid = va.alloc(&live);
        let mut so = Engine::seg(&l.wo, ar.attn_g, self.v(mid));
        so.epi = 1; so.res_off = if block_start { -1 } else { self.v(cur) as i32 };
        so.cs_off = cs_slot + (2 * NKV * HD * 3) as u32; so.convw_off = l.attn_post_conv;
        self.mv(e, C_WO, &[so]);
        if block_start { blocks.push(cur); }
        // ---- FFN input: depth mix (+ control) -> normalized
        let mut st = blocks.clone(); st.push(mid);
        let x_off = if let Some(lw) = ctl {
            let mut jf = self.mix_job(&st, l.ffn_res_query as i32, 1, &[ar.fin], &[]);
            jf.ncopy[0][0] = ar.cat[3] as i32;
            self.mix(e, C_MIXF, &[jf]);
            self.ctrl_dec(e, &[CtrlJob { u_off: ar.fin, m_off: ar.msg[3], d_off: lw.down[3], out_off: ar.ctrlg[3], gain_off: lw.gain[3],
                                         forget_off: lw.forget[3], res_out: ar.finc, ss_off: ar.ssp[3], norm_w: l.ffn_norm as i32, ctr: 11 }]);
            ar.finc
        } else {
            self.mix(e, C_MIXF, &[self.mix_job(&st, l.ffn_res_query as i32, 1, &[ar.fin], &[l.ffn_norm as i32])]);
            ar.fin
        };
        // ---- router with fused top-k (last threadgroup)
        let sel = ar.sel_log + ((self.done.get() - 1) * 16) as u32;
        let rpost = MvArgs { post: (!self.sep_topk) as u32, ctr: 12, bias_off: l.exp_bias, sel_off: sel, wscale: self.wscale, ..Default::default() };
        self.mv_post(e, C_ROUTER, &[Engine::seg_pb(l.router, NEXP as u32, E as u32, x_off, ar.rlog)], rpost);
        let mut live: Vec<usize> = blocks.clone(); live.push(mid); live.extend_from_slice(pinned);
        let out = va.alloc(&live);
        let topk_args = MoeArgs { logits_off: ar.rlog, bias_off: l.exp_bias, sel_off: sel, wscale: self.wscale, ..Default::default() };
        if self.sep_topk { self.go(e, C_ROUTER, "k_topk", &topk_args, (1, 1, 1), 32); }
        let ma = MoeArgs {
            logits_off: ar.rlog, bias_off: l.exp_bias, sel_off: sel, x_off,
            norm_off: -1, ss_off: -1, ss_n: 0, eps: self.eps,
            gu_lo: l.gu.off as u32, gu_hi: (l.gu.off >> 32) as u32, gu_expert_bytes: l.gu.expert_bytes as u32, gu_row_bytes: l.gu.row_bytes,
            act_off: ar.act, wscale: self.wscale, beta: self.situ_beta, lbeta: self.situ_lbeta,
            dn_lo: l.dn.off as u32, dn_hi: (l.dn.off >> 32) as u32, dn_expert_bytes: l.dn.expert_bytes as u32, dn_row_bytes: l.dn.row_bytes,
            dn_type: l.dn.ty, out_off: self.v(out), res_off: self.v(mid) as i32, cs_off: cs_slot + ((2 * NKV * HD + E) * 3) as u32,
            convw_off: l.ffn_post_conv, pad: [0; 3],
        };
        // ---- experts
        self.go(e, C_GU, "k_moe_gu", &ma, (FF / 4, TOPK, 1), 128);
        self.go(e, C_DOWN, "k_moe_down", &ma, (E / 4, 1, 1), 64);
        self.last_out.set(out); self.last_mid.set(mid);
        out
    }

    // ======================================================================== batched prefill
    /// Encode prefill of `t` tokens (toks[p0..p0+t]) of this lane as one batch: a step with one span.
    pub fn encode_prefill(&self, cb: &CmdBuf, p0: usize, t: usize, logits: bool, argmax: bool) {
        self.eng.encode_spans(cb, &[Span { lane: self.lane, p0, t }], logits, argmax);
    }
}

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

    /// Chunk 4096 keeps `min(t, MAX_SPANS)` vocabulary rows. A verify span of k=3 needs 4 of them.
    /// Growing `lg` to one row per token would be 32× those rows; this layout does not.
    /// fp16 path, fp16 K/V, vocab pad 201024 (`output.weight` rows of Inkling-2-Mini).
    #[test]
    fn chunk_4096_batch_arena_is_unchanged_for_verify_rows() {
        let vocab_pad = 201024usize;
        let b = BArena::new(0, 4096, true, 2, vocab_pad);
        assert_eq!(b.lg_rows, 128);
        assert_eq!(b.cap, 4096);
        let bytes = b.end as usize * 4;
        assert_eq!(bytes, 1_791_665_152);
        assert!(4 <= b.lg_rows as usize, "k=3 is 4 rows and must fit the existing lg");
        assert!(b.lg_rows as usize * 32 == 4096);
    }

    /// One image is N_SLOT × NCH × 3 floats. At k=3 the span holds 4 of them (the page's 14.64 MiB); at k=7, 8 (29.28 MiB).
    #[test]
    fn verify_cs_image_is_one_convolution_state() {
        let one = N_SLOT * NCH * 3 * 4;
        assert_eq!(one, 3_833_856);
        assert_eq!(one * 4, 15_335_424);
        assert_eq!(one * 8, 30_670_848);
        assert_eq!(Engine::cs_image_bytes(), one);
    }
}