cortiq-engine 0.5.71

Portable inference runtime for the CMF model format, with no ML framework underneath: runs on CPU, and on GPU (Vulkan / Metal / DX12) with the `gpu` feature; tokenizer, chat templates and dynamic per-skill weight overlay.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
//! DeepSeek-V4 blocks that no other supported architecture has.
//!
//! Transcribed from the reference `inference/model.py` + `inference/kernel.py`
//! shipped with the checkpoint, not inferred from tensor names — the pieces
//! below have enough hidden structure (a second normalization on the heads, a
//! bias that steers selection but not weights, a mixing matrix normalized by
//! Sinkhorn) that guessing produces a model which *almost* answers.
//!
//! Each function is the smallest unit the reference defines, so it can be
//! checked on its own. The forward that stitches them together comes after
//! the attention and compressor land.

/// Hyper-connections. The hidden state of this model is not a vector: it is
/// `hc` copies of one (`hc_mult`, 4 in the release). A block folds them to
/// one, runs attention or the FFN, then expands back — there is no ordinary
/// residual anywhere in the stack.
///
/// `mixes` is the per-token projection `F.linear(x.flatten(), hc_fn) * rsqrt`
/// of length `(2 + hc) * hc`; it splits into three parts:
///   * `pre[j]`  — how much of copy `j` goes into the folded vector,
///   * `post[j]` — how much of the block's output returns to copy `j`,
///   * `comb`    — an `hc x hc` matrix mixing the old copies into the new.
///
/// `comb` is made doubly stochastic by Sinkhorn: a row softmax, then
/// alternating row/column normalization. The reference runs the column step
/// once before the loop and `iters - 1` times inside it, which is why the
/// loop below starts from the column-normalized matrix.
pub fn hc_split_sinkhorn(
    mixes: &[f32],
    hc_scale: &[f32; 3],
    hc_base: &[f32],
    hc: usize,
    iters: usize,
    eps: f32,
    pre: &mut [f32],
    post: &mut [f32],
    comb: &mut [f32],
) {
    debug_assert_eq!(mixes.len(), (2 + hc) * hc);
    debug_assert_eq!(comb.len(), hc * hc);
    for j in 0..hc {
        pre[j] = sigmoid(mixes[j] * hc_scale[0] + hc_base[j]) + eps;
        // The post weights carry a factor 2 in the reference — with a
        // sigmoid alone the block's output could never exceed the residual.
        post[j] = 2.0 * sigmoid(mixes[j + hc] * hc_scale[1] + hc_base[j + hc]);
    }
    for j in 0..hc {
        for k in 0..hc {
            let idx = j * hc + k + hc * 2;
            comb[j * hc + k] = mixes[idx] * hc_scale[2] + hc_base[idx];
        }
    }
    // row softmax + eps
    for j in 0..hc {
        let row = &mut comb[j * hc..(j + 1) * hc];
        let m = row.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
        let mut sum = 0.0;
        for v in row.iter_mut() {
            *v = (*v - m).exp();
            sum += *v;
        }
        for v in row.iter_mut() {
            *v = *v / sum + eps;
        }
    }
    // one column normalization, then (iters - 1) row/column rounds
    normalize_cols(comb, hc, eps);
    for _ in 0..iters.saturating_sub(1) {
        normalize_rows(comb, hc, eps);
        normalize_cols(comb, hc, eps);
    }
}

fn normalize_rows(m: &mut [f32], n: usize, eps: f32) {
    for j in 0..n {
        let s: f32 = m[j * n..(j + 1) * n].iter().sum::<f32>() + eps;
        for v in m[j * n..(j + 1) * n].iter_mut() {
            *v /= s;
        }
    }
}

fn normalize_cols(m: &mut [f32], n: usize, eps: f32) {
    for k in 0..n {
        let mut s = eps;
        for j in 0..n {
            s += m[j * n + k];
        }
        for j in 0..n {
            m[j * n + k] /= s;
        }
    }
}

#[inline]
fn sigmoid(x: f32) -> f32 {
    1.0 / (1.0 + (-x).exp())
}

/// The projection feeding `hc_split_sinkhorn`: the `hc` copies are flattened
/// to one `hc*dim` vector, RMS-scaled (no learned weight — the reference uses
/// a bare rsqrt of the mean square), and projected by `hc_fn` `[mix_hc, hc*dim]`.
pub fn hc_mixes(
    x_flat: &[f32],
    hc_fn: &[f32],
    mix_hc: usize,
    eps: f32,
    pool: Option<&crate::pool::Pool>,
    out: &mut [f32],
) {
    let n = x_flat.len();
    debug_assert_eq!(hc_fn.len(), mix_hc * n);
    debug_assert_eq!(out.len(), mix_hc);
    let ms = x_flat.iter().map(|v| v * v).sum::<f32>() / n as f32;
    let rsqrt = 1.0 / (ms + eps).sqrt();
    // A dense f32 matvec of mix_hc rows over hc*dim — 1.6 MB read per call on
    // the release, and TWO calls per layer, so 135 MB a token. It ran on one
    // thread and cost more than the whole attention block.
    match pool {
        Some(p) if n >= 4096 => {
            let addr = crate::pool::SendMut::new(out.as_mut_ptr());
            p.run_rows(mix_hc, &|start, end| {
                for i in start..end {
                    let row = &hc_fn[i * n..(i + 1) * n];
                    let v = row.iter().zip(x_flat).map(|(a, b)| a * b).sum::<f32>() * rsqrt;
                    unsafe { *addr.at(i) = v };
                }
            });
        }
        _ => {
            for (i, o) in out.iter_mut().enumerate() {
                let row = &hc_fn[i * n..(i + 1) * n];
                *o = row.iter().zip(x_flat).map(|(a, b)| a * b).sum::<f32>() * rsqrt;
            }
        }
    }
}

/// Fold `hc` copies into one vector: `y = Σ_j pre[j] · x[j]`.
pub fn hc_fold(x: &[f32], pre: &[f32], hc: usize, dim: usize, out: &mut [f32]) {
    debug_assert_eq!(x.len(), hc * dim);
    out.fill(0.0);
    for j in 0..hc {
        let w = pre[j];
        let src = &x[j * dim..(j + 1) * dim];
        for (o, v) in out.iter_mut().zip(src) {
            *o += w * v;
        }
    }
}

/// Expand the block's output back into `hc` copies:
/// `y[j] = post[j] · out + Σ_k comb[k][j] · residual[k]`.
///
/// Note the transpose: the reference sums over the SECOND-to-last axis of
/// `comb.unsqueeze(-1) * residual.unsqueeze(-2)`, i.e. copy `k` of the
/// residual contributes to new copy `j` with weight `comb[k][j]`.
pub fn hc_expand(
    block_out: &[f32],
    residual: &[f32],
    post: &[f32],
    comb: &[f32],
    hc: usize,
    dim: usize,
    out: &mut [f32],
) {
    debug_assert_eq!(residual.len(), hc * dim);
    debug_assert_eq!(out.len(), hc * dim);
    for j in 0..hc {
        let dst = &mut out[j * dim..(j + 1) * dim];
        let p = post[j];
        for (d, o) in dst.iter_mut().enumerate() {
            *o = p * block_out[d];
        }
        for k in 0..hc {
            let w = comb[k * hc + j];
            let src = &residual[k * dim..(k + 1) * dim];
            for (o, v) in dst.iter_mut().zip(src) {
                *o += w * v;
            }
        }
    }
}

/// The head fold, run once after the last layer: same shape as `hc_fold`'s
/// weights but WITHOUT Sinkhorn — a plain sigmoid gate per copy.
pub fn hc_head_pre(mixes: &[f32], scale: f32, base: &[f32], hc: usize, eps: f32, pre: &mut [f32]) {
    for j in 0..hc {
        pre[j] = sigmoid(mixes[j] * scale + base[j]) + eps;
    }
}

/// MoE routing. Three details decide whether this model answers or merely
/// produces fluent text:
///   * the score is `sqrt(softplus(x))`, not a softmax or a sigmoid;
///   * the selection bias shifts WHICH experts win but never the weights —
///     those come from the pre-bias scores;
///   * the weights are renormalized over the chosen experts, then scaled.
///
/// `bias` is `None` on the hash layers, where `indices` come from a
/// token-id table instead (see `hash_route`).
/// `forced` fixes the chosen experts (the hash layers' token-id table). They
/// have to be known here rather than swapped in afterwards: the weights are
/// the scores gathered at whichever indices win, so substituting the indices
/// later leaves every weight attached to a different expert.
pub fn route(
    scores_in: &[f32],
    bias: Option<&[f32]>,
    top_k: usize,
    route_scale: f32,
    forced: Option<&[usize]>,
    mask: Option<&[bool]>,
    indices: &mut Vec<usize>,
    weights: &mut Vec<f32>,
) {
    let n = scores_in.len();
    let mut scores = Vec::with_capacity(n);
    for &s in scores_in {
        // softplus, guarded like the reference's F.softplus (linear past 20)
        let sp = if s > 20.0 { s } else { (1.0 + s.exp()).ln() };
        scores.push(sp.sqrt());
    }
    indices.clear();
    weights.clear();
    match forced {
        Some(f) => indices.extend(f.iter().copied()),
        None => {
            let mut shifted: Vec<f32> = match bias {
                Some(b) => scores.iter().zip(b).map(|(s, b)| s + b).collect(),
                None => scores.clone(),
            };
            if let Some(m) = mask {
                for (i, s) in shifted.iter_mut().enumerate() {
                    if !m.get(i).copied().unwrap_or(true) {
                        *s = f32::NEG_INFINITY;
                    }
                }
            }
            for _ in 0..top_k.min(n) {
                let mut best = 0usize;
                let mut bv = f32::NEG_INFINITY;
                for (i, &v) in shifted.iter().enumerate() {
                    if v > bv {
                        bv = v;
                        best = i;
                    }
                }
                if !bv.is_finite() {
                    break;
                }
                indices.push(best);
                shifted[best] = f32::NEG_INFINITY;
            }
        }
    }
    // The weight is always the PRE-bias score of the chosen expert.
    for &i in indices.iter() {
        weights.push(scores.get(i).copied().unwrap_or(0.0));
    }
    let sum: f32 = weights.iter().sum();
    if sum > 0.0 {
        for w in weights.iter_mut() {
            *w = *w / sum * route_scale;
        }
    }
}

/// Hash layers: the experts of token `tid` are a row of the `tid2eid` table,
/// and the router does not run at all. Their weights still come from the
/// scored path (the reference gathers `original_scores` at those indices).
pub fn hash_route(tid2eid: &[f32], vocab: usize, top_k: usize, tid: u32) -> Vec<usize> {
    let row = (tid as usize).min(vocab.saturating_sub(1)) * top_k;
    (0..top_k)
        .map(|k| tid2eid.get(row + k).copied().unwrap_or(0.0) as usize)
        .collect()
}

/// Rotary on the LAST `rd` dims only — the rest of the head carries no
/// position. `inverse` runs the rotation backwards, which the reference
/// applies to the attention OUTPUT before the o-projection (the value
/// stream carries the same rope-tagged tail as the keys, and it has to be
/// untagged again). Missing that step leaves a model that reads fluently
/// and attends to the wrong offsets.
pub fn rope_tail(v: &mut [f32], inv_freq: &[f32], pos: usize, rd: usize, inverse: bool) {
    let n = v.len();
    debug_assert!(
        rd <= n && rd % 2 == 0,
        "rope tail {rd} wider than the vector {n}"
    );
    // A tail wider than the vector is a configuration mistake, and `n - rd`
    // would wrap into an index in the billions rather than say so.
    let rd = rd.min(n) & !1;
    let base = n - rd;
    // ADJACENT pairs, not halves. The reference forms its complex numbers
    // with `unflatten(-1, (-1, 2))` + `view_as_complex`, i.e. (x0,x1),
    // (x2,x3), … — the interleaved convention. Half-split pairing agrees
    // with it exactly at position 0, where the rotation is the identity,
    // and disagrees everywhere else. That is why short answers came out
    // right and everything longer drifted, repeated itself and could not
    // count: every position past the first was rotated into the wrong
    // basis.
    for i in 0..rd / 2 {
        let theta = pos as f32 * inv_freq[i];
        let (s, c) = (theta.sin(), theta.cos());
        let s = if inverse { -s } else { s };
        let a = v[base + 2 * i];
        let b = v[base + 2 * i + 1];
        v[base + 2 * i] = a * c - b * s;
        v[base + 2 * i + 1] = a * s + b * c;
    }
}

/// RMS normalize in place with no learned weight — the reference applies
/// this to each attention head AFTER `wq_b`, on top of the `q_norm` that
/// already normalized the LoRA rank. Two normalizations, not one.
pub fn rms_inplace(v: &mut [f32], eps: f32) {
    let ms = v.iter().map(|x| x * x).sum::<f32>() / v.len() as f32;
    let inv = 1.0 / (ms + eps).sqrt();
    for x in v.iter_mut() {
        *x *= inv;
    }
}

/// Attention over an explicit position LIST (window ⊕ compressed), with a
/// learned per-head sink. The sink is an extra logit with no value vector:
/// it lets a head attend to "nothing", so its softmax denominator carries
/// `exp(sink - max)` while contributing no output. Index `usize::MAX`
/// marks a masked slot (the reference writes -1 into topk_idxs).
pub fn sparse_attend(
    q: &[f32],
    kv: &[f32],
    idxs: &[usize],
    sink: f32,
    scale: f32,
    head_dim: usize,
    out: &mut [f32],
) {
    let mut m = sink;
    let mut scores = Vec::with_capacity(idxs.len());
    for &p in idxs {
        if p == usize::MAX {
            scores.push(f32::NEG_INFINITY);
            continue;
        }
        let k = &kv[p * head_dim..(p + 1) * head_dim];
        let dot: f32 = q.iter().zip(k).map(|(a, b)| a * b).sum::<f32>() * scale;
        m = m.max(dot);
        scores.push(dot);
    }
    let mut denom = (sink - m).exp();
    out.fill(0.0);
    for (&p, &s) in idxs.iter().zip(&scores) {
        if p == usize::MAX {
            continue;
        }
        let w = (s - m).exp();
        denom += w;
        let v = &kv[p * head_dim..(p + 1) * head_dim];
        for (o, x) in out.iter_mut().zip(v) {
            *o += w * x;
        }
    }
    if std::env::var("CMF_ATTN_DEBUG").is_ok() {
        eprintln!(
            "    [порт] позиций={} score={:?} sink={sink:.4} denom={denom:.4} |q|={:.3}",
            idxs.iter().filter(|&&p| p != usize::MAX).count(),
            scores
                .iter()
                .map(|x| (x * 10000.0).round() / 10000.0)
                .collect::<Vec<_>>(),
            q.iter().map(|x| x * x).sum::<f32>().sqrt()
        );
    }
    let inv = 1.0 / denom;
    for o in out.iter_mut() {
        *o *= inv;
    }
}

/// The grouped low-rank output projection: heads are split into `groups`,
/// each group's slice is projected to `lora` by its own block of `wo_a`,
/// and the concatenation goes through `wo_b`. `wo_a` is stored
/// `[groups, lora, per_group]`.
/// `wo_a_row` is `(row, x) -> dot`, reading one row of `wo_a` against the
/// slice of `attn` its group owns; `wo_b` is the plain projection of the
/// concatenated groups. Both arrive as closures so the caller can serve them
/// straight from quantized tensors.
pub fn o_project(
    attn: &[f32],
    wo_a_row: &(dyn Fn(usize, &[f32], &mut [f32]) -> f32 + Sync),
    scratch_len: usize,
    wo_b: &dyn Fn(&[f32], &mut [f32]),
    groups: usize,
    lora: usize,
    pool: Option<&crate::pool::Pool>,
    out: &mut [f32],
) {
    let per_group = attn.len() / groups;
    let mut mid = vec![0.0f32; groups * lora];
    let slice_of = |i: usize| {
        let g = i / lora;
        &attn[g * per_group..(g + 1) * per_group]
    };
    match pool {
        // Each row of `mid` is one dot product against its group's slice —
        // independent, so the rows split cleanly. This is the largest
        // single-threaded cost in the decode otherwise: on the release
        // checkpoint wo_a is 33M weights, read once per layer per token.
        Some(p) if mid.len() >= 256 => {
            let addr = crate::pool::SendMut::new(mid.as_mut_ptr());
            p.run_rows(mid.len(), &|start, end| {
                let mut sc = vec![0.0f32; scratch_len];
                for i in start..end {
                    let v = wo_a_row(i, slice_of(i), &mut sc);
                    unsafe { *addr.at(i) = v };
                }
            });
        }
        _ => {
            let mut sc = vec![0.0f32; scratch_len];
            for (i, m) in mid.iter_mut().enumerate() {
                *m = wo_a_row(i, slice_of(i), &mut sc);
            }
        }
    }
    wo_b(&mid, out);
}

pub fn compress_window(
    kv: &[f32],
    score: &[f32],
    ape: &[f32],
    ratio: usize,
    width: usize,
    out: &mut [f32],
) {
    debug_assert_eq!(kv.len(), ratio * width);
    debug_assert_eq!(ape.len(), ratio * width);
    let biased: Vec<f32> = score.iter().zip(ape).map(|(s, a)| s + a).collect();
    pool_by_score(kv, &biased, ratio, width, out);
}

/// Softmax over the `slots` axis, per dimension, then the weighted sum —
/// the pooling both the plain and the overlapping compressor end in.
/// `-inf` scores are how an absent slot votes for nothing, so the
/// max-subtraction has to survive a whole column of them.
pub fn pool_by_score(kv: &[f32], score: &[f32], slots: usize, width: usize, out: &mut [f32]) {
    debug_assert_eq!(kv.len(), slots * width);
    debug_assert_eq!(score.len(), slots * width);
    out.fill(0.0);
    for d in 0..width {
        let mut m = f32::NEG_INFINITY;
        for t in 0..slots {
            m = m.max(score[t * width + d]);
        }
        if !m.is_finite() {
            continue;
        }
        let mut denom = 0.0;
        for t in 0..slots {
            denom += (score[t * width + d] - m).exp();
        }
        if denom <= 0.0 {
            continue;
        }
        for t in 0..slots {
            out[d] += ((score[t * width + d] - m).exp() / denom) * kv[t * width + d];
        }
    }
}

/// The overlapping compressor (the release uses it wherever the ratio is 4).
///
/// Each token contributes `2*d` values: the first half belongs to the window
/// that started half a stride earlier, the second half to the current one.
/// At fold time the reference pools `2*ratio` entries of width `d` — the
/// PREVIOUS window's slots taking their first half, the current window's
/// slots taking their second half — then the current window becomes the
/// previous one. An absent previous window votes with `-inf`.
#[allow(clippy::too_many_arguments)]
pub fn compress_window_overlap(
    prev_kv: &[f32],
    prev_score: &[f32],
    cur_kv: &[f32],
    cur_score: &[f32],
    ratio: usize,
    d: usize,
    out: &mut [f32],
) {
    let slots = 2 * ratio;
    let mut kv = vec![0.0f32; slots * d];
    let mut sc = vec![f32::NEG_INFINITY; slots * d];
    let have_prev = prev_kv.len() == ratio * 2 * d;
    for t in 0..ratio {
        if have_prev {
            // the previous window's slots, first half of the dimensions
            kv[t * d..(t + 1) * d].copy_from_slice(&prev_kv[t * 2 * d..t * 2 * d + d]);
            sc[t * d..(t + 1) * d].copy_from_slice(&prev_score[t * 2 * d..t * 2 * d + d]);
        }
        // the current window's slots, second half
        let src = t * 2 * d + d;
        let dst = (ratio + t) * d;
        kv[dst..dst + d].copy_from_slice(&cur_kv[src..src + d]);
        sc[dst..dst + d].copy_from_slice(&cur_score[src..src + d]);
    }
    pool_by_score(&kv, &sc, slots, d, out);
}

/// The sparse indexer's scoring pass. For each query it ranks the
/// compressed positions and keeps the best `topk`.
///
/// Three details from the reference that a shape-only reading misses:
///   * the query comes from the SHARED LoRA output `qr` (the output of
///     `q_norm(wq_a(x))`, before attention's own `wq_b`), through the
///     indexer's own `wq_b` — not from attention's queries;
///   * scores are **relu'd** before the per-head weighting, so a head can
///     only ever vote for a position, never against it;
///   * the per-head weights are a projection of the hidden state scaled by
///     `head_dim^-0.5 * n_heads^-0.5`.
///
/// `causal_limit` is the number of compressed positions this query may see
/// (`(pos + 1) / ratio`); anything at or past it is masked.
#[allow(clippy::too_many_arguments)]
pub fn index_scores(
    q_heads: &[f32],
    kv: &[f32],
    head_weights: &[f32],
    n_heads: usize,
    head_dim: usize,
    n_pos: usize,
    causal_limit: usize,
    pool: Option<&crate::pool::Pool>,
    out: &mut Vec<f32>,
) {
    out.clear();
    out.resize(n_pos, 0.0);
    let score_at = |t: usize| -> f32 {
        if t >= causal_limit {
            return f32::NEG_INFINITY;
        }
        let k = &kv[t * head_dim..(t + 1) * head_dim];
        let mut acc = 0.0;
        for h in 0..n_heads {
            let q = &q_heads[h * head_dim..(h + 1) * head_dim];
            let dot: f32 = q.iter().zip(k).map(|(a, b)| a * b).sum();
            // relu BEFORE weighting: a head votes for a position or abstains
            acc += dot.max(0.0) * head_weights[h];
        }
        acc
    };
    // Positions are independent, and their number grows with the context —
    // this was the one loop in the attention step still walking the whole
    // compressed axis on one thread.
    match pool {
        Some(p) if n_pos >= 64 => {
            let addr = crate::pool::SendMut::new(out.as_mut_ptr());
            p.run_rows(n_pos, &|start, end| {
                for t in start..end {
                    unsafe { *addr.at(t) = score_at(t) };
                }
            });
        }
        _ => {
            for (t, o) in out.iter_mut().enumerate() {
                *o = score_at(t);
            }
        }
    }
}

/// Top-`k` positions by score, ties broken by the lower index so the choice
/// is deterministic across backends. Masked slots (-inf) never win, and a
/// short history simply returns fewer than `k`.
pub fn top_k_positions(scores: &[f32], k: usize, out: &mut Vec<usize>) {
    out.clear();
    // When k reaches the whole list there is nothing to choose: every finite
    // position wins, and they come out in index order anyway. The general
    // path is k rounds of argmax — O(k·n) — and at index_topk = 512 against a
    // compressed axis that is still shorter than that, it was doing 160k
    // comparisons a layer to arrive at "all of them". This grows with the
    // context, which is exactly when it hurts.
    if k >= scores.len() {
        out.extend(
            scores
                .iter()
                .enumerate()
                .filter(|(_, v)| v.is_finite())
                .map(|(i, _)| i),
        );
        return;
    }
    let mut taken = vec![false; scores.len()];
    for _ in 0..k.min(scores.len()) {
        let mut best = usize::MAX;
        let mut bv = f32::NEG_INFINITY;
        for (i, &v) in scores.iter().enumerate() {
            if !taken[i] && v > bv && v.is_finite() {
                bv = v;
                best = i;
            }
        }
        if best == usize::MAX {
            break;
        }
        taken[best] = true;
        out.push(best);
    }
    out.sort_unstable();
}

/// SwiGLU expert: `w2(silu(w1(x)) * w3(x))`, with the routing weight folded
/// in before the down projection exactly as the reference does.
///
/// `limit` is the reference's `swiglu_limit` (10.0 in the release), and its
/// asymmetry is not a typo: `up` is clamped on BOTH sides, `gate` only from
/// above — the reference leaves silu's negative tail alone. A limit of 0
/// disables the clamp, which is also what the reference does.
#[allow(clippy::too_many_arguments)]
pub fn expert_swiglu(
    x: &[f32],
    w1: &dyn Fn(&[f32], &mut [f32]),
    w3: &dyn Fn(&[f32], &mut [f32]),
    w2: &dyn Fn(&[f32], &mut [f32]),
    inter: usize,
    weight: f32,
    limit: f32,
    out: &mut [f32],
) {
    let mut gate = vec![0.0f32; inter];
    let mut up = vec![0.0f32; inter];
    w1(x, &mut gate);
    w3(x, &mut up);
    if limit > 0.0 {
        for u in up.iter_mut() {
            *u = u.clamp(-limit, limit);
        }
        for g in gate.iter_mut() {
            *g = g.min(limit);
        }
    }
    for (g, u) in gate.iter_mut().zip(&up) {
        let silu = *g / (1.0 + (-*g).exp());
        *g = silu * u * weight;
    }
    w2(&gate, out);
}

/// Everything one layer needs that is not a plain matrix: the shapes and
/// scalars the reference reads out of `ModelArgs`.
#[derive(Debug, Clone, Copy)]
pub struct Dsv4Cfg {
    pub dim: usize,
    pub n_heads: usize,
    pub head_dim: usize,
    pub rope_head_dim: usize,
    pub q_lora_rank: usize,
    pub o_lora_rank: usize,
    pub o_groups: usize,
    pub hc_mult: usize,
    pub hc_sinkhorn_iters: usize,
    pub hc_eps: f32,
    pub norm_eps: f32,
    pub n_routed_experts: usize,
    pub top_k: usize,
    pub moe_inter: usize,
    pub route_scale: f32,
    /// The reference's `swiglu_limit`; 0 disables the clamp.
    pub swiglu_limit: f32,
    /// Sliding-window size (`window_size`, 128 in the release).
    pub window: usize,
    pub index_topk: usize,
    pub vocab: usize,
}

/// The per-block hyper-connection cycle, which is the same shape around
/// attention and around the FFN: fold the copies, normalize, run the
/// block, expand back. `block` sees a plain `dim`-vector and knows nothing
/// about the copies — that separation is what keeps attention and the MoE
/// free of hyper-connection bookkeeping.
///
/// `hc_fn` is `[mix_hc, hc*dim]`, `hc_base` is `[mix_hc]`, `hc_scale` is 3.
#[allow(clippy::too_many_arguments)]
#[allow(clippy::too_many_arguments)]
pub fn hc_block<F: FnMut(&[f32], &mut [f32])>(
    state: &mut [f32],
    hc_fn: &[f32],
    hc_scale: &[f32; 3],
    hc_base: &[f32],
    norm_w: &[f32],
    cfg: &Dsv4Cfg,
    scratch: &mut HcScratch,
    pool: Option<&crate::pool::Pool>,
    mut block: F,
) {
    let (hc, dim) = (cfg.hc_mult, cfg.dim);
    let mix_hc = (2 + hc) * hc;
    hc_mixes(state, hc_fn, mix_hc, cfg.norm_eps, pool, &mut scratch.mixes);
    hc_split_sinkhorn(
        &scratch.mixes,
        hc_scale,
        hc_base,
        hc,
        cfg.hc_sinkhorn_iters,
        cfg.hc_eps,
        &mut scratch.pre,
        &mut scratch.post,
        &mut scratch.comb,
    );
    hc_fold(state, &scratch.pre, hc, dim, &mut scratch.folded);
    // RMSNorm with the layer's learned weight, on the folded vector.
    let ms = scratch.folded.iter().map(|v| v * v).sum::<f32>() / dim as f32;
    let inv = 1.0 / (ms + cfg.norm_eps).sqrt();
    for (v, w) in scratch.folded.iter_mut().zip(norm_w) {
        *v = *v * inv * w;
    }
    block(&scratch.folded, &mut scratch.block_out);
    scratch.residual.copy_from_slice(state);
    hc_expand(
        &scratch.block_out,
        &scratch.residual,
        &scratch.post,
        &scratch.comb,
        hc,
        dim,
        state,
    );
}

/// Reusable buffers for `hc_block` — one allocation per pipeline, not per
/// layer per token.
pub struct HcScratch {
    pub mixes: Vec<f32>,
    pub pre: Vec<f32>,
    pub post: Vec<f32>,
    pub comb: Vec<f32>,
    pub folded: Vec<f32>,
    pub block_out: Vec<f32>,
    pub residual: Vec<f32>,
}

impl HcScratch {
    pub fn new(cfg: &Dsv4Cfg) -> Self {
        let (hc, dim) = (cfg.hc_mult, cfg.dim);
        Self {
            mixes: vec![0.0; (2 + hc) * hc],
            pre: vec![0.0; hc],
            post: vec![0.0; hc],
            comb: vec![0.0; hc * hc],
            folded: vec![0.0; dim],
            block_out: vec![0.0; dim],
            residual: vec![0.0; hc * dim],
        }
    }
}

/// The final fold, after the last layer: `hc` copies to one vector, with a
/// plain sigmoid gate (no Sinkhorn), then the model's output norm.
pub fn hc_head_fold(
    state: &[f32],
    hc_fn: &[f32],
    hc_scale: f32,
    hc_base: &[f32],
    cfg: &Dsv4Cfg,
    pool: Option<&crate::pool::Pool>,
    out: &mut [f32],
) {
    let (hc, dim) = (cfg.hc_mult, cfg.dim);
    let mut mixes = vec![0.0f32; hc];
    hc_mixes(state, hc_fn, hc, cfg.norm_eps, pool, &mut mixes);
    let mut pre = vec![0.0f32; hc];
    hc_head_pre(&mixes, hc_scale, hc_base, hc, cfg.hc_eps, &mut pre);
    hc_fold(state, &pre, hc, dim, out);
}

/// One layer's weights. Everything quantized rides as `QTensor` so the
/// existing kernels (and the mmap) serve them; the small fp32 pieces —
/// norms, the hyper-connection projections, the sink, the compressor's
/// position bias — are plain vectors, exactly as the reference keeps them
/// in fp32 regardless of the checkpoint's storage dtype.
pub struct Dsv4Layer {
    pub attn_norm: Vec<f32>,
    pub ffn_norm: Vec<f32>,
    // attention: the double LoRA, the compressed KV, the grouped output
    pub wq_a: crate::qtensor::QTensor,
    pub q_norm: Vec<f32>,
    pub wq_b: crate::qtensor::QTensor,
    pub wkv: crate::qtensor::QTensor,
    pub kv_norm: Vec<f32>,
    pub wo_a: crate::qtensor::QTensor,
    pub wo_b: crate::qtensor::QTensor,
    pub attn_sink: Vec<f32>,
    /// `None` on the pure sliding-window layers (`compress_ratio == 0`).
    pub compressor: Option<Dsv4Compressor>,
    /// Only on the layers whose ratio is 4.
    pub indexer: Option<Dsv4Indexer>,
    // hyper-connections, one set for the attention half and one for the FFN
    pub hc_attn_fn: Vec<f32>,
    pub hc_attn_base: Vec<f32>,
    pub hc_attn_scale: [f32; 3],
    pub hc_ffn_fn: Vec<f32>,
    pub hc_ffn_base: Vec<f32>,
    pub hc_ffn_scale: [f32; 3],
    // MoE
    pub gate: crate::qtensor::QTensor,
    /// noaux_tc selection bias — `None` on the hash layers.
    pub gate_bias: Option<Vec<f32>>,
    /// Token-id → expert table on the hash layers, `None` elsewhere.
    pub tid2eid: Option<Vec<f32>>,
    pub experts: Vec<Dsv4Expert>,
    pub shared: Dsv4Expert,
    /// Task-conditional restriction over the routed experts
    /// (`CMF_MOE_MASK` + `CMF_MOE_MASK_COVER`): `false` experts are not
    /// selectable and the weights renormalize over what remains. `None` on
    /// the hash layers — their table names specific experts, so masking
    /// there would silently reroute rather than restrict.
    pub mask: Option<Vec<bool>>,
}

pub struct Dsv4Expert {
    pub w1: crate::qtensor::QTensor,
    pub w2: crate::qtensor::QTensor,
    pub w3: crate::qtensor::QTensor,
}

pub struct Dsv4Compressor {
    pub wkv: crate::qtensor::QTensor,
    pub wgate: crate::qtensor::QTensor,
    pub norm: Vec<f32>,
    /// `[ratio, coff*head_dim]` — the in-window position bias.
    pub ape: Vec<f32>,
    pub ratio: usize,
    /// Overlapping windows (the reference sets this when ratio == 4), which
    /// doubles the projection width.
    pub overlap: bool,
}

pub struct Dsv4Indexer {
    pub wq_b: crate::qtensor::QTensor,
    pub weights_proj: crate::qtensor::QTensor,
    pub compressor: Dsv4Compressor,
}

/// Model-global pieces: the embedding, the output head and the final
/// hyper-connection fold.
pub struct Dsv4Globals {
    /// RoPE frequencies for the layers that carry a KV compressor: base
    /// `compress_rope_theta` (160 000 in the release) WITH YaRN.
    pub inv_freq_compress: Vec<f32>,
    /// …and for the pure sliding-window layers: base `rope_theta` (10 000)
    /// with YaRN OFF. The reference picks per layer:
    ///   if compress_ratio { original_seq_len, compress_rope_theta }
    ///   else              { 0, rope_theta }   // "disable YaRN"
    /// One shared table gets both groups wrong — the model still retrieves
    /// facts, because attention still attends, but every position is rotated
    /// by the wrong angle, so it repeats itself and cannot count.
    pub inv_freq_window: Vec<f32>,
    pub embed: crate::qtensor::QTensor,
    pub norm: Vec<f32>,
    pub head: crate::qtensor::QTensor,
    pub hc_head_fn: Vec<f32>,
    pub hc_head_base: Vec<f32>,
    pub hc_head_scale: f32,
}

/// Per-sequence state. The compressor and the indexer each keep their own
/// compressed cache and a partial window, so decode picks up mid-window
/// exactly where prefill left off.
pub struct Dsv4State {
    /// Sliding-window KV per layer, `[window, head_dim]` ring.
    pub window: Vec<Vec<f32>>,
    /// Compressed KV per layer, appended once per `ratio` tokens.
    pub compressed: Vec<Vec<f32>>,
    /// The indexer's own compressed cache per layer.
    pub index_kv: Vec<Vec<f32>>,
    /// Partial window being accumulated, per layer: kv and score streams.
    pub pending_kv: Vec<Vec<f32>>,
    pub pending_score: Vec<Vec<f32>>,
    /// The window before it, kept only by the overlapping compressor —
    /// its fold reads half its dimensions from the previous stride.
    pub prev_kv: Vec<Vec<f32>>,
    pub prev_score: Vec<Vec<f32>>,
    /// The indexer's compressor runs alongside the attention one and keeps
    /// its own window — same shape, different width and different weights.
    pub pending_ix_kv: Vec<Vec<f32>>,
    pub pending_ix_score: Vec<Vec<f32>>,
    pub prev_ix_kv: Vec<Vec<f32>>,
    pub prev_ix_score: Vec<Vec<f32>>,
    pub pos: usize,
    /// Identifies this sequence's caches on the device. A fresh state gets a
    /// fresh id, so a device buffer left over from the previous conversation
    /// can never be read as if it belonged to this one.
    pub kv_id: u64,
    /// When the token graph owns a layer's caches, the CONTENTS live on the
    /// card and only these counts stay here — how much of the window is
    /// filled, and how many compressed entries each cache holds. All three
    /// follow from the position, so keeping them costs nothing and reading
    /// them back would cost a round trip.
    pub dev_filled: Vec<usize>,
    pub dev_n_comp: Vec<usize>,
    pub dev_n_ix: Vec<usize>,
    /// True once this sequence has run a layer on the card with the device
    /// owning its state. The host copies above are stale from then on, so
    /// the CPU path must not be used for that layer again.
    pub dev_owned: bool,
    /// The device-layer set of the FIRST chained token. If it ever differs,
    /// some layer's caches are on the wrong side and the answer would be
    /// quietly wrong — the loop refuses instead.
    pub dev_set: Vec<bool>,
    /// Which layers run their MoE on the card from a PARTIAL expert pack.
    /// Their walk attention must stay on the host: the device attention
    /// frame and the device MoE frame of one layer share pooled slots and
    /// poison each other across tokens (see `attention_step`).
    pub partial_set: Vec<bool>,
    /// More than one layer walks past the device prefix. The stale-slot
    /// poison needs a CHAIN of walk frames handing state through the pooled
    /// slots; a single tail layer (the canonical shape) never chains and
    /// its device attention is measured exact.
    pub split_deep: bool,
}

impl Dsv4State {
    pub fn new(layers: usize) -> Self {
        use std::sync::atomic::{AtomicU64, Ordering};
        static NEXT: AtomicU64 = AtomicU64::new(1);
        Self {
            kv_id: NEXT.fetch_add(1, Ordering::Relaxed),
            dev_filled: vec![0; layers],
            dev_n_comp: vec![0; layers],
            dev_n_ix: vec![0; layers],
            dev_owned: false,
            dev_set: Vec::new(),
            partial_set: Vec::new(),
            split_deep: false,
            window: vec![Vec::new(); layers],
            compressed: vec![Vec::new(); layers],
            index_kv: vec![Vec::new(); layers],
            pending_kv: vec![Vec::new(); layers],
            pending_score: vec![Vec::new(); layers],
            prev_kv: vec![Vec::new(); layers],
            prev_score: vec![Vec::new(); layers],
            pending_ix_kv: vec![Vec::new(); layers],
            pending_ix_score: vec![Vec::new(); layers],
            prev_ix_kv: vec![Vec::new(); layers],
            prev_ix_score: vec![Vec::new(); layers],
            pos: 0,
        }
    }
}

