[HIP] P1: FP16 WMMA grouped NVFP4 MoE GEMM (tensor cores)
First P1 WMMA brick (Federico approved relaxing the bit-exact M=1-vs-M=B parity to a numeric tolerance; greedy-vs-reference stays the blocking gate). Adds gfx11 (RDNA3.5) FP16 WMMA helpers (v_h16/v_f8, wmma_f32_16x16x16_f16_w32, acc lane map 2r+(lane>>4)) + k_nvfp4_grouped_gemm_wmma: a drop-in for the fdot2 grouped GEMM with the same arena/CSR I/O. BM=16 (one M tile ~ tokens/expert at chunk 256), BN=128 (8 waves, one 16x16 N tile each), BK=64 K-slab in LDS. NVFP4 E2M1 -> exact FP16 prescaled e4m3*0.0625, WMMA f32 accumulation, epilogue *outer*16. Wired into expert_arena_grouped_gemv_f16 for NVFP4 (K%64==0); VETTA_NO_MOE_WMMA=1 A/B fallback. M=1 decode keeps fdot2 (WMMA needs M>=16 tiles). Verified: WMMA-vs-CPU-oracle parity max_rel=1.2e-4; gw serve_gen greedy-vs- reference EXACT (16 tokens + Paris + EOS, 4/4); forward_batch argmax batch==seq. Relaxed the internal batched-vs-sequential tolerances to the P1 numeric budget (global-scale normalization, not per-entry near-zero): forward_batch max_abs<0.15 (~2e-3 rel on +/-30 logits), single-layer cur + MoE global-scale <3e-3, KV cache stays BYTE-IDENTICAL (attention precedes MoE). MEASURED prefill 2048tok A/B: fdot2 41.6s -> WMMA 36.9s (-11%), moe_launch ~1888->1400ms (-26%). Gain bounded by dequant/bandwidth (the reference INT8-fused sf4_i8 path is the further 2x axis). Suite green: hip34 ld23 gw4 GPU serial, CPU -D warnings, Lean 0 sorryAx.
This commit is contained in:
1 parent
47f8b52070
commit
755626fae2
4 files changed
+253
-6
No files matched your search
@@ -908,6 +908,31 @@ __device__ __forceinline__ float wave32_sum(float v) {
|
||||
return v;
|
||||
}
|
||||
|
||||
// ---- RDNA3.5 (gfx11) FP16 WMMA tensor-core helpers -------------------------
|
||||
// 16x16x16 f16->f32 matrix tile. The f16 product is exact in f32; only the
|
||||
// accumulation ORDER differs from serial fdot2 (within the ~1e-3 parity budget
|
||||
// Federico approved for P1; greedy-vs-reference is the blocking gate).
|
||||
typedef _Float16 v_h2 __attribute__((ext_vector_type(2)));
|
||||
typedef _Float16 v_h16 __attribute__((ext_vector_type(16)));
|
||||
typedef float v_f8 __attribute__((ext_vector_type(8)));
|
||||
__device__ __forceinline__ v_f8 v_f8_zero() { return v_f8{0,0,0,0,0,0,0,0}; }
|
||||
__device__ __forceinline__ uint32_t h2_splat_f(float f) { const v_h2 h{(_Float16)f,(_Float16)f}; return __builtin_bit_cast(uint32_t,h); }
|
||||
__device__ __forceinline__ uint32_t h2_mul_f(uint32_t a, uint32_t b) { return __builtin_bit_cast(uint32_t, __builtin_bit_cast(v_h2,a)*__builtin_bit_cast(v_h2,b)); }
|
||||
// Load 16 consecutive f16 (one K-row of a 16x16 tile) as the WMMA operand vector.
|
||||
__device__ __forceinline__ v_h16 wmma_ld_h16(const uint16_t* p) {
|
||||
const uint4 a = *reinterpret_cast<const uint4*>(p);
|
||||
const uint4 b = *reinterpret_cast<const uint4*>(p + 8);
|
||||
typedef unsigned u32x8 __attribute__((ext_vector_type(8)));
|
||||
const u32x8 v = {a.x,a.y,a.z,a.w,b.x,b.y,b.z,b.w};
|
||||
return __builtin_bit_cast(v_h16, v);
|
||||
}
|
||||
__device__ __forceinline__ v_f8 wmma_f16_g11(v_h16 a, v_h16 b, v_f8 c) {
|
||||
return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b, c);
|
||||
}
|
||||
// gfx11 accumulator lane mapping: lane l holds col (l&15), rows {2r+(l>>4)}.
|
||||
__device__ __forceinline__ int wmma_acc_m11(int r, int lane) { return 2*r + (lane>>4); }
|
||||
__device__ __forceinline__ int wmma_acc_n11(int lane) { return lane & 15; }
|
||||
|
||||
// ---- k_nvfp4_gemv (M=1 decode): y[N] = dequant(W)[N,K] @ x[K] --------------
|
||||
// One block per output row; threads stride over K/16 groups, f32 accumulate.
|
||||
// Correctness-first; the Wave32 bandwidth-saturated path is the performance pass.
|
||||
@@ -2244,6 +2269,120 @@ extern "C" int vetta_nvfp4_grouped_gemv_f16(const uint8_t* arena_packed,
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_nvfp4_grouped_gemm_wmma: FP16 WMMA tensor-core grouped MoE GEMM ------
|
||||
// Drop-in for k_nvfp4_grouped_gemv_f16 (same arena/CSR I/O, Y[total,N] f32 in
|
||||
// bucket order). BM=16 (one M tile ~ tokens/expert at chunk 256), BN=128 (8 waves,
|
||||
// one 16x16 N tile each), BK=64 K-slab staged in LDS. NVFP4 E2M1 -> exact FP16
|
||||
// prescaled by e4m3*0.0625 (exact in f16), WMMA f32 accumulation across K, epilogue
|
||||
// *outer*16. Matrix cores give ~8x the fdot2 ALU throughput. Accumulation order
|
||||
// differs from serial fdot2 => ~1e-3 (P1 tolerance; greedy-vs-reference is the gate).
|
||||
#define GW_BM 16
|
||||
#define GW_BN 128
|
||||
#define GW_BK 64
|
||||
#define GW_LD (GW_BK + 8)
|
||||
__global__ void __launch_bounds__(256) k_nvfp4_grouped_gemm_wmma(
|
||||
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 eid = blockIdx.y;
|
||||
const int beg = exp_off[eid];
|
||||
const int end = exp_off[eid + 1];
|
||||
const int m_e = end - beg;
|
||||
if (m_e <= 0) return;
|
||||
const int n_blk = blockIdx.x * GW_BN;
|
||||
if (n_blk >= N) return;
|
||||
const uint8_t* wp = arena_packed + (long)eid * w_stride;
|
||||
const uint8_t* ws = arena_scales + (long)eid * sc_stride;
|
||||
const float out_scale = outer_arena[eid] * 16.0f;
|
||||
__shared__ uint16_t sX[GW_BM * GW_LD];
|
||||
__shared__ uint16_t sW[GW_BN * GW_LD];
|
||||
const int tid = threadIdx.x;
|
||||
const int wave = tid >> 5; // 0..7 -> one 16-wide N tile
|
||||
const int lane = tid & 31;
|
||||
const int l16 = lane & 15;
|
||||
for (int t0 = 0; t0 < m_e; t0 += GW_BM) {
|
||||
const int tm = (m_e - t0 < GW_BM) ? (m_e - t0) : GW_BM;
|
||||
v_f8 acc = v_f8_zero();
|
||||
for (int k0 = 0; k0 < K; k0 += GW_BK) {
|
||||
// load X [tm, BK] f16 (gather bucket tokens); 128 threads x uint4
|
||||
if (tid < 128) {
|
||||
const int xr = tid >> 3, xc = tid & 7;
|
||||
uint4 v = make_uint4(0,0,0,0);
|
||||
if (xr < tm) {
|
||||
const int tok = bucket_tok ? bucket_tok[beg + t0 + xr] : (beg + t0 + xr);
|
||||
v = *reinterpret_cast<const uint4*>(X + (size_t)tok * K + k0 + xc * 8);
|
||||
}
|
||||
*reinterpret_cast<uint4*>(&sX[xr * GW_LD + xc * 8]) = v;
|
||||
}
|
||||
// dequant W [BN, BK] NVFP4 -> f16 prescaled e4m3*0.0625 (512 groups)
|
||||
#pragma unroll
|
||||
for (int iter = 0; iter < 2; ++iter) {
|
||||
const int idx = iter * 256 + tid;
|
||||
const int r = idx >> 2, b4 = idx & 3;
|
||||
const int gn = n_blk + r;
|
||||
const int gb = (k0 >> 4) + b4;
|
||||
uint16_t* dst = &sW[r * GW_LD + b4 * 16];
|
||||
if (gn < N) {
|
||||
const float sc = e4m3fn_to_f32(ws[(size_t)gn * (K >> 4) + gb]) * 0.0625f;
|
||||
const uint32_t sc2 = h2_splat_f(sc);
|
||||
const uint2 raw = *reinterpret_cast<const uint2*>(wp + (size_t)gn * (K >> 1) + gb * 8);
|
||||
uint32_t o0[4], o1[4];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const uint32_t b0 = (raw.x >> (i*8)) & 0xFFu;
|
||||
const uint32_t b1 = (raw.y >> (i*8)) & 0xFFu;
|
||||
const uint32_t h0 = __builtin_bit_cast(uint32_t, __halves2half2(sf4_e2m1_f16(b0 & 15u), sf4_e2m1_f16(b0 >> 4)));
|
||||
const uint32_t h1 = __builtin_bit_cast(uint32_t, __halves2half2(sf4_e2m1_f16(b1 & 15u), sf4_e2m1_f16(b1 >> 4)));
|
||||
o0[i] = h2_mul_f(h0, sc2);
|
||||
o1[i] = h2_mul_f(h1, sc2);
|
||||
}
|
||||
*reinterpret_cast<uint4*>(dst) = make_uint4(o0[0],o0[1],o0[2],o0[3]);
|
||||
*reinterpret_cast<uint4*>(dst + 8) = make_uint4(o1[0],o1[1],o1[2],o1[3]);
|
||||
} else {
|
||||
*reinterpret_cast<uint4*>(dst) = make_uint4(0,0,0,0);
|
||||
*reinterpret_cast<uint4*>(dst + 8) = make_uint4(0,0,0,0);
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
// 4x 16x16x16 WMMA over the BK=64 slab; wave handles N cols [wave*16,+16)
|
||||
#pragma unroll
|
||||
for (int kk = 0; kk < 4; ++kk) {
|
||||
v_h16 a = wmma_ld_h16(&sX[l16 * GW_LD + kk * 16]);
|
||||
v_h16 b = wmma_ld_h16(&sW[(wave * 16 + l16) * GW_LD + kk * 16]);
|
||||
acc = wmma_f16_g11(a, b, acc);
|
||||
}
|
||||
__syncthreads();
|
||||
}
|
||||
const int gn = n_blk + wave * 16 + wmma_acc_n11(lane);
|
||||
#pragma unroll
|
||||
for (int r = 0; r < 8; ++r) {
|
||||
const int lm = wmma_acc_m11(r, lane);
|
||||
if (lm < tm && gn < N) {
|
||||
Y[(size_t)(beg + t0 + lm) * N + gn] = acc[r] * out_scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_nvfp4_grouped_gemm_wmma(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 % GW_BK) != 0 || n_experts <= 0) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
dim3 grid((N + GW_BN - 1) / GW_BN, n_experts);
|
||||
k_nvfp4_grouped_gemm_wmma<<<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;
|
||||
}
|
||||
|
||||
// ---- 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
|
||||
|
||||
Reference in new issue
Block a user