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-scheduler / tests / kv_incremental_admission_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 | //! GB10-2: the KV manager is the sole authority for how many NEW physical |
| 17 | //! blocks a sequence needs, and the scheduler admits by that number. |
| 18 | //! |
| 19 | //! On main (c75c583) `build_scheduled_batch` step 3 computes |
| 20 | //! `required_blocks = prompt_len.div_ceil(16)` (a literal 16, not the pool's |
| 21 | //! block size) and breaks when the free pool is below it, BEFORE it looks at |
| 22 | //! the sequence's block table. A fork child that already shares its parent's |
| 23 | //! prefix blocks is therefore refused unless the pool could hold the WHOLE |
| 24 | //! prompt again, and a pool with a block size other than 16 is mis-counted. |
| 25 | //! These tests use only APIs that exist on main, so they run (and FAIL) there. |
| 26 | |
| 27 | use aien_inference_abi::{SamplingParams, SequenceRequest, KV_POOL_EXHAUSTED_PREFIX}; |
| 28 | use aien_kv_cache::create_shared_kv_manager; |
| 29 | use aien_scheduler::{AienScheduler, SchedulerConfig, SequenceId}; |
| 30 | |
| 31 | fn config() -> SchedulerConfig { |
| 32 | SchedulerConfig { |
| 33 | max_batch_size: 16, |
| 34 | max_batch_tokens: 4096, |
| 35 | max_prefill_tokens: 4096, |
| 36 | prefill_chunk_size: 4096, |
| 37 | chunk_prefill: true, |
| 38 | watermark_blocks: 1, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn request(id: u64, prompt: &[u32]) -> SequenceRequest { |
| 43 | SequenceRequest { |
| 44 | request_id: id, |
| 45 | prompt_tokens: prompt.to_vec(), |
| 46 | sampling_params: SamplingParams { |
| 47 | temperature: 0.0, |
| 48 | top_p: 1.0, |
| 49 | max_tokens: 4, |
| 50 | stop_token_ids: vec![], |
| 51 | }, |
| 52 | arrival_time_ns: 0, |
| 53 | priority: 1, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// Parent with a computed 40-token prefix (3 blocks of 16, last one partial), |
| 58 | /// forked to a child that shares all three blocks. |
| 59 | fn parent_and_forked_child( |
| 60 | total_blocks: usize, |
| 61 | block_size: usize, |
| 62 | ) -> (aien_kv_cache::SharedKvManager, AienScheduler, u64, Vec<u32>) { |
| 63 | let kv = create_shared_kv_manager(total_blocks, block_size); |
| 64 | let scheduler = AienScheduler::new(config(), kv.clone()); |
| 65 | let parent = SequenceId::new(2, 1).unwrap().to_u64(); |
| 66 | let child = SequenceId::new(3, 1).unwrap().to_u64(); |
| 67 | let prompt: Vec<u32> = (10..50).collect(); |
| 68 | { |
| 69 | let mut w = kv.write(); |
| 70 | w.allocate_sequence(parent, &prompt).expect("allocate"); |
| 71 | w.mark_prefill_pending(parent).expect("pending"); |
| 72 | w.complete_prefill(parent).expect("fence"); |
| 73 | w.fork_prefilled(parent, child).expect("fork"); |
| 74 | } |
| 75 | (kv, scheduler, child, prompt) |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn fork_child_is_admitted_on_incremental_blocks_only() { |
| 80 | // 5-block pool. Parent holds 3, child shares them: 2 blocks are free. |
| 81 | // The child's full prompt is 3 blocks (> 2 free) but it only needs ONE new |
| 82 | // block (copy-on-write of the shared partial tail on its first decode). |
| 83 | let (kv, mut scheduler, child, prompt) = parent_and_forked_child(5, 16); |
| 84 | assert_eq!(kv.read().available_blocks(), 2, "setup: 2 free blocks"); |
| 85 | scheduler.submit_request(request(child, &prompt)); |
| 86 | |
| 87 | let batch = scheduler.build_scheduled_batch().expect("schedule"); |
| 88 | let admitted = batch |
| 89 | .as_ref() |
| 90 | .is_some_and(|b| b.decode_requests.contains(&child)); |
| 91 | assert!( |
| 92 | admitted, |
| 93 | "KV_ADMISSION_TRAP: forked child {child} shares its parent's 3 prefix blocks and needs \ |
| 94 | 1 new block, the pool has 2 free, but admission demanded a whole prompt's blocks (3); \ |
| 95 | batch = {batch:?}" |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn admission_uses_the_pool_block_size_not_a_literal_16() { |
| 101 | // Block size 32: a 40-token prompt needs 2 blocks and the pool has 2. |
| 102 | // Main counts 40.div_ceil(16) = 3 and refuses it. |
| 103 | let kv = create_shared_kv_manager(2, 32); |
| 104 | let mut scheduler = AienScheduler::new(config(), kv.clone()); |
| 105 | let prompt: Vec<u32> = (10..50).collect(); |
| 106 | let id = SequenceId::new(4, 1).unwrap().to_u64(); |
| 107 | scheduler.submit_request(request(id, &prompt)); |
| 108 | |
| 109 | let batch = scheduler.build_scheduled_batch().expect("schedule"); |
| 110 | let admitted = batch |
| 111 | .as_ref() |
| 112 | .is_some_and(|b| b.prefill_requests.iter().any(|r| r.request_id == id)); |
| 113 | assert!( |
| 114 | admitted, |
| 115 | "KV_ADMISSION_BLOCK_SIZE: 40 tokens at block size 32 need 2 blocks and 2 are free, but \ |
| 116 | admission used a hard-coded 16 (3 blocks); batch = {batch:?}" |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn exhausted_pool_with_nothing_running_fails_loud() { |
| 122 | // Negative control. 3-block pool, all held by the parent, child shares them. |
| 123 | // The child needs 1 new block (copy-on-write), 0 are free, nothing is running |
| 124 | // that could ever free one: the scheduler must say so, not wait forever. |
| 125 | let (kv, mut scheduler, child, prompt) = parent_and_forked_child(3, 16); |
| 126 | assert_eq!(kv.read().available_blocks(), 0, "setup: pool is full"); |
| 127 | scheduler.submit_request(request(child, &prompt)); |
| 128 | |
| 129 | let result = scheduler.build_scheduled_batch(); |
| 130 | let err = result.expect_err( |
| 131 | "KV_ADMISSION_SILENT_WAIT: pool cannot admit the child and nothing is running, yet \ |
| 132 | build_scheduled_batch returned Ok", |
| 133 | ); |
| 134 | assert!( |
| 135 | err.starts_with(KV_POOL_EXHAUSTED_PREFIX), |
| 136 | "error must start with the shared marker, got: {err}" |
| 137 | ); |
| 138 | assert!( |
| 139 | err.contains(&format!("sequence {child}")), |
| 140 | "error must name the sequence, got: {err}" |
| 141 | ); |
| 142 | assert!( |
| 143 | err.contains("needs 1 new KV blocks") && err.contains("only 0 are free"), |
| 144 | "error must say needed vs available, got: {err}" |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn shortfall_while_others_run_waits_without_error() { |
| 150 | // Control (must pass before and after): when a running sequence may still |
| 151 | // free blocks, a shortfall is ordinary queueing, not an error. |
| 152 | let kv = create_shared_kv_manager(3, 16); |
| 153 | let mut scheduler = AienScheduler::new(config(), kv.clone()); |
| 154 | let first: Vec<u32> = (10..58).collect(); // 48 tokens = 3 blocks |
| 155 | let second: Vec<u32> = (10..26).collect(); // 16 tokens = 1 block |
| 156 | let a = SequenceId::new(5, 1).unwrap().to_u64(); |
| 157 | let b = SequenceId::new(6, 1).unwrap().to_u64(); |
| 158 | scheduler.submit_request(request(a, &first)); |
| 159 | scheduler.submit_request(request(b, &second)); |
| 160 | |
| 161 | let batch = scheduler |
| 162 | .build_scheduled_batch() |
| 163 | .expect("a shortfall with a running sequence must not be an error") |
| 164 | .expect("first sequence is admitted"); |
| 165 | assert!(batch.prefill_requests.iter().any(|r| r.request_id == a)); |
| 166 | assert!(!batch.prefill_requests.iter().any(|r| r.request_id == b)); |
| 167 | assert_eq!(scheduler.waiting_count(), 1, "second request keeps waiting"); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn same_step_fork_admissions_reserve_their_copy_on_write_blocks() { |
| 172 | // 4-block pool: parent holds 3, ONE block free. Two children each need one |
| 173 | // new block on their first token. Admitting both in one step would promise |
| 174 | // the same free block twice; only the first may enter this batch. |
| 175 | let (kv, mut scheduler, first_child, prompt) = parent_and_forked_child(4, 16); |
| 176 | let second_child = SequenceId::new(7, 1).unwrap().to_u64(); |
| 177 | let parent = SequenceId::new(2, 1).unwrap().to_u64(); |
| 178 | kv.write() |
| 179 | .fork_prefilled(parent, second_child) |
| 180 | .expect("fork"); |
| 181 | assert_eq!(kv.read().available_blocks(), 1, "setup: 1 free block"); |
| 182 | scheduler.submit_request(request(first_child, &prompt)); |
| 183 | scheduler.submit_request(request(second_child, &prompt)); |
| 184 | |
| 185 | let batch = scheduler |
| 186 | .build_scheduled_batch() |
| 187 | .expect("second child waits, it is not an error: the first is scheduled") |
| 188 | .expect("first child admitted"); |
| 189 | assert_eq!(batch.decode_requests, vec![first_child]); |
| 190 | assert_eq!(scheduler.waiting_count(), 1, "second child keeps waiting"); |
| 191 | } |