abaddon 0.2.0-rc.2

LLM inference engine - The Destroyer renders judgment
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
//! Core inference engine implementation.

use std::sync::Arc;
use std::time::Instant;

use async_trait::async_trait;
use candle_core::{DType, Device, IndexOp, Tensor};
use candle_nn::VarBuilder;
use infernum_core::{
    EmbedRequest, EmbedResponse, GenerateRequest, GenerateResponse, Message, ModelMetadata, Result,
    Role, SamplingParams, TokenStream,
};
use parking_lot::Mutex;
use tokio::sync::mpsc;

#[cfg(feature = "cuda")]
use crate::backend::to_candle_dtype;
use crate::config::EngineConfig;
#[cfg(feature = "cuda")]
use crate::cuda_inference::{Generator as CudaGenerator, WeightStore as CudaWeightStore};
use crate::loader::{ModelConfig, ModelFiles, ModelLoader, WeightFiles};
use crate::models::llama::{Llama, LlamaConfig};
use crate::models::qwen2::{Qwen2, Qwen2Config};
use crate::models::{ArchitectureType, ModelKind};
use crate::sampler::Sampler;
use crate::speculative::SpeculativeDecoder;
use crate::tokenizer::Tokenizer;

/// Trait defining the inference engine interface.
#[async_trait]
pub trait InferenceEngine: Send + Sync {
    /// Generates text from the given request.
    async fn generate(&self, request: GenerateRequest) -> Result<GenerateResponse>;

    /// Generates text for a batch of requests.
    ///
    /// The default implementation processes requests concurrently using async parallelism.
    /// Implementations may override this to provide true batched GPU inference.
    async fn generate_batch(
        &self,
        requests: Vec<GenerateRequest>,
    ) -> Vec<Result<GenerateResponse>> {
        use futures::future::join_all;

        join_all(requests.into_iter().map(|req| self.generate(req))).await
    }

    /// Generates text with streaming output.
    async fn generate_stream(&self, request: GenerateRequest) -> Result<TokenStream>;

    /// Generates embeddings from the given request.
    async fn embed(&self, request: EmbedRequest) -> Result<EmbedResponse>;

    /// Returns metadata about the loaded model.
    fn model_info(&self) -> &ModelMetadata;

    /// Returns true if the engine is ready for inference.
    fn is_ready(&self) -> bool;
}

/// Loaded model state (wrapped in Arc for streaming support).
struct LoadedModel {
    model: Mutex<ModelKind>,
    tokenizer: Tokenizer,
    /// EOS token ID for generation termination.
    eos_token_id: u32,
}

/// The main inference engine.
pub struct Engine {
    config: EngineConfig,
    metadata: ModelMetadata,
    loaded: Option<Arc<LoadedModel>>,
    device: Device,
    #[allow(dead_code)] // Used for model quantization in future
    dtype: DType,
    /// Optional speculative decoder for accelerated inference.
    speculative_decoder: Option<Arc<SpeculativeDecoder>>,
    /// Optional CUDA-optimized generator for HoloTensor models.
    /// When available, bypasses Candle for 5-6x faster inference.
    #[cfg(feature = "cuda")]
    cuda_generator: Option<Mutex<CudaGenerator>>,
}

impl Engine {
    /// Creates a new engine with the given configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the model cannot be loaded.
    pub async fn new(config: EngineConfig) -> Result<Self> {
        tracing::info!("Initializing Abaddon inference engine");
        tracing::debug!(?config, "Engine configuration");

        // Determine device
        let (device, device_info) = Self::select_device(&config)?;
        tracing::info!(device = ?device, "Using compute device");

        // Print device info prominently for user visibility
        eprintln!("\x1b[1mCompute Backend:\x1b[0m {}", device_info);

        // Determine dtype based on device capabilities
        let dtype = Self::select_dtype(&device, &config)?;

        let dtype_name = match dtype {
            DType::F32 => "F32",
            DType::F16 => "F16 (tensor cores)",
            DType::BF16 => "BF16 (tensor cores)",
            _ => "other",
        };
        tracing::info!(dtype = dtype_name, "Selected inference precision");

        // Load model files
        let loader = ModelLoader::default_cache()?;
        let files = loader.resolve(&config.model)?;

        // Load model config
        let model_config = ModelConfig::from_file(&files.config)?;
        tracing::debug!(?model_config, "Loaded model configuration");

        // Build metadata
        let metadata = Self::build_metadata(&config, &model_config)?;

        // Load the model
        let loaded = Self::load_model(&files, &model_config, &device, dtype)?;

        // Load speculative decoder if configured
        let speculative_decoder = if let Some(spec_config) = &config.speculative {
            tracing::info!("Loading draft model for speculative decoding");
            match Self::load_speculative_decoder(spec_config, &loader, &device, dtype) {
                Ok(decoder) => {
                    tracing::info!(
                        num_speculative_tokens = spec_config.num_speculative_tokens,
                        acceptance_threshold = spec_config.acceptance_threshold,
                        "Speculative decoding enabled"
                    );
                    Some(Arc::new(decoder))
                },
                Err(e) => {
                    tracing::warn!(
                        "Failed to load draft model, disabling speculative decoding: {}",
                        e
                    );
                    None
                },
            }
        } else {
            None
        };

        // Try to initialize CUDA optimized path for HoloTensor models
        #[cfg(feature = "cuda")]
        let cuda_generator = if let WeightFiles::HoloTensor { directory, .. } = &files.weights {
            if matches!(device, Device::Cuda(_)) {
                tracing::info!(
                    "Detected HoloTensor model on CUDA, attempting optimized inference path"
                );
                let max_seq_len = model_config.max_position_embeddings.unwrap_or(4096);
                match Self::init_cuda_generator(directory, max_seq_len) {
                    Some(gen) => {
                        eprintln!(
                            "\x1b[32m✓ CUDA optimized inference enabled (5-6x faster)\x1b[0m"
                        );
                        Some(Mutex::new(gen))
                    },
                    None => {
                        tracing::info!("Falling back to Candle inference path");
                        None
                    },
                }
            } else {
                None
            }
        } else {
            None
        };

        #[cfg(feature = "cuda")]
        let cuda_optimized = cuda_generator.is_some();
        #[cfg(not(feature = "cuda"))]
        let cuda_optimized = false;

        tracing::info!(
            model = %metadata.id,
            layers = model_config.num_hidden_layers,
            speculative = speculative_decoder.is_some(),
            cuda_optimized = cuda_optimized,
            "Engine initialized successfully"
        );

        Ok(Self {
            config,
            metadata,
            loaded: Some(Arc::new(loaded)),
            device,
            dtype,
            speculative_decoder,
            #[cfg(feature = "cuda")]
            cuda_generator,
        })
    }

    /// Tries to initialize the optimized CUDA inference path for HCT models.
    ///
    /// This bypasses Candle's tensor operations for 5-6x faster inference using
    /// fused CUDA kernels, pre-allocated buffers, and Flash Attention.
    ///
    /// Returns the CUDA generator if initialization succeeded.
    #[cfg(feature = "cuda")]
    fn init_cuda_generator(
        hct_directory: &std::path::Path,
        max_seq_len: usize,
    ) -> Option<CudaGenerator> {
        tracing::info!(
            directory = %hct_directory.display(),
            "Attempting to initialize optimized CUDA inference path"
        );

        // Try to load weights using cuda_inference
        let weights = match CudaWeightStore::load_hct(hct_directory, None, 0) {
            Ok(w) => {
                tracing::info!(
                    memory_mb = w.memory_used as f64 / 1024.0 / 1024.0,
                    num_layers = w.layers.len(),
                    "CUDA weights loaded successfully"
                );
                w
            },
            Err(e) => {
                tracing::warn!(
                    error = %e,
                    "Failed to load CUDA weights, falling back to Candle path"
                );
                return None;
            },
        };

        // Create the optimized generator
        match CudaGenerator::new(weights, max_seq_len) {
            Ok(generator) => {
                tracing::info!("CUDA generator initialized successfully");
                Some(generator)
            },
            Err(e) => {
                tracing::warn!(
                    error = %e,
                    "Failed to create CUDA generator, falling back to Candle path"
                );
                None
            },
        }
    }

    /// Loads the speculative decoder with the draft model.
    fn load_speculative_decoder(
        spec_config: &crate::config::SpeculativeConfig,
        loader: &ModelLoader,
        device: &Device,
        dtype: DType,
    ) -> Result<SpeculativeDecoder> {
        // Load draft model files
        let draft_files = loader.resolve(&spec_config.draft_model)?;
        let draft_model_config = ModelConfig::from_file(&draft_files.config)?;

        // Load draft model
        let draft_loaded = Self::load_model(&draft_files, &draft_model_config, device, dtype)?;

        Ok(SpeculativeDecoder::new(
            draft_loaded.model.into_inner(),
            draft_loaded.tokenizer,
            spec_config.clone(),
            device.clone(),
            dtype,
        ))
    }

    /// Selects the compute device based on configuration and availability.
    /// Returns both the device and a human-readable description.
    fn select_device(config: &EngineConfig) -> Result<(Device, String)> {
        use infernum_core::DeviceType;

        match &config.device {
            DeviceType::Cpu => Ok((Device::Cpu, "CPU".to_string())),
            DeviceType::Cuda { device_id } => {
                #[cfg(feature = "cuda")]
                {
                    let device = Device::new_cuda(*device_id).map_err(|e| {
                        infernum_core::Error::Backend {
                            backend: "cuda".to_string(),
                            message: e.to_string(),
                        }
                    })?;
                    Ok((device, format!("CUDA (GPU {})", device_id)))
                }
                #[cfg(not(feature = "cuda"))]
                {
                    let _ = device_id; // Silence unused warning when cuda feature disabled
                    eprintln!("\x1b[33mWarning:\x1b[0m CUDA requested but not compiled in, falling back to CPU");
                    eprintln!("         Rebuild with: cargo build --features cuda");
                    tracing::warn!("CUDA requested but not compiled in, falling back to CPU");
                    Ok((Device::Cpu, "CPU (CUDA unavailable)".to_string()))
                }
            },
            DeviceType::Metal { device_id } => {
                #[cfg(feature = "metal")]
                {
                    let device = Device::new_metal(*device_id).map_err(|e| {
                        infernum_core::Error::Backend {
                            backend: "metal".to_string(),
                            message: e.to_string(),
                        }
                    })?;
                    Ok((device, "Metal (Apple GPU)".to_string()))
                }
                #[cfg(not(feature = "metal"))]
                {
                    let _ = device_id; // Silence unused warning when metal feature disabled
                    eprintln!("\x1b[33mWarning:\x1b[0m Metal requested but not compiled in, falling back to CPU");
                    eprintln!("         Rebuild with: cargo build --features metal");
                    tracing::warn!("Metal requested but not compiled in, falling back to CPU");
                    Ok((Device::Cpu, "CPU (Metal unavailable)".to_string()))
                }
            },
            DeviceType::WebGpu => {
                eprintln!("\x1b[33mWarning:\x1b[0m WebGPU not yet supported, falling back to CPU");
                tracing::warn!("WebGPU not yet supported, falling back to CPU");
                Ok((Device::Cpu, "CPU (WebGPU not yet supported)".to_string()))
            },
        }
    }

