Skip to main content

qualia_core_db/q42/p64_weight/compiler/
mod.rs

1//! GGUF compiler policies and compatibility entrypoints.
2use super::*;
3use crate::gguf_sharder::{GgufTensorIndex, GgufTensorInfo};
4mod converted;
5mod ffn;
6mod legacy;
7mod names;
8mod native_metadata;
9pub use native_metadata::gguf_native_model_metadata;
10mod planning;
11mod verbatim;
12pub use ffn::*;
13use names::p64_tensor_name;
14pub use planning::GgufCompileTensor;
15pub use verbatim::*;
16/// Conversion-time weight layout policy for [`compile_gguf_to_p64_with_layout`].
17///
18/// The designed product path is: import GGUF once → store GPU-friendly bytes in `.p64`.
19/// [`P64ConvertLayout::Verbatim`] is the historical byte-preserving container swap
20/// (same kernels, same speed). [`P64ConvertLayout::F16Expand`] dequantizes 2-D weight
21/// matrices to IEEE f16 so decode can use the fast `unpack2x16float` path.
22/// [`P64ConvertLayout::Q4kSoa`] rewrites Q4_K matrices to a 160 B/superblock SoA with
23/// pre-expanded f16 sub-scales (decode GEMV skips scale unpack + header barriers).
24#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
25pub enum P64ConvertLayout {
26    /// Copy GGML quant blocks byte-for-byte (no speed change vs running the GGUF).
27    #[default]
28    Verbatim,
29    /// Expand 2-D matrices (attn/FFN/embd/output) to f16; leave 1-D norms as source.
30    /// Rejected if the result would exceed the 4 GiB u32-offset container limit.
31    F16Expand,
32    /// Convert 2-D Q4_K weight matrices to [`crate::ggml_quants::GGML_TYPE_Q4_K_SOA`].
33    /// Other tensors stay verbatim. ~11% larger than Q4_K; aimed at 3B-class decode.
34    Q4kSoa,
35}
36
37/// Compile all source tensors with the original semantic order and metadata.
38/// Verbatim payloads use Etalon's checked layer-packed P64 writer. A zero page
39/// selector retains the 16 KiB default. Parsing retains its existing cold policy.
40pub fn compile_gguf_to_p64(input: &[u8], page_log2: u16) -> Result<Vec<u8>, String> {
41    compile_gguf_to_p64_with_layout(input, page_log2, P64ConvertLayout::Verbatim)
42}
43/// Compatibility alias for the historical pre-P64 API name.
44pub fn compile_gguf_to_q42(input: &[u8], page_log2: u16) -> Result<Vec<u8>, String> {
45    compile_gguf_to_p64(input, page_log2)
46}
47
48/// Select the original numerical conversion policy. Verbatim uses Etalon serialization.
49pub fn compile_gguf_to_p64_with_layout(
50    input: &[u8],
51    page_log2: u16,
52    layout: P64ConvertLayout,
53) -> Result<Vec<u8>, String> {
54    if layout == P64ConvertLayout::Verbatim {
55        verbatim::compile_compat(input, page_log2)
56    } else {
57        converted::compile_converted(input, page_log2, layout)
58    }
59}