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 / llama32_pin.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 | //! Pins unsloth/Llama-3.2-1B-Instruct (the CAND-4 model, snapshot |
| 17 | //! 5a8abab4a5d6f164389b1079fb721cfab8d7126c) on the CPU reference path so the Qwen3 loader change |
| 18 | //! cannot alter a Llama-architecture model: composed prompt ids, and the exact last-position |
| 19 | //! logits (FNV-1a over every f32 bit pattern, plus the top-5 ids). Values were computed on |
| 20 | //! origin/main 845f58f (before the Qwen3 change). Set `AIEN_LLAMA3_DIR` to that snapshot |
| 21 | //! directory to run; skipped otherwise. |
| 22 | |
| 23 | use aien_inference_abi::tokenizer::ChatTokenizer; |
| 24 | use aien_inference_abi::{load_model_config, TransformerWeights}; |
| 25 | use std::path::Path; |
| 26 | |
| 27 | const TURNS: &[(&str, &str)] = &[("system", "You are terse."), ("user", " What is 2+2?\n")]; |
| 28 | |
| 29 | fn fnv1a(logits: &[f32]) -> u64 { |
| 30 | let mut h: u64 = 0xcbf29ce484222325; |
| 31 | for v in logits { |
| 32 | for b in v.to_bits().to_le_bytes() { |
| 33 | h ^= b as u64; |
| 34 | h = h.wrapping_mul(0x100000001b3); |
| 35 | } |
| 36 | } |
| 37 | h |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn llama32_prompt_ids_and_forward_logits_are_pinned() { |
| 42 | let Ok(dir) = std::env::var("AIEN_LLAMA3_DIR") else { |
| 43 | eprintln!("AIEN_LLAMA3_DIR not set: Llama 3.2 pin skipped"); |
| 44 | return; |
| 45 | }; |
| 46 | let dir = Path::new(&dir); |
| 47 | let tok = ChatTokenizer::from_model_dir(dir, None).expect("tokenizer"); |
| 48 | let text = tok.template().render(TURNS); |
| 49 | let ids = tok.encode(&text).expect("encode"); |
| 50 | println!("PIN llama32 ids={ids:?}"); |
| 51 | assert_eq!(ids, PIN_IDS); |
| 52 | |
| 53 | let config = load_model_config(dir).expect("config"); |
| 54 | let weights = TransformerWeights::load_from_safetensors(dir.join("model.safetensors"), &config) |
| 55 | .expect("weights"); |
| 56 | let logits = |
| 57 | weights.forward_sequence_with_diagnostics(&ids).activations["last_token_logits"].clone(); |
| 58 | let mut order: Vec<usize> = (0..logits.len()).collect(); |
| 59 | order.sort_by(|a, b| logits[*b].total_cmp(&logits[*a])); |
| 60 | let top5: Vec<usize> = order[..5].to_vec(); |
| 61 | println!( |
| 62 | "PIN llama32 len={} fnv={:#018x} top5={top5:?}", |
| 63 | logits.len(), |
| 64 | fnv1a(&logits) |
| 65 | ); |
| 66 | assert_eq!(logits.len(), PIN_LEN); |
| 67 | assert_eq!(top5, PIN_TOP5); |
| 68 | assert_eq!(fnv1a(&logits), PIN_FNV); |
| 69 | } |
| 70 | |
| 71 | const PIN_IDS: &[u32] = &[ |
| 72 | 128000, 128006, 9125, 128007, 271, 2675, 527, 51637, 13, 128009, 128006, 882, 128007, 271, |
| 73 | 3923, 374, 220, 17, 10, 17, 30, 128009, 128006, 78191, 128007, 271, |
| 74 | ]; |
| 75 | const PIN_LEN: usize = 128256; |
| 76 | const PIN_TOP5: [usize; 5] = [18, 19, 40, 17, 16]; |
| 77 | const PIN_FNV: u64 = 0x00d44eba4b6ca94b; |