[HIP] Grouped MoE expert GEMM (one launch over all experts, bucket-indexed)
k_nvfp4_grouped_gemv_f16<MT=16>: grid.y=n_experts, expert eid processes its bucket tokens [exp_off[eid],exp_off[eid+1]) read from bucket_tok; W group dequantized once per MT-token tile (fdot2), amortizing expert weight bandwidth MT-fold across the tokens routed to that expert. X [B,K] f16 gathered by bucket_tok -> Y [total,N] f32 in bucket order. This is the prefill MoE GEMM (w13/w2 NVFP4); composes with the landed bucket CSR (count/prefix/scatter). SiTU + reduce_buckets_masked follow. Parity vs CPU dequant oracle per (token,expert) pair: B=20, 4 experts, 160 pairs, max_rel_err 2.79e-5. Additive — decode default unchanged. Verified: hip GPU 22 green, workspace CPU -D warnings green, 6 Lean 0 sorryAx. Next: reduce_buckets_masked + batched SiTU, then batched attention -> forward_batch.
This commit is contained in:
1 parent
e1b16b5874
commit
870133d6e5
2 files changed
+171
No files matched your search
@@ -99,6 +99,11 @@ mod ffi {
|
||||
pub fn vetta_moe_count_buckets(topk_ids: *const c_int, exp_cnt: *mut c_int, total: c_int, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_moe_prefix_buckets(exp_cnt: *const c_int, exp_off: *mut c_int, exp_cur: *mut c_int, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_moe_scatter_buckets(topk_ids: *const c_int, exp_cur: *mut c_int, bucket_tok: *mut c_int, slotk_to_bidx: *mut c_int, total: c_int, top_k: c_int, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_nvfp4_grouped_gemv_f16(
|
||||
arena_packed: *const u8, arena_scales: *const u8, outer_arena: *const c_float,
|
||||
exp_off: *const c_int, bucket_tok: *const c_int, x: *const u16, y: *mut c_float,
|
||||
n: c_int, k: c_int, n_experts: c_int, w_stride: c_long, sc_stride: c_long, stream: *mut c_void,
|
||||
) -> c_int;
|
||||
pub fn vetta_shortconv4_decode(cur: *const c_float, static_w: *const c_float, hist: *mut c_float, out: *mut c_float, slot_ids: *const i32, m: c_int, c: c_int, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_shortconv4_dyn_decode(cur: *const c_float, static_w: *const c_float, dyn_taps: *const c_float, hist: *mut c_float, out: *mut c_float, slot_ids: *const i32, m: c_int, c: c_int, n_heads: c_int, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_attn_res_mix(blocks: *const c_float, cur: *const c_float, queries: *const c_float, out: *mut c_float, n_blocks: c_int, m: c_int, n_queries: c_int, d: c_int, eps: c_float, stream: *mut c_void) -> c_int;
|
||||
@@ -545,6 +550,19 @@ pub fn moe_scatter_buckets(topk_ids: &DeviceBuf, exp_cur: &DeviceBuf, bucket_tok
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Grouped MoE expert GEMM (one launch, grid.y = n_experts): expert eid processes
|
||||
/// its bucket tokens [exp_off[eid], exp_off[eid+1]) via bucket_tok, W dequant
|
||||
/// amortized MT-fold. X [B,K] f16, Y [total_pairs,N] f32 in bucket order.
|
||||
#[cfg(feature = "hip")]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn nvfp4_grouped_gemv_f16(arena_packed: &DeviceBuf, arena_scales: &DeviceBuf, outer_arena: &DeviceBuf, exp_off: &DeviceBuf, bucket_tok: &DeviceBuf, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32, n_experts: i32, w_stride: i64, sc_stride: i64) -> Result<(), String> {
|
||||
let rc = unsafe {
|
||||
ffi::vetta_nvfp4_grouped_gemv_f16(arena_packed.ptr as *const u8, arena_scales.ptr as *const u8, outer_arena.ptr as *const f32, exp_off.ptr as *const i32, bucket_tok.ptr as *const i32, x.ptr as *const u16, y.ptr as *mut f32, n, k, n_experts, w_stride, sc_stride, std::ptr::null_mut())
|
||||
};
|
||||
if rc != 0 { return Err("nvfp4_grouped_gemv_f16 launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Launch k_shortconv4_decode (static): updates `hist` ring in place.
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn shortconv4_decode(cur: &DeviceBuf, static_w: &DeviceBuf, hist: &DeviceBuf, out: &DeviceBuf, slot_ids: &DeviceBuf, m: i32, c: i32) -> Result<(), String> {
|
||||
@@ -1150,6 +1168,66 @@ mod gpu_tests {
|
||||
eprintln!("moe_buckets B={b} top_k={top_k}: CSR offsets + bijection + per-expert multiset exact");
|
||||
}
|
||||
|
||||
/// Grouped MoE expert GEMM parity vs a CPU dequant oracle per (token,expert)
|
||||
/// pair. Builds a synthetic 4-expert arena + buckets, runs the grouped GEMM
|
||||
/// (one launch over all experts), and checks Y[bucket_pos] == dequant(W[e]) @
|
||||
/// X[token] for every pair. Integer activations are exact in f16.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn nvfp4_grouped_gemv_f16_parity() {
|
||||
let (b, top_k, n_exp, n, k) = (20usize, 8usize, 4usize, 16usize, 128usize);
|
||||
let total = b * top_k;
|
||||
let w_stride = n * (k / 2);
|
||||
let sc_stride = n * (k / 16);
|
||||
let packed: Vec<u8> = (0..n_exp * w_stride).map(|i| (i % 256) as u8).collect();
|
||||
let scales: Vec<u8> = (0..n_exp * sc_stride).map(|i| ((i * 7 + 3) % 251) as u8).collect();
|
||||
let outer: Vec<f32> = (0..n_exp).map(|i| 0.3 + 0.1 * i as f32).collect();
|
||||
let ids: Vec<i32> = (0..total).map(|i| ((i * 7 + (i / top_k) * 13) % n_exp) as i32).collect();
|
||||
let xf: Vec<f32> = (0..b * k).map(|i| (i % 29) as f32 - 14.0).collect();
|
||||
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(n_exp * 4).unwrap(); ao.h2d(&outer).unwrap();
|
||||
let dxf = DeviceBuf::alloc(b * k * 4).unwrap(); dxf.h2d(&xf).unwrap();
|
||||
let dxh = DeviceBuf::alloc(b * k * 2).unwrap(); cast_f32_f16(&dxf, &dxh, b * k).unwrap();
|
||||
let ids_bytes: Vec<u8> = ids.iter().flat_map(|x| x.to_le_bytes()).collect();
|
||||
let dids = DeviceBuf::alloc(total * 4).unwrap(); dids.h2d_bytes(&ids_bytes).unwrap();
|
||||
let dcnt = DeviceBuf::alloc(129 * 4).unwrap(); dcnt.h2d_bytes(&vec![0u8; 129 * 4]).unwrap();
|
||||
let doff = DeviceBuf::alloc(129 * 4).unwrap();
|
||||
let dcur = DeviceBuf::alloc(128 * 4).unwrap();
|
||||
let dbtk = DeviceBuf::alloc(total * 4).unwrap();
|
||||
let ds2b = DeviceBuf::alloc(total * 4).unwrap();
|
||||
moe_count_buckets(&dids, &dcnt, total as i32).unwrap();
|
||||
moe_prefix_buckets(&dcnt, &doff, &dcur).unwrap();
|
||||
moe_scatter_buckets(&dids, &dcur, &dbtk, &ds2b, total as i32, top_k as i32).unwrap();
|
||||
let dy = DeviceBuf::alloc(total * n * 4).unwrap();
|
||||
nvfp4_grouped_gemv_f16(&ap, &asc, &ao, &doff, &dbtk, &dxh, &dy, n as i32, k as i32, n_exp as i32, w_stride as i64, sc_stride as i64).unwrap();
|
||||
let mut got = vec![0f32; total * n]; dy.d2h(&mut got).unwrap();
|
||||
let mut s2b_b = vec![0u8; total * 4]; ds2b.d2h_bytes(&mut s2b_b).unwrap();
|
||||
let s2b: Vec<i32> = s2b_b.chunks_exact(4).map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]])).collect();
|
||||
let mut max_rel = 0f32;
|
||||
for i in 0..total {
|
||||
let tok = i / top_k;
|
||||
let e = ids[i] as usize;
|
||||
let bp = s2b[i] as usize;
|
||||
for ni in 0..n {
|
||||
let mut acc = 0f32;
|
||||
for bb in 0..k / 16 {
|
||||
let sc = e4m3(scales[e * sc_stride + ni * (k / 16) + bb]) * outer[e];
|
||||
let mut d = 0f32;
|
||||
for j in 0..8 {
|
||||
let byte = packed[e * w_stride + ni * (k / 2) + bb * 8 + j];
|
||||
d += xf[tok * k + bb * 16 + j * 2] * e2m1(byte & 15) + xf[tok * k + bb * 16 + j * 2 + 1] * e2m1(byte >> 4);
|
||||
}
|
||||
acc = d.mul_add(sc, acc);
|
||||
}
|
||||
let g = got[bp * n + ni];
|
||||
max_rel = max_rel.max((g - acc).abs() / acc.abs().max(1e-3));
|
||||
}
|
||||
}
|
||||
eprintln!("nvfp4_grouped_gemv_f16 B={b} experts={n_exp} pairs={total} max_rel_err={max_rel:.3e}");
|
||||
assert!(max_rel < 2e-3, "grouped NVFP4 GEMM parity failed: {max_rel}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn shortconv4_multistep_parity() {
|
||||
|
||||
Reference in new issue
Block a user