/// One attention block for a single position. `hidden` is the folded,
/// normalized vector `hc_block` hands over; the result goes back to it.
///
/// The order matters and is the reference's: q through the LoRA pair with
/// a normalization at each end, kv compressed to one head's width, rope on
/// the tails, the window and the compressed positions concatenated into
/// one index list, sparse attention with the sink, the INVERSE rope on the
/// output, then the grouped low-rank projection.
#[allow(clippy::too_many_arguments)]
/// Advance one compressor by a token and return its folded entry when the
/// window closes. Both the attention compressor and the indexer's own run
/// through here — the indexer's was simply never called, so its cache stayed
/// empty and every layer that has an indexer selected ZERO compressed
/// positions, discarding a correctly-built long-range memory.
#[allow(clippy::too_many_arguments)]
fn compressor_step(
    cp: &Dsv4Compressor,
    hidden: &[f32],
    pos: usize,
    rd: usize,
    norm_eps: f32,
    inv_freq: &[f32],
    pool: Option<&crate::pool::Pool>,
    pending_kv: &mut Vec<f32>,
    pending_score: &mut Vec<f32>,
    prev_kv: &mut Vec<f32>,
    prev_score: &mut Vec<f32>,
) -> Option<Vec<f32>> {
    let width = cp.wkv.rows();
    let ew = if cp.overlap { width / 2 } else { width };
    let mut ckv = vec![0.0f32; width];
    let mut cscore = vec![0.0f32; width];
    // Same input, so one dispatch instead of two — and this runs twice a
    // layer (the compressor and the indexer's own), 43 layers a token.
    crate::qtensor::QTensor::matvec_many(
        [&cp.wkv, &cp.wgate],
        hidden,
        [&mut ckv, &mut cscore],
        pool,
    );
    if cp.overlap {
        // The reference biases the score as the token arrives and keeps it
        // biased across the shift, so ape is added ONCE, here.
        let slot = pos % cp.ratio;
        for (c, a) in cscore
            .iter_mut()
            .zip(&cp.ape[slot * width..(slot + 1) * width])
        {
            *c += a;
        }
    }
    pending_kv.extend_from_slice(&ckv);
    pending_score.extend_from_slice(&cscore);
    if pending_kv.len() / width < cp.ratio {
        return None;
    }
    let mut folded = vec![0.0f32; ew];
    if cp.overlap {
        compress_window_overlap(
            prev_kv,
            prev_score,
            pending_kv,
            pending_score,
            cp.ratio,
            ew,
            &mut folded,
        );
        *prev_kv = std::mem::take(pending_kv);
        *prev_score = std::mem::take(pending_score);
    } else {
        compress_window(
            pending_kv,
            pending_score,
            &cp.ape,
            cp.ratio,
            width,
            &mut folded,
        );
    }
    rms_weighted(&mut folded, &cp.norm, norm_eps);
    // The entry carries the same rope-tagged tail as a window key, at the
    // position of the window's first token.
    rope_tail(&mut folded, inv_freq, pos + 1 - cp.ratio, rd, false);
    pending_kv.clear();
    pending_score.clear();
    Some(folded)
}

/// `CMF_DSV4_PROFILE=1` accumulates wall time per stage and prints the split
/// when the process ends. Guessing which half of a layer costs what is how
/// one ends up optimising the cheap one: the fused attention block came out a
/// wash on the release checkpoint, and no amount of reasoning about MAC
/// counts settles whether that is because attention was already cheap or
/// because the device arm was slow.
pub(crate) mod prof {
    use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};

    pub static ATTN_NS: AtomicU64 = AtomicU64::new(0);
    pub static MOE_NS: AtomicU64 = AtomicU64::new(0);
    pub static CALLS: AtomicU64 = AtomicU64::new(0);
    /// Everything in a layer that is neither attention nor the experts: the
    /// hyper-connection fold and expand, the two norms, the residual.
    pub static HC_NS: AtomicU64 = AtomicU64::new(0);
    /// The head: final norm plus lm_head over 129280 rows.
    pub static HEAD_NS: AtomicU64 = AtomicU64::new(0);
    /// The whole forward, so the buckets can be checked against a total
    /// instead of against a guess. 78 ms of measured work in a 108 ms token
    /// left 30 ms that no counter had ever looked at.
    pub static ALL_NS: AtomicU64 = AtomicU64::new(0);
    pub static TOKENS: AtomicU64 = AtomicU64::new(0);

    /// One token = one visit to layer zero. Counting `moe_step` calls instead
    /// counts layers.
    pub fn note_layer(li: usize) {
        CALLS.fetch_add(1, Ordering::Relaxed);
        if li == 0 {
            // The first token pays for the whole expert set reaching the card
            // — tens of seconds of it. Left in, that one-time cost is divided
            // by every later call and reads as a per-call price: it is what
            // made "the host encodes for 4.45 ms a layer" out of an upload
            // that happens once. Everything measured before the SECOND token
            // starts is therefore thrown away, and the report describes
            // steady state, which is the only thing worth optimising.
            // `swap` and not a TOKENS comparison: resetting TOKENS to 1 made
            // the test true again on every later token, so the report
            // described one token instead of the run.
            if TOKENS.fetch_add(1, Ordering::Relaxed) == 1 && !ZEROED.swap(true, Ordering::Relaxed)
            {
                for a in [&ATTN_NS, &MOE_NS, &HC_NS, &HEAD_NS, &ALL_NS, &CALLS] {
                    a.store(0, Ordering::Relaxed);
                }
                TOKENS.store(1, Ordering::Relaxed);
                #[cfg(feature = "gpu")]
                for a in [
                    &crate::gpu_wgpu::MOE_ENC_NS,
                    &crate::gpu_wgpu::MOE_WAIT_NS,
                    &crate::gpu_wgpu::MOE_BUFS_NS,
                    &crate::gpu_wgpu::MOE_UP_NS,
                    &crate::gpu_wgpu::MOE_PASS_NS,
                    &crate::gpu_wgpu::ATT_ENC_NS,
                    &crate::gpu_wgpu::ATT_WAIT_NS,
                    &crate::gpu_wgpu::CHAIN_ENC_NS,
                    &crate::gpu_wgpu::CHAIN_WAIT_NS,
                    &crate::gpu_wgpu::CHAIN_LAYERS,
                    &crate::gpu_wgpu::CHAIN_RUNS,
                    &crate::gpu_wgpu::SUBMITS,
                    &crate::gpu_wgpu::PASSES,
                ] {
                    a.store(0, Ordering::Relaxed);
                }
            }
        }
    }
    static REPORT: AtomicBool = AtomicBool::new(false);
    /// The one-time "drop the first token's numbers" latch.
    static ZEROED: AtomicBool = AtomicBool::new(false);

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

    /// Print once, from wherever the last caller happens to be — a process
    /// that exits through several paths would otherwise report zero or twice.
    pub fn report() {
        if !on() || REPORT.swap(true, Ordering::Relaxed) {
            return;
        }
        // CALLS counts layer visits, not tokens — dividing by it and calling
        // the result "per token" is off by the layer count, which is 43 on
        // the release and reads as a plausible number either way.
        let calls = CALLS.load(Ordering::Relaxed).max(1);
        let toks = TOKENS.load(Ordering::Relaxed).max(1);
        let (a, m) = (
            ATTN_NS.load(Ordering::Relaxed) as f64 / 1e6,
            MOE_NS.load(Ordering::Relaxed) as f64 / 1e6,
        );
        let all = ALL_NS.load(Ordering::Relaxed) as f64 / 1e6;
        // HC_NS wraps the FFN half's hc_block WHOLE, and moe_step runs
        // inside that block — so the raw counter double-counts every MoE
        // millisecond as hyper-connection time. Reported as the difference:
        // the glue alone. (This inflation is what made moving the
        // hyper-connections to the card look like a 19 ms win when the glue
        // is ~4.)
        let hc = (HC_NS.load(Ordering::Relaxed) as f64 / 1e6
            - MOE_NS.load(Ordering::Relaxed) as f64 / 1e6)
            .max(0.0);
        let hd = HEAD_NS.load(Ordering::Relaxed) as f64 / 1e6;
        eprintln!(
            "[dsv4-профиль] {calls} вызовов слоя за {toks} токенов | \
             на токен: внимание {:.0} мс, MoE {:.0} мс, гипер-связи+нормы {:.0} мс, \
             голова {:.0} мс | на вызов: внимание {:.2}, MoE {:.2}, связи {:.2}",
            a / toks as f64,
            m / toks as f64,
            hc / toks as f64,
            hd / toks as f64,
            a / calls as f64,
            m / calls as f64,
            hc / calls as f64,
        );
        eprintln!(
            "[dsv4-профиль] весь проход {:.0} мс на токен; вне счётчиков {:.0} мс",
            all / toks as f64,
            (all - a - m - hd) / toks as f64,
        );
        #[cfg(feature = "gpu")]
        {
            let ae = crate::gpu_wgpu::ATT_ENC_NS.load(Ordering::Relaxed) as f64 / 1e6;
            let aw = crate::gpu_wgpu::ATT_WAIT_NS.load(Ordering::Relaxed) as f64 / 1e6;
            if ae + aw > 0.0 {
                eprintln!(
                    "[dsv4-профиль] кадр внимания на вызов: кодирование {:.2} мс, \
                     отправка и ожидание {:.2} мс",
                    ae / calls as f64,
                    aw / calls as f64,
                );
            }
            // At the OUTER level on purpose: this used to sit inside the MoE
            // frame's own report, and the chain does not use the MoE frame —
            // so the one number that says where a chained token goes was
            // printed only when the chain was not running.
            let ub = crate::gpu_wgpu::UPLOAD_BYTES.load(Ordering::Relaxed);
            let un = crate::gpu_wgpu::UPLOAD_NS.load(Ordering::Relaxed);
            if ub > 0 && un > 0 {
                eprintln!(
                    "[dsv4-профиль] ЗАЛИВКА весов: {:.1} ГБ за {:.1} с ({:.0} МБ/с)",
                    ub as f64 / 1e9,
                    un as f64 / 1e9,
                    ub as f64 / (un as f64 / 1e9) / 1e6,
                );
            }
            let sub = crate::gpu_wgpu::SUBMITS.load(Ordering::Relaxed);
            if sub > 0 {
                eprintln!(
                    "[dsv4-профиль] ОТПРАВОК на карту: {:.1} на токен, ПРОХОДОВ {:.0} \
                     ({:.1} на слой)",
                    sub as f64 / toks as f64,
                    crate::gpu_wgpu::PASSES.load(Ordering::Relaxed) as f64 / toks as f64,
                    crate::gpu_wgpu::PASSES.load(Ordering::Relaxed) as f64 / calls as f64,
                );
            }
            let cl = crate::gpu_wgpu::CHAIN_LAYERS.load(Ordering::Relaxed);
            if cl > 0 {
                let toks2 = toks.max(1) as f64;
                eprintln!(
                    "[dsv4-профиль] ЦЕПОЧКА на токен: кодирование {:.2} мс, \
                     ожидание {:.2} мс ({} слоёв, {} отправок)",
                    crate::gpu_wgpu::CHAIN_ENC_NS.load(Ordering::Relaxed) as f64 / 1e6 / toks2,
                    crate::gpu_wgpu::CHAIN_WAIT_NS.load(Ordering::Relaxed) as f64 / 1e6 / toks2,
                    cl / toks.max(1),
                    crate::gpu_wgpu::CHAIN_RUNS.load(Ordering::Relaxed) / toks.max(1),
                );
            }
            let e = crate::gpu_wgpu::MOE_ENC_NS.load(Ordering::Relaxed) as f64 / 1e6;
            let wt = crate::gpu_wgpu::MOE_WAIT_NS.load(Ordering::Relaxed) as f64 / 1e6;
            if e + wt > 0.0 {
                let ns = |a: &std::sync::atomic::AtomicU64| {
                    a.load(Ordering::Relaxed) as f64 / 1e6 / calls as f64
                };
                eprintln!(
                    "[dsv4-профиль] кадр MoE на вызов: кодирование {:.2} мс, \
                     отправка и ожидание {:.2} мс",
                    e / calls as f64,
                    wt / calls as f64,
                );
                let an = crate::gpu_wgpu::ATT_GPU_N.load(Ordering::Relaxed);
                if an > 0 {
                    let g = |i: usize| {
                        crate::gpu_wgpu::ATT_GPU_NS[i].load(Ordering::Relaxed) as f64
                            / 1e6 / an as f64
                    };
                    eprintln!(
                        "[dsv4-профиль]   ВНИМАНИЕ НА КАРТЕ на вызов: одиночное {:.3} мс, \
                         оценки {:.3} мс, применение {:.3} мс",
                        g(0), g(1), g(2),
                    );
                }
                let gn = crate::gpu_wgpu::MOE_GPU_N.load(Ordering::Relaxed);
                let gns = crate::gpu_wgpu::MOE_GPU_NS[0].load(Ordering::Relaxed);
                if gn > 0 && gns > 0 {
                    eprintln!(
                        "[dsv4-профиль]   MoE НА КАРТЕ: {:.3} мс на вызов ({gn} замеров)",
                        gns as f64 / 1e6 / gn as f64,
                    );
                } else if gn > 0 {
                    // Zero across thousands of samples is a broken query, not
                    // an instant kernel, and printing it as a time is how a
                    // profile starts lying.
                    eprintln!(
                        "[dsv4-профиль]   MoE НА КАРТЕ: метки вернули НОЛЬ на {gn} замерах — \
                         запрос времени не сработал, число не использовать"
                    );
                }
                eprintln!(
                    "[dsv4-профиль]   из кодирования: буферы экспертов {:.2} мс, \
                     загрузки {:.2} мс, проходы {:.2} мс",
                    ns(&crate::gpu_wgpu::MOE_BUFS_NS),
                    ns(&crate::gpu_wgpu::MOE_UP_NS),
                    ns(&crate::gpu_wgpu::MOE_PASS_NS),
                );
            }
        }
    }
}

/// Print the per-token split, if `CMF_DSV4_PROFILE` asked for one.
pub fn profile_report() {
    prof::report();
}

/// `CMF_DSV4_GPU_ATTN=1` moves the attention block onto the device as one
/// submission. Off by default: it needs every attention weight in q4tp and a
/// working wgpu context, and a frame that declines mid-layer after the state
/// has been advanced would be worse than one that never ran.
fn gpu_attn_enabled() -> bool {
    #[cfg(feature = "gpu")]
    {
        use std::sync::OnceLock;
        static ON: OnceLock<bool> = OnceLock::new();
        *ON.get_or_init(|| {
            let want = std::env::var("CMF_DSV4_GPU_ATTN")
                .map(|v| v != "0")
                .unwrap_or(true);
            let have = want && crate::gpu::backend_available();
            if want && !have && std::env::var("CMF_DSV4_GPU_ATTN").is_ok() {
                tracing::warn!(
                    "CMF_DSV4_GPU_ATTN задан, но устройства нет — блок внимания                      остаётся на CPU. Проверьте CMF_GPU=wgpu и Vulkan-ICD."
                );
            }
            if std::env::var("CMF_DSV4_FRAME_DEBUG").is_ok() {
                eprintln!("кадр dsv4: запрошен={want} доступен={have}");
            }
            have
        })
    }
    #[cfg(not(feature = "gpu"))]
    {
        false
    }
}

/// The device half of `attention_step`. Returns false — having changed
/// nothing — whenever it cannot do the whole block, so the caller's CPU path
/// is still correct to run.
#[cfg(feature = "gpu")]
#[allow(clippy::too_many_arguments)]
fn attn_frame(
    l: &Dsv4Layer,
    cfg: &Dsv4Cfg,
    st: &Dsv4State,
    li: usize,
    hidden: &[f32],
    qn: &[f32],
    idxs: &[usize],
    inv_freq: &[f32],
    pos: usize,
    win_len: usize,
    scale: f32,
    // Present: the frame also does this layer's hyper-connection handover
    // and leaves the MoE half's input on the card. `out` may then be empty.
    hc: Option<&crate::gpu_wgpu::Dsv4HcTail>,
    out: &mut [f32],
) -> bool {
    let hd = cfg.head_dim;
    let (Some(wq_a), Some(wq_b), Some(wo_a), Some(wo_b)) = (
        l.wq_a.model_idx(),
        l.wq_b.model_idx(),
        l.wo_a.model_idx(),
        l.wo_b.model_idx(),
    ) else {
        return false;
    };
    let Some(model) = l.wq_b.model_arc() else {
        return false;
    };
    // Fixed window region, then the compressed tail — so a token writes one
    // window slot's worth of movement and whatever the compressor just added,
    // not the whole cache. `cap` has to cover the longest run this sequence
    // will reach; the compressed axis grows by one entry per `ratio` tokens.
    let n_comp = st.compressed[li].len() / hd;
    let cap = (cfg.window + n_comp.next_power_of_two().max(64)) * hd;
    let kv_id = st.kv_id;
    // The window is rewritten whole. A ring would write one slot instead of
    // 128 — 2 KB against 256 — and was tried: it bought NOTHING (the cost is
    // per-dispatch driver bookkeeping, not the copy) and moved perplexity by
    // 6e-5 because the attended positions arrive in a different order and the
    // softmax accumulates differently. Not a trade worth making.
    if !crate::gpu_wgpu::dsv4_cache_write(kv_id, li, 0, &st.window[li], cap) {
        return false;
    }
    // The compressed axis only ever grows, so write the TAIL. Rewriting it
    // whole was 22 MB a token at 1024 positions — the cache write, not the
    // arithmetic, was what the attention block had left to pay.
    // The compressed tail is written WHOLE every token. Writing only the new
    // part was tried and gave nothing measurable, and the bookkeeping it
    // needs — a per-layer tail count invalidated by every buffer growth — is
    // exactly the kind of state that drifts silently and shows up as a model
    // that stops early. Not worth carrying for zero.
    if n_comp > 0
        && !crate::gpu_wgpu::dsv4_cache_write(
            kv_id,
            li,
            cfg.window * hd,
            &st.compressed[li],
            cap,
        )
    {
        return false;
    }
    let idx32: Vec<u32> = idxs
        .iter()
        .map(|&p| {
            if p < win_len {
                p as u32
            } else {
                (cfg.window + (p - win_len)) as u32
            }
        })
        .collect();
    let w = crate::gpu_wgpu::Dsv4AttnW {
        wq_a,
        wq_b,
        wo_a,
        wo_b,
        q_norm: &l.q_norm,
        sink: &l.attn_sink,
    };
    let g = crate::gpu_wgpu::Dsv4AttnGeom {
        dim: cfg.dim,
        nh: cfg.n_heads,
        hd,
        rd: cfg.rope_head_dim,
        q_lora: cfg.q_lora_rank,
        o_lora: cfg.o_lora_rank,
        o_groups: cfg.o_groups,
        eps: cfg.norm_eps,
        scale,
    };
    // The host fold, explicitly. The frame used to read this half's input
    // from the pooled x2 slot — which a device MoE frame of the SAME layer
    // overwrites each token with the NEXT layer's input, so the second
    // token of any chain+partial configuration attended over garbage
    // (perplexity 5.3 against the 4.578 gold on every budget small enough
    // to split a layer). The host has the exact vector either way; one
    // hidden-width upload per call is what correctness costs.
    crate::gpu_wgpu::dsv4_attn_frame(
        &model, &w, g, hidden, Some(qn), kv_id, li, &idx32, inv_freq, pos, hc, out,
    )
}

/// What the host still owes the device before a layer frame can run: the
/// shared LoRA vector the indexer reads, and the attended position list.
#[derive(Default)]
pub struct AttnPrep {
    pub qr: Vec<f32>,
    pub idxs: Vec<usize>,
    pub win_len: usize,
}

#[allow(clippy::too_many_arguments)]
pub fn attention_step(
    hidden: &[f32],
    l: &Dsv4Layer,
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    li: usize,
    // Chosen by the caller from the layer's kind — see Dsv4Globals.
    inv_freq: &[f32],
    pool: Option<&crate::pool::Pool>,
    // When set, stop once the caches are advanced and the index list is
    // built, and hand those back instead of running attention: the layer
    // frame does the rest on the device.
    prep_out: Option<&mut AttnPrep>,
    out: &mut [f32],
) {
    let _t0 = prof::on().then(std::time::Instant::now);
    let _guard = scopeguard_attn(_t0);
    let (hd, rd) = (cfg.head_dim, cfg.rope_head_dim);
    let pos = st.pos;
    if std::env::var("CMF_FREQ_DEBUG").is_ok() && li == 0 && pos == 0 {
        eprintln!(
            "    [порт] rd={rd} частот={} inv_freq[0..4]={:?}",
            inv_freq.len(),
            &inv_freq[..4.min(inv_freq.len())]
        );
    }

    // ── q and kv: both read the same hidden state, so they go out as ONE
    // dispatch. The norms after them differ, and they stay separate.
    // (q: wq_a → q_norm → wq_b → per-head norm → rope tail;
    //  kv: one head's width, shared by every query head.)
    let mut qr = vec![0.0f32; cfg.q_lora_rank];
    let mut kv = vec![0.0f32; hd];
    crate::qtensor::QTensor::matvec_many([&l.wq_a, &l.wkv], hidden, [&mut qr, &mut kv], pool);
    rms_weighted(&mut qr, &l.q_norm, cfg.norm_eps);
    // The queries are built further down, after the frame has had its chance
    // at the whole block. `qr` is needed either way: the indexer reads it.
    // A PARTIAL layer walks its attention on the host. Its device MoE
    // frame refills the pooled walk slots (x2, the hyper-connection state)
    // each token with the NEXT layer's values, so the same layer's device
    // attention frame attends over the previous token's leftovers on the
    // second token — measured as perplexity 5.3 against the 4.578 gold on
    // every budget small enough to split a layer, and exact the moment
    // that one layer's attention walks on the host. Layers whose MoE runs
    // on the HOST keep their device attention: nothing refills their
    // slots mid-walk, and the MAX_LI ladder measures them bit-exact.
    // …and it spreads: the partial layer's MoE frame cycles slots that the
    // FOLLOWING host-MoE layers' device attention also reads, so in any
    // configuration that holds a partial layer, every layer past the chain
    // prefix walks its attention on the host. A configuration with no
    // partial layer keeps device attention everywhere — the canonical
    // stand and the MAX_LI ladder both measure that bit-exact.
    let split_config = st.partial_set.iter().any(|&p| p) && st.split_deep;
    let past_chain = st.dev_owned
        && (li >= st.dev_set.len() || !st.dev_set.get(li).copied().unwrap_or(false));
    if std::env::var("CMF_DSV4_GATE_DBG").is_ok() {
        eprintln!(
            "[gate] li={li} pos={} split={split_config} past={past_chain} dev_owned={} set_len={} part_len={}",
            st.pos, st.dev_owned, st.dev_set.len(), st.partial_set.len()
        );
    }
    let on_gpu = gpu_attn_enabled() && !(split_config && past_chain);

    rms_weighted(&mut kv, &l.kv_norm, cfg.norm_eps);
    rope_tail(&mut kv, inv_freq, pos, rd, false);

    // ── the compressor: accumulate `ratio` tokens, then fold them into
    // one compressed entry. The reference fires when (pos+1) % ratio == 0,
    // so a partial window simply waits — which is why the state carries
    // the pending streams across tokens.
    if let Some(cp) = &l.compressor {
        let mut pk = std::mem::take(&mut st.pending_kv[li]);
        let mut ps = std::mem::take(&mut st.pending_score[li]);
        let mut qk = std::mem::take(&mut st.prev_kv[li]);
        let mut qs = std::mem::take(&mut st.prev_score[li]);
        let entry = compressor_step(
            cp,
            hidden,
            pos,
            rd,
            cfg.norm_eps,
            inv_freq,
            pool,
            &mut pk,
            &mut ps,
            &mut qk,
            &mut qs,
        );
        st.pending_kv[li] = pk;
        st.pending_score[li] = ps;
        st.prev_kv[li] = qk;
        st.prev_score[li] = qs;
        if let Some(e) = entry {
            st.compressed[li].extend_from_slice(&e);
        }
    }
    // The indexer scores against ITS OWN compressed cache, built by its own
    // compressor. Without this the cache is empty, `n_ix` is zero, and every
    // indexer layer picks no compressed positions at all — the long-range
    // memory is built and then never read.
    if let Some(ix) = &l.indexer {
        let mut pk = std::mem::take(&mut st.pending_ix_kv[li]);
        let mut ps = std::mem::take(&mut st.pending_ix_score[li]);
        let mut qk = std::mem::take(&mut st.prev_ix_kv[li]);
        let mut qs = std::mem::take(&mut st.prev_ix_score[li]);
        let entry = compressor_step(
            &ix.compressor,
            hidden,
            pos,
            rd,
            cfg.norm_eps,
            inv_freq,
            pool,
            &mut pk,
            &mut ps,
            &mut qk,
            &mut qs,
        );
        st.pending_ix_kv[li] = pk;
        st.pending_ix_score[li] = ps;
        st.prev_ix_kv[li] = qk;
        st.prev_ix_score[li] = qs;
        if let Some(e) = entry {
            st.index_kv[li].extend_from_slice(&e);
        }
    }

    st.window[li].extend_from_slice(&kv);
    // The reference keeps the window in a ring of `window_size`; holding the
    // last N in order is the same set, and without this the "window" grows
    // for the whole generation — wrong attention AND unbounded memory.
    let cap = cfg.window * hd;
    if st.window[li].len() > cap {
        let drop = st.window[li].len() - cap;
        st.window[li].drain(..drop);
    }
    let win_len = st.window[li].len() / hd;
    let n_pos = win_len + st.compressed[li].len() / hd;

    // Index list: every window position, plus whatever the indexer picked
    // (or, without an indexer, every compressed position).
    //
    // CMF_DSV4_NO_COMPRESSED=1 attends to the sliding window ALONE. That is
    // not a mode anyone should serve — it drops the model's long-range
    // memory — but it separates two failure modes that look identical from
    // the outside: output that degrades because the compressed path is
    // wrong, and output that degrades because the weights are too coarse.
    let mut idxs: Vec<usize> = (0..win_len).collect();
    if !st.compressed[li].is_empty() && !no_compressed() {
        let n_comp = st.compressed[li].len() / hd;
        match &l.indexer {
            Some(ix) => {
                // The indexer scores from the SHARED LoRA output through
                // its own wq_b — not from attention's queries — and its
                // per-head weights are a projection of the hidden state,
                // scaled by head_dim^-0.5 * n_heads^-0.5 as the reference
                // folds into `weights_proj`'s output.
                //
                // The reference also applies a randomized Hadamard rotation
                // to the queries here and to the keys in the indexer's
                // compressor, then simulates FP4 on both. That transform is
                // orthogonal (`hadamard_transform` scaled by d^-0.5) and it
                // hits BOTH sides of the same dot product, so it cancels:
                // its purpose is to condition the FP4 quantization, which we
                // do not do either. Omitting the pair is exact, and keeping
                // f32 is strictly more accurate than the reference — not an
                // approximation to be fixed later.
                let ih = ix.weights_proj.rows();
                let idim = ix.wq_b.rows() / ih.max(1);
                let mut qi = vec![0.0f32; ix.wq_b.rows()];
                ix.wq_b.matvec(&qr, &mut qi, pool);
                for h in 0..ih {
                    rope_tail(&mut qi[h * idim..(h + 1) * idim], inv_freq, pos, rd, false);
                }
                let mut hw = vec![0.0f32; ih];
                ix.weights_proj.matvec(hidden, &mut hw, pool);
                let sc_factor = (idim as f32).powf(-0.5) * (ih as f32).powf(-0.5);
                for w in hw.iter_mut() {
                    *w *= sc_factor;
                }
                let n_ix = st.index_kv[li].len() / idim.max(1);
                let mut sc = Vec::new();
                index_scores(
                    &qi,
                    &st.index_kv[li],
                    &hw,
                    ih,
                    idim,
                    n_ix.min(n_comp),
                    n_ix.min(n_comp),
                    pool,
                    &mut sc,
                );
                let mut picked = Vec::new();
                top_k_positions(&sc, cfg.index_topk, &mut picked);
                idxs.extend(picked.into_iter().map(|p| win_len + p));
            }
            None => idxs.extend((0..n_comp).map(|p| win_len + p)),
        }
    }
    debug_assert!(idxs.iter().all(|&p| p < n_pos));
    if let Some(p) = prep_out {
        p.qr = qr;
        p.idxs = idxs;
        p.win_len = win_len;
        return;
    }

    // ── the whole block on the device, or nothing ──
    let scale = (hd as f32).powf(-0.5);
    #[cfg(feature = "gpu")]
    if on_gpu
        && {
            if std::env::var("CMF_DSV4_XCHK").is_ok() {
                // The frame reads this half's input from the card's x2
                // slot; the host walked its own. Disagreement = the
                // chain→walk handoff, and the number says by how much.
                if let Some(card) = crate::gpu_wgpu::dsv4_dbg_read_tag(45, 0, hidden.len()) {
                    let md = hidden
                        .iter()
                        .zip(card.iter())
                        .map(|(a, b)| (a - b).abs())
                        .fold(0.0f32, f32::max);
                    eprintln!("[xchk] li={li} pos={pos} x2 maxdiff={md:.3e}");
                }
            }
            true
        }
        && attn_frame(
            l, cfg, st, li, hidden, &qr, &idxs, inv_freq, pos, win_len, scale, None, out,
        )
    {
        return;
    }

    // ── queries: wq_b, then a norm and the rope tail per head ──
    let mut q = vec![0.0f32; cfg.n_heads * hd];
    l.wq_b.matvec(&qr, &mut q, pool);
    for h in 0..cfg.n_heads {
        let head = &mut q[h * hd..(h + 1) * hd];
        rms_inplace(head, cfg.norm_eps);
        rope_tail(head, inv_freq, pos, rd, false);
    }
    let mut cache: Vec<f32> = st.window[li].clone();
    cache.extend_from_slice(&st.compressed[li]);

    // ── sparse attention per head, then the inverse rope ──
    let mut attn = vec![0.0f32; cfg.n_heads * hd];
    for h in 0..cfg.n_heads {
        let qh = &q[h * hd..(h + 1) * hd];
        // Straight into this head's slice of the output: the scratch vector
        // that used to sit here was an allocation and a copy per head, so 64
        // of each per layer per token, for a value that was never read
        // anywhere else.
        let oh = &mut attn[h * hd..(h + 1) * hd];
        sparse_attend(qh, &cache, &idxs, l.attn_sink[h], scale, hd, oh);
        rope_tail(oh, inv_freq, pos, rd, true);
    }

    // ── grouped low-rank output ──
    // Read the two blocks through the quantized readers. Materializing them
    // here instead costs ~270 MB of dequantization per layer per token on
    // the release checkpoint (wo_a and wo_b are 33M weights each), which is
    // the difference between decoding and not.
    o_project(
        &attn,
        &|r, x, sc| l.wo_a.row_dot(r, x, sc),
        l.wo_a.cols(),
        &|mid, dst| l.wo_b.matvec(mid, dst, pool),
        cfg.o_groups,
        cfg.o_lora_rank,
        pool,
        out,
    );
}

/// RMSNorm with a learned weight, in place.
pub fn rms_weighted(v: &mut [f32], w: &[f32], eps: f32) {
    let ms = v.iter().map(|x| x * x).sum::<f32>() / v.len() as f32;
    let inv = 1.0 / (ms + eps).sqrt();
    for (x, g) in v.iter_mut().zip(w) {
        *x = *x * inv * g;
    }
}

/// The MoE half of a block: route, run the chosen experts plus the shared
/// one, and sum. `token_id` is only read on the hash layers.
/// Per-layer expert-selection counts, the routing field a task-conditional
/// expert set is derived from (`CMF_MOE_STATS`). The generic MoE path keeps
/// these on its `MoeFfn`; this architecture has its own experts and never
/// touches that struct, so without this the field cannot be recorded for
/// DeepSeek-V4 at all — and its hash layers already make defrag useless, so
/// the only interesting question is what the OTHER forty layers do.
///
/// Decode drives this from one thread; the pool parallelizes inside the
/// matvecs, below this point.
thread_local! {
    static ROUTE_COUNTS: std::cell::RefCell<Vec<Vec<u64>>> =
        const { std::cell::RefCell::new(Vec::new()) };
}

fn route_stats_on() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_MOE_STATS").is_ok())
}

fn record_route(li: usize, n_layers_hint: usize, n_experts: usize, idx: &[usize]) {
    ROUTE_COUNTS.with(|c| {
        let mut c = c.borrow_mut();
        if c.len() <= li.max(n_layers_hint) {
            c.resize(li.max(n_layers_hint) + 1, Vec::new());
        }
        let row = &mut c[li];
        if row.len() < n_experts {
            row.resize(n_experts, 0);
        }
        for &e in idx {
            if e < row.len() {
                row[e] += 1;
            }
        }
    });
}

/// Take the recorded routing field, leaving the counters empty.
pub fn take_route_counts() -> Vec<Vec<u64>> {
    ROUTE_COUNTS.with(|c| std::mem::take(&mut *c.borrow_mut()))
}

