Skip to main content

qualia_core_db/inference/gguf_sharder/
mod.rs

1//! Q-GGUF Hybrid Packaging
2//! Parses monolithic `.gguf` files: vocabulary (KV section) and tensor names/offsets
3//! (tensor-info section) are extracted into native Rust types; multi-gigabyte tensor
4//! payloads are left on disk for direct VRAM mapping via `gguf_bridge.rs`.
5
6mod hyperparams;
7mod sharder;
8mod tensor_index;
9mod tokenizer;
10mod types;
11
12pub use hyperparams::*;
13pub use sharder::*;
14pub use tensor_index::*;
15pub use tokenizer::*;
16pub use types::*;
17
18// ─── Module-level GGUF helpers ───────────────────────────────────────────────
19
20/// FNV-1a hash over raw bytes — same algorithm as `crate::q_hash` but for
21/// byte slices parsed at runtime (e.g. tensor names from the binary header).
22fn gguf_name_hash(bytes: &[u8]) -> u64 {
23    let mut h: u64 = 0xcbf29ce484222325;
24    for &b in bytes {
25        h ^= b as u64;
26        h = h.wrapping_mul(0x100000001b3);
27    }
28    h
29}
30
31/// Skip over one GGUF KV value of the given type without storing it.
32/// Returns `None` on any parse error (truncated data, unknown type).
33/// Used by both `GgufTokenizer` and `GgufTensorIndex`.
34fn gguf_skip_value(mmap: &[u8], pos: &mut usize, vtype: u32) -> Option<()> {
35    match vtype {
36        0 | 1 | 7 => {
37            if *pos + 1 > mmap.len() {
38                return None;
39            }
40            *pos += 1;
41        }
42        2 | 3 => {
43            if *pos + 2 > mmap.len() {
44                return None;
45            }
46            *pos += 2;
47        }
48        4 | 5 | 6 => {
49            if *pos + 4 > mmap.len() {
50                return None;
51            }
52            *pos += 4;
53        }
54        10 | 11 | 12 => {
55            if *pos + 8 > mmap.len() {
56                return None;
57            }
58            *pos += 8;
59        }
60        8 => {
61            if *pos + 8 > mmap.len() {
62                return None;
63            }
64            let slen = u64::from_le_bytes(mmap[*pos..*pos + 8].try_into().ok()?) as usize;
65            *pos += 8;
66            if *pos + slen > mmap.len() {
67                return None;
68            }
69            *pos += slen;
70        }
71        9 => {
72            if *pos + 12 > mmap.len() {
73                return None;
74            }
75            let etype = u32::from_le_bytes(mmap[*pos..*pos + 4].try_into().ok()?);
76            *pos += 4;
77            let cnt = u64::from_le_bytes(mmap[*pos..*pos + 8].try_into().ok()?) as usize;
78            *pos += 8;
79            for _ in 0..cnt {
80                gguf_skip_value(mmap, pos, etype)?;
81            }
82        }
83        _ => return None,
84    }
85    Some(())
86}
87
88#[cfg(test)]
89mod tests;