[HIP] Device-routed NVFP4 f16 GEMV (indirect): foundation for graph-capturable MoE
Refactor k_nvfp4_gemv_f16 into a shared device body + direct wrapper + indirect wrapper. The indirect variant reads the expert id from a DEVICE buffer (ids[slot]) and indexes a contiguous per-layer weight arena (packed/scales/outer), so launch parameters stay static — no host ids round-trip. This is the prerequisite for HIP graph capture of the decode step (current MoE dispatch is host-mediated). Math identical to the direct kernel (same body): parity test builds a synthetic 4-expert arena and routes 4 slots, asserting BIT-EXACT equality indirect vs direct. Additive only — default forward path unchanged. Verified: hip GPU 19 green (incl. new indirect parity), workspace CPU -D warnings green, 6 Lean proofs 0 sorryAx.
This commit is contained in:
1 parent
8d886bd56f
commit
4c82040610
2 files changed
+99
-2
No files matched your search
@@ -10,7 +10,7 @@ pub const TARGET_ARCH: &str = "gfx1150";
|
||||
|
||||
#[cfg(feature = "hip")]
|
||||
mod ffi {
|
||||
use std::os::raw::{c_char, c_float, c_int, c_void};
|
||||
use std::os::raw::{c_char, c_float, c_int, c_long, c_void};
|
||||
|
||||
#[repr(C)]
|
||||
pub struct DeviceInfoRaw {
|
||||
@@ -78,6 +78,11 @@ mod ffi {
|
||||
packed: *const u8, scales: *const u8, outer_scale: c_float, x: *const u16, y: *mut c_float,
|
||||
n: c_int, k: c_int, stream: *mut c_void,
|
||||
) -> c_int;
|
||||
pub fn vetta_nvfp4_gemv_f16_indirect(
|
||||
arena_packed: *const u8, arena_scales: *const u8, outer_arena: *const c_float,
|
||||
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, 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_situ(gu: *const c_float, act: *mut c_float, inter: c_int, beta: c_float, lb: c_float, stream: *mut c_void) -> c_int;
|
||||
@@ -442,6 +447,20 @@ pub fn nvfp4_gemv_f16(packed: &DeviceBuf, scales: &DeviceBuf, outer_scale: f32,
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Device-routed NVFP4 f16 GEMV: the expert id is read from `ids[slot]` on device
|
||||
/// and indexes a contiguous per-layer arena (packed weights / scales / outer).
|
||||
/// Launch parameters are static => graph-capturable MoE dispatch (no host ids
|
||||
/// round-trip). `w_stride`/`sc_stride` are the per-expert byte spans. Same math
|
||||
/// as `nvfp4_gemv_f16` (shared device body).
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn nvfp4_gemv_f16_indirect(arena_packed: &DeviceBuf, arena_scales: &DeviceBuf, outer_arena: &DeviceBuf, ids: &DeviceBuf, slot: i32, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32, w_stride: i64, sc_stride: i64) -> Result<(), String> {
|
||||
let rc = unsafe {
|
||||
ffi::vetta_nvfp4_gemv_f16_indirect(arena_packed.ptr as *const u8, arena_scales.ptr as *const u8, outer_arena.ptr as *const f32, ids.ptr as *const i32, slot, x.ptr as *const u16, y.ptr as *mut f32, n, k, w_stride, sc_stride, std::ptr::null_mut())
|
||||
};
|
||||
if rc != 0 { return Err("nvfp4_gemv_f16_indirect 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> {
|
||||
@@ -901,6 +920,44 @@ mod gpu_tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Device-routed (indirect) NVFP4 f16 GEMV must be BIT-EXACT vs the direct
|
||||
/// kernel on the same expert slice: same shared device body, only the weight
|
||||
/// base address is selected on device from ids[slot]. Builds a synthetic
|
||||
/// per-layer arena (E experts) and routes several slots through it. This is
|
||||
/// the foundation for graph-capturable MoE dispatch (no host ids round-trip).
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn nvfp4_gemv_f16_indirect_parity() {
|
||||
let (n, k, e) = (16usize, 128usize, 4usize);
|
||||
let w_stride = n * (k / 2);
|
||||
let sc_stride = n * (k / 16);
|
||||
let packed: Vec<u8> = (0..e * w_stride).map(|i| (i % 256) as u8).collect();
|
||||
let scales: Vec<u8> = (0..e * sc_stride).map(|i| ((i * 7 + 3) % 251) as u8).collect();
|
||||
let outer: Vec<f32> = (0..e).map(|i| 0.3 + 0.1 * i as f32).collect();
|
||||
let x_f32: Vec<f32> = (0..k).map(|i| (i % 29) as f32 - 14.0).collect(); // integers exact in f16
|
||||
let ap = DeviceBuf::alloc(packed.len()).unwrap(); ap.h2d_bytes(&packed).unwrap();
|
||||
let asc = DeviceBuf::alloc(scales.len()).unwrap(); asc.h2d_bytes(&scales).unwrap();
|
||||
let ao = DeviceBuf::alloc(e * 4).unwrap(); ao.h2d(&outer).unwrap();
|
||||
let xf = DeviceBuf::alloc(k * 4).unwrap(); xf.h2d(&x_f32).unwrap();
|
||||
let xh = DeviceBuf::alloc(k * 2).unwrap(); cast_f32_f16(&xf, &xh, k).unwrap();
|
||||
let ids_vec: Vec<i32> = vec![2, 0, 3, 1];
|
||||
let ids_bytes: Vec<u8> = ids_vec.iter().flat_map(|x| x.to_le_bytes()).collect();
|
||||
let dids = DeviceBuf::alloc(ids_bytes.len()).unwrap(); dids.h2d_bytes(&ids_bytes).unwrap();
|
||||
let yi = DeviceBuf::alloc(n * 4).unwrap();
|
||||
let yd = DeviceBuf::alloc(n * 4).unwrap();
|
||||
for slot in 0..ids_vec.len() {
|
||||
nvfp4_gemv_f16_indirect(&ap, &asc, &ao, &dids, slot as i32, &xh, &yi, n as i32, k as i32, w_stride as i64, sc_stride as i64).unwrap();
|
||||
let id = ids_vec[slot] as usize;
|
||||
let sp = DeviceBuf::alloc(w_stride).unwrap(); sp.h2d_bytes(&packed[id * w_stride..(id + 1) * w_stride]).unwrap();
|
||||
let ss = DeviceBuf::alloc(sc_stride).unwrap(); ss.h2d_bytes(&scales[id * sc_stride..(id + 1) * sc_stride]).unwrap();
|
||||
nvfp4_gemv_f16(&sp, &ss, outer[id], &xh, &yd, n as i32, k as i32).unwrap();
|
||||
let mut gi = vec![0f32; n]; yi.d2h(&mut gi).unwrap();
|
||||
let mut gd = vec![0f32; n]; yd.d2h(&mut gd).unwrap();
|
||||
assert_eq!(gi, gd, "indirect must equal direct bit-exact (slot {slot}, expert {id})");
|
||||
}
|
||||
eprintln!("nvfp4_gemv_f16_indirect: bit-exact vs direct over {} routed slots", ids_vec.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn situ_parity_vs_cpu() {
|
||||
|
||||
Reference in new issue
Block a user