/// Charge elapsed time to a counter when it goes out of scope — the two
/// steps have several early returns each, and a timer that only stops on the
/// long path measures the short one as free.
struct Charge(Option<std::time::Instant>, &'static std::sync::atomic::AtomicU64);
impl Drop for Charge {
    fn drop(&mut self) {
        if let Some(t) = self.0 {
            self.1.fetch_add(
                t.elapsed().as_nanos() as u64,
                std::sync::atomic::Ordering::Relaxed,
            );
        }
    }
}
fn scopeguard_attn(t: Option<std::time::Instant>) -> Charge {
    Charge(t, &prof::ATTN_NS)
}
fn scopeguard_moe(t: Option<std::time::Instant>, li: usize) -> Charge {
    if t.is_some() {
        prof::note_layer(li);
    }
    Charge(t, &prof::MOE_NS)
}

/// The whole token, one submission per layer. Returns false having changed
/// nothing if the device declines any layer — the caller's loop is then still
/// correct to run.
#[cfg(feature = "gpu")]
#[allow(clippy::too_many_arguments)]
fn dsv4_layer_loop(
    state: &mut [f32],
    layers: &[Dsv4Layer],
    g: &Dsv4Globals,
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    token_id: u32,
    inv_freq: &[f32],
    pool: Option<&crate::pool::Pool>,
    scratch: &mut HcScratch,
) -> bool {
    let dim = cfg.dim;
    let freqs_of = |l: &Dsv4Layer| -> &[f32] {
        let f = if l.compressor.is_some() {
            &g.inv_freq_compress
        } else {
            &g.inv_freq_window
        };
        if f.is_empty() { inv_freq } else { f.as_slice() }
    };
    // PRE-FLIGHT. The prep inside the loop advances the window and the
    // compressor caches, so a refusal halfway leaves state that the CPU
    // fallback would advance a SECOND time — which is not a slow answer but a
    // wrong one. Everything that can decline is therefore asked before the
    // first byte of state moves. The expert upload happens here too, which is
    // where it belonged anyway.
    // The head goes to the card BEFORE the experts ask for room. It is the
    // single most-used tensor in the file — every token reads all of it —
    // and it is a rounding error next to the expert stack: 265 MB against
    // ninety-odd gigabytes on the release. Uploaded in first-touch order it
    // arrived last, after the budget was gone, and stayed on the host for
    // the life of the process.
    {
        static SAID: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
        if !SAID.swap(true, std::sync::atomic::Ordering::Relaxed) {
            if let (Some(idx), Some(model)) = (g.head.model_idx(), g.head.model_arc()) {
                let ok = crate::gpu_wgpu::dsv4_weight_ready(&model, idx);
                tracing::info!("dsv4: голова на карте: {}", if ok { "да" } else { "нет" });
            }
        }
    }
    let mut on_dev = vec![false; layers.len()];
    let mut partial_dev = vec![false; layers.len()];
    for (li, l) in layers.iter().enumerate() {
        if l.wq_a.model_idx().is_none()
            || l.wq_b.model_idx().is_none()
            || l.wo_a.model_idx().is_none()
            || l.wo_b.model_idx().is_none()
        {
            return false;
        }
        let Some(model) = l.experts.first().and_then(|e| e.w1.model_arc()) else {
            return false;
        };
        let gu_q2 = l
            .experts
            .first()
            .is_some_and(|e| e.w1.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP));
        // A layer whose experts do not fit is not a reason to abandon the
        // token: 100 GB of experts against a 98 GB card means SOME layer will
        // always miss. Those run on the host, with the state fetched and put
        // back around them — two transfers for the few that need it.
        // The attention weights have to be asked for too. Experts fill the
        // card first, and a wo_b that misses at layer 11 used to surface as a
        // mid-loop refusal — after the caches had advanced, which the CPU
        // fallback then advanced again.
        // …and, when the layer is to prepare itself, everything that
        // preparation reads: the KV projection, both compressors and the
        // indexer. Leaving them out is how the chain came to refuse ninety
        // times a token on the release — the experts had taken the card by
        // the time `dsv4_encode_prep` asked, and it declined silently into a
        // fallback that looked like "the chain simply does not help".
        let mut want = vec![
            l.wq_a.model_idx(),
            l.wq_b.model_idx(),
            l.wo_a.model_idx(),
            l.wo_b.model_idx(),
        ];
        if chain_enabled() {
            want.push(l.wkv.model_idx());
            if let Some(cp) = &l.compressor {
                want.push(cp.wkv.model_idx());
                want.push(cp.wgate.model_idx());
            }
            if let Some(ix) = &l.indexer {
                want.push(ix.wq_b.model_idx());
                want.push(ix.weights_proj.model_idx());
                want.push(ix.compressor.wkv.model_idx());
                want.push(ix.compressor.wgate.model_idx());
            }
        }
        let attn_ok = want
            .into_iter()
            .flatten()
            .all(|i| crate::gpu_wgpu::dsv4_weight_ready(&model, i));
        // Size the expert pack only AFTER this layer's attention skeleton is
        // resident. Otherwise the pack consumes the apparent free budget,
        // the much smaller skeleton arrives next, and the supposedly fitting
        // pack misses by exactly those bytes.
        let pk = pack_for(l, cfg, li);
        if let Some(pk) = pk {
            let dn_q2 = l
                .experts
                .first()
                .is_some_and(|e| e.w2.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP));
            let experts_ok = crate::gpu_wgpu::dsv4_experts_ready(
                &model,
                &pk.tensors,
                cfg.moe_inter,
                dim,
                gu_q2,
                dn_q2,
            );
            on_dev[li] = attn_ok && experts_ok && pk.globals.len() == cfg.n_routed_experts;
            partial_dev[li] = attn_ok && experts_ok && pk.globals.len() < cfg.n_routed_experts;
        }
    }
    let active_dev: Vec<bool> = on_dev
        .iter()
        .zip(&partial_dev)
        .map(|(&full, &partial)| full || partial)
        .collect();
    if !active_dev.iter().any(|&x| x) {
        return false;
    }
    // The attention gate below needs to know about partial layers BEFORE
    // the decode path commits the device set — a perplexity run only ever
    // prefills, and with this left empty every split budget scored the
    // model wrong (measured; see `attention_step`).
    if st.partial_set.len() != partial_dev.len() || st.partial_set != partial_dev {
        st.partial_set = partial_dev.clone();
        st.split_deep = active_dev
            .iter()
            .zip(&partial_dev)
            .filter(|(a, p)| !**a || **p)
            .count()
            > 1;
    }

    // Which layers the card actually took, said once. A layer that falls to
    // the host costs an order of magnitude more than one that does not, and
    // "the GPU path is on" hid the difference between all of them and most.
    {
        static SAID: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
        if !SAID.swap(true, std::sync::atomic::Ordering::Relaxed) {
            let host: Vec<usize> = active_dev
                .iter()
                .enumerate()
                .filter(|&(_, d)| !*d)
                .map(|(i, _)| i)
                .collect();
            let partial: Vec<(usize, usize)> = partial_dev
                .iter()
                .enumerate()
                .filter(|&(_, d)| *d)
                .filter_map(|(li, _)| pack_for(&layers[li], cfg, li).map(|p| (li, p.globals.len())))
                .collect();
            if host.is_empty() && partial.is_empty() {
                tracing::info!("dsv4: все {} слоёв на карте", on_dev.len());
            } else {
                tracing::info!(
                    "dsv4: {} из {} слоёв используют карту; частичные {:?}; на хосте {:?}",
                    active_dev.len() - host.len(),
                    on_dev.len(),
                    partial,
                    host,
                );
            }
        }
    }

    // Layer zero's opening fold has no frame before it to have prepared it.
    let (mut folded, post0, comb0) = hc_fold_norm(
        state,
        &layers[0].hc_attn_fn,
        &layers[0].hc_attn_scale,
        &layers[0].hc_attn_base,
        &layers[0].attn_norm,
        cfg,
        pool,
    );
    if !crate::gpu_wgpu::dsv4_state_write(state)
        || !crate::gpu_wgpu::dsv4_hc_write(&post0, &comb0)
    {
        return false;
    }
    // The device-owned set must not move once a token has run on it — but
    // the two directions are not the same risk. At a tight budget the set
    // GROWS between tokens as more weights finish uploading, and a layer that
    // merely joined can be left on the host: its caches are there and nothing
    // is inconsistent. Refusing on that was costing the whole fast path once
    // per token — 125 times in a 48-token run on an emulated 24 GB card, on
    // which the engine is slow enough already.
    //
    // A layer LEAVING the set is the dangerous direction: its caches are on
    // the card and the host would advance its own. That still refuses.
    if st.dev_owned && st.dev_set != active_dev {
        let left: Vec<usize> = (0..active_dev.len().min(st.dev_set.len()))
            .filter(|&i| st.dev_set[i] && !active_dev[i])
            .collect();
        if !left.is_empty() {
            tracing::warn!("слои {left:?} ушли с карты — кеши на разных сторонах");
            return false;
        }
        // A layer that was active remains device-owned. Its full/partial mode
        // is still derived from the current pack; only cache ownership is
        // sticky across tokens.
    }
    let chain = chain_enabled();
    // CMF_DSV4_LAYERS_PROBE=N — TIMING ONLY, the answer is garbage. Runs the
    // first N layers and leaves the rest alone. Decode time against N is a
    // line whose SLOPE is the per-layer cost and whose intercept is
    // everything that happens once a token. Unlike the skip probe it does
    // not change what a layer does — which on a MoE model is the difference
    // between a measurement and an artefact, because dropping any stage
    // changes the routing and the routing changes what the experts cost.
    let layer_cap = {
        static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
        *N.get_or_init(|| {
            std::env::var("CMF_DSV4_LAYERS_PROBE")
                .ok()
                .and_then(|v| v.parse::<usize>().ok())
                .unwrap_or(usize::MAX)
        })
    };
    let mut run: Vec<usize> = Vec::new();
    let mut sink_out = vec![0.0f32; dim];
    // `state` starts current on both sides. A device run makes the host copy
    // stale unless that same run carries it home. Tracking this explicitly
    // avoids a separate state fence before a host layer and, for a final host
    // layer, the old upload-immediately-followed-by-readback pair.
    let mut state_on_host = true;
    for (li, l) in layers.iter().enumerate() {
        if li >= layer_cap {
            break;
        }
        // The device path never ticked the profiler, so every per-token
        // number it printed described the two host-path tokens at the start
        // of a run — the ones that also pay for the upload. Ticking here is
        // what makes the chain's encode-and-wait split a per-token figure at
        // all.
        if prof::on() {
            prof::note_layer(li);
        }
        if chain && on_dev[li] {
            // Hash layers used to break the run in two: their forced expert
            // list changes per token, went through the (tag, len) upload
            // pool, and every layer of a submission shared one buffer. The
            // list has a per-layer slot now, so they chain like the rest.
            run.push(li);
            // CMF_DSV4_CHAIN_MAX=N caps a run's length. Diagnostic, not a
            // tuning knob: length-1 runs put ONE layer per submission, which
            // separates "the layer frame is wrong" from "layers in one
            // encoder contaminate each other" in a single ppl run.
            if run.len() >= chain_max() || dspark_wants(li) {
                let need_qn = run[0] == 0 || !on_dev[run[0] - 1];
                let captured = *run.last().unwrap();
                if !dsv4_chain_run(
                    layers, &run, cfg, g, st, token_id, &mut folded,
                    Some(state), 1, &[], need_qn, pool,
                ) {
                    return false;
                }
                state_on_host = true;
                dspark_note(captured, state, cfg);
                run.clear();
            }
            continue;
        }
        if chain && !run.is_empty() {
            // The very next layer is on the host, so bring its state back in
            // the chain's existing readback. Reading it in a second submit
            // below cost one fence per token on the release's 42+1 split.
            if !dsv4_chain_run(
                layers,
                &run,
                cfg,
                g,
                st,
                token_id,
                &mut folded,
                Some(state),
                1,
                &[],
                run[0] == 0 || !on_dev[run[0] - 1],
                pool,
            ) {
                return false;
            }
            state_on_host = true;
            dspark_note(*run.last().unwrap(), state, cfg);
        }
        run.clear();
        if partial_dev[li] && partial_walk_on() {
            // Attention and the resident expert subset stay on the card. The
            // router still sees every expert and returns only the winners
            // that did not fit; those are completed on the CPU and their
            // exact linear contribution is added back to device state.
            let Some(home) = dsv4_partial_layer(
                state,
                &mut folded,
                layers,
                l,
                cfg,
                st,
                token_id,
                li,
                freqs_of(l),
                pool,
            ) else {
                return false;
            };
            state_on_host = home;
            if home {
                dspark_note(li, state, cfg);
            }
            continue;
        }
        if !on_dev[li] {
            if !state_on_host && !crate::gpu_wgpu::dsv4_state_read(state) {
                return false;
            }
            state_on_host = true;
            let freqs = freqs_of(l);
            hc_block(
                state,
                &l.hc_attn_fn,
                &l.hc_attn_scale,
                &l.hc_attn_base,
                &l.attn_norm,
                cfg,
                scratch,
                pool,
                |f, o| attention_step(f, l, cfg, st, li, freqs, pool, None, o),
            );
            hc_block(
                state,
                &l.hc_ffn_fn,
                &l.hc_ffn_scale,
                &l.hc_ffn_base,
                &l.ffn_norm,
                cfg,
                scratch,
                pool,
                // The layer the card had no room for. Its experts are
                // reached one matvec at a time and the probe sends each to
                // the device — right per op, and a fence per op: this one
                // layer is why a token that submits ONCE for 42 layers
                // submits 13 times. CMF_DSV4_HOST_CPU_MOE=1 keeps them on
                // the host instead, trading arithmetic for round trips.
                |f, o| {
                    if host_cpu_moe() {
                        crate::gpu::cpu_scope(|| moe_step(f, l, cfg, token_id, li, pool, o))
                    } else {
                        moe_step(f, l, cfg, token_id, li, pool, o)
                    }
                },
            );
            // Only a following DEVICE layer needs the fold/hc slots and an
            // uploaded state. Consecutive host layers consume `state`
            // directly, and a final host layer is already exactly where the
            // head needs it — uploading then reading it back was pure sync.
            if layers.get(li + 1).is_some() && on_dev.get(li + 1).copied().unwrap_or(false) {
                let n = &layers[li + 1];
                let (f, p2, c2) = hc_fold_norm(
                    state,
                    &n.hc_attn_fn,
                    &n.hc_attn_scale,
                    &n.hc_attn_base,
                    &n.attn_norm,
                    cfg,
                    pool,
                );
                folded = f;
                if !crate::gpu_wgpu::dsv4_hc_write(&p2, &c2) {
                    return false;
                }
                if !crate::gpu_wgpu::dsv4_state_write(state) {
                    return false;
                }
            }
            dspark_note(li, state, cfg);
            continue;
        }
        let mut prep = AttnPrep::default();
        attention_step(
            &folded,
            l,
            cfg,
            st,
            li,
            freqs_of(l),
            pool,
            Some(&mut prep),
            &mut sink_out,
        );
        // The caches the frame will read.
        let hd = cfg.head_dim;
        let n_comp = st.compressed[li].len() / hd;
        let cap = (cfg.window + n_comp.next_power_of_two().max(64)) * hd;
        let kv_id = st.kv_id;
        if !crate::gpu_wgpu::dsv4_cache_write(kv_id, li, 0, &st.window[li], cap)
            || (n_comp > 0
                && !crate::gpu_wgpu::dsv4_cache_write(
                    kv_id,
                    li,
                    cfg.window * hd,
                    &st.compressed[li],
                    cap,
                ))
        {
            return false;
        }
        let idx32: Vec<u32> = prep
            .idxs
            .iter()
            .map(|&p| {
                if p < prep.win_len {
                    p as u32
                } else {
                    (cfg.window + (p - prep.win_len)) as u32
                }
            })
            .collect();
        let Some(pk) = pack_for(l, cfg, li) else {
            return false;
        };
        let (Some(wq_a), Some(wq_b), Some(wo_a), Some(wo_b)) = (
            l.wq_a.model_idx(),
            l.wq_b.model_idx(),
            l.wo_a.model_idx(),
            l.wo_b.model_idx(),
        ) else {
            return false;
        };
        let Some(model) = l.experts.first().and_then(|e| e.w1.model_arc()) else {
            return false;
        };
        let forced: Option<Vec<usize>> = l.tid2eid.as_ref().and_then(|tbl| {
            let v: Vec<usize> = hash_route(tbl, cfg.vocab, cfg.top_k, token_id)
                .into_iter()
                .map(|gi| pk.to_slot[gi])
                .collect();
            if v.iter().any(|&x| x == usize::MAX) {
                None
            } else {
                Some(v)
            }
        });
        if l.tid2eid.is_some() && forced.is_none() {
            return false;
        }
        let nxt = layers.get(li + 1);
        let w = crate::gpu_wgpu::Dsv4LayerW {
            attn: crate::gpu_wgpu::Dsv4AttnW {
                wq_a,
                wq_b,
                wo_a,
                wo_b,
                q_norm: &l.q_norm,
                sink: &l.attn_sink,
            },
            moe: crate::gpu_wgpu::Dsv4MoeW {
                router: &[],
                experts: &pk.tensors,
                logits: &[],
                // The PACK's bias, whose address outlives the process: the
                // frame's const cache is keyed on it, and a per-layer Vec
                // here handed every layer the first layer's — the exact
                // transient-Vec trap the const_buf war story describes,
                // reintroduced by this session and caught because the OFF
                // baseline moved.
                bias: pk.bias.as_deref(),
                forced: forced.as_deref(),
                remap: None,
            },
            hc_ffn_fn: &l.hc_ffn_fn,
            hc_ffn_scale: &l.hc_ffn_scale,
            hc_ffn_base: &l.hc_ffn_base,
            hc_next_fn: nxt.map(|n| n.hc_attn_fn.as_slice()),
            hc_next_scale: nxt.map_or(&l.hc_attn_scale, |n| &n.hc_attn_scale),
            hc_next_base: nxt.map_or(&l.hc_attn_base, |n| n.hc_attn_base.as_slice()),
            ffn_norm: &l.ffn_norm,
            next_norm: nxt.map_or(&l.attn_norm, |n| n.attn_norm.as_slice()),
            next_q_norm: nxt.map_or(&l.q_norm, |n| n.q_norm.as_slice()),
            next_wq_a: nxt.and_then(|n| n.wq_a.model_idx()),
            router: &pk.router,
        };
        let geom = crate::gpu_wgpu::Dsv4LayerGeom {
            attn: crate::gpu_wgpu::Dsv4AttnGeom {
                dim,
                nh: cfg.n_heads,
                hd,
                rd: cfg.rope_head_dim,
                q_lora: cfg.q_lora_rank,
                o_lora: cfg.o_lora_rank,
                o_groups: cfg.o_groups,
                eps: cfg.norm_eps,
                scale: (hd as f32).powf(-0.5),
            },
            moe: crate::gpu_wgpu::Dsv4MoeGeom {
                hidden: dim,
                inter: cfg.moe_inter,
                top_k: cfg.top_k,
                route_scale: cfg.route_scale,
                swiglu_limit: cfg.swiglu_limit,
                gu_q2: l.experts.first().is_some_and(|e| {
                    e.w1.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)
                }),
            },
            hc: cfg.hc_mult,
            hc_eps: cfg.hc_eps,
            sinkhorn_iters: cfg.hc_sinkhorn_iters,
        };
        let mut next = vec![0.0f32; dim];
        if !crate::gpu_wgpu::dsv4_layer_frame(
            &model,
            &w,
            geom,
            kv_id,
            li,
            Some(&prep.qr),
            &idx32,
            freqs_of(l),
            st.pos,
            &mut next,
        ) {
            return false;
        }
        state_on_host = false;
        folded = next;
        dspark_note(li, state, cfg);
    }
    let mut state_home = false;
    if chain {
        if !run.is_empty() {
            // The token's LAST run brings the state back with it. Only the
            // last: an earlier run's state is one the layers after it still
            // change.
            let need_qn = run[0] == 0 || !on_dev[run[0] - 1];
            let last_on_dev = *on_dev.last().unwrap_or(&false);
            let carry = last_on_dev && run.last() == Some(&(layers.len() - 1));
            let ok = if carry {
                let r = dsv4_chain_run(
                    layers, &run, cfg, g, st, token_id, &mut folded,
                    Some(state), 1, &[], need_qn, pool,
                );
                state_home = r;
                state_on_host = r;
                if r {
                    dspark_note(*run.last().unwrap(), state, cfg);
                }
                r
            } else {
                let r = dsv4_chain_run(
                    layers, &run, cfg, g, st, token_id, &mut folded, None, 1, &[], need_qn, pool,
                );
                if r {
                    state_on_host = false;
                }
                r
            };
            if !ok {
                return false;
            }
        }
        if st.dev_set.is_empty() {
            st.dev_set = active_dev.clone();
            st.partial_set = partial_dev.clone();
            // The set is committed, so the card must keep it. Eviction by
            // score is right while the set is still being chosen and wrong
            // afterwards: an evicted layer drops off the card while its
            // caches stay there, and the loop then refuses the whole fast
            // path rather than read state from two sides.
            let mut idxs = Vec::new();
            for (li, l) in layers.iter().enumerate() {
                if !active_dev.get(li).copied().unwrap_or(false) {
                    continue;
                }
                for t in [&l.wq_a, &l.wq_b, &l.wkv, &l.wo_a, &l.wo_b, &l.gate] {
                    idxs.extend(t.model_idx());
                }
                if let Some(pk) = pack_for(l, cfg, li) {
                    for &(a, b, c) in &pk.tensors {
                        idxs.extend([a, b, c]);
                    }
                }
            }
            // Why a HOST layer stayed on the host, said in numbers. Its MoE
            // can still run on the card with a partial pack — `moe_frame` has
            // the remap and hands cold picks back — so the interesting figure
            // is how many experts it got. Zero means the upload order never
            // reached it; a few hundred means the readiness gate refused. The
            // two have different fixes and reading the code cannot tell them
            // apart.
            for (li, l) in layers.iter().enumerate() {
                if active_dev.get(li).copied().unwrap_or(false) {
                    continue;
                }
                let packed = pack_for(l, cfg, li).map_or(0, |p| p.globals.len());
                tracing::info!(
                    "слой {li} на хосте: упаковано {packed} экспертов из {}",
                    cfg.n_routed_experts
                );
            }
            let pinned = layers
                .iter()
                .find_map(|l| l.experts.first().and_then(|e| e.w1.model_arc()))
                .map_or(0, |m| crate::gpu_wgpu::pin_weights(&m, &idxs));
            tracing::info!(
                "закреплено на карте: {pinned} тензоров {} слоёв",
                on_dev.iter().filter(|&&x| x).count()
            );
        }
    }
    if state_home || state_on_host {
        return true;
    }
    crate::gpu_wgpu::dsv4_state_read(state)
}

/// Run a layer whose attention skeleton fits but only a subset of its MoE
/// experts does. This path is selected from the live VRAM budget, never from
/// a layer number. It is exact: routing spans all experts and cold winners
/// are folded back into the hyper-connection state before the next layer.
#[cfg(feature = "gpu")]
#[allow(clippy::too_many_arguments)]
fn dsv4_partial_layer(
    state: &mut [f32],
    folded: &mut Vec<f32>,
    layers: &[Dsv4Layer],
    l: &Dsv4Layer,
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    token_id: u32,
    li: usize,
    freqs: &[f32],
    pool: Option<&crate::pool::Pool>,
) -> Option<bool> {
    let dim = cfg.dim;
    let mut prep = AttnPrep::default();
    let mut sink = vec![0.0f32; dim];
    attention_step(
        folded,
        l,
        cfg,
        st,
        li,
        freqs,
        pool,
        Some(&mut prep),
        &mut sink,
    );
    let hd = cfg.head_dim;
    let n_comp = st.compressed[li].len() / hd;
    let cap = (cfg.window + n_comp.next_power_of_two().max(64)) * hd;
    if !crate::gpu_wgpu::dsv4_cache_write(st.kv_id, li, 0, &st.window[li], cap)
        || (n_comp > 0
            && !crate::gpu_wgpu::dsv4_cache_write(
                st.kv_id,
                li,
                cfg.window * hd,
                &st.compressed[li],
                cap,
            ))
    {
        return None;
    }
    let a_tail = crate::gpu_wgpu::Dsv4HcTail {
        fn_: &l.hc_ffn_fn,
        scale: &l.hc_ffn_scale,
        base: &l.hc_ffn_base,
        norm: &l.ffn_norm,
        hc: cfg.hc_mult,
        sinkhorn_iters: cfg.hc_sinkhorn_iters,
        hc_eps: cfg.hc_eps,
        eps: cfg.norm_eps,
    };
    let scale = (cfg.head_dim as f32).powf(-0.5);
    if !attn_frame(
        l,
        cfg,
        st,
        li,
        folded,
        &prep.qr,
        &prep.idxs,
        freqs,
        st.pos,
        prep.win_len,
        scale,
        Some(&a_tail),
        &mut [],
    ) {
        return None;
    }
    let nxt = layers.get(li + 1);
    let forced = l
        .tid2eid
        .as_ref()
        .map(|tbl| hash_route(tbl, cfg.vocab, cfg.top_k, token_id));
    let mut next = vec![0.0f32; dim];
    let (cold_sum, cold_count) = moe_frame(
        &[],
        l,
        cfg,
        li,
        &[],
        forced.as_deref(),
        pool,
        Some(&a_tail),
        // Do not pre-fold the next layer yet. That fold reuses the canonical
        // `post` slot; a cold correction still needs THIS layer's post. Once
        // the corrected state is home, the exact next fold is cheap on the
        // host and seeds either another partial frame or the next full run.
        None,
        &mut next,
    )?;
    // The resident contribution has already been expanded on the device. If
    // there were cold winners, add `post[j] * cold_sum` and retrieve the
    // corrected state in that submission; otherwise a plain readback is
    // enough. This state handoff is what makes partial layers composable at
    // arbitrary positions, not just at the tail of one checkpoint.
    let state_ok = if cold_count == 0 {
        crate::gpu_wgpu::dsv4_state_read(state)
    } else {
        crate::gpu_wgpu::dsv4_state_add_cold(&cold_sum, cfg.hc_mult, state)
    };
    if !state_ok {
        return None;
    }
    if let Some(n) = nxt {
        let (f, post, comb) = hc_fold_norm(
            state,
            &n.hc_attn_fn,
            &n.hc_attn_scale,
            &n.hc_attn_base,
            &n.attn_norm,
            cfg,
            pool,
        );
        *folded = f;
        if !crate::gpu_wgpu::dsv4_hc_write(&post, &comb)
            || !crate::gpu_wgpu::dsv4_state_write(state)
        {
            return None;
        }
    }
    // NB: the CALLER notes this layer for the draft's ring — a note here
    // as well double-counts the capture and fails `dspark_take`'s
    // completeness check (seen 4 of 3, measured), which reads exactly like
    // the starvation it was meant to fix.
    Some(true)
}

#[cfg(feature = "gpu")]
fn chain_max() -> usize {
    static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *N.get_or_init(|| {
        std::env::var("CMF_DSV4_CHAIN_MAX")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(usize::MAX)
    })
}

/// `CMF_DSV4_CHAIN=1`: put a run of consecutive device-capable layers in ONE
/// submission. Off by default until it has been measured on a real card.
#[cfg(feature = "gpu")]
/// `CMF_DSV4_HOST_CPU_MOE=1`: a layer that fell off the card runs its MoE on
/// the host WITHOUT the per-op device route — one fence a token instead of
/// one a matvec. Whether that wins is a measurement.
/// `CMF_DSV4_PARTIAL_WALK=1`: the fused device walk of a partial layer.
/// OFF until its self-poisoning is repaired: its attention frame reads the
/// pooled slots its own MoE frame rewrote on the previous token, so every
/// token after the first attends over leftovers — the drafts it captures
/// от такого состояния never match the verify (acceptance 0, measured).
/// The host branch walks these layers correctly; the pack stays resident
/// for the verify tail.
fn partial_walk_on() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSV4_PARTIAL_WALK").is_ok_and(|v| v != "0"))
}

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

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

/// Encode a maximal run of consecutive device-capable layers and submit it
/// ONCE. Every layer in the run builds its own attention inputs on the card,
/// so nothing comes back between them — that is the whole saving.
///
/// The run's state belongs to the device from here on: `st.window`,
/// `st.compressed` and the compressor streams for these layers are stale on
/// the host afterwards, and only the counts in `st.dev_*` are kept. A layer
/// that has ever been in a run must therefore never be handed to the CPU
/// path again, which `dev_owned` records.
#[cfg(feature = "gpu")]
#[allow(clippy::too_many_arguments)]
fn dsv4_chain_run(
    layers: &[Dsv4Layer],
    run: &[usize],
    cfg: &Dsv4Cfg,
    g: &Dsv4Globals,
    st: &mut Dsv4State,
    token_id: u32,
    // In AND out: the run reads the fold it starts from and MUST leave the
    // fold it produced, because whatever follows — a host layer, or the next
    // run after a cap — seeds from this. Passing it read-only left every
    // later segment starting from a stale fold: exact with one unbroken run,
    // release-scale garbage the moment anything splits the chain.
    folded: &mut Vec<f32>,
    // When present, the hyper-connection state rides home in the run's own
    // submission instead of costing a second fence afterwards. Only the
    // token's LAST run passes it — an earlier one would read a state the
    // layers after it still change.
    state_out: Option<&mut [f32]>,
    // How many consecutive tokens this run carries. One is decode; more is a
    // prompt chunk or a speculative verify, which are the same shape of work.
    batch: usize,
    // Their ids, needed only when `batch > 1`: a hash layer forces its expert
    // list from the token's id, so the batch needs one list per token and the
    // single `token_id` above cannot supply them.
    batch_ids: &[u32],
    // Whether the device's qn buffer is stale: true at layer zero and after
    // a host layer. When the previous layer was chained, its frame's tail
    // already left THIS layer's LoRA vector on the card, and recomputing it
    // here was a full wq_a matvec on the CPU per run — at CHAIN_MAX=1 that
    // is one per LAYER, which is how a 43-fence path measured slower than
    // an 86-fence one.
    need_qn: bool,
    pool: Option<&crate::pool::Pool>,
) -> bool {
    if run.is_empty() {
        return true;
    }
    let (dim, hd) = (cfg.dim, cfg.head_dim);
    let first = run[0];
    let Some(model) = layers[first].experts.first().and_then(|e| e.w1.model_arc()) else {
        return false;
    };
    // Batch callers seed every token's fold and qn in its own slot. Seeding
    // the legacy shared slot here is not merely redundant: `folded` carries
    // only the eventual LAST output and is empty before the batch runs.
    if batch <= 1 && need_qn {
        let mut qn0 = vec![0.0f32; cfg.q_lora_rank];
        layers[first].wq_a.matvec(folded, &mut qn0, pool);
        rms_weighted(&mut qn0, &layers[first].q_norm, cfg.norm_eps);
        if !crate::gpu_wgpu::dsv4_chain_seed(folded, &qn0) {
            return false;
        }
    } else if batch <= 1 && !crate::gpu_wgpu::dsv4_chain_seed_fold(folded) {
        return false;
    }

    // Held apart from the borrowing structs below, which point into them.
    let mut packs = Vec::with_capacity(run.len());
    let mut forceds: Vec<Option<Vec<usize>>> = Vec::with_capacity(run.len());
    for &li in run {
        let Some(pk) = pack_for(&layers[li], cfg, li) else {
            return false;
        };
        let forced: Option<Vec<usize>> = layers[li].tid2eid.as_ref().and_then(|tbl| {
            let v: Vec<usize> = hash_route(tbl, cfg.vocab, cfg.top_k, token_id)
                .into_iter()
                .map(|gi| pk.to_slot[gi])
                .collect();
            if v.iter().any(|&x| x == usize::MAX) { None } else { Some(v) }
        });
        if layers[li].tid2eid.is_some() && forced.is_none() {
            return false;
        }
        forceds.push(forced);
        packs.push(pk);
    }

    let mut items = Vec::with_capacity(run.len());
    let mut freqs = Vec::with_capacity(run.len());
    for (i, &li) in run.iter().enumerate() {
        let l = &layers[li];
        let (Some(wq_a), Some(wq_b), Some(wo_a), Some(wo_b), Some(wkv)) = (
            l.wq_a.model_idx(),
            l.wq_b.model_idx(),
            l.wo_a.model_idx(),
            l.wo_b.model_idx(),
            l.wkv.model_idx(),
        ) else {
            return false;
        };
        let comp = match &l.compressor {
            None => None,
            Some(cp) => {
                let (Some(a), Some(b)) = (cp.wkv.model_idx(), cp.wgate.model_idx()) else {
                    return false;
                };
                Some((
                    crate::gpu_wgpu::Dsv4CompW { wkv: a, wgate: b, norm: &cp.norm, ape: &cp.ape },
                    crate::gpu_wgpu::Dsv4CompGeom {
                        width: cp.wkv.rows(),
                        hidden: dim,
                        ratio: cp.ratio,
                        overlap: cp.overlap,
                        rope_dim: cfg.rope_head_dim,
                        eps: cfg.norm_eps,
                    },
                ))
            }
        };
        let ix = match &l.indexer {
            None => None,
            Some(ixr) => {
                let cp = &ixr.compressor;
                let (Some(a), Some(b), Some(qb), Some(wp)) = (
                    cp.wkv.model_idx(),
                    cp.wgate.model_idx(),
                    ixr.wq_b.model_idx(),
                    ixr.weights_proj.model_idx(),
                ) else {
                    return false;
                };
                let ih = ixr.weights_proj.rows();
                Some((
                    crate::gpu_wgpu::Dsv4CompW { wkv: a, wgate: b, norm: &cp.norm, ape: &cp.ape },
                    crate::gpu_wgpu::Dsv4CompGeom {
                        width: cp.wkv.rows(),
                        hidden: dim,
                        ratio: cp.ratio,
                        overlap: cp.overlap,
                        rope_dim: cfg.rope_head_dim,
                        eps: cfg.norm_eps,
                    },
                    crate::gpu_wgpu::Dsv4IxW { wq_b: qb, weights_proj: wp },
                    crate::gpu_wgpu::Dsv4IxGeom {
                        ih,
                        idim: ixr.wq_b.rows() / ih.max(1),
                        q_lora: cfg.q_lora_rank,
                        hidden: dim,
                        rope_dim: cfg.rope_head_dim,
                        eps: cfg.norm_eps,
                        top_k: cfg.index_topk,
                        window: cfg.window,
                    },
                ))
            }
        };
        // The cache has to be big enough BEFORE the frame appends into it:
        // a chained layer never calls dsv4_cache_write, which is what used
        // to create and grow it.
        let ew_c0 = l.compressor.as_ref().map_or(0, |cp| {
            if cp.overlap { cp.wkv.rows() / 2 } else { cp.wkv.rows() }
        });
        let comp_extra = l.compressor.as_ref().map_or(0, |cp| {
            batch.max(1).div_ceil(cp.ratio.max(1))
        });
        let need = cfg.window * hd
            + (st.dev_n_comp[li] + comp_extra + 1) * ew_c0.max(1)
            + (batch.max(1) + 1) * hd;
        if !crate::gpu_wgpu::dsv4_cache_ensure(st.kv_id, li, need.next_power_of_two()) {
            return false;
        }
        let ew_c = comp.as_ref().map_or(0, |(_, cg)| {
            if cg.overlap { cg.width / 2 } else { cg.width }
        });
        let ew_i = ix.as_ref().map_or(0, |(_, cg, _, _)| {
            if cg.overlap { cg.width / 2 } else { cg.width }
        });
        let prep = crate::gpu_wgpu::Dsv4Prep {
            wkv,
            kv_norm: &l.kv_norm,
            comp,
            ix,
            filled: st.dev_filled[li],
            window: cfg.window,
            n_comp: st.dev_n_comp[li],
            n_ix: st.dev_n_ix[li],
            comp_dst_off: cfg.window * hd + st.dev_n_comp[li] * ew_c,
            ix_dst_off: st.dev_n_ix[li] * ew_i,
            idx_cap: cfg.window
                + if l.indexer.is_some() {
                    cfg.index_topk
                } else {
                    st.dev_n_comp[li] + comp_extra + 1
                },
        };
        let nxt = layers.get(li + 1);
        let w = crate::gpu_wgpu::Dsv4LayerW {
            attn: crate::gpu_wgpu::Dsv4AttnW {
                wq_a, wq_b, wo_a, wo_b,
                q_norm: &l.q_norm,
                sink: &l.attn_sink,
            },
            moe: crate::gpu_wgpu::Dsv4MoeW {
                router: &packs[i].router,
                experts: &packs[i].tensors,
                logits: &[],
                // The PACK's slice, not a per-run Vec: the address stability
                // is the whole point (see Pack::bias).
                bias: packs[i].bias.as_deref(),
                forced: forceds[i].as_deref(),
                remap: None,
            },
            hc_ffn_fn: &l.hc_ffn_fn,
            hc_ffn_scale: &l.hc_ffn_scale,
            hc_ffn_base: &l.hc_ffn_base,
            hc_next_fn: nxt.map(|n| n.hc_attn_fn.as_slice()),
            hc_next_scale: nxt.map_or(&l.hc_attn_scale, |n| &n.hc_attn_scale),
            hc_next_base: nxt.map_or(&l.hc_attn_base, |n| n.hc_attn_base.as_slice()),
            ffn_norm: &l.ffn_norm,
            next_norm: nxt.map_or(&l.attn_norm, |n| n.attn_norm.as_slice()),
            next_q_norm: nxt.map_or(&l.q_norm, |n| n.q_norm.as_slice()),
            next_wq_a: nxt.and_then(|n| n.wq_a.model_idx()),
            router: &packs[i].router,
        };
        let geom = crate::gpu_wgpu::Dsv4LayerGeom {
            attn: crate::gpu_wgpu::Dsv4AttnGeom {
                dim,
                nh: cfg.n_heads,
                hd,
                rd: cfg.rope_head_dim,
                q_lora: cfg.q_lora_rank,
                o_lora: cfg.o_lora_rank,
                o_groups: cfg.o_groups,
                eps: cfg.norm_eps,
                scale: (hd as f32).powf(-0.5),
            },
            moe: crate::gpu_wgpu::Dsv4MoeGeom {
                hidden: dim,
                inter: cfg.moe_inter,
                top_k: cfg.top_k,
                route_scale: cfg.route_scale,
                swiglu_limit: cfg.swiglu_limit,
                gu_q2: l.experts.first().is_some_and(|e| {
                    e.w1.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)
                }),
            },
            hc: cfg.hc_mult,
            hc_eps: cfg.hc_eps,
            sinkhorn_iters: cfg.hc_sinkhorn_iters,
        };
        freqs.push(if l.compressor.is_some() {
            g.inv_freq_compress.as_slice()
        } else {
            g.inv_freq_window.as_slice()
        });
        items.push((w, geom, prep));
    }

    let mut out = vec![0.0f32; dim * batch.max(1)];
    if batch > 1 {
        // A batch keeps its own state per token. When a host tail follows,
        // all of those states ride home beside the folds in the same fence.
        // One forced row per token: same layers, the hash rows re-derived
        // from each token's own id.
        let mut forced_pt: Vec<Vec<Option<Vec<usize>>>> = Vec::with_capacity(batch);
        for t in 0..batch {
            let id = batch_ids.get(t).copied().unwrap_or(token_id);
            let mut row = Vec::with_capacity(run.len());
            for (i, &li) in run.iter().enumerate() {
                row.push(layers[li].tid2eid.as_ref().and_then(|tbl| {
                    let v: Vec<usize> = hash_route(tbl, cfg.vocab, cfg.top_k, id)
                        .into_iter()
                        .map(|gi| packs[i].to_slot[gi])
                        .collect();
                    if v.iter().any(|&x| x == usize::MAX) { None } else { Some(v) }
                }));
                if layers[li].tid2eid.is_some() && row[i].is_none() {
                    return false;
                }
            }
            forced_pt.push(row);
        }
        if !crate::gpu_wgpu::dsv4_chain_batch(
            &model, &items, st.kv_id, first, &freqs, st.pos, batch,
            Some(&forced_pt), &mut out, state_out,
        ) {
            return false;
        }
        // The caller wants the LAST token's fold: it is the one whose logits
        // continue the sequence.
        *folded = out[(batch - 1) * dim..batch * dim].to_vec();
    } else {
        if !crate::gpu_wgpu::dsv4_layer_chain(
            &model, &items, st.kv_id, first, &freqs, st.pos, &mut out, state_out,
        ) {
            return false;
        }
        *folded = out;
    }
    // The device advanced these; the host keeps only the arithmetic. A batch
    // advanced them once per token, in order, so the host replays the same
    // rule that many times rather than inventing a closed form for it.
    for (i, &li) in run.iter().enumerate() {
      for t in 0..batch.max(1) {
        let pos = st.pos + t;
        st.dev_filled[li] = (st.dev_filled[li] + 1).min(cfg.window);
        if let Some((_, cg, ..)) = items[i].2.ix.as_ref() {
            if (pos + 1) % cg.ratio == 0 {
                st.dev_n_ix[li] += 1;
            }
        }
        if let Some((_, cg)) = items[i].2.comp.as_ref() {
            if (pos + 1) % cg.ratio == 0 {
                st.dev_n_comp[li] += 1;
            }
        }
      }
    }
    st.dev_owned = true;
    true
}

/// `CMF_DSV4_HC_DEV=0` puts the hyper-connections back on the host.
#[cfg(feature = "gpu")]
fn hc_on_device() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| {
        // OPT-IN. On the release checkpoint this path reads 3.234 against
        // the CPU's 3.282 — divergent — and the speed is unchanged, so there
        // is no trade to weigh: it must not be the default until it is
        // exact. The toy's near-agreement (129.787 vs 129.792) hid a real
        // fault the release exposes.
        std::env::var("CMF_DSV4_HC_DEV").is_ok_and(|v| v != "0")
            && crate::gpu::backend_available()
    })
}

