MXFP4 decode GEMMs past 16 rows: gemv_wide two column tiles a block, the decode row's weight to registers

Bit for bit as before, faster where several streams speculate (25 to 128 rows a step):

- gemm_mxfp4a8_gemv_wide carries two 16-column tiles a block from 25 rows. The activation is read
  per block, so one tile a block read all of it N/16 times -- 356 MB from L2 at 64 rows on a 27B's
  17408-row gate_up, against a 47 MB weight. Each activation fragment now serves both tiles; a
  tile's WMMA chain is unchanged. Four tiles spill past 32 rows.
- gemm_mxfp4a8_decode's multi-fragment kernel (17..128 rows) loads each wave's weight words straight
  to registers, as the gemv kernel does (a fragment-order word is the WMMA fragment's register), and
  double-buffers only the shared activation in LDS: one barrier a slab, the next slab's loads in
  flight across this one's WMMAs. Same fold, k-step and slab order, scale carry and epilogue; its
  whole output compared byte for byte with the LDS-staged kernel's on Qwen3.8-27B's and
  TranslateGemma-27B's per-rank shapes at 17..128 rows, both scale grids, the 64-wide slab, parts
  and forced splits (312 cases, an experiment-only selftest case).

r4d_selftest --perfmx with the clocks warmed (a first timed pass from an idle card catches them
ramping up and read up to 1.5x slow), before -> after:
  17408 x 5120   32 rows 125 -> 85 us, 64 157 -> 122, 96 205 -> 159, 128 245 -> 214
  5120 x 8704    32 rows  75 -> 56,    64  95 -> 75,  128 169 -> 139
  6144 x 5120    64 rows  70 -> 57,   128 121 -> 92
  21504 x 5376   32 rows 154 -> 111,   64 183 -> 150
  5376 x 10752   32 rows 105 -> 68,    64 123 -> 98
At 16 to 24 rows within a few percent either way (unchanged kernels there).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-24 11:09:03 +02:00
1 parent b66752adcc
commit 2382686d3b
2 files changed
+151 -94

No files matched your search

+78 -52
View File
@@ -4,12 +4,14 @@
//
// At decode the GEMM is a weight stream and the goal is DRAM bandwidth. The skinny kernel (split-K
// within a workgroup, weight straight to registers) falls short on narrow projections. This one
// splits K across workgroups with a fused last-arriver reduction, unpacks the weight through LDS,
// and streams it with non-temporal loads.
// splits K across workgroups with a fused last-arriver reduction and streams the weight with
// non-temporal loads.
//
// Tile: DWN = 8 waves over BND = 128 output columns (one 16-column fragment per wave) and DTM
// 16-row M fragments, the fewest that cover M. Each K slab of DBK bytes stages A (16*DTM rows) and
// 128 weight rows, unpacked to e4m3 through r4d_mxfp4.h.
// 16-row M fragments, the fewest that cover M. Each K slab of DBK bytes stages A (16*DTM rows) in
// LDS, shared by the eight waves; each wave loads its own fragment's weight words for the slab
// straight to registers and unpacks them to e4m3 there (r4d_mxfp4.h). The one-fragment form
// (M <= 16) stages the weight through LDS instead.
//
// Split-K is required: N/16 fragments alone (320 waves at N=5120) do not fill the part. DKS blocks
// split the K slabs; each writes an fp32 partial [DKS][M][N], and the last to arrive (an
@@ -195,6 +197,15 @@ constexpr int mxd_waves(int dbk, int dtm, bool ablk) {
: 6;
}
// The multi-fragment form (M > 16). A fragment-order weight word is the WMMA fragment's own register
// (one dword a lane a k-step), so a wave loads its tile's words for a slab straight to registers and
// folds them there, with no barrier between a wave and its weight; only the activation, shared by the
// eight tiles, goes through LDS, double-buffered so a slab takes one barrier. The next slab's
// activation and weight are loaded while this slab computes. The arithmetic -- the fold, the k-step
// and slab order, the scale carry and the epilogue -- is the one the kernel staging the weight through
// LDS had, so the bits are its own (checked byte for byte when it was replaced). r4d_selftest
// --perfmx, clocks warmed, LDS-staged -> registers: 17408 x 5120 at 64 rows 149 -> 129 us, at 96
// 205 -> 159, at 128 245 -> 214; 5120 x 8704 at 128 169 -> 139; 6144 x 5120 at 128 121 -> 92.
template <int DBK, int DKS, int DTM, bool NT, bool ABLK>
__global__ __launch_bounds__(R4D_MXD_DWN * 32) R4D_WAVES(mxd_waves(DBK, DTM, ABLK))
void r4d_gemm_mxfp4a8_decode_kernel(
@@ -206,16 +217,16 @@ void r4d_gemm_mxfp4a8_decode_kernel(
constexpr int DWN = R4D_MXD_DWN, BND = DWN * 16, NTHR = DWN * 32;
constexpr int ROWS = R4D_MXD_MTILE * DTM;
constexpr int STR = DBK + R4D_MXD_PAD;
__shared__ unsigned char sA[ROWS * STR];
__shared__ unsigned char sW[BND * STR];
__shared__ float sR[ROWS]; // ABLK: a boundary's ratio a row; then every row's final scale
constexpr int KST = DBK / 16; // k-steps a slab
constexpr int A_IT = (ROWS * DBK + NTHR * 16 - 1) / (NTHR * 16);
__shared__ unsigned char sA[2][ROWS * STR];
__shared__ float sR[2][ROWS]; // ABLK: a boundary's ratio a row; then every row's final scale
__shared__ unsigned s_last;
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5;
const int col = lane & 15, kb8 = (lane >> 4) * 8;
const int n0 = blockIdx.x * BND;
const int ks = blockIdx.z;
const int slabs = (K + DBK - 1) / DBK;
const int spb = (slabs + DKS - 1) / DKS;
const int s_lo = ks * spb, s_hi = min(slabs, s_lo + spb);
@@ -231,86 +242,101 @@ void r4d_gemm_mxfp4a8_decode_kernel(
const int kb = ((s_lo * DBK) >> 7) + 1;
if (tid < ROWS && ((s_lo * DBK) & 127) && kb * 128 < k_hi) { ns = asrow[kb]; np = asrow[kb - 1]; }
}
// This wave's tile and this lane's column. A tile past N (N % 128 != 0) loads the last one, and
// the epilogue drops its columns.
const int nt = min(n0 / 16 + wave, N / 16 - 1);
const int ncol = nt * 16 + col;
const int ref = (int)Wref[ncol];
const unsigned int* __restrict__ Wp = (const unsigned int*)W;
const int ksteps_g = K / 16;
for (int s = s_lo; s < s_hi; ++s) {
unsigned rw[KST];
int re[KST / 2];
mxd_u4 ra[A_IT];
auto load = [&](int s) {
const int k0 = s * DBK;
const bool bnd = ABLK && s > s_lo && (k0 & 127) == 0;
if constexpr (ABLK) {
if (bnd && tid < ROWS) sR[tid] = mxd_ratio(ns, np, asrow, k0 >> 7);
}
// ---- A: ROWS x DBK bytes, clamped rows, 16 B a thread
#pragma unroll
for (int off = 0; off < ROWS * DBK; off += NTHR * 16) {
const int idx = off + tid * 16;
for (int j = 0; j < A_IT; ++j) {
const int idx = j * NTHR * 16 + tid * 16;
if (idx < ROWS * DBK) {
const int r = idx / DBK, c = idx % DBK;
const int rc = r < M - 1 ? r : M - 1; // clamp, never predicate
*(mxd_u4*)(&sA[r * STR + c]) = *(const mxd_u4*)(A + (size_t)rc * lda + k0 + c);
ra[j] = *(const mxd_u4*)(A + (size_t)rc * lda + k0 + c);
}
}
// ---- W: fragment-order slots, two adjacent rows a thread (an 8 B request), folded to e4m3
{
constexpr int KSTEPS_T = DBK / 16, NTILES_T = BND / 16;
const int kstep0 = k0 / 16;
#pragma unroll
for (int off = 0; off < NTILES_T * KSTEPS_T * 32; off += NTHR * 2) {
const int sl = off + tid * 2;
const int lane_ = sl & 31, rest = sl >> 5;
const int kst = rest % KSTEPS_T, ntl = rest / KSTEPS_T;
const int r = ntl * 16 + (lane_ & 15), kloc = kst * 16 + (lane_ >> 4) * 8;
const int gn = n0 + r;
// gn is even and N % 16 == 0, so clamping to N - 2 keeps the 8 B read aligned and gc + 1 in
// bounds; the slot address is recomputed from the clamped row.
const int gc = gn < N - 2 ? gn : N - 2;
const int lanec = (lane_ & 16) | (gc & 15);
const mxd_u2 wv =
mxd_ld<NT>((const mxd_u2*)&Wp[((size_t)(gc >> 4) * ksteps_g + kstep0 + kst) * 32 +
lanec]);
const int blk = (k0 + kst * 16) / R4D_MXFP4_GROUP;
for (int t = 0; t < KST; ++t)
rw[t] = mxd_ld<NT>(&Wp[((size_t)nt * ksteps_g + k0 / 16 + t) * 32 + lane]);
#pragma unroll
for (int q = 0; q < 2; ++q) {
const int d = r4d_mxfp4_fold_d(Wref[gc + q], Ws[(size_t)blk * ldws + gc + q]);
unsigned int lo, hi;
r4d_mxfp4_unpack8(q ? wv[1] : wv[0], d, &lo, &hi);
*(mxd_u2*)(&sW[(r + q) * STR + kloc]) = mxd_u2{lo, hi};
}
for (int q = 0; q < KST / 2; ++q)
re[q] = (int)Ws[(size_t)(k0 / R4D_MXFP4_GROUP + q) * ldws + ncol];
};
auto store_a = [&](int buf) {
#pragma unroll
for (int j = 0; j < A_IT; ++j) {
const int idx = j * NTHR * 16 + tid * 16;
if (idx < ROWS * DBK) {
const int r = idx / DBK, c = idx % DBK;
*(mxd_u4*)(&sA[buf][r * STR + c]) = ra[j];
}
}
__syncthreads();
};
if (s_lo < s_hi) {
load(s_lo);
store_a(0); // the first slab never rescales (s > s_lo below)
}
__syncthreads();
for (int s = s_lo; s < s_hi; ++s) {
const int buf = (s - s_lo) & 1;
const int k0 = s * DBK;
const bool bnd = ABLK && s > s_lo && (k0 & 127) == 0;
mxd_v2i wf[KST];
#pragma unroll
for (int t = 0; t < KST; ++t) {
unsigned lo, hi;
r4d_mxfp4_unpack8(rw[t], r4d_mxfp4_fold_d(ref, re[t >> 1]), &lo, &hi);
wf[t][0] = (int)lo; wf[t][1] = (int)hi;
}
if (s + 1 < s_hi) load(s + 1); // the next slab's loads fly across this slab's WMMAs
if constexpr (ABLK) {
if (bnd) mxd_rescale(acc, sR, col);
if (bnd) mxd_rescale(acc, sR[buf], col);
if (tid < ROWS && (k0 & 127) == 0 && k0 + 128 < k_hi) {
ns = asrow[(k0 >> 7) + 1];
np = asrow[k0 >> 7];
}
}
#pragma unroll
for (int step = 0; step < DBK / 16; ++step) {
for (int step = 0; step < KST; ++step) {
const int kk = step * 16 + kb8;
mxd_v2i af[DTM], wf;
mxd_v2i af[DTM];
#pragma unroll
for (int i = 0; i < DTM; ++i) {
const unsigned char* pa = &sA[(i * 16 + col) * STR + kk];
const unsigned char* pa = &sA[buf][(i * 16 + col) * STR + kk];
af[i][0] = *(const int*)pa; af[i][1] = *(const int*)(pa + 4);
}
const unsigned char* pw = &sW[(wave * 16 + col) * STR + kk];
wf[0] = *(const int*)pw; wf[1] = *(const int*)(pw + 4);
#pragma unroll
for (int i = 0; i < DTM; ++i)
acc[i] = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(wf, af[i], acc[i]);
acc[i] = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(wf[step], af[i], acc[i]);
}
// The next slab's activation into the other buffer, and its boundary ratio (published by the
// barrier below; a wave still rescaling this slab reads the other half).
if (s + 1 < s_hi) {
store_a(buf ^ 1);
if constexpr (ABLK) {
const int k1 = (s + 1) * DBK;
if ((k1 & 127) == 0 && tid < ROWS) sR[buf ^ 1][tid] = mxd_ratio(ns, np, asrow, k1 >> 7);
}
}
__syncthreads();
}
const int kb_end = s_hi > s_lo ? (k_hi - 1) >> 7 : 0;
mxd_epilogue<DTM, DKS, ABLK>(acc, As, as_rs, Wref, P, cnt, C, M, N, ldc, n0, wave, col, kb8, ks,
kb_end, &s_last, sR);
kb_end, &s_last, sR[0]);
}
// The one-fragment form (M <= 16), every single-sequence verify step. Same tile, fold and split as
// above, plus: the next slab's A, codes and exponents are loaded into registers while this slab
// above, with the weight staged through LDS, plus: the next slab's A, codes and exponents are loaded into registers while this slab
// computes, keeping loads in flight across barriers; a thread takes four adjacent slots (one 16 B
// load); and each row's reference exponent is read once. Bit-identical to the kernel above.
template <int DBK, int DKS, bool NT, bool ABLK>
+73 -42
View File
@@ -13,9 +13,9 @@
// Two forms, chosen from K alone so that a row's output bits do not depend on how many rows the
// step carries (a verify step's first row is bit-identical to a plain decode of it):
//
// - K a multiple of 128: the WMMA form. One 16-column tile a block, eight waves on its K in chunks
// of one 128 block, and up to four 16-row tiles (M <= 64), each weight word multiplied with every
// tile's fragment once it is loaded. A lane loads its own B-fragment word of each k-step straight
// - K a multiple of 128: the WMMA form. One 16-column tile a block (two from 25 rows), eight waves on
// its K in chunks of one 128 block, and up to four 16-row tiles (M <= 64), each weight word
// multiplied with every row tile's fragment once it is loaded. A lane loads its own B-fragment word of each k-step straight
// from the fragment-order weight (the order is the fragment's), the rows sit in the A fragments
// (rows >= M zero), and v_wmma_f32_16x16x16_fp8_fp8 multiplies. The per-(row, 128) activation scale is
// applied to each chunk's partial. At M = 1 the WMMA does sixteen times the work it needs, which
@@ -153,11 +153,18 @@ void r4d_gemm_mxfp4a8_gemv_kernel(
}
}
// The WMMA form: one 16-column tile a block, RT 16-row tiles (M <= 16 RT). Needs K % 128 == 0 (a
// chunk is a whole 128 block; a tail would be dropped); the launcher guarantees it and picks RT. A wave
// loads each weight word of a chunk once and runs one WMMA chain per row tile over it, so the weight
// streams once whatever M is, and a row's sum is its tile's chain alone -- the same at every RT.
template <bool ABLK, int RT>
// The WMMA form: TN 16-column tiles a block, RT 16-row tiles (M <= 16 RT). Needs K % 128 == 0 (a
// chunk is a whole 128 block; a tail would be dropped); the launcher guarantees it and picks RT and TN.
// A wave loads each weight word of a chunk once and runs one WMMA chain per row tile over it, so the
// weight streams once whatever M is, and a row's sum is its tile's chain alone -- the same at every RT
// and TN.
//
// TN: the activation is read per block, so one column tile a block reads all of it N/16 times (356 MB
// from L2 at M = 64 on a 27B's 17408-row gate_up, against its 47 MB weight). Two tiles a block load
// each activation fragment once for both. r4d_selftest --perfmx, clocks warmed, TN 1 -> 2: 17408 x 5120
// at 32 rows 125 -> 85 us, at 64 157 -> 122; 5120 x 8704 75 -> 56, 95 -> 75; 21504 x 5376 154 -> 111,
// 183 -> 150; at 16 to 24 rows within a few percent either way. Four tiles spill past 32 rows.
template <bool ABLK, int RT, int TN = 1>
__global__ __launch_bounds__(R4D_MXG_KW * 32)
void r4d_gemm_mxfp4a8_gemvw_kernel(
const unsigned char* __restrict__ A, const float* __restrict__ As,
@@ -168,29 +175,39 @@ void r4d_gemm_mxfp4a8_gemvw_kernel(
__shared__ float sP[KW][16 * RT][16];
const int lane = threadIdx.x & 31;
const int wave = __builtin_amdgcn_readfirstlane(threadIdx.x >> 5);
const int nt = blockIdx.x;
const int nt0 = blockIdx.x * TN;
const int col = lane & 15, khalf = lane >> 4; // B fragment: this lane's column and k half
const int ksteps = K >> 4, chunks = K >> 7;
const unsigned* __restrict__ Wp = (const unsigned*)W;
const int ref = (int)Wref[nt * 16 + col];
mxg_v8f tot[RT];
int ref[TN];
#pragma unroll
for (int r = 0; r < RT; ++r) tot[r] = (mxg_v8f)(0.f);
for (int t = 0; t < TN; ++t) ref[t] = (int)Wref[(nt0 + t) * 16 + col];
mxg_v8f tot[RT][TN];
#pragma unroll
for (int r = 0; r < RT; ++r)
#pragma unroll
for (int t = 0; t < TN; ++t) tot[r][t] = (mxg_v8f)(0.f);
for (int ch = wave; ch < chunks; ch += KW) {
unsigned wv[8];
unsigned wv[TN][8];
#pragma unroll
for (int s = 0; s < 8; ++s)
wv[s] = __builtin_nontemporal_load(&Wp[((size_t)nt * ksteps + ch * 8 + s) * 32 + lane]);
int e8[4];
for (int t = 0; t < TN; ++t)
#pragma unroll
for (int q = 0; q < 4; ++q) e8[q] = (int)Ws[(size_t)(ch * 4 + q) * ldws + nt * 16 + col];
mxg_v2i wf[8];
for (int s = 0; s < 8; ++s)
wv[t][s] = __builtin_nontemporal_load(&Wp[((size_t)(nt0 + t) * ksteps + ch * 8 + s) * 32 + lane]);
int e8[TN][4];
#pragma unroll
for (int s = 0; s < 8; ++s) {
unsigned lo, hi;
r4d_mxfp4_unpack8(wv[s], r4d_mxfp4_fold_d(ref, e8[s >> 1]), &lo, &hi);
wf[s][0] = (int)lo; wf[s][1] = (int)hi;
}
for (int t = 0; t < TN; ++t)
#pragma unroll
for (int q = 0; q < 4; ++q) e8[t][q] = (int)Ws[(size_t)(ch * 4 + q) * ldws + (nt0 + t) * 16 + col];
mxg_v2i wf[TN][8];
#pragma unroll
for (int t = 0; t < TN; ++t)
#pragma unroll
for (int s = 0; s < 8; ++s) {
unsigned lo, hi;
r4d_mxfp4_unpack8(wv[t][s], r4d_mxfp4_fold_d(ref[t], e8[t][s >> 1]), &lo, &hi);
wf[t][s][0] = (int)lo; wf[t][s][1] = (int)hi;
}
#pragma unroll
for (int r = 0; r < RT; ++r) {
const int row = r * 16 + col; // A fragment: this lane's row in tile r
@@ -205,29 +222,38 @@ void r4d_gemm_mxfp4a8_gemvw_kernel(
af[s][0] = 0; af[s][1] = 0;
}
}
mxg_v8f acc = (mxg_v8f)(0.f);
#pragma unroll
for (int s = 0; s < 8; ++s) acc = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(wf[s], af[s], acc);
// acc[e]: row (lane & 15) of tile r, column (lane >> 4) * 8 + e -- as the decode epilogue reads it.
const float sc = ABLK ? (live ? As[(size_t)row * as_rs + ch] : 0.f) : 1.f;
#pragma unroll
for (int e = 0; e < 8; ++e) tot[r][e] = fmaf(acc[e], sc, tot[r][e]);
for (int t = 0; t < TN; ++t) {
mxg_v8f acc = (mxg_v8f)(0.f);
#pragma unroll
for (int s = 0; s < 8; ++s)
acc = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(wf[t][s], af[s], acc);
// acc[e]: row (lane & 15) of tile r, column (lane >> 4) * 8 + e -- as the decode epilogue reads it.
#pragma unroll
for (int e = 0; e < 8; ++e) tot[r][t][e] = fmaf(acc[e], sc, tot[r][t][e]);
}
}
}
// One column tile at a time through sP (TN of them would not fit in LDS at four row tiles).
#pragma unroll
for (int r = 0; r < RT; ++r)
for (int t = 0; t < TN; ++t) {
if (t) __syncthreads();
#pragma unroll
for (int e = 0; e < 8; ++e) sP[wave][r * 16 + col][khalf * 8 + e] = tot[r][e];
__syncthreads();
for (int idx = threadIdx.x; idx < 16 * RT * 16; idx += KW * 32) {
const int m = idx >> 4, c = idx & 15;
if (m < M) {
float sum = 0.f;
for (int r = 0; r < RT; ++r)
#pragma unroll
for (int w = 0; w < KW; ++w) sum += sP[w][m][c];
const int cc = nt * 16 + c;
const float rf = __int_as_float((int)Wref[cc] << 23);
C[(size_t)m * ldc + cc] = (__hip_bfloat16)(ABLK ? sum * rf : sum * rf * As[(size_t)m * as_rs]);
for (int e = 0; e < 8; ++e) sP[wave][r * 16 + col][khalf * 8 + e] = tot[r][t][e];
__syncthreads();
for (int idx = threadIdx.x; idx < 16 * RT * 16; idx += KW * 32) {
const int m = idx >> 4, c = idx & 15;
if (m < M) {
float sum = 0.f;
#pragma unroll
for (int w = 0; w < KW; ++w) sum += sP[w][m][c];
const int cc = (nt0 + t) * 16 + c;
const float rf = __int_as_float((int)Wref[cc] << 23);
C[(size_t)m * ldc + cc] = (__hip_bfloat16)(ABLK ? sum * rf : sum * rf * As[(size_t)m * as_rs]);
}
}
}
}
@@ -253,11 +279,16 @@ void mxg_launch(long a, long ascale, long wq, long ws, long wref, long c, int M,
const dim3 block(R4D_MXG_KW * 32);
if (K % 128 == 0) {
const int rt = (M + 15) / 16;
#define MXW_L(AB_, RT_) hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gemvw_kernel<AB_, RT_>), dim3(tiles), block, 0, \
st, Ap, Sp, Wp, Zp, Rp, Cp, M, K, as_rs, lda, ldws, ldc)
#define MXW_AB(RT_) { if (ablk) MXW_L(true, RT_); else MXW_L(false, RT_); }
// Two column tiles a block from 25 rows (see the kernel); an odd tile count keeps one.
const bool tn2 = M > 24 && tiles % 2 == 0;
#define MXW_L(AB_, RT_, TN_) hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gemvw_kernel<AB_, RT_, TN_>), \
dim3(tiles / TN_), block, 0, st, Ap, Sp, Wp, Zp, Rp, \
Cp, M, K, as_rs, lda, ldws, ldc)
#define MXW_TN(AB_, RT_) { if (tn2) MXW_L(AB_, RT_, 2); else MXW_L(AB_, RT_, 1); }
#define MXW_AB(RT_) { if (ablk) MXW_TN(true, RT_) else MXW_TN(false, RT_) }
if (rt <= 1) MXW_AB(1) else if (rt == 2) MXW_AB(2) else if (rt == 3) MXW_AB(3) else MXW_AB(4)
#undef MXW_AB
#undef MXW_TN
#undef MXW_L
return;
}