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{};
rad_op op_norm = 0, op_act = 0, op_post = 0, op_ar = 0, op_add = 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). */
G3Tap tap_gu{}, tap_dn{};
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_NOWEIGHTS),
{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)
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),
@@ -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,
bool ar_fused) const {
const int64_t T = batch->n_tok;
const bool fold = op_fold && T <= fold_rows;
if (entry_done) {
ln_gu.gemm(c, w.gu, T);
if (!fold) ln_gu.gemm(c, w.gu, T);
} else {
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);
}
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);
ln_down.gemm(c, w.h, T);
} else {