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-residency / tests / common / mod.rs
| 1 | #![allow(dead_code)] |
| 2 | |
| 3 | use aien_expert_residency::{ |
| 4 | Acquired, ExpertIdentity, ExpertPart, FetchTicket, KvShape, MemoryBudget, Pin, ResidencyConfig, |
| 5 | ResidencyManager, |
| 6 | }; |
| 7 | |
| 8 | pub const SLOT: u64 = 4096; |
| 9 | pub const MIB: u64 = 1 << 20; |
| 10 | |
| 11 | pub fn kv_none() -> KvShape { |
| 12 | KvShape { |
| 13 | layers: 0, |
| 14 | kv_heads: 0, |
| 15 | head_dim: 0, |
| 16 | bytes_per_element: 0, |
| 17 | max_context_tokens: 0, |
| 18 | max_sequences: 0, |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | /// A config with exactly `slots` slots of `SLOT` bytes and the given |
| 23 | /// in-flight limits. Staging is sized to `max_in_flight_bytes`. |
| 24 | pub fn config(slots: u32, max_in_flight: u32, max_in_flight_bytes: u64) -> ResidencyConfig { |
| 25 | let staging = max_in_flight_bytes; |
| 26 | ResidencyConfig { |
| 27 | budget: MemoryBudget { |
| 28 | total_bytes: 1000 + staging + u64::from(slots) * SLOT + 100, |
| 29 | safety_headroom_bytes: 1000, |
| 30 | non_expert_weight_bytes: 0, |
| 31 | kv: kv_none(), |
| 32 | scratch_bytes: 0, |
| 33 | staging_bytes: staging, |
| 34 | }, |
| 35 | slot_bytes: SLOT, |
| 36 | max_slots: Some(slots), |
| 37 | max_in_flight, |
| 38 | max_in_flight_bytes, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | pub fn manager(slots: u32) -> ResidencyManager<ExpertIdentity> { |
| 43 | ResidencyManager::new(config(slots, slots.max(1), u64::from(slots.max(1)) * SLOT)).unwrap() |
| 44 | } |
| 45 | |
| 46 | pub fn id(layer: u32, expert: u32) -> ExpertIdentity { |
| 47 | ExpertIdentity { |
| 48 | checkpoint_digest: [7; 32], |
| 49 | layer, |
| 50 | expert, |
| 51 | part: ExpertPart::Down, |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /// Drive a fetch ticket all the way to Ready. |
| 56 | pub fn complete(m: &mut ResidencyManager<ExpertIdentity>, t: &FetchTicket, now: u64) { |
| 57 | m.mark_verified(t).unwrap(); |
| 58 | m.mark_copying(t).unwrap(); |
| 59 | m.publish(t, now).unwrap(); |
| 60 | } |
| 61 | |
| 62 | /// Acquire `key`, completing a fetch if it misses. Returns the pin. |
| 63 | pub fn load(m: &mut ResidencyManager<ExpertIdentity>, key: ExpertIdentity) -> Pin { |
| 64 | match m.acquire(key, SLOT, 0).unwrap() { |
| 65 | Acquired::Ready(p) | Acquired::Pending(p) => p, |
| 66 | Acquired::Fetch(p, t) => { |
| 67 | complete(m, &t, 0); |
| 68 | p |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// Load and immediately release. |
| 74 | pub fn touch(m: &mut ResidencyManager<ExpertIdentity>, key: ExpertIdentity) { |
| 75 | let p = load(m, key); |
| 76 | m.release(p).unwrap(); |
| 77 | } |
| 78 | |
| 79 | /// xorshift64*: deterministic, dependency-free generator for property tests. |
| 80 | pub struct Rng(pub u64); |
| 81 | |
| 82 | impl Rng { |
| 83 | pub fn next(&mut self) -> u64 { |
| 84 | let mut x = self.0; |
| 85 | x ^= x >> 12; |
| 86 | x ^= x << 25; |
| 87 | x ^= x >> 27; |
| 88 | self.0 = x; |
| 89 | x.wrapping_mul(0x2545_F491_4F6C_DD1D) |
| 90 | } |
| 91 | pub fn below(&mut self, n: u64) -> u64 { |
| 92 | self.next() % n |
| 93 | } |
| 94 | } |