mold-ai-inference 0.13.1

Candle-based inference engine for mold — FLUX, SDXL, SD3.5, Z-Image diffusion models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
use crate::engine::LoadStrategy;
use crate::progress::ProgressReporter;
use mold_core::types::GpuSelection;
use std::cell::Cell;

// ── Thread-local GPU ordinal guard ─────────────────────────────────────────
//
// Each GPU worker thread is pinned to a single ordinal. We stash that ordinal
// in a thread-local so cross-engine hotpaths (`create_device`, `reclaim_gpu_memory`)
// can debug-assert the caller isn't drifting onto a sibling GPU's context —
// the exact footgun that took the process down on <gpu-host> when LTX-2 had
// `reclaim_gpu_memory(0)` hardcoded and nuked GPU 0's context while SD3.5
// was still denoising there.
//
// Threads without a bound ordinal (tokio blocking pool, tests) see `None`
// and the assert is skipped.

thread_local! {
    static THREAD_GPU_ORDINAL: Cell<Option<usize>> = const { Cell::new(None) };
}

/// Bind the current thread to a GPU ordinal. Call once from each GPU worker
/// thread's entry point. Any subsequent `create_device` / `reclaim_gpu_memory`
/// call on this thread must match `ordinal` (debug builds only).
pub fn init_thread_gpu_ordinal(ordinal: usize) {
    THREAD_GPU_ORDINAL.with(|c| c.set(Some(ordinal)));
}

/// Clear the thread's GPU binding. Not strictly needed in production (workers
/// run for the process lifetime) but useful for tests that reuse threads.
pub fn clear_thread_gpu_ordinal() {
    THREAD_GPU_ORDINAL.with(|c| c.set(None));
}

/// Returns the currently-bound ordinal, if any.
pub fn thread_gpu_ordinal() -> Option<usize> {
    THREAD_GPU_ORDINAL.with(|c| c.get())
}

/// Panic in debug builds if `ordinal` doesn't match the thread's bound GPU.
/// A mismatch means a call site is ignoring its engine's `gpu_ordinal` and
/// reaching for another GPU's context — the SD3.5/LTX-2 crash pattern.
#[inline]
fn debug_assert_ordinal_matches_thread(ordinal: usize, context: &'static str) {
    if cfg!(debug_assertions) {
        if let Some(expected) = thread_gpu_ordinal() {
            assert_eq!(
                expected, ordinal,
                "{context}: ordinal {ordinal} does not match this thread's \
                 bound GPU {expected} — hardcoded ordinal regression?"
            );
        }
    }
}

// ── GPU discovery ──────────────────────────────────────────────────────────

/// Discovered GPU information for multi-GPU support.
#[derive(Debug, Clone)]
pub struct DiscoveredGpu {
    pub ordinal: usize,
    pub name: String,
    pub total_vram_bytes: u64,
    pub free_vram_bytes: u64,
}

/// Discover all available GPUs on the system.
pub fn discover_gpus() -> Vec<DiscoveredGpu> {
    let mut gpus = Vec::new();

    #[cfg(feature = "cuda")]
    {
        use candle_core::cuda_backend::cudarc::driver;
        if candle_core::utils::cuda_is_available() {
            // `CudaContext::device_count()` calls `cuInit(0)` first, which is
            // required before any driver API — bare `result::device::get_count()`
            // returns `ErrorNotInitialized` and we'd silently see zero GPUs.
            match driver::CudaContext::device_count() {
                Ok(count) => {
                    for ordinal in 0..count as usize {
                        match driver::CudaContext::new(ordinal) {
                            Ok(ctx) => {
                                let name = ctx
                                    .name()
                                    .unwrap_or_else(|_| format!("CUDA Device {ordinal}"));
                                // `CudaContext::new` binds the calling thread to
                                // this ordinal, so `mem_get_info` returns this GPU's
                                // VRAM.
                                let (free, total) =
                                    driver::result::mem_get_info().unwrap_or((0, 0));
                                gpus.push(DiscoveredGpu {
                                    ordinal,
                                    name,
                                    total_vram_bytes: total as u64,
                                    free_vram_bytes: free as u64,
                                });
                            }
                            Err(e) => tracing::warn!("failed to open CUDA device {ordinal}: {e}"),
                        }
                    }
                }
                Err(e) => tracing::warn!("CUDA device count failed: {e}"),
            }
        }
    }

    #[cfg(not(feature = "cuda"))]
    {
        if candle_core::utils::metal_is_available() {
            // Metal: single device on macOS (unified memory).
            let total = available_system_memory_bytes().unwrap_or(0);
            let free = free_system_memory_bytes().unwrap_or(0);
            gpus.push(DiscoveredGpu {
                ordinal: 0,
                name: "Apple Metal GPU".to_string(),
                total_vram_bytes: total,
                free_vram_bytes: free,
            });
        }
    }

    gpus
}

/// Filter discovered GPUs by user selection.
pub fn filter_gpus(gpus: &[DiscoveredGpu], selection: &GpuSelection) -> Vec<DiscoveredGpu> {
    match selection {
        GpuSelection::All => gpus.to_vec(),
        GpuSelection::Specific(ordinals) => gpus
            .iter()
            .filter(|g| ordinals.contains(&g.ordinal))
            .cloned()
            .collect(),
    }
}

/// Select the single best GPU (most free VRAM) for local CLI use.
pub fn select_best_gpu(gpus: &[DiscoveredGpu]) -> Option<&DiscoveredGpu> {
    gpus.iter().max_by_key(|g| g.free_vram_bytes)
}

// ── Device creation ────────────────────────────────────────────────────────

/// Create a device on the specified GPU ordinal.
/// Use ordinal 0 for single-GPU setups.
/// Reports device selection via the progress reporter.
pub fn create_device(
    ordinal: usize,
    progress: &ProgressReporter,
) -> anyhow::Result<candle_core::Device> {
    use candle_core::Device;
    // MOLD_DEVICE=cpu forces CPU inference (for debugging Metal issues)
    let force_cpu = std::env::var("MOLD_DEVICE")
        .map(|v| v.eq_ignore_ascii_case("cpu"))
        .unwrap_or(false);
    if force_cpu {
        progress.info("CPU forced via MOLD_DEVICE=cpu");
        tracing::info!("CPU forced via MOLD_DEVICE=cpu");
        return Ok(Device::Cpu);
    }
    debug_assert_ordinal_matches_thread(ordinal, "create_device");
    if candle_core::utils::cuda_is_available() {
        progress.info(&format!("Using CUDA device {ordinal}"));
        tracing::info!("Using CUDA device {ordinal}");
        Ok(Device::new_cuda(ordinal)?)
    } else if candle_core::utils::metal_is_available() {
        progress.info(&format!("Using Metal device {ordinal}"));
        tracing::info!("Using Metal device {ordinal}");
        Ok(Device::new_metal(ordinal)?)
    } else {
        progress.info("No GPU detected, using CPU");
        tracing::warn!("No GPU detected, falling back to CPU");
        Ok(Device::Cpu)
    }
}

/// Headroom above model size for activation memory during encoding.
pub const T5_ACTIVATION_HEADROOM: u64 = 2_000_000_000; // 2GB

/// Compute VRAM threshold for a T5 model of a given size.
/// The model needs its own weight size plus headroom for activations.
pub fn t5_vram_threshold(model_size_bytes: u64) -> u64 {
    model_size_bytes + T5_ACTIVATION_HEADROOM
}

/// Minimum free VRAM (bytes) required to place FP16 T5-XXL on GPU.
/// Kept for backward compatibility — equivalent to `t5_vram_threshold(9_200_000_000)`.
pub const T5_VRAM_THRESHOLD: u64 = 16_000_000_000;
/// Minimum free VRAM (bytes) required to place CLIP-L on GPU: ~246MB model + 500MB headroom.
pub const CLIP_VRAM_THRESHOLD: u64 = 800_000_000;
/// Minimum free VRAM (bytes) required to place CLIP-G on GPU: ~1.39GB model + ~1.4GB headroom.
pub const CLIPG_VRAM_THRESHOLD: u64 = 2_800_000_000;

/// Compute VRAM threshold for a Qwen3 text encoder of a given size.
/// Uses the same headroom formula as T5 (model size + 2GB activations).
pub fn qwen3_vram_threshold(model_size_bytes: u64) -> u64 {
    model_size_bytes + T5_ACTIVATION_HEADROOM
}

/// Compute VRAM threshold for a Qwen2.5-VL text encoder of a given size.
/// Uses the same headroom formula as T5/Qwen3 (model size + 2GB activations).
pub fn qwen2_vram_threshold(model_size_bytes: u64) -> u64 {
    model_size_bytes + T5_ACTIVATION_HEADROOM
}

/// Headroom above the expand LLM weights for activations + KV cache.
/// The expander generates short sequences (<= 512 tokens) so 2 GB is generous.
/// Matches `T5_ACTIVATION_HEADROOM` convention (decimal GB) for easy comparison.
pub const EXPAND_ACTIVATION_HEADROOM: u64 = 2_000_000_000;

// ── Activation-aware memory budget ───────────────────────────────────────────
//
// ComfyUI computes a per-arch `memory_required(input_shape)` (see
// `comfy/model_base.py:387-409`) instead of a fixed inference headroom. The
// fixed 3 GB / 5 GB heuristics below over-budget at small resolutions (forcing
// unneeded offload at 768²) and under-budget at large ones (causing the OOMs
// that motivated the pre-VAE force-drop in `1c276c6`). The helper here scales
// the activation budget with `area × dtype × batch × per_arch_factor`.

/// Architecture family for activation-budget purposes.
///
/// Mirrors the engine families in `crates/mold-inference/src/`. The factors
/// in [`activation_bytes`] are calibrated empirically per arch — flash /
/// memory-efficient attention reshapes the constant by a substantial factor
/// because peak attention workspace stops scaling as `B × H × N × N`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActivationFamily {
    /// FLUX v1 dit (dev / schnell / krea / kontext / fill).
    FluxDit,
    /// Flux.2 dit (Klein / Pro).
    Flux2Dit,
    /// SD3 / SD3.5 MMDiT.
    Sd3Mmdit,
    /// SDXL UNet (CFG-batched + cross-attn KV cache).
    SdxlUnet,
    /// Qwen-Image / Qwen-Image-Edit dit.
    QwenImageDit,
    /// Z-Image dit.
    ZImageDit,
    /// Wuerstchen v2 cascade (Stage C/B decoder).
    Wuerstchen,
    /// T5 / CLIP / Qwen3 / Gemma text encoder workspace.
    SmallTransformer,
    /// LTX-Video (0.9.6 / 0.9.8 2B or 13B) video transformer. Loads the
    /// entire transformer into VRAM at generation time (not streamed). The
    /// 13B BF16 variant is ~26 GB on disk. The file-size-based preflight
    /// applies unchanged; no streaming cap override.
    LtxVideo,
    /// LTX-2 (19B / 22B) video transformer. Always loaded via
    /// the streaming block source (`new_streaming` in
    /// `crates/mold-inference/src/ltx2/model/video_transformer.rs`) — only
    /// `streaming_prefetch_count` blocks are GPU-resident at any one time,
    /// so the file size on disk (~46 GB at BF16 for the 22B preset)
    /// massively over-estimates GPU residency.
    Ltx2Video,
}

impl ActivationFamily {
    /// Whether this family loads its transformer in a block-streaming mode
    /// (only a few blocks GPU-resident at a time, the rest mmap'd / paged).
    /// The preflight uses this to bypass the file-size-based transformer
    /// budget, which would otherwise reject 22B LTX-2 on a 24 GB card even
    /// though only ~2 GB of transformer weights are co-resident at peak.
    pub fn streaming_transformer(self) -> bool {
        matches!(self, ActivationFamily::Ltx2Video)
    }

    /// Whether this family needs the full-weight peak (transformer fully
    /// resident on GPU at inference time). Used to choose the right headroom
    /// constant in the preflight suggestion message.
    pub fn is_full_weight_video(self) -> bool {
        matches!(self, ActivationFamily::LtxVideo)
    }
}