    /// Selects the optimal dtype based on device capabilities.
    ///
    /// For CUDA devices with tensor cores (compute >= 7.0), uses FP16 or BF16.
    /// For older CUDA devices, falls back to F32.
    /// Metal and CPU use F16 by default.
    fn select_dtype(device: &Device, config: &EngineConfig) -> Result<DType> {
        // If user explicitly requested a quantization, that takes precedence
        if config.quantization.is_some() {
            // Quantized models typically use F16 for activation
            return Ok(DType::F16);
        }

        match device {
            Device::Cuda(_) => {
                #[cfg(feature = "cuda")]
                {
                    use crate::backend::cuda::CudaDevice;

                    // Query GPU capabilities
                    if let infernum_core::DeviceType::Cuda { device_id } = &config.device {
                        if let Ok(cuda_dev) = CudaDevice::new(*device_id) {
                            let caps = cuda_dev.capabilities();
                            let dtype = caps.recommended_dtype();

                            eprintln!(
                                "\x1b[1mGPU:\x1b[0m Compute {}.{} | {} GB VRAM | Tensor Cores: {} | BF16: {}",
                                caps.compute_major,
                                caps.compute_minor,
                                caps.total_memory / (1024 * 1024 * 1024),
                                if caps.has_tensor_cores { "Yes" } else { "No" },
                                if caps.has_bf16 { "Yes" } else { "No" }
                            );

                            return Ok(to_candle_dtype(dtype));
                        }
                    }
                    // Fallback for capability query failure - assume modern GPU
                    Ok(DType::F16)
                }
                #[cfg(not(feature = "cuda"))]
                {
                    Ok(DType::F32)
                }
            },
            Device::Metal(_) => {
                // Apple Silicon GPUs handle F16 well
                Ok(DType::F16)
            },
            Device::Cpu => {
                // CPU can use F16 with modern SIMD (AVX-512, NEON)
                // but F32 is safer and often faster
                #[cfg(any(feature = "mkl", feature = "accelerate"))]
                {
                    Ok(DType::F32) // MKL/Accelerate optimized for F32
                }
                #[cfg(not(any(feature = "mkl", feature = "accelerate")))]
                {
                    Ok(DType::F16) // Native Candle can use F16
                }
            },
        }
    }

    /// Loads the model from files.
    fn load_model(
        files: &ModelFiles,
        model_config: &ModelConfig,
        device: &Device,
        dtype: DType,
    ) -> Result<LoadedModel> {
        tracing::info!("Loading model weights...");
        let start = Instant::now();

        // Detect architecture from config
        let arch_type = ArchitectureType::detect(
            model_config.model_type.as_deref(),
            model_config.architectures.as_deref(),
        );

        tracing::info!(
            architecture = arch_type.name(),
            "Detected model architecture"
        );

        // Check if this is a HoloTensor progressive load (for 405B+ models)
        if let WeightFiles::HoloTensor {
            directory,
            min_quality,
            target_quality,
            vram_budget,
            ram_budget,
        } = &files.weights
        {
            return Self::load_model_lazy(
                files,
                model_config,
                device,
                dtype,
                directory,
                *min_quality,
                *target_quality,
                *vram_budget,
                *ram_budget,
                arch_type,
            );
        }

        // Standard loading path - load all weights into VarBuilder
        let vb = Self::load_weights(&files.weights, device, dtype)?;

        // Get EOS token ID
        let eos_token_id = model_config.eos_token_ids().first().copied().unwrap_or(2);

        // Load the appropriate model based on architecture
        let model = match arch_type {
            ArchitectureType::Qwen2 => {
                let qwen2_config = Qwen2Config {
                    hidden_size: model_config.hidden_size.unwrap_or(3584),
                    intermediate_size: model_config.intermediate_size.unwrap_or(18944),
                    vocab_size: model_config.vocab_size.unwrap_or(151936),
                    num_hidden_layers: model_config.num_hidden_layers.unwrap_or(28),
                    num_attention_heads: model_config.num_attention_heads.unwrap_or(28),
                    num_key_value_heads: model_config.num_key_value_heads,
                    rms_norm_eps: model_config.rms_norm_eps.unwrap_or(1e-6),
                    rope_theta: model_config.rope_theta.unwrap_or(1000000.0),
                    max_position_embeddings: model_config.max_position_embeddings.unwrap_or(32768),
                    tie_word_embeddings: model_config.tie_word_embeddings.unwrap_or(false),
                    bos_token_id: model_config.bos_token_id,
                    eos_token_id: Some(eos_token_id),
                    use_sliding_window: false,
                    sliding_window: None,
                };

                let qwen2 =
                    Qwen2::load(qwen2_config, vb).map_err(|e| infernum_core::Error::ModelLoad {
                        message: format!("Failed to load Qwen2 model: {}", e),
                    })?;
                ModelKind::Qwen2(qwen2)
            },
            ArchitectureType::Llama | ArchitectureType::Unknown => {
                // Default to Llama for unknown architectures (most compatible)
                if arch_type == ArchitectureType::Unknown {
                    tracing::warn!(
                        "Unknown architecture, defaulting to Llama. \
                         Model may not work correctly."
                    );
                }

                let llama_config = LlamaConfig {
                    hidden_size: model_config.hidden_size.unwrap_or(4096),
                    intermediate_size: model_config.intermediate_size.unwrap_or(11008),
                    vocab_size: model_config.vocab_size.unwrap_or(32000),
                    num_hidden_layers: model_config.num_hidden_layers.unwrap_or(32),
                    num_attention_heads: model_config.num_attention_heads.unwrap_or(32),
                    num_key_value_heads: model_config.num_key_value_heads,
                    rms_norm_eps: model_config.rms_norm_eps.unwrap_or(1e-5),
                    rope_theta: model_config.rope_theta.unwrap_or(10000.0),
                    max_position_embeddings: model_config.max_position_embeddings.unwrap_or(4096),
                    tie_word_embeddings: model_config.tie_word_embeddings.unwrap_or(false),
                    bos_token_id: model_config.bos_token_id,
                    eos_token_id: Some(eos_token_id),
                    rope_scaling: model_config.rope_scaling.clone(),
                };

                let llama =
                    Llama::load(llama_config, vb).map_err(|e| infernum_core::Error::ModelLoad {
                        message: format!("Failed to load Llama model: {}", e),
                    })?;
                ModelKind::Llama(llama)
            },
        };

        // Load tokenizer
        let tokenizer = if let Some(tokenizer_path) = &files.tokenizer {
            Tokenizer::from_file(tokenizer_path)?
        } else {
            return Err(infernum_core::Error::ModelLoad {
                message: "No tokenizer found for model".to_string(),
            });
        };

        let elapsed = start.elapsed();
        tracing::info!(
            elapsed_ms = elapsed.as_millis(),
            "Model loaded successfully"
        );

        Ok(LoadedModel {
            model: Mutex::new(model),
            tokenizer,
            eos_token_id,
        })
    }

