[HIP] P1: FP16 WMMA grouped NVFP4 MoE GEMM (tensor cores)

First P1 WMMA brick (Federico approved relaxing the bit-exact M=1-vs-M=B parity
to a numeric tolerance; greedy-vs-reference stays the blocking gate). Adds gfx11
(RDNA3.5) FP16 WMMA helpers (v_h16/v_f8, wmma_f32_16x16x16_f16_w32, acc lane map
2r+(lane>>4)) + k_nvfp4_grouped_gemm_wmma: a drop-in for the fdot2 grouped GEMM
with the same arena/CSR I/O. BM=16 (one M tile ~ tokens/expert at chunk 256),
BN=128 (8 waves, one 16x16 N tile each), BK=64 K-slab in LDS. NVFP4 E2M1 -> exact
FP16 prescaled e4m3*0.0625, WMMA f32 accumulation, epilogue *outer*16. Wired into
expert_arena_grouped_gemv_f16 for NVFP4 (K%64==0); VETTA_NO_MOE_WMMA=1 A/B fallback.
M=1 decode keeps fdot2 (WMMA needs M>=16 tiles).

Verified: WMMA-vs-CPU-oracle parity max_rel=1.2e-4; gw serve_gen greedy-vs-
reference EXACT (16 tokens + Paris + EOS, 4/4); forward_batch argmax batch==seq.
Relaxed the internal batched-vs-sequential tolerances to the P1 numeric budget
(global-scale normalization, not per-entry near-zero): forward_batch max_abs<0.15
(~2e-3 rel on +/-30 logits), single-layer cur + MoE global-scale <3e-3, KV cache
stays BYTE-IDENTICAL (attention precedes MoE). MEASURED prefill 2048tok A/B:
fdot2 41.6s -> WMMA 36.9s (-11%), moe_launch ~1888->1400ms (-26%). Gain bounded by
dequant/bandwidth (the reference INT8-fused sf4_i8 path is the further 2x axis).
Suite green: hip34 ld23 gw4 GPU serial, CPU -D warnings, Lean 0 sorryAx.
This commit is contained in:
adm committed 2026-12-02 03:04:58 +01:00
1 parent 47f8b52070
commit 755626fae2
4 files changed
+253 -6

No files matched your search

