diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index 61c70fc..bd38202 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -119,6 +119,11 @@ mod ffi { 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_nvfp4_grouped_gemm_wmma( + 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_moe_reduce_buckets_masked(bucket_y: *const c_float, slotk_to_bidx: *const c_int, tk_w: *const c_float, out: *mut c_float, b: c_int, top_k: c_int, d: c_int, 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; @@ -735,6 +740,21 @@ pub fn nvfp4_grouped_gemv_f16(arena_packed: &DeviceBuf, arena_scales: &DeviceBuf Ok(()) } +/// FP16 WMMA tensor-core grouped MoE GEMM (drop-in for nvfp4_grouped_gemv_f16). +/// Same arena/CSR I/O and Y[total,N] f32 bucket order; K must be a multiple of 64. +/// Matrix-core 16x16x16 tiles (~8x fdot2 ALU throughput). Accumulation order differs +/// from serial fdot2 => ~1e-3 (P1 tolerance; greedy-vs-reference is the blocking gate). +#[cfg(feature = "hip")] +#[allow(clippy::too_many_arguments)] +pub fn nvfp4_grouped_gemm_wmma(arena_packed: &DeviceBuf, arena_scales: &DeviceBuf, outer_arena: &DeviceBuf, exp_off: &DeviceBuf, bucket_tok: Option<&DeviceBuf>, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32, n_experts: i32, w_stride: i64, sc_stride: i64) -> Result<(), String> { + let bt = bucket_tok.map(|b| b.ptr as *const i32).unwrap_or(std::ptr::null()); + let rc = unsafe { + ffi::vetta_nvfp4_grouped_gemm_wmma(arena_packed.ptr as *const u8, arena_scales.ptr as *const u8, outer_arena.ptr as *const f32, exp_off.ptr as *const i32, bt, 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_gemm_wmma launch failed".into()); } + Ok(()) +} + /// Scatter-back + weighted reduce per token from bucket-order expert outputs: /// out[b,d] = sum_kk tk_w[b*top_k+kk] * bucket_y[slotk_to_bidx[b*top_k+kk]*D+d]. #[cfg(feature = "hip")] @@ -1474,6 +1494,67 @@ mod gpu_tests { assert!(max_rel < 2e-3, "grouped NVFP4 GEMM parity failed: {max_rel}"); } + /// FP16 WMMA grouped GEMM parity vs the same CPU oracle. N=128 exercises all 8 + /// wave N-tiles; K=128 (multiple of BK=64). WMMA changes the f32 accumulation + /// order and prescales the dequantized weight in f16 (e4m3*0.0625 exact, E2M1*scale + /// rounded) => P1 numeric tolerance (~1e-3), not bit-exact. Greedy-vs-reference is + /// the blocking production gate (ld forward_batch_parity / gw serve_gen). + #[test] + #[ignore] + fn nvfp4_grouped_gemm_wmma_parity() { + let (b, top_k, n_exp, n, k) = (20usize, 8usize, 4usize, 128usize, 128usize); + let total = b * top_k; + let w_stride = n * (k / 2); + let sc_stride = n * (k / 16); + let packed: Vec = (0..n_exp * w_stride).map(|i| (i % 256) as u8).collect(); + let scales: Vec = (0..n_exp * sc_stride).map(|i| ((i * 7 + 3) % 251) as u8).collect(); + let outer: Vec = (0..n_exp).map(|i| 0.3 + 0.1 * i as f32).collect(); + let ids: Vec = (0..total).map(|i| ((i * 7 + (i / top_k) * 13) % n_exp) as i32).collect(); + let xf: Vec = (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 = 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_gemm_wmma(&ap, &asc, &ao, &doff, Some(&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 = 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_gemm_wmma B={b} experts={n_exp} pairs={total} N={n} K={k} max_rel_err={max_rel:.3e}"); + assert!(max_rel < 5e-3, "WMMA grouped NVFP4 GEMM parity failed: {max_rel}"); + } + /// reduce_buckets_masked parity: scatter-back from bucket order + weighted /// reduce per token vs a CPU oracle using the same slotk_to_bidx map. #[test] diff --git a/crates/vetta-infer-ld/src/layer.rs b/crates/vetta-infer-ld/src/layer.rs index 0e611ad..f05be2a 100644 --- a/crates/vetta-infer-ld/src/layer.rs +++ b/crates/vetta-infer-ld/src/layer.rs @@ -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 = 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}"); } } \ No newline at end of file diff --git a/crates/vetta-infer-ld/src/packer.rs b/crates/vetta-infer-ld/src/packer.rs index 4a8f6f1..ac3f7b7 100644 --- a/crates/vetta-infer-ld/src/packer.rs +++ b/crates/vetta-infer-ld/src/packer.rs @@ -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) } diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index 7d0f881..176c93d 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -908,6 +908,31 @@ __device__ __forceinline__ float wave32_sum(float v) { return v; } +// ---- RDNA3.5 (gfx11) FP16 WMMA tensor-core helpers ------------------------- +// 16x16x16 f16->f32 matrix tile. The f16 product is exact in f32; only the +// accumulation ORDER differs from serial fdot2 (within the ~1e-3 parity budget +// Federico approved for P1; greedy-vs-reference is the blocking gate). +typedef _Float16 v_h2 __attribute__((ext_vector_type(2))); +typedef _Float16 v_h16 __attribute__((ext_vector_type(16))); +typedef float v_f8 __attribute__((ext_vector_type(8))); +__device__ __forceinline__ v_f8 v_f8_zero() { return v_f8{0,0,0,0,0,0,0,0}; } +__device__ __forceinline__ uint32_t h2_splat_f(float f) { const v_h2 h{(_Float16)f,(_Float16)f}; return __builtin_bit_cast(uint32_t,h); } +__device__ __forceinline__ uint32_t h2_mul_f(uint32_t a, uint32_t b) { return __builtin_bit_cast(uint32_t, __builtin_bit_cast(v_h2,a)*__builtin_bit_cast(v_h2,b)); } +// Load 16 consecutive f16 (one K-row of a 16x16 tile) as the WMMA operand vector. +__device__ __forceinline__ v_h16 wmma_ld_h16(const uint16_t* p) { + const uint4 a = *reinterpret_cast(p); + const uint4 b = *reinterpret_cast(p + 8); + typedef unsigned u32x8 __attribute__((ext_vector_type(8))); + const u32x8 v = {a.x,a.y,a.z,a.w,b.x,b.y,b.z,b.w}; + return __builtin_bit_cast(v_h16, v); +} +__device__ __forceinline__ v_f8 wmma_f16_g11(v_h16 a, v_h16 b, v_f8 c) { + return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b, c); +} +// gfx11 accumulator lane mapping: lane l holds col (l&15), rows {2r+(l>>4)}. +__device__ __forceinline__ int wmma_acc_m11(int r, int lane) { return 2*r + (lane>>4); } +__device__ __forceinline__ int wmma_acc_n11(int lane) { return lane & 15; } + // ---- k_nvfp4_gemv (M=1 decode): y[N] = dequant(W)[N,K] @ x[K] -------------- // One block per output row; threads stride over K/16 groups, f32 accumulate. // Correctness-first; the Wave32 bandwidth-saturated path is the performance pass. @@ -2244,6 +2269,120 @@ extern "C" int vetta_nvfp4_grouped_gemv_f16(const uint8_t* arena_packed, return 0; } +// ---- k_nvfp4_grouped_gemm_wmma: FP16 WMMA tensor-core grouped MoE GEMM ------ +// Drop-in for k_nvfp4_grouped_gemv_f16 (same arena/CSR I/O, Y[total,N] f32 in +// bucket order). BM=16 (one M tile ~ tokens/expert at chunk 256), BN=128 (8 waves, +// one 16x16 N tile each), BK=64 K-slab staged in LDS. NVFP4 E2M1 -> exact FP16 +// prescaled by e4m3*0.0625 (exact in f16), WMMA f32 accumulation across K, epilogue +// *outer*16. Matrix cores give ~8x the fdot2 ALU throughput. Accumulation order +// differs from serial fdot2 => ~1e-3 (P1 tolerance; greedy-vs-reference is the gate). +#define GW_BM 16 +#define GW_BN 128 +#define GW_BK 64 +#define GW_LD (GW_BK + 8) +__global__ void __launch_bounds__(256) k_nvfp4_grouped_gemm_wmma( + const uint8_t* __restrict__ arena_packed, + const uint8_t* __restrict__ arena_scales, + const float* __restrict__ outer_arena, + const int* __restrict__ exp_off, + const int* __restrict__ bucket_tok, + const uint16_t* __restrict__ X, + float* __restrict__ Y, int N, int K, + long w_stride, long sc_stride) { + const int eid = blockIdx.y; + const int beg = exp_off[eid]; + const int end = exp_off[eid + 1]; + const int m_e = end - beg; + if (m_e <= 0) return; + const int n_blk = blockIdx.x * GW_BN; + if (n_blk >= N) return; + const uint8_t* wp = arena_packed + (long)eid * w_stride; + const uint8_t* ws = arena_scales + (long)eid * sc_stride; + const float out_scale = outer_arena[eid] * 16.0f; + __shared__ uint16_t sX[GW_BM * GW_LD]; + __shared__ uint16_t sW[GW_BN * GW_LD]; + const int tid = threadIdx.x; + const int wave = tid >> 5; // 0..7 -> one 16-wide N tile + const int lane = tid & 31; + const int l16 = lane & 15; + for (int t0 = 0; t0 < m_e; t0 += GW_BM) { + const int tm = (m_e - t0 < GW_BM) ? (m_e - t0) : GW_BM; + v_f8 acc = v_f8_zero(); + for (int k0 = 0; k0 < K; k0 += GW_BK) { + // load X [tm, BK] f16 (gather bucket tokens); 128 threads x uint4 + if (tid < 128) { + const int xr = tid >> 3, xc = tid & 7; + uint4 v = make_uint4(0,0,0,0); + if (xr < tm) { + const int tok = bucket_tok ? bucket_tok[beg + t0 + xr] : (beg + t0 + xr); + v = *reinterpret_cast(X + (size_t)tok * K + k0 + xc * 8); + } + *reinterpret_cast(&sX[xr * GW_LD + xc * 8]) = v; + } + // dequant W [BN, BK] NVFP4 -> f16 prescaled e4m3*0.0625 (512 groups) + #pragma unroll + for (int iter = 0; iter < 2; ++iter) { + const int idx = iter * 256 + tid; + const int r = idx >> 2, b4 = idx & 3; + const int gn = n_blk + r; + const int gb = (k0 >> 4) + b4; + uint16_t* dst = &sW[r * GW_LD + b4 * 16]; + if (gn < N) { + const float sc = e4m3fn_to_f32(ws[(size_t)gn * (K >> 4) + gb]) * 0.0625f; + const uint32_t sc2 = h2_splat_f(sc); + const uint2 raw = *reinterpret_cast(wp + (size_t)gn * (K >> 1) + gb * 8); + uint32_t o0[4], o1[4]; + #pragma unroll + for (int i = 0; i < 4; ++i) { + const uint32_t b0 = (raw.x >> (i*8)) & 0xFFu; + const uint32_t b1 = (raw.y >> (i*8)) & 0xFFu; + const uint32_t h0 = __builtin_bit_cast(uint32_t, __halves2half2(sf4_e2m1_f16(b0 & 15u), sf4_e2m1_f16(b0 >> 4))); + const uint32_t h1 = __builtin_bit_cast(uint32_t, __halves2half2(sf4_e2m1_f16(b1 & 15u), sf4_e2m1_f16(b1 >> 4))); + o0[i] = h2_mul_f(h0, sc2); + o1[i] = h2_mul_f(h1, sc2); + } + *reinterpret_cast(dst) = make_uint4(o0[0],o0[1],o0[2],o0[3]); + *reinterpret_cast(dst + 8) = make_uint4(o1[0],o1[1],o1[2],o1[3]); + } else { + *reinterpret_cast(dst) = make_uint4(0,0,0,0); + *reinterpret_cast(dst + 8) = make_uint4(0,0,0,0); + } + } + __syncthreads(); + // 4x 16x16x16 WMMA over the BK=64 slab; wave handles N cols [wave*16,+16) + #pragma unroll + for (int kk = 0; kk < 4; ++kk) { + v_h16 a = wmma_ld_h16(&sX[l16 * GW_LD + kk * 16]); + v_h16 b = wmma_ld_h16(&sW[(wave * 16 + l16) * GW_LD + kk * 16]); + acc = wmma_f16_g11(a, b, acc); + } + __syncthreads(); + } + const int gn = n_blk + wave * 16 + wmma_acc_n11(lane); + #pragma unroll + for (int r = 0; r < 8; ++r) { + const int lm = wmma_acc_m11(r, lane); + if (lm < tm && gn < N) { + Y[(size_t)(beg + t0 + lm) * N + gn] = acc[r] * out_scale; + } + } + } +} + +extern "C" int vetta_nvfp4_grouped_gemm_wmma(const uint8_t* arena_packed, + const uint8_t* arena_scales, const float* outer_arena, + const int* exp_off, const int* bucket_tok, + const uint16_t* X, float* Y, int N, int K, int n_experts, + long w_stride, long sc_stride, vetta_stream_t stream) { + if (N <= 0 || K <= 0 || (K % GW_BK) != 0 || n_experts <= 0) return 1; + hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0; + dim3 grid((N + GW_BN - 1) / GW_BN, n_experts); + k_nvfp4_grouped_gemm_wmma<<>>( + arena_packed, arena_scales, outer_arena, exp_off, bucket_tok, X, Y, N, K, w_stride, sc_stride); + HIP_CHECK(hipGetLastError()); + return 0; +} + // ---- k_moe_reduce_buckets_masked: scatter-back + weighted reduce per token -- // Exact strata k_moe_reduce_buckets_masked. bucket_y is [total_pairs, D] in // bucket order; slotk_to_bidx maps pair (b*top_k+kk) -> bucket position. For each