[HIP] MXFP8 Wave32 fdot2 (grouped + M=1): MoE -31%, prefill 44.4->40.9s

Peer review P2: layers 20-23 MXFP8 grouped GEMV was scalar (256 threads/row +
shared reduction). Rewrite both k_mxfp8_grouped_gemv_f16 (M=B) and
mxfp8_gemv_f16_body (M=1 decode/indirect) as Wave32 mirrors of the NVFP4 path:
one wave per row (8 rows/block), E4M3 group dequantized once to exact FP16 (3
mantissa bits => exact in f16), hardware fdot2_f32 partial dot + scale-fma, DPP
wave32_sum (no shared mem). Both paths share the per-token group order => M=1 and
M=B stay BIT-EXACT (forward_batch_parity max_abs=0.0 restored; mxfp8 grouped oracle
1.3e-6 < 2e-3). MoE GPU time 2727->1888ms/chunk (-31%, the 65% hotspot). Prefill
2048: 44.4->40.9s. Suite GREEN ld23 hip33 gw4 (serial, server stopped), CPU -D
warnings, Lean 6/6. Cumulative prefill 60->40.9s (-32%).
This commit is contained in:
adm committed 2026-12-02 01:56:25 +01:00
1 parent 0f0bb42949
commit 088ac1f66d
1 file changed
+102 -46
+102 -46
View File
@@ -1182,36 +1182,65 @@ extern "C" int vetta_mxfp8_gemv(const uint8_t* weights, const uint8_t* scales,
}
// ---- k_mxfp8_gemv_f16: FP16-activation variant (exact strata MX8 path) ------
// E4M3 weights are exact in FP16; group-of-32 partial dot then scale-fma.
// Wave32 mirror of nvfp4_gemv_f16_body: one WAVE per output row (8 rows/block),
// E4M3 group dequantized ONCE to exact FP16, hardware fdot2_f32 partial dot then
// scale-fma, DPP wave32 reduction. Same per-token group order as the grouped
// (batched) MXFP8 kernel => M=1 decode and M=B prefill stay bit-exact consistent.
__device__ __forceinline__ void mxfp8_gemv_f16_body(const uint8_t* __restrict__ weights,
const uint8_t* __restrict__ scales,
const uint16_t* __restrict__ x, float* __restrict__ y,
int N, int K) {
const int n = blockIdx.x;
if (n >= N) return;
const int tid = threadIdx.x;
const int nt = blockDim.x;
const uint8_t* wp = weights + (size_t)n * K;
const uint8_t* ws = scales + (size_t)n * (K >> 5);
const int nblk = K >> 5;
const int wave = threadIdx.x >> 5;
const int lane = threadIdx.x & 31;
const int row = blockIdx.x * 8 + wave;
if (row >= N) return;
const uint8_t* wp = weights + (size_t)row * K;
const uint8_t* ws = scales + (size_t)row * (K >> 5);
const __half* xh = reinterpret_cast<const __half*>(x);
const int nblk32 = K >> 5;
float acc = 0.f;
for (int b = tid; b < nblk; b += nt) {
for (int b = lane; b < nblk32; b += 32) {
const float sc = ue8m0_to_f32(ws[b]);
const int k0 = b << 5;
const uint8_t* w8 = wp + k0;
const uint4 wr0 = *reinterpret_cast<const uint4*>(wp + k0);
const uint4 wr1 = *reinterpret_cast<const uint4*>(wp + k0 + 16);
const uint32_t wb[8] = {wr0.x, wr0.y, wr0.z, wr0.w, wr1.x, wr1.y, wr1.z, wr1.w};
uint32_t w2[16];
#pragma unroll
for (int i = 0; i < 16; ++i) {
const int j0 = 2 * i, j1 = 2 * i + 1;
const uint32_t b0 = (wb[j0 >> 2] >> ((j0 & 3) * 8)) & 0xFFu;
const uint32_t b1 = (wb[j1 >> 2] >> ((j1 & 3) * 8)) & 0xFFu;
const __half h0 = __float2half_rn(e4m3fn_to_f32((uint8_t)b0));
const __half h1 = __float2half_rn(e4m3fn_to_f32((uint8_t)b1));
w2[i] = __builtin_bit_cast(uint32_t, __halves2half2(h0, h1));
}
const uint4 x0 = *reinterpret_cast<const uint4*>(xh + k0);
const uint4 x1 = *reinterpret_cast<const uint4*>(xh + k0 + 8);
const uint4 x2 = *reinterpret_cast<const uint4*>(xh + k0 + 16);
const uint4 x3 = *reinterpret_cast<const uint4*>(xh + k0 + 24);
float d = 0.f;
for (int j = 0; j < 32; ++j) d += f16_to_f32(x[k0 + j]) * e4m3fn_to_f32(w8[j]);
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);
d = fdot2_f32(w2[8], x2.x, d);
d = fdot2_f32(w2[9], x2.y, d);
d = fdot2_f32(w2[10], x2.z, d);
d = fdot2_f32(w2[11], x2.w, d);
d = fdot2_f32(w2[12], x3.x, d);
d = fdot2_f32(w2[13], x3.y, d);
d = fdot2_f32(w2[14], x3.z, d);
d = fdot2_f32(w2[15], x3.w, d);
acc = fmaf(d, sc, acc);
}
__shared__ float red[256];
red[tid] = acc;
__syncthreads();
for (int s = nt / 2; s > 0; s >>= 1) {
if (tid < s) red[tid] += red[tid + s];
__syncthreads();
}
if (tid == 0) y[n] = red[0];
const float sum = wave32_sum(acc);
if (lane == 0) y[row] = sum;
}
__global__ void k_mxfp8_gemv_f16(const uint8_t* __restrict__ weights,
@@ -1239,7 +1268,7 @@ extern "C" int vetta_mxfp8_gemv_f16_indirect(const uint8_t* arena_weights,
long w_stride, long sc_stride, long y_offset, vetta_stream_t stream) {
if (N <= 0 || K <= 0 || (K % 32) != 0 || slot < 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_mxfp8_gemv_f16_indirect<<<N, 256, 0, s>>>(arena_weights, arena_scales, ids, slot, x, y, N, K, w_stride, sc_stride, y_offset);
k_mxfp8_gemv_f16_indirect<<<(N + 7) / 8, 256, 0, s>>>(arena_weights, arena_scales, ids, slot, x, y, N, K, w_stride, sc_stride, y_offset);
HIP_CHECK(hipGetLastError());
return 0;
}
@@ -1249,7 +1278,7 @@ extern "C" int vetta_mxfp8_gemv_f16(const uint8_t* weights, const uint8_t* scale
vetta_stream_t stream) {
if (N <= 0 || K <= 0 || (K % 32) != 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_mxfp8_gemv_f16<<<N, 256, 0, s>>>(weights, scales, x, y, N, K);
k_mxfp8_gemv_f16<<<(N + 7) / 8, 256, 0, s>>>(weights, scales, x, y, N, K);
HIP_CHECK(hipGetLastError());
return 0;
}
@@ -1325,11 +1354,13 @@ extern "C" int vetta_situ_f16_batched(const float* gu, uint16_t* act, int inter,
}
// ---- k_mxfp8_grouped_gemv_f16: grouped MXFP8 expert GEMM (layers 20-23) -----
// MXFP8 analogue of k_nvfp4_grouped_gemv_f16: grid (N, n_experts); expert eid
// processes its bucket tokens [exp_off[eid],exp_off[eid+1]) via bucket_tok. The
// 32-byte E4M3 group is dequantized once per MT-token tile (UE8M0 scale, no outer),
// amortizing weight bandwidth MT-fold. X [B,K] f16 -> Y [total_pairs,N] f32 bucket
// order. Block reduction (256 threads) per token, MT=8 register tile.
// Wave32 mirror of k_nvfp4_grouped_gemv_f16: grid ((N+7)/8, n_experts), one WAVE
// per output row (8 rows/block), the 32-byte E4M3 group dequantized ONCE to exact
// FP16 (E4M3 has 3 mantissa bits => exact in FP16) and reused across MT tokens via
// hardware fdot2_f32 (v_dot2_f32_f16), DPP wave32 reduction (no shared mem). UE8M0
// scale applied per group AFTER the partial dot (acc = fmaf(d, sc, acc)). Same
// products as the scalar oracle; only the f32 accumulation order differs (within
// the 2e-3 parity tolerance, exactly like the NVFP4 fdot2 path).
template <int MT>
__global__ void __launch_bounds__(256) k_mxfp8_grouped_gemv_f16(
const uint8_t* __restrict__ arena_weights,
@@ -1339,36 +1370,64 @@ __global__ void __launch_bounds__(256) k_mxfp8_grouped_gemv_f16(
const uint16_t* __restrict__ X,
float* __restrict__ Y, int N, int K,
long w_stride, long sc_stride) {
const int n = blockIdx.x;
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 (n >= N || m_e <= 0) return;
const int tid = threadIdx.x;
const int nt = blockDim.x;
const uint8_t* wp = arena_weights + (long)eid * w_stride + (size_t)n * K;
const uint8_t* ws = arena_scales + (long)eid * sc_stride + (size_t)n * (K >> 5);
const int nblk = K >> 5;
__shared__ float red[256];
if (row >= N || m_e <= 0) return;
const uint8_t* wp = arena_weights + (long)eid * w_stride + (size_t)row * K;
const uint8_t* ws = arena_scales + (long)eid * sc_stride + (size_t)row * (K >> 5);
const __half* xh = reinterpret_cast<const __half*>(X);
const int nblk32 = K >> 5;
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 = tid; b < nblk; b += nt) {
for (int b = lane; b < nblk32; b += 32) {
const float sc = ue8m0_to_f32(ws[b]);
const int k0 = b << 5;
float wv[32];
const uint4 wr0 = *reinterpret_cast<const uint4*>(wp + k0);
const uint4 wr1 = *reinterpret_cast<const uint4*>(wp + k0 + 16);
const uint32_t wb[8] = {wr0.x, wr0.y, wr0.z, wr0.w, wr1.x, wr1.y, wr1.z, wr1.w};
uint32_t w2[16];
#pragma unroll
for (int j = 0; j < 32; ++j) wv[j] = e4m3fn_to_f32(wp[k0 + j]);
for (int i = 0; i < 16; ++i) {
const int j0 = 2 * i, j1 = 2 * i + 1;
const uint32_t b0 = (wb[j0 >> 2] >> ((j0 & 3) * 8)) & 0xFFu;
const uint32_t b1 = (wb[j1 >> 2] >> ((j1 & 3) * 8)) & 0xFFu;
const __half h0 = __float2half_rn(e4m3fn_to_f32((uint8_t)b0));
const __half h1 = __float2half_rn(e4m3fn_to_f32((uint8_t)b1));
w2[i] = __builtin_bit_cast(uint32_t, __halves2half2(h0, h1));
}
#pragma unroll
for (int m = 0; m < MT; ++m) {
if (t0 + m < m_e) {
const int tok = bucket_tok ? bucket_tok[beg + t0 + m] : (beg + t0 + m);
const uint16_t* xr = X + (size_t)tok * K + k0;
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);
const uint4 x2 = *reinterpret_cast<const uint4*>(xh + xo + 16);
const uint4 x3 = *reinterpret_cast<const uint4*>(xh + xo + 24);
float d = 0.f;
#pragma unroll
for (int j = 0; j < 32; ++j) d += f16_to_f32(xr[j]) * wv[j];
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);
d = fdot2_f32(w2[8], x2.x, d);
d = fdot2_f32(w2[9], x2.y, d);
d = fdot2_f32(w2[10], x2.z, d);
d = fdot2_f32(w2[11], x2.w, d);
d = fdot2_f32(w2[12], x3.x, d);
d = fdot2_f32(w2[13], x3.y, d);
d = fdot2_f32(w2[14], x3.z, d);
d = fdot2_f32(w2[15], x3.w, d);
acc[m] = fmaf(d, sc, acc[m]);
}
}
@@ -1376,11 +1435,8 @@ __global__ void __launch_bounds__(256) k_mxfp8_grouped_gemv_f16(
#pragma unroll
for (int m = 0; m < MT; ++m) {
if (t0 + m < m_e) {
red[tid] = acc[m];
__syncthreads();
for (int s = nt / 2; s > 0; s >>= 1) { if (tid < s) red[tid] += red[tid + s]; __syncthreads(); }
if (tid == 0) Y[(size_t)(beg + t0 + m) * N + n] = red[0];
__syncthreads();
const float sum = wave32_sum(acc[m]);
if (lane == 0) Y[(size_t)(beg + t0 + m) * N + row] = sum;
}
}
}
@@ -1393,7 +1449,7 @@ extern "C" int vetta_mxfp8_grouped_gemv_f16(const uint8_t* arena_weights,
vetta_stream_t stream) {
if (N <= 0 || K <= 0 || (K % 32) != 0 || n_experts <= 0) return 1;
constexpr int MT = 8;
dim3 grid(N, n_experts);
dim3 grid((N + 7) / 8, n_experts);
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_mxfp8_grouped_gemv_f16<MT><<<grid, 256, 0, s>>>(arena_weights, arena_scales, exp_off, bucket_tok, X, Y, N, K, w_stride, sc_stride);
HIP_CHECK(hipGetLastError());