From 8f795c1022c6f52fb438de589d9658b71605487d Mon Sep 17 00:00:00 2001 From: picoder Date: Sun, 4 Oct 2026 14:50:37 +0000 Subject: [PATCH] plain bf16 experts: the model's width, and an uneven share of the tiles radiance: 'layer 34's plain experts are 384 wide, which 4 ranks cannot share in 64-wide tiles' -- on ranks 1 and 2 only. The protected experts are declared by a second grouped GEMM over their own slice of the expert's width, and that width was read off THIS RANK's parity slices (max ff_hi). At two ranks that is the model's width; at four it is 384, 512, 640 depending on who asks. Ranks 0 and 3 passed the check, ranks 1 and 2 refused, so the all-reduce's rendezvous got two of four ranks and the launch died -- the AR error was the cascade, not a bug. The width is g.n_ff_all. And an even share of it is not available here: 640 columns are ten 64-wide output tiles, which four ranks take as 3, 3, 2, 2. An uneven share is still whole tiles and every tile still has exactly one owner, so the rank takes a span -- uniform across a layer's protected experts, because their GEMM takes ONE table and every entry of a table has one geometry (unlike the quantised experts, which alternate by parity precisely because they have two). A rank count that divides the width keeps the old declaration verbatim, span included: no span at all, the loader dividing the entry itself. --- arch/common/rad_block_moe_fp8.h | 42 ++++++++++++++++++----- tests/arch_test.cpp | 59 +++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 8 deletions(-) diff --git a/arch/common/rad_block_moe_fp8.h b/arch/common/rad_block_moe_fp8.h index e490159b..36fc010d 100644 --- a/arch/common/rad_block_moe_fp8.h +++ b/arch/common/rad_block_moe_fp8.h @@ -1057,19 +1057,43 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const int64_t np = (int64_t)prot.size(); int64_t wp = c.n_ff_exp; int psh_row = RAD_SHARD_NONE, psh_col = RAD_SHARD_NONE, pparts = 0; + /* This rank's tile range within one plain expert, in the model's numbering; `phi` 0 is the + * even split, which needs no span because the loader divides the entry itself. */ + int64_t plo = 0, phi = 0; if (split) { - /* `n_ff_exp` is this rank's wider slice here; the expert's whole width is where the - * parity slices end. */ - const int64_t full = c.ff_hi[0] > c.ff_hi[1] ? c.ff_hi[0] : c.ff_hi[1]; - if (full % g.world || (full / g.world) % 64) { + /* THE EXPERT'S WHOLE WIDTH, not this rank's slice of it: `n_ff_all` is the model's, and + * where the parity tables end says the same thing at two ranks and nothing else at + * more -- reading it off `ff_hi` gave a per-rank answer, which happened to be right + * only when there were two of them. A plain expert has no scale blocks to land on, so + * the unit here is the GEMM's 64-wide output tile. */ + const int64_t full = g.n_ff_all > 0 ? g.n_ff_all + : (c.ff_hi[0] > c.ff_hi[1] ? c.ff_hi[0] + : c.ff_hi[1]); + /* ONE SLICE A RANK, and it does not have to be the same size on every rank: an uneven + * share of the 64-wide tiles is still whole tiles, and an uneven share is the only + * kind available when the width does not divide -- this family's 640 columns are ten + * tiles, which four ranks take as 3, 3, 2, 2. Every tile has exactly one owner, each + * rank writes its own columns of gate/up and reads its own of down, and the block's + * all-reduce sums the partials exactly as it does for the quantised experts and for + * the shared arm. The even case keeps the old declaration verbatim -- no span at all -- + * so a rank count that divides stays byte-identical to the tree this fork started on. */ + psh_row = RAD_SHARD_ROW; + psh_col = RAD_SHARD_COL; + pparts = 2; + if (full % g.world == 0 && (full / g.world) % 64 == 0) { + wp = full / g.world; + } else if (full % 64 || full / 64 < g.world) { fprintf(stderr, "radiance: layer %d's plain experts are %lld wide, which %d ranks " "cannot share in 64-wide tiles\n", l, (long long)full, g.world); return RAD_E_INVAL; + } else { + const int64_t nt = full / 64, base = nt / g.world, extra = nt % g.world; + const int64_t t0 = (int64_t)g.rank * base + std::min(g.rank, extra); + const int64_t t1 = t0 + base + ((int64_t)g.rank < extra ? 1 : 0); + plo = t0 * 64; + phi = t1 * 64; + wp = phi - plo; } - wp = full / g.world; - psh_row = RAD_SHARD_ROW; - psh_col = RAD_SHARD_COL; - pparts = 2; } w_pgu.assign((size_t)np, 0); w_pdn.assign((size_t)np, 0); @@ -1080,6 +1104,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, RAD_BF16, {2 * wp, g.n_embd}, RAD_ACCESS_PER_TOKEN, psh_row, grp_layer(l), 0, parts, pparts); if (!w_pgu[(size_t)j]) return RAD_E_INVAL; + if (phi > 0) RAD_ARCH_TRY(rad_weight_shard_span(b, w_pgu[(size_t)j], plo, phi)); } for (int64_t j = 0; j < np; ++j) { w_pdn[(size_t)j] = decl_w(b, nm.f("blk.%d.ffn_down_exps.%lld.weight", l, @@ -1087,6 +1112,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, RAD_BF16, {g.n_embd, wp}, RAD_ACCESS_PER_TOKEN, psh_col, grp_layer(l)); if (!w_pdn[(size_t)j]) return RAD_E_INVAL; + if (phi > 0) RAD_ARCH_TRY(rad_weight_shard_span(b, w_pdn[(size_t)j], plo, phi)); } /* The gate_up output and the gate's, a sorted row each and in bf16: the rows of the * regular experts are never written and never read. */ diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index c21481ea..5931a7f3 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -2077,6 +2077,65 @@ TEST(flashnext_tp4_replicates_kv_heads_and_splits_expert_widths) { c3.max_tok = 2048; c3.max_seqs = 8; c3.max_spec = 3; c3.max_ctx = 200000; CHECK(qwen4exp_fp8::declare(&b3, &meta, &c3) != RAD_OK); } + + /* ---- THE PLAIN bf16 EXPERTS AT FOUR RANKS. + * + * They are declared beside the quantised ones by a SECOND grouped GEMM, over their own slice of + * the same width, and that slice has its own rule: whole 64-wide output tiles, one range a rank, + * uniform across the protected experts of a layer (their GEMM takes ONE table, so every entry + * must have one geometry -- unlike the quantised experts, they do not alternate by parity). + * The width was read off this rank's parity slices, which at two ranks is the model's width and + * at four is not, so the rank took a number that was not its expert's width, and the check on + * it refused the model on the ranks where that number was wrong -- two of four, which then + * never arrived at the all-reduce's rendezvous and took the whole launch down with it. + * + * Ten tiles over four ranks is 3, 3, 2, 2: an UNEVEN share, still whole tiles, every tile owned + * once. Ranks 0 and 1 compute 192 columns of every protected expert, ranks 2 and 3 compute 128. + * Two protected experts so the even-count rule the parity tables need holds, as the production + * recipe's pairs make it hold. */ + { + const RadEncoding w4p = [] { RadEncoding e = rad_enc_affine(RAD_I4, RAD_BF16, 1, 128); + rad_enc_copy_str(e.transform, "fwht128"); return e; }(); + const RadEncoding plain = rad_enc_plain(RAD_BF16); + /* [lo, hi) of the plain expert's columns, per rank -- literal, for the reason given above. */ + const int64_t p_lo[4] = { 0, 192, 384, 512 }; + const int64_t p_hi[4] = { 192, 384, 512, 640 }; + for (int r = 0; r < world; ++r) { + RadBuilder bp; + bp.encs = { { "blk.1.ffn_gate_up_exps.3.weight", plain }, + { "blk.1.ffn_down_exps.3.weight", plain }, + { "blk.1.ffn_gate_up_exps.10.weight", plain }, + { "blk.1.ffn_down_exps.10.weight", plain }, + { "ffn_gate_up_exps", w4p }, { "ffn_down_exps", w4p } }; + RadBuildCtx cp = build_ctx(r, world); + cp.max_tok = 2048; cp.max_seqs = 8; cp.max_spec = 3; cp.max_ctx = 200000; + REQUIRE_EQ(qwen4exp_fp8::declare(&bp, &meta, &cp), RAD_OK); + const int64_t wp = p_hi[r] - p_lo[r]; + int seen = 0; + for (const RecWeight& w : bp.weights) { + if (w.name.rfind("blk.1.ffn_gate_up_exps.", 0) != 0 && + w.name.rfind("blk.1.ffn_down_exps.", 0) != 0) continue; + if (w.d.dtype != RAD_BF16) continue; /* only the plain ones are in scope */ + if (w.name.compare(w.name.size() - 7, 7, ".weight") != 0) continue; + CHECK(w.span_set); + CHECK_EQ(w.span_lo, p_lo[r]); + CHECK_EQ(w.span_hi, p_hi[r]); + ++seen; + } + /* Both projections of both protected experts. */ + CHECK_EQ(seen, 4); + /* The gate/up GEMM is as wide as this rank's share, doubled for the stacked pair, and + * the down GEMM's K is that share: the partial this rank contributes. */ + int gu_ok = 0, dn_ok = 0; + for (const RecOp& o : bp.ops) { + if (o.op != "moe_gemm") continue; + if (pint(&o, "N") == 2 * wp && pint(&o, "n_expert") == 2) ++gu_ok; + if (pint(&o, "K") == wp && pint(&o, "n_expert") == 2) ++dn_ok; + } + CHECK_EQ(gu_ok, 1); + CHECK_EQ(dn_ok, 1); + } + } } /* ==================================================================== media */