franken_ocr 0.8.0

Pure-Rust, CPU-hyper-optimized runner for the Baidu Unlimited-OCR model (single-binary CLI: focr)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
//! SAM ViT-B encoder forward ([SPEC-040..046], PROPOSED_ARCHITECTURE.md §6.3).
//! Vision config provenance: SAM ViT-B width 768 / depth 12 / 12 heads /
//! global attention at [2,5,8,11], image size 1024 ([SPEC-017]).
//!
//! Real fp32 forward. Patch-embed Conv2d k16s16 -> 64x64 tokens (width 768);
//! learned `pos_embed` (1,64,64,768) bicubic-interpolated to the runtime grid;
//! 12 [`Block`]s with window attention (`window=14`, OQ-15) except global at
//! `[2,5,8,11]`; decomposed relative-position bias added to the SDPA logits; the
//! neck (Conv2d 768->256 k1 -> LayerNorm2d -> Conv2d 256->256 k3 p1 ->
//! LayerNorm2d) then two stride-2 downsamples (`net_2` 256->512, `net_3`
//! 512->1024) -> `[B, 1024, 16, 16]`, returned as a row-major `Mat` of shape
//! `[1024, 16*16]` (channels x flattened spatial), matching the `flatten(2)`
//! layout the bridge concatenates (OQ-6: `local_features_1.flatten(2)`).
//!
//! Weights are owned by the parallel weights wave; this module operates over a
//! [`SamWeights`] parameter bundle so the full math is unit-testable on tiny
//! synthetic inputs with no model present. The public [`forward`] entrypoint
//! adapts a loaded [`Weights`] into a [`SamWeights`] through the `.focrq`
//! reader's named-tensor accessors (bd-1es.3) and runs the real SAM forward.

use rayon::prelude::*;

use super::nn;
use super::tensor::Mat;
use super::weights::Weights;
use crate::error::{FocrError, FocrResult};

// Clock seam: `std::time::Instant` traps on wasm32-unknown-unknown; `web-time`
// re-exports std's types on native targets, so native behavior is unchanged.
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

// ── fixed SAM-ViT-B geometry ([SPEC-040..046]) ─────────────────────────────

/// Patch embedding / transformer width.
pub const EMBED_DIM: usize = 768;
/// Number of transformer blocks.
pub const DEPTH: usize = 12;
/// Attention heads per block.
pub const NUM_HEADS: usize = 12;
/// Per-head channel count (`768 / 12 = 64`).
pub const HEAD_DIM: usize = EMBED_DIM / NUM_HEADS;
/// Patch-embed kernel / stride (`16`), so `1024 -> 64`.
pub const PATCH: usize = 16;
/// Window size for non-global blocks (OQ-15: 14).
pub const WINDOW: usize = 14;
/// Block indices that run global (full-grid) attention.
pub const GLOBAL_BLOCKS: [usize; 4] = [2, 5, 8, 11];
/// Neck / `out_chans` channel count.
pub const NECK_CH: usize = 256;
/// `net_2` output channels.
pub const NET2_CH: usize = 512;
/// `net_3` output channels (the returned feature width).
pub const OUT_CH: usize = 1024;
/// LayerNorm eps for the transformer norms and `LayerNorm2d`.
pub const LN_EPS: f32 = 1e-6;
/// MLP hidden = `dim * mlp_ratio` (`mlp_ratio = 4`).
pub const MLP_HIDDEN: usize = EMBED_DIM * 4;

fn checked_shape_mul(context: &str, lhs: usize, rhs: usize, expression: &str) -> FocrResult<usize> {
    lhs.checked_mul(rhs).ok_or_else(|| {
        FocrError::Other(anyhow::anyhow!(
            "{context}: usize overflow computing {expression} ({lhs} * {rhs})"
        ))
    })
}

fn checked_shape_add(context: &str, lhs: usize, rhs: usize, expression: &str) -> FocrResult<usize> {
    lhs.checked_add(rhs).ok_or_else(|| {
        FocrError::Other(anyhow::anyhow!(
            "{context}: usize overflow computing {expression} ({lhs} + {rhs})"
        ))
    })
}

fn checked_shape_sub(context: &str, lhs: usize, rhs: usize, expression: &str) -> FocrResult<usize> {
    lhs.checked_sub(rhs).ok_or_else(|| {
        FocrError::Other(anyhow::anyhow!(
            "{context}: usize underflow computing {expression} ({lhs} - {rhs})"
        ))
    })
}

fn checked_nchw_len(
    context: &str,
    ch: usize,
    h: usize,
    w: usize,
    expression: &str,
) -> FocrResult<usize> {
    let hw = checked_shape_mul(context, h, w, "h*w")?;
    checked_shape_mul(context, ch, hw, expression)
}

fn checked_conv_weight_len(
    context: &str,
    out_ch: usize,
    in_ch: usize,
    kh: usize,
    kw: usize,
) -> FocrResult<usize> {
    let out_in = checked_shape_mul(context, out_ch, in_ch, "out_ch*in_ch")?;
    let kernel = checked_shape_mul(context, kh, kw, "kh*kw")?;
    checked_shape_mul(context, out_in, kernel, "out_ch*in_ch*kh*kw")
}

// ── parameter bundles ──────────────────────────────────────────────────────

/// A `nn.Linear` parameter pair, weight pre-transposed to the GEMM-ready
/// `[in, out]` row-major layout plus a length-`out` bias.
///
/// PyTorch stores `Linear.weight` as `[out_features, in_features]`;
/// [`Self::from_row_major`] transposes it ONCE at construction so `apply` is a
/// straight matmul — bd-av64.10 measured the old transpose-at-apply-time as
/// hundreds of MB of pure data movement per vision forward, and TrOMR's AR
/// loop paid it PER DECODE STEP. Same floats in a different order ⇒ outputs
/// byte-identical. The weight field is private so a mis-shaped Linear is
/// unrepresentable outside the validating constructor.
#[derive(Debug, Clone)]
pub struct Linear {
    /// Weight pre-transposed to `[in_, out]` row-major.
    wt: Mat,
    /// Length-`out` bias (may be empty for `bias=False`).
    pub b: Vec<f32>,
    /// Output features.
    pub out: usize,
    /// Input features.
    pub in_: usize,
}

impl Linear {
    /// Build from a PyTorch `[out, in]` row-major weight, transposing once and
    /// validating every length at construction (fail-fast, so `apply` never
    /// discovers a malformed bundle mid-forward).
    ///
    /// # Errors
    /// [`FocrError::Other`] when `w.len() != out*in` or a non-empty bias has
    /// `b.len() != out`.
    pub fn from_row_major(w: &[f32], b: Vec<f32>, out: usize, in_: usize) -> FocrResult<Self> {
        let expected_weight_len = checked_shape_mul("vision_sam linear", out, in_, "out*in")?;
        if w.len() != expected_weight_len {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam linear: weight len {} != out*in {}",
                w.len(),
                expected_weight_len
            )));
        }
        if !b.is_empty() && b.len() != out {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam linear: bias len {} != out_features {}",
                b.len(),
                out
            )));
        }
        // w is [out, in]; transpose to [in, out] so matmul([m,in],[in,out]).
        let wt = Mat::from_vec(in_, out, transpose(w, out, in_));
        Ok(Self { wt, b, out, in_ })
    }

    /// `y[m,out] = x[m,in] @ w^T + b`. `x.cols` must equal `self.in_`. Also the
    /// GOT `mm_projector_vary` connector (a plain Linear(1024→1024)+bias).
    ///
    /// # Errors
    /// [`FocrError::Other`] if `x.cols != self.in_` or the public shape
    /// metadata was corrupted after construction.
    pub fn apply(&self, x: &Mat) -> FocrResult<Mat> {
        if x.cols != self.in_ {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam linear: input cols {} != expected in_features {}",
                x.cols,
                self.in_
            )));
        }
        if self.wt.rows != self.in_ || self.wt.cols != self.out {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam linear: pretransposed weight shape [{}, {}] != [in,out] [{}, {}]",
                self.wt.rows,
                self.wt.cols,
                self.in_,
                self.out
            )));
        }
        if !self.b.is_empty() && self.b.len() != self.out {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam linear: bias len {} != out_features {}",
                self.b.len(),
                self.out
            )));
        }
        let mut y = nn::matmul(x, &self.wt)?;
        if !self.b.is_empty() {
            for r in 0..y.rows {
                let row = y.row_mut(r);
                for (c, v) in row.iter_mut().enumerate() {
                    *v += self.b[c];
                }
            }
        }
        Ok(y)
    }
}

/// A LayerNorm affine pair over `cols` features.
#[derive(Debug, Clone)]
pub struct LayerNormP {
    /// Length-`cols` gain.
    pub w: Vec<f32>,
    /// Length-`cols` shift.
    pub b: Vec<f32>,
}

/// A 2-D convolution weight (`[out_ch, in_ch, kh, kw]` row-major) + optional
/// bias.
#[derive(Debug, Clone)]
pub struct Conv {
    /// `[out_ch, in_ch, kh, kw]` row-major.
    pub w: Vec<f32>,
    /// Optional length-`out_ch` bias.
    pub b: Option<Vec<f32>>,
    /// Output channels.
    pub out_ch: usize,
    /// Input channels.
    pub in_ch: usize,
    /// Kernel height.
    pub kh: usize,
    /// Kernel width.
    pub kw: usize,
}

/// Per-block attention parameters (qkv fused, output proj, rel-pos tables).
#[derive(Debug, Clone)]
pub struct AttnP {
    /// Fused qkv linear: `[3*dim, dim]` weight, `3*dim` bias.
    pub qkv: Linear,
    /// Output projection `[dim, dim]`.
    pub proj: Linear,
    /// `rel_pos_h`: `[2*size_h - 1, head_dim]` row-major.
    pub rel_pos_h: Vec<f32>,
    /// `rel_pos_w`: `[2*size_w - 1, head_dim]` row-major.
    pub rel_pos_w: Vec<f32>,
    /// Rel-pos table spatial size along H (window for windowed blocks, 64 for
    /// global). `rel_pos_h` has `2*size_h - 1` rows.
    pub size_h: usize,
    /// Rel-pos table spatial size along W.
    pub size_w: usize,
}

/// One transformer block's parameters.
#[derive(Debug, Clone)]
pub struct BlockP {
    /// `norm1` (pre-attention).
    pub norm1: LayerNormP,
    /// Attention.
    pub attn: AttnP,
    /// `norm2` (pre-MLP).
    pub norm2: LayerNormP,
    /// MLP `lin1` (`dim -> 4*dim`).
    pub lin1: Linear,
    /// MLP `lin2` (`4*dim -> dim`).
    pub lin2: Linear,
    /// Effective window size (0 => global block).
    pub window: usize,
}

/// The full SAM-ViT-B parameter set.
#[derive(Debug, Clone)]
pub struct SamWeights {
    /// Patch-embed conv (`3 -> 768`, k16 s16).
    pub patch_embed: Conv,
    /// Learned abs pos-embed, row-major `[grid_h, grid_w, dim]` (canonical
    /// `64x64x768`).
    pub pos_embed: Vec<f32>,
    /// Pos-embed source grid height (canonical 64).
    pub pos_grid_h: usize,
    /// Pos-embed source grid width (canonical 64).
    pub pos_grid_w: usize,
    /// The 12 transformer blocks.
    pub blocks: Vec<BlockP>,
    /// Neck conv1 (`768 -> 256`, k1, no bias).
    pub neck_conv1: Conv,
    /// Neck LayerNorm2d #1 (`256`).
    pub neck_ln1: LayerNormP,
    /// Neck conv2 (`256 -> 256`, k3 p1, no bias).
    pub neck_conv2: Conv,
    /// Neck LayerNorm2d #2 (`256`).
    pub neck_ln2: LayerNormP,
    /// `net_2` (`256 -> 512`, k3 s2 p1, no bias).
    pub net2: Conv,
    /// `net_3` (`512 -> 1024`, k3 s2 p1, no bias).
    pub net3: Conv,
}

// ── public entrypoints ─────────────────────────────────────────────────────

/// Run the SAM tower over a normalized `[3, H, W]` image, returning the `x3`
/// 1024-channel feature flattened to `[1024, (H/64spatial)^2]` — i.e. the
/// `net_3` output `[1, 1024, 16, 16]` reshaped channel-major
/// (`flatten(2)` layout, OQ-6).
///
/// # Errors
/// Propagates accessor errors from building the [`SamWeights`] (e.g. a missing
/// or mis-shaped `model.sam_model.*` tensor), the input-shape checks above, and
/// whatever [`forward_with`] returns. The real math lives in [`forward_with`],
/// which is exercised by the unit tests below.
pub fn forward(weights: &Weights, image: &Mat) -> FocrResult<Mat> {
    forward_prefix(weights, image, "model.sam_model")
}

/// [`forward`] with an explicit `.focrq` tensor-name prefix for the SAM-family
/// vision tower — Baidu Unlimited-OCR uses `model.sam_model` (the default), GOT-OCR2
/// uses `model.vision_tower_high`; the leaf tensor names + all geometry are identical.
///
/// # Errors
/// As [`forward`] — the first vision-stage error (missing/mis-shaped tensor or a
/// kernel failure).
pub fn forward_prefix(weights: &Weights, image: &Mat, prefix: &str) -> FocrResult<Mat> {
    if image.rows != 3 {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam::forward: expected 3 input channels, got {}",
            image.rows
        )));
    }
    // Base-mode vision input is square: image.cols == H*W with H == W.
    let side = (image.cols as f64).sqrt() as usize;
    if side * side != image.cols {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam::forward: image.cols {} is not a perfect square",
            image.cols
        )));
    }
    let th = Instant::now();
    let w = sam_weights_from(weights, prefix)?;
    super::timing_log(&format!(
        "    sam.hydrate {:.2}s",
        th.elapsed().as_secs_f64()
    ));
    let tf = Instant::now();
    let out = forward_with(&w, image, side, side);
    super::timing_log(&format!(
        "    sam.forward {:.2}s",
        tf.elapsed().as_secs_f64()
    ));
    out
}

/// Build a [`SamWeights`] from the named `{prefix}.*` tensors in `weights`
/// (BF16→f32 widened at the accessor). Dims are read from each tensor's shape;
/// rel-pos table sizes are derived from the stored rows. `pub(crate)` so the
/// batch spine (bd-1azu.10) hydrates ONCE per batch instead of once per view.
pub(crate) fn sam_weights_from(weights: &Weights, prefix: &str) -> FocrResult<SamWeights> {
    // The embed/neck/per-block builders are the SAME functions the streamed
    // forward uses (bd-4l71), so the two hydration modes cannot drift; only
    // WHERE the hydrated blocks live differs. Hydration ORDER (patch/pos ->
    // blocks -> neck) is the historical one, so error precedence on malformed
    // artifacts is unchanged.
    let (patch_embed, pos_embed, pos_grid_h, pos_grid_w) = sam_embed_from(weights, prefix)?;
    let mut blocks = Vec::with_capacity(DEPTH);
    for i in 0..DEPTH {
        blocks.push(sam_block_from(weights, prefix, i)?);
    }
    let mut w = sam_neck_shell(
        weights,
        prefix,
        patch_embed,
        pos_embed,
        pos_grid_h,
        pos_grid_w,
    )?;
    w.blocks = blocks;
    Ok(w)
}

/// Hydrate ONE SAM transformer block from the `{prefix}.blocks.{i}.*` tensors —
/// the exact per-block build [`sam_weights_from`] performs (it calls this), so
/// the streamed per-block forward (bd-4l71 wasm-lane residency) and the cached
/// whole-tower hydration CANNOT drift.
pub(crate) fn sam_block_from(weights: &Weights, prefix: &str, i: usize) -> FocrResult<BlockP> {
    let flat = |n: &str| weights.vec(n);
    let ln = |n: &str| -> FocrResult<LayerNormP> {
        Ok(LayerNormP {
            w: flat(&format!("{n}.weight"))?,
            b: flat(&format!("{n}.bias"))?,
        })
    };
    let b = format!("{prefix}.blocks.{i}");
    let window = if GLOBAL_BLOCKS.contains(&i) {
        0
    } else {
        WINDOW
    };
    let qkv_name = format!("{b}.attn.qkv.weight");
    let proj_name = format!("{b}.attn.proj.weight");
    let rph_name = format!("{b}.attn.rel_pos_h");
    let rpw_name = format!("{b}.attn.rel_pos_w");
    let lin1_name = format!("{b}.mlp.lin1.weight");
    let lin2_name = format!("{b}.mlp.lin2.weight");
    let (q_out, q_in) = tensor_rank2_shape(weights, &qkv_name)?;
    let (proj_out, proj_in) = tensor_rank2_shape(weights, &proj_name)?;
    let (rph_rows, _rph_cols) = tensor_rank2_shape(weights, &rph_name)?;
    let (rpw_rows, _rpw_cols) = tensor_rank2_shape(weights, &rpw_name)?;
    let (lin1_out, lin1_in) = tensor_rank2_shape(weights, &lin1_name)?;
    let (lin2_out, lin2_in) = tensor_rank2_shape(weights, &lin2_name)?;
    Ok(BlockP {
        norm1: ln(&format!("{b}.norm1"))?,
        attn: AttnP {
            qkv: Linear::from_row_major(
                &flat(&qkv_name)?,
                flat(&format!("{b}.attn.qkv.bias"))?,
                q_out,
                q_in,
            )?,
            proj: Linear::from_row_major(
                &flat(&proj_name)?,
                flat(&format!("{b}.attn.proj.bias"))?,
                proj_out,
                proj_in,
            )?,
            rel_pos_h: flat(&rph_name)?,
            rel_pos_w: flat(&rpw_name)?,
            size_h: rph_rows.div_ceil(2),
            size_w: rpw_rows.div_ceil(2),
        },
        norm2: ln(&format!("{b}.norm2"))?,
        lin1: Linear::from_row_major(
            &flat(&lin1_name)?,
            flat(&format!("{b}.mlp.lin1.bias"))?,
            lin1_out,
            lin1_in,
        )?,
        lin2: Linear::from_row_major(
            &flat(&lin2_name)?,
            flat(&format!("{b}.mlp.lin2.bias"))?,
            lin2_out,
            lin2_in,
        )?,
        window,
    })
}

