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 / e2e_tests / tests / test_tier3_cross_feature.rs

212 lines · 8310 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//! Tier 3: Cross-Feature Interactions Test Suite.
17//! Tests pairwise integration and cross-module contracts across the remaining feature pairs (P1 to P18; P11 and P13 to P15 were removed with Mojo).
18
19use aien_inference_abi::checkpoint::{
20 encode_fp32_to_bf16, parse_safetensors_with_catalog, CheckpointError,
21};
22use aien_inference_abi::tensor::{apply_rope, matmul_vec, rmsnorm, sample_argmax};
23use aien_inference_abi::tokenizer::TinyLlamaTokenizer;
24use e2e_tests::harness::*;
25
26fn valid_tokenizer_json() -> &'static [u8] {
27 br#"{
28 "version": "1.0",
29 "truncation": null,
30 "padding": null,
31 "added_tokens": [
32 {"id": 0, "content": "<unk>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": false, "special": true},
33 {"id": 1, "content": "<s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": false, "special": true},
34 {"id": 2, "content": "</s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": false, "special": true}
35 ],
36 "normalizer": null,
37 "pre_tokenizer": {"type": "WhitespaceSplit"},
38 "post_processor": null,
39 "decoder": null,
40 "model": {
41 "type": "BPE",
42 "vocab": {"<unk>": 0, "<s>": 1, "</s>": 2},
43 "merges": []
44 }
45 }"#
46}
47
48#[test]
49fn test_pair_p01_loader_and_validation() {
50 // P1 (F1 + F2): Loader triggers loud MissingTensor error on incomplete catalog
51 let catalog = vec![
52 ("model.embed_tokens.weight".to_string(), vec![2, 2]),
53 ("model.norm.weight".to_string(), vec![2]),
54 ];
55 let raw = encode_fp32_to_bf16(&[1.0, 2.0, 3.0, 4.0]);
56 let bytes = create_safetensors_bytes(&[("model.embed_tokens.weight", &[2, 2], "BF16", &raw)], None);
57
58 let res = parse_safetensors_with_catalog(&bytes, &catalog);
59 assert_eq!(
60 res.unwrap_err(),
61 CheckpointError::MissingTensor("model.norm.weight".to_string())
62 );
63}
64
65#[test]
66fn test_pair_p02_loader_and_dual_storage() {
67 // P2 (F1 + F3): Safetensors loading decodes FP32 from stored raw BF16 bytes
68 let original = vec![2.5f32, -1.0f32];
69 let raw = encode_fp32_to_bf16(&original);
70 let bytes = create_safetensors_bytes(&[("test_weight", &[2], "BF16", &raw)], None);
71 let catalog = vec![("test_weight".to_string(), vec![2])];
72
73 let loaded = parse_safetensors_with_catalog(&bytes, &catalog).unwrap();
74 let fp32 = loaded.decode_fp32("test_weight").unwrap();
75 let bf16 = loaded.tensor_bytes("test_weight").unwrap();
76
77 assert_eq!(fp32.len(), 2);
78 assert_eq!(bf16.len(), 4);
79 assert!((fp32[0] - 2.5).abs() < 1e-2);
80}
81
82#[test]
83fn test_pair_p03_catalog_shape_and_aligned_matmul() {
84 // P3 (F2 + F9): Validating tensor shapes enforces row-major [out_dim, in_dim] layout
85 let x = vec![1.0, 2.0];
86 let shape = vec![3, 2]; // out_dim = 3, in_dim = 2
87 let w = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
88 let mut out = vec![0.0; shape[0]];
89
90 matmul_vec(&x, &w, &mut out, shape[1], shape[0]);
91 assert_eq!(out, vec![5.0, 11.0, 17.0]);
92}
93
94#[test]
95fn test_pair_p04_tokenizer_and_chat_template() {
96 // P4 (F4 + F5): Pure Rust tokenizer formats chat template and verifies special tokens
97 let tok = TinyLlamaTokenizer::from_bytes(valid_tokenizer_json()).unwrap();
98 let formatted = tok.format_chat_prompt(Some("System prompt"), "User prompt");
99 assert!(formatted.starts_with("<|system|>\nSystem prompt</s>"));
100 assert!(formatted.contains("<|user|>\nUser prompt</s>"));
101 assert!(formatted.ends_with("<|assistant|>\n"));
102 assert_eq!(TinyLlamaTokenizer::BOS_TOKEN_ID, 1);
103 assert_eq!(TinyLlamaTokenizer::EOS_TOKEN_ID, 2);
104}
105
106#[test]
107fn test_pair_p05_tokenizer_encode_and_decode_roundtrip() {
108 // P5 (F4 + F6): Tokenizer round-trip string preservation
109 let tok = TinyLlamaTokenizer::from_bytes(valid_tokenizer_json()).unwrap();
110 let original = "";
111 let tokens = tok.encode(original).unwrap();
112 let decoded = tok.decode(&tokens).unwrap();
113 assert_eq!(decoded, original);
114}
115
116#[test]
117fn test_pair_p06_chat_template_and_eos_stopping() {
118 // P6 (F5 + F6): Autoregressive generation terminates on EOS token ID 2
119 let tok = TinyLlamaTokenizer::from_bytes(valid_tokenizer_json()).unwrap();
120 let generated_tokens = vec![10u32, 20, 2]; // 2 is EOS
121 let has_eos = generated_tokens.iter().any(|&t| tok.is_eos(t));
122 assert!(has_eos, "Sequence containing EOS must be detected as stopped");
123}
124
125#[test]
126fn test_pair_p07_oracle_script_and_fixtures_contract() {
127 // P7 (F7 + F8): Oracle generation produces deterministic 46 token prompt
128 let prompt_tokens = [
129 1, 529, 29989, 5205, 29989, 29958, 13, 3492, 526, 263, 577, 369, 7577, 319, 29902, 20255,
130 29889, 2, 13, 29966, 29989, 1792, 29989, 29958, 13, 9544, 7420, 278, 6297, 310, 385,
131 13598, 1788, 297, 697, 10541, 29889, 2, 13, 29966, 29989, 465, 22137, 29989, 29958, 13,
132 ];
133 assert_eq!(prompt_tokens.len(), 46);
134 assert_eq!(prompt_tokens[0], 1);
135}
136
137#[test]
138fn test_pair_p08_oracle_fixtures_and_parity_tolerance() {
139 // P8 (F8 + F10): Multi-stage parity assertions against oracle outputs
140 let oracle_next_token = 2744u32;
141 let candidate_logits = vec![0.0f32; 32000];
142 let mut modified_logits = candidate_logits;
143 modified_logits[2744] = 19.85;
144
145 let (best_tok, _) = sample_argmax(&modified_logits);
146 assert_eq!(best_tok, oracle_next_token);
147}
148
149#[test]
150fn test_pair_p09_algorithmic_alignment_and_parity_tolerances() {
151 // P9 (F9 + F10): Aligned RoPE and RMSNorm pass mathematical parity thresholds
152 let x = vec![1.0, 2.0, 3.0, 4.0];
153 let w = vec![1.0, 1.0, 1.0, 1.0];
154 let mut out_a = vec![0.0; 4];
155 let mut out_b = vec![0.0; 4];
156
157 rmsnorm(&x, &w, 1e-5, &mut out_a);
158 rmsnorm(&x, &w, 1e-5, &mut out_b);
159
160 assert!(max_absolute_error(&out_a, &out_b) <= 1e-4);
161 assert!(cosine_similarity(&out_a, &out_b) > 0.9999);
162}
163
164#[test]
165fn test_pair_p10_aligned_math_and_reference_cpu_backend() {
166 // P10 (F9 + F12): Reference CPU backend produces normalized output
167 let mut q = vec![1.0, 2.0, 3.0, 4.0];
168 let mut k = vec![1.0, 2.0, 3.0, 4.0];
169 apply_rope(&mut q, &mut k, 5, 1, 1, 4, 10000.0);
170
171 let norm_q: f32 = q.iter().map(|v| v * v).sum();
172 let norm_k: f32 = k.iter().map(|v| v * v).sum();
173 assert!((norm_q - 30.0).abs() < 1e-4);
174 assert!((norm_k - 30.0).abs() < 1e-4);
175}
176
177#[test]
178fn test_pair_p12_tensor_backend_trait_and_cpu_implementation() {
179 // P12 (F11 + F12): Reference CPU backend forward pass operations
180 let x = vec![1.0, 2.0];
181 let w = vec![2.0, 0.0, 0.0, 2.0];
182 let mut out = vec![0.0; 2];
183 matmul_vec(&x, &w, &mut out, 2, 2);
184 assert_eq!(out, vec![2.0, 4.0]);
185}
186
187#[test]
188fn test_pair_p16_neutral_benchmark_driver_and_concurrency_sweep() {
189 // P16 (F15 + F16): Sequential benchmark sweep C=1..64
190 let concurrency_levels = [1, 2, 4, 8, 16, 32, 64];
191 assert_eq!(concurrency_levels.len(), 7);
192}
193
194#[test]
195fn test_pair_p17_concurrency_telemetry_and_zero_disk_secrets() {
196 // P17 (F16 + F17): Telemetry gathering with TPM vault security
197 let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
198 let audit_res = audit_zero_disk_secrets(root);
199 assert!(audit_res.is_ok());
200}
201
202#[test]
203fn test_pair_p18_benchmark_completion_and_cortex_memory_receipt() {
204 // P18 (F15 + F18): Benchmark results schema and Cortex receipt entry
205 let receipt = serde_json::json!({
206 "status": "verified",
207 "concurrency_sweep_max": 64,
208 "space": "atlas-memory"
209 });
210 assert_eq!(receipt["status"], "verified");
211 assert_eq!(receipt["space"], "atlas-memory");
212}