[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() {
|
||||
|
||||
@@ -1565,3 +1565,96 @@ extern "C" int vetta_moe_scatter_buckets(const int* topk_ids, int* exp_cur, int*
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_nvfp4_grouped_gemv_f16: grouped MoE expert GEMM over buckets ---------
|
||||
// One launch covers ALL experts (grid.y = n_experts). Expert eid processes its
|
||||
// bucket tokens [exp_off[eid], exp_off[eid+1]) read from bucket_tok; the W group
|
||||
// is dequantized once per MT-token tile (fdot2), amortizing the expert weight
|
||||
// bandwidth MT-fold across the tokens routed to that expert. X is [B,K] f16
|
||||
// (gathered by bucket_tok), Y is [total_pairs,N] f32 in bucket order. This is the
|
||||
// prefill MoE GEMM (w13 and w2 both NVFP4); SiTU + reduce_buckets follow.
|
||||
template <int MT>
|
||||
__global__ void __launch_bounds__(256) k_nvfp4_grouped_gemv_f16(
|
||||
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 wave = threadIdx.x >> 5;
|
||||
const int lane = threadIdx.x & 31;
|
||||
const int row = blockIdx.x * 8 + wave;
|
||||
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 (row >= N || m_e <= 0) return;
|
||||
const uint8_t* wp = arena_packed + (long)eid * w_stride + (size_t)row * (K >> 1);
|
||||
const uint8_t* ws = arena_scales + (long)eid * sc_stride + (size_t)row * (K >> 4);
|
||||
const float outer = outer_arena[eid];
|
||||
const __half* xh = reinterpret_cast<const __half*>(X);
|
||||
const int nblk16 = K >> 4;
|
||||
for (int t0 = 0; t0 < m_e; t0 += MT) {
|
||||
float acc[MT];
|
||||
#pragma unroll
|
||||
for (int m = 0; m < MT; ++m) acc[m] = 0.f;
|
||||
for (int b = lane; b < nblk16; b += 32) {
|
||||
const float sc = e4m3fn_to_f32(ws[b]) * outer;
|
||||
const uint2 raw = *reinterpret_cast<const uint2*>(wp + b * 8);
|
||||
uint32_t w2[8];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const uint32_t byte0 = (raw.x >> (i * 8)) & 0xFFu;
|
||||
const uint32_t byte1 = (raw.y >> (i * 8)) & 0xFFu;
|
||||
const __half2 h0 = __halves2half2(sf4_e2m1_f16(byte0 & 15u), sf4_e2m1_f16(byte0 >> 4));
|
||||
const __half2 h1 = __halves2half2(sf4_e2m1_f16(byte1 & 15u), sf4_e2m1_f16(byte1 >> 4));
|
||||
w2[i] = __builtin_bit_cast(uint32_t, h0);
|
||||
w2[4 + i] = __builtin_bit_cast(uint32_t, h1);
|
||||
}
|
||||
const int k0 = b << 4;
|
||||
#pragma unroll
|
||||
for (int m = 0; m < MT; ++m) {
|
||||
if (t0 + m < m_e) {
|
||||
const int tok = bucket_tok[beg + t0 + m];
|
||||
const size_t xo = (size_t)tok * K + k0;
|
||||
const uint4 x0 = *reinterpret_cast<const uint4*>(xh + xo);
|
||||
const uint4 x1 = *reinterpret_cast<const uint4*>(xh + xo + 8);
|
||||
float d = 0.f;
|
||||
d = fdot2_f32(w2[0], x0.x, d);
|
||||
d = fdot2_f32(w2[1], x0.y, d);
|
||||
d = fdot2_f32(w2[2], x0.z, d);
|
||||
d = fdot2_f32(w2[3], x0.w, d);
|
||||
d = fdot2_f32(w2[4], x1.x, d);
|
||||
d = fdot2_f32(w2[5], x1.y, d);
|
||||
d = fdot2_f32(w2[6], x1.z, d);
|
||||
d = fdot2_f32(w2[7], x1.w, d);
|
||||
acc[m] = fmaf(d, sc, acc[m]);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma unroll
|
||||
for (int m = 0; m < MT; ++m) {
|
||||
if (t0 + m < m_e) {
|
||||
const float sum = wave32_sum(acc[m]);
|
||||
if (lane == 0) Y[(size_t)(beg + t0 + m) * N + row] = sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_nvfp4_grouped_gemv_f16(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 % 16) != 0 || n_experts <= 0) return 1;
|
||||
constexpr int MT = 16;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
dim3 grid((N + 7) / 8, n_experts);
|
||||
k_nvfp4_grouped_gemv_f16<MT><<<grid, 256, 0, s>>>(
|
||||
arena_packed, arena_scales, outer_arena, exp_off, bucket_tok, X, Y, N, K, w_stride, sc_stride);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user