[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
@@ -1564,4 +1564,97 @@ extern "C" int vetta_moe_scatter_buckets(const int* topk_ids, int* exp_cur, int*
|
||||
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;
|
||||
}
|
||||
|
||||
// ---- 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