kizzasi-model 0.2.1

Model architectures for Kizzasi AGSP - Mamba, RWKV, S4, Transformer
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
//! GGUF Format Support
//!
//! Implements parsing and tensor loading for the GGUF (Georgi Gerganov Unified Format)
//! binary format used by llama.cpp and related tools for storing quantized LLM weights.
//!
//! # Format Overview
//!
//! A GGUF file consists of:
//! - Magic bytes `b"GGUF"` followed by a u32 version number
//! - Tensor count and metadata KV count (u64 for v2+, u32 for v1)
//! - Metadata key-value pairs (typed)
//! - Tensor info array (name, shape, quant type, offset into data section)
//! - Data section (32-byte aligned after header)
//!
//! # Quantization Types
//!
//! Supports F32, F16, BF16, Q4_0, Q4_1, Q5_0, Q5_1, Q8_0, Q6_K, Q2_K, Q3_K, Q4_K,
//! Q5_K, and Q8_K dequantization. IQ* types return an unsupported error.

use crate::error::{ModelError, ModelResult};
use scirs2_core::ndarray::Array2;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Return type of [`parse_gguf_buffer`]: `(version, metadata, tensors, data_offset)`.
type GgufBufferParsed = (
    u32,
    HashMap<String, GgufMetaValue>,
    Vec<GgufTensorInfo>,
    u64,
);

// ──────────────────────────────────────────────────────────────────────────────
// Binary parsing primitives
// ──────────────────────────────────────────────────────────────────────────────

fn read_u8(buf: &[u8], pos: &mut usize) -> ModelResult<u8> {
    if *pos >= buf.len() {
        return Err(ModelError::simple_load_error(format!(
            "Buffer underflow reading u8 at position {}",
            pos
        )));
    }
    let v = buf[*pos];
    *pos += 1;
    Ok(v)
}

fn read_u16_le(buf: &[u8], pos: &mut usize) -> ModelResult<u16> {
    let end = *pos + 2;
    if end > buf.len() {
        return Err(ModelError::simple_load_error(format!(
            "Buffer underflow reading u16 at position {}",
            pos
        )));
    }
    let v = u16::from_le_bytes([buf[*pos], buf[*pos + 1]]);
    *pos = end;
    Ok(v)
}

fn read_u32_le(buf: &[u8], pos: &mut usize) -> ModelResult<u32> {
    let end = *pos + 4;
    if end > buf.len() {
        return Err(ModelError::simple_load_error(format!(
            "Buffer underflow reading u32 at position {}",
            pos
        )));
    }
    let v = u32::from_le_bytes([buf[*pos], buf[*pos + 1], buf[*pos + 2], buf[*pos + 3]]);
    *pos = end;
    Ok(v)
}

fn read_u64_le(buf: &[u8], pos: &mut usize) -> ModelResult<u64> {
    let end = *pos + 8;
    if end > buf.len() {
        return Err(ModelError::simple_load_error(format!(
            "Buffer underflow reading u64 at position {}",
            pos
        )));
    }
    let v = u64::from_le_bytes([
        buf[*pos],
        buf[*pos + 1],
        buf[*pos + 2],
        buf[*pos + 3],
        buf[*pos + 4],
        buf[*pos + 5],
        buf[*pos + 6],
        buf[*pos + 7],
    ]);
    *pos = end;
    Ok(v)
}

fn read_i8(buf: &[u8], pos: &mut usize) -> ModelResult<i8> {
    read_u8(buf, pos).map(|v| v as i8)
}

fn read_i16_le(buf: &[u8], pos: &mut usize) -> ModelResult<i16> {
    read_u16_le(buf, pos).map(|v| v as i16)
}

fn read_i32_le(buf: &[u8], pos: &mut usize) -> ModelResult<i32> {
    read_u32_le(buf, pos).map(|v| v as i32)
}

fn read_i64_le(buf: &[u8], pos: &mut usize) -> ModelResult<i64> {
    read_u64_le(buf, pos).map(|v| v as i64)
}

fn read_f32_le(buf: &[u8], pos: &mut usize) -> ModelResult<f32> {
    read_u32_le(buf, pos).map(f32::from_bits)
}

fn read_f64_le(buf: &[u8], pos: &mut usize) -> ModelResult<f64> {
    read_u64_le(buf, pos).map(f64::from_bits)
}

fn read_bool(buf: &[u8], pos: &mut usize) -> ModelResult<bool> {
    read_u8(buf, pos).map(|v| v != 0)
}

/// Read a GGUF string using v2+ encoding (u64 length prefix).
fn read_string_v2(buf: &[u8], pos: &mut usize) -> ModelResult<String> {
    let len = read_u64_le(buf, pos)? as usize;
    let end = *pos + len;
    if end > buf.len() {
        return Err(ModelError::simple_load_error(format!(
            "Buffer underflow reading string of length {} at position {}",
            len, pos
        )));
    }
    let s = std::str::from_utf8(&buf[*pos..end]).map_err(|e| {
        ModelError::simple_load_error(format!("Invalid UTF-8 in GGUF string: {}", e))
    })?;
    let owned = s.to_owned();
    *pos = end;
    Ok(owned)
}

/// Read a GGUF string using v1 encoding (u32 length prefix).
fn read_string_v1(buf: &[u8], pos: &mut usize) -> ModelResult<String> {
    let len = read_u32_le(buf, pos)? as usize;
    let end = *pos + len;
    if end > buf.len() {
        return Err(ModelError::simple_load_error(format!(
            "Buffer underflow reading v1 string of length {} at position {}",
            len, pos
        )));
    }
    let s = std::str::from_utf8(&buf[*pos..end]).map_err(|e| {
        ModelError::simple_load_error(format!("Invalid UTF-8 in GGUF v1 string: {}", e))
    })?;
    let owned = s.to_owned();
    *pos = end;
    Ok(owned)
}

// ──────────────────────────────────────────────────────────────────────────────
// Public types
// ──────────────────────────────────────────────────────────────────────────────

/// GGUF metadata value type discriminants.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum GgufValueType {
    Uint8 = 0,
    Int8 = 1,
    Uint16 = 2,
    Int16 = 3,
    Uint32 = 4,
    Int32 = 5,
    Float32 = 6,
    Bool = 7,
    String = 8,
    Array = 9,
    Uint64 = 10,
    Int64 = 11,
    Float64 = 12,
}

impl GgufValueType {
    fn from_u32(v: u32) -> ModelResult<Self> {
        match v {
            0 => Ok(Self::Uint8),
            1 => Ok(Self::Int8),
            2 => Ok(Self::Uint16),
            3 => Ok(Self::Int16),
            4 => Ok(Self::Uint32),
            5 => Ok(Self::Int32),
            6 => Ok(Self::Float32),
            7 => Ok(Self::Bool),
            8 => Ok(Self::String),
            9 => Ok(Self::Array),
            10 => Ok(Self::Uint64),
            11 => Ok(Self::Int64),
            12 => Ok(Self::Float64),
            other => Err(ModelError::simple_load_error(format!(
                "Unknown GGUF value type: {}",
                other
            ))),
        }
    }
}

/// Typed GGUF metadata value.
#[derive(Debug, Clone)]
pub enum GgufMetaValue {
    Uint8(u8),
    Int8(i8),
    Uint16(u16),
    Int16(i16),
    Uint32(u32),
    Int32(i32),
    Float32(f32),
    Bool(bool),
    String(String),
    Uint64(u64),
    Int64(i64),
    Float64(f64),
    Array(Vec<GgufMetaValue>),
}

