Archived
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) <noreply@anthropic.com>
314 lines
14 KiB
Plaintext
314 lines
14 KiB
Plaintext
// 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
|
|
// slab, which leaves narrow projections at a third to two thirds of DRAM bandwidth. This kernel
|
|
// keeps the weight out of LDS altogether: every lane loads the weight words it multiplies, folds
|
|
// them to e4m3 in registers by r4d_mxfp4_unpack8 exactly as the other MXFP4 kernels fold them (the
|
|
// same weight bytes in every M band), and the block's waves split K and meet once in LDS at the end.
|
|
//
|
|
// 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
|
|
// 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).
|
|
// - Otherwise (K = 64 mod 128): the dot form. A lane loads 16 bytes -- four columns' eight codes of
|
|
// one k-step -- so a wave reads four k-steps, 512 contiguous bytes, from each of its TN tiles, and
|
|
// multiplies with v_dot4_f32_fp8_fp8; the activation a lane reads (eight k of each row) serves its
|
|
// four columns in all TN tiles. A k-step group (64 of K) lies in one 128 block, so the activation
|
|
// scale is applied as each partial is made. The lanes holding the same columns are summed by
|
|
// shuffles. TN only widens a block; the sum order of a column is fixed by KW, which is constant.
|
|
//
|
|
// The column's 2^(ref - 127) is applied once, as in the decode kernel's epilogue. No scratch and no
|
|
// counters: there is no split across blocks.
|
|
#include "r4d.h"
|
|
#include "r4d_gemm_args.h"
|
|
#include "r4d_common.h"
|
|
#include "r4d_mxfp4.h"
|
|
|
|
#include <hip/hip_bf16.h>
|
|
#include <cstdint>
|
|
#include <stdexcept>
|
|
|
|
#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)));
|
|
typedef unsigned mxg_u2 __attribute__((ext_vector_type(2)));
|
|
typedef float mxg_v8f __attribute__((ext_vector_type(8)));
|
|
typedef int mxg_v2i __attribute__((ext_vector_type(2)));
|
|
|
|
// The dot form: TN 16-column tiles a block, MT >= M rows. Needs K % 64 == 0.
|
|
template <int MT, int TN, bool ABLK>
|
|
__global__ __launch_bounds__(R4D_MXG_KW * 32)
|
|
void r4d_gemm_mxfp4a8_gemv_kernel(
|
|
const unsigned char* __restrict__ A, const float* __restrict__ As,
|
|
const unsigned char* __restrict__ W, const unsigned char* __restrict__ Ws,
|
|
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][MT][16 * TN];
|
|
const int lane = threadIdx.x & 31;
|
|
const int wave = __builtin_amdgcn_readfirstlane(threadIdx.x >> 5); // uniform: scalar loads
|
|
const int nt0 = blockIdx.x * TN; // the block's first 16-column tile
|
|
const int g = lane & 7, j = lane >> 3; // the slot quad in a k-step; the k-step of four
|
|
const int khalf = g >> 2, cq = (g * 4) & 15; // this lane's k half and first of its four columns
|
|
const int ksteps = K >> 4, groups = K >> 6;
|
|
const mxg_u4* __restrict__ Wv = (const mxg_u4*)W;
|
|
|
|
int ref[TN][4];
|
|
#pragma unroll
|
|
for (int t = 0; t < TN; ++t) {
|
|
const unsigned r4 = *(const unsigned*)(Wref + (nt0 + t) * 16 + cq);
|
|
#pragma unroll
|
|
for (int c = 0; c < 4; ++c) ref[t][c] = (int)((r4 >> (8 * c)) & 0xFFu);
|
|
}
|
|
|
|
float acc[MT][TN][4];
|
|
#pragma unroll
|
|
for (int m = 0; m < MT; ++m)
|
|
#pragma unroll
|
|
for (int t = 0; t < TN; ++t)
|
|
#pragma unroll
|
|
for (int c = 0; c < 4; ++c) acc[m][t][c] = 0.f;
|
|
|
|
#pragma unroll 4
|
|
for (int gi = wave; gi < groups; gi += KW) {
|
|
const int kstep = gi * 4 + j;
|
|
const int k0 = kstep * 16 + khalf * 8;
|
|
const int kb = gi >> 1; // the wave's 128 block (a group is 64 of K)
|
|
mxg_u4 wv[TN];
|
|
unsigned e4[TN];
|
|
#pragma unroll
|
|
for (int t = 0; t < TN; ++t) {
|
|
wv[t] = __builtin_nontemporal_load(&Wv[((size_t)(nt0 + t) * ksteps + kstep) * 8 + g]);
|
|
e4[t] = *(const unsigned*)(Ws + (size_t)(k0 >> 5) * ldws + (nt0 + t) * 16 + cq);
|
|
}
|
|
unsigned lo[TN][4], hi[TN][4];
|
|
#pragma unroll
|
|
for (int t = 0; t < TN; ++t)
|
|
#pragma unroll
|
|
for (int c = 0; c < 4; ++c)
|
|
r4d_mxfp4_unpack8(wv[t][c], r4d_mxfp4_fold_d(ref[t][c], (int)((e4[t] >> (8 * c)) & 0xFFu)),
|
|
&lo[t][c], &hi[t][c]);
|
|
#pragma unroll
|
|
for (int m = 0; m < MT; ++m) {
|
|
if (m < M) {
|
|
const mxg_u2 av = *(const mxg_u2*)(A + (size_t)m * lda + k0);
|
|
const float sc = ABLK ? As[(size_t)m * as_rs + kb] : 1.f;
|
|
#pragma unroll
|
|
for (int t = 0; t < TN; ++t)
|
|
#pragma unroll
|
|
for (int c = 0; c < 4; ++c) {
|
|
float d = __builtin_amdgcn_dot4_f32_fp8_fp8(lo[t][c], av.x, 0.f);
|
|
d = __builtin_amdgcn_dot4_f32_fp8_fp8(hi[t][c], av.y, d);
|
|
acc[m][t][c] = ABLK ? fmaf(d, sc, acc[m][t][c]) : acc[m][t][c] + d;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// The lanes with the same columns: the other k half (lane ^ 4) and the other k-steps (^ 8, ^ 16).
|
|
#pragma unroll
|
|
for (int m = 0; m < MT; ++m)
|
|
#pragma unroll
|
|
for (int t = 0; t < TN; ++t)
|
|
#pragma unroll
|
|
for (int c = 0; c < 4; ++c) {
|
|
float v = acc[m][t][c];
|
|
v += __shfl_xor(v, 4, 32);
|
|
v += __shfl_xor(v, 8, 32);
|
|
v += __shfl_xor(v, 16, 32);
|
|
acc[m][t][c] = v;
|
|
}
|
|
if (lane < 4) {
|
|
#pragma unroll
|
|
for (int m = 0; m < MT; ++m)
|
|
#pragma unroll
|
|
for (int t = 0; t < TN; ++t)
|
|
#pragma unroll
|
|
for (int c = 0; c < 4; ++c) sP[wave][m][t * 16 + cq + c] = acc[m][t][c];
|
|
}
|
|
__syncthreads();
|
|
for (int idx = threadIdx.x; idx < MT * 16 * TN; idx += KW * 32) {
|
|
const int m = idx / (16 * TN), cc = idx % (16 * TN);
|
|
if (m < M) {
|
|
float sum = 0.f;
|
|
#pragma unroll
|
|
for (int w = 0; w < KW; ++w) sum += sP[w][m][cc];
|
|
const int col = nt0 * 16 + cc;
|
|
const float rf = __int_as_float((int)Wref[col] << 23);
|
|
C[(size_t)m * ldc + col] = (__hip_bfloat16)(ABLK ? sum * rf : sum * rf * As[(size_t)m * as_rs]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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>
|
|
__global__ __launch_bounds__(R4D_MXG_KW * 32)
|
|
void r4d_gemm_mxfp4a8_gemvw_kernel(
|
|
const unsigned char* __restrict__ A, const float* __restrict__ As,
|
|
const unsigned char* __restrict__ W, const unsigned char* __restrict__ Ws,
|
|
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][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 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];
|
|
#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
|
|
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];
|
|
#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];
|
|
#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;
|
|
}
|
|
#pragma unroll
|
|
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 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 < 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 = 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]);
|
|
}
|
|
}
|
|
}
|
|
|
|
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 > (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)
|
|
throw std::runtime_error("r4d gemm_mxfp4a8_gemv: the activation scale is per row or per 128 of K");
|
|
const bool ablk = as_group == 128;
|
|
const int tiles = N / 16;
|
|
hipStream_t st = (hipStream_t)stream;
|
|
auto Ap = (const unsigned char*)a;
|
|
auto Sp = (const float*)ascale;
|
|
auto Wp = (const unsigned char*)wq;
|
|
auto Zp = (const unsigned char*)ws;
|
|
auto Rp = (const unsigned char*)wref;
|
|
auto Cp = (__hip_bfloat16*)c;
|
|
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_); }
|
|
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
|
|
// divides the tile count.
|
|
int tn = M >= 3 ? 4 : (M == 2 ? 2 : 1);
|
|
while (tn > 1 && tiles % tn) tn >>= 1;
|
|
const dim3 grid(tiles / tn);
|
|
#define MXG_L(MT_, TN_, AB_) \
|
|
hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gemv_kernel<MT_, TN_, AB_>), grid, block, 0, st, Ap, Sp, \
|
|
Wp, Zp, Rp, Cp, M, K, as_rs, lda, ldws, ldc)
|
|
#define MXG_AB(MT_, TN_) { if (ablk) MXG_L(MT_, TN_, true); else MXG_L(MT_, TN_, false); }
|
|
#define MXG_TN(MT_) { if (tn == 4) MXG_AB(MT_, 4) else if (tn == 2) MXG_AB(MT_, 2) else MXG_AB(MT_, 1) }
|
|
if (M == 1) MXG_AB(1, 1)
|
|
else if (M == 2) { if (tn == 2) MXG_AB(2, 2) else MXG_AB(2, 1) }
|
|
else if (M <= 4) MXG_TN(4)
|
|
else MXG_TN(8)
|
|
#undef MXG_TN
|
|
#undef MXG_AB
|
|
#undef MXG_L
|
|
}
|
|
} // namespace
|
|
|
|
// ============================================================== the plugin ABI entry point
|
|
extern "C" int r4d_rad_gemm_mxfp4a8_gemv(const RadArgs* a, RadStream s) {
|
|
GemmOps g;
|
|
int rc = gemm_ops(a, &g, 1, /*parts=*/1);
|
|
if (rc != RAD_OK) return rc;
|
|
if (g.N < 16 || g.N % 16) return R4D_NO(RAD_E_SHAPE);
|
|
if (!g.ascale || !g.bref) return R4D_NO(RAD_E_INVAL);
|
|
/* The dot form reads four columns' exponents and references as one word. */
|
|
if (!r4d_aligned((const void*)r4d_addr(g.bscale), 4) || !r4d_aligned((const void*)r4d_addr(g.bref), 4))
|
|
return R4D_NO(RAD_E_ALIGN);
|
|
MxParts mp;
|
|
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 > (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;
|
|
const long long kb = (g.K + 127) / 128;
|
|
for (int p = 0; p < mp.n; ++p) {
|
|
const long long n0 = mp.n0[p], np = mp.n1[p] - mp.n0[p];
|
|
R4D_TRY("gemm_mxfp4a8_gemv",
|
|
mxg_launch(r4d_addr(g.a) + p * g.K, r4d_addr(g.ascale) + (long)(p * kb * 4),
|
|
r4d_addr(g.b) + n0 * g.K / 2, r4d_addr(g.bscale) + n0,
|
|
r4d_addr(g.bref) + n0, r4d_addr(g.y) + n0 * 2,
|
|
(int)g.M, (int)g.K, (int)np, as_group, as_rs, (long)(uintptr_t)s,
|
|
(int)(mp.lda ? mp.lda : g.K), (int)g.N, (int)g.N));
|
|
}
|
|
return RAD_OK;
|
|
}
|