    /// Loads a model using lazy layer-by-layer loading for 405B+ models.
    ///
    /// This uses `LazyLlama` or `LazyQwen2` (based on architecture) which loads
    /// decoder layers on-demand during inference, enabling 405B inference on
    /// systems with limited memory (24GB VRAM + 80GB RAM).
    #[allow(clippy::too_many_arguments)]
    fn load_model_lazy(
        files: &ModelFiles,
        model_config: &ModelConfig,
        device: &Device,
        dtype: DType,
        directory: &std::path::Path,
        min_quality: f32,
        target_quality: f32,
        vram_budget: u64,
        ram_budget: u64,
        arch_type: ArchitectureType,
    ) -> Result<LoadedModel> {
        use crate::holotensor::tiered_loading::{TieredConfig, TieredHoloLoader};
        use crate::lazy_varbuilder::LazyVarBuilder;
        use crate::models::{LazyLlama, LazyQwen2, LlamaConfig, Qwen2Config};

        // Check if adaptive tiering is enabled (experimental)
        let use_adaptive = std::env::var("INFERNUM_ADAPTIVE_TIERING")
            .map(|v| v == "1" || v.to_lowercase() == "true")
            .unwrap_or(false);

        if use_adaptive {
            return Self::load_lazy_model_adaptive(
                files,
                model_config,
                device,
                dtype,
                directory,
                vram_budget,
                ram_budget,
                arch_type,
            );
        }

        tracing::info!(
            directory = %directory.display(),
            min_quality = %min_quality,
            target_quality = %target_quality,
            vram_gb = vram_budget / (1024 * 1024 * 1024),
            ram_gb = ram_budget / (1024 * 1024 * 1024),
            "Loading model with lazy layer loading (405B mode)"
        );

        let start = Instant::now();

        // Configure tiered loading
        let tiered_config = TieredConfig {
            vram_budget,
            ram_budget,
            min_quality,
            target_quality,
            enable_background_streaming: true,
            background_streams: 4,
        };

        // Create the tiered loader
        let mut loader = TieredHoloLoader::new(directory, tiered_config, device.clone(), dtype)
            .map_err(|e| infernum_core::Error::ModelLoad {
                message: format!("Failed to create tiered loader: {}", e),
            })?;

        // Enable NVMe cache for decompressed tensors if configured
        // This provides ~1000x speedup on subsequent layer loads (100ms vs 100s)
        if let Ok(cache_dir) = std::env::var("INFERNUM_CACHE_DIR") {
            let cache_path = std::path::Path::new(&cache_dir);
            if cache_path.exists() || std::fs::create_dir_all(cache_path).is_ok() {
                tracing::info!(
                    cache_dir = %cache_dir,
                    "NVMe cache enabled - subsequent layer loads will be ~1000x faster"
                );
                loader = loader.with_safetensors_dir(cache_path);
            } else {
                tracing::warn!(
                    cache_dir = %cache_dir,
                    "Could not create NVMe cache directory, falling back to HCT reconstruction"
                );
            }
        }

        // Start background streaming for quality improvement
        loader.start_background_streaming();

        // Create lazy VarBuilder backed by tiered loader with VRAM-appropriate cache
        let provider: std::sync::Arc<dyn crate::lazy_varbuilder::TensorProvider> =
            std::sync::Arc::new(loader);

        // CRITICAL: Disable LazyVarBuilder cache entirely!
        // TieredHoloLoader already caches tensors in its HashMap.
        // If LazyVarBuilder also caches, we get TRIPLE copies of each tensor:
        //   1. TieredHoloLoader's HashMap (original)
        //   2. LazyVarBuilder's LRU cache (clone)
        //   3. LazyLlama's loaded_layers (clone from LazyVarBuilder)
        // This causes OOM: 7 layers × 2GB × 3 copies = 42GB on 24GB VRAM!
        // Setting max_memory_bytes=0 disables the cache.
        let cache_config = crate::lazy_varbuilder::CacheConfig {
            max_memory_bytes: 0, // Disable cache - TieredHoloLoader already caches
            max_entries: 0,
        };
        tracing::info!("LazyVarBuilder cache disabled - TieredHoloLoader provides caching");
        let lazy_vb = LazyVarBuilder::with_cache_config(
            std::sync::Arc::clone(&provider),
            device.clone(),
            dtype,
            cache_config,
        );

        // Calculate max layers to keep in VRAM based on model dimensions
        // Each layer has: attention (q,k,v,o projections) + MLP (gate, up, down)
        let hidden = model_config.hidden_size.unwrap_or(8192) as u64;
        let intermediate = model_config.intermediate_size.unwrap_or(28672) as u64;
        let dtype_bytes: u64 = match dtype {
            DType::BF16 | DType::F16 => 2,
            DType::F32 => 4,
            _ => 2, // Default to 2 for other types
        };

        // Layer size = attention weights + MLP weights (approximate, ignores GQA savings)
        // Attention: 4 * hidden^2 (q,k,v,o projections)
        // MLP: 3 * hidden * intermediate (gate, up, down projections)
        let layer_size_bytes = (4 * hidden * hidden + 3 * hidden * intermediate) * dtype_bytes;
        let layer_size_gb = (layer_size_bytes as f64 / (1024.0 * 1024.0 * 1024.0)).ceil() as u64;

        // Use VRAM budget (not RAM!) with headroom for fixed allocations
        let vram_budget_gb = vram_budget / (1024 * 1024 * 1024);

        // Calculate headroom for non-layer tensors:
        // - Embeddings: vocab_size * hidden * dtype_bytes (~2GB for 70B)
        // - lm_head: same as embeddings (~2GB for 70B, unless tied)
        // - KV cache: grows with sequence length (~1-4GB typically)
        // - CUDA overhead, activations, gradients (~2-4GB)
        let vocab = model_config.vocab_size.unwrap_or(128256) as u64;
        let embedding_size_gb = (vocab * hidden * dtype_bytes) as f64 / (1024.0 * 1024.0 * 1024.0);
        let lm_head_size_gb = if model_config.tie_word_embeddings.unwrap_or(false) {
            0.0 // Tied embeddings share memory
        } else {
            embedding_size_gb
        };
        let fixed_overhead_gb = 4u64; // KV cache, activations, CUDA overhead
        let headroom_gb = (embedding_size_gb + lm_head_size_gb).ceil() as u64 + fixed_overhead_gb;

        let available_vram_gb = vram_budget_gb.saturating_sub(headroom_gb);

        // Calculate max layers, minimum 1 for basic operation (layer-by-layer streaming)
        let max_loaded_layers = if layer_size_gb > 0 && available_vram_gb > 0 {
            (available_vram_gb / layer_size_gb).max(1) as usize
        } else {
            // Not enough VRAM for even 1 layer - use streaming mode with 1 layer
            tracing::warn!(
                vram_budget_gb = vram_budget_gb,
                headroom_gb = headroom_gb,
                layer_size_gb = layer_size_gb,
                "Insufficient VRAM for layer caching, using single-layer streaming mode"
            );
            1
        };

        tracing::info!(
            hidden_size = hidden,
            intermediate_size = intermediate,
            layer_size_gb = layer_size_gb,
            vram_budget_gb = vram_budget_gb,
            headroom_gb = headroom_gb,
            available_vram_gb = available_vram_gb,
            max_loaded_layers = max_loaded_layers,
            "Calculated layer budget from VRAM"
        );

        // Get EOS token ID for config
        let eos_token_id_config = model_config.eos_token_ids().first().copied();

        // Load the appropriate lazy model based on architecture
        let model = match arch_type {
            ArchitectureType::Qwen2 => {
                tracing::info!("Loading LazyQwen2 model (Qwen2 architecture detected)");

                // Build Qwen2 config
                let qwen2_config = Qwen2Config {
                    hidden_size: model_config.hidden_size.unwrap_or(5120), // 14B default
                    intermediate_size: model_config.intermediate_size.unwrap_or(13824), // 14B default
                    vocab_size: model_config.vocab_size.unwrap_or(152064),
                    num_hidden_layers: model_config.num_hidden_layers.unwrap_or(48), // 14B has 48 layers
                    num_attention_heads: model_config.num_attention_heads.unwrap_or(40),
                    num_key_value_heads: model_config.num_key_value_heads,
                    rms_norm_eps: model_config.rms_norm_eps.unwrap_or(1e-6), // Qwen2 uses 1e-6
                    rope_theta: model_config.rope_theta.unwrap_or(1000000.0), // Qwen2 uses 1M
                    max_position_embeddings: model_config.max_position_embeddings.unwrap_or(32768),
                    tie_word_embeddings: model_config.tie_word_embeddings.unwrap_or(false),
                    bos_token_id: model_config.bos_token_id,
                    eos_token_id: eos_token_id_config,
                    use_sliding_window: false,
                    sliding_window: None,
                };

                let lazy_qwen2 = LazyQwen2::load(qwen2_config, lazy_vb, max_loaded_layers)
                    .map_err(|e| infernum_core::Error::ModelLoad {
                        message: format!("Failed to load LazyQwen2: {}", e),
                    })?;

                ModelKind::LazyQwen2(lazy_qwen2)
            },
            ArchitectureType::Llama | ArchitectureType::Unknown => {
                if arch_type == ArchitectureType::Unknown {
                    tracing::warn!(
                        "Unknown architecture, defaulting to LazyLlama. Model may not work correctly."
                    );
                } else {
                    tracing::info!("Loading LazyLlama model (Llama architecture detected)");
                }

                // Build Llama config
                let llama_config = LlamaConfig {
                    hidden_size: model_config.hidden_size.unwrap_or(16384), // 405B default
                    intermediate_size: model_config.intermediate_size.unwrap_or(53248), // 405B default
                    vocab_size: model_config.vocab_size.unwrap_or(128256),
                    num_hidden_layers: model_config.num_hidden_layers.unwrap_or(126), // 405B has 126 layers
                    num_attention_heads: model_config.num_attention_heads.unwrap_or(128),
                    num_key_value_heads: model_config.num_key_value_heads,
                    rms_norm_eps: model_config.rms_norm_eps.unwrap_or(1e-5),
                    rope_theta: model_config.rope_theta.unwrap_or(500000.0),
                    max_position_embeddings: model_config.max_position_embeddings.unwrap_or(131072),
                    tie_word_embeddings: model_config.tie_word_embeddings.unwrap_or(false),
                    bos_token_id: model_config.bos_token_id,
                    eos_token_id: eos_token_id_config,
                    rope_scaling: model_config.rope_scaling.clone(),
                };

                let lazy_llama = LazyLlama::load(llama_config, lazy_vb, max_loaded_layers)
                    .map_err(|e| infernum_core::Error::ModelLoad {
                        message: format!("Failed to load LazyLlama: {}", e),
                    })?;

                ModelKind::LazyLlama(lazy_llama)
            },
        };

        // Load tokenizer
        let tokenizer = if let Some(tokenizer_path) = &files.tokenizer {
            Tokenizer::from_file(tokenizer_path)?
        } else {
            return Err(infernum_core::Error::ModelLoad {
                message: "No tokenizer found for model".to_string(),
            });
        };

        // Get EOS token with architecture-appropriate default
        let eos_token_id =
            model_config
                .eos_token_ids()
                .first()
                .copied()
                .unwrap_or(match arch_type {
                    ArchitectureType::Qwen2 => 151643, // Qwen2 EOS token
                    _ => 128001,                       // Llama 3 EOS token
                });

        let elapsed = start.elapsed();
        tracing::info!(
            elapsed_ms = elapsed.as_millis(),
            architecture = arch_type.name(),
            "Lazy model base loaded (layers will load on-demand during inference)"
        );

        Ok(LoadedModel {
            model: Mutex::new(model),
            tokenizer,
            eos_token_id,
        })
    }

