franken_ocr 0.10.0

Pure-Rust, CPU-hyper-optimized runner for the Baidu Unlimited-OCR model (single-binary CLI: focr)
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
//! SR-SWA — Unlimited-OCR-2's page-aware sparse attention (plan
//! `docs/planning/UNLIMITED_OCR_2_PLAN.md` §5.4–§5.6). This file starts with the
//! page bookkeeping and the two-axis **page2d** RoPE that both the main
//! attention and the Token Sieve router rotate with (U2-B1).
//!
//! Sources are pinned at HF `baidu/Unlimited-OCR-2@5632329a`
//! (`docs/truth-pack/SOURCE_HASHES.md`): `SB` = `_sparse_base_impl.py`, `MU` =
//! `modeling_unlimitedocr.py`, `MD` = `modeling_deepseekv2.py`; `W/` is the
//! pinned SGLang wheel's `sglang/srt/`.
//!
//! **Coordinates.** A v2 prompt is `BOS (<PAGE> <image>×n)×N instruction`.
//! `cum[i]` counts `<PAGE>` markers up to and including row `i`, `last_img` is
//! the last `<image>` row and `N = cum[last_img]` the number of prompt pages.
//! - **Bucket** (SB:2554-2555): rows `≤ last_img` take `cum` (1..N); later rows
//!   (the instruction and every generated token) take `cum − N`, clamped at 0.
//!   Bucket 0 is the reserved "no page" bucket (BOS and the instruction).
//! - **`tok_pos`** (SB:2562-2565): the 0-based running count of earlier rows in
//!   the same bucket, so page `b`'s response continues after its prompt rows.
//! - **`pf_cnt[b]`**: bucket `b`'s rows `≤ last_img`.
//! - **Rope page** (SB:2052-2054 with `sparse_rope_page_mod = 2`): the bucket,
//!   shifted by the window's `page_base` parity and folded mod 2.
//!
//! There is **no 1-D fallback** (OQ-V2-6): HF falls back to YaRN RoPE when a
//! row has no `<PAGE>` or no image, but franken_ocr always builds `<PAGE><image>`
//! prompts, so such a prompt is an error here.
//!
//! **Token Sieve router** (U2-B2, plan §5.6): per layer and query row, the
//! remote 32-row blocks the main attention reads and the bias each one adds.
//! `DA` = `dash_attention.py`. Selection is discrete, so this is a scalar f32
//! reference that follows HF's inference path operation for operation.
//!
//! **Attention and cache** (U2-B3, U2-B4): block-sparse biased attention over
//! any [`BlockKv`], and the per-layer cache that stores K/V and index keys in
//! 32-row blocks and frees whole blocks no later query can read (plan §5.12).

use super::decoder::{RopeTable, apply_rope, config as decoder_config, prof};
use super::nn;
use super::tensor::Mat;
use super::weights::Weights;
use crate::error::{FocrError, FocrResult};
use crate::tokenizer::special;
use rayon::prelude::*;
use std::ops::Range;
use std::time::Instant;

/// `<PAGE>`, the v2 page marker (`config.json` `page_token_id`, added token
/// 128827).
pub const PAGE_TOKEN_ID: u32 = 128_827;

/// Attention head dimension of both the main attention and the router (128).
pub const HEAD_DIM: usize = 128;

/// Frequency pairs on the page axis (`sparse_rope_page_pairs`).
const PAGE_PAIRS: usize = 1;

/// Token-axis frequency pairs: `HEAD_DIM / 2 − PAGE_PAIRS` = 63 (SB:2027).
pub const N_TOK_FREQS: usize = HEAD_DIM / 2 - PAGE_PAIRS;

/// Token-axis RoPE base (`sparse_rope_tok_theta`).
const TOK_THETA: f32 = 10_000.0;

/// The page axis is folded modulo this (`sparse_rope_page_mod`).
const PAGE_MOD: u32 = 2;

/// Page-axis frequency: `1 / page_theta^(0 / page_pairs)` = 1 for the single
/// page pair (SB:2033-2034).
const INV_PAGE: f32 = 1.0;

/// The 63 token-axis inverse frequencies in the reference's operation order,
/// "resample" mode (SB:2030-2032): `1 / (10000 ** (f32(i) / 63))`, all f32.
/// Note the `/63`, not v1's `2i/128`.
#[must_use]
pub fn inv_tok_freqs() -> [f32; N_TOK_FREQS] {
    let mut inv = [0.0f32; N_TOK_FREQS];
    for (i, slot) in inv.iter_mut().enumerate() {
        let exponent = i as f32 / N_TOK_FREQS as f32;
        *slot = 1.0 / TOK_THETA.powf(exponent);
    }
    inv
}

/// The page-axis RoPE coordinate of a row in local `bucket` of a window whose
/// first owned page sits at global page `page_base` (W/`layers/attention/
/// unlimited_ocr_v2_backend.py:1001-1031`, W/`session/uocr_doc_reuse.py:109-121`),
/// already folded mod 2 as the rotary does.
///
/// `page_base` is 0 unwindowed and for window 0, `lo` for a recomputed window
/// starting at global page `lo`, and `lo − 1` for a reused window. Bucket 0
/// stays at 0 because the base is added only where the bucket is positive.
#[must_use]
pub fn rope_page(bucket: u32, page_base: u32) -> u32 {
    if bucket == 0 {
        0
    } else {
        (bucket % PAGE_MOD + page_base % PAGE_MOD) % PAGE_MOD
    }
}

/// The 64 rotation angles of one row: 63 token-axis angles then the page
/// angle (SB:2055-2057).
fn row_angles(inv_tok: &[f32; N_TOK_FREQS], tok_pos: u32, rope_page: u32, out: &mut [f32]) {
    let t = tok_pos as f32;
    for (o, &f) in out[..N_TOK_FREQS].iter_mut().zip(inv_tok) {
        *o = t * f;
    }
    out[N_TOK_FREQS] = rope_page as f32 * INV_PAGE;
}

/// Page2d cos/sin tables for rows given as `(tok_pos, rope_page)` pairs, in the
/// layout [`super::decoder::apply_rope`] consumes.
#[must_use]
pub fn page2d_rope_table(tok_pos: &[u32], rope_pages: &[u32]) -> RopeTable {
    assert_eq!(
        tok_pos.len(),
        rope_pages.len(),
        "page2d_rope_table: row count"
    );
    let inv_tok = inv_tok_freqs();
    let half = HEAD_DIM / 2;
    let mut angles = vec![0.0f32; tok_pos.len() * half];
    for ((row, &t), &p) in angles.chunks_exact_mut(half).zip(tok_pos).zip(rope_pages) {
        row_angles(&inv_tok, t, p, row);
    }
    RopeTable::from_angles(&angles, HEAD_DIM)
}

/// Prefill-time page coordinates of a token sequence (SB:2433-2456 for the page
/// ids, SB:2511-2568 for buckets and positions).
///
/// Built over a prompt, or over a prompt plus teacher-forced generated tokens:
/// rows after `last_img` follow the response rule either way. With more than `N`
/// generated `<PAGE>` markers the bucket is not clamped at `N` here (SB:2555),
/// unlike [`DecodePageState`] (DISC-U2-7).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PageCoords {
    /// Page bucket per row; 0 is the reserved no-page bucket.
    pub bucket: Vec<u32>,
    /// Running 0-based count of earlier rows in the same bucket.
    pub tok_pos: Vec<u32>,
    /// 0-based page of each `<image>` row, −1 for every other row.
    pub img_page: Vec<i32>,
    /// Query page per row (`_PAGE_SCOPE_PREFILL = True`, MD:333): `cum − 1` up
    /// to `last_img`, `cum − 1 − N` after it, and −1 wherever that is negative.
    pub query_page: Vec<i32>,
    /// Prefill (`≤ last_img`) rows per bucket; its length is the bucket count.
    pub pf_cnt: Vec<u32>,
    /// `N`: `<PAGE>` markers up to the last image.
    pub prompt_pages: u32,
    /// `<PAGE>` markers in the whole sequence (MU:512's `mcum` seed).
    pub markers: u32,
    /// Index of the last `<image>` row.
    pub last_img: usize,
}

impl PageCoords {
    /// Compute the coordinates of `ids`.
    ///
    /// # Errors
    /// [`FocrError::Other`] when `ids` has no `<image>` row or no `<PAGE>`
    /// marker before its last image: v2 has no 1-D RoPE fallback (OQ-V2-6).
    pub fn build(ids: &[u32]) -> FocrResult<Self> {
        let Some(last_img) = ids.iter().rposition(|&t| t == special::IMAGE) else {
            return Err(no_fallback("the sequence has no <image> token"));
        };
        let mut cum = Vec::with_capacity(ids.len());
        let mut c = 0u32;
        for &t in ids {
            c += u32::from(t == PAGE_TOKEN_ID);
            cum.push(c);
        }
        let n = cum[last_img];
        if n == 0 {
            return Err(no_fallback("no <PAGE> marker precedes the last <image>"));
        }
        let mut bucket = Vec::with_capacity(ids.len());
        let mut query_page = Vec::with_capacity(ids.len());
        let mut img_page = Vec::with_capacity(ids.len());
        for (i, (&t, &c)) in ids.iter().zip(&cum).enumerate() {
            let c = i64::from(c);
            let n = i64::from(n);
            let in_prefill = i <= last_img;
            let b = if in_prefill { c } else { (c - n).max(0) };
            bucket.push(b as u32);
            let q = if in_prefill { c - 1 } else { c - 1 - n };
            query_page.push(if q < 0 { -1 } else { q as i32 });
            img_page.push(if t == special::IMAGE {
                (c - 1) as i32
            } else {
                -1
            });
        }
        let n_buckets = bucket.iter().copied().max().unwrap_or(0) as usize + 1;
        let mut running = vec![0u32; n_buckets];
        let mut pf_cnt = vec![0u32; n_buckets];
        let mut tok_pos = Vec::with_capacity(ids.len());
        for (i, &b) in bucket.iter().enumerate() {
            let b = b as usize;
            tok_pos.push(running[b]);
            running[b] += 1;
            if i <= last_img {
                pf_cnt[b] += 1;
            }
        }
        Ok(Self {
            bucket,
            tok_pos,
            img_page,
            query_page,
            pf_cnt,
            prompt_pages: n,
            markers: c,
            last_img,
        })
    }

    /// Rope page coordinate per row for a window at `page_base`
    /// ([`rope_page`]).
    #[must_use]
    pub fn rope_pages(&self, page_base: u32) -> Vec<u32> {
        self.bucket
            .iter()
            .map(|&b| rope_page(b, page_base))
            .collect()
    }

    /// The page2d cos/sin tables for every row.
    #[must_use]
    pub fn rope_table(&self, page_base: u32) -> RopeTable {
        page2d_rope_table(&self.tok_pos, &self.rope_pages(page_base))
    }
}

fn no_fallback(why: &str) -> FocrError {
    FocrError::Other(anyhow::anyhow!(
        "unlimited-ocr-2 page2d RoPE needs a <PAGE><image> prompt and has no 1-D \
         fallback (OQ-V2-6): {why}"
    ))
}

/// The coordinates of one decoded row.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DecodeCoord {
    /// Page bucket (0 = no page).
    pub bucket: u32,
    /// Running position inside the bucket.
    pub tok_pos: u32,
    /// Page-axis RoPE coordinate, `page_base` applied and folded mod 2.
    pub rope_page: u32,
    /// Query page for the Page Gate (MU:516-518), −1 when negative.
    pub query_page: i32,
}

/// Per-sequence decode-time page state (SB:1451-1489 for RoPE, MU:516-518
/// for the query page). Each fed token advances it once, and the coordinates
/// it returns are that token's.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecodePageState {
    bucket: u32,
    tok_pos: u32,
    pf_cnt: Vec<u32>,
    /// Buckets `0..n_buckets`; a generated `<PAGE>` clamps at the last one.
    n_buckets: u32,
    /// Markers seen, for the query page (`mcum`).
    markers: u32,
    /// `pp`, the prompt pages the query page is relative to.
    prompt_pages: u32,
    page_base: u32,
}

impl DecodePageState {
    /// The HF seed (SB:1467-1477, MU:512): continue from the last prompt row,
    /// whose bucket is the instruction's 0. Applies to unwindowed runs,
    /// window 0 and recomputed windows.
    #[must_use]
    pub fn after_prompt(prompt: &PageCoords, page_base: u32) -> Self {
        let last = prompt.bucket.len() - 1;
        Self {
            bucket: prompt.bucket[last],
            tok_pos: prompt.tok_pos[last],
            pf_cnt: prompt.pf_cnt.clone(),
            n_buckets: prompt.pf_cnt.len() as u32,
            markers: prompt.markers,
            prompt_pages: prompt.prompt_pages,
            page_base,
        }
    }

    /// The seed of a reused window (W/`session/uocr_doc_reuse.py:418-437,
    /// 1103-1109`). Its rewritten prompt has no BOS and no instruction: bucket
    /// 1 is the copied tail page and buckets 2..=M the newly prefilled pages.
    /// The HF seed would leave the state in bucket M and send the forced
    /// `<PAGE>` there; this one resumes in bucket 1, so the forced `<PAGE>` lands
    /// in bucket 2 with `tok_pos = pf_cnt[2]` and query page 1.
    ///
    /// # Errors
    /// [`FocrError::Other`] if the rewritten prompt does not end on its last
    /// image or has fewer than two pages.
    pub fn reused_window(rewritten: &PageCoords, page_base: u32) -> FocrResult<Self> {
        let m = rewritten.prompt_pages;
        if rewritten.last_img + 1 != rewritten.bucket.len() || m < 2 || rewritten.markers != m {
            return Err(FocrError::Other(anyhow::anyhow!(
                "reused-window seed: the rewritten prompt must be pages only (tail + \
                 >= 1 new page) ending on its last image; got {m} pages, {} markers, \
                 last image at {} of {} rows",
                rewritten.markers,
                rewritten.last_img,
                rewritten.bucket.len()
            )));
        }
        Ok(Self {
            bucket: 1,
            // A non-marker row would continue bucket 1's running count, as the
            // wheel's per-bucket `cnt` does.
            tok_pos: rewritten.pf_cnt[1] - 1,
            pf_cnt: rewritten.pf_cnt.clone(),
            n_buckets: m + 1,
            markers: m + 1,
            prompt_pages: m,
            page_base,
        })
    }

    /// The window's page base ([`rope_page`]).
    #[must_use]
    pub fn page_base(&self) -> u32 {
        self.page_base
    }

    /// Advance over the fed token `id` and return its coordinates. An extra
    /// `<PAGE>` beyond the last bucket clamps there with `tok_pos = pf_cnt[last]`
    /// (HF; SGLang keeps the running count instead, DISC-U2-7).
    pub fn feed(&mut self, id: u32) -> DecodeCoord {
        if id == PAGE_TOKEN_ID {
            self.bucket = (self.bucket + 1).min(self.n_buckets - 1);
            self.tok_pos = self.pf_cnt[self.bucket as usize];
            self.markers += 1;
        } else {
            self.tok_pos += 1;
        }
        let q = i64::from(self.markers) - 1 - i64::from(self.prompt_pages);
        DecodeCoord {
            bucket: self.bucket,
            tok_pos: self.tok_pos,
            rope_page: rope_page(self.bucket, self.page_base),
            query_page: if q < 0 { -1 } else { q as i32 },
        }
    }
}

// ── Token Sieve router (U2-B2) ──────────────────────────────────────────────
//
// Constants are the checkpoint snapshot of `sparse_attention_config`, which is
// what inference reads (MD:56-64, 296-329): `_persist_router_cfg` rewrites the
// live config at construction. `DASH_STAGE0` must be unset in the oracle.

/// Rows per router block (`sparse_block_size`).
pub const BLOCK_SIZE: usize = 32;

/// Blocks in the local window (`sparse_local_block`, 128 rows). Block `c` is
/// remote for a query in block `qb` when `c ≤ qb − LOCAL_BLOCKS`.
pub const LOCAL_BLOCKS: usize = 4;

/// `_SCORE_NEG` (DA:741): the score of a block that is not remote.
pub const SCORE_NEG: f32 = -1.0e9;

/// `_ENTMAX_NEG` (MD:65): the entmax input of a non-candidate block.
pub const ENTMAX_NEG: f32 = -1.0e4;

/// `_ACT_CLAMP` (DA:746).
const ACT_CLAMP: f32 = 1.0e4;

/// `rms_norm_eps` of the router's q/k norms (`config.json`).
const ROUTER_NORM_EPS: f32 = 1e-6;

/// `inv_sqrt_d = 1.0 / math.sqrt(128)` (MD:387). The reference multiplies by
/// it (never divides by √128), and torch rounds a Python float to f32 when it
/// scales an f32 tensor, so this is that f32.
#[must_use]
pub fn inv_sqrt_d() -> f32 {
    (1.0 / (HEAD_DIM as f64).sqrt()) as f32
}

fn finite_or_zero(v: f32) -> f32 {
    if v.is_finite() { v } else { 0.0 }
}

/// Per-layer routing parameters (`router._infer_params`, MD:320-328).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RouterParams {
    /// `dash_scale_per_layer[l] / √128`, the entmax input scale (as f32).
    pub idx_scale: f32,
    /// `dash_floor_per_layer[l]`: priors are floored here before the log.
    pub floor: f32,
    /// `1 / dash_sigma`.
    pub inv_sigma: f32,
    /// K, the read budget in blocks (`dash_topk_blocks`).
    pub budget: usize,
    /// The entmax window before the block count caps it (`DASH_INFER_TOPM`).
    pub topm: usize,
}

impl RouterParams {
    /// Parameters from the config values, rounded the way the reference
    /// rounds them: each is a Python float that torch casts to f32 when it
    /// meets an f32 tensor.
    #[must_use]
    pub fn new(scale: f64, floor: f64, sigma: f64, budget: usize, topm: usize) -> Self {
        Self {
            idx_scale: (scale / (HEAD_DIM as f64).sqrt()) as f32,
            floor: floor as f32,
            inv_sigma: (1.0 / sigma) as f32,
            budget,
            topm,
        }
    }

    /// Unlimited-OCR-2's snapshot, the same for all 12 layers: scale 128,
    /// floor 1e-4, σ 1, K 12, M 16.
    #[must_use]
    pub fn uocr2() -> Self {
        Self::new(128.0, 1e-4, 1.0, 12, 16)
    }

    /// M, the entmax window of a forward with `nblk` blocks (MD:352). It
    /// depends on the forward's block count, never on the query row.
    #[must_use]
    pub fn window(&self, nblk: usize) -> usize {
        if self.topm > 0 {
            self.topm.min(nblk).max(1)
        } else {
            nblk
        }
    }

    /// K', the read slots of a window of `window` blocks (MD:356).
    #[must_use]
    pub fn read_slots(&self, window: usize) -> usize {
        self.budget.min(window).max(1)
    }
}

/// One layer's router weights (`self_attn.dash_router.*`), widened to f32
/// once. The `self_attn.indexer.*` tensors are not used at inference
/// (OQ-V2-5) and are not loaded here.
#[derive(Debug, Clone, PartialEq)]
pub struct SrSwaLayerWeights {
    /// `q_proj.weight`, `[HEAD_DIM, hidden]` row-major (no bias).
    q_proj: Vec<f32>,
    /// `k_proj.weight`, `[HEAD_DIM, hidden]` row-major (no bias).
    k_proj: Vec<f32>,
    /// `q_proj` and `k_proj` transposed once to `[hidden, HEAD_DIM]`, the
    /// layout [`project`]'s matmul contracts over (U2-B5: a decode step
    /// would otherwise transpose both per layer).
    q_proj_t: Mat,
    k_proj_t: Mat,
    /// `q_norm.weight` (Gemma style: the scale is `1 + w`).
    pub q_norm: Vec<f32>,
    /// `k_norm.weight`.
    pub k_norm: Vec<f32>,
    /// `head_cls`, the learned block-summary query.
    pub head_cls: Vec<f32>,
    /// The decoder hidden size the projections read (1280).
    pub hidden: usize,
}

impl SrSwaLayerWeights {
    /// Shape-checked construction.
    ///
    /// # Errors
    /// [`FocrError::Other`] if a projection is not `HEAD_DIM × hidden` or a
    /// vector is not `HEAD_DIM` long.
    pub fn new(
        hidden: usize,
        q_proj: Vec<f32>,
        k_proj: Vec<f32>,
        q_norm: Vec<f32>,
        k_norm: Vec<f32>,
        head_cls: Vec<f32>,
    ) -> FocrResult<Self> {
        let proj = HEAD_DIM.checked_mul(hidden);
        for (name, len) in [("q_proj", q_proj.len()), ("k_proj", k_proj.len())] {
            if Some(len) != proj {
                return Err(router_shape(name, len, &format!("{HEAD_DIM} x {hidden}")));
            }
        }
        for (name, len) in [
            ("q_norm", q_norm.len()),
            ("k_norm", k_norm.len()),
            ("head_cls", head_cls.len()),
        ] {
            if len != HEAD_DIM {
                return Err(router_shape(name, len, &HEAD_DIM.to_string()));
            }
        }
        Ok(Self {
            q_proj_t: transposed(&q_proj, HEAD_DIM, hidden),
            k_proj_t: transposed(&k_proj, HEAD_DIM, hidden),
            q_proj,
            k_proj,
            q_norm,
            k_norm,
            head_cls,
            hidden,
        })
    }

    /// Layer `layer`'s `self_attn.dash_router.*` from a checkpoint, widened
    /// to f32 once (U2-B5).
    ///
    /// # Errors
    /// [`FocrError::FormatMismatch`] if a tensor is absent, [`FocrError::Other`]
    /// on a shape mismatch.
    pub fn load(weights: &Weights, layer: usize) -> FocrResult<Self> {
        let p = format!("model.layers.{layer}.self_attn.dash_router");
        Self::new(
            decoder_config::HIDDEN_SIZE,
            weights.mat(&format!("{p}.q_proj.weight"))?.data,
            weights.mat(&format!("{p}.k_proj.weight"))?.data,
            weights.vec(&format!("{p}.q_norm.weight"))?,
            weights.vec(&format!("{p}.k_norm.weight"))?,
            weights.vec(&format!("{p}.head_cls"))?,
        )
    }

    /// Every layer's router weights ([`Self::load`]).
    ///
    /// # Errors
    /// As [`Self::load`].
    pub fn load_all(weights: &Weights) -> FocrResult<Vec<Self>> {
        (0..decoder_config::NUM_HIDDEN_LAYERS)
            .map(|layer| Self::load(weights, layer))
            .collect()
    }
}

/// `w` (`[rows, cols]` row-major) as a `[cols, rows]` matrix: the transpose
/// `decoder::linear_no_bias` builds per call, so a matmul against it is
/// bit-identical to that linear.
fn transposed(w: &[f32], rows: usize, cols: usize) -> Mat {
    let mut t = vec![0.0f32; w.len()];
    for i in 0..cols {
        for (o, slot) in t[i * rows..(i + 1) * rows].iter_mut().enumerate() {
            *slot = w[o * cols + i];
        }
    }
    Mat::from_vec(cols, rows, t)
}

fn router_shape(name: &str, len: usize, want: &str) -> FocrError {
    FocrError::Other(anyhow::anyhow!(
        "dash_router.{name}: {len} values, want {want}"
    ))
}

/// The router's Gemma-style RMSNorm over one row, in place (DA:844-855):
/// non-finite inputs become 0, `y = x·rsqrt(mean(x²) + 1e-6)` is clamped to
/// ±1e4, then `y·(1 + w)` is clamped to ±1e4 again.
pub fn gemma_rms_norm(x: &mut [f32], weight: &[f32]) {
    assert_eq!(x.len(), weight.len(), "gemma_rms_norm: weight length");
    for v in x.iter_mut() {
        *v = finite_or_zero(*v);
    }
    let mean = x.iter().map(|v| v * v).sum::<f32>() / x.len() as f32;
    let rsqrt = 1.0 / (mean + ROUTER_NORM_EPS).sqrt();
    for (v, &w) in x.iter_mut().zip(weight) {
        let y = (*v * rsqrt).clamp(-ACT_CLAMP, ACT_CLAMP);
        *v = (y * (1.0 + w)).clamp(-ACT_CLAMP, ACT_CLAMP);
    }
}

