lattice-inference 0.9.0

Pure Rust transformer inference engine — safetensors loading, SIMD matmul, BGE/Qwen3 embeddings
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
//! Loader for the real Qwen3.5 vision-language checkpoint's 153 `model.visual.*`
//! tensors (ADR-069 stage S2).
//!
//! This is deliberately independent from [`super::vit::VisionWeights`] (the ADR-049
//! CPU ViT scaffold, which targets a hypothetical 7B model). Reconciling that
//! scaffold's forward-pass semantics to the real 12-layer/768-hidden geometry —
//! including its temporal-patch-folded patch embedding (`[hidden, in_channels,
//! temporal_patch_size, patch_size, patch_size]`, vs. the scaffold's
//! `[d_model, patch_size^2 * 3]` assumption with no temporal factor) and dropping
//! the scaffold's windowed-attention assumption (the real checkpoint has no
//! window-specific weights) — is ADR-069 stage S3a (CPU reference; the Metal
//! port is a separate S3b fast-follow gated against this CPU reference), not
//! S1/S2. This module only loads the real tensors as flat data; it does not
//! wire them into any forward pass.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

use crate::error::InferenceError;
use crate::model::qwen35_config::VisionModelConfig;
use crate::quant::q4_manifest;
use crate::weights::f32_weights::{
    SafetensorsFile, ShardedSafetensors, TensorSource, contained_shard_path,
    open_manifest_entry_once, parse_index,
};
use crate::weights::q4_weights::{
    F16LoadError, dequantize_q4_to_f32, load_f16_tensor_from_open_file,
    load_f16_tensor_from_open_file_expecting, load_q4_from_open_file,
};

/// Tensor name to its dequantized data paired with the shape it was declared with.
///
/// The shape travels alongside the data because a checkpoint's declared shape is what
/// preflight validates against, and it must stay available after materialization for
/// the sources that cannot report a shape without reading the tensor.
type NamedTensors = HashMap<String, (Vec<f32>, Vec<usize>)>;

/// One ViT transformer block's real tensors (`model.visual.blocks.{i}.*`).
#[derive(Debug, Clone)]
pub struct VisualBlockWeights {
    /// `attn.qkv.weight` — fused Q/K/V projection, `[3 * hidden_size, hidden_size]`.
    pub qkv_weight: Vec<f32>,
    /// `attn.qkv.bias` — `[3 * hidden_size]`.
    pub qkv_bias: Vec<f32>,
    /// `attn.proj.weight` — output projection, `[hidden_size, hidden_size]`.
    pub proj_weight: Vec<f32>,
    /// `attn.proj.bias` — `[hidden_size]`.
    pub proj_bias: Vec<f32>,
    /// `mlp.linear_fc1.weight` — `[4 * hidden_size, hidden_size]`.
    pub fc1_weight: Vec<f32>,
    /// `mlp.linear_fc1.bias` — `[4 * hidden_size]`.
    pub fc1_bias: Vec<f32>,
    /// `mlp.linear_fc2.weight` — `[hidden_size, 4 * hidden_size]`.
    pub fc2_weight: Vec<f32>,
    /// `mlp.linear_fc2.bias` — `[hidden_size]`.
    pub fc2_bias: Vec<f32>,
    /// `norm1.weight` — `[hidden_size]`.
    pub norm1_weight: Vec<f32>,
    /// `norm1.bias` — `[hidden_size]`.
    pub norm1_bias: Vec<f32>,
    /// `norm2.weight` — `[hidden_size]`.
    pub norm2_weight: Vec<f32>,
    /// `norm2.bias` — `[hidden_size]`.
    pub norm2_bias: Vec<f32>,
}

/// The `model.visual.merger.*` tensors: spatial-merge MLP projecting ViT output into
/// the decoder's embedding space.
#[derive(Debug, Clone)]
pub struct VisualMergerWeights {
    /// `merger.linear_fc1.weight` — `[spatial_merge_size^2 * hidden_size, spatial_merge_size^2 * hidden_size]`.
    pub fc1_weight: Vec<f32>,
    /// `merger.linear_fc1.bias`.
    pub fc1_bias: Vec<f32>,
    /// `merger.linear_fc2.weight` — `[out_hidden_size, spatial_merge_size^2 * hidden_size]`.
    pub fc2_weight: Vec<f32>,
    /// `merger.linear_fc2.bias` — `[out_hidden_size]`.
    pub fc2_bias: Vec<f32>,
    /// `merger.norm.weight` — `[hidden_size]`.
    pub norm_weight: Vec<f32>,
    /// `merger.norm.bias` — `[hidden_size]`.
    pub norm_bias: Vec<f32>,
}

/// All 153 real `model.visual.*` tensors from the Qwen3.5 vision-language checkpoint
/// (ADR-069 S2): `patch_embed.proj.{weight,bias}` + `pos_embed.weight` + `depth` blocks
/// (12 tensors each) + 6 merger tensors. Distinct from [`super::vit::VisionWeights`] —
/// see the module docs for why.
#[derive(Debug, Clone)]
pub struct Qwen35VisionWeights {
    /// `patch_embed.proj.weight`, flattened. Raw shape is `[hidden_size, in_channels,
    /// temporal_patch_size, patch_size, patch_size]` (a Conv3d-shaped weight); this
    /// loader does not reinterpret it, only carries it plus `patch_embed_weight_shape`.
    pub patch_embed_weight: Vec<f32>,
    /// Raw shape of `patch_embed_weight`, as above.
    pub patch_embed_weight_shape: Vec<usize>,
    /// `patch_embed.proj.bias` — `[hidden_size]`.
    pub patch_embed_bias: Vec<f32>,
    /// `pos_embed.weight` — `[num_position_embeddings, hidden_size]`.
    pub pos_embed: Vec<f32>,
    /// Per-block weights, length == `vision_cfg.depth`.
    pub blocks: Vec<VisualBlockWeights>,
    /// The spatial-merge projection MLP.
    pub merger: VisualMergerWeights,
}

impl Qwen35VisionWeights {
    /// Total tensor count actually loaded: `2 (patch_embed) + 1 (pos_embed) + depth * 12
    /// (per-block) + 6 (merger)`. For the real 0.8B checkpoint (`depth == 12`) this is 153.
    pub fn tensor_count(&self) -> usize {
        2 + 1 + self.blocks.len() * 12 + 6
    }
}

/// Load the real `model.visual.*` tensors from a model directory. Manifest-backed
/// checkpoints retain their existing precedence: `quantize_index.json`, then
/// `model.safetensors.index.json`. When neither manifest exists, exactly one regular
/// `*.safetensors` file must be present and it must be named `model.safetensors`.
/// Its header is parsed and its exact visual tensor inventory is validated before any
/// tensor payload is materialized.
///
/// # Errors
///
/// Returns [`InferenceError::ModelNotFound`] if no supported checkpoint is present,
/// [`InferenceError::InvalidSafetensors`] when an unindexed layout is ambiguous or its
/// header is corrupt, and [`InferenceError::MissingTensor`] /
/// [`InferenceError::ShapeMismatch`] if an expected tensor (derived from `vision_cfg`)
/// is absent or has the wrong shape.
pub fn load_qwen35_vision_weights(
    model_dir: &Path,
    vision_cfg: &VisionModelConfig,
) -> Result<Qwen35VisionWeights, InferenceError> {
    let mut never_cancel = || false;
    match load_qwen35_vision_weights_with_cancel(model_dir, vision_cfg, &mut never_cancel)? {
        Some(weights) => Ok(weights),
        None => Err(InferenceError::Inference(
            "non-cancellable vision weight load was cancelled".to_string(),
        )),
    }
}

/// **Unstable**: the reader-based checkpoint-loading surface may evolve before 1.0.
///
/// Load Qwen3.5 `model.visual.*` tensors from an already-open safetensors
/// reader. This is the descriptor-binding entry point for callers that also
/// load decoder tensors from the same [`SafetensorsFile`]: both components
/// then come from one mmap-backed open file even if the directory entry is
/// replaced while a multi-gigabyte checkpoint is loading.
///
/// The reader may also contain decoder tensors, but its `model.visual.*`
/// inventory must exactly match `vision_cfg`. `checkpoint_path` is used only
/// for attributable error messages; it does not reopen the file.
///
/// # Errors
///
/// Returns [`InferenceError::InvalidSafetensors`] for a corrupt header,
/// [`InferenceError::MissingTensor`] or [`InferenceError::ShapeMismatch`] for
/// an incompatible visual inventory, and an inference error when a tensor
/// cannot be materialized safely.
pub fn load_qwen35_vision_weights_from_safetensors(
    reader: &mut SafetensorsFile,
    checkpoint_path: &Path,
    vision_cfg: &VisionModelConfig,
) -> Result<Qwen35VisionWeights, InferenceError> {
    vision_cfg.validate()?;
    let mut never_cancel = || false;
    match load_from_safetensors_reader_with_cancel(
        reader,
        checkpoint_path,
        vision_cfg,
        &mut never_cancel,
    )? {
        Some(weights) => Ok(weights),
        None => Err(InferenceError::Inference(
            "non-cancellable vision weight load was cancelled".to_string(),
        )),
    }
}

pub(crate) fn load_qwen35_vision_weights_with_cancel(
    model_dir: &Path,
    vision_cfg: &VisionModelConfig,
    should_cancel: &mut dyn FnMut() -> bool,
) -> Result<Option<Qwen35VisionWeights>, InferenceError> {
    if should_cancel() {
        return Ok(None);
    }
    // Callers can construct `VisionModelConfig` directly (it's a public struct), so this
    // boundary re-validates rather than trusting that every caller went through
    // `Qwen35Config::from_config_json_str`'s parse-time check.
    vision_cfg.validate()?;
    if checkpoint_entry_present(&model_dir.join("quantize_index.json"))? {
        load_from_q4_dir_with_cancel(model_dir, vision_cfg, should_cancel)
    } else if checkpoint_entry_present(&model_dir.join("model.safetensors.index.json"))? {
        load_from_fp16_dir_with_cancel(model_dir, vision_cfg, should_cancel)
    } else {
        load_from_single_file_with_cancel(model_dir, vision_cfg, should_cancel)
    }
}

/// Validate the exact vision tensor inventory without materializing tensor data.
///
/// This is the serving capability preflight: every expected `model.visual.*`
/// name must occur exactly once and every referenced shard/file must be
/// openable before HTTP request normalization advertises vision support. For
/// an unindexed `model.safetensors`, the bounded safetensors header is parsed
/// and every expected tensor shape is checked without decoding tensor values.
pub fn validate_qwen35_vision_weight_inventory(
    model_dir: &Path,
    vision_cfg: &VisionModelConfig,
) -> Result<(), InferenceError> {
    vision_cfg.validate()?;
    let expected: HashSet<String> = tensor_names(vision_cfg).into_iter().collect();
    if checkpoint_entry_present(&model_dir.join("quantize_index.json"))? {
        let manifest = q4_manifest::load_manifest(model_dir)
            .map_err(|err| {
                InferenceError::InvalidSafetensors(format!(
                    "failed to read quantize_index.json in {}: {err}",
                    model_dir.display()
                ))
            })?
            .ok_or_else(|| {
                InferenceError::ModelNotFound(format!(
                    "quantize_index.json missing in {}",
                    model_dir.display()
                ))
            })?;
        let mut actual = HashSet::with_capacity(expected.len());
        for entry in manifest
            .tensors
            .iter()
            .filter(|entry| entry.name.starts_with("model.visual."))
        {
            if !actual.insert(entry.name.clone()) {
                return Err(InferenceError::Inference(format!(
                    "vision checkpoint manifest in {} contains duplicate tensor {}",
                    model_dir.display(),
                    entry.name
                )));
            }
            let _ = open_manifest_entry_once(model_dir, &entry.file)?;
        }
        if actual != expected {
            return Err(InferenceError::Inference(format!(
                "vision checkpoint inventory mismatch in {}: found {} unique model.visual.* \
                 tensor(s), expected {} for depth {}",
                model_dir.display(),
                actual.len(),
                expected.len(),
                vision_cfg.depth
            )));
        }
        return Ok(());
    }

    let index_path = model_dir.join("model.safetensors.index.json");
    if checkpoint_entry_present(&index_path)? {
        let reader = ShardedSafetensors::open_index(&index_path)?;
        let actual: HashSet<String> = reader
            .index()
            .weight_map
            .keys()
            .filter(|name| name.starts_with("model.visual."))
            .cloned()
            .collect();
        if actual != expected {
            return Err(InferenceError::Inference(format!(
                "vision checkpoint inventory mismatch in {}: found {} unique model.visual.* \
                 tensor(s), expected {} for depth {}",
                index_path.display(),
                actual.len(),
                expected.len(),
                vision_cfg.depth
            )));
        }
        let shards: HashSet<&str> = expected
            .iter()
            .filter_map(|name| reader.index().weight_map.get(name).map(String::as_str))
            .collect();
        for shard in shards {
            let _ = open_manifest_entry_once(model_dir, shard)?;
        }
        return Ok(());
    }

    let single_path = resolve_qwen35_single_decoder_safetensors(model_dir)?;
    let reader = SafetensorsFile::open(&single_path)?;
    validate_single_file_inventory(&reader, &single_path, vision_cfg)
}

