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 / omega_backend_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 | //! FB-1 cut 3b: OmegaGb10Backend against ReferenceCpuBackend on TinyLlama layer shapes. |
| 17 | //! |
| 18 | //! Host test (runs everywhere, stub-aware): in a stub build the backend must |
| 19 | //! report itself unavailable. Chip test (`#[ignore]`, run through the heavy |
| 20 | //! queue with a native build): |
| 21 | //! AIEN_OMEGA_DIR=... AIEN_PHYSICS_DIR=... cargo test -p aien-inference-abi --release \ |
| 22 | //! --test omega_backend_parity -- --ignored --nocapture |
| 23 | //! |
| 24 | //! Error metric: |chip - ref| divided by sum_i |x_i * w_i| (the accumulation |
| 25 | //! scale). Both sides see bf16-rounded inputs on the chip and f32 on the |
| 26 | //! reference, so each product carries at most ~2 * 2^-8 relative error; the bound |
| 27 | //! asserted is 8e-3 of the scale, and the typical value is far lower (random signs). |
| 28 | use aien_inference_abi::{OmegaGb10Backend, ReferenceCpuBackend, TensorBackend}; |
| 29 | |
| 30 | fn lcg(s: &mut u32) -> f32 { |
| 31 | *s = s.wrapping_mul(1664525).wrapping_add(1013904223); |
| 32 | ((*s >> 8) as f32 / (1u32 << 24) as f32) - 0.5 |
| 33 | } |
| 34 | |
| 35 | /// Max over outputs of |got - want| / sum|x*w|, for out = X * W^T. |
| 36 | fn worst_ratio( |
| 37 | got: &[f32], |
| 38 | want: &[f32], |
| 39 | x: &[f32], |
| 40 | w: &[f32], |
| 41 | m: usize, |
| 42 | k: usize, |
| 43 | n: usize, |
| 44 | ) -> f64 { |
| 45 | let mut worst = 0.0f64; |
| 46 | for r in 0..m { |
| 47 | for o in 0..n { |
| 48 | let mut scale = 0.0f64; |
| 49 | for i in 0..k { |
| 50 | scale += (x[r * k + i] as f64 * w[o * k + i] as f64).abs(); |
| 51 | } |
| 52 | let d = (got[r * n + o] as f64 - want[r * n + o] as f64).abs(); |
| 53 | worst = worst.max(d / scale.max(1e-30)); |
| 54 | } |
| 55 | } |
| 56 | worst |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn stub_backend_reports_unavailable() { |
| 61 | let b = OmegaGb10Backend::new(); |
| 62 | assert_eq!(b.is_available(), aien_omega_gpu::is_native()); |
| 63 | assert_eq!(b.native_ops().native_ops().len(), 10); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | #[ignore = "chip test: needs a native build and the GB10; run through the heavy queue"] |
| 68 | fn chip_parity_on_tinyllama_shapes() { |
| 69 | let omega = OmegaGb10Backend::new(); |
| 70 | assert!(omega.is_available(), "native build required"); |
| 71 | let reference = ReferenceCpuBackend::new(); |
| 72 | // (name, out_dim, in_dim): TinyLlama 1.1B hidden 2048, kv 256, inter 5632, vocab 32000. |
| 73 | let shapes = [ |
| 74 | ("q_proj/o_proj", 2048usize, 2048usize), |
| 75 | ("k_proj/v_proj", 256, 2048), |
| 76 | ("gate/up_proj", 5632, 2048), |
| 77 | ("down_proj", 2048, 5632), |
| 78 | ("lm_head", 32000, 2048), |
| 79 | ]; |
| 80 | let mut seed = 12345u32; |
| 81 | let mut overall = 0.0f64; |
| 82 | for (name, out_dim, in_dim) in shapes { |
| 83 | let w: Vec<f32> = (0..out_dim * in_dim).map(|_| lcg(&mut seed)).collect(); |
| 84 | // matmul_vec (and compute_logits for lm_head), batch 1 |
| 85 | let x: Vec<f32> = (0..in_dim).map(|_| lcg(&mut seed)).collect(); |
| 86 | let mut got = vec![0.0f32; out_dim]; |
| 87 | let mut want = vec![0.0f32; out_dim]; |
| 88 | if name == "lm_head" { |
| 89 | omega.compute_logits(&mut got, &x, &w, out_dim, in_dim); |
| 90 | reference.compute_logits(&mut want, &x, &w, out_dim, in_dim); |
| 91 | } else { |
| 92 | omega.matmul_vec(&mut got, &x, &w, out_dim, in_dim); |
| 93 | reference.matmul_vec(&mut want, &x, &w, out_dim, in_dim); |
| 94 | } |
| 95 | let r1 = worst_ratio(&got, &want, &x, &w, 1, in_dim, out_dim); |
| 96 | // matmul_batch, batch 7 (same resident weights, different m) |
| 97 | let batch = 7usize; |
| 98 | let xb: Vec<f32> = (0..batch * in_dim).map(|_| lcg(&mut seed)).collect(); |
| 99 | let mut gotb = vec![0.0f32; batch * out_dim]; |
| 100 | let mut wantb = vec![0.0f32; batch * out_dim]; |
| 101 | omega.matmul_batch(&mut gotb, &xb, &w, batch, in_dim, out_dim); |
| 102 | reference.matmul_batch(&mut wantb, &xb, &w, batch, in_dim, out_dim); |
| 103 | let rb = worst_ratio(&gotb, &wantb, &xb, &w, batch, in_dim, out_dim); |
| 104 | println!( |
| 105 | "PARITY {name} {out_dim}x{in_dim}: vec max_err/scale={r1:.3e} batch7 max_err/scale={rb:.3e}" |
| 106 | ); |
| 107 | assert!(r1 < 8e-3 && rb < 8e-3, "{name}: parity bound exceeded"); |
| 108 | overall = overall.max(r1).max(rb); |
| 109 | } |
| 110 | assert_eq!(omega.fallback_count(), 0, "{}", omega.last_error()); |
| 111 | assert_eq!(omega.chip_errors(), 0, "{}", omega.last_error()); |
| 112 | println!("PARITY overall worst max_err/scale = {overall:.3e}"); |
| 113 | println!("PARITY resident weights = {}", omega.resident_count()); |
| 114 | println!("PARITY {}", omega.op_report().line()); |
| 115 | } |
| 116 | |
| 117 | /// Worst of |got - want| / (1e-6 + 1e-5 * |want|) over all elements; <= 1 means inside the |
| 118 | /// omega cut-4 elementwise tolerance (1e-5 relative + 1e-6 absolute). |
| 119 | fn worst_tol_ratio(got: &[f32], want: &[f32]) -> f64 { |
| 120 | assert_eq!(got.len(), want.len()); |
| 121 | got.iter() |
| 122 | .zip(want) |
| 123 | .map(|(g, w)| (*g as f64 - *w as f64).abs() / (1e-6 + 1e-5 * (*w as f64).abs())) |
| 124 | .fold(0.0, f64::max) |
| 125 | } |
| 126 | |
| 127 | /// FB-1 cut 3c: rmsnorm, apply_rope and swiglu on the chip at TinyLlama shapes |
| 128 | /// (hidden 2048, 32 q heads, 4 kv heads, head_dim 64, intermediate 5632, eps 1e-5, |
| 129 | /// theta 10000) against ReferenceCpuBackend. Rope must be bit-exact (omega emits |
| 130 | /// FMUL/FMUL/FADD, no fused multiply-add); rmsnorm and swiglu within the tolerance |
| 131 | /// stated in omega docs/numeric/FB1_CUT4_ELEMENTWISE.md (1e-5 relative + 1e-6 absolute). |
| 132 | #[test] |
| 133 | #[ignore = "chip test: needs a native build and the GB10; run through the heavy queue"] |
| 134 | fn chip_elementwise_parity_on_tinyllama_shapes() { |
| 135 | let omega = OmegaGb10Backend::new(); |
| 136 | assert!(omega.is_available(), "native build required"); |
| 137 | let reference = ReferenceCpuBackend::new(); |
| 138 | let mut seed = 777u32; |
| 139 | |
| 140 | // rmsnorm, dim 2048 (hidden), 20 different rows of data incl. a large-magnitude one. |
| 141 | let dim = 2048usize; |
| 142 | let w: Vec<f32> = (0..dim).map(|_| lcg(&mut seed) * 4.0 + 1.0).collect(); |
| 143 | let mut rms_worst = 0.0f64; |
| 144 | for trial in 0..20 { |
| 145 | let scale = if trial == 19 { 50.0 } else { 2.0 }; |
| 146 | let x: Vec<f32> = (0..dim).map(|_| lcg(&mut seed) * scale).collect(); |
| 147 | let (mut got, mut want) = (vec![0.0f32; dim], vec![0.0f32; dim]); |
| 148 | omega.rmsnorm(&mut got, &x, &w, 1e-5); |
| 149 | reference.rmsnorm(&mut want, &x, &w, 1e-5); |
| 150 | rms_worst = rms_worst.max(worst_tol_ratio(&got, &want)); |
| 151 | } |
| 152 | println!("PARITY rmsnorm 2048 worst err/tolerance = {rms_worst:.3e} (must be <= 1)"); |
| 153 | assert!(rms_worst <= 1.0, "rmsnorm outside tolerance"); |
| 154 | |
| 155 | // rmsnorm_rows: 20 rows in one launch equal the 20 single-row references. |
| 156 | let rows = 20usize; |
| 157 | let x: Vec<f32> = (0..rows * dim).map(|_| lcg(&mut seed) * 2.0).collect(); |
| 158 | let (mut got, mut want) = (vec![0.0f32; rows * dim], vec![0.0f32; rows * dim]); |
| 159 | omega.rmsnorm_rows(&mut got, &x, &w, rows, 1e-5); |
| 160 | for r in 0..rows { |
| 161 | reference.rmsnorm( |
| 162 | &mut want[r * dim..(r + 1) * dim], |
| 163 | &x[r * dim..(r + 1) * dim], |
| 164 | &w, |
| 165 | 1e-5, |
| 166 | ); |
| 167 | } |
| 168 | let rows_worst = worst_tol_ratio(&got, &want); |
| 169 | println!("PARITY rmsnorm_rows 20x2048 worst err/tolerance = {rows_worst:.3e} (must be <= 1)"); |
| 170 | assert!(rows_worst <= 1.0, "rmsnorm_rows outside tolerance"); |
| 171 | |
| 172 | // rope, 32 q heads + 4 kv heads of 64, several positions. |
| 173 | let (hd, nq, nkv) = (64usize, 32usize, 4usize); |
| 174 | let theta = &aien_inference_abi::RopeParams::plain(10000.0); |
| 175 | let mut rope_mismatch = 0usize; |
| 176 | for pos in [0usize, 1, 2, 37, 511, 1500, 2047] { |
| 177 | let q0: Vec<f32> = (0..nq * hd).map(|_| lcg(&mut seed) * 3.0).collect(); |
| 178 | let k0: Vec<f32> = (0..nkv * hd).map(|_| lcg(&mut seed) * 3.0).collect(); |
| 179 | let (mut qg, mut kg) = (q0.clone(), k0.clone()); |
| 180 | let (mut qw, mut kw) = (q0.clone(), k0.clone()); |
| 181 | omega.apply_rope(&mut qg, &mut kg, pos, hd, nq, nkv, theta); |
| 182 | reference.apply_rope(&mut qw, &mut kw, pos, hd, nq, nkv, theta); |
| 183 | rope_mismatch += qg |
| 184 | .iter() |
| 185 | .zip(&qw) |
| 186 | .chain(kg.iter().zip(&kw)) |
| 187 | .filter(|(a, b)| a.to_bits() != b.to_bits()) |
| 188 | .count(); |
| 189 | } |
| 190 | println!("PARITY rope 32+4 heads x 64, 7 positions: bit mismatches = {rope_mismatch}"); |
| 191 | assert_eq!(rope_mismatch, 0, "rope must be bit-exact"); |
| 192 | |
| 193 | // swiglu, n = 5632 (intermediate), gate in a wide range. |
| 194 | let n = 5632usize; |
| 195 | let mut sw_worst = 0.0f64; |
| 196 | for _ in 0..10 { |
| 197 | let gate: Vec<f32> = (0..n).map(|_| lcg(&mut seed) * 16.0).collect(); |
| 198 | let up: Vec<f32> = (0..n).map(|_| lcg(&mut seed) * 4.0).collect(); |
| 199 | let (mut got, mut want) = (vec![0.0f32; n], vec![0.0f32; n]); |
| 200 | omega.swiglu(&mut got, &gate, &up); |
| 201 | reference.swiglu(&mut want, &gate, &up); |
| 202 | sw_worst = sw_worst.max(worst_tol_ratio(&got, &want)); |
| 203 | } |
| 204 | println!("PARITY swiglu 5632 worst err/tolerance = {sw_worst:.3e} (must be <= 1)"); |
| 205 | assert!(sw_worst <= 1.0, "swiglu outside tolerance"); |
| 206 | |
| 207 | assert_eq!(omega.fallback_count(), 0, "{}", omega.last_error()); |
| 208 | assert_eq!(omega.chip_errors(), 0, "{}", omega.last_error()); |
| 209 | println!("PARITY elementwise {}", omega.op_report().line()); |
| 210 | } |
| 211 | |
| 212 | /// Qwen3-4B shapes: `rmsnorm_heads` (32 q heads and 8 kv heads of 128, eps 1e-6, one launch each) |
| 213 | /// within the elementwise tolerance, and rope at head_dim 128 with theta 5000000 bit-exact, |
| 214 | /// against ReferenceCpuBackend. Same criteria as the TinyLlama elementwise test above. |
| 215 | #[test] |
| 216 | #[ignore = "chip test: needs a native build and the GB10; run through the heavy queue"] |
| 217 | fn chip_elementwise_parity_on_qwen3_shapes() { |
| 218 | let omega = OmegaGb10Backend::new(); |
| 219 | assert!(omega.is_available(), "native build required"); |
| 220 | let reference = ReferenceCpuBackend::new(); |
| 221 | let mut seed = 4242u32; |
| 222 | let (hd, nq, nkv) = (128usize, 32usize, 8usize); |
| 223 | |
| 224 | let mut heads_worst = 0.0f64; |
| 225 | for heads in [nq, nkv] { |
| 226 | let w: Vec<f32> = (0..hd).map(|_| lcg(&mut seed) * 4.0 + 1.0).collect(); |
| 227 | for trial in 0..10 { |
| 228 | let scale = if trial == 9 { 50.0 } else { 2.0 }; |
| 229 | let x: Vec<f32> = (0..heads * hd).map(|_| lcg(&mut seed) * scale).collect(); |
| 230 | let (mut got, mut want) = (x.clone(), x.clone()); |
| 231 | omega.rmsnorm_heads(&mut got, &w, hd, 1e-6); |
| 232 | reference.rmsnorm_heads(&mut want, &w, hd, 1e-6); |
| 233 | heads_worst = heads_worst.max(worst_tol_ratio(&got, &want)); |
| 234 | } |
| 235 | } |
| 236 | println!("PARITY rmsnorm_heads 32x128 and 8x128 worst err/tolerance = {heads_worst:.3e} (must be <= 1)"); |
| 237 | assert!(heads_worst <= 1.0, "rmsnorm_heads outside tolerance"); |
| 238 | |
| 239 | let theta = &aien_inference_abi::RopeParams::plain(5_000_000.0); |
| 240 | let mut rope_mismatch = 0usize; |
| 241 | for pos in [0usize, 1, 2, 37, 511, 1500, 4095] { |
| 242 | let q0: Vec<f32> = (0..nq * hd).map(|_| lcg(&mut seed) * 3.0).collect(); |
| 243 | let k0: Vec<f32> = (0..nkv * hd).map(|_| lcg(&mut seed) * 3.0).collect(); |
| 244 | let (mut qg, mut kg) = (q0.clone(), k0.clone()); |
| 245 | let (mut qw, mut kw) = (q0.clone(), k0.clone()); |
| 246 | omega.apply_rope(&mut qg, &mut kg, pos, hd, nq, nkv, theta); |
| 247 | reference.apply_rope(&mut qw, &mut kw, pos, hd, nq, nkv, theta); |
| 248 | rope_mismatch += qg |
| 249 | .iter() |
| 250 | .zip(&qw) |
| 251 | .chain(kg.iter().zip(&kw)) |
| 252 | .filter(|(a, b)| a.to_bits() != b.to_bits()) |
| 253 | .count(); |
| 254 | } |
| 255 | println!( |
| 256 | "PARITY rope 32+8 heads x 128 theta 5e6, 7 positions: bit mismatches = {rope_mismatch}" |
| 257 | ); |
| 258 | assert_eq!(rope_mismatch, 0, "rope must be bit-exact"); |
| 259 | |
| 260 | assert_eq!(omega.fallback_count(), 0, "{}", omega.last_error()); |
| 261 | assert_eq!(omega.chip_errors(), 0, "{}", omega.last_error()); |
| 262 | println!("PARITY qwen3 elementwise {}", omega.op_report().line()); |
| 263 | } |
| 264 | |
| 265 | /// Diagnostic for the cut-3c finding: elementwise launches after the matmul device is |
| 266 | /// open. Run with AIEN_DEV_FALLBACK=1 so a failure is reported instead of a strict panic. |
| 267 | #[test] |
| 268 | #[ignore = "chip diagnostic: heavy queue only"] |
| 269 | fn chip_mixed_matmul_then_elementwise() { |
| 270 | let omega = OmegaGb10Backend::new(); |
| 271 | assert!(omega.is_available(), "native build required"); |
| 272 | let mut seed = 4242u32; |
| 273 | let (k, n) = (256usize, 64usize); |
| 274 | let w: Vec<f32> = (0..n * k).map(|_| lcg(&mut seed)).collect(); |
| 275 | let x: Vec<f32> = (0..k).map(|_| lcg(&mut seed)).collect(); |
| 276 | let mut out = vec![0.0f32; n]; |
| 277 | let wn: Vec<f32> = (0..256).map(|_| lcg(&mut seed)).collect(); |
| 278 | let mut no = vec![0.0f32; 256]; |
| 279 | omega.rmsnorm(&mut no, &x, &wn, 1e-5); |
| 280 | println!( |
| 281 | "MIXED rmsnorm before matmul: chip_errors={}", |
| 282 | omega.chip_errors() |
| 283 | ); |
| 284 | omega.matmul_vec(&mut out, &x, &w, n, k); |
| 285 | println!("MIXED matmul: chip_errors={}", omega.chip_errors()); |
| 286 | omega.rmsnorm(&mut no, &x, &wn, 1e-5); |
| 287 | println!( |
| 288 | "MIXED rmsnorm after matmul: chip_errors={} last_error={}", |
| 289 | omega.chip_errors(), |
| 290 | omega.last_error() |
| 291 | ); |
| 292 | } |
| 293 | |
| 294 | fn worst_attn_ratio(got: &[f32], want: &[f32]) -> f64 { |
| 295 | // omega tests/gpu_attention_test.c tolerances (FB-1 cut 5): rel 2e-4, abs 2e-5. |
| 296 | assert_eq!(got.len(), want.len()); |
| 297 | got.iter() |
| 298 | .zip(want) |
| 299 | .map(|(g, w)| (*g as f64 - *w as f64).abs() / (2e-5 + 2e-4 * (*w as f64).abs())) |
| 300 | .fold(0.0, f64::max) |
| 301 | } |
| 302 | |
| 303 | /// Fill `n_tokens` tokens of `layer` into a fresh TinyLlama-shaped pool (block_size 16), |
| 304 | /// blocks used in a scrambled order so the block table is not the identity. |
| 305 | fn filled_pool( |
| 306 | dtype: aien_kv_cache::KvDType, |
| 307 | n_tokens: usize, |
| 308 | layer: usize, |
| 309 | seed: &mut u32, |
| 310 | ) -> (aien_kv_cache::UnifiedKvTensorPool, Vec<usize>) { |
| 311 | let (num_blocks, bs) = (160usize, 16usize); |
| 312 | let cfg = aien_kv_cache::KvPoolConfig::for_tinyllama(num_blocks, bs, dtype); |
| 313 | let mut pool = aien_kv_cache::UnifiedKvTensorPool::allocate(cfg).expect("pool"); |
| 314 | let need = n_tokens.div_ceil(bs); |
| 315 | let blocks: Vec<usize> = (0..need).map(|i| (i * 37 + 11) % num_blocks).collect(); |
| 316 | let kv_dim = 4 * 64; |
| 317 | for t in 0..n_tokens { |
| 318 | let k: Vec<f32> = (0..kv_dim).map(|_| lcg(seed) * 2.0).collect(); |
| 319 | let v: Vec<f32> = (0..kv_dim).map(|_| lcg(seed) * 2.0).collect(); |
| 320 | pool.write_token_kv(blocks[t / bs], layer, t % bs, &k, &v); |
| 321 | } |
| 322 | (pool, blocks) |
| 323 | } |
| 324 | |
| 325 | /// FB-1 cut 3d: the three attention ops on the chip at TinyLlama shapes (32 q heads, |
| 326 | /// 4 kv heads, head_dim 64) against ReferenceCpuBackend: contiguous gqa, paged over an |
| 327 | /// Fp32 pool (the model's pool; host gather + f32 kernel), paged over a bf16 pool (omega's |
| 328 | /// paged kernel), and the batch op with ragged rows, a negative entry and an empty row. |
| 329 | #[test] |
| 330 | #[ignore = "chip test: needs a native build and the GB10; run through the heavy queue"] |
| 331 | fn chip_attention_parity_on_tinyllama_shapes() { |
| 332 | let omega = OmegaGb10Backend::new(); |
| 333 | assert!(omega.is_available(), "native build required"); |
| 334 | let reference = ReferenceCpuBackend::new(); |
| 335 | let mut seed = 4242u32; |
| 336 | let (nq, nkv, hd) = (32usize, 4usize, 64usize); |
| 337 | |
| 338 | let mut gqa_worst = 0.0f64; |
| 339 | for seq in [1usize, 7, 64, 300, 2048] { |
| 340 | let q: Vec<f32> = (0..nq * hd).map(|_| lcg(&mut seed) * 2.0).collect(); |
| 341 | let k: Vec<f32> = (0..seq * nkv * hd).map(|_| lcg(&mut seed) * 2.0).collect(); |
| 342 | let v: Vec<f32> = (0..seq * nkv * hd).map(|_| lcg(&mut seed) * 2.0).collect(); |
| 343 | let (mut got, mut want) = (vec![0.0f32; nq * hd], vec![0.0f32; nq * hd]); |
| 344 | omega.gqa_attention(&mut got, &q, &k, &v, seq, nq, nkv, hd); |
| 345 | reference.gqa_attention(&mut want, &q, &k, &v, seq, nq, nkv, hd); |
| 346 | let r = worst_attn_ratio(&got, &want); |
| 347 | let at = got |
| 348 | .iter() |
| 349 | .zip(&want) |
| 350 | .map(|(g, w)| (*g as f64 - *w as f64).abs()) |
| 351 | .enumerate() |
| 352 | .fold( |
| 353 | (0usize, 0.0f64), |
| 354 | |m, (i, d)| if d > m.1 { (i, d) } else { m }, |
| 355 | ); |
| 356 | println!( |
| 357 | "PARITY gqa seq={seq} err/tol={r:.3e} worst at {} got={} want={}", |
| 358 | at.0, got[at.0], want[at.0] |
| 359 | ); |
| 360 | gqa_worst = gqa_worst.max(r); |
| 361 | } |
| 362 | // omega#406: the prompt read's rows path. 40 rows after a 300-token base is 2 launches at |
| 363 | // 32 q heads; every row against the reference at its own causal length. |
| 364 | { |
| 365 | let (base, rows) = (300usize, 40usize); |
| 366 | let q: Vec<f32> = (0..rows * nq * hd).map(|_| lcg(&mut seed) * 2.0).collect(); |
| 367 | let k: Vec<f32> = (0..(base + rows) * nkv * hd) |
| 368 | .map(|_| lcg(&mut seed) * 2.0) |
| 369 | .collect(); |
| 370 | let v: Vec<f32> = (0..(base + rows) * nkv * hd) |
| 371 | .map(|_| lcg(&mut seed) * 2.0) |
| 372 | .collect(); |
| 373 | let (mut got, mut want) = (vec![0.0f32; rows * nq * hd], vec![0.0f32; rows * nq * hd]); |
| 374 | omega.gqa_attention_rows(&mut got, &q, &k, &v, base, rows, nq, nkv, hd); |
| 375 | reference.gqa_attention_rows(&mut want, &q, &k, &v, base, rows, nq, nkv, hd); |
| 376 | let r = worst_attn_ratio(&got, &want); |
| 377 | println!("PARITY gqa_rows base={base} rows={rows} err/tol={r:.3e}"); |
| 378 | gqa_worst = gqa_worst.max(r); |
| 379 | let mut one = vec![0.0f32; nq * hd]; |
| 380 | omega.gqa_attention(&mut one, &q[..nq * hd], &k, &v, base + 1, nq, nkv, hd); |
| 381 | assert_eq!( |
| 382 | &got[..nq * hd], |
| 383 | &one[..], |
| 384 | "row 0 of the rows path == the single-row call" |
| 385 | ); |
| 386 | } |
| 387 | println!( |
| 388 | "PARITY gqa_attention seq 1..2048 worst err/tolerance = {gqa_worst:.3e} (must be <= 1)" |
| 389 | ); |
| 390 | assert!(gqa_worst <= 1.0, "gqa_attention outside tolerance"); |
| 391 | |
| 392 | for dtype in [aien_kv_cache::KvDType::Fp32, aien_kv_cache::KvDType::Bf16] { |
| 393 | let mut worst = 0.0f64; |
| 394 | for (ctx, layer) in [(1usize, 0usize), (17, 3), (250, 21), (1000, 9)] { |
| 395 | let (pool, blocks) = filled_pool(dtype, ctx, layer, &mut seed); |
| 396 | let q: Vec<f32> = (0..nq * hd).map(|_| lcg(&mut seed) * 2.0).collect(); |
| 397 | let (mut got, mut want) = (vec![0.0f32; nq * hd], vec![0.0f32; nq * hd]); |
| 398 | omega.paged_attention(&mut got, &q, &pool, &blocks, ctx, layer, nq, nkv, hd); |
| 399 | reference.paged_attention(&mut want, &q, &pool, &blocks, ctx, layer, nq, nkv, hd); |
| 400 | worst = worst.max(worst_attn_ratio(&got, &want)); |
| 401 | } |
| 402 | println!( |
| 403 | "PARITY paged_attention {dtype:?} worst err/tolerance = {worst:.3e} (must be <= 1)" |
| 404 | ); |
| 405 | assert!(worst <= 1.0, "paged_attention {dtype:?} outside tolerance"); |
| 406 | |
| 407 | // batch: 3 rows (ctx 40, 0 = empty, 90 with a trailing -1), max_blocks 8. |
| 408 | let (pool, blocks) = filled_pool(dtype, 90, 5, &mut seed); |
| 409 | let mb = 8usize; |
| 410 | let mut tables = vec![-1i32; 3 * mb]; |
| 411 | for (i, b) in blocks.iter().take(3).enumerate() { |
| 412 | tables[i] = *b as i32; |
| 413 | } |
| 414 | for (i, b) in blocks.iter().enumerate() { |
| 415 | tables[2 * mb + i] = *b as i32; |
| 416 | } |
| 417 | let lens = [40i32, 0, 90]; |
| 418 | let q: Vec<f32> = (0..3 * nq * hd).map(|_| lcg(&mut seed) * 2.0).collect(); |
| 419 | let (mut got, mut want) = (vec![0.0f32; 3 * nq * hd], vec![0.0f32; 3 * nq * hd]); |
| 420 | omega.paged_attention_batch(&mut got, &q, &pool, &tables, &lens, mb, 3, 5, nq, nkv, hd); |
| 421 | reference |
| 422 | .paged_attention_batch(&mut want, &q, &pool, &tables, &lens, mb, 3, 5, nq, nkv, hd); |
| 423 | let bw = worst_attn_ratio(&got, &want); |
| 424 | println!( |
| 425 | "PARITY paged_attention_batch {dtype:?} worst err/tolerance = {bw:.3e} (must be <= 1)" |
| 426 | ); |
| 427 | assert!( |
| 428 | bw <= 1.0, |
| 429 | "paged_attention_batch {dtype:?} outside tolerance" |
| 430 | ); |
| 431 | } |
| 432 | |
| 433 | assert_eq!(omega.fallback_count(), 0, "{}", omega.last_error()); |
| 434 | assert_eq!(omega.chip_errors(), 0, "{}", omega.last_error()); |
| 435 | println!("PARITY attention {}", omega.op_report().line()); |
| 436 | } |
| 437 | |
| 438 | /// Negative control on the chip build: an attention geometry the native engine does not |
| 439 | /// support (head_dim 32; omega refuses head_dim != 64 with OMEGA_GPU_ATTN_TOO_LARGE in |
| 440 | /// src/omega_gpu_attention_api.c before any launch) goes through the native library, is |
| 441 | /// refused there, and in a strict process the refusal is fatal: no CPU answer appears behind |
| 442 | /// the GPU backend. In a dev process it is a counted fallback. This is a library refusal, not |
| 443 | /// an injected device fault; a device fault takes the same `fail` -> `reference_path` route. |
| 444 | #[test] |
| 445 | #[ignore = "chip test: needs a native build and the GB10; run through the heavy queue"] |
| 446 | fn chip_unsupported_attention_is_refused() { |
| 447 | let omega = OmegaGb10Backend::new(); |
| 448 | assert!(omega.is_available(), "native build required"); |
| 449 | let (nq, nkv, hd, seq) = (4usize, 2usize, 32usize, 3usize); |
| 450 | let mut seed = 99u32; |
| 451 | let q: Vec<f32> = (0..nq * hd).map(|_| lcg(&mut seed)).collect(); |
| 452 | let k: Vec<f32> = (0..seq * nkv * hd).map(|_| lcg(&mut seed)).collect(); |
| 453 | let v: Vec<f32> = (0..seq * nkv * hd).map(|_| lcg(&mut seed)).collect(); |
| 454 | let mut out = vec![0.0f32; nq * hd]; |
| 455 | let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { |
| 456 | omega.gqa_attention(&mut out, &q, &k, &v, seq, nq, nkv, hd) |
| 457 | })); |
| 458 | println!( |
| 459 | "NEGATIVE unsupported head_dim={hd}: chip_errors={} fallback_count={} last_error={:?} strict={}", |
| 460 | omega.chip_errors(), |
| 461 | omega.fallback_count(), |
| 462 | omega.last_error(), |
| 463 | aien_inference_abi::strict::production_strict() |
| 464 | ); |
| 465 | assert_eq!(omega.chip_errors(), 1, "the native library must refuse"); |
| 466 | assert_eq!(omega.fallback_count(), 1); |
| 467 | if aien_inference_abi::strict::production_strict() { |
| 468 | let err = r.expect_err("strict: an unsupported native op must be fatal"); |
| 469 | let msg = err |
| 470 | .downcast_ref::<String>() |
| 471 | .cloned() |
| 472 | .or_else(|| err.downcast_ref::<&str>().map(|s| s.to_string())) |
| 473 | .unwrap_or_default(); |
| 474 | assert!( |
| 475 | msg.contains(aien_inference_abi::strict::STRICT_VIOLATION_PREFIX) |
| 476 | && msg.contains("gqa_attention"), |
| 477 | "{msg}" |
| 478 | ); |
| 479 | println!("NEGATIVE strict refusal: {msg}"); |
| 480 | } else { |
| 481 | r.expect("dev: counted fallback, run continues"); |
| 482 | } |
| 483 | } |