Archived
libr4d: gemm_mxfp4a8_gemv, the MXFP4 x fp8 GEMM for one to eight rows
One stream's decode (M = 1 + the draft depth) is a pure weight stream; the decode row stages the weight through LDS and leaves narrow projections at a third to two thirds of DRAM bandwidth. The new row (priority 58; M <= 8, K % 64, N % 16; the same packed weight bytes as every MXFP4 row) keeps the weight in registers: WMMA from register fragments when K % 128 == 0, v_dot4 otherwise. The form and the wave count per block depend on K alone, so a row's output bits do not depend on M (a verify step's first row equals the plain decode of it). The scale and reference planes must be 4-byte aligned. Selftest: every reachable instantiation (both forms, both activation grids, M = 1..8, tile counts divisible by 4, by 2 only and odd, parts), the output prefilled with NaN, an odd column stride, and the first row's bits against M for gemv and decode. --perfmx times the MXFP4 rows at one shape. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
d6957e88a1
commit
75b4d0baa7
5 files changed
+458
-7
No files matched your search
@@ -148,6 +148,7 @@ set(R4D_UNITS
|
||||
"r4d_gemm_mxfp4a8_nt_m64|"
|
||||
"r4d_gemm_mxfp4a8_tiled|"
|
||||
"r4d_gemm_mxfp4a8_decode|"
|
||||
"r4d_gemm_mxfp4a8_gemv|"
|
||||
# Block-scaled FP8 (the format Qwen3.8-27B-FP8 ships in). The 128x128 block is fixed by the
|
||||
# checkpoint, and the entry points reject a mismatch.
|
||||
"r4d_gemm_fp8a16_nt_m1|"
|
||||
|
||||
@@ -0,0 +1,300 @@
|
||||
// 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).
|
||||
//
|
||||
// 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. 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
|
||||
// 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_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. 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 <bool ABLK>
|
||||
__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][R4D_MXG_MAX_M][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);
|
||||
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 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) {
|
||||
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);
|
||||
}
|
||||
// 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) {
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) sP[wave][row][khalf * 8 + e] = tot[e];
|
||||
}
|
||||
__syncthreads();
|
||||
for (int idx = threadIdx.x; idx < R4D_MXG_MAX_M * 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 > R4D_MXG_MAX_M) throw std::runtime_error("r4d gemm_mxfp4a8_gemv: M must be 1..8");
|
||||
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) {
|
||||
if (ablk)
|
||||
hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gemvw_kernel<true>), 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<false>), dim3(tiles), block, 0, st, Ap, Sp, Wp, Zp,
|
||||
Rp, Cp, M, K, as_rs, lda, ldws, ldc);
|
||||
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 > 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;
|
||||
}
|
||||
@@ -49,6 +49,8 @@ enum {
|
||||
R4D_PRIO_GEMM_SKINNY = 50,
|
||||
/* The MXFP4 decode row outranks the MXFP4 skinny row, whose band it shares up to N = 36864. */
|
||||
R4D_PRIO_GEMM_MXDEC = 55,
|
||||
/* The MXFP4 gemv row (one to eight rows) outranks the decode row, whose band it shares there. */
|
||||
R4D_PRIO_GEMM_MXGEMV = 58,
|
||||
/* The skinny m64 row has no upper bound on M, so the tiled bf16 row (M > 64 only) must outrank
|
||||
* it, and stays below m16, whose band it never enters. */
|
||||
R4D_PRIO_GEMM_BF16_TILED = 55,
|
||||
@@ -450,6 +452,7 @@ int r4d_rad_gemm_mxfp4a8_tiled(const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_gemm_mxfp4a8_tiled_scratch(const RadArgs*);
|
||||
int r4d_rad_gemm_mxfp4a8_decode(const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_gemm_mxfp4a8_decode_scratch(const RadArgs*);
|
||||
int r4d_rad_gemm_mxfp4a8_gemv(const RadArgs*, RadStream);
|
||||
int r4d_rad_dequant_w4 (const RadArgs*, RadStream);
|
||||
int r4d_rad_rowtopk (const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_rowtopk_scratch(const RadArgs*);
|
||||
|
||||
+21
-1
@@ -4798,10 +4798,17 @@ 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, the tiled row everything else. */
|
||||
* wider N up to M = 64 (from M = 9: the gemv row below takes M <= 8 at any N), 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"),
|
||||
};
|
||||
/* r4d_gemm_mxfp4a8_gemv. Entry point: M <= 8, N % 16, K % 64, no N bound (no counters). Above the
|
||||
* decode row by priority, so one stream's decode (M = 1 + the draft depth) takes it, at any N; the
|
||||
* decode row keeps M = 9..128. */
|
||||
static const RadConstraint cGemmMxfp4A8Gemv[] = {
|
||||
C_LE("M", 8), C_DIV("K", 64), 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. */
|
||||
@@ -7504,6 +7511,19 @@ 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_m64, nullptr, r4d_rad_layout_mxfp4,
|
||||
r4d_rad_relayout_mxfp4 },
|
||||
|
||||
{ "gemm_mxfp4a8_gemv", "gemm_nt_q", "gemm",
|
||||
"the OCP-MXFP4 GEMM for ONE TO EIGHT rows: the weight streamed straight to registers and folded "
|
||||
"to e4m3 there, no LDS staging; WMMA when K is a multiple of 128, v_dot4 otherwise, chosen from "
|
||||
"K alone so a row's bits do not depend on M",
|
||||
"M <= 8; N a multiple of 16; K a multiple of 64; 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(cGemmMxfp4A8Gemv), 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",
|
||||
|
||||
+133
-6
@@ -4257,10 +4257,13 @@ static float mxfp4_act(int64_t i, int64_t k, uint32_t seed) {
|
||||
*
|
||||
* `ablk` passes the activation scale on the block-scaled fp8 grid, one f32 per (row, 128 of K), as
|
||||
* fused quantisers write it. Scales span two decades, and every eleventh block's is zero with zero
|
||||
* codes (an all-zero activation block), where the kernels carry their previous scale through. */
|
||||
* codes (an all-zero activation block), where the kernels carry their previous scale through.
|
||||
*
|
||||
* The output starts as NaN, so a column a kernel never writes fails the check instead of reading
|
||||
* 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<RadParam> extra = {},
|
||||
int64_t pb1 = 0, int64_t pb2 = 0) {
|
||||
int64_t pb1 = 0, int64_t pb2 = 0, std::vector<uint16_t>* row0 = nullptr) {
|
||||
if (!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. */
|
||||
@@ -4311,7 +4314,7 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_
|
||||
av[(size_t)(i * PK + k)] = rad_load_f32(ha.data(), RAD_F8E4M3, i * PK + k);
|
||||
}
|
||||
}
|
||||
std::vector<uint16_t> hy((size_t)(M * N), 0);
|
||||
std::vector<uint16_t> hy((size_t)(M * N), 0x7fc0); /* bf16 NaN */
|
||||
void* d_a = dalloc(ha.size());
|
||||
void* d_s = dalloc(as.size() * 4);
|
||||
void* d_w = dalloc(wq.size());
|
||||
@@ -4319,6 +4322,7 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_
|
||||
void* d_r = dalloc(wr.size());
|
||||
void* d_y = dalloc(hy.size() * 2);
|
||||
if (!d_a || !d_s || !d_w || !d_e || !d_r || !d_y) { ok(nm, false, "hipMalloc failed"); return; }
|
||||
st_memcpy(d_y, hy.data(), hy.size() * 2, hipMemcpyHostToDevice);
|
||||
st_memcpy(d_a, ha.data(), ha.size(), hipMemcpyHostToDevice);
|
||||
st_memcpy(d_s, as.data(), as.size() * 4, hipMemcpyHostToDevice);
|
||||
st_memcpy(d_w, wq.data(), wq.size(), hipMemcpyHostToDevice);
|
||||
@@ -4358,10 +4362,13 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_
|
||||
return;
|
||||
}
|
||||
st_memcpy(hy.data(), d_y, hy.size() * 2, hipMemcpyDeviceToHost);
|
||||
if (row0) row0->assign(hy.begin(), hy.begin() + N);
|
||||
|
||||
/* Every column for small N, a stride otherwise, always including the last column (the clamped
|
||||
* tail of the tiled row's weight staging). */
|
||||
const int64_t nstep = N > 96 ? N / 96 : 1;
|
||||
* tail of the tiled row's weight staging). The stride is odd, so it walks through every
|
||||
* position in a group of 2, 4 or 16 columns -- a kernel that gets one lane's column wrong in
|
||||
* such a group cannot hide behind an even step. */
|
||||
const int64_t nstep = N > 96 ? (N / 96) | 1 : 1;
|
||||
double num = 0, den = 0, worst = 0;
|
||||
for (int64_t i = 0; i < M; ++i)
|
||||
for (int64_t n = 0; n < N; n = (n + 1 == N) ? N : std::min(n + nstep, N - 1)) {
|
||||
@@ -4385,6 +4392,28 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_
|
||||
ok(nm, rel < 1e-2, d);
|
||||
}
|
||||
|
||||
/* A row's output bits must not depend on how many rows ride with it: a verify step's first row
|
||||
* then decodes exactly as the plain step would, and greedy text does not change with the draft
|
||||
* depth. The first row of M = 1..8 (identical inputs, mxfp4a8_numeric's activations depend on the
|
||||
* row index only) compared bit for bit. */
|
||||
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,
|
||||
(long long)N, (long long)K, ablk ? " ascale=row x 128" : "");
|
||||
std::vector<uint16_t> ref, got;
|
||||
mxfp4a8_numeric(pl, row, 1, N, K, 0, ablk, {}, 0, 0, &ref);
|
||||
for (int64_t M = 2; M <= 8; ++M) {
|
||||
got.clear();
|
||||
mxfp4a8_numeric(pl, row, M, N, K, 0, ablk, {}, 0, 0, &got);
|
||||
if (ref.size() != (size_t)N || got != ref) {
|
||||
ok(nm, false, "M=" + std::to_string(M) + " differs from M=1");
|
||||
return;
|
||||
}
|
||||
}
|
||||
ok(nm, true);
|
||||
}
|
||||
|
||||
|
||||
/* What givens_numeric runs: the sequential rotation to f16, or the dense rotation with either
|
||||
* quantiser. */
|
||||
@@ -12798,6 +12827,64 @@ static void perf_row(const Plugin& pl, const char* name, std::vector<RadTensor>
|
||||
std::printf(" %-22s %8.1f us %6.1f GB/s\n", name, us, bytes / (us * 1e3));
|
||||
}
|
||||
|
||||
/* --perfmx: every MXFP4 GEMM row at one shape, the weight streamed from DRAM (rotated copies), on
|
||||
* the per-(row, 128) activation grid. A row that does not serve the shape says so. */
|
||||
static void mx_perf(const Plugin& pl, int64_t N, int64_t K, int64_t M, int iters) {
|
||||
if (M < 1 || N < 16 || N % 16 || K < 64 || K % 64) {
|
||||
std::printf("--perfmx: N must be a multiple of 16, K of 64, and M >= 1\n");
|
||||
return;
|
||||
}
|
||||
const char* rows[] = { "gemm_mxfp4a8_gemv", "gemm_mxfp4a8_decode", "gemm_mxfp4a8_nt_m64",
|
||||
"gemm_mxfp4a8_tiled" };
|
||||
for (const char* row : rows) {
|
||||
dfree_all();
|
||||
const RadKernelInfo* r = find(pl, row);
|
||||
if (!r || !r->layout || !r->relayout) { std::printf(" %-22s row or hooks absent\n", row); continue; }
|
||||
Planes p;
|
||||
p.enc = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32);
|
||||
p.size(N, K);
|
||||
const int ic = p.find("codes"), is = p.find("scale");
|
||||
mxfp4_fill(p, ic, is, N, K, 0x51u, true);
|
||||
std::vector<RadParam> lp = { PI("N", N), PI("K", K), PI("group", 32) };
|
||||
std::vector<uint8_t> wq, ws, wr;
|
||||
if (stored(r, lp, 2, p, { "codes" }, &wq) != RAD_OK || stored(r, lp, 3, p, { "scale" }, &ws) != RAD_OK ||
|
||||
stored(r, lp, 6, p, { "scale" }, &wr) != RAD_OK) {
|
||||
std::printf(" %-22s relayout refused\n", row);
|
||||
continue;
|
||||
}
|
||||
const int64_t nkb = (K + 127) / 128;
|
||||
std::vector<uint8_t> ha((size_t)(M * K));
|
||||
std::vector<float> as((size_t)(M * nkb));
|
||||
for (int64_t i = 0; i < M; ++i) {
|
||||
for (int64_t c = 0; c < nkb; ++c) as[(size_t)(i * nkb + c)] = 0.002f * (float)(1 + (i + c) % 97);
|
||||
for (int64_t k = 0; k < K; ++k) rad_store_f32(ha.data(), RAD_F8E4M3, i * K + k, mxfp4_act(i, k, 0x3cu));
|
||||
}
|
||||
const int copies = (int)std::max<int64_t>(2, (256ll << 20) / (int64_t)wq.size() + 1);
|
||||
void* d_a = dalloc(ha.size());
|
||||
void* d_s = dalloc(as.size() * 4);
|
||||
void* d_w = dalloc(wq.size() * (size_t)copies);
|
||||
void* d_e = dalloc(ws.size());
|
||||
void* d_r = dalloc(wr.size());
|
||||
void* d_y = dalloc((size_t)(M * N * 2));
|
||||
if (!d_a || !d_s || !d_w || !d_e || !d_r || !d_y) { std::printf(" %-22s hipMalloc failed\n", row); continue; }
|
||||
st_memcpy(d_a, ha.data(), ha.size(), hipMemcpyHostToDevice);
|
||||
st_memcpy(d_s, as.data(), as.size() * 4, hipMemcpyHostToDevice);
|
||||
for (int c = 0; c < copies; ++c)
|
||||
st_memcpy((uint8_t*)d_w + (size_t)c * wq.size(), wq.data(), wq.size(), hipMemcpyHostToDevice);
|
||||
st_memcpy(d_e, ws.data(), ws.size(), hipMemcpyHostToDevice);
|
||||
st_memcpy(d_r, wr.data(), wr.size(), hipMemcpyHostToDevice);
|
||||
std::vector<RadTensor> ts = {
|
||||
T(d_a, RAD_F8E4M3, { M, K }), T(d_s, RAD_F32, { M, nkb }),
|
||||
T(d_w, RAD_U8, { (int64_t)wq.size() }), T(d_e, RAD_U8, { (int64_t)ws.size() }),
|
||||
T(d_y, RAD_BF16, { M, N }), T(nullptr, RAD_F32, { 1 }), T(d_r, RAD_U8, { (int64_t)wr.size() }),
|
||||
};
|
||||
absent(ts[5]);
|
||||
std::vector<RadParam> ps = { PI("M", M), PI("N", N), PI("K", K), PI("group", 32),
|
||||
PS("dtype", "mxfp4a8") };
|
||||
perf_row(pl, row, ts, ps, d_w, wq.size(), copies, (double)(wq.size() + ws.size()), iters);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------ the small ops, timed
|
||||
*
|
||||
@@ -26796,6 +26883,31 @@ static void device_phase(const Plugin& pl) {
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_decode", 64, 512, 2560, 0, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_decode", 5, 1040, 576, 0, true);
|
||||
mxd_overlap_case(pl);
|
||||
/* The gemv row (M <= 8). K a multiple of 128 takes the WMMA form at every M: one tile and
|
||||
* one chunk with masked rows, the production shapes, both activation grids. K = 64 mod 128
|
||||
* takes the dot form, every instantiation the launcher reaches: M = 1, 2, 3-4, 5-8 each at
|
||||
* a tile count divisible by 4 (68), by 2 only (66) and odd (67), both grids, and one 64-K,
|
||||
* 16-column minimum. Then the first row's bits against M, for both forms and the decode
|
||||
* row. */
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 3, 16, 128, 0, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 1, 272, 640, 0, false);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 1, 17408, 5120, 0, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 2, 5120, 8704, 0, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 4, 17408, 5120, 0, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 5, 1072, 2560, 0, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 7, 10288, 640, 0, false);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 8, 5120, 3072, 0, false);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 8, 5376, 10752, 0, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 1, 16, 64, 0, true);
|
||||
for (const int64_t m : { 1, 2, 3, 4, 5, 6, 7, 8 })
|
||||
for (const int64_t n : { 1088, 1056, 1072 }) {
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", m, n, 576, 0, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", m, n, 576, 0, false);
|
||||
}
|
||||
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);
|
||||
mxfp4a8_m_invariant(pl, "gemm_mxfp4a8_decode", 5120, 5120, true);
|
||||
/* Parts (ParoQuant): each weight part against its own activation copy, with bounds off any
|
||||
* kernel's tile, two and three parts, at every band. */
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_nt_m64", 7, 400, 640, 0, true, {}, 144);
|
||||
@@ -26803,6 +26915,11 @@ static void device_phase(const Plugin& pl) {
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_decode", 8, 400, 2560, 0, true, {}, 128);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_decode", 40, 512, 2560, 0, true, {}, 128, 384);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_decode", 4, 34816, 5120, 0, true, {}, 17408);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 1, 400, 640, 0, true, {}, 144);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 8, 400, 2560, 0, true, {}, 128);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 4, 34816, 5120, 0, true, {}, 17408);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 2, 400, 576, 0, true, {}, 144, 288);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv", 5, 512, 576, 0, true, {}, 128, 384);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 300, 400, 640, 2, true, {}, 192);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 300, 400, 640, 4, true, {}, 96, 304);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 2100, 512, 2560, 0, true, {}, 256);
|
||||
@@ -28011,6 +28128,8 @@ static void usage(void) {
|
||||
" u64 at byte 0> + the tensor's own data_offsets[0].\n"
|
||||
" --perf N K M [iters]: time the fp8 decode GEMMs at one shape and print the\n"
|
||||
" weight bandwidth each reaches.\n"
|
||||
" --perfmx N K M [iters]: the MXFP4 GEMM rows (gemv, decode, skinny, tiled) at\n"
|
||||
" one shape, with the weight bandwidth each reaches.\n"
|
||||
" --perfbf16 N K M [iters]: the bf16 skinny GEMM at every variant it has.\n"
|
||||
" --perff32 N K M [iters]: gemm_nt_f32 at the tile form its rule picks, compared\n"
|
||||
" byte for byte with the host arithmetic, with GB/s and TFLOP/s.\n"
|
||||
@@ -28123,7 +28242,7 @@ int main(int argc, char** argv) {
|
||||
const char* real_file = nullptr;
|
||||
long long real_woff = 0, real_soff = 0, real_n = 0, real_k = 0;
|
||||
long long perf_n = 0, perf_k = 0, perf_m = 0;
|
||||
int perf_it = 50, perf_w2 = 0, perf_bf = 0, perf_w8 = 0, perf_f32 = 0, perf_q = 0;
|
||||
int perf_it = 50, perf_w2 = 0, perf_bf = 0, perf_w8 = 0, perf_f32 = 0, perf_q = 0, perf_mx = 0;
|
||||
long long sop_m = 0, sop_k = 0;
|
||||
int sop_it = 2000;
|
||||
long long qsa_ctx = 0, qsa_m = 4, dsa_ctx = 0, dsa_m = 6;
|
||||
@@ -28184,6 +28303,13 @@ int main(int argc, char** argv) {
|
||||
perf_k = std::strtoll(argv[++i], nullptr, 0);
|
||||
perf_m = std::strtoll(argv[++i], nullptr, 0);
|
||||
if (i + 1 < argc && argv[i + 1][0] != '-') perf_it = (int)std::strtol(argv[++i], nullptr, 0);
|
||||
} else if (!std::strcmp(argv[i], "--perfmx")) {
|
||||
if (i + 3 >= argc) { usage(); return 2; }
|
||||
perf_mx = 1;
|
||||
perf_n = std::strtoll(argv[++i], nullptr, 0);
|
||||
perf_k = std::strtoll(argv[++i], nullptr, 0);
|
||||
perf_m = std::strtoll(argv[++i], nullptr, 0);
|
||||
if (i + 1 < argc && argv[i + 1][0] != '-') perf_it = (int)std::strtol(argv[++i], nullptr, 0);
|
||||
} else if (!std::strcmp(argv[i], "--perfbf16")) {
|
||||
/* The bf16 skinny GEMM at every variant (see bf16_perf). */
|
||||
if (i + 3 >= argc) { usage(); return 2; }
|
||||
@@ -28585,6 +28711,7 @@ int main(int argc, char** argv) {
|
||||
#ifdef RAD_HAVE_HIP
|
||||
if (perf_f32) f32_forms(pl, perf_m, perf_n, perf_k, perf_it, true);
|
||||
else if (perf_q) q_perf(pl, perf_n, perf_k, perf_m, perf_it);
|
||||
else if (perf_mx) mx_perf(pl, perf_n, perf_k, perf_m, perf_it);
|
||||
else if (perf_bf) bf16_perf(pl, perf_n, perf_k, perf_m, perf_it);
|
||||
else if (perf_w8) w8_perf(pl, perf_n, perf_k, perf_m, perf_it);
|
||||
else if (perf_w2) w2_perf(pl, perf_n, perf_k, perf_m, perf_it);
|
||||
|
||||
Reference in new issue
Block a user