diff --git a/crates/gptoss-igpu/examples/layer0_probe.rs b/crates/gptoss-igpu/examples/layer0_probe.rs index 9c870dcc..cc626dac 100644 --- a/crates/gptoss-igpu/examples/layer0_probe.rs +++ b/crates/gptoss-igpu/examples/layer0_probe.rs @@ -12,33 +12,11 @@ use std::fs::File; use std::io::BufReader; use std::path::Path; -fn main() { - let path = std::env::args().nth(1).expect("model path"); - let path = Path::new(&path); - let size = std::fs::metadata(path).expect("model file").len(); - let gguf = Gguf::read( - BufReader::with_capacity(1 << 20, File::open(path).expect("open")), - size, - ) - .expect("valid GGUF"); - let d = Dispatch::from_env().expect("kernel level"); - let model = Model::load( - std::sync::Arc::new(Mapping::open(path).expect("map model")), - &gguf, - d, - 4, - ) - .expect("load model"); +/// The CPU stages: normalised rows, their Q8_0 blocks and the K projection with its bias. +fn cpu_stages(model: &Model, d: Dispatch, x: &[f32], m: usize) -> (Vec, Vec, Vec) { let c = &model.config; let e = c.n_embd; - let tokens = [1000u32, 5000, 20000, 41234, 777, 13, 99999, 55555]; - let m = tokens.len(); let lv = model.layer_view(0); - let mut x = vec![0.0f32; m * e]; - for (t, row) in x.chunks_exact_mut(e).enumerate() { - model.embed(tokens[t], row).unwrap(); - } - // CPU stages let mut h = vec![0.0f32; m * e]; let mut q = vec![0u8; m * e / 32 * 34]; for t in 0..m { @@ -62,43 +40,51 @@ fn main() { + lv.bk[r]; } } - // GPU stages + (h, q, cpu_k) +} + +/// What the GPU stages left: the normalised rows, the quantized words and scales, and the K projection. +type GpuStages = (Vec, Vec, Vec, Vec); + +fn gpu_stages(model: &Model, x: &[f32], m: usize) -> GpuStages { + let c = &model.config; + let e = c.n_embd; + let lv = model.layer_view(0); + let wk = &lv.wk; let gpu = Gpu::new().expect("gpu"); - let xb = gpu.buf_f32(&x); + let xb = gpu.buf_f32(x); let wb = gpu.buf_f32(lv.attn_norm); let hb = gpu.zeros(m * e); let act = gpu.new_activations(m, e).unwrap(); - let wkm = gpu - .upload_matrix(WeightKind::Q8_0, wk.rows, wk.cols, wk.data) - .unwrap(); + let wkm = gpu.upload_matrix(WeightKind::Q8_0, wk.rows, wk.cols, wk.data).unwrap(); let bias = gpu.buf_f32(lv.bk); let kb = gpu.zeros(m * wk.rows); let mut rec = gpu.recorder(); - rec.rmsnorm_quant(&xb, &wb, c.rms_eps, m, e, &hb, &act) - .unwrap(); + rec.rmsnorm_quant(&xb, &wb, c.rms_eps, m, e, &hb, &act).unwrap(); rec.matmul(&wkm, &act, &kb, Some(&bias), false).unwrap(); rec.submit().unwrap(); let gh = gpu.read_f32(&hb).unwrap(); let (words, scales) = gpu.read_activations(&act).unwrap(); let gk = gpu.read_f32(&kb).unwrap(); + (gh, words, scales, gk) +} - let rel = |a: &[f32], b: &[f32]| { - let (mut num, mut den, mut max) = (0f64, 0f64, 0f64); - for (x, y) in a.iter().zip(b) { - let (x, y) = (f64::from(*x), f64::from(*y)); - num += (x - y) * (x - y); - den += x * x; - max = max.max((x - y).abs()); - } - ((num / den).sqrt(), max) - }; - println!( - "norm output: relative rms {:.2e}, max abs {:.2e}", - rel(&h, &gh).0, - rel(&h, &gh).1 - ); +/// Relative rms and largest absolute difference of two vectors. +fn rel(a: &[f32], b: &[f32]) -> (f64, f64) { + let (mut num, mut den, mut max) = (0f64, 0f64, 0f64); + for (x, y) in a.iter().zip(b) { + let (x, y) = (f64::from(*x), f64::from(*y)); + num += (x - y) * (x - y); + den += x * x; + max = max.max((x - y).abs()); + } + ((num / den).sqrt(), max) +} + +/// Quantized values and scales that differ between the CPU blocks `q` and the GPU words and scales. +fn compare_quantized(q: &[u8], (words, scales): (&[u32], &[f32]), values: usize) { let (mut flips, mut scale_diff, mut worst) = (0usize, 0usize, 0i32); - for b in 0..m * e / 32 { + for b in 0..values / 32 { let blk = &q[b * 34..(b + 1) * 34]; let cs = f32::from_bits(0) + half(u16::from_le_bytes([blk[0], blk[1]])); if (scales[b] - cs).abs() > 0.0 { @@ -113,10 +99,37 @@ fn main() { } } println!( - "quantized: {flips} of {} values differ (worst {worst}), {scale_diff} of {} scales differ", - m * e, - m * e / 32 + "quantized: {flips} of {values} values differ (worst {worst}), {scale_diff} of {} scales differ", + values / 32 ); +} + +fn main() { + let path = std::env::args().nth(1).expect("model path"); + let path = Path::new(&path); + let size = std::fs::metadata(path).expect("model file").len(); + let gguf = + Gguf::read(BufReader::with_capacity(1 << 20, File::open(path).expect("open")), size).expect("valid GGUF"); + let d = Dispatch::from_env().expect("kernel level"); + let model = Model::load( + std::sync::Arc::new(Mapping::open(path).expect("map model")), + &gguf, + d, + 4, + ) + .expect("load model"); + let e = model.config.n_embd; + let tokens = [1000u32, 5000, 20000, 41234, 777, 13, 99999, 55555]; + let m = tokens.len(); + let mut x = vec![0.0f32; m * e]; + for (t, row) in x.chunks_exact_mut(e).enumerate() { + model.embed(tokens[t], row).unwrap(); + } + let (h, q, cpu_k) = cpu_stages(&model, d, &x, m); + let (gh, words, scales, gk) = gpu_stages(&model, &x, m); + let (norm_rms, norm_max) = rel(&h, &gh); + println!("norm output: relative rms {norm_rms:.2e}, max abs {norm_max:.2e}"); + compare_quantized(&q, (&words, &scales), m * e); let (r, max) = rel(&cpu_k, &gk); println!("K projection: relative rms {r:.2e}, max abs {max:.2e}"); }