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-cache / tests / holder_ownership_tests.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 | //! Holder ownership primitives (persistent-KV cut C3, ADR 0003 section 5.2). |
| 17 | //! |
| 18 | //! A holder is an ordinary block table whose id lives in the reserved slot |
| 19 | //! `HOLDER_SLOT` (`u32::MAX`, the high 32 bits of the u64 sequence id). A |
| 20 | //! storage job forks a sequence into a holder to keep its blocks alive after |
| 21 | //! the sequence frees them, and `adopt_table` moves a holder's table to a real |
| 22 | //! sequence id without touching reference counts. Every test here uses only the |
| 23 | //! existing refcount, copy-on-write and free-list paths plus the new primitives. |
| 24 | |
| 25 | use aien_kv_cache::{ |
| 26 | holder_id, is_holder_id, AienKvManager, HeapUnifiedBuffer, KvDType, KvPoolConfig, PrefillState, |
| 27 | SuffixPolicy, HOLDER_SLOT, |
| 28 | }; |
| 29 | |
| 30 | const BS: usize = 4; |
| 31 | const LAYERS: usize = 2; |
| 32 | const KV_HEADS: usize = 2; |
| 33 | const HEAD_DIM: usize = 2; |
| 34 | const KV_DIM: usize = KV_HEADS * HEAD_DIM; |
| 35 | |
| 36 | type Mgr = AienKvManager<HeapUnifiedBuffer>; |
| 37 | |
| 38 | fn new_mgr(total_blocks: usize) -> Mgr { |
| 39 | let cfg = KvPoolConfig { |
| 40 | num_blocks: total_blocks, |
| 41 | block_size: BS, |
| 42 | num_layers: LAYERS, |
| 43 | num_kv_heads: KV_HEADS, |
| 44 | head_dim: HEAD_DIM, |
| 45 | dtype: KvDType::Fp32, |
| 46 | }; |
| 47 | let buf = HeapUnifiedBuffer::allocate(cfg.total_bytes(), 64).expect("heap buffer"); |
| 48 | AienKvManager::with_buffer(total_blocks, BS, cfg, buf).expect("manager") |
| 49 | } |
| 50 | |
| 51 | fn kval(val: f32, layer: usize) -> f32 { |
| 52 | val + layer as f32 * 0.25 |
| 53 | } |
| 54 | |
| 55 | /// Raw read of every slot of every layer of `block`, whatever the tables say. |
| 56 | fn raw_block(m: &Mgr, block: usize) -> Vec<f32> { |
| 57 | let pool = m.tensor_pool().expect("pool attached"); |
| 58 | let mut out = Vec::new(); |
| 59 | for layer in 0..LAYERS { |
| 60 | for slot in 0..BS { |
| 61 | let mut k = [0.0f32; KV_DIM]; |
| 62 | let mut v = [0.0f32; KV_DIM]; |
| 63 | pool.read_token_kv(block, layer, slot, &mut k, &mut v); |
| 64 | out.extend_from_slice(&k); |
| 65 | out.extend_from_slice(&v); |
| 66 | } |
| 67 | } |
| 68 | out |
| 69 | } |
| 70 | |
| 71 | /// Allocate `seq`, write one value per token in every layer, fire the prefill fence. |
| 72 | fn admit(m: &mut Mgr, seq: u64, vals: &[f32]) { |
| 73 | let prompt: Vec<u32> = (0..vals.len() as u32).collect(); |
| 74 | m.allocate_sequence(seq, &prompt).expect("allocate"); |
| 75 | for (pos, &val) in vals.iter().enumerate() { |
| 76 | for layer in 0..LAYERS { |
| 77 | m.write_token_kv( |
| 78 | seq, |
| 79 | pos, |
| 80 | layer, |
| 81 | &[kval(val, layer); KV_DIM], |
| 82 | &[-kval(val, layer); KV_DIM], |
| 83 | ) |
| 84 | .expect("write"); |
| 85 | } |
| 86 | } |
| 87 | m.mark_prefill_pending(seq).expect("pending"); |
| 88 | m.complete_prefill(seq).expect("fence"); |
| 89 | } |
| 90 | |
| 91 | fn table_blocks(m: &Mgr, seq: u64) -> Vec<usize> { |
| 92 | m.get_block_table(seq).expect("table").block_ids.clone() |
| 93 | } |
| 94 | |
| 95 | fn ref_count(m: &Mgr, block: usize) -> usize { |
| 96 | m.get_block(block).expect("block").ref_count |
| 97 | } |
| 98 | |
| 99 | fn vals(n: usize, base: f32) -> Vec<f32> { |
| 100 | (0..n).map(|i| base + i as f32).collect() |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn holder_fork_keeps_blocks_after_owner_free() { |
| 105 | let total = 16; |
| 106 | let mut m = new_mgr(total); |
| 107 | admit(&mut m, 1, &vals(10, 1.0)); // 3 blocks, partial tail |
| 108 | let blocks = table_blocks(&m, 1); |
| 109 | assert_eq!(blocks.len(), 3); |
| 110 | let before: Vec<Vec<f32>> = blocks.iter().map(|&b| raw_block(&m, b)).collect(); |
| 111 | |
| 112 | let holder = holder_id(7).expect("holder id"); |
| 113 | m.fork_sequence(1, holder).expect("fork into holder"); |
| 114 | assert_eq!(m.holder_block_count(), 3); |
| 115 | |
| 116 | m.free_sequence(1).expect("owner free"); |
| 117 | |
| 118 | // The owner is gone, the holder keeps every block alive. |
| 119 | assert_eq!(m.free_block_count(), total - 3); |
| 120 | assert_eq!(m.holder_block_count(), 3); |
| 121 | assert_eq!(table_blocks(&m, holder), blocks); |
| 122 | for &b in &blocks { |
| 123 | assert_eq!(ref_count(&m, b), 1, "block {b} held by the holder only"); |
| 124 | } |
| 125 | // None of the held blocks is handed to a new sequence, even when the rest |
| 126 | // of the pool is drained. |
| 127 | let drain: Vec<u32> = (0..((total - 3) * BS) as u32).collect(); |
| 128 | let drained = m.allocate_sequence(2, &drain).expect("drain free pool"); |
| 129 | assert_eq!(m.free_block_count(), 0); |
| 130 | for b in &drained { |
| 131 | assert!(!blocks.contains(b), "held block {b} was reused"); |
| 132 | } |
| 133 | // Held bytes are untouched. |
| 134 | for (i, &b) in blocks.iter().enumerate() { |
| 135 | assert_eq!(raw_block(&m, b), before[i], "block {b} bytes changed"); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn holder_free_returns_blocks_only_at_zero_refcount() { |
| 141 | let total = 8; |
| 142 | let mut m = new_mgr(total); |
| 143 | admit(&mut m, 1, &vals(8, 1.0)); // 2 full blocks |
| 144 | let blocks = table_blocks(&m, 1); |
| 145 | let holder = holder_id(1).unwrap(); |
| 146 | m.fork_sequence(1, holder).unwrap(); |
| 147 | for &b in &blocks { |
| 148 | assert_eq!(ref_count(&m, b), 2); |
| 149 | } |
| 150 | |
| 151 | // Holder released first: the owner still has the blocks. |
| 152 | m.free_sequence(holder).expect("holder free"); |
| 153 | assert_eq!(m.holder_block_count(), 0); |
| 154 | assert_eq!(m.free_block_count(), total - 2); |
| 155 | for &b in &blocks { |
| 156 | assert_eq!(ref_count(&m, b), 1); |
| 157 | } |
| 158 | |
| 159 | // Park again, owner frees first, holder free is the last reference. |
| 160 | m.fork_sequence(1, holder).unwrap(); |
| 161 | m.free_sequence(1).unwrap(); |
| 162 | assert_eq!(m.free_block_count(), total - 2, "holder still holds them"); |
| 163 | m.free_sequence(holder).unwrap(); |
| 164 | assert_eq!(m.free_block_count(), total, "last reference returns blocks"); |
| 165 | for &b in &blocks { |
| 166 | assert_eq!(ref_count(&m, b), 0); |
| 167 | } |
| 168 | assert_eq!(m.holder_block_count(), 0); |
| 169 | assert_eq!(m.active_sequence_count(), 0); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn adopt_table_moves_table_and_keeps_refcounts() { |
| 174 | let total = 12; |
| 175 | let mut m = new_mgr(total); |
| 176 | // Restore shape: the holder allocates its own blocks, the load fills them, |
| 177 | // the fence fires, then the table is adopted by the request's sequence id. |
| 178 | let holder = holder_id(42).unwrap(); |
| 179 | let prompt: Vec<u32> = (0..10).collect(); |
| 180 | m.allocate_sequence(holder, &prompt).unwrap(); |
| 181 | m.mark_prefill_pending(holder).unwrap(); |
| 182 | m.complete_prefill(holder).unwrap(); |
| 183 | let blocks = table_blocks(&m, holder); |
| 184 | assert_eq!(blocks.len(), 3); |
| 185 | assert_eq!(m.holder_block_count(), 3); |
| 186 | |
| 187 | let free_before = m.free_block_count(); |
| 188 | let counts_before: Vec<usize> = blocks.iter().map(|&b| ref_count(&m, b)).collect(); |
| 189 | let active_before = m.debug_active_blocks(); |
| 190 | let seq_count_before = m.active_sequence_count(); |
| 191 | |
| 192 | let seq = 5u64; |
| 193 | m.adopt_table(holder, seq).expect("adopt"); |
| 194 | |
| 195 | assert!(m.get_block_table(holder).is_none(), "holder table is gone"); |
| 196 | let t = m.get_block_table(seq).expect("seq has the table"); |
| 197 | assert_eq!(t.sequence_id, seq); |
| 198 | assert_eq!(t.block_ids, blocks); |
| 199 | assert_eq!(t.total_tokens, 10); |
| 200 | assert_eq!(t.prefill_state, PrefillState::PrefillReady); |
| 201 | assert_eq!(m.free_block_count(), free_before); |
| 202 | assert_eq!(m.active_sequence_count(), seq_count_before); |
| 203 | assert_eq!(m.debug_active_blocks(), active_before); |
| 204 | let counts_after: Vec<usize> = blocks.iter().map(|&b| ref_count(&m, b)).collect(); |
| 205 | assert_eq!(counts_after, counts_before); |
| 206 | assert_eq!(m.holder_block_count(), 0, "no longer a holder block"); |
| 207 | |
| 208 | // The adopted sequence is a normal one: it frees to the pool. |
| 209 | m.free_sequence(seq).unwrap(); |
| 210 | assert_eq!(m.free_block_count(), total); |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn adopt_table_refusals_change_nothing() { |
| 215 | let mut m = new_mgr(12); |
| 216 | let holder = holder_id(1).unwrap(); |
| 217 | m.allocate_sequence(holder, &[0, 1, 2, 3, 4]).unwrap(); |
| 218 | admit(&mut m, 9, &vals(3, 1.0)); |
| 219 | let free = m.free_block_count(); |
| 220 | let active = m.debug_active_blocks(); |
| 221 | let holder_blocks = table_blocks(&m, holder); |
| 222 | let nine_blocks = table_blocks(&m, 9); |
| 223 | |
| 224 | // Unknown holder. |
| 225 | assert!(m.adopt_table(holder_id(2).unwrap(), 5).is_err()); |
| 226 | // Destination already has a table: not replaced. |
| 227 | assert!(m.adopt_table(holder, 9).is_err()); |
| 228 | // Source is not a holder id: a real sequence cannot be stolen this way. |
| 229 | assert!(m.adopt_table(9, 5).is_err()); |
| 230 | // Destination is a holder id. |
| 231 | assert!(m.adopt_table(holder, holder_id(3).unwrap()).is_err()); |
| 232 | // Self. |
| 233 | assert!(m.adopt_table(holder, holder).is_err()); |
| 234 | |
| 235 | assert_eq!(m.free_block_count(), free); |
| 236 | assert_eq!(m.debug_active_blocks(), active); |
| 237 | assert_eq!(table_blocks(&m, holder), holder_blocks); |
| 238 | assert_eq!(table_blocks(&m, 9), nine_blocks); |
| 239 | assert_eq!(m.get_block_table(9).unwrap().sequence_id, 9); |
| 240 | } |
| 241 | |
| 242 | #[test] |
| 243 | fn owner_append_on_shared_partial_block_copies_not_holder() { |
| 244 | let mut m = new_mgr(12); |
| 245 | admit(&mut m, 1, &vals(6, 1.0)); // block 0 full, block 1 holds 2 of 4 |
| 246 | let blocks = table_blocks(&m, 1); |
| 247 | assert_eq!(blocks.len(), 2); |
| 248 | let holder = holder_id(3).unwrap(); |
| 249 | m.fork_sequence(1, holder).unwrap(); |
| 250 | let held_bytes: Vec<Vec<f32>> = blocks.iter().map(|&b| raw_block(&m, b)).collect(); |
| 251 | let faults_before = m.cow_faults(); |
| 252 | |
| 253 | // The owner resumes decoding while the park is in flight. |
| 254 | let (new_block, slot) = m.append_token_with_slot(1).expect("append"); |
| 255 | for layer in 0..LAYERS { |
| 256 | m.write_explicit_token_kv(new_block, layer, slot, &[99.0; KV_DIM], &[-99.0; KV_DIM]) |
| 257 | .unwrap(); |
| 258 | } |
| 259 | |
| 260 | assert_eq!(m.cow_faults(), faults_before + 1, "exactly one COW fault"); |
| 261 | assert_ne!( |
| 262 | new_block, blocks[1], |
| 263 | "owner copied the shared partial block" |
| 264 | ); |
| 265 | let owner_blocks = table_blocks(&m, 1); |
| 266 | assert_eq!(owner_blocks[0], blocks[0], "full prefix block stays shared"); |
| 267 | assert_eq!(owner_blocks[1], new_block); |
| 268 | // The holder still points at the frozen originals, bytes unchanged. |
| 269 | assert_eq!(table_blocks(&m, holder), blocks); |
| 270 | assert_eq!(m.get_block_table(holder).unwrap().total_tokens, 6); |
| 271 | for (i, &b) in blocks.iter().enumerate() { |
| 272 | assert_eq!(raw_block(&m, b), held_bytes[i], "held block {b} changed"); |
| 273 | } |
| 274 | assert_eq!(ref_count(&m, blocks[0]), 2); |
| 275 | assert_eq!(ref_count(&m, blocks[1]), 1, "held only by the holder now"); |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn shared_prefix_sibling_valid_after_holder_free() { |
| 280 | let total = 16; |
| 281 | let mut m = new_mgr(total); |
| 282 | admit(&mut m, 1, &vals(8, 1.0)); // 2 full blocks |
| 283 | m.fork_prefilled(1, 2).expect("sibling shares the prefix"); |
| 284 | let prefix = table_blocks(&m, 2); |
| 285 | assert_eq!(prefix, table_blocks(&m, 1)); |
| 286 | let bytes: Vec<Vec<f32>> = prefix.iter().map(|&b| raw_block(&m, b)).collect(); |
| 287 | |
| 288 | // Sequence 1 parks (holder) and finishes; then the park is published and |
| 289 | // the holder released. |
| 290 | let holder = holder_id(11).unwrap(); |
| 291 | m.fork_sequence(1, holder).unwrap(); |
| 292 | m.free_sequence(1).unwrap(); |
| 293 | assert_eq!(m.holder_block_count(), 2); |
| 294 | m.free_sequence(holder).unwrap(); |
| 295 | assert_eq!(m.holder_block_count(), 0); |
| 296 | |
| 297 | // Sequence 2 still owns the prefix and can decode: it reads its blocks and |
| 298 | // appends a new one. |
| 299 | assert_eq!(table_blocks(&m, 2), prefix); |
| 300 | assert!(m.get_block_table(2).unwrap().is_prefill_ready()); |
| 301 | assert_eq!(m.free_block_count(), total - 2); |
| 302 | for (i, &b) in prefix.iter().enumerate() { |
| 303 | assert_eq!(ref_count(&m, b), 1); |
| 304 | assert_eq!(raw_block(&m, b), bytes[i], "prefix block {b} changed"); |
| 305 | } |
| 306 | let mut k = Vec::new(); |
| 307 | let mut v = Vec::new(); |
| 308 | m.gather_sequence_layer_kv(2, 0, &mut k, &mut v) |
| 309 | .expect("sibling gathers its prefix"); |
| 310 | assert_eq!(k.len(), 8 * KV_DIM); |
| 311 | assert_eq!(k[0], kval(1.0, 0)); |
| 312 | assert_eq!(k[7 * KV_DIM], kval(8.0, 0)); |
| 313 | let (b, slot) = m.append_token_with_slot(2).expect("sibling appends"); |
| 314 | assert!(!prefix.contains(&b)); |
| 315 | assert_eq!(slot, 0); |
| 316 | |
| 317 | m.free_sequence(2).unwrap(); |
| 318 | assert_eq!(m.free_block_count(), total); |
| 319 | } |
| 320 | |
| 321 | #[test] |
| 322 | fn holder_id_never_collides_with_arena_ids() { |
| 323 | // Arena ids are `slot << 32 | generation` with generation >= 1 |
| 324 | // (aien-scheduler SequenceId::to_u64). The arena hands out slots from 0 and |
| 325 | // never reaches u32::MAX in practice; the holder range is exactly the ids |
| 326 | // whose slot IS u32::MAX. |
| 327 | assert_eq!(HOLDER_SLOT, u32::MAX); |
| 328 | for job in [1u64, 2, 255, 65_536, u32::MAX as u64 - 1, u32::MAX as u64] { |
| 329 | let id = holder_id(job).expect("valid job id"); |
| 330 | assert_eq!(id >> 32, HOLDER_SLOT as u64, "slot is the reserved one"); |
| 331 | assert_eq!(id & 0xFFFF_FFFF, job, "generation is the job id"); |
| 332 | assert!(is_holder_id(id)); |
| 333 | } |
| 334 | // Distinct jobs give distinct ids. |
| 335 | assert_ne!(holder_id(1).unwrap(), holder_id(2).unwrap()); |
| 336 | |
| 337 | // Every arena-shaped id below the reserved slot is not a holder id, for |
| 338 | // the first generations and for the generation boundary. |
| 339 | for slot in (0u32..70_000).chain([u32::MAX - 2, u32::MAX - 1]) { |
| 340 | for generation in [1u32, 2, 3, u32::MAX - 1, u32::MAX] { |
| 341 | let arena_id = ((slot as u64) << 32) | generation as u64; |
| 342 | assert!(!is_holder_id(arena_id), "slot {slot} gen {generation}"); |
| 343 | assert_ne!(Some(arena_id), holder_id(generation as u64).ok()); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Refusals instead of silent truncation: generation zero is invalid, and a |
| 348 | // job id past u32 would wrap onto another live holder. |
| 349 | assert!(holder_id(0).is_err()); |
| 350 | assert!(holder_id(u32::MAX as u64 + 1).is_err()); |
| 351 | assert!(holder_id(u64::MAX).is_err()); |
| 352 | } |
| 353 | |
| 354 | #[test] |
| 355 | fn holder_block_count_counts_distinct_blocks_of_holders_only() { |
| 356 | let mut m = new_mgr(16); |
| 357 | assert_eq!(m.holder_block_count(), 0); |
| 358 | admit(&mut m, 1, &vals(8, 1.0)); // 2 blocks |
| 359 | admit(&mut m, 2, &vals(4, 5.0)); // 1 block |
| 360 | assert_eq!( |
| 361 | m.holder_block_count(), |
| 362 | 0, |
| 363 | "ordinary sequences are not holders" |
| 364 | ); |
| 365 | |
| 366 | let h1 = holder_id(1).unwrap(); |
| 367 | let h2 = holder_id(2).unwrap(); |
| 368 | m.fork_sequence(1, h1).unwrap(); |
| 369 | m.fork_sequence(1, h2).unwrap(); // second holder over the same blocks |
| 370 | assert_eq!(m.holder_block_count(), 2, "shared blocks counted once"); |
| 371 | m.fork_sequence(2, holder_id(3).unwrap()).unwrap(); |
| 372 | assert_eq!(m.holder_block_count(), 3); |
| 373 | |
| 374 | m.free_sequence(h1).unwrap(); |
| 375 | assert_eq!(m.holder_block_count(), 3, "h2 still holds the first two"); |
| 376 | m.free_sequence(h2).unwrap(); |
| 377 | assert_eq!(m.holder_block_count(), 1); |
| 378 | m.free_sequence(holder_id(3).unwrap()).unwrap(); |
| 379 | assert_eq!(m.holder_block_count(), 0); |
| 380 | } |
| 381 | |
| 382 | #[test] |
| 383 | fn holder_partial_failure_keeps_inflight_destinations() { |
| 384 | // One restore allocates a holder over four chunks (one block each). Each |
| 385 | // chunk load gets its own sub-holder over exactly its destination block. |
| 386 | // Chunk 2 fails and its sub-holder is freed; the parent holder is freed too. |
| 387 | // Chunks 0, 1 and 3 are still being written: their blocks must stay out of |
| 388 | // the free list and must not be handed to another sequence. |
| 389 | let total = 8; |
| 390 | let mut m = new_mgr(total); |
| 391 | let parent = holder_id(100).unwrap(); |
| 392 | let prompt: Vec<u32> = (0..16).collect(); |
| 393 | m.allocate_sequence(parent, &prompt).unwrap(); |
| 394 | m.mark_prefill_pending(parent).unwrap(); |
| 395 | let blocks = table_blocks(&m, parent); |
| 396 | assert_eq!(blocks.len(), 4); |
| 397 | |
| 398 | let jobs: Vec<u64> = (0..4).map(|i| holder_id(101 + i).unwrap()).collect(); |
| 399 | for (i, &job) in jobs.iter().enumerate() { |
| 400 | let got = m.fork_range(parent, i, 1, job).expect("sub-holder"); |
| 401 | assert_eq!(got, vec![blocks[i]]); |
| 402 | assert_eq!(m.get_block_table(job).unwrap().total_tokens, BS); |
| 403 | } |
| 404 | for &b in &blocks { |
| 405 | assert_eq!(ref_count(&m, b), 2, "parent holder plus its job holder"); |
| 406 | } |
| 407 | |
| 408 | // Chunk 2 fails: its job holder goes. Then the whole-restore holder goes. |
| 409 | m.free_sequence(jobs[2]).unwrap(); |
| 410 | m.free_sequence(parent).unwrap(); |
| 411 | |
| 412 | assert_eq!( |
| 413 | m.free_block_count(), |
| 414 | total - 3, |
| 415 | "only the failed chunk returned" |
| 416 | ); |
| 417 | for (i, &b) in blocks.iter().enumerate() { |
| 418 | let expect = if i == 2 { 0 } else { 1 }; |
| 419 | assert_eq!(ref_count(&m, b), expect, "block {i}"); |
| 420 | } |
| 421 | // Drain the pool: the three in-flight destinations are never reused. |
| 422 | let drain: Vec<u32> = (0..((total - 3) * BS) as u32).collect(); |
| 423 | let got = m.allocate_sequence(50, &drain).unwrap(); |
| 424 | for (i, b) in blocks.iter().enumerate() { |
| 425 | if i != 2 { |
| 426 | assert!(!got.contains(b), "in-flight destination {b} was reused"); |
| 427 | } |
| 428 | } |
| 429 | assert_eq!(m.holder_block_count(), 3); |
| 430 | |
| 431 | // The surviving jobs finish and release; now everything is returned. |
| 432 | for &i in &[0usize, 1, 3] { |
| 433 | m.free_sequence(jobs[i]).unwrap(); |
| 434 | } |
| 435 | assert_eq!(m.holder_block_count(), 0); |
| 436 | m.free_sequence(50).unwrap(); |
| 437 | assert_eq!(m.free_block_count(), total); |
| 438 | } |
| 439 | |
| 440 | #[test] |
| 441 | fn fork_of_prefix_chunk_does_not_share_suffix_blocks() { |
| 442 | // Coalescing two requests whose common part is only the first chunk must |
| 443 | // not share the whole table: the suffix blocks stay owned by one side. |
| 444 | let total = 12; |
| 445 | let mut m = new_mgr(total); |
| 446 | admit(&mut m, 1, &vals(14, 1.0)); // 4 blocks, last one partial |
| 447 | let blocks = table_blocks(&m, 1); |
| 448 | assert_eq!(blocks.len(), 4); |
| 449 | |
| 450 | let child = 2u64; |
| 451 | let got = m.fork_range(1, 0, 2, child).expect("prefix fork"); |
| 452 | assert_eq!(got, blocks[..2].to_vec()); |
| 453 | let t = m.get_block_table(child).unwrap(); |
| 454 | assert_eq!(t.block_ids, blocks[..2].to_vec()); |
| 455 | assert_eq!(t.total_tokens, 2 * BS); |
| 456 | assert!(t.is_prefill_ready()); |
| 457 | assert_eq!(ref_count(&m, blocks[0]), 2); |
| 458 | assert_eq!(ref_count(&m, blocks[1]), 2); |
| 459 | assert_eq!(ref_count(&m, blocks[2]), 1, "suffix not shared"); |
| 460 | assert_eq!(ref_count(&m, blocks[3]), 1, "suffix not shared"); |
| 461 | |
| 462 | // Freeing the owner returns the suffix and keeps the shared prefix. |
| 463 | m.free_sequence(1).unwrap(); |
| 464 | assert_eq!(m.free_block_count(), total - 2); |
| 465 | assert_eq!(table_blocks(&m, child), blocks[..2].to_vec()); |
| 466 | |
| 467 | // A range ending at the tail block carries the partial token count. |
| 468 | let mut m = new_mgr(total); |
| 469 | admit(&mut m, 1, &vals(14, 1.0)); |
| 470 | m.fork_range(1, 2, 2, 3).unwrap(); |
| 471 | assert_eq!(m.get_block_table(3).unwrap().total_tokens, 14 - 2 * BS); |
| 472 | } |
| 473 | |
| 474 | #[test] |
| 475 | fn fork_range_refusals_change_nothing() { |
| 476 | let mut m = new_mgr(12); |
| 477 | admit(&mut m, 1, &vals(8, 1.0)); |
| 478 | admit(&mut m, 2, &vals(4, 5.0)); |
| 479 | let free = m.free_block_count(); |
| 480 | let active = m.debug_active_blocks(); |
| 481 | assert!(m.fork_range(1, 0, 0, 3).is_err(), "empty range"); |
| 482 | assert!(m.fork_range(1, 1, 2, 3).is_err(), "past the table"); |
| 483 | assert!( |
| 484 | m.fork_range(1, usize::MAX, 2, 3).is_err(), |
| 485 | "overflowing range" |
| 486 | ); |
| 487 | assert!(m.fork_range(9, 0, 1, 3).is_err(), "unknown parent"); |
| 488 | assert!(m.fork_range(1, 0, 1, 1).is_err(), "onto itself"); |
| 489 | assert!(m.fork_range(1, 0, 1, 2).is_err(), "live child kept"); |
| 490 | assert_eq!(m.free_block_count(), free); |
| 491 | assert_eq!(m.debug_active_blocks(), active); |
| 492 | // Readiness travels with the blocks, like fork_sequence: an unready parent |
| 493 | // (a restore destination still loading) gives an unready sub-holder. |
| 494 | m.allocate_sequence(7, &[0, 1, 2, 3]).unwrap(); |
| 495 | m.fork_range(7, 0, 1, 8).unwrap(); |
| 496 | assert_eq!(m.prefill_state(8), Some(PrefillState::Allocated)); |
| 497 | assert_eq!(m.prefill_state(7), Some(PrefillState::Allocated)); |
| 498 | } |
| 499 | |
| 500 | #[test] |
| 501 | fn fork_range_forks_only_the_named_blocks_and_does_not_replace_a_live_child() { |
| 502 | let total = 16; |
| 503 | let mut m = new_mgr(total); |
| 504 | admit(&mut m, 1, &vals(16, 1.0)); // 4 full blocks |
| 505 | let blocks = table_blocks(&m, 1); |
| 506 | assert_eq!(m.prefill_state(1), Some(PrefillState::PrefillReady)); |
| 507 | |
| 508 | // Only the two middle blocks, and the parent's state is left alone. |
| 509 | let h = holder_id(1).unwrap(); |
| 510 | let got = m.fork_range(1, 1, 2, h).expect("middle range"); |
| 511 | assert_eq!(got, blocks[1..3].to_vec()); |
| 512 | assert_eq!(table_blocks(&m, h), blocks[1..3].to_vec()); |
| 513 | assert_eq!(m.get_block_table(h).unwrap().total_tokens, 2 * BS); |
| 514 | assert_eq!(ref_count(&m, blocks[0]), 1); |
| 515 | assert_eq!(ref_count(&m, blocks[1]), 2); |
| 516 | assert_eq!(ref_count(&m, blocks[2]), 2); |
| 517 | assert_eq!(ref_count(&m, blocks[3]), 1); |
| 518 | assert_eq!( |
| 519 | m.prefill_state(1), |
| 520 | Some(PrefillState::PrefillReady), |
| 521 | "fork_range leaves the parent's state alone" |
| 522 | ); |
| 523 | |
| 524 | // A live child is refused and untouched (fork_sequence would free it). |
| 525 | admit(&mut m, 2, &vals(8, 50.0)); |
| 526 | let own = table_blocks(&m, 2); |
| 527 | let free = m.free_block_count(); |
| 528 | let before: Vec<Vec<f32>> = own.iter().map(|&b| raw_block(&m, b)).collect(); |
| 529 | assert!(m.fork_range(1, 0, 1, 2).is_err()); |
| 530 | assert_eq!(table_blocks(&m, 2), own); |
| 531 | assert_eq!(m.free_block_count(), free); |
| 532 | for (i, &b) in own.iter().enumerate() { |
| 533 | assert_eq!(ref_count(&m, b), 1); |
| 534 | assert_eq!(raw_block(&m, b), before[i]); |
| 535 | } |
| 536 | // A live holder is refused the same way. |
| 537 | assert!(m.fork_range(1, 0, 1, h).is_err()); |
| 538 | assert_eq!(table_blocks(&m, h), blocks[1..3].to_vec()); |
| 539 | assert_eq!(ref_count(&m, blocks[0]), 1); |
| 540 | } |
| 541 | |
| 542 | /// Three chunk holders over a 14-token prefix: 8 tokens (2 blocks), 4 tokens |
| 543 | /// (1 block), 2 tokens (1 partial block). Returns (holders, their blocks). |
| 544 | fn chunk_holders(m: &mut Mgr) -> (Vec<u64>, Vec<Vec<usize>>) { |
| 545 | let specs = [(1u64, 8usize), (2, 4), (3, 2)]; |
| 546 | let mut holders = Vec::new(); |
| 547 | let mut blocks = Vec::new(); |
| 548 | for (job, n) in specs { |
| 549 | let h = holder_id(job).unwrap(); |
| 550 | let prompt: Vec<u32> = (0..n as u32).collect(); |
| 551 | m.allocate_sequence(h, &prompt).unwrap(); |
| 552 | m.mark_prefill_pending(h).unwrap(); |
| 553 | m.complete_prefill(h).unwrap(); |
| 554 | blocks.push(table_blocks(m, h)); |
| 555 | holders.push(h); |
| 556 | } |
| 557 | (holders, blocks) |
| 558 | } |
| 559 | |
| 560 | #[test] |
| 561 | fn assemble_table_orders_holders_and_refcounts() { |
| 562 | let total = 16; |
| 563 | let mut m = new_mgr(total); |
| 564 | let (holders, blocks) = chunk_holders(&mut m); |
| 565 | let free = m.free_block_count(); |
| 566 | |
| 567 | // Holders given out of storage order are laid out in the given order. |
| 568 | let order = [holders[0], holders[1], holders[2]]; |
| 569 | let got = m |
| 570 | .assemble_table(5, &order, SuffixPolicy::AllowPartialTail) |
| 571 | .expect("assemble"); |
| 572 | let expect: Vec<usize> = blocks.iter().flatten().copied().collect(); |
| 573 | assert_eq!(got, expect); |
| 574 | let t = m.get_block_table(5).unwrap(); |
| 575 | assert_eq!(t.sequence_id, 5); |
| 576 | assert_eq!(t.block_ids, expect); |
| 577 | assert_eq!(t.total_tokens, 14); |
| 578 | assert_eq!(t.prefill_state, PrefillState::PrefillPending); |
| 579 | assert_eq!(m.free_block_count(), free, "no block allocated or freed"); |
| 580 | for &b in &expect { |
| 581 | assert_eq!(ref_count(&m, b), 2, "holder plus the new table"); |
| 582 | } |
| 583 | // Holders are untouched, and still hold their blocks. |
| 584 | for (h, bl) in holders.iter().zip(&blocks) { |
| 585 | assert_eq!(&table_blocks(&m, *h), bl); |
| 586 | } |
| 587 | |
| 588 | // Two requests assemble from the same chunk holder: refcounts add up, and |
| 589 | // each table is independent. |
| 590 | m.assemble_table(6, &holders[..2], SuffixPolicy::FullBlocksOnly) |
| 591 | .expect("second request, full blocks only"); |
| 592 | assert_eq!(ref_count(&m, blocks[0][0]), 3); |
| 593 | assert_eq!(m.get_block_table(6).unwrap().total_tokens, 12); |
| 594 | |
| 595 | // Freeing holders then tables returns everything exactly once. |
| 596 | for &h in &holders { |
| 597 | m.free_sequence(h).unwrap(); |
| 598 | } |
| 599 | assert_eq!(m.free_block_count(), free); |
| 600 | m.free_sequence(5).unwrap(); |
| 601 | m.free_sequence(6).unwrap(); |
| 602 | assert_eq!(m.free_block_count(), total); |
| 603 | } |
| 604 | |
| 605 | #[test] |
| 606 | fn assemble_table_failure_leaves_every_holder_untouched() { |
| 607 | let mut m = new_mgr(16); |
| 608 | let (holders, blocks) = chunk_holders(&mut m); |
| 609 | admit(&mut m, 9, &vals(4, 1.0)); |
| 610 | let counts: Vec<usize> = (0..16).map(|b| ref_count(&m, b)).collect(); |
| 611 | let unknown = holder_id(77).unwrap(); |
| 612 | let pending = holder_id(78).unwrap(); |
| 613 | m.allocate_sequence(pending, &[0, 1, 2, 3]).unwrap(); // still loading |
| 614 | let free = m.free_block_count(); |
| 615 | let active = m.debug_active_blocks(); |
| 616 | let counts_p: Vec<usize> = (0..16).map(|b| ref_count(&m, b)).collect(); |
| 617 | |
| 618 | let allow = SuffixPolicy::AllowPartialTail; |
| 619 | // Seq already has a table: never replaced. |
| 620 | assert!(m.assemble_table(9, &holders, allow).is_err()); |
| 621 | // Unknown holder in the last position: the good ones must not be touched. |
| 622 | assert!(m |
| 623 | .assemble_table(5, &[holders[0], holders[1], unknown], allow) |
| 624 | .is_err()); |
| 625 | // A real sequence id is not a holder. |
| 626 | assert!(m.assemble_table(5, &[holders[0], 9], allow).is_err()); |
| 627 | // A holder that is not complete (prefill never fenced). |
| 628 | assert!(m.assemble_table(5, &[holders[0], pending], allow).is_err()); |
| 629 | // A holder listed twice would double its blocks. |
| 630 | assert!(m |
| 631 | .assemble_table(5, &[holders[0], holders[0]], allow) |
| 632 | .is_err()); |
| 633 | // A partial block in the middle leaves a gap in positions. |
| 634 | assert!(m |
| 635 | .assemble_table(5, &[holders[2], holders[0]], allow) |
| 636 | .is_err()); |
| 637 | // The suffix policy: a partial tail is refused under FullBlocksOnly. |
| 638 | assert!(m |
| 639 | .assemble_table(5, &holders, SuffixPolicy::FullBlocksOnly) |
| 640 | .is_err()); |
| 641 | // Empty list and a holder id as the destination. |
| 642 | assert!(m.assemble_table(5, &[], allow).is_err()); |
| 643 | assert!(m.assemble_table(unknown, &holders, allow).is_err()); |
| 644 | |
| 645 | assert!(m.get_block_table(5).is_none()); |
| 646 | assert!(m.get_block_table(unknown).is_none()); |
| 647 | assert_eq!(m.free_block_count(), free); |
| 648 | assert_eq!(m.debug_active_blocks(), active); |
| 649 | let after: Vec<usize> = (0..16).map(|b| ref_count(&m, b)).collect(); |
| 650 | assert_eq!(after, counts_p); |
| 651 | assert_ne!(counts, counts_p); |
| 652 | for (h, bl) in holders.iter().zip(&blocks) { |
| 653 | assert_eq!(&table_blocks(&m, *h), bl); |
| 654 | } |
| 655 | assert_eq!(m.get_block_table(9).unwrap().sequence_id, 9); |
| 656 | } |