diff --git a/arch/common/rad_arch.h b/arch/common/rad_arch.h index 6b06bfbf..23193c8c 100644 --- a/arch/common/rad_arch.h +++ b/arch/common/rad_arch.h @@ -675,14 +675,30 @@ inline int divide_or_fail(int64_t v, int world, const char* what, int64_t* out) * absurd. So the last rank holds fewer rows. The sampler merges candidate sets rather than * gathering full logits (spec §9, §13), so a ragged shard costs it nothing; a padded one would * cost a fake token id that has to be masked everywhere forever. */ -inline int64_t shard_extent(int64_t total, int world, int rank) { +/* THE VOCABULARY SPLIT: ceil(total / world) rows a rank, ROUNDED UP TO A WHOLE 128-ROW BLOCK, the + * last rank short. The block is RAD_FP8_BLOCK: a block-scaled fp8 lm_head is stored in 128x128 + * tiles and a row span that cuts a tile is refused (rad_weight_shard_span), so the cut has to land + * on one. At two ranks on 248320 or 151936 the rounding changes nothing -- both halves are block + * multiples -- and a vocabulary the world divides is never rounded; at three it is what makes the + * head shardable at all (248320 / 3 = 82773.3; the ranks take 82816, 82816, 82688). core/engine.cpp + * RESTATES this rule and checks it against the width the plugin declared; the two must move together. */ +inline int64_t vocab_per_rank(int64_t total, int world) { + /* A vocabulary the world divides keeps the even share, whatever the world: every container + * served today splits that way (248320 at 2 and 4, 151936 at 2 -- the last halves mid-block and + * is served with an untiled head). Only a vocabulary the world does NOT divide takes the + * block-aligned ceil, which is what makes a tiled head shardable at all. */ + if (total % world == 0) return total / world; const int64_t per = (total + world - 1) / world; + return (per + 127) / 128 * 128; +} +inline int64_t shard_extent(int64_t total, int world, int rank) { + const int64_t per = vocab_per_rank(total, world); const int64_t beg = per * rank; const int64_t end = beg + per < total ? beg + per : total; return end > beg ? end - beg : 0; } inline int64_t shard_begin(int64_t total, int world, int rank) { - const int64_t per = (total + world - 1) / world; + const int64_t per = vocab_per_rank(total, world); const int64_t beg = per * rank; return beg < total ? beg : total; } diff --git a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp index cf5778dc..aa2670ee 100644 --- a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp +++ b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp @@ -914,6 +914,14 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c weight_codes_dtype(b, nm.f("output.weight"), RAD_BF16), {g.n_vocab, g.n_embd}, RAD_ACCESS_VOCAB, RAD_SHARD_ROW, grp_model()); if (!m.w_tok || !m.w_lm_head) return RAD_E_INVAL; + /* THE RANK'S ROWS, STATED: a vocabulary that does not divide by the world (248320 at three + * ranks) is split by rad_arch.h's rule -- ceil to a 128-row block, last rank short -- and that + * is a span, not the even share the core would otherwise cut. Harmless where it does divide + * (the span IS the even share). */ + if (g.world > 1) { + RAD_ARCH_TRY(rad_weight_shard_span(b, m.w_tok, g.vocab_off, g.vocab_off + g.n_vocab)); + RAD_ARCH_TRY(rad_weight_shard_span(b, m.w_lm_head, g.vocab_off, g.vocab_off + g.n_vocab)); + } m.op_embed = rw(b, RAD_OP(b, "embed_lookup", RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n_embd", g.n_embd), diff --git a/core/engine.cpp b/core/engine.cpp index 8736b0dc..71b4dea8 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -122,13 +122,18 @@ int Engine::declare_sampler(int rank, RadBuilder* b, int64_t logits_width, bool sc.n_vocab_local = logits_width > 0 ? logits_width : meta_.n_vocab; /* THIS RANK'S FIRST VOCABULARY ROW, and it is the one thing the declared width cannot say: * a width tells you how many rows a rank owns, not which. The rule is arch/common/rad_arch.h's - * shard_begin -- ceil(total/world) per rank with the last one short -- and it is restated here + * shard_begin -- ceil(total/world) per rank, rounded up to a 128-row block, the last one short -- and it is restated here * rather than carried through the ABI, so the restatement is CHECKED against what the plugin * actually declared. A plugin that shards some other way gets a named refusal at startup * instead of a sampler that names rank 0's tokens with rank 1's ids. */ sc.vocab_off = 0; if (sc.n_vocab_local < sc.n_vocab) { - const int64_t per = (sc.n_vocab + cfg_.tp - 1) / cfg_.tp; + /* rad_arch.h vocab_per_rank, restated: the even share when the world divides the vocabulary; + * otherwise ceil(n / tp) rounded up to a whole 128-row block so a block-scaled head can be + * cut there. */ + const int64_t ceil_per = (sc.n_vocab + cfg_.tp - 1) / cfg_.tp; + const int64_t per = (sc.n_vocab % cfg_.tp == 0) ? sc.n_vocab / cfg_.tp + : (ceil_per + 127) / 128 * 128; sc.vocab_off = per * rank; const int64_t want = std::min(per, sc.n_vocab - sc.vocab_off); if (sc.vocab_off >= sc.n_vocab || want != sc.n_vocab_local) {