/// The two-frame path with the hyper-connections on the card.
///
/// The host still prepares each layer's attention inputs — the compressor,
/// the indexer and the window, which are exact there — but it no longer
/// folds, Sinkhorns or norms, and it no longer carries the MoE half's input
/// between the halves: the attention frame leaves it on the device and the
/// MoE frame reads it from there. One readback a layer instead of two, and
/// 19 ms of host arithmetic a token gone.
#[cfg(feature = "gpu")]
#[allow(clippy::too_many_arguments)]
fn dsv4_two_frame_loop(
    state: &mut [f32],
    layers: &[Dsv4Layer],
    g: &Dsv4Globals,
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    token_id: u32,
    inv_freq: &[f32],
    pool: Option<&crate::pool::Pool>,
    scratch: &mut HcScratch,
) -> bool {
    let dim = cfg.dim;
    let freqs_of = |l: &Dsv4Layer| -> &[f32] {
        let f = if l.compressor.is_some() {
            &g.inv_freq_compress
        } else {
            &g.inv_freq_window
        };
        if f.is_empty() { inv_freq } else { f.as_slice() }
    };
    // Layer zero's fold has no frame before it, exactly as in the layer path.
    let (mut folded, post0, comb0) = hc_fold_norm(
        state,
        &layers[0].hc_attn_fn,
        &layers[0].hc_attn_scale,
        &layers[0].hc_attn_base,
        &layers[0].attn_norm,
        cfg,
        pool,
    );
    if !crate::gpu_wgpu::dsv4_state_write(state)
        || !crate::gpu_wgpu::dsv4_hc_write(&post0, &comb0)
    {
        return false;
    }
    // PRE-FLIGHT, before the first byte of state moves: a mid-loop refusal
    // would hand the token back to the ordinary loop AFTER these caches
    // advanced, and the second advance is not a slow answer but a wrong one.
    // The same discipline the layer loop states in the same words.
    let mut on_dev = vec![false; layers.len()];
    for (li, l) in layers.iter().enumerate() {
        let Some(pk) = pack_for(l, cfg, li) else {
            return false;
        };
        let Some(model) = l.experts.first().and_then(|e| e.w1.model_arc()) else {
            return false;
        };
        let gu_q2 = l
            .experts
            .first()
            .is_some_and(|e| e.w1.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP));
        let attn_ok = [
            l.wq_a.model_idx(),
            l.wq_b.model_idx(),
            l.wo_a.model_idx(),
            l.wo_b.model_idx(),
        ]
        .into_iter()
        .flatten()
        .all(|i| crate::gpu_wgpu::dsv4_weight_ready(&model, i));
        on_dev[li] = attn_ok
            && pk.globals.len() == cfg.n_routed_experts
            && crate::gpu_wgpu::dsv4_experts_ready(
                &model,
                &pk.tensors,
                cfg.moe_inter,
                dim,
                gu_q2,
                l.experts.first().is_some_and(|e| {
                    e.w2.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)
                }),
            );
    }
    if !on_dev.iter().any(|&x| x) {
        return false;
    }
    let mut sink = vec![0.0f32; dim];
    for (li, l) in layers.iter().enumerate() {
        // A layer the card cannot hold runs on the host WHOLE, with the
        // state fetched and put back around it — the mixed ownership the
        // layer loop already proved out.
        if !on_dev[li] {
            if !crate::gpu_wgpu::dsv4_state_read(state) {
                return false;
            }
            let freqs = freqs_of(l);
            hc_block(
                state,
                &l.hc_attn_fn,
                &l.hc_attn_scale,
                &l.hc_attn_base,
                &l.attn_norm,
                cfg,
                scratch,
                pool,
                |f, o| attention_step(f, l, cfg, st, li, freqs, pool, None, o),
            );
            hc_block(
                state,
                &l.hc_ffn_fn,
                &l.hc_ffn_scale,
                &l.hc_ffn_base,
                &l.ffn_norm,
                cfg,
                scratch,
                pool,
                |f, o| moe_step(f, l, cfg, token_id, li, pool, o),
            );
            let nref = layers.get(li + 1).unwrap_or(l);
            let (f, p2, c2) = hc_fold_norm(
                state,
                &nref.hc_attn_fn,
                &nref.hc_attn_scale,
                &nref.hc_attn_base,
                &nref.attn_norm,
                cfg,
                pool,
            );
            folded = f;
            if !crate::gpu_wgpu::dsv4_hc_write(&p2, &c2)
                || !crate::gpu_wgpu::dsv4_state_write(state)
            {
                return false;
            }
            continue;
        }
        // The host's half: the caches and the attended list, untouched.
        let mut prep = AttnPrep::default();
        attention_step(
            &folded, l, cfg, st, li, freqs_of(l), pool, Some(&mut prep), &mut sink,
        );
        let hd = cfg.head_dim;
        let n_comp = st.compressed[li].len() / hd;
        let cap = (cfg.window + n_comp.next_power_of_two().max(64)) * hd;
        if !crate::gpu_wgpu::dsv4_cache_write(st.kv_id, li, 0, &st.window[li], cap)
            || (n_comp > 0
                && !crate::gpu_wgpu::dsv4_cache_write(
                    st.kv_id, li, cfg.window * hd, &st.compressed[li], cap,
                ))
        {
            return false;
        }
        let idx32: Vec<u32> = prep
            .idxs
            .iter()
            .map(|&p| {
                if p < prep.win_len {
                    p as u32
                } else {
                    (cfg.window + (p - prep.win_len)) as u32
                }
            })
            .collect();
        let nxt = layers.get(li + 1);
        let a_tail = crate::gpu_wgpu::Dsv4HcTail {
            fn_: &l.hc_ffn_fn,
            scale: &l.hc_ffn_scale,
            base: &l.hc_ffn_base,
            norm: &l.ffn_norm,
            hc: cfg.hc_mult,
            sinkhorn_iters: cfg.hc_sinkhorn_iters,
            hc_eps: cfg.hc_eps,
            eps: cfg.norm_eps,
        };
        let scale = (cfg.head_dim as f32).powf(-0.5);
        if !attn_frame(
            l, cfg, st, li, &folded, &prep.qr, &prep.idxs, freqs_of(l), st.pos,
            prep.win_len, scale, Some(&a_tail), &mut [],
        ) {
            return false;
        }
        let m_tail = nxt.map(|n| crate::gpu_wgpu::Dsv4HcTail {
            fn_: &n.hc_attn_fn,
            scale: &n.hc_attn_scale,
            base: &n.hc_attn_base,
            norm: &n.attn_norm,
            hc: cfg.hc_mult,
            sinkhorn_iters: cfg.hc_sinkhorn_iters,
            hc_eps: cfg.hc_eps,
            eps: cfg.norm_eps,
        });
        let mut next = vec![0.0f32; dim];
        let pair = m_tail
            .as_ref()
            .zip(nxt)
            .map(|(t, n)| (t, n.attn_norm.as_slice()));
        let forced = l
            .tid2eid
            .as_ref()
            .map(|tbl| hash_route(tbl, cfg.vocab, cfg.top_k, token_id));
        if moe_frame(&[], l, cfg, li, &[], forced.as_deref(), pool, Some(&a_tail), pair, &mut next)
            .is_none()
        {
            return false;
        }
        folded = next;
    }
    let _ = scratch;
    crate::gpu_wgpu::dsv4_state_read(state)
}

/// The host half of one hyper-connection block: mixes, Sinkhorn, fold, norm.
/// The device does this for every layer but the first, whose state it has not
/// seen yet.
#[cfg(feature = "gpu")]
#[allow(clippy::too_many_arguments)]
fn hc_fold_norm(
    state: &[f32],
    hc_fn: &[f32],
    hc_scale: &[f32; 3],
    hc_base: &[f32],
    norm_w: &[f32],
    cfg: &Dsv4Cfg,
    pool: Option<&crate::pool::Pool>,
) -> (Vec<f32>, Vec<f32>, Vec<f32>) {
    let (hc, dim) = (cfg.hc_mult, cfg.dim);
    let mix_hc = (2 + hc) * hc;
    let mut mixes = vec![0.0f32; mix_hc];
    hc_mixes(state, hc_fn, mix_hc, cfg.norm_eps, pool, &mut mixes);
    let mut pre = vec![0.0f32; hc];
    let mut post = vec![0.0f32; hc];
    let mut comb = vec![0.0f32; hc * hc];
    hc_split_sinkhorn(
        &mixes,
        hc_scale,
        hc_base,
        hc,
        cfg.hc_sinkhorn_iters,
        cfg.hc_eps,
        &mut pre,
        &mut post,
        &mut comb,
    );
    let mut folded = vec![0.0f32; dim];
    hc_fold(state, &pre, hc, dim, &mut folded);
    rms_weighted(&mut folded, norm_w, cfg.norm_eps);
    // post and comb travel with the fold: the frame's opening expand needs
    // exactly those, and they are not recoverable from the state alone.
    (folded, post, comb)
}

/// `CMF_DSV4_GPU_LAYER=1`: one submission per layer instead of two, with the
/// hyper-connection glue and the router on the device.
///
/// CORRECT — perplexity 5.211 against the CPU's 5.211 on the release, 128.576
/// against 128.576 on the toy — and SLOWER on this hardware: 6.0 tok/s where
/// the two-frame path gets 9.3. The reason is not the frame, it is the
/// all-or-nothing granularity underneath it. A layer whose experts miss VRAM
/// runs entirely on the host, attention included (6.5 ms a call against 0.9),
/// and with 100 GB of experts against a 98 GB card a fifth of the layers
/// miss. The two-frame path only loses the MoE half of those layers.
///
/// So the barrier it saves is real and the fallback it forces costs more. The
/// fix is the granularity: pack the experts that FIT, route over all of them
/// anyway, and run the few cold picks of a token on the host — per EXPERT,
/// not per layer. Then no layer ever leaves the device and this frame wins by
/// the 15 ms a token it was built to save.
#[cfg(feature = "gpu")]
fn gpu_layer_enabled() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| {
        std::env::var("CMF_DSV4_GPU_LAYER").map(|v| v != "0").unwrap_or(true)
            && crate::gpu::backend_available()
    })
}

/// The packed expert set of one layer: which globals made it in, and their
/// directory indices in packing order with the shared expert last. Built once
/// — the mask does not change during a run — and keyed by layer.
#[cfg(feature = "gpu")]
struct Pack {
    /// The router as dense f32, expanded once. It is 4 MB a layer against a
    /// 112 GB model, it lives as long as the process — so the address-keyed
    /// device cache is sound for it, unlike anything built per call.
    router: Vec<f32>,
    /// global expert id -> packed slot, `usize::MAX` for the ones left out.
    to_slot: Vec<usize>,
    /// The same, as the u32 table the router reads.
    remap: Vec<u32>,
    /// packed order, globals only (shared is not in here).
    globals: Vec<usize>,
    tensors: Vec<(usize, usize, usize)>,
    /// The noaux_tc bias in PACKED order, kept here because it is the same
    /// every token and the pack lives as long as the process: a stable
    /// address means a stable device buffer, and a stable device buffer is
    /// what lets many layers share one submission. A bias uploaded through
    /// the per-call pool is written by every layer of a run BEFORE the run's
    /// single submit — queue writes do not interleave with passes — so every
    /// layer routed with the LAST layer's bias. On the release every scored
    /// layer carries one, which is the 50.280.
    bias: Option<Vec<f32>>,
}

#[cfg(feature = "gpu")]
/// Candidate order for a budget-limited pack: hottest expert first, by the
/// measured tally `CMF_DSV4_PACK_FREQ` points at (`layer<TAB>expert<TAB>count`
/// lines). None when the variable is unset, the file is unreadable, or the
/// tally has nothing for this layer — the caller keeps id order then. Ties
/// and untallied experts follow in id order, so the choice is deterministic.
fn pack_freq_order(li: usize, n: usize) -> Option<Vec<usize>> {
    use std::collections::HashMap;
    use std::sync::OnceLock;
    static FREQ: OnceLock<Option<HashMap<(usize, usize), u64>>> = OnceLock::new();
    let map = FREQ
        .get_or_init(|| {
            let path = std::env::var("CMF_DSV4_PACK_FREQ").ok()?;
            let text = match std::fs::read_to_string(&path) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("CMF_DSV4_PACK_FREQ={path} не читается ({e}) — порядок по id");
                    return None;
                }
            };
            let mut m = HashMap::new();
            for line in text.lines() {
                let mut it = line.split('\t');
                if let (Some(l), Some(e), Some(c)) = (it.next(), it.next(), it.next()) {
                    if let (Ok(l), Ok(e), Ok(c)) =
                        (l.trim().parse(), e.trim().parse(), c.trim().parse::<u64>())
                    {
                        *m.entry((l, e)).or_insert(0) += c;
                    }
                }
            }
            Some(m)
        })
        .as_ref()?;
    if !(0..n).any(|e| map.contains_key(&(li, e))) {
        return None;
    }
    let mut idx: Vec<usize> = (0..n).collect();
    idx.sort_by_key(|&e| (std::cmp::Reverse(map.get(&(li, e)).copied().unwrap_or(0)), e));
    Some(idx)
}

#[cfg(feature = "gpu")]
fn pack_for(l: &Dsv4Layer, cfg: &Dsv4Cfg, li: usize) -> Option<std::sync::Arc<Pack>> {
    use std::collections::HashMap;
    use std::sync::{Arc, Mutex, OnceLock};
    static CACHE: OnceLock<Mutex<HashMap<(u64, usize, usize), Option<Arc<Pack>>>>> =
        OnceLock::new();
    let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
    // Keyed by the layer's IDENTITY, not its ordinal. The draft's three
    // stages are layers too and they number 0, 1, 2 — under an ordinal key
    // they would be handed the trunk's first three packs: another layer's
    // router, another layer's tensor indices, another layer's bias. The gate
    // tensor is what actually distinguishes them.
    let model_uid = l
        .experts
        .first()
        .and_then(|e| e.w1.model_arc())
        .map_or(0, |m| m.uid());
    // Dense f32 routers need not have a directory handle, so the gate index
    // alone can be `None` for every layer. Pair the ordinal with the first
    // expert's mapped identity; model UID keeps long-lived multi-model
    // servers separate, while the expert index distinguishes trunk and MTP
    // layers that reuse ordinal 0/1/2.
    let first_expert = l
        .experts
        .first()
        .and_then(|e| e.w1.model_idx())
        .unwrap_or(usize::MAX);
    let key = (model_uid, li, first_expert);
    if let Some(v) = cache.lock().unwrap().get(&key) {
        return v.clone();
    }
    // `CMF_DSV4_PACK_MAX_LI=N` — do not pack layers above N at all. A layer
    // with no pack stays wholly host-owned, which is what both the batched
    // prefill and a speculative verify need of the tail: a device-owned
    // partial layer can join neither the batch (incomplete pack) nor the
    // causal host tail (its caches live on the card). This also carves the
    // VRAM the tail would have taken for the draft's own pack.
    if let Ok(v) = std::env::var("CMF_DSV4_PACK_MAX_LI") {
        if v.parse::<usize>().is_ok_and(|max| li > max) {
            cache.lock().unwrap().insert(key, None);
            return None;
        }
    }
    let build = || -> Option<Arc<Pack>> {
        let mut to_slot = vec![usize::MAX; cfg.n_routed_experts];
        let mut globals = Vec::new();
        let mut tensors = Vec::new();
        let idx3 = |e: &Dsv4Expert| -> Option<(usize, usize, usize)> {
            Some((e.w1.model_idx()?, e.w3.model_idx()?, e.w2.model_idx()?))
        };
        // How many experts the card still has room for, minus one for the
        // shared expert, which always rides. Everything past that stays on the
        // host and is reached through the remap — the router still ranges over
        // all of them, so this costs speed and not a single bit of quality.
        let gu_q2 = l
            .experts
            .first()
            .is_some_and(|e| e.w1.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP));
        // Pack what fits and leave the rest to the host. The router still
        // ranges over every expert; a missing winner is returned as a cold
        // pick and completed on the CPU. This is deliberately budget-driven,
        // not layer-driven: the same model scales from a small card (more
        // partial/host layers) to a large one (all experts resident) without
        // a checkpoint-specific cutoff.
        // `CMF_DSV4_PACK_MAX=N` caps the packing directly, so a toy can
        // reproduce the subset path without needing a card that runs out.
        if let Some(n) = std::env::var("CMF_DSV4_PACK_MAX")
            .ok()
            .and_then(|v| v.parse::<usize>().ok())
        {
            let mut to_slot = vec![usize::MAX; cfg.n_routed_experts];
            let mut globals = Vec::new();
            let mut tensors = Vec::new();
            for (gi, e) in l.experts.iter().enumerate().take(n) {
                to_slot[gi] = globals.len();
                globals.push(gi);
                tensors.push(idx3(e)?);
            }
            tensors.push(idx3(&l.shared)?);
            let (rows, cols) = (l.gate.rows(), l.gate.cols());
            let mut router = vec![0.0f32; rows * cols];
            for r in 0..rows {
                l.gate.row_f32(r, &mut router[r * cols..(r + 1) * cols]);
            }
            let remap: Vec<u32> = to_slot
                .iter()
                .map(|&sl| if sl == usize::MAX { u32::MAX } else { sl as u32 })
                .collect();
            return Some(Arc::new(Pack {
                bias: l.gate_bias.as_deref().map(|b| {
                    globals.iter().map(|&g| b[g]).collect()
                }),
                router,
                to_slot,
                remap,
                globals,
                tensors,
            }));
        }
        let dn_q2_fit = l
            .experts
            .first()
            .is_some_and(|e| e.w2.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP));
        let room = crate::gpu_wgpu::dsv4_experts_fit(cfg.moe_inter, cfg.dim, gu_q2, dn_q2_fit)
            .saturating_sub(1);
        // When the budget packs a SUBSET, which subset matters: a partial
        // layer completes its cold picks from the host, so every resident
        // expert that the routing actually reaches is host work saved.
        // `CMF_DSV4_PACK_FREQ` names a measured tally
        // (`CMF_DSV4_TRUNK_PICK_DUMP` wrote it) and reorders the candidates
        // hottest-first; layers absent from the tally keep id order. The
        // router still ranges over every expert either way — residency
        // choice changes speed, never a bit of the answer.
        let order = pack_freq_order(li, l.experts.len())
            .unwrap_or_else(|| (0..l.experts.len()).collect());
        for gi in order {
            let e = &l.experts[gi];
            if l.mask.as_deref().is_some_and(|m| !m.get(gi).copied().unwrap_or(true)) {
                continue;
            }
            if globals.len() >= room {
                break;
            }
            to_slot[gi] = globals.len();
            globals.push(gi);
            match idx3(e) {
                Some(t) => tensors.push(t),
                None => {
                    if std::env::var("CMF_DSV4_FRAME_DEBUG").is_ok() {
                        eprintln!("слой {li}: эксперт {gi} без индексов в каталоге");
                    }
                    return None;
                }
            }
        }
        if globals.is_empty() {
            // Two very different causes, and blaming the mask for the other
            // one sent a reader looking for a mask that was never set: an
            // actual empty mask, or a VRAM budget with no room left for even
            // one expert (`room` is 0, which is what a nearly-full card does
            // to the last layers).
            if room == 0 {
                static SAID_ZERO: std::sync::atomic::AtomicBool =
                    std::sync::atomic::AtomicBool::new(false);
                if !SAID_ZERO.swap(true, std::sync::atomic::Ordering::Relaxed) {
                    tracing::warn!(
                        "начиная со слоя {li}, в бюджете VRAM не осталось места даже под одного \
                         эксперта — остальные веса остаются mmap-backed и читаются по требованию"
                    );
                }
            } else {
                tracing::warn!("слой {li}: маска не оставила ни одного эксперта");
            }
            return None;
        }
        tensors.push(idx3(&l.shared)?); // shared rides last, as the kernels expect
        let (rows, cols) = (l.gate.rows(), l.gate.cols());
        let mut router = vec![0.0f32; rows * cols];
        for r in 0..rows {
            l.gate.row_f32(r, &mut router[r * cols..(r + 1) * cols]);
        }
        let remap: Vec<u32> = to_slot
            .iter()
            .map(|&sl| if sl == usize::MAX { u32::MAX } else { sl as u32 })
            .collect();
        Some(Arc::new(Pack {
            bias: l.gate_bias.as_deref().map(|b| {
                globals.iter().map(|&g| b[g]).collect()
            }),
            router,
            to_slot,
            remap,
            globals,
            tensors,
        }))
    };
    let v = build();
    cache.lock().unwrap().insert(key, v.clone());
    v
}

/// The whole MoE block in one submission, experts resident (default on;
/// `CMF_DSV4_GPU_MOE2=0` restores the host path). Returns false having
/// changed nothing if it cannot — a missing pack, a refused budget — so the
/// caller's CPU path stays correct to run. The early divergence this frame
/// once carried (0.44 relative, perplexity 5.162 vs 5.211) was the partial
/// -capture and hidden-seed defects, fixed since: perplexity gold 4.578 is
/// bit-exact against the CPU on every budget from 64 to 96.5 GB.
#[cfg(feature = "gpu")]
fn moe_frame(
    hidden: &[f32],
    l: &Dsv4Layer,
    cfg: &Dsv4Cfg,
    li: usize,
    logits: &[f32],
    forced: Option<&[usize]>,
    pool: Option<&crate::pool::Pool>,
    // The state handover: expand always when the device owns the state,
    // fold only when there is a next layer.
    hc_cur: Option<&crate::gpu_wgpu::Dsv4HcTail>,
    hc_next: Option<(&crate::gpu_wgpu::Dsv4HcTail, &[f32])>,
    out: &mut [f32],
) -> Option<(Vec<f32>, usize)> {
    macro_rules! no {
        ($($t:tt)*) => {{
            if std::env::var("CMF_DSV4_FRAME_DEBUG").is_ok() {
                eprintln!("кадр MoE отклонён: {}", format_args!($($t)*));
            }
            return None;
        }};
    }
    let Some(pk) = pack_for(l, cfg, li) else {
        no!("слой {li}: упаковка экспертов не построена");
    };
    // The router is a small f32 tensor and is usually NOT mapped; the handle
    // has to come from something that is.
    let Some(model) = l.experts.first().and_then(|e| e.w1.model_arc()) else {
        no!("слой {li}: эксперты не отображены из файла");
    };
    let subset = pk.globals.len() < cfg.n_routed_experts;
    // With a complete pack the forced row is translated to packed numbering.
    // With a subset it stays global: the router's remap either finds its slot
    // or returns the forced expert as a cold pick, exactly like a scored one.
    let fpack: Option<Vec<usize>> = match forced {
        Some(f) if subset => Some(f.to_vec()),
        Some(f) => {
            let v: Vec<usize> = f.iter().map(|&g| pk.to_slot[g]).collect();
            if v.iter().any(|&s| s == usize::MAX) {
                no!("слой {li}: хеш-слой называет эксперта вне упаковки");
            }
            Some(v)
        }
        None => None,
    };
    // Routing ranges over EVERY expert; the remap turns a winner into a slot
    // or marks it cold. Nothing is masked, so nothing is lost.
    // Empty logits are the device-scored case: the frame computes them from
    // pk.router, whose rows are already in global order, so there is nothing
    // to reorder — and indexing an empty slice is how this line greeted the
    // first engaged run.
    let lg: Vec<f32> = if logits.is_empty() || subset {
        logits.to_vec()
    } else {
        pk.globals.iter().map(|&g| logits[g]).collect()
    };
    let bias: Option<Vec<f32>> = l.gate_bias.as_deref().map(|b| {
        if subset {
            b.to_vec()
        } else {
            pk.globals.iter().map(|&g| b[g]).collect()
        }
    });
    let w = crate::gpu_wgpu::Dsv4MoeW {
        router: &pk.router,
        experts: &pk.tensors,
        logits: &lg,
        bias: bias.as_deref(),
        forced: fpack.as_deref(),
        remap: if subset { Some(&pk.remap) } else { None },
    };
    let g = crate::gpu_wgpu::Dsv4MoeGeom {
        hidden: cfg.dim,
        inter: cfg.moe_inter,
        top_k: cfg.top_k,
        route_scale: cfg.route_scale,
        swiglu_limit: cfg.swiglu_limit,
        gu_q2: l.experts.first().is_some_and(|e| {
            e.w1.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)
        }),
    };
    let mut cold = Vec::new();
    let mut cold_x = Vec::new();
    if !crate::gpu_wgpu::dsv4_moe_frame(
        &model,
        &w,
        g,
        hidden,
        &mut cold,
        &mut cold_x,
        hc_cur,
        hc_next,
        out,
    ) {
        return None;
    }
    // The picks the card had no room for, finished here and added in. Their
    // weights already carry the top-k normalisation the device applied.
    if std::env::var("CMF_DSV4_MOE_CHECK").is_ok() {
        let csum: f32 = cold.iter().map(|c| c.1).sum();
        eprintln!(
            "[холодные] слой {li}: вернулось {} из {} | сумма холодных {csum:.4} | \
             route_scale {:.4} | {:?}",
            cold.len(),
            cfg.top_k,
            cfg.route_scale,
            &cold[..cold.len().min(3)]
        );
    }
    let mut acc = vec![0.0f32; cfg.dim];
    let mut cold_sum = vec![0.0f32; cfg.dim];
    let cold_input = if hidden.is_empty() { cold_x.as_slice() } else { hidden };
    for &(gi, wt) in &cold {
        let Some(exp) = l.experts.get(gi) else { continue };
        // Cold means out-of-core by contract. The tensors remain mmap-backed:
        // missing pages are faulted from the CMF file and the OS may evict
        // them again under RAM pressure. Do not let the generic matvec probe
        // turn this into an unbounded second GPU cache behind the packer's
        // back.
        crate::gpu::cpu_scope(|| run_expert(cold_input, exp, cfg, wt, pool, &mut acc));
        for ((o, sum), a) in out.iter_mut().zip(&mut cold_sum).zip(&acc) {
            *o += a;
            *sum += a;
        }
    }
    Some((cold_sum, cold.len()))
}

/// How much of each layer's compressed cache already sits on the card. ONE
/// map: a reader and a writer with a `static` each are two maps, and the
/// reader would never see a thing the writer put down.
/// The reallocation counter as of the last successful tail write. Any change
/// means some buffer was rebuilt and every tail count is stale.
#[cfg(feature = "gpu")]
fn last_grew(now: u64) -> u64 {
    use std::sync::atomic::{AtomicU64, Ordering};
    static SEEN: AtomicU64 = AtomicU64::new(0);
    let was = SEEN.load(Ordering::Relaxed);
    if was != now {
        SEEN.store(now, Ordering::Relaxed);
        compressed_map().lock().unwrap().clear();
        return u64::MAX; // force a full write this round
    }
    now
}

#[cfg(feature = "gpu")]
fn compressed_map() -> &'static std::sync::Mutex<std::collections::HashMap<(u64, usize), usize>> {
    use std::collections::HashMap;
    use std::sync::{Mutex, OnceLock};
    static W: OnceLock<Mutex<HashMap<(u64, usize), usize>>> = OnceLock::new();
    W.get_or_init(|| Mutex::new(HashMap::new()))
}

#[cfg(feature = "gpu")]
fn compressed_written(kv_id: u64, li: usize) -> usize {
    compressed_map()
        .lock()
        .unwrap()
        .get(&(kv_id, li))
        .copied()
        .unwrap_or(0)
}

/// Reset to zero whenever a write fails or the buffer grows — a grown buffer
/// keeps none of its contents.
#[cfg(feature = "gpu")]
fn note_compressed(kv_id: u64, li: usize, n: usize) {
    compressed_map().lock().unwrap().insert((kv_id, li), n);
}

#[cfg(feature = "gpu")]
fn gpu_moe2_enabled() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| {
        std::env::var("CMF_DSV4_GPU_MOE2").map(|v| v != "0").unwrap_or(true)
            && crate::gpu::backend_available()
    })
}

pub fn moe_step(
    hidden: &[f32],
    l: &Dsv4Layer,
    cfg: &Dsv4Cfg,
    token_id: u32,
    // Layer index — only used to bucket routing statistics.
    li: usize,
    pool: Option<&crate::pool::Pool>,
    out: &mut [f32],
) {
    let _t0 = prof::on().then(std::time::Instant::now);
    let _guard = scopeguard_moe(_t0, li);
    let mut logits = vec![0.0f32; cfg.n_routed_experts];
    l.gate.matvec(hidden, &mut logits, pool);
    let (mut idx, mut w) = (Vec::new(), Vec::new());
    route(
        &logits,
        l.gate_bias.as_deref(),
        cfg.top_k,
        cfg.route_scale,
        l.tid2eid
            .as_ref()
            .map(|tbl| hash_route(tbl, cfg.vocab, cfg.top_k, token_id))
            .as_deref(),
        l.mask.as_deref(),
        &mut idx,
        &mut w,
    );
    if route_stats_on() {
        record_route(li, 0, cfg.n_routed_experts, &idx);
    }
    // The whole block on the device, in one submission, or nothing. Routing
    // happens there too — the logits above are what it starts from, so the
    // CPU's own choice is discarded rather than second-guessed.
    #[cfg(feature = "gpu")]
    if gpu_moe2_enabled() && crate::gpu::enabled_here() {
        let forced = l
            .tid2eid
            .as_ref()
            .map(|tbl| hash_route(tbl, cfg.vocab, cfg.top_k, token_id));
        if moe_frame(hidden, l, cfg, li, &logits, forced.as_deref(), pool, None, None, out)
            .is_some()
        {
            // CMF_DSV4_MOE_CHECK=1 recomputes the same block on the CPU and
            // reports where they part. A wrong MoE does not fail — it answers
            // differently — and the toy agreed bit for bit while the release
            // did not, so the difference lives in something the toy has no
            // instance of. Only a per-layer number will say which.
            if std::env::var("CMF_DSV4_MOE_CHECK").is_ok() {
                let mut want = vec![0.0f32; out.len()];
                let mut acc = vec![0.0f32; cfg.dim];
                for (e, &ei) in idx.iter().enumerate() {
                    let Some(exp) = l.experts.get(ei) else { continue };
                    run_expert(hidden, exp, cfg, w.get(e).copied().unwrap_or(0.0), pool, &mut acc);
                    for (o, a) in want.iter_mut().zip(&acc) {
                        *o += a;
                    }
                }
                run_expert(hidden, &l.shared, cfg, 1.0, pool, &mut acc);
                for (o, a) in want.iter_mut().zip(&acc) {
                    *o += a;
                }
                let num: f32 = want.iter().zip(out.iter()).map(|(a, b)| (a - b) * (a - b)).sum();
                let den: f32 = want.iter().map(|a| a * a).sum::<f32>().max(1e-20);
                let rel = (num / den).sqrt();
                if rel > 1e-3 {
                    let packed = pack_for(l, cfg, li).map_or(0, |p| p.globals.len());
                    eprintln!(
                        "[кадр MoE] слой {li}: расхождение {rel:.3e} | выбрано {} | \
                         упаковано {packed} из {} | хеш={} | смещение={}",
                        idx.len(),
                        cfg.n_routed_experts,
                        l.tid2eid.is_some(),
                        l.gate_bias.is_some()
                    );
                }
            }
            return;
        }
    }
    // Cheap tally for the batching question: how many DISTINCT experts a
    // group of tokens reaches. If five tokens want thirty different experts,
    // a batched MoE reads thirty weights and amortises nothing — which is
    // the difference between a speculative verify that pays for itself and
    // one that does not. Disarmed it costs one thread-local read.
    PICK_TALLY.with(|t| {
        if let Some(v) = t.borrow_mut().as_mut() {
            v.push((li, idx.to_vec()));
        }
    });
    if dump_path().is_some() {
        PICKED.with(|p| {
            let mut p = p.borrow_mut();
            if p.len() <= li {
                p.resize(li + 1, Vec::new());
            }
            p[li] = idx.clone();
        });
    }
    // One submission for the whole block — the chosen experts plus the
    // shared one. Per-expert dispatches are what made MoE slow elsewhere,
    // and the device keeps the weights across tokens, so the cost is the
    // arithmetic rather than the traffic. A refusal (missing kernel, mixed
    // layouts, weights that do not fit the budget) falls to the CPU whole,
    // never half.
    // CORRECT but SLOWER, so off by default. Parity holds on real weights
    // (perplexity 6.808 → 6.839 at 64 tokens, 5.102 → 5.146 at 200), and an
    // honest alternating A/B says 1.0 tok/s against the CPU's 2.2. The first
    // measurement claimed the opposite — 0.7 → 2.0 — because the CPU arm ran
    // first and paged in 158 GB for the GPU arm to inherit.
    //
    // The cost is not arithmetic, it is round trips: this submits and reads
    // back once per layer, forty-three times a token, and a discrete card
    // charges milliseconds for each. Fixing it means one submission per
    // token — the whole-token graph — not a faster kernel.
    //
    // `CMF_DSV4_GPU_MOE=1` opts in.
    fn gpu_moe_on() -> bool {
        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
        *ON.get_or_init(|| std::env::var("CMF_DSV4_GPU_MOE").is_ok_and(|v| v != "0"))
    }
    if gpu_moe_on() && crate::gpu::enabled_here() {
        let mut jobs = Vec::with_capacity(idx.len() + 1);
        let mut model_ref = None;
        let mut ok = true;
        for (e, &ei) in idx.iter().enumerate() {
            let Some(exp) = l.experts.get(ei) else { continue };
            ok &= crate::pipeline::moe_push_job_parts(
                &exp.w1,
                &exp.w3,
                &exp.w2,
                hidden,
                w.get(e).copied().unwrap_or(0.0),
                cfg.swiglu_limit,
                &mut jobs,
                &mut model_ref,
            )
            .is_some();
        }
        ok &= crate::pipeline::moe_push_job_parts(
            &l.shared.w1,
            &l.shared.w3,
            &l.shared.w2,
            hidden,
            1.0,
            cfg.swiglu_limit,
            &mut jobs,
            &mut model_ref,
        )
        .is_some();
        if ok {
            if let Some(m) = model_ref.as_ref() {
                if crate::gpu::moe_block(m, &jobs, out) {
                    // CMF_DSV4_GPU_CHECK=1 recomputes the same block on the
                    // CPU and reports the divergence. A GPU MoE that is wrong
                    // does not fail — it answers differently — so the only way
                    // to know is to ask both.
                    if std::env::var("CMF_DSV4_GPU_CHECK").is_ok() {
                        let mut want = vec![0.0f32; out.len()];
                        let mut acc = vec![0.0f32; cfg.dim];
                        for (e, &ei) in idx.iter().enumerate() {
                            let Some(exp) = l.experts.get(ei) else { continue };
                            run_expert(
                                hidden, exp, cfg,
                                w.get(e).copied().unwrap_or(0.0), pool, &mut acc,
                            );
                            for (o, a) in want.iter_mut().zip(&acc) {
                                *o += a;
                            }
                        }
                        run_expert(hidden, &l.shared, cfg, 1.0, pool, &mut acc);
                        for (o, a) in want.iter_mut().zip(&acc) {
                            *o += a;
                        }
                        let num: f32 = want
                            .iter()
                            .zip(out.iter())
                            .map(|(a, b)| (a - b) * (a - b))
                            .sum();
                        let den: f32 = want.iter().map(|a| a * a).sum::<f32>().max(1e-20);
                        eprintln!(
                            "[dsv4-gpu] слой {li}: расхождение {:.3e} | |CPU|={:.5} |GPU|={:.5} | экспертов {}",
                            (num / den).sqrt(),
                            den.sqrt(),
                            out.iter().map(|x| x * x).sum::<f32>().sqrt(),
                            jobs.len()
                        );
                    }
                    return;
                }
            }
        }
    }
    out.fill(0.0);
    let mut acc = vec![0.0f32; cfg.dim];
    for (e, &ei) in idx.iter().enumerate() {
        let Some(exp) = l.experts.get(ei) else {
            continue;
        };
        run_expert(
            hidden,
            exp,
            cfg,
            w.get(e).copied().unwrap_or(0.0),
            pool,
            &mut acc,
        );
        for (o, a) in out.iter_mut().zip(&acc) {
            *o += a;
        }
    }
    // The shared expert always runs, at weight 1.
    run_expert(hidden, &l.shared, cfg, 1.0, pool, &mut acc);
    for (o, a) in out.iter_mut().zip(&acc) {
        *o += a;
    }
}

/// The routed and shared experts both come through here, so the clamp and
/// the weight folding have exactly one implementation — `expert_swiglu`.
fn run_expert(
    x: &[f32],
    e: &Dsv4Expert,
    cfg: &Dsv4Cfg,
    weight: f32,
    pool: Option<&crate::pool::Pool>,
    out: &mut [f32],
) {
    expert_swiglu(
        x,
        &|src, dst| e.w1.matvec(src, dst, pool),
        &|src, dst| e.w3.matvec(src, dst, pool),
        &|src, dst| e.w2.matvec(src, dst, pool),
        cfg.moe_inter,
        weight,
        cfg.swiglu_limit,
        out,
    );
}

