cortex-hybrid 0.2.0

Backend-neutral ANN stage layer and deterministic Vec reference backend for hybrid ANN/SNN systems.
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Reference-backend binding over engram-parser checkpoints.
//!
//! Format parsing, tensor inventory, raw payloads, and packed dequantization
//! belong to published `engram-parser` 0.3.0 (`features = ["safetensors"]`;
//! the `mmap` feature is not enabled). This module does not walk GGUF
//! headers or Safetensors JSON. `open_checkpoint` chooses the backend.
//! Malformed input returns that error; there is no in-tree parser to fall
//! back to. [`MappedCheckpoint::bind_router_weights`] copies the decoded
//! gate and the token-embedding payload, then drops the engram checkpoint.
//! A live router does not keep the rest of the file. Mapping policy stays
//! in engram-parser.
//!
//! # Migration shim
//! [`MappedCheckpoint`] exists so [`super::MoeRouter`] can keep its load,
//! token-extract, and gate API while weight selection still lives here.
//! Remove the shim when callers take an [`engram_parser::Checkpoint`] and
//! perform this binding outside the crate. Do not add format parsers or
//! mmap policy here. Token rows call engram's Q8_0, Q5_K, and Q6_K
//! decoders. IQ3_S stays an explicit error; engram 0.3.0 does not decode
//! the IQ3_S or IQ3_XXS wire types.
//!
//! `Checkpoint::tensor_bytes` does not promise `f32` alignment. Values are
//! copied with [`f32::from_le_bytes`], so this module has no `unsafe`.

use engram_parser::checkpoint::{Checkpoint, MetadataValue, TensorDType, TensorInfo};
use engram_parser::gguf::{
    DType, dequantize_q5_k, dequantize_q6_k, dequantize_q8_0, f16_bits_to_f32, packed_row_size,
};
use engram_parser::{ParserError, open_checkpoint};
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::path::Path;

use crate::error::{HybridError, Result};

/// Validated engram-parser release this binding was written against.
#[cfg(test)]
pub(super) const ENGRAM_PARSER_VERSION: &str = "0.3.0";

/// Decoded rank-2 F32 gate, bound once per loaded router.
#[derive(Debug, Clone)]
pub(super) struct BoundGate {
    pub(super) dims: Vec<usize>,
    pub(super) weights: Vec<f32>,
}

/// Router-facing view of one opened checkpoint.
///
/// `inner` is the engram checkpoint. [`Self::bind_router_weights`] copies the
/// tensors the router scores and then drops `inner`, so the full GGUF buffer
/// or Safetensors file handles are not retained.
#[derive(Debug)]
pub(super) struct MappedCheckpoint {
    metadata: engram_parser::checkpoint::CheckpointMetadata,
    tensors: Vec<TensorInfo>,
    payloads: HashMap<String, Vec<u8>>,
    inner: Option<engram_parser::checkpoint::AnyCheckpoint>,
}

/// Outermost-first shape plus the dtypes the reference backend binds.
#[derive(Debug, Clone, PartialEq)]
pub(super) struct TensorView {
    pub(super) dims: Vec<usize>,
    pub(super) kind: TensorKind,
}

/// Dtypes the reference router knows how to bind. Anything else stays a label.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum TensorKind {
    F32,
    F16,
    Q8Block,
    Q5KBlock,
    Q6KBlock,
    Iq3S,
    Other(String),
}

impl fmt::Display for TensorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::F32 => f.write_str("F32"),
            Self::F16 => f.write_str("F16"),
            Self::Q8Block => f.write_str("Q8_0"),
            Self::Q5KBlock => f.write_str("Q5_K"),
            Self::Q6KBlock => f.write_str("Q6_K"),
            Self::Iq3S => f.write_str("IQ3_S"),
            Self::Other(label) => f.write_str(label),
        }
    }
}

impl MappedCheckpoint {
    pub(super) fn architecture(&self) -> &str {
        self.metadata
            .get("general.architecture")
            .and_then(MetadataValue::as_str)
            .filter(|value| !value.is_empty())
            .unwrap_or("unknown")
    }

    pub(super) fn quantization(&self) -> String {
        quantization_label(&self.metadata)
    }

    pub(super) fn numeric(&self, key: &str) -> Option<usize> {
        metadata_usize(&self.metadata, key)
    }

    pub(super) fn tensor_names(&self) -> impl Iterator<Item = &str> {
        self.tensors.iter().map(|tensor| tensor.name.as_str())
    }

    pub(super) fn has_tensor(&self, name: &str) -> bool {
        self.tensors.iter().any(|tensor| tensor.name == name)
    }

    /// Names ending in `suffix`, lowest layer index first.
    ///
    /// Layer indexes come from `blk.N`, `block(s).N`, `layer(s).N`. Names
    /// with no such component sort after every numbered layer, then by name.
    pub(super) fn tensor_names_with_suffix(&self, suffix: &str) -> Vec<String> {
        let mut matches: Vec<&str> = self
            .tensor_names()
            .filter(|name| name.ends_with(suffix))
            .collect();
        matches.sort_unstable_by_key(|name| tensor_block_sort_key(name));
        matches.into_iter().map(str::to_owned).collect()
    }

    pub(super) fn find_first_tensor_with_suffix(&self, suffix: &str) -> Option<String> {
        self.tensor_names_with_suffix(suffix).into_iter().next()
    }

    /// Copy the gate and the token-embedding payload, then release the
    /// engram checkpoint. Later reads of any other tensor fail.
    pub(super) fn bind_router_weights(
        &mut self,
        token_name: &str,
        gate_name: &str,
        path: &str,
    ) -> Result<BoundGate> {
        let view = self.tensor_view(gate_name, path)?;
        if view.kind != TensorKind::F32 {
            return Err(HybridError::UnsupportedFormat(format!(
                "tensor '{gate_name}' must be F32, got {}",
                view.kind
            )));
        }
        let gate_bytes = self.read_payload(gate_name, path)?;
        let elements = element_count(&view.dims, path, gate_name)?;
        let weights = decode_f32(&gate_bytes, elements, path, gate_name)?;
        let token_bytes = self.read_payload(token_name, path)?;
        self.payloads.insert(token_name.to_owned(), token_bytes);
        self.inner = None;
        Ok(BoundGate {
            dims: view.dims,
            weights,
        })
    }