/// Hydrate the patch-embed conv + abs pos-embed (the pre-block head parts, in
/// the historical order). Shared by [`sam_weights_from`] and [`sam_head_from`].
fn sam_embed_from(weights: &Weights, prefix: &str) -> FocrResult<(Conv, Vec<f32>, usize, usize)> {
    let p = prefix;
    let flat = |n: &str| weights.vec(n);
    let pe = format!("{p}.patch_embed.proj.weight");
    let (pe_out, pe_in, pe_kh, pe_kw) = tensor_rank4_shape(weights, &pe)?;
    let patch_embed = Conv {
        w: flat(&pe)?,
        b: Some(flat(&format!("{p}.patch_embed.proj.bias"))?),
        out_ch: pe_out,
        in_ch: pe_in,
        kh: pe_kh,
        kw: pe_kw,
    };
    let pos_name = format!("{p}.pos_embed");
    let (pgh, pgw) = tensor_pos_grid_shape(weights, &pos_name)?;
    let pos_embed = flat(&pos_name)?;
    Ok((patch_embed, pos_embed, pgh, pgw))
}

/// Hydrate the neck/net convs and assemble a [`SamWeights`] with an EMPTY
/// `blocks` vec (the caller supplies blocks, or streams them per use).
fn sam_neck_shell(
    weights: &Weights,
    prefix: &str,
    patch_embed: Conv,
    pos_embed: Vec<f32>,
    pos_grid_h: usize,
    pos_grid_w: usize,
) -> FocrResult<SamWeights> {
    let p = prefix;
    let flat = |n: &str| weights.vec(n);
    let conv = |n: &str, bias: bool| -> FocrResult<Conv> {
        let d = tensor_min_rank_shape(weights, n, 2)?;
        Ok(Conv {
            w: flat(n)?,
            b: if bias {
                Some(flat(&n.replace(".weight", ".bias"))?)
            } else {
                None
            },
            out_ch: d[0],
            in_ch: d[1],
            kh: d.get(2).copied().unwrap_or(1),
            kw: d.get(3).copied().unwrap_or(1),
        })
    };
    let ln = |n: &str| -> FocrResult<LayerNormP> {
        Ok(LayerNormP {
            w: flat(&format!("{n}.weight"))?,
            b: flat(&format!("{n}.bias"))?,
        })
    };
    Ok(SamWeights {
        patch_embed,
        pos_embed,
        pos_grid_h,
        pos_grid_w,
        blocks: Vec::new(),
        neck_conv1: conv(&format!("{p}.neck.0.weight"), false)?,
        neck_ln1: ln(&format!("{p}.neck.1"))?,
        neck_conv2: conv(&format!("{p}.neck.2.weight"), false)?,
        neck_ln2: ln(&format!("{p}.neck.3"))?,
        net2: conv(&format!("{p}.net_2.weight"), false)?,
        net3: conv(&format!("{p}.net_3.weight"), false)?,
    })
}

/// Hydrate ONLY the small non-block SAM parameters (patch embed, pos embed,
/// neck/net convs — ~10 MB f32) with an EMPTY `blocks` vec: the head of the
/// streamed per-block forward (bd-4l71 wasm-lane residency).
pub(crate) fn sam_head_from(weights: &Weights, prefix: &str) -> FocrResult<SamWeights> {
    let (patch_embed, pos_embed, pgh, pgw) = sam_embed_from(weights, prefix)?;
    sam_neck_shell(weights, prefix, patch_embed, pos_embed, pgh, pgw)
}

fn tensor_min_rank_shape(weights: &Weights, name: &str, min_rank: usize) -> FocrResult<Vec<usize>> {
    let view = weights.tensor(name)?;
    if view.shape.len() < min_rank {
        return Err(FocrError::FormatMismatch(format!(
            "tensor {name:?} has rank {}; expected at least {min_rank}",
            view.shape.len()
        )));
    }
    Ok(view.shape.to_vec())
}

fn tensor_rank2_shape(weights: &Weights, name: &str) -> FocrResult<(usize, usize)> {
    let view = weights.tensor(name)?;
    let [rows, cols] = view.shape else {
        return Err(FocrError::FormatMismatch(format!(
            "tensor {name:?} has rank {}; expected 2 ([rows, cols])",
            view.shape.len()
        )));
    };
    Ok((*rows, *cols))
}

fn tensor_rank4_shape(weights: &Weights, name: &str) -> FocrResult<(usize, usize, usize, usize)> {
    let view = weights.tensor(name)?;
    let [out_ch, in_ch, kh, kw] = view.shape else {
        return Err(FocrError::FormatMismatch(format!(
            "tensor {name:?} has rank {}; expected 4 ([out_ch, in_ch, kh, kw])",
            view.shape.len()
        )));
    };
    Ok((*out_ch, *in_ch, *kh, *kw))
}

fn tensor_pos_grid_shape(weights: &Weights, name: &str) -> FocrResult<(usize, usize)> {
    let view = weights.tensor(name)?;
    match view.shape {
        [_h0, h, w, _c] => Ok((*h, *w)),
        [h, w, _c] => Ok((*h, *w)),
        shape => Err(FocrError::FormatMismatch(format!(
            "tensor {name:?} has rank {}; expected 3 ([h, w, c]) or 4 ([1, h, w, c]), got {shape:?}",
            shape.len()
        ))),
    }
}

/// Run the SAM tower with an explicit parameter bundle over a `[3, H, W]`
/// image (laid out `[in_ch=3, H, W]` row-major in `image.data`, `image.rows=3`,
/// `image.cols=H*W` with `H == W` a multiple of `PATCH`).
///
/// Returns the `net_3` feature as a row-major `[OUT_CH, gh3*gw3]` [`Mat`]
/// (channel-major / `flatten(2)` order).
///
/// # Errors
/// [`FocrError::Other`] on a shape contract violation (non-3-channel input,
/// non-square or non-`PATCH`-divisible spatial dims, or a kernel rejection).
pub fn forward_with(w: &SamWeights, image: &Mat, h: usize, win: usize) -> FocrResult<Mat> {
    forward_core(
        w,
        image,
        h,
        win,
        w.blocks.len(),
        &mut |i| Ok(std::borrow::Cow::Borrowed(&w.blocks[i])),
        false,
    )
}

/// [`forward`] semantics WITHOUT retaining the whole ~0.4 GB f32 SAM tower
/// (bd-4l71 wasm-lane residency): the small head hydrates once
/// ([`sam_head_from`]), then each block is hydrated ([`sam_block_from`] — the
/// SAME builder the cached path uses), run, and DROPPED. Global-attention
/// blocks run the bounded-scratch query-slab kernel
/// ([`attention_global_bounded`], bit-identical — see its gate) so the
/// transient working set stays tens of MB instead of ~0.8 GB.
///
/// # Errors
/// As [`forward_prefix`].
pub(crate) fn forward_streamed(weights: &Weights, image: &Mat, prefix: &str) -> FocrResult<Mat> {
    if image.rows != 3 {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam::forward: expected 3 input channels, got {}",
            image.rows
        )));
    }
    let side = (image.cols as f64).sqrt() as usize;
    if side * side != image.cols {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam::forward: image.cols {} is not a perfect square",
            image.cols
        )));
    }
    let th = Instant::now();
    let head = sam_head_from(weights, prefix)?;
    super::timing_log(&format!(
        "    sam.hydrate(head) {:.2}s",
        th.elapsed().as_secs_f64()
    ));
    let tf = Instant::now();
    let out = forward_core(
        &head,
        image,
        side,
        side,
        DEPTH,
        &mut |i| Ok(std::borrow::Cow::Owned(sam_block_from(weights, prefix, i)?)),
        true,
    );
    super::timing_log(&format!(
        "    sam.forward(streamed) {:.2}s",
        tf.elapsed().as_secs_f64()
    ));
    out
}

/// [`forward_streamed`] over several views at once, hydrating each block ONCE
/// and running EVERY view through it before dropping it (bd-K2 per-view
/// hydration hoist). The head hydrates once as well.
///
/// The `i`-th result is bit-identical to `forward_streamed(weights, images[i],
/// prefix)`: only the loop nest changes (views-inner instead of views-outer),
/// and the views are mathematically independent — see [`forward_core_views`]
/// and the `streamed_views_is_bit_identical_to_per_view` gate.
///
/// Cost: one live activation per view (`[gh·gw, 768]` f32) instead of one, in
/// exchange for `V`× less bf16→f32 block-hydration traffic.
///
/// # Errors
/// As [`forward_streamed`], plus an empty view list.
pub(crate) fn forward_streamed_views(
    weights: &Weights,
    images: &[&Mat],
    prefix: &str,
) -> FocrResult<Vec<Mat>> {
    if images.is_empty() {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam::forward_streamed_views: empty view list"
        )));
    }
    let mut dims = Vec::with_capacity(images.len());
    for image in images {
        if image.rows != 3 {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam::forward: expected 3 input channels, got {}",
                image.rows
            )));
        }
        let side = (image.cols as f64).sqrt() as usize;
        if side * side != image.cols {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam::forward: image.cols {} is not a perfect square",
                image.cols
            )));
        }
        dims.push((side, side));
    }
    let th = Instant::now();
    let head = sam_head_from(weights, prefix)?;
    super::timing_log(&format!(
        "    sam.hydrate(head) {:.2}s",
        th.elapsed().as_secs_f64()
    ));
    let tf = Instant::now();
    let out = forward_core_views(
        &head,
        images,
        &dims,
        DEPTH,
        &mut |i| Ok(std::borrow::Cow::Owned(sam_block_from(weights, prefix, i)?)),
        true,
    );
    super::timing_log(&format!(
        "    sam.forward(streamed, {} views) {:.2}s",
        images.len(),
        tf.elapsed().as_secs_f64()
    ));
    out
}

/// The shared SAM tower core: patch embed + pos embed, `depth` transformer
/// blocks obtained from `block_at` (borrowed from a cached [`SamWeights`] or
/// hydrated per use and dropped), then the neck/net convs. ONE body serves the
/// cached and streamed forwards, so they cannot drift; `low_mem` selects the
/// bounded-scratch global-attention kernel (bit-identical, gated).
#[allow(clippy::too_many_arguments)]
fn forward_core<'w>(
    w: &SamWeights,
    image: &Mat,
    h: usize,
    win: usize,
    depth: usize,
    block_at: &mut dyn FnMut(usize) -> FocrResult<std::borrow::Cow<'w, BlockP>>,
    low_mem: bool,
) -> FocrResult<Mat> {
    let mut out = forward_core_views(
        w,
        std::slice::from_ref(&image),
        &[(h, win)],
        depth,
        block_at,
        low_mem,
    )?;
    Ok(out.remove(0))
}

/// [`forward_core`] over `V` independent views with the **view loop inside the
/// block loop** (bd-K2 per-view hydration hoist): each block is obtained ONCE
/// from `block_at` and applied to every view's activation before the next block
/// is requested. For the streamed lane that turns `V` full hydration sweeps of
/// the bf16 blob into ONE.
///
/// Bit-identity is by construction, not by measurement: view `i`'s activation
/// only ever meets view `i`'s own data, and it passes through exactly the same
/// sequence of kernel calls with exactly the same shapes as it would in the
/// views-outer nest ([`forward_core`] is literally this function at `V == 1`).
/// Reordering *independent* computations cannot move a bit; nothing here is
/// reassociated, restacked, or batched.
///
/// Views may differ in geometry (`dims[i]` is that view's `(H, W)`), since every
/// stage runs per view with that view's own grid.
fn forward_core_views<'w>(
    w: &SamWeights,
    images: &[&Mat],
    dims: &[(usize, usize)],
    depth: usize,
    block_at: &mut dyn FnMut(usize) -> FocrResult<std::borrow::Cow<'w, BlockP>>,
    low_mem: bool,
) -> FocrResult<Vec<Mat>> {
    if images.len() != dims.len() {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam: {} views but {} geometries",
            images.len(),
            dims.len()
        )));
    }
    let dim = w.patch_embed.out_ch;
    // ── per-view validation + patch embed + abs pos-embed. ──────────────────
    // (Identical checks, in the identical order, to the single-view core.)
    let mut grids = Vec::with_capacity(images.len());
    let mut xs = Vec::with_capacity(images.len());
    for (image, &(h, win)) in images.iter().zip(dims.iter()) {
        if image.rows != 3 {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam: expected 3 input channels, got {}",
                image.rows
            )));
        }
        if h == 0 || win == 0 {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam: spatial dims ({h},{win}) must be non-zero"
            )));
        }
        let expected_cols = checked_shape_mul("vision_sam", h, win, "H*W")?;
        if image.cols != expected_cols {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam: image.cols {} != H*W {}*{} ({})",
                image.cols,
                h,
                win,
                expected_cols
            )));
        }
        if !h.is_multiple_of(PATCH) || !win.is_multiple_of(PATCH) {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam: spatial dims ({h},{win}) must be multiples of patch {PATCH}"
            )));
        }
        let gh = h / PATCH;
        let gw = win / PATCH;

        // ── patch embed: Conv2d(3->768, k16, s16), permute B,C,H,W->B,H,W,C.
        // conv2d kernel wants pre-padded NCHW; patch embed has no padding.
        let conv_out = conv_apply(&w.patch_embed, &image.data, h, win, 0, PATCH)?;
        ensure_flat_len("vision_sam patch_embed output", &conv_out, dim, gh, gw)?;
        // conv_out is [1, dim, gh, gw] (channel-major). Tokens we carry as
        // [gh*gw, dim] (spatial-major rows) for the transformer (NHWC flat).
        let mut x = nchw_to_nhwc_rows(&conv_out, dim, gh, gw);

        // ── abs pos-embed (added once before the blocks; bicubic if needed).
        let pos = abs_pos(&w.pos_embed, w.pos_grid_h, w.pos_grid_w, dim, gh, gw)?;
        for (xv, pv) in x.data.iter_mut().zip(pos.iter()) {
            *xv += *pv;
        }
        grids.push((gh, gw));
        xs.push(x);
    }

    // ── 12 transformer blocks; ONE hydration per block, all views inside. ──
    let tb = Instant::now();
    for i in 0..depth {
        let blk = block_at(i)?;
        for (x, &(gh, gw)) in xs.iter_mut().zip(grids.iter()) {
            *x = block_forward_impl(&blk, x, gh, gw, low_mem)?;
            // Sequential, on the thread that entered the forward — the only
            // place a progress event may be raised (see `super::progress`).
            super::progress::vision_step();
        }
    }
    super::timing_log(&format!(
        "    sam.blocks {:.2}s",
        tb.elapsed().as_secs_f64()
    ));

    // ── per-view neck + net_2 + net_3. ──────────────────────────────────────
    let mut out = Vec::with_capacity(xs.len());
    for (x, &(gh, gw)) in xs.iter().zip(grids.iter()) {
        // neck: x is [gh*gw, dim] NHWC rows; neck operates NCHW.
        // permute(0,3,1,2): NHWC-rows -> NCHW flat.
        let x_nchw = nhwc_rows_to_nchw(x, dim, gh, gw);

        // neck conv1: 768 -> 256, k1, no pad.
        let nc1 = conv_apply(&w.neck_conv1, &x_nchw, gh, gw, 0, 1)?;
        ensure_flat_len("vision_sam neck_conv1 output", &nc1, NECK_CH, gh, gw)?;
        let nc1 = layer_norm_2d(&nc1, &w.neck_ln1, NECK_CH, gh, gw)?;
        // neck conv2: 256 -> 256, k3, pad1.
        let nc2 = conv_apply(&w.neck_conv2, &nc1, gh, gw, 1, 1)?;
        ensure_flat_len("vision_sam neck_conv2 output", &nc2, NECK_CH, gh, gw)?;
        let neck = layer_norm_2d(&nc2, &w.neck_ln2, NECK_CH, gh, gw)?;

        // net_2: 256 -> 512, k3, s2, p1 -> grid /2.
        let (gh2, gw2) = (gh.div_ceil(2), gw.div_ceil(2));
        let x2 = conv_apply(&w.net2, &neck, gh, gw, 1, 2)?;
        ensure_flat_len("vision_sam net2 output", &x2, NET2_CH, gh2, gw2)?;
        // net_3: 512 -> 1024, k3, s2, p1 -> grid /2 again.
        let (gh3, gw3) = (gh2.div_ceil(2), gw2.div_ceil(2));
        let x3 = conv_apply(&w.net3, &x2, gh2, gw2, 1, 2)?;
        ensure_flat_len("vision_sam net3 output", &x3, OUT_CH, gh3, gw3)?;

        // x3 is [OUT_CH, gh3*gw3] NCHW flat — exactly flatten(2) layout.
        out.push(Mat::from_vec(OUT_CH, gh3 * gw3, x3));
    }
    Ok(out)
}

