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-heartbeat / src / bin / heartbeat_bench.rs
| 1 | //! Heartbeat H1 rhythm bench: full rotations of the expert ring from NVMe. |
| 2 | //! |
| 3 | //! heartbeat_bench --model DIR [--manifest PATH] [--slots N] [--workers W] |
| 4 | //! [--rotations R] [--hot-per-layer K] [--raw] [--buffered] |
| 5 | //! |
| 6 | //! --raw read + discard, no digest, no publish (raw device cost) |
| 7 | //! --buffered no O_DIRECT (page cache allowed; not a valid ceiling run) |
| 8 | //! Prints one JSON object per rotation, then a summary object. |
| 9 | |
| 10 | use std::collections::HashSet; |
| 11 | use std::path::PathBuf; |
| 12 | use std::time::{Duration, Instant}; |
| 13 | |
| 14 | use aien_heartbeat::{Manifest, Ring, Schedule, StreamConfig, Streamer}; |
| 15 | |
| 16 | fn arg<T: std::str::FromStr>(args: &[String], name: &str, default: T) -> T { |
| 17 | args.iter() |
| 18 | .position(|a| a == name) |
| 19 | .and_then(|i| args.get(i + 1)) |
| 20 | .map(|v| v.parse().unwrap_or_else(|_| panic!("bad value for {name}"))) |
| 21 | .unwrap_or(default) |
| 22 | } |
| 23 | |
| 24 | fn main() { |
| 25 | let args: Vec<String> = std::env::args().collect(); |
| 26 | let model: PathBuf = arg(&args, "--model", PathBuf::from(".")); |
| 27 | let manifest_path: PathBuf = arg( |
| 28 | &args, |
| 29 | "--manifest", |
| 30 | std::env::temp_dir().join("aien-heartbeat-manifest.json"), |
| 31 | ); |
| 32 | let slots: usize = arg(&args, "--slots", 64); |
| 33 | let workers: usize = arg(&args, "--workers", 8); |
| 34 | let rotations: usize = arg(&args, "--rotations", 3); |
| 35 | let hot_per_layer: u16 = arg(&args, "--hot-per-layer", 0); |
| 36 | let raw = args.iter().any(|a| a == "--raw"); |
| 37 | let direct = !args.iter().any(|a| a == "--buffered"); |
| 38 | |
| 39 | let m = match Manifest::load(&manifest_path) { |
| 40 | Ok(m) if m.model_dir == model => m, |
| 41 | _ => { |
| 42 | let t = Instant::now(); |
| 43 | let m = Manifest::build(&model).unwrap_or_else(|e| panic!("manifest: {e}")); |
| 44 | m.save(&manifest_path) |
| 45 | .unwrap_or_else(|e| panic!("save manifest: {e}")); |
| 46 | eprintln!( |
| 47 | "built manifest: {} experts, {} bytes, {:.1}s -> {}", |
| 48 | m.experts.len(), |
| 49 | m.total_bytes(), |
| 50 | t.elapsed().as_secs_f64(), |
| 51 | manifest_path.display() |
| 52 | ); |
| 53 | m |
| 54 | } |
| 55 | }; |
| 56 | let hot: HashSet<(u16, u16)> = (0..m.num_layers) |
| 57 | .flat_map(|l| (0..hot_per_layer).map(move |e| (l, e))) |
| 58 | .collect(); |
| 59 | let sched = Schedule::layer_order(&m, &hot); |
| 60 | let ring = Ring::new(slots, Streamer::slot_bytes(&m)); |
| 61 | let cfg = StreamConfig { |
| 62 | slots, |
| 63 | workers, |
| 64 | direct, |
| 65 | publish: !raw, |
| 66 | lease_wait: Duration::from_secs(60), |
| 67 | }; |
| 68 | let s = Streamer::open(&m.shard_paths(), cfg).unwrap_or_else(|e| panic!("open: {e}")); |
| 69 | |
| 70 | let mut walls = Vec::new(); |
| 71 | let (mut pay, mut dev, mut dig, mut rd) = (0u64, 0u64, Duration::ZERO, Duration::ZERO); |
| 72 | for r in 0..rotations { |
| 73 | let st = s |
| 74 | .rotate(&ring, &m, &sched) |
| 75 | .unwrap_or_else(|e| panic!("rotation {r}: {e}")); |
| 76 | println!( |
| 77 | "{}", |
| 78 | serde_json::json!({ |
| 79 | "rotation": r, "experts": st.experts, "payload_bytes": st.payload_bytes, |
| 80 | "device_bytes": st.device_bytes, "wall_s": st.wall.as_secs_f64(), |
| 81 | "payload_gbps": st.payload_gbps(), "device_gbps": st.device_gbps(), |
| 82 | "read_worker_s": st.read_time.as_secs_f64(), |
| 83 | "digest_worker_s": st.digest_time.as_secs_f64(), |
| 84 | "lease_wait_s": st.lease_wait_time.as_secs_f64(), |
| 85 | }) |
| 86 | ); |
| 87 | walls.push(st.wall.as_secs_f64()); |
| 88 | pay += st.payload_bytes; |
| 89 | dev += st.device_bytes; |
| 90 | dig += st.digest_time; |
| 91 | rd += st.read_time; |
| 92 | } |
| 93 | let total: f64 = walls.iter().sum(); |
| 94 | let min = walls.iter().cloned().fold(f64::INFINITY, f64::min); |
| 95 | let max = walls.iter().cloned().fold(0.0, f64::max); |
| 96 | println!( |
| 97 | "{}", |
| 98 | serde_json::json!({ |
| 99 | "summary": true, "mode": if raw { "raw" } else { "verify+publish" }, "direct": direct, |
| 100 | "slots": slots, "workers": workers, "rotations": rotations, |
| 101 | "hot_per_layer": hot_per_layer, "experts_per_rotation": sched.items.len(), |
| 102 | "payload_bytes_total": pay, "device_bytes_total": dev, "wall_s_total": total, |
| 103 | "beat_s_min": min, "beat_s_max": max, "beat_s_mean": total / rotations as f64, |
| 104 | "payload_gbps": pay as f64 / total / 1e9, "device_gbps": dev as f64 / total / 1e9, |
| 105 | "read_worker_s_total": rd.as_secs_f64(), "digest_worker_s_total": dig.as_secs_f64(), |
| 106 | }) |
| 107 | ); |
| 108 | } |