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).
22#[allow(dead_code)]
23fn gguf_name_hash(bytes: &[u8]) -> u64 {
24    let mut h: u64 = 0xcbf29ce484222325;
25    for &b in bytes {
26        h ^= b as u64;
27        h = h.wrapping_mul(0x100000001b3);
28    }
29    h
30}
31
32/// Skip over one GGUF KV value of the given type without storing it.
33/// Returns `None` on any parse error (truncated data, unknown type).
34/// Used by both `GgufTokenizer` and `GgufTensorIndex`.
35fn gguf_skip_value(mmap: &[u8], pos: &mut usize, vtype: u32) -> Option<()> {
36    match vtype {
37        0 | 1 | 7 => {
38            if *pos + 1 > mmap.len() {
39                return None;
40            }
41            *pos += 1;
42        }
43        2 | 3 => {
44            if *pos + 2 > mmap.len() {
45                return None;
46            }
47            *pos += 2;
48        }
49        4 | 5 | 6 => {
50            if *pos + 4 > mmap.len() {
51                return None;
52            }
53            *pos += 4;
54        }
55        10 | 11 | 12 => {
56            if *pos + 8 > mmap.len() {
57                return None;
58            }
59            *pos += 8;
60        }
61        8 => {
62            if *pos + 8 > mmap.len() {
63                return None;
64            }
65            let slen = u64::from_le_bytes(mmap[*pos..*pos + 8].try_into().ok()?) as usize;
66            *pos += 8;
67            if *pos + slen > mmap.len() {
68                return None;
69            }
70            *pos += slen;
71        }
72        9 => {
73            if *pos + 12 > mmap.len() {
74                return None;
75            }
76            let etype = u32::from_le_bytes(mmap[*pos..*pos + 4].try_into().ok()?);
77            *pos += 4;
78            let cnt = u64::from_le_bytes(mmap[*pos..*pos + 8].try_into().ok()?) as usize;
79            *pos += 8;
80            for _ in 0..cnt {
81                gguf_skip_value(mmap, pos, etype)?;
82            }
83        }
84        _ => return None,
85    }
86    Some(())
87}
88
89#[cfg(test)]
90mod tests;