Adds two EXPERIMENTAL AVX2 MXFP4 kernels that take the activation as f32 scales plus contiguous int8 quants (q8r, q8r4). q8r is tested bit-identical to the ggml-layout kernel; q8r4 against the stated tolerance. Adds Windows core topology and thread pinning (cpu-sys topology module) and three kernel-level benchmarks that interleave variants and report paired ratios. Findings, recorded in docs/experiments/2026-10-14-cpu-kernel-layout-and-pinning.md: the chip has 2 fast cores and 10 slower ones; the new layout was faster for a single thread (paired median 1.32x, 15 pairs) and inconclusive with many threads; thread pinning did not beat the OS default. The machine was not in a controlled state, so these are not accepted performance results. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
126 lines
4.5 KiB
Rust
126 lines
4.5 KiB
Rust
//! EXPERIMENT: paired comparison of MXFP4 x Q8 kernel variants on identical data.
|
|
//!
|
|
//! Single-kernel throughput only, never an inference result. Machine state drifts between runs
|
|
//! (power, thermals, other load), so variants are interleaved inside every repeat in a rotating order
|
|
//! and compared as PAIRED ratios against the first variant, not as absolute numbers from separate runs.
|
|
//!
|
|
//! Run: `cargo run --release -p leleka-cpu-kernels --example bench_variants -- [MiB] [repeats]`
|
|
|
|
use leleka_cpu_sys as sys;
|
|
use std::time::Instant;
|
|
|
|
const BLOCKS_PER_ROW: usize = 90;
|
|
const MXFP4: usize = 17;
|
|
const Q8: usize = 34;
|
|
|
|
type Kernel<'a> = Box<dyn Fn(&[u8]) -> f32 + Sync + 'a>;
|
|
|
|
fn main() {
|
|
let args: Vec<String> = std::env::args().collect();
|
|
let mib: usize = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(768);
|
|
let repeats: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(15);
|
|
let row_bytes = BLOCKS_PER_ROW * MXFP4;
|
|
let rows = (mib << 20) / row_bytes;
|
|
|
|
let mut state = 0x1234_5678_9ABC_DEF0u64;
|
|
let mut next = move || {
|
|
state = state
|
|
.wrapping_mul(6364136223846793005)
|
|
.wrapping_add(1442695040888963407);
|
|
(state >> 33) as u32
|
|
};
|
|
let mut weights = vec![0u8; rows * row_bytes];
|
|
for block in weights.as_chunks_mut::<MXFP4>().0.iter_mut() {
|
|
block[0] = 112 + (next() % 16) as u8;
|
|
for b in &mut block[1..] {
|
|
*b = next() as u8;
|
|
}
|
|
}
|
|
let mut act = vec![0u8; BLOCKS_PER_ROW * Q8];
|
|
for block in act.as_chunks_mut::<Q8>().0.iter_mut() {
|
|
block[..2].copy_from_slice(&0x2C00u16.to_le_bytes()); // 0.0625
|
|
for b in &mut block[2..] {
|
|
*b = ((next() % 255) as i64 - 127) as i8 as u8;
|
|
}
|
|
}
|
|
let ys = vec![0.0625f32; BLOCKS_PER_ROW];
|
|
let yq: Vec<i8> = act
|
|
.as_chunks::<Q8>()
|
|
.0
|
|
.iter()
|
|
.flat_map(|b| b[2..].iter().map(|v| *v as i8))
|
|
.collect();
|
|
|
|
let kernels: Vec<(&str, Kernel)> = vec![
|
|
(
|
|
"ggml-layout avx2 (baseline)",
|
|
Box::new(|r| sys::dot_mxfp4_q8_0_avx2(r, &act).unwrap()),
|
|
),
|
|
(
|
|
"q8r avx2, 2 acc",
|
|
Box::new(|r| sys::dot_mxfp4_q8r_avx2(r, &ys, &yq).unwrap()),
|
|
),
|
|
(
|
|
"q8r4 avx2, 4 acc",
|
|
Box::new(|r| sys::dot_mxfp4_q8r4_avx2(r, &ys, &yq).unwrap()),
|
|
),
|
|
(
|
|
"ggml-layout avx-vnni",
|
|
Box::new(|r| sys::dot_mxfp4_q8_0_avxvnni(r, &act).unwrap()),
|
|
),
|
|
];
|
|
|
|
println!(
|
|
"buffer {} MiB = {rows} rows; {repeats} interleaved repeats; ratios are paired against the baseline",
|
|
weights.len() >> 20
|
|
);
|
|
for threads in [1usize, 4, 8, 10] {
|
|
let mut samples: Vec<Vec<f64>> = vec![Vec::new(); kernels.len()];
|
|
for rep in 0..repeats {
|
|
for k in 0..kernels.len() {
|
|
let idx = (k + rep) % kernels.len(); // rotate the order each repeat
|
|
let kernel = &kernels[idx].1;
|
|
let start = Instant::now();
|
|
std::thread::scope(|s| {
|
|
let per = rows.div_ceil(threads);
|
|
for t in 0..threads {
|
|
let w = &weights;
|
|
s.spawn(move || {
|
|
let (lo, hi) = (t * per, ((t + 1) * per).min(rows));
|
|
let mut acc = 0f32;
|
|
for r in lo..hi {
|
|
acc += kernel(&w[r * row_bytes..(r + 1) * row_bytes]);
|
|
}
|
|
std::hint::black_box(acc);
|
|
});
|
|
}
|
|
});
|
|
samples[idx].push(weights.len() as f64 / start.elapsed().as_secs_f64() / 1e9);
|
|
}
|
|
}
|
|
println!("-- {threads} thread(s)");
|
|
println!(
|
|
"{:<30} median GB/s paired ratio median [min .. max]",
|
|
"variant"
|
|
);
|
|
for (i, (name, _)) in kernels.iter().enumerate() {
|
|
let mut s = samples[i].clone();
|
|
s.sort_by(|a, b| a.total_cmp(b));
|
|
let mut ratios: Vec<f64> = samples[i]
|
|
.iter()
|
|
.zip(&samples[0])
|
|
.map(|(a, b)| a / b)
|
|
.collect();
|
|
ratios.sort_by(|a, b| a.total_cmp(b));
|
|
println!(
|
|
"{:<30} {:>11.1} {:>6.3} [{:.3} .. {:.3}]",
|
|
name,
|
|
s[s.len() / 2],
|
|
ratios[ratios.len() / 2],
|
|
ratios[0],
|
|
ratios[ratios.len() - 1]
|
|
);
|
|
}
|
|
}
|
|
}
|