TP4 for Qwen3.8-Flash-Next: unfused chain, four refusals answered

World 4 refused at declare on two counts and at boot on a third the audit
found; the fourth is unchanged by design.

- rad_arch.h: a world above the KV head count replicates the heads in rank
  pairs instead of dividing them. Geom gains kv_off; each rank declares
  n_head_kv 1 and gqa 6. A world that is not a multiple of the head count
  is still refused, by name.
- rad_block_attn_gated_fp8.h: k/v pass the row span LinearFP8 already
  takes, gated on world > n_head_kv_all so an even shard is untouched and
  TP2 declares byte-identically.
- qwen4exp_fp8.cpp: above two ranks the ranks own whole experts (5 blocks
  of 128 do not cut four ways); the parity tables stay two-rank and the
  router stays replicated.
- libr4d: attn_{prefill,decode}_h256_gqa6_bs4 rows and their TU -- the
  gqa8 body at ratio 6, page 4. No fused form is instantiated at this
  geometry; the unfused pair is what resolves.
- libr4d: r4d_ar_gather_Nrank.hip. The vocab-parallel sampler declares an
  all_gather at any world > 1 and libr4d shipped only the 2-rank row, so a
  TP4 boot was refused by the reference-kernel gate rather than served
  wrong. The wide one-shot all-reduce with the reduction removed.
- rad_block_attn_gated.h / rad_block_attn_fp8.h: refuse a world above the
  KV head count. They read n_head_kv and declare no span, so they would
  have booted and read half a KV head -- fluent wrong text.
- tests/arch_test.cpp: the four-rank declare test, and the fake records
  the shard span the real builder stores.
- README, ARCHITECTURES: TP4 in the present tense, with the costs named
  and the rate against TP2 left unmeasured.
This commit is contained in:
picoder authored and StillDeadcode committed 2026-10-04 21:47:44 +02:00
1 parent 966bda899a
commit 7cf4c005e1
14 files changed
+926 -31

No files matched your search