/// Quantization type for a GGUF tensor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum GgufQuantType {
    F32 = 0,
    F16 = 1,
    Q4_0 = 2,
    Q4_1 = 3,
    Q5_0 = 6,
    Q5_1 = 7,
    Q8_0 = 8,
    Q8_1 = 9,
    Q2K = 10,
    Q3K = 11,
    Q4K = 12,
    Q5K = 13,
    Q6K = 14,
    Q8K = 15,
    IQ2XXS = 16,
    IQ2XS = 17,
    IQ3XXS = 18,
    IQ1S = 19,
    IQ4NL = 20,
    IQ3S = 21,
    IQ2S = 22,
    IQ4XS = 23,
    BF16 = 30,
}

impl GgufQuantType {
    fn from_u32(v: u32) -> ModelResult<Self> {
        match v {
            0 => Ok(Self::F32),
            1 => Ok(Self::F16),
            2 => Ok(Self::Q4_0),
            3 => Ok(Self::Q4_1),
            6 => Ok(Self::Q5_0),
            7 => Ok(Self::Q5_1),
            8 => Ok(Self::Q8_0),
            9 => Ok(Self::Q8_1),
            10 => Ok(Self::Q2K),
            11 => Ok(Self::Q3K),
            12 => Ok(Self::Q4K),
            13 => Ok(Self::Q5K),
            14 => Ok(Self::Q6K),
            15 => Ok(Self::Q8K),
            16 => Ok(Self::IQ2XXS),
            17 => Ok(Self::IQ2XS),
            18 => Ok(Self::IQ3XXS),
            19 => Ok(Self::IQ1S),
            20 => Ok(Self::IQ4NL),
            21 => Ok(Self::IQ3S),
            22 => Ok(Self::IQ2S),
            23 => Ok(Self::IQ4XS),
            30 => Ok(Self::BF16),
            other => Err(ModelError::simple_load_error(format!(
                "Unknown GGUF quantization type: {}",
                other
            ))),
        }
    }

    /// Human-readable name for inspection output.
    pub fn name(&self) -> &'static str {
        match self {
            Self::F32 => "F32",
            Self::F16 => "F16",
            Self::Q4_0 => "Q4_0",
            Self::Q4_1 => "Q4_1",
            Self::Q5_0 => "Q5_0",
            Self::Q5_1 => "Q5_1",
            Self::Q8_0 => "Q8_0",
            Self::Q8_1 => "Q8_1",
            Self::Q2K => "Q2K",
            Self::Q3K => "Q3K",
            Self::Q4K => "Q4K",
            Self::Q5K => "Q5K",
            Self::Q6K => "Q6K",
            Self::Q8K => "Q8K",
            Self::IQ2XXS => "IQ2XXS",
            Self::IQ2XS => "IQ2XS",
            Self::IQ3XXS => "IQ3XXS",
            Self::IQ1S => "IQ1S",
            Self::IQ4NL => "IQ4NL",
            Self::IQ3S => "IQ3S",
            Self::IQ2S => "IQ2S",
            Self::IQ4XS => "IQ4XS",
            Self::BF16 => "BF16",
        }
    }
}

/// Information about a single tensor stored in a GGUF file.
#[derive(Debug, Clone)]
pub struct GgufTensorInfo {
    /// Name of the tensor (e.g. `"token_embd.weight"`).
    pub name: String,
    /// Shape dimensions in GGUF order (innermost first).
    pub shape: Vec<u64>,
    /// Quantization / data type of the stored tensor.
    pub quant_type: GgufQuantType,
    /// Byte offset of this tensor's data relative to the start of the data section.
    pub offset: u64,
    /// Absolute byte offset of this tensor's data within the file.
    /// Populated as `data_section_start + offset` after parsing.
    pub data_offset: u64,
}

impl GgufTensorInfo {
    /// Total number of scalar elements in this tensor.
    pub fn n_elements(&self) -> u64 {
        self.shape.iter().product()
    }
}

/// A parsed GGUF file ready for tensor loading.
#[derive(Debug)]
pub struct GgufFile {
    /// GGUF format version (1, 2, or 3).
    pub version: u32,
    /// All metadata key-value pairs.
    pub metadata: HashMap<String, GgufMetaValue>,
    /// Ordered list of tensor descriptors.
    pub tensors: Vec<GgufTensorInfo>,
    /// Byte offset in the file where the data section begins.
    data_offset: u64,
    /// Path to the source file (used when reading tensor data).
    file_path: PathBuf,
}

/// Lightweight summary of a GGUF file for quick inspection.
#[derive(Debug, Clone)]
pub struct GgufInspection {
    /// GGUF format version.
    pub version: u32,
    /// Number of tensors.
    pub tensor_count: usize,
    /// Number of metadata entries.
    pub metadata_count: usize,
    /// Model architecture string from `general.architecture`, if present.
    pub architecture: Option<String>,
    /// All tensor names in file order.
    pub tensor_names: Vec<String>,
    /// Total number of scalar parameters across all tensors.
    pub total_param_count: u64,
    /// Unique quantization type names used across all tensors.
    pub quant_types_used: Vec<String>,
}

// ──────────────────────────────────────────────────────────────────────────────
// Parsing helpers
// ──────────────────────────────────────────────────────────────────────────────

/// Read a single typed metadata value from `buf` at `*pos`.
/// `version` controls whether strings use u32 or u64 length prefixes.
fn read_meta_value(
    buf: &[u8],
    pos: &mut usize,
    vtype: GgufValueType,
    version: u32,
) -> ModelResult<GgufMetaValue> {
    match vtype {
        GgufValueType::Uint8 => Ok(GgufMetaValue::Uint8(read_u8(buf, pos)?)),
        GgufValueType::Int8 => Ok(GgufMetaValue::Int8(read_i8(buf, pos)?)),
        GgufValueType::Uint16 => Ok(GgufMetaValue::Uint16(read_u16_le(buf, pos)?)),
        GgufValueType::Int16 => Ok(GgufMetaValue::Int16(read_i16_le(buf, pos)?)),
        GgufValueType::Uint32 => Ok(GgufMetaValue::Uint32(read_u32_le(buf, pos)?)),
        GgufValueType::Int32 => Ok(GgufMetaValue::Int32(read_i32_le(buf, pos)?)),
        GgufValueType::Float32 => Ok(GgufMetaValue::Float32(read_f32_le(buf, pos)?)),
        GgufValueType::Bool => Ok(GgufMetaValue::Bool(read_bool(buf, pos)?)),
        GgufValueType::String => {
            let s = if version >= 2 {
                read_string_v2(buf, pos)?
            } else {
                read_string_v1(buf, pos)?
            };
            Ok(GgufMetaValue::String(s))
        }
        GgufValueType::Uint64 => Ok(GgufMetaValue::Uint64(read_u64_le(buf, pos)?)),
        GgufValueType::Int64 => Ok(GgufMetaValue::Int64(read_i64_le(buf, pos)?)),
        GgufValueType::Float64 => Ok(GgufMetaValue::Float64(read_f64_le(buf, pos)?)),
        GgufValueType::Array => {
            let elem_type_raw = read_u32_le(buf, pos)?;
            let elem_type = GgufValueType::from_u32(elem_type_raw)?;
            let count = if version >= 2 {
                read_u64_le(buf, pos)? as usize
            } else {
                read_u32_le(buf, pos)? as usize
            };
            let mut elements = Vec::with_capacity(count);
            for _ in 0..count {
                elements.push(read_meta_value(buf, pos, elem_type, version)?);
            }
            Ok(GgufMetaValue::Array(elements))
        }
    }
}

