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-inference-abi / tests / paged_attention_parity.rs

164 lines · 5949 bytes Raw Blame History
1#![allow(clippy::too_many_arguments, clippy::needless_range_loop)]
2// SPDX-License-Identifier: Apache-2.0
3// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17//! Paged KV pool layout certification (host only).
18//!
19//! Enforces:
20//! 1. Pure layout stride invariants against independent calculation.
21//! 2. Coordinate-encoded byte pattern integrity in the physical pool.
22//!
23//! The GPU parity layers of this suite exercised the CUDA bf16 kernel, which FB-1 cut 6
24//! removed. Omega's paged attention is checked against the CPU reference by the
25//! aien-inference-runtime parity and strict real-model gates.
26
27use aien_kv_cache::{KvDType, KvLayout, KvPoolConfig, UnifiedKvTensorPool};
28
29/// Independent byte-offset formula calculating addresses without any production code.
30fn oracle_byte_offset(
31 block: usize,
32 layer: usize,
33 is_value: bool,
34 token: usize,
35 head: usize,
36 dim: usize,
37 num_layers: usize,
38 num_kv_heads: usize,
39 head_dim: usize,
40 block_size: usize,
41 elem_bytes: usize,
42) -> usize {
43 let head_stride = head_dim * elem_bytes;
44 let token_stride = num_kv_heads * head_stride;
45 let plane_stride = block_size * token_stride;
46 let layer_stride = 2 * plane_stride;
47 let block_stride = num_layers * layer_stride;
48
49 block * block_stride
50 + layer * layer_stride
51 + (if is_value { plane_stride } else { 0 })
52 + token * token_stride
53 + head * head_stride
54 + dim * elem_bytes
55}
56#[test]
57fn test_layer1_layout_strides_independent() {
58 let config = KvPoolConfig {
59 num_blocks: 16,
60 block_size: 16,
61 num_layers: 22,
62 num_kv_heads: 4,
63 head_dim: 64,
64 dtype: KvDType::Bf16,
65 };
66
67 let layout = KvLayout::for_bf16(&config).unwrap();
68 let desc = layout.to_desc();
69
70 let elem_bytes = 2;
71 let expected_head_stride = 64 * elem_bytes;
72 let expected_token_stride = 4 * expected_head_stride;
73 let expected_plane_stride = 16 * expected_token_stride;
74 let expected_layer_stride = 2 * expected_plane_stride;
75 let expected_block_stride = 22 * expected_layer_stride;
76
77 assert_eq!(desc.head_stride_bytes, expected_head_stride as u64);
78 assert_eq!(desc.token_stride_bytes, expected_token_stride as u64);
79 assert_eq!(desc.kv_plane_stride_bytes, expected_plane_stride as u64);
80 assert_eq!(desc.layer_stride_bytes, expected_layer_stride as u64);
81 assert_eq!(desc.block_stride_bytes, expected_block_stride as u64);
82
83 for b in [0, 5, 15] {
84 for l in [0, 11, 21] {
85 for is_v in [false, true] {
86 for tok in [0, 8, 15] {
87 for h in [0, 2, 3] {
88 for d in [0, 31, 63] {
89 let prod_offset = layout.element_offset(b, l, is_v, tok, h, d);
90 let oracle_off = oracle_byte_offset(
91 b, l, is_v, tok, h, d, 22, 4, 64, 16, elem_bytes,
92 );
93 assert_eq!(
94 prod_offset, oracle_off,
95 "Mismatch at ({}, {}, {}, {}, {}, {})",
96 b, l, is_v, tok, h, d
97 );
98 }
99 }
100 }
101 }
102 }
103 }
104}
105
106#[test]
107fn test_layer2_physical_coordinate_byte_pattern() {
108 let config = KvPoolConfig {
109 num_blocks: 4,
110 block_size: 16,
111 num_layers: 4,
112 num_kv_heads: 2,
113 head_dim: 32,
114 dtype: KvDType::Bf16,
115 };
116
117 let mut pool = UnifiedKvTensorPool::allocate(config.clone()).unwrap();
118
119 // Write unique coordinate patterns
120 for b in 0..4 {
121 for l in 0..4 {
122 for tok in 0..16 {
123 let mut k_vec = vec![0.0f32; 2 * 32];
124 let mut v_vec = vec![0.0f32; 2 * 32];
125 for h in 0..2 {
126 for d in 0..32 {
127 let idx = h * 32 + d;
128 let val = (b * 32 + l * 8 + tok + h * 20 + 1) as f32;
129 k_vec[idx] = val;
130 v_vec[idx] = -val;
131 }
132 }
133 pool.write_token_kv(b, l, tok, &k_vec, &v_vec);
134 }
135 }
136 }
137
138 // Read back and verify exact recovery
139 for b in 0..4 {
140 for l in 0..4 {
141 for tok in 0..16 {
142 let mut k_out = vec![0.0f32; 2 * 32];
143 let mut v_out = vec![0.0f32; 2 * 32];
144 pool.read_token_kv(b, l, tok, &mut k_out, &mut v_out);
145 for h in 0..2 {
146 for d in 0..32 {
147 let idx = h * 32 + d;
148 let expected_val = (b * 32 + l * 8 + tok + h * 20 + 1) as f32;
149 assert_eq!(
150 k_out[idx], expected_val,
151 "K mismatch at ({}, {}, {}, {}, {}): expected {}, got {}",
152 b, l, tok, h, d, expected_val, k_out[idx]
153 );
154 assert_eq!(
155 v_out[idx], -expected_val,
156 "V mismatch at ({}, {}, {}, {}, {}): expected {}, got {}",
157 b, l, tok, h, d, -expected_val, v_out[idx]
158 );
159 }
160 }
161 }
162 }
163 }
164}