    /// Loads a model using adaptive memory tiering (experimental).
    ///
    /// Uses intelligent tensor placement based on importance scoring and
    /// mixed precision to minimize or eliminate layer swapping.
    #[allow(clippy::too_many_arguments)]
    fn load_lazy_model_adaptive(
        files: &ModelFiles,
        model_config: &ModelConfig,
        device: &Device,
        dtype: DType,
        directory: &std::path::Path,
        vram_budget: u64,
        ram_budget: u64,
        arch_type: ArchitectureType,
    ) -> Result<LoadedModel> {
        use crate::adaptive_tiering::{
            AdaptiveLoader, AdaptiveTieringConfig, AllocationPlanner, LoadingBackend, ModelProfile,
        };
        use crate::holotensor::tiered_loading::{TieredConfig, TieredHoloLoader};
        use crate::lazy_varbuilder::LazyVarBuilder;
        use crate::models::{LazyLlama, LazyQwen2, LlamaConfig, Qwen2Config};

        let start = Instant::now();

        tracing::info!(
            directory = %directory.display(),
            vram_gb = vram_budget / (1024 * 1024 * 1024),
            ram_gb = ram_budget / (1024 * 1024 * 1024),
            "Loading model with adaptive memory tiering"
        );

        // Build model profile from HCT directory
        let profile = ModelProfile::from_hct_directory(directory).map_err(|e| {
            infernum_core::Error::ModelLoad {
                message: format!("Failed to build model profile: {}", e),
            }
        })?;

        // Log reconstruction path distribution
        let reconstruction_summary = profile.reconstruction_summary();
        tracing::info!(
            model_size_gb = format!("{:.2}", profile.size_gb()),
            num_layers = profile.num_layers,
            tensor_count = profile.tensors.len(),
            gpu_path_count = reconstruction_summary.gpu_fast_count,
            cpu_path_count = reconstruction_summary.cpu_direct_count,
            "Model profile built"
        );

        // Configure and run allocation planner
        let adaptive_config = AdaptiveTieringConfig {
            vram_budget,
            ram_budget,
            quality_target: 0.95,
            enable_mixed_precision: true,
            ..AdaptiveTieringConfig::default()
        };

        let planner = AllocationPlanner::new(adaptive_config);
        let plan = planner
            .plan(&profile)
            .map_err(|e| infernum_core::Error::ModelLoad {
                message: format!("Failed to plan allocation: {}", e),
            })?;

        // Select loading backend based on allocation plan
        let backend = LoadingBackend::select(&plan);
        tracing::info!(
            vram_usage_gb = format!("{:.2}", plan.vram_usage as f64 / 1e9),
            ram_usage_gb = format!("{:.2}", plan.ram_usage as f64 / 1e9),
            nvme_usage_gb = format!("{:.2}", plan.nvme_usage as f64 / 1e9),
            swap_count = plan.swap_count,
            quality_score = format!("{:.3}", plan.quality_score),
            backend = ?backend,
            "Adaptive allocation plan computed"
        );

        // Route to appropriate loading backend
        match backend {
            LoadingBackend::Eager | LoadingBackend::EagerWithRamCache => {
                return Self::load_model_eager_with_placement(
                    files,
                    model_config,
                    device,
                    dtype,
                    directory,
                    &profile,
                    &plan,
                    arch_type,
                    start,
                );
            },
            LoadingBackend::Progressive => {
                // Continue with TieredHoloLoader for 405B+ models
                tracing::info!(
                    "Using progressive loading (405B mode) - model requires layer swapping"
                );
            },
        }

        // Create underlying tiered loader (only for Progressive backend)
        let tiered_config = TieredConfig {
            vram_budget,
            ram_budget,
            min_quality: 0.7,
            target_quality: 0.95,
            enable_background_streaming: true,
            background_streams: 4,
        };

        let mut tiered_loader =
            TieredHoloLoader::new(directory, tiered_config, device.clone(), dtype).map_err(
                |e| infernum_core::Error::ModelLoad {
                    message: format!("Failed to create tiered loader: {}", e),
                },
            )?;

        // Enable NVMe cache if configured
        if let Ok(cache_dir) = std::env::var("INFERNUM_CACHE_DIR") {
            let cache_path = std::path::Path::new(&cache_dir);
            if cache_path.exists() || std::fs::create_dir_all(cache_path).is_ok() {
                tracing::info!(cache_dir = %cache_dir, "NVMe cache enabled");
                tiered_loader = tiered_loader.with_safetensors_dir(cache_path);
            }
        }

        tiered_loader.start_background_streaming();

        // Wrap with adaptive loader for intelligent caching
        let adaptive_loader = AdaptiveLoader::new(plan.clone(), tiered_loader, device.clone());

        // Preload VRAM tensors for faster first inference
        tracing::info!("Preloading VRAM tensors...");
        if let Err(e) = adaptive_loader.preload_vram_tensors() {
            tracing::warn!(error = %e, "Failed to preload VRAM tensors, continuing with lazy loading");
        }

        let provider: std::sync::Arc<dyn crate::lazy_varbuilder::TensorProvider> =
            std::sync::Arc::new(adaptive_loader);

        // Disable LazyVarBuilder cache - AdaptiveLoader handles caching
        let cache_config = crate::lazy_varbuilder::CacheConfig {
            max_memory_bytes: 0,
            max_entries: 0,
        };

        let lazy_vb = LazyVarBuilder::with_cache_config(
            std::sync::Arc::clone(&provider),
            device.clone(),
            dtype,
            cache_config,
        );

        // With adaptive tiering, we can fit more layers in VRAM
        let max_loaded_layers = if plan.swap_count == 0 {
            profile.num_layers // All layers fit in VRAM
        } else {
            // Use existing calculation but take advantage of better packing
            let layer_count = profile.num_layers.max(1);
            let per_layer_vram = plan.vram_usage / layer_count as u64;
            (vram_budget / per_layer_vram.max(1)).min(layer_count as u64) as usize
        };

        tracing::info!(
            max_loaded_layers = max_loaded_layers,
            "Adaptive loading: max layers in VRAM"
        );

        // Get EOS token ID for config
        let eos_token_id_config = model_config.eos_token_ids().first().copied();

        // Load the appropriate lazy model based on architecture
        let model = match arch_type {
            ArchitectureType::Qwen2 => {
                tracing::info!("Loading LazyQwen2 model (Qwen2 architecture detected)");

                let qwen2_config = Qwen2Config {
                    hidden_size: model_config.hidden_size.unwrap_or(5120),
                    intermediate_size: model_config.intermediate_size.unwrap_or(13824),
                    vocab_size: model_config.vocab_size.unwrap_or(152064),
                    num_hidden_layers: model_config.num_hidden_layers.unwrap_or(48),
                    num_attention_heads: model_config.num_attention_heads.unwrap_or(40),
                    num_key_value_heads: model_config.num_key_value_heads,
                    rms_norm_eps: model_config.rms_norm_eps.unwrap_or(1e-6),
                    rope_theta: model_config.rope_theta.unwrap_or(1000000.0),
                    max_position_embeddings: model_config.max_position_embeddings.unwrap_or(32768),
                    tie_word_embeddings: model_config.tie_word_embeddings.unwrap_or(false),
                    bos_token_id: model_config.bos_token_id,
                    eos_token_id: eos_token_id_config,
                    use_sliding_window: false,
                    sliding_window: None,
                };

                let lazy_qwen2 = LazyQwen2::load(qwen2_config, lazy_vb, max_loaded_layers)
                    .map_err(|e| infernum_core::Error::ModelLoad {
                        message: format!("Failed to load LazyQwen2: {}", e),
                    })?;

                ModelKind::LazyQwen2(lazy_qwen2)
            },
            ArchitectureType::Llama | ArchitectureType::Unknown => {
                if arch_type == ArchitectureType::Unknown {
                    tracing::warn!(
                        "Unknown architecture, defaulting to LazyLlama. Model may not work correctly."
                    );
                } else {
                    tracing::info!("Loading LazyLlama model (Llama architecture detected)");
                }

                let llama_config = LlamaConfig {
                    hidden_size: model_config.hidden_size.unwrap_or(16384),
                    intermediate_size: model_config.intermediate_size.unwrap_or(53248),
                    vocab_size: model_config.vocab_size.unwrap_or(128256),
                    num_hidden_layers: model_config.num_hidden_layers.unwrap_or(126),
                    num_attention_heads: model_config.num_attention_heads.unwrap_or(128),
                    num_key_value_heads: model_config.num_key_value_heads,
                    rms_norm_eps: model_config.rms_norm_eps.unwrap_or(1e-5),
                    rope_theta: model_config.rope_theta.unwrap_or(500000.0),
                    max_position_embeddings: model_config.max_position_embeddings.unwrap_or(131072),
                    tie_word_embeddings: model_config.tie_word_embeddings.unwrap_or(false),
                    bos_token_id: model_config.bos_token_id,
                    eos_token_id: eos_token_id_config,
                    rope_scaling: model_config.rope_scaling.clone(),
                };

                let lazy_llama = LazyLlama::load(llama_config, lazy_vb, max_loaded_layers)
                    .map_err(|e| infernum_core::Error::ModelLoad {
                        message: format!("Failed to load LazyLlama: {}", e),
                    })?;

                ModelKind::LazyLlama(lazy_llama)
            },
        };

        // Load tokenizer
        let tokenizer = if let Some(tokenizer_path) = &files.tokenizer {
            Tokenizer::from_file(tokenizer_path)?
        } else {
            return Err(infernum_core::Error::ModelLoad {
                message: "No tokenizer found for model".to_string(),
            });
        };

        // Get EOS token with architecture-appropriate default
        let eos_token_id =
            model_config
                .eos_token_ids()
                .first()
                .copied()
                .unwrap_or(match arch_type {
                    ArchitectureType::Qwen2 => 151643,
                    _ => 128001,
                });

        let elapsed = start.elapsed();
        tracing::info!(
            elapsed_ms = elapsed.as_millis(),
            architecture = arch_type.name(),
            swap_count = plan.swap_count,
            quality_score = format!("{:.3}", plan.quality_score),
            "Adaptive model loaded successfully"
        );

        Ok(LoadedModel {
            model: Mutex::new(model),
            tokenizer,
            eos_token_id,
        })
    }