/// The router's index queries and keys for the rows of `h` (MD:365-377).
///
/// `h` (`[rows, hidden]`) is the `input_layernorm` output that also feeds the
/// main q/k/v (SB:3692-3697); `rope` holds those rows' page2d tables (the
/// router rotates with the main attention's coordinates and frequencies,
/// MD:126-143). Non-finite values become 0 before the projections and after
/// the rotation. Returns `(iq, ik)`, each `[rows, HEAD_DIM]`; the index-key
/// cache holds `ik` as returned (post-norm, post-RoPE, f32; MD:440-444).
///
/// # Errors
/// [`FocrError::Other`] on a shape mismatch.
pub fn project(h: &Mat, w: &SrSwaLayerWeights, rope: &RopeTable) -> FocrResult<(Mat, Mat)> {
    let mut hs = h.clone();
    for v in &mut hs.data {
        *v = finite_or_zero(*v);
    }
    let mut iq = nn::matmul(&hs, &w.q_proj_t)?;
    let mut ik = nn::matmul(&hs, &w.k_proj_t)?;
    for r in 0..iq.rows {
        gemma_rms_norm(iq.row_mut(r), &w.q_norm);
        gemma_rms_norm(ik.row_mut(r), &w.k_norm);
    }
    apply_rope(&mut iq, rope)?;
    apply_rope(&mut ik, rope)?;
    for v in iq.data.iter_mut().chain(ik.data.iter_mut()) {
        *v = finite_or_zero(*v);
    }
    Ok((iq, ik))
}

/// Per-block summary keys over a KV sequence's index keys (stage0 =
/// `head_cls`, MD:391-408).
#[derive(Debug, Clone, PartialEq)]
pub struct BlockSummaries {
    /// `k̄`, `[nblk, HEAD_DIM]` row-major.
    pub kbar: Vec<f32>,
    /// Whether each block holds a real row (`chunk_valid`). Only the partial
    /// tail block can be short, and it is never remote.
    pub valid: Vec<bool>,
}

impl BlockSummaries {
    /// Number of blocks.
    #[must_use]
    pub fn nblk(&self) -> usize {
        self.valid.len()
    }

    /// Block `c`'s summary key.
    #[must_use]
    pub fn kbar(&self, c: usize) -> &[f32] {
        &self.kbar[c * HEAD_DIM..(c + 1) * HEAD_DIM]
    }
}

/// Summaries of the index keys `ik` (`[rows, HEAD_DIM]`, KV order) in
/// `ceil(rows / 32)` blocks (MD:391-408). For block `c` and its rows `j`:
/// `a_j = clamp(ik_j·head_cls × inv_sqrt_d, ±1e4)`, `w_j = exp(a_j − max a) /
/// max(Σ exp(a − max a), 1e-6)`, and `k̄_c = Σ_j w_j·ik_j` with non-finite
/// values mapped to 0. A zero `head_cls` is a mean pool.
#[must_use]
pub fn summaries(ik: &Mat, head_cls: &[f32]) -> BlockSummaries {
    assert_eq!(ik.cols, HEAD_DIM, "summaries: index key width");
    assert_eq!(head_cls.len(), HEAD_DIM, "summaries: head_cls length");
    let nblk = ik.rows.div_ceil(BLOCK_SIZE);
    let mut kbar = vec![0.0f32; nblk * HEAD_DIM];
    let mut valid = vec![false; nblk];
    for (c, out) in kbar.as_chunks_mut::<HEAD_DIM>().0.iter_mut().enumerate() {
        let rows = c * BLOCK_SIZE..((c + 1) * BLOCK_SIZE).min(ik.rows);
        valid[c] = !rows.is_empty();
        block_summary(
            &ik.data[rows.start * HEAD_DIM..rows.end * HEAD_DIM],
            head_cls,
            out,
        );
    }
    BlockSummaries { kbar, valid }
}

/// One block's `k̄` from its index keys `ik` (`[n, HEAD_DIM]`, n ≤ 32), as
/// [`summaries`] defines it. The cache (U2-B4) calls this too, so its
/// incremental summaries are this computation by construction.
fn block_summary(ik: &[f32], head_cls: &[f32], out: &mut [f32; HEAD_DIM]) {
    let inv = inv_sqrt_d();
    let keys = ik.as_chunks::<HEAD_DIM>().0;
    let aw: Vec<f32> = keys
        .iter()
        .map(|k| (dot(k, head_cls) * inv).clamp(-ACT_CLAMP, ACT_CLAMP))
        .collect();
    let max = aw.iter().copied().fold(f32::NEG_INFINITY, f32::max);
    let e: Vec<f32> = aw.iter().map(|&a| (a - max).exp()).collect();
    let denom = e.iter().sum::<f32>().max(1e-6);
    out.fill(0.0);
    for (k, &ej) in keys.iter().zip(&e) {
        let wj = ej / denom;
        for (o, &x) in out.iter_mut().zip(k) {
            *o += wj * x;
        }
    }
    for o in out.iter_mut() {
        *o = finite_or_zero(*o);
    }
}

fn dot(a: &[f32], b: &[f32]) -> f32 {
    a.iter().zip(b).map(|(x, y)| x * y).sum()
}

/// Remote block scores of the query `iq` at KV position `q_pos` (MD:409-417):
/// `clamp(iq·k̄_c × inv_sqrt_d, ±1e4)` for every valid block `c ≤ q_pos/32 −
/// 4`, [`SCORE_NEG`] for the rest (the local window, the future, and invalid
/// blocks). `q_pos` is window-local in windowed mode.
#[must_use]
pub fn scores(iq: &[f32], q_pos: usize, sums: &BlockSummaries) -> Vec<f32> {
    let inv = inv_sqrt_d();
    let qb = q_pos / BLOCK_SIZE;
    (0..sums.nblk())
        .map(|c| {
            if c + LOCAL_BLOCKS <= qb && sums.valid[c] {
                (dot(iq, sums.kbar(c)) * inv).clamp(-ACT_CLAMP, ACT_CLAMP)
            } else {
                SCORE_NEG
            }
        })
        .collect()
}

/// The image pages each block of a KV sequence holds, from its per-row page
/// ids (`PageCoords::img_page`: −1 for non-image rows).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockPages {
    /// Distinct image pages per block, ascending.
    pages: Vec<Vec<i32>>,
    /// Lowest image page per block, −1 when it holds none (MU:510-511).
    pub bmin: Vec<i32>,
    /// Highest image page per block, −1 when it holds none (MU:509).
    pub bmax: Vec<i32>,
}

impl BlockPages {
    /// Group `img_page` into `ceil(len / 32)` blocks.
    #[must_use]
    pub fn build(img_page: &[i32]) -> Self {
        let mut pages: Vec<Vec<i32>> = img_page
            .chunks(BLOCK_SIZE)
            .map(|blk| blk.iter().copied().filter(|&p| p >= 0).collect())
            .collect();
        for p in &mut pages {
            p.sort_unstable();
            p.dedup();
        }
        let bmin = pages
            .iter()
            .map(|p| p.first().copied().unwrap_or(-1))
            .collect();
        let bmax = pages
            .iter()
            .map(|p| p.last().copied().unwrap_or(-1))
            .collect();
        Self { pages, bmin, bmax }
    }

    /// Number of blocks.
    #[must_use]
    pub fn nblk(&self) -> usize {
        self.pages.len()
    }
}

/// The prefill Page Gate of a query on page `qp` (`image_bucket_constraint =
/// true`; SB:2629-2649, 2706-2737): the blocks holding an image row of page
/// `qp`. A query with no such block, or with `qp < 0` (BOS, the instruction),
/// may read every block (the per-query fallback).
#[must_use]
pub fn page_gate_prefill(blocks: &BlockPages, qp: i32) -> Vec<bool> {
    let allow: Vec<bool> = blocks
        .pages
        .iter()
        .map(|p| qp >= 0 && p.binary_search(&qp).is_ok())
        .collect();
    if allow.contains(&true) {
        allow
    } else {
        vec![true; allow.len()]
    }
}

/// The decode Page Gate of a query on page `qp` (MU:514-528): block `c` of
/// the prompt is allowed when `bmin[c] ≥ 0` and `bmin[c] ≤ qp ≤ bmax[c]`;
/// blocks past the prompt's (generated text) never are. An open row (`qp <
/// 0`, or no block allowed) allows all `nblk` blocks.
#[must_use]
pub fn page_gate_decode(bmin: &[i32], bmax: &[i32], qp: i32, nblk: usize) -> Vec<bool> {
    assert_eq!(bmin.len(), bmax.len(), "page_gate_decode: bmin/bmax");
    let mut allow = vec![false; nblk];
    for (c, a) in allow.iter_mut().enumerate().take(bmin.len()) {
        *a = qp >= 0 && bmin[c] >= 0 && bmin[c] <= qp && qp <= bmax[c];
    }
    if allow.contains(&true) {
        allow
    } else {
        vec![true; nblk]
    }
}

/// The candidate blocks of one query (MD:346-348): remote blocks (score >
/// −5e8) that `allow` admits; when that leaves none, every remote block (an
/// open row). With no remote block the set is empty and the query reads only
/// its local window.
#[must_use]
pub fn candidates(scores: &[f32], allow: Option<&[bool]>) -> Vec<bool> {
    let remote: Vec<bool> = scores.iter().map(|&s| s > SCORE_NEG * 0.5).collect();
    let Some(allow) = allow else {
        return remote;
    };
    assert_eq!(allow.len(), scores.len(), "candidates: allow length");
    let cand: Vec<bool> = remote.iter().zip(allow).map(|(&r, &a)| r && a).collect();
    if cand.contains(&true) { cand } else { remote }
}

/// The entmax input of every block (MD:349-350): `score × idx_scale` for a
/// candidate, [`ENTMAX_NEG`] otherwise, and [`ENTMAX_NEG`] for anything
/// non-finite.
#[must_use]
pub fn entmax_inputs(scores: &[f32], cand: &[bool], p: &RouterParams) -> Vec<f32> {
    scores
        .iter()
        .zip(cand)
        .map(|(&s, &c)| {
            let x = if c { s * p.idx_scale } else { ENTMAX_NEG };
            if x.is_finite() { x } else { ENTMAX_NEG }
        })
        .collect()
}

/// 1.5-entmax over `x` (DA:772-800) in the reference's operation order:
/// `z = (x − max x)/2`, sorted descending; prefix means `mean_k` and `meansq_k`;
/// `τ_k = mean_k − √max((1 − k(meansq_k − mean_k²))/k, 0)`; the support is
/// `#{k : τ_k ≤ z_(k)}` (at least 1) and `p = max(z − τ_support, 0)²`.
///
/// The prefix sums accumulate in f64 and round to f32 per step: torch's CPU
/// `cumsum` does that for f32 (`acc_type<float, CPU>` is double), and the
/// oracle is CPU. Returns the probabilities in input order and the support.
#[must_use]
pub fn entmax15(x: &[f32]) -> (Vec<f32>, usize) {
    if x.is_empty() {
        return (Vec::new(), 0);
    }
    let max = x.iter().copied().fold(f32::NEG_INFINITY, f32::max);
    let z: Vec<f32> = x.iter().map(|&v| (v - max) / 2.0).collect();
    let mut sorted = z.clone();
    sorted.sort_unstable_by(|a, b| b.total_cmp(a));
    let (mut sum, mut sum_sq) = (0.0f64, 0.0f64);
    let mut support = 0usize;
    let mut taus = Vec::with_capacity(sorted.len());
    for (k, &zk) in sorted.iter().enumerate() {
        sum += f64::from(zk);
        sum_sq += f64::from(zk * zk);
        let rho = (k + 1) as f32;
        let mean = sum as f32 / rho;
        let mean_sq = sum_sq as f32 / rho;
        let ss = rho * (mean_sq - mean * mean);
        let delta = (1.0 - ss) / rho;
        let tau = mean - delta.max(0.0).sqrt();
        support += usize::from(tau <= zk);
        taus.push(tau);
    }
    let support = support.max(1);
    let tau = taus[support - 1];
    let p = z
        .iter()
        .map(|&v| {
            let d = (v - tau).max(0.0);
            d * d
        })
        .collect();
    (p, support)
}

/// Which entmax the router runs (D21).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntmaxDomain {
    /// HF (MD:346-362), the parity default: entmax over the top-M window of
    /// all blocks, then the candidates among its first K' slots.
    TopM,
    /// SGLang (W/`layers/attention/unlimited_dash_ops/route_triton.py`):
    /// entmax over every candidate and none of the rest; read the K best
    /// candidates by score. SGLang solves Ï„ by a 24-step bisection and the
    /// read set by a 40-step one, so a tie at K-th place can read fewer than
    /// K blocks there; this is the exact sort-based form (DISC-U2-1).
    AllCandidates,
}

/// Selects the router's [`EntmaxDomain`]: `topm` (the default) or `all`.
pub const ENTMAX_ENV: &str = "FOCR_SRSWA_ENTMAX";

/// The [`EntmaxDomain`] an [`ENTMAX_ENV`] value names: unset, empty or
/// `topm` is [`EntmaxDomain::TopM`], `all` is [`EntmaxDomain::AllCandidates`]
/// (case and surrounding space ignored).
///
/// # Errors
/// Any other value, with a message naming the accepted ones.
pub fn entmax_domain_for(value: Option<&str>) -> Result<EntmaxDomain, String> {
    match value.map(|v| v.trim().to_ascii_lowercase()).as_deref() {
        None | Some("" | "topm") => Ok(EntmaxDomain::TopM),
        Some("all") => Ok(EntmaxDomain::AllCandidates),
        Some(other) => Err(format!(
            "{ENTMAX_ENV}={other:?} is not an entmax domain; use `topm` (the default) or `all`"
        )),
    }
}

/// [`ENTMAX_ENV`], read once per process.
pub fn entmax_domain() -> &'static Result<EntmaxDomain, String> {
    use std::sync::OnceLock;
    static DOMAIN: OnceLock<Result<EntmaxDomain, String>> = OnceLock::new();
    DOMAIN.get_or_init(|| entmax_domain_for(std::env::var(ENTMAX_ENV).ok().as_deref()))
}

/// One query's read set.
#[derive(Debug, Clone, PartialEq)]
pub struct Route {
    /// Selected remote blocks, ascending.
    pub blocks: Vec<u32>,
    /// The bias added to every logit of the block at the same position.
    pub bias: Vec<f32>,
    /// The entmax support size (a routing-margin diagnostic, U2-C1).
    pub support: usize,
}

/// The top-M window of the entmax inputs (MD:353-354).
#[derive(Debug, Clone, PartialEq)]
pub struct TopM {
    /// `(entmax input, block)` in slot order: descending, and among equal
    /// inputs the lower block first.
    pub slots: Vec<(f32, u32)>,
    /// The entmax prior of each slot.
    pub prior: Vec<f32>,
    /// The entmax support size.
    pub support: usize,
}

/// Slot order: descending input, ties by lower block index. torch's CPU
/// `topk` leaves the order of equal values to `std::nth_element` /
/// `std::partial_sort`, which is not a stable order, so no rule reproduces it
/// in general; ties matter only between equal candidate scores at the K'-th
/// slot or a candidate at exactly −1e4, and C1 classes such flips.
fn slot_order(a: &(f32, u32), b: &(f32, u32)) -> std::cmp::Ordering {
    b.0.total_cmp(&a.0).then(a.1.cmp(&b.1))
}

/// The top-`window` entmax window of `x` and its prior (MD:351-354).
#[must_use]
pub fn entmax15_topm(x: &[f32], window: usize) -> TopM {
    let mut all: Vec<(f32, u32)> = x.iter().zip(0u32..).map(|(&v, c)| (v, c)).collect();
    let m = window.min(all.len());
    if m < all.len() && m > 0 {
        all.select_nth_unstable_by(m - 1, slot_order);
    }
    all.truncate(m);
    all.sort_unstable_by(slot_order);
    window_prior(all)
}

fn window_prior(slots: Vec<(f32, u32)>) -> TopM {
    let xv: Vec<f32> = slots.iter().map(|&(v, _)| v).collect();
    let (prior, support) = entmax15(&xv);
    TopM {
        slots,
        prior,
        support,
    }
}

/// The read set and bias of a [`TopM`] window (MD:355-362): the candidates
/// among its first K' slots, each with `bias = (ln max(p, floor) − μ)/σ`, μ
/// being the mean of those log-priors. A zero-prior candidate in those slots
/// is still read, with bias `ln(floor) − μ`. No clamp at inference.
#[must_use]
pub fn select_bias(top: &TopM, cand: &[bool], p: &RouterParams) -> Route {
    let k = p.read_slots(top.slots.len()).min(top.slots.len());
    let picked: Vec<(u32, f32)> = top.slots[..k]
        .iter()
        .zip(&top.prior)
        .filter(|((_, c), _)| cand[*c as usize])
        .map(|(&(_, c), &pr)| (c, pr))
        .collect();
    finish_route(picked, top.support, p)
}

/// Center the floored log-priors of the read set and sort it by block.
fn finish_route(mut picked: Vec<(u32, f32)>, support: usize, p: &RouterParams) -> Route {
    let logp: Vec<f32> = picked.iter().map(|&(_, pr)| pr.max(p.floor).ln()).collect();
    let mu = logp.iter().sum::<f32>() / logp.len().max(1) as f32;
    for ((_, b), &lp) in picked.iter_mut().zip(&logp) {
        *b = (lp - mu) * p.inv_sigma;
    }
    picked.sort_unstable_by_key(|&(c, _)| c);
    Route {
        blocks: picked.iter().map(|&(c, _)| c).collect(),
        bias: picked.iter().map(|&(_, b)| b).collect(),
        support,
    }
}

/// [`EntmaxDomain::AllCandidates`]: entmax over the candidates' inputs only,
/// then the `min(K, 16)` candidates with the highest input (all of them when
/// there are no more), μ taken over that read set. Zero-prior candidates in
/// it are read, with bias `ln(floor) − μ`.
#[must_use]
pub fn entmax15_all(x: &[f32], cand: &[bool], p: &RouterParams) -> Route {
    let mut slots: Vec<(f32, u32)> = x
        .iter()
        .zip(cand)
        .zip(0u32..)
        .filter(|&((_, &c), _)| c)
        .map(|((&v, _), b)| (v, b))
        .collect();
    slots.sort_unstable_by(slot_order);
    let xv: Vec<f32> = slots.iter().map(|&(v, _)| v).collect();
    let (prior, support) = entmax15(&xv);
    let k = p.read_slots(p.topm.max(1)).min(slots.len());
    let picked = slots[..k]
        .iter()
        .zip(&prior)
        .map(|(&(_, c), &pr)| (c, pr))
        .collect();
    finish_route(picked, support, p)
}

/// Route one query from its block scores and Page Gate (MD:338-362).
#[must_use]
pub fn route(
    scores: &[f32],
    allow: Option<&[bool]>,
    p: &RouterParams,
    domain: EntmaxDomain,
) -> Route {
    let cand = candidates(scores, allow);
    let x = entmax_inputs(scores, &cand, p);
    match domain {
        EntmaxDomain::TopM => {
            let top = entmax15_topm(&x, p.window(scores.len()));
            select_bias(&top, &cand, p)
        }
        EntmaxDomain::AllCandidates => entmax15_all(&x, &cand, p),
    }
}

/// [`EntmaxDomain::TopM`] from the candidates alone (U2-F2's lever): `cands`
/// holds each candidate's block and entmax input, `nblk` the forward's block
/// count. Every non-candidate's input is [`ENTMAX_NEG`], so the ones the full
/// window would hold are the `min(M, nblk − #cand)` lowest-indexed
/// non-candidates; they are merged into the descending order as pads, not
/// appended, because a candidate's input can sit below −1e4 (its score is
/// clamped to ±1e4 before the ×11.3 scale) and then ranks under them. Equal
/// to [`route`] over the full score row.
#[must_use]
pub fn route_topm_candidates(cands: &[(u32, f32)], nblk: usize, p: &RouterParams) -> Route {
    let mut is_cand = vec![false; nblk];
    for &(c, _) in cands {
        is_cand[c as usize] = true;
    }
    let m = p.window(nblk).min(nblk);
    let mut slots: Vec<(f32, u32)> = cands.iter().map(|&(c, v)| (v, c)).collect();
    slots.extend(
        (0u32..)
            .zip(&is_cand)
            .filter(|&(_, &c)| !c)
            .map(|(b, _)| (ENTMAX_NEG, b))
            .take(m),
    );
    slots.sort_unstable_by(slot_order);
    slots.truncate(m);
    select_bias(&window_prior(slots), &is_cand, p)
}

/// Route every row of a prefill from an empty cache (MD:433-446): row `r` sits
/// at KV position `r`, the Page Gate is [`page_gate_prefill`] on the row's
/// query page, and the forward's `nblk = ceil(S / 32)` sets the window M of
/// **every** row, early rows included (MD:437, 351-352). `iq`/`ik` come from
/// [`project`]; `coords` describes the same rows.
///
/// # Errors
/// [`FocrError::Other`] if the row counts disagree.
pub fn prefill_routes(
    iq: &Mat,
    ik: &Mat,
    coords: &PageCoords,
    head_cls: &[f32],
    p: &RouterParams,
    domain: EntmaxDomain,
) -> FocrResult<Vec<Route>> {
    if iq.rows != ik.rows || iq.rows != coords.bucket.len() {
        return Err(FocrError::Other(anyhow::anyhow!(
            "prefill_routes: {} query rows, {} key rows, {} coordinate rows",
            iq.rows,
            ik.rows,
            coords.bucket.len()
        )));
    }
    let sums = summaries(ik, head_cls);
    let blocks = BlockPages::build(&coords.img_page);
    Ok(prefill_rows_routes(
        iq, 0, &sums, &blocks, coords, p, domain,
    ))
}

/// The routes of prefill rows `start..start + iq.rows` (`iq` row `r` is KV
/// position `start + r`) over the block summaries `sums` and block pages
/// `blocks` of every row the forward holds, the earlier cached ones
/// included: the summaries' `nblk` sets the window M of every row.
fn prefill_rows_routes(
    iq: &Mat,
    start: usize,
    sums: &BlockSummaries,
    blocks: &BlockPages,
    coords: &PageCoords,
    p: &RouterParams,
    domain: EntmaxDomain,
) -> Vec<Route> {
    (0..iq.rows)
        .map(|r| {
            let sc = scores(iq.row(r), start + r, sums);
            let allow = page_gate_prefill(blocks, coords.query_page[start + r]);
            route(&sc, Some(&allow), p, domain)
        })
        .collect()
}

// ── Block-sparse biased attention (U2-B3) ───────────────────────────────────
//
// A query attends its 128-row local window without bias and the rows of its
// selected remote blocks with that block's bias (MD:454-481 decode,
// DA:1230-1261 dense prefill):
//
//   local(j)  = q − 128 < j ≤ q
//   remote(j) = j ≤ q ∧ block(j) ∈ B ∧ ¬local(j)      (local wins on overlap)
//   logit_j   = (q·k_j) × f32(1/√128) + (remote(j) ? β_block(j) : 0)
//   out       = softmax over local ∪ remote of logit, times v
//
// Every head shares the layer's selection (MD:480). franken_ocr runs it in f32
// against the f32 oracle (DISC-U2-5). Both paths below visit the attended keys
// in ascending position order and reduce in that order, so they agree bit for
// bit: the dense one only adds exact zeros for the masked keys.

/// Attention heads (`num_attention_heads` = `num_key_value_heads` = 10).
pub const NUM_HEADS: usize = 10;

/// The local window in rows (`local_window = sparse_local_block × block_size`).
pub const LOCAL_WINDOW: usize = LOCAL_BLOCKS * BLOCK_SIZE;

/// The remote blocks one query reads and the bias each adds to the logits of
/// its rows, shared by every head of the layer. A [`Route`] maps onto it one
/// to one.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct BlockSelection {
    blocks: Vec<u32>,
    bias: Vec<f32>,
}

impl BlockSelection {
    /// A selection of `blocks`, each with the bias at the same position.
    ///
    /// # Errors
    /// [`FocrError::Other`] unless `blocks` is strictly ascending and as long
    /// as `bias`.
    pub fn new(blocks: Vec<u32>, bias: Vec<f32>) -> FocrResult<Self> {
        if blocks.len() != bias.len() || !blocks.windows(2).all(|w| w[0] < w[1]) {
            return Err(FocrError::Other(anyhow::anyhow!(
                "block selection: {} blocks with {} biases; blocks must be strictly \
                 ascending: {blocks:?}",
                blocks.len(),
                bias.len()
            )));
        }
        Ok(Self { blocks, bias })
    }

