Files
leleka/crates/cpu-sys/tests/rust_vs_c.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 c1444d98ac perf: run the CPU kernels from Rust std::arch instead of C (STRA-214)
The MXFP4, Q8_0, int8 and int4 kernels, the Q8_0 quantizer and the half conversions are now Rust
(cpu-sys/src/simd.rs), written operation for operation after the C versions. The C code stays only behind
the test-only feature c-reference, and tests/rust_vs_c.rs checks bit-identical results at the portable,
AVX2 and AVX-VNNI levels. A runtime build compiles no C.

Paired decode on gpt-oss-20b (40 tokens, 10 threads, 5 alternating rounds): median 98 ms per token for
both builds, identical tokens and first-step log-probabilities.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 18:32:31 +02:00

273 lines
9.4 KiB
Rust

//! Differential test: the Rust `std::arch` kernels in `simd` must give bit-identical results to the C
//! kernels they replace, for every instruction-set level this machine runs. Needs the c-reference`n//! feature: cargo test -p leleka-cpu-sys --features c-reference --test rust_vs_c.
use leleka_cpu_sys::avx2_available;
use leleka_cpu_sys::c_reference as c;
use leleka_cpu_sys::simd as r;
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
self.0
}
fn i8_in_contract(&mut self) -> i8 {
((self.next() % 255) as i64 - 127) as i8
}
fn f32_unit(&mut self) -> f32 {
((self.next() >> 40) as f32 / (1u64 << 24) as f32 - 0.5) * 20.0
}
}
fn mx_blocks(rng: &mut Rng, nb: usize) -> Vec<u8> {
let mut v = Vec::new();
for _ in 0..nb {
v.push(110 + (rng.next() % 30) as u8);
for _ in 0..16 {
v.push(rng.next() as u8);
}
}
v
}
fn q8_blocks(rng: &mut Rng, nb: usize) -> Vec<u8> {
let mut v = Vec::new();
for _ in 0..nb {
let scale = rng.f32_unit().abs() * 0.01 + 1e-4;
v.extend_from_slice(&r::f32_to_f16(scale).to_le_bytes());
for _ in 0..32 {
v.push(rng.i8_in_contract() as u8);
}
}
v
}
const BLOCK_COUNTS: [usize; 9] = [0, 1, 2, 3, 4, 5, 7, 8, 90];
const LENGTHS: [usize; 12] = [0, 1, 5, 15, 16, 31, 32, 33, 63, 64, 65, 2880];
#[test]
fn half_conversions_cover_every_pattern() {
for h in 0..=u16::MAX {
let a = r::f16_to_f32(h);
let exp = (h >> 10) & 0x1F;
let man = h & 0x3FF;
if exp == 31 && man != 0 {
assert!(a.is_nan());
continue;
}
// every finite half converts to f32 exactly and back to the same half
assert_eq!(r::f32_to_f16(a), h, "round trip of {h:#06x}");
}
let mut rng = Rng(0x1234_5678_9abc_def1);
for _ in 0..2_000_000 {
let f = f32::from_bits(rng.next() as u32);
if f.is_nan() {
continue;
}
assert_eq!(
r::f32_to_f16(f),
c::f32_to_f16_bits_scalar(f),
"bits {:#010x}",
f.to_bits()
);
}
// sweep the subnormal and overflow edges densely
for bits in (0x3000_0000u32..0x3880_0000)
.step_by(997)
.chain((0x4780_0000u32..0x4800_0000).step_by(331))
{
let f = f32::from_bits(bits);
assert_eq!(r::f32_to_f16(f), c::f32_to_f16_bits_scalar(f));
}
}
#[test]
fn portable_functions_match_c() {
let mut rng = Rng(42);
for &n in &LENGTHS {
let w: Vec<i8> = (0..n).map(|_| rng.i8_in_contract()).collect();
let x: Vec<i8> = (0..n).map(|_| rng.i8_in_contract()).collect();
assert_eq!(
r::dot_i8i8_scalar(&w, &x),
c::dot_i8i8_scalar(&w, &x).unwrap()
);
let w4: Vec<u8> = (0..n.div_ceil(2)).map(|_| rng.next() as u8).collect();
assert_eq!(
r::dot_i4i8_scalar(&w4, &x),
c::dot_i4i8_scalar(&w4, &x).unwrap()
);
}
for &nb in &BLOCK_COUNTS {
let (xm, ym) = (mx_blocks(&mut rng, nb), q8_blocks(&mut rng, nb));
assert_eq!(
r::dot_mxfp4_q8_0_scalar(&xm, &ym).to_bits(),
c::dot_mxfp4_q8_0_scalar(&xm, &ym).unwrap().to_bits()
);
let (mut a, mut b) = (vec![0; nb], vec![0; nb]);
r::mxfp4_q8_0_isums_scalar(&xm, &ym, &mut a);
c::mxfp4_q8_0_isums_scalar(&xm, &ym, &mut b).unwrap();
assert_eq!(a, b);
let xq = q8_blocks(&mut rng, nb);
assert_eq!(
r::dot_q8_0_q8_0_scalar(&xq, &ym).to_bits(),
c::dot_q8_0_q8_0_scalar(&xq, &ym).unwrap().to_bits()
);
r::q8_0_q8_0_isums_scalar(&xq, &ym, &mut a);
c::q8_0_q8_0_isums_scalar(&xq, &ym, &mut b).unwrap();
assert_eq!(a, b);
let (ys, yq) = split_q8(&ym);
assert_eq!(
r::dot_mxfp4_q8r_scalar(&xm, &ys, &yq).to_bits(),
c::dot_mxfp4_q8r_scalar(&xm, &ys, &yq).unwrap().to_bits()
);
let floats: Vec<f32> = (0..nb * 32).map(|_| rng.f32_unit()).collect();
let (mut qa, mut qb) = (vec![0u8; nb * 34], vec![0u8; nb * 34]);
r::quantize_q8_0_scalar(&floats, &mut qa);
c::quantize_q8_0_scalar(&floats, &mut qb).unwrap();
assert_eq!(qa, qb);
}
}
fn split_q8(y: &[u8]) -> (Vec<f32>, Vec<i8>) {
let mut ys = Vec::new();
let mut yq = Vec::new();
for b in y.as_chunks::<34>().0 {
ys.push(r::f16_to_f32(u16::from_le_bytes([b[0], b[1]])));
yq.extend(b[2..].iter().map(|v| *v as i8));
}
(ys, yq)
}
#[cfg(target_arch = "x86_64")]
#[test]
fn avx2_kernels_match_c_bit_for_bit() {
if !avx2_available() {
eprintln!("skipped: no AVX2");
return;
}
let mut rng = Rng(7);
for &n in &LENGTHS {
let w: Vec<i8> = (0..n).map(|_| rng.i8_in_contract()).collect();
let x: Vec<i8> = (0..n).map(|_| rng.i8_in_contract()).collect();
let w4: Vec<u8> = (0..n.div_ceil(2)).map(|_| rng.next() as u8).collect();
// SAFETY: AVX2 verified above; slice lengths are consistent.
unsafe {
assert_eq!(
r::x86::dot_i8i8_avx2(&w, &x),
c::dot_i8i8_avx2(&w, &x).unwrap()
);
assert_eq!(
r::x86::dot_i4i8_avx2(&w4, &x),
c::dot_i4i8_avx2(&w4, &x).unwrap()
);
}
}
for &nb in &BLOCK_COUNTS {
let (xm, ym, xq) = (
mx_blocks(&mut rng, nb),
q8_blocks(&mut rng, nb),
q8_blocks(&mut rng, nb),
);
let (ys, yq) = split_q8(&ym);
let (mut a, mut b) = (vec![0; nb], vec![0; nb]);
let floats: Vec<f32> = (0..nb * 32).map(|_| rng.f32_unit()).collect();
let (mut qa, mut qb) = (vec![0u8; nb * 34], vec![0u8; nb * 34]);
// SAFETY: AVX2, FMA and F16C verified above; lengths are consistent.
unsafe {
assert_eq!(
r::x86::dot_mxfp4_q8_0_avx2(&xm, &ym).to_bits(),
c::dot_mxfp4_q8_0_avx2(&xm, &ym).unwrap().to_bits()
);
r::x86::mxfp4_q8_0_isums_avx2(&xm, &ym, &mut a);
c::mxfp4_q8_0_isums_avx2(&xm, &ym, &mut b).unwrap();
assert_eq!(a, b);
assert_eq!(
r::x86::dot_mxfp4_q8r4_avx2(&xm, &ys, &yq).to_bits(),
c::dot_mxfp4_q8r4_avx2(&xm, &ys, &yq).unwrap().to_bits()
);
assert_eq!(
r::x86::dot_q8_0_q8_0_avx2(&xq, &ym).to_bits(),
c::dot_q8_0_q8_0_avx2(&xq, &ym).unwrap().to_bits()
);
r::x86::q8_0_q8_0_isums_avx2(&xq, &ym, &mut a);
c::q8_0_q8_0_isums_avx2(&xq, &ym, &mut b).unwrap();
assert_eq!(a, b);
r::x86::quantize_q8_0_avx2(&floats, &mut qa);
c::quantize_q8_0_avx2(&floats, &mut qb).unwrap();
assert_eq!(qa, qb);
}
// the SIMD quantizer must also equal the portable one
let mut qs = vec![0u8; nb * 34];
r::quantize_q8_0_scalar(&floats, &mut qs);
assert_eq!(qa, qs);
}
for _ in 0..20_000 {
let f = f32::from_bits(rng.next() as u32);
if f.is_nan() {
continue;
}
// SAFETY: F16C verified above.
let hw = unsafe { r::x86::f32_to_f16_f16c(f) };
assert_eq!(hw, r::f32_to_f16(f), "bits {:#010x}", f.to_bits());
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn avxvnni_kernels_match_c_bit_for_bit() {
if !c::avxvnni_available() {
eprintln!("skipped: no AVX-VNNI");
return;
}
let mut rng = Rng(99);
for &n in &LENGTHS {
let w: Vec<i8> = (0..n).map(|_| rng.i8_in_contract()).collect();
let x: Vec<i8> = (0..n).map(|_| rng.i8_in_contract()).collect();
let w4: Vec<u8> = (0..n.div_ceil(2)).map(|_| rng.next() as u8).collect();
// SAFETY: AVX2 and AVX-VNNI verified above; lengths are consistent.
unsafe {
assert_eq!(
r::x86::dot_i8i8_avxvnni(&w, &x),
c::dot_i8i8_avxvnni(&w, &x).unwrap()
);
assert_eq!(
r::x86::dot_i4i8_avxvnni(&w4, &x),
c::dot_i4i8_avxvnni(&w4, &x).unwrap()
);
}
}
for &nb in &BLOCK_COUNTS {
let (xm, ym, xq) = (
mx_blocks(&mut rng, nb),
q8_blocks(&mut rng, nb),
q8_blocks(&mut rng, nb),
);
let (ys, yq) = split_q8(&ym);
let (mut a, mut b) = (vec![0; nb], vec![0; nb]);
// SAFETY: AVX2, FMA, F16C and AVX-VNNI verified above; lengths are consistent.
unsafe {
assert_eq!(
r::x86::dot_mxfp4_q8_0_avxvnni(&xm, &ym).to_bits(),
c::dot_mxfp4_q8_0_avxvnni(&xm, &ym).unwrap().to_bits()
);
r::x86::mxfp4_q8_0_isums_avxvnni(&xm, &ym, &mut a);
c::mxfp4_q8_0_isums_avxvnni(&xm, &ym, &mut b).unwrap();
assert_eq!(a, b);
assert_eq!(
r::x86::dot_mxfp4_q8r4_avxvnni(&xm, &ys, &yq).to_bits(),
c::dot_mxfp4_q8r4_avxvnni(&xm, &ys, &yq).unwrap().to_bits()
);
assert_eq!(
r::x86::dot_q8_0_q8_0_avxvnni(&xq, &ym).to_bits(),
c::dot_q8_0_q8_0_avxvnni(&xq, &ym).unwrap().to_bits()
);
r::x86::q8_0_q8_0_isums_avxvnni(&xq, &ym, &mut a);
c::q8_0_q8_0_isums_avxvnni(&xq, &ym, &mut b).unwrap();
assert_eq!(a, b);
}
}
}