four-rank Flash-Next: the shared and plain experts' remainders go to ranks 2 and 3

At four ranks the routed parity split leaves ranks 0 and 1 widest, and the
shared expert's extra block and the plain bf16 experts' extra tiles were cut
from rank 0 as well. Both remainders now go to the ranks the routed split
leaves narrowest. One and two ranks declare byte for byte as before.
This commit is contained in:
keptn42 authored and StillDeadcode committed 2026-10-05 12:52:44 +02:00
1 parent bb4dddc424
commit bfae23e368
4 files changed
+82 -31

No files matched your search

+17 -7
View File
@@ -183,6 +183,12 @@ struct MoeFP8 {
* The arm's stages run behind the routed ones on the same queue (see pass()): a routed
* GEMM's tail leaves compute units idle, and the arm fills them. */
int64_t shared_lo = 0, shared_hi = 0;
/* THIS RANK'S PLACE WHERE THE PLAIN EXPERTS' 64-WIDE TILES DO NOT DIVIDE over the ranks:
* the first places take one tile more. -1 is the rank's own index. A permutation of the
* ranks, the same on every rank, so every tile still has exactly one owner; the
* architecture sets it above two ranks to keep the extra tiles off the ranks that already
* take the routed experts' extra blocks. */
int64_t extra_place = -1;
int norm_topk = 1; /* renormalise the kept routing weights to sum to one */
/* THE EXPERT WEIGHT AT FOUR BITS rather than E4M3, which for Qwen3.8-Flash-Next is the
* difference between a model that fits two cards and one that does not (the registry row
@@ -1072,11 +1078,12 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
/* 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 even-split declaration verbatim -- no span
* at all -- so a rank count that divides declares exactly as it did before. */
* tiles, which four ranks take as 2, 2, 3, 3 (Config::extra_place puts ranks 2 and 3
* first, the ranks without a routed extra block). 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 even-split declaration verbatim --
* no span at all -- so a rank count that divides declares exactly as it did before. */
psh_row = RAD_SHARD_ROW;
psh_col = RAD_SHARD_COL;
pparts = 2;
@@ -1088,8 +1095,11 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
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<int64_t>(g.rank, extra);
const int64_t t1 = t0 + base + ((int64_t)g.rank < extra ? 1 : 0);
/* Cut by the rank's place (Config::extra_place), not necessarily its index. */
const int64_t pr = c.extra_place >= 0 ? c.extra_place : (int64_t)g.rank;
if (pr >= g.world) return RAD_E_INVAL;
const int64_t t0 = pr * base + std::min<int64_t>(pr, extra);
const int64_t t1 = t0 + base + (pr < extra ? 1 : 0);
plo = t0 * 64;
phi = t1 * 64;
wp = phi - plo;
+34 -4
View File
@@ -292,6 +292,28 @@ static int64_t ngram_table_rows(int64_t base, int64_t heads, int64_t divisor) {
return divisor > 1 ? ((total + divisor - 1) / divisor) * divisor : total;
}
/* A RANK'S PLACE WHEN A WIDTH THAT DOES NOT DIVIDE IS CUT OVER THE RANKS, the first places taking
* one unit more: the ranks in order of how many 128-column blocks of an even/odd pair of routed
* experts declare()'s parity split gives each, fewest first, by rank among equals. At four ranks
* and five blocks that is ranks 2, 3, 0, 1 -- ranks 0 and 1 hold the routed extra blocks, and
* cutting the shared expert and the plain experts from rank 0 as well stacks their extra columns on
* the ranks every block's all-reduce already waits for. Where the routed width divides it is the
* rank; declare() asks only above two ranks. */
static int64_t narrow_place(int64_t n_ff_all, int world, int rank) {
const int64_t nb = n_ff_all / RAD_FP8_BLOCK;
if (n_ff_all % RAD_FP8_BLOCK || nb < world || nb % world == 0) return rank;
auto blocks = [&](int r) {
int64_t n = 0;
for (int q = 0; q < 2; ++q)
n += nb / world + ((r - q + world) % world < nb % world ? 1 : 0);
return n;
};
int64_t place = 0;
for (int r = 0; r < world; ++r)
if (blocks(r) < blocks(rank) || (blocks(r) == blocks(rank) && r < rank)) ++place;
return place;
}
static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx) {
if (!b || !meta || !ctx) return RAD_E_INVAL;
if (ctx->rank < 0 || ctx->rank >= MAX_RANKS) return RAD_E_INVAL;
@@ -405,9 +427,15 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
mc.n_ff_exp = g.n_ff;
mc.n_ff_shared = meta_int2(meta, "qwen4exp.expert_shared_feed_forward_length",
"shared_expert_intermediate_size", 0);
/* This rank's place where a remainder is handed out: the rank itself at one or two ranks,
* and above that the narrow ranks first (narrow_place). The plain experts' tile split in
* MoeFP8 takes the same place. */
const int64_t place = g.world > 2 ? narrow_place(g.n_ff_all, g.world, g.rank) : g.rank;
mc.extra_place = g.world > 2 ? place : -1;
/* THE SHARED ARM IS SPLIT BY WHOLE fp8 SCALE BLOCKS: evenly when its width halves into
* them, and otherwise as evenly as whole blocks allow -- this family's 640 columns are
* five blocks, three on rank 0 and two on rank 1. See MoeFP8::Config::shared_lo. */
* five blocks, three on rank 0 and two on rank 1; at four ranks two on rank 2 and one on
* each of the others. See MoeFP8::Config::shared_lo. */
if (mc.n_ff_shared > 0 && g.world > 1 && (mc.n_ff_shared / g.world) % RAD_FP8_BLOCK == 0) {
int s;
if ((s = divide_or_fail(mc.n_ff_shared, g.world,
@@ -423,8 +451,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
}
const int64_t nb = mc.n_ff_shared / RAD_FP8_BLOCK;
const int64_t base = nb / g.world, extra = nb % g.world;
const int64_t b0 = (int64_t)g.rank * base + std::min<int64_t>(g.rank, extra);
const int64_t b1 = b0 + base + ((int64_t)g.rank < extra ? 1 : 0);
const int64_t b0 = place * base + std::min<int64_t>(place, extra);
const int64_t b1 = b0 + base + (place < extra ? 1 : 0);
mc.shared_lo = b0 * RAD_FP8_BLOCK;
mc.shared_hi = b1 * RAD_FP8_BLOCK;
mc.n_ff_shared = mc.shared_hi - mc.shared_lo;
@@ -496,7 +524,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
* go, so ranks 0 and 1 compute three blocks of every even/odd pair of experts and ranks 2
* and 3 compute two, and the all-reduce has ranks 2 and 3 wait for the other two. Equal
* shares need the extra block rotated over four places -- the expert's number mod 4, four
* slice tables where moe_gemm_q takes two.
* slice tables where moe_gemm_q takes two. What can move without them does: the shared
* expert's extra block and the plain experts' extra tiles go to ranks 2 and 3
* (narrow_place), so ranks 0 and 1 take one shared block and two plain tiles.
*
* WHY NOT EXPERT PARALLELISM, which splits the EXPERTS: rank r owns [r*E/W, (r+1)*E/W)
* whole. The bytes a rank reads are the same on average either way, but which rank a
+6 -5
View File
@@ -305,14 +305,15 @@ rank 2, `[4, 5)` on rank 3, and each rank's place on the odd parity shifts by on
fat block there instead of rank 0 — so no rank takes the extra block of both parities. A world
above the block count is refused at declare, by name: a rank with an empty slice computes nothing
and still joins the all-reduce. The shared expert's 640 columns split as evenly as whole blocks
allow: five blocks over four ranks is rank 0 `[0, 256)` and one block each on ranks 1, 2 and 3.
allow: five blocks over four ranks is rank 2 `[0, 256)` and one block each on ranks 3, 0 and 1.
**At four ranks the shares are not equal.** Parity gives the extra block only two places to go, so
ranks 0 and 1 compute three blocks of every even/odd pair of routed experts and ranks 2 and 3
compute two; the shared expert's extra block is rank 0's, and the protected bf16 experts' ten
64-wide tiles go 3, 3, 2, 2. Ranks 2 and 3 wait for the other two at every block's all-reduce.
Equal shares need the extra block rotated by the expert's number mod 4: four slice tables where
`moe_gemm_q` takes two.
compute two. What does not divide elsewhere is cut from the other end: the shared expert's extra
block is rank 2's, and the protected bf16 experts' ten 64-wide tiles go 2, 2, 3, 3. That takes one
shared block off rank 0, the slowest rank, and leaves ranks 0 and 1 still a routed block per
even/odd pair ahead of ranks 2 and 3 at every block's all-reduce. Equal shares need the extra block
rotated by the expert's number mod 4: four slice tables where `moe_gemm_q` takes two.
Expert parallelism — whole experts a rank — is what this replaced, and why it stays replaced: the
recipe protects its bf16 experts in pairs precisely so each rank's quantised count stays even, and
sharding experts by rank breaks that parity property the weight tables rest on.
+25 -15
View File
@@ -2022,20 +2022,29 @@ TEST(flashnext_tp4_replicates_kv_heads_and_splits_expert_widths) {
CHECK_EQ(join(op_names(b[r])), join(op_names(b[0])));
}
/* THE SHARED ARM IS SPLIT BY WHOLE BLOCKS AND IS UNEVEN AT FOUR: 640 is five blocks of 128, so
* the rank taking the extra block is rank 0 -- [0,256) [256,384) [384,512) [512,640) in the
* model's column numbering, the same rule that gives rank 0 three of five blocks at two ranks
* (qwen4exp_fp8.cpp's uneven-block branch). This is the one world-4 span that is NOT a KV head. */
/* THE SHARED ARM IS SPLIT BY WHOLE BLOCKS AND IS UNEVEN AT FOUR: 640 is five blocks of 128, and
* the rank taking the extra block is rank 2, the first rank the routed split gives no extra
* block (qwen4exp_fp8.cpp's narrow_place) -- ranks 2, 3, 0, 1 cut [0,256) [256,384) [384,512)
* [512,640) in the model's column numbering. At two ranks rank 0 still takes three of five.
* This is the one world-4 span that is NOT a KV head. */
const int64_t sh_lo[] = { 384, 512, 0, 256 };
const int64_t sh_hi[] = { 512, 640, 256, 384 };
for (int r = 0; r < world; ++r) {
const RecWeight* sg = find_w(b[r], "blk.0.ffn_gate_up_shexp.weight");
REQUIRE(sg != nullptr);
CHECK(sg->span_set);
const int64_t want_lo[] = { 0, 256, 384, 512 };
const int64_t want_hi[] = { 256, 384, 512, 640 };
CHECK_EQ(sg->span_lo, want_lo[r]);
CHECK_EQ(sg->span_hi, want_hi[r]);
CHECK_EQ(sg->d.shape[0], 2 * (want_hi[r] - want_lo[r])); /* gate's rows and up's */
if (r) CHECK_EQ(sg->span_lo, find_w(b[r - 1], "blk.0.ffn_gate_up_shexp.weight")->span_hi);
CHECK_EQ(sg->span_lo, sh_lo[r]);
CHECK_EQ(sg->span_hi, sh_hi[r]);
CHECK_EQ(sg->d.shape[0], 2 * (sh_hi[r] - sh_lo[r])); /* gate's rows and up's */
}
/* Every block of the shared expert has exactly one owner, read off the declared spans. */
for (int64_t i = 0; i < 640; i += 128) {
int owners = 0;
for (int r = 0; r < world; ++r) {
const RecWeight* sg = find_w(b[r], "blk.0.ffn_gate_up_shexp.weight");
owners += sg->span_lo <= i && i < sg->span_hi;
}
CHECK_EQ(owners, 1);
}
/* AT TWO, THE OLD DECLARATION: the KV heads split, so the even shard already selects the right
@@ -2089,17 +2098,18 @@ TEST(flashnext_tp4_replicates_kv_heads_and_splits_expert_widths) {
* 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
* Ten tiles over four ranks is 2, 2, 3, 3: an UNEVEN share, still whole tiles, every tile owned
* once. Ranks 2 and 3 compute 192 columns of every protected expert and ranks 0 and 1 compute
* 128, because ranks 0 and 1 take the routed experts' extra blocks (narrow_place). 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 };
const int64_t p_lo[4] = { 384, 512, 0, 192 };
const int64_t p_hi[4] = { 512, 640, 192, 384 };
for (int r = 0; r < world; ++r) {
RadBuilder bp;
bp.encs = { { "blk.1.ffn_gate_up_exps.3.weight", plain },