Skip to main content

qualia_core_db/q42/p64_weight/
mod.rs

1//! Phase 4: AOT GGUF → `.p64` **LLM-weight container** compiler.
2//!
3//! A *sibling* of the semantic `.q42` graph format — it carries an **independent section magic** `b"p64\0"`
4//! so the two never collide. Per the architectural decision, the weights are stored as **opaque,
5//! cache-aligned (64-byte), contiguous quantized blobs**.
6//!
7//! The `NQuin` epistemic scaffold is removed from this probabilistic container. The 48-byte
8//! declarative `q42` system manages truth, while this 64-byte aligned `p64` system manages
9//! pure mathematical inference with zero-copy relative WASM pointers.
10//!
11//! Output is **little-endian** on every host via explicit serialization.
12//!
13//! Layout:
14//! ```text
15//! [ P64WeightHeader (64B) ]              magic, version, flags, 32-bit relative offsets
16//! [ P64TensorEntry[] (64B each) ]        role, dtype, rank, dims, relative blob offsets
17//! [ pad → 1<<page_log2 ]
18//! [ tensor blob region ]                 quantized bytes; each tensor START page-aligned
19//!                                        (default 16KB) for single-fetch mmap.
20//! ```
21
22mod compiler;
23mod layout;
24mod reader;
25#[cfg(test)]
26mod tests;
27mod transcode;
28// Historical tests for the pre-P64 Q42W layout are retained as migration
29// documentation only. They refer to the removed 144/80-byte API.
30#[cfg(all(test, any()))]
31mod legacy_tests;
32
33pub use compiler::*;
34pub use layout::*;
35pub use reader::*;
36pub use transcode::*;
37
38// Shared, crate-internal helpers used across the submodules above. They live in the parent
39// module so every child (compiler / transcode / reader / tests) can reach them unchanged.
40
41#[inline]
42fn align_up(x: usize, a: usize) -> usize {
43    debug_assert!(a.is_power_of_two());
44    (x + a - 1) & !(a - 1)
45}
46
47fn write_manifold_coordinate(
48    coordinate: &crate::modalities::manifold::ManifoldCoordinate10D,
49    out: &mut [u8],
50) {
51    assert!(out.len() >= P64_MANIFOLD_ENTRY_BYTES);
52    out[..P64_MANIFOLD_ENTRY_BYTES].fill(0);
53    for (index, value) in coordinate.as_f32_array().iter().enumerate() {
54        let start = index * 4;
55        out[start..start + 4].copy_from_slice(&value.to_le_bytes());
56    }
57}
58
59fn write_tensor_entry(entry: &P64TensorEntry, out: &mut [u8]) {
60    assert!(out.len() >= P64_TENSOR_ENTRY_BYTES);
61    out[..P64_TENSOR_ENTRY_BYTES].fill(0);
62    out[0..4].copy_from_slice(&entry.name_offset.to_le_bytes());
63    out[4..6].copy_from_slice(&entry.role_id.to_le_bytes());
64    out[6..8].copy_from_slice(&entry.dtype.to_le_bytes());
65    out[8..12].copy_from_slice(&entry.manifold_idx.to_le_bytes());
66    out[12..16].copy_from_slice(&entry.rank.to_le_bytes());
67    for (index, dimension) in entry.dimensions.iter().enumerate() {
68        let start = 16 + index * 4;
69        out[start..start + 4].copy_from_slice(&dimension.to_le_bytes());
70    }
71    out[32..36].copy_from_slice(&entry.blob_offset.to_le_bytes());
72    out[36..40].copy_from_slice(&entry.blob_size.to_le_bytes());
73    out[40..48].copy_from_slice(&entry.source_offset.to_le_bytes());
74    out[48..56].copy_from_slice(&entry.source_name_hash.to_le_bytes());
75    out[56..58].copy_from_slice(&entry.alt_dtype.to_le_bytes());
76    out[58..60].copy_from_slice(&entry.precision_views_mask.to_le_bytes());
77    out[60..64].copy_from_slice(&entry.alt_blob_offset.to_le_bytes());
78}
79
80/// GGUF tensor-name suffix for a per-layer P64 role (None for global tensors, named directly).
81fn p64_role_suffix(role_id: u16) -> Option<&'static [u8]> {
82    match role_id {
83        P64_ROLE_ATTN_K => Some(b"attn_k.weight"),
84        P64_ROLE_ATTN_V => Some(b"attn_v.weight"),
85        P64_ROLE_ATTN_Q => Some(b"attn_q.weight"),
86        P64_ROLE_ATTN_OUTPUT => Some(b"attn_output.weight"),
87        P64_ROLE_FFN_GATE => Some(b"ffn_gate.weight"),
88        P64_ROLE_FFN_UP => Some(b"ffn_up.weight"),
89        P64_ROLE_FFN_DOWN => Some(b"ffn_down.weight"),
90        P64_ROLE_ATTN_NORM => Some(b"attn_norm.weight"),
91        P64_ROLE_FFN_NORM => Some(b"ffn_norm.weight"),
92        _ => None,
93    }
94}