89 lines
2.7 KiB
Rust
89 lines
2.7 KiB
Rust
//! Model-free routed-expert fixture, using the production KAR writer.
|
|
use sha2::{Digest, Sha256};
|
|
use std::path::Path;
|
|
use leleka_control_plane::kar_writer::{self, SourceIdentity, Tensor, TensorData};
|
|
|
|
const H: u64 = 64;
|
|
const F: u64 = 32;
|
|
const E: u64 = 2;
|
|
const ROLE_BYTES: usize = (H * F * E * 4) as usize;
|
|
const NAMES: [&str; 3] = [
|
|
"blk.0.ffn_gate_exps.weight",
|
|
"blk.0.ffn_up_exps.weight",
|
|
"blk.0.ffn_down_exps.weight",
|
|
];
|
|
|
|
/// The three raw F32 tensors: expert 0 is 1/32 everywhere and expert 1 is -1/32.
|
|
fn raw_source() -> Vec<u8> {
|
|
(0..3)
|
|
.flat_map(|_| {
|
|
(0..H * F * E).flat_map(|i| {
|
|
(if i < H * F {
|
|
1.0f32 / 32.0
|
|
} else {
|
|
-1.0f32 / 32.0
|
|
})
|
|
.to_le_bytes()
|
|
})
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn source_identity(raw: &[u8]) -> SourceIdentity<'static> {
|
|
SourceIdentity {
|
|
name: "synthetic-routed-experts",
|
|
architecture: "synthetic",
|
|
builder: "leleka-api/kar-expert-fixture-v1",
|
|
digest: Sha256::digest(raw).into(),
|
|
total_elements: H * F * E * 3,
|
|
bytes: raw.len() as u64,
|
|
}
|
|
}
|
|
|
|
/// One routed-expert tensor of `role` (gate, up, down) over the shared scales, book and plane.
|
|
fn expert_tensor<'a>(
|
|
role: usize,
|
|
raw: &[u8],
|
|
dims: &'a [u64; 3],
|
|
nested: (&'a [u16], &'a [&'a [u16]], &'a [&'a [u32]]),
|
|
) -> Tensor<'a> {
|
|
let (scales, books, planes) = nested;
|
|
Tensor {
|
|
name: NAMES[role],
|
|
dims,
|
|
source_dtype: 0,
|
|
source_digest: Sha256::digest(&raw[role * ROLE_BYTES..(role + 1) * ROLE_BYTES]).into(),
|
|
source_offset: (role * ROLE_BYTES) as u64,
|
|
data: TensorData::Nested {
|
|
group: 32,
|
|
nesting: 0,
|
|
planes_min: 1,
|
|
planes_max: 1,
|
|
scales,
|
|
books,
|
|
planes,
|
|
},
|
|
}
|
|
}
|
|
|
|
fn main() -> Result<(), String> {
|
|
let args: Vec<_> = std::env::args().collect();
|
|
if args.len() != 2 {
|
|
return Err("usage: kar-expert-fixture NEW_OUTPUT.kar".into());
|
|
}
|
|
let scales = vec![0x2800u16; (H * F * E / 32) as usize]; // 1/32
|
|
let mut plane = vec![u32::MAX; (H * F / 32) as usize];
|
|
plane.extend(vec![0u32; (H * F / 32) as usize]);
|
|
let book = [0xbc00u16, 0x3c00]; // expert 0 positive, expert 1 negative
|
|
let books: [&[u16]; 1] = [&book];
|
|
let planes: [&[u32]; 1] = [&plane];
|
|
let raw = raw_source();
|
|
let source = source_identity(&raw);
|
|
let dims = [[H, F, E], [H, F, E], [F, H, E]];
|
|
let tensors: Vec<_> = (0..3)
|
|
.map(|role| expert_tensor(role, &raw, &dims[role], (&scales, &books, &planes)))
|
|
.collect();
|
|
kar_writer::write_archive(Path::new(&args[1]), &source, &tensors)?;
|
|
Ok(())
|
|
}
|