leleka-cpu-sys holds the C kernels (one translation unit per ISA level, built by build.rs with cc) behind a checked FFI; it is the only unsafe code of the CPU runtime and opts out of the workspace unsafe_code lint with a recorded exception. leleka-cpu-kernels does safe run-time dispatch with a LELEKA_CPU_ISA override that fails rather than silently changing level. Ports the int8xint8 and int4xint8 dot products from Colibri c/idot.h at scalar, AVX2 and AVX-VNNI. Tests compare every level with the C scalar kernel and an independent i64 oracle (13 tests; Windows MSVC and Linux GCC 14.2). Activations must be in [-127, 127]: -128 is outside the contract and a test pins that. No performance claim: nothing was timed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
182 lines
5.5 KiB
Rust
182 lines
5.5 KiB
Rust
//! Parity of every supported kernel level against two oracles: the portable C scalar kernel and an
|
|
//! independent naive i64 computation written in this test (an oracle must not be built from the
|
|
//! code it checks). Inputs follow the kernel contract: activations in [-127, 127], weights any int8.
|
|
|
|
use leleka_cpu_kernels::{Dispatch, Isa, KernelError, MAX_DOT_LEN};
|
|
|
|
struct Lcg(u64);
|
|
|
|
impl Lcg {
|
|
fn next(&mut self) -> u64 {
|
|
self.0 = self
|
|
.0
|
|
.wrapping_mul(6364136223846793005)
|
|
.wrapping_add(1442695040888963407);
|
|
self.0 >> 33
|
|
}
|
|
fn weight(&mut self) -> i8 {
|
|
(self.next() % 256) as i64 as i8 // full range, including -128
|
|
}
|
|
fn activation(&mut self) -> i8 {
|
|
((self.next() % 255) as i64 - 127) as i8 // [-127, 127]
|
|
}
|
|
}
|
|
|
|
fn naive_i8(w: &[i8], x: &[i8]) -> i64 {
|
|
w.iter()
|
|
.zip(x)
|
|
.map(|(a, b)| i64::from(*a) * i64::from(*b))
|
|
.sum()
|
|
}
|
|
|
|
fn naive_i4(w4: &[u8], x: &[i8]) -> i64 {
|
|
(0..x.len())
|
|
.map(|i| {
|
|
let byte = w4[i / 2];
|
|
let nibble = if i % 2 == 0 { byte & 0xF } else { byte >> 4 };
|
|
(i64::from(nibble) - 8) * i64::from(x[i])
|
|
})
|
|
.sum()
|
|
}
|
|
|
|
fn levels() -> Vec<Dispatch> {
|
|
Isa::ALL
|
|
.iter()
|
|
.filter_map(|isa| Dispatch::with_isa(*isa).ok())
|
|
.collect()
|
|
}
|
|
|
|
fn lengths() -> Vec<usize> {
|
|
let mut lens: Vec<usize> = (0..=300).collect();
|
|
lens.extend([511, 512, 513, 1023, 1024, 4096, 4097, 65_536, MAX_DOT_LEN]);
|
|
lens
|
|
}
|
|
|
|
#[test]
|
|
fn the_scalar_level_is_present_and_the_host_levels_are_reported() {
|
|
let found = levels();
|
|
assert_eq!(found[0].isa(), Isa::Scalar);
|
|
// Informational: a line a human can read in `cargo test -- --nocapture`.
|
|
eprintln!(
|
|
"levels under test: {:?}",
|
|
found.iter().map(|d| d.isa().name()).collect::<Vec<_>>()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn int8_dot_is_exact_and_equal_across_levels() {
|
|
let mut rng = Lcg(0x5EED_0001);
|
|
for len in lengths() {
|
|
let w: Vec<i8> = (0..len).map(|_| rng.weight()).collect();
|
|
let x: Vec<i8> = (0..len).map(|_| rng.activation()).collect();
|
|
let expected = naive_i8(&w, &x);
|
|
for d in levels() {
|
|
assert_eq!(
|
|
i64::from(d.dot_i8i8(&w, &x).unwrap()),
|
|
expected,
|
|
"level {} len {len}",
|
|
d.isa()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn int4_dot_is_exact_and_equal_across_levels() {
|
|
let mut rng = Lcg(0x5EED_0002);
|
|
for len in lengths() {
|
|
let w4: Vec<u8> = (0..len.div_ceil(2))
|
|
.map(|_| (rng.next() % 256) as u8)
|
|
.collect();
|
|
let x: Vec<i8> = (0..len).map(|_| rng.activation()).collect();
|
|
let expected = naive_i4(&w4, &x);
|
|
for d in levels() {
|
|
assert_eq!(
|
|
i64::from(d.dot_i4i8(&w4, &x).unwrap()),
|
|
expected,
|
|
"level {} len {len}",
|
|
d.isa()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn extreme_values_at_the_maximum_length_do_not_overflow() {
|
|
// The worst case of the documented bound: weights -128, activations 127 (and 127 * 127).
|
|
let n = MAX_DOT_LEN;
|
|
for (wv, xv) in [(-128i8, 127i8), (127, 127), (127, -127), (-128, -127)] {
|
|
let w = vec![wv; n];
|
|
let x = vec![xv; n];
|
|
let expected = naive_i8(&w, &x);
|
|
assert!(expected.abs() < i64::from(i32::MAX));
|
|
for d in levels() {
|
|
assert_eq!(
|
|
i64::from(d.dot_i8i8(&w, &x).unwrap()),
|
|
expected,
|
|
"level {} w={wv} x={xv}",
|
|
d.isa()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn unaligned_slices_give_the_same_answer() {
|
|
let mut rng = Lcg(0x5EED_0003);
|
|
let w: Vec<i8> = (0..600).map(|_| rng.weight()).collect();
|
|
let x: Vec<i8> = (0..600).map(|_| rng.activation()).collect();
|
|
for offset in 1..=7 {
|
|
let (ws, xs) = (&w[offset..offset + 513], &x[offset..offset + 513]);
|
|
let expected = naive_i8(ws, xs);
|
|
for d in levels() {
|
|
assert_eq!(
|
|
i64::from(d.dot_i8i8(ws, xs).unwrap()),
|
|
expected,
|
|
"level {} offset {offset}",
|
|
d.isa()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Documents the contract limit instead of hiding it: an activation of -128 under a negative
|
|
/// weight cannot be negated in int8, so the SIMD levels disagree with the scalar level there.
|
|
/// If this test starts failing, the kernels changed and the contract in `leleka_cpu.h` must be
|
|
/// revisited.
|
|
#[test]
|
|
fn activation_minus_128_is_outside_the_contract_and_simd_levels_differ() {
|
|
let w = vec![-1i8; 64];
|
|
let x = vec![-128i8; 64];
|
|
let scalar = Dispatch::with_isa(Isa::Scalar)
|
|
.unwrap()
|
|
.dot_i8i8(&w, &x)
|
|
.unwrap();
|
|
assert_eq!(i64::from(scalar), naive_i8(&w, &x));
|
|
assert_eq!(scalar, 128 * 64);
|
|
for d in levels().into_iter().filter(|d| d.isa() != Isa::Scalar) {
|
|
let got = d.dot_i8i8(&w, &x).unwrap();
|
|
assert_ne!(
|
|
got,
|
|
scalar,
|
|
"level {} unexpectedly agrees outside the contract",
|
|
d.isa()
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn bad_operands_are_refused_at_every_level() {
|
|
for d in levels() {
|
|
assert_eq!(d.dot_i8i8(&[1, 2], &[1]), Err(KernelError::LengthMismatch));
|
|
assert_eq!(
|
|
d.dot_i4i8(&[0], &[1, 2, 3]),
|
|
Err(KernelError::LengthMismatch)
|
|
);
|
|
let big = vec![0i8; MAX_DOT_LEN + 1];
|
|
assert_eq!(d.dot_i8i8(&big, &big), Err(KernelError::TooLong));
|
|
assert_eq!(d.dot_i8i8(&[], &[]), Ok(0));
|
|
assert_eq!(d.dot_i4i8(&[], &[]), Ok(0));
|
|
}
|
|
}
|