/// **Unstable**: checkpoint-layout resolution may evolve before 1.0.
///
/// Resolve the one safetensors file required by the pooled vision-embedding
/// decoder loader. An existing `model.safetensors.index.json` is authoritative,
/// even when a convenience `model.safetensors` also exists; its `weight_map`
/// must name exactly one contained shard. Without an index, the directory must
/// contain exactly one `*.safetensors` candidate whose resolved metadata is a
/// file, and that candidate must be named `model.safetensors`. Symlinks are
/// followed consistently with the checkpoint mmap trust policy.
///
/// This is the shared file-set policy used by `lattice-embed`'s
/// `VisionEmbeddingModel`: decoder and visual tensors cannot independently
/// select the plain and indexed layouts when both sentinels are present. The
/// embed constructor opens the returned path once and loads both components
/// through that reader; this resolver itself performs structural discovery
/// and does not compute a checkpoint content digest.
///
/// # Errors
///
/// Returns [`InferenceError::ModelNotFound`] when no supported single-file
/// decoder checkpoint exists, or [`InferenceError::InvalidSafetensors`] when
/// an index escapes the model directory or an unindexed layout is ambiguous.
pub fn resolve_qwen35_single_decoder_safetensors(
    model_dir: &Path,
) -> Result<PathBuf, InferenceError> {
    Ok(resolve_qwen35_single_checkpoint_file_set(model_dir)?.path)
}

/// **Unstable**: checkpoint-layout opening may evolve before 1.0.
///
/// Resolve and open the one safetensors file required by the pooled
/// vision-embedding decoder loader. The returned [`SafetensorsFile`] is the
/// only open of the selected checkpoint needed to materialize both visual and
/// decoder tensors. For an indexed layout, the authoritative `weight_map`
/// must exactly match the opened shard's header inventory; a sparse, stale, or
/// otherwise contradictory index fails before any tensor payload is decoded.
///
/// # Errors
///
/// Returns the same layout errors as
/// [`resolve_qwen35_single_decoder_safetensors`], an invalid-safetensors error
/// when an indexed `weight_map` and shard header disagree, or an open/header
/// error from [`SafetensorsFile`].
pub fn open_qwen35_single_decoder_safetensors(
    model_dir: &Path,
) -> Result<(SafetensorsFile, PathBuf), InferenceError> {
    let resolved = resolve_qwen35_single_checkpoint_file_set(model_dir)?;
    let reader = SafetensorsFile::open(&resolved.path)?;
    if let Some(indexed_weight_map) = &resolved.indexed_weight_map {
        validate_indexed_single_shard_inventory(&reader, &resolved.path, indexed_weight_map)?;
    }
    Ok((reader, resolved.path))
}

struct ResolvedQwen35SingleCheckpoint {
    path: PathBuf,
    indexed_weight_map: Option<HashMap<String, String>>,
}

fn resolve_qwen35_single_checkpoint_file_set(
    model_dir: &Path,
) -> Result<ResolvedQwen35SingleCheckpoint, InferenceError> {
    let index_path = model_dir.join("model.safetensors.index.json");
    if checkpoint_entry_present(&index_path)? {
        let index = parse_index(model_dir)?;
        let mut shards: Vec<String> = index.weight_map.values().cloned().collect();
        shards.sort_unstable();
        shards.dedup();
        return match shards.as_slice() {
            [one] => Ok(ResolvedQwen35SingleCheckpoint {
                path: contained_shard_path(model_dir, one)?,
                indexed_weight_map: Some(index.weight_map),
            }),
            [] => Err(InferenceError::InvalidSafetensors(format!(
                "empty weight_map in {}",
                index_path.display()
            ))),
            _ => Err(InferenceError::InvalidSafetensors(format!(
                "checkpoint at {} is sharded across {} files; pooled vision embedding requires \
                 one decoder safetensors shard",
                model_dir.display(),
                shards.len()
            ))),
        };
    }

    Ok(ResolvedQwen35SingleCheckpoint {
        path: resolve_unindexed_safetensors(model_dir)?,
        indexed_weight_map: None,
    })
}

fn validate_indexed_single_shard_inventory(
    reader: &SafetensorsFile,
    checkpoint_path: &Path,
    indexed_weight_map: &HashMap<String, String>,
) -> Result<(), InferenceError> {
    let declared: HashSet<&str> = indexed_weight_map.keys().map(String::as_str).collect();
    let actual: HashSet<&str> = reader.tensor_names().into_iter().collect();
    if declared == actual {
        return Ok(());
    }

    let mut missing_from_shard: Vec<&str> = declared.difference(&actual).copied().collect();
    missing_from_shard.sort_unstable();
    let mut missing_from_index: Vec<&str> = actual.difference(&declared).copied().collect();
    missing_from_index.sort_unstable();
    Err(InferenceError::InvalidSafetensors(format!(
        "authoritative model.safetensors.index.json weight_map/header inventory mismatch for {}: \
         weight_map declares {} tensor(s), shard header contains {}; missing_from_shard={:?}, \
         missing_from_index={:?}",
        checkpoint_path.display(),
        declared.len(),
        actual.len(),
        missing_from_shard.into_iter().take(5).collect::<Vec<_>>(),
        missing_from_index.into_iter().take(5).collect::<Vec<_>>()
    )))
}

fn checkpoint_entry_present(path: &Path) -> Result<bool, InferenceError> {
    match std::fs::symlink_metadata(path) {
        Ok(_) => Ok(true),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(false),
        Err(err) => Err(InferenceError::InvalidSafetensors(format!(
            "failed to inspect checkpoint sentinel {}: {err}",
            path.display()
        ))),
    }
}

fn resolve_unindexed_safetensors(model_dir: &Path) -> Result<PathBuf, InferenceError> {
    let entries = std::fs::read_dir(model_dir).map_err(|err| {
        InferenceError::InvalidSafetensors(format!(
            "failed to inspect unindexed checkpoint directory {}: {err}",
            model_dir.display()
        ))
    })?;
    let mut candidates = Vec::new();
    for entry in entries {
        let entry = entry.map_err(|err| {
            InferenceError::InvalidSafetensors(format!(
                "failed to inspect an entry in unindexed checkpoint directory {}: {err}",
                model_dir.display()
            ))
        })?;
        let path = entry.path();
        if path.extension() != Some(std::ffi::OsStr::new("safetensors")) {
            continue;
        }
        let metadata = path.metadata().map_err(|err| {
            InferenceError::InvalidSafetensors(format!(
                "failed to inspect safetensors candidate {}: {err}",
                path.display()
            ))
        })?;
        if metadata.is_file() {
            candidates.push(path);
        }
    }
    candidates.sort();

    match candidates.as_slice() {
        [only] if only.file_name() == Some(std::ffi::OsStr::new("model.safetensors")) => {
            Ok(only.clone())
        }
        [] => Err(InferenceError::ModelNotFound(format!(
            "no model.safetensors.index.json or model.safetensors in {}",
            model_dir.display()
        ))),
        [only] => Err(InferenceError::ModelNotFound(format!(
            "unindexed checkpoint in {} contains {}, but the sole safetensors file must be named \
             model.safetensors",
            model_dir.display(),
            only.display()
        ))),
        _ => Err(InferenceError::InvalidSafetensors(format!(
            "ambiguous unindexed checkpoint in {}: found {} safetensors files; provide exactly \
             one model.safetensors or a model.safetensors.index.json",
            model_dir.display(),
            candidates.len()
        ))),
    }
}

fn validate_single_file_inventory(
    reader: &SafetensorsFile,
    checkpoint_path: &Path,
    vision_cfg: &VisionModelConfig,
) -> Result<(), InferenceError> {
    let expected_names = tensor_names(vision_cfg);
    let expected: HashSet<&str> = expected_names.iter().map(String::as_str).collect();
    let actual: HashSet<String> = reader
        .tensor_names()
        .into_iter()
        .filter(|name| name.starts_with("model.visual."))
        .map(str::to_string)
        .collect();
    if actual.len() != expected.len() || actual.iter().any(|name| !expected.contains(name.as_str()))
    {
        let mut missing: Vec<&str> = expected
            .iter()
            .copied()
            .filter(|name| !actual.contains(*name))
            .collect();
        missing.sort_unstable();
        let mut unexpected: Vec<&str> = actual
            .iter()
            .map(String::as_str)
            .filter(|name| !expected.contains(name))
            .collect();
        unexpected.sort_unstable();
        return Err(InferenceError::Inference(format!(
            "vision checkpoint inventory mismatch in {}: found {} unique model.visual.* \
             tensor(s), expected {} for depth {}; missing={:?}, unexpected={:?}",
            checkpoint_path.display(),
            actual.len(),
            expected.len(),
            vision_cfg.depth,
            missing.into_iter().take(5).collect::<Vec<_>>(),
            unexpected.into_iter().take(5).collect::<Vec<_>>()
        )));
    }

    for name in &expected_names {
        if let Some(expected_shape) = expected_visual_tensor_shape(name, vision_cfg) {
            let actual_shape = reader
                .tensor_shape(name)
                .ok_or_else(|| InferenceError::MissingTensor(name.clone()))?;
            if actual_shape != expected_shape {
                return Err(InferenceError::ShapeMismatch {
                    name: name.clone(),
                    expected: expected_shape,
                    actual: actual_shape.to_vec(),
                });
            }
        }
    }
    Ok(())
}

/// The 153 real tensor names for a `depth`-block checkpoint (used to drive the fp16
/// sharded-safetensors fetch; the q4 path instead filters the manifest by prefix).
fn tensor_names(vision_cfg: &VisionModelConfig) -> Vec<String> {
    let mut names = vec![
        "model.visual.patch_embed.proj.weight".to_string(),
        "model.visual.patch_embed.proj.bias".to_string(),
        "model.visual.pos_embed.weight".to_string(),
        "model.visual.merger.linear_fc1.weight".to_string(),
        "model.visual.merger.linear_fc1.bias".to_string(),
        "model.visual.merger.linear_fc2.weight".to_string(),
        "model.visual.merger.linear_fc2.bias".to_string(),
        "model.visual.merger.norm.weight".to_string(),
        "model.visual.merger.norm.bias".to_string(),
    ];
    for i in 0..vision_cfg.depth {
        for suffix in [
            "attn.qkv.weight",
            "attn.qkv.bias",
            "attn.proj.weight",
            "attn.proj.bias",
            "mlp.linear_fc1.weight",
            "mlp.linear_fc1.bias",
            "mlp.linear_fc2.weight",
            "mlp.linear_fc2.bias",
            "norm1.weight",
            "norm1.bias",
            "norm2.weight",
            "norm2.bias",
        ] {
            names.push(format!("model.visual.blocks.{i}.{suffix}"));
        }
    }
    names
}

/// The shape `assemble` will require for a given `model.visual.*` tensor name, mirroring
/// its per-tensor `take(name, expected_shape)` calls. `None` means the name is not one of
/// the expected tensors (unreachable given `tensor_names()`, but the caller falls back to
/// the post-materialization check in that case rather than assuming a shape).
fn expected_visual_tensor_shape(name: &str, vision_cfg: &VisionModelConfig) -> Option<Vec<usize>> {
    let hidden = vision_cfg.hidden_size;
    let qkv_out = 3 * hidden;
    let mlp_intermediate = vision_cfg.intermediate_size.unwrap_or(4 * hidden);
    let merge_in = vision_cfg.spatial_merge_size * vision_cfg.spatial_merge_size * hidden;
    let out_hidden = vision_cfg.out_hidden_size;

    if let Some(rest) = name.strip_prefix("model.visual.blocks.") {
        let suffix = rest.split_once('.').map(|(_, s)| s).unwrap_or(rest);
        return match suffix {
            "attn.qkv.weight" => Some(vec![qkv_out, hidden]),
            "attn.qkv.bias" => Some(vec![qkv_out]),
            "attn.proj.weight" => Some(vec![hidden, hidden]),
            "attn.proj.bias" => Some(vec![hidden]),
            "mlp.linear_fc1.weight" => Some(vec![mlp_intermediate, hidden]),
            "mlp.linear_fc1.bias" => Some(vec![mlp_intermediate]),
            "mlp.linear_fc2.weight" => Some(vec![hidden, mlp_intermediate]),
            "mlp.linear_fc2.bias" => Some(vec![hidden]),
            "norm1.weight" | "norm2.weight" | "norm1.bias" | "norm2.bias" => Some(vec![hidden]),
            _ => None,
        };
    }

    match name {
        "model.visual.patch_embed.proj.weight" => Some(vec![
            hidden,
            vision_cfg.in_channels,
            vision_cfg.temporal_patch_size,
            vision_cfg.patch_size,
            vision_cfg.patch_size,
        ]),
        "model.visual.patch_embed.proj.bias" => Some(vec![hidden]),
        "model.visual.pos_embed.weight" => Some(vec![vision_cfg.num_position_embeddings, hidden]),
        "model.visual.merger.linear_fc1.weight" => Some(vec![merge_in, merge_in]),
        "model.visual.merger.linear_fc1.bias" => Some(vec![merge_in]),
        "model.visual.merger.linear_fc2.weight" => Some(vec![out_hidden, merge_in]),
        "model.visual.merger.linear_fc2.bias" => Some(vec![out_hidden]),
        "model.visual.merger.norm.weight" | "model.visual.merger.norm.bias" => Some(vec![hidden]),
        _ => None,
    }
}