/// Estimated activation memory (in bytes) for a single forward pass.
///
/// Mirrors ComfyUI's `model_base.memory_required(input_shape)` shape: peak
/// activation memory at fp16/bf16 scales as
/// `area × dtype_bytes × batch × per_arch_factor` where `area = h × w` in
/// image space. The factor encapsulates per-arch overhead (residuals,
/// attention KV cache, MLP intermediate, etc.). When flash /
/// memory-efficient attention is in use the factor is smaller because peak
/// attention workspace stops scaling as `B × H × N × N`.
///
/// `width`/`height` are image-space dimensions — the helper internally
/// accounts for the 8× VAE downsample via the per-family factor, so you
/// pass `req.width`/`req.height` directly, not latent dims.
/// `batch` is typically `1` for non-CFG (FLUX, Z-Image, distilled
/// schedulers) and `2` for CFG-doubled forwards (SDXL, SD3 with
/// `guidance != 1.0`).
/// `dtype_bytes` is `2` for bf16/fp16, `4` for f32, `8` for f64.
///
/// Floors at 256 MB so even tiny inputs reserve enough for kernel workspaces
/// (cuBLAS / cuDNN scratch, tokenizer / embedding buffers).
///
/// Empirical anchors:
///   * FLUX dit 1024² bf16 cfg=1   → ~273 MB
///   * FLUX dit 2048² bf16 cfg=1   → ~1.09 GB
///   * SDXL UNet 1024² bf16 cfg=2  → ~726 MB
///   * Wuerstchen 1024² bf16       → ~456 MB
pub fn activation_bytes(
    width: u32,
    height: u32,
    batch: u32,
    dtype_bytes: u32,
    family: ActivationFamily,
) -> u64 {
    let area = (width as u64).saturating_mul(height as u64);
    let bytes_per_pixel = (dtype_bytes as u64).saturating_mul(batch.max(1) as u64);
    // Per-family factor — calibrated to produce ~273 MB at 1024² bf16 cfg=1
    // for FLUX dit (just above the 256 MB floor, ~1.09 GB at 2048²), with
    // arch-specific multipliers for the heavier-attention families. Units
    // are roughly "bytes of activation per pixel per dtype-byte per batch".
    //
    // The original spec doc had factors in the 0.01 – 0.025 range, which
    // produced sub-megabyte raw budgets at 1024² (the formula's units make
    // those values meaningful only if reinterpreted as megabytes-per-
    // megapixel) — the floor would always dominate and the
    // resolution-scaling test would fail. The factors here are scaled up
    // by ~8000× to actually realize the documented ~250 MB / ~1 GB
    // targets.
    let factor: f64 = match family {
        // FLUX v1 dit: double + single block residuals dominate, flash-attn
        // collapses the B×N×N peak. 1024² → 273 MB; 2048² → 1.09 GB.
        ActivationFamily::FluxDit => 130.0,
        // Flux.2 dit: same activation shape as FLUX v1 (Klein/Pro).
        ActivationFamily::Flux2Dit => 130.0,
        // Z-Image dit: single chunk of dit blocks, similar to FLUX.
        ActivationFamily::ZImageDit => 130.0,
        // SD3 MMDiT: joint attention sits between FLUX's split and SDXL's
        // cross-attn — calibrated ~20% above FLUX.
        ActivationFamily::Sd3Mmdit => 156.0,
        // SDXL UNet: CFG runs `[uncond, cond]` and cross-attn KV is cached
        // for the prompt sequence — ~33% above FLUX. Callers pass `batch=2`
        // when CFG is active, so this factor covers per-batch overhead.
        ActivationFamily::SdxlUnet => 173.0,
        // Qwen-Image dit: similar dual-stream structure to SDXL.
        ActivationFamily::QwenImageDit => 173.0,
        // Wuerstchen v2: cascade Stage B has a chunky conv stack — ~67%
        // above FLUX.
        ActivationFamily::Wuerstchen => 217.0,
        // T5 / CLIP / Qwen3 encoders work over tokens × hidden, not pixels.
        // Image-space scaling is a soft proxy for "small workspace" — the
        // floor usually dominates for typical inputs.
        ActivationFamily::SmallTransformer => 87.0,
        // LTX-Video (0.9.6 / 0.9.8): per-frame activation is similar to FLUX
        // dit (split blocks, RMSNorm, no CFG-batched workspace). The full
        // transformer is resident on GPU during denoise, so the activation
        // budget here is an additional workspace on top of the weight peak
        // captured by the file-size estimator.
        ActivationFamily::LtxVideo => 130.0,
        // LTX-2: same per-pixel activation shape as LTX-Video. Only 1-2
        // streaming blocks are GPU-resident at a time, so the dominant
        // cost on a 24 GB card is encoder + block workspace, not the
        // full-weight sum.
        ActivationFamily::Ltx2Video => 130.0,
    };
    let raw = (area as f64 * bytes_per_pixel as f64 * factor) as u64;
    /// Sanity floor: even tiny inputs reserve ~256 MB for kernel workspaces
    /// (cuBLAS / cuDNN scratch, tokenizer / embedding buffers).
    const ACTIVATION_FLOOR_BYTES: u64 = 256_000_000;
    raw.max(ACTIVATION_FLOOR_BYTES)
}

/// Bytes per element for a given candle dtype, used to feed
/// [`activation_bytes`] from a runtime `DType`. Returns `2` for bf16/fp16 and
/// `4` for f32; integer / quantized weights still flow as bf16/fp16
/// activations during forward, so `2` is the right answer there too.
pub fn dtype_bytes(dt: candle_core::DType) -> u32 {
    use candle_core::DType;
    match dt {
        DType::BF16 | DType::F16 => 2,
        DType::F32 => 4,
        DType::F64 => 8,
        // Everything else (quantized / int storage / sub-byte floats):
        // activations during forward travel as bf16/fp16, so `2` matches
        // the runtime activation cost.
        _ => 2,
    }
}

/// Map a manifest family slug (e.g. `"flux"`, `"sdxl"`, `"qwen-image"`) to the
/// activation-budget family. Falls back to [`ActivationFamily::FluxDit`] for
/// unknown slugs — the FLUX factor is the most common diffusion default and
/// errs toward a conservative-but-not-over-budget estimate.
pub fn activation_family_for(family_slug: &str) -> ActivationFamily {
    match family_slug {
        "flux" => ActivationFamily::FluxDit,
        "flux2" => ActivationFamily::Flux2Dit,
        "sd3" => ActivationFamily::Sd3Mmdit,
        "sdxl" | "sd15" => ActivationFamily::SdxlUnet,
        "qwen-image" | "qwen-image-edit" => ActivationFamily::QwenImageDit,
        "z-image" => ActivationFamily::ZImageDit,
        "wuerstchen" => ActivationFamily::Wuerstchen,
        // LTX-Video (0.9.6 / 0.9.8 2B or 13B): loads the entire transformer
        // into VRAM during each generate call. The file-size-based preflight
        // applies normally — the 13B BF16 checkpoint is ~26 GB and must be
        // counted in full.
        "ltx-video" => ActivationFamily::LtxVideo,
        // LTX-2 (19B / 22B): streaming-loaded transformer — only a couple of
        // blocks are GPU-resident at peak, so the preflight skips the
        // file-size estimate and uses a fixed streaming cap instead.
        "ltx2" | "ltx-2" | "ltx-2.3" => ActivationFamily::Ltx2Video,
        // Unknown families default to FLUX dit shape — same activation
        // class, conservative against unknowns.
        _ => ActivationFamily::FluxDit,
    }
}

/// Compute VRAM threshold for an expand LLM of a given size (weights + headroom).
pub fn expand_vram_threshold(model_size_bytes: u64) -> u64 {
    model_size_bytes + EXPAND_ACTIVATION_HEADROOM
}

/// Resolved placement for the expand LLM.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExpandPlacement {
    /// Place on GPU with the given ordinal.
    Gpu(usize),
    /// Place on CPU (system RAM).
    Cpu,
}

/// Pick where to run the expand LLM: main GPU first, then remaining GPUs in
/// ordinal order, then CPU.
///
/// - `gpus` must be in ordinal order (as returned by `discover_gpus()`).
/// - On Metal (unified memory) the single discovered "GPU" is always chosen
///   — memory policing happens via system-RAM preflight at the call site,
///   since GPU VRAM and system RAM are the same pool.
/// - A GPU is considered to fit when `free_vram_bytes > threshold`.
/// - Returns `ExpandPlacement::Cpu` when no GPU has room (or when `gpus` is
///   empty). The caller is responsible for running a system-RAM preflight
///   before actually allocating on CPU.
pub fn select_expand_device(
    gpus: &[DiscoveredGpu],
    threshold: u64,
    is_metal: bool,
) -> ExpandPlacement {
    select_expand_device_with_preference(gpus, threshold, is_metal, None)
}

/// Same as [`select_expand_device`], but prefers `preferred_ordinal` when it
/// is in the allowed GPU set and has enough free VRAM.
pub fn select_expand_device_with_preference(
    gpus: &[DiscoveredGpu],
    threshold: u64,
    is_metal: bool,
    preferred_ordinal: Option<usize>,
) -> ExpandPlacement {
    if is_metal {
        if let Some(ordinal) = preferred_ordinal {
            if let Some(g) = gpus.iter().find(|g| g.ordinal == ordinal) {
                return ExpandPlacement::Gpu(g.ordinal);
            }
        }
        if let Some(g) = gpus.first() {
            return ExpandPlacement::Gpu(g.ordinal);
        }
        return ExpandPlacement::Cpu;
    }
    if let Some(ordinal) = preferred_ordinal {
        if let Some(g) = gpus
            .iter()
            .find(|g| g.ordinal == ordinal && g.free_vram_bytes > threshold)
        {
            return ExpandPlacement::Gpu(g.ordinal);
        }
    }
    for g in gpus {
        if g.free_vram_bytes > threshold {
            return ExpandPlacement::Gpu(g.ordinal);
        }
    }
    ExpandPlacement::Cpu
}

// ── LTX-2 Gemma encoder placement ────────────────────────────────────────────

/// Minimum free VRAM (bytes) needed to land Gemma 3 12B BF16 on a single GPU
/// alongside its activation workspace. ~23 GB resident weights + ~1 GB
/// activation overhead. Encoder isn't streamed — picking GPU means the whole
/// thing is co-resident with the LTX-2 transformer phase.
pub const LTX2_GEMMA_VRAM_THRESHOLD: u64 = 24_000_000_000;

/// Resolved placement for the LTX-2 Gemma 3 12B prompt encoder.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LtxGemmaPlacement {
    /// Place the encoder on GPU with the given ordinal.
    Gpu(usize),
    /// Place the encoder on CPU (system RAM).
    Cpu,
}

impl LtxGemmaPlacement {
    /// Convert to a candle `Device`. CUDA failures fall back to CPU rather
    /// than panic — the caller already paid for the placement decision and a
    /// runtime CUDA error here is far worse than honoring the hint as CPU.
    pub fn into_device(self) -> candle_core::Device {
        match self {
            LtxGemmaPlacement::Gpu(ordinal) => match candle_core::Device::new_cuda(ordinal) {
                Ok(d) => d,
                Err(err) => {
                    tracing::warn!(
                        ordinal,
                        error = %err,
                        "failed to open CUDA device for LTX-2 Gemma encoder, falling back to CPU"
                    );
                    candle_core::Device::Cpu
                }
            },
            LtxGemmaPlacement::Cpu => candle_core::Device::Cpu,
        }
    }
}

/// Pick where to load the LTX-2 Gemma 3 12B prompt encoder: active GPU first,
/// then sibling GPUs in ordinal order, then CPU.
///
/// - `gpus` is the output of [`discover_gpus`] — ordinals in ascending order.
/// - `active_ordinal` is the GPU the LTX-2 transformer was loaded onto. We
///   prefer co-residency (no cross-device tensor copy at encode time) but
///   fall through to siblings when the active GPU is full.
/// - A GPU is considered to fit when `free_vram_bytes > threshold` (strict
///   greater-than, mirrors [`select_expand_device`]).
/// - Returns [`LtxGemmaPlacement::Cpu`] when no GPU has room.
pub fn select_ltx2_gemma_device(
    gpus: &[DiscoveredGpu],
    active_ordinal: usize,
    threshold: u64,
) -> LtxGemmaPlacement {
    if let Some(g) = gpus
        .iter()
        .find(|g| g.ordinal == active_ordinal && g.free_vram_bytes > threshold)
    {
        return LtxGemmaPlacement::Gpu(g.ordinal);
    }
    for g in gpus {
        if g.ordinal == active_ordinal {
            continue;
        }
        if g.free_vram_bytes > threshold {
            return LtxGemmaPlacement::Gpu(g.ordinal);
        }
    }
    LtxGemmaPlacement::Cpu
}

/// Read [`MOLD_LTX2_GEMMA_DEVICE`] (and the deprecated
/// [`MOLD_LTX2_DEBUG_FORCE_CPU_PROMPT_ENCODER`] alias) and return an explicit
/// placement override. `auto`, an unset env, or a value the parser doesn't
/// recognise return `None` so the caller falls through to the auto-resolver.
///
/// The returned `Gpu` placement always points at `gpu_ordinal` — explicit
/// `gpu` doesn't try to outsmart the user by walking siblings.
pub fn resolve_ltx2_gemma_device_override(gpu_ordinal: usize) -> Option<LtxGemmaPlacement> {
    if let Ok(raw) = std::env::var("MOLD_LTX2_GEMMA_DEVICE") {
        let trimmed = raw.trim();
        if !trimmed.is_empty() {
            let lower = trimmed.to_ascii_lowercase();
            match lower.as_str() {
                "cpu" => return Some(LtxGemmaPlacement::Cpu),
                "gpu" => return Some(LtxGemmaPlacement::Gpu(gpu_ordinal)),
                "auto" => return None,
                _ => {
                    tracing::warn!(
                        value = %trimmed,
                        "unrecognised MOLD_LTX2_GEMMA_DEVICE value; expected cpu/gpu/auto",
                    );
                    return None;
                }
            }
        }
    }

    if std::env::var_os("MOLD_LTX2_DEBUG_FORCE_CPU_PROMPT_ENCODER").is_some() {
        warn_once_legacy_force_cpu_prompt_encoder();
        return Some(LtxGemmaPlacement::Cpu);
    }

    None
}