/// The same expert computation for several inputs, streaming each selected
/// weight once. Used by DSpark's trained five-position block: running five
/// ordinary `moe_step`s rereads the shared expert five times and every
/// coincident routed expert once per position.
fn moe_step_block(
    xs: &[f32],
    b: usize,
    l: &Dsv4Layer,
    cfg: &Dsv4Cfg,
    token_ids: &[u32],
    tally_layer: usize,
    pool: Option<&crate::pool::Pool>,
    out: &mut [f32],
) {
    let (dim, inter) = (cfg.dim, cfg.moe_inter);
    debug_assert_eq!(xs.len(), b * dim);
    debug_assert_eq!(out.len(), b * dim);
    out.fill(0.0);

    let mut logits = vec![0.0f32; b * cfg.n_routed_experts];
    l.gate.matmat(xs, b, &mut logits, pool);
    let mut picks: Vec<Vec<usize>> = Vec::with_capacity(b);
    let mut weights: Vec<Vec<f32>> = Vec::with_capacity(b);
    for bi in 0..b {
        let mut idx = Vec::new();
        let mut wt = Vec::new();
        let forced = l.tid2eid.as_ref().map(|tbl| {
            hash_route(
                tbl,
                cfg.vocab,
                cfg.top_k,
                token_ids.get(bi).copied().unwrap_or(0),
            )
        });
        route(
            &logits[bi * cfg.n_routed_experts..(bi + 1) * cfg.n_routed_experts],
            l.gate_bias.as_deref(),
            cfg.top_k,
            cfg.route_scale,
            forced.as_deref(),
            l.mask.as_deref(),
            &mut idx,
            &mut wt,
        );
        PICK_TALLY.with(|t| {
            if let Some(v) = t.borrow_mut().as_mut() {
                v.push((tally_layer, idx.clone()));
            }
        });
        picks.push(idx);
        weights.push(wt);
    }

    // Preserve the scalar path's accumulation order by keeping every routed
    // slot separate; grouping below changes only when a weight is read.
    let mut routed = vec![0.0f32; b * cfg.top_k * dim];
    for ei in 0..l.experts.len() {
        let mut jobs = Vec::new();
        for bi in 0..b {
            for (slot, &picked) in picks[bi].iter().enumerate() {
                if picked == ei {
                    jobs.push((bi, slot, weights[bi][slot]));
                }
            }
        }
        if jobs.is_empty() {
            continue;
        }
        let e = &l.experts[ei];
        let n = jobs.len();
        let mut xj = vec![0.0f32; n * dim];
        for (j, &(bi, _, _)) in jobs.iter().enumerate() {
            xj[j * dim..(j + 1) * dim]
                .copy_from_slice(&xs[bi * dim..(bi + 1) * dim]);
        }
        let mut gate = vec![0.0f32; n * inter];
        let mut up = vec![0.0f32; n * inter];
        e.w1.matmat(&xj, n, &mut gate, pool);
        e.w3.matmat(&xj, n, &mut up, pool);
        for (j, &(_, _, wt)) in jobs.iter().enumerate() {
            let (gj, uj) = (
                &mut gate[j * inter..(j + 1) * inter],
                &mut up[j * inter..(j + 1) * inter],
            );
            if cfg.swiglu_limit > 0.0 {
                for u in uj.iter_mut() {
                    *u = u.clamp(-cfg.swiglu_limit, cfg.swiglu_limit);
                }
                for g in gj.iter_mut() {
                    *g = g.min(cfg.swiglu_limit);
                }
            }
            for (g, &u) in gj.iter_mut().zip(uj.iter()) {
                *g = (*g / (1.0 + (-*g).exp())) * u * wt;
            }
        }
        let mut down = vec![0.0f32; n * dim];
        e.w2.matmat(&gate, n, &mut down, pool);
        for (j, &(bi, slot, _)) in jobs.iter().enumerate() {
            routed[(bi * cfg.top_k + slot) * dim..(bi * cfg.top_k + slot + 1) * dim]
                .copy_from_slice(&down[j * dim..(j + 1) * dim]);
        }
    }

    // Shared expert: all positions always use it, so this is the highest
    // certainty weight-sharing win in the block.
    let mut sg = vec![0.0f32; b * inter];
    let mut su = vec![0.0f32; b * inter];
    l.shared.w1.matmat(xs, b, &mut sg, pool);
    l.shared.w3.matmat(xs, b, &mut su, pool);
    for bi in 0..b {
        let (gj, uj) = (
            &mut sg[bi * inter..(bi + 1) * inter],
            &mut su[bi * inter..(bi + 1) * inter],
        );
        if cfg.swiglu_limit > 0.0 {
            for u in uj.iter_mut() {
                *u = u.clamp(-cfg.swiglu_limit, cfg.swiglu_limit);
            }
            for g in gj.iter_mut() {
                *g = g.min(cfg.swiglu_limit);
            }
        }
        for (g, &u) in gj.iter_mut().zip(uj.iter()) {
            *g = (*g / (1.0 + (-*g).exp())) * u;
        }
    }
    let mut shared = vec![0.0f32; b * dim];
    l.shared.w2.matmat(&sg, b, &mut shared, pool);

    for bi in 0..b {
        let dst = &mut out[bi * dim..(bi + 1) * dim];
        for slot in 0..picks[bi].len() {
            let src = &routed[(bi * cfg.top_k + slot) * dim
                ..(bi * cfg.top_k + slot + 1) * dim];
            for (o, &v) in dst.iter_mut().zip(src) {
                *o += v;
            }
        }
        for (o, &v) in dst.iter_mut().zip(&shared[bi * dim..(bi + 1) * dim]) {
            *o += v;
        }
    }
}

/// Grouped output projection for a block. `wo_a` cannot use a plain matmat
/// because each group sees a different attention slice; reading a quantized
/// row once and applying it to every block position gives the same dot order
/// without rereading/dequantizing that row B times.
fn o_project_block(
    attn: &[f32],
    b: usize,
    wo_a: &crate::qtensor::QTensor,
    wo_b: &crate::qtensor::QTensor,
    groups: usize,
    lora: usize,
    pool: Option<&crate::pool::Pool>,
    out: &mut [f32],
) {
    let attn_len = attn.len() / b;
    let per_group = attn_len / groups;
    let rows = groups * lora;
    let mut mid = vec![0.0f32; b * rows];
    let mid_addr = crate::pool::SendMut::new(mid.as_mut_ptr());
    let run = |start: usize, end: usize| {
        let mut wr = vec![0.0f32; wo_a.cols()];
        for r in start..end {
            wo_a.row_f32(r, &mut wr);
            let group = r / lora;
            for bi in 0..b {
                let x = &attn[bi * attn_len + group * per_group
                    ..bi * attn_len + (group + 1) * per_group];
                let v = wr.iter().zip(x).map(|(w, x)| w * x).sum();
                unsafe { *mid_addr.at(bi * rows + r) = v };
            }
        }
    };
    match pool {
        Some(p) if rows >= 256 => p.run_rows(rows, &run),
        _ => run(0, rows),
    }
    wo_b.matmat(&mid, b, out, pool);
}

/// `CMF_DSV4_TRACE=1` prints the hidden state's RMS after each half-block and
/// the logits' shape at the end. A 300B model that decodes nonsense gives no
/// other handle: this says whether the state grew, collapsed or went
/// non-finite, and at which layer — before anyone reaches for a debugger on a
/// hundred-gigabyte file.
fn no_compressed() -> bool {
    static OFF: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *OFF.get_or_init(|| std::env::var("CMF_DSV4_NO_COMPRESSED").is_ok_and(|v| v != "0"))
}

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

fn rms_of(v: &[f32]) -> f32 {
    if v.is_empty() {
        return 0.0;
    }
    (v.iter().map(|x| x * x).sum::<f32>() / v.len() as f32).sqrt()
}

/// `CMF_DSV4_DUMP=<path>` appends one JSON line per token: the embedding, the
/// hyper-connection state after every layer, the folded-and-normed head input
/// and the logits. It exists to be diffed against the reference forward on
/// the same weights — the numerical parity this port has never had, which at
/// toy scale is a few thousand floats and entirely tractable.
thread_local! {
    /// The attention body's input and output per layer, interleaved.
    static BODY: std::cell::RefCell<Vec<String>> = const { std::cell::RefCell::new(Vec::new()) };
    /// Experts chosen per layer for the token being decoded — the dump needs
    /// them, because two implementations that pick DIFFERENT experts diverge
    /// hugely for a reason that is not a bug in either.
    static PICKED: std::cell::RefCell<Vec<Vec<usize>>> =
        const { std::cell::RefCell::new(Vec::new()) };
    /// (layer, chosen experts) in call order, when armed.
    static PICK_TALLY: std::cell::RefCell<Option<Vec<(usize, Vec<usize>)>>> =
        const { std::cell::RefCell::new(None) };
}

/// Start recording expert picks. Idempotent; the previous tally is dropped.
pub fn pick_tally_arm() {
    PICK_TALLY.with(|t| *t.borrow_mut() = Some(Vec::new()));
}

/// Take what was recorded and stop recording.
pub fn pick_tally_take() -> Vec<(usize, Vec<usize>)> {
    PICK_TALLY.with(|t| t.borrow_mut().take().unwrap_or_default())
}

/// How many distinct experts a set of per-token pick lists reaches, and how
/// many picks it makes. The ratio is what a batched MoE can hope to save.
pub fn tally_unique(picks: &[(usize, Vec<usize>)]) -> (usize, usize) {
    // Keyed by (layer, expert). Expert 17 of layer 3 and expert 17 of layer 4
    // are different weights, and counting them as one understated the traffic
    // a batch has to read — badly for the draft, whose three stages each have
    // their own 256.
    let mut seen = std::collections::HashSet::new();
    let mut total = 0;
    for (li, v) in picks {
        total += v.len();
        for &e in v {
            seen.insert((*li, e));
        }
    }
    (seen.len(), total)
}

fn dump_path() -> Option<&'static str> {
    static P: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
    P.get_or_init(|| std::env::var("CMF_DSV4_DUMP").ok())
        .as_deref()
}

fn dump_line(json: &str) {
    if let Some(p) = dump_path() {
        use std::io::Write as _;
        if let Ok(mut f) = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(p)
        {
            let _ = writeln!(f, "{json}");
        }
    }
}

fn vec_json(v: &[f32]) -> String {
    let mut s = String::with_capacity(v.len() * 9);
    s.push('[');
    for (i, x) in v.iter().enumerate() {
        if i > 0 {
            s.push(',');
        }
        s.push_str(&format!("{x:.6e}"));
    }
    s.push(']');
    s
}

/// One token through the whole stack.
///
/// The hidden state is `hc_mult` copies of a `dim`-vector from the very
/// first line to the very last: the embedding is replicated, every layer
/// folds/expands around its two halves, and only `hc_head_fold` collapses
/// it before the output norm and the head. There is no point in this
/// function where an ordinary residual would fit.
#[allow(clippy::too_many_arguments)]
/// A chunk of prompt tokens. Stage one of the batched prefill (see
/// docs/DSV4_PREFILL.md): the walk itself, with the head skipped for every
/// token but the last.
///
/// Prefill costs `len × per-token` today, and on a 2500-token prompt that is
/// a minute and a half before the first word. The stages that follow batch
/// the weight reads — which is where the nine-fold gap to the bandwidth
/// floor lives — but this one is the scaffolding they hang on, and it
/// already stops computing 129 280 logits for tokens nobody asks about.
#[allow(clippy::too_many_arguments)]
/// `CMF_DSV4_BATCH=N` — how many prompt tokens go through the card in one
/// submission. 1 keeps the walk. The chunk still bounds it: a batch never
/// spans two chunks, so cancellation stays as responsive as it was.
fn batch_prefill() -> usize {
    static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *N.get_or_init(|| {
        std::env::var("CMF_DSV4_BATCH")
            .ok()
            .and_then(|v| v.parse::<usize>().ok())
            .filter(|&n| (1..=32).contains(&n))
            .unwrap_or(1)
    })
}

/// The prompt as batches instead of a walk, when every layer will take one.
///
/// Refuses before touching any state, never half way: the caller's fallback
/// is the per-token walk, and a batch that advanced the caches and then gave
/// up would have them advanced twice. So everything that can decline is asked
/// first, and after the first dispatch the only outcomes are success and a
/// hard failure.
///
/// Hash layers are the one shape it cannot take: their expert list is forced
/// by the TOKEN's id and the layer description carries one list, not one per
/// token. The release has three of them (0, 1, 2); a file without them
/// batches the whole stack.
#[allow(clippy::too_many_arguments)]
fn forward_chunk_batched(
    g: &Dsv4Globals,
    layers: &[Dsv4Layer],
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    ids: &[u32],
    pos0: usize,
    inv_freq: &[f32],
    pool: Option<&crate::pool::Pool>,
    logits: &mut Vec<f32>,
    want_logits: bool,
) -> bool {
    #[cfg(not(feature = "gpu"))]
    {
        let _ = (
            g, layers, cfg, st, ids, pos0, inv_freq, pool, logits, want_logits,
        );
        false
    }
    #[cfg(feature = "gpu")]
    {
        let b = ids.len();
        // The batch encoder currently requires a complete expert pack. A
        // partial layer is still device-owned for decode, but becomes part of
        // the causal host tail here instead of being silently treated as a
        // full chain layer.
        let gpu_end = st
            .dev_set
            .iter()
            .enumerate()
            .position(|(li, &on)| {
                !on
                    || pack_for(&layers[li], cfg, li)
                        .is_none_or(|p| p.globals.len() < cfg.n_routed_experts)
            })
            .unwrap_or(st.dev_set.len());
        let why = if b < 2 {
            "токенов меньше двух"
        } else if !chain_enabled() {
            "цепочка выключена"
        } else if !st.dev_owned {
            "карта ещё не владеет состоянием"
        } else if st.dev_set.len() != layers.len() {
            "набор слоёв ещё не зафиксирован"
        } else if gpu_end == 0
            || st.dev_set[gpu_end.min(st.dev_set.len())..]
                .iter()
                .enumerate()
                .any(|(i, &on)| {
                    on && !st.partial_set.get(gpu_end + i).copied().unwrap_or(false)
                })
        {
            "слои на карте не образуют префикс"
        } else {
            ""
        };
        if !why.is_empty() {
            static SAID: std::sync::Once = std::sync::Once::new();
            SAID.call_once(|| tracing::warn!("dsv4: пакет отказал — {why}"));
            return false;
        }
        let (hc, dim) = (cfg.hc_mult, cfg.dim);
        let mut emb = vec![0.0f32; dim];
        for (t, &id) in ids.iter().enumerate() {
            let mut state = vec![0.0f32; hc * dim];
            g.embed.row_f32(id as usize, &mut emb);
            for j in 0..hc {
                state[j * dim..(j + 1) * dim].copy_from_slice(&emb);
            }
            let (folded, post0, comb0) = hc_fold_norm(
                &state,
                &layers[0].hc_attn_fn,
                &layers[0].hc_attn_scale,
                &layers[0].hc_attn_base,
                &layers[0].attn_norm,
                cfg,
                pool,
            );
            let mut qn0 = vec![0.0f32; layers[0].wq_a.rows()];
            layers[0].wq_a.matvec(&folded, &mut qn0, pool);
            rms_weighted(&mut qn0, &layers[0].q_norm, cfg.norm_eps);
            if !crate::gpu_wgpu::dsv4_state_write_t(&state, t)
                || !crate::gpu_wgpu::dsv4_hc_write_t(&post0, &comb0, t)
                || !crate::gpu_wgpu::dsv4_chain_seed_t(&folded, &qn0, t)
                || !crate::gpu_wgpu::dsv4_chain_seed_bt(t, b, &state, &post0, &comb0, &folded, &qn0)
            {
                return false;
            }
        }
        let run: Vec<usize> = (0..gpu_end).collect();
        let mut folded = Vec::new();
        let mut states = vec![0.0f32; b * hc * dim];
        st.pos = pos0;
        if !dsv4_chain_run(
            layers,
            &run,
            cfg,
            g,
            st,
            *ids.last().unwrap(),
            &mut folded,
            Some(&mut states),
            b,
            ids,
            true,
            pool,
        ) {
            return false;
        }
        // Finish the trailing host layers in causal token order. Their KV
        // caches are host-owned, while the device prefix advanced its own
        // caches inside the one submission above. On the release this loop
        // is exactly layer 42; keeping it general makes smaller VRAM budgets
        // correct as long as the resident layers remain one prefix.
        let mut scratch = HcScratch::new(cfg);
        for t in 0..b {
            st.pos = pos0 + t;
            let state = &mut states[t * hc * dim..(t + 1) * hc * dim];
            for (li, l) in layers.iter().enumerate().skip(gpu_end) {
                let freqs = if l.compressor.is_some() {
                    &g.inv_freq_compress
                } else {
                    &g.inv_freq_window
                };
                let freqs = if freqs.is_empty() {
                    inv_freq
                } else {
                    freqs.as_slice()
                };
                hc_block(
                    state,
                    &l.hc_attn_fn,
                    &l.hc_attn_scale,
                    &l.hc_attn_base,
                    &l.attn_norm,
                    cfg,
                    &mut scratch,
                    pool,
                    |f, o| attention_step(f, l, cfg, st, li, freqs, pool, None, o),
                );
                hc_block(
                    state,
                    &l.hc_ffn_fn,
                    &l.hc_ffn_scale,
                    &l.hc_ffn_base,
                    &l.ffn_norm,
                    cfg,
                    &mut scratch,
                    pool,
                    |f, o| {
                        if host_cpu_moe() {
                            crate::gpu::cpu_scope(|| moe_step(f, l, cfg, ids[t], li, pool, o))
                        } else {
                            moe_step(f, l, cfg, ids[t], li, pool, o)
                        }
                    },
                );
                dspark_note(li, state, cfg);
            }
        }
        st.pos = pos0 + b;
        // Said once. A gate that compares a batched prompt against a walked
        // one proves nothing if the batch quietly declined — the numbers match
        // because the same code produced both. This line is what tells the
        // two apart.
        {
            static SAID: std::sync::Once = std::sync::Once::new();
            SAID.call_once(|| tracing::warn!("dsv4: префилл пакетами по {b}"));
        }
        // Only the last token's logits are read; the rest of the chunk exists
        // to fill the caches. The head consumes the hyper-connection state,
        // not the chain's intermediate fold — skipping this final learned
        // fold used to make a full-device batch fast and wrong.
        if want_logits {
            let last = &states[(b - 1) * hc * dim..b * hc * dim];
            let mut h = vec![0.0f32; dim];
            hc_head_fold(
                last,
                &g.hc_head_fn,
                g.hc_head_scale,
                &g.hc_head_base,
                cfg,
                pool,
                &mut h,
            );
            rms_weighted(&mut h, &g.norm, cfg.norm_eps);
            logits.resize(cfg.vocab, 0.0);
            g.head.matvec(&h, logits, pool);
        } else {
            logits.clear();
        }
        true
    }
}

/// Everything a speculative verify must be able to put back.
///
/// Device caches roll back by restore-then-replay: the shadow puts the
/// window rings and compressor streams where they were BEFORE the pass, and
/// the replay re-appends the accepted tokens' state from the hidden inputs
/// the pass retained. Append-only regions roll back by count. Host-owned
/// tail layers roll back by clone-and-rewalk.
#[cfg(feature = "gpu")]
pub struct Dsv4SpecTxn {
    pos0: usize,
    batch: usize,
    gpu_end: usize,
    dev_filled: Vec<usize>,
    dev_n_comp: Vec<usize>,
    dev_n_ix: Vec<usize>,
    host: Vec<(usize, HostLayerSnap)>,
    /// Per host layer, per verified token: the layer's state right after
    /// that token's attention — what a rollback restores INSTEAD of
    /// re-walking the tail it already walked (the values are identical;
    /// only the side effects were ever needed).
    host_steps: Vec<(usize, Vec<HostLayerSnap>)>,
    /// Every token's hyper-connection state as it left the device prefix,
    /// BEFORE the host tail walked (and mutated) anything: the rewalk's
    /// input, and the head's.
    pub states: Vec<f32>,
    shadow: Option<crate::gpu_wgpu::Dsv4SpecShadow>,
}

#[cfg(feature = "gpu")]
struct HostLayerSnap {
    window: Vec<f32>,
    compressed: Vec<f32>,
    index_kv: Vec<f32>,
    pending_kv: Vec<f32>,
    pending_score: Vec<f32>,
    prev_kv: Vec<f32>,
    prev_score: Vec<f32>,
    pending_ix_kv: Vec<f32>,
    pending_ix_score: Vec<f32>,
    prev_ix_kv: Vec<f32>,
    prev_ix_score: Vec<f32>,
}

#[cfg(feature = "gpu")]
fn host_snap(st: &Dsv4State, li: usize) -> HostLayerSnap {
    HostLayerSnap {
        window: st.window[li].clone(),
        compressed: st.compressed[li].clone(),
        index_kv: st.index_kv[li].clone(),
        pending_kv: st.pending_kv[li].clone(),
        pending_score: st.pending_score[li].clone(),
        prev_kv: st.prev_kv[li].clone(),
        prev_score: st.prev_score[li].clone(),
        pending_ix_kv: st.pending_ix_kv[li].clone(),
        pending_ix_score: st.pending_ix_score[li].clone(),
        prev_ix_kv: st.prev_ix_kv[li].clone(),
        prev_ix_score: st.prev_ix_score[li].clone(),
    }
}

#[cfg(feature = "gpu")]
fn host_restore(st: &mut Dsv4State, li: usize, s: &HostLayerSnap) {
    st.window[li] = s.window.clone();
    st.compressed[li] = s.compressed.clone();
    st.index_kv[li] = s.index_kv.clone();
    st.pending_kv[li] = s.pending_kv.clone();
    st.pending_score[li] = s.pending_score.clone();
    st.prev_kv[li] = s.prev_kv.clone();
    st.prev_score[li] = s.prev_score.clone();
    st.pending_ix_kv[li] = s.pending_ix_kv.clone();
    st.pending_ix_score[li] = s.pending_ix_score.clone();
    st.prev_ix_kv[li] = s.prev_ix_kv.clone();
    st.prev_ix_score[li] = s.prev_ix_score.clone();
}

/// One host-tail walk of token `t`'s state through layers `gpu_end..`,
/// mutating `state` in place and the layers' host caches. Exactly the loop
/// the batch runs, factored so the verify can re-run it for accepted tokens.
#[cfg(feature = "gpu")]
#[allow(clippy::too_many_arguments)]
fn host_tail_walk(
    g: &Dsv4Globals,
    layers: &[Dsv4Layer],
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    gpu_end: usize,
    state: &mut [f32],
    token_id: u32,
    pos: usize,
    inv_freq: &[f32],
    scratch: &mut HcScratch,
    pool: Option<&crate::pool::Pool>,
) {
    st.pos = pos;
    for (li, l) in layers.iter().enumerate().skip(gpu_end) {
        let freqs = if l.compressor.is_some() {
            &g.inv_freq_compress
        } else {
            &g.inv_freq_window
        };
        let freqs = if freqs.is_empty() { inv_freq } else { freqs.as_slice() };
        hc_block(
            state,
            &l.hc_attn_fn,
            &l.hc_attn_scale,
            &l.hc_attn_base,
            &l.attn_norm,
            cfg,
            scratch,
            pool,
            |f, o| attention_step(f, l, cfg, st, li, freqs, pool, None, o),
        );
        hc_block(
            state,
            &l.hc_ffn_fn,
            &l.hc_ffn_scale,
            &l.hc_ffn_base,
            &l.ffn_norm,
            cfg,
            scratch,
            pool,
            |f, o| {
                if host_cpu_moe() {
                    crate::gpu::cpu_scope(|| moe_step(f, l, cfg, token_id, li, pool, o))
                } else {
                    moe_step(f, l, cfg, token_id, li, pool, o)
                }
            },
        );
        dspark_note(li, state, cfg);
    }
}

/// The host tail for a whole batch: attention stays causal per token (its
/// window mutates), the MoE half runs through the block-grouped path — the
/// same accumulation order as the position walk, which the block tests pin
/// bit for bit. This is the verify's tail; the single-token paths keep
/// `hc_block`.
#[cfg(feature = "gpu")]
#[allow(clippy::too_many_arguments)]
fn host_tail_walk_batch(
    g: &Dsv4Globals,
    layers: &[Dsv4Layer],
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    gpu_end: usize,
    states: &mut [f32],
    ids: &[u32],
    pos0: usize,
    b: usize,
    inv_freq: &[f32],
    scratch: &mut HcScratch,
    pool: Option<&crate::pool::Pool>,
    mut steps: Option<&mut Vec<(usize, Vec<HostLayerSnap>)>>,
) {
    let (hc, dim) = (cfg.hc_mult, cfg.dim);
    let mix_hc = (2 + hc) * hc;
    let mut folds = vec![0.0f32; b * dim];
    let mut mo = vec![0.0f32; b * dim];
    let mut posts = vec![0.0f32; b * hc];
    let mut combs = vec![0.0f32; b * hc * hc];
    let mut resid = vec![0.0f32; b * hc * dim];
    let spec_time = {
        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
        *ON.get_or_init(|| std::env::var("CMF_DSV4_SPEC_TIME").is_ok_and(|v| v != "0"))
    };
    for (li, l) in layers.iter().enumerate().skip(gpu_end) {
        let t_attn = std::time::Instant::now();
        let freqs = if l.compressor.is_some() {
            &g.inv_freq_compress
        } else {
            &g.inv_freq_window
        };
        let freqs = if freqs.is_empty() { inv_freq } else { freqs.as_slice() };
        for t in 0..b {
            st.pos = pos0 + t;
            let state = &mut states[t * hc * dim..(t + 1) * hc * dim];
            hc_block(
                state,
                &l.hc_attn_fn,
                &l.hc_attn_scale,
                &l.hc_attn_base,
                &l.attn_norm,
                cfg,
                scratch,
                pool,
                |f, o| attention_step(f, l, cfg, st, li, freqs, pool, None, o),
            );
            if let Some(steps) = steps.as_mut() {
                match steps.iter_mut().find(|(l, _)| *l == li) {
                    Some((_, v)) => v.push(host_snap(st, li)),
                    None => steps.push((li, vec![host_snap(st, li)])),
                }
            }
        }
        let t_glue = std::time::Instant::now();
        for t in 0..b {
            let state = &states[t * hc * dim..(t + 1) * hc * dim];
            hc_mixes(state, &l.hc_ffn_fn, mix_hc, cfg.norm_eps, pool, &mut scratch.mixes);
            hc_split_sinkhorn(
                &scratch.mixes,
                &l.hc_ffn_scale,
                &l.hc_ffn_base,
                hc,
                cfg.hc_sinkhorn_iters,
                cfg.hc_eps,
                &mut scratch.pre,
                &mut posts[t * hc..(t + 1) * hc],
                &mut combs[t * hc * hc..(t + 1) * hc * hc],
            );
            let fold = &mut folds[t * dim..(t + 1) * dim];
            hc_fold(state, &scratch.pre, hc, dim, fold);
            let ms = fold.iter().map(|v| v * v).sum::<f32>() / dim as f32;
            let inv = 1.0 / (ms + cfg.norm_eps).sqrt();
            for (v, w) in fold.iter_mut().zip(&l.ffn_norm) {
                *v = *v * inv * w;
            }
            resid[t * hc * dim..(t + 1) * hc * dim]
                .copy_from_slice(&states[t * hc * dim..(t + 1) * hc * dim]);
        }
        let t_moe = std::time::Instant::now();
        // A tail layer with a device expert pack (partial or full) runs its
        // hot winners on the card per token and completes the cold ones on
        // the host — the same exact split the partial walk uses. Default on
        // (measured: the tail fell 27.4 → 18.2 ms of the verify round);
        // `CMF_DSV4_TAIL_PACK=0` restores the batched host block.
        let tail_pack = {
            static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
            *ON.get_or_init(|| {
                std::env::var("CMF_DSV4_TAIL_PACK").map(|v| v != "0").unwrap_or(true)
            })
        };
        let mut packed_done = false;
        if tail_pack && pack_for(l, cfg, li).is_some() {
            packed_done = true;
            for t in 0..b {
                let f = &folds[t * dim..(t + 1) * dim];
                let forced = l.tid2eid.as_ref().map(|tbl| {
                    hash_route(tbl, cfg.vocab, cfg.top_k, ids.get(t).copied().unwrap_or(0))
                });
                let o = &mut mo[t * dim..(t + 1) * dim];
                match moe_frame(f, l, cfg, li, &[], forced.as_deref(), pool, None, None, o) {
                    Some((cold_sum, n)) => {
                        if n > 0 {
                            for (od, cd) in o.iter_mut().zip(cold_sum.iter()) {
                                *od += cd;
                            }
                        }
                    }
                    None => {
                        packed_done = false;
                        break;
                    }
                }
            }
        }
        if !packed_done {
            if host_cpu_moe() {
                crate::gpu::cpu_scope(|| {
                    moe_step_block(&folds, b, l, cfg, ids, li, pool, &mut mo)
                });
            } else {
                moe_step_block(&folds, b, l, cfg, ids, li, pool, &mut mo);
            }
        }
        let t_exp = std::time::Instant::now();
        for t in 0..b {
            let state = &mut states[t * hc * dim..(t + 1) * hc * dim];
            hc_expand(
                &mo[t * dim..(t + 1) * dim],
                &resid[t * hc * dim..(t + 1) * hc * dim],
                &posts[t * hc..(t + 1) * hc],
                &combs[t * hc * hc..(t + 1) * hc * hc],
                hc,
                dim,
                state,
            );
            dspark_note(li, state, cfg);
        }
        if spec_time {
            eprintln!(
                "хвост слоя {li}: attn {:.1} мс, клей {:.1}, moe {:.1}, expand {:.1}",
                (t_glue - t_attn).as_secs_f64() * 1e3,
                (t_moe - t_glue).as_secs_f64() * 1e3,
                (t_exp - t_moe).as_secs_f64() * 1e3,
                t_exp.elapsed().as_secs_f64() * 1e3,
            );
        }
    }
}

/// A speculative verify pass: run `ids` (the committed next token followed
/// by draft proposals) at positions `pos0..pos0+B` through the trunk in one
/// batched submission, WITHOUT giving up the ability to roll back, and
/// return every position's greedy answer. The caller decides the accepted
/// prefix and calls [`dsv4_spec_finish`], which either keeps everything
/// (`accepted == B`) or restores-and-replays to the accepted length.
///
/// `logits_out` takes B rows of vocab logits, `argmax_out` their argmaxes.
#[cfg(feature = "gpu")]
#[allow(clippy::too_many_arguments)]
pub fn dsv4_verify_chunk(
    g: &Dsv4Globals,
    layers: &[Dsv4Layer],
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    ids: &[u32],
    pos0: usize,
    inv_freq: &[f32],
    pool: Option<&crate::pool::Pool>,
    cap_targets: &[usize],
    argmax_out: &mut Vec<u32>,
    logits_out: &mut Vec<f32>,
    walked_out: &mut Vec<f32>,
) -> Option<Dsv4SpecTxn> {
    let b = ids.len();
    let gpu_end = st
        .dev_set
        .iter()
        .enumerate()
        .position(|(li, &on)| {
            !on || pack_for(&layers[li], cfg, li)
                .is_none_or(|p| p.globals.len() < cfg.n_routed_experts)
        })
        .unwrap_or(st.dev_set.len());
    // A PARTIAL layer past the prefix is fine: it walks in the host tail
    // like any host layer. Only a FULL device layer out there means the
    // prefix assumption is really broken.
    let full_beyond = st.dev_set[gpu_end.min(st.dev_set.len())..]
        .iter()
        .enumerate()
        .any(|(i, &on)| {
            on && !st.partial_set.get(gpu_end + i).copied().unwrap_or(false)
        });
    if b < 2
        || !chain_enabled()
        || !st.dev_owned
        || st.dev_set.len() != layers.len()
        || gpu_end == 0
        || full_beyond
    {
        return None;
    }
    let (hc, dim, hd) = (cfg.hc_mult, cfg.dim, cfg.head_dim);
    // ── the transaction ──
    let metas: Vec<(usize, usize, usize, usize)> = (0..gpu_end)
        .map(|li| (li, hd, cfg.window, st.dev_filled[li]))
        .collect();
    let shadow = crate::gpu_wgpu::dsv4_spec_shadow(st.kv_id, &metas, b)?;
    let mut txn = Dsv4SpecTxn {
        pos0,
        batch: b,
        gpu_end,
        dev_filled: st.dev_filled.clone(),
        dev_n_comp: st.dev_n_comp.clone(),
        dev_n_ix: st.dev_n_ix.clone(),
        host: (gpu_end..layers.len())
            .map(|li| (li, host_snap(st, li)))
            .collect(),
        states: Vec::new(),
        host_steps: Vec::new(),
        shadow: Some(shadow),
    };
    // The capture targets that live on the device: photograph their states.
    let dev_caps: Vec<usize> = cap_targets.iter().copied().filter(|&t| t < gpu_end).collect();
    crate::gpu_wgpu::dsv4_spec_retain_arm(gpu_end, &dev_caps);

    // ── seed and run the batch (the prefill batch's own shape) ──
    let mut emb = vec![0.0f32; dim];
    for (t, &id) in ids.iter().enumerate() {
        let mut state = vec![0.0f32; hc * dim];
        g.embed.row_f32(id as usize, &mut emb);
        for j in 0..hc {
            state[j * dim..(j + 1) * dim].copy_from_slice(&emb);
        }
        let (folded, post0, comb0) = hc_fold_norm(
            &state,
            &layers[0].hc_attn_fn,
            &layers[0].hc_attn_scale,
            &layers[0].hc_attn_base,
            &layers[0].attn_norm,
            cfg,
            pool,
        );
        let mut qn0 = vec![0.0f32; layers[0].wq_a.rows()];
        layers[0].wq_a.matvec(&folded, &mut qn0, pool);
        rms_weighted(&mut qn0, &layers[0].q_norm, cfg.norm_eps);
        if !crate::gpu_wgpu::dsv4_state_write_t(&state, t)
            || !crate::gpu_wgpu::dsv4_hc_write_t(&post0, &comb0, t)
            || !crate::gpu_wgpu::dsv4_chain_seed_t(&folded, &qn0, t)
            || !crate::gpu_wgpu::dsv4_chain_seed_bt(t, b, &state, &post0, &comb0, &folded, &qn0)
        {
            crate::gpu_wgpu::dsv4_spec_retain_arm(0, &[]);
            return None;
        }
    }
    let spec_time = {
        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
        *ON.get_or_init(|| std::env::var("CMF_DSV4_SPEC_TIME").is_ok_and(|v| v != "0"))
    };
    let t0 = std::time::Instant::now();
    let run: Vec<usize> = (0..gpu_end).collect();
    let mut folded = Vec::new();
    let mut states = vec![0.0f32; b * hc * dim];
    st.pos = pos0;
    let ok = dsv4_chain_run(
        layers,
        &run,
        cfg,
        g,
        st,
        *ids.last().unwrap(),
        &mut folded,
        Some(&mut states),
        b,
        ids,
        true,
        pool,
    );
    crate::gpu_wgpu::dsv4_spec_retain_arm(0, &[]);
    if !ok {
        // Nothing committed on the host; the device may hold half-appended
        // state, so put the snapshot back before declining.
        if let Some(sh) = txn.shadow.take() {
            let _ = crate::gpu_wgpu::dsv4_spec_restore(&sh);
        }
        st.dev_filled = txn.dev_filled;
        st.dev_n_comp = txn.dev_n_comp;
        st.dev_n_ix = txn.dev_n_ix;
        st.pos = pos0;
        return None;
    }
    txn.states = states.clone();
    let t_chain = t0.elapsed();
    if std::env::var("CMF_DSV4_FOLD_DBG").is_ok() {
        // Any indexer fold this window landed: read the entry back and
        // print a fingerprint, so the fused and per-token folds can be
        // held against each other on the release shapes.
        for li in 0..gpu_end {
            let Some(ixr) = &layers[li].indexer else { continue };
            let ratio = ixr.compressor.ratio;
            for t in 0..b {
                if (pos0 + t + 1) % ratio == 0 {
                    let ew = {
                        let w = ixr.compressor.wkv.rows();
                        if ixr.compressor.overlap { w / 2 } else { w }
                    };
                    let idx_new = txn.dev_n_ix[li] + (0..=t)
                        .filter(|k| (pos0 + k + 1) % ratio == 0)
                        .count()
                        - 1;
                    if let Some(v) =
                        crate::gpu_wgpu::dsv4_dbg_read_ix(st.kv_id, li, idx_new * ew, ew.min(8))
                    {
                        let sum: f32 = v.iter().sum();
                        eprintln!(
                            "[fold] li={li} pos={} entry={idx_new} head={:?} sum={sum:.6}",
                            pos0 + t,
                            &v[..4.min(v.len())]
                        );
                    }
                }
            }
        }
    }

    // ── host tail + every position's head ──
    let mut scratch = HcScratch::new(cfg);
    argmax_out.clear();
    logits_out.clear();
    logits_out.resize(b * cfg.vocab, 0.0);
    let mut head_in = vec![0.0f32; b * dim];
    let mut host_steps: Vec<(usize, Vec<HostLayerSnap>)> = Vec::new();
    host_tail_walk_batch(
        g, layers, cfg, st, gpu_end, &mut states, ids, pos0, b, inv_freq, &mut scratch, pool,
        Some(&mut host_steps),
    );
    txn.host_steps = host_steps;
    for t in 0..b {
        let state = &states[t * hc * dim..(t + 1) * hc * dim];
        let h = &mut head_in[t * dim..(t + 1) * dim];
        hc_head_fold(state, &g.hc_head_fn, g.hc_head_scale, &g.hc_head_base, cfg, pool, h);
        rms_weighted(h, &g.norm, cfg.norm_eps);
    }
    // One B-wide head submission instead of B fenced matvecs.
    let head_gpu = g.head.model_idx().is_some_and(|hi| {
        let model = layers[0].experts.first().and_then(|e| e.w1.model_arc());
        model.is_some_and(|m| {
            crate::gpu_wgpu::q4tp_matvec_batch_for_test(
                &m, hi, &head_in, b, cfg.vocab, dim, logits_out,
            )
        })
    });
    for t in 0..b {
        if !head_gpu {
            let h = &head_in[t * dim..(t + 1) * dim];
            g.head.matvec(h, &mut logits_out[t * cfg.vocab..(t + 1) * cfg.vocab], pool);
        }
        let row = &logits_out[t * cfg.vocab..(t + 1) * cfg.vocab];
        let mut best = 0usize;
        for v in 1..cfg.vocab {
            if row[v] > row[best] {
                best = v;
            }
        }
        argmax_out.push(best as u32);
    }
    walked_out.clear();
    walked_out.extend_from_slice(&states);
    st.pos = pos0 + b;
    if spec_time {
        eprintln!(
            "verify: тень+сид+цепочка {:.1} мс, хвост+голова {:.1} мс",
            t_chain.as_secs_f64() * 1e3,
            (t0.elapsed() - t_chain).as_secs_f64() * 1e3,
        );
    }
    Some(txn)
}