/// Batched SAM tower over `V` views (images) in ONE forward (bd-1azu.10).
///
/// Returns one `[OUT_CH, gh3*gw3]` matrix per view, where the `i`-th output is
/// **byte-for-byte identical** to [`forward_with`] called on `images[i]` alone.
///
/// Structure (lossless, doctrine #5 "one live forward"): every view's patch
/// tokens are stacked along a leading batch dim into a single `[V·gh·gw, dim]`
/// buffer. The transformer blocks then run their M-independent stages
/// (`norm1`/`norm2` LayerNorm and the `lin1`/`lin2` MLP linears) once over the
/// whole stack — bit-identical per row because [`nn::layer_norm`] is strictly
/// row-wise and the f32 GEMMs are M-independent. Attention is **block-diagonal**:
/// each block's [`attention`] (or window-partitioned [`attention_windowed`]) runs
/// per view over only that view's own tokens, so a view never attends across
/// into another view. The convs (patch-embed, neck, `net_2`, `net_3`, all cheap
/// relative to attention) run per-view in a sequential loop, exactly as
/// [`forward_with`] does.
///
/// No nested rayon: the outer loops over views/blocks are sequential, so each
/// `nn::matmul` / `conv_apply` / `attention` fans out one-at-a-time
/// (bd-1azu.14, doctrine #5).
///
/// # Errors
/// [`FocrError::Other`] on an empty batch, a ragged batch (a view whose
/// `image.cols != h*win`), a non-3-channel view, non-`PATCH`-divisible or
/// zero spatial dims, or any kernel rejection.
pub fn forward_with_batched(
    w: &SamWeights,
    images: &[&Mat],
    h: usize,
    win: usize,
) -> FocrResult<Vec<Mat>> {
    let v = images.len();
    if v == 0 {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam forward_with_batched: empty view batch"
        )));
    }
    if h == 0 || win == 0 {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam forward_with_batched: spatial dims ({h},{win}) must be non-zero"
        )));
    }
    let expected_cols = checked_shape_mul("vision_sam forward_with_batched", h, win, "H*W")?;
    if !h.is_multiple_of(PATCH) || !win.is_multiple_of(PATCH) {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam forward_with_batched: spatial dims ({h},{win}) must be multiples of patch {PATCH}"
        )));
    }
    for (i, img) in images.iter().enumerate() {
        if img.rows != 3 {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam forward_with_batched: view {i} expected 3 input channels, got {}",
                img.rows
            )));
        }
        if img.cols != expected_cols {
            return Err(FocrError::Other(anyhow::anyhow!(
                "vision_sam forward_with_batched: view {i} cols {} != H*W {}*{} ({expected_cols}) (ragged batch)",
                img.cols,
                h,
                win
            )));
        }
    }

    let gh = h / PATCH;
    let gw = win / PATCH;
    let n = checked_shape_mul("vision_sam forward_with_batched", gh, gw, "gh*gw")?;
    let dim = w.patch_embed.out_ch;

    // The abs pos-embed depends only on (pos_embed, gh, gw) — identical for every
    // view, so build it once and add the same contribution to each view's block.
    let pos = abs_pos(&w.pos_embed, w.pos_grid_h, w.pos_grid_w, dim, gh, gw)?;

    // ── per-view patch_embed → NHWC rows → +pos, stacked into [V*n, dim]. ──
    let row_span = checked_shape_mul("vision_sam forward_with_batched", n, dim, "n*dim")?;
    let total_rows = checked_shape_mul("vision_sam forward_with_batched", v, n, "V*n")?;
    let stacked_len = checked_shape_mul(
        "vision_sam forward_with_batched",
        total_rows,
        dim,
        "V*n*dim",
    )?;
    let mut stacked = vec![0.0f32; stacked_len];
    for (vv, img) in images.iter().enumerate() {
        let conv_out = conv_apply(&w.patch_embed, &img.data, h, win, 0, PATCH)?;
        ensure_flat_len(
            "vision_sam forward_with_batched patch_embed output",
            &conv_out,
            dim,
            gh,
            gw,
        )?;
        let x_view = nchw_to_nhwc_rows(&conv_out, dim, gh, gw);
        let base = vv * row_span;
        let dst = &mut stacked[base..base + row_span];
        for (xv, pv) in dst.iter_mut().zip(x_view.data.iter().zip(pos.iter())) {
            *xv = *pv.0 + *pv.1;
        }
    }
    let mut x = Mat::from_vec(total_rows, dim, stacked);

    // ── 12 transformer blocks over the stacked [V*n, dim] buffer. ──
    for blk in &w.blocks {
        x = block_forward_batched(blk, &x, gh, gw, v)?;
    }

    // ── split back to V per-view [n, dim], run neck + net_2 + net_3 per view. ──
    let mut out = Vec::with_capacity(v);
    for vv in 0..v {
        let base = vv * row_span;
        let x_view = Mat::from_vec(n, dim, x.data[base..base + row_span].to_vec());

        // neck: x_view is [n, dim] NHWC rows; neck operates NCHW.
        let x_nchw = nhwc_rows_to_nchw(&x_view, dim, gh, gw);
        let nc1 = conv_apply(&w.neck_conv1, &x_nchw, gh, gw, 0, 1)?;
        ensure_flat_len(
            "vision_sam forward_with_batched neck_conv1 output",
            &nc1,
            NECK_CH,
            gh,
            gw,
        )?;
        let nc1 = layer_norm_2d(&nc1, &w.neck_ln1, NECK_CH, gh, gw)?;
        let nc2 = conv_apply(&w.neck_conv2, &nc1, gh, gw, 1, 1)?;
        ensure_flat_len(
            "vision_sam forward_with_batched neck_conv2 output",
            &nc2,
            NECK_CH,
            gh,
            gw,
        )?;
        let neck = layer_norm_2d(&nc2, &w.neck_ln2, NECK_CH, gh, gw)?;

        let (gh2, gw2) = (gh.div_ceil(2), gw.div_ceil(2));
        let x2 = conv_apply(&w.net2, &neck, gh, gw, 1, 2)?;
        ensure_flat_len(
            "vision_sam forward_with_batched net2 output",
            &x2,
            NET2_CH,
            gh2,
            gw2,
        )?;
        let (gh3, gw3) = (gh2.div_ceil(2), gw2.div_ceil(2));
        let x3 = conv_apply(&w.net3, &x2, gh2, gw2, 1, 2)?;
        ensure_flat_len(
            "vision_sam forward_with_batched net3 output",
            &x3,
            OUT_CH,
            gh3,
            gw3,
        )?;
        out.push(Mat::from_vec(OUT_CH, gh3 * gw3, x3));
    }
    Ok(out)
}

// ── transformer block ──────────────────────────────────────────────────────

/// One [`BlockP`] over NHWC token rows `[gh*gw, dim]`.
///
/// `shortcut = x; x = norm1(x);` (window_partition if windowed) `x = attn(x);`
/// (window_unpartition); `x = shortcut + x; x = x + mlp(norm2(x))`.
///
/// The forwards now call [`block_forward_impl`] directly (they thread the
/// bd-4l71 `low_mem` selector); this alias pins the HISTORICAL `low_mem=false`
/// contract for the block-level unit tests.
#[cfg_attr(not(test), allow(dead_code))]
fn block_forward(blk: &BlockP, x: &Mat, gh: usize, gw: usize) -> FocrResult<Mat> {
    block_forward_impl(blk, x, gh, gw, false)
}

/// [`block_forward`] with the `low_mem` global-attention selector (bd-4l71):
/// `false` is byte-for-byte the historical path; `true` routes GLOBAL blocks
/// through the bounded-scratch [`attention_global_bounded`] (bit-identical —
/// gated by `bounded_global_attention_is_bit_identical`). Windowed blocks are
/// identical either way (their per-window logits are already tiny).
fn block_forward_impl(
    blk: &BlockP,
    x: &Mat,
    gh: usize,
    gw: usize,
    low_mem: bool,
) -> FocrResult<Mat> {
    let normed = layer_norm_rows(x, &blk.norm1)?;

    let ta = Instant::now();
    let attn_out = if blk.window > 0 {
        attention_windowed(&blk.attn, &normed, gh, gw, blk.window)?
    } else if low_mem {
        attention_global_bounded(&blk.attn, &normed, gh, gw)?
    } else {
        attention(&blk.attn, &normed, gh, gw, None)?
    };
    super::timing_log(&format!(
        "      sam.block attn({}) {:.3}s",
        if blk.window > 0 { "win" } else { "GLOBAL" },
        ta.elapsed().as_secs_f64()
    ));
    ensure_same_shape("vision_sam block attention residual", &attn_out, x)?;

    // residual 1: x = shortcut + attn
    let mut h1 = x.clone();
    for (a, b) in h1.data.iter_mut().zip(attn_out.data.iter()) {
        *a += *b;
    }

    // residual 2: x = h1 + mlp(norm2(h1))
    let tm = Instant::now();
    let normed2 = layer_norm_rows(&h1, &blk.norm2)?;
    let mlp = if low_mem {
        mlp_row_chunked(blk, &normed2)?
    } else {
        let mut hidden = blk.lin1.apply(&normed2)?;
        nn::gelu(&mut hidden);
        blk.lin2.apply(&hidden)?
    };
    super::timing_log(&format!(
        "      sam.block mlp {:.3}s",
        tm.elapsed().as_secs_f64()
    ));
    ensure_same_shape("vision_sam block mlp residual", &mlp, &h1)?;
    for (a, b) in h1.data.iter_mut().zip(mlp.data.iter()) {
        *a += *b;
    }
    Ok(h1)
}

/// Token rows the low-memory block MLP processes per pass. At the SAM tower's
/// 4096-token, 3072-wide hidden this trades ONE 48 MB `lin1` output for a
/// 6 MB scratch (the `[n, dim]` result is allocated either way); the chunk is
/// still far above the GEMM kernels' internal parallel thresholds, so the cost
/// is unmeasurable next to the residency win.
const MLP_ROW_CHUNK: usize = 512;

/// `lin2(gelu(lin1(x)))` computed in [`MLP_ROW_CHUNK`]-row passes — the
/// bd-4l71 low-memory twin of the whole-tensor MLP, so the streamed vision
/// lane never materializes the full `[n, mlp_dim]` hidden.
///
/// BIT-IDENTICAL to the whole-tensor form: every op here is row-independent —
/// [`nn::matmul`] row blocks are exact (gated by
/// `matmul_is_row_block_invariant`), the `Linear` bias add is per-row, and
/// [`nn::gelu`] is element-wise. Chunk `[r0, r1)` therefore sees exactly the
/// inputs and produces exactly the values the whole-tensor pass wrote there.
///
/// # Errors
/// Whatever [`Linear::apply`] rejects (a shape/metadata mismatch).
fn mlp_row_chunked(blk: &BlockP, normed2: &Mat) -> FocrResult<Mat> {
    mlp_row_chunked_with(blk, normed2, MLP_ROW_CHUNK)
}

/// [`mlp_row_chunked`] with an explicit chunk height, so the bit-identity gate
/// (`row_chunked_block_mlp_is_bit_identical`) can sweep partitions.
///
/// # Errors
/// Whatever [`Linear::apply`] rejects (a shape/metadata mismatch).
fn mlp_row_chunked_with(blk: &BlockP, normed2: &Mat, chunk: usize) -> FocrResult<Mat> {
    if chunk == 0 {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam mlp_row_chunked: row chunk must be non-zero"
        )));
    }
    let (rows, cols) = (normed2.rows, normed2.cols);
    let mut out: Vec<f32> = Vec::with_capacity(rows * blk.lin2.out);
    let mut start = 0usize;
    while start < rows {
        let take = chunk.min(rows - start);
        let chunk = Mat::from_vec(
            take,
            cols,
            normed2.data[start * cols..(start + take) * cols].to_vec(),
        );
        let mut hidden = blk.lin1.apply(&chunk)?;
        nn::gelu(&mut hidden);
        let done = blk.lin2.apply(&hidden)?;
        out.extend_from_slice(&done.data);
        start += take;
    }
    Ok(Mat::from_vec(rows, blk.lin2.out, out))
}

/// Batched analogue of [`block_forward`] over a stacked `[V*n, dim]` buffer
/// (`n = gh*gw`), bd-1azu.10.
///
/// `norm1`, `norm2` and the MLP (`lin1`/`gelu`/`lin2`) run once over the whole
/// `V*n`-row stack — byte-identical per row to the per-view forward because
/// [`nn::layer_norm`] is row-wise, [`nn::gelu`] is element-wise, and the GEMMs in
/// [`Linear::apply`] are M-independent. Attention is computed **per view** over a
/// slice of the (row-wise-identical) normalized buffer, via the same
/// [`attention`] / [`attention_windowed`] the per-view path uses — so a view
/// never attends across into another view's tokens (block-diagonal). The two
/// residual adds are element-wise over the stack.
fn block_forward_batched(blk: &BlockP, x: &Mat, gh: usize, gw: usize, v: usize) -> FocrResult<Mat> {
    let dim = x.cols;
    let n = checked_shape_mul("vision_sam block_forward_batched", gh, gw, "gh*gw")?;
    let row_span = checked_shape_mul("vision_sam block_forward_batched", n, dim, "n*dim")?;
    let total_rows = checked_shape_mul("vision_sam block_forward_batched", v, n, "V*n")?;
    if x.rows != total_rows {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam block_forward_batched: x.rows {} != V*n {total_rows}",
            x.rows
        )));
    }
    ensure_flat_len("vision_sam block_forward_batched input", &x.data, v, n, dim)?;

    // norm1 over the whole stack (row-wise -> identical to per-view).
    let normed = layer_norm_rows(x, &blk.norm1)?;

    // Attention per view (block-diagonal): slice this view's normalized rows and
    // run the exact same attention the per-view forward uses.
    let mut attn_data = vec![0.0f32; x.data.len()];
    for view in 0..v {
        let base = view * row_span;
        let normed_view = Mat::from_vec(n, dim, normed.data[base..base + row_span].to_vec());
        let av = if blk.window > 0 {
            attention_windowed(&blk.attn, &normed_view, gh, gw, blk.window)?
        } else {
            attention(&blk.attn, &normed_view, gh, gw, None)?
        };
        ensure_mat_shape(
            &av,
            n,
            dim,
            "vision_sam block_forward_batched attention view",
        )?;
        attn_data[base..base + row_span].copy_from_slice(&av.data);
    }

    // residual 1: x = shortcut + attn (element-wise over the stack).
    let mut h1 = x.clone();
    for (a, b) in h1.data.iter_mut().zip(attn_data.iter()) {
        *a += *b;
    }

    // residual 2: x = h1 + mlp(norm2(h1)), all batched over the stack.
    let normed2 = layer_norm_rows(&h1, &blk.norm2)?;
    let mut mlp = blk.lin1.apply(&normed2)?;
    nn::gelu(&mut mlp);
    let mlp = blk.lin2.apply(&mlp)?;
    ensure_same_shape("vision_sam block_forward_batched mlp residual", &mlp, &h1)?;
    for (a, b) in h1.data.iter_mut().zip(mlp.data.iter()) {
        *a += *b;
    }
    Ok(h1)
}

/// Window-partitioned multi-head attention over `norm1`-normalized NHWC token
/// rows `[gh*gw, dim]`: pad to a multiple of `ws`, run [`attention`] once per
/// `ws×ws` window (zero-padded tail tokens included exactly as upstream), then
/// `window_unpartition` back to `[gh*gw, dim]` stripping the padding.
///
/// Extracted verbatim from the windowed branch of [`block_forward`] so the
/// per-view (sequential) and batched paths share one definition — the batched
/// SAM tower (bd-1azu.10) calls this per view, guaranteeing byte-identical
/// output to the per-view forward.
fn attention_windowed(p: &AttnP, normed: &Mat, gh: usize, gw: usize, ws: usize) -> FocrResult<Mat> {
    let dim = normed.cols;
    // window_partition: pad to multiple of window, tile into win x win.
    let pad_h = (ws - gh % ws) % ws;
    let pad_w = (ws - gw % ws) % ws;
    let hp = gh + pad_h;
    let wp = gw + pad_w;
    let nwin_h = hp / ws;
    let nwin_w = wp / ws;
    let nwin = nwin_h * nwin_w;

    // Build per-window token blocks [nwin][ws*ws, dim] (zero-padded tail).
    let mut windows = vec![0.0f32; nwin * ws * ws * dim];
    for wy in 0..nwin_h {
        for wx in 0..nwin_w {
            let widx = wy * nwin_w + wx;
            for ly in 0..ws {
                for lx in 0..ws {
                    let gy = wy * ws + ly;
                    let gxx = wx * ws + lx;
                    let dst = ((widx * ws + ly) * ws + lx) * dim;
                    if gy < gh && gxx < gw {
                        let src = (gy * gw + gxx) * dim;
                        windows[dst..dst + dim].copy_from_slice(&normed.data[src..src + dim]);
                    }
                }
            }
        }
    }

    // Attention per window (each window: ws x ws grid). The rel-pos tables
    // depend only on the window size — compute them ONCE for all 25 windows
    // instead of per window (bd-av64.10).
    let nh = NUM_HEADS;
    let hd = dim / nh;
    let rh = get_rel_pos(ws, ws, &p.rel_pos_h, p.size_h, hd);
    let rw = get_rel_pos(ws, ws, &p.rel_pos_w, p.size_w, hd);
    // Windows are independent — run them across the pool (bd-av64.10: the
    // serial loop left ~90% of cores idle because each 196-token window sits
    // below the inner kernels' own parallel thresholds). Per-window
    // arithmetic is unchanged and outputs land in disjoint spans, so the
    // result is bit-identical to the serial loop.
    let win_span = ws * ws * dim;
    let mut out_windows = vec![0.0f32; windows.len()];
    out_windows
        .par_chunks_mut(win_span)
        .enumerate()
        .try_for_each(|(widx, out_chunk)| -> FocrResult<()> {
            let base = widx * win_span;
            let win_in = Mat::from_vec(ws * ws, dim, windows[base..base + win_span].to_vec());
            let win_out = attention(p, &win_in, ws, ws, Some((&rh, &rw)))?;
            out_chunk.copy_from_slice(&win_out.data);
            Ok(())
        })?;

    // window_unpartition: scatter back, strip padding.
    let mut merged = vec![0.0f32; gh * gw * dim];
    for wy in 0..nwin_h {
        for wx in 0..nwin_w {
            let widx = wy * nwin_w + wx;
            for ly in 0..ws {
                for lx in 0..ws {
                    let gy = wy * ws + ly;
                    let gxx = wx * ws + lx;
                    if gy < gh && gxx < gw {
                        let src = ((widx * ws + ly) * ws + lx) * dim;
                        let dst = (gy * gw + gxx) * dim;
                        merged[dst..dst + dim].copy_from_slice(&out_windows[src..src + dim]);
                    }
                }
            }
        }
    }
    Ok(Mat::from_vec(gh * gw, dim, merged))
}

// ── attention with decomposed relative position ([SPEC-044]) ───────────────

