[HIP] Grouped-MoE bucket CSR build (count/prefix/scatter): device-side tokens-per-expert
Port strata k_moe_count/prefix/scatter_buckets: for total=B*top_k (token,expert) pairs build exp_off[129] CSR offsets, bucket_tok[total] (bucket->token) and slotk_to_bidx[total] (pair->bucket position), entirely on device (no host round-trip). This lets the grouped expert GEMM read each expert weights ONCE for all tokens routed to it — the prefill weight-bandwidth amortization behind the reference 40:1 prefill speedup. Parity vs CPU oracle (B=37, top_k=8): CSR offsets == prefix-sum of counts, slotk_to_bidx consistent bijection, per-expert bucket token multiset exact (scatter order within a bucket is non-deterministic via atomicAdd, so invariants are checked rather than element order). Verified: hip GPU 21 green, workspace CPU -D warnings green, 6 Lean 0 sorryAx. Next: grouped w13/w2 GEMM indexed by bucket_tok + reduce_buckets_masked, then batched attention -> forward_batch.
This commit is contained in:
1 parent
bfdd665434
commit
e1b16b5874
2 files changed
+154
No files matched your search
@@ -96,6 +96,9 @@ mod ffi {
|
||||
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;
|
||||
pub fn vetta_moe_reduce(y_part: *const c_float, tk_w: *const c_float, out: *mut c_float, top_k: c_int, hidden: c_int, stream: *mut c_void) -> c_int;
|
||||
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_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;
|
||||
@@ -517,6 +520,31 @@ pub fn moe_reduce(y_part: &DeviceBuf, tk_w: &DeviceBuf, out: &DeviceBuf, top_k:
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Grouped-MoE bucket CSR build (device-side, no host round-trip). `exp_cnt`
|
||||
/// [129] must be pre-zeroed; after count+prefix, `exp_off` [129] holds CSR
|
||||
/// offsets and `exp_cur` [128] the write cursors; scatter fills `bucket_tok`
|
||||
/// [total] (bucket->token) and `slotk_to_bidx` [total] (pair->bucket position).
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn moe_count_buckets(topk_ids: &DeviceBuf, exp_cnt: &DeviceBuf, total: i32) -> Result<(), String> {
|
||||
let rc = unsafe { ffi::vetta_moe_count_buckets(topk_ids.ptr as *const i32, exp_cnt.ptr as *mut i32, total, std::ptr::null_mut()) };
|
||||
if rc != 0 { return Err("moe_count_buckets launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn moe_prefix_buckets(exp_cnt: &DeviceBuf, exp_off: &DeviceBuf, exp_cur: &DeviceBuf) -> Result<(), String> {
|
||||
let rc = unsafe { ffi::vetta_moe_prefix_buckets(exp_cnt.ptr as *const i32, exp_off.ptr as *mut i32, exp_cur.ptr as *mut i32, std::ptr::null_mut()) };
|
||||
if rc != 0 { return Err("moe_prefix_buckets launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn moe_scatter_buckets(topk_ids: &DeviceBuf, exp_cur: &DeviceBuf, bucket_tok: &DeviceBuf, slotk_to_bidx: &DeviceBuf, total: i32, top_k: i32) -> Result<(), String> {
|
||||
let rc = unsafe { ffi::vetta_moe_scatter_buckets(topk_ids.ptr as *const i32, exp_cur.ptr as *mut i32, bucket_tok.ptr as *mut i32, slotk_to_bidx.ptr as *mut i32, total, top_k, std::ptr::null_mut()) };
|
||||
if rc != 0 { return Err("moe_scatter_buckets 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> {
|
||||
@@ -1075,6 +1103,53 @@ mod gpu_tests {
|
||||
assert!(max_rel < 1e-5, "moe_reduce parity failed: {max_rel}");
|
||||
}
|
||||
|
||||
/// Grouped-MoE bucket CSR build parity vs a CPU oracle. Scatter order within
|
||||
/// a bucket is non-deterministic (atomicAdd), so we verify INVARIANTS: CSR
|
||||
/// offsets == CPU prefix-sum of counts, slotk_to_bidx is a consistent
|
||||
/// bijection (bucket_tok[s2b[i]] == i/top_k), and each expert's bucket holds
|
||||
/// exactly the multiset of tokens routed to it.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn moe_buckets_parity_vs_cpu() {
|
||||
let (b, top_k, n_exp) = (37usize, 8usize, 128usize);
|
||||
let total = b * top_k;
|
||||
let ids: Vec<i32> = (0..total).map(|i| ((i * 7 + (i / top_k) * 13) % n_exp) as i32).collect();
|
||||
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 rd = |buf: &DeviceBuf, n: usize| -> Vec<i32> {
|
||||
let mut bytes = vec![0u8; n * 4]; buf.d2h_bytes(&mut bytes).unwrap();
|
||||
bytes.chunks_exact(4).map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]])).collect()
|
||||
};
|
||||
let off = rd(&doff, 129);
|
||||
let btk = rd(&dbtk, total);
|
||||
let s2b = rd(&ds2b, total);
|
||||
let mut cnt = vec![0i32; 128];
|
||||
for &e in &ids { cnt[e as usize] += 1; }
|
||||
let mut coff = vec![0i32; 129];
|
||||
let mut acc = 0i32;
|
||||
for e in 0..128 { coff[e] = acc; acc += cnt[e]; }
|
||||
coff[128] = acc;
|
||||
assert_eq!(off, coff, "CSR offsets must match CPU prefix-sum");
|
||||
assert_eq!(off[128], total as i32);
|
||||
for i in 0..total {
|
||||
assert_eq!(btk[s2b[i] as usize], (i / top_k) as i32, "slotk_to_bidx bijection broken at {i}");
|
||||
}
|
||||
for e in 0..128 {
|
||||
let mut got: Vec<i32> = btk[off[e] as usize..off[e + 1] as usize].to_vec(); got.sort();
|
||||
let mut want: Vec<i32> = (0..total).filter(|&i| ids[i] == e as i32).map(|i| (i / top_k) as i32).collect(); want.sort();
|
||||
assert_eq!(got, want, "expert {e} bucket token multiset mismatch");
|
||||
}
|
||||
eprintln!("moe_buckets B={b} top_k={top_k}: CSR offsets + bijection + per-expert multiset exact");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn shortconv4_multistep_parity() {
|
||||
|
||||
Reference in new issue
Block a user