[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
@@ -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);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
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