aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-expert-store / tests / common / mod.rs
| 1 | #![allow(dead_code)] |
| 2 | |
| 3 | use aien_expert_store::format::build_artifact; |
| 4 | use aien_expert_store::{ExpertKey, IndexEntry, Projection, QuantFormat, ScaleDtype}; |
| 5 | use std::path::{Path, PathBuf}; |
| 6 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 7 | |
| 8 | pub const MODEL: [u8; 32] = [7u8; 32]; |
| 9 | |
| 10 | pub fn key(layer: u32, expert: u32, projection: Projection) -> ExpertKey { |
| 11 | ExpertKey { |
| 12 | model_digest: MODEL, |
| 13 | layer, |
| 14 | expert, |
| 15 | projection, |
| 16 | quant: QuantFormat::Fp8E4m3Block128, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | /// Deterministic filler (no randomness). |
| 21 | pub fn pattern(len: usize, seed: u32) -> Vec<u8> { |
| 22 | let mut x = seed.wrapping_mul(2_654_435_761).wrapping_add(1); |
| 23 | (0..len) |
| 24 | .map(|_| { |
| 25 | x ^= x << 13; |
| 26 | x ^= x >> 17; |
| 27 | x ^= x << 5; |
| 28 | (x >> 8) as u8 |
| 29 | }) |
| 30 | .collect() |
| 31 | } |
| 32 | |
| 33 | /// A canonical artifact and its index entry for a `[rows][cols]` projection. |
| 34 | pub fn artifact( |
| 35 | k: ExpertKey, |
| 36 | rows: u64, |
| 37 | cols: u64, |
| 38 | seed: u32, |
| 39 | ) -> (IndexEntry, Vec<u8>, Vec<u8>, Vec<u8>) { |
| 40 | let w = pattern((rows * cols) as usize, seed); |
| 41 | let s = pattern( |
| 42 | (rows.div_ceil(128) * cols.div_ceil(128) * 2) as usize, |
| 43 | seed + 1, |
| 44 | ); |
| 45 | let (h, bytes) = build_artifact(k, rows, cols, ScaleDtype::Bf16, &w, &s, u64::MAX).unwrap(); |
| 46 | let e = IndexEntry { |
| 47 | key: k, |
| 48 | data_digest: aien_expert_store::format::sha256(&bytes), |
| 49 | object_len: h.object_len, |
| 50 | }; |
| 51 | (e, bytes, w, s) |
| 52 | } |
| 53 | |
| 54 | static DIR_COUNTER: AtomicU64 = AtomicU64::new(0); |
| 55 | |
| 56 | /// A fresh empty directory under the system temp dir, removed on drop. |
| 57 | pub struct TempDir(pub PathBuf); |
| 58 | |
| 59 | impl TempDir { |
| 60 | pub fn new(tag: &str) -> Self { |
| 61 | let p = std::env::temp_dir().join(format!( |
| 62 | "aien-expert-store-{tag}-{}-{}", |
| 63 | std::process::id(), |
| 64 | DIR_COUNTER.fetch_add(1, Ordering::Relaxed) |
| 65 | )); |
| 66 | let _ = std::fs::remove_dir_all(&p); |
| 67 | std::fs::create_dir_all(&p).unwrap(); |
| 68 | Self(p) |
| 69 | } |
| 70 | |
| 71 | pub fn path(&self) -> &Path { |
| 72 | &self.0 |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl Drop for TempDir { |
| 77 | fn drop(&mut self) { |
| 78 | let _ = std::fs::remove_dir_all(&self.0); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// One tensor for [`write_shard`]. |
| 83 | pub struct T { |
| 84 | pub name: String, |
| 85 | pub dtype: &'static str, |
| 86 | pub shape: Vec<u64>, |
| 87 | pub data: Vec<u8>, |
| 88 | } |
| 89 | |
| 90 | /// Writes a safetensors file holding `tensors` in order. |
| 91 | pub fn write_shard(path: &Path, tensors: &[T]) { |
| 92 | let mut header = serde_json::Map::new(); |
| 93 | let mut off = 0u64; |
| 94 | for t in tensors { |
| 95 | let end = off + t.data.len() as u64; |
| 96 | header.insert( |
| 97 | t.name.clone(), |
| 98 | serde_json::json!({"dtype": t.dtype, "shape": t.shape, "data_offsets": [off, end]}), |
| 99 | ); |
| 100 | off = end; |
| 101 | } |
| 102 | let h = serde_json::to_vec(&serde_json::Value::Object(header)).unwrap(); |
| 103 | let mut out = (h.len() as u64).to_le_bytes().to_vec(); |
| 104 | out.extend_from_slice(&h); |
| 105 | for t in tensors { |
| 106 | out.extend_from_slice(&t.data); |
| 107 | } |
| 108 | std::fs::write(path, out).unwrap(); |
| 109 | } |
| 110 | |
| 111 | pub const H: u64 = 256; |
| 112 | pub const I: u64 = 192; // not a block multiple: exercises the ceil scale shape |
| 113 | pub const LAYERS: u32 = 2; |
| 114 | pub const EXPERTS: u32 = 3; |
| 115 | |
| 116 | pub fn proj_shape(p: Projection) -> (u64, u64) { |
| 117 | match p { |
| 118 | Projection::Gate | Projection::Up => (I, H), |
| 119 | Projection::Down => (H, I), |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// A tiny Qwen3-MoE FP8 checkpoint: layer `l` in shard `l`, BF16 scales, |
| 124 | /// plus a non-expert tensor. Returns the expected (weights, scales) per key. |
| 125 | pub fn write_checkpoint(dir: &Path) -> Vec<(ExpertKey, Vec<u8>, Vec<u8>)> { |
| 126 | let mut expected = Vec::new(); |
| 127 | let mut weight_map = serde_json::Map::new(); |
| 128 | for l in 0..LAYERS { |
| 129 | let shard = format!("model-{:05}-of-{:05}.safetensors", l + 1, LAYERS); |
| 130 | let mut ts = vec![T { |
| 131 | name: format!("model.layers.{l}.mlp.gate.weight"), |
| 132 | dtype: "BF16", |
| 133 | shape: vec![EXPERTS as u64, H], |
| 134 | data: pattern((EXPERTS as u64 * H * 2) as usize, 999 + l), |
| 135 | }]; |
| 136 | for e in 0..EXPERTS { |
| 137 | for p in Projection::ALL { |
| 138 | let (r, c) = proj_shape(p); |
| 139 | let seed = l * 100 + e * 10 + p as u32; |
| 140 | let w = pattern((r * c) as usize, seed); |
| 141 | let s = pattern( |
| 142 | (r.div_ceil(128) * c.div_ceil(128) * 2) as usize, |
| 143 | seed + 5000, |
| 144 | ); |
| 145 | let base = format!("model.layers.{l}.mlp.experts.{e}.{}", p.checkpoint_name()); |
| 146 | ts.push(T { |
| 147 | name: format!("{base}.weight"), |
| 148 | dtype: "F8_E4M3", |
| 149 | shape: vec![r, c], |
| 150 | data: w.clone(), |
| 151 | }); |
| 152 | ts.push(T { |
| 153 | name: format!("{base}.weight_scale_inv"), |
| 154 | dtype: "BF16", |
| 155 | shape: vec![r.div_ceil(128), c.div_ceil(128)], |
| 156 | data: s.clone(), |
| 157 | }); |
| 158 | expected.push((key(l, e, p), w, s)); |
| 159 | } |
| 160 | } |
| 161 | for t in &ts { |
| 162 | weight_map.insert(t.name.clone(), serde_json::Value::String(shard.clone())); |
| 163 | } |
| 164 | write_shard(&dir.join(&shard), &ts); |
| 165 | } |
| 166 | let index = serde_json::json!({"metadata": {}, "weight_map": weight_map}); |
| 167 | std::fs::write( |
| 168 | dir.join("model.safetensors.index.json"), |
| 169 | serde_json::to_vec(&index).unwrap(), |
| 170 | ) |
| 171 | .unwrap(); |
| 172 | expected |
| 173 | } |