Files
leleka/crates/igpu/src/tests.rs
T

102 lines
3.5 KiB
Rust

//! Unit tests of this crate (moved out of `lib.rs` by STRA-338; the test ids are unchanged).
use super::*;
#[test]
fn q8_0_repacking_separates_quants_and_half_scales() {
let mut blocks = Vec::new();
for b in 0..3u8 {
blocks.extend_from_slice(&(0x3C00u16 + u16::from(b)).to_le_bytes());
blocks.extend((0..32u8).map(|i| i.wrapping_mul(7).wrapping_add(b)));
}
let (quants, scales) = repack_q8_0(&blocks);
assert_eq!(quants.len(), 24);
assert_eq!(quants[0], u32::from_le_bytes([0, 7, 14, 21]));
assert_eq!(quants[8], u32::from_le_bytes([1, 8, 15, 22]));
assert_eq!(scales, vec![0x3C00 | (0x3C01 << 16), 0x3C02]);
}
#[test]
fn mxfp4_repacking_separates_nibbles_and_exponents() {
let mut blocks = Vec::new();
for b in 0..5u8 {
blocks.push(120 + b);
blocks.extend((0..16u8).map(|i| i + 16 * b));
}
let (nibbles, exponents) = repack_mxfp4(&blocks);
assert_eq!(nibbles.len(), 20);
assert_eq!(nibbles[0], u32::from_le_bytes([0, 1, 2, 3]));
assert_eq!(nibbles[4], u32::from_le_bytes([16, 17, 18, 19]));
assert_eq!(exponents, vec![u32::from_le_bytes([120, 121, 122, 123]), 124]);
}
#[test]
fn activations_get_float_scales() {
let mut blocks = Vec::new();
blocks.extend_from_slice(&0x4000u16.to_le_bytes()); // 2.0
blocks.extend(0..32u8);
let (quants, scales) = repack_activations(&blocks);
assert_eq!(scales, vec![2.0]);
assert_eq!(quants.len(), 8);
}
fn xorshift_bytes(seed: u64) -> impl FnMut() -> u8 {
let mut seed = seed;
move || {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
seed as u8
}
}
/// The straightforward Q8_0 definition: the quants as words, the half scales packed in pairs.
fn plain_q8_0(q8: &[u8]) -> (Vec<u32>, Vec<u32>) {
let mut want_q = Vec::new();
let mut halves = Vec::new();
for b in q8.as_chunks::<Q8_0_BYTES>().0 {
halves.push(u16::from_le_bytes([b[0], b[1]]));
want_q.extend(le_words(&b[2..]));
}
let want_s: Vec<u32> = halves
.chunks(2)
.map(|p| u32::from(p[0]) | (u32::from(*p.get(1).unwrap_or(&0)) << 16))
.collect();
(want_q, want_s)
}
/// The straightforward MXFP4 definition: the nibbles as words, the exponents packed as words.
fn plain_mxfp4(mx: &[u8]) -> (Vec<u32>, Vec<u32>) {
let mut want_n = Vec::new();
let mut want_e = Vec::new();
for b in mx.as_chunks::<MXFP4_BYTES>().0 {
want_e.push(b[0]);
want_n.extend(le_words(&b[1..]));
}
(want_n, le_words(&want_e))
}
/// The straightforward definitions, to check the threaded repackers against on inputs large enough to be
/// split (ragged sizes included).
#[test]
fn threaded_repacking_equals_the_plain_definition() {
let mut next = xorshift_bytes(0x1234_5678_9ABC_DEF1u64);
for n in [20_000usize, 40_001, 65_536 + 3] {
let q8: Vec<u8> = (0..n * Q8_0_BYTES).map(|_| next()).collect();
let (quants, scales) = repack_q8_0(&q8);
let (want_q, want_s) = plain_q8_0(&q8);
assert!(quants == want_q && scales == want_s, "q8_0, {n} blocks");
let mx: Vec<u8> = (0..n * MXFP4_BYTES).map(|_| next()).collect();
let (nibbles, exponents) = repack_mxfp4(&mx);
let (want_n, want_e) = plain_mxfp4(&mx);
assert!(nibbles == want_n && exponents == want_e, "mxfp4, {n} blocks");
}
}
#[test]
fn empty_inputs_repack_to_nothing() {
assert!(repack_q8_0(&[]).0.is_empty());
assert!(repack_mxfp4(&[0u8; 16]).0.is_empty());
}