[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(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_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_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_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_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;
|
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(())
|
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.
|
/// Launch k_shortconv4_decode (static): updates `hist` ring in place.
|
||||||
#[cfg(feature = "hip")]
|
#[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> {
|
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}");
|
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]
|
#[test]
|
||||||
#[ignore]
|
#[ignore]
|
||||||
fn shortconv4_multistep_parity() {
|
fn shortconv4_multistep_parity() {
|
||||||
|
|||||||
@@ -1485,4 +1485,83 @@ extern "C" int vetta_ctrl_apply(const float* cur, const float* msg, const float*
|
|||||||
k_ctrl_apply<<<(n + nt - 1) / nt, nt, 0, s>>>(cur, msg, gain, forget, out, n);
|
k_ctrl_apply<<<(n + nt - 1) / nt, nt, 0, s>>>(cur, msg, gain, forget, out, n);
|
||||||
HIP_CHECK(hipGetLastError());
|
HIP_CHECK(hipGetLastError());
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Grouped-MoE bucket build (device-side CSR of tokens-per-expert) --------
|
||||||
|
// Exact strata k_moe_count/prefix/scatter_buckets. For total = B*top_k
|
||||||
|
// (token,expert) pairs, builds exp_off[129] (CSR offsets), bucket_tok[total]
|
||||||
|
// (bucket position -> token index) and slotk_to_bidx[total] (pair -> bucket
|
||||||
|
// position) so the grouped expert GEMM reads each expert's weights ONCE for all
|
||||||
|
// tokens routed to it (the prefill weight-bandwidth amortization). exp_cnt must
|
||||||
|
// be pre-zeroed [129]. No host round-trip; n_experts = 128 (Inkling).
|
||||||
|
|
||||||
|
__global__ void __launch_bounds__(256) k_moe_count_buckets(
|
||||||
|
const int* __restrict__ topk_ids,
|
||||||
|
int* __restrict__ exp_cnt, int total) {
|
||||||
|
__shared__ int s_cnt[128];
|
||||||
|
const int tid = threadIdx.x;
|
||||||
|
if (tid < 128) s_cnt[tid] = 0;
|
||||||
|
__syncthreads();
|
||||||
|
const int stride = gridDim.x * blockDim.x;
|
||||||
|
for (int i = blockIdx.x * blockDim.x + tid; i < total; i += stride) {
|
||||||
|
const int e = topk_ids[i];
|
||||||
|
if (e >= 0 && e < 128) atomicAdd(&s_cnt[e], 1);
|
||||||
|
}
|
||||||
|
__syncthreads();
|
||||||
|
if (tid < 128 && s_cnt[tid] > 0) atomicAdd(&exp_cnt[tid], s_cnt[tid]);
|
||||||
|
}
|
||||||
|
|
||||||
|
__global__ void __launch_bounds__(128) k_moe_prefix_buckets(
|
||||||
|
const int* __restrict__ exp_cnt,
|
||||||
|
int* __restrict__ exp_off, int* __restrict__ exp_cur) {
|
||||||
|
__shared__ int s_off[129];
|
||||||
|
const int e = threadIdx.x;
|
||||||
|
s_off[e] = exp_cnt[e];
|
||||||
|
__syncthreads();
|
||||||
|
if (e == 0) {
|
||||||
|
int acc = 0;
|
||||||
|
for (int i = 0; i < 128; ++i) { const int c = s_off[i]; s_off[i] = acc; acc += c; }
|
||||||
|
s_off[128] = acc;
|
||||||
|
exp_off[128] = acc;
|
||||||
|
}
|
||||||
|
__syncthreads();
|
||||||
|
exp_off[e] = s_off[e];
|
||||||
|
exp_cur[e] = s_off[e];
|
||||||
|
}
|
||||||
|
|
||||||
|
__global__ void __launch_bounds__(256) k_moe_scatter_buckets(
|
||||||
|
const int* __restrict__ topk_ids,
|
||||||
|
int* __restrict__ exp_cur, int* __restrict__ bucket_tok,
|
||||||
|
int* __restrict__ slotk_to_bidx, int total, int top_k) {
|
||||||
|
const int i = blockIdx.x * blockDim.x + threadIdx.x;
|
||||||
|
if (i >= total) return;
|
||||||
|
const int e = topk_ids[i];
|
||||||
|
const int pos = atomicAdd(&exp_cur[e], 1);
|
||||||
|
bucket_tok[pos] = i / top_k;
|
||||||
|
slotk_to_bidx[i] = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" int vetta_moe_count_buckets(const int* topk_ids, int* exp_cnt, int total, vetta_stream_t stream) {
|
||||||
|
if (total <= 0) return 1;
|
||||||
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||||
|
const int cblks = (total + 255) / 256 < 64 ? (total + 255) / 256 : 64;
|
||||||
|
k_moe_count_buckets<<<cblks, 256, 0, s>>>(topk_ids, exp_cnt, total);
|
||||||
|
HIP_CHECK(hipGetLastError());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" int vetta_moe_prefix_buckets(const int* exp_cnt, int* exp_off, int* exp_cur, vetta_stream_t stream) {
|
||||||
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||||
|
k_moe_prefix_buckets<<<1, 128, 0, s>>>(exp_cnt, exp_off, exp_cur);
|
||||||
|
HIP_CHECK(hipGetLastError());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" int vetta_moe_scatter_buckets(const int* topk_ids, int* exp_cur, int* bucket_tok,
|
||||||
|
int* slotk_to_bidx, int total, int top_k, vetta_stream_t stream) {
|
||||||
|
if (total <= 0 || top_k <= 0) return 1;
|
||||||
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||||
|
k_moe_scatter_buckets<<<(total + 255) / 256, 256, 0, s>>>(topk_ids, exp_cur, bucket_tok, slotk_to_bidx, total, top_k);
|
||||||
|
HIP_CHECK(hipGetLastError());
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user