From b66752adccdb12e5dd2f7a962b62acaaedf9df82 Mon Sep 17 00:00:00 2001 From: mici81 Date: Tue, 20 Oct 2026 05:36:49 +0000 Subject: [PATCH] libr4d: gemm_mxfp4a8_gemv_wide, the gemv row's WMMA form at 9 to 64 rows The WMMA form takes up to four 16-row tiles a wave: each weight word of a chunk is loaded and folded once and multiplied with every tile's activation fragment, so the weight streams once from DRAM to registers whatever the row count, with no LDS staging. A row's sum is its tile's WMMA chain alone, so its bits are the same at every tile count: the first row is bit-identical from M = 1 to 64 (selftest), across the gemv row (M <= 8) and the new gemv_wide row (9 <= M <= 64, K % 128 == 0, same entry and priority). Several streams with speculation land there (4 x (1 + 7) = 32, 16 x (1 + 3) = 64). --perfmx against the decode row that served 9..64 before (us, Qwen3.8-27B / TranslateGemma per-rank decode shapes): down 51-80 vs 56-105, ssm_out 27-42 vs 30-55, attn_qg 40-63 vs 44-74, TranslateGemma down 61-97 vs 66-123, o 22-35 vs 25-51, gate_up at 24-48 rows 110-141 vs 145-170; gate_up at 48-64 rows about even (Qwen) or behind (TranslateGemma at 64, 171 vs 153). Selftest: gemv_wide at RT 2..4, both activation grids, a partly live tile, parts; the M-invariance check runs the wide row under the gemv row's selection (mxfp4a8_numeric's any_row). Co-Authored-By: Claude Opus 5.5 (1M context) --- libr4d/r4d_gemm_mxfp4a8_gemv.hip | 97 ++++++++++++++++++-------------- libr4d/r4d_rows.cpp | 22 +++++++- libr4d/r4d_selftest.cpp | 29 ++++++++-- 3 files changed, 98 insertions(+), 50 deletions(-) diff --git a/libr4d/r4d_gemm_mxfp4a8_gemv.hip b/libr4d/r4d_gemm_mxfp4a8_gemv.hip index 4a92df9c..20ad1982 100644 --- a/libr4d/r4d_gemm_mxfp4a8_gemv.hip +++ b/libr4d/r4d_gemm_mxfp4a8_gemv.hip @@ -1,6 +1,7 @@ -// r4d_gemm_mxfp4a8_gemv.hip - r4d_gemm_mxfp4a8_gemv: the OCP-MXFP4 x fp8 GEMM for one to eight rows, -// gfx1201. C[M,N] = A[M,K] @ dequant(Wq)[N,K]^T with the operands, weight layout and fold of -// r4d_gemm_mxfp4a8_decode (and _nt_m64, _tiled). +// r4d_gemm_mxfp4a8_gemv.hip - r4d_gemm_mxfp4a8_gemv: the OCP-MXFP4 x fp8 GEMM for one to eight rows +// (and, its WMMA form alone, up to 64: the gemv_wide row), gfx1201. C[M,N] = A[M,K] @ +// dequant(Wq)[N,K]^T with the operands, weight layout and fold of r4d_gemm_mxfp4a8_decode (and +// _nt_m64, _tiled). // // At one stream's decode (M = 1, or 1 + the draft depth) the GEMM is a pure weight stream, and the // decode kernel's WMMA tile spends its time staging the weight through LDS between two barriers a @@ -13,9 +14,10 @@ // 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. 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 fragment (rows >= M -// zero), and v_wmma_f32_16x16x16_fp8_fp8 multiplies. The per-(row, 128) activation scale is +// 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 +// 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 // costs nothing next to the weight stream; from M = 2 up it is what keeps the kernel on the // stream (the dot form pays a dot per row). @@ -38,6 +40,7 @@ #include #define R4D_MXG_MAX_M 8 +#define R4D_MXG_WMMA_MAX_M 64 // the WMMA form's bound: four 16-row tiles a wave #define R4D_MXG_KW 8 // waves on a block's K, both forms; a constant, so the sum order is too typedef unsigned mxg_u4 __attribute__((ext_vector_type(4))); @@ -150,9 +153,11 @@ void r4d_gemm_mxfp4a8_gemv_kernel( } } -// The WMMA form: one 16-column tile a block. Needs K % 128 == 0 (a chunk is a whole 128 block; a -// tail would be dropped) and M <= 8 (sP holds eight rows); the launcher guarantees both. -template +// 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 __global__ __launch_bounds__(R4D_MXG_KW * 32) void r4d_gemm_mxfp4a8_gemvw_kernel( const unsigned char* __restrict__ A, const float* __restrict__ As, @@ -160,17 +165,17 @@ void r4d_gemm_mxfp4a8_gemvw_kernel( const unsigned char* __restrict__ Wref, __hip_bfloat16* __restrict__ C, int M, int K, int as_rs, int lda, int ldws, int ldc) { constexpr int KW = R4D_MXG_KW; - __shared__ float sP[KW][R4D_MXG_MAX_M][16]; + __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 col = lane & 15, khalf = lane >> 4; // B fragment: this lane's column and k half - const int row = col; // A fragment: this lane's row - const bool live = row < M; 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 = (mxg_v8f)(0.f); + mxg_v8f tot[RT]; +#pragma unroll + for (int r = 0; r < RT; ++r) tot[r] = (mxg_v8f)(0.f); for (int ch = wave; ch < chunks; ch += KW) { unsigned wv[8]; #pragma unroll @@ -179,36 +184,42 @@ void r4d_gemm_mxfp4a8_gemvw_kernel( int e8[4]; #pragma unroll for (int q = 0; q < 4; ++q) e8[q] = (int)Ws[(size_t)(ch * 4 + q) * ldws + nt * 16 + col]; - mxg_v2i af[8]; -#pragma unroll - for (int s = 0; s < 8; ++s) { - if (live) { - const unsigned* pa = (const unsigned*)(A + (size_t)row * lda + (ch * 8 + s) * 16 + khalf * 8); - af[s][0] = (int)pa[0]; af[s][1] = (int)pa[1]; - } else { - af[s][0] = 0; af[s][1] = 0; - } - } - mxg_v8f acc = (mxg_v8f)(0.f); + mxg_v2i wf[8]; #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); - mxg_v2i wf; - wf[0] = (int)lo; wf[1] = (int)hi; - acc = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(wf, af[s], acc); + wf[s][0] = (int)lo; wf[s][1] = (int)hi; } - // acc[e]: row (lane & 15), column (lane >> 4) * 8 + e of the tile -- 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[e] = fmaf(acc[e], sc, tot[e]); - } - if (row < R4D_MXG_MAX_M) { + for (int r = 0; r < RT; ++r) { + const int row = r * 16 + col; // A fragment: this lane's row in tile r + const bool live = row < M; + mxg_v2i af[8]; #pragma unroll - for (int e = 0; e < 8; ++e) sP[wave][row][khalf * 8 + e] = tot[e]; + for (int s = 0; s < 8; ++s) { + if (live) { + const unsigned* pa = (const unsigned*)(A + (size_t)row * lda + (ch * 8 + s) * 16 + khalf * 8); + af[s][0] = (int)pa[0]; af[s][1] = (int)pa[1]; + } else { + 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]); + } } +#pragma unroll + for (int r = 0; r < RT; ++r) +#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 < R4D_MXG_MAX_M * 16; idx += KW * 32) { + 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; @@ -224,7 +235,8 @@ void r4d_gemm_mxfp4a8_gemvw_kernel( namespace { void mxg_launch(long a, long ascale, long wq, long ws, long wref, long c, int M, int K, int N, int as_group, int as_rs, long stream, int lda, int ldws, int ldc) { - if (M < 1 || M > R4D_MXG_MAX_M) throw std::runtime_error("r4d gemm_mxfp4a8_gemv: M must be 1..8"); + if (M < 1 || M > (K % 128 == 0 ? R4D_MXG_WMMA_MAX_M : R4D_MXG_MAX_M)) + throw std::runtime_error("r4d gemm_mxfp4a8_gemv: M must be 1..8, or 1..64 when K % 128 == 0"); if (N < 16 || N % 16) throw std::runtime_error("r4d gemm_mxfp4a8_gemv: N must be a multiple of 16"); if (K < 64 || K % 64) throw std::runtime_error("r4d gemm_mxfp4a8_gemv: K must be a multiple of 64"); if (as_group != 0 && as_group != 128) @@ -240,12 +252,13 @@ void mxg_launch(long a, long ascale, long wq, long ws, long wref, long c, int M, auto Cp = (__hip_bfloat16*)c; const dim3 block(R4D_MXG_KW * 32); if (K % 128 == 0) { - if (ablk) - hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gemvw_kernel), dim3(tiles), block, 0, st, Ap, Sp, Wp, Zp, - Rp, Cp, M, K, as_rs, lda, ldws, ldc); - else - hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gemvw_kernel), dim3(tiles), block, 0, st, Ap, Sp, Wp, Zp, - Rp, Cp, M, K, as_rs, lda, ldws, ldc); + const int rt = (M + 15) / 16; +#define MXW_L(AB_, RT_) hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gemvw_kernel), 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_); } + 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_L return; } // Tiles a block carries: enough to amortise the activation loads at several rows, the widest that @@ -282,7 +295,7 @@ extern "C" int r4d_rad_gemm_mxfp4a8_gemv(const RadArgs* a, RadStream s) { rc = mx_parts(a, &g, &mp); if (rc != RAD_OK) return rc; if (g.K % 64) return R4D_NO(RAD_E_SHAPE); - if (g.M > R4D_MXG_MAX_M) return R4D_NO(RAD_E_SHAPE); + if (g.M > (g.K % 128 == 0 ? R4D_MXG_WMMA_MAX_M : R4D_MXG_MAX_M)) return R4D_NO(RAD_E_SHAPE); int as_group = 0, as_rs = 1; rc = mx_ascale_grid(g, &as_group, &as_rs, mp.n); if (rc != RAD_OK) return rc; diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 14738dc6..93660471 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -4798,8 +4798,8 @@ static const RadConstraint cGemmW4A8Tiled[] = { }; /* r4d_gemm_mxfp4a8_decode. Entry point: M <= 128, N % 16, K % 64, and N up to its counter block's * 36864. Above the skinny row by priority, so it takes the decode band; the skinny row keeps the - * wider N up to M = 64 (from M = 9: the gemv row below takes M <= 8 at any N), the tiled row - * everything else. */ + * wider N up to M = 64 (from M = 9: the gemv row below takes M <= 8 at any N, and gemv_wide + * M <= 64 where K % 128 == 0), the tiled row everything else. */ static const RadConstraint cGemmMxfp4A8Decode[] = { C_LE("M", 128), C_LE("N", 36864), C_DIV("K", 64), C_DIV("N", 16), C_IN("dtype", "mxfp4a8"), }; @@ -4809,6 +4809,12 @@ static const RadConstraint cGemmMxfp4A8Decode[] = { static const RadConstraint cGemmMxfp4A8Gemv[] = { C_LE("M", 8), C_DIV("K", 64), C_DIV("N", 16), C_IN("dtype", "mxfp4a8"), }; +/* gemm_mxfp4a8_gemv_wide: the same entry at 9..64 rows, where only the WMMA form serves (K % 128). + * The form's sum order for a row is its 16-row tile's chain alone, so a row's bits are the same in + * both rows at every M. Several streams with speculation (4 x 1 + 7 drafts = 32) land here. */ +static const RadConstraint cGemmMxfp4A8GemvWide[] = { + C_GE("M", 9), C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "mxfp4a8"), +}; /* r4d_gemm_mxfp4a8_tiled. Entry point: N % 16 (the fragment-order weight), K % 64 (its narrowest * K slab), M >= 1. The decode and skinny rows outrank it in their bands by priority, so the band * starts at 1 rather than at a hand-picked crossover. */ @@ -7524,6 +7530,18 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, nullptr, nullptr, r4d_rad_gemm_mxfp4a8_gemv, nullptr, r4d_rad_layout_mxfp4, r4d_rad_relayout_mxfp4 }, +{ "gemm_mxfp4a8_gemv_wide", "gemm_nt_q", "gemm", + "the gemv row's WMMA form at 9 to 64 rows: up to four 16-row tiles a wave over each weight word, " + "so the weight streams once from DRAM to registers whatever the row count; no LDS staging", + "9 <= M <= 64; K a multiple of 128; N a multiple of 16; the skinny kernel's fragment-order " + "weight, [K/32][N] E8M0 exponents and per-row reference exponent", + "e4m3 A with an f32 scale per row or per (row, 128), e2m1 W with an E8M0 scale per 32 K folded " + "into the element against a per-row reference, bf16 C, fp32 accumulate", + RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_MXGEMV, + ROW(cGemmMxfp4A8GemvWide), NOTUNE, + nullptr, nullptr, r4d_rad_gemm_mxfp4a8_gemv, nullptr, + r4d_rad_layout_mxfp4, r4d_rad_relayout_mxfp4 }, + { "gemm_mxfp4a8_decode", "gemm_nt_q", "gemm", "the OCP-MXFP4 GEMM for the DECODE band: split-K across workgroups with a fused last-arriver " "reduction, the weight unpacked through LDS, streaming weight loads", diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 1690fb5f..a388b789 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -4263,8 +4263,9 @@ static float mxfp4_act(int64_t i, int64_t k, uint32_t seed) { * as the zero a fresh allocation holds. `row0`, when given, receives the first row's bits. */ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_t N, int64_t K, int tn, bool ablk = false, std::vector extra = {}, - int64_t pb1 = 0, int64_t pb2 = 0, std::vector* row0 = nullptr) { - if (!sel(row)) return; + int64_t pb1 = 0, int64_t pb2 = 0, std::vector* row0 = nullptr, + bool any_row = false) { + if (!any_row && !sel(row)) return; /* Parts (ParoQuant): weight rows [0, pb1), [pb1, pb2), [pb2, N) each meet their own copy of * the activation, side by side in a row. `K` is one copy's width. */ const PartCut cut(N, pb1, pb2); @@ -4399,13 +4400,20 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_ static void mxfp4a8_m_invariant(const Plugin& pl, const char* row, int64_t N, int64_t K, bool ablk) { if (!sel(row)) return; char nm[160]; - std::snprintf(nm, sizeof nm, "%s first row bit-identical at M = 1..8, N=%lld K=%lld%s", row, + std::snprintf(nm, sizeof nm, "%s first row bit-identical at M = 1..%s, N=%lld K=%lld%s", row, + !std::strcmp(row, "gemm_mxfp4a8_gemv") && K % 128 == 0 ? "64 (gemv_wide past 8)" : "8", (long long)N, (long long)K, ablk ? " ascale=row x 128" : ""); std::vector ref, got; - mxfp4a8_numeric(pl, row, 1, N, K, 0, ablk, {}, 0, 0, &ref); - for (int64_t M = 2; M <= 8; ++M) { + mxfp4a8_numeric(pl, row, 1, N, K, 0, ablk, {}, 0, 0, &ref, true); + /* With K % 128 == 0 the gemv row's WMMA form continues in gemv_wide to 64 rows: the first row + * must keep its bits there too (every tile count). Run under this check's own selection, so the + * wide row runs here whichever row the process was started for. */ + const bool wide = !std::strcmp(row, "gemm_mxfp4a8_gemv") && K % 128 == 0; + for (const int64_t M : { 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 32, 33, 48, 64 }) { + if (M > 8 && !wide) break; got.clear(); - mxfp4a8_numeric(pl, row, M, N, K, 0, ablk, {}, 0, 0, &got); + mxfp4a8_numeric(pl, M > 8 ? "gemm_mxfp4a8_gemv_wide" : row, M, N, K, 0, ablk, {}, 0, 0, &got, + true); if (ref.size() != (size_t)N || got != ref) { ok(nm, false, "M=" + std::to_string(M) + " differs from M=1"); return; @@ -26904,6 +26912,15 @@ static void device_phase(const Plugin& pl) { mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", m, n, 576, 0, true); mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", m, n, 576, 0, false); } + /* gemv_wide: the WMMA form over two to four 16-row tiles, both grids, a tile partly + * live, and parts. */ + mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 9, 5120, 5120, 0, true); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 17, 1072, 2560, 0, false); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 32, 17408, 5120, 0, true); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 40, 5376, 10752, 0, true); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 64, 10288, 640, 0, false); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 33, 16, 128, 0, true); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 24, 34816, 5120, 0, true, {}, 17408); mxfp4a8_m_invariant(pl, "gemm_mxfp4a8_gemv", 5120, 5120, true); mxfp4a8_m_invariant(pl, "gemm_mxfp4a8_gemv", 1088, 576, true); mxfp4a8_m_invariant(pl, "gemm_mxfp4a8_gemv", 1056, 576, false);