fn load_from_fp16_dir_with_cancel(
    model_dir: &Path,
    vision_cfg: &VisionModelConfig,
    should_cancel: &mut dyn FnMut() -> bool,
) -> Result<Option<Qwen35VisionWeights>, InferenceError> {
    if should_cancel() {
        return Ok(None);
    }
    let index_path = model_dir.join("model.safetensors.index.json");
    let mut reader = ShardedSafetensors::open_index(&index_path)?;
    if should_cancel() {
        return Ok(None);
    }

    let expected_names = tensor_names(vision_cfg);
    // Inventory-exactness: this path only ever fetches the names it asks for, so an
    // insufficient `depth` (or other geometry field) would silently ignore the rest of
    // the checkpoint's `model.visual.*` tensors rather than erroring. Compare the count
    // this vision_cfg expects against how many `model.visual.*` entries the index
    // actually has.
    let actual_visual_count = reader
        .index()
        .weight_map
        .keys()
        .filter(|name| name.starts_with("model.visual."))
        .count();
    if actual_visual_count != expected_names.len() {
        return Err(InferenceError::Inference(format!(
            "vision checkpoint inventory mismatch in {}: found {actual_visual_count} \
             model.visual.* tensor(s) but vision_config (depth={}) expects exactly {}",
            index_path.display(),
            vision_cfg.depth,
            expected_names.len(),
        )));
    }

    let Some(tensors) =
        fetch_expected_tensors_with_cancel(&mut reader, expected_names, vision_cfg, should_cancel)?
    else {
        return Ok(None);
    };
    if should_cancel() {
        return Ok(None);
    }
    assemble(tensors, vision_cfg).map(Some)
}

fn load_from_single_file_with_cancel(
    model_dir: &Path,
    vision_cfg: &VisionModelConfig,
    should_cancel: &mut dyn FnMut() -> bool,
) -> Result<Option<Qwen35VisionWeights>, InferenceError> {
    if should_cancel() {
        return Ok(None);
    }
    let checkpoint_path = resolve_qwen35_single_decoder_safetensors(model_dir)?;
    let mut reader = SafetensorsFile::open(&checkpoint_path)?;
    load_from_safetensors_reader_with_cancel(
        &mut reader,
        &checkpoint_path,
        vision_cfg,
        should_cancel,
    )
}

fn load_from_safetensors_reader_with_cancel(
    reader: &mut SafetensorsFile,
    checkpoint_path: &Path,
    vision_cfg: &VisionModelConfig,
    should_cancel: &mut dyn FnMut() -> bool,
) -> Result<Option<Qwen35VisionWeights>, InferenceError> {
    validate_single_file_inventory(reader, checkpoint_path, vision_cfg)?;
    if should_cancel() {
        return Ok(None);
    }
    let expected_names = tensor_names(vision_cfg);
    let Some(tensors) =
        fetch_expected_tensors_with_cancel(reader, expected_names, vision_cfg, should_cancel)?
    else {
        return Ok(None);
    };
    if should_cancel() {
        return Ok(None);
    }
    assemble(tensors, vision_cfg).map(Some)
}

/// Fetch every name in `names` from `source`, checking each one's header-declared shape
/// against `expected_visual_tensor_shape` before materializing it -- mirroring the
/// Qwen3.5 text-decoder loader's `load_owned_tensor_checked`. A shape mismatch is
/// rejected here instead of after a full owned read and allocation. Generic over
/// `TensorSource` (rather than inlined into `load_from_fp16_dir`) so tests can exercise
/// the preflight-before-materialize ordering with a mock source.
#[cfg(test)]
fn fetch_expected_tensors<T: TensorSource + ?Sized>(
    source: &mut T,
    names: Vec<String>,
    vision_cfg: &VisionModelConfig,
) -> Result<NamedTensors, InferenceError> {
    let mut never_cancel = || false;
    match fetch_expected_tensors_with_cancel(source, names, vision_cfg, &mut never_cancel)? {
        Some(tensors) => Ok(tensors),
        None => Err(InferenceError::Inference(
            "non-cancellable vision tensor fetch was cancelled".to_string(),
        )),
    }
}

fn fetch_expected_tensors_with_cancel<T: TensorSource + ?Sized>(
    source: &mut T,
    names: Vec<String>,
    vision_cfg: &VisionModelConfig,
    should_cancel: &mut dyn FnMut() -> bool,
) -> Result<Option<NamedTensors>, InferenceError> {
    if should_cancel() {
        return Ok(None);
    }
    let mut tensors = HashMap::with_capacity(names.len());
    for name in names {
        if should_cancel() {
            return Ok(None);
        }
        if let Some(expected) = expected_visual_tensor_shape(&name, vision_cfg)
            && let Some(declared) = source.tensor_shape(&name)?
            && declared != expected
        {
            return Err(InferenceError::ShapeMismatch {
                name,
                expected,
                actual: declared,
            });
        }
        // `get_f32_tensor` (reached via `get_f32_tensor_owned`) fully decodes and copies
        // the tensor -- including FP16->F32 expansion -- before `assemble`'s `take` ever
        // sees it. Not every expected name has a config-derived shape to compare above,
        // so budget the *declared* header shape (a cheap lookup, no decode) against
        // `MAX_VISION_TENSOR_BYTES` as well, mirroring the budget-before-materialize
        // pattern used on the q4 side, so an oversized declared tensor is rejected before
        // its expansion rather than after.
        if let Some(declared_shape) = source.tensor_shape(&name)? {
            let declared_elems: u128 = declared_shape.iter().map(|&d| d as u128).product();
            let declared_bytes = declared_elems * 4;
            if declared_bytes > crate::model::qwen35_config::MAX_VISION_TENSOR_BYTES {
                return Err(InferenceError::Inference(format!(
                    "vision checkpoint tensor {name}: declared size ({declared_bytes} bytes) \
                     exceeds MAX_VISION_TENSOR_BYTES ({}) -- rejected before decoding",
                    crate::model::qwen35_config::MAX_VISION_TENSOR_BYTES
                )));
            }
        }
        let (data, shape) = source.get_f32_tensor_owned(&name)?;
        tensors.insert(name, (data, shape));
    }
    if should_cancel() {
        return Ok(None);
    }
    Ok(Some(tensors))
}

fn load_from_q4_dir_with_cancel(
    model_dir: &Path,
    vision_cfg: &VisionModelConfig,
    should_cancel: &mut dyn FnMut() -> bool,
) -> Result<Option<Qwen35VisionWeights>, InferenceError> {
    if should_cancel() {
        return Ok(None);
    }
    let manifest = q4_manifest::load_manifest(model_dir)
        .map_err(|e| {
            InferenceError::InvalidSafetensors(format!(
                "failed to read quantize_index.json in {}: {e}",
                model_dir.display()
            ))
        })?
        .ok_or_else(|| {
            InferenceError::ModelNotFound(format!(
                "quantize_index.json missing in {}",
                model_dir.display()
            ))
        })?;
    if should_cancel() {
        return Ok(None);
    }

    // FIX 4: `expected_names` is the exact, bounded (<= 12 * MAX_VISION_DEPTH + 9) set of
    // `model.visual.*` tensors `vision_cfg` implies. Previously every visual-prefixed
    // manifest entry was dequantized/decoded up front, and only *afterward* did
    // `assemble`'s leftover check reject names it didn't expect -- so a manifest with
    // many extra `model.visual.*` entries drove unbounded aggregate dequant memory
    // despite each individual tensor's own `MAX_VISION_TENSOR_BYTES` cap. Reject an
    // unexpected name before touching its file at all, and track a running aggregate
    // budget across the (now-bounded) set of tensors this loop can ever process.
    let expected_names: std::collections::HashSet<String> =
        tensor_names(vision_cfg).into_iter().collect();
    let aggregate_budget_bytes: u128 =
        expected_names.len() as u128 * crate::model::qwen35_config::MAX_VISION_TENSOR_BYTES;
    let mut aggregate_bytes: u128 = 0;

    let mut tensors = HashMap::new();
    for entry in manifest
        .tensors
        .iter()
        .filter(|e| e.name.starts_with("model.visual."))
    {
        if should_cancel() {
            return Ok(None);
        }
        if !expected_names.contains(&entry.name) {
            return Err(InferenceError::Inference(format!(
                "vision checkpoint manifest in {}: unexpected tensor {} not accounted for \
                 by vision_config (depth={}) -- rejected before dequantizing/decoding",
                model_dir.display(),
                entry.name,
                vision_cfg.depth,
            )));
        }
        // `HashMap::insert` below would silently let a later duplicate-named entry
        // overwrite an earlier one, making `tensors.is_empty()` (the inventory-exactness
        // check in `assemble`) blind to a manifest that names the same tensor twice.
        // Reject the duplicate here, before reading its file, so a corrupted manifest
        // fails deterministically instead of depending on entry order.
        if tensors.contains_key(&entry.name) {
            return Err(InferenceError::Inference(format!(
                "vision checkpoint manifest in {}: duplicate entry for tensor {} -- \
                 each model.visual.* tensor name must appear exactly once",
                model_dir.display(),
                entry.name,
            )));
        }
        // Config-shape preflight, mirroring what `fetch_expected_tensors` does for the
        // fp16 path. The manifest's declared shape is compared against what `vision_cfg`
        // implies BEFORE the tensor's file is opened, so a checkpoint that disagrees with
        // the config is rejected without reading or allocating it. This sits ahead of the
        // branch below because BOTH arms materialize: the q4 arm through
        // `dequantize_q4_to_f32` and the f16 arm inside the `.f16` loader.
        if let Some(expected) = expected_visual_tensor_shape(&entry.name, vision_cfg)
            && let Some(declared) = &entry.shape
            && declared != &expected
        {
            return Err(InferenceError::ShapeMismatch {
                name: entry.name.clone(),
                expected,
                actual: declared.clone(),
            });
        }
        // PATH CONTAINMENT -- `entry.file` comes from `quantize_index.json`, part of the
        // untrusted checkpoint directory, so it is validated before the join. The file is
        // opened exactly once and read from that fd rather than reopened by path.
        let (file, real_path) = open_manifest_entry_once(model_dir, &entry.file)?;
        let (data, shape) = if entry.quantized.unwrap_or(false) {
            let q4 = load_q4_from_open_file(file, &real_path, None).map_err(|e| {
                InferenceError::InvalidSafetensors(format!(
                    "failed to load q4 tensor {} from {}: {e}",
                    entry.name,
                    real_path.display()
                ))
            })?;
            // The manifest's own recorded shape (when present) must agree with the
            // shape carried in the tensor's `.q4` header; a mismatch means the
            // manifest and the on-disk tensor have drifted apart.
            if let Some(manifest_shape) = &entry.shape
                && manifest_shape != &q4.shape
            {
                return Err(InferenceError::ShapeMismatch {
                    name: entry.name.clone(),
                    expected: q4.shape.clone(),
                    actual: manifest_shape.clone(),
                });
            }
            // `entry.shape` is optional, so when the manifest omits it the `.q4` header
            // is the only declared shape that exists and the preflight above had nothing
            // to check. Compare the header against the config here, while the tensor is
            // still its compressed self and before `dequantize_q4_to_f32` allocates the
            // full f32 buffer.
            if let Some(expected) = expected_visual_tensor_shape(&entry.name, vision_cfg)
                && q4.shape != expected
            {
                return Err(InferenceError::ShapeMismatch {
                    name: entry.name.clone(),
                    expected,
                    actual: q4.shape.clone(),
                });
            }
            // ORDERING: `dequantize_q4_to_f32` expands the packed q4
            // buffer into a full f32 `Vec` sized by `q4.shape`'s product -- a value read
            // from the on-disk tensor header, independent of `vision_cfg` and not yet
            // checked against any expected shape at this point. Budget that product
            // BEFORE dequantizing (materializing) rather than after, so a hostile
            // declared shape is rejected before the allocation, not once `assemble`'s
            // per-tensor shape check runs on the already-materialized buffer.
            let q4_elems: u128 = q4.shape.iter().map(|&d| d as u128).product();
            let q4_bytes = q4_elems * 4;
            if q4_bytes > crate::model::qwen35_config::MAX_VISION_TENSOR_BYTES {
                return Err(InferenceError::Inference(format!(
                    "vision checkpoint tensor {} in {}: dequantized size ({q4_bytes} bytes) \
                     exceeds MAX_VISION_TENSOR_BYTES ({}) -- rejected before dequantizing",
                    entry.name,
                    real_path.display(),
                    crate::model::qwen35_config::MAX_VISION_TENSOR_BYTES
                )));
            }
            aggregate_bytes += q4_bytes;
            if aggregate_bytes > aggregate_budget_bytes {
                return Err(InferenceError::Inference(format!(
                    "vision checkpoint manifest in {}: aggregate dequantized size \
                     ({aggregate_bytes} bytes) exceeds the aggregate budget \
                     ({aggregate_budget_bytes} bytes) for {} expected tensor(s)",
                    model_dir.display(),
                    expected_names.len(),
                )));
            }
            let shape = q4.shape.clone();
            (dequantize_q4_to_f32(&q4), shape)
        } else if let Some(expected) = expected_visual_tensor_shape(&entry.name, vision_cfg) {
            // Same reasoning as the `.q4` header check above, for the arm that reads an
            // `.f16` companion: when the manifest omits `shape`, the preflight before the
            // open had nothing to compare, and this arm would otherwise materialize the
            // whole tensor before `assemble` noticed the disagreement.
            //
            // The check and the payload read happen inside the ONE handle opened and
            // identity-verified above. Checking a shape through one open and
            // materializing through a second leaves nothing binding the validated header
            // to the bytes actually read, since the pathname can be replaced in between,
            // and the checkpoint directory is untrusted input.
            let display_path = real_path.display().to_string();
            load_f16_tensor_from_open_file_expecting(file, &display_path, &expected).map_err(
                |e| match e {
                    F16LoadError::ShapeMismatch { declared } => InferenceError::ShapeMismatch {
                        name: entry.name.clone(),
                        expected,
                        actual: declared,
                    },
                    F16LoadError::Other(e) => InferenceError::InvalidSafetensors(format!(
                        "failed to load f16 tensor {} from {display_path}: {e}",
                        entry.name,
                    )),
                },
            )?
        } else {
            load_f16_tensor_from_open_file(file, &real_path.display().to_string(), None).map_err(
                |e| {
                    InferenceError::InvalidSafetensors(format!(
                        "failed to load f16 tensor {} from {}: {e}",
                        entry.name,
                        real_path.display()
                    ))
                },
            )?
        };
        tensors.insert(entry.name.clone(), (data, shape));
    }
    if should_cancel() {
        return Ok(None);
    }
    assemble(tensors, vision_cfg).map(Some)
}