/// Multi-head attention over a `gh x gw` token grid `[gh*gw, dim]`, adding the
/// decomposed rel-pos bias to the logits before softmax. Returns `proj(out)`
/// shaped `[gh*gw, dim]`.
fn attention(
    p: &AttnP,
    x: &Mat,
    gh: usize,
    gw: usize,
    relpos: Option<(&[f32], &[f32])>,
) -> FocrResult<Mat> {
    let n = gh * gw;
    if x.rows != n {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam attention: input rows {} != gh*gw {}*{}",
            x.rows,
            gh,
            gw
        )));
    }
    let dim = x.cols;
    let nh = NUM_HEADS;
    if dim == 0 || !dim.is_multiple_of(nh) {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam attention: dim {} must be non-zero and divisible by heads {}",
            dim,
            nh
        )));
    }
    if p.qkv.in_ != dim || p.qkv.out != 3 * dim {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam attention: qkv shape out/in {}x{} incompatible with dim {}",
            p.qkv.out,
            p.qkv.in_,
            dim
        )));
    }
    if p.proj.in_ != dim || p.proj.out != dim {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam attention: proj shape out/in {}x{} incompatible with dim {}",
            p.proj.out,
            p.proj.in_,
            dim
        )));
    }
    let hd = dim / nh;
    ensure_rel_pos_len("rel_pos_h", p.size_h, hd, p.rel_pos_h.len())?;
    ensure_rel_pos_len("rel_pos_w", p.size_w, hd, p.rel_pos_w.len())?;
    let scale = (hd as f32).powf(-0.5);

    // qkv: [n, 3*dim].
    let qkv = p.qkv.apply(x)?;
    ensure_mat_shape(&qkv, n, 3 * dim, "vision_sam attention qkv output")?;
    // Split into per-head q,k,v: layout [n, 3, nh, hd] (PyTorch reshape order).
    // qkv row r: [3*dim] = for s in 0..3 { for head in 0..nh { hd } }.
    // We build flat per-head buffers [nh][n, hd] for q,k,v.
    let mut q = vec![0.0f32; nh * n * hd];
    let mut k = vec![0.0f32; nh * n * hd];
    let mut v = vec![0.0f32; nh * n * hd];
    for r in 0..n {
        let row = qkv.row(r);
        for head in 0..nh {
            // Each (row, head) segment is hd contiguous floats in both the
            // source row and the per-head destination — copy, don't loop.
            let dst = (head * n + r) * hd;
            q[dst..dst + hd].copy_from_slice(&row[head * hd..(head + 1) * hd]);
            k[dst..dst + hd].copy_from_slice(&row[(nh + head) * hd..(nh + head + 1) * hd]);
            v[dst..dst + hd].copy_from_slice(&row[(2 * nh + head) * hd..(2 * nh + head + 1) * hd]);
        }
    }

    // Decomposed rel-pos bias [nh][n(q), n(k)] = rel_h[qy, ky] + rel_w[qx, kx].
    // Rh = get_rel_pos(gh, gh, rel_pos_h) -> [gh, gh, hd]
    // Rw = get_rel_pos(gw, gw, rel_pos_w) -> [gw, gw, hd]
    // rel_h[head, q, ky] = sum_c q[head,q,c] * Rh[qy, ky, c]
    // rel_w[head, q, kx] = sum_c q[head,q,c] * Rw[qx, kx, c]
    //
    // The tables depend only on (gh, gw) — the windowed path passes them in,
    // computed once per block instead of once per window (bd-av64.10).
    let (rh_owned, rw_owned);
    let (rh, rw): (&[f32], &[f32]) = match relpos {
        Some((rh, rw)) => (rh, rw),
        None => {
            rh_owned = get_rel_pos(gh, gh, &p.rel_pos_h, p.size_h, hd);
            rw_owned = get_rel_pos(gw, gw, &p.rel_pos_w, p.size_w, hd);
            (&rh_owned, &rw_owned)
        }
    };

    // Compute attention head-by-head with explicit logits so we can add bias.
    // Heads are independent and write disjoint output spans — run them across
    // the pool (bd-av64.10 pass 2); per-head arithmetic is unchanged, so the
    // result is bit-identical to the sequential loop. The inner matmuls also
    // parallelize; rayon's work stealing shares the pool between the levels.
    let mut out = vec![0.0f32; nh * n * hd]; // [nh][n, hd]
    out.par_chunks_mut(n * hd)
        .enumerate()
        .try_for_each(|(head, out_chunk)| -> FocrResult<()> {
            let qh = &q[head * n * hd..(head + 1) * n * hd];
            let kh = &k[head * n * hd..(head + 1) * n * hd];
            let vh = &v[head * n * hd..(head + 1) * n * hd];
            let (rel_h_bias, rel_w_bias) = decomposed_rel_pos_bias(qh, rh, rw, gh, gw, hd);

            // logits = scale * (Q @ K^T) + decomposed rel-pos bias.
            let qh_mat = Mat::from_vec(n, hd, qh.to_vec());
            let kt_mat = Mat::from_vec(hd, n, transpose_contiguous_stores(kh, n, hd));
            let mut lm = nn::matmul(&qh_mat, &kt_mat)?;
            // Bias add, row-structured: j = ky*gw + kx, so walk (ky, kx) directly
            // instead of dividing per element — bit-identical arithmetic in the
            // same order, but branch/div-free and autovectorizable (bd-av64.10:
            // the naive j/gw + j%gw form burned ~200M integer divisions per
            // global block).
            for i in 0..n {
                let lrow = &mut lm.data[i * n..(i + 1) * n];
                let brow_w = &rel_w_bias[i * gw..(i + 1) * gw];
                for ky in 0..gh {
                    let bh = rel_h_bias[i * gh + ky];
                    let seg = &mut lrow[ky * gw..(ky + 1) * gw];
                    for (l, &bw) in seg.iter_mut().zip(brow_w) {
                        *l = scale * *l + bh + bw;
                    }
                }
            }
            // softmax rows then weighted sum of v.
            nn::softmax_rows(&mut lm)?;
            let vh_mat = Mat::from_vec(n, hd, vh.to_vec());
            let head_out = nn::matmul(&lm, &vh_mat)?;
            out_chunk.copy_from_slice(&head_out.data);
            Ok(())
        })?;

    // Reassemble [n, dim] from [nh][n, hd] (head-major -> token rows).
    let mut ctx = vec![0.0f32; n * dim];
    for head in 0..nh {
        for r in 0..n {
            let src = (head * n + r) * hd;
            let dst = r * dim + head * hd;
            ctx[dst..dst + hd].copy_from_slice(&out[src..src + hd]);
        }
    }
    let ctx_mat = Mat::from_vec(n, dim, ctx);
    let y = p.proj.apply(&ctx_mat)?;
    ensure_mat_shape(&y, n, dim, "vision_sam attention projection output")?;
    Ok(y)
}

/// Query rows per slab in [`attention_global_bounded`]: bounds the per-head
/// logits scratch to `slab × n × 4` bytes (2 MB at the 64×64 global grid)
/// instead of the full `n × n` (~64 MB × 12 concurrent heads ≈ 0.8 GB). The
/// heads run concurrently, so this slab is paid ONCE PER HEAD — 128 rows keeps
/// the whole global-attention logits footprint at ~24 MB while still handing
/// the GEMM/softmax kernels rows in the thousands of elements.
const GLOBAL_ATTN_QUERY_SLAB: usize = 128;

/// Bounded-scratch global attention (bd-4l71 wasm-lane residency): identical
/// math to [`attention`] with the per-head logits computed in CONTIGUOUS
/// query-row slabs. Each query row's logits, bias adds, softmax, and `·V`
/// reduction are computed with the SAME kernels over the SAME operands in the
/// SAME order — a query row never reduces across other query rows, so slabbing
/// only changes how rows are grouped into kernel calls. Bit-identity is gated
/// by `bounded_global_attention_is_bit_identical`.
fn attention_global_bounded(p: &AttnP, x: &Mat, gh: usize, gw: usize) -> FocrResult<Mat> {
    attention_global_bounded_with(p, x, gh, gw, GLOBAL_ATTN_QUERY_SLAB)
}

fn attention_global_bounded_with(
    p: &AttnP,
    x: &Mat,
    gh: usize,
    gw: usize,
    slab: usize,
) -> FocrResult<Mat> {
    let n = gh * gw;
    if x.rows != n {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam attention: input rows {} != gh*gw {}*{}",
            x.rows,
            gh,
            gw
        )));
    }
    let dim = x.cols;
    let nh = NUM_HEADS;
    if dim == 0 || !dim.is_multiple_of(nh) {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam attention: dim {} must be non-zero and divisible by heads {}",
            dim,
            nh
        )));
    }
    if p.qkv.in_ != dim || p.qkv.out != 3 * dim {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam attention: qkv shape out/in {}x{} incompatible with dim {}",
            p.qkv.out,
            p.qkv.in_,
            dim
        )));
    }
    if p.proj.in_ != dim || p.proj.out != dim {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam attention: proj shape out/in {}x{} incompatible with dim {}",
            p.proj.out,
            p.proj.in_,
            dim
        )));
    }
    if slab == 0 {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam attention: query slab must be non-zero"
        )));
    }
    let hd = dim / nh;
    ensure_rel_pos_len("rel_pos_h", p.size_h, hd, p.rel_pos_h.len())?;
    ensure_rel_pos_len("rel_pos_w", p.size_w, hd, p.rel_pos_w.len())?;
    let scale = (hd as f32).powf(-0.5);

    // qkv + per-head split — identical to `attention`.
    let qkv = p.qkv.apply(x)?;
    ensure_mat_shape(&qkv, n, 3 * dim, "vision_sam attention qkv output")?;
    let mut q = vec![0.0f32; nh * n * hd];
    let mut k = vec![0.0f32; nh * n * hd];
    let mut v = vec![0.0f32; nh * n * hd];
    for r in 0..n {
        let row = qkv.row(r);
        for head in 0..nh {
            let dst = (head * n + r) * hd;
            q[dst..dst + hd].copy_from_slice(&row[head * hd..(head + 1) * hd]);
            k[dst..dst + hd].copy_from_slice(&row[(nh + head) * hd..(nh + head + 1) * hd]);
            v[dst..dst + hd].copy_from_slice(&row[(2 * nh + head) * hd..(2 * nh + head + 1) * hd]);
        }
    }
    // The fused `[n, 3*dim]` projection is dead once it has been split into the
    // per-head q/k/v planes; releasing it HERE (bd-4l71) keeps its ~37 MB (SAM
    // at 4096 tokens) out of the peak that the per-head parallel section below
    // sets. Pure lifetime — no value changes.
    drop(qkv);
    let rh = get_rel_pos(gh, gh, &p.rel_pos_h, p.size_h, hd);
    let rw = get_rel_pos(gw, gw, &p.rel_pos_w, p.size_w, hd);

    let mut out = vec![0.0f32; nh * n * hd]; // [nh][n, hd]
    out.par_chunks_mut(n * hd)
        .enumerate()
        .try_for_each(|(head, out_chunk)| -> FocrResult<()> {
            let qh = &q[head * n * hd..(head + 1) * n * hd];
            let kh = &k[head * n * hd..(head + 1) * n * hd];
            let vh = &v[head * n * hd..(head + 1) * n * hd];
            let (rel_h_bias, rel_w_bias) = decomposed_rel_pos_bias(qh, &rh, &rw, gh, gw, hd);
            let kt_mat = Mat::from_vec(hd, n, transpose_contiguous_stores(kh, n, hd));
            let vh_mat = Mat::from_vec(n, hd, vh.to_vec());

            // Query slabs: rows [q0, q0+qc) of the logits, biased/softmaxed/
            // reduced exactly as in `attention` (global row index i = q0 + li).
            let mut q0 = 0usize;
            while q0 < n {
                let qc = slab.min(n - q0);
                let qb_mat = Mat::from_vec(qc, hd, qh[q0 * hd..(q0 + qc) * hd].to_vec());
                let mut lm = nn::matmul(&qb_mat, &kt_mat)?;
                for li in 0..qc {
                    let i = q0 + li;
                    let lrow = &mut lm.data[li * n..(li + 1) * n];
                    let brow_w = &rel_w_bias[i * gw..(i + 1) * gw];
                    for ky in 0..gh {
                        let bh = rel_h_bias[i * gh + ky];
                        let seg = &mut lrow[ky * gw..(ky + 1) * gw];
                        for (l, &bw) in seg.iter_mut().zip(brow_w) {
                            *l = scale * *l + bh + bw;
                        }
                    }
                }
                nn::softmax_rows(&mut lm)?;
                let slab_out = nn::matmul(&lm, &vh_mat)?;
                out_chunk[q0 * hd..(q0 + qc) * hd].copy_from_slice(&slab_out.data);
                q0 += qc;
            }
            Ok(())
        })?;

    // Reassemble + project — identical to `attention`.
    let mut ctx = vec![0.0f32; n * dim];
    for head in 0..nh {
        for r in 0..n {
            let src = (head * n + r) * hd;
            let dst = r * dim + head * hd;
            ctx[dst..dst + hd].copy_from_slice(&out[src..src + hd]);
        }
    }
    let ctx_mat = Mat::from_vec(n, dim, ctx);
    let y = p.proj.apply(&ctx_mat)?;
    ensure_mat_shape(&y, n, dim, "vision_sam attention projection output")?;
    Ok(y)
}

fn ensure_mat_shape(mat: &Mat, rows: usize, cols: usize, context: &str) -> FocrResult<()> {
    if mat.rows == rows && mat.cols == cols {
        return Ok(());
    }
    Err(FocrError::Other(anyhow::anyhow!(
        "{context}: shape {:?} != expected ({rows}, {cols})",
        mat.shape()
    )))
}

fn ensure_flat_len(context: &str, data: &[f32], ch: usize, h: usize, w: usize) -> FocrResult<()> {
    let expected = checked_nchw_len(context, ch, h, w, "ch*h*w")?;
    if data.len() == expected {
        return Ok(());
    }
    Err(FocrError::Other(anyhow::anyhow!(
        "{context}: len {} != ch*h*w {}*{}*{} ({expected})",
        data.len(),
        ch,
        h,
        w
    )))
}

fn ensure_same_shape(context: &str, actual: &Mat, expected: &Mat) -> FocrResult<()> {
    if actual.shape() == expected.shape() {
        return Ok(());
    }
    Err(FocrError::Other(anyhow::anyhow!(
        "{context}: shape {:?} != expected {:?}",
        actual.shape(),
        expected.shape()
    )))
}

fn ensure_rel_pos_len(name: &str, size: usize, hd: usize, actual_len: usize) -> FocrResult<()> {
    let rows = size
        .checked_mul(2)
        .and_then(|n| n.checked_sub(1))
        .ok_or_else(|| {
            FocrError::Other(anyhow::anyhow!(
                "vision_sam attention: {name} size {size} is invalid"
            ))
        })?;
    let expected_len = rows.checked_mul(hd).ok_or_else(|| {
        FocrError::Other(anyhow::anyhow!(
            "vision_sam attention: {name} size {size} overflows for head_dim {hd}"
        ))
    })?;
    if actual_len == expected_len {
        return Ok(());
    }
    Err(FocrError::Other(anyhow::anyhow!(
        "vision_sam attention: {name} len {actual_len} != expected {expected_len} \
         for size {size} and head_dim {hd}"
    )))
}

fn decomposed_rel_pos_bias(
    qh: &[f32],
    rh: &[f32],
    rw: &[f32],
    gh: usize,
    gw: usize,
    hd: usize,
) -> (Vec<f32>, Vec<f32>) {
    let n = gh * gw;
    debug_assert_eq!(qh.len(), n * hd);
    debug_assert_eq!(rh.len(), gh * gh * hd);
    debug_assert_eq!(rw.len(), gw * gw * hd);

    let mut rel_h_bias = vec![0.0f32; n * gh];
    let mut rel_w_bias = vec![0.0f32; n * gw];
    for i in 0..n {
        let qy = i / gw;
        let qx = i % gw;
        let qi = &qh[i * hd..(i + 1) * hd];
        for ky in 0..gh {
            let rh_base = (qy * gh + ky) * hd;
            let mut bh = 0.0f32;
            for c in 0..hd {
                bh += qi[c] * rh[rh_base + c];
            }
            rel_h_bias[i * gh + ky] = bh;
        }
        for kx in 0..gw {
            let rw_base = (qx * gw + kx) * hd;
            let mut bw = 0.0f32;
            for c in 0..hd {
                bw += qi[c] * rw[rw_base + c];
            }
            rel_w_bias[i * gw + kx] = bw;
        }
    }
    (rel_h_bias, rel_w_bias)
}

/// `get_rel_pos(q_size, k_size, rel_pos)` -> `[q_size, k_size, head_dim]`.
///
/// The table `rel_pos` is `[2*size - 1, hd]`. When `size == q_size == k_size`
/// (our case — windows / global grid match the table), no interpolation is
/// needed and we index `rel_pos[(q - k) + (k_size - 1)]` directly (the
/// `q_coords - k_coords + (k_size-1)` formula with `q_size == k_size`).
fn get_rel_pos(q_size: usize, k_size: usize, rel_pos: &[f32], size: usize, hd: usize) -> Vec<f32> {
    let max_rel = 2 * q_size.max(k_size) - 1;
    // Resize the table to max_rel rows via linear interpolation if its row
    // count differs (matches F.interpolate(mode="linear") in get_rel_pos).
    let table_rows = rel_pos.len() / hd;
    debug_assert_eq!(table_rows, 2 * size - 1);
    let resized: Vec<f32> = if table_rows != max_rel {
        interp_linear_rows(rel_pos, table_rows, hd, max_rel)
    } else {
        rel_pos.to_vec()
    };

    let qf = q_size as f32;
    let kf = k_size as f32;
    let ratio_qk = (kf / qf).max(1.0);
    let ratio_kq = (qf / kf).max(1.0);
    let mut out = vec![0.0f32; q_size * k_size * hd];
    for qi in 0..q_size {
        for ki in 0..k_size {
            let qc = qi as f32 * ratio_qk;
            let kc = ki as f32 * ratio_kq;
            // PyTorch SAM uses max(q_size/k_size, 1.0) = ratio_kq for this offset
            // term (audit rank 8). Identical to ratio_qk only when q_size == k_size
            // (every current call site), so this hardens the helper for q != k.
            let rc = (qc - kc) + (k_size as f32 - 1.0) * ratio_kq;
            let idx = rc as usize; // .long() truncation
            let src = idx * hd;
            let dst = (qi * k_size + ki) * hd;
            out[dst..dst + hd].copy_from_slice(&resized[src..src + hd]);
        }
    }
    out
}

/// Linear (1-D) interpolation of a `[rows, hd]` table to `[new_rows, hd]`,
/// matching `F.interpolate(mode="linear", align_corners=False)` over the row
/// axis (per-feature). Only hit when a rel-pos table size mismatches the grid.
fn interp_linear_rows(src: &[f32], rows: usize, hd: usize, new_rows: usize) -> Vec<f32> {
    if new_rows == rows {
        return src.to_vec();
    }
    let mut out = vec![0.0f32; new_rows * hd];
    let scale = rows as f32 / new_rows as f32;
    for i in 0..new_rows {
        // align_corners=False source coordinate.
        let s = (i as f32 + 0.5) * scale - 0.5;
        let s_clamped = s.clamp(0.0, (rows - 1) as f32);
        let lo = s_clamped.floor() as usize;
        let hi = (lo + 1).min(rows - 1);
        let frac = s_clamped - lo as f32;
        for c in 0..hd {
            let a = src[lo * hd + c];
            let b = src[hi * hd + c];
            out[i * hd + c] = a + (b - a) * frac;
        }
    }
    out
}

// ── normalization helpers ──────────────────────────────────────────────────

/// LayerNorm over the last dim of token rows `[n, cols]` with affine params.
fn layer_norm_rows(x: &Mat, ln: &LayerNormP) -> FocrResult<Mat> {
    nn::layer_norm(x, Some(&ln.w), Some(&ln.b), LN_EPS)
}

