[HIP] reduce_buckets_masked: scatter-back + weighted reduce per token (batched MoE closed)

k_moe_reduce_buckets_masked (exact strata semantics): bucket_y [total,D] in bucket
order + slotk_to_bidx maps pair (b*top_k+kk) -> bucket position; out[b,d] =
sum_kk tk_w[b*top_k+kk]*bucket_y[s2b[b*top_k+kk]*D+d]. grid (D tiles, B).

This CLOSES the batched MoE compute path: bucket CSR (count/prefix/scatter) ->
grouped expert GEMM (W amortized MT-fold) -> reduce_buckets_masked (scatter-back +
weighted reduce). All device-side, no host round-trip. Parity vs CPU oracle:
max_rel 1.75e-5 (f32 FMA accumulation noise, threshold 1e-4).

Additive — decode default unchanged. Remaining for batched PREFILL: batched SiTU
(M=total), batched attention (flash causal M=B + ShortConv prefill + attn_res_mix
M=B + kv_store B), batched depth-blocks/controllers/bank, forward_batch + serve
wiring + EXACT parity.

Verified: hip GPU 23 green, workspace CPU -D warnings green, 6 Lean 0 sorryAx.
This commit is contained in:
adm committed 2026-12-01 23:35:55 +01:00
1 parent 870133d6e5
commit b6113b549a
2 files changed
+88

No files matched your search

+32
View File
@@ -1657,4 +1657,36 @@ extern "C" int vetta_nvfp4_grouped_gemv_f16(const uint8_t* arena_packed,
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
// token b: out[b,d] = sum_kk tk_w[b*top_k+kk] * bucket_y[s2b[b*top_k+kk]*D+d].
__global__ void k_moe_reduce_buckets_masked(
const float* __restrict__ bucket_y,
const int* __restrict__ slotk_to_bidx,
const float* __restrict__ tk_w,
float* __restrict__ out, int B, int top_k, int D) {
const int d = blockIdx.x * blockDim.x + threadIdx.x;
const int b = blockIdx.y;
if (d >= D || b >= B) return;
float acc = 0.f;
for (int kk = 0; kk < top_k; ++kk) {
const int pair = b * top_k + kk;
const int bp = slotk_to_bidx[pair];
acc += tk_w[pair] * bucket_y[(size_t)bp * D + d];
}
out[(size_t)b * D + d] = acc;
}
extern "C" int vetta_moe_reduce_buckets_masked(const float* bucket_y,
const int* slotk_to_bidx, const float* tk_w, float* out,
int B, int top_k, int D, vetta_stream_t stream) {
if (B <= 0 || top_k <= 0 || D <= 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
dim3 grid((D + 255) / 256, B);
k_moe_reduce_buckets_masked<<<grid, 256, 0, s>>>(bucket_y, slotk_to_bidx, tk_w, out, B, top_k, D);
HIP_CHECK(hipGetLastError());
return 0;
}