From 140987fa7b0d9a1ec60e19809a70988f711997fe Mon Sep 17 00:00:00 2001 From: Deadcode Date: Sun, 4 Oct 2026 21:59:28 +0200 Subject: [PATCH] radiance 1.0.8 - Qwen3.8-Flash-Next serves at --tp 4, contributed by goodman-b (PR 9). Rank pairs share each of the model's two KV heads, every rank computes a slice of every expert's width, and four ranks run the unfused all-reduce plus a new four-rank all-gather for the vocab-parallel sampler. Two ranks declare and compute exactly as before. The README has a four-rank flag row; it pins --host-pool-mib on every card, 48 GiB in all. At four ranks the expert shares are not equal: ranks 0 and 1 compute three blocks of every even/odd pair of experts and ranks 2 and 3 two (docs/ARCHITECTURES.md). - The four- and eight-rank all-gathers require peer access between the cards, as every other collective does since 1.0.7, and the kernel library's selftest now refuses any multi-rank kernel that does not. --- CMakeLists.txt | 2 +- NOTICE | 5 ++- README.md | 21 +++++----- arch/common/rad_arch.h | 4 +- arch/common/rad_block_attn_gated.h | 2 +- arch/common/rad_block_moe_fp8.h | 4 +- arch/qwen4exp_fp8/qwen4exp_fp8.cpp | 54 ++++++++++--------------- docs/ARCHITECTURES.md | 21 ++++++---- libr4d/r4d_attn_paged_h256_gqa12.hip | 3 +- libr4d/r4d_attn_paged_h256_gqa6_bs4.hip | 3 +- libr4d/r4d_rows.cpp | 2 + libr4d/r4d_selftest.cpp | 22 +++++++++- tests/arch_test.cpp | 6 +-- 13 files changed, 86 insertions(+), 63 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f37c769c..121c5f07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.21) # VERSION is what /version reports and what a client checks compatibility against. It is NOT the # ABI version -- that lives in abi/rad_abi.h and is read out of the header below, because # a plugin contract and a server release do not move together. -project(radiance VERSION 1.0.7 LANGUAGES C CXX) +project(radiance VERSION 1.0.8 LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/NOTICE b/NOTICE index 387ffb55..0b37443e 100644 --- a/NOTICE +++ b/NOTICE @@ -1,8 +1,9 @@ radiance Copyright 2026 Deadcode and the radiance contributors -Written by Deadcode, with contributions from tcclaviger (the N-rank all-reduce kernels) and ggz14 -(the MXFP4 GEMM and the GDN kernels' overflow guards). +Written by Deadcode, with contributions from tcclaviger (the N-rank all-reduce kernels), ggz14 +(the MXFP4 GEMM and the GDN kernels' overflow guards) and goodman-b (four-rank tensor parallelism +for Qwen3.8-Flash-Next). This product includes software from the following projects, each under the MIT License. The license text is in the file named beside each. diff --git a/README.md b/README.md index 0720b6f5..eb744d60 100644 --- a/README.md +++ b/README.md @@ -94,15 +94,18 @@ sequence holds its own recurrent state and 32 of them leave the attention cache keeps the 200K request and costs single-stream decode about 15%) and the 27B in bf16. See [`docs/GUIDE.md`](docs/GUIDE.md) §4.4. -`--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; 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. +`--tp 4` runs the unfused all-reduce chain, because the fused forms are two-rank, and it takes no +`--tp-wire`: `wht6` is a two-rank wire and four ranks run the exact one. It is a capacity +configuration; its per-step rate against `--tp 2` on the same cards has not been measured. The +tiering flags are the tp2 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. **`--host-pool-mib` is pinned on every card**, so the row +above pins 4 × 12 GiB = 48 GiB, and the amdgpu driver caps pinned memory at `ttm.pages_limit`, half +of RAM by default: that row needs 96 GiB of RAM or a raised limit +([`docs/GUIDE.md`](docs/GUIDE.md) §7). Add `--api-key KEY` to require `Authorization: Bearer KEY`. Add `--debug-graph` to print every op and the kernel it resolved to, then exit without serving. Then: diff --git a/arch/common/rad_arch.h b/arch/common/rad_arch.h index 01997a18..6b06bfbf 100644 --- a/arch/common/rad_arch.h +++ b/arch/common/rad_arch.h @@ -782,8 +782,8 @@ inline int geom_from(Geom& g, const RadModelMeta* m, const RadBuildCtx* c, * replaces (Flash-Next: 2 KV heads x 5120 x 256 rows read twice instead of once). What it buys * is that world 4 is declarable at all. * - * When world <= n_head_kv_all the split below is untouched: TP2 stays byte-identical, the - * geometry paro is served on cannot regress. */ + * When world <= n_head_kv_all the split below is untouched, so a world that divides the KV + * heads -- TP2 on this family -- declares exactly as it did before replication existed. */ if (g.world > g.n_head_kv_all) { if (g.world % g.n_head_kv_all != 0) { fprintf(stderr, "radiance: world_size = %d cannot replicate n_head_kv = %lld; " diff --git a/arch/common/rad_block_attn_gated.h b/arch/common/rad_block_attn_gated.h index cb5c04f0..7a488cda 100644 --- a/arch/common/rad_block_attn_gated.h +++ b/arch/common/rad_block_attn_gated.h @@ -103,7 +103,7 @@ inline int AttnGatedBF16::declare(RadBuilder* b, Names& nm, const Geom& geom, in return RAD_E_INVAL; } - /* KV-HEAD REPLICATION IS NOT PLUMBLED HERE. `geom_from` answers a world size above the KV head + /* KV-HEAD REPLICATION IS NOT PLUMBED HERE. `geom_from` answers a world size above the KV head * count by giving every rank ONE kv head and a `kv_off` row span into a projection that stays * `n_head_kv_all * head_dim` rows in the container -- and this block declares its k and v with * the plain even row-shard, which at four ranks would hand a rank half of one KV head. The diff --git a/arch/common/rad_block_moe_fp8.h b/arch/common/rad_block_moe_fp8.h index 36fc010d..481b74cf 100644 --- a/arch/common/rad_block_moe_fp8.h +++ b/arch/common/rad_block_moe_fp8.h @@ -1075,8 +1075,8 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, * 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. */ + * 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; diff --git a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp index 5fbd0b90..393a7b3f 100644 --- a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp +++ b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp @@ -479,31 +479,33 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c g.w_bf16 = mc.expert_bf16 != 0; /* ...and the connections in front of the blocks emit no codes, since nothing reads any. */ m.hccfg.quant = g.w_bf16 ? 0 : 1; - /* ================= THE RANKS EITHER SLICE EVERY EXPERT, OR OWN WHOLE ONES ================= + /* ============================== EVERY RANK HOLDS A SLICE OF EVERY EXPERT ============================== * * An expert of this model is 640 columns, five 128-column blocks -- the fp8 scale block, * the 4-bit plane's scale group and the rotation's width all at once -- so two ranks * cannot halve one. * - * AT TWO RANKS EVERY EXPERT IS SLICED BY PARITY: each rank takes three blocks of the - * experts of one parity and two of the other's -- rank r's slice of expert e is the first - * three blocks when e % 2 == r and the last two otherwise (MoeFP8::Config::ff_lo says why - * by parity). The router stays replicated, every rank computes its slice of every routed - * expert, and the block's all-reduce sums the partials. + * THE SPLIT IS BY PARITY: each parity's five blocks are cut contiguously over the ranks, + * and the rank that takes the extra block alternates with the expert's parity + * (MoeFP8::Config::ff_lo says why by parity). At two ranks, rank r's slice of expert e is + * the first three blocks when e % 2 == r and the last two otherwise. The router stays + * replicated, every rank computes its slice of every routed expert, and the block's + * all-reduce sums the partials. * - * ABOVE TWO RANKS THE RANKS OWN WHOLE EXPERTS: rank r owns [r*E/W, (r+1)*E/W). A width - * that divides into whole blocks at the rank count is still split evenly and has one - * slice, but five blocks cannot be cut into four even whole-block slices, and the parity - * trick is by construction a two-rank rule. So the block's all-reduce sums whole-expert - * partials instead. + * 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 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. * - * THE TRADE IS ROUTING BALANCE FOR A WORKING TP4. The bytes a rank reads are the same on - * average either way, but which rank a routed expert lands on is a coin flip, and a verify - * step's forty routed slots leave one rank about five experts ahead -- which the all-reduce - * turns into the other ranks waiting, about a millisecond a step at one sequence. Split by - * parity the ranks differ by a fifth of an expert per unit of the even/odd difference - * instead. This is correctness-first v1: the follow-up is a 4-way block split, an - * ff_lo[4]/ff_hi[4] generalising the parity tables, which keeps that balance at four. + * 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 + * routed expert lands on is a coin flip, and a verify step's forty routed slots leave one + * rank about five experts ahead -- which the all-reduce turns into the other ranks waiting, + * about a millisecond a step at one sequence. Split by parity the ranks differ by a fifth + * of an expert per unit of the even/odd difference instead. Whole experts would also break + * the protected experts' pairing: the recipe protects them in pairs so each rank's count of + * quantised experts stays even, which the two parity tables need. * * A CALIBRATION RUN SPLITS THE EXPERTS INSTEAD, whole experts per rank. The down * projection's Hessian is over an expert's whole K, and a rank holding a slice of each @@ -522,21 +524,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c mc.n_ff_exp = g.n_ff_all; mc.n_expert = per; mc.expert_base = (int64_t)g.rank * per; - /* WHY this run owns whole experts: whichever of the three reasons applies. The parity - * case says so by the block count, because that is the number that refuses the split. */ - char why[192]; - if (mc.calib) - snprintf(why, sizeof why, - "calibration (each expert's Hessian is over its whole width)"); - else if (mc.expert_bf16) - snprintf(why, sizeof why, "bf16 experts"); - else - snprintf(why, sizeof why, - "%lld blocks of %d split %d ways are not whole blocks, so the ranks own " - "whole experts", (long long)(g.n_ff_all / RAD_FP8_BLOCK), - RAD_FP8_BLOCK, g.world); rad_note(b, "%s: rank %d owns experts [%lld, %lld) of %lld whole.", - why, g.rank, + mc.calib ? "calibration (each expert's Hessian is over its whole width)" + : "bf16 experts", g.rank, (long long)mc.expert_base, (long long)(mc.expert_base + mc.n_expert), (long long)mc.n_expert_all); } else if (g.world > 1) { diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index ef43fe9a..57ea1017 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -299,14 +299,20 @@ three blocks on rank 0 and two on rank 1. 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 +exactly one owner, and each rank still computes a slice of every routed expert. 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. + +**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. 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. @@ -325,8 +331,9 @@ time. **The fused all-reduce forms are two-rank.** At four ranks nothing fuses: the hyper-connection pair runs the standalone sequence — `all_reduce` (libr4d's 4-rank oneshot/twoshot), then `hc_write_bf16`, then `moe_gather` — 2 to 3 extra launches per pair against the two-rank fused -form, and the fused attention ops have no bs4/gqa6 instantiation either. The unfused sequence is -declared beside the probe, so nothing is unresolved at boot. The per-step cost against tp2 on the +form. The unfused sequence is declared beside the probe, so nothing is unresolved at boot. The +attention's own fusion is not a collective and does carry over: the sparse decode with the output +gate and the quantiser in its merge (`attn_paged_gate_quant`) has gqa-6, page-4 rows. The per-step cost against tp2 on the same cards is unmeasured. **One collective does generalise, because it has to.** The lm_head is vocab-sharded, so the sampler diff --git a/libr4d/r4d_attn_paged_h256_gqa12.hip b/libr4d/r4d_attn_paged_h256_gqa12.hip index 068196b2..bf09236f 100644 --- a/libr4d/r4d_attn_paged_h256_gqa12.hip +++ b/libr4d/r4d_attn_paged_h256_gqa12.hip @@ -16,7 +16,8 @@ // unit entirely rather than shipping them untested. #define R4D_ATTN_GQA 12 #define R4D_ATTN_H128 0 -// THE KV PAGE IS FOUR TOKENS HERE, AND IT IS THE ONLY GEOMETRY IN THIS LIBRARY WHERE IT IS NOT 16. +// THE KV PAGE IS FOUR TOKENS HERE, as in the gqa 6 unit that serves the same layers at four +// ranks; every other geometry in this library pages at 16. // // Qwen3.8-Flash-Next's QSA indexer selects 4-TOKEN BLOCKS of the key history. With a page of 4 a // selected block IS a page, so the selection is a paged block table and the sparse attention is diff --git a/libr4d/r4d_attn_paged_h256_gqa6_bs4.hip b/libr4d/r4d_attn_paged_h256_gqa6_bs4.hip index 956f63ec..35662959 100644 --- a/libr4d/r4d_attn_paged_h256_gqa6_bs4.hip +++ b/libr4d/r4d_attn_paged_h256_gqa6_bs4.hip @@ -21,7 +21,8 @@ // points out of this translation unit entirely rather than shipping them untested. #define R4D_ATTN_GQA 6 #define R4D_ATTN_H128 0 -// THE KV PAGE IS FOUR TOKENS HERE, AND IT IS THE ONLY GEOMETRY IN THIS LIBRARY WHERE IT IS NOT 16. +// THE KV PAGE IS FOUR TOKENS HERE, as in the gqa 12 unit; every other geometry in this library +// pages at 16. // // Qwen3.8-Flash-Next's QSA indexer selects 4-TOKEN BLOCKS of the key history. With a page of 4 a // selected block IS a page, so the selection is a paged block table and the sparse attention is diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 0eef10dc..2fda504a 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -2351,9 +2351,11 @@ static const RadConstraint cArGather2[] = { * dtype set is cArGather2's verbatim for the same reason -- the two callers' synonyms. */ static const RadConstraint cArGather4[] = { C_EQ("world_size", 4), C_IN("dtype", "bf16 fp16 f16 fp32 f32"), + C_P2P, }; static const RadConstraint cArGather8[] = { C_EQ("world_size", 8), C_IN("dtype", "bf16 fp16 f16 fp32 f32"), + C_P2P, }; static const RadConstraint cArExact[] = { diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 2f2c9e5c..8c63b82e 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -285,10 +285,10 @@ static void cross_check(const Plugin& pl) { ok("op schema names are unique", dup_schema == 0); ok("kernel names are unique", dup_kernel == 0); - int no_schema = 0, bad_key = 0, no_launch = 0, init_no_fini = 0, bad_domain = 0; + int no_schema = 0, bad_key = 0, no_launch = 0, init_no_fini = 0, bad_domain = 0, no_p2p = 0; int bad_axis = 0, bad_deflt = 0, dup_axis = 0; std::string axisdetail; - std::string keydetail; + std::string keydetail, p2pdetail; for (int k = 0; k < nk; ++k) { const RadKernelInfo* r = pl.kernel_at(k); if (!r->launch) { ++no_launch; continue; } @@ -351,6 +351,23 @@ static void cross_check(const Plugin& pl) { "' which op '" + r->op + "' does not declare; "; } } + /* EVERY MULTI-CARD ROW NEEDS PEER ACCESS. Each kernel in this library that runs only at + * two or more ranks reads or writes the other cards' memory directly, so without + * device.p2p a machine whose cards cannot reach each other resolves it and the launch + * faults or hangs instead of being refused by name at selection. */ + bool multi = false, p2p = false; + for (int c = 0; c < r->n_constraints; ++c) { + const RadConstraint& rc = r->constraints[c]; + if (!std::strcmp(rc.key, "world_size") && + (rc.op == RAD_C_EQ || rc.op == RAD_C_GE) && rc.ival >= 2) + multi = true; + if (!std::strcmp(rc.key, RAD_FACT_P2P) && rc.op == RAD_C_EQ && rc.ival == 1) + p2p = true; + } + if (multi && !p2p) { + ++no_p2p; + p2pdetail += std::string(r->name) + "; "; + } } ok("every row has a launch", no_launch == 0); ok("every row with init has fini", init_no_fini == 0); @@ -360,6 +377,7 @@ static void cross_check(const Plugin& pl) { ok("no row declares the same axis key twice", dup_axis == 0, axisdetail); ok("every row's op has a schema", no_schema == 0, keydetail); ok("every constraint key is a declared parameter of its op", bad_key == 0, keydetail); + ok("every row that needs two or more ranks requires device.p2p", no_p2p == 0, p2pdetail); /* The libr4d orderings the integer priority has to preserve. */ int p_ts = -1, p_os = -1, p_dec = -1, p_pre = -1, p_m16 = -1, p_m64 = -1, p_bt = -1; diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 5931a7f3..39b05b44 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -1832,7 +1832,7 @@ TEST(a_sizing_declare_leaves_the_step_state_alone_and_shrinks_only_rows) { } -/* FOUR RANKS: THE KV HEADS REPLICATE, THE EXPERTS SPLIT WHOLE, THE FUSED COLLECTIVES STOP. +/* FOUR RANKS: THE KV HEADS REPLICATE, THE EXPERT WIDTHS SPLIT, THE FUSED COLLECTIVES STOP. * * This is the tensor-parallel test above's sibling for the other model in the tree, and it exists * because world 4 was not declarable at all: `geom_from` refused any world larger than the KV head @@ -2039,8 +2039,8 @@ TEST(flashnext_tp4_replicates_kv_heads_and_splits_expert_widths) { } /* AT TWO, THE OLD DECLARATION: the KV heads split, so the even shard already selects the right - * rows and NO SPAN is declared at all -- TP2 is byte-identical to the tree this fork started - * from. The fused forms are the tell that world 2 is a different deployment and not the same + * rows and NO SPAN is declared at all -- TP2 declares as it did before replication existed. + * The fused forms are the tell that world 2 is a different deployment and not the same * one stretched: they resolve there and nowhere else. */ { RadBuilder h[2];