/// `LayerNorm2d` over an NCHW-flat `[C, H*W]` buffer: normalize across the
/// CHANNEL axis at each spatial location, then per-channel affine
/// (`deepencoder.py:590-602`: `mean(1)`/`var(1)` over channels).
fn layer_norm_2d(
    x: &[f32],
    ln: &LayerNormP,
    ch: usize,
    gh: usize,
    gw: usize,
) -> FocrResult<Vec<f32>> {
    if ch == 0 || gh == 0 || gw == 0 {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam layer_norm_2d: channels and grid dims must be non-zero (ch={ch}, gh={gh}, gw={gw})"
        )));
    }
    ensure_flat_len("vision_sam layer_norm_2d input", x, ch, gh, gw)?;
    if ln.w.len() != ch {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam layer_norm_2d: weight len {} != channels {}",
            ln.w.len(),
            ch
        )));
    }
    if ln.b.len() != ch {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam layer_norm_2d: bias len {} != channels {}",
            ln.b.len(),
            ch
        )));
    }
    let hw = checked_shape_mul("vision_sam layer_norm_2d", gh, gw, "gh*gw")?;
    let out_len = checked_shape_mul("vision_sam layer_norm_2d", ch, hw, "ch*gh*gw")?;
    let mut out = vec![0.0f32; out_len];
    for s in 0..hw {
        // mean over channels at spatial location s.
        let mut mean = 0.0f32;
        for c in 0..ch {
            mean += x[c * hw + s];
        }
        mean /= ch as f32;
        let mut var = 0.0f32;
        for c in 0..ch {
            let d = x[c * hw + s] - mean;
            var += d * d;
        }
        var /= ch as f32;
        let inv = 1.0 / (var + LN_EPS).sqrt();
        for c in 0..ch {
            let norm = (x[c * hw + s] - mean) * inv;
            out[c * hw + s] = ln.w[c] * norm + ln.b[c];
        }
    }
    Ok(out)
}

// ── conv + layout helpers ──────────────────────────────────────────────────
// `pub(crate)` where noted: these are the A8 shared vision conv leaves
// (bd-3jo6.1.8) — the SigLIP patch-embed (vision_siglip.rs) drives the SAME
// im2col+GEMM conv path SAM/GOT certify, exactly like GOT reuses
// `forward_prefix`/`Linear` (B3's precedent: share by import, never relocate
// certified code).

/// Apply a [`Conv`] over an NCHW-flat `[in_ch, gh*gw]` buffer with symmetric
/// zero padding `pad` and stride `stride`, returning the NCHW-flat output.
pub(crate) fn conv_apply(
    conv: &Conv,
    input: &[f32],
    gh: usize,
    gw: usize,
    pad: usize,
    stride: usize,
) -> FocrResult<Vec<f32>> {
    if gh == 0 || gw == 0 {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam conv: input grid ({gh},{gw}) must be non-zero"
        )));
    }
    if stride == 0 {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam conv: stride must be non-zero"
        )));
    }
    if conv.in_ch == 0 || conv.out_ch == 0 || conv.kh == 0 || conv.kw == 0 {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam conv: channels and kernel dims must be non-zero (out={}, in={}, kh={}, kw={})",
            conv.out_ch,
            conv.in_ch,
            conv.kh,
            conv.kw
        )));
    }
    let expected_input =
        checked_nchw_len("vision_sam conv input", conv.in_ch, gh, gw, "in_ch*gh*gw")?;
    if input.len() != expected_input {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam conv: input len {} != in_ch*gh*gw {}",
            input.len(),
            expected_input
        )));
    }
    let expected_weight = checked_conv_weight_len(
        "vision_sam conv weight",
        conv.out_ch,
        conv.in_ch,
        conv.kh,
        conv.kw,
    )?;
    if conv.w.len() != expected_weight {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam conv: weight len {} != out_ch*in_ch*kh*kw {}",
            conv.w.len(),
            expected_weight
        )));
    }
    if let Some(bias) = &conv.b
        && bias.len() != conv.out_ch
    {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam conv: bias len {} != out_ch {}",
            bias.len(),
            conv.out_ch
        )));
    }

    let two_pad = checked_shape_mul("vision_sam conv", 2, pad, "2*pad")?;
    let ph = checked_shape_add("vision_sam conv", gh, two_pad, "gh+2*pad")?;
    let pw = checked_shape_add("vision_sam conv", gw, two_pad, "gw+2*pad")?;
    let oh_base = checked_shape_sub("vision_sam conv", ph, conv.kh, "padded_h-kh")?;
    let ow_base = checked_shape_sub("vision_sam conv", pw, conv.kw, "padded_w-kw")?;
    let oh = checked_shape_add("vision_sam conv", oh_base / stride, 1, "output_h+1")?;
    let ow = checked_shape_add("vision_sam conv", ow_base / stride, 1, "output_w+1")?;
    let expected_out = checked_nchw_len(
        "vision_sam conv output",
        conv.out_ch,
        oh,
        ow,
        "out_ch*oh*ow",
    )?;
    let padded = pad_nchw(input, conv.in_ch, gh, gw, pad)?;
    let out = nn::conv2d(
        &padded,
        &conv.w,
        conv.b.as_deref(),
        1,
        conv.in_ch,
        ph,
        pw,
        conv.kh,
        conv.kw,
        oh,
        ow,
        stride,
        stride,
        conv.out_ch,
    );
    if out.len() != expected_out {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam conv: kernel output len {} != out_ch*oh*ow {}",
            out.len(),
            expected_out
        )));
    }
    Ok(out)
}

/// Zero-pad an NCHW-flat `[ch, gh*gw]` buffer by `pad` on every spatial side ->
/// `[ch, (gh+2p)*(gw+2p)]`.
fn pad_nchw(input: &[f32], ch: usize, gh: usize, gw: usize, pad: usize) -> FocrResult<Vec<f32>> {
    let expected_input = checked_nchw_len("vision_sam pad_nchw input", ch, gh, gw, "ch*gh*gw")?;
    if input.len() != expected_input {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam pad_nchw: input len {} != ch*gh*gw {}",
            input.len(),
            expected_input
        )));
    }
    if pad == 0 {
        return Ok(input.to_vec());
    }
    let two_pad = checked_shape_mul("vision_sam pad_nchw", 2, pad, "2*pad")?;
    let ph = checked_shape_add("vision_sam pad_nchw", gh, two_pad, "gh+2*pad")?;
    let pw = checked_shape_add("vision_sam pad_nchw", gw, two_pad, "gw+2*pad")?;
    let out_len = checked_nchw_len("vision_sam pad_nchw output", ch, ph, pw, "ch*ph*pw")?;
    let mut out = vec![0.0f32; out_len];
    for c in 0..ch {
        for y in 0..gh {
            for x in 0..gw {
                let src = c * gh * gw + y * gw + x;
                let dst = c * ph * pw + (y + pad) * pw + (x + pad);
                out[dst] = input[src];
            }
        }
    }
    Ok(out)
}

/// `[1, ch, gh, gw]` channel-major conv output -> NHWC token rows
/// `[gh*gw, ch]`.
pub(crate) fn nchw_to_nhwc_rows(nchw: &[f32], ch: usize, gh: usize, gw: usize) -> Mat {
    let n = gh * gw;
    let mut data = vec![0.0f32; n * ch];
    for s in 0..n {
        let dst = &mut data[s * ch..(s + 1) * ch];
        for c in 0..ch {
            dst[c] = nchw[c * n + s];
        }
    }
    Mat::from_vec(n, ch, data)
}

/// NHWC token rows `[gh*gw, ch]` -> NCHW-flat `[ch, gh*gw]`
/// (`permute(0,3,1,2)`).
fn nhwc_rows_to_nchw(x: &Mat, ch: usize, gh: usize, gw: usize) -> Vec<f32> {
    let n = gh * gw;
    debug_assert_eq!(x.rows, n);
    debug_assert_eq!(x.cols, ch);
    let mut out = vec![0.0f32; ch * n];
    for c in 0..ch {
        let dst = &mut out[c * n..(c + 1) * n];
        for (s, slot) in dst.iter_mut().enumerate() {
            *slot = x.data[s * ch + c];
        }
    }
    out
}

/// Transpose a `[rows, cols]` row-major matrix to `[cols, rows]`.
fn transpose(m: &[f32], rows: usize, cols: usize) -> Vec<f32> {
    let mut out = vec![0.0f32; rows * cols];
    for c in 0..cols {
        let dst = &mut out[c * rows..(c + 1) * rows];
        for (r, slot) in dst.iter_mut().enumerate() {
            *slot = m[r * cols + c];
        }
    }
    out
}

fn transpose_contiguous_stores(m: &[f32], rows: usize, cols: usize) -> Vec<f32> {
    let mut out = vec![0.0f32; rows * cols];
    for c in 0..cols {
        let dst = &mut out[c * rows..(c + 1) * rows];
        for r in 0..rows {
            dst[r] = m[r * cols + c];
        }
    }
    out
}

// ── absolute position embedding (bicubic interp, [SPEC-042]) ───────────────

/// Build the abs pos-embed contribution for a `[gh, gw, dim]` grid from the
/// learned `[src_h, src_w, dim]` table, bicubic-interpolating if the runtime
/// grid differs (matches `get_abs_pos_sam`). Returns NHWC token-row order
/// `[gh*gw, dim]` flattened.
fn abs_pos(
    pos: &[f32],
    src_h: usize,
    src_w: usize,
    dim: usize,
    gh: usize,
    gw: usize,
) -> FocrResult<Vec<f32>> {
    if src_h == 0 || src_w == 0 || dim == 0 || gh == 0 || gw == 0 {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam abs_pos: source ({src_h},{src_w}), target ({gh},{gw}), and dim {dim} must be non-zero"
        )));
    }
    let src_hw = checked_shape_mul("vision_sam abs_pos", src_h, src_w, "src_h*src_w")?;
    let expected_pos_len = checked_shape_mul("vision_sam abs_pos", src_hw, dim, "src_h*src_w*dim")?;
    if pos.len() != expected_pos_len {
        return Err(FocrError::Other(anyhow::anyhow!(
            "vision_sam abs_pos: pos_embed len {} != src_h*src_w*dim {}*{}*{} ({expected_pos_len})",
            pos.len(),
            src_h,
            src_w,
            dim
        )));
    }
    let tgt_hw = checked_shape_mul("vision_sam abs_pos", gh, gw, "gh*gw")?;
    let out_len = checked_shape_mul("vision_sam abs_pos", tgt_hw, dim, "gh*gw*dim")?;
    if src_h == gh && src_w == gw {
        return Ok(pos.to_vec()); // already [gh*gw, dim] NHWC rows
    }
    // Interpolate per-channel over the spatial grid (bicubic, align_corners=
    // False). pos is [src_h, src_w, dim] (channel-last); we resample each
    // channel independently into [gh, gw].
    let mut out = vec![0.0f32; out_len];
    let scale_y = src_h as f32 / gh as f32;
    let scale_x = src_w as f32 / gw as f32;
    for oy in 0..gh {
        let sy = (oy as f32 + 0.5) * scale_y - 0.5;
        for ox in 0..gw {
            let sx = (ox as f32 + 0.5) * scale_x - 0.5;
            let dst = (oy * gw + ox) * dim;
            for c in 0..dim {
                out[dst + c] = bicubic_sample(pos, src_h, src_w, dim, c, sy, sx);
            }
        }
    }
    Ok(out)
}

/// Bicubic sample of channel `c` from a `[src_h, src_w, dim]` channel-last
/// table at fractional `(sy, sx)` using the Catmull-Rom-ish cubic convolution
/// kernel (`a = -0.75`, PyTorch default), edge-clamped.
fn bicubic_sample(
    pos: &[f32],
    src_h: usize,
    src_w: usize,
    dim: usize,
    c: usize,
    sy: f32,
    sx: f32,
) -> f32 {
    let iy = sy.floor();
    let ix = sx.floor();
    let fy = sy - iy;
    let fx = sx - ix;
    let wy = cubic_weights(fy);
    let wx = cubic_weights(fx);
    let mut acc = 0.0f32;
    // indexed loop: spatial kernel offset
    #[allow(clippy::needless_range_loop)]
    for m in 0..4 {
        let yy = clamp_idx(iy as isize - 1 + m as isize, src_h);
        // indexed loop: spatial kernel offset
        #[allow(clippy::needless_range_loop)]
        for n in 0..4 {
            let xx = clamp_idx(ix as isize - 1 + n as isize, src_w);
            let val = pos[(yy * src_w + xx) * dim + c];
            acc += val * wy[m] * wx[n];
        }
    }
    acc
}

/// Cubic-convolution weights for the 4-tap stencil around fractional `t` in
/// `[0,1)` with `a = -0.75` (PyTorch `mode="bicubic"`).
fn cubic_weights(t: f32) -> [f32; 4] {
    let a = -0.75f32;
    // distances of the four samples (indices -1,0,1,2) from t.
    let d0 = 1.0 + t;
    let d1 = t;
    let d2 = 1.0 - t;
    let d3 = 2.0 - t;
    [
        cubic_k(d0, a),
        cubic_k(d1, a),
        cubic_k(d2, a),
        cubic_k(d3, a),
    ]
}

/// Keys cubic kernel `W(x)` with parameter `a`.
fn cubic_k(x: f32, a: f32) -> f32 {
    let x = x.abs();
    if x <= 1.0 {
        (a + 2.0) * x * x * x - (a + 3.0) * x * x + 1.0
    } else if x < 2.0 {
        a * x * x * x - 5.0 * a * x * x + 8.0 * a * x - 4.0 * a
    } else {
        0.0
    }
}

/// Clamp an index to `[0, n-1]`.
fn clamp_idx(i: isize, n: usize) -> usize {
    if i < 0 {
        0
    } else if i as usize >= n {
        n - 1
    } else {
        i as usize
    }
}

/// Synthetic-tower construction shared by this module's tests and by the
/// model-level tests in `got`/`onechart`, which need a SAM tower to prove their
/// streamed and cached vision arms agree. Extracted rather than copied so the
/// three callers cannot drift into testing three different towers.
#[cfg(test)]
pub(crate) mod test_support {
    use super::{DEPTH, GLOBAL_BLOCKS, NUM_HEADS, PATCH, WINDOW};
    use crate::quant::focrq::{FocrqBuilder, WriteDType};

    /// Deterministic pseudo-random values in a small symmetric range. The salt
    /// is folded in AFTER the multiply and the low bits are shifted off, so
    /// nearby salts must be kept far apart or they collide into the same tensor.
    pub(crate) fn synth_values(len: usize, salt: u64) -> Vec<f32> {
        (0..len)
            .map(|i| {
                let raw = ((i as u64)
                    .wrapping_mul(6_364_136_223_846_793_005)
                    .wrapping_add(salt)
                    >> 33) as u32;
                (raw as f32 / u32::MAX as f32 - 0.5) * 0.3
            })
            .collect()
    }

