Files
leleka/crates/offline-tools/src/gguf_write.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 3485221865 feat(offline-tools): port embd_bf16_pack, ple_fp8_pack, draft_vocab, make_profile, mtp_rt
New crate leleka-offline-tools wired as 'strata-next tools ...'. Every output is byte-identical to the unmodified
Python tool (scripts/offline/diff_tools.py: packs incl. refusal cases, 14 draft-vocab subsets x out/stats, make_profile
at 512/256/8 experts with ranked bases and traces, mtp_rt at the real H/FF geometry). Committed tiny goldens +
integration tests; mtp_rt's multi-MB input is regenerated from a shared splitmix64 rule.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:40:02 +02:00

46 lines
1.5 KiB
Rust

//! The one-tensor GGUF v3 headers `embd_bf16_pack.py` and `ple_fp8_pack.py` write by hand.
pub const ALIGN: usize = 32;
const TYPE_STRING: u32 = 8;
const TYPE_F32: u32 = 6;
pub fn string(value: &str) -> Vec<u8> {
let mut out = (value.len() as u64).to_le_bytes().to_vec();
out.extend_from_slice(value.as_bytes());
out
}
pub fn kv_string(key: &str, value: &str) -> Vec<u8> {
let mut out = string(key);
out.extend_from_slice(&TYPE_STRING.to_le_bytes());
out.extend(string(value));
out
}
pub fn kv_f32(key: &str, value: f32) -> Vec<u8> {
let mut out = string(key);
out.extend_from_slice(&TYPE_F32.to_le_bytes());
out.extend_from_slice(&value.to_le_bytes());
out
}
/// Header of a GGUF with `kvs` metadata entries and exactly one 2-D tensor at data offset 0, padded to 32 bytes.
/// `dims` is GGUF order (fastest first).
pub fn one_tensor_header(kvs: &[Vec<u8>], name: &str, dims: [u64; 2], ggml_type: u32) -> Vec<u8> {
let mut head = b"GGUF".to_vec();
head.extend_from_slice(&3u32.to_le_bytes());
head.extend_from_slice(&1u64.to_le_bytes());
head.extend_from_slice(&(kvs.len() as u64).to_le_bytes());
for kv in kvs {
head.extend_from_slice(kv);
}
head.extend(string(name));
head.extend_from_slice(&2u32.to_le_bytes());
head.extend_from_slice(&dims[0].to_le_bytes());
head.extend_from_slice(&dims[1].to_le_bytes());
head.extend_from_slice(&ggml_type.to_le_bytes());
head.extend_from_slice(&0u64.to_le_bytes());
head.resize(head.len().next_multiple_of(ALIGN), 0);
head
}