diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index 479b2d9..7fbc212 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -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 = (0..e * w_stride).map(|i| (i % 256) as u8).collect(); + let scales: Vec = (0..e * sc_stride).map(|i| ((i * 7 + 3) % 251) as u8).collect(); + let outer: Vec = (0..e).map(|i| 0.3 + 0.1 * i as f32).collect(); + let x_f32: Vec = (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 = vec![2, 0, 3, 1]; + let ids_bytes: Vec = 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() { diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index bac8e39..5aae2c2 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -744,7 +744,7 @@ extern "C" int vetta_nvfp4_gemv(const uint8_t* packed, const uint8_t* scales, // oracle (E2M1 exact in FP16, f16*f16 product exact in f32), only the f32 // accumulation order differs (within the 2e-3 parity tolerance). -__global__ void __launch_bounds__(256) k_nvfp4_gemv_f16(const uint8_t* __restrict__ packed, +__device__ __forceinline__ void nvfp4_gemv_f16_body(const uint8_t* __restrict__ packed, const uint8_t* __restrict__ scales, float outer_scale, const uint16_t* __restrict__ x, float* __restrict__ y, int N, int K) { @@ -788,6 +788,46 @@ __global__ void __launch_bounds__(256) k_nvfp4_gemv_f16(const uint8_t* __restric if (lane == 0) y[row] = sum; } +__global__ void __launch_bounds__(256) k_nvfp4_gemv_f16(const uint8_t* __restrict__ packed, + const uint8_t* __restrict__ scales, + float outer_scale, const uint16_t* __restrict__ x, + float* __restrict__ y, int N, int K) { + nvfp4_gemv_f16_body(packed, scales, outer_scale, x, y, N, K); +} + +// Indirect (device-routed) variant for graph-capturable MoE: the expert id is +// read from a DEVICE buffer (ids[slot]) and indexes a contiguous per-layer +// weight arena, so launch parameters stay static (no host round-trip). Strides +// are the per-expert byte spans of the packed-weight / scale arenas; outer_arena +// holds one precomputed outer_scale per expert. Math identical to the direct +// kernel (same body) — only the weight base address is selected on device. +__global__ void __launch_bounds__(256) k_nvfp4_gemv_f16_indirect( + const uint8_t* __restrict__ arena_packed, + const uint8_t* __restrict__ arena_scales, + const float* __restrict__ outer_arena, + const int* __restrict__ ids, int slot, + const uint16_t* __restrict__ x, + float* __restrict__ y, int N, int K, + long w_stride, long sc_stride) { + const int id = ids[slot]; + nvfp4_gemv_f16_body(arena_packed + (long)id * w_stride, + arena_scales + (long)id * sc_stride, + outer_arena[id], x, y, N, K); +} + +extern "C" int vetta_nvfp4_gemv_f16_indirect(const uint8_t* arena_packed, + const uint8_t* arena_scales, const float* outer_arena, + const int* ids, int slot, const uint16_t* x, float* y, + int N, int K, long w_stride, long sc_stride, + vetta_stream_t stream) { + if (N <= 0 || K <= 0 || (K % 16) != 0 || slot < 0) return 1; + hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0; + k_nvfp4_gemv_f16_indirect<<<(N + 7) / 8, 256, 0, s>>>( + arena_packed, arena_scales, outer_arena, ids, slot, x, y, N, K, w_stride, sc_stride); + HIP_CHECK(hipGetLastError()); + return 0; +} + extern "C" int vetta_nvfp4_gemv_f16(const uint8_t* packed, const uint8_t* scales, float outer_scale, const uint16_t* x, float* y, int N, int K, vetta_stream_t stream) {