From 10f3d6875b007beed5a0df492ae94d7c2b4ee567 Mon Sep 17 00:00:00 2001 From: picoder Date: Sun, 4 Oct 2026 13:55:24 +0000 Subject: [PATCH] qwen4exp at four ranks: slice the width by parity, do not shard experts The real-container declare gate refused this container at layers 46-47: 'keeps 1 experts plain bf16 on this rank, which leaves 127 quantised'. Root cause: protected-expert counts are per-rank, and the recipe protects IN PAIRS precisely so that each rank's quantised count stays even. Expert parallelism broke that property the parity tables rest on; fixing it properly would need unequal-length weight tables, which the builder's equal-split rule refuses by design. The width split was always the right answer above two ranks and serves more ranks than the old code realised. Each parity's blocks are cut contiguously across the ranks and the rank taking the odd block alternates with the parity: at four ranks and five blocks, [0,2) [2,3) [3,4) [4,5), with the fat class rotating between the two parities so no rank takes both. Every block of every expert has exactly one owner, protected counts go back to the recipe's global even numbers, and -- because every rank still computes a slice of every routed expert -- the routing-balance cost the EP design carried disappears with it. World 2 is byte-identical: base, extra and the parity rotation reduce to the same expressions at g.world == 2. Worlds up to the block count now declare; beyond it the refusal names the reason. --- README.md | 8 +++++--- arch/qwen4exp_fp8/qwen4exp_fp8.cpp | 30 +++++++++++++++++++++++++----- docs/ARCHITECTURES.md | 25 +++++++++++++++---------- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f18fcb0d..0720b6f5 100644 --- a/README.md +++ b/README.md @@ -97,9 +97,11 @@ keeps the 200K request and costs single-stream decode about 15%) and the 27B in `--tp 4` runs the unfused all-reduce chain, so it is slower per step than `--tp 2` on the same cards (unmeasured) — it is a capacity configuration, not a speed one, and it takes no `--tp-wire`: `wht6` is a two-rank wire and four ranks run the exact one. The tiering flags above are the tp2 -recipe carried over, and at four ranks a rank owns a quarter of the experts WHOLE instead of half -the width of every one, so the resident expert bytes per rank are smaller and the elastic window -is a different shape: read the boot log's `elastic` line and re-derive `--expert-vs-cache-ratio` +recipe carried over; above two ranks each rank still owns a **slice of every expert's width** — +each parity's 128-column blocks are cut contiguously over the ranks and the odd block alternates +with the parity, so every rank's share is whole blocks and every block has exactly one owner — +and the resident expert bytes per rank are smaller, so the elastic window is a +different shape: read the boot log's `elastic` line and re-derive `--expert-vs-cache-ratio` and `--host-pool-mib` before trusting them. Add `--api-key KEY` to require `Authorization: Bearer KEY`. Add `--debug-graph` to print every op diff --git a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp index 47133651..af39667d 100644 --- a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp +++ b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp @@ -515,7 +515,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c * is two row ranges and a column range of the container, which the file tier cannot read * as one run, and nothing about bf16 needs the parity split's balance -- a bf16 model is * served to calibrate and to compare, not for its decode rate. */ - if (g.world > 2 || (g.world > 1 && (mc.calib || mc.expert_bf16))) { + if (g.world > 1 && (mc.calib || mc.expert_bf16)) { int s; int64_t per = 0; if ((s = divide_or_fail(mc.n_expert_all, g.world, "expert_count", &per)) < 0) return s; @@ -549,11 +549,19 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c const int64_t nb = g.n_ff_all / RAD_FP8_BLOCK; if (nb % g.world == 0) { mc.n_ff_exp = g.n_ff_all / g.world; - } else if (g.world == 2) { - const int64_t base = nb / 2, extra = nb % 2; + } else if (g.world <= nb) { + /* The uneven split is TWO SLICES, not two RANKS: each parity's blocks are cut + * contiguously across the ranks and the rank taking the odd block alternates with + * the parity, so every rank's slices are whole blocks and every block of every + * expert has exactly one owner. At two ranks this is the split the tree has always + * made, byte for byte; at four it gives widths of ceil(nb/W) and floor(nb/W). + * A rank must come away with at least one block of each parity -- an empty slice + * is a rank that computes nothing and still joins the all-reduce -- which needs + * the ranks to fit inside the two parities' blocks together. */ + const int64_t base = nb / g.world, extra = nb % g.world; for (int q = 0; q < 2; ++q) { /* This rank's place in expert parity q's split: first for its own parity. */ - const int64_t p = (g.rank - q + 2) % 2; + const int64_t p = (g.rank - q + g.world) % g.world; const int64_t b0 = p * base + std::min(p, extra); const int64_t b1 = b0 + base + (p < extra ? 1 : 0); mc.ff_lo[q] = b0 * RAD_FP8_BLOCK; @@ -562,11 +570,23 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c mc.n_ff_exp = std::max(mc.ff_hi[0] - mc.ff_lo[0], mc.ff_hi[1] - mc.ff_lo[1]); rad_note(b, "routed experts: rank %d computes columns [%lld, %lld) of each even " "expert and [%lld, %lld) of each odd one, of %lld. The width is %lld " - "blocks of %d, which two ranks split unevenly, so the rank taking the " + "blocks of %d, which %lld ranks split unevenly, so the rank taking the " "extra block alternates with the expert's parity; the block's " "all-reduce sums the partials.", g.rank, (long long)mc.ff_lo[0], (long long)mc.ff_hi[0], (long long)mc.ff_lo[1], (long long)mc.ff_hi[1], (long long)g.n_ff_all, + (long long)nb, RAD_FP8_BLOCK, (long long)g.world); + } else { + fprintf(stderr, "radiance: qwen4exp: an expert is %lld blocks of %d, which %d ranks " + "cannot each take a whole block of in both parities. Lower the rank " + "count, or serve whole experts (a bf16 container or a calibration " + "run does).\n", + (long long)nb, RAD_FP8_BLOCK, g.world); + return RAD_E_INVAL; + } + } + g.rank, (long long)mc.ff_lo[0], (long long)mc.ff_hi[0], + (long long)mc.ff_lo[1], (long long)mc.ff_hi[1], (long long)g.n_ff_all, (long long)nb, RAD_FP8_BLOCK); } else { fprintf(stderr, "radiance: qwen4exp: an expert is %lld blocks of %d, which %d ranks " diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index 1e3a15e8..ef43fe9a 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -295,16 +295,21 @@ its slice of every routed expert, and the block's existing end-of-block all-redu partials. No all-to-all, no extra collective. The shared expert's 640 columns split the same way, three blocks on rank 0 and two on rank 1. -**Five blocks is why that split stops at two ranks.** 5 is odd, so no world above 2 cuts it into -equal whole blocks — at tp4 an even share is 160 columns, 1.25 blocks. Above two ranks the routed -experts split by expert instead: each rank owns 128 whole experts of the 512, reads them through the -same `n_ff_exp` 640, the router stays replicated so every rank agrees on the routing, and the same -end-of-block all-reduce sums the partials. A slot whose expert is not local is dropped by -`moe_scatter`, which is how the split is expressed; the parity tables stay two-rank. The shared -expert's 640 columns take the uneven-block branch as they always did: five blocks over four ranks -is rank 0 `[0, 256)` and ranks 1, 2 and 3 one block each. The known cost is expert-selection imbalance — a token's top-10 lands -locally 2.5 times out of 10 — and removing it is exactly what the parity split buys at two ranks. -A whole-block width split is the follow-up. +**Five blocks do not divide evenly, and an even share is not required.** 5 is odd, so at tp4 an +even share is 160 columns, 1.25 blocks — but the split is TWO SLICES per expert, not one slice a +rank: each parity's blocks are cut contiguously across the ranks and the rank taking the odd block +alternates with the parity, so every rank's share is whole blocks, every block of every expert has +exactly one owner, and each rank still computes a slice of every routed expert — the balance the +parity tables buy at two ranks survives at any world. At four ranks and five blocks an even-parity +expert is `[0, 2)` blocks on rank 0, `[2, 3)` on rank 1, `[3, 4)` on rank 2, `[4, 5)` on rank 3, +and each rank's place on the odd parity shifts by one — rank 1 takes the 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. +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. **Attention splits by KV head, and replicates once the ranks outrun the heads.** There are 2 KV heads of 256 rows, so each rank declares `n_head_kv 1` and reads one head's row-span of `k_proj` and