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 / budget_tests.rs
| 1 | mod common; |
| 2 | |
| 3 | use aien_expert_residency::{ |
| 4 | AllocationReport, BudgetError, Category, KvShape, Ledger, MemoryBudget, ResidencyConfig, |
| 5 | ResidencyManager, |
| 6 | }; |
| 7 | use common::*; |
| 8 | |
| 9 | const GIB: u64 = 1 << 30; |
| 10 | |
| 11 | /// Qwen3-30B-A3B-like attention shape: 48 layers, 4 KV heads, head dim 128, |
| 12 | /// BF16, 32K context, 1 sequence. |
| 13 | fn qwen_kv(ctx: u32, seqs: u32) -> KvShape { |
| 14 | KvShape { |
| 15 | layers: 48, |
| 16 | kv_heads: 4, |
| 17 | head_dim: 128, |
| 18 | bytes_per_element: 2, |
| 19 | max_context_tokens: ctx, |
| 20 | max_sequences: seqs, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | fn spark_budget(ctx: u32) -> MemoryBudget { |
| 25 | MemoryBudget { |
| 26 | total_bytes: 128 * GIB, |
| 27 | safety_headroom_bytes: 8 * GIB, |
| 28 | non_expert_weight_bytes: 2 * GIB, |
| 29 | kv: qwen_kv(ctx, 1), |
| 30 | scratch_bytes: 2 * GIB, |
| 31 | staging_bytes: 256 * MIB, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[test] |
| 36 | fn kv_bytes_match_hand_computation() { |
| 37 | // 2 * 48 * 4 * 128 * 2 * 32768 * 1 = 3 GiB. |
| 38 | assert_eq!(qwen_kv(32768, 1).bytes().unwrap(), 3 * GIB); |
| 39 | assert_eq!(qwen_kv(32768, 4).bytes().unwrap(), 12 * GIB); |
| 40 | assert_eq!(kv_none().bytes().unwrap(), 0); |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn kv_overflow_is_refused() { |
| 45 | let kv = KvShape { |
| 46 | layers: u32::MAX, |
| 47 | kv_heads: u32::MAX, |
| 48 | head_dim: u32::MAX, |
| 49 | bytes_per_element: u32::MAX, |
| 50 | max_context_tokens: u32::MAX, |
| 51 | max_sequences: u32::MAX, |
| 52 | }; |
| 53 | assert_eq!(kv.bytes(), Err(BudgetError::Overflow { what: "kv cache" })); |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn ledger_partitions_total_exactly() { |
| 58 | let slot = 4_718_592; // one Qwen3-30B expert, 3 x 2048 x 768 FP8 |
| 59 | for ctx in [4096, 32768, 65536] { |
| 60 | let b = spark_budget(ctx); |
| 61 | let l = Ledger::plan(&b, slot, None).unwrap(); |
| 62 | let sum = l.safety_headroom_bytes |
| 63 | + l.non_expert_weight_bytes |
| 64 | + l.kv_cache_bytes |
| 65 | + l.scratch_bytes |
| 66 | + l.staging_bytes |
| 67 | + l.expert_pool_bytes |
| 68 | + l.unassigned_bytes; |
| 69 | assert_eq!(sum, b.total_bytes, "ctx {ctx}"); |
| 70 | assert!(l.unassigned_bytes < slot); |
| 71 | assert_eq!(l.expert_pool_bytes, u64::from(l.slot_count) * slot); |
| 72 | assert_eq!(l.committed_bytes() + l.unassigned_bytes, b.total_bytes); |
| 73 | // Never assume all memory can store experts. |
| 74 | assert!(l.expert_pool_bytes <= b.total_bytes - b.safety_headroom_bytes); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn longer_context_leaves_fewer_slots() { |
| 80 | let slot = 4_718_592; |
| 81 | let counts: Vec<u32> = [4096, 32768, 65536] |
| 82 | .iter() |
| 83 | .map(|&c| { |
| 84 | Ledger::plan(&spark_budget(c), slot, None) |
| 85 | .unwrap() |
| 86 | .slot_count |
| 87 | }) |
| 88 | .collect(); |
| 89 | assert!(counts[0] > counts[1] && counts[1] > counts[2], "{counts:?}"); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn oversubscription_is_refused() { |
| 94 | let mut b = spark_budget(32768); |
| 95 | b.non_expert_weight_bytes = 120 * GIB; |
| 96 | match Ledger::plan(&b, 4096, None) { |
| 97 | Err(BudgetError::Oversubscribed { required, total }) => { |
| 98 | assert!(required > total); |
| 99 | assert_eq!(total, 128 * GIB); |
| 100 | } |
| 101 | other => panic!("{other:?}"), |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #[test] |
| 106 | fn exactly_full_reservations_leave_no_slot() { |
| 107 | let b = MemoryBudget { |
| 108 | total_bytes: 10_000, |
| 109 | safety_headroom_bytes: 10_000, |
| 110 | non_expert_weight_bytes: 0, |
| 111 | kv: kv_none(), |
| 112 | scratch_bytes: 0, |
| 113 | staging_bytes: 0, |
| 114 | }; |
| 115 | assert_eq!( |
| 116 | Ledger::plan(&b, 1, None), |
| 117 | Err(BudgetError::NoExpertSlots { |
| 118 | available_bytes: 0, |
| 119 | slot_bytes: 1 |
| 120 | }) |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn less_than_one_slot_is_refused() { |
| 126 | let b = MemoryBudget { |
| 127 | total_bytes: 10_000, |
| 128 | safety_headroom_bytes: 6_000, |
| 129 | non_expert_weight_bytes: 0, |
| 130 | kv: kv_none(), |
| 131 | scratch_bytes: 0, |
| 132 | staging_bytes: 0, |
| 133 | }; |
| 134 | assert_eq!( |
| 135 | Ledger::plan(&b, 4096, None), |
| 136 | Err(BudgetError::NoExpertSlots { |
| 137 | available_bytes: 4_000, |
| 138 | slot_bytes: 4096 |
| 139 | }) |
| 140 | ); |
| 141 | assert_eq!(Ledger::plan(&b, 0, None), Err(BudgetError::ZeroSlotBytes)); |
| 142 | assert_eq!( |
| 143 | Ledger::plan(&b, 4000, Some(0)).map(|l| l.slot_count), |
| 144 | Err(BudgetError::NoExpertSlots { |
| 145 | available_bytes: 4_000, |
| 146 | slot_bytes: 4000 |
| 147 | }) |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn reservation_sum_overflow_is_refused() { |
| 153 | let mut b = spark_budget(4096); |
| 154 | b.safety_headroom_bytes = u64::MAX; |
| 155 | assert_eq!( |
| 156 | Ledger::plan(&b, 4096, None), |
| 157 | Err(BudgetError::Overflow { |
| 158 | what: "fixed reservations" |
| 159 | }) |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | fn max_slots_caps_pool_and_rest_is_unassigned() { |
| 165 | let b = spark_budget(4096); |
| 166 | let l = Ledger::plan(&b, MIB, Some(10)).unwrap(); |
| 167 | assert_eq!(l.slot_count, 10); |
| 168 | assert_eq!(l.expert_pool_bytes, 10 * MIB); |
| 169 | assert!(l.unassigned_bytes > 100 * GIB); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn allocation_report_checked_per_category() { |
| 174 | let l = Ledger::plan(&spark_budget(32768), MIB, Some(100)).unwrap(); |
| 175 | let ok = AllocationReport { |
| 176 | non_expert_weight_bytes: 2 * GIB, |
| 177 | kv_cache_bytes: 3 * GIB, |
| 178 | scratch_bytes: GIB, |
| 179 | staging_bytes: 256 * MIB, |
| 180 | expert_pool_bytes: 100 * MIB, |
| 181 | }; |
| 182 | assert_eq!(l.check_allocations(&ok), Ok(())); |
| 183 | let over = AllocationReport { |
| 184 | kv_cache_bytes: 3 * GIB + 1, |
| 185 | ..ok |
| 186 | }; |
| 187 | assert_eq!( |
| 188 | l.check_allocations(&over), |
| 189 | Err(BudgetError::AllocationExceedsReservation { |
| 190 | category: Category::KvCache, |
| 191 | reserved: 3 * GIB, |
| 192 | actual: 3 * GIB + 1 |
| 193 | }) |
| 194 | ); |
| 195 | let pool_over = AllocationReport { |
| 196 | expert_pool_bytes: 100 * MIB + 1, |
| 197 | ..ok |
| 198 | }; |
| 199 | assert!(matches!( |
| 200 | l.check_allocations(&pool_over), |
| 201 | Err(BudgetError::AllocationExceedsReservation { |
| 202 | category: Category::ExpertPool, |
| 203 | .. |
| 204 | }) |
| 205 | )); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn manager_config_limits_are_validated() { |
| 210 | let mut c = config(4, 2, 2 * SLOT); |
| 211 | c.max_in_flight = 0; |
| 212 | assert_eq!( |
| 213 | ResidencyManager::<u32>::new(c).err(), |
| 214 | Some(BudgetError::ZeroInFlight) |
| 215 | ); |
| 216 | let mut c = config(4, 2, 2 * SLOT); |
| 217 | c.max_in_flight_bytes = 3 * SLOT; |
| 218 | assert_eq!( |
| 219 | ResidencyManager::<u32>::new(c).err(), |
| 220 | Some(BudgetError::InFlightBytesExceedStaging { |
| 221 | max_in_flight_bytes: 3 * SLOT, |
| 222 | staging_bytes: 2 * SLOT |
| 223 | }) |
| 224 | ); |
| 225 | let mut c = config(4, 2, 2 * SLOT); |
| 226 | c.max_in_flight_bytes = SLOT - 1; |
| 227 | assert_eq!( |
| 228 | ResidencyManager::<u32>::new(c).err(), |
| 229 | Some(BudgetError::InFlightBytesBelowSlot { |
| 230 | max_in_flight_bytes: SLOT - 1, |
| 231 | slot_bytes: SLOT |
| 232 | }) |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | /// High-water marks stay within their reservations and are stable: running |
| 237 | /// the same workload twice more does not raise them past the first plateau. |
| 238 | #[test] |
| 239 | fn high_water_is_bounded_and_stable_at_varied_cache_sizes() { |
| 240 | for slots in [1u32, 2, 3, 8, 33] { |
| 241 | let c: ResidencyConfig = config(slots, 2, 2 * SLOT); |
| 242 | let mut m = ResidencyManager::new(c).unwrap(); |
| 243 | let mut rng = Rng(0x9E37_79B9 ^ u64::from(slots)); |
| 244 | let mut run = |m: &mut ResidencyManager<_>| { |
| 245 | for _ in 0..500 { |
| 246 | let k = id(rng.below(4) as u32, rng.below(16) as u32); |
| 247 | touch(m, k); |
| 248 | } |
| 249 | }; |
| 250 | run(&mut m); |
| 251 | let first = m.stats(); |
| 252 | run(&mut m); |
| 253 | run(&mut m); |
| 254 | let s = m.stats(); |
| 255 | assert_eq!( |
| 256 | s.peak_resident_bytes, first.peak_resident_bytes, |
| 257 | "slots {slots}" |
| 258 | ); |
| 259 | assert_eq!(s.peak_in_flight_bytes, first.peak_in_flight_bytes); |
| 260 | assert!(s.peak_resident_bytes <= m.ledger().expert_pool_bytes); |
| 261 | assert!(s.peak_in_flight_bytes <= m.ledger().staging_bytes); |
| 262 | assert_eq!(s.peak_resident_bytes, u64::from(slots.min(64)) * SLOT); |
| 263 | m.check_invariants().unwrap(); |
| 264 | } |
| 265 | } |