File size: 10,841 Bytes
f4f166e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//! 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);
    }
}