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

+29 -3
View File
@@ -406,9 +406,35 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
(long long)gc.conv_width);
return RAD_E_INVAL;
}
int s;
if ((s = divide_or_fail(nk, g.world, "linear_num_key_heads", &gc.n_head_k)) < 0) return s;
if ((s = divide_or_fail(nv, g.world, "linear_num_value_heads", &gc.n_head_v)) < 0) return s;
/* THE HEADS SPLIT EVENLY WHEN THEY CAN, as before, and UNEVENLY WHEN THEY CANNOT: 16 K
* heads over three ranks are 6, 5 and 5, each rank's V heads the same multiple of its K
* heads (48 / 16 = 3 -> 18, 15, 15), declared as spans of the container's rows
* (rad_block_gdn_fp8.h Config::k_first). Nothing is padded and every rank holds real
* heads; the kernels take any head count. A V count that is not a multiple of the K count
* cannot be grouped that way and is refused. */
gc.n_head_k_all = nk;
gc.n_head_v_all = nv;
if (g.world > 1 && (nk % g.world != 0 || nv % g.world != 0)) {
if (nv % nk != 0) {
fprintf(stderr, "radiance: qwen4exp: %lld value heads over %lld key heads do not "
"group, so an uneven split over %d ranks is not defined\n",
(long long)nv, (long long)nk, g.world);
return RAD_E_INVAL;
}
const int64_t ratio = nv / nk, base = nk / g.world, rem = nk % g.world;
gc.n_head_k = base + (g.rank < rem ? 1 : 0);
gc.k_first = g.rank * base + (g.rank < rem ? g.rank : rem);
gc.n_head_v = ratio * gc.n_head_k;
gc.v_first = ratio * gc.k_first;
fprintf(stderr, "radiance: qwen4exp: rank %d takes delta-net K heads [%lld, %lld) of "
"%lld and V heads [%lld, %lld) of %lld\n", g.rank,
(long long)gc.k_first, (long long)(gc.k_first + gc.n_head_k), (long long)nk,
(long long)gc.v_first, (long long)(gc.v_first + gc.n_head_v), (long long)nv);
} else {
int s;
if ((s = divide_or_fail(nk, g.world, "linear_num_key_heads", &gc.n_head_k)) < 0) return s;
if ((s = divide_or_fail(nv, g.world, "linear_num_value_heads", &gc.n_head_v)) < 0) return s;
}
}
/* ---- the routed feed-forward.