Files
leleka/crates/igpu/examples/gemm_bench.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 9a26ace643 feat: add the leleka-igpu crate with GPU matrix products over Q8_0 and MXFP4 weights (STRA-219, phase A)
New pure-Rust crate on wgpu 26 with generated WGSL kernels: C[m][n] = sum over 32-element blocks of the
exact int8 block sum (packed dot-product built-in) times the activation and weight block scales. Weights
are repacked at upload (ggml's unaligned 34- and 17-byte blocks become int8 or nibble words plus separate
half-float or E8M0 scale words); MXFP4 values are decoded in the shader from a nibble-packed magnitude table.
Every accumulator and operand is a named scalar (dynamically indexed private arrays spill on this GPU).

Checked against the CPU kernels: random Q8_0 and MXFP4 weights on awkward shapes (single block, ragged
tiles, 1 to 130 rows) and, with LELEKA_GPTOSS_GGUF set, real tensors of gpt-oss-20b (blk.3.attn_q Q8_0
4096 x 2880; blk.5 gate expert 11 and blk.23 down expert 31, MXFP4 2880 x 2880): all outputs within the
float32 accumulation bound, worst error 0.01-0.02 of it.

Speed (gemm_bench, reference iGPU, one run each): a 4x4-outputs-per-thread tile is best, about 0.7 TOPS
int8 including the block scales (MXFP4 2880x2880: 0.58 TOPS at 32 tokens, 0.68 at 64, 0.72 at 256;
Q8_0 4096x2880 at 256 tokens: 0.79); 8x8 spills registers (0.45). The CPU decoder's equivalent is about
0.25 TOPS. Small products use only ~45 workgroups; batching experts will fill the chip.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 23:27:03 +02:00

86 lines
3.0 KiB
Rust

//! Speed of the GPU matrix products on prefill-like shapes, in the ggml weight formats.
//!
//! Run: `cargo run --release -p leleka-igpu --example gemm_bench`
//! Reports milliseconds per product and the integer throughput (2 * m * n * k operations).
use leleka_cpu_kernels::Dispatch;
use leleka_igpu::{Gpu, WeightKind};
fn main() {
// LELEKA_IGPU_TILE=TMxTN picks the outputs per thread (default 4x4, the measured best)
let (tm, tn) = std::env::var("LELEKA_IGPU_TILE")
.ok()
.and_then(|s| {
s.split_once('x')
.map(|(a, b)| (a.parse().ok(), b.parse().ok()))
})
.and_then(|(a, b)| Some((a?, b?)))
.unwrap_or((4, 4));
let gpu = Gpu::with_tile(tm, tn).expect("a GPU");
println!(
"adapter: {}, tile {tm}x{tn} outputs per thread",
gpu.adapter_name()
);
let d = Dispatch::best();
let mut seed = 0x9E37_79B9_7F4A_7C15u64;
let mut next = move || {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
seed
};
let blocks = 90; // K = 2880, gpt-oss' hidden size
println!(
"{:<8} {:>5} {:>6} {:>10} {:>9}",
"weights", "m", "n", "ms/product", "TOPS"
);
for (kind, n) in [
(WeightKind::Mxfp4, 2880usize),
(WeightKind::Mxfp4, 5760),
(WeightKind::Q8_0, 4096),
(WeightKind::Q8_0, 2880),
] {
let w: Vec<u8> = match kind {
WeightKind::Q8_0 => (0..n * blocks)
.flat_map(|_| {
let mut b = vec![0x00, 0x2C]; // half 0.0625
b.extend((0..32).map(|_| next() as u8));
b
})
.collect(),
WeightKind::Mxfp4 => (0..n * blocks)
.flat_map(|_| {
let mut b = vec![120u8];
b.extend((0..16).map(|_| next() as u8));
b
})
.collect(),
};
let wm = gpu.upload_matrix(kind, n, blocks * 32, &w).expect("upload");
for m in [8usize, 16, 32, 64, 128, 256] {
let x: Vec<f32> = (0..m * blocks * 32)
.map(|_| (next() % 1000) as f32 / 100.0 - 5.0)
.collect();
let mut q = vec![0u8; m * blocks * 34];
d.quantize_q8_0(&x, &mut q).expect("quantize");
let am = gpu
.upload_activations(m, blocks * 32, &q)
.expect("upload activations");
let mut times: Vec<f64> = (0..5)
.map(|_| gpu.time_matmul(&wm, &am, 10).expect("run"))
.collect();
times.sort_by(|a, b| a.total_cmp(b));
let t = times[2];
let ops = 2.0 * m as f64 * n as f64 * (blocks * 32) as f64;
println!(
"{:<8} {:>5} {:>6} {:>10.3} {:>9.2}",
format!("{kind:?}"),
m,
n,
t * 1e3,
ops / t / 1e12
);
}
}
}