fn warn_once_legacy_force_cpu_prompt_encoder() {
    use std::sync::OnceLock;
    static WARNED: OnceLock<()> = OnceLock::new();
    WARNED.get_or_init(|| {
        tracing::warn!(
            "MOLD_LTX2_DEBUG_FORCE_CPU_PROMPT_ENCODER is deprecated; \
             use MOLD_LTX2_GEMMA_DEVICE=cpu instead",
        );
    });
}

/// Resolve the LTX-2 Gemma encoder placement once, honoring the env override
/// before falling through to the GPU-walk + CPU fallback. The runtime and
/// the server-side preflight both call this so they reach the same decision
/// for the same observation of free VRAM and env vars.
pub fn resolve_ltx2_gemma_placement(gpu_ordinal: usize) -> LtxGemmaPlacement {
    if let Some(p) = resolve_ltx2_gemma_device_override(gpu_ordinal) {
        return p;
    }
    let gpus = discover_gpus();
    select_ltx2_gemma_device(&gpus, gpu_ordinal, LTX2_GEMMA_VRAM_THRESHOLD)
}

/// Minimum free VRAM for BF16 Qwen3-4B on GPU with drop-and-reload.
/// 8.2GB model + 2GB activation headroom = 10.2GB.
/// With drop-and-reload, the encoder is temporary — loaded for encoding, then dropped.
pub const QWEN3_FP16_VRAM_THRESHOLD: u64 = 10_200_000_000;

/// Headroom for activation memory during inference (denoising + VAE decode workspace).
const MEMORY_BUDGET_HEADROOM: u64 = 2_000_000_000; // 2GB

// ── Placement resolution ─────────────────────────────────────────────────────

/// Resolve a caller-supplied `DeviceRef` override into a concrete candle
/// `Device`, falling back to `auto` when the override is missing or `Auto`.
///
/// - `None`, `Some(Auto)` — call `auto()` (existing VRAM-aware logic).
/// - `Some(Cpu)`          — `Device::Cpu`, never invoke `auto()`.
/// - `Some(Gpu { ordinal })` — try CUDA first, then Metal. Each backend is
///   gated by its candle feature flag so a CPU-only build returns a clear
///   error message instead of a build failure.
pub fn resolve_device<F>(
    req: Option<mold_core::types::DeviceRef>,
    auto: F,
) -> anyhow::Result<candle_core::Device>
where
    F: FnOnce() -> anyhow::Result<candle_core::Device>,
{
    use mold_core::types::DeviceRef;
    match req {
        None | Some(DeviceRef::Auto) => auto(),
        Some(DeviceRef::Cpu) => Ok(candle_core::Device::Cpu),
        Some(DeviceRef::Gpu { ordinal }) => resolve_gpu_ordinal(ordinal),
    }
}

#[cfg(feature = "cuda")]
fn resolve_gpu_ordinal(ordinal: usize) -> anyhow::Result<candle_core::Device> {
    debug_assert_ordinal_matches_thread(ordinal, "resolve_device");
    candle_core::Device::new_cuda(ordinal)
        .map_err(|e| anyhow::anyhow!("failed to open CUDA device {ordinal}: {e}"))
}

#[cfg(all(not(feature = "cuda"), feature = "metal"))]
fn resolve_gpu_ordinal(ordinal: usize) -> anyhow::Result<candle_core::Device> {
    debug_assert_ordinal_matches_thread(ordinal, "resolve_device");
    candle_core::Device::new_metal(ordinal)
        .map_err(|e| anyhow::anyhow!("failed to open Metal device {ordinal}: {e}"))
}

#[cfg(all(not(feature = "cuda"), not(feature = "metal")))]
fn resolve_gpu_ordinal(ordinal: usize) -> anyhow::Result<candle_core::Device> {
    Err(anyhow::anyhow!(
        "GPU ordinal {ordinal} requested but this build has neither CUDA nor Metal enabled"
    ))
}

/// Resolve a component-level `DeviceRef` from a `DevicePlacement`, honoring
/// the Tier 2 per-component override first, then the Tier 1 `text_encoders`
/// group knob when appropriate.
///
/// Precedence:
///   1. `advanced_override` (Tier 2 per-component) if `Some`.
///   2. Fall back to `placement.text_encoders` (group knob) when
///      `fallback_is_component_auto` is `true` (typically for text-encoder
///      components — T5/CLIP-L/Qwen — that follow the group knob by default).
///   3. Fall back to `DeviceRef::Auto` (non-text-encoder components like the
///      VAE, which don't inherit from the text-encoder group knob).
pub fn effective_device_ref(
    placement: Option<&mold_core::types::DevicePlacement>,
    advanced_override: impl FnOnce(
        &mold_core::types::AdvancedPlacement,
    ) -> Option<mold_core::types::DeviceRef>,
    fallback_is_component_auto: bool,
) -> mold_core::types::DeviceRef {
    use mold_core::types::DeviceRef;
    let Some(placement) = placement else {
        return DeviceRef::Auto;
    };
    if let Some(adv) = placement.advanced.as_ref() {
        if let Some(r) = advanced_override(adv) {
            return r;
        }
        if fallback_is_component_auto {
            return placement.text_encoders;
        }
        DeviceRef::Auto
    } else {
        placement.text_encoders
    }
}

// ── macOS memory query ───────────────────────────────────────────────────────

/// Raw VM statistics from macOS host_statistics64.
#[cfg(target_os = "macos")]
struct MacOSMemInfo {
    free: u64,
    inactive: u64,
}

/// Query macOS VM statistics using host_statistics64 FFI.
#[cfg(target_os = "macos")]
fn macos_vm_stats() -> Option<MacOSMemInfo> {
    type MachPort = u32;
    type KernReturn = i32;
    type HostFlavor = i32;
    type MachMsgType = u32;

    const HOST_VM_INFO64: HostFlavor = 4;
    const HOST_VM_INFO64_COUNT: MachMsgType = 38;
    const KERN_SUCCESS: KernReturn = 0;

    extern "C" {
        fn mach_host_self() -> MachPort;
        fn host_statistics64(
            host: MachPort,
            flavor: HostFlavor,
            info: *mut i32,
            count: *mut MachMsgType,
        ) -> KernReturn;
        fn host_page_size(host: MachPort, page_size: *mut usize) -> KernReturn;
    }

    unsafe {
        let mut buf = [0i32; HOST_VM_INFO64_COUNT as usize];
        let mut count = HOST_VM_INFO64_COUNT;
        let ret = host_statistics64(
            mach_host_self(),
            HOST_VM_INFO64,
            buf.as_mut_ptr(),
            &mut count,
        );
        if ret != KERN_SUCCESS {
            return None;
        }
        let mut page_size: usize = 0;
        let ret = host_page_size(mach_host_self(), &mut page_size);
        if ret != KERN_SUCCESS {
            return None;
        }
        let page_size = page_size as u64;
        // Layout: [0]=free_count, [1]=active_count, [2]=inactive_count (all natural_t = u32)
        Some(MacOSMemInfo {
            free: buf[0] as u32 as u64 * page_size,
            inactive: buf[2] as u32 as u64 * page_size,
        })
    }
}

/// Immediately free system memory on macOS (free pages only).
///
/// This is the conservative metric — memory available WITHOUT reclaiming inactive pages.
/// Use this for variant selection to avoid triggering page reclamation storms that
/// make the system unresponsive.
#[cfg(target_os = "macos")]
pub fn free_system_memory_bytes() -> Option<u64> {
    macos_vm_stats().map(|s| s.free)
}

/// Total available system memory on macOS (free + inactive pages).
///
/// Inactive pages are trivially reclaimable by the OS (no I/O for anonymous pages).
/// Used for both memory budget checks and variant selection on unified-memory systems,
/// where free-only is too conservative (often ~1-2GB on a busy 16GB Mac).
#[cfg(target_os = "macos")]
pub fn available_system_memory_bytes() -> Option<u64> {
    macos_vm_stats().map(|s| s.free + s.inactive)
}

#[cfg(not(target_os = "macos"))]
pub fn free_system_memory_bytes() -> Option<u64> {
    None
}

#[cfg(not(target_os = "macos"))]
pub fn available_system_memory_bytes() -> Option<u64> {
    None
}

// ── Text-encoder retention ───────────────────────────────────────────────────

/// Whether to park text-encoder weights on CPU instead of dropping them after
/// encoding finishes (opt-in via `MOLD_KEEP_TE_RAM=1`).
///
/// Default off for backward compatibility — the existing drop-and-reload path
/// re-mmaps safetensors / re-dequantizes GGUF on every request, costing
/// ~2-4 s per FLUX generation (~1 s on SD3).
///
/// When on, FP16/BF16 encoders survive between requests on host RAM (~9 GB
/// for T5-XXL fp16); only the lightweight GPU↔CPU tensor copy happens between
/// requests. Quantized GGUF encoders fall back to drop-and-reload regardless,
/// because their `QTensor` storage is device-tied and not trivially walkable.
///
/// This mirrors ComfyUI's `text_encoder_offload_device()` behavior
/// (`comfy/model_management.py:1012`).
pub fn keep_te_in_ram() -> bool {
    std::env::var("MOLD_KEEP_TE_RAM")
        .map(|v| v == "1")
        .unwrap_or(false)
}

// ── GPU memory reclamation ───────────────────────────────────────────────────

/// Reclaim GPU memory by resetting the CUDA primary context for the specified device.
///
/// **Must only be called when no CUDA objects (tensors, devices, engines) exist on this device.**
/// This resets CUDA state on the specified GPU: driver context, cuBLAS workspace caches,
/// compiled kernel modules, and memory pools. After calling this, the next
/// `Device::new_cuda(ordinal)` will create a fresh context.
///
/// On non-CUDA platforms, this is a no-op.
#[cfg(feature = "cuda")]
pub fn reclaim_gpu_memory(ordinal: usize) {
    use candle_core::cuda_backend::cudarc::driver::{result, sys};

    debug_assert_ordinal_matches_thread(ordinal, "reclaim_gpu_memory");

    // Synchronize to ensure all async GPU work completes before reset.
    let _ = result::ctx::synchronize();

    // Get the CUdevice handle for the specified GPU ordinal.
    let cu_device = match result::device::get(ordinal as i32) {
        Ok(d) => d,
        Err(e) => {
            tracing::warn!("reclaim_gpu_memory: failed to get device {ordinal}: {e}");
            return;
        }
    };

    // Reset the primary context — frees all allocations, destroys cuBLAS/cuDNN
    // workspace caches, and releases compiled kernel modules.
    let result = unsafe { sys::cuDevicePrimaryCtxReset_v2(cu_device) };
    if result != sys::CUresult::CUDA_SUCCESS {
        tracing::warn!(
            "reclaim_gpu_memory: cuDevicePrimaryCtxReset for device {ordinal} returned {result:?}"
        );
    } else {
        tracing::info!("CUDA primary context reset for device {ordinal}, GPU memory reclaimed");
    }
}

/// No-op on non-CUDA platforms.
#[cfg(not(feature = "cuda"))]
pub fn reclaim_gpu_memory(_ordinal: usize) {}

/// Best-effort CUDA device synchronize, ignoring errors.
///
/// After a `CUDA_ERROR_OUT_OF_MEMORY` the CUDA context may have in-flight work
/// that hasn't been flushed; subsequent allocations can inherit a poisoned
/// scheduler state. Calling synchronize before reporting the OOM and before
/// any retry lets CUDA drain pending work and reset internal queues so the
/// next allocation attempt starts clean.
///
/// Errors are silently swallowed — this is a "best effort" hygiene step, not a
/// hard requirement. The caller has already decided to surface an OOM error;
/// a secondary synchronize failure shouldn't shadow the primary message.
///
/// On non-CUDA platforms this is a no-op.
#[cfg(feature = "cuda")]
pub fn try_synchronize_device(_ordinal: usize) {
    use candle_core::cuda_backend::cudarc::driver::result;
    let _ = result::ctx::synchronize();
}

/// No-op on non-CUDA platforms.
#[cfg(not(feature = "cuda"))]
pub fn try_synchronize_device(_ordinal: usize) {}

// ── VRAM query ───────────────────────────────────────────────────────────────

/// Query free VRAM in bytes for the specified GPU ordinal.
///
/// On CUDA, sets the context to the specified device before querying.
/// On macOS (unified memory), returns available system memory (free + inactive).
/// On other non-CUDA platforms, no VRAM info available.
#[cfg(feature = "cuda")]
pub fn free_vram_bytes(ordinal: usize) -> Option<u64> {
    // Create/bind the device context for the specified ordinal before querying.
    if candle_core::cuda_backend::cudarc::driver::CudaContext::new(ordinal).is_ok() {
        candle_core::cuda_backend::cudarc::driver::result::mem_get_info()
            .ok()
            .map(|(free, _total)| free as u64)
    } else {
        None
    }
}

/// On macOS (unified memory), return available system memory (free + inactive).
///
/// macOS reclaims inactive pages trivially with no I/O, so free-only is too
/// conservative for variant selection — it can reject quantized encoders that
/// would actually fit, forcing a BF16 fallback that doesn't fit either.
/// On other non-CUDA platforms, no VRAM info available.
#[cfg(not(feature = "cuda"))]
pub fn free_vram_bytes(_ordinal: usize) -> Option<u64> {
    available_system_memory_bytes().or_else(free_system_memory_bytes)
}