    pub(super) fn tensor_view(&self, name: &str, path: &str) -> Result<TensorView> {
        let info = self.tensor_info(name, path)?;
        Ok(TensorView {
            dims: info.shape.dims().to_vec(),
            kind: kind_of(info),
        })
    }

    #[cfg(test)]
    pub(super) fn f32_tensor(&self, name: &str, path: &str) -> Result<Vec<f32>> {
        let view = self.tensor_view(name, path)?;
        if view.kind != TensorKind::F32 {
            return Err(HybridError::UnsupportedFormat(format!(
                "tensor '{name}' must be F32, got {}",
                view.kind
            )));
        }
        let bytes = self.payload(name, path)?;
        let elements = element_count(&view.dims, path, name)?;
        decode_f32(&bytes, elements, path, name)
    }

    pub(super) fn extract_token_embedding(
        &self,
        tensor_name: &str,
        path: &str,
        token_id: usize,
    ) -> Result<Vec<f32>> {
        let view = self.tensor_view(tensor_name, path)?;
        let (vocab, hidden) = token_embedding_dims(&view, tensor_name, path)?;
        if token_id >= vocab {
            return Err(HybridError::TokenIndex {
                index: token_id,
                vocab_size: vocab,
            });
        }
        let bytes = self.payload(tensor_name, path)?;
        match view.kind {
            TensorKind::F32 => {
                let row_size = hidden
                    .checked_mul(std::mem::size_of::<f32>())
                    .ok_or_else(|| row_overflow(path, tensor_name))?;
                let row = row_bytes(&bytes, row_size, token_id, path, tensor_name)?;
                decode_f32(row, hidden, path, tensor_name)
            }
            TensorKind::F16 => {
                let row_size = hidden
                    .checked_mul(std::mem::size_of::<u16>())
                    .ok_or_else(|| row_overflow(path, tensor_name))?;
                let row = row_bytes(&bytes, row_size, token_id, path, tensor_name)?;
                decode_f16(row, hidden, path, tensor_name)
            }
            TensorKind::Q8Block => dequantize_row(
                DType::Q8_0,
                &bytes,
                hidden,
                token_id,
                path,
                tensor_name,
                dequantize_q8_0,
            ),
            TensorKind::Q5KBlock => dequantize_row(
                DType::Q5_K,
                &bytes,
                hidden,
                token_id,
                path,
                tensor_name,
                dequantize_q5_k,
            ),
            TensorKind::Q6KBlock => dequantize_row(
                DType::Q6_K,
                &bytes,
                hidden,
                token_id,
                path,
                tensor_name,
                dequantize_q6_k,
            ),
            TensorKind::Iq3S => Err(HybridError::UnsupportedFormat(format!(
                "tensor '{tensor_name}' uses IQ3_S token embeddings; use llama.cpp prompt embeddings for this checkpoint"
            ))),
            TensorKind::Other(label) => Err(HybridError::UnsupportedFormat(format!(
                "tensor '{tensor_name}' has unsupported dtype {label}"
            ))),
        }
    }

    fn tensor_info<'a>(&'a self, name: &str, path: &str) -> Result<&'a TensorInfo> {
        self.tensors
            .iter()
            .find(|tensor| tensor.name == name)
            .ok_or_else(|| HybridError::MissingTensor {
                name: name.to_owned(),
                path: path.to_owned(),
            })
    }

    /// Whole-tensor bytes, borrowed whenever the source already holds them.
    ///
    /// A bound payload and an in-memory GGUF checkpoint are borrowed, so a
    /// token-row read copies only the row it decodes. Safetensors backends
    /// hand back an owned read of the tensor; that copy is engram-parser's.
    fn payload<'a>(&'a self, name: &str, path: &str) -> Result<Cow<'a, [u8]>> {
        if let Some(bytes) = self.payloads.get(name) {
            return Ok(Cow::Borrowed(bytes));
        }
        let Some(inner) = self.inner.as_ref() else {
            return Err(unbound_tensor(name, path));
        };
        inner.tensor_bytes(name).map_err(map_parser_error)
    }

    fn read_payload(&self, name: &str, path: &str) -> Result<Vec<u8>> {
        if let Some(bytes) = self.payloads.get(name) {
            return Ok(bytes.clone());
        }
        let Some(inner) = self.inner.as_ref() else {
            return Err(unbound_tensor(name, path));
        };
        let bytes = inner.tensor_bytes(name).map_err(map_parser_error)?;
        Ok(bytes.into_owned())
    }
}

fn unbound_tensor(name: &str, path: &str) -> HybridError {
    HybridError::ModelLoad {
        path: path.to_owned(),
        reason: format!("tensor '{name}' is not bound"),
    }
}

pub(super) fn open_mapped_checkpoint(path: &str) -> Result<MappedCheckpoint> {
    let inner = open_checkpoint(Path::new(path)).map_err(map_parser_error)?;
    Ok(MappedCheckpoint {
        metadata: inner.metadata().clone(),
        tensors: inner.tensors().to_vec(),
        payloads: HashMap::new(),
        inner: Some(inner),
    })
}

