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-abi-core / tests / attention_geometry.rs

166 lines · 5374 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//! Contract tests for `AttentionGeometry` (Drake's attention hardening cut, phase 1).
17//! Numbers come from the model configs in this repo, not from guesses.
18
19use aien_abi_core::{AttentionGeometry, AttentionGeometryError, ModelConfig};
20
21#[test]
22fn qwen3_coder_q_dim_differs_from_hidden_dim() {
23 // crates/aien-inference-abi/src/qwen3_coder.rs: 32 q heads, 4 kv heads, head_dim 128,
24 // hidden 2048. q_dim (4096) is NOT hidden_dim and the contract must not care.
25 let g = AttentionGeometry::new(32, 4, 128).expect("Qwen3-Coder geometry");
26 assert_eq!(g.q_dim(), 4096);
27 assert_eq!(g.kv_dim(), 512);
28 assert_eq!(g.gqa_ratio(), 8);
29 assert_ne!(
30 g.q_dim(),
31 2048,
32 "q_dim must be allowed to differ from hidden_dim"
33 );
34 assert_eq!(g.kv_head_of(0), Some(0));
35 assert_eq!(g.kv_head_of(7), Some(0));
36 assert_eq!(g.kv_head_of(8), Some(1));
37 assert_eq!(g.kv_head_of(31), Some(3));
38 assert_eq!(g.kv_head_of(32), None);
39}
40
41#[test]
42fn tinyllama_and_default_configs_build() {
43 let t = ModelConfig::tinyllama_1_1b()
44 .attention_geometry()
45 .expect("TinyLlama geometry");
46 assert_eq!(
47 (t.num_q_heads(), t.num_kv_heads(), t.head_dim()),
48 (32, 4, 64)
49 );
50 assert_eq!(t.q_dim(), 2048);
51 assert_eq!(t.kv_dim(), 256);
52 let d = ModelConfig::default()
53 .attention_geometry()
54 .expect("default geometry");
55 assert_eq!((d.q_dim(), d.kv_dim(), d.gqa_ratio()), (4096, 1024, 4));
56}
57
58#[test]
59fn multi_head_attention_is_ratio_one() {
60 let g = AttentionGeometry::new(16, 16, 64).unwrap();
61 assert_eq!(g.gqa_ratio(), 1);
62 for h in 0..16 {
63 assert_eq!(g.kv_head_of(h), Some(h));
64 }
65 assert_eq!(g.q_dim(), g.kv_dim());
66}
67
68#[test]
69fn non_power_of_two_ratio_is_accepted() {
70 // The contract describes the model, not one kernel's envelope.
71 let g = AttentionGeometry::new(24, 8, 64).unwrap();
72 assert_eq!(g.gqa_ratio(), 3);
73 assert_eq!(g.kv_head_of(5), Some(1));
74 let g = AttentionGeometry::new(40, 8, 128).unwrap();
75 assert_eq!(g.gqa_ratio(), 5);
76 assert_eq!(g.kv_head_of(39), Some(7));
77}
78
79#[test]
80fn zero_counts_are_refused() {
81 assert_eq!(
82 AttentionGeometry::new(0, 4, 64),
83 Err(AttentionGeometryError::ZeroQueryHeads)
84 );
85 assert_eq!(
86 AttentionGeometry::new(32, 0, 64),
87 Err(AttentionGeometryError::ZeroKvHeads)
88 );
89 assert_eq!(
90 AttentionGeometry::new(32, 4, 0),
91 Err(AttentionGeometryError::ZeroHeadDim)
92 );
93}
94
95#[test]
96fn non_divisible_heads_are_refused() {
97 assert_eq!(
98 AttentionGeometry::new(10, 4, 64),
99 Err(AttentionGeometryError::QueryHeadsNotDivisible {
100 num_q_heads: 10,
101 num_kv_heads: 4
102 })
103 );
104 // more kv heads than query heads is also not a whole ratio
105 assert_eq!(
106 AttentionGeometry::new(4, 8, 64),
107 Err(AttentionGeometryError::QueryHeadsNotDivisible {
108 num_q_heads: 4,
109 num_kv_heads: 8
110 })
111 );
112}
113
114#[test]
115fn overflow_is_refused_not_wrapped() {
116 let heads = usize::MAX / 2 + 1;
117 assert_eq!(
118 AttentionGeometry::new(heads, 1, 4),
119 Err(AttentionGeometryError::Overflow { heads, head_dim: 4 })
120 );
121 // largest product that still fits is accepted
122 let g = AttentionGeometry::new(usize::MAX / 64, 1, 64).unwrap();
123 assert_eq!(g.q_dim(), (usize::MAX / 64) * 64);
124}
125
126#[test]
127fn kv_pool_mismatch_is_named() {
128 let g = AttentionGeometry::new(32, 4, 64).unwrap();
129 assert_eq!(g.check_kv_pool(4, 64), Ok(()));
130 assert_eq!(
131 g.check_kv_pool(8, 64),
132 Err(AttentionGeometryError::KvPoolMismatch {
133 geometry_kv_heads: 4,
134 geometry_head_dim: 64,
135 pool_kv_heads: 8,
136 pool_head_dim: 64,
137 })
138 );
139 let msg = g.check_kv_pool(4, 128).unwrap_err().to_string();
140 assert!(msg.contains("4 kv heads x head_dim 64") && msg.contains("4 kv heads x head_dim 128"));
141}
142
143#[test]
144fn model_config_with_bad_heads_is_refused() {
145 let mut c = ModelConfig::tinyllama_1_1b();
146 c.num_kv_heads = 6;
147 assert_eq!(
148 c.attention_geometry(),
149 Err(AttentionGeometryError::QueryHeadsNotDivisible {
150 num_q_heads: 32,
151 num_kv_heads: 6
152 })
153 );
154}
155
156#[test]
157fn serde_round_trip_rechecks() {
158 let g = AttentionGeometry::new(32, 4, 128).unwrap();
159 let json = serde_json::to_string(&g).unwrap();
160 let back: AttentionGeometry = serde_json::from_str(&json).unwrap();
161 assert_eq!(back, g);
162 let bad = serde_json::from_str::<AttentionGeometry>(
163 r#"{"num_q_heads":10,"num_kv_heads":4,"head_dim":64}"#,
164 );
165 assert!(bad.is_err(), "deserialising must re-run the checks");
166}