From 78925248019ab60b6d264f665c07b039ab389a6a Mon Sep 17 00:00:00 2001 From: keptn42 Date: Wed, 28 Oct 2026 18:06:02 +0100 Subject: [PATCH] gemma4_bf16: fused junctions and the prefill overlap on the i8a8, a4 and w5 grids The residual junctions take the `_i8g` twins where the linear after them reads quant_act_i8g's pair, so the prefill overlap runs on these trunks too. An a4 or w5 linear reads an f16 copy of its input up to 64 rows: in such a step its junction also writes the bf16 norm output into the linear's input row, where the unfused norm would, and the cast and w4a16_asym / w5a16 run as before; the feed-forward keeps gelu_mul there. At four ranks the all-reduce is issued apart. arch_test: the 31B on the three grids at one, two and four ranks -- the junctions, the decode band's bf16 row, the same GEMMs and casts as the unfused step, the slices against the whole step, codes and scales of every int8 pair on the same rows -- and the E4B form. --- arch/gemma4_bf16/gemma4_bf16.cpp | 132 ++++++++++++------ docs/ARCHITECTURES.md | 10 ++ tests/arch_test.cpp | 231 ++++++++++++++++++++++++++++++- 3 files changed, 329 insertions(+), 44 deletions(-) diff --git a/arch/gemma4_bf16/gemma4_bf16.cpp b/arch/gemma4_bf16/gemma4_bf16.cpp index 0b7e9413..9a84cc0f 100644 --- a/arch/gemma4_bf16/gemma4_bf16.cpp +++ b/arch/gemma4_bf16/gemma4_bf16.cpp @@ -42,7 +42,13 @@ using namespace rad::arch; * v read one quantised pair instead of three. `qkv_norm_rope` does the same for the attention * prologue's five ops, `gelu_mul_quant_i8` for down's input, and at two ranks * `ar_rmsnorm_quant_i8` also folds in the all-reduce before each junction. A kernel set without an - * op keeps that op's chain. */ + * op keeps that op's chain. + * + * The linears on quant_act_i8g's grid (i8a8, a4 and its five-bit form) take the `_i8g` twins of the + * three: the same junctions with a scale per (row, 128) instead of per row. An a4 linear reads no + * int8 pair up to its decode band (G4Linear::kA4Split rows) but an f16 copy of the bf16 norm output + * (`cast`, then w4a16_asym or w5a16), so there its junction also writes that norm output where the + * unfused norm would, into the linear's input row, and the cast and the skinny GEMM run as before. */ /* layer_types is optional. When present it must name every layer "full_attention" or * "sliding_attention"; when absent, layer l is global when (l + 1) % interval == 0. */ @@ -123,8 +129,8 @@ struct G4Linear { /* true: i8a8 as arch/gemma3's G3Linear: int8 codes with a bf16 scale per [1 x 128] (libquant * `codes=i8 group=128 scale=bf16`) over quant_act_i8g, an activation scale per (row, 128). * Gemma 4 E4B's activations carry a few channels 40-200 times the row's median (the down - * projection's input most), and one int8 scale a row spends its codes on them. There is no - * fused norm/quantiser at this grid, so the junctions stay off for a model that uses it. */ + * projection's input most), and one int8 scale a row spends its codes on them. Its junctions + * are the `_i8g` twins (see "Residual junctions" above). */ bool i8g = false; /* true: asymmetric int4 as arch/gemma3's G3Linear: u4 codes, a u8 zero point and an f16 scale * per [1 x 128] (libquant `codes=u4 zero=u8 group=128 scale=f16`), gemm_nt_q at w4a8i8_asym @@ -135,6 +141,11 @@ struct G4Linear { * w5a8i8 and w5a16 where a4 reads w4a8i8_asym and w4a16_asym, over the same activations. */ bool w5 = false; bool grouped() const { return i8g || a4; } + /* reads an int8 pair a junction can write, at either grid */ + bool pair() const { return quant || grouped(); } + /* the pair's scales a row: one, or one per 128 columns */ + int64_t scale_w() const { return grouped() ? K / RAD_FP8_BLOCK : 1; } + rad_buf in = 0; /* the bf16 input row `a` given at declare */ /* a4 at decode heights, as LinearFP8's w4 (rad_fp8.h): up to kA4Split rows the bf16 input is * cast to f16 (`a16`) for w4a16_asym's skinny kernel; w4a8i8_asym has only a prefill kernel, * which at one row does 16 times the matrix work and reads its weight a quarter as fast. */ @@ -154,7 +165,8 @@ struct G4Linear { std::initializer_list row_parts = {}, const G4Linear* in_from = nullptr); void step(RadCtx* c, rad_buf a, rad_buf y, int64_t T) const; - /* the GEMM alone, for a caller that wrote the int8 pair itself (a fused norm + quantiser) */ + /* the GEMM alone, for a caller that wrote the int8 pair itself (a fused norm + quantiser); an + * a4 linear's decode band reads the bf16 input row instead, which that caller then wrote */ void gemm(RadCtx* c, rad_buf y, int64_t T) const; /* The same two over rows [r0, r0 + n) of the step: a slice of the prefill overlap. The * quantiser and the GEMM are a row at a time above their bands, so a slice's rows are the @@ -191,7 +203,7 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char rad_buf a, rad_buf y, std::initializer_list row_parts, const G4Linear* in_from) { - N = n_out; K = n_in; + N = n_out; K = n_in; in = a; if (N <= 0 || K <= 0) return RAD_E_INVAL; const char* wn = nm.f("%s.weight", base); if (src.size() == 1) RAD_ARCH_TRY(map_copy(b, wn, *src.begin())); @@ -362,6 +374,13 @@ inline void G4Linear::step(RadCtx* c, rad_buf a, rad_buf y, int64_t T) const { } inline void G4Linear::gemm(RadCtx* c, rad_buf y, int64_t T) const { + if (a4 && T <= a4_split) { + /* the decode band reads no pair: the junction wrote the norm output into `in` instead */ + RAD_ISSUE_N(c, op_a16, T, brows(in, T), brows(a16, T)); + RAD_ISSUE_N(c, op_dec, T, brows(a16, T), RAD_NONE, RAD_W(w), RAD_W(ws), brows(y, T), + RAD_NONE, RAD_NONE); + return; + } RAD_ISSUE_N(c, op, T, brows(q8, T), brows(s8, T), RAD_W(w), RAD_W(ws), brows(y, T), RAD_NONE, RAD_NONE); } @@ -377,20 +396,19 @@ inline void G4Linear::step_rows(RadCtx* c, rad_buf a, rad_buf y, int64_t r0, int RAD_ISSUE_N(c, op, n, brow_slice(a, r0, n, K), RAD_W(w), brow_slice(y, r0, n, N)); return; } - const int64_t sw = grouped() ? K / RAD_FP8_BLOCK : 1; /* the scale plane's row width */ if (!shared_in && grouped()) RAD_ISSUE_N(c, op_q, n, brow_slice(a, r0, n, K), brow_slice(q8, r0, n, K), - brow_slice(s8, r0, n, sw)); + brow_slice(s8, r0, n, scale_w())); else if (!shared_in) RAD_ISSUE_N(c, op_q, n, brow_slice(a, r0, n, K), brow_slice(q8, r0, n, K), brow_slice(s8, r0, n, 1), RAD_NONE); gemm_rows(c, y, r0, n); } +/* A slice of the prefill overlap only, which is above every decode band (kOverlapRows). */ inline void G4Linear::gemm_rows(RadCtx* c, rad_buf y, int64_t r0, int64_t n) const { - const int64_t sw = grouped() ? K / RAD_FP8_BLOCK : 1; - RAD_ISSUE_N(c, op, n, brow_slice(q8, r0, n, K), brow_slice(s8, r0, n, sw), RAD_W(w), RAD_W(ws), - brow_slice(y, r0, n, N), RAD_NONE, RAD_NONE); + RAD_ISSUE_N(c, op, n, brow_slice(q8, r0, n, K), brow_slice(s8, r0, n, scale_w()), RAD_W(w), + RAD_W(ws), brow_slice(y, r0, n, N), RAD_NONE, RAD_NONE); } /* ---- one attention block, parameterised by class. */ @@ -447,9 +465,9 @@ struct Gemma4Attn { /* The linear whose int8 pair a junction writes, and whether every projection reads it. */ const G4Linear& ln_in() const { return fused ? ln_qkv : ln_q; } bool reads_pair() const { - if (shared) return ln_q.quant; - return fused ? ln_qkv.quant - : ln_q.quant && ln_k.shared_in && (!cls.has_v || ln_v.shared_in); + if (shared) return ln_q.pair(); + return fused ? ln_qkv.pair() + : ln_q.pair() && ln_k.shared_in && (!cls.has_v || ln_v.shared_in); } /* `kv_src` < 0: the layer keeps its own cache; else the layer whose cache it reads. */ @@ -1064,8 +1082,11 @@ struct Gemma4Mlp { rad_weight w_norm = 0, w_post = 0; /* the norms (always bf16) */ G4Linear ln_gu{}, ln_down{}; /* the GEMM weights (bf16 or w4a8) */ rad_op op_norm = 0, op_act = 0, op_post = 0, op_ar = 0, op_add = 0; - /* gelu_mul and down's quantiser as one, where it resolves (libr4d `gelu_mul_quant_i8`) */ - rad_op op_gq = 0; + /* gelu_mul and down's quantiser as one, where it resolves (libr4d `gelu_mul_quant_i8`, or its + * `_i8g` twin before a down_proj on quant_act_i8g's grid); not in an a4 down_proj's decode + * band, which reads bf16 (gq_lo: the most rows that band takes, else 0) */ + rad_op op_gq = 0; + int64_t gq_lo = 0; /* the next layer's q|k|v read into the cache beside this block's all-reduce, or 0 (see * "Prefetch beside the reductions") */ rad_op op_pf = 0; @@ -1084,9 +1105,9 @@ struct Gemma4Mlp { void Gemma4Mlp::arm(RadCtx* c, int64_t r0, int64_t n) const { const int64_t F = g.n_ff; ln_gu.gemm_rows(c, w.gu, r0, n); - if (op_gq) { + if (op_gq && n > gq_lo) { RAD_ISSUE_N(c, op_gq, n, brow_slice(w.gu, r0, n, 2 * F), brow_slice(ln_down.q8, r0, n, F), - brow_slice(ln_down.s8, r0, n, 1), RAD_NONE); + brow_slice(ln_down.s8, r0, n, ln_down.scale_w()), RAD_NONE); ln_down.gemm_rows(c, w.h, r0, n); } else { RAD_ISSUE_N(c, op_act, n, brow_slice(w.gu, r0, n, 2 * F), brow_slice(w.ffn, r0, n, F)); @@ -1134,11 +1155,15 @@ int Gemma4Mlp::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}, {w.ffn}); - if (ln_down.quant && !ln_down.shared_in && rad_op_declared(b, "gelu_mul_quant_i8")) - op_gq = rw(b, RAD_OP(b, "gelu_mul_quant_i8", + const char* gq = ln_down.quant ? "gelu_mul_quant_i8" + : ln_down.grouped() ? "gelu_mul_quant_i8g" : nullptr; + if (gq && !ln_down.shared_in && rad_op_declared(b, gq)) { + op_gq = rw(b, RAD_OP(b, gq, 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}); + gq_lo = ln_down.a4 ? ln_down.a4_split : 0; + } /* down_proj is column-sharded: reduce first, then the post-norm. */ if (g.world > 1) op_ar = rw(b, RAD_OP(b, "all_reduce", @@ -1168,7 +1193,7 @@ void Gemma4Mlp::step(RadCtx* c, const RadBatch* batch, bool entry_done, bool tai 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 (op_gq) { + if (op_gq && T > gq_lo) { 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 { @@ -1184,8 +1209,11 @@ void Gemma4Mlp::step(RadCtx* c, const RadBatch* batch, bool entry_done, bool tai } /* ---- one residual junction as one op: [post-norm of the branch `x`, the add into `res`, the - * layer scalar,] the next block's norm and its int8 quantiser (libr4d `rmsnorm_quant_i8`). With - * `res` null it is the plain norm of `x`, the first block's entry. */ + * layer scalar,] the next block's norm and its int8 quantiser (libr4d `rmsnorm_quant_i8`, or + * `rmsnorm_quant_i8g` before a linear on quant_act_i8g's grid). With `res` null it is the plain + * norm of `x`, the first block's entry. Before an a4 linear the op also writes the bf16 norm output + * into the linear's input row (`out_bf16`, `y`) in a step its decode band takes; that row is `x` + * itself past the first block, as the unfused chain normalises into `h`. */ /* The four-rank fold's band. libr4d's four-rank row serves it in three forms, all byte-identical * to all_reduce + rmsnorm_quant_i8. Up to 4 rows it is the exact one-shot with the junction as its * epilogue: three times the message per rank and one fenced handshake a row, which wins at one row @@ -1215,14 +1243,26 @@ struct G4Junction { int64_t sp_hi = 0; /* the most rows op_sp serves, from kArSpMinRows */ rad_buf x = 0, res = 0, q8 = 0, s8 = 0; rad_weight w = 0, wp = 0, sc = 0; + int64_t sw = 1; /* the pair's scales a row (G4Linear::scale_w) */ + rad_buf y = 0; /* an a4 linear's input row, written in its decode band, or 0 */ + int64_t y_hi = 0; /* ...that band's most rows */ /* `full_res`: something besides the next junction reads the residual stream after this one, * so the split form must leave it on every row. */ int declare(RadBuilder* b, const Geom& g, rad_buf x_, rad_buf res_, rad_weight w_, rad_weight wp_, rad_weight sc_, const G4Linear& into, bool full_res = false) { x = x_; res = res_; w = w_; wp = wp_; sc = sc_; q8 = into.q8; s8 = into.s8; - if (!into.quant || into.shared_in || !q8 || !s8 || !w || (sc && !wp) || ((wp || sc) && !res)) + sw = into.scale_w(); + if (!into.pair() || into.shared_in || !q8 || !s8 || !w || (sc && !wp) || ((wp || sc) && !res)) return RAD_E_INVAL; + if (into.a4) { + y = into.in; + y_hi = into.a4_split; + if (!y) return RAD_E_INVAL; + } + const bool grp = into.grouped(); + const char* rq = grp ? "rmsnorm_quant_i8g" : "rmsnorm_quant_i8"; + const char* arq = grp ? "ar_rmsnorm_quant_i8g" : "ar_rmsnorm_quant_i8"; const RadParam p[] = { RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), RAD_F64("eps", g.eps), RAD_STR("dtype", g.dtype), RAD_F64("wadd", g.wadd) }; @@ -1230,35 +1270,40 @@ struct G4Junction { const int nw = sc ? 3 : (wp ? 2 : 1); /* Not declared by any loaded library (CPU-only) or not resolved: the caller keeps the * unfused chain. */ - if (rad_op_declared(b, "rmsnorm_quant_i8")) - op = rad_decl_op(b, "rmsnorm_quant_i8", p, (int)(sizeof p / sizeof p[0]), ws, nw); + if (rad_op_declared(b, rq)) + op = rad_decl_op(b, rq, p, (int)(sizeof p / sizeof p[0]), ws, nw); if (!op) return RAD_OK; const rad_buf rd[2] = { x, res }; - const rad_buf wr[3] = { q8, s8, res }; + rad_buf wr[4] = { q8, s8, 0, 0 }; + int nwr = 2; + if (res) wr[nwr++] = res; + if (y) wr[nwr++] = y; rad_op_reads (b, op, rd, res ? 2 : 1); - rad_op_writes(b, op, wr, res ? 3 : 2); + rad_op_writes(b, op, wr, nwr); /* With the all-reduce in front, at two or four ranks on the exact wire: `x` is then a * rank's partial sum of a column-sharded linear. With the lossy wire allowed, the * standalone all-reduce is kept, since it may choose that wire. At four ranks the fold - * serves steps of up to kAr4FoldRows rows. */ + * serves steps of up to kAr4FoldRows rows; the i8a8 grid has no four-rank fold, so there the + * standalone all-reduce stays. */ const bool four = g.world == 4; const int64_t hi = four ? std::min(kAr4FoldRows, g.max_tok) : g.max_tok; - if (res && g.wire_exact && (g.world == 2 || four) && hi >= 1 && - rad_op_declared(b, "ar_rmsnorm_quant_i8")) { + if (res && g.wire_exact && (g.world == 2 || (four && !grp)) && hi >= 1 && + rad_op_declared(b, arq)) { const RadParam pa[] = { RAD_RANGE("M", 1, hi), RAD_INT("n", g.n_embd), RAD_F64("eps", g.eps), RAD_INT("world_size", g.world), RAD_STR("dtype", g.dtype), RAD_F64("wadd", g.wadd) }; - op_ar = rad_decl_op(b, "ar_rmsnorm_quant_i8", pa, (int)(sizeof pa / sizeof pa[0]), ws, nw); + op_ar = rad_decl_op(b, arq, pa, (int)(sizeof pa / sizeof pa[0]), ws, nw); if (op_ar) { - const rad_buf wa[4] = { x, q8, s8, res }; + const rad_buf wa[5] = { x, q8, s8, res, y }; rad_op_reads (b, op_ar, rd, 2); - rad_op_writes(b, op_ar, wa, 4); + rad_op_writes(b, op_ar, wa, y ? 5 : 4); ar_rows = hi; } /* The split form from kArSpMinRows to kArSpMaxRows rows. It needs the post-norm, as - * every trunk junction past the first has; `x` is declared written, as the fold's. */ + * every trunk junction past the first has; `x` is declared written, as the fold's. + * Not at quant_act_i8g's grid, whose fold has no split form. */ const int64_t top = std::min(g.max_tok, kArSpMaxRows); - if (op_ar && four && wp && top >= kArSpMinRows) { + if (op_ar && four && wp && !grp && top >= kArSpMinRows) { const RadParam ps[] = { RAD_RANGE("M", 1, top), RAD_INT("n", g.n_embd), RAD_F64("eps", g.eps), RAD_INT("world_size", g.world), RAD_STR("dtype", g.dtype), RAD_F64("wadd", g.wadd), @@ -1280,14 +1325,14 @@ struct G4Junction { void step(RadCtx* c, int64_t T, bool with_ar = false) const { const rad_op o = !with_ar ? op : (op_sp && T >= kArSpMinRows && T <= sp_hi) ? op_sp : op_ar; RAD_ISSUE_N(c, o, T, brows(x, T), res ? brows(res, T) : RAD_NONE, RAD_W(w), - brows(q8, T), brows(s8, T), RAD_NONE, + brows(q8, T), brows(s8, T), y && T <= y_hi ? brows(y, T) : RAD_NONE, wp ? RAD_W(wp) : RAD_NONE, sc ? RAD_W(sc) : RAD_NONE); } /* Rows [r0, r0 + n), without the all-reduce: a slice of the prefill overlap, whose reduction - * rode the second lane. */ + * rode the second lane. A slice is above every decode band, so no bf16 row. */ void step_rows(RadCtx* c, int64_t E, int64_t r0, int64_t n) const { RAD_ISSUE_N(c, op, n, brow_slice(x, r0, n, E), res ? brow_slice(res, r0, n, E) : RAD_NONE, - RAD_W(w), brow_slice(q8, r0, n, E), brow_slice(s8, r0, n, 1), RAD_NONE, + RAD_W(w), brow_slice(q8, r0, n, E), brow_slice(s8, r0, n, sw), RAD_NONE, wp ? RAD_W(wp) : RAD_NONE, sc ? RAD_W(sc) : RAD_NONE); } }; @@ -1757,6 +1802,9 @@ void Gemma4Head::step(RadCtx* c, const RadBatch* batch) const { * sum-of-squares partition. And it is large enough that a slice's reduction is worth a second * lane. */ static constexpr int64_t kOverlapRows = 512; +/* ...and above an a4 linear's decode band, so a slice always reads the int8 pair a junction's + * slice writes (G4Linear::gemm_rows, G4Junction::step_rows). */ +static_assert(kOverlapRows > G4Linear::kA4Split, "a prefill slice inside an a4 decode band"); struct Layer { bool global = false; @@ -2543,13 +2591,13 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c /* ---- the junctions, declared once every layer exists (j_ma reads the next layer's gain). * Used only when all resolved and every linear a junction feeds reads its int8 pair (q, k and - * v through q's, and gate_up): a junction writes that pair, not the bf16 norm output, so a bf16 - * k or v beside an int8 q would read a stale buffer. Otherwise the blocks keep their own norms, - * quantisers, post-norms and adds. */ + * v through q's, and gate_up), at either grid: a junction writes that pair, not the bf16 norm + * output (an a4 linear's decode band aside), so a bf16 k or v beside an int8 q would read a + * stale buffer. Otherwise the blocks keep their own norms, quantisers, post-norms and adds. */ if (m.n_main > 0) { bool ok = true; for (const Layer& lay : m.layers) - ok = ok && lay.attn.reads_pair() && lay.mlp.ln_gu.quant; + ok = ok && lay.attn.reads_pair() && lay.mlp.ln_gu.pair(); if (ok) { RAD_ARCH_TRY(m.j0.declare(b, g, m.b_x, 0, m.layers[0].attn.w_norm, 0, 0, m.layers[0].attn.ln_in())); diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index d25bef52..9a26457c 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -1265,6 +1265,16 @@ step at tp 4 then has two standalone reductions (the embedding's and the last fe it had 121. `r4d_selftest --case ar_rmsnorm_quant_i8_4rank` checks every form on four cards, and the startup note says which forms a deployment takes. +**Junctions on the int8 grids with a scale per (row, 128).** A trunk at i8a8, or at asymmetric int4 +or int5 (`codes=u4|u5 zero=u8 group=128 scale=f16`), reads `quant_act_i8g`'s pair. Its junctions +are libr4d's `_i8g` twins of the three above (`rmsnorm_quant_i8g`, `gelu_mul_quant_i8g`, and at two +ranks `ar_rmsnorm_quant_i8g`; at four the all-reduce in front of each is issued apart), so the +prefill overlap runs on these trunks too. An int4 or int5 linear reads an f16 copy of its input up +to 64 rows (`w4a16_asym`, `w5a16`): in such a step each junction also writes the bf16 norm output +into `h`, where the unfused norm would, the linear casts it as before, and the feed-forward keeps +`gelu_mul`. `r4d_selftest --case rmsnorm_quant_i8g` (and `gelu_mul_quant_i8g`, +`ar_rmsnorm_quant_i8g`) holds each to its chain. + **Weight reads beside the all-reduces.** At more than one rank, on decode and verify steps of up to 16 rows that carry no prompt rows, the 31B reads the next GEMM's leading weight bytes into the on-die cache (libr4d `prefetch`) while an all-reduce leaves the card's memory idle: gate_up's first diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index f338ed96..96fcc005 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -4856,7 +4856,7 @@ TEST(the_gemma4_e4b_form_is_refused_where_it_cannot_run) { * (scale, zero) view, up to 64 rows the input cast to f16 for w4a16_asym / w5a16, above that * quant_act_i8g's int8 codes for w4a8i8_asym / w5a8i8. q|k|v stay one GEMM; o_proj and down_proj * are cut along K, in whole scale groups. A decode step issues only the f16 band, a step of 200 - * rows only the int8 one. */ + * rows only the int8 one. The residual junctions are fused (the next case covers them). */ TEST(the_gemma4_31b_form_reads_asymmetric_int4_and_int5_linears) { static const int32_t tall_out[] = { 99, 199 }; for (const uint32_t cdt : { (uint32_t)RAD_U4, (uint32_t)RAD_U5 }) { @@ -4872,7 +4872,7 @@ TEST(the_gemma4_31b_form_reads_asymmetric_int4_and_int5_linears) { RadModelMeta meta = gemma4_meta(); RadBuildCtx ctx = build_ctx(rank, world); CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx)); - CHECK(!gemma4_bf16::g_model[rank].fuse); + CHECK(gemma4_bf16::g_model[rank].fuse); int n_dec = 0, n_pre = 0, other = 0; for (const RecOp& o : b.ops) { if (o.op != "gemm_nt_q") continue; @@ -7045,6 +7045,233 @@ TEST(a_gemma4_31b_prefill_step_runs_as_two_slices_with_its_reductions_on_the_sec for (const auto& i : sc.issues) CHECK_EQ(i.lane, 0); } +/* The 31B on the grids that read quant_act_i8g's pair -- int8 a 128 (i8a8), a4 and its five-bit + * form -- fuses its residual junctions as w4a8 does, through the `_i8g` twins: one + * rmsnorm_quant_i8g a junction (2L), one gelu_mul_quant_i8g a feed-forward, at two ranks the + * all-reduce folded in (ar_rmsnorm_quant_i8g on all but the embedding's and the last + * feed-forward's) and a prefill of 1024 rows or more in two slices. An a4 or w5 linear reads bf16 + * up to 64 rows: in such a step every junction also writes the norm output into `h`, the row each + * linear's cast reads, and the feed-forward keeps gelu_mul and down's own cast; above that no + * junction writes `h` and no linear casts. Either way the fused step runs the unfused step's GEMMs + * and casts over the same rows. Without the twins (a CPU-only engine) the chains are declared and no + * undeclared op is asked for. */ +TEST(the_gemma4_31b_fuses_its_junctions_on_the_grouped_int8_grids) { + RadEncoding u4 = rad_enc_affine(RAD_U4, RAD_F16, 1, 128); + rad_enc_add_plane(&u4, "zero", RAD_U8, 1, 128); + RadEncoding u5 = rad_enc_affine(RAD_U5, RAD_F16, 1, 128); + rad_enc_add_plane(&u5, "zero", RAD_U8, 1, 128); + struct Fmt { const char* name; RadEncoding e; bool band; }; + const Fmt fmts[] = { { "i8g", rad_enc_affine(RAD_I8, RAD_BF16, 1, 128), false }, + { "a4", u4, true }, { "w5", u5, true } }; + const int64_t L = 12; + const char* const twins[] = { "rmsnorm_quant_i8g", "gelu_mul_quant_i8g", "ar_rmsnorm_quant_i8g" }; + for (const Fmt& f : fmts) + for (int world : { 1, 2 }) { + auto model = [&](RadBuilder& b, bool fused, bool lossy) { + b.encs = { { "attn_qkv.", f.e }, { "attn_o.", f.e }, { "ffn_gate_up.", f.e }, + { "ffn_down.", f.e } }; + if (!fused) for (const char* op : twins) b.undeclared.insert(op); + RadModelMeta meta = gemma4_meta(); + RadBuildCtx ctx = build_ctx(0, world); + ctx.max_tok = 4096; + if (lossy) ctx.tp_wire_lossy = 1; + return gemma4_bf16::declare(&b, &meta, &ctx); + }; + { + RadBuilder d; + CHECK_OK(model(d, true, false)); + const gemma4_bf16::Model& m = gemma4_bf16::g_model[0]; + CHECK(m.fuse); + CHECK_EQ(m.fuse_ar, world == 2); + CHECK_EQ(m.overlap, world == 2); + if (world == 2) + CHECK(noted(d, "prefill overlap: a step of 1024 rows or more runs as two slices")); + CHECK_EQ(count_op(d, "rmsnorm_quant_i8g"), 2 * (int)L); + CHECK_EQ(count_op(d, "gelu_mul_quant_i8g"), (int)L); + CHECK_EQ(count_op(d, "ar_rmsnorm_quant_i8g"), world == 2 ? 2 * (int)L - 1 : 0); + for (const char* op : { "rmsnorm_quant_i8", "gelu_mul_quant_i8", "ar_rmsnorm_quant_i8" }) + CHECK_EQ(count_op(d, op), 0); + /* a junction declares `h` written only where a linear after it has a decode band; + * the all-reduce form writes `h` (its `x`, the reduced sum) either way, and the bf16 + * row besides in the band */ + const rad_buf h = (rad_buf)buf_named(d, "h"); + for (const RecOp& o : d.ops) { + if (o.op == "rmsnorm_quant_i8g") + CHECK_EQ(std::find(o.writes.begin(), o.writes.end(), h) != o.writes.end(), f.band); + if (o.op == "ar_rmsnorm_quant_i8g") { + CHECK_EQ(o.writes.size(), (size_t)(f.band ? 5 : 4)); + CHECK(param(&o, "sp") == nullptr); + } + } + } + { + RadBuilder u; + CHECK_OK(model(u, false, false)); + CHECK(!gemma4_bf16::g_model[0].fuse); + CHECK(u.asked_undeclared.empty()); + } + + /* step() runs the model declare last made, so each step below follows its own */ + auto linears = [](const RadBuilder& bb, const RadCtx& cc, int64_t TT) { + std::map w; + for (const auto& i : cc.issues) { + const RecOp* o = bb.op_of(i.op); + if (!o) continue; + if (o->op == "gemm_nt_q") w["gemm_nt_q " + pstr(o, "dtype")] += i.n < 0 ? TT : i.n; + if (o->op == "cast" && pstr(o, "to") == "f16") w["cast f16"] += i.n < 0 ? TT : i.n; + } + return w; + }; + for (const int64_t T : { (int64_t)1, (int64_t)16, (int64_t)64, (int64_t)65, (int64_t)200, + (int64_t)4096 }) { + const int32_t out[1] = { (int32_t)(T - 1) }; + const bool band = f.band && T <= 64; + const bool sliced = world == 2 && T >= 1024; + RadBuilder b; + CHECK_OK(model(b, true, false)); + RadBatch batch = one_prompt(T, out); + RadCtx c; c.b = &b; c.rank = 0; c.world = world; c.batch = &batch; + gemma4_bf16::step(&c, &batch); + CHECK(c.step_fail.empty()); + CHECK_LINT(b, &c); + CHECK_EQ(races(b, c), 0); + const std::vector ops = issued_ops(b, c); + const rad_buf h = (rad_buf)buf_named(b, "h"); + const int k = sliced ? 2 : 1; /* issues a row op takes: one a slice */ + const int n_ar = count_issued(ops, "ar_rmsnorm_quant_i8g"); + CHECK_EQ(count_issued(ops, "rmsnorm_quant_i8g") + n_ar, 1 + (k * (2 * (int)L - 1))); + CHECK_EQ(n_ar, world == 2 && !sliced ? 2 * (int)L - 1 : 0); + CHECK_EQ(count_issued(ops, "gelu_mul_quant_i8g"), band ? 0 : k * (int)L); + CHECK_EQ(count_issued(ops, "gelu_mul"), band ? (int)L : 0); + /* o_proj's own quantiser only: every other linear reads a junction's pair */ + CHECK_EQ(count_issued(ops, "quant_act_i8g"), band ? 0 : k * (int)L); + int y_written = 0; + for (size_t i = 0; i < c.issues.size(); ++i) { + if (ops[i] != "rmsnorm_quant_i8g" && ops[i] != "ar_rmsnorm_quant_i8g") continue; + REQUIRE(c.issues[i].opd.size() > 5); + const RadOperand& y = c.issues[i].opd[5]; + if (y.kind != RAD_OPK_BUF) continue; + ++y_written; + CHECK_EQ((rad_buf)y.handle, h); + } + CHECK_EQ(y_written, band ? 2 * (int)L : 0); + /* Every int8 pair a step writes or reads, a slice's included, is the same rows of codes + * and of scales: a scale row is a codes row's 128th. */ + int pairs = 0; + for (size_t i = 0; i < c.issues.size(); ++i) { + const RecOp* o = b.op_of(c.issues[i].op); + if (!o) continue; + int iq = -1; + if (o->op == "rmsnorm_quant_i8g" || o->op == "ar_rmsnorm_quant_i8g") iq = 3; + else if (o->op == "gelu_mul_quant_i8g" || o->op == "quant_act_i8g") iq = 1; + else if (o->op == "gemm_nt_q" && pstr(o, "dtype") != "w4a16_asym" && + pstr(o, "dtype") != "w5a16") iq = 0; + if (iq < 0) continue; + REQUIRE(c.issues[i].opd.size() > (size_t)iq + 1); + const RadOperand& q = c.issues[i].opd[(size_t)iq]; + const RadOperand& s = c.issues[i].opd[(size_t)iq + 1]; + CHECK_EQ(s.offset * 128, q.offset); + CHECK_EQ(s.rows, q.rows); + ++pairs; + } + CHECK(pairs > 0); + int lane1 = 0; + for (const auto& i : c.issues) lane1 += i.lane == 1; + CHECK_EQ(lane1, sliced ? 4 * (int)L : 0); + const std::map fused_lin = linears(b, c, T); + const int64_t casts = fused_lin.count("cast f16") ? fused_lin.at("cast f16") : 0; + CHECK_EQ(casts, band ? 4 * L * T : 0); + + /* the same GEMMs and casts over the same rows as the unfused step */ + RadBuilder u; + CHECK_OK(model(u, false, false)); + RadBatch ub = one_prompt(T, out); + RadCtx uc; uc.b = &u; uc.rank = 0; uc.world = world; uc.batch = &ub; + gemma4_bf16::step(&uc, &ub); + CHECK(uc.step_fail.empty()); + CHECK_LINT(u, &uc); + CHECK(fused_lin == linears(u, uc, T)); + + /* the slices issue the rows the whole step issues (the lossy wire runs it whole) */ + if (sliced) { + std::map sl = rows_by_op(b, c, T); + RadBuilder w; + CHECK_OK(model(w, true, true)); + CHECK(!gemma4_bf16::g_model[0].overlap); + RadBatch wb = one_prompt(T, out); + RadCtx wc; wc.b = &w; wc.rank = 0; wc.world = world; wc.batch = &wb; + gemma4_bf16::step(&wc, &wb); + CHECK(wc.step_fail.empty()); + sl.erase("kv_len_trim"); + CHECK(sl == rows_by_op(w, wc, T)); + } + } + } + + /* At four ranks libr4d has no ar_rmsnorm_quant_i8g row: the junctions fuse, the all-reduce in + * front of each is issued apart, and the prefill still runs as two slices. */ + for (const Fmt& f : fmts) + for (const int64_t T : { (int64_t)1, (int64_t)4096 }) { + const int32_t out[1] = { (int32_t)(T - 1) }; + RadBuilder b; + b.refuse.insert("ar_rmsnorm_quant_i8g"); + b.encs = { { "attn_qkv.", f.e }, { "attn_o.", f.e }, { "ffn_gate_up.", f.e }, { "ffn_down.", f.e } }; + RadModelMeta meta = gemma4_meta(); + RadBuildCtx ctx = build_ctx(0, 4); + ctx.max_tok = 4096; + CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx)); + const gemma4_bf16::Model& m = gemma4_bf16::g_model[0]; + CHECK(m.fuse); + CHECK(!m.fuse_ar); + CHECK(m.overlap); + CHECK(noted(b, "residual junctions: the all-reduce in front of each issued apart")); + RadBatch batch = one_prompt(T, out); + RadCtx c; c.b = &b; c.rank = 0; c.world = 4; c.batch = &batch; + gemma4_bf16::step(&c, &batch); + CHECK(c.step_fail.empty()); + CHECK_LINT(b, &c); + CHECK_EQ(races(b, c), 0); + const std::vector ops = issued_ops(b, c); + CHECK_EQ(count_issued(ops, "ar_rmsnorm_quant_i8g"), 0); + CHECK_EQ(count_issued(ops, "rmsnorm_quant_i8g"), T > 1 ? 1 + 2 * (2 * 12 - 1) : 2 * 12); + CHECK_EQ(count_issued(ops, "all_reduce"), T > 1 ? 1 + 4 * 12 : 1 + 2 * 12); + } + + /* The E2B/E4B form (KV-shared layers, the per-layer branch) on the same grids. With q, k and v + * apart, i8g's k and v read q's pair and the junctions fuse; an a4 linear shares no pair, so an + * a4 trunk with q, k and v apart keeps its chain. With q|k|v one linear (and the KV-shared + * layers' q on the same grid) it fuses, and its decode band reads the junctions' bf16 output. */ + struct E4 { const Fmt* f; bool qkv; bool fuse; }; + for (const E4 k : { E4{ &fmts[0], false, true }, E4{ &fmts[1], false, false }, + E4{ &fmts[2], true, true } }) + for (int world : { 1, 2 }) + for (const int64_t T : { (int64_t)4, (int64_t)200 }) { + RadBuilder b; + const RadEncoding& e = k.f->e; + b.encs = { { "attn_q.", e }, { "attn_o.", e }, { "ffn_gate_up.", e }, { "ffn_down.", e } }; + if (k.qkv) b.encs.push_back({ "attn_qkv.", e }); + else for (const char* x : { "attn_k.", "attn_v." }) b.encs.push_back({ x, e }); + RadModelMeta meta = gemma4_e4b_meta(); + RadBuildCtx ctx = build_ctx(0, world); + CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx)); + CHECK_EQ(gemma4_bf16::g_model[0].fuse, k.fuse); + CHECK(!gemma4_bf16::g_model[0].overlap); + static const int32_t outs[] = { 99, 199 }; + RadBatch batch = T == 4 ? decode_batch() : prefill_batch(); + if (T != 4) { + batch.n_tok = 200; batch.max_q_len = 100; batch.max_ctx_len = 100; + batch.out_ids = outs; + } + RadCtx c; c.b = &b; c.rank = 0; c.world = world; c.batch = &batch; + gemma4_bf16::step(&c, &batch); + CHECK(c.step_fail.empty()); + CHECK_LINT(b, &c); + const std::vector ops = issued_ops(b, c); + CHECK_EQ(count_issued(ops, "rmsnorm_quant_i8g") + count_issued(ops, "ar_rmsnorm_quant_i8g"), + k.fuse ? 2 * 12 : 0); + } +} + /* Where no loaded library declares libr4d's fused ops (a CPU-only build, where rad-convert and the * host backend run), asking for one is an error, so the 31B with its DFlash drafter asks first and * declares the chains: the junctions, the attention prologue, the gated quantiser, the folded