From c5278632485e7000becf2b40063f4c0c339fe7e4 Mon Sep 17 00:00:00 2001 From: Ashlyn Date: Mon, 12 Oct 2026 10:28:43 -0500 Subject: [PATCH] arch/gemma4: the dense arm on the second lane beside the routed arm, in a step that runs whole The two feed-forward arms of a Gemma 4 MoE layer read the same normed residual and meet only at the feed-forward join; issued one after the other, the dense arm's three launches -- gate_up, act_mul, down, ~36 us under --profile-ops at a 26B-A4B decode step on 4x R9700 -- wait behind the routed arm's ~120 every layer. At a few rows its GEMMs are latency-bound, nowhere near the bandwidth the experts' GEMMs take. In a step that runs whole -- every decode and verify step, and a prefill chunk too short to cut -- the dense arm now rides the second lane: issued behind a join from lane 0 (the attention join is its producer), beside the routed arm on lane 0, and joined back before the feed-forward reduction reads both. Its rows of the join buffer and the routed arm's are disjoint. No collective rides it, so the reductions keep lane 0 here and lane 1 in a cut prefill chunk, and a step's collectives are on one lane as before. h, gate_up, ffn and ff2 are named to rad_buf_concurrent. Off with the overlap's switch, RADIANCE_GEMMA4_OVERLAP=0, which now keeps every step on one stream; a startup note says which. arch_test: a decode step issues the dense arm's three launches a layer on lane 1 and every all-reduce on lane 0, two joins a layer, and nothing on the two lanes that touches a common element unordered; a 4095-token chunk the same; the cut chunks keep lane 1 for their reductions alone. GUIDE.md's switch and ARCHITECTURES.md say so. Not yet run on the cards. Co-Authored-By: Claude Opus 5.5 --- arch/gemma4/gemma4.cpp | 35 +++++++++++++++++++++++++++++------ docs/ARCHITECTURES.md | 7 +++++-- docs/GUIDE.md | 2 +- tests/arch_test.cpp | 40 +++++++++++++++++++++++++++++----------- 4 files changed, 64 insertions(+), 20 deletions(-) diff --git a/arch/gemma4/gemma4.cpp b/arch/gemma4/gemma4.cpp index 7dba1b8f..be3af261 100644 --- a/arch/gemma4/gemma4.cpp +++ b/arch/gemma4/gemma4.cpp @@ -273,6 +273,9 @@ struct Model { /* A prefill chunk of 2 * moe_rows tokens or more runs as two slices, the trunk's all-reduces * on the second lane beside the other slice's compute (trunk_overlap). Set at declare. */ bool overlap = false; + /* In a step that runs whole -- every decode and verify step -- the dense arm rides the second + * lane beside the routed arm (trunk). Set at declare. */ + bool arms = false; double escale = 1.0, softcap = 0.0; rad_buf b_x = 0, b_h = 0, b_o = 0; @@ -1481,10 +1484,17 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c for (const Layer& y : m.layers) m.overlap = m.overlap && (y.op_join_a || y.op_join_r) && y.op_join_f && y.next_norm && y.op_ar_o && y.op_ar_f; - if (m.overlap) { - RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_o)); - RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_ff2)); - } + /* ---- THE TWO ARMS SIDE BY SIDE (trunk), in a step that runs whole: the dense arm on the + * second lane beside the routed arm, which needs nothing of it until the feed-forward join. At a + * decode step on 4x R9700 the dense arm is three launches, ~36 us under --profile-ops, behind + * the routed arm's ~120 in a layer, and its GEMMs at a few rows are latency-bound -- far from + * the bandwidth the experts take. No collective rides it, so the reductions keep lane 0. Off + * with the overlap's switch. The buffers the dense arm touches leave the arena's sharing. */ + m.arms = m.moe && !ov_off; + if (m.overlap) RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_o)); + if (m.overlap || m.arms) RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_ff2)); + if (m.arms) + for (rad_buf bb : { m.b_h, m.b_gu, m.b_act }) RAD_ARCH_TRY(rad_buf_concurrent(b, bb)); if (m.layers.empty() || !m.layers.back().next_norm) m.op_onorm = rmsnorm(T, E, m.w_onorm, 1.0, m.b_x, m.b_h); @@ -1563,6 +1573,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c : !g.wire_exact ? "off (the lossy all-reduce wire)" : T < 2 * m.moe_rows ? "off (a step holds fewer than two routed passes)" : "off (the sandwich joins are not fused)"); + rad_note(b, "the two arms: %s", m.arms + ? "in a step that runs whole, the dense arm on a second stream beside the " + "routed" + : "one after the other (RADIANCE_GEMMA4_OVERLAP=0)"); } if (m.kf.rot_pairs < m.kf.hd / 2) rad_note(b, "full layers: PROPORTIONAL rope -- %lld of %lld frequency pairs rotate at " @@ -1712,9 +1726,10 @@ static void attend(RadCtx* c, const Model& m, const Layer& y, size_t li, const R } } -/* The trunk over the whole step. */ +/* The trunk over the whole step, the dense arm beside the routed one where the model says so. */ static void trunk(RadCtx* c, const Model& m, const RadBatch* batch) { const int64_t T = batch->n_tok, E = m.E; + bool lanes_ok = true; for (size_t li = 0; li < m.layers.size(); ++li) { const Layer& y = m.layers[li]; if (y.op_anorm) RAD_ISSUE(c, y.op_anorm, brows(m.b_x, T), RAD_W(y.w_anorm), brows(m.b_h, T)); @@ -1740,13 +1755,18 @@ static void trunk(RadCtx* c, const Model& m, const RadBatch* batch) { RAD_ISSUE(c, y.op_fnorm, brows(m.b_x, T), RAD_W(y.w_fnorm), brows(m.b_h, T)); } - /* dense arm -> rows [0, T) of the join */ + /* dense arm -> rows [0, T) of the join: on lane 1 behind the attention join, where the arms + * run side by side, and lane 0 joins it back before the reduction reads them */ + if (m.arms) + lanes_ok = lanes_ok && rad_lane_join(c, 0, 1) == RAD_OK && rad_lane(c, 1) == RAD_OK; RAD_ISSUE(c, y.op_gu, brows(m.b_h, T), RAD_W(y.w_gu), brows(m.b_gu, T)); RAD_ISSUE(c, y.op_act, bcol(m.b_gu, 0, m.n_ff, T), bcol(m.b_gu, m.n_ff, m.n_ff, T), brows(m.b_act, T)); RAD_ISSUE(c, y.op_dn, brows(m.b_act, T), RAD_W(y.w_dn), brows(m.b_ff2, T)); + if (m.arms) lanes_ok = rad_lane(c, 0) == RAD_OK && lanes_ok; if (m.moe) { routed(c, m, y, T, 0, T, T); + if (m.arms) lanes_ok = rad_lane_join(c, 1, 0) == RAD_OK && lanes_ok; if (y.op_ar_f) RAD_ISSUE_N(c, y.op_ar_f, 2 * T * E, brows(m.b_ff2, 2 * T), RAD_NONE); if (y.op_join_f) { RAD_ISSUE(c, y.op_join_f, brows(m.b_ff2, T), RAD_W(y.w_fpost1), @@ -1776,6 +1796,9 @@ static void trunk(RadCtx* c, const Model& m, const RadBatch* batch) { bcol(m.b_dfsrc, (int64_t)t * E, E, T)); } } + if (!lanes_ok) + rad_step_fail(c, "gemma4: the dense arm's second lane could not be ordered against the " + "first (RADIANCE_GEMMA4_OVERLAP=0 keeps one)"); } /* ============================================================== the prefill overlap */ diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index 21daa5d8..ff7c5cab 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -520,8 +520,11 @@ are above every row band the sliced kernels have, so the step issues the same ke same rows and its output is the whole step's, bit for bit. On 4x R9700 the reductions were 47% of a 20K-token prefill under `--profile-ops`; a model of the measured times puts the overlap at up to a quarter off an unprofiled prefill. The per-op profile syncs every op, so it cannot show it -- -time a prefill without `--profile-ops`. `RADIANCE_GEMMA4_OVERLAP=0` runs every chunk whole, and the -startup note says which. A decode step never takes the second lane. +time a prefill without `--profile-ops`. A step that runs whole -- every decode and verify step -- +takes the second lane for the dense arm instead, beside the routed arm, which needs nothing of it +until the feed-forward join: at decode the dense arm's GEMMs are a few rows, latency-bound and far +from the bandwidth the experts take, and its reductions stay on lane 0. `RADIANCE_GEMMA4_OVERLAP=0` +keeps every step on one stream, and the startup notes say which. **The draft head in fp8.** The drafter's head is the widest read on the draft path: the target's bf16 table, 352 MiB a rank at tp 4, every draft pass. A container converted with the line diff --git a/docs/GUIDE.md b/docs/GUIDE.md index cc9b1ba9..1fee7b8a 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1860,7 +1860,7 @@ rad-kbench --home $RADIANCE_HOME --max-cases 1 # are the kernels correct at al | `RADIANCE_LOG_STEPS` | log one line per step: its phase, sequences, and each one's token count and context (§4.5) | | `RADIANCE_G4K_WMMA` | `0` keeps `gemma4k`'s prefill attention on its scalar kernel instead of the WMMA one. An A/B switch and a way out if the matrix path misbehaves on a card; read once | | `RADIANCE_GEMMA4_FUSE` | `0` makes `arch/gemma4` declare the chains instead of `gemma4k`'s fusions: the rmsnorm / add chain for the sandwich joins (`norm_add_norm`, `norm_add_norm_route`, `join_add_scale`), `embed_lookup` + `mul` for `expert_scale`, and a `cast` lifting q for `attn_paged` for `attn_paged_qrow`. An A/B switch; read at declare | -| `RADIANCE_GEMMA4_OVERLAP` | `0` runs every `arch/gemma4` prefill chunk whole, its all-reduces on the compute stream, instead of as two slices whose all-reduces ride a second stream beside the other slice's compute. An A/B switch; read at declare | +| `RADIANCE_GEMMA4_OVERLAP` | `0` keeps every `arch/gemma4` step on one stream: a prefill chunk runs whole, its all-reduces on the compute stream, instead of as two slices whose all-reduces ride a second stream beside the other slice's compute; and a decode step runs the dense arm after the routed arm instead of beside it. An A/B switch; read at declare | There is also a family of instruments for engine development — `RADIANCE_DEBUG_*` (residual probes, idle scrubs, routing and issue dumps), `RADIANCE_OP_ORACLE*` (named ops checked against `libref` on diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 2ab1ac44..a1f0adf9 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -3972,8 +3972,9 @@ bool noted(const RadBuilder& b, const char* text) { * order and the work: every reduction rides lane 1 behind its producer and ahead of its consumer, * nothing else does, the cut is a whole number of routed passes, and the slices issue exactly the * rows the whole step issues -- the same routed passes, the drafter's taps -- which is what makes - * the output the whole step's. A chunk under two passes, and a decode step, run whole on one - * lane. */ + * the output the whole step's. A chunk under two passes, and a decode step, run whole: their + * reductions on lane 0 and the dense arm on lane 1 beside the routed arm (trunk), with nothing + * unordered either. */ TEST(a_gemma4_prefill_chunk_runs_as_two_slices_with_its_reductions_on_the_second_lane) { const std::vector keys = g4df_keys(), vals = g4df_vals(); const int64_t E = 2816, L = 6; @@ -4002,8 +4003,8 @@ TEST(a_gemma4_prefill_chunk_runs_as_two_slices_with_its_reductions_on_the_second RadBuilder b; CHECK_OK(model(b, false)); CHECK(noted(b, "prefill overlap: a chunk of two routed passes")); - CHECK_EQ((int)b.concurrent.size(), 2); - for (const char* name : { "o", "ff2" }) + CHECK_EQ((int)b.concurrent.size(), 5); + for (const char* name : { "o", "ff2", "h", "gate_up", "ffn" }) CHECK(std::count(b.concurrent.begin(), b.concurrent.end(), (rad_buf)buf_named(b, name)) == 1); RadBatch batch = one_prompt(k.T, out); @@ -4013,17 +4014,20 @@ TEST(a_gemma4_prefill_chunk_runs_as_two_slices_with_its_reductions_on_the_second CHECK_EQ(c.lane, 0); CHECK_EQ(races(b, c), 0); - std::vector lane1; + std::vector lane1; /* the reductions on lane 1 */ + int dense1 = 0; /* and the dense arm's launches */ for (const auto& i : c.issues) { if (i.lane != 1) continue; const RecOp* o = b.op_of(i.op); - CHECK(o && o->op == "all_reduce"); - lane1.push_back(i.n); + REQUIRE(o != nullptr); + if (o->op == "all_reduce") lane1.push_back(i.n); + else ++dense1; } if (k.cut == 0) { CHECK(lane1.empty()); - CHECK(c.joins.empty()); + CHECK_EQ(dense1, 3 * (int)L); /* gate_up, act_mul, down: beside the routed arm */ } else { + CHECK_EQ(dense1, 0); REQUIRE(lane1.size() == (size_t)(4 * L)); for (int64_t l = 0; l < L; ++l) { CHECK_EQ(lane1[4 * l + 0], k.cut * E); /* attention, slice A */ @@ -4048,15 +4052,29 @@ TEST(a_gemma4_prefill_chunk_runs_as_two_slices_with_its_reductions_on_the_second CHECK_EQ((int)ns_of(b, c, "cast").size(), k.cut ? 6 : 3); /* three taps, a slice each */ } - /* A decode step never leaves lane 0. */ + /* A decode step: the reductions on lane 0, the dense arm on lane 1 joined in before the + * feed-forward's, nothing unordered. */ RadBuilder b; CHECK_OK(model(b, false)); + CHECK(noted(b, "the two arms: in a step that runs whole")); RadBatch d = decode_batch(); RadCtx c; run(b, c, d); CHECK(c.step_fail.empty()); - CHECK(c.joins.empty()); - for (const auto& i : c.issues) CHECK_EQ(i.lane, 0); + CHECK_EQ(c.lane, 0); + CHECK_EQ(races(b, c), 0); + int dense1 = 0; + for (const auto& i : c.issues) { + const RecOp* o = b.op_of(i.op); + REQUIRE(o != nullptr); + if (o->op == "all_reduce") CHECK_EQ(i.lane, 0); + if (i.lane == 1) { + ++dense1; + CHECK(o->op == "gemm_nt" || o->op == "act_mul"); + } + } + CHECK_EQ(dense1, 3 * (int)L); + CHECK_EQ((int)c.joins.size(), 2 * (int)L); } /* THE ATTENTION JOIN WRITES THE ROUTED ARM'S INPUTS on a layer whose gate_up is block fp8