    /// Loads a model using eager decompression with intelligent tensor placement.
    ///
    /// This is the fast path for models that fit in VRAM+RAM. Uses `hct_sequential`
    /// for parallel tensor decompression instead of progressive streaming.
    ///
    /// Achieves ~2+ tk/s for 14B models vs ~0.3 tk/s with progressive loading.
    #[allow(clippy::too_many_arguments)]
    fn load_model_eager_with_placement(
        files: &ModelFiles,
        model_config: &ModelConfig,
        device: &Device,
        dtype: DType,
        directory: &std::path::Path,
        profile: &crate::adaptive_tiering::ModelProfile,
        plan: &crate::adaptive_tiering::AllocationPlan,
        arch_type: ArchitectureType,
        start: Instant,
    ) -> Result<LoadedModel> {
        use crate::adaptive_tiering::{AdaptiveLoader, EagerTensorProvider};
        use crate::lazy_varbuilder::{CacheConfig, LazyVarBuilder};
        use crate::models::{LazyLlama, LazyQwen2, LlamaConfig, Qwen2Config};

        tracing::info!(
            directory = %directory.display(),
            tensor_count = profile.tensors.len(),
            vram_budget_gb = format!("{:.2}", plan.vram_usage as f64 / 1e9),
            ram_budget_gb = format!("{:.2}", plan.ram_usage as f64 / 1e9),
            "Using eager loading with tiered placement (fast path)"
        );

        // PHASE 1: Eager decompression to CPU (RAM)
        // Load all tensors to CPU first - this is fast parallel decompression
        // without the streaming overhead of TieredHoloLoader
        tracing::info!("Phase 1: Eager decompression to RAM...");
        let cpu_device = Device::Cpu;
        let tensors = crate::hct_sequential::load_hct_directory_parallel(
            directory,
            &cpu_device, // Load to CPU (RAM), not GPU
            dtype,
        )
        .map_err(|e| infernum_core::Error::ModelLoad {
            message: format!("Failed to load HCT tensors: {}", e),
        })?;

        let total_bytes: u64 = tensors
            .values()
            .map(|t| (t.elem_count() * t.dtype().size_in_bytes()) as u64)
            .sum();

        tracing::info!(
            tensor_count = tensors.len(),
            total_gb = format!("{:.2}", total_bytes as f64 / 1e9),
            "Phase 1 complete: All tensors loaded to RAM"
        );

        // PHASE 2: Create tiered provider
        // EagerTensorProvider wraps the pre-loaded tensors
        // AdaptiveLoader adds VRAM/RAM tiering based on the allocation plan
        tracing::info!("Phase 2: Creating tiered provider...");
        let eager_provider = EagerTensorProvider::new(tensors);
        let adaptive_loader = AdaptiveLoader::new(plan.clone(), eager_provider, device.clone());

        // PHASE 3: Preload hot tensors to VRAM
        // This moves the most important tensors to GPU memory upfront
        tracing::info!("Phase 3: Preloading hot tensors to VRAM...");
        if let Err(e) = adaptive_loader.preload_vram_tensors() {
            tracing::warn!(error = %e, "Failed to preload VRAM tensors, continuing with on-demand loading");
        }

        let vram_usage = adaptive_loader.vram_cache_usage();
        let ram_usage = adaptive_loader.ram_cache_usage();
        tracing::info!(
            vram_gb = format!("{:.2}", vram_usage as f64 / 1e9),
            ram_gb = format!("{:.2}", ram_usage as f64 / 1e9),
            "Phase 3 complete: Hot tensors in VRAM, warm tensors in RAM"
        );

        // Create LazyVarBuilder with the adaptive loader
        // Disable LazyVarBuilder's own cache - AdaptiveLoader handles caching
        let provider: std::sync::Arc<dyn crate::lazy_varbuilder::TensorProvider> =
            std::sync::Arc::new(adaptive_loader);
        let cache_config = CacheConfig {
            max_memory_bytes: 0,
            max_entries: 0,
        };
        let lazy_vb = LazyVarBuilder::with_cache_config(
            std::sync::Arc::clone(&provider),
            device.clone(),
            dtype,
            cache_config,
        );

        // All layers can be "loaded" since tensors are already in RAM
        let max_loaded_layers = profile.num_layers;

        // Get EOS token ID for config
        let eos_token_id_config = model_config.eos_token_ids().first().copied();

        // PHASE 4: Build the model using lazy loading
        tracing::info!("Phase 4: Building model with lazy tensor access...");
        let model = match arch_type {
            ArchitectureType::Qwen2 => {
                tracing::info!("Loading LazyQwen2 model with eager-tiered weights");

                let qwen2_config = Qwen2Config {
                    hidden_size: model_config.hidden_size.unwrap_or(5120),
                    intermediate_size: model_config.intermediate_size.unwrap_or(13824),
                    vocab_size: model_config.vocab_size.unwrap_or(152064),
                    num_hidden_layers: model_config.num_hidden_layers.unwrap_or(48),
                    num_attention_heads: model_config.num_attention_heads.unwrap_or(40),
                    num_key_value_heads: model_config.num_key_value_heads,
                    rms_norm_eps: model_config.rms_norm_eps.unwrap_or(1e-6),
                    rope_theta: model_config.rope_theta.unwrap_or(1000000.0),
                    max_position_embeddings: model_config.max_position_embeddings.unwrap_or(32768),
                    tie_word_embeddings: model_config.tie_word_embeddings.unwrap_or(false),
                    bos_token_id: model_config.bos_token_id,
                    eos_token_id: eos_token_id_config,
                    use_sliding_window: false,
                    sliding_window: None,
                };

                let lazy_qwen2 = LazyQwen2::load(qwen2_config, lazy_vb, max_loaded_layers)
                    .map_err(|e| infernum_core::Error::ModelLoad {
                        message: format!("Failed to load LazyQwen2: {}", e),
                    })?;

                ModelKind::LazyQwen2(lazy_qwen2)
            },
            ArchitectureType::Llama | ArchitectureType::Unknown => {
                if arch_type == ArchitectureType::Unknown {
                    tracing::warn!(
                        "Unknown architecture, defaulting to LazyLlama. Model may not work correctly."
                    );
                } else {
                    tracing::info!("Loading LazyLlama model with eager-tiered weights");
                }

                let llama_config = LlamaConfig {
                    hidden_size: model_config.hidden_size.unwrap_or(16384),
                    intermediate_size: model_config.intermediate_size.unwrap_or(53248),
                    vocab_size: model_config.vocab_size.unwrap_or(128256),
                    num_hidden_layers: model_config.num_hidden_layers.unwrap_or(126),
                    num_attention_heads: model_config.num_attention_heads.unwrap_or(128),
                    num_key_value_heads: model_config.num_key_value_heads,
                    rms_norm_eps: model_config.rms_norm_eps.unwrap_or(1e-5),
                    rope_theta: model_config.rope_theta.unwrap_or(500000.0),
                    max_position_embeddings: model_config.max_position_embeddings.unwrap_or(131072),
                    tie_word_embeddings: model_config.tie_word_embeddings.unwrap_or(false),
                    bos_token_id: model_config.bos_token_id,
                    eos_token_id: eos_token_id_config,
                    rope_scaling: model_config.rope_scaling.clone(),
                };

                let lazy_llama = LazyLlama::load(llama_config, lazy_vb, max_loaded_layers)
                    .map_err(|e| infernum_core::Error::ModelLoad {
                        message: format!("Failed to load LazyLlama: {}", e),
                    })?;

                ModelKind::LazyLlama(lazy_llama)
            },
        };

        // Load tokenizer
        let tokenizer = if let Some(tokenizer_path) = &files.tokenizer {
            Tokenizer::from_file(tokenizer_path)?
        } else {
            return Err(infernum_core::Error::ModelLoad {
                message: "No tokenizer found for model".to_string(),
            });
        };

        // Get EOS token with architecture-appropriate default
        let eos_token_id =
            model_config
                .eos_token_ids()
                .first()
                .copied()
                .unwrap_or(match arch_type {
                    ArchitectureType::Qwen2 => 151643,
                    _ => 128001,
                });

        let elapsed = start.elapsed();
        tracing::info!(
            elapsed_ms = elapsed.as_millis(),
            architecture = arch_type.name(),
            vram_gb = format!("{:.2}", vram_usage as f64 / 1e9),
            ram_gb = format!("{:.2}", ram_usage as f64 / 1e9),
            quality_score = format!("{:.3}", plan.quality_score),
            "Model loaded successfully with eager-tiered placement"
        );

        Ok(LoadedModel {
            model: Mutex::new(model),
            tokenizer,
            eos_token_id,
        })
    }

