[HIP] NVFP4 expert GEMV kernel (exact E2M1+E4M3 dequant, GPU-verified ~4e-7)

- kernels/hip: NVFP4 (SF4) codecs ported exactly from strata ink_hip_common —
  sf4_e2m1_f32 (E2M1 nibble LUT {0,0.5,1,1.5,2,3,4,6,...}), e4m3fn_to_f32
  (F8_E4M3FN: subnormal mant*2^-9, NaN->0, bias 7). Layout: packed_e2m1
  [N,K/2] (low nib even col), scales_e4m3 [N,K/16], f32 outer_scale.
- k_nvfp4_gemv (M=1 decode): y[N] = dequant(W)[N,K] @ x[K], one block/row,
  threads stride K/16 groups, f32 accumulate. Correctness-first; the Wave32
  bandwidth-saturated path is the performance pass. This is the core of the
  MoE expert compute (w13/w2); the full direct path (SiTU activation +
  weighted reduce) builds on it.
- C ABI + safe wrapper nvfp4_gemv + CPU-oracle parity test (Rust e2m1/e4m3
  matching the kernel).

Verified LIVE on gfx1150: nvfp4_gemv max_rel_err ~4e-7 across (8,64)/(16,256)/
(32,128). Eight compute kernels now verified. Default suite 97/97, lean 4
files green; GPU parity 8/8 (--features hip).
This commit is contained in:
adm committed 2026-12-01 14:07:14 +01:00
1 parent 87a66aee37
commit bd49da5636
3 files changed
+149

No files matched your search

+68
View File
@@ -50,6 +50,10 @@ mod ffi {
) -> c_int;
pub fn vetta_vec_add(a: *const c_float, b: *const c_float, y: *mut c_float, n: usize, stream: *mut c_void) -> c_int;
pub fn vetta_cast_f32_bf16(x: *const c_float, y: *mut u16, n: usize, stream: *mut c_void) -> c_int;
pub fn vetta_nvfp4_gemv(
packed: *const u8, scales: *const u8, outer_scale: c_float, x: *const c_float, y: *mut c_float,
n: c_int, k: c_int, stream: *mut c_void,
) -> c_int;
}
}
@@ -208,6 +212,16 @@ pub fn cast_f32_bf16(x: &DeviceBuf, y: &DeviceBuf, n: usize) -> Result<(), Strin
Ok(())
}
/// Launch k_nvfp4_gemv (M=1): y[n] = dequant(W)[n,k] @ x[k] (NVFP4).
#[cfg(feature = "hip")]
pub fn nvfp4_gemv(packed: &DeviceBuf, scales: &DeviceBuf, outer_scale: f32, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32) -> Result<(), String> {
let rc = unsafe {
ffi::vetta_nvfp4_gemv(packed.ptr as *const u8, scales.ptr as *const u8, outer_scale, x.ptr as *const f32, y.ptr as *mut f32, n, k, std::ptr::null_mut())
};
if rc != 0 { return Err("nvfp4_gemv launch failed".into()); }
Ok(())
}
#[cfg(all(test, feature = "hip"))]
mod gpu_tests {
use super::*;
@@ -433,6 +447,60 @@ mod gpu_tests {
assert_eq!(got, want, "vec_add must be exact");
}
fn e2m1(nib: u8) -> f32 {
const LUT: [f32; 16] = [0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0, -0.0, -0.5, -1.0, -1.5, -2.0, -3.0, -4.0, -6.0];
LUT[(nib & 15) as usize]
}
fn e4m3(b: u8) -> f32 {
let sign = ((b & 0x80) as u32) << 24;
let exp = (b >> 3) & 0xF;
let mant = b & 7;
if exp == 0 {
if mant == 0 { return f32::from_bits(sign); }
let val = mant as f32 / 512.0;
return if b & 0x80 != 0 { -val } else { val };
} else if exp == 15 && mant == 7 { return 0.0; }
f32::from_bits(sign | ((exp as u32 + 120) << 23) | ((mant as u32) << 20))
}
#[test]
#[ignore]
fn nvfp4_gemv_parity_vs_cpu() {
for &(n, k) in &[(8usize, 64usize), (16, 256), (32, 128)] {
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 x: Vec<f32> = (0..k).map(|i| ((i % 29) as f32 - 14.0) * 0.04).collect();
// CPU oracle: dequant then dot, f32.
let mut want = vec![0f32; n];
for ni in 0..n {
let mut acc = 0f32;
for b in 0..k / 16 {
let sc = e4m3(scales[ni * (k / 16) + b]) * outer;
for j in 0..8 {
let byte = packed[ni * (k / 2) + b * 8 + j];
let w0 = e2m1(byte & 15) * sc;
let w1 = e2m1(byte >> 4) * sc;
acc += x[b * 16 + j * 2] * w0 + x[b * 16 + j * 2 + 1] * w1;
}
}
want[ni] = acc;
}
let dp = DeviceBuf::alloc(packed.len()).unwrap();
let ds = DeviceBuf::alloc(scales.len()).unwrap();
let dx = DeviceBuf::alloc(x.len() * 4).unwrap();
let dy = DeviceBuf::alloc(n * 4).unwrap();
dp.h2d_bytes(&packed).unwrap(); ds.h2d_bytes(&scales).unwrap(); dx.h2d(&x).unwrap();
nvfp4_gemv(&dp, &ds, outer, &dx, &dy, n as i32, k as i32).unwrap();
let mut got = vec![0f32; n];
dy.d2h(&mut got).unwrap();
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 n={n} k={k} max_rel_err={max_rel:.3e}");
assert!(max_rel < 1e-4, "nvfp4 parity failed: {max_rel}");
}
}
fn bytemuck_u16(v: &[u16]) -> &[u8] {
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, v.len() * 2) }
}