[HIP] Batched NVFP4 f16 GEMM (M>1) for prefill: weight dequant amortized MT-fold
Port strata k_sf4_gemv<MT> multi-token path: k_nvfp4_gemv_f16_mt<MT=16>, one wave per output row, the W group is dequantized ONCE and reused across MT tokens (fdot2 per token), M tiled by blockIdx.y. X [M,K] f16 -> Y [M,N] f32. This is the expert-GEMM foundation for batched prefill: reading each expert weight once per MT tokens instead of once per token amortizes the dominant NVFP4 weight bandwidth MT-fold. For M=1 the math reduces to the decode GEMV (same fdot2 + scale-fma). Parity vs CPU dequant oracle [M,N]: M=37 (exercises 2 full MT tiles + 5-token remainder) max_rel_err 1.75e-6. Additive — decode default unchanged. Verified: hip GPU green (incl. new mt parity), workspace CPU -D warnings green, 6 Lean proofs 0 sorryAx. Next: batched MoE (router M=B + grouped expert GEMM + reduce M=B) and batched attention/depth-blocks -> forward_batch for prefill.
This commit is contained in:
1 parent
0914fae8c8
commit
bfdd665434
2 files changed
+132
No files matched your search
@@ -83,6 +83,10 @@ mod ffi {
|
||||
ids: *const c_int, slot: c_int, x: *const u16, y: *mut c_float,
|
||||
n: c_int, k: c_int, w_stride: c_long, sc_stride: c_long, y_offset: c_long, stream: *mut c_void,
|
||||
) -> c_int;
|
||||
pub fn vetta_nvfp4_gemv_f16_mt(
|
||||
packed: *const u8, scales: *const u8, outer_scale: c_float, x: *const u16, y: *mut c_float,
|
||||
m: c_int, n: c_int, k: c_int, stream: *mut c_void,
|
||||
) -> c_int;
|
||||
pub fn vetta_mxfp8_gemv(weights: *const u8, scales: *const u8, x: *const c_float, y: *mut c_float, n: c_int, k: c_int, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_mxfp8_gemv_f16(weights: *const u8, scales: *const u8, x: *const u16, y: *mut c_float, n: c_int, k: c_int, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_mxfp8_gemv_f16_indirect(
|
||||
@@ -465,6 +469,18 @@ pub fn nvfp4_gemv_f16_indirect(arena_packed: &DeviceBuf, arena_scales: &DeviceBu
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Batched (M>1) NVFP4 f16 GEMM for prefill: X [M,K] f16, Y [M,N] f32. The W
|
||||
/// group is dequantized once and reused across MT=16 tokens (M tiled by
|
||||
/// blockIdx.y), amortizing expert weight bandwidth MT-fold vs the M=1 GEMV.
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn nvfp4_gemv_f16_mt(packed: &DeviceBuf, scales: &DeviceBuf, outer_scale: f32, x: &DeviceBuf, y: &DeviceBuf, m: i32, n: i32, k: i32) -> Result<(), String> {
|
||||
let rc = unsafe {
|
||||
ffi::vetta_nvfp4_gemv_f16_mt(packed.ptr as *const u8, scales.ptr as *const u8, outer_scale, x.ptr as *const u16, y.ptr as *mut f32, m, n, k, std::ptr::null_mut())
|
||||
};
|
||||
if rc != 0 { return Err("nvfp4_gemv_f16_mt launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Launch k_mxfp8_gemv_f16: MXFP8 GEMV reading FP16 activations (strata B=1 path).
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn mxfp8_gemv_f16(weights: &DeviceBuf, scales: &DeviceBuf, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32) -> Result<(), String> {
|
||||
@@ -972,6 +988,46 @@ mod gpu_tests {
|
||||
eprintln!("nvfp4_gemv_f16_indirect: bit-exact vs direct over {} routed slots", ids_vec.len());
|
||||
}
|
||||
|
||||
/// Batched (M>1) NVFP4 f16 GEMM parity vs a CPU dequant oracle over [M,N].
|
||||
/// M=37 exercises the MT=16 tiling (2 full tiles + a 5-token remainder).
|
||||
/// Integer activations are exact in f16, so the oracle uses them directly.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn nvfp4_gemv_f16_mt_parity() {
|
||||
let (m, n, k) = (37usize, 16usize, 128usize);
|
||||
let outer = 0.5f32;
|
||||
let packed: Vec<u8> = (0..n * (k / 2)).map(|i| (i % 256) as u8).collect();
|
||||
let scales: Vec<u8> = (0..n * (k / 16)).map(|i| ((i * 7 + 3) % 251) as u8).collect();
|
||||
let xf: Vec<f32> = (0..m * k).map(|i| (i % 29) as f32 - 14.0).collect();
|
||||
let dp = DeviceBuf::alloc(packed.len()).unwrap(); dp.h2d_bytes(&packed).unwrap();
|
||||
let ds = DeviceBuf::alloc(scales.len()).unwrap(); ds.h2d_bytes(&scales).unwrap();
|
||||
let dxf = DeviceBuf::alloc(m * k * 4).unwrap(); dxf.h2d(&xf).unwrap();
|
||||
let dxh = DeviceBuf::alloc(m * k * 2).unwrap(); cast_f32_f16(&dxf, &dxh, m * k).unwrap();
|
||||
let dy = DeviceBuf::alloc(m * n * 4).unwrap();
|
||||
nvfp4_gemv_f16_mt(&dp, &ds, outer, &dxh, &dy, m as i32, n as i32, k as i32).unwrap();
|
||||
let mut got = vec![0f32; m * n]; dy.d2h(&mut got).unwrap();
|
||||
let mut want = vec![0f32; m * n];
|
||||
for mi in 0..m {
|
||||
for ni in 0..n {
|
||||
let mut acc = 0f32;
|
||||
for b in 0..k / 16 {
|
||||
let sc = e4m3(scales[ni * (k / 16) + b]) * outer;
|
||||
let mut d = 0f32;
|
||||
for j in 0..8 {
|
||||
let byte = packed[ni * (k / 2) + b * 8 + j];
|
||||
d += xf[mi * k + b * 16 + j * 2] * e2m1(byte & 15) + xf[mi * k + b * 16 + j * 2 + 1] * e2m1(byte >> 4);
|
||||
}
|
||||
acc = d.mul_add(sc, acc);
|
||||
}
|
||||
want[mi * n + ni] = acc;
|
||||
}
|
||||
}
|
||||
let mut max_rel = 0f32;
|
||||
for (g, w) in got.iter().zip(want.iter()) { max_rel = max_rel.max((g - w).abs() / w.abs().max(1e-3)); }
|
||||
eprintln!("nvfp4_gemv_f16_mt m={m} n={n} k={k} max_rel_err={max_rel:.3e}");
|
||||
assert!(max_rel < 2e-3, "batched NVFP4 f16 GEMM parity failed: {max_rel}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn situ_parity_vs_cpu() {
|
||||
|
||||
Reference in new issue
Block a user