/// Keep the accepted prefix of a verify pass and put everything else back.
///
/// `accepted` counts the FED tokens whose state stays (at least 1 — the
/// first fed token was already committed by the caller). With
/// `accepted == batch` this is free; otherwise the device restores its
/// snapshot and replays the accepted tokens' state appends, and the host
/// tail re-walks them.
#[cfg(feature = "gpu")]
pub fn dsv4_spec_finish(
    g: &Dsv4Globals,
    layers: &[Dsv4Layer],
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    mut txn: Dsv4SpecTxn,
    accepted: usize,
    ids: &[u32],
    inv_freq: &[f32],
    pool: Option<&crate::pool::Pool>,
) -> bool {
    macro_rules! sfail {
        ($($t:tt)*) => {{
            if std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok() {
                eprintln!("spec_finish: {}", format_args!($($t)*));
            }
            return false;
        }};
    }
    let b = txn.batch;
    let k = accepted.min(b);
    let (hc, dim, hd) = (cfg.hc_mult, cfg.dim, cfg.head_dim);
    // The staged batch never slid the windows; land the accepted prefix now,
    // whatever k is.
    let win_metas: Vec<(usize, usize, usize, usize)> = (0..txn.gpu_end)
        .map(|li| (li, txn.dev_filled[li], cfg.window, hd))
        .collect();
    if !crate::gpu_wgpu::dsv4_spec_commit_windows(st.kv_id, &win_metas, b, k) {
        sfail!("коммит окон");
    }
    if k == b {
        // Every stream mutation was the walk's own kernels in walk order —
        // nothing to put back.
        return true;
    }
    // ── device: restore to the snapshot, then replay the accepted tokens ──
    let Some(sh) = txn.shadow.take() else { sfail!("нет тени") };
    if !crate::gpu_wgpu::dsv4_spec_restore(&sh) {
        sfail!("restore");
    }
    let Some(model) = layers[0].experts.first().and_then(|e| e.w1.model_arc()) else {
        sfail!("нет модели");
    };
    let mut plan: Vec<(usize, crate::gpu_wgpu::Dsv4Prep)> = Vec::new();
    let mut freqs_own: Vec<&[f32]> = Vec::new();
    for li in 0..txn.gpu_end {
        let l = &layers[li];
        let Some(wkv) = l.wkv.model_idx() else { sfail!("wkv слоя {li}") };
        let comp = match &l.compressor {
            None => None,
            Some(cp) => {
                let (Some(a), Some(bx)) = (cp.wkv.model_idx(), cp.wgate.model_idx()) else {
                    sfail!("компрессор слоя {li}");
                };
                Some((
                    crate::gpu_wgpu::Dsv4CompW { wkv: a, wgate: bx, norm: &cp.norm, ape: &cp.ape },
                    crate::gpu_wgpu::Dsv4CompGeom {
                        width: cp.wkv.rows(),
                        hidden: dim,
                        ratio: cp.ratio,
                        overlap: cp.overlap,
                        rope_dim: cfg.rope_head_dim,
                        eps: cfg.norm_eps,
                    },
                ))
            }
        };
        let ix = match &l.indexer {
            None => None,
            Some(ixr) => {
                let cp = &ixr.compressor;
                let (Some(a), Some(bx), Some(qb), Some(wp)) = (
                    cp.wkv.model_idx(),
                    cp.wgate.model_idx(),
                    ixr.wq_b.model_idx(),
                    ixr.weights_proj.model_idx(),
                ) else {
                    sfail!("индексер слоя {li}");
                };
                let ih = ixr.weights_proj.rows();
                Some((
                    crate::gpu_wgpu::Dsv4CompW { wkv: a, wgate: bx, norm: &cp.norm, ape: &cp.ape },
                    crate::gpu_wgpu::Dsv4CompGeom {
                        width: cp.wkv.rows(),
                        hidden: dim,
                        ratio: cp.ratio,
                        overlap: cp.overlap,
                        rope_dim: cfg.rope_head_dim,
                        eps: cfg.norm_eps,
                    },
                    crate::gpu_wgpu::Dsv4IxW { wq_b: qb, weights_proj: wp },
                    crate::gpu_wgpu::Dsv4IxGeom {
                        ih,
                        idim: ixr.wq_b.rows() / ih.max(1),
                        q_lora: cfg.q_lora_rank,
                        hidden: dim,
                        rope_dim: cfg.rope_head_dim,
                        eps: cfg.norm_eps,
                        top_k: cfg.index_topk,
                        window: cfg.window,
                    },
                ))
            }
        };
        let ew_c = comp.as_ref().map_or(0, |(_, cg)| {
            if cg.overlap { cg.width / 2 } else { cg.width }
        });
        let ew_i = ix.as_ref().map_or(0, |(_, cg, _, _)| {
            if cg.overlap { cg.width / 2 } else { cg.width }
        });
        let prep = crate::gpu_wgpu::Dsv4Prep {
            wkv,
            kv_norm: &l.kv_norm,
            comp,
            ix,
            filled: txn.dev_filled[li],
            window: cfg.window,
            n_comp: txn.dev_n_comp[li],
            n_ix: txn.dev_n_ix[li],
            comp_dst_off: cfg.window * hd + txn.dev_n_comp[li] * ew_c,
            ix_dst_off: txn.dev_n_ix[li] * ew_i,
            idx_cap: cfg.window + if l.indexer.is_some() { cfg.index_topk } else { 0 },
        };
        let fr = if l.compressor.is_some() {
            g.inv_freq_compress.as_slice()
        } else {
            g.inv_freq_window.as_slice()
        };
        freqs_own.push(if fr.is_empty() { inv_freq } else { fr });
        plan.push((li, prep));
    }
    if !crate::gpu_wgpu::dsv4_spec_replay(
        &model,
        &plan,
        st.kv_id,
        txn.pos0,
        b,
        k,
        &freqs_own,
        hd,
        dim,
        cfg.rope_head_dim,
        cfg.norm_eps,
        true,
    ) {
        sfail!("replay k={k}");
    }
    // ── host counts: the snapshot advanced by k tokens ──
    let advanced = |ratio: usize| -> usize {
        if ratio == 0 {
            return 0;
        }
        (0..k).filter(|t| (txn.pos0 + t + 1) % ratio == 0).count()
    };
    for li in 0..txn.gpu_end {
        let l = &layers[li];
        st.dev_filled[li] = (txn.dev_filled[li] + k).min(cfg.window);
        let ac = l.compressor.as_ref().map_or(0, |cp| advanced(cp.ratio));
        let ai = l.indexer.as_ref().map_or(0, |ix| advanced(ix.compressor.ratio));
        st.dev_n_comp[li] = txn.dev_n_comp[li] + ac;
        st.dev_n_ix[li] = txn.dev_n_ix[li] + ai;
        note_compressed(st.kv_id, li, st.dev_n_comp[li]);
    }
    // ── host tail: the verify pass already walked these tokens; restore
    //    the per-token snapshot it took instead of walking them again. ──
    if k >= 1 && txn.host_steps.iter().all(|(_, v)| v.len() >= k) && !txn.host_steps.is_empty()
    {
        for (li, v) in &txn.host_steps {
            host_restore(st, *li, &v[k - 1]);
        }
    } else {
        for (li, snap) in &txn.host {
            host_restore(st, *li, snap);
        }
        let mut scratch = HcScratch::new(cfg);
        let mut states = txn.states.clone();
        host_tail_walk_batch(
            g, layers, cfg, st, txn.gpu_end, &mut states[..k * hc * dim], ids, txn.pos0, k,
            inv_freq, &mut scratch, pool, None,
        );
    }
    st.pos = txn.pos0 + k;
    true
}

pub fn forward_chunk(
    g: &Dsv4Globals,
    layers: &[Dsv4Layer],
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    ids: &[u32],
    pos0: usize,
    inv_freq: &[f32],
    pool: Option<&crate::pool::Pool>,
    logits: &mut Vec<f32>,
    want_logits: bool,
) {
    let bs = batch_prefill();
    if bs > 1 {
        // The first token walks, always. The batch will only run where every
        // layer has already proved it takes the card, and that proof is a
        // completed single-token run — with the whole prompt arriving as one
        // chunk there is otherwise no first run to give it, and the batch
        // declines for the entire prompt while a gate comparing it against
        // the walk reports agreement it never tested.
        let mut i = 0;
        if !st.dev_owned && !ids.is_empty() {
            st.pos = pos0;
            forward_token_inner(g, layers, cfg, st, ids[0], inv_freq, pool, logits, ids.len() == 1);
            i = 1;
        }
        while i < ids.len() {
            let end = (i + bs).min(ids.len());
            st.pos = pos0 + i;
            if !forward_chunk_batched(
                g,
                layers,
                cfg,
                st,
                &ids[i..end],
                pos0 + i,
                inv_freq,
                pool,
                logits,
                want_logits && end == ids.len(),
            ) {
                break;
            }
            i = end;
        }
        if i == ids.len() {
            return;
        }
        // Refused before touching anything; the walk starts where it left off.
        for (k, &id) in ids.iter().enumerate().skip(i) {
            st.pos = pos0 + k;
            let last = want_logits && k + 1 == ids.len();
            forward_token_inner(g, layers, cfg, st, id, inv_freq, pool, logits, last);
        }
        return;
    }
    for (i, &id) in ids.iter().enumerate() {
        st.pos = pos0 + i;
        let last = want_logits && i + 1 == ids.len();
        forward_token_inner(g, layers, cfg, st, id, inv_freq, pool, logits, last);
    }
}

pub fn forward_token(
    g: &Dsv4Globals,
    layers: &[Dsv4Layer],
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    token_id: u32,
    inv_freq: &[f32],
    pool: Option<&crate::pool::Pool>,
    logits: &mut Vec<f32>,
) {
    forward_token_inner(g, layers, cfg, st, token_id, inv_freq, pool, logits, true);
}

#[allow(clippy::too_many_arguments)]
fn forward_token_inner(
    g: &Dsv4Globals,
    layers: &[Dsv4Layer],
    cfg: &Dsv4Cfg,
    st: &mut Dsv4State,
    token_id: u32,
    inv_freq: &[f32],
    pool: Option<&crate::pool::Pool>,
    logits: &mut Vec<f32>,
    // Prompt tokens other than the last one have their logits thrown away.
    want_logits: bool,
) {
    let _t_all = prof::on().then(std::time::Instant::now);
    let _all_guard = Charge(_t_all, &prof::ALL_NS);
    let (hc, dim) = (cfg.hc_mult, cfg.dim);

    // Embedding, replicated into the copies.
    let mut emb = vec![0.0f32; dim];
    g.embed.row_f32(token_id as usize, &mut emb);
    let mut state = vec![0.0f32; hc * dim];
    for j in 0..hc {
        state[j * dim..(j + 1) * dim].copy_from_slice(&emb);
    }

    let mut scratch = HcScratch::new(cfg);
    let mut dump: Vec<String> = Vec::new();
    if dump_path().is_some() {
        dump.push(format!("\"embed\":{}", vec_json(&emb)));
        PICKED.with(|p| p.borrow_mut().clear());
        BODY.with(|b| b.borrow_mut().clear());
        dump.push(",\"layers\":[".into());
    }
    if trace_on() {
        eprintln!(
            "[dsv4] tok={token_id} pos={} embed rms={:.5}",
            st.pos,
            rms_of(&emb)
        );
    }
    // ── one submission per layer, when the device will take it ──
    #[cfg(feature = "gpu")]
    let layer_frames = gpu_layer_enabled()
        && dsv4_layer_loop(
            &mut state, layers, g, cfg, st, token_id, inv_freq, pool, &mut scratch,
        );
    #[cfg(not(feature = "gpu"))]
    let layer_frames = false;

    // ── the fast two-frame path: hyper-connections on the card ──
    // Measured on the release, the fold, the Sinkhorn and the norms cost 19
    // ms of a 57 ms token on the host and hundredths of one on the device.
    // With both frames doing their own, the host carries nothing between a
    // layer's halves and the MoE half's input never leaves the card — one
    // readback a layer instead of two.
    #[cfg(feature = "gpu")]
    let hc_dev = hc_on_device()
        && !layer_frames
        && gpu_attn_enabled()
        && gpu_moe2_enabled()
        && dump_path().is_none();
    #[cfg(not(feature = "gpu"))]
    let hc_dev = false;
    // The device loop's verdict as a VALUE, not as a cfg-gated `if`. It used
    // to be the latter, with the CPU loop in the `else` arm — so a build
    // without the gpu feature compiled no layer loop at all and every token
    // passed through untouched. The window test said so ("sliding window
    // never filled") and only in the CPU-only build, which is the one
    // configuration the gate was not running.
    #[cfg(feature = "gpu")]
    let two_frame_done = hc_dev
        && dsv4_two_frame_loop(
            &mut state, layers, g, cfg, st, token_id, inv_freq, pool, &mut scratch,
        );
    #[cfg(not(feature = "gpu"))]
    let two_frame_done = false;
    if !two_frame_done {

    for (li, l) in layers.iter().enumerate() {
        if layer_frames {
            break;
        }
        // attention half
        hc_block(
            &mut state,
            &l.hc_attn_fn,
            &l.hc_attn_scale,
            &l.hc_attn_base,
            &l.attn_norm,
            cfg,
            &mut scratch,
            pool,
            |folded, out| {
                if dump_path().is_some() {
                    // The body's own input and output, so the reference can be
                    // fed the port's input: then only the body can differ.
                    BODY.with(|b| b.borrow_mut().push(vec_json(folded)));
                }
                // The layer's kind decides its frequencies, not the model's.
                let freqs = if l.compressor.is_some() {
                    &g.inv_freq_compress
                } else {
                    &g.inv_freq_window
                };
                let freqs = if freqs.is_empty() {
                    inv_freq
                } else {
                    freqs.as_slice()
                };
                attention_step(folded, l, cfg, st, li, freqs, pool, None, out);
                if dump_path().is_some() {
                    BODY.with(|b| b.borrow_mut().push(vec_json(out)));
                }
            },
        );
        if dump_path().is_some() {
            // After the attention half only — this is what separates an
            // attention discrepancy from an expert one.
            dump.push(format!(
                "{}{}",
                if li == 0 { "" } else { "," },
                vec_json(&state)
            ));
        }
        // FFN half
        let _t_hc2 = prof::on().then(std::time::Instant::now);
        hc_block(
            &mut state,
            &l.hc_ffn_fn,
            &l.hc_ffn_scale,
            &l.hc_ffn_base,
            &l.ffn_norm,
            cfg,
            &mut scratch,
            pool,
            |folded, out| moe_step(folded, l, cfg, token_id, li, pool, out),
        );
        if let Some(t) = _t_hc2 {
            // The block's own time minus the expert step inside it — what the
            // fold, the norm and the expand cost on their own.
            prof::HC_NS.fetch_add(
                t.elapsed().as_nanos() as u64,
                std::sync::atomic::Ordering::Relaxed,
            );
        }
        if dump_path().is_some() {
            dump.push(format!(",{}", vec_json(&state)));
        }
        if trace_on() && (st.pos % 64 == 0 || st.pos == 199) {
            eprintln!(
                "[dsv4]  кеши слоя {li}: окно={} сжатых={} индекс={} (ratio={:?})",
                st.window[li].len() / cfg.head_dim.max(1),
                st.compressed[li].len() / cfg.head_dim.max(1),
                st.index_kv[li].len().max(1) / 128,
                l.compressor.as_ref().map(|c| (c.ratio, c.overlap)),
            );
        }
        if trace_on() {
            let bad = state.iter().filter(|v| !v.is_finite()).count();
            eprintln!(
                "[dsv4]  layer {li:>2}: rms={:.5}{}",
                rms_of(&state),
                if bad > 0 {
                    format!("  NON-FINITE x{bad}")
                } else {
                    String::new()
                }
            );
        }
        dspark_note(li, &state, cfg);
    }
    }
    st.pos += 1;

    // Collapse the copies, normalize, project to the vocabulary.
    let mut h = vec![0.0f32; dim];
    hc_head_fold(
        &state,
        &g.hc_head_fn,
        g.hc_head_scale,
        &g.hc_head_base,
        cfg,
        pool,
        &mut h,
    );
    if !want_logits {
        logits.clear();
        return;
    }
    let _t_head = prof::on().then(std::time::Instant::now);
    rms_weighted(&mut h, &g.norm, cfg.norm_eps);
    logits.clear();
    logits.resize(g.head.rows(), 0.0);
    g.head.matvec(&h, logits, pool);
    if let Some(t) = _t_head {
        prof::HEAD_NS.fetch_add(
            t.elapsed().as_nanos() as u64,
            std::sync::atomic::Ordering::Relaxed,
        );
    }
    if dump_path().is_some() {
        dump.push("]".into());
        let picked = PICKED.with(|p| {
            p.borrow()
                .iter()
                .map(|v| {
                    format!(
                        "[{}]",
                        v.iter()
                            .map(|e| e.to_string())
                            .collect::<Vec<_>>()
                            .join(",")
                    )
                })
                .collect::<Vec<_>>()
                .join(",")
        });
        dump.push(format!(",\"experts\":[{picked}]"));
        let body = BODY.with(|b| b.borrow().join(","));
        dump.push(format!(",\"attn_io\":[{body}]"));
        dump_line(&format!(
            "{{\"tok\":{token_id},\"pos\":{},{},\"head\":{},\"logits\":{}}}",
            st.pos - 1,
            dump.join(""),
            vec_json(&h),
            vec_json(logits)
        ));
    }
    if trace_on() {
        let (mut top, mut best) = (0usize, f32::NEG_INFINITY);
        for (i, &v) in logits.iter().enumerate() {
            if v > best {
                best = v;
                top = i;
            }
        }
        let lo = logits.iter().cloned().fold(f32::MAX, f32::min);
        eprintln!(
            "[dsv4]  head: rms={:.5} logits[{}..{:.3}] argmax={top}",
            rms_of(&h),
            format_args!("{lo:.3}"),
            best
        );
    }
}

/// Build the runtime weights from a converted `.cmf`.
///
/// Names are the converter's output (see `canon_name`'s deepseek_v4 arm):
/// attention keeps DeepSeek's own spelling under `self_attn.`, the MoE is
/// rewritten into the layout every other MoE here uses, and the hyper-
/// connection tensors ride under the layer prefix.
pub fn load(
    model: &std::sync::Arc<cortiq_core::CmfModel>,
    cfg: &Dsv4Cfg,
    n_layers: usize,
) -> Result<(Dsv4Globals, Vec<Dsv4Layer>), String> {
    let q = |name: &str| -> Result<crate::qtensor::QTensor, String> {
        crate::qtensor::QTensor::from_model(model, name)
    };
    // The small pieces — norms, the sink, ape, the hyper-connection
    // projections — are read as plain f32. They are not all 2-D (a norm is a
    // vector), so this cannot go through QTensor, which requires a matrix.
    let f = |name: &str| -> Result<Vec<f32>, String> {
        crate::loader::load_f32(model, name, &crate::loader::Overlay::None)
    };

    // Two frequency tables, chosen per layer by whether it compresses. The
    // release's compress_rope_theta (160 000) is not in config.json — it
    // lives in inference/config.json — so it is pinned here with the other
    // constants the header cannot carry.
    let rope_of = |base: f32, yarn: bool| -> Vec<f32> {
        if yarn {
            crate::attention::yarn_inv_freq(cfg.rope_head_dim, base, 16.0, 65536, 32.0, 1.0)
        } else {
            crate::attention::rope_inv_freq(cfg.rope_head_dim, base)
        }
    };
    let globals = Dsv4Globals {
        inv_freq_compress: rope_of(160_000.0, true),
        inv_freq_window: rope_of(10_000.0, false),
        embed: q("model.embed_tokens.weight")?,
        norm: f("model.norm.weight")?,
        head: q("lm_head.weight")?,
        hc_head_fn: f("model.hc_head_fn")?,
        hc_head_base: f("model.hc_head_base")?,
        hc_head_scale: *f("model.hc_head_scale")?
            .first()
            .ok_or("dsv4: empty hc_head_scale")?,
    };

    let mut layers = Vec::with_capacity(n_layers);
    for li in 0..n_layers {
        layers.push(load_layer(
            model,
            cfg,
            &format!("model.layers.{li}"),
            Scheme::Main,
        )?);
    }
    Ok((globals, layers))
}

/// Where a layer's tensors live in the file.
///
/// The MTP modules are the same layer as any other — attention, a
/// hyper-connection pair, a gated MoE over 256 experts — but the converter
/// wrote them under DeepSeek's internal names rather than the HF ones it used
/// for the trunk. Two schemes, one loader: a second copy would drift.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Scheme {
    Main,
    Mtp,
}

impl Scheme {
    fn attn(self) -> &'static str {
        match self {
            Scheme::Main => "self_attn",
            Scheme::Mtp => "attn",
        }
    }
    fn attn_norm(self) -> &'static str {
        match self {
            Scheme::Main => "input_layernorm.weight",
            Scheme::Mtp => "attn_norm.weight",
        }
    }
    fn ffn_norm(self) -> &'static str {
        match self {
            Scheme::Main => "post_attention_layernorm.weight",
            Scheme::Mtp => "ffn_norm.weight",
        }
    }
    fn mlp(self) -> &'static str {
        match self {
            Scheme::Main => "mlp",
            Scheme::Mtp => "ffn",
        }
    }
    /// The router's per-expert bias. Absent on the trunk's hash layers, which
    /// is how they are recognised; always present on an MTP module.
    fn gate_bias(self) -> &'static str {
        match self {
            Scheme::Main => "expert_bias",
            Scheme::Mtp => "gate.bias",
        }
    }
    fn shared(self) -> &'static str {
        match self {
            Scheme::Main => "shared_expert",
            Scheme::Mtp => "shared_experts",
        }
    }
    /// gate, down, up — in that order, which is w1/w2/w3 upstream.
    fn w(self, i: u8) -> &'static str {
        match (self, i) {
            (Scheme::Main, 1) => "gate_proj.weight",
            (Scheme::Main, 2) => "down_proj.weight",
            (Scheme::Main, _) => "up_proj.weight",
            (Scheme::Mtp, 1) => "w1.weight",
            (Scheme::Mtp, 2) => "w2.weight",
            (Scheme::Mtp, _) => "w3.weight",
        }
    }
}

/// One layer, wherever it lives in the file.
pub fn load_layer(
    model: &std::sync::Arc<cortiq_core::CmfModel>,
    cfg: &Dsv4Cfg,
    p: &str,
    s: Scheme,
) -> Result<Dsv4Layer, String> {
    let q = |name: &str| -> Result<crate::qtensor::QTensor, String> {
        crate::qtensor::QTensor::from_model(model, name)
    };
    let f = |name: &str| -> Result<Vec<f32>, String> {
        crate::loader::load_f32(model, name, &crate::loader::Overlay::None)
    };
    let opt_f = |name: &str| -> Option<Vec<f32>> { f(name).ok() };
    let at = s.attn();
    let ml = s.mlp();
    {
        let scale3 = |name: &str| -> Result<[f32; 3], String> {
            let v = f(name)?;
            if v.len() < 3 {
                return Err(format!("{name}: expected 3 scales, got {}", v.len()));
            }
            Ok([v[0], v[1], v[2]])
        };
        // The compressor exists on every layer whose ratio is non-zero;
        // its presence in the file is the only signal we need.
        let compressor = match q(&format!("{p}.{at}.compressor.wkv.weight")) {
            Ok(wkv) => {
                let ape = f(&format!("{p}.{at}.compressor.ape"))?;
                // ape is [ratio, coff*head_dim]; coff is 2 when the windows
                // overlap, which the release does at ratio 4.
                let width = wkv.rows();
                let ratio = (ape.len() / width.max(1)).max(1);
                Some(Dsv4Compressor {
                    wkv,
                    wgate: q(&format!("{p}.{at}.compressor.wgate.weight"))?,
                    norm: f(&format!("{p}.{at}.compressor.norm.weight"))?,
                    ape,
                    ratio,
                    overlap: ratio == 4,
                })
            }
            Err(_) => None,
        };
        let indexer = match q(&format!("{p}.{at}.indexer.wq_b.weight")) {
            Ok(wq_b) => {
                let ape = f(&format!("{p}.{at}.indexer.compressor.ape"))?;
                let cwkv = q(&format!("{p}.{at}.indexer.compressor.wkv.weight"))?;
                let width = cwkv.rows();
                let ratio = (ape.len() / width.max(1)).max(1);
                Some(Dsv4Indexer {
                    wq_b,
                    weights_proj: q(&format!("{p}.{at}.indexer.weights_proj.weight"))?,
                    compressor: Dsv4Compressor {
                        wkv: cwkv,
                        wgate: q(&format!("{p}.{at}.indexer.compressor.wgate.weight"))?,
                        norm: f(&format!("{p}.{at}.indexer.compressor.norm.weight"))?,
                        ape,
                        ratio,
                        overlap: ratio == 4,
                    },
                })
            }
            Err(_) => None,
        };

        let mut experts = Vec::with_capacity(cfg.n_routed_experts);
        for e in 0..cfg.n_routed_experts {
            let ep = format!("{p}.{ml}.experts.{e}");
            experts.push(Dsv4Expert {
                w1: q(&format!("{ep}.{w}", w = s.w(1)))?,
                w2: q(&format!("{ep}.{w}", w = s.w(2)))?,
                w3: q(&format!("{ep}.{w}", w = s.w(3)))?,
            });
        }

        Ok(Dsv4Layer {
            attn_norm: f(&format!("{p}.{an}", an = s.attn_norm()))?,
            ffn_norm: f(&format!("{p}.{fnm}", fnm = s.ffn_norm()))?,
            wq_a: q(&format!("{p}.{at}.wq_a.weight"))?,
            q_norm: f(&format!("{p}.{at}.q_norm.weight"))?,
            wq_b: q(&format!("{p}.{at}.wq_b.weight"))?,
            wkv: q(&format!("{p}.{at}.wkv.weight"))?,
            kv_norm: f(&format!("{p}.{at}.kv_norm.weight"))?,
            wo_a: q(&format!("{p}.{at}.wo_a.weight"))?,
            wo_b: q(&format!("{p}.{at}.wo_b.weight"))?,
            attn_sink: f(&format!("{p}.{at}.attn_sink"))?,
            compressor,
            indexer,
            hc_attn_fn: f(&format!("{p}.hc_attn_fn"))?,
            hc_attn_base: f(&format!("{p}.hc_attn_base"))?,
            hc_attn_scale: scale3(&format!("{p}.hc_attn_scale"))?,
            hc_ffn_fn: f(&format!("{p}.hc_ffn_fn"))?,
            hc_ffn_base: f(&format!("{p}.hc_ffn_base"))?,
            hc_ffn_scale: scale3(&format!("{p}.hc_ffn_scale"))?,
            gate: q(&format!("{p}.{ml}.gate.weight"))?,
            // The bias is absent exactly on the hash layers, and the table
            // is present exactly there — the file itself says which is which.
            gate_bias: opt_f(&format!("{p}.{ml}.{b}", b = s.gate_bias())),
            tid2eid: opt_f(&format!("{p}.{ml}.tid2eid")),
            experts,
            mask: if model.tensor(&format!("{p}.{ml}.tid2eid")).is_some() {
                None
            } else {
                crate::loader::moe_task_mask(&format!("{p}."), cfg.n_routed_experts)
            },
            shared: Dsv4Expert {
                w1: q(&format!("{p}.{ml}.{sh}.{w}", sh = s.shared(), w = s.w(1)))?,
                w2: q(&format!("{p}.{ml}.{sh}.{w}", sh = s.shared(), w = s.w(2)))?,
                w3: q(&format!("{p}.{ml}.{sh}.{w}", sh = s.shared(), w = s.w(3)))?,
            },
        })
    }
}

/// One module of the speculation stack.
///
/// The release carries three, so the draft is three deep, and the last one
/// also holds a confidence head — the model scores its own proposals rather
/// than leaving acceptance to a threshold we would have to invent. Each
/// module is a full layer with its own 256 experts; what makes it an MTP
/// module rather than a 44th layer is `main_proj`, which folds the previous
/// hidden state into the next embedding before the layer runs.
pub struct Dsv4Mtp {
    pub layer: Dsv4Layer,
    /// Stage 0 only: the projection that turns the trunk's captured hidden
    /// states into the block's input. Later stages take the block from the
    /// stage before them, so they carry none.
    pub main_proj: Option<crate::qtensor::QTensor>,
    pub main_norm: Option<Vec<f32>>,
    /// Last module only: what turns a draft hidden state into logits.
    pub norm: Option<Vec<f32>>,
    pub hc_head_fn: Option<Vec<f32>>,
    pub hc_head_base: Option<Vec<f32>>,
    pub hc_head_scale: Option<f32>,
    pub confidence: Option<crate::qtensor::QTensor>,
    /// Last stage only: a rank-256 bigram table that biases the draft's
    /// logits, and whose embedding also feeds the confidence head. Cheap
    /// enough that the draft samples through it position by position while
    /// the network itself runs the whole block at once.
    pub markov_w1: Option<crate::qtensor::QTensor>,
    pub markov_w2: Option<crate::qtensor::QTensor>,
}