fn kind_of(info: &TensorInfo) -> TensorKind {
    match &info.dtype {
        TensorDType::F32 => TensorKind::F32,
        TensorDType::F16 => TensorKind::F16,
        TensorDType::GgufPacked { ggml_type } => match DType::from_ggml_type(*ggml_type) {
            DType::Q8_0 => TensorKind::Q8Block,
            DType::Q5_K => TensorKind::Q5KBlock,
            DType::Q6_K => TensorKind::Q6KBlock,
            DType::IQ3_S => TensorKind::Iq3S,
            other => TensorKind::Other(other.label().to_owned()),
        },
        other => TensorKind::Other(other.label().to_owned()),
    }
}

fn token_embedding_dims(view: &TensorView, name: &str, path: &str) -> Result<(usize, usize)> {
    if view.dims.len() != 2 || view.dims[0] == 0 || view.dims[1] == 0 {
        return Err(HybridError::UnsupportedFormat(format!(
            "token embedding tensor '{name}' in '{path}' has unsupported shape/type: dtype={} dims={:?} (expected [vocab>0, hidden>0])",
            view.kind, view.dims
        )));
    }
    Ok((view.dims[0], view.dims[1]))
}

fn dequantize_row(
    dtype: DType,
    bytes: &[u8],
    hidden: usize,
    token_id: usize,
    path: &str,
    tensor_name: &str,
    dequant: fn(&[u8], &[usize]) -> engram_parser::Result<Vec<f32>>,
) -> Result<Vec<f32>> {
    let row_size = packed_row_size(dtype.ggml_type(), hidden).map_err(map_parser_error)?;
    let row = row_bytes(bytes, row_size, token_id, path, tensor_name)?;
    // `dims` is innermost-first; a single row's width is the contiguous axis.
    dequant(row, &[hidden]).map_err(map_parser_error)
}

fn row_bytes<'a>(
    bytes: &'a [u8],
    row_size: usize,
    row_idx: usize,
    path: &str,
    tensor_name: &str,
) -> Result<&'a [u8]> {
    let start = row_idx
        .checked_mul(row_size)
        .ok_or_else(|| row_overflow(path, tensor_name))?;
    let end = start
        .checked_add(row_size)
        .ok_or_else(|| row_overflow(path, tensor_name))?;
    if end > bytes.len() {
        return Err(HybridError::ModelLoad {
            path: path.to_owned(),
            reason: format!("tensor '{tensor_name}' row extends beyond payload"),
        });
    }
    Ok(&bytes[start..end])
}

fn decode_f32(bytes: &[u8], elements: usize, path: &str, name: &str) -> Result<Vec<f32>> {
    let expected = elements
        .checked_mul(std::mem::size_of::<f32>())
        .ok_or_else(|| row_overflow(path, name))?;
    if bytes.len() != expected {
        return Err(HybridError::ModelLoad {
            path: path.to_owned(),
            reason: format!(
                "tensor '{name}' F32 payload is {} bytes, expected {expected}",
                bytes.len()
            ),
        });
    }
    let (chunks, remainder) = bytes.as_chunks::<4>();
    debug_assert!(remainder.is_empty());
    Ok(chunks
        .iter()
        .map(|chunk| f32::from_le_bytes(*chunk))
        .collect())
}

/// Decode exactly `elements` little-endian F16 values from `bytes`.
///
/// `bytes` must be the row (or tensor) being decoded and nothing more: the
/// length is checked against `elements`, so a caller cannot hand this the
/// whole `token_embd` payload and have it convert every row (GH#100).
fn decode_f16(bytes: &[u8], elements: usize, path: &str, name: &str) -> Result<Vec<f32>> {
    let expected = elements
        .checked_mul(std::mem::size_of::<u16>())
        .ok_or_else(|| row_overflow(path, name))?;
    if bytes.len() != expected {
        return Err(HybridError::ModelLoad {
            path: path.to_owned(),
            reason: format!(
                "tensor '{name}' F16 payload is {} bytes, expected {expected}",
                bytes.len()
            ),
        });
    }
    let (chunks, remainder) = bytes.as_chunks::<2>();
    debug_assert!(remainder.is_empty());
    Ok(chunks
        .iter()
        .map(|chunk| f16_bits_to_f32(u16::from_le_bytes(*chunk)))
        .collect())
}

fn element_count(dims: &[usize], path: &str, name: &str) -> Result<usize> {
    dims.iter()
        .try_fold(1usize, |acc, &dim| acc.checked_mul(dim))
        .ok_or_else(|| HybridError::ModelLoad {
            path: path.to_owned(),
            reason: format!("tensor '{name}' element count overflow"),
        })
}

fn row_overflow(path: &str, tensor_name: &str) -> HybridError {
    HybridError::ModelLoad {
        path: path.to_owned(),
        reason: format!("tensor '{tensor_name}' row offset overflow"),
    }
}

fn quantization_label(metadata: &engram_parser::checkpoint::CheckpointMetadata) -> String {
    if let Some(label) = metadata
        .get("general.quantization_type")
        .and_then(MetadataValue::as_str)
        .filter(|label| !label.is_empty())
    {
        return label.to_owned();
    }
    match metadata_usize(metadata, "general.file_type") {
        Some(0) => "F32".to_owned(),
        Some(1) => "F16".to_owned(),
        Some(other) => format!("GGUF({other})"),
        None => metadata
            .get("general.file_type")
            .and_then(MetadataValue::as_str)
            .filter(|label| !label.is_empty())
            .map(str::to_owned)
            .unwrap_or_else(|| "unknown".to_owned()),
    }
}

fn metadata_usize(
    metadata: &engram_parser::checkpoint::CheckpointMetadata,
    key: &str,
) -> Option<usize> {
    match metadata.get(key)? {
        MetadataValue::UInt(value) => usize::try_from(*value).ok(),
        MetadataValue::Int(value) => usize::try_from(*value).ok(),
        MetadataValue::String(value) => value.parse().ok(),
        MetadataValue::F32(value) => whole_usize(f64::from(*value)),
        MetadataValue::F64(value) => whole_usize(*value),
        _ => None,
    }
}

