[LD][HIP] Device-resident MoE path (opt-in VETTA_DEVICE_MOE): no host ids/output round-trip

moe_device runs the whole MoE on device: router ids/weights stay on device, the
arena indirect GEMV selects experts on device, per-expert outputs accumulate in a
device y_part via a STATIC y_offset=kk*hidden (added to both indirect kernels).
No ids D2H, no per-expert output D2H/H2D => the MoE block is now graph-capturable.

WeightCache gains a memoized expert_arena(ip,mat) accessor. Both SWA and full-attn
MoE blocks dispatch to moe_device under VETTA_DEVICE_MOE=1; the default f32 host
path is unchanged (parity baseline preserved).

Parity: 16/16 greedy tokens IDENTICAL between VETTA_DEVICE_MOE=1 and the
VETTA_F16_EXPERT=1 host path (same kernels/data). Arena indirect bit-exact vs
direct on real weights (NVFP4 layer 0 + MXFP8 layer 20).

Verified: ld GPU 19 + hip GPU 19 green, workspace CPU -D warnings green, 6 Lean
proofs 0 sorryAx. Next: device-resident depth-block/bank machinery, then HIP graph
capture of the decode step.
This commit is contained in:
adm committed 2026-12-01 22:43:47 +01:00
1 parent 752edeaade
commit 0914fae8c8
4 files changed
+87 -34

No files matched your search

+7 -7
View File
@@ -81,13 +81,13 @@ mod ffi {
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,
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_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(
arena_weights: *const u8, arena_scales: *const u8, 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,
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_situ(gu: *const c_float, act: *mut c_float, inter: c_int, beta: c_float, lb: c_float, stream: *mut c_void) -> c_int;
pub fn vetta_situ_f16(gu: *const c_float, act: *mut u16, inter: c_int, beta: c_float, lb: c_float, stream: *mut c_void) -> c_int;
@@ -457,9 +457,9 @@ pub fn nvfp4_gemv_f16(packed: &DeviceBuf, scales: &DeviceBuf, outer_scale: f32,
/// 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> {
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, y_offset: 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())
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, y_offset, std::ptr::null_mut())
};
if rc != 0 { return Err("nvfp4_gemv_f16_indirect launch failed".into()); }
Ok(())
@@ -477,9 +477,9 @@ pub fn mxfp8_gemv_f16(weights: &DeviceBuf, scales: &DeviceBuf, x: &DeviceBuf, y:
/// Device-routed MXFP8 f16 GEMV (arena + ids[slot]); graph-capturable, no outer.
#[cfg(feature = "hip")]
pub fn mxfp8_gemv_f16_indirect(arena_weights: &DeviceBuf, arena_scales: &DeviceBuf, ids: &DeviceBuf, slot: i32, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32, w_stride: i64, sc_stride: i64) -> Result<(), String> {
pub fn mxfp8_gemv_f16_indirect(arena_weights: &DeviceBuf, arena_scales: &DeviceBuf, ids: &DeviceBuf, slot: i32, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32, w_stride: i64, sc_stride: i64, y_offset: i64) -> Result<(), String> {
let rc = unsafe {
ffi::vetta_mxfp8_gemv_f16_indirect(arena_weights.ptr as *const u8, arena_scales.ptr as *const u8, 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())
ffi::vetta_mxfp8_gemv_f16_indirect(arena_weights.ptr as *const u8, arena_scales.ptr as *const u8, ids.ptr as *const i32, slot, x.ptr as *const u16, y.ptr as *mut f32, n, k, w_stride, sc_stride, y_offset, std::ptr::null_mut())
};
if rc != 0 { return Err("mxfp8_gemv_f16_indirect launch failed".into()); }
Ok(())
@@ -960,7 +960,7 @@ mod gpu_tests {
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();
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, 0).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();