/// Load as much of the speculation stack as the file carries, up to
/// `max_depth`. Missing is not an error: a checkpoint without MTP simply
/// yields an empty stack, and the caller falls back to plain decoding.
pub fn load_mtp(
    model: &std::sync::Arc<cortiq_core::CmfModel>,
    cfg: &Dsv4Cfg,
    max_depth: usize,
) -> Vec<Dsv4Mtp> {
    let f = |name: &str| -> Option<Vec<f32>> {
        crate::loader::load_f32(model, name, &crate::loader::Overlay::None).ok()
    };
    let mut out = Vec::new();
    for d in 0..max_depth {
        let p = format!("model.mtp.{d}");
        // A stage is recognised by its attention, not by `main_proj`: only
        // stage 0 has that, and only the last has the head. Keying on either
        // end found one module of three.
        if model.tensor(&format!("{p}.attn.wq_a.weight")).is_none() {
            break;
        }
        let layer = match load_layer(model, cfg, &p, Scheme::Mtp) {
            Ok(l) => l,
            Err(e) => {
                eprintln!("MTP {d}: пропущен, {e}");
                break;
            }
        };
        out.push(Dsv4Mtp {
            layer,
            main_proj: crate::qtensor::QTensor::from_model(model, &format!("{p}.main_proj.weight"))
                .ok(),
            main_norm: f(&format!("{p}.main_norm.weight")),
            norm: f(&format!("{p}.norm.weight")),
            hc_head_fn: f(&format!("{p}.hc_head_fn")),
            hc_head_base: f(&format!("{p}.hc_head_base")),
            hc_head_scale: f(&format!("{p}.hc_head_scale")).and_then(|v| v.first().copied()),
            confidence: crate::qtensor::QTensor::from_model(
                model,
                &format!("{p}.confidence_head.proj.weight"),
            )
            .ok(),
            markov_w1: crate::qtensor::QTensor::from_model(
                model,
                &format!("{p}.markov_head.markov_w1.weight"),
            )
            .ok(),
            markov_w2: crate::qtensor::QTensor::from_model(
                model,
                &format!("{p}.markov_head.markov_w2.weight"),
            )
            .ok(),
        });
    }
    dspark_apply_mask(&mut out);
    if !out.is_empty() {
        let mp = out
            .iter()
            .find_map(|m| m.main_proj.as_ref())
            .map(|t| format!("[{}, {}]", t.rows(), t.cols()))
            .unwrap_or_else(|| "нет".into());
        eprintln!(
            "MTP: {} стади(я/и/й), main_proj {mp}, экспертов {}, \
             голова уверенности {}, марков {}",
            out.len(),
            out[0].layer.experts.len(),
            if out.iter().any(|m| m.confidence.is_some()) { "есть" } else { "нет" },
            if out.iter().any(|m| m.markov_w1.is_some()) { "есть" } else { "нет" },
        );
    }
    out
}

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

    // A whole model, small enough to reason about: 2 layers, 4 heads, 8
    // experts. Weights are deterministic and tiny, which is the point —
    // this test is about shapes, indexing and cache bookkeeping, the things
    // that a 138 GB file would surface only after an hour of loading.
    fn toy() -> (Dsv4Globals, Vec<Dsv4Layer>, Dsv4Cfg) {
        use crate::qtensor::QTensor;
        let cfg = Dsv4Cfg {
            dim: 32,
            n_heads: 4,
            head_dim: 8,
            rope_head_dim: 4,
            q_lora_rank: 16,
            o_lora_rank: 16,
            o_groups: 2,
            hc_mult: 4,
            hc_sinkhorn_iters: 20,
            hc_eps: 1e-6,
            norm_eps: 1e-6,
            n_routed_experts: 8,
            top_k: 2,
            moe_inter: 16,
            route_scale: 1.0,
            swiglu_limit: 10.0,
            window: 6,
            index_topk: 8,
            vocab: 24,
        };
        // Deterministic pseudo-random in a narrow band: big enough to move
        // the state, small enough that nothing saturates.
        let w = |n: usize, seed: usize| -> Vec<f32> {
            (0..n)
                .map(|i| (((i * 7 + seed * 13) % 101) as f32 / 101.0 - 0.5) * 0.3)
                .collect()
        };
        let t = |rows: usize, cols: usize, seed: usize| {
            QTensor::from_f32(w(rows * cols, seed), rows, cols)
        };
        let ones = |n: usize| vec![1.0f32; n];

        let (dim, hc) = (cfg.dim, cfg.hc_mult);
        // q is n_heads*head_dim wide and kv is one head wide; rope rides the
        // tail of each rather than widening anything.
        let q_width = cfg.n_heads * cfg.head_dim;
        let kv_width = cfg.head_dim;
        let o_per_group = q_width / cfg.o_groups;
        let mut layers = Vec::new();
        for li in 0..2 {
            let experts: Vec<Dsv4Expert> = (0..cfg.n_routed_experts)
                .map(|e| Dsv4Expert {
                    w1: t(cfg.moe_inter, dim, 40 + e + li * 8),
                    w2: t(dim, cfg.moe_inter, 60 + e + li * 8),
                    w3: t(cfg.moe_inter, dim, 80 + e + li * 8),
                })
                .collect();
            // Layer 0 is a hash layer (table-routed); layer 1 routes normally
            // and carries the compressor — both paths get exercised.
            layers.push(Dsv4Layer {
                attn_norm: ones(dim),
                ffn_norm: ones(dim),
                wq_a: t(cfg.q_lora_rank, dim, 1 + li),
                q_norm: ones(cfg.q_lora_rank),
                wq_b: t(q_width, cfg.q_lora_rank, 3 + li),
                wkv: t(kv_width, dim, 5 + li),
                kv_norm: ones(kv_width),
                wo_a: t(cfg.o_groups * cfg.o_lora_rank, o_per_group, 7 + li),
                wo_b: t(dim, cfg.o_groups * cfg.o_lora_rank, 9 + li),
                attn_sink: vec![0.1; cfg.n_heads],
                // Layer 1 carries the OVERLAPPING compressor, as the release
                // does at ratio 4: the projection is twice the entry width.
                compressor: if li == 1 {
                    Some(Dsv4Compressor {
                        wkv: t(2 * kv_width, dim, 11),
                        wgate: t(2 * kv_width, dim, 13),
                        norm: ones(kv_width),
                        ape: vec![0.01; 4 * 2 * kv_width],
                        ratio: 4,
                        overlap: true,
                    })
                } else {
                    None
                },
                indexer: if li == 1 {
                    Some(Dsv4Indexer {
                        wq_b: t(2 * 16, cfg.q_lora_rank, 41),
                        weights_proj: t(2, dim, 43),
                        compressor: Dsv4Compressor {
                            wkv: t(2 * 16, dim, 45),
                            wgate: t(2 * 16, dim, 47),
                            norm: ones(16),
                            ape: vec![0.01; 4 * 2 * 16],
                            ratio: 4,
                            overlap: true,
                        },
                    })
                } else {
                    None
                },
                hc_attn_fn: w((2 + hc) * hc * hc * dim, 15 + li),
                hc_attn_base: w((2 + hc) * hc, 17 + li),
                hc_attn_scale: [1.0, 1.0, 1.0],
                hc_ffn_fn: w((2 + hc) * hc * hc * dim, 19 + li),
                hc_ffn_base: w((2 + hc) * hc, 21 + li),
                hc_ffn_scale: [1.0, 1.0, 1.0],
                gate: t(cfg.n_routed_experts, dim, 23 + li),
                gate_bias: if li == 1 {
                    Some(vec![0.0; cfg.n_routed_experts])
                } else {
                    None
                },
                tid2eid: if li == 0 {
                    Some(
                        (0..cfg.vocab * cfg.top_k)
                            .map(|i| (i % cfg.n_routed_experts) as f32)
                            .collect(),
                    )
                } else {
                    None
                },
                experts,
                mask: None,
                shared: Dsv4Expert {
                    w1: t(cfg.moe_inter, dim, 25 + li),
                    w2: t(dim, cfg.moe_inter, 27 + li),
                    w3: t(cfg.moe_inter, dim, 29 + li),
                },
            });
        }
        let inv = |base: f32| -> Vec<f32> {
            (0..cfg.rope_head_dim / 2)
                .map(|i| 1.0 / base.powf(2.0 * i as f32 / cfg.rope_head_dim as f32))
                .collect()
        };
        let g = Dsv4Globals {
            inv_freq_compress: inv(160000.0),
            inv_freq_window: inv(10000.0),
            embed: t(cfg.vocab, dim, 31),
            norm: ones(dim),
            head: t(cfg.vocab, dim, 33),
            hc_head_fn: w(hc * hc * dim, 35),
            hc_head_base: w(hc, 37),
            hc_head_scale: 1.0,
        };
        (g, layers, cfg)
    }

    /// The whole stack, decoding a sequence. Every block is on the path:
    /// hyper-connections, the double-LoRA attention with its sink, the KV
    /// compressor firing on its ratio boundary, hash routing on one layer
    /// and score routing on the other.
    #[test]
    fn forward_token_decodes_a_sequence_without_falling_over() {
        let (g, layers, cfg) = toy();
        let mut st = Dsv4State::new(layers.len());
        let inv_freq: Vec<f32> = (0..cfg.rope_head_dim / 2)
            .map(|i| 1.0 / 10000f32.powf(2.0 * i as f32 / cfg.rope_head_dim as f32))
            .collect();
        let mut logits = Vec::new();

        // Ten tokens: more than twice the compressor's ratio, so the
        // compressed cache is written on a boundary and read afterwards.
        let mut first: Option<Vec<f32>> = None;
        for (step, tok) in [3u32, 7, 1, 9, 4, 2, 8, 5, 6, 0].into_iter().enumerate() {
            forward_token(
                &g,
                &layers,
                &cfg,
                &mut st,
                tok,
                &inv_freq,
                None,
                &mut logits,
            );
            assert_eq!(logits.len(), cfg.vocab, "step {step}: logit count");
            assert!(
                logits.iter().all(|v| v.is_finite()),
                "step {step}: non-finite logit — {logits:?}"
            );
            // A model that has collapsed returns the same distribution
            // regardless of input; that is the failure this catches.
            let spread = logits.iter().cloned().fold(f32::MIN, f32::max)
                - logits.iter().cloned().fold(f32::MAX, f32::min);
            assert!(spread > 1e-6, "step {step}: logits are flat ({spread})");
            if step == 0 {
                first = Some(logits.clone());
            }
            assert_eq!(st.pos, step + 1, "position bookkeeping");
        }

        // The cache has to have grown, and the compressor layer must have
        // emitted compressed entries (10 tokens / ratio 4 = 2 windows).
        assert!(!st.window[0].is_empty(), "sliding window never filled");
        // Ten tokens through a window of six: it must have slid, not grown.
        for (li, w) in st.window.iter().enumerate() {
            assert!(
                w.len() / cfg.head_dim <= cfg.window,
                "layer {li}: window holds {} positions, cap is {}",
                w.len() / cfg.head_dim,
                cfg.window
            );
        }
        assert!(
            !st.compressed[1].is_empty(),
            "compressor layer produced no compressed KV in 10 tokens"
        );
        // Ten tokens at ratio 4 fold twice, and the entries must be one head
        // wide — the overlapping projection is 2x that, so a width mistake
        // shows up here rather than as quiet nonsense.
        assert_eq!(
            st.compressed[1].len() / cfg.head_dim,
            2,
            "expected two folds in ten tokens at ratio 4"
        );
        assert!(
            !st.prev_kv[1].is_empty(),
            "the overlapping compressor never kept a previous window"
        );
        // Every layer that HAS an indexer must have filled the indexer's own
        // cache: it is what decides which compressed positions attention
        // reads, and an empty one silently discards the whole long-range
        // memory rather than failing.
        for (li, l) in layers.iter().enumerate() {
            if l.indexer.is_some() {
                assert!(
                    !st.index_kv[li].is_empty(),
                    "layer {li} has an indexer but its cache stayed empty"
                );
            }
        }

        // Context must matter: the same token at position 0 of a fresh state
        // and at the end of a filled one cannot give identical logits.
        let mut fresh = Dsv4State::new(layers.len());
        let mut relogits = Vec::new();
        forward_token(
            &g,
            &layers,
            &cfg,
            &mut fresh,
            3,
            &inv_freq,
            None,
            &mut relogits,
        );
        assert_eq!(
            relogits,
            first.unwrap(),
            "the same token from a fresh state must reproduce exactly"
        );
    }

    /// The reference clamps `up` on both sides but `gate` only from above.
    /// Getting that symmetric would quietly change every expert's output on
    /// the tokens that saturate, which is the hardest kind of bug to see.
    #[test]
    fn swiglu_limit_clamps_up_both_ways_and_gate_only_from_above() {
        let inter = 4;
        // gate = [-50, 50, 1, -1], up = [50, -50, 1, -1]
        let gate_src = [-50.0f32, 50.0, 1.0, -1.0];
        let up_src = [50.0f32, -50.0, 1.0, -1.0];
        let limit = 10.0f32;
        let mut got = vec![0.0f32; inter];
        expert_swiglu(
            &[0.0],
            &|_, d| d.copy_from_slice(&gate_src),
            &|_, d| d.copy_from_slice(&up_src),
            &|src, d| d.copy_from_slice(src),
            inter,
            1.0,
            limit,
            &mut got,
        );
        let silu = |g: f32| g / (1.0 + (-g).exp());
        // gate: only the +50 is cut, the -50 rides through silu untouched.
        let want = [
            silu(-50.0) * limit,
            silu(limit) * -limit,
            silu(1.0) * 1.0,
            silu(-1.0) * -1.0,
        ];
        for (i, w) in want.iter().enumerate() {
            assert!(
                (got[i] - w).abs() < 1e-5,
                "lane {i}: got {} want {w}",
                got[i]
            );
        }
        // And with the clamp off nothing is touched.
        let mut raw = vec![0.0f32; inter];
        expert_swiglu(
            &[0.0],
            &|_, d| d.copy_from_slice(&gate_src),
            &|_, d| d.copy_from_slice(&up_src),
            &|src, d| d.copy_from_slice(src),
            inter,
            1.0,
            0.0,
            &mut raw,
        );
        assert!(
            (raw[1] - silu(50.0) * -50.0).abs() < 1e-3,
            "limit 0 must not clamp"
        );
    }

    /// The grouped projection writes its intermediate from several threads
    /// at once. Disjoint indices are the whole argument for that being safe,
    /// so the pooled result has to equal the serial one exactly — a race
    /// here would show up as occasional wrong tokens, not as a crash.
    #[test]
    fn grouped_projection_is_identical_with_and_without_a_pool() {
        let (groups, lora, per_group, dim) = (4usize, 128usize, 64usize, 32usize);
        let attn: Vec<f32> = (0..groups * per_group)
            .map(|i| ((i * 13) as f32 * 0.021).sin())
            .collect();
        let wo_a: Vec<f32> = (0..groups * lora * per_group)
            .map(|i| ((i * 7) as f32 * 0.011).cos())
            .collect();
        let wo_b: Vec<f32> = (0..dim * groups * lora)
            .map(|i| ((i * 5) as f32 * 0.009).sin())
            .collect();
        let row = |r: usize, x: &[f32], _sc: &mut [f32]| -> f32 {
            wo_a[r * per_group..(r + 1) * per_group]
                .iter()
                .zip(x)
                .map(|(a, b)| a * b)
                .sum()
        };
        let project = |mid: &[f32], dst: &mut [f32]| {
            for (d, o) in dst.iter_mut().enumerate() {
                *o = wo_b[d * mid.len()..(d + 1) * mid.len()]
                    .iter()
                    .zip(mid)
                    .map(|(a, b)| a * b)
                    .sum();
            }
        };

        let mut serial = vec![0.0f32; dim];
        o_project(
            &attn,
            &row,
            per_group,
            &project,
            groups,
            lora,
            None,
            &mut serial,
        );

        let pool = crate::pool::Pool::new(4);
        let mut pooled = vec![0.0f32; dim];
        o_project(
            &attn,
            &row,
            per_group,
            &project,
            groups,
            lora,
            Some(&pool),
            &mut pooled,
        );
        assert_eq!(serial, pooled, "the pooled projection diverged");
        assert!(
            serial.iter().any(|v| v.abs() > 1e-6),
            "test data is degenerate"
        );
    }

    #[test]
    fn block_grouped_projection_matches_position_walk() {
        let (_g, layers, cfg) = toy();
        let l = &layers[1];
        let b = 5;
        let attn_len = cfg.n_heads * cfg.head_dim;
        let attn: Vec<f32> = (0..b * attn_len)
            .map(|i| ((i * 17) as f32 * 0.013).sin())
            .collect();
        let mut walked = vec![0.0f32; b * cfg.dim];
        for bi in 0..b {
            o_project(
                &attn[bi * attn_len..(bi + 1) * attn_len],
                &|r, x, sc| l.wo_a.row_dot(r, x, sc),
                l.wo_a.cols(),
                &|mid, dst| l.wo_b.matvec(mid, dst, None),
                cfg.o_groups,
                cfg.o_lora_rank,
                None,
                &mut walked[bi * cfg.dim..(bi + 1) * cfg.dim],
            );
        }
        let mut batched = vec![0.0f32; b * cfg.dim];
        o_project_block(
            &attn,
            b,
            &l.wo_a,
            &l.wo_b,
            cfg.o_groups,
            cfg.o_lora_rank,
            None,
            &mut batched,
        );
        assert_eq!(batched, walked);
    }

    #[test]
    fn block_moe_matches_position_walk_in_route_order() {
        let (_g, layers, cfg) = toy();
        // The scored layer exercises repeated and distinct experts without
        // tying the result to a token-id table.
        let l = &layers[1];
        let b = 5;
        let xs: Vec<f32> = (0..b * cfg.dim)
            .map(|i| ((i * 11) as f32 * 0.019).cos())
            .collect();
        let ids = [1u32, 2, 3, 4, 5];
        let mut walked = vec![0.0f32; b * cfg.dim];
        for bi in 0..b {
            moe_step(
                &xs[bi * cfg.dim..(bi + 1) * cfg.dim],
                l,
                &cfg,
                ids[bi],
                1,
                None,
                &mut walked[bi * cfg.dim..(bi + 1) * cfg.dim],
            );
        }
        let mut batched = vec![0.0f32; b * cfg.dim];
        moe_step_block(&xs, b, l, &cfg, &ids, 1, None, &mut batched);
        assert_eq!(batched, walked);
    }

    /// The overlapping compressor folds 2*ratio slots, not ratio: the
    /// previous window contributes its first half of dimensions and the
    /// current one its second half. Treating it as a plain compressor makes
    /// the entry twice as wide as the cache expects, which lands the whole
    /// thing in the wrong store rather than raising anything.
    #[test]
    fn overlapping_compressor_folds_both_windows() {
        let (ratio, d) = (2usize, 3usize);
        // Current window: two tokens, 2*d wide each. Second half is what the
        // current window contributes.
        let cur_kv: Vec<f32> = vec![
            1.0, 1.0, 1.0, /*|*/ 10.0, 20.0, 30.0, // token 0
            2.0, 2.0, 2.0, /*|*/ 40.0, 50.0, 60.0, // token 1
        ];
        // Make the current window's second-half scores dominate everywhere.
        let cur_sc: Vec<f32> = vec![
            0.0, 0.0, 0.0, /*|*/ 0.0, 0.0, 100.0, //
            0.0, 0.0, 0.0, /*|*/ 100.0, 100.0, 0.0,
        ];
        // Previous window: its FIRST half is what it contributes.
        let prev_kv: Vec<f32> = vec![
            7.0, 8.0, 9.0, /*|*/ 0.0, 0.0, 0.0, //
            5.0, 6.0, 7.0, /*|*/ 0.0, 0.0, 0.0,
        ];
        let prev_sc = vec![0.0f32; ratio * 2 * d];

        let mut out = vec![0.0f32; d];
        compress_window_overlap(&prev_kv, &prev_sc, &cur_kv, &cur_sc, ratio, d, &mut out);
        // dim 0 and 1: token 1's second half wins (score 100)
        assert!((out[0] - 40.0).abs() < 1e-3, "dim0 = {}", out[0]);
        assert!((out[1] - 50.0).abs() < 1e-3, "dim1 = {}", out[1]);
        // dim 2: token 0's second half wins
        assert!((out[2] - 30.0).abs() < 1e-3, "dim2 = {}", out[2]);

        // With no previous window the fold still works and uses only the
        // current one — this is the very first window of a generation.
        let mut first = vec![0.0f32; d];
        compress_window_overlap(&[], &[], &cur_kv, &cur_sc, ratio, d, &mut first);
        assert!(
            first.iter().all(|v| v.is_finite()),
            "first window: {first:?}"
        );
        assert!((first[0] - 40.0).abs() < 1e-3, "first dim0 = {}", first[0]);

        // And a previous window with real scores does pull the result.
        let mut both = vec![0.0f32; d];
        let strong_prev = vec![100.0f32; ratio * 2 * d];
        compress_window_overlap(
            &prev_kv,
            &strong_prev,
            &cur_kv,
            &cur_sc,
            ratio,
            d,
            &mut both,
        );
        assert!(
            (both[0] - 40.0).abs() > 1.0,
            "a scored previous window must move the fold, got {}",
            both[0]
        );
    }

    /// Numerical parity with the reference. The vectors below come from
    /// running `kernel.py::hc_split_sinkhorn`'s own formula on a fixed
    /// input; matching them pins the exponent order, the eps placement and
    /// the off-by-one in the iteration count all at once — a property test
    /// alone would pass with any of those wrong.
    #[test]
    fn sinkhorn_matches_the_reference_numbers() {
        let hc = 4;
        let mixes: Vec<f32> = (0..24).map(|i| (i as f32 * 0.37).sin() * 3.0).collect();
        let base: Vec<f32> = (0..24).map(|i| (i as f32 * 0.11).cos()).collect();
        let (mut pre, mut post, mut comb) = (vec![0.0; hc], vec![0.0; hc], vec![0.0; hc * hc]);
        hc_split_sinkhorn(
            &mixes,
            &[1.0, 1.0, 1.0],
            &base,
            hc,
            20,
            1e-6,
            &mut pre,
            &mut post,
            &mut comb,
        );
        let want_pre = [0.7310596, 0.8888268, 0.9525191, 0.97424865];
        let want_post = [1.9600224, 1.9534285, 1.9201256, 1.8160983];
        let want_comb = [
            0.5996052,
            0.28253591,
            0.09218107,
            0.025676856,
            0.17564717,
            0.22228767,
            0.27174541,
            0.33031881,
            0.029528176,
            0.12206022,
            0.32619134,
            0.5222193,
            0.19521846,
            0.37311527,
            0.30988118,
            0.12178412,
        ];
        for (i, w) in want_pre.iter().enumerate() {
            assert!((pre[i] - w).abs() < 1e-5, "pre[{i}]: {} vs {w}", pre[i]);
        }
        for (i, w) in want_post.iter().enumerate() {
            assert!((post[i] - w).abs() < 1e-5, "post[{i}]: {} vs {w}", post[i]);
        }
        for (i, w) in want_comb.iter().enumerate() {
            assert!((comb[i] - w).abs() < 1e-4, "comb[{i}]: {} vs {w}", comb[i]);
        }
    }

    /// Sinkhorn's whole point is a doubly stochastic matrix: every row and
    /// every column sums to one. If the alternating normalization is wrong
    /// (or the loop count is off by one) the sums drift, and the residual
    /// mixing quietly gains or loses mass on every layer.
    #[test]
    fn sinkhorn_leaves_the_mixing_matrix_doubly_stochastic() {
        let hc = 4;
        let mix_hc = (2 + hc) * hc;
        // a deliberately lopsided projection
        let mixes: Vec<f32> = (0..mix_hc).map(|i| (i as f32 * 0.37).sin() * 3.0).collect();
        let base: Vec<f32> = (0..mix_hc).map(|i| (i as f32 * 0.11).cos()).collect();
        let (mut pre, mut post, mut comb) = (vec![0.0; hc], vec![0.0; hc], vec![0.0; hc * hc]);
        hc_split_sinkhorn(
            &mixes,
            &[1.0, 1.0, 1.0],
            &base,
            hc,
            20,
            1e-6,
            &mut pre,
            &mut post,
            &mut comb,
        );
        for j in 0..hc {
            let r: f32 = comb[j * hc..(j + 1) * hc].iter().sum();
            assert!((r - 1.0).abs() < 2e-3, "row {j} sums to {r}");
            let c: f32 = (0..hc).map(|k| comb[k * hc + j]).sum();
            assert!((c - 1.0).abs() < 2e-3, "col {j} sums to {c}");
        }
        // pre is a gate in (eps, 1+eps); post carries the factor 2
        assert!(pre.iter().all(|&v| v > 0.0 && v < 1.001));
        assert!(post.iter().all(|&v| v >= 0.0 && v <= 2.0));
    }

    /// Folding four copies and expanding them back must preserve a constant
    /// state exactly when the block contributes nothing: with post = 0 the
    /// expansion is a doubly stochastic mix of identical copies, i.e. itself.
    #[test]
    fn expand_of_identical_copies_is_a_fixed_point() {
        let (hc, dim) = (4usize, 3usize);
        let residual: Vec<f32> = std::iter::repeat([1.5f32, -2.0, 0.25])
            .take(hc)
            .flatten()
            .collect();
        let comb = {
            // exactly doubly stochastic: uniform
            vec![0.25f32; hc * hc]
        };
        let post = vec![0.0f32; hc];
        let mut out = vec![0.0f32; hc * dim];
        hc_expand(&[0.0; 3], &residual, &post, &comb, hc, dim, &mut out);
        for (o, r) in out.iter().zip(&residual) {
            assert!((o - r).abs() < 1e-6, "{o} vs {r}");
        }
    }

    /// The bias must move the SELECTION without touching the weights: with a
    /// large bias on a low-scoring expert it gets picked, but its weight is
    /// still its own (small) score, renormalized.
    #[test]
    fn selection_bias_steers_the_choice_but_not_the_weights() {
        let scores = [3.0f32, 0.1, 2.0, 0.05];
        let bias = [0.0f32, 10.0, 0.0, 0.0];
        let (mut idx, mut w) = (Vec::new(), Vec::new());
        route(&scores, Some(&bias), 2, 1.5, None, None, &mut idx, &mut w);
        assert_eq!(idx[0], 1, "the biased expert must win selection");
        assert_eq!(idx[1], 0);
        // weights come from sqrt(softplus(score)) BEFORE the bias, so the
        // biased expert's share must be the smaller of the two
        assert!(w[0] < w[1], "biased expert kept its own (small) weight");
        let sum: f32 = w.iter().sum();
        assert!((sum - 1.5).abs() < 1e-5, "weights renormalize then scale");
    }

    /// The sink is an extra logit with no value: it must lower every
    /// weight without adding output. With a huge sink the head should
    /// attend to almost nothing.
    #[test]
    fn attention_sink_drains_weight_without_contributing_output() {
        let hd = 2;
        let q = [1.0f32, 0.0];
        let kv = [1.0f32, 0.0, 0.0, 1.0];
        let mut out = vec![0.0f32; hd];
        sparse_attend(&q, &kv, &[0, 1], f32::NEG_INFINITY, 1.0, hd, &mut out);
        let plain = out.clone();
        assert!(plain[0] > plain[1], "the aligned key must dominate");
        sparse_attend(&q, &kv, &[0, 1], 20.0, 1.0, hd, &mut out);
        assert!(
            out[0] < plain[0] * 0.01 && out[1] < plain[1] * 0.01,
            "a large sink must drain nearly all the mass: {out:?}"
        );
    }

    /// A masked slot must be ignored entirely — not folded in as a zero
    /// key, which would still add exp(0) to the denominator.
    #[test]
    fn masked_positions_leave_the_denominator_alone() {
        let hd = 2;
        let q = [1.0f32, 0.0];
        let kv = [1.0f32, 0.0, 0.0, 1.0];
        let (mut a, mut b) = (vec![0.0f32; hd], vec![0.0f32; hd]);
        sparse_attend(&q, &kv, &[0], f32::NEG_INFINITY, 1.0, hd, &mut a);
        sparse_attend(
            &q,
            &kv,
            &[0, usize::MAX],
            f32::NEG_INFINITY,
            1.0,
            hd,
            &mut b,
        );
        for (x, y) in a.iter().zip(&b) {
            assert!((x - y).abs() < 1e-6, "{x} vs {y}");
        }
    }

    /// Forward then inverse rotation is the identity — the property the
    /// output path depends on.
    #[test]
    fn rope_tail_inverts_itself() {
        let inv_freq = [1.0f32, 0.5];
        let orig = [9.0f32, 8.0, 1.0, 2.0, 3.0, 4.0];
        let mut v = orig;
        rope_tail(&mut v, &inv_freq, 7, 4, false);
        assert!(v[..2] == orig[..2], "the non-rope head must not move");
        assert!(v[2..] != orig[2..], "the tail must actually rotate");
        rope_tail(&mut v, &inv_freq, 7, 4, true);
        for (a, b) in v.iter().zip(&orig) {
            assert!((a - b).abs() < 1e-5, "{a} vs {b}");
        }
    }

    /// The window pooling is a softmax per DIMENSION over the ratio, with
    /// the position bias inside the exponent.
    #[test]
    fn compressor_pools_the_window_per_dimension() {
        let (ratio, width) = (2usize, 2usize);
        let kv = [1.0f32, 10.0, 3.0, 20.0];
        // dim 0: equal scores → mean; dim 1: second token wins by a mile
        let score = [0.0f32, 0.0, 0.0, 50.0];
        let ape = vec![0.0f32; ratio * width];
        let mut out = vec![0.0f32; width];
        compress_window(&kv, &score, &ape, ratio, width, &mut out);
        assert!(
            (out[0] - 2.0).abs() < 1e-5,
            "equal scores average: {}",
            out[0]
        );
        assert!(
            (out[1] - 20.0).abs() < 1e-3,
            "a dominant score wins: {}",
            out[1]
        );
    }

    /// A negative dot product must not drag a position down: the relu
    /// means heads abstain rather than veto.
    #[test]
    fn index_scores_relu_before_weighting() {
        let (nh, hd) = (2usize, 2usize);
        // head 0 aligns with position 0, head 1 anti-aligns with it
        let q = [1.0f32, 0.0, -1.0, 0.0];
        let kv = [1.0f32, 0.0, 0.0, 1.0];
        let w = [1.0f32, 1.0];
        let mut sc = Vec::new();
        index_scores(&q, &kv, &w, nh, hd, 2, 2, None, &mut sc);
        // without the relu the anti-aligned head would cancel head 0 to zero
        assert!(sc[0] > 0.9, "abstention, not veto: {:?}", sc);
    }

    #[test]
    fn index_scores_mask_the_future() {
        let (nh, hd) = (1usize, 2usize);
        let q = [1.0f32, 0.0];
        let kv = [1.0f32, 0.0, 1.0, 0.0, 1.0, 0.0];
        let w = [1.0f32];
        let mut sc = Vec::new();
        index_scores(&q, &kv, &w, nh, hd, 3, 2, None, &mut sc);
        assert!(sc[0].is_finite() && sc[1].is_finite());
        assert!(sc[2] == f32::NEG_INFINITY, "position 2 is in the future");
        let mut idx = Vec::new();
        top_k_positions(&sc, 3, &mut idx);
        assert_eq!(idx, vec![0, 1], "a masked slot never wins a slot");
    }

    #[test]
    fn top_k_is_deterministic_on_ties() {
        let sc = [1.0f32, 1.0, 1.0, 0.0];
        let mut idx = Vec::new();
        top_k_positions(&sc, 2, &mut idx);
        assert_eq!(idx, vec![0, 1], "ties resolve to the lower index");
    }

    /// The block cycle must leave the state's SHAPE intact (hc copies in,
    /// hc copies out) and must actually route the block's output back in:
    /// a block that writes a constant has to move every copy.
    #[test]
    fn hc_block_preserves_the_copy_structure_and_applies_the_block() {
        let cfg = Dsv4Cfg {
            dim: 4,
            n_heads: 1,
            head_dim: 4,
            rope_head_dim: 2,
            q_lora_rank: 4,
            o_lora_rank: 2,
            o_groups: 1,
            hc_mult: 4,
            hc_sinkhorn_iters: 20,
            hc_eps: 1e-6,
            norm_eps: 1e-6,
            n_routed_experts: 2,
            top_k: 1,
            moe_inter: 4,
            route_scale: 1.0,
            swiglu_limit: 10.0,
            window: 128,
            index_topk: 4,
            vocab: 8,
        };
        let (hc, dim) = (cfg.hc_mult, cfg.dim);
        let mix_hc = (2 + hc) * hc;
        let hc_fn: Vec<f32> = (0..mix_hc * hc * dim)
            .map(|i| ((i % 13) as f32 - 6.0) * 0.05)
            .collect();
        let hc_base: Vec<f32> = (0..mix_hc).map(|i| (i as f32 * 0.2).sin()).collect();
        let norm_w = vec![1.0f32; dim];
        let mut state: Vec<f32> = (0..hc * dim).map(|i| (i as f32 * 0.3).cos()).collect();
        let before = state.clone();
        let mut scratch = HcScratch::new(&cfg);
        hc_block(
            &mut state,
            &hc_fn,
            &[1.0, 1.0, 1.0],
            &hc_base,
            &norm_w,
            &cfg,
            &mut scratch,
            None,
            |_folded, out: &mut [f32]| out.iter_mut().for_each(|o| *o = 1.0),
        );
        assert_eq!(state.len(), before.len(), "copy structure must survive");
        assert!(state.iter().all(|v| v.is_finite()), "{state:?}");
        assert!(
            state.iter().zip(&before).any(|(a, b)| (a - b).abs() > 1e-4),
            "the block's output has to reach the state"
        );
    }

    #[test]
    fn hash_route_reads_the_table_row() {
        // vocab 3, top_k 2
        let table = [7.0f32, 9.0, 1.0, 2.0, 5.0, 6.0];
        assert_eq!(hash_route(&table, 3, 2, 0), vec![7, 9]);
        assert_eq!(hash_route(&table, 3, 2, 2), vec![5, 6]);
        // out-of-range ids clamp instead of panicking
        assert_eq!(hash_route(&table, 3, 2, 99), vec![5, 6]);
    }

    /// A task mask restricts SELECTION and nothing else: the weights still
    /// come from the pre-bias scores and still renormalize, now over what
    /// survives. Masking must never reroute — an expert the mask forbids has
    /// to be absent, not replaced by a neighbour with the wrong weight.
    #[test]
    fn a_task_mask_restricts_selection_and_renormalizes() {
        // Expert 3 scores highest, then 1, then 2, then 0.
        let scores = [0.1f32, 4.0, 1.0, 9.0];
        let (mut idx, mut w) = (Vec::new(), Vec::new());
        route(&scores, None, 2, 1.0, None, None, &mut idx, &mut w);
        assert_eq!(idx, vec![3, 1], "unmasked: the two best win");
        let sum: f32 = w.iter().sum();
        assert!((sum - 1.0).abs() < 1e-5, "weights must sum to route_scale");

        // Forbid the winner: the next two take its place and the weights
        // renormalize over them.
        let mask = [true, false, true, true];
        let (mut i2, mut w2) = (Vec::new(), Vec::new());
        route(&scores, None, 2, 1.0, None, Some(&mask), &mut i2, &mut w2);
        assert_eq!(i2, vec![3, 2], "masked expert must not be selected");
        let sum2: f32 = w2.iter().sum();
        assert!((sum2 - 1.0).abs() < 1e-5, "masked weights must renormalize");

        // A mask leaving fewer than top_k experts yields fewer, not garbage.
        let tight = [false, false, false, true];
        let (mut i3, mut w3) = (Vec::new(), Vec::new());
        route(&scores, None, 2, 1.0, None, Some(&tight), &mut i3, &mut w3);
        assert_eq!(i3, vec![3]);
        assert_eq!(w3.len(), 1);
    }

    /// On a hash layer the reference gathers the scores AT THE TABLE's
    /// experts. Choosing top-k first and swapping the indices afterwards
    /// leaves every weight attached to a different expert than the one it
    /// scales — silently, since both lists are the right length.
    #[test]
    fn hash_layers_weight_the_experts_the_table_names() {
        // Expert 3 scores highest, expert 0 lowest; the table names 0 and 1.
        let scores = [0.1f32, 0.4, 0.2, 5.0];
        let table = vec![0.0f32, 1.0];
        let idx_forced = hash_route(&table, 1, 2, 0);
        assert_eq!(idx_forced, vec![0, 1]);

        let (mut idx, mut w) = (Vec::new(), Vec::new());
        route(
            &scores,
            None,
            2,
            1.0,
            Some(&idx_forced),
            None,
            &mut idx,
            &mut w,
        );
        assert_eq!(idx, vec![0, 1], "the table must decide the experts");

        // The weights must be the table experts' own scores, normalized.
        let sp = |x: f32| (1.0 + x.exp()).ln().sqrt();
        let (s0, s1) = (sp(scores[0]), sp(scores[1]));
        let tot = s0 + s1;
        assert!(
            (w[0] - s0 / tot).abs() < 1e-6,
            "w[0]={} want {}",
            w[0],
            s0 / tot
        );
        assert!(
            (w[1] - s1 / tot).abs() < 1e-6,
            "w[1]={} want {}",
            w[1],
            s1 / tot
        );

        // And the top-k path is untouched: expert 3 still wins there.
        let (mut idx2, mut w2) = (Vec::new(), Vec::new());
        route(&scores, None, 2, 1.0, None, None, &mut idx2, &mut w2);
        assert_eq!(idx2[0], 3, "without a table the highest score still wins");
    }
}


// ══ DSpark: the block-parallel draft ══════════════════════════════════
//
// Not a classic MTP chain. One pass through the three stages produces the
// WHOLE block of `block_size` positions at once: position 0 carries the token
// the trunk just emitted, the rest carry a noise token, and every position
// attends to every other one — which is why the block cannot be measured a
// position at a time and pretend to be faithful. Depth comes from the block,
// not from the stage count.
//
// The stages' KV cache is built from the trunk's hidden state, not from the
// draft's own tokens: one entry per real position, `kv_norm(wkv(main_x))`,
// in a ring of `window`. The block's own keys and values are appended for
// the duration of the block and then discarded.

/// The noise token the block's unknown positions carry
/// (`dspark_noise_token_id`).
pub const DSPARK_NOISE_TOKEN: u32 = 128799;
/// `dspark_block_size` — the width of the draft block, and NOT a tuning knob.
///
/// All five positions attend to each other and the model was trained with
/// exactly four noise slots behind the real token, so a narrower block is a
/// different draft model, not a cheaper one. What the survival curve argues
/// for is verifying fewer of the five — see `dspark_verify_k` — which costs
/// less without changing what the draft computes.
pub fn dspark_block() -> usize {
    5
}

/// How many of the block's proposals the trunk actually checks.
///
/// Survival is [0.67, 0.50, 0.29, 0.08, 0.04]: positions four and five are
/// paid for on every verify and delivered on a twelfth of them. Three yields
/// 2.46 tokens a cycle against five's 2.58, for three fifths of the verify.
/// `CMF_DSPARK_VERIFY_K=N` sets it.
pub fn dspark_verify_k() -> usize {
    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *K.get_or_init(|| {
        std::env::var("CMF_DSPARK_VERIFY_K")
            .ok()
            .and_then(|v| v.parse::<usize>().ok())
            .filter(|&n| (1..=DSPARK_BLOCK_MAX).contains(&n))
            .unwrap_or(DSPARK_BLOCK_MAX)
    })
}

/// The trained block width.
pub const DSPARK_BLOCK_MAX: usize = 5;

/// Per-sequence state of the draft: one KV ring per stage, and the trunk
/// hidden states the block's input is projected from.
pub struct DsparkState {
    /// `[stage][window * kv_width]`, written at `pos % window`.
    pub win: Vec<Vec<f32>>,
    /// How many real positions each ring holds, capped at `window`.
    pub filled: Vec<usize>,
    /// The trunk's captured hidden, `dim * n_targets`, refreshed every token.
    pub main_hidden: Vec<f32>,
    /// True once `main_hidden` holds this position's capture.
    pub have_hidden: bool,
}

impl DsparkState {
    pub fn new(stages: usize, cfg: &Dsv4Cfg, targets: usize) -> Self {
        Self {
            win: vec![Vec::new(); stages],
            filled: vec![0; stages],
            main_hidden: vec![0.0; cfg.dim * targets],
            have_hidden: false,
        }
    }
}

/// Which trunk layers the draft reads. Upstream names them explicitly
/// (`dspark_target_layer_ids`); the file says the same thing less directly —
/// `main_proj` has one `dim`-wide input block per captured layer — and the
/// release captures the last three. Deriving it from the weight keeps the
/// two from disagreeing.
pub fn dspark_targets(mtp: &[Dsv4Mtp], cfg: &Dsv4Cfg, n_layers: usize) -> Vec<usize> {
    let Some(mp) = mtp.iter().find_map(|m| m.main_proj.as_ref()) else {
        return Vec::new();
    };
    let n = (mp.cols() / cfg.dim.max(1)).clamp(1, n_layers);
    (n_layers - n..n_layers).collect()
}

thread_local! {
    /// The armed capture: which layers to take, and the buffer they fill.
    /// A thread-local rather than a parameter because the capture has to
    /// reach into the middle of a layer loop that eight call sites share,
    /// and threading an optional buffer through all of them to serve one
    /// diagnostic is a worse trade than this.
    static DSPARK_CAP: std::cell::RefCell<(Vec<usize>, Vec<f32>, usize)> =
        const { std::cell::RefCell::new((Vec::new(), Vec::new(), 0)) };
}

/// Arm the capture for the layers `targets`, in order.
pub fn dspark_arm(targets: &[usize], dim: usize) {
    DSPARK_CAP.with(|c| {
        let mut c = c.borrow_mut();
        c.0 = targets.to_vec();
        c.1 = vec![0.0; dim * targets.len()];
        c.2 = 0;
    });
}

/// Whether the armed MTP capture needs the state immediately after `li`.
/// The normal decode path keeps a full run in one submission; DSpark is the
/// only caller that needs an intermediate state to cross the device boundary.
fn dspark_wants(li: usize) -> bool {
    DSPARK_CAP.with(|c| c.borrow().0.contains(&li))
}

/// Called after every host layer. Free when nothing is armed.
pub fn dspark_note(li: usize, state: &[f32], cfg: &Dsv4Cfg) {
    DSPARK_CAP.with(|c| {
        let mut c = c.borrow_mut();
        if c.0.is_empty() {
            return;
        }
        if let Some(slot) = c.0.iter().position(|&t| t == li) {
            let (_, buf, seen) = &mut *c;
            dspark_capture(state, cfg, slot, buf);
            // Counted, not "was the last one" — under the device chain only
            // the layers left on the host call this, and taking the last
            // target as the signal would hand the draft a buffer whose other
            // slots still hold the previous token, or nothing at all.
            *seen = if slot == 0 { 1 } else { *seen + 1 };
            if std::env::var("CMF_DSPARK_CAP_DBG").is_ok() {
                eprintln!("[cap] note li={li} slot={slot} seen={}", *seen);
            }
        }
    });
}

/// Read one slot of the armed capture buffer as-is, complete or not. The
/// speculative verify fills the DEVICE targets from its own photographs and
/// only needs the host layers' slots from here — `dspark_take`'s
/// completeness contract would never be met on that path.
pub fn dspark_peek_slot(slot: usize, dim: usize, out: &mut [f32]) -> bool {
    DSPARK_CAP.with(|c| {
        let c = c.borrow();
        let lo = slot * dim;
        if c.1.len() < lo + dim {
            return false;
        }
        out[..dim].copy_from_slice(&c.1[lo..lo + dim]);
        true
    })
}

/// Move the capture out, if this token produced a complete one.
pub fn dspark_take(out: &mut Vec<f32>) -> bool {
    DSPARK_CAP.with(|c| {
        let mut c = c.borrow_mut();
        if c.0.is_empty() || c.2 != c.0.len() {
            if std::env::var("CMF_DSPARK_CAP_DBG").is_ok() {
                eprintln!("[cap] take FAIL armed={:?} seen={}", c.0, c.2);
            }
            return false;
        }
        out.clear();
        out.extend_from_slice(&c.1);
        c.2 = 0;
        true
    })
}

/// The trunk's contribution: the mean over the hyper-connection copies,
/// appended in target order. Costs one pass over `hc * dim` per captured
/// layer and nothing else.
pub fn dspark_capture(state: &[f32], cfg: &Dsv4Cfg, slot: usize, out: &mut [f32]) {
    let (hc, dim) = (cfg.hc_mult, cfg.dim);
    let dst = &mut out[slot * dim..(slot + 1) * dim];
    let inv = 1.0 / hc as f32;
    for d in 0..dim {
        let mut s = 0.0;
        for j in 0..hc {
            s += state[j * dim + d];
        }
        dst[d] = s * inv;
    }
}

/// `CMF_DSPARK_PICK_DUMP=path` — accumulate the draft's expert picks per
/// stage and periodically rewrite `path` with `stage<TAB>expert<TAB>count`
/// lines. Rewritten every 32 blocks rather than at exit, so a run that is
/// killed still leaves the tallies on disk.
pub fn dspark_freq_note(picks: &[(usize, Vec<usize>)]) {
    static FREQ: std::sync::Mutex<Option<(std::collections::HashMap<(usize, usize), u64>, u64)>> =
        std::sync::Mutex::new(None);
    let Ok(path) = std::env::var("CMF_DSPARK_PICK_DUMP") else {
        return;
    };
    let mut g = FREQ.lock().unwrap();
    let (map, blocks) = g.get_or_insert_with(|| (std::collections::HashMap::new(), 0));
    for (stage, idx) in picks {
        for &e in idx {
            *map.entry((*stage, e)).or_insert(0) += 1;
        }
    }
    *blocks += 1;
    if *blocks % 32 == 0 {
        let mut lines: Vec<_> = map.iter().collect();
        lines.sort();
        let body: String = lines
            .iter()
            .map(|((s, e), n)| format!("{s}\t{e}\t{n}\n"))
            .collect();
        let _ = std::fs::write(&path, body);
    }
}