    /// Selected blocks, ascending.
    #[must_use]
    pub fn blocks(&self) -> &[u32] {
        &self.blocks
    }

    /// The bias of each selected block.
    #[must_use]
    pub fn bias(&self) -> &[f32] {
        &self.bias
    }
}

impl From<Route> for BlockSelection {
    /// A route's blocks are ascending and unique by construction.
    fn from(route: Route) -> Self {
        Self {
            blocks: route.blocks,
            bias: route.bias,
        }
    }
}

/// Read access to one layer's keys and values, block by block. The gathered
/// path reads only through it; U2-B4's cache implements it as well.
pub trait BlockKv {
    /// Rows in the sequence; positions index `0..rows()`.
    fn rows(&self) -> usize;

    /// Head `h`'s keys in block `b`: contiguous `[n, HEAD_DIM]` for the
    /// block's `n` live rows.
    fn keys(&self, h: usize, b: usize) -> &[f32];

    /// Head `h`'s values in block `b`, laid out like [`BlockKv::keys`].
    fn values(&self, h: usize, b: usize) -> &[f32];
}

/// An in-memory head-major [`BlockKv`]: head `h`'s keys are one `[rows,
/// HEAD_DIM]` run.
#[derive(Debug, Clone, PartialEq)]
pub struct HeadMajorKv {
    rows: usize,
    k: Vec<Vec<f32>>,
    v: Vec<Vec<f32>>,
}

impl HeadMajorKv {
    /// From per-row K and V, each `[rows, NUM_HEADS × HEAD_DIM]` (the
    /// projection layout, RoPE already applied to K).
    ///
    /// # Errors
    /// [`FocrError::Other`] if the shapes disagree or are not that width.
    pub fn from_rows(k: &Mat, v: &Mat) -> FocrResult<Self> {
        let width = NUM_HEADS * HEAD_DIM;
        if k.shape() != v.shape() || k.cols != width {
            return Err(FocrError::Other(anyhow::anyhow!(
                "srswa kv: K {:?} and V {:?}, want [rows, {width}] each",
                k.shape(),
                v.shape()
            )));
        }
        let split = |m: &Mat| -> Vec<Vec<f32>> {
            (0..NUM_HEADS)
                .map(|h| {
                    (0..m.rows)
                        .flat_map(|r| &m.row(r)[h * HEAD_DIM..(h + 1) * HEAD_DIM])
                        .copied()
                        .collect()
                })
                .collect()
        };
        Ok(Self {
            rows: k.rows,
            k: split(k),
            v: split(v),
        })
    }

    fn block(&self, b: usize) -> std::ops::Range<usize> {
        b * BLOCK_SIZE * HEAD_DIM..((b + 1) * BLOCK_SIZE).min(self.rows) * HEAD_DIM
    }
}

impl BlockKv for HeadMajorKv {
    fn rows(&self) -> usize {
        self.rows
    }

    fn keys(&self, h: usize, b: usize) -> &[f32] {
        &self.k[h][self.block(b)]
    }

    fn values(&self, h: usize, b: usize) -> &[f32] {
        &self.v[h][self.block(b)]
    }
}

/// A contiguous run of attended rows `[from, to)` inside `block`, and the bias
/// their logits take.
#[derive(Debug, Clone, Copy, PartialEq)]
struct Run {
    block: usize,
    from: usize,
    to: usize,
    bias: f32,
}

/// The rows the query at `q_pos` attends, as runs in ascending position order:
/// each selected block's causal rows outside the local window (with its bias),
/// then the local window (no bias). A selected block's rows inside the window
/// are read once, as local rows.
fn attended_runs(q_pos: usize, sel: &BlockSelection) -> Vec<Run> {
    let local_start = (q_pos + 1).saturating_sub(LOCAL_WINDOW);
    let mut runs = Vec::with_capacity(sel.blocks.len() + LOCAL_BLOCKS + 1);
    for (&b, &bias) in sel.blocks.iter().zip(&sel.bias) {
        let start = b as usize * BLOCK_SIZE;
        let end = (start + BLOCK_SIZE).min(local_start);
        if start < end {
            runs.push(Run {
                block: b as usize,
                from: 0,
                to: end - start,
                bias,
            });
        }
    }
    let mut j = local_start;
    while j <= q_pos {
        let b = j / BLOCK_SIZE;
        let end = ((b + 1) * BLOCK_SIZE).min(q_pos + 1);
        runs.push(Run {
            block: b,
            from: j - b * BLOCK_SIZE,
            to: end - b * BLOCK_SIZE,
            bias: 0.0,
        });
        j = end;
    }
    runs
}

/// Max-subtracted softmax in place, accumulated in f32.
fn softmax_in_place(x: &mut [f32]) {
    let max = x.iter().copied().fold(f32::NEG_INFINITY, f32::max);
    let mut den = 0.0f32;
    for v in x.iter_mut() {
        *v = (*v - max).exp();
        den += *v;
    }
    for v in x.iter_mut() {
        *v /= den;
    }
}

/// One head of one query over its runs: logits in run order, softmax, then
/// `Σ p_j·v_j` in the same order. `logits` is reusable scratch.
fn attend_head(
    qh: &[f32],
    runs: &[Run],
    kv: &impl BlockKv,
    h: usize,
    logits: &mut Vec<f32>,
    out: &mut [f32],
) {
    let scale = inv_sqrt_d();
    logits.clear();
    for run in runs {
        let keys = kv.keys(h, run.block);
        for j in run.from..run.to {
            logits.push(dot(qh, &keys[j * HEAD_DIM..(j + 1) * HEAD_DIM]) * scale + run.bias);
        }
    }
    softmax_in_place(logits);
    out.fill(0.0);
    let mut probs = logits.iter();
    for run in runs {
        let values = kv.values(h, run.block);
        for j in run.from..run.to {
            let p = *probs.next().expect("one probability per attended row");
            for (o, &v) in out
                .iter_mut()
                .zip(&values[j * HEAD_DIM..(j + 1) * HEAD_DIM])
            {
                *o += p * v;
            }
        }
    }
}

fn check_attend_inputs(
    q: &Mat,
    q_pos: &[usize],
    sel: &[BlockSelection],
    kv: &impl BlockKv,
) -> FocrResult<()> {
    let width = NUM_HEADS * HEAD_DIM;
    if q.cols != width || q_pos.len() != q.rows || sel.len() != q.rows {
        return Err(FocrError::Other(anyhow::anyhow!(
            "srswa attention: q {:?} (want [rows, {width}]), {} positions, {} selections",
            q.shape(),
            q_pos.len(),
            sel.len()
        )));
    }
    if let Some(&p) = q_pos.iter().find(|&&p| p >= kv.rows()) {
        return Err(FocrError::Other(anyhow::anyhow!(
            "srswa attention: query position {p} past the {} KV rows",
            kv.rows()
        )));
    }
    Ok(())
}

/// SR-SWA attention, the gathered production path: each query reads only its
/// local window and selected blocks. `q` is `[rows, NUM_HEADS × HEAD_DIM]`
/// (RoPE applied), `q_pos[r]` row `r`'s KV position and `sel[r]` its selection;
/// `kv` holds every row up to the last query. Prefill passes all its rows,
/// decode one. Returns the concatenated heads, `[rows, NUM_HEADS × HEAD_DIM]`,
/// ready for `o_proj`.
///
/// (row, head) pairs run on the Rayon pool; each is reduced alone in a fixed
/// order, so the result does not depend on the thread count. Hold no lock
/// across this call (doctrine #5).
///
/// # Errors
/// [`FocrError::Other`] on a shape mismatch or a query past the KV rows.
pub fn attend(
    q: &Mat,
    q_pos: &[usize],
    sel: &[BlockSelection],
    kv: &(impl BlockKv + Sync),
) -> FocrResult<Mat> {
    check_attend_inputs(q, q_pos, sel, kv)?;
    let mut out = Mat::zeros(q.rows, NUM_HEADS * HEAD_DIM);
    out.data
        .par_chunks_mut(HEAD_DIM)
        .enumerate()
        .for_each_init(Vec::new, |logits, (i, dst)| {
            let (r, h) = (i / NUM_HEADS, i % NUM_HEADS);
            let runs = attended_runs(q_pos[r], &sel[r]);
            let qh = &q.row(r)[h * HEAD_DIM..(h + 1) * HEAD_DIM];
            attend_head(qh, &runs, kv, h, logits, dst);
        });
    Ok(out)
}

/// The dense oracle (DA:1230-1261's structure): every key `0..rows` gets a
/// logit, `−∞` off the attended set and the block bias on remote rows, then
/// one softmax over the full row. Equal to [`attend`] bit for bit; for tests
/// and diagnosis (U2-B5 puts it behind `FOCR_SRSWA_DENSE=1`).
///
/// # Errors
/// As [`attend`].
pub fn attend_dense(
    q: &Mat,
    q_pos: &[usize],
    sel: &[BlockSelection],
    kv: &impl BlockKv,
) -> FocrResult<Mat> {
    check_attend_inputs(q, q_pos, sel, kv)?;
    let scale = inv_sqrt_d();
    let rows = kv.rows();
    let mut out = Mat::zeros(q.rows, NUM_HEADS * HEAD_DIM);
    let mut logits = vec![0.0f32; rows];
    for (r, (&qp, s)) in q_pos.iter().zip(sel).enumerate() {
        let bias_of = |b: usize| {
            s.blocks
                .iter()
                .position(|&x| x as usize == b)
                .map(|i| s.bias[i])
        };
        for h in 0..NUM_HEADS {
            let qh = &q.row(r)[h * HEAD_DIM..(h + 1) * HEAD_DIM];
            for (j, logit) in logits.iter_mut().enumerate() {
                let (b, o) = (j / BLOCK_SIZE, j % BLOCK_SIZE);
                let local = j <= qp && j + LOCAL_WINDOW > qp;
                let remote = j <= qp && !local && bias_of(b).is_some();
                *logit = if local || remote {
                    let k = &kv.keys(h, b)[o * HEAD_DIM..(o + 1) * HEAD_DIM];
                    dot(qh, k) * scale
                        + if remote {
                            bias_of(b).unwrap_or(0.0)
                        } else {
                            0.0
                        }
                } else {
                    f32::NEG_INFINITY
                };
            }
            softmax_in_place(&mut logits);
            let dst = &mut out.row_mut(r)[h * HEAD_DIM..(h + 1) * HEAD_DIM];
            for (j, &p) in logits.iter().enumerate() {
                let (b, o) = (j / BLOCK_SIZE, j % BLOCK_SIZE);
                let v = &kv.values(h, b)[o * HEAD_DIM..(o + 1) * HEAD_DIM];
                for (d, &vd) in dst.iter_mut().zip(v) {
                    *d += p * vd;
                }
            }
        }
    }
    Ok(out)
}

/// The switch for the dense oracle attention ([`attend_dense`]): bit-identical
/// to [`attend`] and far slower, for diagnosis.
pub const DENSE_ENV: &str = "FOCR_SRSWA_DENSE";

/// Whether a [`DENSE_ENV`] value turns the dense attention on (a truthy value;
/// unset is off).
#[must_use]
pub fn dense_enabled_for(value: Option<&str>) -> bool {
    value.is_some_and(crate::quant::recipe::is_truthy)
}

/// [`DENSE_ENV`], read once per process.
#[must_use]
pub fn dense_enabled() -> bool {
    use std::sync::OnceLock;
    static FLAG: OnceLock<bool> = OnceLock::new();
    *FLAG.get_or_init(|| dense_enabled_for(std::env::var(DENSE_ENV).ok().as_deref()))
}

/// [`attend`], or [`attend_dense`] when `dense`.
///
/// # Errors
/// As [`attend`].
pub fn attend_with(
    q: &Mat,
    q_pos: &[usize],
    sel: &[BlockSelection],
    kv: &(impl BlockKv + Sync),
    dense: bool,
) -> FocrResult<Mat> {
    if dense {
        attend_dense(q, q_pos, sel, kv)
    } else {
        attend(q, q_pos, sel, kv)
    }
}

/// One layer's attention inputs for a run of rows (U2-B5): the
/// `input_layernorm` output, which the router reads, and the main attention's
/// RoPE'd queries and keys and its values.
#[derive(Debug, Clone, Copy)]
pub struct LayerRows<'a> {
    /// `[rows, hidden]`.
    pub normed: &'a Mat,
    /// `[rows, NUM_HEADS × HEAD_DIM]`, RoPE'd.
    pub q: &'a Mat,
    /// `[rows, NUM_HEADS × HEAD_DIM]`, RoPE'd.
    pub k: &'a Mat,
    /// `[rows, NUM_HEADS × HEAD_DIM]`.
    pub v: &'a Mat,
}

/// One decoded row's coordinates (U2-B5): its page2d RoPE table, which the
/// main attention and the router share, and its Page Gate query page.
#[derive(Debug, Clone)]
pub struct DecodeStep {
    /// The row's page2d cos/sin table.
    pub rope: RopeTable,
    /// Its query page, −1 when there is none.
    pub query_page: i32,
    /// The page-count guard forces the token this row's hidden state
    /// predicts, so its attention cannot change the output: the retirement
    /// detector stands down and retirement pauses (U2-B4). [`SrSwaState::feed`]
    /// sets it false; the decode loop sets it from the guard.
    pub forced: bool,
}

/// One v2 sequence's attention state between decode steps (U2-B5): each
/// layer's cache, the page coordinates of the next fed token, and the
/// routing settings, all fixed at prefill.
#[derive(Debug, Clone)]
pub struct SrSwaState {
    /// Per layer, in order.
    pub layers: Vec<SrSwaLayerCache>,
    /// Advanced once per fed token.
    pub page: DecodePageState,
    /// The router's parameters.
    pub params: RouterParams,
    /// The entmax the router runs.
    pub domain: EntmaxDomain,
    /// Whether attention runs [`attend_dense`].
    pub dense: bool,
    /// The routing a parity test reads back (U2-C2), when on. Off by default.
    pub trace: Option<SrSwaTrace>,
}

/// What a traced [`SrSwaState`] keeps of its routing (U2-C2): the routing
/// rungs compare it with the f32 oracle's, the re-prefill metamorphic
/// relation compares an incremental step with a fresh prefill, and the trace
/// diff prints it at a token mismatch.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SrSwaTrace {
    /// Per layer, the prefill's last row's route.
    pub prefill_last: Vec<Option<Route>>,
    /// Per layer, the prefill rows [`window_pad_sensitive`] flags.
    pub pad_sensitive: Vec<Vec<usize>>,
    /// Per layer, the latest decode step's route.
    pub decode: Vec<Option<DecodeRoute>>,
}

impl SrSwaTrace {
    /// An empty trace for `layers` layers.
    #[must_use]
    pub fn new(layers: usize) -> Self {
        Self {
            prefill_last: vec![None; layers],
            pad_sensitive: vec![Vec::new(); layers],
            decode: vec![None; layers],
        }
    }

    /// Record one layer's prefill routing of rows `start..start + iq.rows`
    /// from their index queries, routed over `sums` and `blocks`.
    #[allow(clippy::too_many_arguments)]
    fn record_prefill(
        &mut self,
        layer: usize,
        iq: &Mat,
        start: usize,
        (sums, blocks): (&BlockSummaries, &BlockPages),
        coords: &PageCoords,
        p: &RouterParams,
        last: Option<&Route>,
    ) {
        self.pad_sensitive[layer] = (start..start + iq.rows)
            .filter(|&q| {
                let sc = scores(iq.row(q - start), q, sums);
                let allow = page_gate_prefill(blocks, coords.query_page[q]);
                let cand = candidates(&sc, Some(&allow));
                window_pad_sensitive(&entmax_inputs(&sc, &cand, p), &cand)
            })
            .collect();
        self.prefill_last[layer] = last.cloned();
    }
}

/// Whether a row's route can depend on the router window M alone (plan §5.8).
/// A window wider than the row's own blocks adds non-candidate pads at
/// [`ENTMAX_NEG`]. They stay out of the entmax support and rank below every
/// candidate when the best candidate input is at least `ENTMAX_NEG + 2` and
/// none is at or below `ENTMAX_NEG`; a row without candidates reads no remote
/// block in any window. A prefill sets every row's M from its whole length,
/// and a decode step from its own position, so only a flagged row can route
/// differently in a re-prefill than it did incrementally.
#[must_use]
pub fn window_pad_sensitive(x: &[f32], cand: &[bool]) -> bool {
    let mut inputs = x.iter().zip(cand).filter(|&(_, &c)| c).map(|(&v, _)| v);
    let Some(first) = inputs.next() else {
        return false;
    };
    let (lo, hi) = inputs.fold((first, first), |(lo, hi), v| (lo.min(v), hi.max(v)));
    hi < ENTMAX_NEG + 2.0 || lo <= ENTMAX_NEG
}

impl SrSwaState {
    /// Empty caches for a sequence whose prompt has `coords`, with the
    /// parity defaults ([`RouterParams::uocr2`], [`EntmaxDomain::TopM`]) and
    /// [`dense_enabled`]. Block retirement starts off: it is safe only under
    /// the page-count guard, and the decode loop turns it on
    /// ([`Self::set_retire`]).
    ///
    /// # Errors
    /// As [`SrSwaLayerCache::new`].
    pub fn new(
        router: &[SrSwaLayerWeights],
        coords: &PageCoords,
        page_base: u32,
    ) -> FocrResult<Self> {
        Ok(Self {
            layers: router
                .iter()
                .map(|w| SrSwaLayerCache::new(&w.head_cls, false))
                .collect::<FocrResult<_>>()?,
            page: DecodePageState::after_prompt(coords, page_base),
            params: RouterParams::uocr2(),
            domain: EntmaxDomain::TopM,
            dense: dense_enabled(),
            trace: None,
        })
    }

    /// One layer's prefill of the rows after those its cache holds: the
    /// prompt into an empty cache, or a reused window's new pages after the
    /// rows it copied (U2-D1a's extend). `coords` describes every row of the
    /// forward, cached ones included, and `rows` the new ones, `start =
    /// cached rows` onward. The router's index queries and keys
    /// ([`project`]); the new rows' K, V and index keys stored with their
    /// image pages; every new row routed over all the rows' block summaries
    /// and pages, whose `nblk` sets the window M of every row (MD:437,
    /// 351-352; recorded when tracing); and attention through the cache.
    /// `rope` is the new rows' page2d table, already applied to
    /// `rows.q`/`rows.k`. From an empty cache this is [`prefill_routes`]'
    /// routing exactly: a block's cached summary is [`summaries`]'.
    ///
    /// # Errors
    /// [`FocrError::Other`] if `coords` does not describe the cached rows
    /// plus `rows`, or as [`project`], [`SrSwaLayerCache::append`] and
    /// [`attend`].
    pub fn prefill_layer(
        &mut self,
        layer: usize,
        w: &SrSwaLayerWeights,
        rows: LayerRows<'_>,
        rope: &RopeTable,
        coords: &PageCoords,
    ) -> FocrResult<Mat> {
        let start = self.layers[layer].rows();
        if start + rows.q.rows != coords.bucket.len() {
            return Err(FocrError::Other(anyhow::anyhow!(
                "srswa prefill: layer {layer} caches {start} rows and gets {} more, but the \
                 coordinates describe {} rows",
                rows.q.rows,
                coords.bucket.len()
            )));
        }
        let (iq, ik) = project(rows.normed, w, rope)?;
        let cache = &mut self.layers[layer];
        cache.append(rows.k, rows.v, &ik, &coords.img_page[start..])?;
        let blocks = BlockPages::build(&coords.img_page);
        let (p, domain) = (&self.params, self.domain);
        let routes = prefill_rows_routes(&iq, start, cache.summaries(), &blocks, coords, p, domain);
        if let Some(trace) = &mut self.trace {
            let sums = (cache.summaries(), &blocks);
            trace.record_prefill(layer, &iq, start, sums, coords, p, routes.last());
        }
        let sel: Vec<BlockSelection> = routes.into_iter().map(BlockSelection::from).collect();
        let positions: Vec<usize> = (start..start + rows.q.rows).collect();
        attend_with(rows.q, &positions, &sel, &*cache, self.dense)
    }

    /// Turn block retirement on or off in every layer (U2-B6). Pass
    /// [`retire_enabled`] only while the page-count guard runs; attention that
    /// runs dense reads every row, so it keeps retirement off.
    pub fn set_retire(&mut self, retire: bool) {
        let on = retire && !self.dense;
        for cache in &mut self.layers {
            cache.retire = on;
        }
    }

    /// Rows each layer caches; the layers grow in step.
    #[must_use]
    pub fn rows(&self) -> usize {
        self.layers.first().map_or(0, BlockKv::rows)
    }

    /// Retired blocks across the layers.
    #[must_use]
    pub fn dead_blocks(&self) -> usize {
        self.layers.iter().map(SrSwaLayerCache::dead_blocks).sum()
    }

    /// The layers' peak resident KV bytes, summed. The layers grow and retire
    /// in step, so this is the sequence's peak.
    #[must_use]
    pub fn peak_bytes(&self) -> usize {
        self.layers.iter().map(SrSwaLayerCache::peak_bytes).sum()
    }

    /// Advance the page state over the fed token `id` and return its row's
    /// coordinates.
    pub fn feed(&mut self, id: u32) -> DecodeStep {
        let coord = self.page.feed(id);
        DecodeStep {
            rope: page2d_rope_table(&[coord.tok_pos], &[coord.rope_page]),
            query_page: coord.query_page,
            forced: false,
        }
    }

    /// One decode step's attention in `layer` for the single row `rows` at
    /// `step` ([`Self::feed`]): index the row, append its K, V and index key
    /// (a generated row, so no image page), route it from the cache, attend,
    /// then retire the blocks dead at this row
    /// ([`SrSwaLayerCache::retire_dead_blocks`]).
    ///
    /// # Errors
    /// [`FocrError::Other`] on a shape mismatch, or wrapping the
    /// [`RetireFallback`] (recoverable with [`retire_fallback`]) when the
    /// retirement detector fires, which is impossible while retirement is off.
    pub fn decode_layer(
        &mut self,
        layer: usize,
        w: &SrSwaLayerWeights,
        rows: LayerRows<'_>,
        step: &DecodeStep,
    ) -> FocrResult<Mat> {
        // FOCR_PROFILE_DECODE splits this layer's share of `attn` by phase.
        let profiling = prof::enabled();
        let mut t = profiling.then(Instant::now);
        let mut lap = |counter: &std::sync::atomic::AtomicU64| {
            if let Some(start) = t {
                let now = Instant::now();
                prof::add(counter, (now - start).as_nanos() as u64);
                t = Some(now);
            }
        };
        let (iq, ik) = project(rows.normed, w, &step.rope)?;
        lap(&prof::SRSWA_PROJECT_NS);
        let cache = &mut self.layers[layer];
        cache.append(rows.k, rows.v, &ik, &[-1])?;
        lap(&prof::SRSWA_APPEND_NS);
        let route = cache
            .decode_route(
                iq.row(0),
                step.query_page,
                &self.params,
                self.domain,
                step.forced,
            )
            .map_err(|e| {
                FocrError::Other(anyhow::Error::new(e).context(format!("layer {layer}")))
            })?;
        if let Some(trace) = &mut self.trace {
            trace.decode[layer] = Some(route.clone());
        }
        lap(&prof::SRSWA_ROUTE_NS);
        let q_pos = cache.rows() - 1;
        let out = attend_with(
            rows.q,
            &[q_pos],
            &[BlockSelection::from(route.route)],
            &*cache,
            self.dense,
        )?;
        lap(&prof::SRSWA_ATTEND_NS);
        cache.retire_dead_blocks(step.query_page, q_pos, step.forced)?;
        lap(&prof::SRSWA_RETIRE_NS);
        Ok(out)
    }
}

/// The [`RetireFallback`] behind a decode error, if the retirement detector
/// raised it ([`SrSwaState::decode_layer`]).
#[must_use]
pub fn retire_fallback(error: &FocrError) -> Option<&RetireFallback> {
    match error {
        FocrError::Other(e) => e.downcast_ref::<RetireFallback>(),
        _ => None,
    }
}