    /// Loads weights from files into a VarBuilder.
    fn load_weights(
        weights: &WeightFiles,
        device: &Device,
        dtype: DType,
    ) -> Result<VarBuilder<'static>> {
        match weights {
            WeightFiles::SingleSafetensors(path) => {
                let data = std::fs::read(path).map_err(|e| infernum_core::Error::ModelLoad {
                    message: format!("Failed to read weights: {}", e),
                })?;

                let vb =
                    VarBuilder::from_buffered_safetensors(data, dtype, device).map_err(|e| {
                        infernum_core::Error::ModelLoad {
                            message: format!("Failed to create VarBuilder: {}", e),
                        }
                    })?;
                Ok(vb)
            },
            WeightFiles::ShardedSafetensors { shards, .. } => {
                // Use memory-mapped loading for sharded files for better memory efficiency
                // SAFETY: The files are read-only and we control the paths
                let vb = unsafe {
                    VarBuilder::from_mmaped_safetensors(shards, dtype, device).map_err(|e| {
                        infernum_core::Error::ModelLoad {
                            message: format!("Failed to mmap shards: {}", e),
                        }
                    })?
                };
                Ok(vb)
            },
            WeightFiles::Gguf(path) => {
                // Load and validate the GGUF file
                let loader = crate::gguf::GgufLoader::from_file(path)?;
                let metadata = loader.metadata();

                tracing::info!(
                    architecture = %metadata.architecture,
                    quantization = %metadata.quantization_type,
                    layers = metadata.num_layers,
                    vocab_size = metadata.vocab_size,
                    "Loaded GGUF metadata"
                );

                // For now, GGUF quantized inference is experimental
                // Return an informative error with details about the model
                Err(infernum_core::Error::ModelLoad {
                    message: format!(
                        "GGUF quantized inference is experimental. \
                        Model: {} ({} architecture, {} layers, {} quantization). \
                        Please use safetensors format for production use, or enable \
                        experimental GGUF support with --experimental-gguf flag.",
                        metadata.name.as_deref().unwrap_or("unknown"),
                        metadata.architecture,
                        metadata.num_layers,
                        metadata.quantization_type
                    ),
                })
            },
            WeightFiles::PyTorch(_) | WeightFiles::ShardedPyTorch { .. } => {
                Err(infernum_core::Error::ModelLoad {
                    message: "PyTorch format not supported, please use safetensors".to_string(),
                })
            },
            WeightFiles::Hct { directory, files } => {
                // Load HCT compressed tensors using sequential loader to prevent OOM
                tracing::info!(
                    directory = %directory.display(),
                    file_count = files.len(),
                    "Loading HCT compressed weights (sequential)"
                );

                // Use sequential loading to prevent OOM on large models
                let tensors =
                    crate::hct_sequential::load_hct_directory_sequential(directory, device, dtype)?;

                tracing::info!(tensor_count = tensors.len(), "Loaded HCT tensors");

                // Create a VarBuilder from the tensor map
                let vb = VarBuilder::from_tensors(tensors, dtype, device);
                Ok(vb)
            },
            WeightFiles::HoloTensor {
                directory,
                min_quality,
                target_quality,
                vram_budget,
                ram_budget,
            } => {
                // Progressive loading with tiered memory management for 405B+ models
                tracing::info!(
                    directory = %directory.display(),
                    min_quality = %min_quality,
                    target_quality = %target_quality,
                    vram_budget_gb = vram_budget / (1024 * 1024 * 1024),
                    ram_budget_gb = ram_budget / (1024 * 1024 * 1024),
                    "Loading HoloTensor weights (progressive)"
                );

                use crate::holotensor::tiered_loading::{TieredConfig, TieredHoloLoader};
                use crate::lazy_varbuilder::LazyVarBuilder;

                // Configure tiered loading
                let tiered_config = TieredConfig {
                    vram_budget: *vram_budget,
                    ram_budget: *ram_budget,
                    min_quality: *min_quality,
                    target_quality: *target_quality,
                    enable_background_streaming: true,
                    background_streams: 4,
                };

                // Create the tiered loader
                let mut loader =
                    TieredHoloLoader::new(directory, tiered_config, device.clone(), dtype)
                        .map_err(|e| infernum_core::Error::ModelLoad {
                            message: format!("Failed to create tiered loader: {}", e),
                        })?;

                // Enable NVMe cache for decompressed tensors if configured
                // This provides ~1000x speedup on subsequent layer loads (100ms vs 100s)
                if let Ok(cache_dir) = std::env::var("INFERNUM_CACHE_DIR") {
                    let cache_path = std::path::Path::new(&cache_dir);
                    if cache_path.exists() || std::fs::create_dir_all(cache_path).is_ok() {
                        tracing::info!(
                            cache_dir = %cache_dir,
                            "NVMe cache enabled - subsequent layer loads will be ~1000x faster"
                        );
                        loader = loader.with_safetensors_dir(cache_path);
                    } else {
                        tracing::warn!(
                            cache_dir = %cache_dir,
                            "Could not create NVMe cache directory, falling back to HCT reconstruction"
                        );
                    }
                }

                // Start background streaming for quality improvement
                loader.start_background_streaming();

                // Create lazy VarBuilder backed by tiered loader
                let provider: std::sync::Arc<dyn crate::lazy_varbuilder::TensorProvider> =
                    std::sync::Arc::new(loader);

                // CRITICAL: Disable LazyVarBuilder cache - TieredHoloLoader already caches on CPU
                // Without this, we'd have duplicate caches:
                //   1. TieredHoloLoader.cpu_cache: CPU tensors (correct)
                //   2. LazyVarBuilder.cache: GPU tensors (60GB default - OOM risk!)
                let cache_config = crate::lazy_varbuilder::CacheConfig {
                    max_memory_bytes: 0, // Disable cache
                    max_entries: 0,
                };
                let lazy_vb = LazyVarBuilder::with_cache_config(
                    std::sync::Arc::clone(&provider),
                    device.clone(),
                    dtype,
                    cache_config,
                );

                tracing::info!(
                    min_quality = %min_quality,
                    "Progressive loading initialized, background streaming started"
                );

                // Return a VarBuilder-compatible wrapper
                // For now, we load all tensors eagerly since Llama/Qwen2 expect VarBuilder
                // Future: Modify model loading to use lazy access
                let tensor_names = provider.tensor_names();
                let mut tensors = std::collections::HashMap::new();

                for name in tensor_names {
                    match lazy_vb.get(&name) {
                        Ok(tensor) => {
                            tensors.insert(name.clone(), tensor);
                        },
                        Err(e) => {
                            tracing::warn!(name = %name, error = %e, "Failed to load tensor, skipping");
                        },
                    }
                }

                tracing::info!(tensor_count = tensors.len(), "Progressive loading complete");

                let vb = VarBuilder::from_tensors(tensors, dtype, device);
                Ok(vb)
            },
        }
    }

    /// Builds model metadata from config.
    fn build_metadata(config: &EngineConfig, model_config: &ModelConfig) -> Result<ModelMetadata> {
        use infernum_core::model::LlamaVersion;
        use infernum_core::{ModelArchitecture, ModelId};

        let id = match &config.model {
            infernum_core::ModelSource::HuggingFace { repo_id, .. } => repo_id.clone(),
            infernum_core::ModelSource::LocalPath { path } => path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("local-model")
                .to_string(),
            infernum_core::ModelSource::S3 { key, .. } => key.clone(),
            infernum_core::ModelSource::Gguf { path } => path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("gguf-model")
                .to_string(),
            infernum_core::ModelSource::HoloTensor { path, .. } => path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("holo-model")
                .to_string(),
        };

        // Detect Llama version from config
        let version = match model_config.architecture() {
            Some(arch) if arch.contains("Llama3") || arch.contains("llama-3") => LlamaVersion::V3,
            _ => LlamaVersion::V3_2, // Default to latest
        };

        Ok(
            ModelMetadata::builder(ModelId::new(&id), ModelArchitecture::Llama { version })
                .source(config.model.clone())
                .context_length(model_config.max_position_embeddings.unwrap_or(4096) as u32)
                .vocab_size(model_config.vocab_size.unwrap_or(32000) as u32)
                .hidden_size(model_config.hidden_size.unwrap_or(4096) as u32)
                .num_layers(model_config.num_hidden_layers.unwrap_or(32) as u32)
                .num_attention_heads(model_config.num_attention_heads.unwrap_or(32) as u32)
                .build(),
        )
    }

    /// Returns the engine configuration.
    #[must_use]
    pub fn config(&self) -> &EngineConfig {
        &self.config
    }

    /// Returns speculative decoding statistics, if speculative decoding is enabled.
    #[must_use]
    pub fn speculative_stats(&self) -> Option<crate::speculative::SpeculativeStats> {
        self.speculative_decoder.as_ref().map(|d| d.stats())
    }

    /// Returns true if speculative decoding is enabled.
    #[must_use]
    pub fn has_speculative_decoding(&self) -> bool {
        self.speculative_decoder.is_some()
    }

    /// Creates a shareable reference to this engine.
    #[must_use]
    pub fn into_shared(self) -> Arc<Self> {
        Arc::new(self)
    }

    /// Generates tokens from the model.
    ///
    /// If speculative decoding is enabled, uses the draft model to accelerate generation.
    /// Otherwise, falls back to standard token-by-token generation.
    fn generate_tokens(
        &self,
        prompt_tokens: &[u32],
        max_tokens: u32,
        sampler: &mut Sampler,
    ) -> Result<(Vec<u32>, Vec<String>)> {
        let loaded = self
            .loaded
            .as_ref()
            .ok_or_else(|| infernum_core::Error::Internal {
                message: "Model not loaded".to_string(),
            })?;

        // Use speculative decoding if available
        if let Some(spec_decoder) = &self.speculative_decoder {
            let eos_token = loaded.eos_token_id;
            let mut model = loaded.model.lock();

            let result = spec_decoder.generate(
                &mut model,
                prompt_tokens,
                max_tokens,
                sampler.params(),
                eos_token,
            );

            // Log speculative stats
            let stats = spec_decoder.stats();
            tracing::debug!(
                acceptance_rate = %format!("{:.1}%", stats.acceptance_rate() * 100.0),
                avg_tokens_per_round = %format!("{:.2}", stats.avg_tokens_per_round()),
                rounds = stats.rounds,
                "Speculative decoding stats"
            );

            return result;
        }

        // Standard generation path
        let mut model = loaded.model.lock();
        model.clear_cache();

        // Convert prompt to tensor
        let input_ids = Tensor::new(prompt_tokens, &self.device).map_err(|e| {
            infernum_core::Error::Internal {
                message: format!("Failed to create input tensor: {}", e),
            }
        })?;
        let input_ids = input_ids
            .unsqueeze(0)
            .map_err(|e| infernum_core::Error::Internal {
                message: format!("Failed to unsqueeze: {}", e),
            })?;

        // Prefill: process the entire prompt
        let logits = model
            .forward(&input_ids, 0)
            .map_err(|e| infernum_core::Error::Internal {
                message: format!("Forward pass failed: {}", e),
            })?;

        // Get logits for last position
        let seq_len = prompt_tokens.len();
        let last_logits =
            logits
                .i((0, seq_len - 1, ..))
                .map_err(|e| infernum_core::Error::Internal {
                    message: format!("Failed to index logits: {}", e),
                })?;

        // Convert to F32 if needed before extracting to Vec
        let last_logits = last_logits.to_dtype(candle_core::DType::F32).map_err(|e| {
            infernum_core::Error::Internal {
                message: format!("Failed to convert logits to F32: {}", e),
            }
        })?;

        let logits_vec: Vec<f32> =
            last_logits
                .to_vec1()
                .map_err(|e| infernum_core::Error::Internal {
                    message: format!("Failed to convert logits: {}", e),
                })?;

        // Note: We don't initialize context with prompt tokens as this can
        // penalize important tokens like special formatting tokens.
        // The context builds naturally as we generate.

        // Sample first token
        let mut generated_tokens = Vec::new();
        let mut generated_text = Vec::new();
        let mut next_token = sampler.sample(&logits_vec);
        sampler.add_token(next_token);

        // Check for EOS
        let eos_token = loaded.eos_token_id;

        for _ in 0..max_tokens {
            if next_token == eos_token {
                break;
            }

            generated_tokens.push(next_token);

            // Decode token
            let token_text = loaded.tokenizer.decode_token(next_token)?;
            generated_text.push(token_text);

            // Check stop sequences
            let full_text: String = generated_text.join("");
            if sampler.is_stop_token(&full_text) {
                break;
            }

            // Generate next token
            let next_input = Tensor::new(&[next_token], &self.device).map_err(|e| {
                infernum_core::Error::Internal {
                    message: format!("Failed to create next input: {}", e),
                }
            })?;
            let next_input =
                next_input
                    .unsqueeze(0)
                    .map_err(|e| infernum_core::Error::Internal {
                        message: format!("Failed to unsqueeze: {}", e),
                    })?;

            let logits = model
                .forward(&next_input, seq_len + generated_tokens.len() - 1)
                .map_err(|e| infernum_core::Error::Internal {
                    message: format!("Forward pass failed: {}", e),
                })?;

            let last_logits = logits
                .i((0, 0, ..))
                .map_err(|e| infernum_core::Error::Internal {
                    message: format!("Failed to index logits: {}", e),
                })?;

            // Convert to F32 if needed
            let last_logits = last_logits.to_dtype(candle_core::DType::F32).map_err(|e| {
                infernum_core::Error::Internal {
                    message: format!("Failed to convert logits to F32: {}", e),
                }
            })?;

            let logits_vec: Vec<f32> =
                last_logits
                    .to_vec1()
                    .map_err(|e| infernum_core::Error::Internal {
                        message: format!("Failed to convert logits: {}", e),
                    })?;

            next_token = sampler.sample(&logits_vec);
            sampler.add_token(next_token);
        }

        Ok((generated_tokens, generated_text))
    }

    // ========================================================================
    // Model Warmup (Sprint 7 - Leviathan Feature Parity)
    // ========================================================================

    /// Warms up the model by running a small inference.
    ///
    /// This populates the KV cache and exercises the model's compute paths,
    /// reducing first-inference latency by 30-50%.
    pub async fn warmup(&self) -> Result<WarmupResult> {
        let start = Instant::now();

        tracing::info!("Starting model warmup");

        let loaded = self
            .loaded
            .as_ref()
            .ok_or_else(|| infernum_core::Error::Internal {
                message: "Model not loaded".to_string(),
            })?;

        // Run a minimal generation to warm up the model
        let warmup_prompt = "Hello";
        let input_ids = loaded.tokenizer.encode(warmup_prompt, true).map_err(|e| {
            infernum_core::Error::Internal {
                message: format!("Tokenization failed: {}", e),
            }
        })?;

        let input_tensor = Tensor::new(input_ids.as_slice(), &self.device)
            .map_err(|e| infernum_core::Error::Internal {
                message: format!("Failed to create tensor: {}", e),
            })?
            .unsqueeze(0)
            .map_err(|e| infernum_core::Error::Internal {
                message: format!("Failed to unsqueeze: {}", e),
            })?;

        // Run forward pass
        let mut model = loaded.model.lock();
        let _logits =
            model
                .forward(&input_tensor, 0)
                .map_err(|e| infernum_core::Error::Internal {
                    message: format!("Warmup forward pass failed: {}", e),
                })?;
        drop(model);

        // Clear KV cache after warmup
        let mut model = loaded.model.lock();
        model.clear_cache();
        drop(model);

        let duration = start.elapsed();
        tracing::info!(duration_ms = duration.as_millis(), "Model warmup complete");

        Ok(WarmupResult {
            success: true,
            duration_ms: duration.as_millis() as u64,
            model_id: self.metadata.id.to_string(),
        })
    }

    /// Warms up the model with a custom prompt.
    pub async fn warmup_with_prompt(
        &self,
        prompt: &str,
        max_tokens: usize,
    ) -> Result<WarmupResult> {
        let start = Instant::now();

        tracing::info!(
            prompt_len = prompt.len(),
            max_tokens,
            "Starting custom warmup"
        );

        let request = GenerateRequest::new(vec![Message {
            role: Role::User,
            content: prompt.to_string(),
            name: None,
            tool_calls: None,
            tool_call_id: None,
        }])
        .with_sampling(
            SamplingParams::default().with_max_tokens(max_tokens.min(10) as u32), // Cap at 10 tokens for warmup
        );

        // Run inference
        let _response = self.generate(request).await?;

        let duration = start.elapsed();
        tracing::info!(duration_ms = duration.as_millis(), "Custom warmup complete");

        Ok(WarmupResult {
            success: true,
            duration_ms: duration.as_millis() as u64,
            model_id: self.metadata.id.to_string(),
        })
    }

    // ========================================================================
    // Graceful Shutdown (Sprint 7 - Leviathan Feature Parity)
    // ========================================================================

    /// Gracefully unloads the model and releases resources.
    ///
    /// This should be called before dropping the engine to ensure proper
    /// cleanup of GPU memory and other resources.
    pub async fn shutdown(&mut self) -> Result<ShutdownResult> {
        let start = Instant::now();

        tracing::info!("Starting graceful shutdown");

        // Clear speculative decoder first
        if self.speculative_decoder.is_some() {
            tracing::debug!("Releasing speculative decoder");
            self.speculative_decoder = None;
        }

        // Unload the model
        if let Some(loaded) = self.loaded.take() {
            tracing::debug!("Releasing model resources");

            // Clear KV cache before dropping
            if let Ok(mut model) = Arc::try_unwrap(loaded) {
                let model_lock = model.model.get_mut();
                model_lock.clear_cache();
                tracing::debug!("KV cache cleared");
            }
            // If Arc::try_unwrap fails, there are other references - that's ok,
            // they will clean up when dropped
        }

        let duration = start.elapsed();
        tracing::info!(
            duration_ms = duration.as_millis(),
            "Graceful shutdown complete"
        );

        Ok(ShutdownResult {
            success: true,
            duration_ms: duration.as_millis() as u64,
            resources_released: vec![
                "model_weights".to_string(),
                "kv_cache".to_string(),
                "speculative_decoder".to_string(),
            ],
        })
    }

    /// Checks if the model is currently loaded.
    #[must_use]
    pub fn is_loaded(&self) -> bool {
        self.loaded.is_some()
    }

    /// Returns the current device.
    #[must_use]
    pub fn device(&self) -> &Device {
        &self.device
    }
}

