any world size: the attention world, uneven delta-net heads, nothing padded

A world the KV head count neither divides nor is a multiple of -- three
ranks over Flash-Next's two -- was refused by geom_from, and 1.0.12
served it by padding the checkpoint offline (docs/TP3.md): zero heads
appended so that 36 / 3 / 18 / 54 divide, a second 115 GiB container
per model, 1/3 of the attention and 2 of 18 delta-net heads computing
zeros. This serves the shipped container at any world size instead.

The attention world. geom_from serves attention on the largest multiple
of the KV head count under the world (split, or replicated as 1.0.7
does when that is still above the count) and the ranks past it are
attention-zero: they declare the same heads and the same program as
their peers -- the scheduler sees one program -- and read nothing for
attn_qg, attn_k, attn_v and attn_output (rad_weight_shard_zero). Their
attention computes zeros from zeros and their output projection adds
exactly nothing to the reduce. Three ranks over two heads: attention on
ranks 0 and 1 exactly as the two-rank serve declares it, 12 heads, one
KV head, gqa 12, the same kernels; rank 2 zero. Five over two:
attention on four, rank 4 zero. Under a smaller attention world the even
shard over `world` is the wrong cut for every projection, so
rad_block_attn_gated_fp8.h declares spans -- q|gate rows and output
columns [attn_rank * n_head, +n_head) heads, k/v at kv_off -- the same
call the KV replication already used. When the attention world is the
world, every TP2 and TP4 deployment, nothing here is declared and the
block declares as it always did.

Uneven delta-net heads. The delta net has no KV-head constraint: its
kernels are bound on head widths and take any head count. 16 K heads
over three ranks are 6, 5 and 5, each rank's V heads the same multiple
(48 / 16 = 3: 18, 15, 15), every rank real heads. qwen4exp computes the
split and rad_block_gdn_fp8.h declares it as spans of the container's
rows: per-part for [q|k|v|z] and the conv channels (the parts are not
equal), one span for [a|b], A_log, dt_bias and the output columns.
LinearFP8 owns the scale plane's units (1/128 of the weight's rows for
block-fp8, the weight's own for int8 and bf16), so a block declares in
the weight's rows and the scale plane follows. The even split, when the
counts divide, is untouched.

The log says what the geometry did:
  world_size = 3 over n_head_kv = 2: attention on 2 rank(s), rank(s)
  2..2 carry zero attention weights and the delta net and experts of a
  full rank
  qwen4exp: rank 1 takes delta-net K heads [6, 11) of 16 and V heads
  [18, 33) of 48
This commit is contained in:
baKon authored and StillDeadcode committed 2026-10-06 11:23:06 +02:00
1 parent 82445ca71f
commit 45963012ee
5 files changed
+155 -15

No files matched your search

+40 -8
View File
@@ -576,6 +576,17 @@ struct Geom {
* 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;
/* THE ATTENTION WORLD. A world size the KV head count neither divides nor is a multiple of
* -- three ranks over Flash-Next's two KV heads -- serves attention on the largest multiple
* of the head count below it (`attn_world`, here 2: split, or replicated when that is still
* above the count) and the remaining ranks are ATTENTION-ZERO: they declare the same heads and
* the same program as their peers, read nothing for the attention projections
* (rad_weight_shard_zero) and add exactly zero to the reduce. `attn_rank` is this rank's
* index inside the attention world (an attention-zero rank clamps to the last). When
* attn_world == world nothing here is used and every block declares as it always did. */
int attn_world = 1;
int attn_rank = 0;
bool attn_zero = false;
int64_t n_ff = 0; /* per rank */
int64_t n_ff_all = 0;
int64_t max_tok = 0;
@@ -786,7 +797,23 @@ 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;
/* THE ATTENTION WORLD (see the field). Below the KV head count, or a multiple of it, it is the
* world and the two rules that follow run on the world as before. Otherwise it is the largest
* multiple of the head count under the world, and the ranks past it carry zero attention. */
g.attn_world = g.world;
g.attn_rank = g.rank;
g.attn_zero = false;
if (g.world > g.n_head_kv_all && g.world % g.n_head_kv_all != 0) {
g.attn_world = (int)(g.n_head_kv_all * (g.world / g.n_head_kv_all));
g.attn_zero = g.rank >= g.attn_world;
g.attn_rank = g.attn_zero ? g.attn_world - 1 : g.rank;
fprintf(stderr, "radiance: world_size = %d over n_head_kv = %lld: attention on %d rank(s), "
"rank(s) %d..%d carry zero attention weights and the delta net and "
"experts of a full rank\n", g.world, (long long)g.n_head_kv_all,
g.attn_world, g.attn_world, g.world - 1);
}
const int aw = g.attn_world, ar = g.attn_rank;
if ((s = divide_or_fail(g.n_head_all, aw, "n_head", &g.n_head)) < 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
@@ -805,19 +832,24 @@ inline int geom_from(Geom& g, const RadModelMeta* m, const RadBuildCtx* c,
*
* 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) {
if (aw > g.n_head_kv_all) {
/* aw is a multiple of the head count by construction (the world itself, checked here, or
* the attention world, which is one). */
if (aw % 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);
aw, (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) {
g.n_head_kv = 1; /* aw / n_head_kv_all ranks share each KV head */
g.kv_off = (ar / (aw / g.n_head_kv_all)) * g.head_dim;
} else if ((s = divide_or_fail(g.n_head_kv_all, aw, "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 */
/* A split rank reads the rows the plain row-shard gives it -- when the attention world
* is the world. Under a smaller attention world the even shard over `world` is the wrong
* cut, and the block spans [kv_off, kv_off + kv_dim) instead. */
g.kv_off = aw == g.world ? 0 : ar * g.n_head_kv * g.head_dim;
}
if (arch_splits_ff && g.world > 1 && g.n_ff_all % g.world != 0) {
g.n_ff = g.n_ff_all; /* the plugin slices it (see the parameter's comment) */