diff --git a/arch/gemma3/gemma3.cpp b/arch/gemma3/gemma3.cpp index 6580ff94..dfdfc83f 100644 --- a/arch/gemma3/gemma3.cpp +++ b/arch/gemma3/gemma3.cpp @@ -651,7 +651,7 @@ struct G3Junction { const rad_buf wr[3] = { q8, s8, res }; rad_op_reads (b, op, rd, res ? 2 : 1); rad_op_writes(b, op, wr, res ? 3 : 2); - if (res && g.world == 2 && g.wire_exact && rad_op_declared(b, "ar_rmsnorm_quant_i8")) { + if (res && g.world == 2 && rad_op_declared(b, "ar_rmsnorm_quant_i8")) { const RadParam pa[] = { RAD_RANGE("M", 1, g.max_tok), 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) }; @@ -678,8 +678,11 @@ struct G3Junction { * embedding stream. */ struct G3JunctionFP8 { rad_op op = 0; - /* At two ranks on the exact wire: the branch's all-reduce and post-norm folded in front - * (ar_rmsnorm_post_quant_fp8), `x` then being this rank's partial sum. One launch. */ + /* At two ranks: the branch's all-reduce and post-norm folded in front + * (ar_rmsnorm_post_quant_fp8), `x` then being this rank's partial sum. One launch. The kernel + * sends the exact wire only, so under --tp-wire it is declared all the same and taken by the + * steps whose message goes out exact anyway (ar_wire_is_exact: a decode step of a few rows, + * under --tp-wire-min-kb); a wider step keeps the lossy all-reduce, unfused. */ rad_op op_ar = 0; rad_buf x = 0, res = 0, q = 0, s = 0; rad_weight w = 0, wp = 0; @@ -700,7 +703,7 @@ struct G3JunctionFP8 { const rad_buf wr[3] = { q, s, res }; rad_op_reads (b, op, rd, res ? 2 : 1); rad_op_writes(b, op, wr, res ? 3 : 2); - if (res && wp && g.world == 2 && g.wire_exact && rad_op_declared(b, "ar_rmsnorm_post_quant_fp8")) { + if (res && wp && g.world == 2 && rad_op_declared(b, "ar_rmsnorm_post_quant_fp8")) { const rad_weight ws[2] = { w, wp }; const RadParam pa[] = { RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", n), RAD_F64("eps", g.eps), RAD_INT("group", RAD_FP8_BLOCK), RAD_INT("world_size", g.world), @@ -1217,7 +1220,8 @@ static void step(RadCtx* c, const RadBatch* batch) { for (size_t li = 0; li < m.layers.size(); ++li) { const Layer& l = m.layers[li]; const bool last = li + 1 == m.layers.size(); - const bool far = m.fuse_mx_ar; + /* 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); if (!far) l.attn.post(c, T); l.jf_am.step(c, T, far); @@ -1233,10 +1237,11 @@ static void step(RadCtx* c, const RadBatch* batch) { for (size_t li = 0; !m.fuse_mx && li < m.layers.size(); ++li) { const Layer& l = m.layers[li]; const bool last = li + 1 == m.layers.size(); - l.attn.step(c, batch, m.fuse, m.fuse, m.fuse_ar); - if (m.fuse) l.j_am.step(c, T, m.fuse_ar); - l.mlp.step(c, batch, m.fuse, m.fuse && !last, m.fuse_ar); - if (m.fuse && !last) l.j_ma.step(c, T, m.fuse_ar); + 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); + if (m.fuse) l.j_am.step(c, T, ar); + l.mlp.step(c, batch, m.fuse, m.fuse && !last, ar); + if (m.fuse && !last) l.j_ma.step(c, T, ar); tap(li); } /* With fused junctions the last layer's tail ran unfused (tail_fused false), so the stream is diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index b2097a34..3739b5a7 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -6550,6 +6550,41 @@ TEST(gemma3_steps_lint_clean_at_one_and_two_ranks) { } } +/* Under a lossy --tp-wire the fused all-reduce junction of the MXFP4 trunk, which sends the exact + * wire only, still serves the steps whose message goes out exact anyway: a decode step's few rows + * fold every junction's all-reduce in, while a prefill chunk above --tp-wire-min-kb keeps the + * lossy all-reduce, unfused. */ +TEST(gemma3_under_a_lossy_wire_small_steps_keep_the_fused_junction) { + MetaKV mk = gemma3_meta(); + const RadEncoding mx = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, rad::arch::RAD_MX_GROUP); + for (int rank = 0; rank < 2; ++rank) { + RadBuilder b; + b.encs = { { "attn_", mx }, { "ffn_", mx } }; + RadBuildCtx ctx = build_ctx(rank, 2); + ctx.tp_wire_lossy = 1; + ctx.tp_wire_min_bytes = 128 * 1024; + REQUIRE_EQ(gemma3::declare(&b, mk.meta(), &ctx), RAD_OK); + CHECK(gemma3::g_model[rank].fuse_mx && gemma3::g_model[rank].fuse_mx_ar); + auto issued = [&](const RadCtx& c, std::initializer_list names) { + int n = 0; + for (const RecIssue& i : c.issues) + if (const RecOp* o = b.op_of(i.op)) + for (const char* nm : names) n += o->op == nm; + return n; + }; + const auto dec = kit(b, { 1 }, {}); + const auto pre = kit(b, {}, { 64 }); /* 64 x 5376 x 2 bytes, above the floor */ + const auto v = run_passes(b, gemma3::step, { dec.get(), pre.get() }, rank, 2); + CHECK_PASSES(b, v); + const int fused_dec = issued(*v[0], { "ar_rmsnorm_quant_i8", "ar_rmsnorm_post_quant_fp8" }); + const int fused_pre = issued(*v[1], { "ar_rmsnorm_quant_i8", "ar_rmsnorm_post_quant_fp8" }); + CHECK(fused_dec > 0); + CHECK_EQ(fused_pre, 0); + /* Every junction the decode step fused is an all-reduce the prefill step issues alone. */ + CHECK_EQ(issued(*v[1], { "all_reduce" }), issued(*v[0], { "all_reduce" }) + fused_dec); + } +} + /* The DFlash2 drafter: a block of 8 (depth 7, fixed), taps copied out after the named layers into * one column-sliced buffer, the mask token from the config, and the drafter's own KV group after * the trunk's two. Its input_embedding_scale must be the trunk's sqrt(hidden) held in bf16