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 / openwaldo_forward_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 | //! OpenWALDO tiny model (1 layer, hidden 32, 4 heads, 2 KV heads, vocab 259, tied |
| 17 | //! embeddings, bf16) loaded by the CPU reference engine from its Hugging Face directory, |
| 18 | //! compared with last-position logits from transformers 5.17.0 (CPU, float32 after |
| 19 | //! upcasting the bf16 weights). Input ids [1, 75, 104, 111] = bos + bytes "Hel". |
| 20 | //! Tolerance: absolute 1e-4, the ABSOLUTE_TOLERANCE of tinyllama_parity.rs (set before |
| 21 | //! the first comparison, not tuned). |
| 22 | |
| 23 | use aien_inference_abi::{load_model_config, TransformerWeights}; |
| 24 | use serde_json::Value; |
| 25 | use std::path::PathBuf; |
| 26 | |
| 27 | const ABSOLUTE_TOLERANCE: f32 = 1e-4; |
| 28 | |
| 29 | fn dir() -> PathBuf { |
| 30 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("fixtures/openwaldo-byte") |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn openwaldo_cpu_logits_match_transformers_reference() { |
| 35 | let dir = dir(); |
| 36 | let config = load_model_config(&dir).expect("config.json"); |
| 37 | let weights = TransformerWeights::load_from_safetensors(dir.join("model.safetensors"), &config) |
| 38 | .expect("weights"); |
| 39 | let r: Value = |
| 40 | serde_json::from_str(&std::fs::read_to_string(dir.join("forward_reference.json")).unwrap()) |
| 41 | .unwrap(); |
| 42 | let ids: Vec<u32> = r["ids"] |
| 43 | .as_array() |
| 44 | .unwrap() |
| 45 | .iter() |
| 46 | .map(|v| v.as_u64().unwrap() as u32) |
| 47 | .collect(); |
| 48 | assert_eq!(ids, vec![1, 75, 104, 111]); |
| 49 | let want: Vec<f32> = r["logits"] |
| 50 | .as_array() |
| 51 | .unwrap() |
| 52 | .iter() |
| 53 | .map(|v| v.as_f64().unwrap() as f32) |
| 54 | .collect(); |
| 55 | let got = |
| 56 | weights.forward_sequence_with_diagnostics(&ids).activations["last_token_logits"].clone(); |
| 57 | assert_eq!(got.len(), 259); |
| 58 | assert_eq!(want.len(), 259); |
| 59 | let max_abs = got |
| 60 | .iter() |
| 61 | .zip(&want) |
| 62 | .map(|(a, b)| (a - b).abs()) |
| 63 | .fold(0.0f32, f32::max); |
| 64 | eprintln!("max abs diff = {max_abs:e}"); |
| 65 | assert!(max_abs <= ABSOLUTE_TOLERANCE, "max abs diff {max_abs:e}"); |
| 66 | let argmax = |v: &[f32]| { |
| 67 | v.iter() |
| 68 | .enumerate() |
| 69 | .max_by(|a, b| a.1.total_cmp(b.1)) |
| 70 | .unwrap() |
| 71 | .0 |
| 72 | }; |
| 73 | assert_eq!(argmax(&got) as u64, r["argmax"].as_u64().unwrap()); |
| 74 | } |