    /// Write every tensor a SAM-ViT-B tower needs under `prefix`, at a reduced
    /// `dim` (must divide `NUM_HEADS`) so the test stays fast. Geometry matches
    /// the real tower's leaf names exactly, which is what lets GOT/OneChart
    /// (`model.vision_tower_high` / `model.vision_tower`) reuse it.
    pub(crate) fn add_synth_sam_tower(b: &mut FocrqBuilder, prefix: &str, dim: usize) {
        let f32_bytes =
            |values: &[f32]| -> Vec<u8> { values.iter().flat_map(|v| v.to_le_bytes()).collect() };
        let mut add = |name: String, shape: Vec<usize>, salt: u64| {
            let len: usize = shape.iter().product();
            b.add_tensor(
                name,
                WriteDType::F32,
                shape,
                f32_bytes(&synth_values(len, salt)),
            )
            .expect("valid synthetic f32 tensor");
        };

        add(
            format!("{prefix}.patch_embed.proj.weight"),
            vec![dim, 3, PATCH, PATCH],
            1,
        );
        add(format!("{prefix}.patch_embed.proj.bias"), vec![dim], 2);
        add(format!("{prefix}.pos_embed"), vec![1, 4, 4, dim], 3);
        for (idx, name) in ["neck.0", "neck.2"].iter().enumerate() {
            let ch = 8usize;
            let in_ch = if idx == 0 { dim } else { ch };
            add(
                format!("{prefix}.{name}.weight"),
                vec![ch, in_ch, 1, 1],
                10 + idx as u64,
            );
        }
        for (idx, name) in ["neck.1", "neck.3"].iter().enumerate() {
            add(format!("{prefix}.{name}.weight"), vec![8], 20 + idx as u64);
            add(format!("{prefix}.{name}.bias"), vec![8], 30 + idx as u64);
        }
        add(format!("{prefix}.net_2.weight"), vec![8, 8, 3, 3], 40);
        add(format!("{prefix}.net_3.weight"), vec![8, 8, 3, 3], 41);

        let hd = dim / NUM_HEADS;
        for i in 0..DEPTH {
            let bb = format!("{prefix}.blocks.{i}");
            let salt = 1000 * (i as u64 + 1);
            let rel_rows = if GLOBAL_BLOCKS.contains(&i) {
                2 * 4 - 1 // global grid 4
            } else {
                2 * WINDOW - 1
            };
            add(format!("{bb}.norm1.weight"), vec![dim], salt + 1);
            add(format!("{bb}.norm1.bias"), vec![dim], salt + 2);
            add(
                format!("{bb}.attn.qkv.weight"),
                vec![3 * dim, dim],
                salt + 3,
            );
            add(format!("{bb}.attn.qkv.bias"), vec![3 * dim], salt + 4);
            add(format!("{bb}.attn.proj.weight"), vec![dim, dim], salt + 5);
            add(format!("{bb}.attn.proj.bias"), vec![dim], salt + 6);
            add(format!("{bb}.attn.rel_pos_h"), vec![rel_rows, hd], salt + 7);
            add(format!("{bb}.attn.rel_pos_w"), vec![rel_rows, hd], salt + 8);
            add(format!("{bb}.norm2.weight"), vec![dim], salt + 9);
            add(format!("{bb}.norm2.bias"), vec![dim], salt + 10);
            add(
                format!("{bb}.mlp.lin1.weight"),
                vec![4 * dim, dim],
                salt + 11,
            );
            add(format!("{bb}.mlp.lin1.bias"), vec![4 * dim], salt + 12);
            add(
                format!("{bb}.mlp.lin2.weight"),
                vec![dim, 4 * dim],
                salt + 13,
            );
            add(format!("{bb}.mlp.lin2.bias"), vec![dim], salt + 14);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::quant::focrq::{FocrqBuilder, WriteDType};
    use half::bf16;
    use serde_json::json;
    use std::time::Instant;

    /// Identity 1x1 conv (out_ch==in_ch, k=1) with an identity-ish weight is a
    /// channel mixing; here we use a diagonal weight so output == input.
    fn identity_conv1(ch: usize) -> Conv {
        let mut w = vec![0.0f32; ch * ch];
        for c in 0..ch {
            w[c * ch + c] = 1.0;
        }
        Conv {
            w,
            b: None,
            out_ch: ch,
            in_ch: ch,
            kh: 1,
            kw: 1,
        }
    }

    fn assert_err_contains<T>(res: FocrResult<T>, needle: &str) {
        let message = match res {
            Ok(_) => String::from("<ok>"),
            Err(err) => err.to_string(),
        };
        assert!(
            message.contains(needle),
            "error {message:?} did not contain {needle:?}"
        );
    }

    fn bf16_zeros(n: usize) -> Vec<u8> {
        (0..n)
            .flat_map(|_| bf16::from_f32(0.0).to_le_bytes())
            .collect()
    }

    fn add_minimal_patch_embed(b: &mut FocrqBuilder) {
        let p = "model.sam_model";
        b.add_tensor(
            format!("{p}.patch_embed.proj.weight"),
            WriteDType::Bf16,
            vec![1, 1, 1, 1],
            bf16_zeros(1),
        )
        .unwrap();
        b.add_tensor(
            format!("{p}.patch_embed.proj.bias"),
            WriteDType::Bf16,
            vec![1],
            bf16_zeros(1),
        )
        .unwrap();
    }

    // ── weight hydration error paths ───────────────────────────────────────

    #[test]
    fn sam_weights_from_rejects_rank1_patch_embed_without_panic() {
        let p = "model.sam_model";
        let mut b = FocrqBuilder::new();
        b.add_tensor(
            format!("{p}.patch_embed.proj.weight"),
            WriteDType::Bf16,
            vec![4],
            bf16_zeros(4),
        )
        .unwrap();
        let weights = Weights::from_bytes(b.build()).unwrap();
        assert_err_contains(sam_weights_from(&weights, "model.sam_model"), "rank 1");
    }

    #[test]
    fn sam_weights_from_rejects_rank1_pos_embed_without_panic() {
        let p = "model.sam_model";
        let mut b = FocrqBuilder::new();
        add_minimal_patch_embed(&mut b);
        b.add_tensor(
            format!("{p}.pos_embed"),
            WriteDType::Bf16,
            vec![4],
            bf16_zeros(4),
        )
        .unwrap();
        let weights = Weights::from_bytes(b.build()).unwrap();
        assert_err_contains(sam_weights_from(&weights, "model.sam_model"), "rank 1");
    }

    #[test]
    fn sam_weights_from_rejects_rank1_block_qkv_without_panic() {
        let p = "model.sam_model";
        let mut b = FocrqBuilder::new();
        add_minimal_patch_embed(&mut b);
        b.add_tensor(
            format!("{p}.pos_embed"),
            WriteDType::Bf16,
            vec![1, 1, 1, 1],
            bf16_zeros(1),
        )
        .unwrap();
        b.add_tensor(
            format!("{p}.blocks.0.attn.qkv.weight"),
            WriteDType::Bf16,
            vec![4],
            bf16_zeros(4),
        )
        .unwrap();
        let weights = Weights::from_bytes(b.build()).unwrap();
        assert_err_contains(sam_weights_from(&weights, "model.sam_model"), "rank 1");
    }

    #[test]
    fn transpose_roundtrips() {
        let m = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]; // [2,3]
        let t = transpose(&m, 2, 3); // [3,2]
        assert_eq!(t, vec![1.0, 4.0, 2.0, 5.0, 3.0, 6.0]);
        assert_eq!(transpose(&t, 3, 2), m);
    }

    fn test_linear(w: Vec<f32>, b: Vec<f32>, out: usize, in_: usize) -> Linear {
        Linear::from_row_major(&w, b, out, in_).expect("test linear shape is valid")
    }

    #[test]
    fn linear_applies_weight_and_bias() -> FocrResult<()> {
        // w = [[1,2,3],[4,5,6]] (out=2,in=3), b=[10,20]
        let lin = test_linear(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], vec![10.0, 20.0], 2, 3);
        // x = [[1,1,1]] -> y = [1+2+3+10, 4+5+6+20] = [16, 35]
        let x = Mat::from_vec(1, 3, vec![1.0, 1.0, 1.0]);
        let y = lin.apply(&x)?;
        assert_eq!(y.shape(), (1, 2));
        assert!((y.data[0] - 16.0).abs() < 1e-5);
        assert!((y.data[1] - 35.0).abs() < 1e-5);
        Ok(())
    }

    #[test]
    fn linear_rejects_malformed_shapes_without_panic() {
        assert_err_contains(
            Linear::from_row_major(&[1.0; 5], vec![], 2, 3),
            "weight len",
        );
        assert_err_contains(
            Linear::from_row_major(&[1.0; 6], vec![0.0], 2, 3),
            "bias len",
        );
        let bad_x = Mat::from_vec(1, 2, vec![1.0, 2.0]);
        let lin = test_linear(vec![1.0; 6], vec![], 2, 3);
        assert_err_contains(lin.apply(&bad_x), "input cols");
    }

    #[test]
    fn pad_nchw_zeros_border() -> FocrResult<()> {
        // single channel 2x2 = [[1,2],[3,4]], pad=1 -> 4x4 with zero border.
        let input = vec![1.0, 2.0, 3.0, 4.0];
        let out = pad_nchw(&input, 1, 2, 2, 1)?;
        assert_eq!(out.len(), 16);
        // center 2x2 holds the original
        assert_eq!(out[4 + 1], 1.0);
        assert_eq!(out[4 + 2], 2.0);
        assert_eq!(out[8 + 1], 3.0);
        assert_eq!(out[8 + 2], 4.0);
        // corners are zero
        assert_eq!(out[0], 0.0);
        assert_eq!(out[15], 0.0);
        Ok(())
    }

    #[test]
    fn nchw_nhwc_roundtrip() {
        // ch=2, grid 2x2 (n=4). NCHW: c0=[1,2,3,4], c1=[5,6,7,8].
        let nchw = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let rows = nchw_to_nhwc_rows(&nchw, 2, 2, 2);
        // row 0 (spatial 0) = [c0=1, c1=5]
        assert_eq!(rows.row(0), &[1.0, 5.0]);
        assert_eq!(rows.row(3), &[4.0, 8.0]);
        let back = nhwc_rows_to_nchw(&rows, 2, 2, 2);
        assert_eq!(back, nchw);
    }

    #[test]
    fn nchw_nhwc_roundtrip_nonsquare_grid() {
        // ch=3, grid 2x3 (n=6). Each output row gathers the same spatial
        // position from all channel planes.
        let nchw = vec![
            1.0, 2.0, 3.0, 4.0, 5.0, 6.0, // c0
            10.0, 11.0, 12.0, 13.0, 14.0, 15.0, // c1
            20.0, 21.0, 22.0, 23.0, 24.0, 25.0, // c2
        ];
        let rows = nchw_to_nhwc_rows(&nchw, 3, 2, 3);
        assert_eq!(rows.row(0), &[1.0, 10.0, 20.0]);
        assert_eq!(rows.row(5), &[6.0, 15.0, 25.0]);

        let back = nhwc_rows_to_nchw(&rows, 3, 2, 3);
        assert_eq!(back, nchw);
    }

    #[test]
    fn layer_norm_2d_normalizes_channels() -> FocrResult<()> {
        // ch=2, grid 1x2 (hw=2). At spatial 0 channels=[1,3] -> mean 2, var 1.
        // normalized = [-1, 1]; affine w=[1,1], b=[0,0].
        let x = vec![1.0, 9.0, 3.0, 11.0]; // c0=[1,9], c1=[3,11] over hw=2
        let ln = LayerNormP {
            w: vec![1.0, 1.0],
            b: vec![0.0, 0.0],
        };
        let out = layer_norm_2d(&x, &ln, 2, 1, 2)?;
        // spatial 0: channels [1,3], mean 2, var 1 -> [-1, 1]
        assert!((out[0] - (-1.0)).abs() < 1e-3); // c0,s0
        assert!((out[2] - 1.0).abs() < 1e-3); // c1,s0
        // spatial 1: channels [9,11], mean 10, var 1 -> [-1, 1]
        assert!((out[1] - (-1.0)).abs() < 1e-3); // c0,s1
        assert!((out[3] - 1.0).abs() < 1e-3); // c1,s1
        Ok(())
    }

    #[test]
    fn layer_norm_2d_rejects_malformed_affine_without_panic() {
        let x = vec![1.0, 2.0, 3.0, 4.0];
        assert_err_contains(
            layer_norm_2d(
                &x,
                &LayerNormP {
                    w: vec![1.0],
                    b: vec![0.0, 0.0],
                },
                2,
                1,
                2,
            ),
            "weight len",
        );
        assert_err_contains(
            layer_norm_2d(
                &x,
                &LayerNormP {
                    w: vec![1.0, 1.0],
                    b: vec![0.0],
                },
                2,
                1,
                2,
            ),
            "bias len",
        );
    }

    #[test]
    fn layer_norm_2d_rejects_malformed_input_without_panic() {
        let ln = LayerNormP {
            w: vec![1.0, 1.0],
            b: vec![0.0, 0.0],
        };
        assert_err_contains(layer_norm_2d(&[1.0, 2.0, 3.0], &ln, 2, 1, 2), "input");
        assert_err_contains(layer_norm_2d(&[], &ln, 0, 1, 2), "non-zero");
    }

    #[test]
    fn conv_apply_identity_1x1_preserves() -> FocrResult<()> {
        // 2 channels, 2x2 grid, identity 1x1 conv -> unchanged.
        let input = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let conv = identity_conv1(2);
        let out = conv_apply(&conv, &input, 2, 2, 0, 1)?;
        assert_eq!(out, input);
        Ok(())
    }

    #[test]
    fn conv_apply_rejects_malformed_geometry_without_panic() {
        let input = vec![1.0, 2.0, 3.0, 4.0];
        let conv = identity_conv1(1);
        assert_err_contains(conv_apply(&conv, &input, 2, 2, 0, 0), "stride");

        let oversized_kernel = Conv {
            w: vec![1.0; 25],
            b: None,
            out_ch: 1,
            in_ch: 1,
            kh: 5,
            kw: 5,
        };
        assert_err_contains(
            conv_apply(&oversized_kernel, &input, 2, 2, 0, 1),
            "padded_h-kh",
        );
    }

    #[test]
    fn conv_apply_rejects_buffer_mismatches_without_panic() {
        let input = vec![1.0, 2.0, 3.0, 4.0];

        let short_weight = Conv {
            w: vec![1.0],
            b: None,
            out_ch: 2,
            in_ch: 1,
            kh: 1,
            kw: 1,
        };
        assert_err_contains(conv_apply(&short_weight, &input, 2, 2, 0, 1), "weight len");

        let bad_bias = Conv {
            w: vec![1.0],
            b: Some(vec![0.0, 1.0]),
            out_ch: 1,
            in_ch: 1,
            kh: 1,
            kw: 1,
        };
        assert_err_contains(conv_apply(&bad_bias, &input, 2, 2, 0, 1), "bias len");

        assert_err_contains(
            conv_apply(&identity_conv1(1), &input[..3], 2, 2, 0, 1),
            "input len",
        );
    }

    #[test]
    fn conv_apply_rejects_padding_overflow_before_allocating() {
        let input = vec![1.0];
        assert_err_contains(
            conv_apply(&identity_conv1(1), &input, 1, 1, usize::MAX / 2 + 1, 1),
            "2*pad",
        );
    }

    #[test]
    fn cubic_weights_sum_to_one() {
        // The cubic-convolution kernel taps sum to 1 for any fractional t.
        for &t in &[0.0f32, 0.25, 0.5, 0.75, 0.99] {
            let w = cubic_weights(t);
            let s: f32 = w.iter().sum();
            assert!((s - 1.0).abs() < 1e-5, "t={t} sum={s}");
        }
    }

    #[test]
    fn abs_pos_identity_when_grid_matches() -> FocrResult<()> {
        // src grid == target grid -> passthrough.
        let pos = vec![1.0, 2.0, 3.0, 4.0]; // 2x2x1
        let out = abs_pos(&pos, 2, 2, 1, 2, 2)?;
        assert_eq!(out, pos);
        Ok(())
    }

    #[test]
    fn abs_pos_bicubic_constant_field_is_constant() -> FocrResult<()> {
        // A constant field must remain constant under bicubic resample
        // (partition-of-unity weights). src 4x4 of 7.0 -> target 6x6.
        let dim = 1;
        let pos = vec![7.0f32; 4 * 4 * dim];
        let out = abs_pos(&pos, 4, 4, dim, 6, 6)?;
        assert_eq!(out.len(), 6 * 6);
        for &v in &out {
            assert!((v - 7.0).abs() < 1e-3, "got {v}");
        }
        Ok(())
    }

    #[test]
    fn abs_pos_rejects_malformed_source_len_without_panic() {
        assert_err_contains(abs_pos(&[1.0, 2.0, 3.0], 2, 2, 1, 2, 2), "pos_embed len");
        assert_err_contains(abs_pos(&[1.0, 2.0, 3.0], 2, 2, 1, 3, 3), "pos_embed len");
    }

    #[test]
    fn abs_pos_rejects_invalid_geometry_without_panic() {
        assert_err_contains(abs_pos(&[], 0, 2, 1, 2, 2), "must be non-zero");
        assert_err_contains(abs_pos(&[], usize::MAX, 2, 1, 2, 2), "src_h*src_w");
    }

    #[test]
    fn get_rel_pos_indexes_table_directly() {
        // size==q==k==2 -> table has 2*2-1=3 rows; hd=1.
        // table rows [10, 20, 30] for offsets (q-k)+(k-1):
        //   q0,k0: 0-0+1 = 1 -> 20
        //   q0,k1: 0-1+1 = 0 -> 10
        //   q1,k0: 1-0+1 = 2 -> 30
        //   q1,k1: 1-1+1 = 1 -> 20
        let table = vec![10.0, 20.0, 30.0];
        let r = get_rel_pos(2, 2, &table, 2, 1);
        // layout [q, k, hd]
        assert_eq!(r[0], 20.0); // q0,k0
        assert_eq!(r[1], 10.0); // q0,k1
        assert_eq!(r[2], 30.0); // q1,k0
        assert_eq!(r[3], 20.0); // q1,k1
    }

    #[test]
    fn decomposed_rel_pos_bias_matches_direct_inner_loop_formula() {
        let (gh, gw, hd) = (3, 2, 5);
        let n = gh * gw;
        let qh: Vec<f32> = (0..n * hd)
            .map(|i| ((i % 11) as f32 - 5.0) * 0.013)
            .collect();
        let rh: Vec<f32> = (0..gh * gh * hd)
            .map(|i| ((i % 7) as f32 - 3.0) * 0.017)
            .collect();
        let rw: Vec<f32> = (0..gw * gw * hd)
            .map(|i| ((i % 5) as f32 - 2.0) * 0.019)
            .collect();

        let (rel_h_bias, rel_w_bias) = decomposed_rel_pos_bias(&qh, &rh, &rw, gh, gw, hd);
        for i in 0..n {
            let qy = i / gw;
            let qx = i % gw;
            let qi = &qh[i * hd..(i + 1) * hd];
            for j in 0..n {
                let ky = j / gw;
                let kx = j % gw;
                let rh_base = (qy * gh + ky) * hd;
                let rw_base = (qx * gw + kx) * hd;
                let mut expected_h = 0.0f32;
                let mut expected_w = 0.0f32;
                for c in 0..hd {
                    expected_h += qi[c] * rh[rh_base + c];
                    expected_w += qi[c] * rw[rw_base + c];
                }
                assert_eq!(rel_h_bias[i * gh + ky], expected_h);
                assert_eq!(rel_w_bias[i * gw + kx], expected_w);
            }
        }
    }

    /// Build a tiny single-block SAM with `dim=NUM_HEADS*hd`. Here we use the
    /// real `EMBED_DIM`/`NUM_HEADS` but a tiny 2x2 grid (no window padding since
    /// global block) to exercise attention + rel-pos + residual + MLP shapes.
    fn tiny_block(window: usize) -> BlockP {
        let dim = EMBED_DIM;
        let hd = HEAD_DIM;
        // zero rel-pos so attention reduces to plain SDPA (tables sized to the
        // grid we test: 2x2 -> 2*2-1 = 3 rows).
        let size = 2;
        let rel_rows = 2 * size - 1;
        BlockP {
            norm1: LayerNormP {
                w: vec![1.0; dim],
                b: vec![0.0; dim],
            },
            attn: AttnP {
                qkv: test_linear(identity_block_3(dim), vec![0.0; 3 * dim], 3 * dim, dim),
                proj: test_linear(identity_mat(dim), vec![0.0; dim], dim, dim),
                rel_pos_h: vec![0.0; rel_rows * hd],
                rel_pos_w: vec![0.0; rel_rows * hd],
                size_h: size,
                size_w: size,
            },
            norm2: LayerNormP {
                w: vec![1.0; dim],
                b: vec![0.0; dim],
            },
            lin1: test_linear(
                vec![0.0; MLP_HIDDEN * dim],
                vec![0.0; MLP_HIDDEN],
                MLP_HIDDEN,
                dim,
            ),
            lin2: test_linear(vec![0.0; dim * MLP_HIDDEN], vec![0.0; dim], dim, MLP_HIDDEN),
            window,
        }
    }

    /// Identity `[dim, dim]` row-major.
    fn identity_mat(dim: usize) -> Vec<f32> {
        let mut w = vec![0.0f32; dim * dim];
        for i in 0..dim {
            w[i * dim + i] = 1.0;
        }
        w
    }