/// Result of a warmup operation.
#[derive(Debug, Clone)]
pub struct WarmupResult {
    /// Whether warmup succeeded.
    pub success: bool,
    /// Duration in milliseconds.
    pub duration_ms: u64,
    /// Model ID that was warmed up.
    pub model_id: String,
}

/// Result of a shutdown operation.
#[derive(Debug, Clone)]
pub struct ShutdownResult {
    /// Whether shutdown succeeded.
    pub success: bool,
    /// Duration in milliseconds.
    pub duration_ms: u64,
    /// Resources that were released.
    pub resources_released: Vec<String>,
}

#[async_trait]
impl InferenceEngine for Engine {
    async fn generate(&self, request: GenerateRequest) -> Result<GenerateResponse> {
        let start = Instant::now();
        tracing::debug!(request_id = %request.request_id, "Starting generation");

        let loaded = self
            .loaded
            .as_ref()
            .ok_or_else(|| infernum_core::Error::Internal {
                message: "Model not loaded".to_string(),
            })?;

        // Encode prompt
        let prompt_text = match &request.prompt {
            infernum_core::request::PromptInput::Text(s) => s.clone(),
            infernum_core::request::PromptInput::Messages(msgs) => {
                loaded.tokenizer.apply_chat_template(msgs, true)?
            },
            infernum_core::request::PromptInput::Tokens(_) => {
                return Err(infernum_core::Error::Internal {
                    message: "Pre-tokenized input not yet supported".to_string(),
                });
            },
        };

        let prompt_tokens = loaded.tokenizer.encode(&prompt_text, true)?;
        let prompt_token_count = prompt_tokens.len() as u32;

        tracing::debug!(prompt_tokens = prompt_token_count, "Encoded prompt");

        let time_to_first_token = start.elapsed();

        // Try CUDA optimized path first (5-6x faster)
        #[cfg(feature = "cuda")]
        if let Some(ref cuda_gen) = self.cuda_generator {
            tracing::debug!("Using CUDA optimized inference path");

            // Convert sampling params
            let cuda_params = crate::cuda_inference::SamplingParams {
                temperature: request.sampling.temperature,
                top_p: request.sampling.top_p,
                top_k: request.sampling.top_k as usize,
                repetition_penalty: request.sampling.repetition_penalty,
                repetition_context: 64,
                max_tokens: request.sampling.max_tokens as usize,
                stop_tokens: vec![loaded.eos_token_id],
                seed: request.sampling.seed.unwrap_or(42),
            };

            // Generate using CUDA path
            let mut generator = cuda_gen.lock();
            let generated_tokens = generator
                .generate(&prompt_tokens, Some(&cuda_params))
                .map_err(|e| infernum_core::Error::Internal {
                    message: format!("CUDA generation failed: {}", e),
                })?;

            // Decode tokens
            let generated_text: Vec<String> = generated_tokens
                .iter()
                .filter_map(|&t| loaded.tokenizer.decode_token(t).ok())
                .collect();

            let completion_token_count = generated_tokens.len() as u32;
            let total_time = start.elapsed();
            let text = generated_text.join("");

            let tokens_per_sec = completion_token_count as f64 / total_time.as_secs_f64();
            tracing::info!(
                tokens_per_sec = format!("{:.1}", tokens_per_sec),
                path = "cuda_optimized",
                "Generation complete"
            );

            return Ok(GenerateResponse {
                request_id: request.request_id,
                created: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs() as i64,
                model: self.metadata.id.clone(),
                choices: vec![infernum_core::response::Choice {
                    index: 0,
                    text,
                    message: None,
                    finish_reason: Some(infernum_core::FinishReason::Stop),
                    logprobs: None,
                }],
                usage: infernum_core::Usage::new(prompt_token_count, completion_token_count),
                time_to_first_token_ms: Some(time_to_first_token.as_secs_f64() * 1000.0),
                total_time_ms: Some(total_time.as_secs_f64() * 1000.0),
            });
        }

        // Fall back to Candle path
        let mut sampler = Sampler::new(request.sampling.clone());
        let (generated_tokens, generated_text) =
            self.generate_tokens(&prompt_tokens, request.sampling.max_tokens, &mut sampler)?;

        let completion_token_count = generated_tokens.len() as u32;
        let total_time = start.elapsed();

        let text = generated_text.join("");

        tracing::debug!(
            request_id = %request.request_id,
            prompt_tokens = prompt_token_count,
            completion_tokens = completion_token_count,
            total_time_ms = total_time.as_millis(),
            "Generation complete"
        );

        Ok(GenerateResponse {
            request_id: request.request_id,
            created: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs() as i64,
            model: self.metadata.id.clone(),
            choices: vec![infernum_core::response::Choice {
                index: 0,
                text,
                message: None,
                finish_reason: Some(infernum_core::FinishReason::Stop),
                logprobs: None,
            }],
            usage: infernum_core::Usage::new(prompt_token_count, completion_token_count),
            time_to_first_token_ms: Some(time_to_first_token.as_secs_f64() * 1000.0),
            total_time_ms: Some(total_time.as_secs_f64() * 1000.0),
        })
    }

    async fn generate_stream(&self, request: GenerateRequest) -> Result<TokenStream> {
        use futures::stream;
        use infernum_core::streaming::{StreamChoice, StreamChunk, StreamDelta};
        use infernum_core::{FinishReason, Usage};

        tracing::debug!(request_id = %request.request_id, "Starting streaming generation");

        let loaded =
            Arc::clone(
                self.loaded
                    .as_ref()
                    .ok_or_else(|| infernum_core::Error::Internal {
                        message: "Model not loaded".to_string(),
                    })?,
            );

        // Encode prompt before moving into the task
        let prompt_text = match &request.prompt {
            infernum_core::request::PromptInput::Text(s) => s.clone(),
            infernum_core::request::PromptInput::Messages(msgs) => {
                loaded.tokenizer.apply_chat_template(msgs, true)?
            },
            infernum_core::request::PromptInput::Tokens(_) => {
                return Err(infernum_core::Error::Internal {
                    message: "Pre-tokenized input not yet supported".to_string(),
                });
            },
        };

        let prompt_tokens = loaded.tokenizer.encode(&prompt_text, true)?;
        let prompt_token_count = prompt_tokens.len() as u32;

        // Create channel for streaming tokens
        let (tx, rx) = mpsc::channel::<Result<StreamChunk>>(32);

        let request_id = request.request_id.clone();
        let model_id = self.metadata.id.clone();
        let device = self.device.clone();
        let max_tokens = request.sampling.max_tokens;
        let sampling_params = request.sampling.clone();
        let eos_token = loaded.eos_token_id;

        // Spawn token generation task - uses Arc<LoadedModel> to share state
        tokio::task::spawn_blocking(move || {
            let mut sampler = Sampler::new(sampling_params);
            let mut model_guard = loaded.model.lock();
            model_guard.clear_cache();

            // Convert prompt to tensor
            let input_ids = match Tensor::new(prompt_tokens.as_slice(), &device) {
                Ok(t) => t,
                Err(e) => {
                    let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                        message: format!("Failed to create input tensor: {}", e),
                    }));
                    return;
                },
            };