/// Bytes reserved from VRAM for the OS / desktop / cuBLAS workspace.
///
/// Even on a "headless" GPU some VRAM is always claimed by the driver, the
/// CUDA runtime, and (on Windows) the Desktop Window Manager — querying
/// `cuMemGetInfo` returns a number that the next allocation cannot fully
/// realise. ComfyUI bakes the same constant in (`comfy/model_management.py`):
/// 400 MB on Linux, 600 MB on Windows, plus an extra 100 MB on 16 GB+ cards.
///
/// Default: 400 MB on Linux, 600 MB on Windows, 0 on macOS (Metal unified
/// memory has its own headroom and the OS swap already accounts for desktop
/// pressure). Override via `MOLD_RESERVE_VRAM_MB`.
pub fn reserved_vram_bytes() -> u64 {
    if let Ok(s) = std::env::var("MOLD_RESERVE_VRAM_MB") {
        if let Ok(mb) = s.parse::<u64>() {
            return mb.saturating_mul(1_000_000);
        }
    }
    #[cfg(target_os = "linux")]
    {
        400_000_000
    }
    #[cfg(target_os = "windows")]
    {
        600_000_000
    }
    #[cfg(target_os = "macos")]
    {
        0
    }
    #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
    {
        400_000_000
    }
}

/// Wraps [`free_vram_bytes`] with the OS reserve subtracted.
///
/// Use this for **budget decisions** (does the transformer fit? should we
/// offload? which T5 variant should we pick?). For **diagnostic logging** keep
/// calling [`free_vram_bytes`] so the displayed value matches what the driver
/// reports — otherwise the ComfyUI-style reserve looks like ghost VRAM in
/// `nvidia-smi`.
pub fn usable_free_vram_bytes(ordinal: usize) -> Option<u64> {
    let reserve = reserved_vram_bytes();
    free_vram_bytes(ordinal).map(|free| usable_free_vram_from_raw(free, reserve))
}

fn usable_free_vram_from_raw(free: u64, reserve: u64) -> u64 {
    free.saturating_sub(reserve)
}

/// Total VRAM currently in use (`total - free`) for the specified GPU
/// ordinal. Returns 0 if unavailable.
///
/// This is a **global** device measurement, not a per-model footprint. To
/// estimate the VRAM consumed by loading a model, take the delta between a
/// pre-load baseline and a post-load reading via [`vram_load_delta`].
#[cfg(feature = "cuda")]
pub fn vram_in_use_bytes(ordinal: usize) -> u64 {
    if candle_core::cuda_backend::cudarc::driver::CudaContext::new(ordinal).is_ok() {
        candle_core::cuda_backend::cudarc::driver::result::mem_get_info()
            .ok()
            .map(|(free, total)| total as u64 - free as u64)
            .unwrap_or(0)
    } else {
        0
    }
}

/// Non-CUDA stub — no VRAM tracking available.
#[cfg(not(feature = "cuda"))]
pub fn vram_in_use_bytes(_ordinal: usize) -> u64 {
    0
}

/// Total VRAM (bytes) physically present on the specified GPU ordinal.
///
/// Used by the preflight memory guard to budget against the post-reclaim
/// state: when an existing model is about to be unloaded and the CUDA
/// primary context reset, the *entire* device returns to the OS, so the
/// realistic upper bound is total VRAM, not `free + active_vram`.
///
/// Returns `None` when the device cannot be queried (CUDA disabled, ordinal
/// out of range, driver error). Callers should fall back to a less generous
/// budget in that case rather than treating `None` as unlimited.
#[cfg(feature = "cuda")]
pub fn total_vram_bytes(ordinal: usize) -> Option<u64> {
    if candle_core::cuda_backend::cudarc::driver::CudaContext::new(ordinal).is_ok() {
        candle_core::cuda_backend::cudarc::driver::result::mem_get_info()
            .ok()
            .map(|(_free, total)| total as u64)
    } else {
        None
    }
}

/// Non-CUDA stub — no per-device total VRAM available outside CUDA.
#[cfg(not(feature = "cuda"))]
pub fn total_vram_bytes(_ordinal: usize) -> Option<u64> {
    None
}

/// Bytes loaded onto the GPU since `baseline` was sampled.
///
/// `baseline = vram_in_use_bytes(ordinal)` taken **before** loading a model;
/// this returns `vram_in_use_bytes(ordinal).saturating_sub(baseline)` so the
/// model cache records the new load's per-model footprint, not whatever the
/// device was already using.
pub fn vram_load_delta(ordinal: usize, baseline: u64) -> u64 {
    vram_in_use_bytes(ordinal).saturating_sub(baseline)
}

// ── Formatting ───────────────────────────────────────────────────────────────

// ── Device helpers ───────────────────────────────────────────────────────────

/// Check whether a device is a GPU (CUDA or Metal).
pub(crate) fn is_gpu(device: &candle_core::Device) -> bool {
    device.is_cuda() || device.is_metal()
}

/// Select the optimal compute dtype for GPU inference.
///
/// - CUDA and Metal: BF16 (well-supported by tensor cores / Apple Neural Engine)
/// - CPU: F32
///
/// Note: this is the default compute dtype for model families that support BF16.
/// Some model families (SD1.5, SDXL) prefer F16 — they handle dtype selection
/// in their own pipelines.
#[allow(dead_code)]
pub(crate) fn gpu_compute_dtype(device: &candle_core::Device) -> candle_core::DType {
    if is_gpu(device) {
        candle_core::DType::BF16
    } else {
        candle_core::DType::F32
    }
}

/// Select the optimal dtype for GPU inference (CUDA-only BF16 variant).
///
/// - CUDA: BF16 (well-supported by tensor cores, standard for diffusion)
/// - Metal/MPS: F32 (BF16 on Metal has precision issues that cause washed-out,
///   blurry images — matmul accumulation errors compound through denoising loops.
///   This matches InvokeAI/diffusers which also avoid BF16 on MPS.)
/// - CPU: F32
pub(crate) fn gpu_dtype(device: &candle_core::Device) -> candle_core::DType {
    if device.is_cuda() {
        candle_core::DType::BF16
    } else {
        candle_core::DType::F32
    }
}

/// Resolve the VAE decode dtype for the current generation.
///
/// Reads `MOLD_VAE_DTYPE` to let users force a different precision for the
/// VAE decode pass than the rest of the model. Default (`auto` or unset)
/// preserves the per-pipeline historical choice (typically BF16 on CUDA,
/// F16 on SDXL/SD1.5, F32 on CPU). Forcing `fp32` fixes occasional banding
/// artifacts on FLUX/SD3 finetuned VAEs whose conv weights round badly at
/// half precision; the trade-off is ~2× peak VRAM on the decode step,
/// which `vae_tiling::decode_with_oom_fallback` will absorb by retrying
/// with tiles when the full-tensor decode OOMs.
///
/// Accepted values: `auto` (= unset), `bf16`, `fp16` / `f16`, `fp32` / `f32`.
/// Any other value emits a one-shot warn and falls back to the default —
/// loud enough to surface typos without failing the request.
pub(crate) fn resolve_vae_dtype(default_dtype: candle_core::DType) -> candle_core::DType {
    use candle_core::DType;
    match std::env::var("MOLD_VAE_DTYPE")
        .ok()
        .as_deref()
        .map(str::trim)
    {
        None | Some("") | Some("auto") => default_dtype,
        Some("bf16") | Some("BF16") => DType::BF16,
        Some("fp16") | Some("f16") | Some("FP16") | Some("F16") => DType::F16,
        Some("fp32") | Some("f32") | Some("FP32") | Some("F32") => DType::F32,
        Some(other) => {
            tracing::warn!(
                value = other,
                "MOLD_VAE_DTYPE has unrecognised value; expected one of auto/bf16/fp16/fp32 — falling back to default"
            );
            default_dtype
        }
    }
}

/// Format bytes as a human-readable size (e.g. "11.7 GB").
pub(crate) fn fmt_gb(bytes: u64) -> String {
    format!("{:.1} GB", bytes as f64 / 1_000_000_000.0)
}

// ── Decision functions ───────────────────────────────────────────────────────

/// Determine whether a component should be placed on GPU given free VRAM.
///
/// On Metal (Apple Silicon), always returns true — unified memory means GPU
/// placement is purely a compute performance decision, not a memory one.
/// On CUDA, checks that free discrete VRAM exceeds the threshold.
pub(crate) fn should_use_gpu(
    is_cuda: bool,
    is_metal: bool,
    _free_vram: u64,
    _threshold: u64,
) -> bool {
    if is_metal {
        return true;
    }
    is_cuda && _free_vram > _threshold
}

/// Check if block-level offloading should be auto-enabled.
///
/// Returns true when the transformer + activation headroom won't fit in VRAM
/// but there's enough for a single block + activations (~4GB). This allows
/// streaming blocks one at a time between CPU and GPU.
/// Minimum VRAM needed for one block + activations during offloaded inference.
pub(crate) const MIN_OFFLOAD_VRAM: u64 = 4_000_000_000; // 4 GB
/// Extra workspace needed when a full BF16/FP transformer stays GPU-resident.
///
/// `activation_bytes` covers resolution-scaled tensor peaks. It does not cover
/// CUDA allocator slack, attention workspaces, and short-lived per-layer
/// buffers that only appear once denoising starts. Without this reserve a
/// 23.8 GB FLUX transformer can pass preflight on a 24 GB card, then OOM in
/// the first denoise step before adaptive offload ever gets a chance to run.
pub(crate) const FULL_RESIDENT_RUNTIME_HEADROOM: u64 = 2_000_000_000; // 2 GB

/// Decide whether to enable block-level offloading.
///
/// `activation_bytes` is the per-request activation budget from
/// [`activation_bytes`] — scaled with resolution and dtype. Replaces the
/// previous fixed 3 GB `INFERENCE_HEADROOM` so a 768² generation isn't
/// false-offloaded on a 16 GB card while a 2048² generation isn't
/// under-budgeted on a 24 GB card. Full-resident inference also reserves
/// [`FULL_RESIDENT_RUNTIME_HEADROOM`] because kernels and allocator workspaces
/// are not represented in the safetensors byte count.
pub(crate) fn should_offload(transformer_size: u64, free_vram: u64, activation_bytes: u64) -> bool {
    let needed = transformer_size
        .saturating_add(activation_bytes)
        .saturating_add(FULL_RESIDENT_RUNTIME_HEADROOM);
    free_vram > 0 && needed > free_vram && free_vram >= MIN_OFFLOAD_VRAM
}

/// Check whether a model component fits comfortably in memory.
///
/// On CUDA, checks discrete VRAM against threshold.
/// On Metal, checks the passed `free_vram` (which should be available system
/// memory on unified-memory systems) against threshold.
pub(crate) fn fits_in_memory(
    is_cuda: bool,
    is_metal: bool,
    free_vram: u64,
    threshold: u64,
) -> bool {
    if is_metal {
        if free_vram > 0 {
            return free_vram > threshold;
        }
        // No memory info — assume it fits
        return true;
    }
    is_cuda && free_vram > threshold
}

// ── Memory budget ────────────────────────────────────────────────────────────

/// Estimate peak memory usage for a model given its component file sizes and loading strategy.
///
/// For Eager: sum of all component files + headroom.
/// For Sequential: max(encoder_total, transformer + VAE) + headroom.
///
/// **Single-file convention.** The catalog bridge sets
/// `paths.transformer == paths.vae` to a single `.safetensors` (transformer
/// and VAE keys both extracted from the same file at runtime — see
/// `crates/mold-cli/src/catalog_bridge.rs:196`). Naive `transformer + vae`
/// double-counts that file. We detect and elide it.
pub fn estimate_peak_memory(paths: &mold_core::ModelPaths, strategy: LoadStrategy) -> u64 {
    let file_size = |p: &std::path::Path| std::fs::metadata(p).map(|m| m.len()).unwrap_or(0);
    let same_file = |a: &std::path::Path, b: &std::path::Path| -> bool {
        a == b
            || std::fs::canonicalize(a)
                .ok()
                .zip(std::fs::canonicalize(b).ok())
                .is_some_and(|(a, b)| a == b)
    };
    let path_matches_any = |path: &std::path::Path, paths: &[std::path::PathBuf]| -> bool {
        paths.iter().any(|candidate| same_file(path, candidate))
    };

    let transformer_size = if !paths.transformer_shards.is_empty() {
        paths.transformer_shards.iter().map(|p| file_size(p)).sum()
    } else {
        file_size(&paths.transformer)
    };
    // Single-file: transformer & vae point at the same on-disk file. Keys are
    // extracted from one mmap, so the file's bytes are paged in once. Don't
    // double-count. Use same-file identity rather than path-string equality
    // because catalog resolution can surface equivalent paths through distinct
    // spellings, and shard-backed configs can name the primary file in
    // transformer_shards.
    let vae_is_transformer_file = if paths.transformer_shards.is_empty() {
        same_file(&paths.transformer, &paths.vae)
    } else {
        paths
            .transformer_shards
            .iter()
            .any(|shard| same_file(shard, &paths.vae))
    };
    let vae_is_separate_file = !vae_is_transformer_file;
    let vae_size = if vae_is_separate_file {
        file_size(&paths.vae)
    } else {
        0
    };

    let mut base_component_paths: Vec<std::path::PathBuf> = paths.transformer_shards.to_vec();
    if base_component_paths.is_empty() {
        base_component_paths.push(paths.transformer.clone());
    }
    if vae_is_separate_file {
        base_component_paths.push(paths.vae.clone());
    }

    let mut counted_encoder_paths: Vec<std::path::PathBuf> = Vec::new();
    let mut encoder_size = |path: &std::path::Path| -> u64 {
        if path_matches_any(path, &base_component_paths)
            || path_matches_any(path, &counted_encoder_paths)
        {
            return 0;
        }
        counted_encoder_paths.push(path.to_path_buf());
        file_size(path)
    };

    let t5_size = paths
        .t5_encoder
        .as_ref()
        .map(|p| encoder_size(p))
        .unwrap_or(0);
    let clip_size = paths
        .clip_encoder
        .as_ref()
        .map(|p| encoder_size(p))
        .unwrap_or(0);
    let clip2_size = paths
        .clip_encoder_2
        .as_ref()
        .map(|p| encoder_size(p))
        .unwrap_or(0);
    let text_encoder_size: u64 = paths
        .text_encoder_files
        .iter()
        .map(|p| encoder_size(p))
        .sum();

    let encoder_total = t5_size + clip_size + clip2_size + text_encoder_size;

    match strategy {
        LoadStrategy::Eager => transformer_size + vae_size + encoder_total + MEMORY_BUDGET_HEADROOM,
        LoadStrategy::Sequential => {
            let peak_encoder = encoder_total;
            let peak_inference = transformer_size + vae_size;
            std::cmp::max(peak_encoder, peak_inference) + MEMORY_BUDGET_HEADROOM
        }
    }
}

