From c9f79c32d8bbacbd1f38b5c0418ff16408b724c5 Mon Sep 17 00:00:00 2001 From: mici81 Date: Thu, 22 Oct 2026 15:51:58 +0000 Subject: [PATCH] gemma3: weight reads beside the decode all-reduces, trunk and DFlash2 drafter As qwen35_fp8 has them (prefetch_beside, rad_arch.h): behind the o GEMM, beside the attention block's all-reduce, gate|up's leading 24 MB are read into the on-die cache; behind the down GEMM, beside the feed-forward's, the next layer's q; the drafter's all-reduces take the same (Dflash2::Config::prefetch). Decode steps of up to 32 rows, none on a step with a prompt's rows, only at more than one rank; RADIANCE_PREFETCH=0 declares none. TranslateGemma-27B MXFP4 + DFlash2, TP2 on two R9700s, no speculation, two rounds, x1 / x4 / x8 tok/s against none (56.8 / 182.0 / 325.0): 8 MB 63.3 / 203.1 / 354.9; 12 MB 63.0 / 202.6 / 354.9; 16 MB 63.5 / 203.9 / 356.6; 20 MB 64.1 / 204.0 / 359.4; 24 MB 64.4 / 206.4 / 359.3 (+13.4 / +13.4 / +10.5 %); 32 MB 63.1 / 201.4 / 351.9. With speculation 7 (three rounds, greedy, medians) x1 / x4 / x8 / x16 158.1 / 409.0 / 592.0 / 710.8 -> 177.3 / 428.4 / 592.3 / 717.6 (+12.1 / +4.7 / 0.0 / +1.0 %). Bit for bit: a prefetch reads weights only. Co-Authored-By: Claude Opus 5.5 (1M context) --- arch/gemma3/gemma3.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/arch/gemma3/gemma3.cpp b/arch/gemma3/gemma3.cpp index deb2d3fb..cec5d9b7 100644 --- a/arch/gemma3/gemma3.cpp +++ b/arch/gemma3/gemma3.cpp @@ -914,12 +914,28 @@ struct G3JunctionFP8 { } }; +/* The weight reads beside the decode all-reduces (prefetch_beside, rad_arch.h): a weight's first + * kPrefetchBytes, on decode steps of up to kPrefetchStepRows rows (a verify of four sequences at + * speculation 7, as qwen35_fp8's). TranslateGemma-27B MXFP4 + DFlash2 at tp 2 on 2x R9700 (Gen5 x8), + * no speculation, two rounds each, x1 / x4 / x8 tok/s against none (56.8 / 182.0 / 325.0): 8 MB 63.3 / + * 203.1 / 354.9; 12 MB 63.0 / 202.6 / 354.9; 16 MB 63.5 / 203.9 / 356.6; 20 MB 64.1 / 204.0 / 359.4; + * 24 MB 64.4 / 206.4 / 359.3; 32 MB 63.1 / 201.4 / 351.9. Larger than qwen35_fp8's 12 MB: this + * model's hidden width (5376) and weights are larger, so its all-reduces take longer and its GEMMs + * read more; past ~24 MB the read outlasts the reduction and takes bandwidth from the GEMM it should + * feed. With speculation 7 (three rounds, greedy, medians) x1 / x4 / x8 / x16 158.1 / 409.0 / 592.0 / 710.8 -> 177.3 / 428.4 / 592.3 / 717.6. The drafter's reads (Dflash2::Config::prefetch) take the same size. */ +constexpr int64_t kPrefetchBytes = (int64_t)24 << 20; +constexpr int64_t kPrefetchStepRows = 32; + struct Layer { bool global = false; Gemma3Attn attn{}; Gemma3Mlp mlp{}; G3Junction j_am{}, j_ma{}; G3JunctionFP8 jf_am{}, jf_ma{}; + /* The reads beside the decode all-reduces: gate|up's leading bytes beside the attention block's + * reduction, the next layer's q's beside the feed-forward's. 0 where none is declared. */ + rad_op op_pf_gu = 0, op_pf_next = 0; + rad_weight pf_gu_w = 0, pf_next_w = 0; }; struct Model { @@ -1004,6 +1020,7 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui Dflash2::Config dc{}; dc.short_block = 1; /* rad_drafter_short_block below */ + dc.prefetch = kPrefetchBytes; /* reads beside the drafter's all-reduces (at tp > 1) */ dc.n_taps = m.n_taps; dc.block = ctx->max_spec + 1; dc.taps = rad_meta_geti(meta, "draft.dflash_config.conv_kernel_size", 2); @@ -1355,6 +1372,19 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c } m.fuse_mx_ar = ar; } + /* ---- the weight reads beside the decode all-reduces (prefetch_beside, rad_arch.h). Only at + * more than one rank; RADIANCE_PREFETCH=0 declares none (decl_prefetch). */ + if (g.world > 1 && rad_op_declared(b, "prefetch")) { + for (int64_t l = 0; l < g.n_layer; ++l) { + Layer& lay = m.layers[(size_t)l]; + lay.pf_gu_w = lay.mlp.ln_gu.w; + lay.op_pf_gu = decl_prefetch(b, lay.pf_gu_w, kPrefetchBytes); + if (l + 1 < g.n_layer) { + lay.pf_next_w = m.layers[(size_t)l + 1].attn.ln_q.w; + lay.op_pf_next = decl_prefetch(b, lay.pf_next_w, kPrefetchBytes); + } + } + } /* The pipeline needs the fused fp8 junctions and every block's overlap all-reduce. */ if (m.pipe_on) { bool ok = m.fuse_mx; @@ -1672,6 +1702,9 @@ static void step(RadCtx* c, const RadBatch* batch) { if (m.op_embed_ar) RAD_ISSUE_N(c, m.op_embed_ar, T * m.g.n_embd, brows(m.b_x, T), RAD_NONE); + /* The reads beside the reductions go on decode steps of up to kPrefetchStepRows rows, none on a + * step that carries a prompt's rows (as qwen35_fp8's). */ + const int64_t pf_h = batch->phase == RAD_PHASE_DECODE && T <= kPrefetchStepRows ? 1 : INT64_MAX; if (m.fuse_mx) { /* MXFP4 trunk: [post-norm, then add + next norm + quantiser as one op] at every junction; * the last block's tail runs unfused into the final norm. */ @@ -1682,9 +1715,11 @@ static void step(RadCtx* c, const RadBatch* batch) { /* Per step: the fused junction sends the exact wire only (G3JunctionFP8::op_ar). */ const bool far = m.fuse_mx_ar && ar_wire_is_exact(m.g, T); l.attn.step(c, batch, true, true, far); + prefetch_beside(c, l.op_pf_gu, l.pf_gu_w, pf_h); if (!far) l.attn.post(c, T); l.jf_am.step(c, T, far); l.mlp.step(c, batch, true, !last, far); + prefetch_beside(c, l.op_pf_next, l.pf_next_w, pf_h); if (!last) { if (!far) l.mlp.post(c, T); l.jf_ma.step(c, T, far); @@ -1698,8 +1733,10 @@ static void step(RadCtx* c, const RadBatch* batch) { const bool last = li + 1 == m.layers.size(); const bool ar = m.fuse_ar && ar_wire_is_exact(m.g, T); /* exact wire only, as above */ l.attn.step(c, batch, m.fuse, m.fuse, ar); + prefetch_beside(c, l.op_pf_gu, l.pf_gu_w, pf_h); if (m.fuse) l.j_am.step(c, T, ar); l.mlp.step(c, batch, m.fuse, m.fuse && !last, ar); + prefetch_beside(c, l.op_pf_next, l.pf_next_w, pf_h); if (m.fuse && !last) l.j_ma.step(c, T, ar); tap(li); }