    /// bd-4l71 acceptance: the row-chunked block MLP (the streamed lane's
    /// bounded-scratch `lin2(gelu(lin1(x)))`) is BIT-IDENTICAL to the
    /// whole-tensor form for every row partition. Every op in the MLP is
    /// row-independent, so the chunk height can only change how many rows a
    /// kernel call sees — never a value.
    #[test]
    fn row_chunked_block_mlp_is_bit_identical() -> FocrResult<()> {
        let dim = EMBED_DIM;
        let mut blk = tiny_block(0);
        // A NON-zero MLP (tiny_block zeroes it) so the comparison has teeth.
        blk.lin1 = test_linear(
            (0..MLP_HIDDEN * dim)
                .map(|i| (((i * 31 + 7) % 101) as f32 - 50.0) * 3.0e-4)
                .collect(),
            (0..MLP_HIDDEN)
                .map(|i| ((i % 9) as f32 - 4.0) * 0.01)
                .collect(),
            MLP_HIDDEN,
            dim,
        );
        blk.lin2 = test_linear(
            (0..dim * MLP_HIDDEN)
                .map(|i| (((i * 17 + 3) % 83) as f32 - 41.0) * 4.0e-4)
                .collect(),
            (0..dim).map(|i| ((i % 6) as f32 - 3.0) * 0.02).collect(),
            dim,
            MLP_HIDDEN,
        );
        let rows = 13usize;
        let x = Mat::from_vec(
            rows,
            dim,
            (0..rows * dim)
                .map(|i| ((i % 23) as f32 - 11.0) * 0.004)
                .collect(),
        );
        let mut whole = blk.lin1.apply(&x)?;
        nn::gelu(&mut whole);
        let whole = blk.lin2.apply(&whole)?;
        for chunk in [1usize, 2, 5, 13, 512] {
            let chunked = mlp_row_chunked_with(&blk, &x, chunk)?;
            assert_eq!(chunked.shape(), whole.shape());
            assert_eq!(
                chunked
                    .data
                    .iter()
                    .map(|f| f.to_bits())
                    .collect::<Vec<u32>>(),
                whole.data.iter().map(|f| f.to_bits()).collect::<Vec<u32>>(),
                "chunk {chunk}: row-chunked MLP must be bit-identical"
            );
        }
        assert!(
            mlp_row_chunked_with(&blk, &x, 0).is_err(),
            "a zero chunk height must be rejected, not loop forever"
        );
        Ok(())
    }

    /// bd-4l71 acceptance: the bounded-scratch global attention
    /// ([`attention_global_bounded_with`]) is BIT-IDENTICAL to the historical
    /// full-logits [`attention`] across slab sizes that partition the query
    /// rows evenly, unevenly, and not at all — with a NON-identity qkv, biases,
    /// and non-zero rel-pos tables so every term is exercised.
    #[test]
    fn bounded_global_attention_is_bit_identical() -> FocrResult<()> {
        let dim = EMBED_DIM;
        let hd = HEAD_DIM;
        for &(grid, slabs) in &[(6usize, [5usize, 16, 36, 512]), (8, [3, 16, 64, 512])] {
            let n = grid * grid;
            let rel_rows = 2 * grid - 1;
            let qkv_w: Vec<f32> = (0..3 * dim * dim)
                .map(|i| (((i * 37 + 11) % 97) as f32 - 48.0) * 4.0e-4)
                .collect();
            let qkv_b: Vec<f32> = (0..3 * dim)
                .map(|i| ((i % 7) as f32 - 3.0) * 0.01)
                .collect();
            let proj_w: Vec<f32> = (0..dim * dim)
                .map(|i| (((i * 53 + 5) % 89) as f32 - 44.0) * 5.0e-4)
                .collect();
            let proj_b: Vec<f32> = (0..dim).map(|i| ((i % 5) as f32 - 2.0) * 0.02).collect();
            let attn = AttnP {
                qkv: test_linear(qkv_w, qkv_b, 3 * dim, dim),
                proj: test_linear(proj_w, proj_b, dim, dim),
                rel_pos_h: (0..rel_rows * hd)
                    .map(|i| ((i % 13) as f32 - 6.0) * 0.0011)
                    .collect(),
                rel_pos_w: (0..rel_rows * hd)
                    .map(|i| ((i % 11) as f32 - 5.0) * 0.0009)
                    .collect(),
                size_h: grid,
                size_w: grid,
            };
            let x = Mat::from_vec(
                n,
                dim,
                (0..n * dim)
                    .map(|i| ((i % 29) as f32 - 14.0) * 0.003)
                    .collect(),
            );
            let full = attention(&attn, &x, grid, grid, None)?;
            for &slab in &slabs {
                let bounded = attention_global_bounded_with(&attn, &x, grid, grid, slab)?;
                assert_eq!(bounded.shape(), full.shape());
                assert_eq!(
                    bounded
                        .data
                        .iter()
                        .map(|f| f.to_bits())
                        .collect::<Vec<u32>>(),
                    full.data.iter().map(|f| f.to_bits()).collect::<Vec<u32>>(),
                    "grid {grid} slab {slab}: bounded global attention must be bit-identical"
                );
            }
            assert!(full.data.iter().any(|&v| v != 0.0));
        }
        Ok(())
    }

    /// bd-4l71: the streamed block hydration ([`sam_block_from`]) IS the cached
    /// hydration ([`sam_weights_from`] calls it), and the streamed forward core
    /// is the cached forward core — pinned structurally here by comparing a
    /// hydrated block against the whole-tower build on synthetic weights.
    #[test]
    fn sam_block_from_matches_whole_tower_hydration() -> FocrResult<()> {
        use crate::quant::focrq::FocrqBuilder;

        let prefix = "model.sam_model";
        let dim = 24usize; // divisible by NUM_HEADS = 12
        let mut b = FocrqBuilder::new();
        // Shared with the streamed-vs-cached gate above, so both prove their
        // claim about the SAME tower.
        test_support::add_synth_sam_tower(&mut b, prefix, dim);
        let weights = Weights::from_bytes(b.build()).expect("synthetic SAM parses");

        let whole = sam_weights_from(&weights, prefix)?;
        assert_eq!(whole.blocks.len(), DEPTH);
        for i in 0..DEPTH {
            let solo = sam_block_from(&weights, prefix, i)?;
            let cached = &whole.blocks[i];
            assert_eq!(solo.window, cached.window, "block {i} window");
            assert_eq!(solo.norm1.w, cached.norm1.w, "block {i} norm1.w");
            assert_eq!(
                solo.attn.qkv.wt.data, cached.attn.qkv.wt.data,
                "block {i} qkv"
            );
            assert_eq!(solo.attn.qkv.b, cached.attn.qkv.b, "block {i} qkv bias");
            assert_eq!(
                solo.attn.proj.wt.data, cached.attn.proj.wt.data,
                "block {i} proj"
            );
            assert_eq!(
                solo.attn.rel_pos_h, cached.attn.rel_pos_h,
                "block {i} rel_pos_h"
            );
            assert_eq!(solo.lin1.wt.data, cached.lin1.wt.data, "block {i} lin1");
            assert_eq!(solo.lin2.wt.data, cached.lin2.wt.data, "block {i} lin2");
        }
        // Head-only hydration matches the whole tower's head fields.
        let head = sam_head_from(&weights, prefix)?;
        assert!(head.blocks.is_empty());
        assert_eq!(head.patch_embed.w, whole.patch_embed.w);
        assert_eq!(head.pos_embed, whole.pos_embed);
        assert_eq!(head.net3.w, whole.net3.w);
        Ok(())
    }

    /// qkv weight `[3*dim, dim]` that copies x into each of q,k,v (identity per
    /// block).
    fn identity_block_3(dim: usize) -> Vec<f32> {
        let mut w = vec![0.0f32; 3 * dim * dim];
        for s in 0..3 {
            for i in 0..dim {
                let row = s * dim + i;
                w[row * dim + i] = 1.0;
            }
        }
        w
    }

    fn attention_scalar_reference(p: &AttnP, x: &Mat, gh: usize, gw: usize) -> FocrResult<Mat> {
        let n = gh * gw;
        let dim = x.cols;
        let nh = NUM_HEADS;
        let hd = dim / nh;
        let scale = (hd as f32).powf(-0.5);
        let qkv = p.qkv.apply(x)?;
        let mut q = vec![0.0f32; nh * n * hd];
        let mut k = vec![0.0f32; nh * n * hd];
        let mut v = vec![0.0f32; nh * n * hd];
        for r in 0..n {
            let row = qkv.row(r);
            for head in 0..nh {
                for d in 0..hd {
                    q[(head * n + r) * hd + d] = row[head * hd + d];
                    k[(head * n + r) * hd + d] = row[(nh + head) * hd + d];
                    v[(head * n + r) * hd + d] = row[(2 * nh + head) * hd + d];
                }
            }
        }

        let rh = get_rel_pos(gh, gh, &p.rel_pos_h, p.size_h, hd);
        let rw = get_rel_pos(gw, gw, &p.rel_pos_w, p.size_w, hd);
        let mut out = vec![0.0f32; nh * n * hd];
        for head in 0..nh {
            let qh = &q[head * n * hd..(head + 1) * n * hd];
            let kh = &k[head * n * hd..(head + 1) * n * hd];
            let vh = &v[head * n * hd..(head + 1) * n * hd];
            let (rel_h_bias, rel_w_bias) = decomposed_rel_pos_bias(qh, &rh, &rw, gh, gw, hd);
            let mut logits = vec![0.0f32; n * n];
            for i in 0..n {
                let qi = &qh[i * hd..(i + 1) * hd];
                for j in 0..n {
                    let ky = j / gw;
                    let kx = j % gw;
                    let kj = &kh[j * hd..(j + 1) * hd];
                    let mut dot = 0.0f32;
                    for c in 0..hd {
                        dot += qi[c] * kj[c];
                    }
                    logits[i * n + j] =
                        scale * dot + rel_h_bias[i * gh + ky] + rel_w_bias[i * gw + kx];
                }
            }
            let mut lm = Mat::from_vec(n, n, logits);
            nn::softmax_rows(&mut lm)?;
            for i in 0..n {
                let probs = lm.row(i);
                let o = &mut out[(head * n + i) * hd..(head * n + i + 1) * hd];
                for (j, &pj) in probs.iter().enumerate() {
                    let vj = &vh[j * hd..(j + 1) * hd];
                    for c in 0..hd {
                        o[c] += pj * vj[c];
                    }
                }
            }
        }

        let mut ctx = vec![0.0f32; n * dim];
        for head in 0..nh {
            for r in 0..n {
                let src = (head * n + r) * hd;
                let dst = r * dim + head * hd;
                ctx[dst..dst + hd].copy_from_slice(&out[src..src + hd]);
            }
        }
        p.proj.apply(&Mat::from_vec(n, dim, ctx))
    }