// ── Layer cache and block retirement (U2-B4) ────────────────────────────────
//
// One layer's K, V and index keys live in 32-row block slabs, so one block can
// be freed on its own (plan §5.12, D9). `k̄`, `valid`, bmin and bmax are kept
// for every **logical** block, retired or not, so block indices, `nblk` and
// the router window M never change.
//
// At decode query position q on query page qp ≥ 0, an unpinned block c is dead
// once (1) `bmin[c] = −1` or `bmax[c] < qp` (no non-open query can allow it
// again: qp never decreases) and (2) its last row ≤ q − 128 (it has left the
// local window (q − 128, q], MD:472-473, for good). A non-open row never allows
// a dead block, so the block is never a candidate there and every selection,
// bias and attention output is the unretired run's. An open row reads every
// remote block. Open rows meeting retired blocks happen only on guard-forced
// steps (the proof in plan §5.12); `decode_route` checks it on every row.
//
// Page state is the same in every layer, so it lives once per sequence in
// `DecodePageState` (U2-B1) and the query page is an argument here.

/// The kill switch for block retirement.
pub const RETIRE_ENV: &str = "FOCR_SRSWA_RETIRE";

/// Whether a [`RETIRE_ENV`] value leaves retirement on: everything but
/// `0`/`off`/`false`/`no` does, unset included.
#[must_use]
pub fn retire_enabled_for(value: Option<&str>) -> bool {
    !matches!(
        value.map(|v| v.trim().to_ascii_lowercase()).as_deref(),
        Some("0" | "off" | "false" | "no")
    )
}

/// [`RETIRE_ENV`], read once per process.
#[must_use]
pub fn retire_enabled() -> bool {
    use std::sync::OnceLock;
    static FLAG: OnceLock<bool> = OnceLock::new();
    *FLAG.get_or_init(|| retire_enabled_for(std::env::var(RETIRE_ENV).ok().as_deref()))
}

/// Bytes one row costs one layer: K and V (`2 × 1280` f32) and the index key
/// (128 f32). Over 12 layers that is plan §6.3's 120 KiB + 6 KiB per token.
pub const ROW_BYTES: usize = (2 * NUM_HEADS * HEAD_DIM + HEAD_DIM) * size_of::<f32>();

/// Bytes kept for every logical block, retired or not: `k̄`, bmin, bmax,
/// `valid` and the pin flag.
pub const BLOCK_META_BYTES: usize = HEAD_DIM * size_of::<f32>() + 2 * size_of::<i32>() + 2;

/// One head's rows inside a block slab.
const HEAD_STRIDE: usize = BLOCK_SIZE * HEAD_DIM;

/// One block's rows: K and V head-major (`[NUM_HEADS][32][HEAD_DIM]`, so a
/// head's rows are one contiguous run for [`BlockKv`]) and the index keys
/// (`[32][HEAD_DIM]`, the layout [`block_summary`] reads).
#[derive(Debug, Clone, PartialEq)]
struct Slab {
    k: Box<[f32]>,
    v: Box<[f32]>,
    ik: Box<[f32]>,
}

impl Slab {
    fn new() -> Self {
        let kv = || vec![0.0f32; NUM_HEADS * HEAD_STRIDE].into_boxed_slice();
        Self {
            k: kv(),
            v: kv(),
            ik: vec![0.0f32; HEAD_STRIDE].into_boxed_slice(),
        }
    }
}

/// Why a decode row is open: its candidates are every remote block (MU:520-528,
/// MD:346-348).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpenRow {
    /// `qp < 0`: no generated `<PAGE>` yet.
    NoPage,
    /// No block holds an image of page qp, i.e. qp ≥ N after an extra
    /// `<PAGE>`.
    NoAllowedBlock,
    /// Blocks of page qp exist but none is remote yet.
    NoRemoteAllowed,
}

/// The detector's event: an open row met retired blocks on a step whose output
/// is not guard-forced, so its read set may differ from the unretired run's.
/// The caller re-runs the window (or the document) without retirement, the
/// deterministic fallback of plan §5.12.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RetireFallback {
    /// The query's KV position.
    pub q_pos: usize,
    /// The query page.
    pub query_page: i32,
    /// Why the row was open.
    pub open: OpenRow,
    /// Retired blocks at that point.
    pub dead_blocks: usize,
}

impl std::fmt::Display for RetireFallback {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "srswa retirement: open decode row ({:?}) at position {} on query page {} with \
             {} retired blocks; re-run without retirement (OQ-V2-11)",
            self.open, self.q_pos, self.query_page, self.dead_blocks
        )
    }
}

impl std::error::Error for RetireFallback {}

/// A decode query's route, whether its row was open, and the score row and
/// Page Gate it was routed from.
#[derive(Debug, Clone, PartialEq)]
pub struct DecodeRoute {
    /// The read set and biases.
    pub route: Route,
    /// `Some` for an open row.
    pub open: Option<OpenRow>,
    /// Every block's score, [`SCORE_NEG`] where it is not remote or retired.
    pub scores: Vec<f32>,
    /// The Page Gate.
    pub allow: Vec<bool>,
}

/// Whether a decode row on page `qp` is open, from the prompt's bmin/bmax and
/// the row's scores (remote iff above `SCORE_NEG / 2`).
fn decode_open_row(bmin: &[i32], bmax: &[i32], qp: i32, scores: &[f32]) -> Option<OpenRow> {
    if qp < 0 {
        return Some(OpenRow::NoPage);
    }
    let mut allowed = false;
    for (c, (&lo, &hi)) in bmin.iter().zip(bmax).enumerate() {
        if lo >= 0 && lo <= qp && qp <= hi {
            if scores[c] > SCORE_NEG * 0.5 {
                return None;
            }
            allowed = true;
        }
    }
    Some(if allowed {
        OpenRow::NoRemoteAllowed
    } else {
        OpenRow::NoAllowedBlock
    })
}

/// Rows read back by [`deserialize_tail`], in the layout
/// [`SrSwaLayerCache::append`] takes.
#[derive(Debug, Clone, PartialEq)]
pub struct TailRows {
    /// `[n, NUM_HEADS × HEAD_DIM]`, RoPE applied.
    pub k: Mat,
    /// `[n, NUM_HEADS × HEAD_DIM]`.
    pub v: Mat,
    /// `[n, HEAD_DIM]`, post-norm and post-RoPE.
    pub ik: Mat,
}

/// Tail format: this magic, then little-endian u32 version, heads and head
/// dim, a u64 row count, and per row its K, V and index key as little-endian
/// f32 (U2-D1a splices, U2-D4 checkpoints).
const TAIL_MAGIC: &[u8; 8] = b"FOCRSRSW";
const TAIL_VERSION: u32 = 1;
const TAIL_HEADER: usize = 8 + 3 * 4 + 8;

/// One layer's SR-SWA cache: K, V, index keys, block summaries, bmin/bmax,
/// the retired-block set and memory accounting (U2-B4). Reads go through
/// [`BlockKv`]; reading a retired block panics, because the detector keeps
/// every route off them.
#[derive(Debug, Clone, PartialEq)]
pub struct SrSwaLayerCache {
    head_cls: Vec<f32>,
    rows: usize,
    /// Per logical block, `None` once retired.
    slabs: Vec<Option<Slab>>,
    sums: BlockSummaries,
    bmin: Vec<i32>,
    bmax: Vec<i32>,
    pinned: Vec<bool>,
    text_tail_margin: bool,
    retire: bool,
    dead: usize,
    peak_bytes: usize,
    /// The last `(qp, q)` passed to `retire_dead_blocks`; neither may fall.
    last_step: Option<(i32, usize)>,
}

impl SrSwaLayerCache {
    /// An empty cache for a layer whose router summary vector is `head_cls`
    /// ([`SrSwaLayerWeights::head_cls`]). `retire` turns block retirement on:
    /// pass [`retire_enabled`], and `false` whenever the page-count guard is
    /// off or attention runs dense ([`attend_dense`] reads every row).
    ///
    /// # Errors
    /// [`FocrError::Other`] unless `head_cls` has [`HEAD_DIM`] values.
    pub fn new(head_cls: &[f32], retire: bool) -> FocrResult<Self> {
        if head_cls.len() != HEAD_DIM {
            return Err(router_shape(
                "dash_router.head_cls",
                head_cls.len(),
                &HEAD_DIM.to_string(),
            ));
        }
        Ok(Self {
            head_cls: head_cls.to_vec(),
            rows: 0,
            slabs: Vec::new(),
            sums: BlockSummaries {
                kbar: Vec::new(),
                valid: Vec::new(),
            },
            bmin: Vec::new(),
            bmax: Vec::new(),
            pinned: Vec::new(),
            text_tail_margin: false,
            retire,
            dead: 0,
            peak_bytes: 0,
            last_step: None,
        })
    }

    /// Logical blocks, `ceil(rows / 32)`, retired ones included.
    #[must_use]
    pub fn nblk(&self) -> usize {
        self.slabs.len()
    }

    /// Retired blocks.
    #[must_use]
    pub fn dead_blocks(&self) -> usize {
        self.dead
    }

    /// Whether block `b` still holds its rows.
    #[must_use]
    pub fn is_live(&self, b: usize) -> bool {
        self.slabs.get(b).is_some_and(Option::is_some)
    }

    /// The block summaries of every logical block.
    #[must_use]
    pub fn summaries(&self) -> &BlockSummaries {
        &self.sums
    }

    /// Lowest image page per block, −1 when it holds none.
    #[must_use]
    pub fn bmin(&self) -> &[i32] {
        &self.bmin
    }

    /// Highest image page per block, −1 when it holds none.
    #[must_use]
    pub fn bmax(&self) -> &[i32] {
        &self.bmax
    }

    /// Bytes held now: the live blocks' slabs plus every logical block's
    /// bookkeeping.
    #[must_use]
    pub fn live_bytes(&self) -> usize {
        (self.nblk() - self.dead) * BLOCK_SIZE * ROW_BYTES + self.nblk() * BLOCK_META_BYTES
    }

    /// The most [`Self::live_bytes`] has been.
    #[must_use]
    pub fn peak_bytes(&self) -> usize {
        self.peak_bytes
    }

    /// What one layer's cache holds for `rows` rows with nothing retired: whole
    /// blocks of [`ROW_BYTES`] rows plus the per-block bookkeeping (the
    /// estimator's input, U2-D3).
    #[must_use]
    pub fn estimate_bytes(rows: usize) -> usize {
        rows.div_ceil(BLOCK_SIZE) * (BLOCK_SIZE * ROW_BYTES + BLOCK_META_BYTES)
    }

    fn block_rows(&self, b: usize) -> usize {
        (self.rows - b * BLOCK_SIZE).min(BLOCK_SIZE)
    }

    fn live_slab(&self, b: usize) -> &Slab {
        self.slabs[b].as_ref().unwrap_or_else(|| {
            panic!("srswa cache: block {b} was retired and must not be read (OQ-V2-11)") // ubs:ignore[rust.ownership.panic-macro] - unreachable: retirement (set_retire, !dense under the page-count guard) retires only U2-B4 dead blocks; the detector returns a recoverable RetireFallback before any read (OQ-V2-11)
        })
    }

    /// Append `n` rows: K and V `[n, NUM_HEADS × HEAD_DIM]` (RoPE applied to
    /// K), index keys `[n, HEAD_DIM]` from [`project`], and each row's image
    /// page: `PageCoords::img_page` for prompt rows and −1 for every generated
    /// row, which never counts as an image token even when it is id 128815
    /// (MU:514-528). Prefill appends its rows at once, decode one. The summary
    /// of each block the rows reach is recomputed from all its index keys, so
    /// a full block's `k̄` is final and the tail's equals [`summaries`].
    ///
    /// # Errors
    /// [`FocrError::Other`] on a shape mismatch.
    pub fn append(&mut self, k: &Mat, v: &Mat, ik: &Mat, img_page: &[i32]) -> FocrResult<()> {
        let (n, width) = (k.rows, NUM_HEADS * HEAD_DIM);
        if k.shape() != (n, width)
            || v.shape() != (n, width)
            || ik.shape() != (n, HEAD_DIM)
            || img_page.len() != n
        {
            return Err(FocrError::Other(anyhow::anyhow!(
                "srswa cache append: K {:?}, V {:?}, index keys {:?}, {} image pages; want \
                 [n, {width}], [n, {width}], [n, {HEAD_DIM}] and n",
                k.shape(),
                v.shape(),
                ik.shape(),
                img_page.len()
            )));
        }
        let first = self.rows / BLOCK_SIZE;
        for (r, &page) in img_page.iter().enumerate() {
            let (b, o) = (self.rows / BLOCK_SIZE, self.rows % BLOCK_SIZE);
            if o == 0 {
                self.slabs.push(Some(Slab::new()));
                self.sums.kbar.extend([0.0f32; HEAD_DIM]);
                self.sums.valid.push(false);
                self.bmin.push(-1);
                self.bmax.push(-1);
                self.pinned.push(false);
            }
            // Retirement needs the block's last row ≤ q − 128 < rows, so the
            // block being filled is live.
            let slab = self.slabs[b]
                .as_mut()
                .expect("the block being appended to is live");
            for h in 0..NUM_HEADS {
                let (dst, src) = (h * HEAD_STRIDE + o * HEAD_DIM, h * HEAD_DIM);
                slab.k[dst..dst + HEAD_DIM].copy_from_slice(&k.row(r)[src..src + HEAD_DIM]);
                slab.v[dst..dst + HEAD_DIM].copy_from_slice(&v.row(r)[src..src + HEAD_DIM]);
            }
            slab.ik[o * HEAD_DIM..(o + 1) * HEAD_DIM].copy_from_slice(ik.row(r));
            if page >= 0 {
                self.bmin[b] = if self.bmin[b] < 0 {
                    page
                } else {
                    self.bmin[b].min(page)
                };
                self.bmax[b] = self.bmax[b].max(page);
            }
            self.rows += 1;
        }
        for b in first..self.nblk() {
            let n = self.block_rows(b);
            let slab = self.slabs[b].as_ref().expect("appended blocks are live");
            let out = &mut self.sums.kbar.as_chunks_mut::<HEAD_DIM>().0[b];
            block_summary(&slab.ik[..n * HEAD_DIM], &self.head_cls, out);
            self.sums.valid[b] = n > 0;
        }
        self.peak_bytes = self.peak_bytes.max(self.live_bytes());
        Ok(())
    }

    /// Route the decode query, the newest row at `rows − 1`, from its index
    /// query `iq` and query page `qp` (MU:481-528, MD:338-362). Retired blocks
    /// score as non-remote, so no route selects one. A non-open row never
    /// allows them anyway, so its route is the unretired one. An open row
    /// meeting retired blocks fires the detector unless `forced_step` (the
    /// guard forces this step's output, so attention cannot change it).
    ///
    /// # Errors
    /// [`RetireFallback`] when the detector fires.
    ///
    /// # Panics
    /// On an empty cache.
    pub fn decode_route(
        &self,
        iq: &[f32],
        qp: i32,
        p: &RouterParams,
        domain: EntmaxDomain,
        forced_step: bool,
    ) -> Result<DecodeRoute, RetireFallback> {
        assert!(
            self.rows > 0,
            "decode_route: the query row is not in the cache"
        );
        let q_pos = self.rows - 1;
        let mut sc = scores(iq, q_pos, &self.sums);
        for (s, slab) in sc.iter_mut().zip(&self.slabs) {
            if slab.is_none() {
                *s = SCORE_NEG;
            }
        }
        let open = decode_open_row(&self.bmin, &self.bmax, qp, &sc);
        if let Some(open) = open
            && self.dead > 0
            && !forced_step
        {
            return Err(RetireFallback {
                q_pos,
                query_page: qp,
                open,
                dead_blocks: self.dead,
            });
        }
        let allow = page_gate_decode(&self.bmin, &self.bmax, qp, self.nblk());
        Ok(DecodeRoute {
            route: route(&sc, Some(&allow), p, domain),
            open,
            scores: sc,
            allow,
        })
    }

    /// Free every block dead at decode query position `q` on query page `qp`:
    /// unpinned, `bmin = −1` or `bmax < qp`, and last row ≤ q − 128 (≤ q − 129
    /// for an imageless block under the text-tail margin). Frees nothing while
    /// `qp < 0`, on a guard-forced step, or with retirement off. Returns the
    /// number of blocks freed. U2-B6 calls it once per decode step, after the
    /// step's row is appended; before or after its attention, as no freed
    /// block can be in that step's window or read set.
    ///
    /// # Errors
    /// [`FocrError::Other`] if `q` is past the cache or `q` or `qp` went
    /// backwards: the rule holds only because neither does.
    pub fn retire_dead_blocks(
        &mut self,
        qp: i32,
        q: usize,
        forced_step: bool,
    ) -> FocrResult<usize> {
        if q >= self.rows {
            return Err(FocrError::Other(anyhow::anyhow!(
                "srswa retirement: query position {q} past the {} cached rows",
                self.rows
            )));
        }
        if let Some((last_qp, last_q)) = self.last_step
            && (qp < last_qp || q < last_q)
        {
            return Err(FocrError::Other(anyhow::anyhow!(
                "srswa retirement: (query page, position) went from ({last_qp}, {last_q}) to \
                 ({qp}, {q})"
            )));
        }
        self.last_step = Some((qp, q));
        if !self.retire || forced_step || qp < 0 {
            return Ok(0);
        }
        let mut freed = 0;
        for c in 0..self.nblk() {
            let last = c * BLOCK_SIZE + BLOCK_SIZE - 1;
            if last + LOCAL_WINDOW > q {
                break;
            }
            let imageless = self.bmin[c] < 0;
            let margin = LOCAL_WINDOW + usize::from(imageless && self.text_tail_margin);
            if last + margin > q || self.pinned[c] || self.slabs[c].is_none() {
                continue;
            }
            if imageless || self.bmax[c] < qp {
                self.slabs[c] = None;
                self.dead += 1;
                freed += 1;
            }
        }
        Ok(freed)
    }

    /// Pin the blocks holding any row of `rows` so retirement never frees
    /// them, replacing the previous set. `text_tail_margin` retires an
    /// imageless block only once its last row ≤ q − 129, so a 128-row text
    /// tail ending just before the final row survives. Per window with a
    /// successor, U2-D1a pins the last own page's marker and image-tail blocks
    /// and the lookahead page's rows (plan §5.10.3, §5.10.9); unwindowed runs
    /// pin nothing.
    ///
    /// # Errors
    /// [`FocrError::Other`] if a range reaches past the cache or a block it
    /// touches is already retired.
    pub fn set_pinned(&mut self, rows: &[Range<usize>], text_tail_margin: bool) -> FocrResult<()> {
        let mut pinned = vec![false; self.nblk()];
        for r in rows.iter().filter(|r| !r.is_empty()) {
            if r.end > self.rows {
                return Err(FocrError::Other(anyhow::anyhow!(
                    "srswa pin: rows {r:?} past the {} cached rows",
                    self.rows
                )));
            }
            let blocks = r.start / BLOCK_SIZE..r.end.div_ceil(BLOCK_SIZE);
            if let Some(b) = blocks.clone().find(|&b| !self.is_live(b)) {
                return Err(FocrError::Other(anyhow::anyhow!(
                    "srswa pin: block {b} of rows {r:?} is already retired"
                )));
            }
            pinned[blocks].fill(true);
        }
        self.pinned = pinned;
        self.text_tail_margin = text_tail_margin;
        Ok(())
    }

    /// The rows of `ranges`, in order: K, V and index keys only, in the
    /// layout [`Self::append`] takes. Page state is replayed in the new frame
    /// (U2-B1's reused-window seed), never copied
    /// (W/`session/uocr_doc_reuse.py:500-516`), and `k̄` is recomputed when
    /// the rows are appended.
    ///
    /// # Errors
    /// [`FocrError::Other`] if a row is past the cache or retired.
    pub fn tail_rows(&self, ranges: &[Range<usize>]) -> FocrResult<TailRows> {
        let n: usize = ranges.iter().map(ExactSizeIterator::len).sum();
        let width = NUM_HEADS * HEAD_DIM;
        let (mut k, mut v, mut ik) = (
            Vec::with_capacity(n * width),
            Vec::with_capacity(n * width),
            Vec::with_capacity(n * HEAD_DIM),
        );
        for pos in ranges.iter().flat_map(Clone::clone) {
            let (b, o) = (pos / BLOCK_SIZE, pos % BLOCK_SIZE);
            if pos >= self.rows || !self.is_live(b) {
                return Err(FocrError::Other(anyhow::anyhow!(
                    "srswa tail: row {pos} is {} (block {b})",
                    if pos >= self.rows {
                        "past the cache"
                    } else {
                        "retired"
                    }
                )));
            }
            let slab = self.live_slab(b);
            for (buf, out) in [(&slab.k, &mut k), (&slab.v, &mut v)] {
                for h in 0..NUM_HEADS {
                    let at = h * HEAD_STRIDE + o * HEAD_DIM;
                    out.extend_from_slice(&buf[at..at + HEAD_DIM]);
                }
            }
            ik.extend_from_slice(&slab.ik[o * HEAD_DIM..(o + 1) * HEAD_DIM]);
        }
        Ok(TailRows {
            k: Mat::from_vec(n, width, k),
            v: Mat::from_vec(n, width, v),
            ik: Mat::from_vec(n, HEAD_DIM, ik),
        })
    }

    /// [`Self::tail_rows`] in the tail format ([`deserialize_tail`]).
    ///
    /// # Errors
    /// As [`Self::tail_rows`].
    pub fn serialize_tail(&self, ranges: &[Range<usize>]) -> FocrResult<Vec<u8>> {
        Ok(self.tail_rows(ranges)?.serialize())
    }
}

impl TailRows {
    /// These rows in the tail format ([`deserialize_tail`]).
    #[must_use]
    pub fn serialize(&self) -> Vec<u8> {
        let n = self.rows();
        let mut out = Vec::with_capacity(TAIL_HEADER + n * ROW_BYTES);
        out.extend_from_slice(TAIL_MAGIC);
        for word in [TAIL_VERSION, NUM_HEADS as u32, HEAD_DIM as u32] {
            out.extend_from_slice(&word.to_le_bytes());
        }
        out.extend_from_slice(&(n as u64).to_le_bytes());
        for r in 0..n {
            for xs in [self.k.row(r), self.v.row(r), self.ik.row(r)] {
                for x in xs {
                    out.extend_from_slice(&x.to_le_bytes());
                }
            }
        }
        out
    }

    /// Rows `r` of these rows, as a new set.
    #[must_use]
    pub fn slice(&self, r: Range<usize>) -> Self {
        let take = |m: &Mat| {
            Mat::from_vec(
                r.len(),
                m.cols,
                m.data[r.start * m.cols..r.end * m.cols].to_vec(),
            )
        };
        Self {
            k: take(&self.k),
            v: take(&self.v),
            ik: take(&self.ik),
        }
    }

    /// Number of rows.
    #[must_use]
    pub fn rows(&self) -> usize {
        self.ik.rows
    }
}

/// Read rows written by [`SrSwaLayerCache::serialize_tail`].
///
/// # Errors
/// [`FocrError::Other`] on a bad magic, version, geometry or length.
pub fn deserialize_tail(bytes: &[u8]) -> FocrResult<TailRows> {
    let bad = |why: String| FocrError::Other(anyhow::anyhow!("srswa tail: {why}"));
    if bytes.len() < TAIL_HEADER || &bytes[..8] != TAIL_MAGIC {
        return Err(bad("not a tail (bad magic or short header)".into()));
    }
    let word = |at: usize| u32::from_le_bytes(bytes[at..at + 4].try_into().expect("4 bytes"));
    let (version, heads, dim) = (word(8), word(12), word(16));
    if version != TAIL_VERSION || heads as usize != NUM_HEADS || dim as usize != HEAD_DIM {
        return Err(bad(format!(
            "version {version} with {heads} heads of {dim}; want version {TAIL_VERSION} with \
             {NUM_HEADS} heads of {HEAD_DIM}"
        )));
    }
    let n = u64::from_le_bytes(bytes[20..28].try_into().expect("8 bytes"));
    let want = usize::try_from(n)
        .ok()
        .and_then(|n| n.checked_mul(ROW_BYTES))
        .and_then(|b| b.checked_add(TAIL_HEADER));
    if want != Some(bytes.len()) {
        return Err(bad(format!(
            "{n} rows need {want:?} bytes, got {}",
            bytes.len()
        )));
    }
    let n = n as usize;
    let width = NUM_HEADS * HEAD_DIM;
    let mut floats = bytes[TAIL_HEADER..]
        .as_chunks::<4>()
        .0
        .iter()
        .map(|b| f32::from_le_bytes(*b));
    let mut take = |len: usize| -> Vec<f32> { floats.by_ref().take(len).collect() };
    let (mut k, mut v, mut ik) = (
        Vec::with_capacity(n * width),
        Vec::with_capacity(n * width),
        Vec::with_capacity(n * HEAD_DIM),
    );
    for _ in 0..n {
        k.extend(take(width));
        v.extend(take(width));
        ik.extend(take(HEAD_DIM));
    }
    Ok(TailRows {
        k: Mat::from_vec(n, width, k),
        v: Mat::from_vec(n, width, v),
        ik: Mat::from_vec(n, HEAD_DIM, ik),
    })
}

