interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-kv-cache / tests / incremental_blocks_tests.rs

112 lines · 3972 bytes Raw Blame History
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: `incremental_blocks_needed` is the one authority for new blocks.
17//! Every prediction is checked against what the real append path allocates.
18
19use aien_kv_cache::AienKvManager;
20
21const BS: usize = 16;
22
23fn prefilled(mgr: &mut AienKvManager, id: u64, tokens: usize) {
24 let prompt: Vec<u32> = (0..tokens as u32).collect();
25 mgr.allocate_sequence(id, &prompt).unwrap();
26 mgr.mark_prefill_pending(id).unwrap();
27 mgr.complete_prefill(id).unwrap();
28}
29
30/// Appends `n` tokens and returns how many free blocks that consumed.
31fn consumed_by_appends(mgr: &mut AienKvManager, id: u64, n: usize) -> usize {
32 let before = mgr.available_blocks();
33 for _ in 0..n {
34 mgr.append_token(id).unwrap();
35 }
36 before - mgr.available_blocks()
37}
38
39#[test]
40fn no_table_needs_a_fresh_allocation_at_the_pool_block_size() {
41 let mgr = AienKvManager::new(64, 32);
42 assert_eq!(mgr.incremental_blocks_needed(1, 40), 2);
43 assert_eq!(mgr.incremental_blocks_needed(1, 64), 2);
44 assert_eq!(mgr.incremental_blocks_needed(1, 65), 3);
45 assert_eq!(mgr.incremental_blocks_needed(1, 0), 0);
46}
47
48#[test]
49fn fork_child_of_a_partial_prefix_needs_one_copy_on_write_block() {
50 let mut mgr = AienKvManager::new(64, BS);
51 prefilled(&mut mgr, 1, 40); // 3 blocks, last holds 8 tokens
52 mgr.fork_prefilled(1, 2).unwrap();
53 assert_eq!(mgr.incremental_blocks_needed(2, 40), 1);
54 assert_eq!(
55 consumed_by_appends(&mut mgr, 2, 1),
56 1,
57 "prediction == reality"
58 );
59}
60
61#[test]
62fn fork_child_of_a_full_prefix_needs_one_fresh_block() {
63 let mut mgr = AienKvManager::new(64, BS);
64 prefilled(&mut mgr, 1, 48); // exactly 3 full blocks
65 mgr.fork_prefilled(1, 2).unwrap();
66 assert_eq!(mgr.incremental_blocks_needed(2, 48), 1);
67 assert_eq!(consumed_by_appends(&mut mgr, 2, 1), 1);
68}
69
70#[test]
71fn private_partial_tail_absorbs_the_first_token() {
72 let mut mgr = AienKvManager::new(64, BS);
73 prefilled(&mut mgr, 1, 40); // private tail with 8 of 16 slots used
74 assert_eq!(mgr.incremental_blocks_needed(1, 40), 0);
75 assert_eq!(consumed_by_appends(&mut mgr, 1, 1), 0);
76}
77
78#[test]
79fn prediction_matches_real_appends_across_shapes() {
80 for prompt in [1usize, 15, 16, 17, 40, 47, 48, 100] {
81 for appends in [1usize, 2, 8, 9, 16, 17, 40] {
82 for fork in [false, true] {
83 let mut mgr = AienKvManager::new(256, BS);
84 prefilled(&mut mgr, 1, prompt);
85 let id = if fork {
86 mgr.fork_prefilled(1, 2).unwrap();
87 2
88 } else {
89 1
90 };
91 let predicted = mgr.blocks_needed_for_appends(id, appends);
92 let actual = consumed_by_appends(&mut mgr, id, appends);
93 assert_eq!(
94 predicted, actual,
95 "prompt {prompt}, appends {appends}, fork {fork}"
96 );
97 }
98 }
99 }
100}
101
102#[test]
103fn unready_table_counts_a_fresh_allocation_minus_blocks_the_drop_returns() {
104 let mut mgr = AienKvManager::new(64, BS);
105 let prompt: Vec<u32> = (0..40).collect();
106 mgr.allocate_sequence(1, &prompt).unwrap(); // Allocated, not computed
107 assert_eq!(
108 mgr.incremental_blocks_needed(1, 40),
109 0,
110 "own blocks come back"
111 );
112}