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 / indexer_file_tests.rs
| 1 | //! Checkpoint indexer and the file-backed read path: random access to one |
| 2 | //! expert, byte-for-byte round trip against the checkpoint bytes, determinism, |
| 3 | //! read-only source, negative controls and on-disk damage. |
| 4 | |
| 5 | mod common; |
| 6 | |
| 7 | use aien_expert_store::format::hex; |
| 8 | use aien_expert_store::indexer::{index_qwen3_moe, read_expert, Checkpoint, Qwen3MoeExperts}; |
| 9 | use aien_expert_store::store::INDEX_FILE; |
| 10 | use aien_expert_store::{ |
| 11 | ExpertStore, IndexEntry, IoMode, Outcome, Projection, ReadAdapter, ReadError, ReadLimits, |
| 12 | StagingBuffer, StoreError, StoreLimits, |
| 13 | }; |
| 14 | use common::*; |
| 15 | use std::sync::Arc; |
| 16 | use std::time::{Duration, Instant}; |
| 17 | |
| 18 | fn shape() -> Qwen3MoeExperts { |
| 19 | Qwen3MoeExperts { |
| 20 | model_digest: MODEL, |
| 21 | hidden: H, |
| 22 | moe_intermediate: I, |
| 23 | experts: EXPERTS, |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn read_one(a: &ReadAdapter, e: IndexEntry) -> Outcome { |
| 28 | let b = StagingBuffer::new(e.object_len as usize).unwrap(); |
| 29 | a.submit(e, b, Instant::now() + Duration::from_secs(30)) |
| 30 | .unwrap(); |
| 31 | a.wait(Duration::from_secs(10)).unwrap().outcome |
| 32 | } |
| 33 | |
| 34 | fn dir_digest(dir: &std::path::Path) -> Vec<(String, [u8; 32])> { |
| 35 | let mut v: Vec<_> = std::fs::read_dir(dir) |
| 36 | .unwrap() |
| 37 | .map(|e| { |
| 38 | let p = e.unwrap().path(); |
| 39 | let d = aien_expert_store::format::sha256(&std::fs::read(&p).unwrap()); |
| 40 | (p.file_name().unwrap().to_string_lossy().into_owned(), d) |
| 41 | }) |
| 42 | .collect(); |
| 43 | v.sort(); |
| 44 | v |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn index_then_random_access_round_trips_byte_for_byte() { |
| 49 | let ck = TempDir::new("ckpt"); |
| 50 | let expected = write_checkpoint(ck.path()); |
| 51 | let before = dir_digest(ck.path()); |
| 52 | let st = TempDir::new("st"); |
| 53 | let mut store = ExpertStore::open(st.path(), StoreLimits::default()).unwrap(); |
| 54 | let mut ckpt = Checkpoint::open(ck.path()).unwrap(); |
| 55 | let entries = index_qwen3_moe(&mut ckpt, &shape(), 0..LAYERS, &mut store).unwrap(); |
| 56 | assert_eq!(entries.len(), (LAYERS * EXPERTS * 3) as usize); |
| 57 | assert_eq!( |
| 58 | dir_digest(ck.path()), |
| 59 | before, |
| 60 | "checkpoint must be untouched" |
| 61 | ); |
| 62 | |
| 63 | let store = ExpertStore::open(st.path(), StoreLimits::default()).unwrap(); |
| 64 | let a = ReadAdapter::new( |
| 65 | Arc::new(store.file_source(IoMode::Buffered)), |
| 66 | ReadLimits::default(), |
| 67 | ) |
| 68 | .unwrap(); |
| 69 | // Random access order, one expert at a time. |
| 70 | for i in [7usize, 0, 17, 3, 11, 16, 1] { |
| 71 | let (k, w, s) = &expected[i]; |
| 72 | let e = store.get(k).expect("indexed"); |
| 73 | match read_one(&a, e) { |
| 74 | Outcome::Ready(r) => { |
| 75 | assert_eq!(r.weights(), &w[..], "weights of {k:?}"); |
| 76 | assert_eq!(r.scales(), &s[..], "scales of {k:?}"); |
| 77 | } |
| 78 | o => panic!("{o:?}"), |
| 79 | } |
| 80 | // Same bytes as a direct read of the checkpoint tensor. |
| 81 | let mut c = Checkpoint::open(ck.path()).unwrap(); |
| 82 | let (dw, ds, _) = |
| 83 | read_expert(&mut c, &shape(), k.layer, k.expert, k.projection, u64::MAX).unwrap(); |
| 84 | assert_eq!((&dw, &ds), (w, s)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn indexing_is_deterministic() { |
| 90 | let ck = TempDir::new("det"); |
| 91 | write_checkpoint(ck.path()); |
| 92 | let (s1, s2) = (TempDir::new("d1"), TempDir::new("d2")); |
| 93 | for s in [&s1, &s2] { |
| 94 | let mut store = ExpertStore::open(s.path(), StoreLimits::default()).unwrap(); |
| 95 | let mut ckpt = Checkpoint::open(ck.path()).unwrap(); |
| 96 | index_qwen3_moe(&mut ckpt, &shape(), 0..LAYERS, &mut store).unwrap(); |
| 97 | } |
| 98 | assert_eq!( |
| 99 | std::fs::read(s1.path().join(INDEX_FILE)).unwrap(), |
| 100 | std::fs::read(s2.path().join(INDEX_FILE)).unwrap() |
| 101 | ); |
| 102 | assert_eq!( |
| 103 | dir_digest(&s1.path().join("objects")), |
| 104 | dir_digest(&s2.path().join("objects")) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | fn negative_controls_missing_dtype_shape_expert_and_store_location() { |
| 110 | let ck = TempDir::new("neg"); |
| 111 | write_checkpoint(ck.path()); |
| 112 | let st = TempDir::new("negst"); |
| 113 | let mut store = ExpertStore::open(st.path(), StoreLimits::default()).unwrap(); |
| 114 | let mut ckpt = Checkpoint::open(ck.path()).unwrap(); |
| 115 | |
| 116 | // Missing layer. |
| 117 | assert!(matches!( |
| 118 | index_qwen3_moe(&mut ckpt, &shape(), 0..LAYERS + 1, &mut store), |
| 119 | Err(StoreError::MissingTensor(_)) |
| 120 | )); |
| 121 | assert!( |
| 122 | !st.path().join(INDEX_FILE).exists(), |
| 123 | "nothing committed on failure" |
| 124 | ); |
| 125 | |
| 126 | // Shape mismatch (wrong hidden size). |
| 127 | let mut bad = shape(); |
| 128 | bad.hidden = H + 128; |
| 129 | assert!(matches!( |
| 130 | read_expert(&mut ckpt, &bad, 0, 0, Projection::Gate, u64::MAX), |
| 131 | Err(StoreError::ShapeMismatch { .. }) |
| 132 | )); |
| 133 | // Expert id out of range. |
| 134 | assert!(matches!( |
| 135 | read_expert(&mut ckpt, &shape(), 0, EXPERTS, Projection::Gate, u64::MAX), |
| 136 | Err(StoreError::MissingTensor(_)) |
| 137 | )); |
| 138 | // Read bound. |
| 139 | assert!(matches!( |
| 140 | read_expert(&mut ckpt, &shape(), 0, 0, Projection::Gate, 100), |
| 141 | Err(StoreError::ObjectTooLarge { .. }) |
| 142 | )); |
| 143 | // Store inside the checkpoint directory. |
| 144 | let inner = ck.path().join("store"); |
| 145 | let mut s2 = ExpertStore::open(&inner, StoreLimits::default()).unwrap(); |
| 146 | assert_eq!( |
| 147 | index_qwen3_moe(&mut ckpt, &shape(), 0..1, &mut s2).unwrap_err(), |
| 148 | StoreError::StoreInsideCheckpoint |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn dtype_mismatch_and_hostile_shard_names_are_refused() { |
| 154 | let ck = TempDir::new("dt"); |
| 155 | let w = pattern((I * H) as usize, 1); |
| 156 | write_shard( |
| 157 | &ck.path().join("a.safetensors"), |
| 158 | &[ |
| 159 | T { |
| 160 | name: "model.layers.0.mlp.experts.0.gate_proj.weight".into(), |
| 161 | dtype: "BF16", |
| 162 | shape: vec![I, H], |
| 163 | data: w, |
| 164 | }, |
| 165 | T { |
| 166 | name: "model.layers.0.mlp.experts.0.up_proj.weight".into(), |
| 167 | dtype: "F8_E4M3", |
| 168 | shape: vec![I, H], |
| 169 | data: pattern((I * H) as usize, 2), |
| 170 | }, |
| 171 | T { |
| 172 | name: "model.layers.0.mlp.experts.0.up_proj.weight_scale_inv".into(), |
| 173 | dtype: "F16", |
| 174 | shape: vec![2, 2], |
| 175 | data: vec![0; 8], |
| 176 | }, |
| 177 | ], |
| 178 | ); |
| 179 | let index = serde_json::json!({"weight_map": { |
| 180 | "model.layers.0.mlp.experts.0.gate_proj.weight": "a.safetensors", |
| 181 | "model.layers.0.mlp.experts.0.up_proj.weight": "a.safetensors", |
| 182 | "model.layers.0.mlp.experts.0.up_proj.weight_scale_inv": "a.safetensors", |
| 183 | "model.layers.0.mlp.experts.0.down_proj.weight": "../escape.safetensors", |
| 184 | }}); |
| 185 | std::fs::write( |
| 186 | ck.path().join("model.safetensors.index.json"), |
| 187 | serde_json::to_vec(&index).unwrap(), |
| 188 | ) |
| 189 | .unwrap(); |
| 190 | let mut c = Checkpoint::open(ck.path()).unwrap(); |
| 191 | assert!(matches!( |
| 192 | read_expert(&mut c, &shape(), 0, 0, Projection::Gate, u64::MAX), |
| 193 | Err(StoreError::DtypeMismatch { .. }) |
| 194 | )); |
| 195 | assert!(matches!( |
| 196 | read_expert(&mut c, &shape(), 0, 0, Projection::Up, u64::MAX), |
| 197 | Err(StoreError::DtypeMismatch { .. }) |
| 198 | )); |
| 199 | assert!(matches!( |
| 200 | read_expert(&mut c, &shape(), 0, 0, Projection::Down, u64::MAX), |
| 201 | Err(StoreError::Checkpoint(_)) |
| 202 | )); |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn on_disk_damage_and_symlinks_fail_closed() { |
| 207 | let st = TempDir::new("dmg"); |
| 208 | let mut store = ExpertStore::open(st.path(), StoreLimits::default()).unwrap(); |
| 209 | let k = key(0, 0, Projection::Down); |
| 210 | let (_, _, w, s) = artifact(k, 256, 192, 4); |
| 211 | let e = store |
| 212 | .put(k, 256, 192, aien_expert_store::ScaleDtype::Bf16, &w, &s) |
| 213 | .unwrap(); |
| 214 | let a = ReadAdapter::new( |
| 215 | Arc::new(store.file_source(IoMode::Buffered)), |
| 216 | ReadLimits::default(), |
| 217 | ) |
| 218 | .unwrap(); |
| 219 | assert!(matches!(read_one(&a, e), Outcome::Ready(_))); |
| 220 | let p = st.path().join("objects").join(hex(&e.data_digest)); |
| 221 | let good = std::fs::read(&p).unwrap(); |
| 222 | |
| 223 | let mut flipped = good.clone(); |
| 224 | flipped[4096 + 10] ^= 0x80; |
| 225 | std::fs::write(&p, &flipped).unwrap(); |
| 226 | assert!(matches!( |
| 227 | read_one(&a, e), |
| 228 | Outcome::Failed { |
| 229 | error: ReadError::DigestMismatch, |
| 230 | .. |
| 231 | } |
| 232 | )); |
| 233 | |
| 234 | std::fs::write(&p, &good[..good.len() - 4096]).unwrap(); |
| 235 | assert!(matches!( |
| 236 | read_one(&a, e), |
| 237 | Outcome::Failed { |
| 238 | error: ReadError::LengthMismatch { .. }, |
| 239 | .. |
| 240 | } |
| 241 | )); |
| 242 | |
| 243 | // A planted symlink is not followed. |
| 244 | let elsewhere = st.path().join("elsewhere"); |
| 245 | std::fs::write(&elsewhere, &good).unwrap(); |
| 246 | std::fs::remove_file(&p).unwrap(); |
| 247 | std::os::unix::fs::symlink(&elsewhere, &p).unwrap(); |
| 248 | match read_one(&a, e) { |
| 249 | Outcome::Failed { |
| 250 | error: ReadError::Io(_), |
| 251 | buf, |
| 252 | } => { |
| 253 | assert!(buf.as_slice().iter().all(|&b| b == 0)) |
| 254 | } |
| 255 | o => panic!("{o:?}"), |
| 256 | } |
| 257 | |
| 258 | std::fs::remove_file(&p).unwrap(); |
| 259 | assert!(matches!( |
| 260 | read_one(&a, e), |
| 261 | Outcome::Failed { |
| 262 | error: ReadError::NotFound, |
| 263 | .. |
| 264 | } |
| 265 | )); |
| 266 | } |
| 267 | |
| 268 | #[test] |
| 269 | fn direct_mode_reads_verify_or_refuse_explicitly() { |
| 270 | let st = TempDir::new("direct"); |
| 271 | let mut store = ExpertStore::open(st.path(), StoreLimits::default()).unwrap(); |
| 272 | let k = key(0, 1, Projection::Gate); |
| 273 | let (_, _, w, s) = artifact(k, 192, 256, 5); |
| 274 | let e = store |
| 275 | .put(k, 192, 256, aien_expert_store::ScaleDtype::Bf16, &w, &s) |
| 276 | .unwrap(); |
| 277 | let a = ReadAdapter::new( |
| 278 | Arc::new(store.file_source(IoMode::Direct)), |
| 279 | ReadLimits::default(), |
| 280 | ) |
| 281 | .unwrap(); |
| 282 | match read_one(&a, e) { |
| 283 | Outcome::Ready(r) => { |
| 284 | assert_eq!(r.weights(), &w[..]); |
| 285 | eprintln!("direct mode: verified read"); |
| 286 | } |
| 287 | // Not Linux, or a file system without O_DIRECT (tmpfs): an explicit |
| 288 | // refusal, never a silent buffered fallback. |
| 289 | Outcome::Failed { |
| 290 | error: ReadError::DirectIoUnsupported, |
| 291 | .. |
| 292 | } => eprintln!("direct mode: refused (DirectIoUnsupported)"), |
| 293 | o => panic!("{o:?}"), |
| 294 | } |
| 295 | } |