diff --git a/arch/common/rad_arch.h b/arch/common/rad_arch.h index 27923bb1..1609d775 100644 --- a/arch/common/rad_arch.h +++ b/arch/common/rad_arch.h @@ -733,6 +733,21 @@ inline long long meta_int2(const RadModelMeta* m, const char* gguf_key, const ch * count). With it set, a width the world does not divide is left whole in g.n_ff and the plugin * takes its slice from there; without it the refusal stands, because for every other plugin g.n_ff * IS the per-rank width. A width that does divide is unaffected either way. */ +/* THE REMAINDER-FIRST ORDER OF THE RANKS. Whatever does not divide evenly over the world -- the + * extra block of an expert's width, the extra delta-net head -- goes to the ranks with NO + * attention first: they pay nothing for attention (rad_block_attn_gated_fp8.h: one fill) and keep + * no attention cache (rad_kv_group_zero), so they have the time and the memory for it, and every + * rank with heads is lighter for it. This is a rank's PLACE in that order: the attention-zero ranks + * take places 0..n-1 whatever `parity` is, and the attention ranks follow in today's rotation by + * `parity` -- so where no rank is attention-zero (every world the heads divide) the place is + * exactly (rank - parity) mod world and every cut is what it always was, byte for byte. */ +inline int64_t remainder_place(const Geom& g, int rank, int parity) { + const int nz = g.world - g.attn_world; + if (rank >= g.attn_world) return rank - g.attn_world; + const int aw = g.attn_world; + return nz + ((rank - parity) % aw + aw) % aw; +} + inline int geom_from(Geom& g, const RadModelMeta* m, const RadBuildCtx* c, const char* dtype, uint32_t act_dtype, bool arch_splits_ff = false) { if (!m || !c) return RAD_E_INVAL; @@ -808,8 +823,8 @@ inline int geom_from(Geom& g, const RadModelMeta* m, const RadBuildCtx* c, 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, + "rank(s) %d..%d carry no attention and take the delta net's and the " + "experts' remainders first\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; diff --git a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp index 64c0726c..85d1667f 100644 --- a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp +++ b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp @@ -422,8 +422,11 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c 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); + /* The extra heads go to the ranks without attention first (rad_arch.h + * remainder_place); with none, this is the rank's own place and the split as before. */ + const int64_t pl = remainder_place(g, g.rank, 0); + gc.n_head_k = base + (pl < rem ? 1 : 0); + gc.k_first = pl * base + (pl < rem ? pl : 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 " @@ -602,8 +605,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c * the ranks to fit inside the two parities' blocks together. */ const int64_t base = nb / g.world, extra = nb % g.world; for (int q = 0; q < 2; ++q) { - /* This rank's place in expert parity q's split: first for its own parity. */ - const int64_t p = (g.rank - q + g.world) % g.world; + /* This rank's place in expert parity q's split: the ranks without attention + * first, then first for its own parity (rad_arch.h remainder_place). */ + const int64_t p = remainder_place(g, g.rank, q); const int64_t b0 = p * base + std::min(p, extra); const int64_t b1 = b0 + base + (p < extra ? 1 : 0); mc.ff_lo[q] = b0 * RAD_FP8_BLOCK; @@ -617,7 +621,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c * rank already takes of some parity. */ if (g.world == 4) { for (int q = 0; q < 4; ++q) { - const int64_t p = (g.rank - q + 4) % 4; + const int64_t p = remainder_place(g, g.rank, q); const int64_t b0 = p * base + std::min(p, extra); const int64_t b1 = b0 + base + (p < extra ? 1 : 0); mc.ff_lo4[q] = b0 * RAD_FP8_BLOCK; @@ -635,9 +639,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c } else { rad_note(b, "routed experts: rank %d computes columns [%lld, %lld) of each " "even expert and [%lld, %lld) of each odd one, of %lld. The width " - "is %lld blocks of %d, which %lld ranks split unevenly, so the rank " - "taking the extra block alternates with the expert's parity; the " - "block's all-reduce sums the partials.", + "is %lld blocks of %d, which %lld ranks split unevenly: the extra block " + "goes to a rank without attention where there is one, else to the " + "rank the expert's parity picks; the block's all-reduce sums the " + "partials.", g.rank, (long long)mc.ff_lo[0], (long long)mc.ff_hi[0], (long long)mc.ff_lo[1], (long long)mc.ff_hi[1], (long long)g.n_ff_all, (long long)nb, RAD_FP8_BLOCK, diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 1a0864cf..18a04757 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -2584,11 +2584,14 @@ TEST(flashnext_tp3_zeroes_the_third_ranks_attention_and_splits_gdn_heads_unevenl CHECK_EQ(o->span_lo, (int64_t)ar * (n_head / attn_world) * head_dim); CHECK_EQ(o->zero, zero); } - /* (3) THE DELTA NET: 6, 5, 5 K heads and 18, 15, 15 V heads, real on every rank, as + /* (3) THE DELTA NET: 5, 5, 6 K heads and 15, 15, 18 V heads, real on every rank, as * per-part spans of [q|k|v|z] and of the conv channels, a one-span of A_log. */ const int64_t base = nk / world, rem = nk % world; - const int64_t my_k = base + (r < rem ? 1 : 0); - const int64_t k0 = r * base + (r < rem ? r : rem); + /* The extra head goes to the rank without attention first (rad_arch.h remainder_place): + * rank 2 takes 6, ranks 0 and 1 take 5 each. */ + const int64_t pl = r >= attn_world ? r - attn_world : (world - attn_world) + r % attn_world; + const int64_t my_k = base + (pl < rem ? 1 : 0); + const int64_t k0 = pl * base + (pl < rem ? pl : rem); const int64_t my_v = (nv / nk) * my_k, v0 = (nv / nk) * k0; const RecWeight* in = find_w(b[r], "blk.0.ssm_inz.weight"); REQUIRE(in != nullptr); @@ -2629,7 +2632,7 @@ TEST(flashnext_tp3_zeroes_the_third_ranks_attention_and_splits_gdn_heads_unevenl /* The three ranks' K heads are the container's 16, once each. */ { int64_t sum = 0; - for (int r = 0; r < world; ++r) sum += nk / world + (r < nk % world ? 1 : 0); + for (int r = 0; r < world; ++r) sum += nk / world + (r < nk % world ? 1 : 0); /* a permutation of the same counts */ CHECK_EQ(sum, nk); } }