/// Check whether estimated peak memory fits within available system memory.
///
/// Uses the generous free+inactive metric (can this model run at all?).
/// Returns a warning message if peak memory exceeds 80% of available memory,
/// or `None` if sufficient memory is available (or if memory info is unavailable).
pub fn check_memory_budget(
    paths: &mold_core::ModelPaths,
    strategy: LoadStrategy,
) -> Option<String> {
    let available = available_system_memory_bytes()?;
    let peak = estimate_peak_memory(paths, strategy);
    let threshold = available * 80 / 100;

    if peak > threshold {
        Some(format!(
            "Model needs ~{} but only ~{} available. \
             Consider a smaller quantized variant or close other applications.",
            fmt_gb(peak),
            fmt_gb(available),
        ))
    } else {
        None
    }
}

// ── Pre-flight memory guard ──────────────────────────────────────────────────

/// Check if loading a component of `size_bytes` plus its activation workspace
/// would exceed available system memory.
///
/// `activation_bytes` is the per-request activation budget from
/// [`activation_bytes`] — when the caller has a resolution / family hint use
/// that; otherwise pass `0` and the check degrades to "does the component
/// itself fit", matching the pre-budget behavior.
///
/// Uses available memory (free + inactive/reclaimable) as the primary metric,
/// since macOS moves recently-freed pages to inactive rather than free —
/// those are trivially reclaimable with no I/O. Hard-fails if
/// `size_bytes + activation_bytes` exceeds 90% of available memory; warns
/// (but proceeds) if the same quantity exceeds 2× free memory. On CUDA or
/// when memory info is unavailable, always returns Ok.
pub(crate) fn preflight_memory_check(
    component_name: &str,
    size_bytes: u64,
    activation_bytes: u64,
) -> anyhow::Result<()> {
    // --eager or MOLD_EAGER=1 bypasses the check
    if std::env::var("MOLD_EAGER").is_ok_and(|v| v == "1") {
        return Ok(());
    }

    let available = match available_system_memory_bytes() {
        Some(a) if a > 0 => a,
        _ => return Ok(()), // No info or CUDA — can't check
    };

    let free = free_system_memory_bytes();
    let total = size_bytes.saturating_add(activation_bytes);

    preflight_check_budget(component_name, total, available, free)
}

/// Pure logic for the preflight memory check, factored out for testability.
/// `available` = free + inactive (reclaimable); `free` = free pages only.
///
/// - Hard-fails if `size_bytes > 90%` of available (truly doesn't fit).
/// - Warns if `size_bytes > 2 * free` but within available (page reclamation expected).
fn preflight_check_budget(
    component_name: &str,
    size_bytes: u64,
    available: u64,
    free: Option<u64>,
) -> anyhow::Result<()> {
    // Hard fail: component won't fit even with full page reclamation
    if size_bytes > available * 90 / 100 {
        anyhow::bail!(
            "Not enough memory to load {} ({} needed, {} available).\n\
             Close other applications or use a smaller quantized model.",
            component_name,
            fmt_gb(size_bytes),
            fmt_gb(available),
        );
    }

    // Soft warning: fits in available but may trigger page reclamation
    if let Some(f) = free {
        if size_bytes > f * 2 {
            tracing::warn!(
                "{} ({}) exceeds free memory ({}), will reclaim inactive pages",
                component_name,
                fmt_gb(size_bytes),
                fmt_gb(f),
            );
        }
    }

    Ok(())
}

// ── Memory status reporting ──────────────────────────────────────────────────