            let input_ids = match input_ids.unsqueeze(0) {
                Ok(t) => t,
                Err(e) => {
                    let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                        message: format!("Failed to unsqueeze: {}", e),
                    }));
                    return;
                },
            };

            // Prefill
            let logits = match model_guard.forward(&input_ids, 0) {
                Ok(l) => l,
                Err(e) => {
                    let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                        message: format!("Forward pass failed: {}", e),
                    }));
                    return;
                },
            };

            let seq_len = prompt_tokens.len();
            let last_logits = match logits.i((0, seq_len - 1, ..)) {
                Ok(l) => l,
                Err(e) => {
                    let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                        message: format!("Failed to index logits: {}", e),
                    }));
                    return;
                },
            };

            // Convert to F32 if needed
            let last_logits = match last_logits.to_dtype(candle_core::DType::F32) {
                Ok(l) => l,
                Err(e) => {
                    let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                        message: format!("Failed to convert logits to F32: {}", e),
                    }));
                    return;
                },
            };

            let logits_vec: Vec<f32> = match last_logits.to_vec1() {
                Ok(v) => v,
                Err(e) => {
                    let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                        message: format!("Failed to convert logits: {}", e),
                    }));
                    return;
                },
            };

            let mut next_token = sampler.sample(&logits_vec);
            let mut generated_count: u32 = 0;
            let mut full_text = String::new();

            for _ in 0..max_tokens {
                if next_token == eos_token {
                    break;
                }

                generated_count += 1;

                // Decode token
                let token_text = match loaded.tokenizer.decode_token(next_token) {
                    Ok(t) => t,
                    Err(e) => {
                        let _ = tx.blocking_send(Err(e));
                        return;
                    },
                };

                full_text.push_str(&token_text);

                // Send streaming chunk
                let chunk = StreamChunk {
                    request_id: request_id.clone(),
                    model: model_id.clone(),
                    choices: vec![StreamChoice {
                        index: 0,
                        delta: StreamDelta::text(&token_text),
                        finish_reason: None,
                    }],
                    usage: None,
                };

                if tx.blocking_send(Ok(chunk)).is_err() {
                    // Receiver dropped, stop generation
                    return;
                }

                // Check stop sequences
                if sampler.is_stop_token(&full_text) {
                    break;
                }

                // Generate next token
                let next_input = match Tensor::new(&[next_token], &device) {
                    Ok(t) => t,
                    Err(e) => {
                        let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                            message: format!("Failed to create next input: {}", e),
                        }));
                        return;
                    },
                };

                let next_input = match next_input.unsqueeze(0) {
                    Ok(t) => t,
                    Err(e) => {
                        let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                            message: format!("Failed to unsqueeze: {}", e),
                        }));
                        return;
                    },
                };

                let logits = match model_guard
                    .forward(&next_input, seq_len + generated_count as usize - 1)
                {
                    Ok(l) => l,
                    Err(e) => {
                        let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                            message: format!("Forward pass failed: {}", e),
                        }));
                        return;
                    },
                };

                let last_logits = match logits.i((0, 0, ..)) {
                    Ok(l) => l,
                    Err(e) => {
                        let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                            message: format!("Failed to index logits: {}", e),
                        }));
                        return;
                    },
                };

                // Convert to F32 if needed
                let last_logits = match last_logits.to_dtype(candle_core::DType::F32) {
                    Ok(l) => l,
                    Err(e) => {
                        let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                            message: format!("Failed to convert logits to F32: {}", e),
                        }));
                        return;
                    },
                };

                let logits_vec: Vec<f32> = match last_logits.to_vec1() {
                    Ok(v) => v,
                    Err(e) => {
                        let _ = tx.blocking_send(Err(infernum_core::Error::Internal {
                            message: format!("Failed to convert logits: {}", e),
                        }));
                        return;
                    },
                };

                next_token = sampler.sample(&logits_vec);
            }

            // Send final chunk with usage info
            let final_chunk = StreamChunk {
                request_id: request_id.clone(),
                model: model_id.clone(),
                choices: vec![StreamChoice {
                    index: 0,
                    delta: StreamDelta::empty(),
                    finish_reason: Some(FinishReason::Stop),
                }],
                usage: Some(Usage::new(prompt_token_count, generated_count)),
            };

            let _ = tx.blocking_send(Ok(final_chunk));
        });

        // Convert receiver to stream
        let stream = stream::unfold(rx, |mut rx| async move {
            rx.recv().await.map(|item| (item, rx))
        });

        Ok(TokenStream::new(stream))
    }

    async fn embed(&self, request: EmbedRequest) -> Result<EmbedResponse> {
        use infernum_core::response::{Embedding, EmbeddingData};

        tracing::debug!(request_id = %request.request_id, "Starting embedding generation");

        let loaded = self
            .loaded
            .as_ref()
            .ok_or_else(|| infernum_core::Error::Internal {
                message: "Model not loaded".to_string(),
            })?;

        // Get input texts
        let texts = request.input.as_texts();
        let mut embeddings = Vec::with_capacity(texts.len());
        let mut total_tokens = 0u32;

        for (idx, text) in texts.iter().enumerate() {
            // Tokenize the input
            let tokens = loaded.tokenizer.encode(text, true)?;
            let token_count = tokens.len();
            total_tokens += token_count as u32;

            // Convert to tensor
            let input_ids = Tensor::new(tokens.as_slice(), &self.device)
                .map_err(|e| infernum_core::Error::Internal {
                    message: format!("Failed to create input tensor: {}", e),
                })?
                .unsqueeze(0)
                .map_err(|e| infernum_core::Error::Internal {
                    message: format!("Failed to unsqueeze tensor: {}", e),
                })?;

            // Extract embeddings
            let embedding_tensor = {
                let mut model = loaded.model.lock();
                model.extract_embeddings(&input_ids).map_err(|e| {
                    infernum_core::Error::Internal {
                        message: format!("Failed to extract embeddings: {}", e),
                    }
                })?
            };

            // Convert to vector
            let embedding_vec: Vec<f32> = embedding_tensor
                .squeeze(0)
                .map_err(|e| infernum_core::Error::Internal {
                    message: format!("Failed to squeeze embedding: {}", e),
                })?
                .to_vec1()
                .map_err(|e| infernum_core::Error::Internal {
                    message: format!("Failed to convert embedding to vector: {}", e),
                })?;

            // Apply dimension reduction if requested
            let final_embedding = if let Some(dims) = request.dimensions {
                let dims = dims as usize;
                if dims < embedding_vec.len() {
                    embedding_vec[..dims].to_vec()
                } else {
                    embedding_vec
                }
            } else {
                embedding_vec
            };

            embeddings.push(Embedding {
                index: idx as u32,
                embedding: EmbeddingData::Float(final_embedding),
            });

            tracing::debug!(
                request_id = %request.request_id,
                text_idx = idx,
                tokens = token_count,
                "Generated embedding"
            );
        }

        tracing::debug!(
            request_id = %request.request_id,
            num_embeddings = embeddings.len(),
            total_tokens = total_tokens,
            "Embedding generation complete"
        );

        Ok(EmbedResponse {
            request_id: request.request_id,
            model: self.metadata.id.clone(),
            data: embeddings,
            usage: infernum_core::Usage::new(total_tokens, 0),
        })
    }

    fn model_info(&self) -> &ModelMetadata {
        &self.metadata
    }

    fn is_ready(&self) -> bool {
        self.loaded.is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU32, Ordering};

    // Note: These tests require model files to be present
    // They are disabled by default
    #[ignore]
    #[tokio::test]
    async fn test_engine_creation() {
        let config = EngineConfig::builder()
            .model("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
            .build()
            .unwrap();

        let engine = Engine::new(config).await.unwrap();
        assert!(engine.is_ready());
    }

    /// Mock engine for testing the batch interface.
    struct MockEngine {
        call_count: AtomicU32,
        metadata: ModelMetadata,
    }

    impl MockEngine {
        fn new() -> Self {
            use infernum_core::{ModelArchitecture, ModelSource};

            Self {
                call_count: AtomicU32::new(0),
                metadata: ModelMetadata::builder("mock-model", ModelArchitecture::Bert)
                    .source(ModelSource::LocalPath {
                        path: std::path::PathBuf::from("/mock"),
                    })
                    .context_length(2048)
                    .vocab_size(32000)
                    .hidden_size(768)
                    .num_layers(12)
                    .num_attention_heads(12)
                    .build(),
            }
        }

        fn call_count(&self) -> u32 {
            self.call_count.load(Ordering::Relaxed)
        }
    }

    #[async_trait]
    impl InferenceEngine for MockEngine {
        async fn generate(&self, request: GenerateRequest) -> Result<GenerateResponse> {
            self.call_count.fetch_add(1, Ordering::Relaxed);

            // Simulate some async work
            tokio::time::sleep(std::time::Duration::from_millis(1)).await;

            Ok(GenerateResponse {
                request_id: request.request_id,
                created: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs() as i64,
                model: self.metadata.id.clone(),
                choices: vec![infernum_core::response::Choice {
                    index: 0,
                    text: "Mock response".to_string(),
                    message: None,
                    finish_reason: Some(infernum_core::FinishReason::Stop),
                    logprobs: None,
                }],
                usage: infernum_core::Usage::new(10, 5),
                time_to_first_token_ms: Some(1.0),
                total_time_ms: Some(5.0),
            })
        }

        async fn generate_stream(&self, _request: GenerateRequest) -> Result<TokenStream> {
            unimplemented!("Mock doesn't support streaming")
        }

        async fn embed(&self, _request: EmbedRequest) -> Result<EmbedResponse> {
            unimplemented!("Mock doesn't support embedding")
        }

        fn model_info(&self) -> &ModelMetadata {
            &self.metadata
        }

        fn is_ready(&self) -> bool {
            true
        }
    }

    #[tokio::test]
    async fn test_generate_batch_default_implementation() {
        let engine = MockEngine::new();

        // Create multiple requests
        let requests: Vec<GenerateRequest> = (0..5)
            .map(|i| GenerateRequest::new(format!("Test prompt {}", i)))
            .collect();

        // Call batch generate (uses default trait implementation)
        let results = engine.generate_batch(requests).await;

        // All 5 requests should have been processed
        assert_eq!(results.len(), 5);
        assert_eq!(engine.call_count(), 5);

        // All results should be Ok
        for result in results {
            assert!(result.is_ok());
            let response = result.unwrap();
            assert_eq!(response.choices.len(), 1);
            assert_eq!(response.choices[0].text, "Mock response");
        }
    }

    #[tokio::test]
    async fn test_generate_batch_empty() {
        let engine = MockEngine::new();

        let results = engine.generate_batch(vec![]).await;

        assert!(results.is_empty());
        assert_eq!(engine.call_count(), 0);
    }

    #[tokio::test]
    async fn test_generate_batch_single_request() {
        let engine = MockEngine::new();

        let requests = vec![GenerateRequest::new("Single request")];
        let results = engine.generate_batch(requests).await;

        assert_eq!(results.len(), 1);
        assert_eq!(engine.call_count(), 1);
        assert!(results[0].is_ok());
    }
}