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-inference-abi / tests / paged_transformer_parity.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 | use aien_inference_abi::{ModelConfig, NativeTransformerBackend, TransformerWeights}; |
| 17 | |
| 18 | #[test] |
| 19 | fn test_paged_vs_contiguous_numerical_parity() { |
| 20 | let config = ModelConfig { |
| 21 | num_layers: 4, |
| 22 | num_heads: 4, |
| 23 | num_kv_heads: 2, |
| 24 | head_dim: 32, |
| 25 | hidden_dim: 128, |
| 26 | intermediate_dim: 256, |
| 27 | vocab_size: 512, |
| 28 | block_size: 16, |
| 29 | ..Default::default() |
| 30 | }; |
| 31 | |
| 32 | let weights = TransformerWeights::reference_test_weights(&config); |
| 33 | |
| 34 | // 1. Contiguous reference backend |
| 35 | let mut contiguous_backend = NativeTransformerBackend::new_reference(weights.clone()); |
| 36 | |
| 37 | // 2. Paged physical COW backend |
| 38 | let mut paged_backend = NativeTransformerBackend::with_paged_kv(weights, 500, 16).unwrap(); |
| 39 | |
| 40 | let prompt: Vec<u32> = (1..32).collect(); |
| 41 | |
| 42 | // Prefill both |
| 43 | let _contiguous_logits = contiguous_backend.prefill_sequence(1, &prompt).unwrap(); |
| 44 | let paged_parent = paged_backend.create_context(&prompt).unwrap(); |
| 45 | let paged_branch = paged_backend.fork_context(paged_parent).unwrap(); |
| 46 | |
| 47 | // Autoregressive generation parity across 10 steps |
| 48 | for step in 0..10 { |
| 49 | let (paged_tok, paged_logits) = paged_backend.decode_branch_step(paged_branch).unwrap(); |
| 50 | |
| 51 | let (contig_tok, contig_logits) = { |
| 52 | let h = { |
| 53 | let seq = contiguous_backend.sequences.get_mut(&1).unwrap(); |
| 54 | let p = seq.tokens.len() - 1; |
| 55 | let lt = *seq.tokens.last().unwrap(); |
| 56 | NativeTransformerBackend::forward_token_impl_paged( |
| 57 | &contiguous_backend.weights, |
| 58 | &*contiguous_backend.tensor_backend, |
| 59 | lt, |
| 60 | p, |
| 61 | seq, |
| 62 | 1, |
| 63 | None, |
| 64 | ) |
| 65 | }; |
| 66 | let l = contiguous_backend.compute_logits(&h); |
| 67 | let (sampled, _) = aien_inference_abi::sample_argmax(&l); |
| 68 | contiguous_backend |
| 69 | .sequences |
| 70 | .get_mut(&1) |
| 71 | .unwrap() |
| 72 | .tokens |
| 73 | .push(sampled); |
| 74 | (sampled, l) |
| 75 | }; |
| 76 | |
| 77 | let max_diff = paged_logits |
| 78 | .iter() |
| 79 | .zip(contig_logits.iter()) |
| 80 | .map(|(a, b)| (a - b).abs()) |
| 81 | .fold(0.0f32, f32::max); |
| 82 | println!( |
| 83 | "Step {}: paged_tok={}, contig_tok={}, max_diff={}", |
| 84 | step, paged_tok, contig_tok, max_diff |
| 85 | ); |
| 86 | |
| 87 | assert_eq!( |
| 88 | paged_tok, contig_tok, |
| 89 | "Token divergence at step {}: paged={}, contiguous={}", |
| 90 | step, paged_tok, contig_tok |
| 91 | ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn test_32_branch_fork_and_cow_acceptance() { |
| 97 | let config = ModelConfig { |
| 98 | num_layers: 2, |
| 99 | num_heads: 4, |
| 100 | num_kv_heads: 2, |
| 101 | head_dim: 16, |
| 102 | hidden_dim: 64, |
| 103 | intermediate_dim: 128, |
| 104 | vocab_size: 256, |
| 105 | block_size: 16, |
| 106 | ..Default::default() |
| 107 | }; |
| 108 | |
| 109 | let weights = TransformerWeights::reference_test_weights(&config); |
| 110 | let mut backend = NativeTransformerBackend::with_paged_kv(weights, 2000, 16).unwrap(); |
| 111 | |
| 112 | let prompt: Vec<u32> = (1..=128).collect(); |
| 113 | let parent = backend.create_context(&prompt).unwrap(); |
| 114 | |
| 115 | let mut branches = Vec::new(); |
| 116 | for _ in 0..32 { |
| 117 | let b = backend.fork_context(parent).unwrap(); |
| 118 | branches.push(b); |
| 119 | } |
| 120 | |
| 121 | let r_init = backend.get_usage_receipt(branches[0]).unwrap(); |
| 122 | assert_eq!(r_init.shared_pages, 8); |
| 123 | assert_eq!(r_init.private_pages, 0); |
| 124 | assert_eq!(r_init.cow_faults, 0); |
| 125 | assert_eq!(r_init.logical_pages, 8); |
| 126 | assert!(r_init.bytes_saved_vs_full_copy > 0); |
| 127 | |
| 128 | for &b in &branches { |
| 129 | let (tok, _) = backend.decode_branch_step(b).unwrap(); |
| 130 | assert!(tok > 0); |
| 131 | } |
| 132 | |
| 133 | let r_branch = backend.get_usage_receipt(branches[0]).unwrap(); |
| 134 | assert_eq!(r_branch.shared_pages, 8); |
| 135 | assert_eq!(r_branch.private_pages, 1); |
| 136 | assert_eq!(r_branch.logical_pages, 9); |
| 137 | |
| 138 | for b in branches { |
| 139 | backend.release_branch(b).unwrap(); |
| 140 | } |
| 141 | |
| 142 | let kv_mgr = backend.kv_manager.as_ref().unwrap().read(); |
| 143 | assert_eq!(kv_mgr.active_sequence_count(), 1); |
| 144 | let m = kv_mgr.metrics(); |
| 145 | assert_eq!(m.physical_pages, 8); |
| 146 | assert_eq!(m.shared_pages, 0); |
| 147 | assert_eq!(m.private_pages, 8); |
| 148 | } |