/// Parse the header and tensor info sections of the buffer.
/// Returns `(version, metadata, tensors, data_offset)`.
fn parse_gguf_buffer(buf: &[u8], file_path: &Path) -> ModelResult<GgufBufferParsed> {
    // Magic
    if buf.len() < 4 {
        return Err(ModelError::simple_load_error("File too small to be GGUF"));
    }
    if &buf[0..4] != b"GGUF" {
        return Err(ModelError::simple_load_error(format!(
            "Invalid GGUF magic in {:?}",
            file_path
        )));
    }
    let mut pos = 4usize;

    // Version
    let version = read_u32_le(buf, &mut pos)?;
    if version == 0 || version > 3 {
        return Err(ModelError::simple_load_error(format!(
            "Unsupported GGUF version: {}",
            version
        )));
    }

    // Counts differ between v1 and v2+
    let (tensor_count, kv_count) = if version >= 2 {
        let tc = read_u64_le(buf, &mut pos)? as usize;
        let kv = read_u64_le(buf, &mut pos)? as usize;
        (tc, kv)
    } else {
        let tc = read_u32_le(buf, &mut pos)? as usize;
        let kv = read_u32_le(buf, &mut pos)? as usize;
        (tc, kv)
    };

    // Metadata KV pairs
    let mut metadata = HashMap::with_capacity(kv_count);
    for _ in 0..kv_count {
        let key = if version >= 2 {
            read_string_v2(buf, &mut pos)?
        } else {
            read_string_v1(buf, &mut pos)?
        };
        let vtype_raw = read_u32_le(buf, &mut pos)?;
        let vtype = GgufValueType::from_u32(vtype_raw)?;
        let value = read_meta_value(buf, &mut pos, vtype, version)?;
        metadata.insert(key, value);
    }

    // Tensor info
    let mut tensors = Vec::with_capacity(tensor_count);
    for _ in 0..tensor_count {
        let name = if version >= 2 {
            read_string_v2(buf, &mut pos)?
        } else {
            read_string_v1(buf, &mut pos)?
        };

        let n_dims = read_u32_le(buf, &mut pos)? as usize;
        let mut shape = Vec::with_capacity(n_dims);
        for _ in 0..n_dims {
            if version >= 2 {
                shape.push(read_u64_le(buf, &mut pos)?);
            } else {
                shape.push(read_u32_le(buf, &mut pos)? as u64);
            }
        }

        let quant_raw = read_u32_le(buf, &mut pos)?;
        let quant_type = GgufQuantType::from_u32(quant_raw)?;
        let offset = read_u64_le(buf, &mut pos)?;

        // data_offset will be patched after we know the data section start
        tensors.push(GgufTensorInfo {
            name,
            shape,
            quant_type,
            offset,
            data_offset: 0, // placeholder; patched below
        });
    }

    // Data section begins at the next 32-byte aligned boundary after the header.
    let aligned_offset = (pos as u64 + 31) & !31u64;

    // Patch data_offset for each tensor to be the absolute file offset.
    for t in &mut tensors {
        t.data_offset = aligned_offset + t.offset;
    }

    Ok((version, metadata, tensors, aligned_offset))
}

// ──────────────────────────────────────────────────────────────────────────────
// GgufFile implementation
// ──────────────────────────────────────────────────────────────────────────────

impl GgufFile {
    /// Parse a GGUF file from the given path.
    ///
    /// The entire file is read into memory; tensor data is then read on demand
    /// via [`load_tensor_f32`](Self::load_tensor_f32).
    pub fn open(path: &Path) -> ModelResult<Self> {
        let buf = std::fs::read(path).map_err(|e| {
            ModelError::simple_load_error(format!("Failed to read GGUF file {:?}: {}", path, e))
        })?;
        let (version, metadata, tensors, data_offset) = parse_gguf_buffer(&buf, path)?;
        Ok(Self {
            version,
            metadata,
            tensors,
            data_offset,
            file_path: path.to_path_buf(),
        })
    }

    /// Load a single tensor by name, dequantizing to f32, and reshape to 2D.
    ///
    /// If the tensor has more than 2 dimensions, all dimensions except the last
    /// are folded into the first axis (i.e. `[d0, d1, …, dn] → [d0*…*d(n-1), dn]`).
    /// A 1-D tensor of length `n` becomes `[1, n]`.
    pub fn load_tensor_f32(&self, name: &str) -> ModelResult<Array2<f32>> {
        let info = self
            .tensors
            .iter()
            .find(|t| t.name == name)
            .ok_or_else(|| {
                ModelError::simple_load_error(format!(
                    "Tensor '{}' not found in GGUF file {:?}",
                    name, self.file_path
                ))
            })?;

        let n_elements = info.n_elements() as usize;

        // Read the raw bytes for this tensor from the file.
        let byte_offset = self.data_offset + info.offset;
        let byte_len = tensor_byte_size(info)?;

        let file_buf = std::fs::read(&self.file_path).map_err(|e| {
            ModelError::simple_load_error(format!(
                "Failed to re-read GGUF file {:?}: {}",
                self.file_path, e
            ))
        })?;

        let start = byte_offset as usize;
        let end = start + byte_len;
        if end > file_buf.len() {
            return Err(ModelError::simple_load_error(format!(
                "Tensor '{}' data region [{}, {}) exceeds file size {}",
                name,
                start,
                end,
                file_buf.len()
            )));
        }
        let raw = &file_buf[start..end];

        let floats = dequant::dequantize(raw, &info.quant_type, n_elements)?;

        // Reshape to 2D
        let (rows, cols) = shape_to_2d(&info.shape);
        Array2::from_shape_vec((rows, cols), floats).map_err(|e| {
            ModelError::simple_load_error(format!(
                "Failed to reshape tensor '{}' to ({}, {}): {}",
                name, rows, cols, e
            ))
        })
    }

    /// Load all tensors in the file into a `HashMap<name, Array2<f32>>`.
    pub fn load_all_tensors_f32(&self) -> ModelResult<HashMap<String, Array2<f32>>> {
        // Read the file once and reuse it for all tensors.
        let file_buf = std::fs::read(&self.file_path).map_err(|e| {
            ModelError::simple_load_error(format!(
                "Failed to read GGUF file {:?}: {}",
                self.file_path, e
            ))
        })?;

        let mut result = HashMap::with_capacity(self.tensors.len());
        for info in &self.tensors {
            let n_elements = info.n_elements() as usize;
            let byte_offset = (self.data_offset + info.offset) as usize;
            let byte_len = tensor_byte_size(info)?;
            let end = byte_offset + byte_len;

            if end > file_buf.len() {
                return Err(ModelError::simple_load_error(format!(
                    "Tensor '{}' data region [{}, {}) exceeds file size {}",
                    info.name,
                    byte_offset,
                    end,
                    file_buf.len()
                )));
            }
            let raw = &file_buf[byte_offset..end];
            let floats = dequant::dequantize(raw, &info.quant_type, n_elements)?;

            let (rows, cols) = shape_to_2d(&info.shape);
            let array = Array2::from_shape_vec((rows, cols), floats).map_err(|e| {
                ModelError::simple_load_error(format!(
                    "Failed to reshape tensor '{}' to ({}, {}): {}",
                    info.name, rows, cols, e
                ))
            })?;
            result.insert(info.name.clone(), array);
        }
        Ok(result)
    }

    /// Return the model architecture string from the `general.architecture` metadata key.
    pub fn architecture(&self) -> Option<&str> {
        match self.metadata.get("general.architecture") {
            Some(GgufMetaValue::String(s)) => Some(s.as_str()),
            _ => None,
        }
    }

