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-omega-gpu / tests / 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 | //! Host tests run everywhere (stub-aware). The chip parity test is #[ignore]d: |
| 17 | //! run it on the Spark through the heavy queue with a native build. |
| 18 | use aien_omega_gpu::*; |
| 19 | |
| 20 | fn lcg(s: &mut u32) -> f32 { |
| 21 | *s = s.wrapping_mul(1664525).wrapping_add(1013904223); |
| 22 | ((*s >> 8) as f32 / (1u32 << 24) as f32) - 0.5 |
| 23 | } |
| 24 | |
| 25 | /// Round to nearest even bf16, return the bits. |
| 26 | fn bf16_bits(f: f32) -> u16 { |
| 27 | let u = f.to_bits(); |
| 28 | if f.is_nan() { |
| 29 | return ((u >> 16) | 0x40) as u16; |
| 30 | } |
| 31 | let r = u.wrapping_add(0x7fff + ((u >> 16) & 1)); |
| 32 | (r >> 16) as u16 |
| 33 | } |
| 34 | fn bf16_val(b: u16) -> f32 { |
| 35 | f32::from_bits((b as u32) << 16) |
| 36 | } |
| 37 | |
| 38 | fn reference(m: usize, k: usize, n: usize, a: &[f32], b: &[f32]) -> Vec<f32> { |
| 39 | let mut c = vec![0f32; m * n]; |
| 40 | for i in 0..m { |
| 41 | for j in 0..n { |
| 42 | let mut s = 0f64; |
| 43 | for p in 0..k { |
| 44 | s += a[i * k + p] as f64 * b[p * n + j] as f64; |
| 45 | } |
| 46 | c[i * n + j] = s as f32; |
| 47 | } |
| 48 | } |
| 49 | c |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn lock_is_full_sha() { |
| 54 | assert_eq!(PINNED_OMEGA_SHA.len(), 40); |
| 55 | // The build reads omega.lock; the constant must be exactly that file's sha. |
| 56 | assert_eq!(PINNED_OMEGA_SHA, include_str!("../../../omega.lock").trim()); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn rc_names() { |
| 61 | assert_eq!(rc_name(0), "OK"); |
| 62 | assert_eq!(rc_name(-2), "TOO_LARGE"); |
| 63 | assert_eq!(rc_name(-5), "PARITY_FAIL"); |
| 64 | assert_eq!(rc_name(-99), "UNKNOWN"); |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn shape_mismatch_is_caught_before_ffi() { |
| 69 | let mut c = vec![0f32; 4]; |
| 70 | let e = matmul_f32(2, 2, 2, &[0.0; 3], &[0.0; 4], &mut c).unwrap_err(); |
| 71 | assert!(matches!(e, OmegaGpuError::ShapeMismatch { what: "a", .. })); |
| 72 | } |
| 73 | |
| 74 | #[test] |
| 75 | fn stub_reports_unavailable() { |
| 76 | if is_native() { |
| 77 | return; |
| 78 | } |
| 79 | let mut c = vec![0f32; 4]; |
| 80 | assert_eq!( |
| 81 | matmul_f32(2, 2, 2, &[0.0; 4], &[0.0; 4], &mut c).unwrap_err(), |
| 82 | OmegaGpuError::Unavailable |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn native_refuses_bad_args_without_chip() { |
| 88 | if !is_native() { |
| 89 | return; |
| 90 | } |
| 91 | let mut c = vec![0f32; 0]; |
| 92 | match matmul_f32(0, 16, 16, &[], &[0.0; 256], &mut c).unwrap_err() { |
| 93 | OmegaGpuError::Rc { rc, name } => { |
| 94 | assert_eq!(rc, -1); |
| 95 | assert_eq!(name, "BAD_ARGS"); |
| 96 | } |
| 97 | e => panic!("unexpected {e}"), |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | #[ignore = "needs the GB10 chip: run through lanes.sh queue --heavy with a native build"] |
| 103 | fn chip_parity_f32_and_bf16() { |
| 104 | assert!( |
| 105 | is_native(), |
| 106 | "build with AIEN_OMEGA_DIR to link the chip library" |
| 107 | ); |
| 108 | let shapes = [ |
| 109 | (16, 16, 16), |
| 110 | (1, 64, 64), |
| 111 | (33, 40, 24), |
| 112 | (64, 256, 32), |
| 113 | (1, 1024, 128), |
| 114 | ]; |
| 115 | let mut seed = 12345u32; |
| 116 | for (m, k, n) in shapes { |
| 117 | let a: Vec<f32> = (0..m * k).map(|_| lcg(&mut seed)).collect(); |
| 118 | let b: Vec<f32> = (0..k * n).map(|_| lcg(&mut seed)).collect(); |
| 119 | // bf16 path: reference uses the bf16-rounded inputs |
| 120 | let ab: Vec<u16> = a.iter().map(|&x| bf16_bits(x)).collect(); |
| 121 | let bb: Vec<u16> = b.iter().map(|&x| bf16_bits(x)).collect(); |
| 122 | let ar: Vec<f32> = ab.iter().map(|&x| bf16_val(x)).collect(); |
| 123 | let br: Vec<f32> = bb.iter().map(|&x| bf16_val(x)).collect(); |
| 124 | let want = reference(m, k, n, &ar, &br); |
| 125 | let scale = (k as f32).sqrt() * 0.25 + 1e-3; // accumulation scale, inputs in [-0.5,0.5] |
| 126 | for (label, got, info) in [ |
| 127 | { |
| 128 | let mut c = vec![0f32; m * n]; |
| 129 | let i = matmul_bf16(m, k, n, &ab, &bb, &mut c).expect("bf16"); |
| 130 | ("bf16", c, i) |
| 131 | }, |
| 132 | { |
| 133 | let mut c = vec![0f32; m * n]; |
| 134 | let i = matmul_f32(m, k, n, &a, &b, &mut c).expect("f32"); |
| 135 | ("f32", c, i) |
| 136 | }, |
| 137 | ] { |
| 138 | let worst = got |
| 139 | .iter() |
| 140 | .zip(&want) |
| 141 | .map(|(g, w)| (g - w).abs()) |
| 142 | .fold(0f32, f32::max); |
| 143 | println!( |
| 144 | "CHIP_PARITY {label} {m}x{k}x{n} worst_abs={worst:e} scale={scale:e} chip_calls={} parity_verified={} chip={}", |
| 145 | info.raw.chip_calls, |
| 146 | info.raw.parity_verified, |
| 147 | info.target_chip() |
| 148 | ); |
| 149 | assert!(info.raw.parity_verified, "{label} {m}x{k}x{n}"); |
| 150 | assert!( |
| 151 | worst < 1e-5 * scale * 10.0, |
| 152 | "{label} {m}x{k}x{n} worst {worst}" |
| 153 | ); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// Shape products that overflow `usize` are refused before the C call (TOO_LARGE), in debug |
| 159 | /// and release alike. Before abi-qual, `seq_len * num_kv_heads * head_dim` wrapped to 0 in a |
| 160 | /// release build, so empty cache slices passed the length check with each dimension still |
| 161 | /// inside u32 range; only omega's own argument checks stood between that call and an |
| 162 | /// out-of-bounds read. |
| 163 | #[test] |
| 164 | fn overflowing_shapes_are_refused_before_ffi() { |
| 165 | let too_large = |e: OmegaGpuError| matches!(e, OmegaGpuError::Rc { rc: -2, .. }); |
| 166 | let q = vec![0f32; 1 << 16]; |
| 167 | let mut out = vec![0f32; 1 << 16]; |
| 168 | let e = gqa_attention_f32(&q, &[], &[], 1 << 31, 1, 1 << 17, 1 << 16, &mut out).unwrap_err(); |
| 169 | assert!(too_large(e.clone()), "{e:?}"); |
| 170 | let mut c = vec![0f32; 0]; |
| 171 | let e = matmul_f32(usize::MAX / 2 + 1, 2, 0, &[], &[], &mut c).unwrap_err(); |
| 172 | assert!(too_large(e.clone()), "{e:?}"); |
| 173 | } |