gemm_nt_q_gated_mx at gelu_tanh: the GeGLU MLP's gate_up, gelu_mul and quant_act_fp8 in one launch

TranslateGemma's MLP over MXFP4 runs gate_up, gelu_mul into a bf16 plane and quant_act_fp8 over it
before down. The gated fold now takes act gelu_tanh: gelu_mul's tanh GELU with its constants, the
product rounded to bf16 as gelu_mul stores it, then quant_act_fp8's quantiser (the same arithmetic as
gated_quant_fp8's). Byte-identical to the three-op chain (selftest at M = 1, 4, 8 on TranslateGemma's
per-rank gate_up, the smallest shape and the per-row scale); the replaces-hook gives the chain. The
GeGLU form writes no out_bf16: the chain's bf16 plane is gelu_mul's own output, which the hook cannot
hand to an optional operand, and the shim refuses one.

The gemma3 plugin declares the fold at up to 8 rows when both MLP linears are MXFP4 and no
calibration tap reads the bf16 plane.

TranslateGemma alone, speculation 7, two rounds each: one stream 144.5 / 144.9 -> 151.5 / 150.5 t/s,
four streams (32 rows a step, past the band) unchanged; greedy translations identical.

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-25 09:59:53 +01:00
1 parent f16854499c
commit a596f68782
6 files changed
+113 -37

No files matched your search