    /// Build a lightweight inspection summary of this file.
    pub fn inspect(&self) -> GgufInspection {
        let tensor_names: Vec<String> = self.tensors.iter().map(|t| t.name.clone()).collect();
        let total_param_count: u64 = self.tensors.iter().map(|t| t.n_elements()).sum();

        // Collect unique quant type names
        let mut quant_set: std::collections::HashSet<&str> = std::collections::HashSet::new();
        for t in &self.tensors {
            quant_set.insert(t.quant_type.name());
        }
        let mut quant_types_used: Vec<String> =
            quant_set.into_iter().map(|s| s.to_owned()).collect();
        quant_types_used.sort();

        GgufInspection {
            version: self.version,
            tensor_count: self.tensors.len(),
            metadata_count: self.metadata.len(),
            architecture: self.architecture().map(|s| s.to_owned()),
            tensor_names,
            total_param_count,
            quant_types_used,
        }
    }

    /// Return a slice of all tensor names in file order.
    pub fn tensor_names(&self) -> Vec<&str> {
        self.tensors.iter().map(|t| t.name.as_str()).collect()
    }

    /// Load a single tensor lazily by name using an open `Read + Seek` handle.
    ///
    /// This avoids re-reading the whole file; instead it seeks directly to the
    /// tensor data region and reads only the required bytes. The result is a
    /// flat `Vec<f32>` of dequantized values.
    ///
    /// # Parameters
    /// - `reader`: An open handle to the GGUF file (must support both `Read` and `Seek`).
    /// - `info`:   The [`GgufTensorInfo`] descriptor for the tensor to load.
    pub fn load_tensor_lazy<R: std::io::Read + std::io::Seek>(
        reader: &mut R,
        info: &GgufTensorInfo,
    ) -> ModelResult<Vec<f32>> {
        use std::io::SeekFrom;

        let n_elements = info.n_elements() as usize;
        let byte_len = tensor_byte_size(info)?;

        // Seek to the absolute file offset of this tensor's data.
        reader
            .seek(SeekFrom::Start(info.data_offset))
            .map_err(|e| {
                ModelError::simple_load_error(format!(
                    "Failed to seek to tensor '{}' at offset {}: {}",
                    info.name, info.data_offset, e
                ))
            })?;

        let mut raw = vec![0u8; byte_len];
        reader.read_exact(&mut raw).map_err(|e| {
            ModelError::simple_load_error(format!(
                "Failed to read {} bytes for tensor '{}': {}",
                byte_len, info.name, e
            ))
        })?;

        dequant::dequantize(&raw, &info.quant_type, n_elements)
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Shape utilities
// ──────────────────────────────────────────────────────────────────────────────

/// Convert an n-dimensional shape to `(rows, cols)` for Array2.
/// - 0-D → (1, 1)
/// - 1-D `[n]` → (1, n)
/// - 2-D `[r, c]` → (r, c)
/// - N-D `[d0, …, d(n-1), dn]` → (d0*…*d(n-2), d(n-1))  [i.e. flatten all but last]
fn shape_to_2d(shape: &[u64]) -> (usize, usize) {
    match shape.len() {
        0 => (1, 1),
        1 => (1, shape[0] as usize),
        2 => (shape[0] as usize, shape[1] as usize),
        n => {
            let cols = shape[n - 1] as usize;
            let rows: u64 = shape[..n - 1].iter().product();
            (rows as usize, cols)
        }
    }
}

/// Compute the number of bytes occupied by a tensor's data on disk.
fn tensor_byte_size(info: &GgufTensorInfo) -> ModelResult<usize> {
    let n = info.n_elements() as usize;
    match info.quant_type {
        GgufQuantType::F32 => Ok(n * 4),
        GgufQuantType::F16 | GgufQuantType::BF16 => Ok(n * 2),
        GgufQuantType::Q4_0 => {
            // 32 elements per block, block = 18 bytes
            if !n.is_multiple_of(32) {
                return Err(ModelError::simple_load_error(format!(
                    "Q4_0 tensor '{}' has {} elements, not divisible by 32",
                    info.name, n
                )));
            }
            Ok((n / 32) * 18)
        }
        GgufQuantType::Q4_1 => {
            if !n.is_multiple_of(32) {
                return Err(ModelError::simple_load_error(format!(
                    "Q4_1 tensor '{}' has {} elements, not divisible by 32",
                    info.name, n
                )));
            }
            Ok((n / 32) * 20)
        }
        GgufQuantType::Q5_0 => {
            if !n.is_multiple_of(32) {
                return Err(ModelError::simple_load_error(format!(
                    "Q5_0 tensor '{}' has {} elements, not divisible by 32",
                    info.name, n
                )));
            }
            Ok((n / 32) * 22)
        }
        GgufQuantType::Q5_1 => {
            if !n.is_multiple_of(32) {
                return Err(ModelError::simple_load_error(format!(
                    "Q5_1 tensor '{}' has {} elements, not divisible by 32",
                    info.name, n
                )));
            }
            Ok((n / 32) * 24)
        }
        GgufQuantType::Q8_0 => {
            if !n.is_multiple_of(32) {
                return Err(ModelError::simple_load_error(format!(
                    "Q8_0 tensor '{}' has {} elements, not divisible by 32",
                    info.name, n
                )));
            }
            Ok((n / 32) * 34)
        }
        GgufQuantType::Q8_1 => {
            if !n.is_multiple_of(32) {
                return Err(ModelError::simple_load_error(format!(
                    "Q8_1 tensor '{}' has {} elements, not divisible by 32",
                    info.name, n
                )));
            }
            Ok((n / 32) * 36)
        }
        GgufQuantType::Q6K => {
            // 256 elements per block, block = 210 bytes
            if !n.is_multiple_of(256) {
                return Err(ModelError::simple_load_error(format!(
                    "Q6K tensor '{}' has {} elements, not divisible by 256",
                    info.name, n
                )));
            }
            Ok((n / 256) * 210)
        }
        GgufQuantType::Q2K => {
            if !n.is_multiple_of(256) {
                return Err(ModelError::simple_load_error(format!(
                    "Q2K tensor has {} elements, not divisible by 256",
                    n
                )));
            }
            Ok((n / 256) * 84)
        }
        GgufQuantType::Q3K => {
            if !n.is_multiple_of(256) {
                return Err(ModelError::simple_load_error(format!(
                    "Q3K tensor has {} elements, not divisible by 256",
                    n
                )));
            }
            Ok((n / 256) * 110)
        }
        GgufQuantType::Q4K => {
            if !n.is_multiple_of(256) {
                return Err(ModelError::simple_load_error(format!(
                    "Q4K tensor has {} elements, not divisible by 256",
                    n
                )));
            }
            Ok((n / 256) * 144)
        }
        GgufQuantType::Q5K => {
            if !n.is_multiple_of(256) {
                return Err(ModelError::simple_load_error(format!(
                    "Q5K tensor has {} elements, not divisible by 256",
                    n
                )));
            }
            Ok((n / 256) * 176)
        }
        GgufQuantType::Q8K => {
            if !n.is_multiple_of(256) {
                return Err(ModelError::simple_load_error(format!(
                    "Q8K tensor has {} elements, not divisible by 256",
                    n
                )));
            }
            Ok((n / 256) * 292)
        }
        // IQ types – variable block sizes; we return an error
        qt => Err(ModelError::simple_load_error(format!(
            "Cannot compute byte size for unsupported quant type {:?}",
            qt
        ))),
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Dequantization
// ──────────────────────────────────────────────────────────────────────────────

pub(crate) mod dequant {
    use super::GgufQuantType;
    use crate::error::{ModelError, ModelResult};
    use crate::gguf_dequant as kquant;

    /// Dequantize `data` bytes to `n_elements` f32 values according to `quant_type`.
    pub fn dequantize(
        data: &[u8],
        quant_type: &GgufQuantType,
        n_elements: usize,
    ) -> ModelResult<Vec<f32>> {
        match quant_type {
            GgufQuantType::F32 => dequant_f32(data, n_elements),
            GgufQuantType::F16 => dequant_f16(data, n_elements),
            GgufQuantType::BF16 => dequant_bf16(data, n_elements),
            GgufQuantType::Q4_0 => dequant_q4_0(data, n_elements),
            GgufQuantType::Q4_1 => dequant_q4_1(data, n_elements),
            GgufQuantType::Q5_0 => dequant_q5_0(data, n_elements),
            GgufQuantType::Q5_1 => dequant_q5_1(data, n_elements),
            GgufQuantType::Q8_0 => dequant_q8_0(data, n_elements),
            GgufQuantType::Q6K => dequant_q6_k(data, n_elements),
            GgufQuantType::Q2K => kquant::dequant_q2_k(data, n_elements),
            GgufQuantType::Q3K => kquant::dequant_q3_k(data, n_elements),
            GgufQuantType::Q4K => kquant::dequant_q4_k(data, n_elements),
            GgufQuantType::Q5K => kquant::dequant_q5_k(data, n_elements),
            GgufQuantType::Q8K => kquant::dequant_q8_k(data, n_elements),
            qt => Err(ModelError::simple_load_error(format!(
                "Unsupported quant type for dequantization: {:?}",
                qt
            ))),
        }
    }

    fn dequant_f32(data: &[u8], n: usize) -> ModelResult<Vec<f32>> {
        if data.len() < n * 4 {
            return Err(ModelError::simple_load_error(format!(
                "F32 tensor needs {} bytes, got {}",
                n * 4,
                data.len()
            )));
        }
        let mut out = Vec::with_capacity(n);
        for i in 0..n {
            let base = i * 4;
            let v =
                f32::from_le_bytes([data[base], data[base + 1], data[base + 2], data[base + 3]]);
            out.push(v);
        }
        Ok(out)
    }

    pub(super) fn dequant_f16(data: &[u8], n: usize) -> ModelResult<Vec<f32>> {
        if data.len() < n * 2 {
            return Err(ModelError::simple_load_error(format!(
                "F16 tensor needs {} bytes, got {}",
                n * 2,
                data.len()
            )));
        }
        let mut out = Vec::with_capacity(n);
        for i in 0..n {
            let base = i * 2;
            let bits = u16::from_le_bytes([data[base], data[base + 1]]);
            out.push(half::f16::from_bits(bits).to_f32());
        }
        Ok(out)
    }

    pub(super) fn dequant_bf16(data: &[u8], n: usize) -> ModelResult<Vec<f32>> {
        if data.len() < n * 2 {
            return Err(ModelError::simple_load_error(format!(
                "BF16 tensor needs {} bytes, got {}",
                n * 2,
                data.len()
            )));
        }
        let mut out = Vec::with_capacity(n);
        for i in 0..n {
            let base = i * 2;
            let bits = u16::from_le_bytes([data[base], data[base + 1]]);
            // BF16 → f32: sign+exp+7 mantissa bits occupy the upper 16 bits of f32
            out.push(f32::from_bits((bits as u32) << 16));
        }
        Ok(out)
    }

    /// Q4_0 block: 2 bytes delta (f16) + 16 bytes quantized nibbles → 32 f32
    ///
    /// Each nibble `q` represents `(q - 8) * delta`.
    pub(super) fn dequant_q4_0(data: &[u8], n: usize) -> ModelResult<Vec<f32>> {
        const BLOCK_ELEMS: usize = 32;
        const BLOCK_BYTES: usize = 18; // 2 delta + 16 nibbles
        if !n.is_multiple_of(BLOCK_ELEMS) {
            return Err(ModelError::simple_load_error(format!(
                "Q4_0: n_elements {} not divisible by {}",
                n, BLOCK_ELEMS
            )));
        }
        let n_blocks = n / BLOCK_ELEMS;
        if data.len() < n_blocks * BLOCK_BYTES {
            return Err(ModelError::simple_load_error("Q4_0 data buffer too small"));
        }
        let mut out = Vec::with_capacity(n);
        for b in 0..n_blocks {
            let base = b * BLOCK_BYTES;
            let delta_bits = u16::from_le_bytes([data[base], data[base + 1]]);
            let delta = half::f16::from_bits(delta_bits).to_f32();
            for byte_idx in 0..16usize {
                let byte = data[base + 2 + byte_idx];
                let lo = (byte & 0x0F) as i32 - 8;
                let hi = ((byte >> 4) & 0x0F) as i32 - 8;
                out.push(lo as f32 * delta);
                out.push(hi as f32 * delta);
            }
        }
        Ok(out)
    }

    /// Q4_1 block: 2 bytes delta (f16) + 2 bytes min (f16) + 16 bytes nibbles → 32 f32
    ///
    /// Each nibble `q` represents `q * delta + min`.
    pub(super) fn dequant_q4_1(data: &[u8], n: usize) -> ModelResult<Vec<f32>> {
        const BLOCK_ELEMS: usize = 32;
        const BLOCK_BYTES: usize = 20;
        if !n.is_multiple_of(BLOCK_ELEMS) {
            return Err(ModelError::simple_load_error(format!(
                "Q4_1: n_elements {} not divisible by {}",
                n, BLOCK_ELEMS
            )));
        }
        let n_blocks = n / BLOCK_ELEMS;
        if data.len() < n_blocks * BLOCK_BYTES {
            return Err(ModelError::simple_load_error("Q4_1 data buffer too small"));
        }
        let mut out = Vec::with_capacity(n);
        for b in 0..n_blocks {
            let base = b * BLOCK_BYTES;
            let delta_bits = u16::from_le_bytes([data[base], data[base + 1]]);
            let delta = half::f16::from_bits(delta_bits).to_f32();
            let min_bits = u16::from_le_bytes([data[base + 2], data[base + 3]]);
            let min = half::f16::from_bits(min_bits).to_f32();
            for byte_idx in 0..16usize {
                let byte = data[base + 4 + byte_idx];
                let lo = (byte & 0x0F) as f32;
                let hi = ((byte >> 4) & 0x0F) as f32;
                out.push(lo * delta + min);
                out.push(hi * delta + min);
            }
        }
        Ok(out)
    }

    /// Q5_0 block: 2 bytes delta (f16) + 4 bytes high bits (u32) + 16 bytes low nibbles → 32 f32
    ///
    /// Each 5-bit value `q` (range 0–31, then subtract 16) scaled by delta.
    pub(super) fn dequant_q5_0(data: &[u8], n: usize) -> ModelResult<Vec<f32>> {
        const BLOCK_ELEMS: usize = 32;
        const BLOCK_BYTES: usize = 22;
        if !n.is_multiple_of(BLOCK_ELEMS) {
            return Err(ModelError::simple_load_error(format!(
                "Q5_0: n_elements {} not divisible by {}",
                n, BLOCK_ELEMS
            )));
        }
        let n_blocks = n / BLOCK_ELEMS;
        if data.len() < n_blocks * BLOCK_BYTES {
            return Err(ModelError::simple_load_error("Q5_0 data buffer too small"));
        }
        let mut out = Vec::with_capacity(n);
        for b in 0..n_blocks {
            let base = b * BLOCK_BYTES;
            let delta_bits = u16::from_le_bytes([data[base], data[base + 1]]);
            let delta = half::f16::from_bits(delta_bits).to_f32();
            // High bits: bit i of qh → 5th bit of element i
            let qh = u32::from_le_bytes([
                data[base + 2],
                data[base + 3],
                data[base + 4],
                data[base + 5],
            ]);
            for byte_idx in 0..16usize {
                let byte = data[base + 6 + byte_idx];
                let lo4 = (byte & 0x0F) as u32;
                let hi4 = ((byte >> 4) & 0x0F) as u32;
                let elem_lo = byte_idx * 2;
                let elem_hi = byte_idx * 2 + 1;
                let hi_lo = (qh >> elem_lo) & 1;
                let hi_hi = (qh >> elem_hi) & 1;
                let q_lo = (lo4 | (hi_lo << 4)) as i32 - 16;
                let q_hi = (hi4 | (hi_hi << 4)) as i32 - 16;
                out.push(q_lo as f32 * delta);
                out.push(q_hi as f32 * delta);
            }
        }
        Ok(out)
    }

    /// Q5_1 block: 2 bytes delta (f16) + 2 bytes min (f16) + 4 bytes high bits + 16 bytes → 32 f32
    pub(super) fn dequant_q5_1(data: &[u8], n: usize) -> ModelResult<Vec<f32>> {
        const BLOCK_ELEMS: usize = 32;
        const BLOCK_BYTES: usize = 24;
        if !n.is_multiple_of(BLOCK_ELEMS) {
            return Err(ModelError::simple_load_error(format!(
                "Q5_1: n_elements {} not divisible by {}",
                n, BLOCK_ELEMS
            )));
        }
        let n_blocks = n / BLOCK_ELEMS;
        if data.len() < n_blocks * BLOCK_BYTES {
            return Err(ModelError::simple_load_error("Q5_1 data buffer too small"));
        }
        let mut out = Vec::with_capacity(n);
        for b in 0..n_blocks {
            let base = b * BLOCK_BYTES;
            let delta_bits = u16::from_le_bytes([data[base], data[base + 1]]);
            let delta = half::f16::from_bits(delta_bits).to_f32();
            let min_bits = u16::from_le_bytes([data[base + 2], data[base + 3]]);
            let min = half::f16::from_bits(min_bits).to_f32();
            let qh = u32::from_le_bytes([
                data[base + 4],
                data[base + 5],
                data[base + 6],
                data[base + 7],
            ]);
            for byte_idx in 0..16usize {
                let byte = data[base + 8 + byte_idx];
                let lo4 = (byte & 0x0F) as u32;
                let hi4 = ((byte >> 4) & 0x0F) as u32;
                let elem_lo = byte_idx * 2;
                let elem_hi = byte_idx * 2 + 1;
                let hi_lo = (qh >> elem_lo) & 1;
                let hi_hi = (qh >> elem_hi) & 1;
                let q_lo = (lo4 | (hi_lo << 4)) as f32;
                let q_hi = (hi4 | (hi_hi << 4)) as f32;
                out.push(q_lo * delta + min);
                out.push(q_hi * delta + min);
            }
        }
        Ok(out)
    }

    /// Q8_0 block: 2 bytes delta (f16) + 32 bytes i8 values → 32 f32
    ///
    /// Each i8 value `q` is scaled: `q * delta`.
    pub(super) fn dequant_q8_0(data: &[u8], n: usize) -> ModelResult<Vec<f32>> {
        const BLOCK_ELEMS: usize = 32;
        const BLOCK_BYTES: usize = 34;
        if !n.is_multiple_of(BLOCK_ELEMS) {
            return Err(ModelError::simple_load_error(format!(
                "Q8_0: n_elements {} not divisible by {}",
                n, BLOCK_ELEMS
            )));
        }
        let n_blocks = n / BLOCK_ELEMS;
        if data.len() < n_blocks * BLOCK_BYTES {
            return Err(ModelError::simple_load_error("Q8_0 data buffer too small"));
        }
        let mut out = Vec::with_capacity(n);
        for b in 0..n_blocks {
            let base = b * BLOCK_BYTES;
            let delta_bits = u16::from_le_bytes([data[base], data[base + 1]]);
            let delta = half::f16::from_bits(delta_bits).to_f32();
            for i in 0..BLOCK_ELEMS {
                let q = data[base + 2 + i] as i8;
                out.push(q as f32 * delta);
            }
        }
        Ok(out)
    }

    /// Q6_K block: 210 bytes → 256 f32 elements.
    ///
    /// Block layout:
    /// - 128 bytes: low 4 bits of each 6-bit value, packed as nibbles (ql)
    /// - 64 bytes:  high 2 bits for groups of 4, packed 4-per-byte (qh)
    /// - 16 bytes:  sub-block scales (i8, one per 16 elements)
    /// - 2 bytes:   block scale delta (f16)
    pub(super) fn dequant_q6_k(data: &[u8], n: usize) -> ModelResult<Vec<f32>> {
        const BLOCK_ELEMS: usize = 256;
        const BLOCK_BYTES: usize = 210;
        if !n.is_multiple_of(BLOCK_ELEMS) {
            return Err(ModelError::simple_load_error(format!(
                "Q6K: n_elements {} not divisible by {}",
                n, BLOCK_ELEMS
            )));
        }
        let n_blocks = n / BLOCK_ELEMS;
        if data.len() < n_blocks * BLOCK_BYTES {
            return Err(ModelError::simple_load_error("Q6K data buffer too small"));
        }
        let mut out = Vec::with_capacity(n);
        for b in 0..n_blocks {
            let base = b * BLOCK_BYTES;
            // ql: 128 bytes (low 4 bits, packed nibbles)
            let ql = &data[base..base + 128];
            // qh: 64 bytes (high 2 bits, 4 per byte)
            let qh = &data[base + 128..base + 192];
            // scales: 16 i8 values
            let scales_raw = &data[base + 192..base + 208];
            // delta: f16
            let delta_bits = u16::from_le_bytes([data[base + 208], data[base + 209]]);
            let delta = half::f16::from_bits(delta_bits).to_f32();

            // Reconstruct 256 6-bit values
            // ql[i] holds nibbles for element i and i+128 (lower 4 bits, upper 4 bits)
            // qh[i] holds high bits for 4 consecutive pairs
            for i in 0..128usize {
                // high bits byte index and bit positions
                let qh_byte = qh[i / 2];
                let shift_lo = (i % 2) * 4; // bits [shift_lo+1 : shift_lo] for even element
                let shift_hi = (i % 2) * 4 + 2; // bits [shift_hi+1 : shift_hi] for odd element (128+i)

                let q_lo_low4 = ql[i] & 0x0F;
                let q_hi_low4 = (ql[i] >> 4) & 0x0F;

                let q_lo_high2 = (qh_byte >> shift_lo) & 0x03;
                let q_hi_high2 = (qh_byte >> shift_hi) & 0x03;

                let q_lo = ((q_lo_high2 << 4) | q_lo_low4) as i32 - 32;
                let q_hi = ((q_hi_high2 << 4) | q_hi_low4) as i32 - 32;

                // Scale: one i8 per 16 elements → 16 sub-blocks of 16 elements each
                let scale_idx_lo = (i * 2) / 16; // element i*2 / 16
                let scale_idx_hi = (i * 2 + 1) / 16;

                if scale_idx_lo >= 16 || scale_idx_hi >= 16 {
                    return Err(ModelError::simple_load_error(
                        "Q6K scale index out of range",
                    ));
                }
                let scale_lo = scales_raw[scale_idx_lo] as i8 as f32;
                let scale_hi = scales_raw[scale_idx_hi] as i8 as f32;

                out.push(delta * scale_lo * q_lo as f32);
                out.push(delta * scale_hi * q_hi as f32);
            }
        }
        Ok(out)
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Tests
// ──────────────────────────────────────────────────────────────────────────────

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

    // ── Helper: write a minimal GGUF header to `buf` ──────────────────────────

    /// Write GGUF magic, version, tensor_count, and kv_count to `buf`.
    fn write_gguf_header(buf: &mut Vec<u8>, version: u32, tensor_count: u64, kv_count: u64) {
        buf.extend_from_slice(b"GGUF");
        buf.extend_from_slice(&version.to_le_bytes());
        if version >= 2 {
            buf.extend_from_slice(&tensor_count.to_le_bytes());
            buf.extend_from_slice(&kv_count.to_le_bytes());
        } else {
            buf.extend_from_slice(&(tensor_count as u32).to_le_bytes());
            buf.extend_from_slice(&(kv_count as u32).to_le_bytes());
        }
    }

    /// Write a v2+ GGUF string (u64 length prefix).
    fn write_str_v2(buf: &mut Vec<u8>, s: &str) {
        let bytes = s.as_bytes();
        buf.extend_from_slice(&(bytes.len() as u64).to_le_bytes());
        buf.extend_from_slice(bytes);
    }

    /// Write a v1 GGUF string (u32 length prefix).
    #[allow(dead_code)]
    fn write_str_v1(buf: &mut Vec<u8>, s: &str) {
        let bytes = s.as_bytes();
        buf.extend_from_slice(&(bytes.len() as u32).to_le_bytes());
        buf.extend_from_slice(bytes);
    }

    /// Pad `buf` to 32-byte alignment.
    fn pad_to_32(buf: &mut Vec<u8>) {
        let rem = buf.len() % 32;
        if rem != 0 {
            let pad = 32 - rem;
            buf.extend(std::iter::repeat_n(0u8, pad));
        }
    }

    // ── Test 1 ────────────────────────────────────────────────────────────────

    #[test]
    fn test_magic_validation() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_bad_magic.bin");
        let mut buf = Vec::new();
        buf.extend_from_slice(b"XXXX");
        buf.extend_from_slice(&2u32.to_le_bytes());
        buf.extend_from_slice(&0u64.to_le_bytes());
        buf.extend_from_slice(&0u64.to_le_bytes());
        std::fs::write(&path, &buf).unwrap();
        let result = GgufFile::open(&path);
        assert!(result.is_err(), "Expected error for bad magic");
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 2 ────────────────────────────────────────────────────────────────

    #[test]
    fn test_version_1_parse() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_v1_empty.bin");
        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 1, 0, 0);
        pad_to_32(&mut buf);
        std::fs::write(&path, &buf).unwrap();
        let file = GgufFile::open(&path).expect("v1 parse failed");
        assert_eq!(file.version, 1);
        assert!(file.tensors.is_empty());
        assert!(file.metadata.is_empty());
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 3 ────────────────────────────────────────────────────────────────

    #[test]
    fn test_version_2_parse() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_v2_empty.bin");
        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 2, 0, 0);
        pad_to_32(&mut buf);
        std::fs::write(&path, &buf).unwrap();
        let file = GgufFile::open(&path).expect("v2 parse failed");
        assert_eq!(file.version, 2);
        assert!(file.tensors.is_empty());
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 4 ────────────────────────────────────────────────────────────────

    #[test]
    fn test_version_3_parse() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_v3_empty.bin");
        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 3, 0, 0);
        pad_to_32(&mut buf);
        std::fs::write(&path, &buf).unwrap();
        let file = GgufFile::open(&path).expect("v3 parse failed");
        assert_eq!(file.version, 3);
        assert!(file.tensors.is_empty());
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 5 ────────────────────────────────────────────────────────────────

    #[test]
    fn test_metadata_uint32() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_meta_uint32.bin");
        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 2, 0, 1);
        // key
        write_str_v2(&mut buf, "my.key");
        // value type = Uint32 = 4
        buf.extend_from_slice(&4u32.to_le_bytes());
        // value
        buf.extend_from_slice(&42u32.to_le_bytes());
        pad_to_32(&mut buf);
        std::fs::write(&path, &buf).unwrap();
        let file = GgufFile::open(&path).expect("parse failed");
        match file.metadata.get("my.key") {
            Some(GgufMetaValue::Uint32(v)) => assert_eq!(*v, 42u32),
            other => panic!("Expected Uint32(42), got {:?}", other),
        }
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 6 ────────────────────────────────────────────────────────────────

    #[test]
    fn test_metadata_string() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_meta_string.bin");
        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 2, 0, 1);
        write_str_v2(&mut buf, "general.name");
        buf.extend_from_slice(&8u32.to_le_bytes()); // String type = 8
        write_str_v2(&mut buf, "test-model");
        pad_to_32(&mut buf);
        std::fs::write(&path, &buf).unwrap();
        let file = GgufFile::open(&path).expect("parse failed");
        match file.metadata.get("general.name") {
            Some(GgufMetaValue::String(s)) => assert_eq!(s, "test-model"),
            other => panic!("Expected String, got {:?}", other),
        }
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 7 ────────────────────────────────────────────────────────────────