impl BlockKv for SrSwaLayerCache {
    fn rows(&self) -> usize {
        self.rows
    }

    fn keys(&self, h: usize, b: usize) -> &[f32] {
        let n = self.block_rows(b);
        &self.live_slab(b).k[h * HEAD_STRIDE..h * HEAD_STRIDE + n * HEAD_DIM]
    }

    fn values(&self, h: usize, b: usize) -> &[f32] {
        let n = self.block_rows(b);
        &self.live_slab(b).v[h * HEAD_STRIDE..h * HEAD_STRIDE + n * HEAD_DIM]
    }
}

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

    const IMG: u32 = special::IMAGE;
    const PG: u32 = PAGE_TOKEN_ID;
    const BOS: u32 = special::BOS;

    /// `BOS (<PAGE> <image>×n)… instruction` with the given per-page image counts.
    fn prompt(images: &[usize], instruction: usize) -> Vec<u32> {
        let mut ids = vec![BOS];
        for &n in images {
            ids.push(PG);
            ids.extend(std::iter::repeat_n(IMG, n));
        }
        ids.extend((0..instruction).map(|k| 1000 + k as u32));
        ids
    }

    /// Small deterministic generator for the property tests.
    struct Lcg(u64);
    impl Lcg {
        fn next(&mut self) -> u64 {
            self.0 = self
                .0
                .wrapping_mul(6_364_136_223_846_793_005)
                .wrapping_add(1_442_695_040_888_963_407);
            self.0 >> 33
        }
        fn below(&mut self, n: u64) -> u64 {
            self.next() % n
        }
    }

    #[test]
    fn two_page_base_prompt_hand_computed() {
        // BOS + 2 × (<PAGE> + 273 images) + 4 instruction tokens = 553 rows.
        let ids = prompt(&[273, 273], 4);
        assert_eq!(ids.len(), 553);
        let c = PageCoords::build(&ids).unwrap();
        assert_eq!((c.prompt_pages, c.markers, c.last_img), (2, 2, 548));
        assert_eq!(c.pf_cnt, vec![1, 274, 274]);
        // BOS: bucket 0, first row of it, no page anywhere.
        assert_eq!(
            (c.bucket[0], c.tok_pos[0], c.query_page[0], c.img_page[0]),
            (0, 0, -1, -1)
        );
        // Page 1: marker at 1, images at 2..=274.
        assert_eq!(
            (c.bucket[1], c.tok_pos[1], c.query_page[1], c.img_page[1]),
            (1, 0, 0, -1)
        );
        for i in 2..=274 {
            assert_eq!(
                (c.bucket[i], c.tok_pos[i], c.query_page[i], c.img_page[i]),
                (1, i as u32 - 1, 0, 0)
            );
        }
        // Page 2: marker at 275, images at 276..=548.
        assert_eq!(
            (c.bucket[275], c.tok_pos[275], c.query_page[275]),
            (2, 0, 1)
        );
        for i in 276..=548 {
            assert_eq!(
                (c.bucket[i], c.tok_pos[i], c.query_page[i], c.img_page[i]),
                (2, i as u32 - 275, 1, 1)
            );
        }
        // Instruction: back in bucket 0 after BOS, query page −1.
        for (k, i) in (549..553).enumerate() {
            assert_eq!(
                (c.bucket[i], c.tok_pos[i], c.query_page[i], c.img_page[i]),
                (0, k as u32 + 1, -1, -1)
            );
        }
        // Rope page parity, unwindowed: bucket 1 → 1, bucket 2 → 0, bucket 0 → 0.
        assert_eq!(c.rope_pages(0)[1..3], [1, 1]);
        assert_eq!(c.rope_pages(0)[275], 0);
        assert_eq!(c.rope_pages(0)[0], 0);

        // Decode: the first generated <PAGE> answers page 1 (qp 0), right after
        // its 274 prompt rows; then text; the second answers page 2.
        let mut st = DecodePageState::after_prompt(&c, 0);
        assert_eq!(
            st.feed(PG),
            DecodeCoord {
                bucket: 1,
                tok_pos: 274,
                rope_page: 1,
                query_page: 0
            }
        );
        assert_eq!(
            st.feed(7),
            DecodeCoord {
                bucket: 1,
                tok_pos: 275,
                rope_page: 1,
                query_page: 0
            }
        );
        assert_eq!(
            st.feed(PG),
            DecodeCoord {
                bucket: 2,
                tok_pos: 274,
                rope_page: 0,
                query_page: 1
            }
        );
    }

    #[test]
    fn prompts_without_pages_are_rejected_not_rotated_in_1d() {
        let no_marker = [BOS, IMG, IMG, 5];
        let err = PageCoords::build(&no_marker).unwrap_err().to_string();
        assert!(err.contains("OQ-V2-6"), "{err}");
        let no_image = [BOS, PG, 5];
        assert!(PageCoords::build(&no_image).is_err());
        // A marker only after the last image is still "no marker before it".
        assert!(PageCoords::build(&[BOS, IMG, PG, 5]).is_err());
    }

    /// A prompt plus a teacher-forced response of at most `N` page answers. The
    /// prompt ends with an instruction, as franken_ocr's always do: that is what
    /// puts the HF decode seed in bucket 0.
    fn random_case(rng: &mut Lcg) -> (Vec<u32>, usize) {
        let n_pages = 1 + rng.below(4) as usize;
        let images: Vec<usize> = (0..n_pages).map(|_| 1 + rng.below(9) as usize).collect();
        let ids = prompt(&images, 1 + rng.below(4) as usize);
        let prompt_len = ids.len();
        let mut ids = ids;
        let answers = rng.below(n_pages as u64 + 1) as usize;
        // Optional text before the first marker, then <PAGE> body per answer.
        ids.extend((0..rng.below(3)).map(|k| 2000 + k as u32));
        for _ in 0..answers {
            ids.push(PG);
            ids.extend((0..rng.below(6)).map(|k| 3000 + k as u32));
        }
        (ids, prompt_len)
    }

    #[test]
    fn decode_state_machine_matches_the_prefill_builder() {
        let mut rng = Lcg(0x5eed_0001);
        for case in 0..500 {
            let (ids, prompt_len) = random_case(&mut rng);
            let full = PageCoords::build(&ids).unwrap();
            let prompt = PageCoords::build(&ids[..prompt_len]).unwrap();
            let mut st = DecodePageState::after_prompt(&prompt, 0);
            for (i, &t) in ids.iter().enumerate().skip(prompt_len) {
                let d = st.feed(t);
                let want = (full.bucket[i], full.tok_pos[i], full.query_page[i]);
                assert_eq!(
                    (d.bucket, d.tok_pos, d.query_page),
                    want,
                    "case {case} row {i}: {ids:?}"
                );
                assert_eq!(d.rope_page, rope_page(full.bucket[i], 0));
            }
        }
    }

    #[test]
    fn coordinates_are_unique_and_parity_follows_the_global_page() {
        let mut rng = Lcg(0x5eed_0002);
        for _ in 0..500 {
            let (ids, _) = random_case(&mut rng);
            let c = PageCoords::build(&ids).unwrap();
            let mut seen = std::collections::HashSet::new();
            for (&b, &t) in c.bucket.iter().zip(&c.tok_pos) {
                assert!(
                    seen.insert((b, t)),
                    "duplicate (bucket, tok_pos) ({b}, {t}) in {ids:?}"
                );
            }
        }
        // Window layouts: a recomputed window starting at global page `lo`
        // (0-based) numbers it local bucket 1, page_base = lo; a reused window's
        // bucket 1 is the tail page lo − 1, page_base = lo − 1. Either way the
        // rope parity of a paged row is that of its global page + 1.
        for _ in 0..1000 {
            let lo = 1 + rng.below(500) as u32;
            let local = 1 + rng.below(12) as u32;
            let recomputed_global = lo + local - 1;
            assert_eq!(rope_page(local, lo), (recomputed_global + 1) % 2);
            let reused_global = lo - 1 + local - 1;
            assert_eq!(rope_page(local, lo - 1), (reused_global + 1) % 2);
            assert_eq!(rope_page(0, lo), 0, "bucket 0 never takes the base");
        }
    }

    #[test]
    fn an_extra_generated_marker_clamps_to_the_last_page() {
        // N = 2; three generated markers. The third clamps to bucket 2 and
        // restarts at pf_cnt[2], duplicating page 2's response-<PAGE>
        // coordinate (DISC-U2-7); the unclamped prefill builder disagrees there.
        let mut ids = prompt(&[3, 5], 2);
        let prompt_len = ids.len();
        ids.extend([PG, 9, PG, 9, PG]);
        let p = PageCoords::build(&ids[..prompt_len]).unwrap();
        let mut st = DecodePageState::after_prompt(&p, 0);
        let coords: Vec<DecodeCoord> = ids[prompt_len..].iter().map(|&t| st.feed(t)).collect();
        assert_eq!((coords[2].bucket, coords[2].tok_pos), (2, 6));
        assert_eq!((coords[4].bucket, coords[4].tok_pos), (2, p.pf_cnt[2]));
        assert_eq!(coords[4].tok_pos, coords[2].tok_pos);
        assert_eq!(
            coords[4].query_page, 2,
            "the query page itself is not clamped (MU:517)"
        );
        let full = PageCoords::build(&ids).unwrap();
        assert_eq!(full.bucket[ids.len() - 1], 3);
    }

    #[test]
    fn reused_window_seed_lands_the_forced_marker_on_page_lo() {
        // Rewritten prompt: tail page lo−1 (4 images) + new pages lo (6) and lo+1 (2).
        let rewritten: Vec<u32> = [4usize, 6, 2]
            .iter()
            .flat_map(|&n| std::iter::once(PG).chain(std::iter::repeat_n(IMG, n)))
            .collect();
        let c = PageCoords::build(&rewritten).unwrap();
        let lo = 7;
        let mut st = DecodePageState::reused_window(&c, lo - 1).unwrap();
        let forced = st.feed(PG);
        assert_eq!(forced.bucket, 2);
        assert_eq!(forced.tok_pos, 1 + 6, "tok_pos = 1 + n_img(lo)");
        assert_eq!(forced.query_page, 1);
        assert_eq!(
            forced.rope_page,
            (lo + 1) % 2,
            "global page lo, parity lo + 1"
        );
        // The HF seed would have clamped the forced marker into bucket M = 3.
        let mut hf = DecodePageState::after_prompt(&c, lo - 1);
        assert_eq!(hf.feed(PG).bucket, 3);
        // A prompt that is not pages-only, or has a single page, is refused.
        assert!(
            DecodePageState::reused_window(&PageCoords::build(&prompt(&[4, 6], 1)).unwrap(), 0)
                .is_err()
        );
        let one_page: Vec<u32> = std::iter::once(PG)
            .chain(std::iter::repeat_n(IMG, 3))
            .collect();
        assert!(DecodePageState::reused_window(&PageCoords::build(&one_page).unwrap(), 0).is_err());
    }

    #[test]
    fn inverse_frequencies_follow_the_resample_rule() {
        // Against an f64 evaluation of 10000^(-i/63): within f32 rounding of the
        // resample rule, and far from v1's 10000^(-2i/128) for every i > 0. The
        // bit-exact check against the oracle's dumped table is U2-C1's L1.
        let inv = inv_tok_freqs();
        assert_eq!(inv[0], 1.0);
        for (i, &f) in inv.iter().enumerate() {
            let resample = 10_000f64.powf(-(i as f64) / 63.0);
            let rel = (f64::from(f) - resample).abs() / resample;
            assert!(
                rel < 4.0 * f64::from(f32::EPSILON),
                "inv_tok[{i}] = {f}, want {resample}"
            );
            if i > 0 {
                let v1 = 10_000f64.powf(-2.0 * i as f64 / 128.0);
                assert!(
                    (f64::from(f) - v1).abs() / v1 > 1e-3,
                    "inv_tok[{i}] matches v1's rule"
                );
            }
        }
        assert!(inv.windows(2).all(|w| w[1] < w[0]));
    }

    #[test]
    fn page2d_rotation_matches_direct_cos_sin() {
        let tok = [0u32, 1, 273, 4096];
        let page = [0u32, 1, 0, 1];
        let table = page2d_rope_table(&tok, &page);
        let inv = inv_tok_freqs();
        let heads = 2;
        let mut rng = Lcg(0x5eed_0003);
        let data: Vec<f32> = (0..tok.len() * heads * HEAD_DIM)
            .map(|_| rng.below(2001) as f32 / 1000.0 - 1.0)
            .collect();
        let mut x = Mat::from_vec(tok.len(), heads * HEAD_DIM, data.clone());
        apply_rope(&mut x, &table).unwrap();
        let half = HEAD_DIM / 2;
        for r in 0..tok.len() {
            for h in 0..heads {
                let base = r * heads * HEAD_DIM + h * HEAD_DIM;
                for i in 0..half {
                    let ang = if i < N_TOK_FREQS {
                        tok[r] as f32 * inv[i]
                    } else {
                        page[r] as f32
                    };
                    let (s, c) = ang.sin_cos();
                    let (a, b) = (data[base + i], data[base + half + i]);
                    let got = (
                        x.row(r)[h * HEAD_DIM + i],
                        x.row(r)[h * HEAD_DIM + half + i],
                    );
                    assert_eq!(got.0.to_bits(), (a * c - b * s).to_bits());
                    assert_eq!(got.1.to_bits(), (b * c + a * s).to_bits());
                }
            }
        }
        // Row 0 (bucket 0, tok 0) is the identity rotation.
        assert_eq!(&x.row(0)[..heads * HEAD_DIM], &data[..heads * HEAD_DIM]);
    }

    // ── Router (U2-B2) ──────────────────────────────────────────────────────

    fn uniform(rng: &mut Lcg, lo: f32, hi: f32) -> f32 {
        lo + (hi - lo) * (rng.below(1_000_001) as f32 / 1_000_000.0)
    }

    fn random_mat(rng: &mut Lcg, rows: usize, cols: usize, scale: f32) -> Mat {
        Mat::from_vec(
            rows,
            cols,
            (0..rows * cols)
                .map(|_| uniform(rng, -scale, scale))
                .collect(),
        )
    }

    /// The full HF window and selection, the reference every variant meets.
    fn full_topm(x: &[f32], cand: &[bool], p: &RouterParams) -> Route {
        select_bias(&entmax15_topm(x, p.window(x.len())), cand, p)
    }

    #[test]
    fn router_constants_follow_the_checkpoint_snapshot() {
        let p = RouterParams::uocr2();
        assert_eq!(p.idx_scale, (128.0f64 / 128f64.sqrt()) as f32);
        assert!((p.idx_scale - 11.313_708).abs() < 1e-5);
        assert_eq!(p.floor, 1e-4f64 as f32);
        assert_eq!(p.inv_sigma, 1.0);
        assert_eq!((p.budget, p.topm), (12, 16));
        assert_eq!(inv_sqrt_d(), (1.0f64 / 128f64.sqrt()) as f32);
        assert_eq!(
            [p.window(1), p.window(9), p.window(16), p.window(1000)],
            [1, 9, 16, 16]
        );
        assert_eq!(
            [p.read_slots(1), p.read_slots(9), p.read_slots(16)],
            [1, 9, 12]
        );
        let bad = SrSwaLayerWeights::new(8, vec![0.0; 3], vec![], vec![], vec![], vec![]);
        assert!(bad.unwrap_err().to_string().contains("q_proj"));
    }

    #[test]
    fn gemma_norm_matches_the_formula_with_clamps_and_non_finite_inputs() {
        let mut rng = Lcg(0x5eed_0b02);
        let w: Vec<f32> = (0..HEAD_DIM)
            .map(|_| uniform(&mut rng, -0.5, 0.5))
            .collect();
        let x: Vec<f32> = (0..HEAD_DIM)
            .map(|_| uniform(&mut rng, -3.0, 3.0))
            .collect();
        let mut y = x.clone();
        gemma_rms_norm(&mut y, &w);
        let ms = x.iter().map(|&v| f64::from(v).powi(2)).sum::<f64>() / HEAD_DIM as f64;
        for i in 0..HEAD_DIM {
            let want = f64::from(x[i]) / (ms + 1e-6).sqrt() * (1.0 + f64::from(w[i]));
            assert!(
                (f64::from(y[i]) - want).abs() < 1e-5,
                "{i}: {} vs {want}",
                y[i]
            );
        }
        // NaN and ±inf count as 0, in the mean as well.
        let (mut bad, mut zeroed) = (x.clone(), x.clone());
        for (i, v) in [(3, f32::NAN), (7, f32::INFINITY), (9, f32::NEG_INFINITY)] {
            bad[i] = v;
            zeroed[i] = 0.0;
        }
        gemma_rms_norm(&mut bad, &w);
        gemma_rms_norm(&mut zeroed, &w);
        assert_eq!(bad, zeroed);
        // (1 + w) can push past ±1e4, and the output clamps there.
        let mut big = vec![1.0f32; HEAD_DIM];
        big[0] = -1.0;
        gemma_rms_norm(&mut big, &[2.0e4; HEAD_DIM]);
        assert_eq!((big[0], big[1]), (-1.0e4, 1.0e4));
        // A zero row stays zero: eps keeps the rsqrt finite.
        let mut zero = vec![0.0f32; HEAD_DIM];
        gemma_rms_norm(&mut zero, &w);
        assert!(zero.iter().all(|&v| v == 0.0));
    }

    #[test]
    fn projections_compose_linear_norm_and_page2d_rope() {
        let (hidden, rows) = (48, 3);
        let mut rng = Lcg(0x5eed_0b03);
        let mut v = |n: usize, s: f32| (0..n).map(|_| uniform(&mut rng, -s, s)).collect();
        let w = SrSwaLayerWeights::new(
            hidden,
            v(HEAD_DIM * hidden, 0.2),
            v(HEAD_DIM * hidden, 0.2),
            v(HEAD_DIM, 0.3),
            v(HEAD_DIM, 0.3),
            v(HEAD_DIM, 0.5),
        )
        .unwrap();
        let mut h: Vec<f32> = v(rows * hidden, 1.0);
        h[5] = f32::NAN;
        h[hidden + 2] = f32::INFINITY;
        let rope = page2d_rope_table(&[0, 5, 300], &[0, 1, 0]);
        let (iq, ik) = project(&Mat::from_vec(rows, hidden, h.clone()), &w, &rope).unwrap();
        for (proj, norm, got) in [(&w.q_proj, &w.q_norm, &iq), (&w.k_proj, &w.k_norm, &ik)] {
            let mut want = Mat::zeros(rows, HEAD_DIM);
            for r in 0..rows {
                let x: Vec<f32> = h[r * hidden..(r + 1) * hidden]
                    .iter()
                    .map(|&v| finite_or_zero(v))
                    .collect();
                for o in 0..HEAD_DIM {
                    want.row_mut(r)[o] = dot(&proj[o * hidden..(o + 1) * hidden], &x);
                }
                gemma_rms_norm(want.row_mut(r), norm);
            }
            apply_rope(&mut want, &rope).unwrap();
            assert_eq!(got.shape(), (rows, HEAD_DIM));
            for (a, b) in got.data.iter().zip(&want.data) {
                assert!((a - b).abs() < 1e-4, "{a} vs {b}");
            }
        }
        assert!(project(&Mat::zeros(rows, hidden + 1), &w, &rope).is_err());
        // The cached transposes are the per-call linear's, bit for bit.
        let hm = random_mat(&mut rng, 37, hidden, 1.0);
        for (proj, t) in [(&w.q_proj, &w.q_proj_t), (&w.k_proj, &w.k_proj_t)] {
            let linear =
                super::super::decoder::linear_no_bias(&hm, proj, hidden, HEAD_DIM).unwrap();
            assert_eq!(nn::matmul(&hm, t).unwrap(), linear);
        }
    }

    #[test]
    fn summaries_and_scores_follow_the_reference_formulas() {
        let mut rng = Lcg(0x5eed_0b04);
        let rows = 9 * BLOCK_SIZE + 5; // 10 blocks, the last one partial
        let ik = random_mat(&mut rng, rows, HEAD_DIM, 1.0);
        // A zero head_cls is a mean pool.
        let mean = summaries(&ik, &[0.0; HEAD_DIM]);
        assert_eq!(mean.nblk(), 10);
        assert!(mean.valid.iter().all(|&v| v));
        // A learned head_cls weights rows by softmax(ik·hc / √128).
        let hc: Vec<f32> = (0..HEAD_DIM)
            .map(|_| uniform(&mut rng, -2.0, 2.0))
            .collect();
        let sums = summaries(&ik, &hc);
        for c in 0..10 {
            let block = c * BLOCK_SIZE..((c + 1) * BLOCK_SIZE).min(rows);
            let n = block.len() as f64;
            let a: Vec<f64> = block
                .clone()
                .map(|j| f64::from(dot(ik.row(j), &hc)) / 128f64.sqrt())
                .collect();
            let max = a.iter().copied().fold(f64::NEG_INFINITY, f64::max);
            let e: Vec<f64> = a.iter().map(|&v| (v - max).exp()).collect();
            let z: f64 = e.iter().sum();
            for d in 0..HEAD_DIM {
                let pooled = block.clone().map(|j| f64::from(ik.get(j, d))).sum::<f64>() / n;
                assert!((f64::from(mean.kbar(c)[d]) - pooled).abs() < 1e-5);
                let soft: f64 = block
                    .clone()
                    .zip(&e)
                    .map(|(j, &ej)| ej / z * f64::from(ik.get(j, d)))
                    .sum();
                assert!((f64::from(sums.kbar(c)[d]) - soft).abs() < 1e-5);
            }
        }
        // The last row's query (block 9) reads blocks 0..=5 remotely.
        let iq: Vec<f32> = (0..HEAD_DIM)
            .map(|_| uniform(&mut rng, -1.0, 1.0))
            .collect();
        let sc = scores(&iq, rows - 1, &sums);
        for (c, &s) in sc.iter().enumerate() {
            if c <= 5 {
                let want = f64::from(dot(&iq, sums.kbar(c))) / 128f64.sqrt();
                assert!((f64::from(s) - want).abs() < 1e-5, "block {c}");
            } else {
                assert_eq!(s, SCORE_NEG, "block {c} is local or future");
            }
        }
        // Nothing is remote before row 128; an invalid block never is; big
        // products clamp at ±1e4.
        assert!(scores(&iq, 127, &sums).iter().all(|&s| s == SCORE_NEG));
        let mut holed = sums.clone();
        holed.valid[2] = false;
        assert_eq!(scores(&iq, rows - 1, &holed)[2], SCORE_NEG);
        let huge: Vec<f32> = sums.kbar(0).iter().map(|&k| k * 1e6).collect();
        assert_eq!(scores(&huge, rows - 1, &sums)[0], 1.0e4);
    }

    /// 1.5-entmax in f64 by bisection on `Σ max(z − τ, 0)² = 1`.
    fn entmax15_reference(x: &[f32]) -> Vec<f64> {
        let max = f64::from(x.iter().copied().fold(f32::NEG_INFINITY, f32::max));
        let z: Vec<f64> = x.iter().map(|&v| (f64::from(v) - max) / 2.0).collect();
        let (mut lo, mut hi) = (-1.0f64, 0.0f64);
        for _ in 0..200 {
            let mid = (lo + hi) / 2.0;
            let s: f64 = z.iter().map(|&v| (v - mid).max(0.0).powi(2)).sum();
            if s > 1.0 {
                lo = mid;
            } else {
                hi = mid;
            }
        }
        let tau = (lo + hi) / 2.0;
        z.iter().map(|&v| (v - tau).max(0.0).powi(2)).collect()
    }

    #[test]
    fn entmax15_is_a_sparse_distribution_matching_a_brute_force_solve() {
        let mut rng = Lcg(0x5eed_0b05);
        for case in 0..2000 {
            let n = 1 + rng.below(40) as usize;
            let spread = [0.5f32, 5.0, 50.0, 2.0e4][rng.below(4) as usize];
            let x: Vec<f32> = (0..n).map(|_| uniform(&mut rng, -spread, spread)).collect();
            let (p, support) = entmax15(&x);
            assert!((1..=n).contains(&support), "case {case}");
            let sum: f64 = p.iter().map(|&v| f64::from(v)).sum();
            assert!((sum - 1.0).abs() < 1e-4, "case {case}: sum {sum}");
            for (a, b) in p.iter().zip(entmax15_reference(&x)) {
                assert!((f64::from(*a) - b).abs() < 1e-4, "case {case}: {a} vs {b}");
            }
            // Raising one input never lowers its probability.
            let i = rng.below(n as u64) as usize;
            let mut up = x.clone();
            up[i] += uniform(&mut rng, 0.0, spread);
            assert!(entmax15(&up).0[i] + 1e-5 >= p[i], "case {case}: monotone");
            // Shift invariance (shifts small enough that x + s rounds exactly
            // enough to compare).
            if spread <= 50.0 {
                let s = rng.below(129) as f32 - 64.0;
                let shifted: Vec<f32> = x.iter().map(|&v| v + s).collect();
                for (a, b) in p.iter().zip(entmax15(&shifted).0) {
                    assert!((a - b).abs() < 1e-4, "case {case}: shift {s}");
                }
            }
        }
        assert_eq!(entmax15(&[3.0]), (vec![1.0], 1));
        let (p, support) = entmax15(&[ENTMAX_NEG; 16]);
        assert_eq!(support, 16);
        assert!(p.iter().all(|&v| v == 1.0 / 16.0));
    }

    #[test]
    fn top_m_slots_break_ties_by_lower_block() {
        let n = ENTMAX_NEG;
        let top = entmax15_topm(&[n, 5.0, n, 5.0, 7.0, n, n], 5);
        let order: Vec<u32> = top.slots.iter().map(|s| s.1).collect();
        assert_eq!(order, [4, 1, 3, 0, 2]);
        // The partial selection agrees with a full sort, duplicates included.
        let mut rng = Lcg(0x5eed_0b06);
        for _ in 0..300 {
            let nblk = 1 + rng.below(300) as usize;
            let x: Vec<f32> = (0..nblk).map(|_| rng.below(7) as f32 - 3.0).collect();
            let mut want: Vec<(f32, u32)> = x.iter().zip(0u32..).map(|(&v, c)| (v, c)).collect();
            want.sort_by(slot_order);
            want.truncate(16);
            assert_eq!(entmax15_topm(&x, 16).slots, want);
        }
    }

    #[test]
    fn page_gates_follow_the_reference_cases() {
        // Decode: blocks 0-1 hold page 0, block 2 straddles pages 0|1, block 3
        // page 1, block 4 page 2, block 5 is prompt text; blocks 6-7 are
        // generated text past the prompt's blocks.
        let (bmin, bmax) = ([0, 0, 0, 1, 2, -1], [0, 0, 1, 1, 2, -1]);
        let f = false;
        assert_eq!(
            page_gate_decode(&bmin, &bmax, 0, 8),
            [true, true, true, f, f, f, f, f]
        );
        assert_eq!(
            page_gate_decode(&bmin, &bmax, 1, 8),
            [f, f, true, true, f, f, f, f]
        );
        // Open rows read everything, generated text included: qp < 0, and a
        // page no block holds (an answer past the prompt's pages).
        assert_eq!(page_gate_decode(&bmin, &bmax, -1, 8), vec![true; 8]);
        assert_eq!(page_gate_decode(&bmin, &bmax, 3, 8), vec![true; 8]);

        // Prefill tests membership, not the [bmin, bmax] range: block 1 holds
        // pages 0 and 2 only (a contrived layout) and stays closed to page 1.
        let mut img_page = vec![0i32; BLOCK_SIZE];
        img_page.extend((0..BLOCK_SIZE).map(|j| if j < 16 { 0 } else { 2 }));
        img_page.extend(std::iter::repeat_n(1, BLOCK_SIZE));
        img_page.extend(std::iter::repeat_n(-1, 7));
        let blocks = BlockPages::build(&img_page);
        assert_eq!(
            (blocks.bmin.clone(), blocks.bmax.clone()),
            (vec![0, 0, 1, -1], vec![0, 2, 1, -1])
        );
        assert_eq!(page_gate_prefill(&blocks, 1), [f, f, true, f]);
        assert_eq!(page_gate_prefill(&blocks, 2), [f, true, f, f]);
        assert_eq!(page_gate_prefill(&blocks, -1), [true; 4]);
        assert_eq!(page_gate_prefill(&blocks, 7), [true; 4]);

        // Allowed blocks that are not remote fall back to every remote block.
        let sc = [3.0, SCORE_NEG, 1.0, SCORE_NEG];
        assert_eq!(
            candidates(&sc, Some(&[f, true, f, true])),
            [true, f, true, f]
        );
        assert_eq!(candidates(&sc, Some(&[f, f, true, f])), [f, f, true, f]);
        // No remote block: no candidate, and the route reads nothing remote.
        let none = route(
            &[SCORE_NEG; 4],
            None,
            &RouterParams::uocr2(),
            EntmaxDomain::TopM,
        );
        assert!(none.blocks.is_empty() && none.bias.is_empty());
    }

    /// Random entmax inputs over `nblk` blocks in the regimes the padding
    /// argument has to cover. Non-candidates sit at −1e4.
    fn random_inputs(rng: &mut Lcg, nblk: usize, max_cands: usize) -> (Vec<f32>, Vec<bool>) {
        let mut cand = vec![false; nblk];
        let n_cand = 1 + rng.below(max_cands.min(nblk) as u64) as usize;
        for _ in 0..n_cand {
            cand[rng.below(nblk as u64) as usize] = true;
        }
        let regime = rng.below(5);
        let x = cand
            .iter()
            .map(|&c| {
                if !c {
                    return ENTMAX_NEG;
                }
                match regime {
                    0 => uniform(rng, -50.0, 50.0),
                    1 => uniform(rng, -1.13e5, 1.13e5),
                    // Every candidate below −1e4 + 2: pads may enter the support.
                    2 => uniform(rng, -1.2e4, -1.0e4 + 2.0),
                    // Ties with the pads at exactly −1e4.
                    3 if rng.below(2) == 0 => ENTMAX_NEG,
                    _ => rng.below(5) as f32 - 1.0e4 + 1.0,
                }
            })
            .collect();
        (x, cand)
    }

    #[test]
    fn candidate_only_top_m_with_merged_pads_equals_the_full_window() {
        let mut rng = Lcg(0x5eed_0b07);
        let p = RouterParams::uocr2();
        let mut pads_in_support = 0;
        for case in 0..3000 {
            let nblk = 1 + rng.below(64) as usize;
            let (x, cand) = random_inputs(&mut rng, nblk, 24);
            let full = full_topm(&x, &cand, &p);
            let cands: Vec<(u32, f32)> = (0u32..)
                .zip(&x)
                .zip(&cand)
                .filter(|&(_, &c)| c)
                .map(|((b, &v), _)| (b, v))
                .collect();
            assert_eq!(
                route_topm_candidates(&cands, nblk, &p),
                full,
                "case {case}: {x:?}"
            );
            let top = entmax15_topm(&x, p.window(nblk));
            pads_in_support += usize::from(
                top.slots
                    .iter()
                    .zip(&top.prior)
                    .any(|(&(_, b), &pr)| !cand[b as usize] && pr > 0.0),
            );
        }
        assert!(
            pads_in_support > 0,
            "the pads-in-support regime was exercised"
        );
    }

    #[test]
    fn all_candidates_equals_top_m_under_its_preconditions() {
        // ≤ 16 candidates, distinct inputs (no tie at the 12th place), the best
        // at least −1e4 + 2 and none at or below −1e4.
        let mut rng = Lcg(0x5eed_0b08);
        let p = RouterParams::uocr2();
        for case in 0..2000 {
            let nblk = 1 + rng.below(80) as usize;
            let mut cand = vec![false; nblk];
            let n_cand = 1 + rng.below(16.min(nblk) as u64) as usize;
            for _ in 0..n_cand {
                cand[rng.below(nblk as u64) as usize] = true;
            }
            let spread = [1.0f32, 30.0, 9000.0][rng.below(3) as usize];
            let x: Vec<f32> = cand
                .iter()
                .zip(0u32..)
                .map(|(&c, b)| {
                    if c {
                        uniform(&mut rng, -spread, spread) + b as f32 * 1e-3
                    } else {
                        ENTMAX_NEG
                    }
                })
                .collect();
            let (all, top) = (entmax15_all(&x, &cand, &p), full_topm(&x, &cand, &p));
            assert_eq!(all.blocks, top.blocks, "case {case}");
            for (a, b) in all.bias.iter().zip(&top.bias) {
                assert!((a - b).abs() < 1e-5, "case {case}: {a} vs {b}");
            }
        }
        // Past 16 candidates the two differ by design once the support is
        // wider than the window: with 20 near-equal candidates AllCandidates
        // spreads the entmax over all 20 and TopM over its 16, so the biases
        // differ, while both still read the top 12.
        let x: Vec<f32> = (0..20).map(|i| 0.01 * i as f32).collect();
        let cand = vec![true; 20];
        let all = entmax15_all(&x, &cand, &p);
        assert_eq!(all.blocks, (8..20).collect::<Vec<u32>>());
        let top = full_topm(&x, &cand, &p);
        assert_eq!(top.blocks, all.blocks);
        assert_eq!((all.support, top.support), (20, 16));
        assert!(
            all.bias
                .iter()
                .zip(&top.bias)
                .any(|(a, b)| (a - b).abs() > 1e-4),
            "{:?} vs {:?}",
            all.bias,
            top.bias
        );
    }

    #[test]
    fn zero_prior_candidates_in_the_read_slots_are_still_read() {
        // One dominant candidate takes all the mass; the other five have prior
        // 0 but sit in the first K' slots, so they are read at ln(floor) − μ.
        let p = RouterParams::uocr2();
        let mut x = vec![ENTMAX_NEG; 20];
        let cand: Vec<bool> = (0..20).map(|b| b < 6).collect();
        x[0] = 1000.0;
        x[1..6].fill(0.0);
        for r in [full_topm(&x, &cand, &p), entmax15_all(&x, &cand, &p)] {
            assert_eq!(r.blocks, [0, 1, 2, 3, 4, 5]);
            let floor = p.floor.ln();
            let mu = (0.0 + 5.0 * floor) / 6.0;
            assert!((r.bias[0] - (0.0 - mu)).abs() < 1e-5);
            for &b in &r.bias[1..] {
                assert!((b - (floor - mu)).abs() < 1e-5);
            }
        }
    }

    #[test]
    fn prefill_window_is_set_by_the_forward_not_the_row() {
        // 600 rows → nblk 19 → M = 16 for every row. Row 300 sits in block 9
        // and has 6 remote blocks, all candidates (its query page is −1), and
        // every candidate input is ≈ −12800 < −1e4. In the forward's window
        // the 13 non-candidates (at −1e4) fill the first 12 slots, so nothing
        // is read. A per-row window over its 10 causal blocks would read all 6.
        let mut ids = vec![BOS, PG];
        ids.extend(std::iter::repeat_n(IMG, 100));
        ids.extend((0..498).map(|k| 1000 + k as u32));
        let coords = PageCoords::build(&ids).unwrap();
        let v = [10.0f32; HEAD_DIM];
        let ik = Mat::from_vec(600, HEAD_DIM, v.repeat(600));
        let iq = Mat::from_vec(600, HEAD_DIM, v.map(|e| -e).repeat(600));
        let p = RouterParams::uocr2();
        let zero = [0.0f32; HEAD_DIM];
        let routes = prefill_routes(&iq, &ik, &coords, &zero, &p, EntmaxDomain::TopM).unwrap();
        assert_eq!(routes.len(), 600);
        assert!(routes[300].blocks.is_empty(), "{:?}", routes[300]);
        let sc = scores(iq.row(300), 300, &summaries(&ik, &zero));
        assert_eq!(sc.len(), 19);
        let x = entmax_inputs(&sc, &candidates(&sc, None), &p);
        assert!(x[..6].iter().all(|&v| v < -1.0e4), "{x:?}");
        let per_row = route(&sc[..10], None, &p, EntmaxDomain::TopM);
        assert_eq!(per_row.blocks, [0, 1, 2, 3, 4, 5]);
        // Every row's window spans the forward (rows before 128 read nothing).
        assert!(routes[..128].iter().all(|r| r.blocks.is_empty()));
        assert!(
            prefill_routes(
                &iq,
                &ik,
                &PageCoords::build(&ids[..599]).unwrap(),
                &zero,
                &p,
                EntmaxDomain::TopM
            )
            .is_err()
        );
    }

    #[test]
    fn single_page_base_reads_every_image_block_of_its_page() {
        // BOS + <PAGE> + 273 images + 8 instruction rows, then a decode query
        // at row 700 answering page 0. The images fill blocks 0..=8, all
        // remote and all allowed, and 9 ≤ K' = 12: every one is read, with
        // biases that differ.
        let mut ids = vec![BOS, PG];
        ids.extend(std::iter::repeat_n(IMG, 273));
        ids.extend((0..8).map(|k| 1000 + k as u32));
        let coords = PageCoords::build(&ids).unwrap();
        let mut img_page = coords.img_page.clone();
        img_page.resize(701, -1);
        let blocks = BlockPages::build(&img_page);
        let mut rng = Lcg(0x5eed_0b09);
        let ik = random_mat(&mut rng, 701, HEAD_DIM, 1.0);
        let hc: Vec<f32> = (0..HEAD_DIM)
            .map(|_| uniform(&mut rng, -1.0, 1.0))
            .collect();
        let iq: Vec<f32> = (0..HEAD_DIM)
            .map(|_| uniform(&mut rng, -1.0, 1.0))
            .collect();
        let sums = summaries(&ik, &hc);
        let sc = scores(&iq, 700, &sums);
        let allow = page_gate_decode(&blocks.bmin, &blocks.bmax, 0, sums.nblk());
        assert_eq!(allow.iter().filter(|&&a| a).count(), 9);
        let p = RouterParams::uocr2();
        for domain in [EntmaxDomain::TopM, EntmaxDomain::AllCandidates] {
            let r = route(&sc, Some(&allow), &p, domain);
            assert_eq!(r.blocks, (0..9).collect::<Vec<u32>>(), "{domain:?}");
            assert!(
                r.bias.iter().any(|&b| (b - r.bias[0]).abs() > 1e-3),
                "{domain:?}"
            );
        }
    }

    /// D21: `FOCR_SRSWA_ENTMAX` names the domain; anything else is refused
    /// with the accepted values, never read as the default.
    #[test]
    fn the_entmax_domain_env_names_a_domain_or_is_refused() {
        for (value, domain) in [
            (None, EntmaxDomain::TopM),
            (Some(""), EntmaxDomain::TopM),
            (Some("topm"), EntmaxDomain::TopM),
            (Some(" TopM "), EntmaxDomain::TopM),
            (Some("all"), EntmaxDomain::AllCandidates),
            (Some("ALL"), EntmaxDomain::AllCandidates),
        ] {
            assert_eq!(entmax_domain_for(value), Ok(domain), "{value:?}");
        }
        for value in ["1", "top-m", "sglang", "all candidates"] {
            let why = entmax_domain_for(Some(value)).expect_err(value);
            assert!(
                why.contains(ENTMAX_ENV) && why.contains("`topm`") && why.contains("`all`"),
                "{why}"
            );
        }
    }

    // ── Attention (U2-B3) ───────────────────────────────────────────────────

    fn random_kv(rng: &mut Lcg, rows: usize) -> (Mat, Mat, HeadMajorKv) {
        let k = random_mat(rng, rows, NUM_HEADS * HEAD_DIM, 1.0);
        let v = random_mat(rng, rows, NUM_HEADS * HEAD_DIM, 1.0);
        let kv = HeadMajorKv::from_rows(&k, &v).unwrap();
        (k, v, kv)
    }

    /// A random selection over blocks `0..nblk`: any blocks at all (future,
    /// local, partially local), biases in ±3.
    fn random_selection(rng: &mut Lcg, nblk: usize) -> BlockSelection {
        let mut blocks: Vec<u32> = (0..rng.below(14))
            .map(|_| rng.below(nblk as u64) as u32)
            .collect();
        blocks.sort_unstable();
        blocks.dedup();
        let bias = blocks.iter().map(|_| uniform(rng, -3.0, 3.0)).collect();
        BlockSelection::new(blocks, bias).unwrap()
    }

    fn assert_bitwise(a: &Mat, b: &Mat, what: &str) {
        assert_eq!(a.shape(), b.shape(), "{what}");
        for (i, (x, y)) in a.data.iter().zip(&b.data).enumerate() {
            assert_eq!(x.to_bits(), y.to_bits(), "{what}: element {i}: {x} vs {y}");
        }
    }

    /// The attention formula evaluated directly in f64 from per-row K/V.
    fn attention_f64(
        q: &[f32],
        q_pos: usize,
        sel: &BlockSelection,
        k: &Mat,
        v: &Mat,
        h: usize,
    ) -> Vec<f64> {
        let head = |m: &Mat, j: usize| m.row(j)[h * HEAD_DIM..(h + 1) * HEAD_DIM].to_vec();
        let qh = &q[h * HEAD_DIM..(h + 1) * HEAD_DIM];
        let mut logits = Vec::new();
        for j in 0..=q_pos {
            let local = j + LOCAL_WINDOW > q_pos;
            let bias = sel
                .blocks()
                .iter()
                .position(|&b| b as usize == j / BLOCK_SIZE)
                .map(|i| f64::from(sel.bias()[i]));
            if local || bias.is_some() {
                let d: f64 = qh
                    .iter()
                    .zip(head(k, j))
                    .map(|(&a, b)| f64::from(a) * f64::from(b))
                    .sum();
                let b = if local { 0.0 } else { bias.unwrap_or(0.0) };
                logits.push((j, d / 128f64.sqrt() + b));
            }
        }
        let max = logits
            .iter()
            .map(|&(_, l)| l)
            .fold(f64::NEG_INFINITY, f64::max);
        let z: f64 = logits.iter().map(|&(_, l)| (l - max).exp()).sum();
        let mut out = vec![0.0f64; HEAD_DIM];
        for &(j, l) in &logits {
            let p = (l - max).exp() / z;
            for (o, x) in out.iter_mut().zip(head(v, j)) {
                *o += p * f64::from(x);
            }
        }
        out
    }

    #[test]
    fn gathered_attention_equals_the_dense_oracle_bit_for_bit() {
        let mut rng = Lcg(0x5eed_0b31);
        for case in 0..40 {
            let rows = 1 + rng.below(420) as usize;
            let (k, v, kv) = random_kv(&mut rng, rows);
            let n_q = 1 + rng.below(6) as usize;
            let mut q_pos: Vec<usize> = (0..n_q).map(|_| rng.below(rows as u64) as usize).collect();
            // Always cover the first rows (no remote block) and the last.
            q_pos[0] = rng.below(rows.min(BLOCK_SIZE * LOCAL_BLOCKS) as u64) as usize;
            *q_pos.last_mut().unwrap() = rows - 1;
            let sel: Vec<BlockSelection> = (0..n_q)
                .map(|i| {
                    if i == 1 {
                        BlockSelection::default()
                    } else {
                        random_selection(&mut rng, rows.div_ceil(BLOCK_SIZE) + 1)
                    }
                })
                .collect();
            let q = random_mat(&mut rng, n_q, NUM_HEADS * HEAD_DIM, 1.0);
            let gathered = attend(&q, &q_pos, &sel, &kv).unwrap();
            let dense = attend_dense(&q, &q_pos, &sel, &kv).unwrap();
            assert_bitwise(&gathered, &dense, &format!("case {case}"));
            // And both evaluate the formula itself.
            for r in 0..n_q {
                for h in [0, NUM_HEADS - 1] {
                    let want = attention_f64(q.row(r), q_pos[r], &sel[r], &k, &v, h);
                    let got = &gathered.row(r)[h * HEAD_DIM..(h + 1) * HEAD_DIM];
                    for (a, b) in got.iter().zip(&want) {
                        assert!(
                            (f64::from(*a) - b).abs() < 1e-5,
                            "case {case} row {r} head {h}"
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn local_rows_win_over_a_selected_block() {
        let mut rng = Lcg(0x5eed_0b32);
        let rows = 12 * BLOCK_SIZE;
        let (_, v, kv) = random_kv(&mut rng, rows);
        let q = random_mat(&mut rng, 1, NUM_HEADS * HEAD_DIM, 1.0);
        let none = BlockSelection::default();
        let one = |b: u32| BlockSelection::new(vec![b], vec![2.5]).unwrap();
        let run = |pos: usize, s: &BlockSelection| {
            attend(&q, &[pos], std::slice::from_ref(s), &kv).unwrap()
        };

        // q = 0 reads only itself.
        let first = run(0, &one(0));
        for h in 0..NUM_HEADS {
            let want = &v.row(0)[h * HEAD_DIM..(h + 1) * HEAD_DIM];
            assert_eq!(&first.row(0)[h * HEAD_DIM..(h + 1) * HEAD_DIM], want);
        }
        // q < 128: block 0 is wholly local, so selecting it changes nothing.
        assert_bitwise(&run(100, &one(0)), &run(100, &none), "q < 128");
        // q = 287 (block 8, last row): the window is rows 160..=287. Block 5
        // (160..192) is wholly local; block 4 (128..160) is just outside it.
        assert_eq!(
            attended_runs(287, &one(4))[0],
            Run {
                block: 4,
                from: 0,
                to: 32,
                bias: 2.5
            }
        );
        assert_bitwise(
            &run(287, &one(5)),
            &run(287, &none),
            "block on the boundary",
        );
        assert_ne!(run(287, &one(4)), run(287, &none));
        // q = 288: the window starts at 161, so block 5 is partially local and
        // only its row 160 takes the bias.
        assert_eq!(
            attended_runs(288, &one(5))[0],
            Run {
                block: 5,
                from: 0,
                to: 1,
                bias: 2.5
            }
        );
        assert_ne!(run(288, &one(5)), run(288, &none));
        // A future block is never read.
        assert_bitwise(&run(287, &one(11)), &run(287, &none), "future block");
    }

    #[test]
    fn prefill_attention_is_thread_count_invariant() {
        let mut rng = Lcg(0x5eed_0b33);
        let rows = 300;
        let (_, _, kv) = random_kv(&mut rng, rows);
        let q = random_mat(&mut rng, rows, NUM_HEADS * HEAD_DIM, 1.0);
        let q_pos: Vec<usize> = (0..rows).collect();
        let sel: Vec<BlockSelection> = (0..rows)
            .map(|_| random_selection(&mut rng, rows.div_ceil(BLOCK_SIZE)))
            .collect();
        let at = |threads: usize| {
            rayon::ThreadPoolBuilder::new()
                .num_threads(threads)
                .build()
                .unwrap()
                .install(|| attend(&q, &q_pos, &sel, &kv).unwrap())
        };
        let one = at(1);
        assert_bitwise(&at(2), &one, "2 threads");
        let n = std::thread::available_parallelism().map_or(4, |n| n.get());
        assert_bitwise(&at(n), &one, "N threads");
    }

    #[test]
    fn attention_inputs_are_validated() {
        let mut rng = Lcg(0x5eed_0b34);
        let (k, _, kv) = random_kv(&mut rng, 40);
        let q = random_mat(&mut rng, 1, NUM_HEADS * HEAD_DIM, 1.0);
        let none = [BlockSelection::default()];
        assert!(
            attend(&q, &[40], &none, &kv).is_err(),
            "position past the rows"
        );
        assert!(
            attend(&q, &[1, 2], &none, &kv).is_err(),
            "positions per row"
        );
        assert!(attend_dense(&Mat::zeros(1, 7), &[0], &none, &kv).is_err());
        assert!(HeadMajorKv::from_rows(&k, &Mat::zeros(39, NUM_HEADS * HEAD_DIM)).is_err());
        assert!(BlockSelection::new(vec![3, 2], vec![0.0, 0.0]).is_err());
        assert!(BlockSelection::new(vec![2, 2], vec![0.0, 0.0]).is_err());
        assert!(BlockSelection::new(vec![2], vec![]).is_err());
        let route = Route {
            blocks: vec![1, 4],
            bias: vec![0.5, -0.5],
            support: 2,
        };
        let sel = BlockSelection::from(route);
        assert_eq!(
            (sel.blocks(), sel.bias()),
            (&[1u32, 4][..], &[0.5f32, -0.5][..])
        );
    }

    // ── U2-B4: layer cache and block retirement ──

    const EOS: u32 = special::EOS;
    const TEXT: u32 = 1000;

    fn random_rows(rng: &mut Lcg, n: usize) -> (Mat, Mat, Mat) {
        let k = random_mat(rng, n, NUM_HEADS * HEAD_DIM, 1.0);
        let v = random_mat(rng, n, NUM_HEADS * HEAD_DIM, 1.0);
        let ik = random_mat(rng, n, HEAD_DIM, 1.0);
        (k, v, ik)
    }

    fn random_head_cls(rng: &mut Lcg) -> Vec<f32> {
        (0..HEAD_DIM).map(|_| uniform(rng, -1.0, 1.0)).collect()
    }

    fn assert_same_summaries(a: &BlockSummaries, b: &BlockSummaries, what: &str) {
        assert_eq!(a.valid, b.valid, "{what}: valid");
        let bits = |s: &BlockSummaries| s.kbar.iter().map(|x| x.to_bits()).collect::<Vec<_>>();
        assert_eq!(bits(a), bits(b), "{what}: kbar");
    }

    #[test]
    fn incremental_summaries_equal_a_recompute_bit_for_bit() {
        let mut rng = Lcg(0x5eed_0b40);
        for case in 0..6 {
            let head_cls = if case == 0 {
                vec![0.0; HEAD_DIM]
            } else {
                random_head_cls(&mut rng)
            };
            let mut cache = SrSwaLayerCache::new(&head_cls, true).unwrap();
            let (mut all_k, mut all_v, mut all_ik) = (Vec::new(), Vec::new(), Vec::new());
            while cache.rows < 420 {
                // Prefill-sized and decode-sized appends, some ending mid-block.
                let n = if rng.below(3) == 0 {
                    1
                } else {
                    1 + rng.below(70) as usize
                };
                let (k, v, mut ik) = random_rows(&mut rng, n);
                if rng.below(8) == 0 {
                    let at = rng.below(ik.data.len() as u64) as usize;
                    ik.data[at] = f32::INFINITY;
                }
                cache.append(&k, &v, &ik, &vec![-1; n]).unwrap();
                all_k.extend_from_slice(&k.data);
                all_v.extend_from_slice(&v.data);
                all_ik.extend_from_slice(&ik.data);
                let rows = cache.rows;
                let ik_all = Mat::from_vec(rows, HEAD_DIM, all_ik.clone());
                assert_same_summaries(
                    cache.summaries(),
                    &summaries(&ik_all, &head_cls),
                    &format!("case {case} at {rows} rows"),
                );
            }
            // The BlockKv view is the head-major layout of the same rows.
            let rows = cache.rows;
            let width = NUM_HEADS * HEAD_DIM;
            let reference = HeadMajorKv::from_rows(
                &Mat::from_vec(rows, width, all_k),
                &Mat::from_vec(rows, width, all_v),
            )
            .unwrap();
            for b in 0..cache.nblk() {
                for h in 0..NUM_HEADS {
                    assert_eq!(cache.keys(h, b), reference.keys(h, b), "keys {h} {b}");
                    assert_eq!(cache.values(h, b), reference.values(h, b), "values {h} {b}");
                }
            }
        }
    }

    #[test]
    fn block_pages_of_a_three_page_base_prompt_hand_computed() {
        // BOS 0; page p: marker 1 + 274p, images 2 + 274p ..= 274 + 274p;
        // instruction 823..=826. 827 rows, 26 blocks.
        let ids = prompt(&[273, 273, 273], 4);
        let coords = PageCoords::build(&ids).unwrap();
        let mut want = vec![(0, 0); 26];
        want[8] = (0, 1); // rows 256..=274 page 0, marker 275, 276..=287 page 1
        for w in &mut want[9..=16] {
            *w = (1, 1);
        }
        want[17] = (1, 2); // rows 544..=548 page 1, marker 549, 550..=575 page 2
        for w in &mut want[18..=25] {
            *w = (2, 2); // block 25 also holds the instruction
        }
        let mut rng = Lcg(0x5eed_0b41);
        let mut whole = SrSwaLayerCache::new(&random_head_cls(&mut rng), true).unwrap();
        let mut chunked = whole.clone();
        let (k, v, ik) = random_rows(&mut rng, ids.len());
        whole.append(&k, &v, &ik, &coords.img_page).unwrap();
        // The same prompt in uneven chunks gives the same pages.
        let mut at = 0;
        for n in [5, 300, 27, 495] {
            let sub =
                |m: &Mat| Mat::from_vec(n, m.cols, m.data[at * m.cols..(at + n) * m.cols].to_vec());
            chunked
                .append(&sub(&k), &sub(&v), &sub(&ik), &coords.img_page[at..at + n])
                .unwrap();
            at += n;
        }
        let got: Vec<(i32, i32)> = whole
            .bmin()
            .iter()
            .copied()
            .zip(whole.bmax().iter().copied())
            .collect();
        assert_eq!(got, want);
        assert_eq!(
            (chunked.bmin(), chunked.bmax()),
            (whole.bmin(), whole.bmax())
        );
        let reference = BlockPages::build(&coords.img_page);
        assert_eq!(
            (whole.bmin(), whole.bmax()),
            (&reference.bmin[..], &reference.bmax[..])
        );
        // Generated rows never count as image rows: block 25 keeps (2, 2) and
        // a block of generated rows has none.
        let (k, v, ik) = random_rows(&mut rng, 40);
        whole.append(&k, &v, &ik, &[-1; 40]).unwrap();
        assert_eq!((whole.bmin()[25], whole.bmax()[25]), (2, 2));
        assert_eq!((whole.bmin()[26], whole.bmax()[26]), (-1, -1));
        // Page 1's gate allows exactly the blocks holding page-1 images.
        let allow = page_gate_decode(whole.bmin(), whole.bmax(), 1, whole.nblk());
        let allowed: Vec<usize> = (0..allow.len()).filter(|&c| allow[c]).collect();
        assert_eq!(allowed, (8..=17).collect::<Vec<_>>());
    }

    /// How a synthetic decode run ends after its last page body.
    #[derive(Debug, Clone, Copy, PartialEq)]
    enum End {
        /// EOS from an ordinary step.
        Eos,
        /// One more `<PAGE>`, whose step's output the guard forces to EOS: an
        /// extra page unwindowed, the lookahead marker in a window.
        ForcedMarker,
    }

    /// A synthetic one-layer decode run. Weights are random; each step's rows
    /// are nudged by the previous attention output and each token follows a
    /// hash of it, so any bit of difference changes what follows.
    #[derive(Debug, Clone)]
    struct DocPlan {
        /// Image page per prompt row.
        img_page: Vec<i32>,
        /// Text tokens before the first generated `<PAGE>` (query page −1).
        lead: usize,
        /// Query page of the first generated `<PAGE>`: 0 fresh, 1 reused.
        first_qp: i32,
        /// Per generated page, the shortest and longest body.
        bodies: Vec<(usize, usize)>,
        end: End,
        /// Pinned rows; a non-empty set also turns the text-tail margin on.
        pins: Vec<Range<usize>>,
        retire: bool,
        /// Whether the guard reports the final marker's step as forced.
        guard: bool,
        domain: EntmaxDomain,
        seed: u64,
    }

    #[derive(Debug, PartialEq)]
    struct Step {
        q: usize,
        qp: i32,
        forced: bool,
        open: Option<OpenRow>,
        blocks: Vec<u32>,
        bias: Vec<u32>,
        out: Vec<u32>,
        next: u32,
    }

    struct DocRun {
        steps: Vec<Step>,
        fallback: Option<RetireFallback>,
        cache: SrSwaLayerCache,
        most_dead: usize,
        /// Blocks the forced step would have freed had it not been forced.
        forced_would_free: usize,
        freed_on_forced: usize,
        peak_seen: usize,
    }

    fn fnv(xs: &[f32]) -> u64 {
        xs.iter().fold(0xcbf2_9ce4_8422_2325, |h, x| {
            (h ^ u64::from(x.to_bits())).wrapping_mul(0x0100_0000_01b3)
        })
    }

    fn run_doc(plan: &DocPlan) -> DocRun {
        let mut rng = Lcg(plan.seed);
        let width = NUM_HEADS * HEAD_DIM;
        let p = RouterParams::uocr2();
        let mut cache = SrSwaLayerCache::new(&random_head_cls(&mut rng), plan.retire).unwrap();
        let (k, v, ik) = random_rows(&mut rng, plan.img_page.len());
        cache.append(&k, &v, &ik, &plan.img_page).unwrap();
        cache.set_pinned(&plan.pins, !plan.pins.is_empty()).unwrap();
        let mut run = DocRun {
            steps: Vec::new(),
            fallback: None,
            cache: cache.clone(),
            most_dead: 0,
            forced_would_free: 0,
            freed_on_forced: 0,
            peak_seen: cache.live_bytes(),
        };
        let mut prev = vec![0.0f32; width];
        let mut token = if plan.lead > 0 { TEXT } else { PG };
        let (mut markers, mut lead_fed, mut body) = (0usize, 0usize, 0usize);
        loop {
            let q = cache.rows;
            let forced = token == PG && markers == plan.bodies.len();
            if token == PG {
                markers += 1;
                body = 0;
            } else if markers == 0 {
                lead_fed += 1;
            } else {
                body += 1;
            }
            let qp = if markers == 0 {
                -1
            } else {
                plan.first_qp + markers as i32 - 1
            };
            let nudge = |m: &mut Mat, from: usize| {
                for (x, &o) in m.data.iter_mut().zip(&prev[from..]) {
                    *x += 0.05 * o;
                }
            };
            let (mut k, mut v, mut ik) = random_rows(&mut rng, 1);
            let mut qm = random_mat(&mut rng, 1, width, 1.0);
            let mut iq = random_mat(&mut rng, 1, HEAD_DIM, 1.0);
            nudge(&mut k, 0);
            nudge(&mut v, 0);
            nudge(&mut qm, 0);
            nudge(&mut ik, 0);
            nudge(&mut iq, HEAD_DIM);
            cache.append(&k, &v, &ik, &[-1]).unwrap();
            run.peak_seen = run.peak_seen.max(cache.live_bytes());
            // Retire before this step's own routing and attention, the order
            // in which an off-by-one in the window rule would be read.
            if forced {
                run.forced_would_free = cache.clone().retire_dead_blocks(qp, q, false).unwrap();
            }
            let freed = cache
                .retire_dead_blocks(qp, q, forced && plan.guard)
                .unwrap();
            if forced {
                run.freed_on_forced = freed;
            }
            run.most_dead = run.most_dead.max(cache.dead_blocks());
            let dr = match cache.decode_route(&iq.data, qp, &p, plan.domain, forced && plan.guard) {
                Ok(dr) => dr,
                Err(event) => {
                    run.fallback = Some(event);
                    break;
                }
            };
            let sel = BlockSelection::from(dr.route.clone());
            let out = attend(&qm, &[q], std::slice::from_ref(&sel), &cache).unwrap();
            let next = if forced {
                EOS
            } else if markers == 0 {
                if lead_fed >= plan.lead { PG } else { TEXT }
            } else {
                let (lo, hi) = plan.bodies[markers - 1];
                let ends = body >= hi || (body >= lo && fnv(&out.data).is_multiple_of(4));
                match (ends, markers < plan.bodies.len(), plan.end) {
                    (false, _, _) => TEXT,
                    (true, true, _) | (true, false, End::ForcedMarker) => PG,
                    (true, false, End::Eos) => EOS,
                }
            };
            run.steps.push(Step {
                q,
                qp,
                forced,
                open: dr.open,
                blocks: dr.route.blocks.clone(),
                bias: dr.route.bias.iter().map(|b| b.to_bits()).collect(),
                out: out.data.iter().map(|x| x.to_bits()).collect(),
                next,
            });
            if next == EOS {
                break;
            }
            prev = out.data;
            token = next;
        }
        run.cache = cache;
        run
    }

    /// A fresh prompt's image pages (BOS, pages of `images` rows, instruction).
    fn fresh_pages(images: &[usize], instruction: usize) -> Vec<i32> {
        PageCoords::build(&prompt(images, instruction))
            .unwrap()
            .img_page
    }

    fn base_plan(img_page: Vec<i32>, bodies: Vec<(usize, usize)>, end: End, seed: u64) -> DocPlan {
        DocPlan {
            img_page,
            lead: 0,
            first_qp: 0,
            bodies,
            end,
            pins: Vec::new(),
            retire: true,
            guard: true,
            domain: EntmaxDomain::TopM,
            seed,
        }
    }

    /// Retired and unretired runs agree on every step whose output is not
    /// guard-forced: selections, biases, attention outputs and tokens.
    fn assert_same_run(retired: &DocRun, full: &DocRun, what: &str) {
        assert!(
            retired.fallback.is_none() && full.fallback.is_none(),
            "{what}: fallback"
        );
        assert_eq!(retired.steps.len(), full.steps.len(), "{what}: steps");
        for (a, b) in retired.steps.iter().zip(&full.steps) {
            if a.forced && a.open.is_some() {
                assert_eq!((a.q, a.next), (b.q, b.next), "{what}: forced step");
            } else {
                assert_eq!(a, b, "{what}: step at {}", a.q);
            }
        }
    }

    #[test]
    fn retired_runs_equal_unretired_runs_bit_for_bit() {
        let shapes: Vec<(&str, DocPlan)> = vec![
            (
                "S1 one page",
                DocPlan {
                    lead: 2,
                    ..base_plan(
                        fresh_pages(&[273], 4),
                        vec![(200, 260)],
                        End::Eos,
                        0x5eed_0b42,
                    )
                },
            ),
            (
                "S2 three pages",
                base_plan(
                    fresh_pages(&[273; 3], 4),
                    vec![(60, 120); 3],
                    End::Eos,
                    0x5eed_0b43,
                ),
            ),
            (
                "S2 three pages, all-candidates entmax",
                DocPlan {
                    domain: EntmaxDomain::AllCandidates,
                    ..base_plan(
                        fresh_pages(&[273; 3], 4),
                        vec![(60, 120); 3],
                        End::Eos,
                        0x5eed_0b44,
                    )
                },
            ),
            (
                "S3 six pages, extra <PAGE> at the end",
                base_plan(
                    fresh_pages(&[273; 6], 4),
                    vec![(20, 60); 6],
                    End::ForcedMarker,
                    0x5eed_0b45,
                ),
            ),
        ];
        for (what, plan) in shapes {
            let retired = run_doc(&plan);
            let full = run_doc(&DocPlan {
                retire: false,
                ..plan.clone()
            });
            assert_same_run(&retired, &full, what);
            assert!(retired.most_dead > 0, "{what}: nothing was retired");
            assert_eq!(full.most_dead, 0, "{what}");
            // Memory: every freed block takes its slab with it.
            let (c, f) = (&retired.cache, &full.cache);
            let slab = BLOCK_SIZE * ROW_BYTES;
            assert_eq!(
                f.live_bytes(),
                SrSwaLayerCache::estimate_bytes(f.rows),
                "{what}"
            );
            assert_eq!(
                c.live_bytes(),
                f.live_bytes() - c.dead_blocks() * slab,
                "{what}"
            );
            assert_eq!(
                (c.peak_bytes(), f.peak_bytes()),
                (retired.peak_seen, full.peak_seen),
                "{what}"
            );
            assert!(
                c.peak_bytes() < f.peak_bytes(),
                "{what}: retirement saved nothing"
            );
        }
    }

    #[test]
    fn three_page_retirement_frees_passed_pages_but_keeps_straddles_and_the_window() {
        let plan = base_plan(
            fresh_pages(&[273; 3], 4),
            vec![(150, 150), (150, 150), (40, 40)],
            End::Eos,
            0x5eed_0b46,
        );
        let run = run_doc(&plan);
        assert!(run.fallback.is_none());
        let c = &run.cache;
        let last = run.steps.last().unwrap().q;
        // Page 0's blocks 0..=7 and page 1's 9..=16 are gone, and so is the
        // 0/1 straddle (bmax 1 < qp 2); the 1/2 straddle 17 and page 2's
        // blocks 18..=25 stay, as does everything the local window touches.
        for b in 0..=16 {
            assert!(!c.is_live(b), "block {b}");
        }
        for b in 17..=25 {
            assert!(c.is_live(b), "block {b}");
        }
        for b in (last + 1 - LOCAL_WINDOW) / BLOCK_SIZE..c.nblk() {
            assert!(c.is_live(b), "window block {b}");
        }
        // Generated text behind the window is gone.
        let gen_first_full = 827usize.div_ceil(BLOCK_SIZE);
        assert!(!c.is_live(gen_first_full));
    }

    #[test]
    fn the_detector_passes_guard_forced_steps_and_fires_otherwise() {
        // An extra <PAGE> after the last page (qp = N): an open row, but the
        // guard forces the step, so no fallback, and that step frees nothing
        // although blocks are dead by the rule.
        let extra = base_plan(
            fresh_pages(&[273; 3], 4),
            vec![(60, 60); 3],
            End::ForcedMarker,
            0x5eed_0b47,
        );
        let run = run_doc(&extra);
        let last = run.steps.last().unwrap();
        assert!(run.fallback.is_none());
        assert_eq!(
            (last.qp, last.forced, last.open, last.next),
            (3, true, Some(OpenRow::NoAllowedBlock), EOS)
        );
        assert!(run.cache.dead_blocks() > 0);
        assert!(run.forced_would_free > 0);
        assert_eq!(run.freed_on_forced, 0);
        // The same row with the guard off fires the detector.
        let unguarded = run_doc(&DocPlan {
            guard: false,
            ..extra.clone()
        });
        let event = unguarded.fallback.expect("the detector fires");
        assert_eq!(
            (event.query_page, event.open, event.q_pos),
            (3, OpenRow::NoAllowedBlock, last.q)
        );
        assert!(event.dead_blocks > 0);
        assert!(event.to_string().contains("OQ-V2-11"));
        // A mid-page open row: a 20-image last page sits inside the local
        // window when its body starts, so page 2 is allowed but not remote.
        let tiny = base_plan(
            fresh_pages(&[273, 273, 20], 2),
            vec![(40, 40), (40, 40), (10, 10)],
            End::Eos,
            0x5eed_0b48,
        );
        let event = run_doc(&tiny).fallback.expect("the detector fires");
        assert_eq!(
            (event.query_page, event.open),
            (2, OpenRow::NoRemoteAllowed)
        );
        assert!(event.dead_blocks > 0);
        // Without retirement nothing fires, and the open row is reported.
        let full = run_doc(&DocPlan {
            retire: false,
            ..tiny
        });
        assert!(full.fallback.is_none());
        assert!(
            full.steps
                .iter()
                .any(|s| s.qp == 2 && s.open == Some(OpenRow::NoRemoteAllowed))
        );
        // Before the first <PAGE> rows are open (qp < 0) and nothing retires.
        let lead = run_doc(&DocPlan {
            lead: 200,
            ..base_plan(fresh_pages(&[273], 4), vec![(5, 5)], End::Eos, 0x5eed_0b49)
        });
        assert!(lead.fallback.is_none());
        let first_page = lead.steps.iter().position(|s| s.qp == 0).unwrap();
        assert!(
            lead.steps[..first_page]
                .iter()
                .all(|s| s.open == Some(OpenRow::NoPage))
        );
        assert_eq!(lead.steps[first_page - 1].qp, -1);
        assert!(
            lead.most_dead > 0,
            "imageless lead blocks retire once qp = 0"
        );
    }

    /// A window's prompt and the rows §5.10.3 retains at its end.
    struct Window {
        img_page: Vec<i32>,
        first_qp: i32,
        own_pages: usize,
        /// The last own page's marker row.
        marker: usize,
        /// `[E − 128 − (E mod 32), E)`.
        image_tail: Range<usize>,
        /// The lookahead page's marker and images.
        lookahead: Range<usize>,
    }

    /// Window 0 of a W-page window: BOS, W own pages, the lookahead page, the
    /// instruction.
    fn fresh_window(w: usize) -> Window {
        let img_page = fresh_pages(&vec![273; w + 1], 4);
        let marker = 1 + 274 * (w - 1);
        let e = marker + 274;
        Window {
            img_page,
            first_qp: 0,
            own_pages: w,
            marker,
            image_tail: e - 128 - e % 32..e,
            lookahead: e..e + 274,
        }
    }

    /// A reused window (§5.10.4): the copied tail marker and `tail_imgs` image
    /// rows of local page 0, the copied lookahead (local page 1), new pages
    /// 2..=m (m the new lookahead), then `text` copied text-tail rows.
    fn reused_window(m: usize, tail_imgs: usize, text: usize) -> Window {
        let mut img_page = vec![-1];
        img_page.extend(std::iter::repeat_n(0, tail_imgs));
        for page in 1..=m as i32 {
            img_page.push(-1);
            img_page.extend(std::iter::repeat_n(page, 273));
        }
        img_page.extend(std::iter::repeat_n(-1, text));
        let marker = 1 + tail_imgs + 274 * (m - 2);
        let e = marker + 274;
        Window {
            img_page,
            first_qp: 1,
            own_pages: m - 1,
            marker,
            image_tail: e - 128 - e % 32..e,
            lookahead: e..e + 274,
        }
    }

    #[test]
    fn pinned_rows_survive_to_the_end_of_a_window() {
        let windows = [
            ("W=8 window 0", fresh_window(8)),
            ("W=2 window 0", fresh_window(2)),
            ("reused, one own page", reused_window(2, 128, 128)),
            ("reused, two own pages", reused_window(3, 159, 37)),
            ("reused, three own pages", reused_window(4, 145, 0)),
        ];
        assert_eq!(windows[0].1.marker, 1919);
        assert_eq!(1919 % 32, 31);
        for (i, (what, win)) in windows.iter().enumerate() {
            // Short bodies, then a tail-page body long enough for a full
            // 128-row text tail.
            let mut bodies = vec![(10, 30); win.own_pages - 1];
            bodies.push((140, 170));
            let plan = DocPlan {
                first_qp: win.first_qp,
                pins: vec![
                    win.marker..win.marker + 1,
                    win.image_tail.clone(),
                    win.lookahead.clone(),
                ],
                ..base_plan(
                    win.img_page.clone(),
                    bodies,
                    End::ForcedMarker,
                    0x5eed_0b50 + i as u64,
                )
            };
            let pinned = run_doc(&plan);
            let full = run_doc(&DocPlan {
                retire: false,
                ..plan.clone()
            });
            assert_same_run(&pinned, &full, what);
            let last = pinned.steps.last().unwrap();
            assert!(
                last.forced && last.open.is_none(),
                "{what}: lookahead marker step"
            );
            let q = last.q;
            let kept = [
                win.marker..win.marker + 1,
                win.image_tail.clone(),
                win.lookahead.clone(),
                q - 128..q,
            ];
            let a = pinned
                .cache
                .serialize_tail(&kept)
                .unwrap_or_else(|e| panic!("{what}: {e}"));
            assert_eq!(a, full.cache.serialize_tail(&kept).unwrap(), "{what}");
            assert!(pinned.most_dead > 0, "{what}: nothing was retired");
            if i == 0 {
                // Unpinned, the marker's block (page-6 images, then row 1919)
                // dies once qp = 7.
                let loose = run_doc(&DocPlan {
                    pins: Vec::new(),
                    ..plan
                });
                assert!(!loose.cache.is_live(1919 / BLOCK_SIZE));
                assert!(loose.cache.serialize_tail(&kept[..1]).is_err());
            }
        }
    }

    #[test]
    fn the_text_tail_margin_keeps_one_more_row() {
        let mut rng = Lcg(0x5eed_0b51);
        let img_page = fresh_pages(&[273], 4);
        let mut cache = SrSwaLayerCache::new(&random_head_cls(&mut rng), true).unwrap();
        let (k, v, ik) = random_rows(&mut rng, img_page.len() + 300);
        let mut pages = img_page.clone();
        pages.resize(k.rows, -1);
        cache.append(&k, &v, &ik, &pages).unwrap();
        // Block 10 (rows 320..=351) holds generated text only; block 8 (rows
        // 256..=287) ends page 0's images.
        assert_eq!((cache.bmin()[10], cache.bmax()[8]), (-1, 0));
        let mut margin = cache.clone();
        margin.set_pinned(&[], true).unwrap();
        let mut image = margin.clone();
        cache.retire_dead_blocks(0, 351 + 127, false).unwrap();
        assert!(cache.is_live(10) && !cache.is_live(9));
        cache.retire_dead_blocks(0, 351 + 128, false).unwrap();
        assert!(!cache.is_live(10));
        margin.retire_dead_blocks(0, 351 + 128, false).unwrap();
        assert!(margin.is_live(10) && !margin.is_live(9));
        margin.retire_dead_blocks(0, 351 + 129, false).unwrap();
        assert!(!margin.is_live(10));
        // The margin is for text blocks only: an image block retires at q − 128.
        image.retire_dead_blocks(1, 287 + 127, false).unwrap();
        assert!(image.is_live(8) && !image.is_live(7));
        image.retire_dead_blocks(1, 287 + 128, false).unwrap();
        assert!(!image.is_live(8));
    }

    #[test]
    fn tails_round_trip_and_reject_bad_input() {
        let mut rng = Lcg(0x5eed_0b52);
        let head_cls = random_head_cls(&mut rng);
        let mut cache = SrSwaLayerCache::new(&head_cls, true).unwrap();
        let (k, v, ik) = random_rows(&mut rng, 300);
        cache.append(&k, &v, &ik, &[-1; 300]).unwrap();
        let ranges = [3..4, 100..160, 250..300];
        let bytes = cache.serialize_tail(&ranges).unwrap();
        assert_eq!(bytes.len(), TAIL_HEADER + 111 * ROW_BYTES);
        let tail = deserialize_tail(&bytes).unwrap();
        let rows: Vec<usize> = ranges.iter().flat_map(Clone::clone).collect();
        for (i, &r) in rows.iter().enumerate() {
            assert_eq!(tail.k.row(i), k.row(r));
            assert_eq!(tail.v.row(i), v.row(r));
            assert_eq!(tail.ik.row(i), ik.row(r));
        }
        // Spliced into a new cache, the summaries are recomputed.
        let mut spliced = SrSwaLayerCache::new(&head_cls, true).unwrap();
        spliced
            .append(&tail.k, &tail.v, &tail.ik, &vec![-1; rows.len()])
            .unwrap();
        assert_same_summaries(
            spliced.summaries(),
            &summaries(&tail.ik, &head_cls),
            "spliced",
        );
        assert_eq!(cache.serialize_tail(&[]).unwrap().len(), TAIL_HEADER);
        // Malformed input.
        assert!(deserialize_tail(&bytes[..bytes.len() - 1]).is_err());
        assert!(deserialize_tail(&bytes[..10]).is_err());
        for at in [0, 8, 12, 16, 20] {
            let mut bad = bytes.clone();
            bad[at] ^= 1;
            assert!(deserialize_tail(&bad).is_err(), "byte {at}");
        }
        let past = 299..301;
        assert!(
            cache.serialize_tail(std::slice::from_ref(&past)).is_err(),
            "past the cache"
        );
        cache.retire_dead_blocks(0, 299, false).unwrap();
        assert!(!cache.is_live(0));
        let retired = 3..4;
        assert!(
            cache
                .serialize_tail(std::slice::from_ref(&retired))
                .is_err(),
            "retired"
        );
    }

    #[test]
    fn memory_accounting_matches_the_allocations_and_the_estimate() {
        // Plan §6.3: over 12 layers, K+V is 120 KiB and index keys 6 KiB per token.
        assert_eq!(12 * ROW_BYTES, (120 + 6) * 1024);
        let mut rng = Lcg(0x5eed_0b53);
        let mut cache = SrSwaLayerCache::new(&random_head_cls(&mut rng), true).unwrap();
        let measured = |c: &SrSwaLayerCache| {
            let slabs: usize = c
                .slabs
                .iter()
                .flatten()
                .map(|s| (s.k.len() + s.v.len() + s.ik.len()) * size_of::<f32>())
                .sum();
            slabs
                + c.sums.kbar.len() * size_of::<f32>()
                + (c.bmin.len() + c.bmax.len()) * size_of::<i32>()
                + c.sums.valid.len()
                + c.pinned.len()
        };
        for n in [1, 31, 1, 100, 200, 7] {
            let (k, v, ik) = random_rows(&mut rng, n);
            cache.append(&k, &v, &ik, &vec![-1; n]).unwrap();
            let rows = cache.rows;
            assert_eq!(cache.live_bytes(), measured(&cache));
            assert_eq!(cache.live_bytes(), SrSwaLayerCache::estimate_bytes(rows));
            assert_eq!(cache.peak_bytes(), cache.live_bytes());
            // Against the per-token figure: at most one partial block plus
            // 16 B of bookkeeping per row over it.
            let slack = cache.live_bytes() - rows * ROW_BYTES;
            assert!(slack < BLOCK_SIZE * ROW_BYTES + cache.nblk() * BLOCK_META_BYTES);
        }
        let peak = cache.peak_bytes();
        let freed = cache.retire_dead_blocks(0, cache.rows - 1, false).unwrap();
        assert!(freed > 0);
        assert_eq!(cache.live_bytes(), measured(&cache));
        assert_eq!(cache.live_bytes(), peak - freed * BLOCK_SIZE * ROW_BYTES);
        assert_eq!(cache.peak_bytes(), peak);
    }

    #[test]
    fn retirement_rules_and_inputs_are_checked() {
        for (value, on) in [
            (None, true),
            (Some("1"), true),
            (Some("yes"), true),
            (Some(""), true),
            (Some("0"), false),
            (Some(" OFF "), false),
            (Some("false"), false),
            (Some("no"), false),
        ] {
            assert_eq!(retire_enabled_for(value), on, "{value:?}");
        }
        let mut rng = Lcg(0x5eed_0b54);
        assert!(SrSwaLayerCache::new(&[0.0; 7], true).is_err());
        let mut cache = SrSwaLayerCache::new(&random_head_cls(&mut rng), true).unwrap();
        let (k, v, ik) = random_rows(&mut rng, 400);
        assert!(cache.append(&k, &v, &ik, &[-1; 399]).is_err());
        assert!(
            cache
                .append(&k, &v, &random_mat(&mut rng, 400, 7, 1.0), &[-1; 400])
                .is_err()
        );
        cache.append(&k, &v, &ik, &[-1; 400]).unwrap();
        assert!(
            cache.retire_dead_blocks(0, 400, false).is_err(),
            "past the cache"
        );
        // qp < 0, a forced step, or retirement off: nothing is freed.
        assert_eq!(cache.retire_dead_blocks(-1, 300, false).unwrap(), 0);
        assert_eq!(cache.retire_dead_blocks(0, 300, true).unwrap(), 0);
        let mut off = SrSwaLayerCache::new(&random_head_cls(&mut rng), false).unwrap();
        off.append(&k, &v, &ik, &[-1; 400]).unwrap();
        assert_eq!(off.retire_dead_blocks(0, 399, false).unwrap(), 0);
        // Pinned blocks survive; pinning a retired one or past the cache fails.
        cache.set_pinned(&[40..41, 70..70], false).unwrap();
        assert_eq!(cache.retire_dead_blocks(0, 300, false).unwrap(), 4);
        assert!(cache.is_live(1) && !cache.is_live(0) && !cache.is_live(2));
        let (retired, past) = (0..1, 390..401);
        assert!(cache.set_pinned(&[retired, 40..41], false).is_err());
        assert!(cache.set_pinned(&[40..41, past], false).is_err());
        assert!(cache.is_live(1), "a failed pin keeps the old set");
        assert_eq!(cache.retire_dead_blocks(0, 300, false).unwrap(), 0);
        // Neither q nor qp may go back.
        assert!(cache.retire_dead_blocks(0, 299, false).is_err());
        assert!(cache.retire_dead_blocks(-1, 301, false).is_err());
        assert!(cache.retire_dead_blocks(1, 301, false).is_ok());
    }

    #[test]
    #[should_panic(expected = "was retired")]
    fn reading_a_retired_block_panics() {
        let mut rng = Lcg(0x5eed_0b55);
        let mut cache = SrSwaLayerCache::new(&random_head_cls(&mut rng), true).unwrap();
        let (k, v, ik) = random_rows(&mut rng, 200);
        cache.append(&k, &v, &ik, &[-1; 200]).unwrap();
        cache.retire_dead_blocks(0, 199, false).unwrap();
        let _ = cache.keys(0, 0);
    }

    // ── U2-B5: the decoder-facing layer state ──

    fn bits(xs: &[f32]) -> Vec<u32> {
        xs.iter().map(|x| x.to_bits()).collect()
    }

    fn row_range(m: &Mat, r: Range<usize>) -> Mat {
        Mat::from_vec(
            r.len(),
            m.cols,
            m.data[r.start * m.cols..r.end * m.cols].to_vec(),
        )
    }

    /// One layer over a base prompt of one or two pages and generated text
    /// with one `<PAGE>` marker per prompt page, each page body longer than
    /// the local window: the prompt prefill plus one
    /// [`SrSwaState::decode_layer`] per row equals a prefill of every row,
    /// bit for bit, on the gathered and the dense attention. The one-page
    /// prompt (283 rows, M = 9) has a smaller router window than the longer
    /// forward; the rows still agree because no candidate's entmax input is
    /// near the −1e4 pads (plan §5.8), unlike
    /// [`prefill_window_is_set_by_the_forward_not_the_row`].
    #[test]
    fn state_decode_equals_a_reprefill_of_the_layer_bit_for_bit() {
        for (pages, seed) in [(&[273][..], 0x5eed_0b05), (&[273, 273][..], 0x5eed_0b06)] {
            state_decode_equals_a_reprefill(pages, seed);
        }
    }

    /// One layer's routing in a single prefill, composed independently of
    /// [`SrSwaState::prefill_layer`]: the index projections, then each row's
    /// selection from an empty cache. Returns the selections and the index
    /// keys, which the cache stores.
    fn prefill_selections(
        normed: &Mat,
        w: &SrSwaLayerWeights,
        rope: &RopeTable,
        coords: &PageCoords,
        p: &RouterParams,
        domain: EntmaxDomain,
    ) -> FocrResult<(Vec<BlockSelection>, Mat)> {
        let (iq, ik) = project(normed, w, rope)?;
        let routes = prefill_routes(&iq, &ik, coords, &w.head_cls, p, domain)?;
        Ok((routes.into_iter().map(BlockSelection::from).collect(), ik))
    }

    /// U2-D1a: a prefill of the rows after cached ones (a reused window's
    /// extend over its copied rows) routes and attends them exactly as one
    /// prefill of every row does: the same outputs and cache, bit for bit,
    /// whatever the split. The cached rows are appended as copied rows are,
    /// never prefilled, so only the new rows' routing (M from every row's
    /// blocks, the Page Gate over every row's pages) is under test.
    #[test]
    fn extend_prefill_equals_one_prefill_on_the_new_rows() {
        let mut rng = Lcg(0xD1A);
        let hidden = 64;
        let w = SrSwaLayerWeights::new(
            hidden,
            random_mat(&mut rng, HEAD_DIM, hidden, 0.3).data,
            random_mat(&mut rng, HEAD_DIM, hidden, 0.3).data,
            (0..HEAD_DIM).map(|_| uniform(&mut rng, 0.5, 1.5)).collect(),
            (0..HEAD_DIM).map(|_| uniform(&mut rng, 0.5, 1.5)).collect(),
            random_head_cls(&mut rng),
        )
        .unwrap();
        // A reused window's rewritten prompt: the tail marker and a 150-row
        // image tail, the copied lookahead page, then two new pages.
        let mut ids = vec![PG];
        ids.extend(std::iter::repeat_n(IMG, 150));
        for _ in 0..3 {
            ids.push(PG);
            ids.extend(std::iter::repeat_n(IMG, 273));
        }
        let s = ids.len();
        let coords = PageCoords::build(&ids).unwrap();
        let width = NUM_HEADS * HEAD_DIM;
        let normed = random_mat(&mut rng, s, hidden, 1.0);
        let q = random_mat(&mut rng, s, width, 1.0);
        let (k, v, _) = random_rows(&mut rng, s);
        let page_base = 3;
        let rope = coords.rope_table(page_base);
        let router = std::slice::from_ref(&w);
        let layer_rows = |r: Range<usize>| [&normed, &q, &k, &v].map(|m| row_range(m, r.clone()));
        let [an, aq, ak, av] = layer_rows(0..s);
        let all = LayerRows {
            normed: &an,
            q: &aq,
            k: &ak,
            v: &av,
        };
        let mut whole = SrSwaState::new(router, &coords, page_base).unwrap();
        whole.trace = Some(SrSwaTrace::new(1));
        let want = whole.prefill_layer(0, &w, all, &rope, &coords).unwrap();
        let trace = whole.trace.take().unwrap();
        let last = trace.prefill_last[0].as_ref().unwrap();
        assert!(!last.blocks.is_empty(), "the last row reads remote blocks");
        assert!(
            whole.layers[0].nblk() > 16,
            "the router window is capped, so M depends on the split"
        );
        let (_, ik) = project(&normed, &w, &rope).unwrap();
        // 425: where a reused window's new pages start; the others land
        // mid-page, mid-block and one row from either end.
        for split in [1, 400, 425, 426, 600, s - 1] {
            let mut st = SrSwaState::new(router, &coords, page_base).unwrap();
            let copied = [&k, &v, &ik].map(|m| row_range(m, 0..split));
            st.layers[0]
                .append(
                    &copied[0],
                    &copied[1],
                    &copied[2],
                    &coords.img_page[..split],
                )
                .unwrap();
            let [nn, nq, nk, nv] = layer_rows(split..s);
            let new = LayerRows {
                normed: &nn,
                q: &nq,
                k: &nk,
                v: &nv,
            };
            let new_rope = page2d_rope_table(
                &coords.tok_pos[split..],
                &coords.rope_pages(page_base)[split..],
            );
            let got = st.prefill_layer(0, &w, new, &new_rope, &coords).unwrap();
            assert_eq!(
                bits(&got.data),
                bits(&want.data[split * width..]),
                "split {split}: the new rows' outputs"
            );
            let all = 0..s;
            let every = std::slice::from_ref(&all);
            let (a, b) = (
                whole.layers[0].tail_rows(every).unwrap(),
                st.layers[0].tail_rows(every).unwrap(),
            );
            for (x, y, what) in [(&a.k, &b.k, "K"), (&a.v, &b.v, "V"), (&a.ik, &b.ik, "ik")] {
                assert_eq!(bits(&x.data), bits(&y.data), "split {split}: cached {what}");
            }
            assert_same_summaries(
                whole.layers[0].summaries(),
                st.layers[0].summaries(),
                &format!("split {split}"),
            );
            assert_eq!(
                (whole.layers[0].bmin(), whole.layers[0].bmax()),
                (st.layers[0].bmin(), st.layers[0].bmax())
            );
        }
    }

    /// U2-D1a: [`SrSwaLayerCache::tail_rows`] reads rows back in the layout
    /// `append` takes, bit for bit, in range order; a retired or absent row
    /// is refused; [`TailRows::slice`] takes a sub-range.
    #[test]
    fn tail_rows_read_back_appended_rows() {
        let mut rng = Lcg(0x7A11);
        let (k, v, ik) = random_rows(&mut rng, 300);
        let pages: Vec<i32> = (0..300).map(|r| if r < 100 { -1 } else { 0 }).collect();
        let mut cache = SrSwaLayerCache::new(&random_head_cls(&mut rng), true).unwrap();
        cache.append(&k, &v, &ik, &pages).unwrap();
        let ranges = [250..260, 3..4, 100..132];
        let got = cache.tail_rows(&ranges).unwrap();
        assert_eq!(got.rows(), 43);
        let mut at = 0;
        for r in &ranges {
            for (m, src) in [(&got.k, &k), (&got.v, &v), (&got.ik, &ik)] {
                assert_eq!(
                    bits(&row_range(m, at..at + r.len()).data),
                    bits(&row_range(src, r.clone()).data)
                );
            }
            at += r.len();
        }
        let part = got.slice(10..11);
        assert_eq!(bits(&part.ik.data), bits(ik.row(3)));
        assert!(
            cache.tail_rows(std::slice::from_ref(&(299..301))).is_err(),
            "past the cache"
        );
        cache.retire_dead_blocks(0, 299, false).unwrap();
        assert!(
            !cache.is_live(0),
            "block 0 is imageless and dead at q = 299"
        );
        assert!(
            cache.tail_rows(std::slice::from_ref(&(3..4))).is_err(),
            "a retired row"
        );
        assert!(cache.tail_rows(std::slice::from_ref(&(250..251))).is_ok());
    }

    fn state_decode_equals_a_reprefill(pages: &[usize], seed: u64) {
        let mut rng = Lcg(seed);
        let hidden = 64;
        let w = SrSwaLayerWeights::new(
            hidden,
            random_mat(&mut rng, HEAD_DIM, hidden, 0.3).data,
            random_mat(&mut rng, HEAD_DIM, hidden, 0.3).data,
            (0..HEAD_DIM).map(|_| uniform(&mut rng, 0.5, 1.5)).collect(),
            (0..HEAD_DIM).map(|_| uniform(&mut rng, 0.5, 1.5)).collect(),
            random_head_cls(&mut rng),
        )
        .unwrap();
        let prompt_ids = prompt(pages, 8);
        // Page bodies longer than the 128-row local window, so the early
        // generated blocks are routed like any remote block.
        let mut generated = vec![TEXT; 30];
        for _ in pages {
            generated.push(PG);
            generated.extend(std::iter::repeat_n(TEXT, 150));
        }
        let all: Vec<u32> = prompt_ids.iter().chain(&generated).copied().collect();
        let (n, s) = (prompt_ids.len(), all.len());
        assert_eq!(n, 9 + 274 * pages.len(), "{pages:?}");
        let width = NUM_HEADS * HEAD_DIM;
        let normed = random_mat(&mut rng, s, hidden, 1.0);
        let q = random_mat(&mut rng, s, width, 1.0);
        let (k, v, _) = random_rows(&mut rng, s);
        let positions: Vec<usize> = (0..s).collect();

        // Every row in one prefill.
        let coords = PageCoords::build(&all).unwrap();
        let p = RouterParams::uocr2();
        let rope = coords.rope_table(0);
        let (sel, ik) =
            prefill_selections(&normed, &w, &rope, &coords, &p, EntmaxDomain::TopM).unwrap();
        let mut whole = SrSwaLayerCache::new(&w.head_cls, false).unwrap();
        whole.append(&k, &v, &ik, &coords.img_page).unwrap();
        let want = attend(&q, &positions, &sel, &whole).unwrap();
        assert_eq!(
            bits(
                &attend_with(&q, &positions, &sel, &whole, true)
                    .unwrap()
                    .data
            ),
            bits(&want.data),
            "the dense prefill"
        );
        // Some generated rows read remote blocks, so routing is exercised.
        assert!(sel[n..].iter().any(|s| !s.blocks.is_empty()));

        // The prompt prefill, then one decode step per generated row.
        let pc = PageCoords::build(&prompt_ids).unwrap();
        let mut state = SrSwaState::new(std::slice::from_ref(&w), &pc, 0).unwrap();
        assert!(!state.dense, "FOCR_SRSWA_DENSE is unset in tests");
        let mut dense = state.clone();
        dense.dense = true;
        // U2-B6's wiring: retirement on through the state; dense attention
        // keeps it off.
        let mut retired = state.clone();
        retired.set_retire(true);
        dense.set_retire(true);
        let span = |m: &Mat, r: Range<usize>| row_range(m, r);
        let (pn, pq, pk, pv) = (
            span(&normed, 0..n),
            span(&q, 0..n),
            span(&k, 0..n),
            span(&v, 0..n),
        );
        let prompt_rows = LayerRows {
            normed: &pn,
            q: &pq,
            k: &pk,
            v: &pv,
        };
        let prompt_rope = pc.rope_table(0);
        for st in [&mut state, &mut dense, &mut retired] {
            let got = st
                .prefill_layer(0, &w, prompt_rows, &prompt_rope, &pc)
                .unwrap();
            assert_eq!(
                bits(&got.data),
                bits(&want.data[..n * width]),
                "prompt rows"
            );
            // Prefilling the same rows again is refused: the coordinates
            // describe only the rows already cached.
            assert!(
                st.prefill_layer(0, &w, prompt_rows, &prompt_rope, &pc)
                    .is_err()
            );
        }
        for (i, &id) in generated.iter().enumerate() {
            let r = n + i;
            let (rn, rq, rk, rv) = (
                span(&normed, r..r + 1),
                span(&q, r..r + 1),
                span(&k, r..r + 1),
                span(&v, r..r + 1),
            );
            let rows = LayerRows {
                normed: &rn,
                q: &rq,
                k: &rk,
                v: &rv,
            };
            for st in [&mut state, &mut dense, &mut retired] {
                let step = st.feed(id);
                assert_eq!(step.query_page, coords.query_page[r], "row {r} query page");
                let at = r * HEAD_DIM..(r + 1) * HEAD_DIM;
                assert_eq!(
                    bits(&step.rope.cos),
                    bits(&rope.cos[at.clone()]),
                    "row {r} cos"
                );
                assert_eq!(bits(&step.rope.sin), bits(&rope.sin[at]), "row {r} sin");
                let out = st.decode_layer(0, &w, rows, &step).unwrap();
                assert_eq!(
                    bits(&out.data),
                    bits(want.row(r)),
                    "row {r} (token {id}), dense {}, dead {}",
                    st.dense,
                    st.dead_blocks()
                );
            }
        }
        assert_eq!(state.layers[0].rows(), s);
        assert_eq!(state.dead_blocks() + dense.dead_blocks(), 0);
        assert!(
            retired.dead_blocks() > 0,
            "{pages:?}: retirement freed nothing"
        );
        assert!(retired.peak_bytes() <= state.peak_bytes());
    }

    #[test]
    fn dense_switch_reads_truthy_values_only() {
        for (value, on) in [
            (None, false),
            (Some(""), false),
            (Some("0"), false),
            (Some("off"), false),
            (Some("1"), true),
            (Some("true"), true),
            (Some("ON"), true),
        ] {
            assert_eq!(dense_enabled_for(value), on, "{value:?}");
        }
    }

    #[test]
    fn router_weights_load_by_layer_and_refuse_a_missing_tensor() {
        let hidden = decoder_config::HIDDEN_SIZE;
        let p = "model.layers.3.self_attn.dash_router";
        let proj: Vec<f32> = (0..HEAD_DIM * hidden).map(|i| (i % 7) as f32).collect();
        let mut tensors = vec![
            (
                format!("{p}.q_proj.weight"),
                vec![HEAD_DIM, hidden],
                proj.clone(),
            ),
            (
                format!("{p}.k_proj.weight"),
                vec![HEAD_DIM, hidden],
                proj.clone(),
            ),
        ];
        for name in ["q_norm.weight", "k_norm.weight", "head_cls"] {
            tensors.push((format!("{p}.{name}"), vec![HEAD_DIM], vec![0.5; HEAD_DIM]));
        }
        // A raw F32 safetensors blob of exactly these tensors.
        let (mut entries, mut payload) = (Vec::new(), Vec::new());
        for (name, shape, data) in &tensors {
            let beg = payload.len();
            payload.extend(data.iter().flat_map(|x| x.to_le_bytes()));
            entries.push(format!(
                "\"{name}\":{{\"dtype\":\"F32\",\"shape\":{shape:?},\"data_offsets\":[{beg},{}]}}",
                payload.len()
            ));
        }
        let header = format!("{{{}}}", entries.join(","));
        let mut blob = (header.len() as u64).to_le_bytes().to_vec();
        blob.extend_from_slice(header.as_bytes());
        blob.extend_from_slice(&payload);
        let weights = Weights::from_bytes(blob).unwrap();
        let w = SrSwaLayerWeights::load(&weights, 3).unwrap();
        assert_eq!((w.hidden, w.q_proj.len()), (hidden, HEAD_DIM * hidden));
        assert_eq!(w.k_proj, proj);
        assert_eq!(w.head_cls, vec![0.5; HEAD_DIM]);
        let missing = SrSwaLayerWeights::load(&weights, 4)
            .unwrap_err()
            .to_string();
        assert!(
            missing.contains("model.layers.4.self_attn.dash_router"),
            "{missing}"
        );
        assert!(
            SrSwaLayerWeights::load_all(&weights).is_err(),
            "layer 0 is absent"
        );
    }
}