/// Return a human-readable memory status string for display.
///
/// On CUDA: "VRAM: X.X GB free"
/// On macOS: "Memory: X.X GB free / Y.Y GB available"
/// Returns None if no memory info is available.
pub fn memory_status_string() -> Option<String> {
    #[cfg(feature = "cuda")]
    {
        if let Some(free) = free_vram_bytes(0) {
            return Some(format!("VRAM: {} free", fmt_gb(free)));
        }
    }
    #[cfg(target_os = "macos")]
    {
        if let Some(stats) = macos_vm_stats() {
            let available = stats.free + stats.inactive;
            return Some(format!(
                "Memory: {} free, {} available",
                fmt_gb(stats.free),
                fmt_gb(available),
            ));
        }
    }
    None
}

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

    // --- fmt_gb tests ---

    #[test]
    fn fmt_gb_zero() {
        assert_eq!(fmt_gb(0), "0.0 GB");
    }

    #[test]
    fn fmt_gb_one_gb() {
        assert_eq!(fmt_gb(1_000_000_000), "1.0 GB");
    }

    #[test]
    fn fmt_gb_fractional() {
        assert_eq!(fmt_gb(14_600_000_000), "14.6 GB");
    }

    #[test]
    fn fmt_gb_small() {
        assert_eq!(fmt_gb(800_000_000), "0.8 GB");
    }

    // --- macOS memory query ---

    #[cfg(target_os = "macos")]
    #[test]
    fn free_system_memory_returns_positive() {
        let mem = free_system_memory_bytes();
        assert!(mem.is_some());
        assert!(mem.unwrap() > 0, "free system memory should be positive");
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn available_includes_inactive() {
        let free = free_system_memory_bytes().unwrap();
        let available = available_system_memory_bytes().unwrap();
        assert!(
            available >= free,
            "available (free+inactive) should be >= free alone"
        );
    }

    // --- free_vram_bytes ---

    #[test]
    fn free_vram_returns_some_on_macos_or_none_on_other() {
        let _result = free_vram_bytes(0);
        #[cfg(target_os = "macos")]
        assert!(_result.is_some(), "macOS should return system memory info");
        #[cfg(not(any(target_os = "macos", feature = "cuda")))]
        assert_eq!(_result, None);
    }

    /// On macOS (unified memory), free_vram_bytes should return available memory
    /// (free + inactive), not just free pages. This ensures variant selection
    /// doesn't reject quantized encoders that would actually fit.
    #[cfg(target_os = "macos")]
    #[test]
    fn free_vram_returns_available_not_just_free_on_macos() {
        let vram = free_vram_bytes(0).unwrap();
        let available = available_system_memory_bytes().unwrap();
        let free = free_system_memory_bytes().unwrap();
        // free_vram_bytes should return available (>= free), not just free
        assert!(
            vram >= free,
            "free_vram_bytes ({vram}) should be >= free_system_memory ({free})"
        );
        // Allow small delta between separate syscalls (TOCTOU: inactive pages may
        // change between the two macos_vm_stats() calls on a busy system)
        let max_drift = 256 * 4096; // 256 pages (~1MB)
        assert!(
            vram.abs_diff(available) < max_drift,
            "free_vram_bytes ({vram}) should approximately equal available_system_memory ({available})"
        );
    }

    // --- should_use_gpu: Metal always GPU ---

    #[test]
    fn metal_always_uses_gpu() {
        assert!(should_use_gpu(false, true, 0, T5_VRAM_THRESHOLD));
        assert!(should_use_gpu(false, true, 1_000, T5_VRAM_THRESHOLD));
        assert!(should_use_gpu(
            false,
            true,
            100_000_000_000,
            T5_VRAM_THRESHOLD
        ));
    }

    // --- fits_in_memory: Metal threshold-based ---

    #[test]
    fn metal_fits_when_enough_free() {
        assert!(fits_in_memory(
            false,
            true,
            20_000_000_000,
            T5_VRAM_THRESHOLD
        ));
    }

    #[test]
    fn metal_does_not_fit_when_free_low() {
        assert!(!fits_in_memory(
            false,
            true,
            2_000_000_000,
            T5_VRAM_THRESHOLD
        ));
    }

    #[test]
    fn metal_fits_fallback_when_no_memory_info() {
        assert!(fits_in_memory(false, true, 0, T5_VRAM_THRESHOLD));
    }

    // --- CUDA threshold tests ---

    #[test]
    fn t5_on_gpu_when_plenty_of_vram() {
        assert!(should_use_gpu(
            true,
            false,
            16_700_000_000,
            T5_VRAM_THRESHOLD
        ));
    }

    #[test]
    fn t5_on_cpu_when_q6_on_24gb() {
        assert!(!should_use_gpu(
            true,
            false,
            14_600_000_000,
            T5_VRAM_THRESHOLD
        ));
    }

    #[test]
    fn t5_on_cpu_when_q8_on_24gb() {
        assert!(!should_use_gpu(
            true,
            false,
            11_700_000_000,
            T5_VRAM_THRESHOLD
        ));
    }

    #[test]
    fn t5_on_cpu_when_bf16_fills_vram() {
        assert!(!should_use_gpu(true, false, 700_000_000, T5_VRAM_THRESHOLD));
    }

    #[test]
    fn t5_on_cpu_when_exactly_at_threshold() {
        assert!(!should_use_gpu(
            true,
            false,
            T5_VRAM_THRESHOLD,
            T5_VRAM_THRESHOLD
        ));
    }

    #[test]
    fn t5_on_cpu_when_no_gpu() {
        assert!(!should_use_gpu(
            false,
            false,
            100_000_000_000,
            T5_VRAM_THRESHOLD
        ));
    }

    #[test]
    fn t5_on_gpu_on_48gb_card() {
        assert!(should_use_gpu(
            true,
            false,
            35_700_000_000,
            T5_VRAM_THRESHOLD
        ));
    }

    #[test]
    fn clip_on_gpu_when_vram_available() {
        assert!(should_use_gpu(
            true,
            false,
            7_500_000_000,
            CLIP_VRAM_THRESHOLD
        ));
    }

    #[test]
    fn clip_on_gpu_with_minimal_vram() {
        assert!(should_use_gpu(
            true,
            false,
            900_000_000,
            CLIP_VRAM_THRESHOLD
        ));
    }

    #[test]
    fn clip_on_cpu_when_vram_tight() {
        assert!(!should_use_gpu(
            true,
            false,
            500_000_000,
            CLIP_VRAM_THRESHOLD
        ));
    }

    // --- Threshold constant sanity checks ---

    #[test]
    fn t5_threshold_accounts_for_headroom() {
        let threshold = std::hint::black_box(T5_VRAM_THRESHOLD);
        assert!(threshold > 9_200_000_000);
        assert!(threshold < 25_000_000_000);
    }

    #[test]
    fn clip_threshold_accounts_for_headroom() {
        let threshold = std::hint::black_box(CLIP_VRAM_THRESHOLD);
        assert!(threshold > 246_000_000);
        assert!(threshold < 2_000_000_000);
    }

    // --- Dynamic T5 threshold tests ---

    #[test]
    fn t5_threshold_for_fp16() {
        let threshold = t5_vram_threshold(9_200_000_000);
        assert!(threshold > 9_200_000_000);
        assert!(threshold <= 16_000_000_000);
    }

    #[test]
    fn t5_threshold_for_q8() {
        let threshold = t5_vram_threshold(5_060_000_000);
        assert_eq!(threshold, 7_060_000_000);
        assert!(should_use_gpu(true, false, 17_000_000_000, threshold));
        assert!(should_use_gpu(true, false, 12_000_000_000, threshold));
    }

    #[test]
    fn t5_threshold_for_q5() {
        let threshold = t5_vram_threshold(3_390_000_000);
        assert_eq!(threshold, 5_390_000_000);
        assert!(should_use_gpu(true, false, 12_000_000_000, threshold));
    }

    #[test]
    fn t5_threshold_for_q3() {
        let threshold = t5_vram_threshold(2_100_000_000);
        assert_eq!(threshold, 4_100_000_000);
    }

    // --- Qwen3 VRAM threshold tests ---

    #[test]
    fn qwen3_fp16_threshold_with_drop_and_reload() {
        assert_eq!(QWEN3_FP16_VRAM_THRESHOLD, 10_200_000_000);
        assert!(should_use_gpu(
            true,
            false,
            17_000_000_000,
            QWEN3_FP16_VRAM_THRESHOLD
        ));
        assert!(should_use_gpu(
            true,
            false,
            19_000_000_000,
            QWEN3_FP16_VRAM_THRESHOLD
        ));
    }

    #[test]
    fn qwen3_threshold_for_q8() {
        let threshold = qwen3_vram_threshold(4_280_000_000);
        assert_eq!(threshold, 6_280_000_000);
        assert!(should_use_gpu(true, false, 17_000_000_000, threshold));
    }

    #[test]
    fn qwen3_threshold_for_q3() {
        let threshold = qwen3_vram_threshold(2_080_000_000);
        assert_eq!(threshold, 4_080_000_000);
        assert!(should_use_gpu(true, false, 5_000_000_000, threshold));
    }

    #[test]
    fn qwen2_threshold_for_q6() {
        let threshold = qwen2_vram_threshold(6_250_000_000);
        assert_eq!(threshold, 8_250_000_000);
        assert!(should_use_gpu(true, false, 12_000_000_000, threshold));
    }

    #[test]
    fn qwen3_fp16_does_not_fit_with_bf16_transformer() {
        assert!(!should_use_gpu(
            true,
            false,
            400_000_000,
            QWEN3_FP16_VRAM_THRESHOLD
        ));
    }

    // --- preflight_check_budget (preflight memory check logic) ---

    const GB: u64 = 1_000_000_000;

    #[test]
    fn budget_ok_when_plenty_of_memory() {
        // 5 GB component, 20 GB available, 10 GB free — no issue
        let result = preflight_check_budget("UNet", 5 * GB, 20 * GB, Some(10 * GB));
        assert!(result.is_ok());
    }

    #[test]
    fn budget_hard_fail_when_exceeds_90pct_available() {
        // 19 GB component, 20 GB available → 19 > 18 (90% of 20) → fail
        let result = preflight_check_budget("UNet", 19 * GB, 20 * GB, Some(GB));
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("Not enough memory"), "got: {msg}");
    }

    #[test]
    fn budget_ok_at_exactly_90pct_available() {
        // 18 GB component, 20 GB available → 18 == 18 (90% of 20) → pass (not >)
        let result = preflight_check_budget("UNet", 18 * GB, 20 * GB, Some(GB));
        assert!(result.is_ok());
    }

    #[test]
    fn budget_hard_fail_just_over_90pct() {
        // Component barely over 90% of available
        let available = 10 * GB;
        let size = available * 90 / 100 + 1; // one byte over
        let result = preflight_check_budget("Transformer", size, available, Some(0));
        assert!(result.is_err());
    }

    #[test]
    fn budget_ok_when_low_free_but_high_available() {
        // The key scenario: 5 GB UNet, only 0.4 GB free, but 18 GB available
        // Old code would bail here; new code proceeds with a warning
        let result = preflight_check_budget("UNet", 5 * GB, 18 * GB, Some(400_000_000));
        assert!(result.is_ok());
    }

    #[test]
    fn budget_ok_with_no_free_info() {
        // free = None (e.g. CUDA), available is sufficient → ok
        let result = preflight_check_budget("UNet", 5 * GB, 20 * GB, None);
        assert!(result.is_ok());
    }

    #[test]
    fn budget_hard_fail_with_no_free_info() {
        // free = None but available too low
        let result = preflight_check_budget("UNet", 19 * GB, 20 * GB, None);
        assert!(result.is_err());
    }

    #[test]
    fn budget_ok_small_component() {
        // Tiny component always fits
        let result = preflight_check_budget("CLIP-L", 250_000_000, 16 * GB, Some(8 * GB));
        assert!(result.is_ok());
    }

    #[test]
    fn budget_error_message_includes_component_name() {
        let result = preflight_check_budget("MyModel", 19 * GB, 20 * GB, Some(GB));
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("MyModel"),
            "error should mention component name"
        );
    }

    #[test]
    fn budget_error_message_includes_sizes() {
        let result = preflight_check_budget("UNet", 19 * GB, 20 * GB, Some(GB));
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("19.0 GB"), "should show needed size");
        assert!(msg.contains("20.0 GB"), "should show available size");
    }

    // ── should_offload tests ─────────────────────────────────────────────

    /// 1024² FLUX bf16 cfg=1 activation budget — used as a default in
    /// existing offload tests so resolution scaling is exercised in the
    /// dedicated `should_offload_uses_resolution_scaled_activation` test.
    fn flux_1024_activation() -> u64 {
        activation_bytes(1024, 1024, 1, 2, ActivationFamily::FluxDit)
    }

    #[test]
    fn offload_when_transformer_exceeds_vram() {
        // 24GB transformer, 16GB free → needs offloading
        assert!(should_offload(24 * GB, 16 * GB, flux_1024_activation()));
    }

    #[test]
    fn offload_when_transformer_fits_but_no_headroom() {
        // 23.8GB transformer on 24.5GB usable free: the file plus activations
        // appears to fit, but runtime workspace does not. This is the 24GB
        // CUDA-card FLUX BF16 regression: without resident-runtime headroom,
        // full load succeeds and denoising OOMs before adaptive offload starts.
        let xformer = 23_800_000_000;
        let free = 24_500_000_000;
        assert!(should_offload(xformer, free, flux_1024_activation()));
    }

    #[test]
    fn no_offload_when_plenty_of_vram() {
        // 12GB transformer on 24GB free → plenty of room
        assert!(!should_offload(12 * GB, 24 * GB, flux_1024_activation()));
    }

    #[test]
    fn no_offload_when_vram_unknown() {
        // free = 0 means we couldn't query VRAM
        assert!(!should_offload(24 * GB, 0, flux_1024_activation()));
    }

    #[test]
    fn no_offload_when_vram_too_small_for_single_block() {
        // 24GB transformer but only 2GB free — not enough for even one block
        assert!(!should_offload(24 * GB, 2 * GB, flux_1024_activation()));
    }

    // ── activation_bytes tests ─────────────────────────────────────────

    /// Doubling each axis quadruples the area, so the activation budget
    /// should also quadruple (modulo the floor) — the core scaling property
    /// that fixed-headroom missed.
    #[test]
    fn activation_bytes_scales_with_area() {
        let small = activation_bytes(1024, 1024, 1, 2, ActivationFamily::FluxDit);
        let big = activation_bytes(2048, 2048, 1, 2, ActivationFamily::FluxDit);
        // Both must be above the floor for the scaling to be observable.
        assert!(
            small > 256_000_000,
            "1024² FLUX bf16 should clear the floor, got {small}"
        );
        let ratio = big as f64 / small as f64;
        assert!(
            (ratio - 4.0).abs() < 0.04,
            "expected 4× scaling, got {ratio:.4} (small={small}, big={big})"
        );
    }

    /// bf16 (dtype_bytes=2) should produce exactly half the budget of f32
    /// (dtype_bytes=4) at the same resolution — both above the floor.
    #[test]
    fn activation_bytes_scales_with_dtype() {
        // Pick a resolution large enough that both dtype variants clear the floor.
        let bf16 = activation_bytes(2048, 2048, 1, 2, ActivationFamily::FluxDit);
        let f32 = activation_bytes(2048, 2048, 1, 4, ActivationFamily::FluxDit);
        assert!(bf16 > 256_000_000, "2048² FLUX bf16 should clear floor");
        let ratio = f32 as f64 / bf16 as f64;
        assert!(
            (ratio - 2.0).abs() < 0.02,
            "expected f32 = 2× bf16, got {ratio:.4} (bf16={bf16}, f32={f32})"
        );
    }

    /// CFG-style batch=2 should double the budget vs non-CFG batch=1.
    #[test]
    fn activation_bytes_scales_with_batch() {
        let b1 = activation_bytes(2048, 2048, 1, 2, ActivationFamily::SdxlUnet);
        let b2 = activation_bytes(2048, 2048, 2, 2, ActivationFamily::SdxlUnet);
        assert!(b1 > 256_000_000, "2048² SDXL bf16 b=1 should clear floor");
        let ratio = b2 as f64 / b1 as f64;
        assert!(
            (ratio - 2.0).abs() < 0.02,
            "expected b=2 → 2× b=1, got {ratio:.4} (b1={b1}, b2={b2})"
        );
    }

    /// Tiny inputs return at least 256 MB (kernel-workspace floor).
    #[test]
    fn activation_bytes_floors_at_256mb() {
        let tiny = activation_bytes(64, 64, 1, 2, ActivationFamily::FluxDit);
        assert_eq!(
            tiny, 256_000_000,
            "tiny input must hit the 256 MB floor exactly, got {tiny}"
        );
        // SmallTransformer family with same inputs should also floor.
        let tiny_te = activation_bytes(64, 64, 1, 2, ActivationFamily::SmallTransformer);
        assert_eq!(tiny_te, 256_000_000);
    }

    /// 1024² FLUX bf16 cfg=1 must land in the [200 MB, 1 GB] sanity band — if
    /// this drifts, recalibrate the FLUX dit factor *and* update the comment
    /// in `activation_bytes` so the empirical anchor stays trustworthy.
    #[test]
    fn activation_bytes_flux_dit_at_1024_is_in_expected_range() {
        let budget = activation_bytes(1024, 1024, 1, 2, ActivationFamily::FluxDit);
        assert!(
            (200_000_000..=1_000_000_000).contains(&budget),
            "FLUX 1024² bf16 cfg=1 budget {budget} bytes outside [200 MB, 1 GB]"
        );
    }

    /// At 2048² the activation budget should be substantially higher than at
    /// 768², so the same `(transformer, free_vram)` pair flips into "offload"
    /// at the larger resolution. This is the regression that motivated the
    /// switch from the fixed 3 GB headroom: under the old constant the
    /// transformer alone would have already triggered offload (or not) the
    /// same way at both resolutions.
    #[test]
    fn should_offload_uses_resolution_scaled_activation() {
        // Pick a transformer + VRAM pair where the answer still depends on
        // resolution after the fixed resident-runtime reserve is included.
        // 22 GB transformer on 24.5 GB usable free: 768² fits; 2048² does not.
        // The old fixed 3 GB inference headroom would have triggered offload
        // at both resolutions even though only the larger one needs it.
        let xformer = 22_000_000_000;
        let free = 24_500_000_000;
        let act_768 = activation_bytes(768, 768, 1, 2, ActivationFamily::FluxDit);
        let act_2048 = activation_bytes(2048, 2048, 1, 2, ActivationFamily::FluxDit);
        assert!(act_2048 > act_768, "2048² must exceed 768²");
        assert!(
            !should_offload(xformer, free, act_768),
            "small budget must NOT trigger offload at this VRAM (act_768={act_768})"
        );
        assert!(
            should_offload(xformer, free, act_2048),
            "large budget MUST trigger offload at this VRAM (act_2048={act_2048})"
        );
    }

    /// `activation_family_for` maps manifest slugs to the right enum and
    /// defaults unknown slugs to the FLUX-dit factor.
    #[test]
    fn activation_family_for_maps_known_and_falls_back() {
        assert_eq!(activation_family_for("flux"), ActivationFamily::FluxDit);
        assert_eq!(activation_family_for("sdxl"), ActivationFamily::SdxlUnet);
        assert_eq!(
            activation_family_for("qwen-image"),
            ActivationFamily::QwenImageDit
        );
        assert_eq!(
            activation_family_for("wuerstchen"),
            ActivationFamily::Wuerstchen
        );
        // Unknown slug — falls back to FluxDit.
        assert_eq!(
            activation_family_for("bogus-family"),
            ActivationFamily::FluxDit
        );
    }

    /// `dtype_bytes` returns the right element width for activation budget math.
    #[test]
    fn dtype_bytes_matches_runtime() {
        use candle_core::DType;
        assert_eq!(dtype_bytes(DType::BF16), 2);
        assert_eq!(dtype_bytes(DType::F16), 2);
        assert_eq!(dtype_bytes(DType::F32), 4);
        assert_eq!(dtype_bytes(DType::F64), 8);
        // Quantized / int storage flows as bf16 activations.
        assert_eq!(dtype_bytes(DType::U8), 2);
    }

    // ── select_expand_device tests ─────────────────────────────────────────

    fn gpu(ordinal: usize, free_gb: u64) -> DiscoveredGpu {
        DiscoveredGpu {
            ordinal,
            name: format!("gpu{ordinal}"),
            total_vram_bytes: 24 * GB,
            free_vram_bytes: free_gb * GB,
        }
    }

    #[test]
    fn expand_picks_main_gpu_when_it_fits() {
        let gpus = vec![gpu(0, 20), gpu(1, 20)];
        assert_eq!(
            select_expand_device(&gpus, 3 * GB, false),
            ExpandPlacement::Gpu(0),
        );
    }

    #[test]
    fn expand_falls_through_to_second_gpu_when_main_full() {
        let gpus = vec![gpu(0, 1), gpu(1, 10)];
        assert_eq!(
            select_expand_device(&gpus, 3 * GB, false),
            ExpandPlacement::Gpu(1),
        );
    }

    #[test]
    fn expand_walks_all_gpus_in_ordinal_order() {
        // GPU 1 also full, GPU 2 fits — should reach ordinal 2
        let gpus = vec![gpu(0, 1), gpu(1, 2), gpu(2, 10)];
        assert_eq!(
            select_expand_device(&gpus, 3 * GB, false),
            ExpandPlacement::Gpu(2),
        );
    }

    #[test]
    fn expand_falls_back_to_cpu_when_no_gpu_fits() {
        let gpus = vec![gpu(0, 1), gpu(1, 2)];
        assert_eq!(
            select_expand_device(&gpus, 3 * GB, false),
            ExpandPlacement::Cpu,
        );
    }

    #[test]
    fn expand_falls_back_to_cpu_when_no_gpus_discovered() {
        let gpus: Vec<DiscoveredGpu> = vec![];
        assert_eq!(
            select_expand_device(&gpus, 3 * GB, false),
            ExpandPlacement::Cpu,
        );
    }

    #[test]
    fn expand_metal_always_picks_gpu_0_when_present() {
        // Metal: unified memory, VRAM threshold doesn't gate — RAM preflight does.
        let gpus = vec![gpu(0, 0)];
        assert_eq!(
            select_expand_device(&gpus, 100 * GB, true),
            ExpandPlacement::Gpu(0),
        );
    }

    #[test]
    fn expand_metal_with_no_gpus_goes_to_cpu() {
        let gpus: Vec<DiscoveredGpu> = vec![];
        assert_eq!(
            select_expand_device(&gpus, 3 * GB, true),
            ExpandPlacement::Cpu,
        );
    }

    #[test]
    fn expand_threshold_sums_weights_and_headroom() {
        // 4 GB q8 model → 4 + 2 = 6 GB threshold
        assert_eq!(expand_vram_threshold(4 * GB), 6 * GB);
        // 1.3 GB q4 model → 1.3 + 2 = 3.3 GB
        assert_eq!(
            expand_vram_threshold(1_300_000_000),
            1_300_000_000 + EXPAND_ACTIVATION_HEADROOM,
        );
    }

    #[test]
    fn expand_strictly_greater_than_threshold() {
        // free_vram must exceed threshold (strict >), not just equal it —
        // matches should_use_gpu's convention so an exactly-fitting model
        // still leaves no room for OS overhead.
        let gpus = vec![gpu(0, 3)]; // exactly 3 GB free
        assert_eq!(
            select_expand_device(&gpus, 3 * GB, false),
            ExpandPlacement::Cpu,
        );
    }

    #[test]
    fn expand_prefers_requested_gpu_when_it_fits() {
        let gpus = vec![gpu(0, 20), gpu(1, 20)];
        assert_eq!(
            select_expand_device_with_preference(&gpus, 3 * GB, false, Some(1)),
            ExpandPlacement::Gpu(1),
        );
    }

    #[test]
    fn expand_preference_falls_back_when_requested_gpu_cannot_fit() {
        let gpus = vec![gpu(0, 20), gpu(1, 1)];
        assert_eq!(
            select_expand_device_with_preference(&gpus, 3 * GB, false, Some(1)),
            ExpandPlacement::Gpu(0),
        );
    }

    // ── select_ltx2_gemma_device ─────────────────────────────────────────

    /// Single GPU with room: encoder lands on the active GPU. Mirrors a
    /// 2× 3090 host where one card is busy with another model and the
    /// other has 24+ GB free for Gemma.
    #[test]
    fn select_ltx2_gemma_device_picks_active_gpu_when_room() {
        let gpus = vec![gpu(0, 25)];
        assert_eq!(
            select_ltx2_gemma_device(&gpus, 0, 24 * GB),
            LtxGemmaPlacement::Gpu(0),
        );
    }

    /// Single 24 GB card already streaming a 22B LTX-2 transformer: only
    /// ~17 GB free, doesn't clear the 24 GB Gemma threshold, so the encoder
    /// must land on CPU instead of OOMing.
    #[test]
    fn select_ltx2_gemma_device_falls_to_cpu_when_no_gpu_fits() {
        let gpus = vec![gpu(0, 17)];
        assert_eq!(
            select_ltx2_gemma_device(&gpus, 0, 24 * GB),
            LtxGemmaPlacement::Cpu,
        );
    }

    /// Multi-GPU host: the active GPU is full (4 GB free), but a sibling
    /// GPU has plenty of room. Encoder runs there and pays a single
    /// cross-device copy at encode time.
    #[test]
    fn select_ltx2_gemma_device_picks_sibling_gpu_when_active_full() {
        let gpus = vec![gpu(0, 4), gpu(1, 25)];
        assert_eq!(
            select_ltx2_gemma_device(&gpus, 0, 24 * GB),
            LtxGemmaPlacement::Gpu(1),
        );
    }

    /// Three-GPU walk: the active GPU is GPU 1; both GPU 0 and GPU 2 have
    /// room. The walk picks the first sibling in ordinal order (GPU 0)
    /// rather than starting from `active_ordinal`.
    #[test]
    fn select_ltx2_gemma_device_walks_remaining_in_ordinal_order() {
        let gpus = vec![gpu(0, 25), gpu(1, 4), gpu(2, 25)];
        assert_eq!(
            select_ltx2_gemma_device(&gpus, 1, 24 * GB),
            LtxGemmaPlacement::Gpu(0),
        );
    }

    #[test]
    fn select_ltx2_gemma_device_returns_cpu_when_no_gpus_discovered() {
        let gpus: Vec<DiscoveredGpu> = vec![];
        assert_eq!(
            select_ltx2_gemma_device(&gpus, 0, 24 * GB),
            LtxGemmaPlacement::Cpu,
        );
    }

    /// `LTX2_GEMMA_VRAM_THRESHOLD` is the headline knob; pin its bytes so
    /// edits go through the constant rather than scattering literal 24-GB
    /// figures across call sites.
    #[test]
    fn ltx2_gemma_vram_threshold_is_24gb() {
        assert_eq!(LTX2_GEMMA_VRAM_THRESHOLD, 24_000_000_000);
    }

    // ── resolve_ltx2_gemma_device_override ───────────────────────────────

    /// All `MOLD_LTX2_GEMMA_DEVICE` / `MOLD_LTX2_DEBUG_FORCE_CPU_PROMPT_ENCODER`
    /// env-var behaviors live under one `#[test]` to serialize access to the
    /// shared process-global env vars (cargo's parallel runner can't race
    /// between `set_var`/`remove_var` of two adjacent tests).
    #[test]
    fn resolve_ltx2_gemma_device_override_env_behaviors() {
        // Snapshot then clear both vars so we start from a known state.
        let prior_main = std::env::var_os("MOLD_LTX2_GEMMA_DEVICE");
        let prior_legacy = std::env::var_os("MOLD_LTX2_DEBUG_FORCE_CPU_PROMPT_ENCODER");
        unsafe {
            std::env::remove_var("MOLD_LTX2_GEMMA_DEVICE");
            std::env::remove_var("MOLD_LTX2_DEBUG_FORCE_CPU_PROMPT_ENCODER");
        }

        // Unset → None (auto path).
        assert_eq!(resolve_ltx2_gemma_device_override(0), None);

        // Explicit cpu → Cpu.
        unsafe { std::env::set_var("MOLD_LTX2_GEMMA_DEVICE", "cpu") };
        assert_eq!(
            resolve_ltx2_gemma_device_override(0),
            Some(LtxGemmaPlacement::Cpu),
        );

        // Explicit gpu → Gpu(active_ordinal).
        unsafe { std::env::set_var("MOLD_LTX2_GEMMA_DEVICE", "gpu") };
        assert_eq!(
            resolve_ltx2_gemma_device_override(1),
            Some(LtxGemmaPlacement::Gpu(1)),
        );

        // Case-insensitive parse.
        unsafe { std::env::set_var("MOLD_LTX2_GEMMA_DEVICE", "CPU") };
        assert_eq!(
            resolve_ltx2_gemma_device_override(0),
            Some(LtxGemmaPlacement::Cpu),
        );

        // Explicit auto → None (lets the auto-resolver run).
        unsafe { std::env::set_var("MOLD_LTX2_GEMMA_DEVICE", "auto") };
        assert_eq!(resolve_ltx2_gemma_device_override(0), None);

        // Garbage → None + warn (warn isn't asserted; we just confirm None).
        unsafe { std::env::set_var("MOLD_LTX2_GEMMA_DEVICE", "wat") };
        assert_eq!(resolve_ltx2_gemma_device_override(0), None);

        // Legacy alias still pins to CPU when the new var is unset.
        unsafe {
            std::env::remove_var("MOLD_LTX2_GEMMA_DEVICE");
            std::env::set_var("MOLD_LTX2_DEBUG_FORCE_CPU_PROMPT_ENCODER", "1");
        }
        assert_eq!(
            resolve_ltx2_gemma_device_override(0),
            Some(LtxGemmaPlacement::Cpu),
        );

        // New var beats legacy alias (legacy `=1` would say cpu, new var
        // pins to gpu — new wins).
        unsafe { std::env::set_var("MOLD_LTX2_GEMMA_DEVICE", "gpu") };
        assert_eq!(
            resolve_ltx2_gemma_device_override(2),
            Some(LtxGemmaPlacement::Gpu(2)),
        );

        // Restore.
        unsafe {
            std::env::remove_var("MOLD_LTX2_GEMMA_DEVICE");
            std::env::remove_var("MOLD_LTX2_DEBUG_FORCE_CPU_PROMPT_ENCODER");
            if let Some(v) = prior_main {
                std::env::set_var("MOLD_LTX2_GEMMA_DEVICE", v);
            }
            if let Some(v) = prior_legacy {
                std::env::set_var("MOLD_LTX2_DEBUG_FORCE_CPU_PROMPT_ENCODER", v);
            }
        }
    }

    // ── keep_te_in_ram ───────────────────────────────────────────────────

    /// `MOLD_KEEP_TE_RAM` defaults to off so the existing drop-and-reload
    /// behavior is preserved when the env var is absent.
    #[test]
    fn test_keep_te_in_ram_env_behaviors() {
        unsafe { std::env::remove_var("MOLD_KEEP_TE_RAM") };
        assert!(!keep_te_in_ram(), "missing var must be off");

        unsafe { std::env::set_var("MOLD_KEEP_TE_RAM", "1") };
        assert!(keep_te_in_ram(), "\"1\" must enable park");

        for v in ["", "0", "true", "yes", "TRUE"] {
            unsafe { std::env::set_var("MOLD_KEEP_TE_RAM", v) };
            assert!(
                !keep_te_in_ram(),
                "value {v:?} must not enable park (helper is strict ==\"1\")"
            );
        }
        unsafe { std::env::remove_var("MOLD_KEEP_TE_RAM") };
    }

    // ── reserved_vram_bytes / usable_free_vram_bytes ─────────────────────

    /// All `MOLD_RESERVE_VRAM_MB` env-var behaviors live under one `#[test]`
    /// to serialize access to the shared process-global env var.
    /// Combined into a single `#[test]` so cargo's parallel runner can't race
    /// between the `set_var`/`remove_var` of `test_reserved_vram_env_behaviors`
    /// and the `remove_var` of `test_usable_free_vram_bytes_subtracts_reserve`.
    /// (Two parallel tests touching the same env var caused intermittent
    /// failures where one test's set_var was clobbered before its assertion.)
    #[test]
    fn test_reserved_vram_and_usable_free_vram() {
        // ── Part 1: reserved_vram_bytes env behavior ────────────────────
        unsafe { std::env::remove_var("MOLD_RESERVE_VRAM_MB") };

        let default = reserved_vram_bytes();
        #[cfg(target_os = "linux")]
        assert_eq!(default, 400_000_000, "Linux default reserve = 400 MB");
        #[cfg(target_os = "macos")]
        assert_eq!(default, 0, "macOS default = 0 (Metal unified memory)");

        unsafe { std::env::set_var("MOLD_RESERVE_VRAM_MB", "1024") };
        assert_eq!(reserved_vram_bytes(), 1_024_000_000);

        unsafe { std::env::set_var("MOLD_RESERVE_VRAM_MB", "0") };
        assert_eq!(reserved_vram_bytes(), 0);

        for v in ["", "abc", "-1"] {
            unsafe { std::env::set_var("MOLD_RESERVE_VRAM_MB", v) };
            assert_eq!(
                reserved_vram_bytes(),
                default,
                "unparseable {v:?} must fall back to default"
            );
        }

        unsafe { std::env::remove_var("MOLD_RESERVE_VRAM_MB") };

        // ── Part 2: usable_free_vram_bytes wrapper ──────────────────────
        assert_eq!(usable_free_vram_from_raw(1_500, 500), 1_000);
        assert_eq!(usable_free_vram_from_raw(500, 1_500), 0);

        unsafe { std::env::set_var("MOLD_RESERVE_VRAM_MB", u64::MAX.to_string()) };
        let has_raw_reading = free_vram_bytes(0).is_some();
        let usable = usable_free_vram_bytes(0);
        assert_eq!(
            usable.is_some(),
            has_raw_reading,
            "usable_free_vram_bytes must mirror free_vram_bytes presence"
        );
        if has_raw_reading {
            assert_eq!(usable, Some(0));
        }

        unsafe { std::env::remove_var("MOLD_RESERVE_VRAM_MB") };
    }

    // ── vram_load_delta ──────────────────────────────────────────────────

    /// `vram_load_delta` must be a pure `saturating_sub` against the
    /// post-load reading. When the device has no CUDA (the test environment),
    /// `vram_in_use_bytes` returns 0, so the delta is always 0 — but the
    /// function shape (saturating_sub, not panic on underflow) must hold
    /// regardless of the live reading.
    #[test]
    fn vram_load_delta_is_saturating_sub() {
        // Without CUDA, vram_in_use_bytes(0) == 0, and 0.saturating_sub(N) == 0
        // for any N. This locks in the saturating semantic — a flaky reading
        // (post < pre) must never panic or wrap.
        assert_eq!(vram_load_delta(0, 0), 0);
        assert_eq!(vram_load_delta(0, 1_000_000_000), 0);
        assert_eq!(vram_load_delta(0, u64::MAX), 0);
    }

    // --- estimate_peak_memory: single-file convention must not double-count ---

    fn write_dummy_file(dir: &std::path::Path, name: &str, size: u64) -> std::path::PathBuf {
        let p = dir.join(name);
        let f = std::fs::File::create(&p).expect("create dummy");
        f.set_len(size).expect("set_len");
        p
    }

    #[test]
    fn estimate_peak_memory_single_file_does_not_double_count_vae() {
        // Civitai single-file convention: ModelPaths::transformer == ModelPaths::vae,
        // both pointing at the primary .safetensors. Naive math would compute
        // transformer_size + vae_size twice for the same on-disk bytes — exactly
        // the bug behind the misleading "94 GB needed" preflight error.
        let dir = tempfile::tempdir().expect("tempdir");
        let single = write_dummy_file(dir.path(), "single.safetensors", 44_000_000_000);
        let te = write_dummy_file(dir.path(), "te.safetensors", 24_000_000_000);
        let paths = mold_core::ModelPaths {
            transformer: single.clone(),
            transformer_shards: vec![],
            vae: single, // Same file as transformer — single-file path
            spatial_upscaler: None,
            temporal_upscaler: None,
            distilled_lora: None,
            t5_encoder: None,
            clip_encoder: None,
            t5_tokenizer: None,
            clip_tokenizer: None,
            clip_encoder_2: None,
            clip_tokenizer_2: None,
            text_encoder_files: vec![te],
            text_tokenizer: None,
            decoder: None,
        };
        let peak = estimate_peak_memory(&paths, LoadStrategy::Sequential);
        // Sequential = max(encoders, transformer + vae[==transformer dedup'd]) + headroom
        //            = max(24 GB, 44 GB) + 2 GB = 46 GB
        // NOT 24 + 44 + 44 + 2 = 114 GB (the double-count bug).
        let peak_gb = peak as f64 / 1e9;
        assert!(
            peak_gb < 50.0,
            "single-file peak should be ~46 GB, got {peak_gb:.1} GB (double-count bug returned)"
        );
        assert!(
            peak_gb > 45.0,
            "single-file peak should be ~46 GB, got {peak_gb:.1} GB"
        );
    }

    #[test]
    fn estimate_peak_memory_separate_vae_file_still_sums() {
        // Sanity: when transformer and vae are distinct files (e.g. FLUX with
        // separate vae companion), both file sizes contribute as before.
        let dir = tempfile::tempdir().expect("tempdir");
        let transformer = write_dummy_file(dir.path(), "tx.safetensors", 4_000_000_000);
        let vae = write_dummy_file(dir.path(), "vae.safetensors", 1_000_000_000);
        let te = write_dummy_file(dir.path(), "te.safetensors", 9_000_000_000);
        let paths = mold_core::ModelPaths {
            transformer,
            transformer_shards: vec![],
            vae,
            spatial_upscaler: None,
            temporal_upscaler: None,
            distilled_lora: None,
            t5_encoder: Some(te),
            clip_encoder: None,
            t5_tokenizer: None,
            clip_tokenizer: None,
            clip_encoder_2: None,
            clip_tokenizer_2: None,
            text_encoder_files: vec![],
            text_tokenizer: None,
            decoder: None,
        };
        let peak = estimate_peak_memory(&paths, LoadStrategy::Sequential);
        // max(9, 4 + 1) + 2 = 11 GB
        let peak_gb = peak as f64 / 1e9;
        assert!(
            (10.5..11.5).contains(&peak_gb),
            "expected ~11 GB, got {peak_gb:.1} GB"
        );
    }

    #[test]
    fn estimate_peak_memory_sharded_transformer_with_separate_vae_sums() {
        // Multi-shard transformer (e.g. FLUX2 diffusers): shards are listed
        // explicitly and vae is a separate file. The single-file dedup must
        // not fire here.
        let dir = tempfile::tempdir().expect("tempdir");
        let s1 = write_dummy_file(dir.path(), "tx-1.safetensors", 4_000_000_000);
        let s2 = write_dummy_file(dir.path(), "tx-2.safetensors", 4_000_000_000);
        let vae = write_dummy_file(dir.path(), "vae.safetensors", 1_000_000_000);
        let paths = mold_core::ModelPaths {
            transformer: s1.clone(), // primary shard
            transformer_shards: vec![s1, s2],
            vae,
            spatial_upscaler: None,
            temporal_upscaler: None,
            distilled_lora: None,
            t5_encoder: None,
            clip_encoder: None,
            t5_tokenizer: None,
            clip_tokenizer: None,
            clip_encoder_2: None,
            clip_tokenizer_2: None,
            text_encoder_files: vec![],
            text_tokenizer: None,
            decoder: None,
        };
        let peak = estimate_peak_memory(&paths, LoadStrategy::Sequential);
        // max(0, 4+4 + 1) + 2 = 11 GB
        let peak_gb = peak as f64 / 1e9;
        assert!(
            (10.5..11.5).contains(&peak_gb),
            "sharded peak should be ~11 GB, got {peak_gb:.1} GB"
        );
    }

    #[test]
    fn estimate_peak_memory_sharded_single_file_vae_does_not_double_count() {
        // Catalog single-file checkpoints can reach the estimator with the
        // primary checkpoint listed as a transformer shard and as the bundled
        // VAE. That is still one mmap-backed safetensors file, not two GPU
        // resident weight sets.
        let dir = tempfile::tempdir().expect("tempdir");
        let single = write_dummy_file(dir.path(), "single.safetensors", 14_000_000_000);
        let paths = mold_core::ModelPaths {
            transformer: single.clone(),
            transformer_shards: vec![single.clone()],
            vae: single,
            spatial_upscaler: None,
            temporal_upscaler: None,
            distilled_lora: None,
            t5_encoder: None,
            clip_encoder: None,
            t5_tokenizer: None,
            clip_tokenizer: None,
            clip_encoder_2: None,
            clip_tokenizer_2: None,
            text_encoder_files: vec![],
            text_tokenizer: None,
            decoder: None,
        };
        let peak = estimate_peak_memory(&paths, LoadStrategy::Sequential);
        // max(0, 14 GB + deduped VAE) + 2 GB headroom = 16 GB.
        let peak_gb = peak as f64 / 1e9;
        assert!(
            (15.5..16.5).contains(&peak_gb),
            "sharded single-file peak should be ~16 GB, got {peak_gb:.1} GB"
        );
    }

    #[test]
    fn estimate_peak_memory_single_file_sdxl_does_not_count_clip_views_as_full_checkpoints() {
        // Cached Civitai SDXL single-file engines expose a diffusers-shaped
        // ModelPaths view where transformer, VAE, CLIP-L, and CLIP-G all point
        // at the same safetensors file. These are component views into one
        // checkpoint, not four full checkpoint-sized GPU phases.
        let dir = tempfile::tempdir().expect("tempdir");
        let single = write_dummy_file(dir.path(), "single.safetensors", 14_000_000_000);
        let paths = mold_core::ModelPaths {
            transformer: single.clone(),
            transformer_shards: vec![],
            vae: single.clone(),
            spatial_upscaler: None,
            temporal_upscaler: None,
            distilled_lora: None,
            t5_encoder: None,
            clip_encoder: Some(single.clone()),
            t5_tokenizer: None,
            clip_tokenizer: None,
            clip_encoder_2: Some(single),
            clip_tokenizer_2: None,
            text_encoder_files: vec![],
            text_tokenizer: None,
            decoder: None,
        };
        let peak = estimate_peak_memory(&paths, LoadStrategy::Sequential);
        // max(deduped encoders=0, transformer + deduped VAE=14 GB) + 2 GB.
        let peak_gb = peak as f64 / 1e9;
        assert!(
            (15.5..16.5).contains(&peak_gb),
            "single-file SDXL peak should be ~16 GB, got {peak_gb:.1} GB"
        );
    }

    // --- resolve_vae_dtype tests ---
    //
    // MOLD_VAE_DTYPE is process-global; tests serialize via a static mutex
    // (mirrors the MOLD_LONG_PROMPTS / MOLD_CFG_PLUS test pattern elsewhere
    // in the crate).

    fn vae_env_lock() -> std::sync::MutexGuard<'static, ()> {
        use std::sync::{Mutex, OnceLock};
        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| Mutex::new(()))
            .lock()
            .unwrap_or_else(|p| p.into_inner())
    }

    #[test]
    fn resolve_vae_dtype_unset_returns_default() {
        let _g = vae_env_lock();
        // SAFETY: serialized via vae_env_lock to avoid racing parallel tests.
        unsafe { std::env::remove_var("MOLD_VAE_DTYPE") };
        assert_eq!(
            resolve_vae_dtype(candle_core::DType::BF16),
            candle_core::DType::BF16
        );
        assert_eq!(
            resolve_vae_dtype(candle_core::DType::F16),
            candle_core::DType::F16
        );
    }

    #[test]
    fn resolve_vae_dtype_auto_returns_default() {
        let _g = vae_env_lock();
        unsafe { std::env::set_var("MOLD_VAE_DTYPE", "auto") };
        let resolved = resolve_vae_dtype(candle_core::DType::BF16);
        unsafe { std::env::remove_var("MOLD_VAE_DTYPE") };
        assert_eq!(resolved, candle_core::DType::BF16);
    }

    #[test]
    fn resolve_vae_dtype_fp32_forces_f32_regardless_of_default() {
        let _g = vae_env_lock();
        unsafe { std::env::set_var("MOLD_VAE_DTYPE", "fp32") };
        let resolved = resolve_vae_dtype(candle_core::DType::BF16);
        unsafe { std::env::remove_var("MOLD_VAE_DTYPE") };
        assert_eq!(resolved, candle_core::DType::F32);
    }

    #[test]
    fn resolve_vae_dtype_bf16_forces_bf16_even_when_default_is_f32() {
        // CPU default is F32; user opts back into BF16 explicitly. Pins the
        // contract that the env knob can both raise *and* lower precision.
        let _g = vae_env_lock();
        unsafe { std::env::set_var("MOLD_VAE_DTYPE", "bf16") };
        let resolved = resolve_vae_dtype(candle_core::DType::F32);
        unsafe { std::env::remove_var("MOLD_VAE_DTYPE") };
        assert_eq!(resolved, candle_core::DType::BF16);
    }

    #[test]
    fn resolve_vae_dtype_fp16_alias_recognised() {
        // f16 / fp16 / F16 / FP16 must all resolve identically — different
        // tools and shells normalise case differently.
        let _g = vae_env_lock();
        for value in ["fp16", "f16", "FP16", "F16"] {
            unsafe { std::env::set_var("MOLD_VAE_DTYPE", value) };
            let resolved = resolve_vae_dtype(candle_core::DType::BF16);
            assert_eq!(
                resolved,
                candle_core::DType::F16,
                "value `{value}` should resolve to F16"
            );
        }
        unsafe { std::env::remove_var("MOLD_VAE_DTYPE") };
    }

    #[test]
    fn resolve_vae_dtype_invalid_value_falls_back_to_default() {
        let _g = vae_env_lock();
        unsafe { std::env::set_var("MOLD_VAE_DTYPE", "fp64") };
        let resolved = resolve_vae_dtype(candle_core::DType::BF16);
        unsafe { std::env::remove_var("MOLD_VAE_DTYPE") };
        assert_eq!(
            resolved,
            candle_core::DType::BF16,
            "invalid value must fall back, not error"
        );
    }

    /// Pin the `MEMORY_BUDGET_HEADROOM` constant so a future change forces a
    /// matching update to the preflight rejection error message in
    /// `mold-server::model_manager::check_model_memory_budget`, which prints
    /// "with 2 GB activation headroom" in its formatted output.
    #[test]
    fn memory_budget_headroom_is_2gb() {
        assert_eq!(
            MEMORY_BUDGET_HEADROOM, 2_000_000_000,
            "MEMORY_BUDGET_HEADROOM changed — update the rejection error message \
             in mold-server::model_manager::check_model_memory_budget to match"
        );
    }
}