    #[test]
    fn test_metadata_float32() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_meta_float32.bin");
        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 2, 0, 1);
        write_str_v2(&mut buf, "param.scale");
        buf.extend_from_slice(&6u32.to_le_bytes()); // Float32 = 6
        buf.extend_from_slice(&std::f32::consts::PI.to_le_bytes());
        pad_to_32(&mut buf);
        std::fs::write(&path, &buf).unwrap();
        let file = GgufFile::open(&path).expect("parse failed");
        match file.metadata.get("param.scale") {
            Some(GgufMetaValue::Float32(v)) => assert!((v - std::f32::consts::PI).abs() < 1e-5),
            other => panic!("Expected Float32, got {:?}", other),
        }
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 8 ────────────────────────────────────────────────────────────────

    #[test]
    fn test_metadata_array_uint32() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_meta_array_uint32.bin");
        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 2, 0, 1);
        write_str_v2(&mut buf, "layer.sizes");
        buf.extend_from_slice(&9u32.to_le_bytes()); // Array = 9
        buf.extend_from_slice(&4u32.to_le_bytes()); // elem type = Uint32
        buf.extend_from_slice(&3u64.to_le_bytes()); // count = 3
        buf.extend_from_slice(&10u32.to_le_bytes());
        buf.extend_from_slice(&20u32.to_le_bytes());
        buf.extend_from_slice(&30u32.to_le_bytes());
        pad_to_32(&mut buf);
        std::fs::write(&path, &buf).unwrap();
        let file = GgufFile::open(&path).expect("parse failed");
        match file.metadata.get("layer.sizes") {
            Some(GgufMetaValue::Array(arr)) => {
                assert_eq!(arr.len(), 3);
                match (&arr[0], &arr[1], &arr[2]) {
                    (
                        GgufMetaValue::Uint32(a),
                        GgufMetaValue::Uint32(b),
                        GgufMetaValue::Uint32(c),
                    ) => {
                        assert_eq!(*a, 10);
                        assert_eq!(*b, 20);
                        assert_eq!(*c, 30);
                    }
                    _ => panic!("Unexpected array element types"),
                }
            }
            other => panic!("Expected Array, got {:?}", other),
        }
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 9 ────────────────────────────────────────────────────────────────

    #[test]
    fn test_f32_tensor_load() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_f32_tensor.bin");
        // 2x3 f32 tensor
        let values: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 2, 1, 0);
        // Tensor info
        write_str_v2(&mut buf, "my_tensor");
        buf.extend_from_slice(&2u32.to_le_bytes()); // n_dims = 2
        buf.extend_from_slice(&3u64.to_le_bytes()); // dim0 = 3 (inner)
        buf.extend_from_slice(&2u64.to_le_bytes()); // dim1 = 2 (outer)
        buf.extend_from_slice(&0u32.to_le_bytes()); // quant type F32 = 0
        buf.extend_from_slice(&0u64.to_le_bytes()); // offset = 0
        pad_to_32(&mut buf);
        // Data section
        for v in &values {
            buf.extend_from_slice(&v.to_le_bytes());
        }
        std::fs::write(&path, &buf).unwrap();
        let file = GgufFile::open(&path).expect("parse failed");
        let arr = file.load_tensor_f32("my_tensor").expect("load failed");
        // GGUF dims are inner-first, so dim0=3, dim1=2 → shape_to_2d gives (dim0=3 inner, dim1=2 outer) → (2, 3) after shape_to_2d
        // shape = [3, 2] → shape_to_2d returns (3, 2) since shape[0]=3, shape[1]=2
        assert_eq!(arr.nrows() * arr.ncols(), 6);
        // Verify all values are present
        let flat: Vec<f32> = arr.iter().cloned().collect();
        for v in &values {
            assert!(flat.contains(v), "Value {} not found", v);
        }
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 10 ───────────────────────────────────────────────────────────────

    #[test]
    fn test_f16_tensor_load() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_f16_tensor.bin");
        // 4x4 f16 tensor = 16 elements
        let n: usize = 16;
        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 2, 1, 0);
        write_str_v2(&mut buf, "f16_tensor");
        buf.extend_from_slice(&2u32.to_le_bytes()); // n_dims = 2
        buf.extend_from_slice(&4u64.to_le_bytes()); // dim0 = 4
        buf.extend_from_slice(&4u64.to_le_bytes()); // dim1 = 4
        buf.extend_from_slice(&1u32.to_le_bytes()); // quant F16 = 1
        buf.extend_from_slice(&0u64.to_le_bytes()); // offset = 0
        pad_to_32(&mut buf);
        // Data: 16 f16 values
        for i in 0..n {
            let val = half::f16::from_f32(i as f32 * 0.5);
            buf.extend_from_slice(&val.to_bits().to_le_bytes());
        }
        std::fs::write(&path, &buf).unwrap();
        let file = GgufFile::open(&path).expect("parse failed");
        let arr = file.load_tensor_f32("f16_tensor").expect("load failed");
        assert_eq!(arr.nrows(), 4);
        assert_eq!(arr.ncols(), 4);
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 11 ───────────────────────────────────────────────────────────────

    #[test]
    fn test_q4_0_tensor_load() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_q4_0_tensor.bin");
        // 32 elements = 1 Q4_0 block (18 bytes)
        // delta = 1.0 (as f16), all nibbles = 8 (maps to 0 after -8)
        let n: usize = 32;
        let delta_f16 = half::f16::from_f32(1.0);
        let mut block = Vec::new();
        block.extend_from_slice(&delta_f16.to_bits().to_le_bytes()); // 2 bytes delta
        block.extend(std::iter::repeat_n(0x88u8, 16)); // nibbles = 8 (lo=8,hi=8)

        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 2, 1, 0);
        write_str_v2(&mut buf, "q4_tensor");
        buf.extend_from_slice(&1u32.to_le_bytes()); // n_dims = 1
        buf.extend_from_slice(&(n as u64).to_le_bytes()); // dim0 = 32
        buf.extend_from_slice(&2u32.to_le_bytes()); // Q4_0 = 2
        buf.extend_from_slice(&0u64.to_le_bytes()); // offset = 0
        pad_to_32(&mut buf);
        buf.extend_from_slice(&block);
        std::fs::write(&path, &buf).unwrap();
        let file = GgufFile::open(&path).expect("parse failed");
        let arr = file.load_tensor_f32("q4_tensor").expect("load failed");
        assert_eq!(arr.nrows() * arr.ncols(), n);
        // nibble 8 - 8 = 0, so all output values should be 0.0
        for v in arr.iter() {
            assert_eq!(*v, 0.0f32, "Expected 0.0, got {}", v);
        }
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 12 ───────────────────────────────────────────────────────────────

    #[test]
    fn test_architecture_extraction() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_arch.bin");
        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 2, 0, 1);
        write_str_v2(&mut buf, "general.architecture");
        buf.extend_from_slice(&8u32.to_le_bytes()); // String
        write_str_v2(&mut buf, "llama");
        pad_to_32(&mut buf);
        std::fs::write(&path, &buf).unwrap();
        let file = GgufFile::open(&path).expect("parse failed");
        assert_eq!(file.architecture(), Some("llama"));
        let _ = std::fs::remove_file(&path);
    }

    // ── Test 13 ───────────────────────────────────────────────────────────────

    #[test]
    fn test_multi_tensor_loading() {
        let dir = std::env::temp_dir();
        let path = dir.join("gguf_multi_tensor.bin");
        // Two 1D tensors of 4 f32 elements each
        let vals_a: [f32; 4] = [1.0, 2.0, 3.0, 4.0];
        let vals_b: [f32; 4] = [5.0, 6.0, 7.0, 8.0];
        let bytes_per_tensor: u64 = 4 * 4; // 4 elements × 4 bytes

        let mut buf = Vec::new();
        write_gguf_header(&mut buf, 2, 2, 0);

        // Tensor A info
        write_str_v2(&mut buf, "tensor_a");
        buf.extend_from_slice(&1u32.to_le_bytes()); // n_dims = 1
        buf.extend_from_slice(&4u64.to_le_bytes()); // dim0 = 4
        buf.extend_from_slice(&0u32.to_le_bytes()); // F32 = 0
        buf.extend_from_slice(&0u64.to_le_bytes()); // offset = 0

        // Tensor B info
        write_str_v2(&mut buf, "tensor_b");
        buf.extend_from_slice(&1u32.to_le_bytes()); // n_dims = 1
        buf.extend_from_slice(&4u64.to_le_bytes()); // dim0 = 4
        buf.extend_from_slice(&0u32.to_le_bytes()); // F32 = 0
        buf.extend_from_slice(&bytes_per_tensor.to_le_bytes()); // offset after first tensor

        pad_to_32(&mut buf);
        // Data: tensor A then tensor B
        for v in &vals_a {
            buf.extend_from_slice(&v.to_le_bytes());
        }
        for v in &vals_b {
            buf.extend_from_slice(&v.to_le_bytes());
        }
        std::fs::write(&path, &buf).unwrap();

        let file = GgufFile::open(&path).expect("parse failed");
        let all = file.load_all_tensors_f32().expect("load all failed");
        assert!(all.contains_key("tensor_a"), "tensor_a missing");
        assert!(all.contains_key("tensor_b"), "tensor_b missing");

        let a = &all["tensor_a"];
        let b = &all["tensor_b"];
        assert_eq!(a.nrows() * a.ncols(), 4);
        assert_eq!(b.nrows() * b.ncols(), 4);

        let flat_a: Vec<f32> = a.iter().cloned().collect();
        let flat_b: Vec<f32> = b.iter().cloned().collect();
        assert_eq!(flat_a, vals_a);
        assert_eq!(flat_b, vals_b);

        let _ = std::fs::remove_file(&path);
    }
}