alter_ego / serving /src /export.rs
geraldaton20's picture
initial upload: source code (no build artifacts)
f4f166e verified
Raw History Blame Contribute Delete
10.8 kB
//! Weight export for local-LLM interop (plan E6.4/E6.5):
//! - safetensors: HF-compatible F32 container of the frozen base
//! (loadable via training/student/model.py prototype class)
//! - GGUF mapped onto llama.cpp's existing `gpt2` architecture
//! (EXPERIMENTAL: tensor naming verified by inspection against llama.cpp
//! conversions; end-to-end load test tracked under plan E6.5 acceptance)
use std::collections::BTreeMap;
use std::io::Write;
use ndarray::Array2;
use crate::error::{Error, Result};
use crate::model::{ParamMap, SanConfig};
// ---------------------------------------------------------------------------
// safetensors
// ---------------------------------------------------------------------------
pub fn write_safetensors(params: &ParamMap, path: &str) -> Result<()> {
let mut header: BTreeMap<String, serde_json::Value> = BTreeMap::new();
let mut offset: usize = 0;
// deterministic ordering
for name in params.keys() {
let t = &params[name];
let nbytes = t.len() * 4;
let end = offset + nbytes;
header.insert(
name.clone(),
serde_json::json!({
"dtype": "F32",
"shape": [t.nrows(), t.ncols()],
"data_offsets": [offset, end],
}),
);
offset = end;
}
let mut header_bytes =
serde_json::to_vec(&header).map_err(|e| Error::Model(format!("header: {e}")))?;
while header_bytes.len() % 8 != 0 {
header_bytes.push(b' ');
}
let mut f = std::fs::File::create(path).map_err(|e| Error::Model(format!("io: {e}")))?;
f.write_all(&(header_bytes.len() as u64).to_le_bytes())
.map_err(|e| Error::Model(format!("io: {e}")))?;
f.write_all(&header_bytes).map_err(|e| Error::Model(format!("io: {e}")))?;
for name in params.keys() {
let t = &params[name];
if let Some(slice) = t.as_slice() {
for v in slice {
f.write_all(&v.to_le_bytes()).map_err(|e| Error::Model(format!("io: {e}")))?;
}
} else {
for v in t.iter() {
f.write_all(&v.to_le_bytes()).map_err(|e| Error::Model(format!("io: {e}")))?;
}
}
}
Ok(())
}
/// Parse a safetensors file we wrote (roundtrip verification helper).
pub fn read_safetensors_header(path: &str) -> Result<(usize, BTreeMap<String, (Vec<usize>, usize, usize)>)> {
let buf = std::fs::read(path).map_err(|e| Error::Model(format!("io: {e}")))?;
if buf.len() < 8 {
return Err(Error::Model("safetensors too short".into()));
}
let hlen = u64::from_le_bytes(buf[0..8].try_into().unwrap()) as usize;
let hdr: BTreeMap<String, serde_json::Value> = serde_json::from_slice(
&buf[8..8 + hlen],
)
.map_err(|e| Error::Model(format!("header parse: {e}")))?;
let mut out = BTreeMap::new();
for (k, v) in hdr {
if k == "__metadata__" {
continue;
}
let shape = v["shape"]
.as_array()
.map(|a| a.iter().filter_map(|x| x.as_u64()).map(|x| x as usize).collect())
.unwrap_or_default();
let offs = v["data_offsets"]
.as_array()
.map(|a| a.iter().filter_map(|x| x.as_u64()).map(|x| x as usize).collect::<Vec<_>>())
.unwrap_or_default();
out.insert(k, (shape, offs.first().copied().unwrap_or(0), offs.get(1).copied().unwrap_or(0)));
}
Ok((hlen, out))
}
// ---------------------------------------------------------------------------
// GGUF (gpt2 layout, F32)
// ---------------------------------------------------------------------------
const GGUF_MAGIC: u32 = 0x46554747; // "GGUF"
const T_F32: u32 = 0;
const KV_STRING: u32 = 8;
const KV_UINT32: u32 = 4;
fn w_str(f: &mut Vec<u8>, s: &str) {
f.extend((s.len() as u64).to_le_bytes());
f.extend(s.as_bytes());
}
fn kv_u32(f: &mut Vec<u8>, key: &str, val: u32) {
w_str(f, key);
f.extend(KV_UINT32.to_le_bytes());
f.extend(val.to_le_bytes());
}
fn kv_str(f: &mut Vec<u8>, key: &str, val: &str) {
w_str(f, key);
f.extend(KV_STRING.to_le_bytes());
w_str(f, val);
}
fn tensor_info(out: &mut Vec<u8>, name: &str, t: &Array2<f32>, offset: u64) {
w_str(out, name);
out.extend(2u32.to_le_bytes()); // n_dims
out.extend((t.ncols() as u64).to_le_bytes()); // ne[0]
out.extend((t.nrows() as u64).to_le_bytes()); // ne[1]
out.extend(T_F32.to_le_bytes()); // type
out.extend(offset.to_le_bytes());
}
/// Split packed QKV rows [3d, d] into three [d, d] matrices.
fn split_qkv(qkv: &Array2<f32>, d: usize) -> (Array2<f32>, Array2<f32>, Array2<f32>) {
(
qkv.slice(ndarray::s![0..d, ..]).to_owned(),
qkv.slice(ndarray::s![d..2 * d, ..]).to_owned(),
qkv.slice(ndarray::s![2 * d..3 * d, ..]).to_owned(),
)
}
pub fn write_gguf_gpt2(params: &ParamMap, cfg: &SanConfig, path: &str) -> Result<()> {
let mut infos: Vec<(String, Array2<f32>)> = Vec::new();
infos.push(("token_embd.weight".into(), params["tok_emb.weight"].clone()));
infos.push(("position_embd.weight".into(), params["pos"].clone()));
for i in 0..cfg.n_layers {
let pfx_in = format!("blocks.{i}.");
let pfx_out = format!("blk.{i}.");
infos.push((format!("{pfx_out}attn_norm.weight"), params[&format!("{pfx_in}ln1.weight")].clone()));
infos.push((format!("{pfx_out}attn_norm.bias"), params[&format!("{pfx_in}ln1.bias")].clone()));
let (q, k, v) = split_qkv(&params[&format!("{pfx_in}attn.qkv.weight")], cfg.d_model);
infos.push((format!("{pfx_out}attn_q.weight"), q));
infos.push((format!("{pfx_out}attn_k.weight"), k));
infos.push((format!("{pfx_out}attn_v.weight"), v));
infos.push((format!("{pfx_out}attn_o.weight"), params[&format!("{pfx_in}attn.o.weight")].clone()));
infos.push((format!("{pfx_out}ff_norm.weight"), params[&format!("{pfx_in}ln2.weight")].clone()));
infos.push((format!("{pfx_out}ff_norm.bias"), params[&format!("{pfx_in}ln2.bias")].clone()));
infos.push((format!("{pfx_out}mlp_fc.weight"), params[&format!("{pfx_in}mlp.0.weight")].clone()));
infos.push((format!("{pfx_out}mlp_proj.weight"), params[&format!("{pfx_in}mlp.2.weight")].clone()));
}
infos.push(("output_norm.weight".into(), params["ln_f.weight"].clone()));
infos.push(("output_norm.bias".into(), params["ln_f.bias"].clone()));
if let Some(h) = params.get("head.weight") {
infos.push(("output.weight".into(), h.clone())); // tied copy
}
let mut meta: Vec<u8> = Vec::new();
kv_str(&mut meta, "general.architecture", "gpt2");
kv_str(&mut meta, "general.name", "alter-ego-san");
kv_u32(&mut meta, "gpt2.context_length", cfg.max_seq_len as u32);
kv_u32(&mut meta, "gpt2.embedding_length", cfg.d_model as u32);
kv_u32(&mut meta, "gpt2.block_count", cfg.n_layers as u32);
kv_u32(&mut meta, "gpt2.head_count", cfg.n_heads as u32);
let mut infos_buf: Vec<u8> = Vec::new();
let mut data_offset: u64 = 0;
for (name, t) in &infos {
tensor_info(&mut infos_buf, name, t, data_offset);
data_offset += (t.len() * 4) as u64;
}
let mut out: Vec<u8> = Vec::new();
out.extend(GGUF_MAGIC.to_le_bytes());
out.extend(3u32.to_le_bytes()); // version
out.extend((infos.len() as u64).to_le_bytes());
out.extend(7u64.to_le_bytes()); // metadata kv count
out.extend(&meta);
out.extend(&infos_buf);
while out.len() % 32 != 0 {
out.push(0); // GENERAL_ALIGNMENT default 32
}
for (_n, t) in &infos {
for v in t.iter() {
out.extend(v.to_le_bytes());
}
}
std::fs::write(path, out).map_err(|e| Error::Model(format!("io: {e}")))?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
fn sample() -> ParamMap {
let mut p: ParamMap = HashMap::new();
p.insert("tok_emb.weight".into(), Array2::from_elem((16, 4), 0.5));
p.insert("pos".into(), Array2::from_elem((8, 4), 0.25));
p.insert("blocks.0.ln1.weight".into(), Array2::from_elem((1, 4), 1.0));
p.insert("blocks.0.ln1.bias".into(), Array2::zeros((1, 4)));
p.insert("blocks.0.attn.qkv.weight".into(), Array2::from_shape_fn((12, 4), |(r, c)| ((r * c) as f32) * 0.01));
p.insert("blocks.0.attn.o.weight".into(), Array2::from_elem((4, 4), 0.125));
p.insert("blocks.0.ln2.weight".into(), Array2::from_elem((1, 4), 1.0));
p.insert("blocks.0.ln2.bias".into(), Array2::zeros((1, 4)));
p.insert("blocks.0.mlp.0.weight".into(), Array2::from_elem((8, 4), 0.0625));
p.insert("blocks.0.mlp.0.bias".into(), Array2::zeros((1, 8)));
p.insert("blocks.0.mlp.2.weight".into(), Array2::from_elem((4, 8), 0.03125));
p.insert("blocks.0.mlp.2.bias".into(), Array2::zeros((1, 4)));
p.insert("ln_f.weight".into(), Array2::from_elem((1, 4), 1.0));
p.insert("ln_f.bias".into(), Array2::zeros((1, 4)));
p.insert("head.weight".into(), Array2::from_elem((16, 4), 0.5));
p
}
#[test]
fn safetensors_roundtrip_offsets() {
let dir = std::env::temp_dir().join("ae_st_test");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("m.safetensors");
let p = sample();
write_safetensors(&p, path.to_str().unwrap()).unwrap();
let (_hlen, entries) = read_safetensors_header(path.to_str().unwrap()).unwrap();
assert_eq!(entries.len(), p.len());
let (shape, s, e) = entries["tok_emb.weight"].clone();
assert_eq!(shape, vec![16, 4]);
assert_eq!(e - s, 16 * 4 * 4);
}
#[test]
fn gguf_header_sanity() {
let dir = std::env::temp_dir().join("ae_gg_test");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("m.gguf");
let cfg = SanConfig {
vocab: 16, d_model: 4, n_layers: 1, ffn_ratio: 2, n_heads: 2,
max_seq_len: 8, tied_embeddings: true,
};
let p = sample();
write_gguf_gpt2(&p, &cfg, path.to_str().unwrap()).unwrap();
let b = std::fs::read(&path).unwrap();
assert_eq!(&b[0..4], &GGUF_MAGIC.to_le_bytes());
assert_eq!(u32::from_le_bytes(b[4..8].try_into().unwrap()), 3);
let tc = u64::from_le_bytes(b[8..16].try_into().unwrap());
assert_eq!(tc, 15); // emb,pos + 10/block + outnorm.w/b + output
let kvc = u64::from_le_bytes(b[16..24].try_into().unwrap());
assert_eq!(kvc, 7);
}
}