+38 -4
View File
@@ -570,6 +570,12 @@ struct Geom {
int64_t n_head_kv = 0; /* per rank */
int64_t n_head_all = 0;
int64_t n_head_kv_all = 0;
/* THIS RANK'S FIRST KV ROW inside the checkpoint's UNSHARDED k/v projection rows, used only
* when the KV heads are replicated over ranks instead of split (world > n_head_kv_all, see
* geom_from). It is in container rows, the same unit a LinearFP8 span takes, so a block that
* declares a row-sharded k/v passes it straight through as `span_lo`. Zero on every split
* deployment, which is what keeps TP2 reading the projection exactly as it always did. */
int64_t kv_off = 0;
int64_t n_ff = 0; /* per rank */
int64_t n_ff_all = 0;
int64_t max_tok = 0;
@@ -760,10 +766,38 @@ inline int geom_from(Geom& g, const RadModelMeta* m, const RadBuildCtx* c,
int s;
if ((s = divide_or_fail(g.n_head_all, g.world, "n_head", &g.n_head)) < 0) return s;
/* A world size larger than n_head_kv would need the KV heads replicated across ranks rather
* than split, which is a different attention declaration and not a rounding decision. Refuse
* it here rather than have the block quietly produce a zero-width KV projection. */
if ((s = divide_or_fail(g.n_head_kv_all, g.world, "n_head_kv", &g.n_head_kv)) < 0) return s;
/* THE KV HEADS ARE REPLICATED WHEN THERE ARE NOT ENOUGH TO SPLIT. There is one row-span per KV
* head and no way to hand four ranks half a head, so `divide_or_fail` on n_head_kv is the
* wrong verb once world exceeds the head count -- the question is not rounding, it is whether
* the declaration is allowed to say one KV head per rank. It is, when world is a multiple of
* the KV head count: query heads 0-11 belong to KV head 0 and 12-23 to KV head 1, so ranks
* {0,1} need only KV head 0 and ranks {2,3} only KV head 1. Each rank then declares
* n_head_kv 1 and reads its own head's row-span of k/v, which `kv_off` locates; the attention
* declaration comes out at gqa 6 over a page of 4 instead of gqa 12 over a page of 2, and
* kv_dim(), gqa() and every block that reads them need no new branch because from here down a
* replicated rank and a split rank look the same.
*
* THE COST IS HONEST AND IT IS MEMORY, NOT MATH: the replicating ranks hold BYTE-IDENTICAL KV
* weights, a replicated read that costs no extra VRAM per rank versus the TP2 split it
* 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. */
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; "
"replication needs world to be a multiple of the KV head count\n",
g.world, (long long)g.n_head_kv_all);
return RAD_E_INVAL;
}
g.n_head_kv = 1; /* world / n_head_kv_all ranks share each KV head */
g.kv_off = (g.rank / (g.world / g.n_head_kv_all)) * g.head_dim;
} else if ((s = divide_or_fail(g.n_head_kv_all, g.world, "n_head_kv", &g.n_head_kv)) < 0) {
return s;
} else {
g.kv_off = 0; /* a split rank reads the rows the plain row-shard already gives it */
}
if ((s = divide_or_fail(g.n_ff_all, g.world, "n_ff", &g.n_ff)) < 0) return s;
/* THE VOCABULARY IS SPLIT. r4d_sample_topk_f32 takes a vocabulary offset and packs its
* candidates as (u32 GLOBAL id, f32 logit) pairs, and the all-gather places every rank's
+11
View File
@@ -99,6 +99,17 @@ inline int AttnFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, r
return RAD_E_INVAL;
}
/* NO KV-HEAD REPLICATION HERE. The fused [q|k|v] row-shards by head with the plain even shard,
* so above the KV head count a rank would get a slice of one KV head and declare fine while
* reading the wrong rows. rad_block_attn_gated.h refuses for the same reason; the block that
* plumbs geom_from's `kv_off` is rad_block_attn_gated_fp8.h. */
if (g.world > g.n_head_kv_all) {
fprintf(stderr, "radiance: %s declares no KV row span and cannot serve world_size = %d "
"with n_head_kv = %lld.\n", __func__, g.world,
(long long)g.n_head_kv_all);
return RAD_E_UNSUPPORTED;
}
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.attn_norm.weight", l), src.norm));
w_norm = decl_w(b, nm.f("blk.%d.attn_norm.weight", l), RAD_F32, {g.n_embd},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
+15
View File
@@ -103,6 +103,21 @@ 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
* 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
* shapes would declare, the numbers would be wrong, and the text would be fluent. Refuse by
* name instead: rad_block_attn_gated_fp8.h is the block that passes the span, and a bf16 model
* that needs TP4 plumbs it the same way. */
if (g.world > g.n_head_kv_all) {
fprintf(stderr, "radiance: %s declares no KV row span and cannot serve world_size = %d "
"with n_head_kv = %lld; the KV-head replication in geom_from needs the "
"span passed to its k/v weights.\n", __func__, g.world,
(long long)g.n_head_kv_all);
return RAD_E_UNSUPPORTED;
}
/* ---- weights. Shapes are [out, in]: gemm_nt is A[M,K] times B transposed [N,K], the layout
* every transformer weight already has, so nothing is transposed at load.
*
+20 -2
View File
@@ -180,6 +180,22 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
const int64_t kv_dim = g.kv_dim();
const int64_t qg_dim = 2 * q_dim;
/* THE KV ROW SPAN, AND IT ONLY EXISTS ABOVE THE KV HEAD COUNT.
* A world size larger than `n_head_kv_all` cannot divide the KV heads, so rad_arch.h's
* `geom_from` replicates them in PAIRS: two ranks serve the same head, `g.n_head_kv` is 1 on
* each, and the checkpoint entry they both read is still `n_head_kv_all * head_dim` rows -- 512
* for 2 KV heads at head_dim 256. This rank wants ONE head's row-span of that entry, which the
* even shard would cut up instead (at 4 ranks it would hand rank 2 rows [256, 384), half of one
* head), so the span `[g.kv_off, g.kv_off + kv_dim)` is declared in the CONTAINER's unsharded
* units -- [0, 256) on ranks 0 and 1, [256, 512) on ranks 2 and 3 -- and the scale plane takes
* the same span in 128-blocks. `kv_off` is a multiple of head_dim, so it is whole blocks and
* never cuts one, and share.cpp's `shard_hi <= cpart` holds with cpart = 512 / 1 part.
* AT WORLD <= n_head_kv_all the ordinary even shard already selects exactly these rows, so the
* span stays 0/0 and the default path runs as before -- TP2 declares identically. */
const bool kv_spanned = g.world > g.n_head_kv_all;
const int64_t kv_span_lo = kv_spanned ? g.kv_off : 0;
const int64_t kv_span_hi = kv_spanned ? g.kv_off + kv_dim : 0;
if (n_rot <= 0 || n_rot > g.head_dim || (n_rot & 1)) {
fprintf(stderr, "radiance: rotary_dim %lld is not an even width inside head_dim %lld\n",
(long long)n_rot, (long long)g.head_dim);
@@ -221,9 +237,11 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
RAD_ARCH_TRY(qg.declare(b, nm, g, nm.f("blk.%d.attn_qg", l), qg_dim, g.n_embd,
RAD_SHARD_ROW, grp_layer(l), { src.qg }, w.h, w.qg));
RAD_ARCH_TRY(kp.declare(b, nm, g, nm.f("blk.%d.attn_k", l), kv_dim, g.n_embd,
RAD_SHARD_ROW, grp_layer(l), { src.k }, w.h, w.k));
RAD_SHARD_ROW, grp_layer(l), { src.k }, w.h, w.k,
0, {}, kv_span_lo, kv_span_hi));
RAD_ARCH_TRY(vp.declare(b, nm, g, nm.f("blk.%d.attn_v", l), kv_dim, g.n_embd,
RAD_SHARD_ROW, grp_layer(l), { src.v }, w.h, w.v));
RAD_SHARD_ROW, grp_layer(l), { src.v }, w.h, w.v,
0, {}, kv_span_lo, kv_span_hi));
/* THE FUSED PROLOGUE, AND IT IS A PROBE: the four ops below are declared either way and are
* what runs when nothing serves this one. rad_block_attn_gated.h does not make the probe,
+35 -18
View File
@@ -479,26 +479,31 @@ 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;
/* ============================== EVERY RANK HOLDS A SLICE OF EVERY EXPERT ==============================
/* ================= THE RANKS EITHER SLICE EVERY EXPERT, OR OWN WHOLE ONES =================
*
* 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. 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.
* cannot halve one.
*
* 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 rank 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.
* 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.
*
* A width that divides into whole blocks at the rank count is split evenly and has one
* slice; five blocks at more than two ranks would need more slices than two parities, and
* is refused by name.
* 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.
*
* 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.
*
* 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
@@ -510,16 +515,28 @@ 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 > 1 && (mc.calib || mc.expert_bf16)) {
if (g.world > 2 || (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;
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.",
mc.calib ? "calibration (each expert's Hessian is over its whole width)"
: "bf16 experts", g.rank,
why, 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) {