From bb48b14005b07a78f0139d08de1f58fabb359d66 Mon Sep 17 00:00:00 2001 From: Constantin Date: Sun, 25 Oct 2026 07:20:49 -0400 Subject: [PATCH] Reuse BF16 add for HYV3 shared/routed combination Replace the equivalent FP32 cast/add/cast chain with the existing BF16 add and enable the existing fused router for HYV3. Preserve separate tensor-parallel arm reductions and BF16 rounding boundaries; add the reference arithmetic check. --- arch/common/rad_block_moe_fp8.h | 50 +++++++++------------------------ arch/hyv3_common/hyv3_impl.cpp | 5 +--- tests/arch_test.cpp | 25 ++++++++++------- tests/ref_test.cpp | 23 +++++++++++++++ 4 files changed, 53 insertions(+), 50 deletions(-) diff --git a/arch/common/rad_block_moe_fp8.h b/arch/common/rad_block_moe_fp8.h index 1c82f109..e7ead972 100644 --- a/arch/common/rad_block_moe_fp8.h +++ b/arch/common/rad_block_moe_fp8.h @@ -282,9 +282,6 @@ struct MoeFP8 { rad_buf edn = 0; /* [rows * top_k, n_embd] bf16, in sorted order */ /* back in token order, and the shared expert, one row per token */ rad_buf moe = 0; /* [max_tok, n_embd] bf16; separate routed output if needed */ - rad_buf moe_f32 = 0; /* [max_tok, n_embd] f32; shared_fp32_combine only */ - rad_buf shared_f32 = 0;/* [max_tok, n_embd] f32; shared_fp32_combine only */ - rad_buf combined_f32 = 0; /* [max_tok, n_embd] f32; shared_fp32_combine only */ rad_buf sgu = 0; /* [max_tok, 2 * n_ff_shared] bf16 */ ActFP8 sff{}; /* [max_tok, n_ff_shared] */ rad_buf sout = 0; /* [max_tok, n_embd] bf16 */ @@ -426,7 +423,7 @@ struct MoeFP8 { mutable bool calib_dne_failed = false; void calib_experts_pass(RadCtx* c_, int64_t slots) const; rad_op op_sgate = 0; - rad_op op_moe_to_f32 = 0, op_shared_to_f32 = 0, op_combine_f32 = 0, op_combined_to_bf16 = 0; + rad_op op_combine = 0; /* BF16 inputs, FP32 addition, one BF16 rounding */ rad_op op_moe_ar = 0, op_shared_ar = 0; rad_op op_ar = 0, op_add = 0; bool ar_out = false; @@ -631,10 +628,9 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, src.shared)) return RAD_E_INVAL; const bool shared = c.n_ff_shared > 0 && (src.shared || src.shared_mlp_gate); if (shared && c.shared_ungated && src.shared_gate) return RAD_E_INVAL; - if (c.shared_fp32_combine && (!shared || !w.moe || !w.moe_f32 || !w.shared_f32 || - !w.combined_f32)) { + if (c.shared_fp32_combine && (!shared || !w.moe || std::strcmp(g.dtype, "bf16"))) { fprintf(stderr, "radiance: layer %d requests an FP32 shared/routed combine without its " - "shared arm and scratch buffers\n", l); + "BF16 shared arm and routed output\n", l); return RAD_E_INVAL; } /* Whether `w.sgate` is excluded from buffer sharing; set with the shared arm and required by @@ -1416,22 +1412,15 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, } if (separate_fp32_combine) { - const auto cast = [&](const char* from, const char* to) { - return RAD_OP(b, "cast", - RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), - RAD_STR("from", from), RAD_STR("to", to)), - RAD_NOWEIGHTS); - }; - op_moe_to_f32 = rw(b, cast("bf16", "f32"), {w.moe}, {w.moe_f32}); - op_shared_to_f32 = rw(b, cast("bf16", "f32"), {w.sout}, {w.shared_f32}); - op_combine_f32 = rw(b, RAD_OP(b, "add", - RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), - RAD_STR("dtype", "f32")), - RAD_NOWEIGHTS), - {w.moe_f32, w.shared_f32}, {w.combined_f32}); - op_combined_to_bf16 = rw(b, cast("f32", "bf16"), {w.combined_f32}, {w.h.x}); - if (!op_moe_to_f32 || !op_shared_to_f32 || !op_combine_f32 || !op_combined_to_bf16) - return RAD_E_INVAL; + /* BF16 add widens both inputs to FP32, adds, then rounds once to BF16: exactly the + * former cast + cast + F32 add + cast chain. Keep the routed gather's BF16 rounding + * and the separate TP reductions; folding the shared arm into the gather is different. */ + op_combine = rw(b, RAD_OP(b, "add", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), + RAD_STR("dtype", "bf16")), + RAD_NOWEIGHTS), + {w.moe, w.sout}, {w.h.x}); + if (!op_combine) return RAD_E_INVAL; } /* ---- The router GEMM and `router_topk_scatter` as one op, plus the shared gate when the @@ -1806,21 +1795,10 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows, bool o if (op_shared_ar) RAD_ISSUE_N(c_, op_shared_ar, rows * g.n_embd, brow_slice(w.sout, r0, rows, g.n_embd), RAD_NONE); - if (c.shared_fp32_combine) { - RAD_ISSUE_N(c_, op_moe_to_f32, rows, - brow_slice(w.moe, r0, rows, g.n_embd), - brow_slice(w.moe_f32, r0, rows, g.n_embd)); - RAD_ISSUE_N(c_, op_shared_to_f32, rows, + if (op_combine) + RAD_ISSUE_N(c_, op_combine, rows, brow_slice(w.moe, r0, rows, g.n_embd), brow_slice(w.sout, r0, rows, g.n_embd), - brow_slice(w.shared_f32, r0, rows, g.n_embd)); - RAD_ISSUE_N(c_, op_combine_f32, rows, - brow_slice(w.moe_f32, r0, rows, g.n_embd), - brow_slice(w.shared_f32, r0, rows, g.n_embd), - brow_slice(w.combined_f32, r0, rows, g.n_embd)); - RAD_ISSUE_N(c_, op_combined_to_bf16, rows, - brow_slice(w.combined_f32, r0, rows, g.n_embd), brow_slice(w.h.x, r0, rows, g.n_embd)); - } /* A write that takes the all-reduce does it once for the whole step, deciding on T, so this * pass must ask with T, not its own rows. */ diff --git a/arch/hyv3_common/hyv3_impl.cpp b/arch/hyv3_common/hyv3_impl.cpp index 363f023c..b1b127ef 100644 --- a/arch/hyv3_common/hyv3_impl.cpp +++ b/arch/hyv3_common/hyv3_impl.cpp @@ -234,7 +234,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c mc.route_scale = meta_text_getf(meta, "router_scaling_factor", 2.826); mc.shared_ungated = 1; mc.shared_fp32_combine = 1; - mc.router_fuse = 0; + mc.router_fuse = 1; mc.rows = T < 2048 ? T : 2048; block_slice_128(m.n_ff_shared, g.world, g.rank, &mc.shared_lo, &mc.shared_hi); mc.n_ff_shared = mc.shared_hi - mc.shared_lo; @@ -260,9 +260,6 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c RAD_ARCH_TRY(m.mw.eff.declare_qs(b, nm, g, "moe_ffn", mc.n_ff_exp, S, !HYV3_FP8_SOURCE)); m.mw.edn = decl_b(b, nm.f("moe_down"), RAD_BF16, {S, g.n_embd}); m.mw.moe = decl_b(b, nm.f("moe_routed"), RAD_BF16, {T, g.n_embd}); - m.mw.moe_f32 = decl_b(b, nm.f("moe_routed_f32"), RAD_F32, {T, g.n_embd}); - m.mw.shared_f32 = decl_b(b, nm.f("moe_shared_f32"), RAD_F32, {T, g.n_embd}); - m.mw.combined_f32 = decl_b(b, nm.f("moe_combined_f32"), RAD_F32, {T, g.n_embd}); m.mw.sgu = decl_b(b, nm.f("shared_gate_up"), RAD_BF16, {T, 2 * mc.n_ff_shared}); m.mw.sff.x = decl_b(b, nm.f("shared_ffn"), RAD_BF16, {T, mc.n_ff_shared}); RAD_ARCH_TRY(m.mw.sff.declare_qs(b, nm, g, "shared_ffn", mc.n_ff_shared, 0, diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index ed8f2b4c..e1cc63f0 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -729,8 +729,13 @@ TEST(hyv3_fp8_declares_dense_then_sigmoid_moe_with_fp32_combine) { CHECK(gather != nullptr); CHECK_EQ(gather->writes.size(), 1u); CHECK_EQ(gather->writes[0], moe.w.moe); - CHECK(moe.op_moe_to_f32 && moe.op_shared_to_f32 && moe.op_combine_f32 && - moe.op_combined_to_bf16); + REQUIRE(moe.op_combine); + const RecOp* combine = b.op_of(moe.op_combine); + REQUIRE(combine); + CHECK_EQ(combine->op, std::string("add")); + CHECK_EQ(std::string(pstr(combine, "dtype")), std::string("bf16")); + CHECK(combine->reads == (std::vector{moe.w.moe, moe.w.sout})); + CHECK(combine->writes == (std::vector{moe.w.h.x})); RadBatch batch = decode_batch(); RadCtx run{}; run.b = &b; run.batch = &batch; @@ -740,11 +745,12 @@ TEST(hyv3_fp8_declares_dense_then_sigmoid_moe_with_fp32_combine) { if (run.issues[i].op == h) return (int)i; return -1; }; - const int gather_i = issue_of(moe.op_gather), routed_i = issue_of(moe.op_moe_to_f32); - const int shared_i = issue_of(moe.op_shared_to_f32), add_i = issue_of(moe.op_combine_f32); - const int cast_i = issue_of(moe.op_combined_to_bf16); - CHECK(gather_i >= 0 && routed_i >= 0 && shared_i >= 0 && add_i >= 0 && cast_i >= 0); - CHECK(gather_i < routed_i && routed_i < add_i && shared_i < add_i && add_i < cast_i); + const int gather_i = issue_of(moe.op_gather), add_i = issue_of(moe.op_combine); + CHECK(gather_i >= 0 && add_i >= 0 && gather_i < add_i); + REQUIRE(moe.op_sigmoid_scatter); + CHECK(issue_of(moe.op_sigmoid_scatter) >= 0); + CHECK_EQ(issue_of(moe.op_scatter), -1); + CHECK_EQ(issue_of(moe.op_topk), -1); const auto lint = lint_step(b, {&run}); CHECK(lint.empty()); } @@ -778,9 +784,8 @@ TEST(hyv3_fp8_tensor_parallel_partitions_experts_and_shared_width) { return -1; }; CHECK(issue_of(moe.op_gather) < issue_of(moe.op_moe_ar)); - CHECK(issue_of(moe.op_moe_ar) < issue_of(moe.op_moe_to_f32)); - CHECK(issue_of(moe.op_shared_ar) < issue_of(moe.op_shared_to_f32)); - CHECK(issue_of(moe.op_combine_f32) < issue_of(moe.op_combined_to_bf16)); + CHECK(issue_of(moe.op_moe_ar) < issue_of(moe.op_combine)); + CHECK(issue_of(moe.op_shared_ar) < issue_of(moe.op_combine)); CHECK(lint_step(b, {&run}).empty()); } } diff --git a/tests/ref_test.cpp b/tests/ref_test.cpp index 2bf85cf0..34c6121a 100644 --- a/tests/ref_test.cpp +++ b/tests/ref_test.cpp @@ -1298,6 +1298,29 @@ TEST(op_add_mul) { CHECK_EQ(y.at(3), 204.0f); } +/* HYV3's two already-rounded arms must be added in FP32, then narrowed once. BF16 add is + * exactly that, without materialising three FP32 scratch planes. Exercise every BF16 bit pattern, + * including subnormals, ties, signed zero and nonfinite values, against the former cast chain. */ +TEST(bf16_add_matches_explicit_fp32_combine) { + constexpr int64_t M = 32, N = 2048; + T routed(RAD_BF16, {M, N}), shared(RAD_BF16, {M, N}); + T rf(RAD_F32, {M, N}), sf(RAD_F32, {M, N}), sum(RAD_F32, {M, N}); + T chain(RAD_BF16, {M, N}), folded(RAD_BF16, {M, N}); + for (uint32_t i = 0; i < M * N; ++i) { + const uint16_t r = (uint16_t)i, s = (uint16_t)(i * 40503u + 73u); + std::memcpy(routed.mem.data() + i * 2, &r, 2); + std::memcpy(shared.mem.data() + i * 2, &s, 2); + } + A r; r.t(routed).t(rf).i("M", M).i("n", N).s("from", "bf16").s("to", "f32"); + A s; s.t(shared).t(sf).i("M", M).i("n", N).s("from", "bf16").s("to", "f32"); + A a; a.t(rf).t(sf).t(sum).i("M", M).i("n", N).s("dtype", "f32"); + A c; c.t(sum).t(chain).i("M", M).i("n", N).s("from", "f32").s("to", "bf16"); + A f; f.t(routed).t(shared).t(folded).i("M", M).i("n", N).s("dtype", "bf16"); + CHECK_OK(run("cast", r)); CHECK_OK(run("cast", s)); + CHECK_OK(run("add", a)); CHECK_OK(run("cast", c)); CHECK_OK(run("add", f)); + CHECK_EQ(std::memcmp(chain.mem.data(), folded.mem.data(), M * N * 2), 0); +} + /* add's `div` divides the sum after its rounding to y's dtype: 1 + 0.01953125 rounds to bf16 * 1.015625 (a tie, to even), and 1.015625 / 3 to 0.337890625; the unrounded sum's third would be * 0.33984375. In f32 the quotient is f32(4 / 3). */