/// Pull the 153 expected tensors out of a name→(data, shape) map, validating each one's
/// full shape (not just its element count) against the shape derived from `vision_cfg`,
/// and assemble them into [`Qwen35VisionWeights`]. Shared by both the fp16 and q4 load
/// paths so shape validation only lives in one place.
fn assemble(
    mut tensors: HashMap<String, (Vec<f32>, Vec<usize>)>,
    vision_cfg: &VisionModelConfig,
) -> Result<Qwen35VisionWeights, InferenceError> {
    let hidden = vision_cfg.hidden_size;
    let qkv_out = 3 * hidden;
    // FIX 16: use the checkpoint's own `intermediate_size` when `config.json` carries one
    // (official Qwen3.5-VL: hidden_size=1152, intermediate_size=4304, NOT 4*1152=4608);
    // fall back to `4 * hidden_size` only when the field is absent, matching HF default
    // semantics for architectures that omit an explicit vision MLP width.
    let mlp_intermediate = vision_cfg.intermediate_size.unwrap_or(4 * hidden);
    let merge_in = vision_cfg.spatial_merge_size * vision_cfg.spatial_merge_size * hidden;
    let out_hidden = vision_cfg.out_hidden_size;

    let mut take = |name: String, expected_shape: Vec<usize>| -> Result<Vec<f32>, InferenceError> {
        let (v, actual_shape) = tensors
            .remove(&name)
            .ok_or_else(|| InferenceError::MissingTensor(name.clone()))?;
        // A same-numel transposition (e.g. FC2 stored as [in, out] instead of
        // [out, in]) has an identical element count but a different shape vector —
        // comparing the full shape (not just `v.len()`) is what rejects it.
        if actual_shape != expected_shape {
            return Err(InferenceError::ShapeMismatch {
                name,
                expected: expected_shape,
                actual: actual_shape,
            });
        }
        Ok(v)
    };

    let patch_embed_weight_shape = vec![
        hidden,
        vision_cfg.in_channels,
        vision_cfg.temporal_patch_size,
        vision_cfg.patch_size,
        vision_cfg.patch_size,
    ];
    let patch_embed_weight = take(
        "model.visual.patch_embed.proj.weight".to_string(),
        patch_embed_weight_shape.clone(),
    )?;
    let patch_embed_bias = take(
        "model.visual.patch_embed.proj.bias".to_string(),
        vec![hidden],
    )?;
    let pos_embed = take(
        "model.visual.pos_embed.weight".to_string(),
        vec![vision_cfg.num_position_embeddings, hidden],
    )?;

    let mut blocks = Vec::with_capacity(vision_cfg.depth);
    for i in 0..vision_cfg.depth {
        let name = |suffix: &str| format!("model.visual.blocks.{i}.{suffix}");
        blocks.push(VisualBlockWeights {
            qkv_weight: take(name("attn.qkv.weight"), vec![qkv_out, hidden])?,
            qkv_bias: take(name("attn.qkv.bias"), vec![qkv_out])?,
            proj_weight: take(name("attn.proj.weight"), vec![hidden, hidden])?,
            proj_bias: take(name("attn.proj.bias"), vec![hidden])?,
            fc1_weight: take(
                name("mlp.linear_fc1.weight"),
                vec![mlp_intermediate, hidden],
            )?,
            fc1_bias: take(name("mlp.linear_fc1.bias"), vec![mlp_intermediate])?,
            fc2_weight: take(
                name("mlp.linear_fc2.weight"),
                vec![hidden, mlp_intermediate],
            )?,
            fc2_bias: take(name("mlp.linear_fc2.bias"), vec![hidden])?,
            norm1_weight: take(name("norm1.weight"), vec![hidden])?,
            norm1_bias: take(name("norm1.bias"), vec![hidden])?,
            norm2_weight: take(name("norm2.weight"), vec![hidden])?,
            norm2_bias: take(name("norm2.bias"), vec![hidden])?,
        });
    }

    let merger = VisualMergerWeights {
        fc1_weight: take(
            "model.visual.merger.linear_fc1.weight".to_string(),
            vec![merge_in, merge_in],
        )?,
        fc1_bias: take(
            "model.visual.merger.linear_fc1.bias".to_string(),
            vec![merge_in],
        )?,
        fc2_weight: take(
            "model.visual.merger.linear_fc2.weight".to_string(),
            vec![out_hidden, merge_in],
        )?,
        fc2_bias: take(
            "model.visual.merger.linear_fc2.bias".to_string(),
            vec![out_hidden],
        )?,
        norm_weight: take("model.visual.merger.norm.weight".to_string(), vec![hidden])?,
        norm_bias: take("model.visual.merger.norm.bias".to_string(), vec![hidden])?,
    };

    // Inventory-exactness: every tensor fetched must be accounted for by the expected
    // set above. The q4 path loads every `model.visual.*` entry in the manifest up
    // front (not just the names `vision_cfg` implies), so a checkpoint that carries
    // more real block tensors than `vision_cfg.depth` implies (e.g. a full 153-entry
    // checkpoint paired with `depth: 0`) would otherwise silently return a truncated
    // `Qwen35VisionWeights` instead of erroring.
    if !tensors.is_empty() {
        let mut leftover: Vec<&String> = tensors.keys().collect();
        leftover.sort();
        return Err(InferenceError::Inference(format!(
            "vision checkpoint has {} unconsumed model.visual.* tensor(s) not accounted for \
             by vision_config (depth={}): {:?}",
            tensors.len(),
            vision_cfg.depth,
            leftover.into_iter().take(5).collect::<Vec<_>>(),
        )));
    }

    Ok(Qwen35VisionWeights {
        patch_embed_weight,
        patch_embed_weight_shape,
        patch_embed_bias,
        pos_embed,
        blocks,
        merger,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::qwen35_config::Qwen35Config;

    fn real_vision_cfg() -> VisionModelConfig {
        VisionModelConfig {
            depth: 12,
            hidden_size: 768,
            num_heads: 12,
            patch_size: 16,
            spatial_merge_size: 2,
            out_hidden_size: 1024,
            temporal_patch_size: 2,
            num_position_embeddings: 2304,
            in_channels: 3,
            deepstack_visual_indexes: vec![],
            intermediate_size: None,
        }
    }

    #[test]
    fn missing_manifest_is_a_descriptive_error_not_a_panic() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = real_vision_cfg();
        let err = load_qwen35_vision_weights(tmp.path(), &cfg)
            .expect_err("a directory with neither manifest must be a hard error");
        let msg = err.to_string();
        assert!(
            msg.contains("model.safetensors.index.json") || msg.contains("quantize_index.json"),
            "error must name the missing manifests: {msg}"
        );
    }

    #[test]
    fn tensor_names_count_matches_153_for_depth_12() {
        let cfg = real_vision_cfg();
        // 2 (patch_embed) + 1 (pos_embed) + 12 * 12 (blocks) + 6 (merger) = 153.
        assert_eq!(tensor_names(&cfg).len(), 153);
    }

    #[test]
    fn depth_zero_vision_config_rejected_at_loader_boundary() {
        // `load_qwen35_vision_weights` is public, so a caller can hand it a directly
        // constructed `VisionModelConfig` that never went through
        // `Qwen35Config::from_config_json_str`'s parse-time validation. depth: 0 must
        // still fail closed here, before the (nonexistent) directory is even touched.
        let tmp = tempfile::tempdir().unwrap();
        let mut cfg = real_vision_cfg();
        cfg.depth = 0;
        let err = load_qwen35_vision_weights(tmp.path(), &cfg)
            .expect_err("depth: 0 must be rejected at the public loader boundary");
        assert!(
            err.to_string().contains("depth"),
            "error must name depth: {err}"
        );
    }

    #[test]
    fn num_heads_zero_vision_config_rejected_at_loader_boundary() {
        let tmp = tempfile::tempdir().unwrap();
        let mut cfg = real_vision_cfg();
        cfg.num_heads = 0;
        let err = load_qwen35_vision_weights(tmp.path(), &cfg)
            .expect_err("num_heads: 0 must be rejected at the public loader boundary");
        assert!(
            err.to_string().contains("num_heads"),
            "error must name num_heads: {err}"
        );
    }

    #[test]
    fn q4_full_inventory_with_depth_zero_is_rejected() {
        // A checkpoint that genuinely carries the full 153-tensor real inventory, paired
        // with a (malformed) vision_config claiming depth: 0, must error rather than
        // silently returning a nine-tensor `Qwen35VisionWeights`.
        let tmp = tempfile::tempdir().unwrap();
        let full_cfg = real_vision_cfg();
        let entries: Vec<String> = tensor_names(&full_cfg)
            .into_iter()
            .map(|name| format!(r#"{{"name":"{name}","file":"missing.f16","quantized":false}}"#))
            .collect();
        std::fs::write(
            tmp.path().join("quantize_index.json"),
            format!("[{}]", entries.join(",")),
        )
        .expect("test setup: write manifest");

        let mut depth_zero_cfg = full_cfg;
        depth_zero_cfg.depth = 0;
        let err = load_qwen35_vision_weights(tmp.path(), &depth_zero_cfg)
            .expect_err("depth: 0 with a full 153-entry inventory present must still be rejected");
        assert!(
            err.to_string().contains("depth"),
            "error must name depth: {err}"
        );
    }

    fn tiny_vision_cfg() -> VisionModelConfig {
        VisionModelConfig {
            depth: 1,
            hidden_size: 4,
            num_heads: 2,
            patch_size: 2,
            spatial_merge_size: 1,
            out_hidden_size: 4,
            temporal_patch_size: 1,
            num_position_embeddings: 4,
            in_channels: 3,
            deepstack_visual_indexes: vec![],
            intermediate_size: None,
        }
    }

    /// The 21 expected (name, shape) pairs for [`tiny_vision_cfg`], computed
    /// independently of `assemble`'s derivation so a fixture bug can't cancel out a
    /// production bug.
    fn tiny_expected_shapes() -> Vec<(String, Vec<usize>)> {
        let hidden = 4;
        let qkv_out = 3 * hidden;
        let mlp_intermediate = 4 * hidden;
        let merge_in = hidden; // spatial_merge_size^2 (1) * hidden
        let out_hidden = 4;
        let mut v = vec![
            (
                // [hidden, in_channels, temporal_patch_size, patch_size, patch_size]
                "model.visual.patch_embed.proj.weight".to_string(),
                vec![hidden, 3, 1, 2, 2],
            ),
            (
                "model.visual.patch_embed.proj.bias".to_string(),
                vec![hidden],
            ),
            ("model.visual.pos_embed.weight".to_string(), vec![4, hidden]),
            (
                "model.visual.merger.linear_fc1.weight".to_string(),
                vec![merge_in, merge_in],
            ),
            (
                "model.visual.merger.linear_fc1.bias".to_string(),
                vec![merge_in],
            ),
            (
                "model.visual.merger.linear_fc2.weight".to_string(),
                vec![out_hidden, merge_in],
            ),
            (
                "model.visual.merger.linear_fc2.bias".to_string(),
                vec![out_hidden],
            ),
            ("model.visual.merger.norm.weight".to_string(), vec![hidden]),
            ("model.visual.merger.norm.bias".to_string(), vec![hidden]),
        ];
        for (suffix, shape) in [
            ("attn.qkv.weight", vec![qkv_out, hidden]),
            ("attn.qkv.bias", vec![qkv_out]),
            ("attn.proj.weight", vec![hidden, hidden]),
            ("attn.proj.bias", vec![hidden]),
            ("mlp.linear_fc1.weight", vec![mlp_intermediate, hidden]),
            ("mlp.linear_fc1.bias", vec![mlp_intermediate]),
            ("mlp.linear_fc2.weight", vec![hidden, mlp_intermediate]),
            ("mlp.linear_fc2.bias", vec![hidden]),
            ("norm1.weight", vec![hidden]),
            ("norm1.bias", vec![hidden]),
            ("norm2.weight", vec![hidden]),
            ("norm2.bias", vec![hidden]),
        ] {
            v.push((format!("model.visual.blocks.0.{suffix}"), shape));
        }
        v
    }

    /// A [`TensorSource`] that records every name passed to `get_f32_tensor_owned`, so a
    /// test can assert a specific tensor's shape mismatch is caught from the
    /// header-declared shape without that tensor's data ever being copied (other,
    /// correctly-shaped tensors ahead of it in iteration order are still fetched
    /// normally).
    struct CountingSource {
        tensors: HashMap<String, (Vec<f32>, Vec<usize>)>,
        materialized: std::cell::RefCell<std::collections::HashSet<String>>,
    }

    impl TensorSource for CountingSource {
        fn has_tensor(&mut self, name: &str) -> Result<bool, InferenceError> {
            Ok(self.tensors.contains_key(name))
        }
        fn tensor_shape(&mut self, name: &str) -> Result<Option<Vec<usize>>, InferenceError> {
            Ok(self.tensors.get(name).map(|(_, s)| s.clone()))
        }
        fn get_f32_tensor_owned(
            &mut self,
            name: &str,
        ) -> Result<(Vec<f32>, Vec<usize>), InferenceError> {
            self.materialized.borrow_mut().insert(name.to_string());
            self.tensors
                .get(name)
                .map(|(d, s)| (d.clone(), s.clone()))
                .ok_or_else(|| InferenceError::MissingTensor(name.to_string()))
        }
    }

    #[test]
    fn cancellable_loader_stops_before_touching_the_model_directory() {
        let result = load_qwen35_vision_weights_with_cancel(
            Path::new("/path/that/must/not/be/read"),
            &tiny_vision_cfg(),
            &mut || true,
        )
        .expect("cancellation is not a loader failure");
        assert!(result.is_none());
    }

    #[test]
    fn tensor_fetch_polls_cancellation_between_materializations() {
        let cfg = tiny_vision_cfg();
        let names = tensor_names(&cfg);
        let first_name = names[0].clone();
        let tensors: HashMap<String, (Vec<f32>, Vec<usize>)> = tiny_expected_shapes()
            .into_iter()
            .map(|(name, shape)| {
                let numel: usize = shape.iter().product();
                (name, (vec![0.5f32; numel], shape))
            })
            .collect();
        let mut source = CountingSource {
            tensors,
            materialized: std::cell::RefCell::new(std::collections::HashSet::new()),
        };
        let mut polls = 0;
        let result = fetch_expected_tensors_with_cancel(&mut source, names, &cfg, &mut || {
            polls += 1;
            polls == 3
        })
        .expect("cancellation is not a tensor-fetch failure");
        assert!(result.is_none());
        assert_eq!(polls, 3);
        assert_eq!(source.materialized.borrow().len(), 1);
        assert!(source.materialized.borrow().contains(&first_name));
    }

    #[test]
    fn tensor_fetch_accepts_configured_non_four_x_mlp_width() {
        let mut cfg = tiny_vision_cfg();
        cfg.hidden_size = 1152;
        cfg.intermediate_size = Some(4304);
        let expected = [
            (
                "model.visual.blocks.0.mlp.linear_fc1.weight",
                vec![4304, 1152],
            ),
            ("model.visual.blocks.0.mlp.linear_fc1.bias", vec![4304]),
            (
                "model.visual.blocks.0.mlp.linear_fc2.weight",
                vec![1152, 4304],
            ),
            ("model.visual.blocks.0.mlp.linear_fc2.bias", vec![1152]),
        ];
        let names = expected
            .iter()
            .map(|(name, _)| (*name).to_string())
            .collect::<Vec<_>>();
        let tensors = expected
            .iter()
            .map(|(name, shape)| ((*name).to_string(), (Vec::new(), shape.clone())))
            .collect();
        let mut source = CountingSource {
            tensors,
            materialized: std::cell::RefCell::new(std::collections::HashSet::new()),
        };

        let fetched = fetch_expected_tensors(&mut source, names.clone(), &cfg)
            .expect("configured 1152/4304 MLP shapes must pass loader preflight");
        assert_eq!(fetched.len(), names.len());
        for name in names {
            assert!(
                source.materialized.borrow().contains(&name),
                "{name} must reach materialization after passing preflight"
            );
        }
    }

    /// An undersized (but declared-shape-visible) block tensor must be rejected from
    /// its header-declared shape, before `get_f32_tensor_owned` ever copies its data.
    /// `assemble`'s own post-materialization check would also catch this shape
    /// mismatch, so `expect_err` alone would not be mutation-sensitive to the
    /// preflight; the "mutated name was never materialized" assertion is what pins the
    /// preflight-before-materialize ordering specifically.
    #[test]
    fn fetch_expected_tensors_rejects_undersized_tensor_before_materialization() {
        let cfg = tiny_vision_cfg();
        let names = tensor_names(&cfg);
        let mutated_name = "model.visual.blocks.0.mlp.linear_fc1.weight".to_string();
        let hidden = cfg.hidden_size;

        let mut tensors: HashMap<String, (Vec<f32>, Vec<usize>)> = tiny_expected_shapes()
            .into_iter()
            .map(|(name, shape)| {
                let numel: usize = shape.iter().product();
                (name, (vec![0.5f32; numel], shape))
            })
            .collect();
        // Declared shape undersized relative to [4*hidden, hidden], data consistent
        // with the (wrong) declared shape.
        tensors.insert(
            mutated_name.clone(),
            (
                vec![0.5f32; (4 * hidden - 1) * hidden],
                vec![4 * hidden - 1, hidden],
            ),
        );
        let mut source = CountingSource {
            tensors,
            materialized: std::cell::RefCell::new(std::collections::HashSet::new()),
        };

        let err = fetch_expected_tensors(&mut source, names, &cfg)
            .expect_err("undersized fc1 weight must be rejected");
        match err {
            InferenceError::ShapeMismatch { name, .. } => {
                assert_eq!(name, mutated_name, "error must name the mutated tensor");
            }
            other => panic!("expected ShapeMismatch, got {other:?}"),
        }
        assert!(
            !source.materialized.borrow().contains(&mutated_name),
            "the mismatched tensor's data must never be copied"
        );
    }

    /// Corrupt the FC2 weight entry in `shapes` to a same-numel transposition
    /// (`[hidden, mlp_intermediate]` -> `[mlp_intermediate, hidden]`; both have 64
    /// elements for [`tiny_vision_cfg`]).
    fn transpose_fc2_weight(shapes: &mut [(String, Vec<usize>)]) {
        for (name, shape) in shapes.iter_mut() {
            if name == "model.visual.blocks.0.mlp.linear_fc2.weight" {
                assert_eq!(
                    *shape,
                    vec![4, 16],
                    "fixture assumption for tiny_vision_cfg"
                );
                *shape = vec![16, 4];
                return;
            }
        }
        panic!("fc2 weight entry not found in fixture");
    }

    fn write_multi_f32_tensor_shard(path: &Path, tensors: &[(String, Vec<usize>, Vec<f32>)]) {
        let mut header_parts = Vec::new();
        let mut data: Vec<u8> = Vec::new();
        for (name, shape, values) in tensors {
            let start = data.len();
            for v in values {
                data.extend_from_slice(&v.to_le_bytes());
            }
            let end = data.len();
            let shape_str = shape
                .iter()
                .map(usize::to_string)
                .collect::<Vec<_>>()
                .join(",");
            header_parts.push(format!(
                r#""{name}":{{"dtype":"F32","shape":[{shape_str}],"data_offsets":[{start},{end}]}}"#
            ));
        }
        let header = format!("{{{}}}", header_parts.join(","));
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&(header.len() as u64).to_le_bytes());
        bytes.extend_from_slice(header.as_bytes());
        bytes.extend_from_slice(&data);
        std::fs::write(path, &bytes).expect("test setup: write shard");
    }

    fn tiny_f32_tensors(value: f32) -> Vec<(String, Vec<usize>, Vec<f32>)> {
        tiny_expected_shapes()
            .into_iter()
            .map(|(name, shape)| {
                let numel: usize = shape.iter().product();
                (name, shape, vec![value; numel])
            })
            .collect()
    }

    fn assert_vision_weights_eq(actual: &Qwen35VisionWeights, expected: &Qwen35VisionWeights) {
        assert_eq!(actual.patch_embed_weight, expected.patch_embed_weight);
        assert_eq!(
            actual.patch_embed_weight_shape,
            expected.patch_embed_weight_shape
        );
        assert_eq!(actual.patch_embed_bias, expected.patch_embed_bias);
        assert_eq!(actual.pos_embed, expected.pos_embed);
        assert_eq!(actual.blocks.len(), expected.blocks.len());
        for (actual, expected) in actual.blocks.iter().zip(&expected.blocks) {
            assert_eq!(actual.qkv_weight, expected.qkv_weight);
            assert_eq!(actual.qkv_bias, expected.qkv_bias);
            assert_eq!(actual.proj_weight, expected.proj_weight);
            assert_eq!(actual.proj_bias, expected.proj_bias);
            assert_eq!(actual.fc1_weight, expected.fc1_weight);
            assert_eq!(actual.fc1_bias, expected.fc1_bias);
            assert_eq!(actual.fc2_weight, expected.fc2_weight);
            assert_eq!(actual.fc2_bias, expected.fc2_bias);
            assert_eq!(actual.norm1_weight, expected.norm1_weight);
            assert_eq!(actual.norm1_bias, expected.norm1_bias);
            assert_eq!(actual.norm2_weight, expected.norm2_weight);
            assert_eq!(actual.norm2_bias, expected.norm2_bias);
        }
        assert_eq!(actual.merger.fc1_weight, expected.merger.fc1_weight);
        assert_eq!(actual.merger.fc1_bias, expected.merger.fc1_bias);
        assert_eq!(actual.merger.fc2_weight, expected.merger.fc2_weight);
        assert_eq!(actual.merger.fc2_bias, expected.merger.fc2_bias);
        assert_eq!(actual.merger.norm_weight, expected.merger.norm_weight);
        assert_eq!(actual.merger.norm_bias, expected.merger.norm_bias);
    }

    #[test]
    fn single_file_checkpoint_loads_without_an_index() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let tensors = tiny_f32_tensors(0.5);
        write_multi_f32_tensor_shard(&tmp.path().join("model.safetensors"), &tensors);

        let weights = load_qwen35_vision_weights(tmp.path(), &cfg)
            .expect("a valid single model.safetensors must not require a synthetic index");
        assert_eq!(weights.tensor_count(), tensors.len());
    }

    #[cfg(unix)]
    #[test]
    fn unindexed_model_safetensors_symlink_to_file_is_a_valid_candidate() {
        use std::os::unix::fs::symlink;

        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("checkpoint-blob"), b"fixture")
            .expect("test setup: write symlink target");
        symlink("checkpoint-blob", tmp.path().join("model.safetensors"))
            .expect("test setup: create model.safetensors symlink");

        let resolved = resolve_qwen35_single_decoder_safetensors(tmp.path())
            .expect("a safetensors symlink resolving to a file is a supported candidate");
        assert_eq!(resolved, tmp.path().join("model.safetensors"));
    }

    #[test]
    fn single_file_inventory_preflight_accepts_valid_header() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let tensors = tiny_f32_tensors(0.5);
        write_multi_f32_tensor_shard(&tmp.path().join("model.safetensors"), &tensors);

        validate_qwen35_vision_weight_inventory(tmp.path(), &cfg)
            .expect("single-file structural preflight must accept the exact visual inventory");
    }

    #[test]
    fn single_file_checkpoint_rejects_ambiguous_unindexed_candidates() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let tensors = tiny_f32_tensors(0.5);
        write_multi_f32_tensor_shard(&tmp.path().join("model.safetensors"), &tensors);
        std::fs::write(tmp.path().join("leftover.safetensors"), b"not a checkpoint")
            .expect("test setup: write second candidate");

        let err = load_qwen35_vision_weights(tmp.path(), &cfg)
            .expect_err("multiple unindexed safetensors files must be ambiguous");
        let msg = err.to_string();
        assert!(msg.contains("ambiguous unindexed checkpoint"), "got: {msg}");
        assert!(msg.contains("2 safetensors files"), "got: {msg}");
    }

    #[test]
    fn single_file_inventory_preflight_rejects_missing_visual_tensor() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let omitted = "model.visual.merger.norm.bias";
        let tensors: Vec<(String, Vec<usize>, Vec<f32>)> = tiny_expected_shapes()
            .into_iter()
            .filter(|(name, _)| name != omitted)
            .map(|(name, shape)| {
                let numel: usize = shape.iter().product();
                (name, shape, vec![0.5f32; numel])
            })
            .collect();
        write_multi_f32_tensor_shard(&tmp.path().join("model.safetensors"), &tensors);

        let err = validate_qwen35_vision_weight_inventory(tmp.path(), &cfg)
            .expect_err("an incomplete visual inventory must fail structural preflight");
        assert!(err.to_string().contains("inventory mismatch"), "got: {err}");
    }

    #[test]
    fn single_file_checkpoint_rejects_corrupt_header() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        std::fs::write(tmp.path().join("model.safetensors"), u64::MAX.to_le_bytes())
            .expect("test setup: write corrupt checkpoint");

        let err = load_qwen35_vision_weights(tmp.path(), &cfg)
            .expect_err("a corrupt safetensors header must fail before tensor loading");
        assert!(
            matches!(err, InferenceError::InvalidSafetensors(_)),
            "got: {err:?}"
        );
    }

    #[test]
    fn indexed_checkpoint_path_remains_preferred_over_plain_file() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let tensors = tiny_f32_tensors(0.5);
        write_multi_f32_tensor_shard(&tmp.path().join("model.safetensors"), &tensors);
        std::fs::write(
            tmp.path().join("model.safetensors.index.json"),
            b"not valid json",
        )
        .expect("test setup: write invalid index");

        load_qwen35_vision_weights(tmp.path(), &cfg)
            .expect_err("an existing index must not silently fall back to model.safetensors");
    }

    #[cfg(unix)]
    #[test]
    fn dangling_index_sentinel_does_not_fall_back_to_plain_file() {
        use std::os::unix::fs::symlink;

        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let tensors = tiny_f32_tensors(0.5);
        write_multi_f32_tensor_shard(&tmp.path().join("model.safetensors"), &tensors);
        symlink(
            "missing-index-target.json",
            tmp.path().join("model.safetensors.index.json"),
        )
        .expect("test setup: create dangling index sentinel");

        resolve_qwen35_single_decoder_safetensors(tmp.path())
            .expect_err("a dangling authoritative index must not resolve the plain file");
        load_qwen35_vision_weights(tmp.path(), &cfg)
            .expect_err("a dangling authoritative index must not load the plain file");
    }

    #[cfg(unix)]
    #[test]
    fn dangling_quantize_sentinel_does_not_fall_back_to_plain_file() {
        use std::os::unix::fs::symlink;

        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let tensors = tiny_f32_tensors(0.5);
        write_multi_f32_tensor_shard(&tmp.path().join("model.safetensors"), &tensors);
        symlink(
            "missing-quantize-target.json",
            tmp.path().join("quantize_index.json"),
        )
        .expect("test setup: create dangling quantized sentinel");

        load_qwen35_vision_weights(tmp.path(), &cfg)
            .expect_err("a dangling authoritative quantized manifest must not load the plain file");
    }

    #[test]
    fn indexed_layout_binds_visual_and_decoder_resolution_to_the_same_shard() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let shapes = tiny_expected_shapes();
        let plain_tensors = tiny_f32_tensors(0.1);
        write_multi_f32_tensor_shard(&tmp.path().join("model.safetensors"), &plain_tensors);

        let shard_name = "model-00001-of-00001.safetensors";
        let indexed_tensors = tiny_f32_tensors(0.9);
        write_multi_f32_tensor_shard(&tmp.path().join(shard_name), &indexed_tensors);
        let weight_map = shapes
            .iter()
            .map(|(name, _)| format!(r#""{name}":"{shard_name}""#))
            .collect::<Vec<_>>()
            .join(",");
        std::fs::write(
            tmp.path().join("model.safetensors.index.json"),
            format!(r#"{{"weight_map":{{{weight_map}}}}}"#),
        )
        .expect("test setup: write index");

        let resolved = resolve_qwen35_single_decoder_safetensors(tmp.path())
            .expect("one-shard index resolves");
        assert_eq!(resolved, tmp.path().join(shard_name));
        let weights =
            load_qwen35_vision_weights(tmp.path(), &cfg).expect("indexed visual checkpoint loads");
        assert_eq!(weights.patch_embed_weight[0], 0.9f32);
    }

    #[test]
    fn single_file_and_one_shard_index_load_identical_vision_weights() {
        let single = tempfile::tempdir().unwrap();
        let indexed = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let tensors: Vec<(String, Vec<usize>, Vec<f32>)> = tiny_expected_shapes()
            .into_iter()
            .enumerate()
            .map(|(i, (name, shape))| {
                let numel: usize = shape.iter().product();
                (name, shape, vec![i as f32 / 100.0; numel])
            })
            .collect();
        write_multi_f32_tensor_shard(&single.path().join("model.safetensors"), &tensors);

        let shard_name = "model-00001-of-00001.safetensors";
        write_multi_f32_tensor_shard(&indexed.path().join(shard_name), &tensors);
        let weight_map = tensors
            .iter()
            .map(|(name, _, _)| format!(r#""{name}":"{shard_name}""#))
            .collect::<Vec<_>>()
            .join(",");
        std::fs::write(
            indexed.path().join("model.safetensors.index.json"),
            format!(r#"{{"weight_map":{{{weight_map}}}}}"#),
        )
        .expect("test setup: write index");

        let from_single =
            load_qwen35_vision_weights(single.path(), &cfg).expect("single-file checkpoint loads");
        let from_index = load_qwen35_vision_weights(indexed.path(), &cfg)
            .expect("one-shard indexed checkpoint loads");
        assert_vision_weights_eq(&from_single, &from_index);
    }

    fn assert_fc2_shape_mismatch(result: Result<Qwen35VisionWeights, InferenceError>) {
        let err = result.expect_err("transposed FC2 (same numel, wrong shape) must be rejected");
        match err {
            InferenceError::ShapeMismatch { name, .. } => {
                assert!(
                    name.contains("fc2"),
                    "expected FC2 shape mismatch, got {name}"
                )
            }
            other => panic!("expected ShapeMismatch, got {other:?}"),
        }
    }

    #[test]
    fn fp16_same_numel_transposed_fc2_is_rejected() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let mut shapes = tiny_expected_shapes();
        transpose_fc2_weight(&mut shapes);

        let shard = tmp.path().join("model-00001-of-00001.safetensors");
        let tensors: Vec<(String, Vec<usize>, Vec<f32>)> = shapes
            .iter()
            .map(|(name, shape)| {
                let numel: usize = shape.iter().product();
                (name.clone(), shape.clone(), vec![0.5f32; numel])
            })
            .collect();
        write_multi_f32_tensor_shard(&shard, &tensors);

        let weight_map = shapes
            .iter()
            .map(|(name, _)| format!(r#""{name}":"model-00001-of-00001.safetensors""#))
            .collect::<Vec<_>>()
            .join(",");
        std::fs::write(
            tmp.path().join("model.safetensors.index.json"),
            format!(r#"{{"weight_map":{{{weight_map}}}}}"#),
        )
        .expect("test setup: write index");

        assert_fc2_shape_mismatch(load_qwen35_vision_weights(tmp.path(), &cfg));
    }

    #[test]
    fn q4_same_numel_transposed_fc2_is_rejected() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let mut shapes = tiny_expected_shapes();
        transpose_fc2_weight(&mut shapes);

        let mut manifest_entries = Vec::new();
        for (i, (name, shape)) in shapes.iter().enumerate() {
            let numel: usize = shape.iter().product();
            let data: Vec<f64> = vec![0.25_f64; numel];
            let q4 = crate::weights::q4_weights::quantize_f64_to_q4(&data, shape)
                .expect("quantize succeeds");
            let file_name = format!("t{i}.q4");
            crate::weights::q4_weights::save_q4_file(&tmp.path().join(&file_name), &q4)
                .expect("test setup: write q4 file");
            manifest_entries.push(format!(
                r#"{{"name":"{name}","file":"{file_name}","quantized":true}}"#
            ));
        }
        std::fs::write(
            tmp.path().join("quantize_index.json"),
            format!("[{}]", manifest_entries.join(",")),
        )
        .expect("test setup: write manifest");

        assert_fc2_shape_mismatch(load_qwen35_vision_weights(tmp.path(), &cfg));
    }

    /// #1069: quantize_index.json file entries are untrusted checkpoint
    /// content; an entry escaping the model directory must be rejected
    /// before its file is read.
    #[test]
    fn q4_manifest_entry_escaping_model_dir_is_rejected() {
        let tmp = tempfile::tempdir().unwrap();
        let model_dir = tmp.path().join("model");
        std::fs::create_dir_all(&model_dir).expect("test setup");
        // A structurally valid q4 file OUTSIDE the model dir — containment,
        // not file validity, must be what rejects the entry.
        let data = vec![0.25_f64; 4];
        let q4 = crate::weights::q4_weights::quantize_f64_to_q4(&data, &[2, 2])
            .expect("quantize succeeds");
        crate::weights::q4_weights::save_q4_file(&tmp.path().join("evil.q4"), &q4)
            .expect("test setup: write q4 file");
        std::fs::write(
            model_dir.join("quantize_index.json"),
            r#"[{"name":"model.visual.patch_embed.proj.weight","file":"../evil.q4","quantized":true}]"#,
        )
        .expect("test setup: write manifest");

        let err = load_qwen35_vision_weights(&model_dir, &tiny_vision_cfg())
            .expect_err("escaping manifest entry must be rejected");
        let msg = err.to_string();
        assert!(
            msg.contains("must stay within the model directory")
                || msg.contains("escapes model root"),
            "unexpected error: {err}"
        );
    }

    /// The manifest's declared shape must be checked against `vision_cfg` BEFORE the
    /// tensor's file is opened, matching what `fetch_expected_tensors` does on the fp16
    /// path.
    ///
    /// Pointing the entry at a file that does not exist is what makes this
    /// mutation-sensitive. With the preflight the loader never gets that far and returns
    /// ShapeMismatch; without it the loader tries to read `missing.q4` and reports a read
    /// failure instead. Asserting merely that *some* error came back would pass either
    /// way and guard nothing.
    #[test]
    fn q4_manifest_shape_disagreeing_with_config_is_rejected_before_the_file_is_read() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(
            tmp.path().join("quantize_index.json"),
            r#"[{"name":"model.visual.patch_embed.proj.bias","file":"missing.q4","quantized":true,"shape":[9999]}]"#,
        )
        .expect("test setup: write manifest");
        assert!(
            !tmp.path().join("missing.q4").exists(),
            "test setup: the tensor file must NOT exist, that absence is the assertion"
        );

        let err = load_qwen35_vision_weights(tmp.path(), &tiny_vision_cfg())
            .expect_err("a manifest shape contradicting vision_cfg must be rejected");
        match err {
            InferenceError::ShapeMismatch {
                name,
                expected,
                actual,
            } => {
                assert_eq!(name, "model.visual.patch_embed.proj.bias");
                assert_eq!(expected, vec![4]);
                assert_eq!(actual, vec![9999]);
            }
            other => panic!("expected ShapeMismatch before any file read, got: {other}"),
        }
    }

    /// When the manifest omits `shape`, the `.q4` header carries the only declared shape
    /// there is and the manifest-level preflight has nothing to compare. The header must
    /// then be checked against `vision_cfg` before `dequantize_q4_to_f32` allocates the
    /// full f32 buffer.
    ///
    /// Mutation-sensitivity needs care here, and it is exactly the trap the fp16-path
    /// test author already called out: `assemble` rejects a contradictory shape too, so
    /// `expect_err` alone still passes with the fix reverted. The discriminator is WHICH
    /// error comes back. This manifest carries exactly one tensor and it is deliberately
    /// not the one `assemble` takes first, so with the header check removed the loader
    /// dequantizes happily and then dies on MissingTensor for `patch_embed.proj.weight`,
    /// a different error about a different tensor.
    #[test]
    fn q4_header_shape_disagreeing_with_config_is_rejected_before_dequantization() {
        let tmp = tempfile::tempdir().unwrap();
        let data = vec![0.25_f64; 64];
        let q4 = crate::weights::q4_weights::quantize_f64_to_q4(&data, &[64])
            .expect("quantize succeeds");
        crate::weights::q4_weights::save_q4_file(&tmp.path().join("t0.q4"), &q4)
            .expect("test setup: write q4 file");
        std::fs::write(
            tmp.path().join("quantize_index.json"),
            r#"[{"name":"model.visual.patch_embed.proj.bias","file":"t0.q4","quantized":true}]"#,
        )
        .expect("test setup: write manifest");

        let err = load_qwen35_vision_weights(tmp.path(), &tiny_vision_cfg())
            .expect_err("a q4 header shape contradicting vision_cfg must be rejected");
        match err {
            InferenceError::ShapeMismatch {
                name,
                expected,
                actual,
            } => {
                assert_eq!(name, "model.visual.patch_embed.proj.bias");
                assert_eq!(expected, vec![4]);
                assert_eq!(actual, vec![64]);
            }
            other => panic!("expected ShapeMismatch before dequantization, got: {other}"),
        }
    }

    fn write_khf1_f16_file(path: &Path, shape: &[usize], values: &[f32]) {
        let mut buf = Vec::new();
        buf.extend_from_slice(b"KHF1");
        buf.extend_from_slice(&1u32.to_le_bytes());
        buf.extend_from_slice(&(shape.len() as u32).to_le_bytes());
        for d in shape {
            buf.extend_from_slice(&(*d as u64).to_le_bytes());
        }
        buf.extend_from_slice(&(values.len() as u64).to_le_bytes());
        for v in values {
            buf.extend_from_slice(&crate::weights::half_bits::f32_to_f16_bits(*v).to_le_bytes());
        }
        std::fs::write(path, &buf).expect("test setup: write f16 file");
    }

    #[test]
    fn f16_companion_same_numel_transposed_fc2_is_rejected() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let mut shapes = tiny_expected_shapes();
        transpose_fc2_weight(&mut shapes);

        let mut manifest_entries = Vec::new();
        for (i, (name, shape)) in shapes.iter().enumerate() {
            let numel: usize = shape.iter().product();
            let values = vec![0.5f32; numel];
            let file_name = format!("t{i}.f16");
            write_khf1_f16_file(&tmp.path().join(&file_name), shape, &values);
            manifest_entries.push(format!(
                r#"{{"name":"{name}","file":"{file_name}","quantized":false}}"#
            ));
        }
        std::fs::write(
            tmp.path().join("quantize_index.json"),
            format!("[{}]", manifest_entries.join(",")),
        )
        .expect("test setup: write manifest");

        assert_fc2_shape_mismatch(load_qwen35_vision_weights(tmp.path(), &cfg));
    }

    /// The `.f16` companion arm needs its own header check, and this is the case that
    /// proves it: a non-quantized entry whose manifest omits `shape` skips the preflight
    /// before `contained_shard_path` (there is no declared shape to compare) and does not
    /// reach the `.q4` header check (that arm is not taken). Without a header check here
    /// the tensor is fully read and converted before `assemble` notices.
    ///
    /// The single manifest entry is the bias rather than `patch_embed.proj.weight`, so
    /// without the guard the loader gets far enough for `assemble` to report the absent
    /// weight as `MissingTensor`. That makes the assertion below discriminating: it can
    /// only pass if the header was checked before materialization, not merely because
    /// loading failed somewhere.
    #[test]
    fn f16_header_shape_disagreeing_with_config_is_rejected_before_materialization() {
        let tmp = tempfile::tempdir().unwrap();
        write_khf1_f16_file(&tmp.path().join("t0.f16"), &[64], &vec![0.5f32; 64]);
        std::fs::write(
            tmp.path().join("quantize_index.json"),
            r#"[{"name":"model.visual.patch_embed.proj.bias","file":"t0.f16","quantized":false}]"#,
        )
        .expect("test setup: write manifest");

        let err = load_qwen35_vision_weights(tmp.path(), &tiny_vision_cfg())
            .expect_err("an f16 header shape contradicting vision_cfg must be rejected");
        match err {
            InferenceError::ShapeMismatch {
                name,
                expected,
                actual,
            } => {
                assert_eq!(name, "model.visual.patch_embed.proj.bias");
                assert_eq!(expected, vec![4]);
                assert_eq!(actual, vec![64]);
            }
            other => panic!("expected ShapeMismatch before materialization, got: {other}"),
        }
    }

    /// Proves the shape comparison happens BEFORE the payload is read, rather than merely
    /// before `assemble`.
    ///
    /// The fixture's header declares 64 elements but the file carries no payload bytes at
    /// all. If the loader read the payload first it would fail on the truncated read and
    /// surface `InvalidSafetensors`; only a loader that compares the header against the
    /// expected shape first can return `ShapeMismatch` for this input. The two outcomes
    /// are therefore distinguishable, which is the whole point of the fixture.
    ///
    /// What this does not prove is the same-handle property. That the header and payload
    /// come from one open cannot be shown without a filesystem seam to swap the file
    /// mid-load; it is structural, held by `load_f16_tensor_file_expecting` performing a
    /// single open, and this test would still pass if that were split back into two.
    #[test]
    fn f16_shape_is_compared_before_the_payload_is_read() {
        let tmp = tempfile::tempdir().unwrap();
        let mut buf = Vec::new();
        buf.extend_from_slice(b"KHF1");
        buf.extend_from_slice(&1u32.to_le_bytes());
        buf.extend_from_slice(&1u32.to_le_bytes());
        buf.extend_from_slice(&64u64.to_le_bytes());
        buf.extend_from_slice(&64u64.to_le_bytes());
        // Deliberately no payload: 64 declared elements, zero bytes of data.
        std::fs::write(tmp.path().join("t0.f16"), &buf).expect("test setup: write f16 header");
        std::fs::write(
            tmp.path().join("quantize_index.json"),
            r#"[{"name":"model.visual.patch_embed.proj.bias","file":"t0.f16","quantized":false}]"#,
        )
        .expect("test setup: write manifest");

        let err = load_qwen35_vision_weights(tmp.path(), &tiny_vision_cfg())
            .expect_err("a header disagreeing with vision_cfg must be rejected");
        match err {
            InferenceError::ShapeMismatch { actual, .. } => assert_eq!(actual, vec![64]),
            other => panic!(
                "expected ShapeMismatch from the header check, which proves the payload \
                 read was never attempted; got: {other}"
            ),
        }
    }

    #[test]
    fn q4_manifest_duplicate_visual_tensor_name_is_rejected() {
        // A tiny valid q4 inventory (all 21 entries `tiny_vision_cfg` expects, each with
        // a correct shape) plus one repeated `model.visual.*` name must be rejected
        // before `assemble`'s inventory-exactness check ever runs -- otherwise
        // `HashMap::insert` would silently let the duplicate's file overwrite the
        // first's, and the loader would return `Ok` depending on manifest entry order.
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let shapes = tiny_expected_shapes();

        let mut manifest_entries = Vec::new();
        for (i, (name, shape)) in shapes.iter().enumerate() {
            let numel: usize = shape.iter().product();
            let data: Vec<f64> = vec![0.25_f64; numel];
            let q4 = crate::weights::q4_weights::quantize_f64_to_q4(&data, shape)
                .expect("quantize succeeds");
            let file_name = format!("t{i}.q4");
            crate::weights::q4_weights::save_q4_file(&tmp.path().join(&file_name), &q4)
                .expect("test setup: write q4 file");
            manifest_entries.push(format!(
                r#"{{"name":"{name}","file":"{file_name}","quantized":true}}"#
            ));
        }
        // Duplicate: same name and shape as an existing entry, written to a distinct
        // file so a naive "does the file exist" check would not catch it.
        let (dup_name, dup_shape) = &shapes[0];
        let dup_numel: usize = dup_shape.iter().product();
        let dup_q4 =
            crate::weights::q4_weights::quantize_f64_to_q4(&vec![0.5_f64; dup_numel], dup_shape)
                .expect("quantize succeeds");
        let dup_file_name = "dup.q4";
        crate::weights::q4_weights::save_q4_file(&tmp.path().join(dup_file_name), &dup_q4)
            .expect("test setup: write duplicate q4 file");
        manifest_entries.push(format!(
            r#"{{"name":"{dup_name}","file":"{dup_file_name}","quantized":true}}"#
        ));

        std::fs::write(
            tmp.path().join("quantize_index.json"),
            format!("[{}]", manifest_entries.join(",")),
        )
        .expect("test setup: write manifest");

        let err = load_qwen35_vision_weights(tmp.path(), &cfg)
            .expect_err("a duplicate model.visual.* manifest entry must be rejected");
        match err {
            InferenceError::Inference(msg) => {
                assert!(
                    msg.contains("duplicate") && msg.contains(dup_name),
                    "expected a duplicate-entry error naming {dup_name}, got: {msg}"
                );
            }
            other => panic!("expected InferenceError::Inference, got {other:?}"),
        }
    }

    #[test]
    fn q4_manifest_unexpected_visual_tensor_rejected_before_dequantizing() {
        // FIX 4 regression: a manifest entry whose name is NOT in the set
        // `vision_cfg` expects must be rejected before its file is even opened -- not
        // decoded and only caught afterward by `assemble`'s "unconsumed leftover" check.
        // A valid tiny inventory plus one hostile extra entry proves the new pre-decode
        // name check fires; the assertion on the error text ("unexpected" + naming the
        // rejected tensor, not "unconsumed") distinguishes this from the old ordering.
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let shapes = tiny_expected_shapes();

        let mut manifest_entries = Vec::new();
        for (i, (name, shape)) in shapes.iter().enumerate() {
            let numel: usize = shape.iter().product();
            let data: Vec<f64> = vec![0.25_f64; numel];
            let q4 = crate::weights::q4_weights::quantize_f64_to_q4(&data, shape)
                .expect("quantize succeeds");
            let file_name = format!("t{i}.q4");
            crate::weights::q4_weights::save_q4_file(&tmp.path().join(&file_name), &q4)
                .expect("test setup: write q4 file");
            manifest_entries.push(format!(
                r#"{{"name":"{name}","file":"{file_name}","quantized":true}}"#
            ));
        }
        // An extra, valid, individually in-budget q4 tensor whose name `vision_cfg`
        // (depth=1) never asks for. Deliberately does NOT contain the substring
        // "unexpected" -- the post-decode "unconsumed leftover" fallback check (see
        // `assemble`) also names the rejected tensor in its error text, so asserting on
        // "unexpected" + the raw name alone would pass via either code path and fail to
        // discriminate a pre-decode rejection from a post-decode one.
        let hostile_name = "model.visual.blocks.0.rogue_injected_tensor";
        let hostile_q4 = crate::weights::q4_weights::quantize_f64_to_q4(&[0.5_f64; 4], &[4])
            .expect("quantize succeeds");
        let hostile_file_name = "hostile.q4";
        crate::weights::q4_weights::save_q4_file(&tmp.path().join(hostile_file_name), &hostile_q4)
            .expect("test setup: write hostile q4 file");
        manifest_entries.push(format!(
            r#"{{"name":"{hostile_name}","file":"{hostile_file_name}","quantized":true}}"#
        ));

        std::fs::write(
            tmp.path().join("quantize_index.json"),
            format!("[{}]", manifest_entries.join(",")),
        )
        .expect("test setup: write manifest");

        let err = load_qwen35_vision_weights(tmp.path(), &cfg)
            .expect_err("an unexpected model.visual.* manifest entry must be rejected");
        match err {
            InferenceError::Inference(msg) => {
                assert!(
                    msg.contains("rejected before dequantizing/decoding")
                        && msg.contains(hostile_name),
                    "expected a pre-decode unexpected-tensor error naming {hostile_name}, \
                     got: {msg}"
                );
            }
            other => panic!("expected InferenceError::Inference, got {other:?}"),
        }
    }

    #[test]
    fn fp16_sharded_index_duplicate_raw_weight_map_key_is_rejected() {
        // A raw `weight_map` JSON object can name the same tensor twice, mapped to two
        // different shards. Ordinary map deserialization collapses that to one
        // `HashMap` entry (last member wins) *before* the inventory-count check ever
        // runs, so the count stays exact and the loader silently resolves the tensor
        // from whichever shard happened to be the last raw JSON member -- raw-member-
        // order-dependent, not a deterministic error. All 21 tiny names resolve to
        // shard A; one name (`dup_name`) is additionally repeated, resolving to shard B
        // with a different (but shape-correct) value, so a silent last-write-wins
        // collapse would succeed with B's value instead of failing.
        let tmp = tempfile::tempdir().unwrap();
        let cfg = tiny_vision_cfg();
        let shapes = tiny_expected_shapes();

        let shard_a = tmp.path().join("model-00001-of-00002.safetensors");
        let tensors_a: Vec<(String, Vec<usize>, Vec<f32>)> = shapes
            .iter()
            .map(|(name, shape)| {
                let numel: usize = shape.iter().product();
                (name.clone(), shape.clone(), vec![0.5f32; numel])
            })
            .collect();
        write_multi_f32_tensor_shard(&shard_a, &tensors_a);

        let (dup_name, dup_shape) = shapes[0].clone();
        let dup_numel: usize = dup_shape.iter().product();
        let shard_b = tmp.path().join("model-00002-of-00002.safetensors");
        write_multi_f32_tensor_shard(
            &shard_b,
            &[(dup_name.clone(), dup_shape, vec![9.0f32; dup_numel])],
        );

        let mut weight_map_members: Vec<String> = shapes
            .iter()
            .map(|(name, _)| format!(r#""{name}":"model-00001-of-00002.safetensors""#))
            .collect();
        // Duplicate raw member for `dup_name`, pointing at the second shard.
        weight_map_members.push(format!(
            r#""{dup_name}":"model-00002-of-00002.safetensors""#
        ));
        std::fs::write(
            tmp.path().join("model.safetensors.index.json"),
            format!(r#"{{"weight_map":{{{}}}}}"#, weight_map_members.join(",")),
        )
        .expect("test setup: write index");

        let err = load_qwen35_vision_weights(tmp.path(), &cfg).expect_err(
            "a duplicate raw weight_map key routed to a second shard must be rejected, \
             not silently resolved by raw member order",
        );
        let msg = err.to_string();
        assert!(
            msg.contains("duplicate") && msg.contains(&dup_name),
            "expected a duplicate-key error naming {dup_name}, got: {msg}"
        );
    }

    #[test]
    fn assemble_rejects_unconsumed_leftover_tensors_when_depth_understates_inventory() {
        // The q4 path loads every `model.visual.*` manifest entry up front, not just the
        // names `vision_cfg.depth` implies. If the checkpoint has more real block tensors
        // than `depth` accounts for, the leftovers must be a hard error, not silently
        // dropped. The 21 tensors `small_cfg` (depth=1) does expect are given correct
        // shapes so this test exercises the leftover check specifically, not the
        // per-tensor shape check above it.
        let full_cfg = real_vision_cfg(); // depth 12, 153 real tensors
        let mut small_cfg = full_cfg.clone();
        small_cfg.depth = 1;

        let hidden = full_cfg.hidden_size;
        let qkv_out = 3 * hidden;
        let mlp_intermediate = 4 * hidden;
        let merge_in = full_cfg.spatial_merge_size * full_cfg.spatial_merge_size * hidden;
        let out_hidden = full_cfg.out_hidden_size;
        let shape_for = |name: &str| -> Vec<usize> {
            match name {
                "model.visual.patch_embed.proj.weight" => vec![
                    hidden,
                    full_cfg.in_channels,
                    full_cfg.temporal_patch_size,
                    full_cfg.patch_size,
                    full_cfg.patch_size,
                ],
                "model.visual.pos_embed.weight" => vec![full_cfg.num_position_embeddings, hidden],
                "model.visual.merger.linear_fc1.weight" => vec![merge_in, merge_in],
                "model.visual.merger.linear_fc1.bias" => vec![merge_in],
                "model.visual.merger.linear_fc2.weight" => vec![out_hidden, merge_in],
                "model.visual.merger.linear_fc2.bias" => vec![out_hidden],
                n if n.ends_with("attn.qkv.weight") => vec![qkv_out, hidden],
                n if n.ends_with("attn.qkv.bias") => vec![qkv_out],
                n if n.ends_with("attn.proj.weight") => vec![hidden, hidden],
                n if n.ends_with("mlp.linear_fc1.weight") => vec![mlp_intermediate, hidden],
                n if n.ends_with("mlp.linear_fc1.bias") => vec![mlp_intermediate],
                n if n.ends_with("mlp.linear_fc2.weight") => vec![hidden, mlp_intermediate],
                _ => vec![hidden], // *.bias, norm1/2.*, merger.norm.*
            }
        };

        let expected_names: std::collections::HashSet<String> =
            tensor_names(&small_cfg).into_iter().collect();
        let mut tensors: HashMap<String, (Vec<f32>, Vec<usize>)> = HashMap::new();
        for name in tensor_names(&full_cfg) {
            if expected_names.contains(&name) {
                let shape = shape_for(&name);
                let numel: usize = shape.iter().product();
                tensors.insert(name, (vec![0.0_f32; numel], shape));
            } else {
                // A leftover tensor beyond small_cfg's depth=1 — its shape is irrelevant
                // because it must never be consumed by `take`.
                tensors.insert(name, (vec![0.0_f32], vec![1]));
            }
        }

        let err = assemble(tensors, &small_cfg)
            .expect_err("leftover model.visual.* tensors beyond depth=1 must be rejected");
        match err {
            InferenceError::Inference(msg) => {
                assert!(
                    msg.contains("unconsumed"),
                    "expected an unconsumed-tensor inventory error, got: {msg}"
                );
            }
            other => panic!("expected InferenceError::Inference, got {other:?}"),
        }
    }

    #[test]
    fn assemble_accepts_official_qwen35_vl_vision_mlp_dims() {
        // FIX 16 regression: official Qwen3.5-VL vision tower dims are hidden_size=1152,
        // intermediate_size=4304 (NOT 4*1152=4608). Both loader preflight and `assemble`
        // must derive the expected MLP shapes from that explicit checkpoint geometry.
        let mut cfg = tiny_vision_cfg();
        cfg.hidden_size = 1152;
        cfg.num_heads = 1; // must divide hidden_size; head geometry is irrelevant here
        cfg.intermediate_size = Some(4304);

        let hidden = cfg.hidden_size;
        let mlp_intermediate = cfg.intermediate_size.unwrap();
        let qkv_out = 3 * hidden;
        let merge_in = cfg.spatial_merge_size * cfg.spatial_merge_size * hidden;
        let out_hidden = cfg.out_hidden_size;
        let shape_for = |name: &str| -> Vec<usize> {
            match name {
                "model.visual.patch_embed.proj.weight" => vec![
                    hidden,
                    cfg.in_channels,
                    cfg.temporal_patch_size,
                    cfg.patch_size,
                    cfg.patch_size,
                ],
                "model.visual.pos_embed.weight" => vec![cfg.num_position_embeddings, hidden],
                "model.visual.merger.linear_fc1.weight" => vec![merge_in, merge_in],
                "model.visual.merger.linear_fc1.bias" => vec![merge_in],
                "model.visual.merger.linear_fc2.weight" => vec![out_hidden, merge_in],
                "model.visual.merger.linear_fc2.bias" => vec![out_hidden],
                n if n.ends_with("attn.qkv.weight") => vec![qkv_out, hidden],
                n if n.ends_with("attn.qkv.bias") => vec![qkv_out],
                n if n.ends_with("attn.proj.weight") => vec![hidden, hidden],
                n if n.ends_with("mlp.linear_fc1.weight") => vec![mlp_intermediate, hidden],
                n if n.ends_with("mlp.linear_fc1.bias") => vec![mlp_intermediate],
                n if n.ends_with("mlp.linear_fc2.weight") => vec![hidden, mlp_intermediate],
                _ => vec![hidden], // *.bias, norm1/2.*, merger.norm.*
            }
        };
        let mut tensors: HashMap<String, (Vec<f32>, Vec<usize>)> = HashMap::new();
        for name in tensor_names(&cfg) {
            let shape = shape_for(&name);
            let numel: usize = shape.iter().product();
            tensors.insert(name, (vec![0.0_f32; numel], shape));
        }

        assemble(tensors, &cfg)
            .expect("official Qwen3.5-VL vision dims (1152/4304) must be accepted");
    }

    // Reading BF16/F16 safetensors tensors requires the `f16` feature (not default);
    // without it `get_f32_tensor` returns `InvalidSafetensors` for every such tensor.
    #[cfg(feature = "f16")]
    #[test]
    fn loads_real_fp16_checkpoint_with_correct_shapes() {
        let home = std::env::var("HOME").unwrap_or_else(|_| "/root".to_string());
        let model_dir = std::path::PathBuf::from(format!("{home}/.lattice/models/qwen3.5-0.8b"));
        if !model_dir.join("config.json").exists() {
            return; // model not downloaded; skip
        }
        let cfg = Qwen35Config::from_model_dir(&model_dir).expect("0.8b config.json parses");
        let vision_cfg = cfg
            .vision_config
            .expect("released 0.8b checkpoint has a vision_config");

        let weights = load_qwen35_vision_weights(&model_dir, &vision_cfg)
            .expect("fp16 vision weights must load without error");

        assert_eq!(weights.tensor_count(), 153);
        assert_eq!(weights.blocks.len(), 12);
        assert_eq!(weights.patch_embed_weight.len(), 768 * 3 * 2 * 16 * 16);
        assert_eq!(weights.patch_embed_bias.len(), 768);
        assert_eq!(weights.pos_embed.len(), 2304 * 768);

        let block0 = &weights.blocks[0];
        assert_eq!(block0.qkv_weight.len(), 2304 * 768);
        assert_eq!(block0.qkv_bias.len(), 2304);
        assert_eq!(block0.proj_weight.len(), 768 * 768);
        assert_eq!(block0.proj_bias.len(), 768);
        assert_eq!(block0.fc1_weight.len(), 3072 * 768);
        assert_eq!(block0.fc1_bias.len(), 3072);
        assert_eq!(block0.fc2_weight.len(), 768 * 3072);
        assert_eq!(block0.fc2_bias.len(), 768);
        assert_eq!(block0.norm1_weight.len(), 768);
        assert_eq!(block0.norm2_weight.len(), 768);

        assert_eq!(weights.merger.fc1_weight.len(), 3072 * 3072);
        assert_eq!(weights.merger.fc2_weight.len(), 1024 * 3072);
        assert_eq!(weights.merger.norm_weight.len(), 768);
    }

    #[test]
    fn loads_real_q4_checkpoint_with_correct_shapes() {
        let home = std::env::var("HOME").unwrap_or_else(|_| "/root".to_string());
        let model_dir = std::path::PathBuf::from(format!("{home}/.lattice/models/qwen3.5-0.8b-q4"));
        if !model_dir.join("config.json").exists() {
            return; // model not downloaded; skip
        }
        let cfg = Qwen35Config::from_model_dir(&model_dir).expect("q4 config.json parses");
        let vision_cfg = cfg
            .vision_config
            .expect("released q4 checkpoint has a vision_config");

        let weights = load_qwen35_vision_weights(&model_dir, &vision_cfg)
            .expect("q4 vision weights must load without error");

        assert_eq!(weights.tensor_count(), 153);
        assert_eq!(weights.blocks.len(), 12);
        assert_eq!(weights.patch_embed_weight.len(), 768 * 3 * 2 * 16 * 16);
        assert_eq!(weights.pos_embed.len(), 2304 * 768);

        let block0 = &weights.blocks[0];
        assert_eq!(block0.qkv_weight.len(), 2304 * 768);
        assert_eq!(block0.proj_weight.len(), 768 * 768);
        assert_eq!(block0.fc1_weight.len(), 3072 * 768);
        assert_eq!(block0.fc2_weight.len(), 768 * 3072);

        assert_eq!(weights.merger.fc1_weight.len(), 3072 * 3072);
        assert_eq!(weights.merger.fc2_weight.len(), 1024 * 3072);

        // Dequantized values must be finite (no NaN/Inf leaking through the q4 path).
        assert!(block0.qkv_weight.iter().all(|v| v.is_finite()));
        assert!(weights.merger.fc2_weight.iter().all(|v| v.is_finite()));
    }
}