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