From 45963012ee4e76d77484b3a7997277c0b4431f61 Mon Sep 17 00:00:00 2001 From: baKon Date: Mon, 5 Oct 2026 13:52:59 -0500 Subject: [PATCH] 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 --- arch/common/rad_arch.h | 48 +++++++++++++++++++++----- arch/common/rad_block_attn_gated_fp8.h | 24 +++++++++++-- arch/common/rad_block_gdn_fp8.h | 31 ++++++++++++++++- arch/common/rad_fp8.h | 35 +++++++++++++++++++ arch/qwen4exp_fp8/qwen4exp_fp8.cpp | 32 +++++++++++++++-- 5 files changed, 155 insertions(+), 15 deletions(-) diff --git a/arch/common/rad_arch.h b/arch/common/rad_arch.h index 2370e0f4..27923bb1 100644 --- a/arch/common/rad_arch.h +++ b/arch/common/rad_arch.h @@ -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) */ diff --git a/arch/common/rad_block_attn_gated_fp8.h b/arch/common/rad_block_attn_gated_fp8.h index 0fe5282a..2a0d1e9c 100644 --- a/arch/common/rad_block_attn_gated_fp8.h +++ b/arch/common/rad_block_attn_gated_fp8.h @@ -192,9 +192,19 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int * 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; + /* AND UNDER A SMALLER ATTENTION WORLD (rad_arch.h Geom::attn_world: three ranks over two KV + * heads) the even shard over `world` is the wrong cut for every projection: q|gate and the + * output take this rank's heads [attn_rank * n_head, +n_head) as a span too, and k/v the + * kv_off span. An attention-zero rank declares the same spans -- the same shapes as its peers + * -- and then reads nothing for them (zero_share below). */ + const bool a_spanned = g.attn_world != g.world; + const bool kv_spanned = g.world > g.n_head_kv_all || a_spanned; 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; + const int64_t qg_span_lo = a_spanned ? g.attn_rank * qg_dim : 0; + const int64_t qg_span_hi = a_spanned ? (g.attn_rank + 1) * qg_dim : 0; + const int64_t o_span_lo = a_spanned ? g.attn_rank * q_dim : 0; + const int64_t o_span_hi = a_spanned ? (g.attn_rank + 1) * q_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", @@ -235,13 +245,19 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int RAD_ARCH_TRY(nq_h.declare(b, g, w_norm, fold ? w.h.x : w.x, w.h, g.n_embd, fold ? w.x : 0, 0, ar_in)); 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_SHARD_ROW, grp_layer(l), { src.qg }, w.h, w.qg, + 0, {}, qg_span_lo, qg_span_hi)); 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, 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, 0, {}, kv_span_lo, kv_span_hi)); + if (g.attn_zero) { + RAD_ARCH_TRY(qg.zero_share(b)); + RAD_ARCH_TRY(kp.zero_share(b)); + RAD_ARCH_TRY(vp.zero_share(b)); + } /* 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, @@ -417,7 +433,9 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int } RAD_ARCH_TRY(o.declare(b, nm, g, nm.f("blk.%d.attn_output", l), g.n_embd, q_dim, - RAD_SHARD_COL, grp_layer(l), { src.o }, w.attn, w.h.x)); + RAD_SHARD_COL, grp_layer(l), { src.o }, w.attn, w.h.x, + 0, {}, o_span_lo, o_span_hi)); + if (g.attn_zero) RAD_ARCH_TRY(o.zero_share(b)); if (g.world > 1) op_ar = rw(b, RAD_OP(b, "all_reduce", diff --git a/arch/common/rad_block_gdn_fp8.h b/arch/common/rad_block_gdn_fp8.h index 833e2af0..67b12a6c 100644 --- a/arch/common/rad_block_gdn_fp8.h +++ b/arch/common/rad_block_gdn_fp8.h @@ -72,6 +72,14 @@ struct GdnFP8 { * `gdn_recurrent_update` composes the same parameter, so this is a string to pass and * not a kernel to write. "silu" or "sigmoid"; anything else is refused below. */ const char* act = "silu"; + /* AN UNEVEN SPLIT OF THE HEADS (rad_weight_shard_span_parts). n_head_k / n_head_v are + * this rank's; k_first / v_first its first head of the container's n_head_k_all / + * n_head_v_all. k_first < 0 is the even split the plain row shard gives, as before. + * Three ranks over 16 K heads take 6, 5 and 5 (and 18, 15, 15 of the 48 V heads) -- + * every rank real heads, nothing padded, every kernel at its own head count. */ + int64_t k_first = -1, v_first = -1; + int64_t n_head_k_all = 0, n_head_v_all = 0; + bool uneven() const { return k_first >= 0; } int64_t k_dim() const { return n_head_k * head_k; } int64_t v_dim() const { return n_head_v * head_v; } @@ -268,6 +276,25 @@ inline int GdnFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, const Con RAD_ARCH_TRY(in.declare(b, nm, g, nm.f("blk.%d.ssm_inz", l), conv_dim + v_dim, g.n_embd, RAD_SHARD_ROW, grp_layer(l), { s.in_qkv, s.in_z }, w.h, w.in.x, 0, { cfg.k_dim(), cfg.k_dim(), cfg.v_dim(), cfg.v_dim() })); + if (cfg.uneven()) { + /* THIS RANK'S HEADS OF EACH PART, in the container's rows: q and k by K head, v and z by + * V head. The scale plane of a row shard has the same rows (its blocks run along K). */ + const int64_t kl = cfg.k_first * cfg.head_k, vl = cfg.v_first * cfg.head_v; + const int64_t ka = cfg.n_head_k_all * cfg.head_k, va = cfg.n_head_v_all * cfg.head_v; + const int64_t lo[4] = { kl, kl, vl, vl }; + const int64_t hi[4] = { kl + cfg.k_dim(), kl + cfg.k_dim(), vl + cfg.v_dim(), vl + cfg.v_dim() }; + const int64_t tot[4] = { ka, ka, va, va }; + RAD_ARCH_TRY(in.span_parts(b, lo, hi, tot, 4)); + const int64_t clo[3] = { kl, kl, vl }, chi[3] = { kl + cfg.k_dim(), kl + cfg.k_dim(), vl + cfg.v_dim() }; + const int64_t ctot[3] = { ka, ka, va }; + RAD_ARCH_TRY(rad_weight_shard_span_parts(b, w_conv, clo, chi, ctot, 3)); + const int64_t alo[2] = { cfg.v_first, cfg.v_first }; + const int64_t ahi[2] = { cfg.v_first + cfg.n_head_v, cfg.v_first + cfg.n_head_v }; + const int64_t atot[2] = { cfg.n_head_v_all, cfg.n_head_v_all }; + RAD_ARCH_TRY(rad_weight_shard_span_parts(b, w_ab, alo, ahi, atot, 2)); + RAD_ARCH_TRY(rad_weight_shard_span(b, w_a_log, cfg.v_first, cfg.v_first + cfg.n_head_v)); + RAD_ARCH_TRY(rad_weight_shard_span(b, w_dt_bias, cfg.v_first, cfg.v_first + cfg.n_head_v)); + } /* NOT SLICED, and why it could need to be is worth keeping. This is a 96-column bf16 GEMM -- * half a percent of the layer's projection bytes -- and a bf16 kernel for this shape that * stopped at M = 64 would leave a hole in its bucket table above 64. Since every op in a step @@ -374,7 +401,9 @@ inline int GdnFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, const Con RAD_ARCH_TRY(q_o.declare(b, g, w.o, v_dim)); RAD_ARCH_TRY(out.declare(b, nm, g, nm.f("blk.%d.ssm_out", l), g.n_embd, v_dim, - RAD_SHARD_COL, grp_layer(l), { s.out }, w.o, w.h.x)); + RAD_SHARD_COL, grp_layer(l), { s.out }, w.o, w.h.x, 0, {}, + cfg.uneven() ? cfg.v_first * cfg.head_v : 0, + cfg.uneven() ? cfg.v_first * cfg.head_v + v_dim : 0)); if (g.world > 1) op_ar = rw(b, RAD_OP(b, "all_reduce", diff --git a/arch/common/rad_fp8.h b/arch/common/rad_fp8.h index ac21e9b4..338bd556 100644 --- a/arch/common/rad_fp8.h +++ b/arch/common/rad_fp8.h @@ -664,6 +664,11 @@ struct LinearFP8 { rad_weight w = 0, ws = 0; rad_op op = 0; int64_t N = 0, K = 0; + /* ROWS OF THE SCALE PLANE PER ROW OF THE WEIGHT: 1 for an int8 or bf16 linear, whose scale + * plane (if any) has the weight's rows, RAD_FP8_BLOCK for a block-fp8 one, whose scale plane + * is 1/128 of them. A row span on `ws` is in the plane's own rows (declare's span code + * divides); span_parts below divides the same way. */ + int64_t ws_row_div = 1; /* A PLAIN bf16 WEIGHT read by `gemm_nt` over the activation's bf16 plane, in a model whose * linears are bf16 (Geom::w_bf16): one weight and no scale plane, and no quantiser in front. */ bool bf16 = false; @@ -722,6 +727,35 @@ struct LinearFP8 { int64_t span_lo = 0, int64_t span_hi = 0); void step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, int64_t r0 = 0, int64_t rows = 0) const; + /* THIS RANK READS NOTHING FOR IT: the weight and its scale plane are zeros + * (rad_weight_shard_zero). After declare(); for an attention-zero rank (rad_arch.h). */ + int zero_share(RadBuilder* b) const { + RAD_ARCH_TRY(rad_weight_shard_zero(b, w)); + if (ws) RAD_ARCH_TRY(rad_weight_shard_zero(b, ws)); + return RAD_OK; + } + /* A SPAN PER STACKED ROW PART (rad_weight_shard_span_parts), in the weight's rows; the scale + * plane takes the same spans in its own rows. After declare(); every span and total has to be + * whole scale blocks when the plane is 1/128, which head widths of 128 are. */ + int span_parts(RadBuilder* b, const int64_t* lo, const int64_t* hi, const int64_t* total, + int n) const { + RAD_ARCH_TRY(rad_weight_shard_span_parts(b, w, lo, hi, total, n)); + if (!ws) return RAD_OK; + if (ws_row_div == 1) return rad_weight_shard_span_parts(b, ws, lo, hi, total, n); + int64_t slo[RAD_MAX_ROW_PARTS], shi[RAD_MAX_ROW_PARTS], stot[RAD_MAX_ROW_PARTS]; + for (int g = 0; g < n && g < RAD_MAX_ROW_PARTS; ++g) { + if (lo[g] % ws_row_div || hi[g] % ws_row_div || total[g] % ws_row_div) { + fprintf(stderr, "radiance: a part span [%lld, %lld) of %lld rows is not whole " + "%lld-row scale blocks\n", (long long)lo[g], (long long)hi[g], + (long long)total[g], (long long)ws_row_div); + return RAD_E_INVAL; + } + slo[g] = lo[g] / ws_row_div; + shi[g] = hi[g] / ws_row_div; + stot[g] = total[g] / ws_row_div; + } + return rad_weight_shard_span_parts(b, ws, slo, shi, stot, n); + } }; /* THE NAME MAP OF A LINEAR, DECLARED BEFORE ITS WEIGHT: it is how the checkpoint -- or the recipe @@ -928,6 +962,7 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha * bf16 release) declares the same two views; what makes the planes exist is rad-convert. */ w = decl_view(b, wn, nullptr, "codes", RAD_F8E4M3, { N, K }, RAD_ACCESS_PER_TOKEN, shard, grp, wparts, np); + ws_row_div = RAD_FP8_BLOCK; ws = decl_view(b, sn, wn, "scale", RAD_BF16, { fp8_blocks(N), fp8_blocks(K) }, RAD_ACCESS_PER_TOKEN, shard, grp, sparts, np); if (!w || !ws) return RAD_E_INVAL; diff --git a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp index 7ae4b287..18aeafe4 100644 --- a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp +++ b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp @@ -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.