    #[test]
    fn attention_gemm_matches_scalar_reference_with_relpos() -> FocrResult<()> {
        let dim = EMBED_DIM;
        let hd = HEAD_DIM;
        let grid = 3usize;
        let n = grid * grid;
        let rel_rows = 2 * grid - 1;
        let attn = AttnP {
            qkv: test_linear(identity_block_3(dim), vec![0.0; 3 * dim], 3 * dim, dim),
            proj: test_linear(identity_mat(dim), vec![0.0; dim], dim, dim),
            rel_pos_h: (0..rel_rows * hd)
                .map(|i| ((i % 13) as f32 - 6.0) * 0.0011)
                .collect(),
            rel_pos_w: (0..rel_rows * hd)
                .map(|i| ((i % 11) as f32 - 5.0) * 0.0009)
                .collect(),
            size_h: grid,
            size_w: grid,
        };
        let x = Mat::from_vec(
            n,
            dim,
            (0..n * dim)
                .map(|i| ((i % 29) as f32 - 14.0) * 0.003)
                .collect(),
        );
        let got = attention(&attn, &x, grid, grid, None)?;
        let expected = attention_scalar_reference(&attn, &x, grid, grid)?;
        let max_abs = got
            .data
            .iter()
            .zip(expected.data.iter())
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max);
        assert!(max_abs <= 2.0e-6, "max_abs={max_abs}");
        Ok(())
    }

    #[test]
    fn attention_rejects_malformed_qkv_and_projection_shapes() {
        let dim = EMBED_DIM;
        let x = Mat::from_vec(4, dim, vec![0.0; 4 * dim]);

        // Each Linear is VALID in isolation (the constructor enforces that);
        // its out is simply incompatible with the attention dim, which is
        // exactly what attention's own shape guards must reject.
        let mut bad_qkv = tiny_block(0).attn;
        bad_qkv.qkv = test_linear(
            vec![0.0; (3 * dim - 1) * dim],
            vec![0.0; 3 * dim - 1],
            3 * dim - 1,
            dim,
        );
        assert_err_contains(attention(&bad_qkv, &x, 2, 2, None), "qkv shape");

        let mut bad_proj = tiny_block(0).attn;
        bad_proj.proj = test_linear(vec![0.0; (dim - 1) * dim], vec![0.0; dim - 1], dim - 1, dim);
        assert_err_contains(attention(&bad_proj, &x, 2, 2, None), "proj shape");
    }

    #[test]
    fn block_forward_preserves_shape_global() -> FocrResult<()> {
        // global block (window=0) on a 2x2 grid; zero MLP + zero rel-pos so
        // output = x + attn(LN(x)) with proj/qkv identity. We only assert shape
        // and that the residual path ran (output differs from input generally).
        let blk = tiny_block(0);
        let n = 4;
        let dim = EMBED_DIM;
        let mut data = vec![0.0f32; n * dim];
        for (i, v) in data.iter_mut().enumerate() {
            *v = ((i % 7) as f32) * 0.1 - 0.3;
        }
        let x = Mat::from_vec(n, dim, data);
        let out = block_forward(&blk, &x, 2, 2)?;
        assert_eq!(out.shape(), (n, dim));
        Ok(())
    }

    #[test]
    fn block_forward_windowed_pads_and_unpartitions() -> FocrResult<()> {
        // windowed block, window=3 over a 2x2 grid forces padding to 3x3 then
        // strips it back to 2x2. Shape must round-trip.
        let blk = tiny_block(3);
        let n = 4;
        let dim = EMBED_DIM;
        let data: Vec<f32> = (0..n * dim).map(|i| (i as f32 % 5.0) * 0.01).collect();
        let x = Mat::from_vec(n, dim, data);
        let out = block_forward(&blk, &x, 2, 2)?;
        assert_eq!(out.shape(), (n, dim));
        Ok(())
    }

    #[test]
    fn block_forward_rejects_mlp_residual_shape_mismatch() {
        let mut blk = tiny_block(0);
        // A VALID Linear (the constructor validates lengths) whose out is one
        // short of EMBED_DIM, so the mlp output genuinely mismatches h1 and the
        // residual shape guard fires. Mutating only the pub `out`/`b` fields
        // would leave the private pre-transposed weight at the old width and
        // panic in the bias add instead of erroring.
        blk.lin2 = test_linear(
            vec![0.0; (EMBED_DIM - 1) * MLP_HIDDEN],
            vec![0.0; EMBED_DIM - 1],
            EMBED_DIM - 1,
            MLP_HIDDEN,
        );
        let n = 4;
        let dim = EMBED_DIM;
        let data: Vec<f32> = (0..n * dim).map(|i| (i as f32 % 5.0) * 0.01).collect();
        let x = Mat::from_vec(n, dim, data);

        assert_err_contains(
            block_forward(&blk, &x, 2, 2),
            "vision_sam block mlp residual",
        );
    }

    #[test]
    fn attention_zero_relpos_is_uniform_average_for_equal_q() -> FocrResult<()> {
        // With identical token vectors, equal logits => uniform softmax =>
        // attention output == the (shared) value vector (proj identity).
        let dim = EMBED_DIM;
        let hd = HEAD_DIM;
        let size = 2;
        let rel_rows = 2 * size - 1;
        let attn = AttnP {
            qkv: test_linear(identity_block_3(dim), vec![0.0; 3 * dim], 3 * dim, dim),
            proj: test_linear(identity_mat(dim), vec![0.0; dim], dim, dim),
            rel_pos_h: vec![0.0; rel_rows * hd],
            rel_pos_w: vec![0.0; rel_rows * hd],
            size_h: size,
            size_w: size,
        };
        // all 4 tokens identical = ones
        let x = Mat::from_vec(4, dim, vec![1.0; 4 * dim]);
        let out = attention(&attn, &x, 2, 2, None)?;
        assert_eq!(out.shape(), (4, dim));
        // uniform average of identical value vectors -> 1.0 everywhere
        for &v in &out.data {
            assert!((v - 1.0).abs() < 1e-4, "got {v}");
        }
        Ok(())
    }

    #[test]
    #[ignore = "local perf probe; run explicitly with --ignored --nocapture"]
    fn sam_attention_relpos_bias_local_probe() -> FocrResult<()> {
        let dim = EMBED_DIM;
        let hd = HEAD_DIM;
        let grid = 14usize;
        let n = grid * grid;
        let rel_rows = 2 * grid - 1;
        let attn = AttnP {
            qkv: test_linear(identity_block_3(dim), vec![0.0; 3 * dim], 3 * dim, dim),
            proj: test_linear(identity_mat(dim), vec![0.0; dim], dim, dim),
            rel_pos_h: (0..rel_rows * hd)
                .map(|i| ((i % 17) as f32 - 8.0) * 0.0007)
                .collect(),
            rel_pos_w: (0..rel_rows * hd)
                .map(|i| ((i % 19) as f32 - 9.0) * 0.0005)
                .collect(),
            size_h: grid,
            size_w: grid,
        };
        let x = Mat::from_vec(
            n,
            dim,
            (0..n * dim)
                .map(|i| ((i % 31) as f32 - 15.0) * 0.002)
                .collect(),
        );

        let runs = std::env::var("FOCR_SAM_ATTN_PROBE_RUNS")
            .ok()
            .and_then(|s| s.parse::<usize>().ok())
            .unwrap_or(3)
            .max(1);
        let warm = attention(&attn, &x, grid, grid, None)?;
        let warm_checksum: f32 = warm.data.iter().step_by(97).copied().sum();
        let start = Instant::now();
        let mut checksum = 0.0f32;
        for _ in 0..runs {
            let out = attention(&attn, &x, grid, grid, None)?;
            checksum += out.data.iter().step_by(97).copied().sum::<f32>();
        }
        let elapsed = start.elapsed();
        let total_ms = elapsed.as_secs_f64() * 1000.0;
        let avg_ms = total_ms / runs as f64;
        assert!(checksum.is_finite());
        println!(
            "{}",
            json!({
                "probe": "sam_attention_relpos_bias_local_probe",
                "grid": grid,
                "tokens": n,
                "dim": dim,
                "heads": NUM_HEADS,
                "head_dim": hd,
                "runs": runs,
                "total_ms": total_ms,
                "avg_ms": avg_ms,
                "warm_checksum": warm_checksum,
                "checksum": checksum
            })
        );
        Ok(())
    }

    #[test]
    fn forward_with_end_to_end_shapes() -> FocrResult<()> {
        // Tiny end-to-end: H=W=32 image -> patch grid 2x2 -> neck keeps 2x2 ->
        // net_2 /2 -> 1x1 -> net_3 /2 -> 1x1. Output [OUT_CH, 1].
        let h = 32;
        let gh = h / PATCH; // 2
        let gw = gh;

        // patch embed conv 3->768 k16 s16
        let patch_embed = Conv {
            w: vec![0.0; EMBED_DIM * 3 * PATCH * PATCH],
            b: Some(vec![0.0; EMBED_DIM]),
            out_ch: EMBED_DIM,
            in_ch: 3,
            kh: PATCH,
            kw: PATCH,
        };

        let blocks: Vec<BlockP> = (0..DEPTH)
            .map(|i| {
                let window = if GLOBAL_BLOCKS.contains(&i) {
                    0
                } else {
                    WINDOW
                };
                // rel-pos tables must be sized to either the window or the grid.
                let size = if window == 0 { gh } else { window };
                let rel_rows = 2 * size - 1;
                let dim = EMBED_DIM;
                let hd = HEAD_DIM;
                BlockP {
                    norm1: LayerNormP {
                        w: vec![1.0; dim],
                        b: vec![0.0; dim],
                    },
                    attn: AttnP {
                        qkv: test_linear(
                            vec![0.0; 3 * dim * dim],
                            vec![0.0; 3 * dim],
                            3 * dim,
                            dim,
                        ),
                        proj: test_linear(vec![0.0; dim * dim], vec![0.0; dim], dim, dim),
                        rel_pos_h: vec![0.0; rel_rows * hd],
                        rel_pos_w: vec![0.0; rel_rows * hd],
                        size_h: size,
                        size_w: size,
                    },
                    norm2: LayerNormP {
                        w: vec![1.0; dim],
                        b: vec![0.0; dim],
                    },
                    lin1: test_linear(
                        vec![0.0; MLP_HIDDEN * dim],
                        vec![0.0; MLP_HIDDEN],
                        MLP_HIDDEN,
                        dim,
                    ),
                    lin2: test_linear(vec![0.0; dim * MLP_HIDDEN], vec![0.0; dim], dim, MLP_HIDDEN),
                    window,
                }
            })
            .collect();

        let w = SamWeights {
            patch_embed,
            pos_embed: vec![0.0; gh * gw * EMBED_DIM],
            pos_grid_h: gh,
            pos_grid_w: gw,
            blocks,
            neck_conv1: Conv {
                w: vec![0.0; NECK_CH * EMBED_DIM],
                b: None,
                out_ch: NECK_CH,
                in_ch: EMBED_DIM,
                kh: 1,
                kw: 1,
            },
            neck_ln1: LayerNormP {
                w: vec![1.0; NECK_CH],
                b: vec![0.0; NECK_CH],
            },
            neck_conv2: Conv {
                w: vec![0.0; NECK_CH * NECK_CH * 9],
                b: None,
                out_ch: NECK_CH,
                in_ch: NECK_CH,
                kh: 3,
                kw: 3,
            },
            neck_ln2: LayerNormP {
                w: vec![1.0; NECK_CH],
                b: vec![0.0; NECK_CH],
            },
            net2: Conv {
                w: vec![0.0; NET2_CH * NECK_CH * 9],
                b: None,
                out_ch: NET2_CH,
                in_ch: NECK_CH,
                kh: 3,
                kw: 3,
            },
            net3: Conv {
                w: vec![0.0; OUT_CH * NET2_CH * 9],
                b: None,
                out_ch: OUT_CH,
                in_ch: NET2_CH,
                kh: 3,
                kw: 3,
            },
        };

        let image = Mat::from_vec(3, h * h, vec![0.5; 3 * h * h]);
        let out = forward_with(&w, &image, h, h)?;
        // gh=2 -> net_2 -> 1 -> net_3 -> 1 ; 1024 channels x 1 spatial.
        assert_eq!(out.shape(), (OUT_CH, 1));
        // all-zero weights -> all-zero feature.
        assert!(out.data.iter().all(|&v| v.abs() < 1e-6));
        Ok(())
    }

    #[test]
    fn forward_with_rejects_bad_channels() {
        let w = tiny_weights_minimal();
        let bad = Mat::from_vec(2, 32 * 32, vec![0.0; 2 * 32 * 32]);
        assert!(forward_with(&w, &bad, 32, 32).is_err());
    }

    #[test]
    fn forward_with_rejects_non_patch_multiple() {
        let w = tiny_weights_minimal();
        // 20 is not a multiple of PATCH(16)
        let img = Mat::from_vec(3, 20 * 20, vec![0.0; 3 * 20 * 20]);
        assert!(forward_with(&w, &img, 20, 20).is_err());
    }

    #[test]
    fn forward_with_rejects_zero_spatial_dims_before_conv() {
        let w = tiny_weights_minimal();
        let img = Mat::from_vec(3, 0, Vec::new());
        assert!(matches!(
            forward_with(&w, &img, 0, 0),
            Err(err) if err.to_string().contains("non-zero")
        ));
    }

    #[test]
    fn forward_with_rejects_spatial_product_overflow_before_conv() {
        let w = tiny_weights_minimal();
        let img = Mat::from_vec(3, 0, Vec::new());
        assert!(matches!(
            forward_with(&w, &img, usize::MAX, 2),
            Err(err) if err.to_string().contains("H*W")
        ));
    }

    /// A structurally-valid all-zero SamWeights for negative-path tests
    /// (shape checks fire before any heavy compute).
    fn tiny_weights_minimal() -> SamWeights {
        let gh = 2;
        let blocks: Vec<BlockP> = (0..DEPTH)
            .map(|i| {
                let window = if GLOBAL_BLOCKS.contains(&i) {
                    0
                } else {
                    WINDOW
                };
                let size = if window == 0 { gh } else { window };
                let rel_rows = 2 * size - 1;
                let dim = EMBED_DIM;
                let hd = HEAD_DIM;
                BlockP {
                    norm1: LayerNormP {
                        w: vec![1.0; dim],
                        b: vec![0.0; dim],
                    },
                    attn: AttnP {
                        qkv: test_linear(
                            vec![0.0; 3 * dim * dim],
                            vec![0.0; 3 * dim],
                            3 * dim,
                            dim,
                        ),
                        proj: test_linear(vec![0.0; dim * dim], vec![0.0; dim], dim, dim),
                        rel_pos_h: vec![0.0; rel_rows * hd],
                        rel_pos_w: vec![0.0; rel_rows * hd],
                        size_h: size,
                        size_w: size,
                    },
                    norm2: LayerNormP {
                        w: vec![1.0; dim],
                        b: vec![0.0; dim],
                    },
                    lin1: test_linear(
                        vec![0.0; MLP_HIDDEN * dim],
                        vec![0.0; MLP_HIDDEN],
                        MLP_HIDDEN,
                        dim,
                    ),
                    lin2: test_linear(vec![0.0; dim * MLP_HIDDEN], vec![0.0; dim], dim, MLP_HIDDEN),
                    window,
                }
            })
            .collect();
        SamWeights {
            patch_embed: Conv {
                w: vec![0.0; EMBED_DIM * 3 * PATCH * PATCH],
                b: Some(vec![0.0; EMBED_DIM]),
                out_ch: EMBED_DIM,
                in_ch: 3,
                kh: PATCH,
                kw: PATCH,
            },
            pos_embed: vec![0.0; gh * gh * EMBED_DIM],
            pos_grid_h: gh,
            pos_grid_w: gh,
            blocks,
            neck_conv1: Conv {
                w: vec![0.0; NECK_CH * EMBED_DIM],
                b: None,
                out_ch: NECK_CH,
                in_ch: EMBED_DIM,
                kh: 1,
                kw: 1,
            },
            neck_ln1: LayerNormP {
                w: vec![1.0; NECK_CH],
                b: vec![0.0; NECK_CH],
            },
            neck_conv2: Conv {
                w: vec![0.0; NECK_CH * NECK_CH * 9],
                b: None,
                out_ch: NECK_CH,
                in_ch: NECK_CH,
                kh: 3,
                kw: 3,
            },
            neck_ln2: LayerNormP {
                w: vec![1.0; NECK_CH],
                b: vec![0.0; NECK_CH],
            },
            net2: Conv {
                w: vec![0.0; NET2_CH * NECK_CH * 9],
                b: None,
                out_ch: NET2_CH,
                in_ch: NECK_CH,
                kh: 3,
                kw: 3,
            },
            net3: Conv {
                w: vec![0.0; OUT_CH * NET2_CH * 9],
                b: None,
                out_ch: OUT_CH,
                in_ch: NET2_CH,
                kh: 3,
                kw: 3,
            },
        }
    }

    // ── bd-1azu.10: batched-SAM parity (model-free, byte-exact) ──────────────
    // Deterministic NON-trivial weights so a cross-view attention leak OR an
    // M-dependent GEMM would change the output and fail the bit-exact assert.
    // (The all-zero `tiny_weights_minimal()` is too weak — every stage collapses
    // to a constant, so it could not detect a view-to-view leak.)
    fn det(i: usize, salt: usize) -> f32 {
        let x = (i as u64)
            .wrapping_mul(2_654_435_761)
            .wrapping_add((salt as u64).wrapping_mul(40_503))
            % 1000;
        (x as f32) / 1000.0 - 0.5
    }
    fn rand_lin(out: usize, inn: usize, salt: usize) -> Linear {
        test_linear(
            (0..out * inn).map(|i| det(i, salt)).collect(),
            (0..out).map(|i| det(i, salt + 1)).collect(),
            out,
            inn,
        )
    }
    fn rand_ln(dim: usize, salt: usize) -> LayerNormP {
        LayerNormP {
            // gains near 1.0 (1 + small varied offset), varied bias.
            w: (0..dim).map(|i| 1.0 + det(i, salt)).collect(),
            b: (0..dim).map(|i| det(i, salt + 2)).collect(),
        }
    }
    fn rand_conv(
        out_ch: usize,
        in_ch: usize,
        kh: usize,
        kw: usize,
        bias: bool,
        salt: usize,
    ) -> Conv {
        Conv {
            w: (0..out_ch * in_ch * kh * kw)
                .map(|i| det(i, salt))
                .collect(),
            b: if bias {
                Some((0..out_ch).map(|i| det(i, salt + 1)).collect())
            } else {
                None
            },
            out_ch,
            in_ch,
            kh,
            kw,
        }
    }

    /// A REAL-shaped (EMBED_DIM/NUM_HEADS/DEPTH/WINDOW/GLOBAL_BLOCKS/PATCH) SAM
    /// with deterministic varied weights over a `gh×gw` patch grid. Rel-pos
    /// tables are sized to the window (windowed blocks) or the grid (global
    /// blocks), so no rel-pos interpolation fires (the deployed identity path).
    fn tiny_weights_nontrivial(gh: usize, gw: usize) -> SamWeights {
        let dim = EMBED_DIM;
        let hd = HEAD_DIM;
        let blocks: Vec<BlockP> = (0..DEPTH)
            .map(|i| {
                let window = if GLOBAL_BLOCKS.contains(&i) {
                    0
                } else {
                    WINDOW
                };
                let size_h = if window == 0 { gh } else { window };
                let size_w = if window == 0 { gw } else { window };
                let rel_rows_h = 2 * size_h - 1;
                let rel_rows_w = 2 * size_w - 1;
                BlockP {
                    norm1: rand_ln(dim, 100 + i * 13),
                    attn: AttnP {
                        qkv: rand_lin(3 * dim, dim, 200 + i * 13),
                        proj: rand_lin(dim, dim, 300 + i * 13),
                        rel_pos_h: (0..rel_rows_h * hd).map(|j| det(j, 700 + i * 13)).collect(),
                        rel_pos_w: (0..rel_rows_w * hd).map(|j| det(j, 800 + i * 13)).collect(),
                        size_h,
                        size_w,
                    },
                    norm2: rand_ln(dim, 400 + i * 13),
                    lin1: rand_lin(MLP_HIDDEN, dim, 500 + i * 13),
                    lin2: rand_lin(dim, MLP_HIDDEN, 600 + i * 13),
                    window,
                }
            })
            .collect();
        SamWeights {
            patch_embed: rand_conv(EMBED_DIM, 3, PATCH, PATCH, true, 11),
            pos_embed: (0..gh * gw * EMBED_DIM).map(|i| det(i, 13)).collect(),
            pos_grid_h: gh,
            pos_grid_w: gw,
            blocks,
            neck_conv1: rand_conv(NECK_CH, EMBED_DIM, 1, 1, false, 21),
            neck_ln1: rand_ln(NECK_CH, 23),
            neck_conv2: rand_conv(NECK_CH, NECK_CH, 3, 3, false, 25),
            neck_ln2: rand_ln(NECK_CH, 27),
            net2: rand_conv(NET2_CH, NECK_CH, 3, 3, false, 29),
            net3: rand_conv(OUT_CH, NET2_CH, 3, 3, false, 31),
        }
    }

    /// A `[3, h*win]` image with deterministic varied (bounded) pixels.
    fn nontrivial_image(h: usize, win: usize, salt: usize) -> Mat {
        let cols = h * win;
        Mat::from_vec(
            3,
            cols,
            (0..3 * cols)
                .map(|i| ((((i + salt * 7919) as f32) * 0.0007).sin()) * 0.5)
                .collect(),
        )
    }

    #[test]
    fn batched_sam_equals_per_view_byte_for_byte() -> FocrResult<()> {
        // H=W=240 -> gh=gw=15: windowed blocks (WINDOW=14) partition into a 2x2
        // grid of windows (real + zero-padded tail) AND global blocks run on the
        // 15x15 grid -> a cross-view leak in EITHER path would change a byte.
        let h = 240;
        let win = 240;
        let gh = h / PATCH;
        let gw = win / PATCH;
        let w = tiny_weights_nontrivial(gh, gw);
        let v0 = nontrivial_image(h, win, 1);
        let v1 = nontrivial_image(h, win, 2);
        let v2 = nontrivial_image(h, win, 3);

        let batched = forward_with_batched(&w, &[&v0, &v1, &v2], h, win)?;
        assert_eq!(batched.len(), 3);
        for (i, view) in [&v0, &v1, &v2].iter().enumerate() {
            let seq = forward_with(&w, view, h, win)?;
            assert_eq!(batched[i].shape(), seq.shape(), "view {i} shape");
            assert_eq!(
                batched[i].data, seq.data,
                "view {i}: batched SAM != per-view sequential (cross-view leak or M-dependence)"
            );
        }
        Ok(())
    }

    #[test]
    fn batched_sam_single_view_equals_forward_with() -> FocrResult<()> {
        let h = 240;
        let win = 240;
        let gh = h / PATCH;
        let gw = win / PATCH;
        let w = tiny_weights_nontrivial(gh, gw);
        let view = nontrivial_image(h, win, 5);
        let batched = forward_with_batched(&w, &[&view], h, win)?;
        let seq = forward_with(&w, &view, h, win)?;
        assert_eq!(batched.len(), 1);
        assert_eq!(batched[0].shape(), seq.shape());
        assert_eq!(batched[0].data, seq.data);
        Ok(())
    }

    /// bd-K2 acceptance: hoisting the streamed lane's per-block hydration OUT
    /// of the view loop (views-inner [`forward_core_views`]) reproduces the
    /// views-outer nest ([`forward_core`], one hydration sweep per view)
    /// BIT-FOR-BIT. `block_at` hands out an OWNED clone per request, exactly as
    /// [`forward_streamed_views`] hands out a freshly dequantized block, and
    /// `low_mem = true` selects the same bounded global-attention kernel the
    /// streamed lane uses.
    #[test]
    fn streamed_views_inner_equals_views_outer_byte_for_byte() -> FocrResult<()> {
        let h = 240;
        let win = 240;
        let gh = h / PATCH;
        let gw = win / PATCH;
        let w = tiny_weights_nontrivial(gh, gw);
        // The "streamed" head: block-free, blocks arrive from `block_at`.
        let mut head = tiny_weights_nontrivial(gh, gw);
        head.blocks = Vec::new();
        let blocks = w.blocks.clone();
        let mut hydrate = |i: usize| -> FocrResult<std::borrow::Cow<'static, BlockP>> {
            Ok(std::borrow::Cow::Owned(blocks[i].clone()))
        };

        // Two same-size views (the deployed unlimited page) plus a smaller one,
        // so per-view geometry (rel-pos interpolation on the global blocks) is
        // exercised too.
        let small = 128usize;
        let views = [
            nontrivial_image(h, win, 1),
            nontrivial_image(h, win, 2),
            nontrivial_image(small, small, 3),
        ];
        let dims = [(h, win), (h, win), (small, small)];

        // views-outer: a full hydration sweep per view (the old shape).
        let mut per_view = Vec::new();
        for (view, &(vh, vw)) in views.iter().zip(dims.iter()) {
            per_view.push(forward_core(
                &head,
                view,
                vh,
                vw,
                DEPTH,
                &mut hydrate,
                true,
            )?);
        }

        // views-inner: ONE hydration sweep, every view through each block.
        let refs: Vec<&Mat> = views.iter().collect();
        let hoisted = forward_core_views(&head, &refs, &dims, DEPTH, &mut hydrate, true)?;

        assert_eq!(hoisted.len(), per_view.len());
        for (i, (outer, inner)) in per_view.iter().zip(hoisted.iter()).enumerate() {
            assert_eq!(outer.shape(), inner.shape(), "view {i} shape");
            assert_eq!(
                outer.data.iter().map(|f| f.to_bits()).collect::<Vec<u32>>(),
                inner.data.iter().map(|f| f.to_bits()).collect::<Vec<u32>>(),
                "view {i}: hydration-hoisted SAM must be bit-identical to per-view"
            );
        }
        // Non-degeneracy + the views really differ (a cross-view leak or a
        // stale activation would otherwise pass trivially).
        assert!(per_view[0].data.iter().any(|&v| v != 0.0));
        assert_ne!(per_view[0].data, per_view[1].data);
        Ok(())
    }

    #[test]
    fn batched_sam_rejects_ragged_and_empty() {
        let h = 32;
        let win = 32;
        let w = tiny_weights_minimal(); // validation fires before any compute
        // empty batch -> Err.
        assert!(forward_with_batched(&w, &[], h, win).is_err());
        // ragged: second view has a different H*W (cols mismatch) -> Err.
        let a = nontrivial_image(h, win, 1);
        let b = nontrivial_image(h, win + PATCH, 2); // cols = h*(win+16) != h*win
        assert!(forward_with_batched(&w, &[&a, &b], h, win).is_err());
        // non-3-channel view -> Err.
        let bad = Mat::from_vec(2, h * win, vec![0.0; 2 * h * win]);
        assert!(forward_with_batched(&w, &[&bad], h, win).is_err());
    }
}