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-runtime / tests / trace_overhead_benchmark.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 | //! Overhead of the swarm trace events (#398). Run by hand: |
| 17 | //! `cargo test -p aien-runtime --release --test trace_overhead_benchmark -- --ignored --nocapture` |
| 18 | //! Prints one line per run; asserts nothing about timing. |
| 19 | |
| 20 | use std::path::PathBuf; |
| 21 | use std::sync::{Arc, OnceLock}; |
| 22 | use std::time::{SystemTime, UNIX_EPOCH}; |
| 23 | |
| 24 | use aien_inference_abi::{ |
| 25 | ModelConfig, NativeTransformerBackend, ReferenceCpuBackend, TransformerWeights, |
| 26 | }; |
| 27 | use aien_runtime::shared_kv::{build_shared_kv_runtime, SharedKvSizing}; |
| 28 | use aien_runtime::spine::AienRuntimeSpine; |
| 29 | use aien_runtime::swarm::SwarmConfig; |
| 30 | use aien_scheduler::SchedulerConfig; |
| 31 | |
| 32 | const PROMPT: [u32; 6] = [5, 17, 42, 3, 88, 61]; |
| 33 | |
| 34 | fn model_config() -> ModelConfig { |
| 35 | ModelConfig { |
| 36 | model_id: "trace-reference".to_string(), |
| 37 | max_sequence_length: 256, |
| 38 | block_size: 4, |
| 39 | num_layers: 3, |
| 40 | num_heads: 4, |
| 41 | head_dim: 8, |
| 42 | num_kv_heads: 2, |
| 43 | hidden_dim: 32, |
| 44 | intermediate_dim: 64, |
| 45 | vocab_size: 97, |
| 46 | rms_norm_eps: 1e-5, |
| 47 | rope_theta: 10000.0, |
| 48 | rope_scaling: None, |
| 49 | tie_word_embeddings: false, |
| 50 | eos_token_ids: Vec::new(), |
| 51 | qk_norm: false, |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn scheduler_config() -> SchedulerConfig { |
| 56 | SchedulerConfig { |
| 57 | max_batch_size: 32, |
| 58 | max_batch_tokens: 2048, |
| 59 | max_prefill_tokens: 1024, |
| 60 | prefill_chunk_size: 128, |
| 61 | chunk_prefill: true, |
| 62 | watermark_blocks: 2, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn swarm_config(branches: usize, max_tokens: usize) -> SwarmConfig { |
| 67 | SwarmConfig { |
| 68 | model_handle: 1, |
| 69 | branch_count: branches, |
| 70 | max_active_sequences: branches, |
| 71 | max_tokens_per_branch: max_tokens, |
| 72 | root_world_id: 0, |
| 73 | priority: 1, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// One fresh controller state directory per test process. |
| 78 | fn fresh_state_dir() -> &'static PathBuf { |
| 79 | static DIR: OnceLock<PathBuf> = OnceLock::new(); |
| 80 | DIR.get_or_init(|| { |
| 81 | let nanos = SystemTime::now() |
| 82 | .duration_since(UNIX_EPOCH) |
| 83 | .expect("clock after epoch") |
| 84 | .as_nanos(); |
| 85 | let dir = |
| 86 | std::env::temp_dir().join(format!("aien-trace-test-{}-{}", std::process::id(), nanos)); |
| 87 | std::fs::create_dir_all(&dir).expect("create fresh state dir"); |
| 88 | std::env::set_var("AIEN_RUNTIME_STATE_DIR", &dir); |
| 89 | dir |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | fn build(branches: usize) -> (AienRuntimeSpine, NativeTransformerBackend) { |
| 94 | fresh_state_dir(); |
| 95 | let weights = TransformerWeights::reference_test_weights(&model_config()); |
| 96 | build_shared_kv_runtime( |
| 97 | weights, |
| 98 | Arc::new(ReferenceCpuBackend::new()), |
| 99 | scheduler_config(), |
| 100 | SharedKvSizing { |
| 101 | arena_capacity: branches + 16, |
| 102 | total_blocks: (branches + 16) * 4, |
| 103 | }, |
| 104 | ) |
| 105 | .expect("build shared KV runtime") |
| 106 | } |
| 107 | |
| 108 | async fn run_to_completion(spine: &mut AienRuntimeSpine, backend: &mut NativeTransformerBackend) { |
| 109 | let mut steps = 0; |
| 110 | while (spine.scheduler.running_count() > 0 || spine.scheduler.waiting_count() > 0) |
| 111 | && steps < 500 |
| 112 | { |
| 113 | steps += 1; |
| 114 | spine.step(backend).await.expect("spine step"); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | use aien_trace::{MemorySink, NullSink, TraceSink}; |
| 119 | |
| 120 | const BRANCHES: usize = 64; |
| 121 | |
| 122 | async fn run(kind: &str, sink: Arc<dyn TraceSink>) { |
| 123 | let (mut spine, mut backend) = build(BRANCHES); |
| 124 | spine.swarm_manager.set_trace_sink(sink); |
| 125 | let t = std::time::Instant::now(); |
| 126 | spine |
| 127 | .launch_swarm(swarm_config(BRANCHES, 3), &PROMPT) |
| 128 | .expect("launch"); |
| 129 | run_to_completion(&mut spine, &mut backend).await; |
| 130 | println!( |
| 131 | "TRACE_OVERHEAD kind={kind} branches={BRANCHES} elapsed_ms={:.3}", |
| 132 | t.elapsed().as_secs_f64() * 1000.0 |
| 133 | ); |
| 134 | assert_eq!( |
| 135 | spine.scheduler.metrics().finished_requests as usize, |
| 136 | BRANCHES |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | #[tokio::test] |
| 141 | #[ignore] |
| 142 | async fn trace_overhead_null_sink() { |
| 143 | run("null", Arc::new(NullSink)).await; |
| 144 | } |
| 145 | |
| 146 | #[tokio::test] |
| 147 | #[ignore] |
| 148 | async fn trace_overhead_memory_sink() { |
| 149 | run("memory", Arc::new(MemorySink::new())).await; |
| 150 | } |