+32 -3
View File
@@ -655,6 +655,11 @@ struct Gemma3Mlp {
G3Linear ln_gu{}, ln_down{}; G3Linear ln_gu{}, ln_down{};
rad_op op_norm = 0, op_act = 0, op_post = 0, op_ar = 0, op_add = 0; rad_op op_norm = 0, op_act = 0, op_post = 0, op_ar = 0, op_add = 0;
rad_op op_gq = 0; rad_op op_gq = 0;
/* gate|up, the GeGLU and down's E4M3 quantiser in one launch (libr4d gemm_nt_q_gated_mx at
* gelu_tanh) at up to `fold_rows` rows, when both linears are MXFP4: byte-identical to the
* GEMM, gelu_mul and quant_act_fp8 it replaces. */
rad_op op_fold = 0;
int64_t fold_rows = 0;
/* Calibration: gate|up's input and down's (this rank's columns). */ /* Calibration: gate|up's input and down's (this rank's columns). */
G3Tap tap_gu{}, tap_dn{}; G3Tap tap_gu{}, tap_dn{};
const bool* tap_live = nullptr; const bool* tap_live = nullptr;
@@ -725,6 +730,21 @@ int Gemma3Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_ff), RAD_STR("dtype", g.dtype)), RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_ff), RAD_STR("dtype", g.dtype)),
RAD_NOWEIGHTS), RAD_NOWEIGHTS),
{w.gu}, {ln_down.q8, ln_down.s8}); {w.gu}, {ln_down.q8, ln_down.s8});
// Not under calibration either: the down tap reads the bf16 product, which the fold never writes.
{
const int64_t hi = g.max_tok < 8 ? g.max_tok : 8;
if (ln_gu.mx && ln_down.mx && !ln_down.shared_in && !calib_dir() && hi > 0 &&
ln_gu.N % 256 == 0 && ln_gu.K % RAD_FP8_BLOCK == 0 &&
rad_op_declared(b, "gemm_nt_q_gated_mx")) {
op_fold = rw(b, RAD_OP(b, "gemm_nt_q_gated_mx",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", ln_gu.N), RAD_INT("K", ln_gu.K),
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "mxfp4a8"),
RAD_STR("act", "gelu_tanh")),
RAD_WEIGHTS(ln_gu.w, ln_gu.ws, ln_gu.wref)),
{ln_gu.q8, ln_gu.s8}, {ln_down.q8, ln_down.s8});
if (op_fold) fold_rows = hi;
}
}
if (g.world > 1) if (g.world > 1)
op_ar = rw(b, RAD_OP(b, "all_reduce", op_ar = rw(b, RAD_OP(b, "all_reduce",
RAD_PARAMS(RAD_INT("world_size", g.world), RAD_RANGE("numel", g.n_embd, g.max_tok * g.n_embd), RAD_PARAMS(RAD_INT("world_size", g.world), RAD_RANGE("numel", g.n_embd, g.max_tok * g.n_embd),
@@ -754,14 +774,23 @@ int Gemma3Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
void Gemma3Mlp::step(RadCtx* c, const RadBatch* batch, bool entry_done, bool tail_fused, void Gemma3Mlp::step(RadCtx* c, const RadBatch* batch, bool entry_done, bool tail_fused,
bool ar_fused) const { bool ar_fused) const {
const int64_t T = batch->n_tok; const int64_t T = batch->n_tok;
const bool fold = op_fold && T <= fold_rows;
if (entry_done) { if (entry_done) {
ln_gu.gemm(c, w.gu, T); if (!fold) ln_gu.gemm(c, w.gu, T);
} else { } else {
RAD_ISSUE(c, op_norm, brows(w.x, T), RAD_W(w_norm), brows(w.h, T)); RAD_ISSUE(c, op_norm, brows(w.x, T), RAD_W(w_norm), brows(w.h, T));
ln_gu.step(c, w.h, w.gu, T); if (!fold)
ln_gu.step(c, w.h, w.gu, T);
else if (!ln_gu.shared_in)
RAD_ISSUE_N(c, ln_gu.op_q, T, brows(w.h, T), brows(ln_gu.q8, T), brows(ln_gu.s8, T));
tap_gu.step(c, w.h, T, tap_live); tap_gu.step(c, w.h, T, tap_live);
} }
if (op_gq) { if (fold) {
RAD_ISSUE_N(c, op_fold, T, brows(ln_gu.q8, T), brows(ln_gu.s8, T), RAD_W(ln_gu.w),
RAD_W(ln_gu.ws), RAD_W(ln_gu.wref), brows(ln_down.q8, T), brows(ln_down.s8, T),
RAD_NONE);
ln_down.gemm(c, w.h, T);
} else if (op_gq) {
RAD_ISSUE_N(c, op_gq, T, brows(w.gu, T), brows(ln_down.q8, T), brows(ln_down.s8, T), RAD_NONE); RAD_ISSUE_N(c, op_gq, T, brows(w.gu, T), brows(ln_down.q8, T), brows(ln_down.s8, T), RAD_NONE);
ln_down.gemm(c, w.h, T); ln_down.gemm(c, w.h, T);
} else { } else {
+2 -1
View File
@@ -607,7 +607,8 @@ another.
`gemm_nt_q_gated_mx` is the same fold over an OCP-MXFP4 weight (`dtype` `mxfp4a8`, `group` 32), which `gemm_nt_q_gated_mx` is the same fold over an OCP-MXFP4 weight (`dtype` `mxfp4a8`, `group` 32), which
`gemm_nt_q` reads with the per-row reference exponent `b_ref` after `b_scale`. A schema of its own `gemm_nt_q` reads with the per-row reference exponent `b_ref` after `b_scale`. A schema of its own
because an op's operand count is fixed for the plugins built against it. because an op's operand count is fixed for the plugins built against it. `act` `gelu_tanh` is the
GeGLU form, `gemm_nt_q` then `gelu_mul` then `quant_act_fp8`; it writes no `out_bf16`.
`swiglu_clamped_quant_fp8` (device-only) is `swiglu_clamped` then `quant_act_fp8` over its `swiglu_clamped_quant_fp8` (device-only) is `swiglu_clamped` then `quant_act_fp8` over its
product in one pass. The product is rounded to bf16 before the quantiser reads it, as the pair product in one pass. The product is rounded to bf16 before the quantiser reads it, as the pair
+20 -4
View File
@@ -518,7 +518,8 @@ extern "C" int r4d_fuse_gemm_nt_q_gated(const RadParam* p, int n_p, uint64_t pre
* *
* The GEMM step is gemm_nt_q at dtype mxfp4a8 with `b_ref` in its last operand. `group` is the * The GEMM step is gemm_nt_q at dtype mxfp4a8 with `b_ref` in its last operand. `group` is the
* weight's 32 on the GEMM and the activation's 128 on the quantiser, so the quantiser's is set, not * weight's 32 on the GEMM and the activation's 128 on the quantiser, so the quantiser's is set, not
* mirrored. * mirrored. At act gelu_tanh the chain is three steps, gelu_mul into a second temporary and
* quant_act_fp8 over it; that form refuses `out_bf16` (the shim), so operand 7 is not wired.
*/ */
extern "C" int r4d_fuse_gemm_nt_q_gated_mx(const RadParam* p, int n_p, uint64_t present, int step, extern "C" int r4d_fuse_gemm_nt_q_gated_mx(const RadParam* p, int n_p, uint64_t present, int step,
RadFuseStep* out) { RadFuseStep* out) {
@@ -528,16 +529,31 @@ extern "C" int r4d_fuse_gemm_nt_q_gated_mx(const RadParam* p, int n_p, uint64_t
if (M <= 0 || N <= 0 || K <= 0 || N % 2) return RAD_E_UNSUPPORTED; if (M <= 0 || N <= 0 || K <= 0 || N % 2) return RAD_E_UNSUPPORTED;
if (pi(p, n_p, "group", 32) != 32) return RAD_E_UNSUPPORTED; if (pi(p, n_p, "group", 32) != 32) return RAD_E_UNSUPPORTED;
if (!dt || std::strcmp(dt, "mxfp4a8")) return RAD_E_UNSUPPORTED; if (!dt || std::strcmp(dt, "mxfp4a8")) return RAD_E_UNSUPPORTED;
if (!act_is(p, n_p, "silu", "silu")) return RAD_E_UNSUPPORTED; const bool gelu = act_is(p, n_p, "silu", "gelu_tanh");
if (!gelu && !act_is(p, n_p, "silu", "silu")) return RAD_E_UNSUPPORTED;
switch (step) { if (step == 0) {
case 0: {
Step s(out, "gemm_nt_q"); Step s(out, "gemm_nt_q");
s.mirror(p, n_p, "M").mirror(p, n_p, "N").mirror(p, n_p, "K") s.mirror(p, n_p, "M").mirror(p, n_p, "N").mirror(p, n_p, "K")
.mirror(p, n_p, "group").mirror(p, n_p, "dtype"); .mirror(p, n_p, "group").mirror(p, n_p, "dtype");
/* a, a_scale, b, b_scale, y, a_sum, b_ref */ /* a, a_scale, b, b_scale, y, a_sum, b_ref */
return s.wire({ 0, 1, 2, 3, RAD_FUSE_TMP(0), RAD_FUSE_NONE, 4 }); return s.wire({ 0, 1, 2, 3, RAD_FUSE_TMP(0), RAD_FUSE_NONE, 4 });
} }
if (gelu) {
if (step == 1) {
Step s(out, "gelu_mul");
s.mirror(p, n_p, "M").pint("n", (long long)(N / 2)).pstr("dtype", "bf16");
return s.wire({ RAD_FUSE_TMP(0), RAD_FUSE_TMP(1) }); /* gate_up, y */
}
if (step == 2) {
Step s(out, "quant_act_fp8");
s.mirror(p, n_p, "M").pint("n", (long long)(N / 2)).pint("group", 128)
.pstr("dtype", "bf16");
return s.wire({ RAD_FUSE_TMP(1), 5, 6 }); /* x, q, scale */
}
return RAD_E_NOTFOUND;
}
switch (step) {
case 1: { case 1: {
Step s(out, "gated_quant_fp8"); Step s(out, "gated_quant_fp8");
s.mirror(p, n_p, "M").pint("n", (long long)(N / 2)).pint("group", 128) s.mirror(p, n_p, "M").pint("n", (long long)(N / 2)).pint("group", 128)
+22 -16
View File
@@ -363,7 +363,9 @@ extern "C" int r4d_rad_gemm_mxfp4a8_gemv(const RadArgs* a, RadStream s) {
// Only one partial is live at a time, which is why the chunks are taken in that order rather than // Only one partial is live at a time, which is why the chunks are taken in that order rather than
// in K order. The scaled sum is rounded to bf16 by the same conversion, and from there the arithmetic // in K order. The scaled sum is rounded to bf16 by the same conversion, and from there the arithmetic
// is r4d_fused_quant_fp8.hip's gated_quant_fp8_kernel: silu(gate) * up rounded to bf16, the group's // is r4d_fused_quant_fp8.hip's gated_quant_fp8_kernel: silu(gate) * up rounded to bf16, the group's
// amax, scale amax / 448, codes through q8_pack4. // amax, scale amax / 448, codes through q8_pack4. ACT 1 is the GeGLU form: gelu_mul's tanh GELU with
// its constants (r4d_model_bf16.hip), the product rounded to bf16 as gelu_mul stores it, then
// quant_act_fp8's quantiser, which is the same arithmetic as gated_quant_fp8's.
namespace { namespace {
__device__ __forceinline__ float mxg_bf2f(uint16_t h) { return __uint_as_float((uint32_t)h << 16); } __device__ __forceinline__ float mxg_bf2f(uint16_t h) { return __uint_as_float((uint32_t)h << 16); }
__device__ __forceinline__ uint16_t mxg_f2bf(float f) { // as r4d_fused_quant_fp8.hip's f2bf __device__ __forceinline__ uint16_t mxg_f2bf(float f) { // as r4d_fused_quant_fp8.hip's f2bf
@@ -372,7 +374,7 @@ __device__ __forceinline__ uint16_t mxg_f2bf(float f) { // as r4d_fused_quant_
} }
} // namespace } // namespace
template <bool ABLK> template <bool ABLK, int ACT>
__global__ __launch_bounds__(16 * 32) __global__ __launch_bounds__(16 * 32)
void r4d_gemm_mxfp4a8_gated_kernel( void r4d_gemm_mxfp4a8_gated_kernel(
const unsigned char* __restrict__ A, const float* __restrict__ As, const unsigned char* __restrict__ A, const float* __restrict__ As,
@@ -447,7 +449,8 @@ void r4d_gemm_mxfp4a8_gated_kernel(
#pragma unroll #pragma unroll
for (int j = 0; j < 4; ++j) { for (int j = 0; j < 4; ++j) {
const float g = mxg_bf2f(sG[t][m][cc + j]); const float g = mxg_bf2f(sG[t][m][cc + j]);
const float a = g / (1.0f + expf(-g)); const float a = ACT == 0 ? g / (1.0f + expf(-g))
: 0.5f * g * (1.0f + tanhf(0.7978845608028654f * (g + 0.044715f * g * g * g)));
const uint16_t h = mxg_f2bf(a * mxg_bf2f(sG[8 + t][m][cc + j])); const uint16_t h = mxg_f2bf(a * mxg_bf2f(sG[8 + t][m][cc + j]));
if (Y) Y[(size_t)m * y_ld + gcol + j] = h; if (Y) Y[(size_t)m * y_ld + gcol + j] = h;
vq[j] = mxg_bf2f(h); vq[j] = mxg_bf2f(h);
@@ -462,21 +465,19 @@ void r4d_gemm_mxfp4a8_gated_kernel(
} }
namespace { namespace {
void mxgg_launch(bool ablk, const void* a, const void* as, const void* w, const void* ws, void mxgg_launch(bool ablk, int act, const void* a, const void* as, const void* w, const void* ws,
const void* wr, void* q, void* s, void* y, int M, int K, int n, int as_rs, int lda, const void* wr, void* q, void* s, void* y, int M, int K, int n, int as_rs, int lda,
int ldws, int q_ld, int s_ld, int y_ld, RadStream stream) { int ldws, int q_ld, int s_ld, int y_ld, RadStream stream) {
const dim3 grid((uint32_t)(n / 128)), block(16 * 32); const dim3 grid((uint32_t)(n / 128)), block(16 * 32);
hipStream_t st = (hipStream_t)stream; hipStream_t st = (hipStream_t)stream;
if (ablk) #define MXGG_L(AB_, ACT_) \
hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gated_kernel<true>), grid, block, 0, st, hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gated_kernel<AB_, ACT_>), grid, block, 0, st, \
(const unsigned char*)a, (const float*)as, (const unsigned char*)w, (const unsigned char*)a, (const float*)as, (const unsigned char*)w, \
(const unsigned char*)ws, (const unsigned char*)wr, (uint8_t*)q, (float*)s, (const unsigned char*)ws, (const unsigned char*)wr, (uint8_t*)q, (float*)s, \
(uint16_t*)y, M, K, n, as_rs, lda, ldws, q_ld, s_ld, y_ld); (uint16_t*)y, M, K, n, as_rs, lda, ldws, q_ld, s_ld, y_ld)
else if (act == 0) { if (ablk) MXGG_L(true, 0); else MXGG_L(false, 0); }
hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gated_kernel<false>), grid, block, 0, st, else { if (ablk) MXGG_L(true, 1); else MXGG_L(false, 1); }
(const unsigned char*)a, (const float*)as, (const unsigned char*)w, #undef MXGG_L
(const unsigned char*)ws, (const unsigned char*)wr, (uint8_t*)q, (float*)s,
(uint16_t*)y, M, K, n, as_rs, lda, ldws, q_ld, s_ld, y_ld);
} }
} // namespace } // namespace
@@ -499,8 +500,13 @@ extern "C" int r4d_rad_gemm_mxfp4a8_gated(const RadArgs* a, RadStream s) {
if (q->rank != 2 || sc->rank != 2 || x->rank < 2) return R4D_NO(RAD_E_SHAPE); if (q->rank != 2 || sc->rank != 2 || x->rank < 2) return R4D_NO(RAD_E_SHAPE);
if (y && y->rank != 2) return R4D_NO(RAD_E_SHAPE); if (y && y->rank != 2) return R4D_NO(RAD_E_SHAPE);
if (r4d_geti_or(a, "group", 32) != 32) return R4D_NO(RAD_E_SHAPE); if (r4d_geti_or(a, "group", 32) != 32) return R4D_NO(RAD_E_SHAPE);
/* silu (the SwiGLU MLP) or gelu_tanh (GeGLU, gelu_mul's). The GeGLU chain's bf16 plane is
* gelu_mul's output, which the fold's replaces-hook cannot hand to the optional `out_bf16`, so
* that form writes none. */
const char* act = r4d_gets(a, "act"); const char* act = r4d_gets(a, "act");
if (!act || std::strcmp(act, "silu")) return R4D_NO(RAD_E_UNSUPPORTED); const int actm = !act ? -1 : !std::strcmp(act, "silu") ? 0 : !std::strcmp(act, "gelu_tanh") ? 1 : -1;
if (actm < 0) return R4D_NO(RAD_E_UNSUPPORTED);
if (actm == 1 && y) return R4D_NO(RAD_E_UNSUPPORTED);
/* The gated width from `q`, the buffer written; N and K from the op's parameters, since the /* The gated width from `q`, the buffer written; N and K from the op's parameters, since the
* weight operands are packed bytes. */ * weight operands are packed bytes. */
@@ -531,7 +537,7 @@ extern "C" int r4d_rad_gemm_mxfp4a8_gated(const RadArgs* a, RadStream s) {
return R4D_NO(RAD_E_ALIGN); return R4D_NO(RAD_E_ALIGN);
R4D_TRY("gemm_mxfp4a8_gated", R4D_TRY("gemm_mxfp4a8_gated",
mxgg_launch(ablk, x->data, xs->data, w->data, ws->data, wr->data, q->data, sc->data, mxgg_launch(ablk, actm, x->data, xs->data, w->data, ws->data, wr->data, q->data, sc->data,
y ? y->data : nullptr, (int)M, (int)K, (int)nout, (int)as_rs, (int)lda, (int)N, y ? y->data : nullptr, (int)M, (int)K, (int)nout, (int)as_rs, (int)lda, (int)N,
(int)qp, (int)sp, (int)yp, s)); (int)qp, (int)sp, (int)yp, s));
return RAD_OK; return RAD_OK;
+4 -3
View File
@@ -4830,7 +4830,7 @@ static const RadConstraint cGemmNtQGatedFp8[] = {
* per 128-column group of the gated output (N/2 a multiple of 128). */ * per 128-column group of the gated output (N/2 a multiple of 128). */
static const RadConstraint cGemmNtQGatedMx[] = { static const RadConstraint cGemmNtQGatedMx[] = {
C_LE("M", 8), C_DIV("K", 128), C_DIV("N", 256), C_EQ("group", 32), C_IN("dtype", "mxfp4a8"), C_LE("M", 8), C_DIV("K", 128), C_DIV("N", 256), C_EQ("group", 32), C_IN("dtype", "mxfp4a8"),
C_IN("act", "silu"), C_IN("act", "silu gelu_tanh"),
}; };
static const RadConstraint cGateQuantFp8[] = { static const RadConstraint cGateQuantFp8[] = {
C_DIV("n", 4), C_EQ("group", R4D_FP8_BLOCK), C_IN("dtype", "bf16"), C_DIV("n", 4), C_EQ("group", R4D_FP8_BLOCK), C_IN("dtype", "bf16"),
@@ -7771,11 +7771,12 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
"block per 128-column group of the gated output, a wave per gate or up tile running all of that " "block per 128-column group of the gated output, a wave per gate or up tile running all of that "
"form's K partials in its order, so the [M, 2n] bf16 product is never written", "form's K partials in its order, so the [M, 2n] bf16 product is never written",
"M <= 8; K a multiple of 128; N a multiple of 256 (the gated width a multiple of the 128-column " "M <= 8; K a multiple of 128; N a multiple of 256 (the gated width a multiple of the 128-column "
"scale group); act silu only; the skinny kernel's fragment-order weight, [K/32][N] E8M0 " "scale group); act silu, or gelu_tanh without out_bf16; the skinny kernel's fragment-order weight, [K/32][N] E8M0 "
"exponents and per-row reference exponent, gate rows then up rows", "exponents and per-row reference exponent, gate rows then up rows",
"e4m3 A with an f32 scale per row or per (row, 128), e2m1 W with an E8M0 scale per 32 K, and the " "e4m3 A with an f32 scale per row or per (row, 128), e2m1 W with an E8M0 scale per 32 K, and the "
"QUANTISER's outputs: E4M3 codes, an f32 scale per (row, 128 columns) and an optional bf16 " "QUANTISER's outputs: E4M3 codes, an f32 scale per (row, 128 columns) and an optional bf16 "
"product. Byte-identical to gemm_mxfp4a8_gemv followed by gated_quant_fp8", "product. Byte-identical to gemm_mxfp4a8_gemv followed by gated_quant_fp8 (silu), or by gelu_mul "
"and quant_act_fp8 (gelu_tanh)",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cGemmNtQGatedMx), NOTUNE, ROW(cGemmNtQGatedMx), NOTUNE,
nullptr, nullptr, r4d_rad_gemm_mxfp4a8_gated, nullptr, nullptr, nullptr, r4d_rad_gemm_mxfp4a8_gated, nullptr,
+33 -10
View File
@@ -6080,16 +6080,22 @@ static void w4a8_multi_user_exact(const Plugin& pl) {
* gated row shares) and activation. Codes, scales and the bf16 product must be identical, and the * gated row shares) and activation. Codes, scales and the bf16 product must be identical, and the
* codes mostly non-zero (zeroed buffers compare equal). Run twice: with `out_bf16`, and without it, * codes mostly non-zero (zeroed buffers compare equal). Run twice: with `out_bf16`, and without it,
* where the codes and scales must still match. */ * where the codes and scales must still match. */
static void mxfp4a8_gated_case(const Plugin& pl, int64_t M, int64_t N, int64_t K, bool ablk) { static void mxfp4a8_gated_case(const Plugin& pl, int64_t M, int64_t N, int64_t K, bool ablk,
bool gelu = false) {
const char* row = "gemm_mxfp4a8_gated_gemv"; const char* row = "gemm_mxfp4a8_gated_gemv";
if (!sel(row)) return; if (!sel(row)) return;
char nm[160]; char nm[192];
std::snprintf(nm, sizeof nm, "%s byte-identical to gemv + gated_quant_fp8, M=%lld N=%lld K=%lld%s", std::snprintf(nm, sizeof nm, "%s byte-identical to gemv + %s, M=%lld N=%lld K=%lld%s", row,
row, (long long)M, (long long)N, (long long)K, ablk ? " ascale=row x 128" : ""); gelu ? "gelu_mul + quant_act_fp8" : "gated_quant_fp8", (long long)M, (long long)N,
(long long)K, ablk ? " ascale=row x 128" : "");
const RadKernelInfo* rg = find(pl, row); const RadKernelInfo* rg = find(pl, row);
const RadKernelInfo* rv = find(pl, "gemm_mxfp4a8_gemv"); const RadKernelInfo* rv = find(pl, "gemm_mxfp4a8_gemv");
const RadKernelInfo* rq = find(pl, "gated_quant_fp8"); const RadKernelInfo* rq = find(pl, gelu ? "quant_act_fp8" : "gated_quant_fp8");
if (!rg || !rv || !rq || !rg->layout || !rg->relayout) { ok(nm, false, "row or hooks absent"); return; } const RadKernelInfo* rm = find(pl, "gelu_mul_bf16");
if (!rg || !rv || !rq || (gelu && !rm) || !rg->layout || !rg->relayout) {
ok(nm, false, "row or hooks absent");
return;
}
Planes p; Planes p;
p.enc = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32); p.enc = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32);
@@ -6145,15 +6151,23 @@ static void mxfp4a8_gated_case(const Plugin& pl, int64_t M, int64_t N, int64_t K
absent(none); absent(none);
int rc = launch_row(rv, { ta, tas, tw, te, T(d_y, RAD_BF16, { M, N }), none, tr }, int rc = launch_row(rv, { ta, tas, tw, te, T(d_y, RAD_BF16, { M, N }), none, tr },
{ PI("M", M), PI("N", N), PI("K", K), PI("group", 32), PS("dtype", "mxfp4a8") }); { PI("M", M), PI("N", N), PI("K", K), PI("group", 32), PS("dtype", "mxfp4a8") });
if (rc == RAD_OK) if (rc == RAD_OK && !gelu)
rc = launch_row(rq, { T(d_y, RAD_BF16, { M, N }), T(d_q[0], RAD_F8E4M3, { M, n }), rc = launch_row(rq, { T(d_y, RAD_BF16, { M, N }), T(d_q[0], RAD_F8E4M3, { M, n }),
T(d_z[0], RAD_F32, { M, gkb }), T(d_b[0], RAD_BF16, { M, n }) }, T(d_z[0], RAD_F32, { M, gkb }), T(d_b[0], RAD_BF16, { M, n }) },
{ PI("M", M), PI("n", n), PI("group", 128), PS("dtype", "bf16"), PS("act", "silu") }); { PI("M", M), PI("n", n), PI("group", 128), PS("dtype", "bf16"), PS("act", "silu") });
if (rc != RAD_OK) { ok(nm, false, "the pair: rc=" + std::to_string(rc)); return; } if (rc == RAD_OK && gelu) {
rc = launch_row(rm, { T(d_y, RAD_BF16, { M, N }), T(d_b[0], RAD_BF16, { M, n }) },
{ PI("M", M), PI("n", n), PS("dtype", "bf16") });
if (rc == RAD_OK)
rc = launch_row(rq, { T(d_b[0], RAD_BF16, { M, n }), T(d_q[0], RAD_F8E4M3, { M, n }),
T(d_z[0], RAD_F32, { M, gkb }) },
{ PI("M", M), PI("n", n), PI("group", 128), PS("dtype", "bf16") });
}
if (rc != RAD_OK) { ok(nm, false, "the chain: rc=" + std::to_string(rc)); return; }
/* The fold, with and without the bf16 product. */ /* The fold, with and without the bf16 product (the GeGLU form refuses it, asserted). */
const std::vector<RadParam> gp = { PI("M", M), PI("N", N), PI("K", K), PI("group", 32), const std::vector<RadParam> gp = { PI("M", M), PI("N", N), PI("K", K), PI("group", 32),
PS("dtype", "mxfp4a8"), PS("act", "silu") }; PS("dtype", "mxfp4a8"), PS("act", gelu ? "gelu_tanh" : "silu") };
for (int v = 1; v <= 2; ++v) { for (int v = 1; v <= 2; ++v) {
st_memset(d_q[v], 0, (size_t)(M * n)); st_memset(d_q[v], 0, (size_t)(M * n));
st_memset(d_z[v], 0, (size_t)(M * gkb * 4)); st_memset(d_z[v], 0, (size_t)(M * gkb * 4));
@@ -6161,6 +6175,12 @@ static void mxfp4a8_gated_case(const Plugin& pl, int64_t M, int64_t N, int64_t K
st_memset(d_b[1], 0, (size_t)(M * n * 2)); st_memset(d_b[1], 0, (size_t)(M * n * 2));
rc = launch_row(rg, { ta, tas, tw, te, tr, T(d_q[1], RAD_F8E4M3, { M, n }), rc = launch_row(rg, { ta, tas, tw, te, tr, T(d_q[1], RAD_F8E4M3, { M, n }),
T(d_z[1], RAD_F32, { M, gkb }), T(d_b[1], RAD_BF16, { M, n }) }, gp); T(d_z[1], RAD_F32, { M, gkb }), T(d_b[1], RAD_BF16, { M, n }) }, gp);
if (gelu) {
if (rc == RAD_OK) { ok(nm, false, "the GeGLU fold served out_bf16"); return; }
rc = launch_row(rg, { ta, tas, tw, te, tr, T(d_q[1], RAD_F8E4M3, { M, n }),
T(d_z[1], RAD_F32, { M, gkb }), none }, gp);
st_memcpy(d_b[1], d_b[0], (size_t)(M * n * 2), hipMemcpyDeviceToDevice);
}
if (rc == RAD_OK) if (rc == RAD_OK)
rc = launch_row(rg, { ta, tas, tw, te, tr, T(d_q[2], RAD_F8E4M3, { M, n }), rc = launch_row(rg, { ta, tas, tw, te, tr, T(d_q[2], RAD_F8E4M3, { M, n }),
T(d_z[2], RAD_F32, { M, gkb }), none }, gp); T(d_z[2], RAD_F32, { M, gkb }), none }, gp);
@@ -27356,6 +27376,9 @@ static void device_phase(const Plugin& pl) {
mxfp4a8_gated_case(pl, 8, 21504, 5376, true); mxfp4a8_gated_case(pl, 8, 21504, 5376, true);
mxfp4a8_gated_case(pl, 3, 256, 128, true); mxfp4a8_gated_case(pl, 3, 256, 128, true);
mxfp4a8_gated_case(pl, 5, 1024, 640, false); mxfp4a8_gated_case(pl, 5, 1024, 640, false);
for (const int64_t m : { 1, 4, 8 }) mxfp4a8_gated_case(pl, m, 21504, 5376, true, true);
mxfp4a8_gated_case(pl, 3, 256, 128, true, true);
mxfp4a8_gated_case(pl, 2, 1024, 640, false, true);
/* Parts (ParoQuant): each weight part against its own activation copy, with bounds off any /* Parts (ParoQuant): each weight part against its own activation copy, with bounds off any
* kernel's tile, two and three parts, at every band. */ * kernel's tile, two and three parts, at every band. */
mxfp4a8_numeric(pl, "gemm_mxfp4a8_nt_m64", 7, 400, 640, 0, true, {}, 144); mxfp4a8_numeric(pl, "gemm_mxfp4a8_nt_m64", 7, 400, 640, 0, true, {}, 144);