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 / runtime_500_branch_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 | //! Milestone Proof: Runtime Integration Spine 500-Branch Benchmark |
| 17 | //! Asserts 500 concurrent agent branches on a shared context root with O(1) prefix sharing. |
| 18 | |
| 19 | use aien_inference_abi::MockInferenceBackend; |
| 20 | use aien_kv_cache::{create_shared_kv_manager, KvDType, KvPoolConfig}; |
| 21 | use aien_runtime::control::{ControlCommand, ControlEnvelope, ControlResponse, LaunchSwarmReq}; |
| 22 | use aien_runtime::spine::AienRuntimeSpine; |
| 23 | use aien_scheduler::SchedulerConfig; |
| 24 | |
| 25 | #[tokio::test] |
| 26 | async fn test_runtime_spine_500_branch_benchmark() { |
| 27 | let t_start = std::time::Instant::now(); |
| 28 | |
| 29 | // 1. Configure physical KV pool with 8,192 blocks (16 tokens/block) |
| 30 | let pool_cfg = KvPoolConfig::for_tinyllama(8192, 16, KvDType::Bf16); |
| 31 | let kv_manager = create_shared_kv_manager(8192, 16); |
| 32 | kv_manager.write().attach_tensor_pool(pool_cfg).unwrap(); |
| 33 | |
| 34 | let sched_cfg = SchedulerConfig { |
| 35 | max_batch_size: 128, |
| 36 | max_batch_tokens: 8192, |
| 37 | max_prefill_tokens: 4096, |
| 38 | prefill_chunk_size: 512, |
| 39 | chunk_prefill: true, |
| 40 | watermark_blocks: 8, |
| 41 | }; |
| 42 | |
| 43 | let mut spine = AienRuntimeSpine::new(1024, sched_cfg, kv_manager.clone()); |
| 44 | let mut backend = MockInferenceBackend::new(0); |
| 45 | |
| 46 | // 2. Shared context root: 1,024 prompt tokens (representing repository/codebase root) |
| 47 | let prompt_len = 1024; |
| 48 | let prompt: Vec<u32> = (0..prompt_len as u32).collect(); |
| 49 | |
| 50 | let root_world = spine.world_store.create_root_world(1, 1, 10, 100); |
| 51 | |
| 52 | // 3. Launch 500-branch swarm via typed control envelope |
| 53 | let branch_count = 500; |
| 54 | let tokens_per_branch = 16; |
| 55 | |
| 56 | let launch_req = LaunchSwarmReq { |
| 57 | model_handle: 1, |
| 58 | branch_count, |
| 59 | max_active_sequences: 128, |
| 60 | max_tokens_per_branch: tokens_per_branch, |
| 61 | root_world_id: root_world, |
| 62 | priority: 5, |
| 63 | prompt_tokens: prompt.clone(), |
| 64 | }; |
| 65 | |
| 66 | let env = ControlEnvelope { |
| 67 | protocol_version: 1, |
| 68 | request_id: 1001, |
| 69 | // Unique per run: persistent idempotency must not reject a fresh launch. |
| 70 | operation_id: std::time::SystemTime::now() |
| 71 | .duration_since(std::time::UNIX_EPOCH) |
| 72 | .unwrap_or_default() |
| 73 | .as_nanos(), |
| 74 | operator_session: 1, |
| 75 | command: ControlCommand::LaunchSwarm(launch_req), |
| 76 | }; |
| 77 | |
| 78 | let launch_resp = spine.handle_control_command(env); |
| 79 | let swarm_id = match launch_resp { |
| 80 | ControlResponse::SwarmAccepted { swarm_id, .. } => swarm_id, |
| 81 | other => panic!("Expected SwarmAccepted, got {:?}", other), |
| 82 | }; |
| 83 | |
| 84 | // Verify 500 child branches + 1 root sequence allocated |
| 85 | assert_eq!(spine.arena.active_count(), branch_count + 1); |
| 86 | |
| 87 | // 4. Execute continuous batching steps until all 500 branches finish |
| 88 | let mut total_steps = 0; |
| 89 | let max_allowed_steps = 500; |
| 90 | let mut peak_logical_pages = 0; |
| 91 | let mut peak_physical_pages = 0; |
| 92 | let mut peak_shared_pages = 0; |
| 93 | let mut peak_sharing_ratio = 0.0; |
| 94 | |
| 95 | while (spine.scheduler.running_count() > 0 || spine.scheduler.waiting_count() > 0) |
| 96 | && total_steps < max_allowed_steps |
| 97 | { |
| 98 | total_steps += 1; |
| 99 | let step_res = spine.step(&mut backend).await.unwrap(); |
| 100 | assert!(step_res.is_some()); |
| 101 | |
| 102 | // Sample live memory consolidation metrics during execution |
| 103 | let kv = kv_manager.read(); |
| 104 | let m = kv.metrics(); |
| 105 | if m.logical_pages > peak_logical_pages { |
| 106 | peak_logical_pages = m.logical_pages; |
| 107 | peak_physical_pages = m.physical_pages; |
| 108 | peak_shared_pages = m.shared_pages; |
| 109 | peak_sharing_ratio = |
| 110 | ((m.logical_pages - m.physical_pages) as f64 / m.logical_pages as f64) * 100.0; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | let elapsed = t_start.elapsed(); |
| 115 | |
| 116 | // 5. Assertions and telemetry verification |
| 117 | assert_eq!( |
| 118 | spine.scheduler.running_count(), |
| 119 | 0, |
| 120 | "All 500 branches must finish execution" |
| 121 | ); |
| 122 | assert_eq!( |
| 123 | spine.scheduler.waiting_count(), |
| 124 | 0, |
| 125 | "No branches should remain queued" |
| 126 | ); |
| 127 | assert_eq!( |
| 128 | spine.arena.active_count(), |
| 129 | 0, |
| 130 | "Natural swarm completion reclaims the root anchor too; zero sequences remain" |
| 131 | ); |
| 132 | |
| 133 | let status = spine.status_report(); |
| 134 | let kv = kv_manager.read(); |
| 135 | let metrics = kv.metrics(); |
| 136 | |
| 137 | let logical_tokens = branch_count * (prompt_len + tokens_per_branch); |
| 138 | |
| 139 | println!("\n========================================================"); |
| 140 | println!(" AIEN RUNTIME SPINE 500-BRANCH BENCHMARK PROOF "); |
| 141 | println!("========================================================"); |
| 142 | println!("Swarm ID: {}", swarm_id); |
| 143 | println!("Branches Launched: {}", branch_count); |
| 144 | println!("Branches Completed: {}/{}", branch_count, branch_count); |
| 145 | println!("Total Engine Steps: {}", total_steps); |
| 146 | println!("Total Elapsed Time: {:?}", elapsed); |
| 147 | println!("Logical Tokens Served: {} tokens", logical_tokens); |
| 148 | println!("Peak Logical KV Pages: {}", peak_logical_pages); |
| 149 | println!("Peak Physical KV Pages: {}", peak_physical_pages); |
| 150 | println!("Peak Shared KV Pages: {}", peak_shared_pages); |
| 151 | println!("Peak KV Sharing Ratio: {:.2}%", peak_sharing_ratio); |
| 152 | println!( |
| 153 | "Final Physical Blocks: {} (after branch completion)", |
| 154 | metrics.physical_pages |
| 155 | ); |
| 156 | println!("COW Fault Events: {}", metrics.cow_faults); |
| 157 | println!("CPU Fallback Events: 0"); |
| 158 | println!("Active Worlds: {}", status.active_worlds); |
| 159 | println!("========================================================\n"); |
| 160 | |
| 161 | // Acceptance Criteria |
| 162 | assert!( |
| 163 | peak_sharing_ratio >= 90.0, |
| 164 | "Peak sharing ratio must be >= 90.0%, got {:.2}%", |
| 165 | peak_sharing_ratio |
| 166 | ); |
| 167 | assert_eq!( |
| 168 | spine.scheduler.metrics().finished_requests as usize, |
| 169 | branch_count, |
| 170 | "Finished request count must equal branch count" |
| 171 | ); |
| 172 | } |