+27 -6
View File
@@ -1598,7 +1598,7 @@ mod tests {
let mut ids_b = vec![0u8; b * top_k * 4]; ids.d2h_bytes(&mut ids_b).unwrap();
let ids_h: Vec<i32> = ids_b.chunks_exact(4).map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]])).collect();
let mut weights_h = vec![0f32; b * top_k]; weights.d2h(&mut weights_h).unwrap();
let mut max_rel = 0f32;
let mut os_all = vec![0f32; b * h];
for m in 0..b {
let mlpin_m = DeviceBuf::alloc(h * 4).unwrap(); mlpin_m.h2d(&mlpin_h[m * h..(m + 1) * h]).unwrap();
let ids_m = DeviceBuf::alloc(top_k * 4).unwrap();
@@ -1607,8 +1607,14 @@ mod tests {
let weights_m = DeviceBuf::alloc(top_k * 4).unwrap(); weights_m.h2d(&weights_h[m * top_k..(m + 1) * top_k]).unwrap();
let moeout_s = moe_device(&cache, ip, &mlpin_m, &ids_m, &weights_m, &d, h).unwrap();
let mut os = vec![0f32; h]; moeout_s.d2h(&mut os).unwrap();
for j in 0..h { max_rel = max_rel.max((ob[m * h + j] - os[j]).abs() / os[j].abs().max(1e-3)); }
os_all[m * h..(m + 1) * h].copy_from_slice(&os);
}
// Global-scale normalization: P1 WMMA (ip=0 NVFP4) vs serial fdot2 differs
// ~1e-4 absolute, which explodes per-entry on near-zero expert outputs.
// ip=20 MXFP8 uses the same Wave32 fdot2 on both paths => bit-exact.
let scale = os_all.iter().fold(0f32, |mx, v| mx.max(v.abs())).max(1e-3);
let mut max_rel = 0f32;
for (g, w) in ob.iter().zip(os_all.iter()) { max_rel = max_rel.max((g - w).abs() / scale); }
eprintln!("moe_batched ip={ip} B={b} max_rel_err={max_rel:.3e}");
assert!(max_rel < 3e-3, "batched MoE parity failed at ip={ip}: {max_rel}");
}
@@ -1652,8 +1658,13 @@ mod tests {
let mut o = vec![0f32; h]; cur_m.d2h(&mut o).unwrap();
cs[m * h..(m + 1) * h].copy_from_slice(&o);
}
// Normalize by the GLOBAL cur scale, not per-entry: the P1 WMMA MoE differs
// from serial fdot2 by ~1e-4 absolute, which explodes as a per-entry relative
// error on near-zero residual entries. The KV cache stays byte-identical
// (attention precedes MoE) — that is the strong correctness gate here.
let cscale = cs.iter().fold(0f32, |m, v| m.max(v.abs())).max(1e-3);
let mut max_rel = 0f32;
for (g, wnt) in cb.iter().zip(cs.iter()) { max_rel = max_rel.max((g - wnt).abs() / wnt.abs().max(1e-3)); }
for (g, wnt) in cb.iter().zip(cs.iter()) { max_rel = max_rel.max((g - wnt).abs() / cscale); }
// KV cache byte-compare (f16).
let kvn = d.n_kv * max_seq * d.hd * 2;
let mut kb = vec![0u8; kvn]; st_b.k_cache[0].d2h_bytes(&mut kb).unwrap();
@@ -1702,8 +1713,13 @@ mod tests {
let mut o = vec![0f32; h]; cur_m.d2h(&mut o).unwrap();
cs[m * h..(m + 1) * h].copy_from_slice(&o);
}
// Normalize by the GLOBAL cur scale, not per-entry: the P1 WMMA MoE differs
// from serial fdot2 by ~1e-4 absolute, which explodes as a per-entry relative
// error on near-zero residual entries. The KV cache stays byte-identical
// (attention precedes MoE) — that is the strong correctness gate here.
let cscale = cs.iter().fold(0f32, |m, v| m.max(v.abs())).max(1e-3);
let mut max_rel = 0f32;
for (g, wnt) in cb.iter().zip(cs.iter()) { max_rel = max_rel.max((g - wnt).abs() / wnt.abs().max(1e-3)); }
for (g, wnt) in cb.iter().zip(cs.iter()) { max_rel = max_rel.max((g - wnt).abs() / cscale); }
let kvn = d.n_kv * max_seq * d.hd * 2;
let mut kb = vec![0u8; kvn]; st_b.k_cache[0].d2h_bytes(&mut kb).unwrap();
let mut ks = vec![0u8; kvn]; st_s.k_cache[0].d2h_bytes(&mut ks).unwrap();
@@ -1744,7 +1760,12 @@ mod tests {
eprintln!("forward_batch B={b} argmax batch={argb} seq={args} max_abs={max_abs:.3e} max_rel={max_rel:.3e}");
set_kv_f16(false);
unsafe { std::env::remove_var("VETTA_DEVICE_MOE") };
assert_eq!(argb, args, "forward_batch argmax must match sequential");
assert!(max_abs < 1e-2, "forward_batch logits parity failed: max_abs={max_abs}");
assert_eq!(argb, args, "forward_batch argmax must match sequential (greedy gate)");
// P1 tolerance: prefill MoE uses FP16 WMMA (tensor-core f32 accumulation
// order) while M=1 decode uses serial fdot2. The ~1e-4 per-expert difference
// amplifies through 24 layers to ~7e-2 absolute on softcap-bounded (+/-30)
// logits (~2e-3 relative). The argmax above + gw serve_gen_multi_token_parity
// (greedy-vs-reference EXACT) are the blocking production gates.
assert!(max_abs < 0.15, "forward_batch logits parity failed: max_abs={max_abs}");
}
}
+6
View File
@@ -205,6 +205,12 @@ pub fn expert_arena_gemv_f16(a: &ExpertArena, ids: &DeviceBuf, slot: i32, x: &De
pub fn expert_arena_grouped_gemv_f16(a: &ExpertArena, exp_off: &DeviceBuf, bucket_tok: Option<&DeviceBuf>, x: &DeviceBuf, y: &DeviceBuf) -> Result<(), String> {
if a.mxfp8 {
vetta_infer_hip::mxfp8_grouped_gemv_f16(&a.packed, &a.scales, exp_off, bucket_tok, x, y, a.n, a.k, a.n_exp as i32, a.w_stride, a.sc_stride)
} else if a.k % 64 == 0 && std::env::var("VETTA_NO_MOE_WMMA").is_err() {
// P1: FP16 WMMA tensor-core grouped GEMM (~8x fdot2 ALU throughput). NVFP4
// E2M1 dequant is exact in f16; only the f32 accumulation order differs
// (~1e-4 measured). K%64==0 required by the BK=64 LDS slab (Inkling w13
// K=2304, w2 K=1152). VETTA_NO_MOE_WMMA=1 falls back to fdot2 for A/B.
vetta_infer_hip::nvfp4_grouped_gemm_wmma(&a.packed, &a.scales, &a.outer, exp_off, bucket_tok, x, y, a.n, a.k, a.n_exp as i32, a.w_stride, a.sc_stride)
} else {
vetta_infer_hip::nvfp4_grouped_gemv_f16(&a.packed, &a.scales, &a.outer, exp_off, bucket_tok, x, y, a.n, a.k, a.n_exp as i32, a.w_stride, a.sc_stride)
}