fn whole_usize(value: f64) -> Option<usize> {
    if !value.is_finite() || value < 0.0 || value.fract() != 0.0 {
        return None;
    }
    let rounded = value as u64;
    if rounded as f64 == value {
        usize::try_from(rounded).ok()
    } else {
        None
    }
}

fn tensor_block_sort_key(name: &str) -> (usize, &str) {
    (layer_ordinal(name), name)
}

/// First `blk` / `block` / `blocks` / `layer` / `layers` index in `name`.
fn layer_ordinal(name: &str) -> usize {
    let parts: Vec<&str> = name.split('.').collect();
    for pair in parts.windows(2) {
        if matches!(pair[0], "blk" | "block" | "blocks" | "layer" | "layers")
            && let Ok(index) = pair[1].parse::<usize>()
        {
            return index;
        }
    }
    usize::MAX
}

fn map_parser_error(err: ParserError) -> HybridError {
    match err {
        ParserError::MissingTensor { name, path } => HybridError::MissingTensor { name, path },
        ParserError::Io { path, source } => HybridError::ModelLoad {
            path,
            reason: source.to_string(),
        },
        ParserError::InvalidLayout { path, reason } => HybridError::ModelLoad { path, reason },
        // The four variants below carry the offending file's `path`; keep it
        // structured like `Io` / `InvalidLayout` so a caller matching
        // `ModelLoad` to detect a bad file sees them too.
        ParserError::LimitExceeded {
            path,
            limit,
            declared,
            budget,
        } => HybridError::ModelLoad {
            path,
            reason: format!("parse limit {limit} exceeded: declared {declared}, budget {budget}"),
        },
        ParserError::HostSizeOverflow { path, field, value } => HybridError::ModelLoad {
            path,
            reason: format!("field {field} value {value} does not fit usize"),
        },
        ParserError::DuplicateTensorOwnership { name, shards, path } => HybridError::ModelLoad {
            path,
            reason: format!(
                "duplicate tensor ownership for '{name}' across shards {}",
                shards.join(", ")
            ),
        },
        ParserError::MissingShard { shard, path } => HybridError::ModelLoad {
            path,
            reason: format!("missing shard '{shard}'"),
        },
        ParserError::UnsupportedFormat { .. }
        | ParserError::FeatureDisabled { .. }
        | ParserError::ExpertOutOfRange { .. } => HybridError::UnsupportedFormat(err.to_string()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::HybridError;
    use crate::moe::test_fixtures::*;
    use crate::types::EMBEDDING_DIM;

    #[test]
    fn validated_engram_release_is_recorded() {
        assert_eq!(ENGRAM_PARSER_VERSION, "0.3.0");
    }

    #[test]
    fn malformed_gguf_fails_without_an_in_tree_parser() {
        let mut bytes = Vec::new();
        bytes.extend_from_slice(b"GGUS");
        push_u32(&mut bytes, GGUF_VERSION);
        push_u64(&mut bytes, 0);
        push_u64(&mut bytes, 0);
        let path = write_temp_file(&bytes, "bad-magic");
        let err = open_mapped_checkpoint(path.to_str().unwrap()).unwrap_err();
        let message = err.to_string();
        assert!(
            message.contains("unsupported")
                || message.contains("invalid")
                || message.contains("model load"),
            "expected an explicit parser error, got {message}"
        );
        assert!(
            !message.contains("parse_checkpoint_layout"),
            "retired parser must not appear in the error: {message}"
        );
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn tensor_inventory_uses_outermost_first_shapes() {
        let d1 = 3usize;
        let payload = vec![0u8; EMBEDDING_DIM * d1 * std::mem::size_of::<f32>()];
        let bytes = build_token_file(GGML_TYPE_F32, d1, payload);
        let path = write_temp_file(&bytes, "inventory-f32");
        let checkpoint = open_mapped_checkpoint(path.to_str().unwrap()).unwrap();

        let names: Vec<&str> = checkpoint.tensor_names().collect();
        assert_eq!(names, vec!["token_embd.weight"]);
        let view = checkpoint
            .tensor_view("token_embd.weight", path.to_str().unwrap())
            .unwrap();
        assert_eq!(view.dims, vec![d1, EMBEDDING_DIM]);
        assert_eq!(view.kind, TensorKind::F32);
        assert_eq!(
            checkpoint
                .find_first_tensor_with_suffix("embd.weight")
                .as_deref(),
            Some("token_embd.weight")
        );

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

    #[test]
    fn suffix_search_orders_blocks_numerically() {
        let hidden = 32usize;
        let gate = vec![0u8; hidden * 4 * std::mem::size_of::<f32>()];
        let bytes = build_test_gguf(
            vec![
                (
                    "blk.10.ffn_gate_inp.weight",
                    vec![hidden, 4],
                    GGML_TYPE_F32,
                    gate.clone(),
                ),
                (
                    "blk.2.ffn_gate_inp.weight",
                    vec![hidden, 4],
                    GGML_TYPE_F32,
                    gate,
                ),
                (
                    "token_embd.weight",
                    vec![hidden, 2],
                    GGML_TYPE_F32,
                    vec![0u8; hidden * 2 * std::mem::size_of::<f32>()],
                ),
            ],
            32,
            hidden,
        );
        let path = write_temp_file(&bytes, "block-order");
        let checkpoint = open_mapped_checkpoint(path.to_str().unwrap()).unwrap();
        assert_eq!(
            checkpoint
                .find_first_tensor_with_suffix("ffn_gate_inp.weight")
                .as_deref(),
            Some("blk.2.ffn_gate_inp.weight")
        );
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn suffix_search_orders_layers_numerically() {
        let hidden = 32usize;
        let gate = vec![0u8; hidden * 4 * std::mem::size_of::<f32>()];
        let bytes = build_test_gguf(
            vec![
                (
                    "model.layers.10.ffn_gate_inp.weight",
                    vec![hidden, 4],
                    GGML_TYPE_F32,
                    gate.clone(),
                ),
                (
                    "model.layers.2.mlp.experts.0.ffn_gate_inp.weight",
                    vec![hidden, 4],
                    GGML_TYPE_F32,
                    gate,
                ),
                (
                    "token_embd.weight",
                    vec![hidden, 2],
                    GGML_TYPE_F32,
                    vec![0u8; hidden * 2 * std::mem::size_of::<f32>()],
                ),
            ],
            32,
            hidden,
        );
        let path = write_temp_file(&bytes, "layer-order");
        let checkpoint = open_mapped_checkpoint(path.to_str().unwrap()).unwrap();
        assert_eq!(
            checkpoint
                .find_first_tensor_with_suffix("ffn_gate_inp.weight")
                .as_deref(),
            Some("model.layers.2.mlp.experts.0.ffn_gate_inp.weight")
        );
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn bind_keeps_token_row_and_drops_other_tensors() {
        let hidden = 32usize;
        let experts = 4usize;
        let vocab = 2usize;
        let mut token = Vec::new();
        for token_id in 0..vocab {
            for dim in 0..hidden {
                let value = token_id as f32 * 10.0 + dim as f32;
                token.extend_from_slice(&value.to_le_bytes());
            }
        }
        let gate = vec![0u8; hidden * experts * std::mem::size_of::<f32>()];
        let bytes = build_test_gguf(
            vec![
                (
                    "blk.0.ffn_gate_inp.weight",
                    vec![hidden, experts],
                    GGML_TYPE_F32,
                    gate,
                ),
                (
                    "blk.0.attn_q.weight",
                    vec![hidden, hidden],
                    GGML_TYPE_F32,
                    vec![1u8; hidden * hidden * std::mem::size_of::<f32>()],
                ),
                (
                    "token_embd.weight",
                    vec![hidden, vocab],
                    GGML_TYPE_F32,
                    token,
                ),
            ],
            32,
            hidden,
        );
        let path = write_temp_file(&bytes, "bind-release");
        let path_str = path.to_str().unwrap();
        let mut checkpoint = open_mapped_checkpoint(path_str).unwrap();
        let bound = checkpoint
            .bind_router_weights("token_embd.weight", "blk.0.ffn_gate_inp.weight", path_str)
            .unwrap();
        assert_eq!(bound.dims, vec![experts, hidden]);
        assert_eq!(bound.weights.len(), experts * hidden);
        let row = checkpoint
            .extract_token_embedding("token_embd.weight", path_str, 1)
            .unwrap();
        assert!((row[0] - 10.0).abs() <= 1e-6);
        let err = checkpoint
            .f32_tensor("blk.0.attn_q.weight", path_str)
            .unwrap_err();
        assert!(err.to_string().contains("not bound"));
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn probe_extracts_f32_token_row_in_normalized_order() {
        let d1 = 3usize;
        let mut payload = Vec::new();
        for token in 0..d1 {
            for dim in 0..EMBEDDING_DIM {
                let value = token as f32 * 10.0 + dim as f32;
                payload.extend_from_slice(&value.to_le_bytes());
            }
        }
        let bytes = build_token_file(GGML_TYPE_F32, d1, payload);
        let path = write_temp_file(&bytes, "token_f32");
        let checkpoint = open_mapped_checkpoint(path.to_str().unwrap()).unwrap();
        let embedding = checkpoint
            .extract_token_embedding("token_embd.weight", path.to_str().unwrap(), 1)
            .unwrap();
        assert_eq!(embedding.len(), EMBEDDING_DIM);
        assert!((embedding[0] - 10.0).abs() <= 1e-6);
        assert!((embedding[1] - 11.0).abs() <= 1e-6);

        let err = checkpoint
            .extract_token_embedding("token_embd.weight", path.to_str().unwrap(), d1)
            .unwrap_err();
        assert!(matches!(
            err,
            HybridError::TokenIndex { index, vocab_size } if index == d1 && vocab_size == d1
        ));
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn f16_tensor_is_not_readable_as_f32() {
        let d1 = 3usize;
        let payload = vec![0u8; EMBEDDING_DIM * d1 * 2];
        let bytes = build_token_file(GGML_TYPE_F16, d1, payload);
        let path = write_temp_file(&bytes, "token_f16");
        let checkpoint = open_mapped_checkpoint(path.to_str().unwrap()).unwrap();
        let err = checkpoint
            .f32_tensor("token_embd.weight", path.to_str().unwrap())
            .unwrap_err();
        assert!(err.to_string().contains("must be F32"));
        let embedding = checkpoint
            .extract_token_embedding("token_embd.weight", path.to_str().unwrap(), 1)
            .unwrap();
        assert_eq!(embedding.len(), EMBEDDING_DIM);
        assert!(embedding.iter().all(|&value| value == 0.0));
        let _ = std::fs::remove_file(path);
    }

    /// Pins GH#100: an F16 token lookup must decode the requested row and
    /// nothing else. The retired `u16_tensor_values` converted all
    /// `vocab * hidden` values and then sliced one row out, so every lookup
    /// on a 151936 x 2048 `token_embd` allocated about 622 MB. Rows here carry
    /// distinct bit patterns, so a row-offset slip or a whole-tensor decode
    /// that returns the wrong window shows up as a value mismatch.
    #[test]
    fn f16_token_row_decodes_exactly_the_requested_row() {
        let vocab = 4096usize;
        let hidden = EMBEDDING_DIM;
        let row_size = hidden * 2;
        // Row bits 0..12 land in the mantissa and the two low exponent bits;
        // the lane varies exponent bits 2..3 and the sign. Exponent bit 4 is
        // never set, so every lane is a finite f16, and for a fixed lane the
        // pattern is injective in `row` over 0..4096: no two rows share bytes.
        let bits_at = |row: usize, lane: usize| -> u16 {
            let lane = lane as u16;
            (row as u16) | ((lane & 0b11) << 12) | (((lane >> 2) & 1) << 15)
        };
        let distinct: std::collections::HashSet<u16> =
            (0..vocab).map(|row| bits_at(row, 0)).collect();
        assert_eq!(distinct.len(), vocab, "row encoding must be injective");
        for &(a, b) in &[(0usize, 64usize), (2047, 4095), (7, 71), (0, 2048)] {
            assert_ne!(bits_at(a, 0), bits_at(b, 0), "rows {a} and {b} must differ");
        }
        let mut payload = vec![0u8; vocab * row_size];
        for row in 0..vocab {
            for lane in 0..hidden {
                let at = row * row_size + lane * 2;
                payload[at..at + 2].copy_from_slice(&bits_at(row, lane).to_le_bytes());
            }
        }
        let bytes = build_token_file(GGML_TYPE_F16, vocab, payload);
        let path = write_temp_file(&bytes, "token_f16_gh100");
        let path_str = path.to_str().unwrap();
        let mut checkpoint = open_mapped_checkpoint(path_str).unwrap();

        let expected_row = |row: usize| -> Vec<u32> {
            (0..hidden)
                .map(|lane| f16_bits_to_f32(bits_at(row, lane)).to_bits())
                .collect()
        };
        for &token in &[0usize, 1, 2047, vocab - 1] {
            let row = checkpoint
                .extract_token_embedding("token_embd.weight", path_str, token)
                .unwrap();
            assert_eq!(row.len(), hidden, "token {token} width");
            let got: Vec<u32> = row.iter().map(|value| value.to_bits()).collect();
            assert_eq!(
                got,
                expected_row(token),
                "token {token} must read row {token}"
            );
            assert!(row.iter().all(|value| value.is_finite()));
        }
        assert_ne!(
            checkpoint
                .extract_token_embedding("token_embd.weight", path_str, 7)
                .unwrap(),
            checkpoint
                .extract_token_embedding("token_embd.weight", path_str, 8)
                .unwrap()
        );

        // One past the end is rejected before any byte is read.
        let err = checkpoint
            .extract_token_embedding("token_embd.weight", path_str, vocab)
            .unwrap_err();
        assert!(matches!(
            err,
            HybridError::TokenIndex { index, vocab_size } if index == vocab && vocab_size == vocab
        ));

        // The bound-router copy serves the same rows from the retained payload.
        let before = checkpoint
            .extract_token_embedding("token_embd.weight", path_str, 2047)
            .unwrap();
        checkpoint.payloads.insert(
            "token_embd.weight".to_owned(),
            checkpoint
                .read_payload("token_embd.weight", path_str)
                .unwrap(),
        );
        checkpoint.inner = None;
        let after = checkpoint
            .extract_token_embedding("token_embd.weight", path_str, 2047)
            .unwrap();
        assert_eq!(after, before);
        let _ = std::fs::remove_file(path);
    }

    /// The F16 decoder is bounded by the slice it is given: handing it more
    /// than one row's bytes is an error, so the whole-tensor conversion GH#100
    /// describes cannot come back through this helper.
    #[test]
    fn decode_f16_rejects_a_payload_wider_than_the_row() {
        let hidden = 8usize;
        let mut two_rows = Vec::with_capacity(hidden * 2 * 2);
        for lane in 0..hidden * 2 {
            // 1.0 (0x3C00) in the first row, 2.0 (0x4000) in the second.
            let bits = if lane < hidden { 0x3C00u16 } else { 0x4000u16 };
            two_rows.extend_from_slice(&bits.to_le_bytes());
        }
        let err = decode_f16(&two_rows, hidden, "p", "t").unwrap_err();
        assert!(
            err.to_string()
                .contains("F16 payload is 32 bytes, expected 16"),
            "{err}"
        );
        let err = decode_f16(&two_rows[..hidden * 2 - 1], hidden, "p", "t").unwrap_err();
        assert!(err.to_string().contains("expected 16"), "{err}");

        let row0 = row_bytes(&two_rows, hidden * 2, 0, "p", "t").unwrap();
        assert_eq!(
            decode_f16(row0, hidden, "p", "t").unwrap(),
            vec![1.0f32; hidden]
        );
        let row1 = row_bytes(&two_rows, hidden * 2, 1, "p", "t").unwrap();
        assert_eq!(
            decode_f16(row1, hidden, "p", "t").unwrap(),
            vec![2.0f32; hidden]
        );
        assert!(row_bytes(&two_rows, hidden * 2, 2, "p", "t").is_err());
        assert!(row_bytes(&two_rows, hidden * 2, usize::MAX, "p", "t").is_err());
    }

    #[test]
    fn extract_rejects_iq3_s_after_a_successful_open() {
        let width = 256usize;
        let vocab = 2usize;
        let bytes = build_native_tensor(
            "token_embd.weight",
            GGML_TYPE_IQ3_S,
            width,
            vocab,
            vec![0u8; 110 * vocab],
        );
        let path = write_temp_file(&bytes, "token_iq3");
        let checkpoint = open_mapped_checkpoint(path.to_str().unwrap()).unwrap();
        let err = checkpoint
            .extract_token_embedding("token_embd.weight", path.to_str().unwrap(), 0)
            .unwrap_err();
        assert!(err.to_string().contains("IQ3_S token embeddings"));
        let err = checkpoint
            .extract_token_embedding("token_embd.weight", path.to_str().unwrap(), 5)
            .unwrap_err();
        assert!(matches!(
            err,
            HybridError::TokenIndex { index, vocab_size } if vocab_size == vocab && index == 5
        ));
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn q6_k_token_row_delegates_to_engram() {
        const GGML_TYPE_Q6_K: u32 = 14;
        const Q6_K_BYTES: usize = 210;
        let mut row0 = [0u8; Q6_K_BYTES];
        for byte in &mut row0[192..208] {
            *byte = 1;
        }
        row0[208..210].copy_from_slice(&0x3C00u16.to_le_bytes());
        let mut row1 = row0;
        row1[0] = 1;
        let mut payload = Vec::with_capacity(Q6_K_BYTES * 2);
        payload.extend_from_slice(&row0);
        payload.extend_from_slice(&row1);
        let bytes = build_native_tensor("token_embd.weight", GGML_TYPE_Q6_K, 256, 2, payload);
        let path = write_temp_file(&bytes, "token_q6k");
        let checkpoint = open_mapped_checkpoint(path.to_str().unwrap()).unwrap();
        let first = checkpoint
            .extract_token_embedding("token_embd.weight", path.to_str().unwrap(), 0)
            .unwrap();
        let second = checkpoint
            .extract_token_embedding("token_embd.weight", path.to_str().unwrap(), 1)
            .unwrap();
        assert_eq!(first.len(), 256);
        assert!(first.iter().all(|value| *value == -32.0));
        assert_eq!(second[0], -31.0);
        assert_eq!(second[32], -32.0);
        let err = checkpoint
            .extract_token_embedding("token_embd.weight", path.to_str().unwrap(), 2)
            .unwrap_err();
        assert!(matches!(
            err,
            HybridError::TokenIndex { index, vocab_size } if index == 2 && vocab_size == 2
        ));
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn q8_0_and_q5_k_rows_delegate_to_engram() {
        let mut q8 = [0u8; 34];
        q8[0..2].copy_from_slice(&0x4000u16.to_le_bytes());
        for (idx, quant) in q8[2..34].iter_mut().enumerate() {
            *quant = idx as u8;
        }
        let out = dequantize_q8_0(&q8, &[32]).unwrap();
        assert_eq!(out.len(), 32);
        assert!((out[3] - 6.0).abs() <= 1e-5);

        let mut q5 = [0u8; 176];
        q5[0..2].copy_from_slice(&0x3C00u16.to_le_bytes());
        q5[4] = 1;
        for byte in q5[48..80].iter_mut() {
            *byte = 0x05;
        }
        let out = dequantize_q5_k(&q5, &[256]).unwrap();
        assert_eq!(out.len(), 256);
        assert!(out[..32].iter().all(|value| (value - 5.0).abs() <= 1e-4));
        assert!(out[32..64].iter().all(|value| value.abs() <= 1e-4));
    }

    /// Regression for GH#88: the retired in-tree Q5_K decoder interleaved the
    /// low and high nibble of every `ql` byte (`[lo, hi, lo, hi, ...]`), and a
    /// golden vector pinned that order. llama.cpp's `dequantize_row_q5_K`
    /// writes each 64-wide group as 32 low-nibble values followed by 32
    /// high-nibble values. This pins the correct order on the token-row path
    /// that routing actually uses, with expectations derived by hand from the
    /// `block_q5_K` layout: `d` f16 `[0..2]`, `dmin` f16 `[2..4]`,
    /// `scales[12]` `[4..16]`, `qh[32]` `[16..48]`, `ql[128]` `[48..176]`.
    #[test]
    fn q5_k_token_row_follows_llama_cpp_low_then_high_nibble_order() {
        const GGML_TYPE_Q5_K: u32 = 13;
        const Q5_K_BYTES: usize = 176;

        let mut block = [0u8; Q5_K_BYTES];
        // d = 1.0, dmin = 0.5
        block[0..2].copy_from_slice(&0x3C00u16.to_le_bytes());
        block[2..4].copy_from_slice(&0x3800u16.to_le_bytes());
        // get_scale_min_k4(j < 4): sc = scales[j] & 63, m = scales[j + 4] & 63.
        // sc0 = 2, sc1 = 3, sc2 = 1, sc3 = 1; m0 = 4, m1..m3 = 0.
        block[4..8].copy_from_slice(&[2, 3, 1, 1]);
        block[8..12].copy_from_slice(&[4, 0, 0, 0]);
        // get_scale_min_k4(j >= 4): sc = (scales[j+4] & 0xF) | ((scales[j-4] >> 6) << 4),
        //                           m  = (scales[j+4] >> 4)  | ((scales[j]   >> 6) << 4).
        // scales[8..12] = 0x11 with no high bits set above -> sc4..sc7 = 1, m4..m7 = 1.
        for byte in &mut block[12..16] {
            *byte = 0x11;
        }
        // qh: group 0 uses u1 = 0x01 (low nibble) and u2 = 0x02 (high nibble).
        // Lanes 0..16 lift the low nibble by 16, lanes 16..32 lift the high nibble.
        for byte in &mut block[16..32] {
            *byte = 0x01;
        }
        for byte in &mut block[32..48] {
            *byte = 0x02;
        }
        // Every ql byte is 0x35: low nibble 5, high nibble 3 (the GH#88 repro).
        for byte in &mut block[48..Q5_K_BYTES] {
            *byte = 0x35;
        }

        let mut payload = Vec::with_capacity(Q5_K_BYTES * 2);
        payload.extend_from_slice(&block);
        // Row 1: same block with the plain GH#88 repro (d = 1, dmin = 0, sc0 = sc1 = 1,
        // no qh bits). Expected `[5; 32]` then `[3; 32]`, never `[5, 3, 5, 3, ...]`.
        let mut plain = [0u8; Q5_K_BYTES];
        plain[0..2].copy_from_slice(&0x3C00u16.to_le_bytes());
        plain[4] = 1;
        plain[5] = 1;
        for byte in &mut plain[48..Q5_K_BYTES] {
            *byte = 0x35;
        }
        payload.extend_from_slice(&plain);

        // A gate tensor is included so the same file can also be bound the way
        // `MoeRouter::load_with_mode` binds it (see the bound-path check below).
        let hidden = 256usize;
        let experts = 4usize;
        let bytes = build_test_gguf(
            vec![
                (
                    "blk.0.ffn_gate_inp.weight",
                    vec![hidden, experts],
                    GGML_TYPE_F32,
                    vec![0u8; hidden * experts * std::mem::size_of::<f32>()],
                ),
                (
                    "token_embd.weight",
                    vec![hidden, 2],
                    GGML_TYPE_Q5_K,
                    payload,
                ),
            ],
            32,
            hidden,
        );
        let path = write_temp_file(&bytes, "token_q5k_gh88");
        let path_str = path.to_str().unwrap();
        let mut checkpoint = open_mapped_checkpoint(path_str).unwrap();

        let row0 = checkpoint
            .extract_token_embedding("token_embd.weight", path_str, 0)
            .unwrap();
        assert_eq!(row0.len(), 256);
        let mut expected0 = Vec::with_capacity(256);
        // Group 0 (ql[0..32], sc0/m0 for low, sc1/m1 for high):
        //   low  lanes 0..16 : 1.0*2*(5+16) - 0.5*4 = 40
        //   low  lanes 16..32: 1.0*2*5      - 0.5*4 = 8
        //   high lanes 0..16 : 1.0*3*3      - 0      = 9
        //   high lanes 16..32: 1.0*3*(3+16) - 0      = 57
        expected0.extend(std::iter::repeat_n(40.0f32, 16));
        expected0.extend(std::iter::repeat_n(8.0f32, 16));
        expected0.extend(std::iter::repeat_n(9.0f32, 16));
        expected0.extend(std::iter::repeat_n(57.0f32, 16));
        // Group 1 (sc2 = sc3 = 1, m2 = m3 = 0, u1 = 0x04 / u2 = 0x08 unset): 5 then 3.
        expected0.extend(std::iter::repeat_n(5.0f32, 32));
        expected0.extend(std::iter::repeat_n(3.0f32, 32));
        // Groups 2 and 3 (sc = 1, m = 1 -> minus 0.5): 4.5 then 2.5.
        for _ in 0..2 {
            expected0.extend(std::iter::repeat_n(4.5f32, 32));
            expected0.extend(std::iter::repeat_n(2.5f32, 32));
        }
        assert_eq!(expected0.len(), row0.len());
        for (idx, (got, want)) in row0.iter().zip(&expected0).enumerate() {
            assert!(
                (got - want).abs() <= 1e-4,
                "row 0 lane {idx}: got {got}, want {want}"
            );
        }

        let row1 = checkpoint
            .extract_token_embedding("token_embd.weight", path_str, 1)
            .unwrap();
        assert_eq!(row1.len(), 256);
        assert!(row1[..32].iter().all(|value| (value - 5.0).abs() <= 1e-4));
        assert!(row1[32..64].iter().all(|value| (value - 3.0).abs() <= 1e-4));
        // The interleaved order the retired decoder produced must not reappear.
        assert_ne!(row1[1], 3.0);
        assert_eq!(
            packed_row_size(DType::Q5_K.ggml_type(), 256).unwrap(),
            Q5_K_BYTES
        );

        // Bound-router path: `bind_router_weights` copies the token payload and
        // drops the parser; `MoeRouter::extract_token_embedding` then reads that
        // copy. The rows must be identical to the pre-bind extraction.
        checkpoint
            .bind_router_weights("token_embd.weight", "blk.0.ffn_gate_inp.weight", path_str)
            .unwrap();
        let bound0 = checkpoint
            .extract_token_embedding("token_embd.weight", path_str, 0)
            .unwrap();
        let bound1 = checkpoint
            .extract_token_embedding("token_embd.weight", path_str, 1)
            .unwrap();
        assert_eq!(bound0, row0);
        assert_eq!(bound1, row1);
        let _ = std::fs::remove_file(path);
    }

    fn build_token_file(ggml_type: u32, vocab: usize, payload: Vec<u8>) -> Vec<u8> {
        build_native_tensor(
            "token_embd.weight",
            ggml_type,
            EMBEDDING_DIM,
            vocab,
            payload,
        )
    }

    fn build_native_tensor(
        name: &str,
        ggml_type: u32,
        inner: usize,
        outer: usize,
        payload: Vec<u8>,
    ) -> Vec<u8> {
        let mut out = Vec::new();
        out.extend_from_slice(&GGUF_MAGIC);
        push_u32(&mut out, GGUF_VERSION);
        push_u64(&mut out, 1);
        push_u64(&mut out, 0);
        push_string(&mut out, name);
        push_u32(&mut out, 2);
        push_u64(&mut out, inner as u64);
        push_u64(&mut out, outer as u64);
        push_u32(&mut out, ggml_type);
        push_u64(&mut out, 0);
        while out.len() % 32 != 0 {
            out.push(0);
        }
        out.extend_from_slice(&payload);
        out
    }
}

#[cfg(test)]
mod parser_error_mapping_tests {
    use super::*;
    use engram_parser::{HostSizeField, ParseLimitKind};

    #[test]
    fn path_carrying_parser_errors_surface_as_model_load() {
        let errs = [
            ParserError::LimitExceeded {
                path: "m.gguf".into(),
                limit: ParseLimitKind::KvCount,
                declared: 10,
                budget: 5,
            },
            ParserError::HostSizeOverflow {
                path: "m.gguf".into(),
                field: HostSizeField::KvCount,
                value: u64::MAX,
            },
            ParserError::DuplicateTensorOwnership {
                name: "t".into(),
                shards: vec!["a".into(), "b".into()],
                path: "m.gguf".into(),
            },
            ParserError::MissingShard {
                shard: "s".into(),
                path: "m.gguf".into(),
            },
        ];
        for err in errs {
            match map_parser_error(err) {
                HybridError::ModelLoad { path, reason } => {
                    assert_eq!(path, "m.gguf");
                    assert!(!reason.is_empty());
                }
                other => panic!("expected ModelLoad, got {other:?}"),
            }
        }
    }
}