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-kv-bench / tests / bench_persistent_kv_test.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 | //! C9 (sc#485, ADR 0003 sections 10, 15.1, 16.5; CUTS.md C9): the persistent |
| 17 | //! KV benchmark's output contract. Host only: no GPU, no store I/O here (the |
| 18 | //! binary is only run with arguments it must refuse before doing any work). |
| 19 | |
| 20 | use aien_kv_bench::report::{ |
| 21 | build_report, required_not_run_rows, summarize, BenchError, Machine, ReportMeta, Row, |
| 22 | RowStatus, Workload, |
| 23 | }; |
| 24 | use std::process::Command; |
| 25 | |
| 26 | fn ok_row(mode: &str, cache_state: &str, latency: &[u64]) -> Row { |
| 27 | let mut row = Row::new_ok(mode, "hosted", cache_state, 2, 512, "second_turn"); |
| 28 | row.queue_depth = Some(4); |
| 29 | row.completed_requests = Some(latency.len() as u64); |
| 30 | row.failed_requests = Some(0); |
| 31 | row.latency_samples_us = latency.to_vec(); |
| 32 | row.ttft_samples_us = latency.iter().map(|l| l / 2).collect(); |
| 33 | row.decode_tokens_per_s = Some(100.0); |
| 34 | row.peak_allocated_bytes = Some(1 << 20); |
| 35 | row.peak_holder_bytes = Some(4096); |
| 36 | row.peak_staging_bytes = Some(3072); |
| 37 | row.peak_rss_bytes = Some(8 << 20); |
| 38 | row.restored_tokens = Some(0); |
| 39 | row.fill_percentiles(); |
| 40 | row |
| 41 | } |
| 42 | |
| 43 | fn meta() -> ReportMeta { |
| 44 | ReportMeta { |
| 45 | measurement_id: "BENCH-PKV-HOST-TEST".into(), |
| 46 | git_sha: "0".repeat(40), |
| 47 | git_dirty: false, |
| 48 | command: "bench_persistent_kv --modes cold-prefill".into(), |
| 49 | started_utc: "2026-10-13T00:00:00Z".into(), |
| 50 | platform: "hosted".into(), |
| 51 | machine: Machine::default(), |
| 52 | backend: "ReferenceCpuBackend".into(), |
| 53 | model: "test".into(), |
| 54 | workload: Workload::default(), |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// B6: a row that reports numbers must carry the completed-request count, |
| 59 | /// request latency p50/p95, TTFT p50/p95 and every peak; a row missing any of |
| 60 | /// them is refused by name, and an accepted report serializes all of them. |
| 61 | #[test] |
| 62 | fn bench_output_requires_completed_requests_latency_percentiles_and_peaks() { |
| 63 | let good = ok_row("cold-prefill", "none", &[100, 200, 300, 400]); |
| 64 | good.check().expect("a complete row is accepted"); |
| 65 | |
| 66 | type Strip = fn(&mut Row); |
| 67 | let cases: [(&str, Strip); 7] = [ |
| 68 | ("completed_requests", |r| r.completed_requests = None), |
| 69 | ("p50_us", |r| r.p50_us = None), |
| 70 | ("p95_us", |r| r.p95_us = None), |
| 71 | ("ttft_p95_us", |r| r.ttft_p95_us = None), |
| 72 | ("peak_allocated_bytes", |r| r.peak_allocated_bytes = None), |
| 73 | ("peak_staging_bytes", |r| r.peak_staging_bytes = None), |
| 74 | ("peak_rss_bytes", |r| r.peak_rss_bytes = None), |
| 75 | ]; |
| 76 | for (field, strip) in cases { |
| 77 | let mut row = good.clone(); |
| 78 | strip(&mut row); |
| 79 | match row.check() { |
| 80 | Err(BenchError::MissingField { field: f, .. }) => assert_eq!(f, field), |
| 81 | other => panic!("row without {field} must be refused, got {other:?}"), |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Zero completed requests is not a measurement. |
| 86 | let mut empty = good.clone(); |
| 87 | empty.completed_requests = Some(0); |
| 88 | assert!(empty.check().is_err(), "zero completed requests refused"); |
| 89 | |
| 90 | // Percentiles must come from the samples (nearest rank) and be ordered. |
| 91 | assert_eq!(good.p50_us, Some(200)); |
| 92 | assert_eq!(good.p95_us, Some(400)); |
| 93 | let mut swapped = good.clone(); |
| 94 | swapped.p50_us = Some(500); |
| 95 | assert!(swapped.check().is_err(), "p50 above p95 refused"); |
| 96 | |
| 97 | let report = build_report(meta(), vec![good]).expect("report"); |
| 98 | let json = serde_json::to_value(&report).expect("json"); |
| 99 | let row = json["rows"] |
| 100 | .as_array() |
| 101 | .expect("rows") |
| 102 | .iter() |
| 103 | .find(|r| r["mode"] == "cold-prefill") |
| 104 | .expect("measured row present"); |
| 105 | for key in [ |
| 106 | "completed_requests", |
| 107 | "failed_requests", |
| 108 | "p50_us", |
| 109 | "p95_us", |
| 110 | "ttft_p50_us", |
| 111 | "ttft_p95_us", |
| 112 | "decode_tokens_per_s", |
| 113 | "peak_allocated_bytes", |
| 114 | "peak_staging_bytes", |
| 115 | "peak_rss_bytes", |
| 116 | "cache_state", |
| 117 | "platform", |
| 118 | "queue_depth", |
| 119 | ] { |
| 120 | assert!(!row[key].is_null(), "serialized row carries {key}: {row:#}"); |
| 121 | } |
| 122 | assert!( |
| 123 | json["samples_sha256"] |
| 124 | .as_str() |
| 125 | .is_some_and(|s| s.len() == 64), |
| 126 | "raw samples are digested" |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /// B7: no row is reported without its mode and cache_state labels, and the |
| 131 | /// binary refuses to start without an explicit mode list or with an unknown |
| 132 | /// mode label (it never picks a default mode for the operator). |
| 133 | #[test] |
| 134 | fn bench_binary_refuses_to_report_without_mode_and_cache_state_labels() { |
| 135 | let mut no_cache = ok_row("cold-prefill", "none", &[10, 20]); |
| 136 | no_cache.cache_state = None; |
| 137 | assert!( |
| 138 | matches!(no_cache.check(), Err(BenchError::MissingLabel { .. })), |
| 139 | "row without cache_state refused" |
| 140 | ); |
| 141 | let mut no_mode = ok_row("cold-prefill", "none", &[10, 20]); |
| 142 | no_mode.mode = String::new(); |
| 143 | assert!( |
| 144 | matches!(no_mode.check(), Err(BenchError::MissingLabel { .. })), |
| 145 | "row without mode refused" |
| 146 | ); |
| 147 | let mut bad_cache = ok_row("cold-prefill", "none", &[10, 20]); |
| 148 | bad_cache.cache_state = Some("warm".into()); |
| 149 | assert!(bad_cache.check().is_err(), "unknown cache_state refused"); |
| 150 | assert!( |
| 151 | build_report(meta(), vec![no_cache]).is_err(), |
| 152 | "report with an unlabelled row refused" |
| 153 | ); |
| 154 | |
| 155 | let bin = env!("CARGO_BIN_EXE_bench_persistent_kv"); |
| 156 | let dir = std::env::temp_dir().join(format!("pkv-c9-b7-{}", std::process::id())); |
| 157 | let _ = std::fs::remove_dir_all(&dir); |
| 158 | std::fs::create_dir_all(&dir).expect("temp dir"); |
| 159 | let out = dir.join("out.json"); |
| 160 | |
| 161 | let no_modes = Command::new(bin) |
| 162 | .args(["--out", out.to_str().unwrap()]) |
| 163 | .output() |
| 164 | .expect("run binary"); |
| 165 | assert!(!no_modes.status.success(), "no --modes: must refuse"); |
| 166 | let err = String::from_utf8_lossy(&no_modes.stderr); |
| 167 | assert!(err.contains("--modes"), "refusal names --modes: {err}"); |
| 168 | |
| 169 | let bad_mode = Command::new(bin) |
| 170 | .args(["--modes", "warm-ish", "--out", out.to_str().unwrap()]) |
| 171 | .output() |
| 172 | .expect("run binary"); |
| 173 | assert!(!bad_mode.status.success(), "unknown mode: must refuse"); |
| 174 | let err = String::from_utf8_lossy(&bad_mode.stderr); |
| 175 | assert!(err.contains("warm-ish"), "refusal names the label: {err}"); |
| 176 | |
| 177 | assert!(!out.exists(), "a refused run writes no report"); |
| 178 | let _ = std::fs::remove_dir_all(&dir); |
| 179 | } |
| 180 | |
| 181 | /// ADR 15.1: page-cache hits and device reads are never averaged together. |
| 182 | #[test] |
| 183 | fn bench_refuses_mixed_cache_state_summary() { |
| 184 | let page = ok_row("restore-from-page-cache", "page_cache", &[100, 110, 120]); |
| 185 | let page2 = ok_row("restore-from-page-cache", "page_cache", &[130, 140]); |
| 186 | let device = ok_row("restore-from-device", "device", &[900, 950]); |
| 187 | |
| 188 | let s = summarize(&[&page, &page2]).expect("same cache_state summarizes"); |
| 189 | assert_eq!(s.cache_state, "page_cache"); |
| 190 | assert_eq!(s.samples, 5); |
| 191 | assert_eq!(s.p50_us, 120); |
| 192 | assert_eq!(s.p95_us, 140); |
| 193 | |
| 194 | match summarize(&[&page, &device]) { |
| 195 | Err(BenchError::MixedCacheState { .. }) => {} |
| 196 | other => panic!("mixed page_cache/device summary must be refused, got {other:?}"), |
| 197 | } |
| 198 | let mut unlabelled = page.clone(); |
| 199 | unlabelled.cache_state = None; |
| 200 | assert!( |
| 201 | summarize(&[&page, &unlabelled]).is_err(), |
| 202 | "an unlabelled row cannot join a summary" |
| 203 | ); |
| 204 | let not_run = Row::not_run("restore-from-device", "hosted", "device", "no root"); |
| 205 | assert!( |
| 206 | summarize(&[¬_run]).is_err(), |
| 207 | "a NOT_RUN row has nothing to summarize" |
| 208 | ); |
| 209 | } |
| 210 | |
| 211 | /// B8: every report carries the native physical rows as NOT_RUN with a |
| 212 | /// reason, never as numbers, and a report that drops them is refused. |
| 213 | #[test] |
| 214 | fn bench_records_not_run_rows_for_native_physical() { |
| 215 | let required = required_not_run_rows(); |
| 216 | let platforms: Vec<&str> = required.iter().map(|r| r.platform.as_str()).collect(); |
| 217 | assert!(platforms.contains(&"native_physical"), "Spark native row"); |
| 218 | assert!( |
| 219 | platforms.contains(&"native physical (Pi 5)"), |
| 220 | "Pi 5 physical row" |
| 221 | ); |
| 222 | for row in &required { |
| 223 | assert_eq!(row.status, RowStatus::NotRun); |
| 224 | assert!(row.reason.as_deref().is_some_and(|r| !r.is_empty())); |
| 225 | row.check().expect("a NOT_RUN row with a reason is valid"); |
| 226 | } |
| 227 | |
| 228 | let report = |
| 229 | build_report(meta(), vec![ok_row("cold-prefill", "none", &[5, 6])]).expect("report"); |
| 230 | for want in &required { |
| 231 | let found = report |
| 232 | .rows |
| 233 | .iter() |
| 234 | .find(|r| r.platform == want.platform && r.mode == want.mode) |
| 235 | .unwrap_or_else(|| panic!("report records {} NOT_RUN", want.platform)); |
| 236 | assert_eq!(found.status, RowStatus::NotRun); |
| 237 | } |
| 238 | let json = serde_json::to_string(&report).expect("json"); |
| 239 | assert!(json.contains("\"NOT_RUN\""), "status serialized as NOT_RUN"); |
| 240 | |
| 241 | // A NOT_RUN row may not carry numbers, and needs a reason. |
| 242 | let mut faked = Row::not_run("native-physical", "native_physical", "device", "aienos#336"); |
| 243 | faked.p50_us = Some(1); |
| 244 | assert!(faked.check().is_err(), "NOT_RUN with numbers refused"); |
| 245 | let mut silent = Row::not_run("native-physical", "native_physical", "device", "x"); |
| 246 | silent.reason = None; |
| 247 | assert!(silent.check().is_err(), "NOT_RUN without a reason refused"); |
| 248 | |
| 249 | // A physical platform can never report measured numbers from this host run. |
| 250 | let mut claimed = ok_row("native-physical", "device", &[1, 2]); |
| 251 | claimed.platform = "native_physical".into(); |
| 252 | assert!( |
| 253 | build_report(meta(), vec![claimed]).is_err(), |
| 254 | "a measured native physical row from a host run is refused" |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | /// The measuring path end to end on a small row (not a CUTS red test): the |
| 259 | /// restore row really restores through the host store and the daemon wiring, |
| 260 | /// the cold row restores nothing, and both rows pass the contract. |
| 261 | #[tokio::test] |
| 262 | async fn small_rows_measure_through_the_runtime_and_restore_from_the_host_store() { |
| 263 | use aien_kv_bench::run::{run_row, Mode, RunSpec}; |
| 264 | let root = std::env::temp_dir().join(format!("pkv-c9-rows-{}", std::process::id())); |
| 265 | let _ = std::fs::remove_dir_all(&root); |
| 266 | let spec = RunSpec { |
| 267 | modes: vec![Mode::ColdPrefill, Mode::RestorePageCache], |
| 268 | concurrency: vec![2], |
| 269 | prompt_tokens: vec![80], |
| 270 | rounds: 2, |
| 271 | turn1_max_tokens: 4, |
| 272 | turn2_suffix_tokens: 8, |
| 273 | turn2_max_tokens: 4, |
| 274 | block_size: 16, |
| 275 | chunk_blocks: 2, |
| 276 | store_root: root.clone(), |
| 277 | }; |
| 278 | let restore = run_row(&spec, Mode::RestorePageCache, 2, 80) |
| 279 | .await |
| 280 | .expect("restore row"); |
| 281 | restore.check().expect("restore row valid"); |
| 282 | assert_eq!(restore.completed_requests, Some(4)); |
| 283 | assert_eq!(restore.cache_state.as_deref(), Some("page_cache")); |
| 284 | // Turn 1 computes 80 + 4 - 1 = 83 tokens: two full 32-token chunks per |
| 285 | // conversation, restored by each of the 4 measured turn-2 requests. |
| 286 | assert_eq!(restore.restored_tokens, Some(4 * 64)); |
| 287 | assert!(restore.peak_holder_bytes.unwrap() > 0, "park holders seen"); |
| 288 | assert!(restore.peak_staging_bytes.unwrap() > 0); |
| 289 | |
| 290 | let cold = run_row(&spec, Mode::ColdPrefill, 2, 80) |
| 291 | .await |
| 292 | .expect("cold row"); |
| 293 | cold.check().expect("cold row valid"); |
| 294 | assert_eq!(cold.restored_tokens, Some(0)); |
| 295 | assert_eq!(cold.queue_depth, Some(0)); |
| 296 | assert!( |
| 297 | run_row(&spec, Mode::RestoreDevice, 1, 80).await.is_err(), |
| 298 | "the device mode is never measured" |
| 299 | ); |
| 300 | let _ = std::fs::remove_dir_all(&root); |
| 301 | } |