/// `CMF_DSV4_TRUNK_PICK_DUMP=path` — the same tally for the TRUNK's layers:
/// `layer<TAB>expert<TAB>count`, rewritten every 32 tokens. The pick lists
/// come from the probe's own tally window, so only layers that route on the
/// host are counted — which is exactly the population a partial pack serves.
pub fn trunk_freq_note(picks: &[(usize, Vec<usize>)]) {
    static FREQ: std::sync::Mutex<Option<(std::collections::HashMap<(usize, usize), u64>, u64)>> =
        std::sync::Mutex::new(None);
    let Ok(path) = std::env::var("CMF_DSV4_TRUNK_PICK_DUMP") else {
        return;
    };
    let mut g = FREQ.lock().unwrap();
    let (map, blocks) = g.get_or_insert_with(|| (std::collections::HashMap::new(), 0));
    for (li, idx) in picks {
        for &e in idx {
            *map.entry((*li, e)).or_insert(0) += 1;
        }
    }
    *blocks += 1;
    if *blocks % 32 == 0 {
        let mut lines: Vec<_> = map.iter().collect();
        lines.sort();
        let body: String = lines
            .iter()
            .map(|((l, e), n)| format!("{l}\t{e}\t{n}\n"))
            .collect();
        let _ = std::fs::write(&path, body);
    }
}

/// `CMF_DSPARK_MASK=path` — restrict the draft's routed experts to an
/// explicit per-stage keep-set: line `d` of the file lists the expert ids
/// stage `d` may route to, comma-separated. Weights renormalize over what
/// remains (the `Dsv4Layer::mask` contract). The draft only proposes — the
/// trunk still verifies every token — so a thinner draft costs acceptance,
/// never correctness. This is the offline dial for sizing a resident
/// device pack before one exists.
fn dspark_apply_mask(out: &mut [Dsv4Mtp]) {
    let Ok(path) = std::env::var("CMF_DSPARK_MASK") else {
        return;
    };
    let Ok(text) = std::fs::read_to_string(&path) else {
        eprintln!("DSpark: CMF_DSPARK_MASK={path} не читается — маска не применена");
        return;
    };
    for (d, line) in text.lines().enumerate() {
        let Some(m) = out.get_mut(d) else { break };
        let n = m.layer.experts.len();
        let mut mask = vec![false; n];
        let mut kept = 0usize;
        for tok in line.split(',') {
            if let Ok(e) = tok.trim().parse::<usize>() {
                if e < n && !mask[e] {
                    mask[e] = true;
                    kept += 1;
                }
            }
        }
        if kept == 0 {
            continue;
        }
        eprintln!("DSpark: стадия {d} ограничена {kept}/{n} экспертами");
        m.layer.mask = Some(mask);
    }
}

/// The draft's device residency: which experts of each stage live on the
/// card, and how the device router reaches them.
///
/// The draft only proposes — the trunk verifies every token — so the pack
/// is free to keep a SUBSET of each stage's experts and mask the routing to
/// it: acceptance pays, correctness never does. The subset is chosen by
/// measured routing frequency (`CMF_DSPARK_PACK` names the tally file that
/// `CMF_DSPARK_PICK_DUMP` wrote; `CMF_DSPARK_RESIDENT` caps experts per
/// stage, default 48).
#[cfg(feature = "gpu")]
pub struct DsparkPack {
    pub stages: Vec<DsparkStagePack>,
    /// Gate/up requantized to q2tp at upload (the binary registered an
    /// encoder); the graph then dispatches the q2tp kernels.
    pub gu_q2: bool,
    /// The down planes too (native in the file, never requantized at
    /// upload); the graph dispatches the 2-bit down kernel.
    pub dn_q2: bool,
    /// Dequantized router and bias per stage, f32 — address-stable for the
    /// life of the pack, which is what the device's const cache needs.
    pub routers: Vec<Vec<f32>>,
    pub biases: Vec<Option<Vec<f32>>>,
}

#[cfg(feature = "gpu")]
pub struct DsparkStagePack {
    /// Selectable experts (true = resident).
    pub mask: Vec<bool>,
    /// Global expert id → pack slot; usize::MAX where cold.
    pub to_slot: Vec<usize>,
    /// The same two as the device consumes them — u32, address-stable for
    /// the pack's lifetime (the const cache keys on the pointer).
    pub mask_u32: Vec<u32>,
    pub map_u32: Vec<u32>,
    /// (gate, up, down) directory indices, pack order, shared LAST.
    pub tensors: Vec<(usize, usize, usize)>,
    pub n_resident: usize,
}

/// The q2tp encoder, registered by the binary that has one (the CLI's
/// converter owns the rung-search implementation and the engine must not
/// depend on the CLI). When present, the draft's gate/up experts are
/// requantized q4tp → q2tp AT UPLOAD — half the VRAM and the same kernels
/// the trunk's q2tp experts already use. Draft-only fidelity: acceptance
/// pays, correctness never does.
pub static DSPARK_Q2TP_ENCODE: std::sync::OnceLock<fn(&[f32], usize, usize) -> Vec<u8>> =
    std::sync::OnceLock::new();

/// `CMF_DSPARK_GPU=1` — the probe (and later the speculative loop) drafts
/// on the card instead of the CPU/disk tier.
#[cfg(feature = "gpu")]
pub fn dspark_gpu_on() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("CMF_DSPARK_GPU").map(|v| v != "0").unwrap_or(true))
}

/// The pack, built once per process (the stand runs one model).
#[cfg(feature = "gpu")]
pub fn dspark_pack_get(mtp: &[Dsv4Mtp], cfg: &Dsv4Cfg) -> Option<&'static DsparkPack> {
    static P: std::sync::OnceLock<Option<Box<DsparkPack>>> = std::sync::OnceLock::new();
    P.get_or_init(|| dspark_pack_build(mtp, cfg).map(Box::new)).as_deref()
}

/// Build and upload the draft's pack. Returns `None` when the stack is
/// absent, the budget refuses, or a stage's weights are not where the
/// device path needs them — the caller falls back to the CPU draft.
/// Reserve the VRAM the speculative draft's device pack will take, so the
/// trunk's greedy packing leaves it room. Called at load, before any trunk
/// pack is built; a no-op when there is no MTP stack or speculation is off.
/// The estimate uses the draft's native dtypes — an upload-time re-encode
/// only shrinks it, which errs on the safe side of the physical ceiling.
///
/// A budget that cannot pack the trunk to the draft's capture layers (the
/// last three) gets NO reservation: speculation will decline there anyway,
/// and the carve-out would only shrink the walk's packs — measured 13% of
/// decode on a 64 GB budget. The threshold is geometric (nine tenths of
/// the trunk's own expert bytes plus the draft), never a card name.
#[cfg(feature = "gpu")]
pub fn dspark_reserve_note(mtp: &[Dsv4Mtp], cfg: &Dsv4Cfg, layers: &[Dsv4Layer]) {
    if mtp.is_empty()
        || std::env::var("CMF_DSV4_SPEC").is_ok_and(|v| v == "0")
        || !dspark_gpu_on()
    {
        return;
    }
    let dt = |q2: bool| {
        if q2 {
            cortiq_core::TensorDtype::Q2TiledP
        } else {
            cortiq_core::TensorDtype::Q4TiledP
        }
    };
    let gu_q2 = mtp[0].layer.experts.first().is_some_and(|e| {
        e.w1.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)
    });
    let dn_q2 = mtp[0].layer.experts.first().is_some_and(|e| {
        e.w2.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)
    });
    let gu = cortiq_core::quant::expected_nbytes(dt(gu_q2), &[cfg.moe_inter, cfg.dim])
        .unwrap_or(0);
    let dn = cortiq_core::quant::expected_nbytes(dt(dn_q2), &[cfg.dim, cfg.moe_inter])
        .unwrap_or(0);
    let per = (2 * gu + dn) as u64;
    let n_res: usize = std::env::var("CMF_DSPARK_RESIDENT")
        .ok()
        .and_then(|v| v.parse().ok())
        // The default matches the measured acceptance plateau's low edge:
        // residency below it costs acceptance, above it only costs VRAM.
        .unwrap_or(40);
    // Routed residents per stage, plus each stage's shared expert.
    let bytes = per * (n_res * mtp.len() + mtp.len() + 1) as u64;
    // The trunk's own expert bytes, from the layers as they are.
    let trunk: u64 = layers
        .iter()
        .map(|l| {
            let Some(e) = l.experts.first() else { return 0 };
            let gu = cortiq_core::quant::expected_nbytes(
                dt(e.w1.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)),
                &[cfg.moe_inter, cfg.dim],
            )
            .unwrap_or(0);
            let dn = cortiq_core::quant::expected_nbytes(
                dt(e.w2.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)),
                &[cfg.dim, cfg.moe_inter],
            )
            .unwrap_or(0);
            ((2 * gu + dn) * (l.experts.len() + 1)) as u64
        })
        .sum();
    if let Some(budget) = crate::gpu_wgpu::dsv4_vram_budget() {
        if budget < trunk / 10 * 9 + bytes {
            return;
        }
    }
    crate::gpu_wgpu::DRAFT_RESERVE.store(bytes, std::sync::atomic::Ordering::Relaxed);
}

#[cfg(not(feature = "gpu"))]
pub fn dspark_reserve_note(_mtp: &[Dsv4Mtp], _cfg: &Dsv4Cfg, _layers: &[Dsv4Layer]) {}

#[cfg(feature = "gpu")]
pub fn dspark_pack_build(mtp: &[Dsv4Mtp], cfg: &Dsv4Cfg) -> Option<DsparkPack> {
    if mtp.is_empty() {
        return None;
    }
    let n_res: usize = std::env::var("CMF_DSPARK_RESIDENT")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or_else(|| {
            // No knob: take what the card actually has left, whatever the
            // card is. The stages split the fit evenly after their shared
            // experts; the clamp keeps the band where drafting is known to
            // be worth the VRAM at the low end and past diminishing
            // returns at the high one.
            let native_q2 = mtp[0].layer.experts.first().is_some_and(|e| {
                e.w1.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)
            });
            let gu_q2 = native_q2 || DSPARK_Q2TP_ENCODE.get().is_some();
            let dn_q2 = mtp[0].layer.experts.first().is_some_and(|e| {
                e.w2.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)
            });
            let room = crate::gpu_wgpu::dsv4_draft_fit(cfg.moe_inter, cfg.dim, gu_q2, dn_q2);
            (room.saturating_sub(mtp.len() + 1) / mtp.len().max(1)).clamp(8, 64)
        });
    // Frequency tallies: lines of `stage<TAB>expert<TAB>count`. Named by
    // `CMF_DSPARK_PACK`, or found as `<model>.dspark.tsv` beside the model
    // file — ship the tally next to the checkpoint and no knob is needed.
    let mut freq: Vec<Vec<(u64, usize)>> = vec![Vec::new(); mtp.len()];
    let pack_path = std::env::var("CMF_DSPARK_PACK").ok().or_else(|| {
        let m = mtp[0].layer.experts.first()?.w1.model_arc()?;
        let mut s = m.path.as_os_str().to_os_string();
        s.push(".dspark.tsv");
        let p = std::path::PathBuf::from(s);
        p.exists().then(|| p.to_string_lossy().into_owned())
    });
    if let Some(path) = pack_path {
        if let Ok(text) = std::fs::read_to_string(&path) {
            for line in text.lines() {
                let mut it = line.split_whitespace();
                if let (Some(s), Some(e), Some(n)) = (it.next(), it.next(), it.next()) {
                    if let (Ok(s), Ok(e), Ok(n)) =
                        (s.parse::<usize>(), e.parse::<usize>(), n.parse::<u64>())
                    {
                        if s < freq.len() {
                            freq[s].push((n, e));
                        }
                    }
                }
            }
        }
    }
    let mut stages = Vec::with_capacity(mtp.len());
    let mut routers = Vec::with_capacity(mtp.len());
    let mut biases = Vec::with_capacity(mtp.len());
    for (si, m) in mtp.iter().enumerate() {
        let l = &m.layer;
        let n = l.experts.len();
        // Frequency order, then the untallied ids — a cold start still
        // packs SOMETHING deterministic.
        let mut order: Vec<usize> = {
            let mut f = freq[si].clone();
            f.sort_by(|a, b| b.0.cmp(&a.0));
            let mut seen = vec![false; n];
            let mut o: Vec<usize> = f
                .into_iter()
                .map(|(_, e)| e)
                .filter(|&e| {
                    if e < n && !seen[e] {
                        seen[e] = true;
                        true
                    } else {
                        false
                    }
                })
                .collect();
            o.extend((0..n).filter(|&e| !seen[e]));
            o
        };
        order.truncate(n_res.min(n));
        let mut mask = vec![false; n];
        let mut to_slot = vec![usize::MAX; n];
        let mut tensors = Vec::with_capacity(order.len() + 1);
        for (slot, &e) in order.iter().enumerate() {
            let ex = &l.experts[e];
            let (Some(w1), Some(w3), Some(w2)) =
                (ex.w1.model_idx(), ex.w3.model_idx(), ex.w2.model_idx())
            else {
                return None;
            };
            mask[e] = true;
            to_slot[e] = slot;
            tensors.push((w1, w3, w2));
        }
        let (Some(s1), Some(s3), Some(s2)) = (
            l.shared.w1.model_idx(),
            l.shared.w3.model_idx(),
            l.shared.w2.model_idx(),
        ) else {
            return None;
        };
        tensors.push((s1, s3, s2));
        // The router and bias, dequantized once.
        let mut router = vec![0.0f32; n * cfg.dim];
        for (r, row) in (0..n).zip(router.chunks_mut(cfg.dim)) {
            l.gate.row_f32(r, row);
        }
        routers.push(router);
        biases.push(l.gate_bias.clone());
        let mask_u32: Vec<u32> = mask.iter().map(|&m| m as u32).collect();
        let map_u32: Vec<u32> = to_slot
            .iter()
            .map(|&x| if x == usize::MAX { u32::MAX } else { x as u32 })
            .collect();
        stages.push(DsparkStagePack {
            mask,
            to_slot,
            mask_u32,
            map_u32,
            tensors,
            n_resident: order.len(),
        });
    }
    // ── upload: the small skeleton FIRST, the expert stacks after — the
    //    documented admission order (experts fill the card and the skeleton
    //    then misses). ──
    let model = mtp[0].layer.experts.first().and_then(|e| e.w1.model_arc())?;
    let mut skeleton = Vec::new();
    for m in mtp {
        let l = &m.layer;
        for t in [&l.wq_a, &l.wq_b, &l.wkv, &l.wo_a, &l.wo_b] {
            skeleton.push(t.model_idx()?);
        }
    }
    if let Some(mp) = mtp[0].main_proj.as_ref() {
        skeleton.push(mp.model_idx()?);
    }
    for &idx in &skeleton {
        if !crate::gpu_wgpu::dsv4_weight_ready(&model, idx) {
            eprintln!("DSpark: скелет драфта не влез в VRAM — GPU-черновик выключен");
            return None;
        }
    }
    // The dtype in the FILE decides: a properly converted CMF stores the
    // draft's gate/up as q2tp and uploads through the same path as the
    // trunk's 2-bit experts. The at-upload requant is only the fallback for
    // files published before the converter's q2tp profile covered the MTP
    // stack (and only when the binary registered an encoder).
    let native_q2 = mtp[0].layer.experts.first().is_some_and(|e| {
        e.w1.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)
    });
    let gu_q2 = native_q2 || crate::dsv4::DSPARK_Q2TP_ENCODE.get().is_some();
    let dn_native = mtp[0].layer.experts.first().is_some_and(|e| {
        e.w2.model_dtype() == Some(cortiq_core::TensorDtype::Q2TiledP)
    });
    for (si, sp) in stages.iter().enumerate() {
        let ok = if native_q2 {
            crate::gpu_wgpu::dsv4_experts_ready(
                &model, &sp.tensors, cfg.moe_inter, cfg.dim, true, dn_native,
            )
        } else if gu_q2 {
            crate::gpu_wgpu::moe_expert_bufs_requant_gu(
                &model,
                &sp.tensors,
                cfg.moe_inter,
                cfg.dim,
            )
            .is_some()
        } else {
            crate::gpu_wgpu::dsv4_experts_ready(
                &model, &sp.tensors, cfg.moe_inter, cfg.dim, false, false,
            )
        };
        if !ok {
            eprintln!(
                "DSpark: эксперты стадии {si} ({} + shared) не влезли в VRAM — GPU-черновик выключен",
                sp.n_resident
            );
            return None;
        }
    }
    let _ = crate::gpu_wgpu::pin_weights(&model, &skeleton);
    eprintln!(
        "DSpark: пак драфта на карте — {} стадии по {} экспертов + shared",
        stages.len(),
        stages.iter().map(|s| s.n_resident.to_string()).collect::<Vec<_>>().join("/")
    );
    Some(DsparkPack {
        stages,
        gu_q2,
        dn_q2: dn_native,
        routers,
        biases,
    })
}

/// Append one real position's entry to every stage's KV ring, from the
/// trunk captures in `ds.main_hidden`. The draft does this for the position
/// it drafts at; a speculative decode also owes an entry for every accepted
/// position it never drafted from — a hole in the ring silently starves
/// later blocks of context, which reads as "acceptance decayed" and not as
/// a bug.
pub fn dspark_ring_append(
    g: &Dsv4Globals,
    mtp: &[Dsv4Mtp],
    cfg: &Dsv4Cfg,
    ds: &mut DsparkState,
    pos: usize,
    pool: Option<&crate::pool::Pool>,
) {
    let (dim, hd, rd) = (cfg.dim, cfg.head_dim, cfg.rope_head_dim);
    let inv_freq = &g.inv_freq_window;
    let Some(stage0) = mtp.first() else { return };
    let (Some(mp), Some(mn)) = (stage0.main_proj.as_ref(), stage0.main_norm.as_ref()) else {
        return;
    };
    let mut main_x = vec![0.0f32; dim];
    mp.matvec(&ds.main_hidden, &mut main_x, pool);
    rms_weighted(&mut main_x, mn, cfg.norm_eps);
    for (si, m) in mtp.iter().enumerate() {
        let kvw = m.layer.wkv.rows();
        if ds.win[si].len() < cfg.window * kvw {
            ds.win[si].resize(cfg.window * kvw, 0.0);
        }
        let mut kv = vec![0.0f32; kvw];
        m.layer.wkv.matvec(&main_x, &mut kv, pool);
        rms_weighted(&mut kv, &m.layer.kv_norm, cfg.norm_eps);
        rope_tail(&mut kv[kvw - hd..], inv_freq, pos, rd, false);
        let slot = pos % cfg.window;
        ds.win[si][slot * kvw..(slot + 1) * kvw].copy_from_slice(&kv);
        ds.filled[si] = (pos + 1).min(cfg.window);
    }
}

/// The draft block on the card: one submission for all three stages and
/// five positions, states home in one fence, the head on the host. The
/// markov bias is skipped (its per-position chain through the previous
/// PROPOSAL is the one part a single graph cannot batch) — compare against
/// the CPU draft under `CMF_DSPARK_NO_MARKOV=1`.
#[cfg(feature = "gpu")]
#[allow(clippy::too_many_arguments)]
pub fn dspark_draft_gpu(
    g: &Dsv4Globals,
    mtp: &[Dsv4Mtp],
    cfg: &Dsv4Cfg,
    ds: &mut DsparkState,
    pack: &DsparkPack,
    kv_id: u64,
    last_token: u32,
    pos: usize,
    pool: Option<&crate::pool::Pool>,
    out_conf: &mut Vec<f32>,
) -> Vec<u32> {
    let (hc, dim) = (cfg.hc_mult, cfg.dim);
    let block = dspark_block();
    let Some(model) = mtp[0].layer.experts.first().and_then(|e| e.w1.model_arc()) else {
        return Vec::new();
    };
    let (Some(mp), Some(mn)) = (mtp[0].main_proj.as_ref(), mtp[0].main_norm.as_ref()) else {
        return Vec::new();
    };
    let Some(mp_idx) = mp.model_idx() else {
        return Vec::new();
    };
    let mut stages = Vec::with_capacity(mtp.len());
    for (si, m) in mtp.iter().enumerate() {
        let l = &m.layer;
        let (Some(wq_a), Some(wq_b), Some(wo_a), Some(wo_b), Some(wkv)) = (
            l.wq_a.model_idx(),
            l.wq_b.model_idx(),
            l.wo_a.model_idx(),
            l.wo_b.model_idx(),
            l.wkv.model_idx(),
        ) else {
            return Vec::new();
        };
        let sp = &pack.stages[si];
        stages.push(crate::gpu_wgpu::DsparkStageW {
            wq_a,
            wq_b,
            wo_a,
            wo_b,
            wkv,
            q_norm: &l.q_norm,
            kv_norm: &l.kv_norm,
            attn_norm: &l.attn_norm,
            ffn_norm: &l.ffn_norm,
            sink: &l.attn_sink,
            hc_attn_fn: &l.hc_attn_fn,
            hc_attn_scale: &l.hc_attn_scale,
            hc_attn_base: &l.hc_attn_base,
            hc_ffn_fn: &l.hc_ffn_fn,
            hc_ffn_scale: &l.hc_ffn_scale,
            hc_ffn_base: &l.hc_ffn_base,
            router: &pack.routers[si],
            bias: pack.biases[si].as_deref(),
            experts: &sp.tensors,
            mask_u32: &sp.mask_u32,
            map_u32: &sp.map_u32,
        });
    }
    let geom = crate::gpu_wgpu::DsparkGeom {
        dim,
        hc,
        nh: cfg.n_heads,
        hd: cfg.head_dim,
        rd: cfg.rope_head_dim,
        q_lora: cfg.q_lora_rank,
        o_lora: cfg.o_lora_rank,
        o_groups: cfg.o_groups,
        inter: cfg.moe_inter,
        n_experts: cfg.n_routed_experts,
        top_k: cfg.top_k,
        window: cfg.window,
        eps: cfg.norm_eps,
        hc_eps: cfg.hc_eps,
        sinkhorn_iters: cfg.hc_sinkhorn_iters,
        route_scale: cfg.route_scale,
        swiglu_limit: cfg.swiglu_limit,
        scale: (cfg.head_dim as f32).powf(-0.5),
        gu_q2: pack.gu_q2,
        dn_q2: pack.dn_q2,
    };
    // ── seed states: the real token, then noise, replicated over copies ──
    let ids: Vec<u32> = (0..block)
        .map(|i| if i == 0 { last_token } else { DSPARK_NOISE_TOKEN })
        .collect();
    let mut states0 = vec![0.0f32; block * hc * dim];
    let mut emb = vec![0.0f32; dim];
    for (i, &id) in ids.iter().enumerate() {
        g.embed.row_f32(id as usize, &mut emb);
        for j in 0..hc {
            states0[(i * hc + j) * dim..(i * hc + j + 1) * dim].copy_from_slice(&emb);
        }
    }
    let dspark_time = {
        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
        *ON.get_or_init(|| std::env::var("CMF_DSPARK_TIME").is_ok_and(|v| v != "0"))
    };
    let t0 = std::time::Instant::now();
    let filled = (pos + 1).min(cfg.window);
    let mut states = vec![0.0f32; block * hc * dim];
    if !crate::gpu_wgpu::dspark_graph(
        &model,
        &stages,
        geom,
        kv_id,
        mp_idx,
        mn,
        &ds.main_hidden,
        &states0,
        pos,
        filled,
        &g.inv_freq_window,
        block,
        &mut states,
    ) {
        return Vec::new();
    }
    for si in 0..mtp.len() {
        ds.filled[si] = filled;
    }
    let t_graph = t0.elapsed();

    // ── head, on the host: fold, norm, one B-wide matmat, argmax ──
    let last = &mtp[mtp.len() - 1];
    let (Some(hfn), Some(hbase), Some(hscale), Some(hnorm)) = (
        last.hc_head_fn.as_ref(),
        last.hc_head_base.as_ref(),
        last.hc_head_scale,
        last.norm.as_ref(),
    ) else {
        return Vec::new();
    };
    let mut head_in = vec![0.0f32; block * dim];
    let mut pre_norms = vec![vec![0.0f32; dim]; block];
    for i in 0..block {
        hc_head_fold(
            &states[i * hc * dim..(i + 1) * hc * dim],
            hfn,
            hscale,
            hbase,
            cfg,
            pool,
            &mut head_in[i * dim..(i + 1) * dim],
        );
        pre_norms[i].copy_from_slice(&head_in[i * dim..(i + 1) * dim]);
        rms_weighted(&mut head_in[i * dim..(i + 1) * dim], hnorm, cfg.norm_eps);
    }
    let t_fold = t0.elapsed();
    let mut logits = vec![0.0f32; block * cfg.vocab];
    // The B-axis q4tp kernel, one submission: `matmat` at B=5 falls to the
    // CPU tile path and measured 46 ms of a 60 ms draft.
    let head_gpu = g.head.model_idx().is_some_and(|hi| {
        crate::gpu_wgpu::q4tp_matvec_batch_for_test(
            &model,
            hi,
            &head_in,
            block,
            cfg.vocab,
            dim,
            &mut logits,
        )
    });
    if !head_gpu {
        g.head.matmat(&head_in, block, &mut logits, pool);
    }
    let t_head = t0.elapsed();
    // The markov bigram is not optional: without it acceptance fell 1.02 →
    // 0.42 on natural text. Its chain runs through the previous PROPOSAL,
    // so it stays position-by-position; the w2 matvec is big enough that
    // the QTensor route puts it on the card by itself.
    let mut proposals = Vec::with_capacity(block);
    out_conf.clear();
    let mut prev = last_token;
    let mut mk_embed = vec![0.0f32; last.markov_w1.as_ref().map_or(0, |t| t.cols())];
    let mut bias = vec![0.0f32; cfg.vocab];
    for i in 0..block {
        let row = &mut logits[i * cfg.vocab..(i + 1) * cfg.vocab];
        if let (Some(w1), Some(w2)) = (last.markov_w1.as_ref(), last.markov_w2.as_ref()) {
            w1.row_f32(prev as usize, &mut mk_embed);
            w2.matvec(&mk_embed, &mut bias, pool);
            for (a, b) in row.iter_mut().zip(&bias) {
                *a += *b;
            }
        }
        let mut best = 0usize;
        for v in 1..row.len() {
            if row[v] > row[best] {
                best = v;
            }
        }
        if let Some(cf) = last.confidence.as_ref() {
            let mut cat = pre_norms[i].clone();
            cat.extend_from_slice(&mk_embed);
            let mut sc = [0.0f32; 1];
            if cat.len() == cf.cols() {
                cf.matvec(&cat, &mut sc, pool);
            }
            out_conf.push(sc[0]);
        }
        proposals.push(best as u32);
        prev = best as u32;
    }
    if dspark_time {
        eprintln!(
            "DSpark GPU: граф {:.1} мс, фолды {:.1}, голова {:.1}, марков+argmax {:.1}",
            t_graph.as_secs_f64() * 1e3,
            (t_fold - t_graph).as_secs_f64() * 1e3,
            (t_head - t_fold).as_secs_f64() * 1e3,
            (t0.elapsed() - t_head).as_secs_f64() * 1e3,
        );
    }
    proposals
}

/// One draft: `DSPARK_BLOCK` proposed tokens and a confidence per position.
///
/// `pos` is the position of `last_token` — the block predicts `pos+1 ..
/// pos+BLOCK`. Returns the proposals in order; `out_conf` takes the
/// confidence head's score where the last stage carries one.
#[allow(clippy::too_many_arguments)]
pub fn dspark_draft(
    g: &Dsv4Globals,
    mtp: &[Dsv4Mtp],
    cfg: &Dsv4Cfg,
    ds: &mut DsparkState,
    last_token: u32,
    pos: usize,
    pool: Option<&crate::pool::Pool>,
    out_conf: &mut Vec<f32>,
) -> Vec<u32> {
    let (hc, dim, hd, rd) = (cfg.hc_mult, cfg.dim, cfg.head_dim, cfg.rope_head_dim);
    let block = dspark_block();
    let inv_freq = &g.inv_freq_window;

    // ── the block's input: main_norm(main_proj(captured hiddens)) ──
    let Some(stage0) = mtp.first() else {
        return Vec::new();
    };
    let (Some(mp), Some(mn)) = (stage0.main_proj.as_ref(), stage0.main_norm.as_ref()) else {
        return Vec::new();
    };
    dspark_ring_append(g, mtp, cfg, ds, pos, pool);

    // ── the block: the real token, then noise ──
    let ids: Vec<u32> = (0..block)
        .map(|i| if i == 0 { last_token } else { DSPARK_NOISE_TOKEN })
        .collect();
    let mut states = vec![vec![0.0f32; hc * dim]; block];
    let mut emb = vec![0.0f32; dim];
    for (i, &id) in ids.iter().enumerate() {
        g.embed.row_f32(id as usize, &mut emb);
        for j in 0..hc {
            states[i][j * dim..(j + 1) * dim].copy_from_slice(&emb);
        }
    }

    let mut scratch = HcScratch::new(cfg);
    for (si, m) in mtp.iter().enumerate() {
        let l = &m.layer;
        let kvw = l.wkv.rows();
        // ── attention half: fold every position first, because each one's
        //    keys are visible to all the others. ──
        let mut post = vec![vec![0.0f32; hc]; block];
        let mut comb = vec![vec![0.0f32; hc * hc]; block];
        let mut resid = vec![vec![0.0f32; hc * dim]; block];
        let mut folded = vec![vec![0.0f32; dim]; block];
        let mix_hc = (2 + hc) * hc;
        for i in 0..block {
            hc_mixes(
                &states[i],
                &l.hc_attn_fn,
                mix_hc,
                cfg.norm_eps,
                pool,
                &mut scratch.mixes,
            );
            hc_split_sinkhorn(
                &scratch.mixes,
                &l.hc_attn_scale,
                &l.hc_attn_base,
                hc,
                cfg.hc_sinkhorn_iters,
                cfg.hc_eps,
                &mut scratch.pre,
                &mut post[i],
                &mut comb[i],
            );
            hc_fold(&states[i], &scratch.pre, hc, dim, &mut folded[i]);
            rms_weighted(&mut folded[i], &l.attn_norm, cfg.norm_eps);
            resid[i].copy_from_slice(&states[i]);
        }
        // Keys and values of the block itself — kept for this block only.
        let folded_all: Vec<f32> = folded.iter().flatten().copied().collect();
        let mut blk_kv = vec![0.0f32; block * kvw];
        l.wkv.matmat(&folded_all, block, &mut blk_kv, pool);
        for i in 0..block {
            let dst = &mut blk_kv[i * kvw..(i + 1) * kvw];
            rms_weighted(dst, &l.kv_norm, cfg.norm_eps);
            rope_tail(&mut dst[kvw - hd..], inv_freq, pos + 1 + i, rd, false);
        }
        // The attended set: every cached real position, then the whole block.
        let win_len = ds.filled[si];
        let mut cache = Vec::with_capacity((win_len + block) * hd);
        for p in 0..win_len {
            let e = &ds.win[si][p * kvw..(p + 1) * kvw];
            cache.extend_from_slice(&e[kvw - hd..]);
        }
        for i in 0..block {
            let e = &blk_kv[i * kvw..(i + 1) * kvw];
            cache.extend_from_slice(&e[kvw - hd..]);
        }
        let idxs: Vec<usize> = (0..win_len + block).collect();
        let scale = (hd as f32).powf(-0.5);
        let qrank = l.wq_a.rows();
        let qdim = cfg.n_heads * hd;
        let mut qr = vec![0.0f32; block * qrank];
        l.wq_a.matmat(&folded_all, block, &mut qr, pool);
        for i in 0..block {
            rms_weighted(
                &mut qr[i * qrank..(i + 1) * qrank],
                &l.q_norm,
                cfg.norm_eps,
            );
        }
        let mut q = vec![0.0f32; block * qdim];
        l.wq_b.matmat(&qr, block, &mut q, pool);
        let mut attn = vec![0.0f32; block * qdim];
        for i in 0..block {
            let qi = &mut q[i * qdim..(i + 1) * qdim];
            let ai = &mut attn[i * qdim..(i + 1) * qdim];
            let qpos = pos + 1 + i;
            for h in 0..cfg.n_heads {
                let head = &mut qi[h * hd..(h + 1) * hd];
                rms_inplace(head, cfg.norm_eps);
                rope_tail(head, inv_freq, qpos, rd, false);
            }
            for h in 0..cfg.n_heads {
                let qh = &qi[h * hd..(h + 1) * hd];
                let oh = &mut ai[h * hd..(h + 1) * hd];
                sparse_attend(qh, &cache, &idxs, l.attn_sink[h], scale, hd, oh);
                rope_tail(oh, inv_freq, qpos, rd, true);
            }
        }
        let mut blk_out = vec![0.0f32; block * dim];
        o_project_block(
            &attn, block, &l.wo_a, &l.wo_b, cfg.o_groups, cfg.o_lora_rank, pool,
            &mut blk_out,
        );
        for i in 0..block {
            let mut next = vec![0.0f32; hc * dim];
            hc_expand(
                &blk_out[i * dim..(i + 1) * dim],
                &resid[i], &post[i], &comb[i], hc, dim, &mut next,
            );
            states[i] = next;
        }
        // ── MoE: fold every position, group equal experts, then expand in
        //    the original per-position route order. ──
        let mut ffn_fold = vec![0.0f32; block * dim];
        let mut ffn_post = vec![vec![0.0f32; hc]; block];
        let mut ffn_comb = vec![vec![0.0f32; hc * hc]; block];
        let mut ffn_resid = vec![vec![0.0f32; hc * dim]; block];
        for i in 0..block {
            hc_mixes(
                &states[i],
                &l.hc_ffn_fn,
                mix_hc,
                cfg.norm_eps,
                pool,
                &mut scratch.mixes,
            );
            hc_split_sinkhorn(
                &scratch.mixes,
                &l.hc_ffn_scale,
                &l.hc_ffn_base,
                hc,
                cfg.hc_sinkhorn_iters,
                cfg.hc_eps,
                &mut scratch.pre,
                &mut ffn_post[i],
                &mut ffn_comb[i],
            );
            hc_fold(
                &states[i], &scratch.pre, hc, dim,
                &mut ffn_fold[i * dim..(i + 1) * dim],
            );
            rms_weighted(
                &mut ffn_fold[i * dim..(i + 1) * dim],
                &l.ffn_norm,
                cfg.norm_eps,
            );
            ffn_resid[i].copy_from_slice(&states[i]);
        }
        let mut moe_out = vec![0.0f32; block * dim];
        moe_step_block(
            &ffn_fold, block, l, cfg, &ids, si, pool, &mut moe_out,
        );
        for i in 0..block {
            let mut next = vec![0.0f32; hc * dim];
            hc_expand(
                &moe_out[i * dim..(i + 1) * dim],
                &ffn_resid[i], &ffn_post[i], &ffn_comb[i], hc, dim, &mut next,
            );
            states[i] = next;
        }
    }

    // ── head: the last stage's fold, the trunk's own head ──
    let last = &mtp[mtp.len() - 1];
    let (Some(hfn), Some(hbase), Some(hscale), Some(hnorm)) = (
        last.hc_head_fn.as_ref(),
        last.hc_head_base.as_ref(),
        last.hc_head_scale,
        last.norm.as_ref(),
    ) else {
        return Vec::new();
    };
    let mut proposals = Vec::with_capacity(block);
    out_conf.clear();
    let mut prev = last_token;
    let mut head_in = vec![0.0f32; block * dim];
    let mut pre_norms = vec![vec![0.0f32; dim]; block];
    for i in 0..block {
        hc_head_fold(
            &states[i], hfn, hscale, hbase, cfg, pool,
            &mut head_in[i * dim..(i + 1) * dim],
        );
        pre_norms[i].copy_from_slice(&head_in[i * dim..(i + 1) * dim]);
        rms_weighted(
            &mut head_in[i * dim..(i + 1) * dim], hnorm, cfg.norm_eps,
        );
    }
    let mut logits = vec![0.0f32; block * cfg.vocab];
    g.head.matmat(&head_in, block, &mut logits, pool);
    let mut mk_embed = vec![0.0f32; last.markov_w1.as_ref().map_or(0, |t| t.cols())];
    for i in 0..block {
        let logits_i = &mut logits[i * cfg.vocab..(i + 1) * cfg.vocab];
        // The markov head biases the logits from the PREVIOUS token — a
        // rank-256 bigram the draft samples through position by position,
        // while the network itself ran the whole block at once.
        // `CMF_DSPARK_NO_MARKOV=1` drops it: the bias is sequential through
        // the block (each position needs the previous PROPOSAL), which is
        // the one part of the draft a single device graph cannot batch — so
        // its acceptance value has to be known before it earns that
        // complexity.
        let no_markov = {
            static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
            *ON.get_or_init(|| std::env::var("CMF_DSPARK_NO_MARKOV").is_ok_and(|v| v != "0"))
        };
        if no_markov {
            // Still feed the confidence head's embedding slot below.
            if let Some(w1) = last.markov_w1.as_ref() {
                w1.row_f32(prev as usize, &mut mk_embed);
            }
        } else if let (Some(w1), Some(w2)) = (last.markov_w1.as_ref(), last.markov_w2.as_ref()) {
            w1.row_f32(prev as usize, &mut mk_embed);
            let mut bias = vec![0.0f32; cfg.vocab];
            w2.matvec(&mk_embed, &mut bias, pool);
            for (a, b) in logits_i.iter_mut().zip(&bias) {
                *a += *b;
            }
        }
        let mut best = 0usize;
        for v in 1..logits_i.len() {
            if logits_i[v] > logits_i[best] {
                best = v;
            }
        }
        if let Some(cf) = last.confidence.as_ref() {
            let mut cat = pre_norms[i].clone();
            cat.extend_from_slice(&mk_embed);
            let mut s = [0.0f32; 1];
            if cat.len() == cf.cols() {
                cf.matvec(&cat, &mut s, pool);
            }
            out_conf.push(s[0]);
        }
        proposals.push(best as u32);
        prev = best as u32;
    }
    proposals
}