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-inference-abi / tests / gb10_serving_reservation.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! sovereign-core#277 (cut toward it, not a fix): when the daemon serves Qwen3 on the GB10 |
| 17 | //! (the already opt-in path), start-up reserves the serving buffers from declared bounds, |
| 18 | //! prepares every matmul kernel, seals, and refuses to serve by name when omega refuses. |
| 19 | //! CPU only: the omega calls are injected, nothing touches a driver. The daemon-level ordering |
| 20 | //! tests (prepare of every rows x shape, seal last) live in aien-cli `commands.rs`. |
| 21 | use aien_inference_abi::gb10_serving::{ |
| 22 | reserve_gb10_serving_with, serving_matmul_shapes, ServingLimits, ServingOps, |
| 23 | }; |
| 24 | use aien_inference_abi::model_config_from_hf_json; |
| 25 | use aien_omega_gpu::ServingBounds; |
| 26 | use std::cell::{Cell, RefCell}; |
| 27 | use std::path::PathBuf; |
| 28 | |
| 29 | fn qwen3_4b() -> aien_inference_abi::ModelConfig { |
| 30 | let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 31 | .join("fixtures") |
| 32 | .join("qwen3-4b-instruct-2507-config"); |
| 33 | let cfg = std::fs::read_to_string(dir.join("config.json")).unwrap(); |
| 34 | let gen = std::fs::read_to_string(dir.join("generation_config.json")).unwrap(); |
| 35 | model_config_from_hf_json("Qwen/Qwen3-4B-Instruct-2507", &cfg, Some(&gen)).unwrap() |
| 36 | } |
| 37 | |
| 38 | /// The daemon's declared limits (max_batch_size 256, prefill chunk 128, arena 4096). |
| 39 | fn daemon_limits() -> ServingLimits { |
| 40 | ServingLimits { |
| 41 | context_tokens: 4096, |
| 42 | max_batch_rows: 256, |
| 43 | prefill_chunk_rows: 128, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[derive(Default)] |
| 48 | struct Ops { |
| 49 | reserved: RefCell<Vec<ServingBounds>>, |
| 50 | prepares: Cell<u32>, |
| 51 | seals: Cell<u32>, |
| 52 | refuse_reserve: bool, |
| 53 | } |
| 54 | |
| 55 | impl ServingOps for Ops { |
| 56 | fn reserve(&self, b: &ServingBounds) -> Result<(), String> { |
| 57 | self.reserved.borrow_mut().push(*b); |
| 58 | if self.refuse_reserve { |
| 59 | Err("omega_gpu rc=-4 (CHIP_FAIL) (stage: nvrm_alloc scratch)".to_string()) |
| 60 | } else { |
| 61 | Ok(()) |
| 62 | } |
| 63 | } |
| 64 | fn prepare(&self, _m: u32, _k: u32, _n: u32) -> Result<(), String> { |
| 65 | self.prepares.set(self.prepares.get() + 1); |
| 66 | Ok(()) |
| 67 | } |
| 68 | fn seal(&self) { |
| 69 | self.seals.set(self.seals.get() + 1); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn qwen3_on_gb10_reserves_once_with_the_declared_bounds() { |
| 75 | let ops = Ops::default(); |
| 76 | let got = reserve_gb10_serving_with(&qwen3_4b(), &daemon_limits(), true, true, &ops) |
| 77 | .expect("reserved") |
| 78 | .expect("Qwen3 on GB10 must reserve"); |
| 79 | let calls = ops.reserved.borrow(); |
| 80 | assert_eq!(calls.len(), 1, "exactly one reservation call"); |
| 81 | let b = calls[0]; |
| 82 | assert_eq!(got.bounds, b); |
| 83 | assert_eq!((b.num_q_heads, b.num_kv_heads, b.head_dim), (32, 8, 128)); |
| 84 | assert_eq!(b.max_context, 4096); |
| 85 | assert_eq!( |
| 86 | b.max_seqs, 1, |
| 87 | "the GB10 backend runs attention one sequence at a time" |
| 88 | ); |
| 89 | assert_eq!(b.kv_block_size as usize, qwen3_4b().block_size); |
| 90 | assert_eq!( |
| 91 | b.max_rows, 256, |
| 92 | "decode batch rows (256) exceed the prefill chunk (128)" |
| 93 | ); |
| 94 | assert_eq!(b.max_k, 9728, "down_proj input is the intermediate size"); |
| 95 | assert_eq!(b.max_n_one_row, 151936, "the logits projection"); |
| 96 | assert!(b.max_n >= 9728); |
| 97 | // omega's own sweep (omega#338, fake layer): Qwen3-4B needs 95 distinct kernels, so the 32 |
| 98 | // slots of omega#333 are not enough; 128 is omega's maximum. |
| 99 | assert_eq!(b.kernel_slots, 128); |
| 100 | assert_eq!( |
| 101 | got.bytes.total(), |
| 102 | 194_170_880, |
| 103 | "bytes asked for the daemon bounds" |
| 104 | ); |
| 105 | assert_eq!((ops.prepares.get(), ops.seals.get()), (256 * 6, 1)); |
| 106 | assert_eq!(got.prepare_calls, 256 * 6); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn shapes_come_from_the_model_config() { |
| 111 | let c = qwen3_4b(); |
| 112 | let s = serving_matmul_shapes(&c).unwrap(); |
| 113 | assert_eq!( |
| 114 | s, |
| 115 | vec![ |
| 116 | (2560, 1024), |
| 117 | (2560, 4096), |
| 118 | (2560, 9728), |
| 119 | (2560, 151936), |
| 120 | (4096, 2560), |
| 121 | (9728, 2560) |
| 122 | ] |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn context_is_the_smaller_of_the_kv_plan_and_the_attention_limit() { |
| 128 | let mut seen = Vec::new(); |
| 129 | for ctx in [2048usize, 262144] { |
| 130 | let ops = Ops::default(); |
| 131 | let mut l = daemon_limits(); |
| 132 | l.context_tokens = ctx; |
| 133 | reserve_gb10_serving_with(&qwen3_4b(), &l, true, true, &ops).unwrap(); |
| 134 | seen.push(ops.reserved.borrow()[0].max_context); |
| 135 | } |
| 136 | assert_eq!(seen, vec![2048, 4096]); |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn a_refusal_stops_serving_by_name_with_no_fallback() { |
| 141 | let ops = Ops { |
| 142 | refuse_reserve: true, |
| 143 | ..Ops::default() |
| 144 | }; |
| 145 | let err = reserve_gb10_serving_with(&qwen3_4b(), &daemon_limits(), true, true, &ops) |
| 146 | .expect_err("a refused reservation must refuse to serve"); |
| 147 | assert!(err.contains("GB10_SERVING_RESERVATION refused"), "{err}"); |
| 148 | assert!(err.contains("CHIP_FAIL"), "names omega's reason: {err}"); |
| 149 | assert!(err.contains("sovereign-core#277"), "{err}"); |
| 150 | assert!(err.contains("no on-demand fallback"), "{err}"); |
| 151 | assert_eq!((ops.prepares.get(), ops.seals.get()), (0, 0)); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn nothing_changes_off_the_enabled_qwen3_gb10_path() { |
| 156 | let none = |native: bool, opted: bool, cfg: &aien_inference_abi::ModelConfig| { |
| 157 | let ops = Ops::default(); |
| 158 | let r = reserve_gb10_serving_with(cfg, &daemon_limits(), native, opted, &ops).unwrap(); |
| 159 | assert!(r.is_none()); |
| 160 | assert!( |
| 161 | ops.reserved.borrow().is_empty() && ops.prepares.get() == 0 && ops.seals.get() == 0 |
| 162 | ); |
| 163 | }; |
| 164 | none(false, true, &qwen3_4b()); // not native (CPU daemon) |
| 165 | none(true, false, &qwen3_4b()); // switched off (=0): the refusal still applies |
| 166 | let mut llama = qwen3_4b(); |
| 167 | llama.qk_norm = false; // a non-Qwen3 model on the GB10 is untouched |
| 168 | none(true, true, &llama); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn the_alloc_probe_logs_the_baseline_and_shows_any_nonzero_delta() { |
| 173 | use aien_inference_abi::gb10_serving::{AllocProbe, ServingProbe}; |
| 174 | use aien_omega_gpu::AllocStats; |
| 175 | use std::sync::{Arc, Mutex}; |
| 176 | let now = Arc::new(Mutex::new(AllocStats { |
| 177 | allocs: 600, |
| 178 | alloc_bytes: 7_000_000_000, |
| 179 | alloc_failures: 0, |
| 180 | frees: 12, |
| 181 | stale_refusals: 0, |
| 182 | })); |
| 183 | let n2 = now.clone(); |
| 184 | let p = AllocProbe::new(Box::new(move || Some(*n2.lock().unwrap()))); |
| 185 | assert_eq!( |
| 186 | p.after_request(), |
| 187 | None, |
| 188 | "no line before the warm-up baseline" |
| 189 | ); |
| 190 | assert_eq!( |
| 191 | p.after_warm_up().unwrap(), |
| 192 | "GB10_SERVING_ALLOC after_warmup allocs=600 bytes=7000000000 failures=0 frees=12" |
| 193 | ); |
| 194 | let quiet = p.after_request().unwrap(); |
| 195 | assert!( |
| 196 | quiet.contains("allocs_since_warmup=0") && !quiet.contains("NONZERO"), |
| 197 | "{quiet}" |
| 198 | ); |
| 199 | // frees alone (teardown) are flagged under their own name, never as an allocation |
| 200 | now.lock().unwrap().frees += 33; |
| 201 | let freed = p.after_request().unwrap(); |
| 202 | assert!( |
| 203 | freed.contains("frees_since_warmup=33") |
| 204 | && freed.contains("FREES:") |
| 205 | && !freed.contains("NONZERO"), |
| 206 | "{freed}" |
| 207 | ); |
| 208 | now.lock().unwrap().allocs += 3; |
| 209 | now.lock().unwrap().alloc_bytes += 12288; |
| 210 | now.lock().unwrap().alloc_failures += 1; |
| 211 | let loud = p.after_request().unwrap(); |
| 212 | assert!(loud.contains("allocs_since_warmup=3"), "{loud}"); |
| 213 | assert!(loud.contains("bytes_since_warmup=12288"), "{loud}"); |
| 214 | assert!(loud.contains("failures_since_warmup=1"), "{loud}"); |
| 215 | assert!( |
| 216 | loud.contains("NONZERO"), |
| 217 | "a nonzero delta must be visible: {loud}" |
| 218 | ); |
| 219 | // the stub build has no counters: the probe stays silent rather than inventing zeros |
| 220 | let none = AllocProbe::new(Box::new(|| None)); |
| 221 | assert_eq!((none.after_warm_up(), none.after_request()), (None, None)); |
| 222 | } |