From 9647b84ee0ee7329723958287f120272a8d0f950 Mon Sep 17 00:00:00 2001 From: overchop Date: Fri, 30 Oct 2026 09:05:25 +0200 Subject: [PATCH] glm5next: expert parallelism -- experts become file-direct Main's glm5next width-shards the routed experts unconditionally (GlmTp::expert_width, ff_lo/ff_hi): an expert's stored form is a shard across ranks, so --weights-disk-tier refuses it and a 151 GiB expert set cannot leave VRAM at any pool size. The helper's expert-parallel declaration (expert_base windows, RAD_SHARD_NONE, no ff spans) already exists upstream -- qwen4exp uses it -- so this retargets glm5next onto it: each rank owns 288/world whole experts, the routed arm's buffers full-width (FE), the shared expert still width-sharded, world 1 unchanged, and a shape probe's rank >= world clamps to rank 0's window. Fixes the 24.78 GiB "no eligible tier" startup OOM on 2-card hosts; the world-8 graph pin is re-recorded from the test's own pin: printout. Assisted-by: GLM-5.3-Flash --- arch/common/rad_block_moe_fp8.h | 22 ++++++++++++----- arch/glm5next/glm5next.cpp | 42 ++++++++++++++++++++++++++------- tests/glm5next_test.cpp | 10 +++++--- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/arch/common/rad_block_moe_fp8.h b/arch/common/rad_block_moe_fp8.h index 614db0ef..b862c1b8 100644 --- a/arch/common/rad_block_moe_fp8.h +++ b/arch/common/rad_block_moe_fp8.h @@ -1621,10 +1621,13 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows, bool o * (rad_expert_prefetch). A failure only loses the prediction. */ if (xp_next && xp_next->xp.n_parts > 0) (void)rad_expert_prefetch(c_, &xp_next->xp, w.h.x, r0, rows, g.n_embd); - /* Write the routing counts straight into the core's row for this layer when it keeps one, so - * the report in step() needs no copy (otherwise a dependent blit per routed layer). */ - int32_t* const counts = rad_route_counts(c_, layer, ne); - const RadOperand ecnt = counts ? praw(counts, RAD_I32, ne) : brows(w.ecnt, ne); + /* Bind the block's own ecnt buffer, never the core's raw routing row: a raw-pointer operand + * does not survive the step tape's replay (the decode path re-issues ops from the tape), so + * the histogram the kernel writes there never lands, and a consumer reading the reported + * counts -- the file stager, at the layer's trigger -- sees zeros and skips every expert's + * weights. The declared buffer is stable across record and replay, and route_report hands + * the same buffer on, so the stager's stream-ordered read sees this pass's counts. */ + const RadOperand ecnt = brows(w.ecnt, ne); /* The fused router when it resolved and the pass fits: router, topk + scatter and (with * `router_fused_gate`) the shared gate in one launch. Otherwise the separate ops. */ const bool rfused = op_router_fused && rows <= router_fused_rows; @@ -1884,12 +1887,19 @@ inline void MoeFP8::route_report(RadCtx* c_) const { * `expert_w` stays null: RadRouting declares it `const float*` but these weights are bf16 * (f32 under the sigmoid router). Nothing in the core reads it (the heat engine uses * `expert_count`). */ + /* The router kernel writes the block's own ecnt buffer (w.ecnt) every pass, so report THAT + * pointer: a consumer reading it stream-ordered (the file stager's trigger runs after this + * layer's router on the same stream) sees this pass's counts. Reporting rad_route_counts' + * row instead names a buffer the router never writes; the pointer then equals the report's + * destination row, the copy here is skipped, and the row stays zero -- which the file stager + * read as "no expert routed" and skipped every layer's weights (NaN logits, empty text). + * Copying w.ecnt into the row (the pointers now differ) also feeds the heat engine real + * histograms instead of the zeros it saw before. */ moe_debug_weights(c_, layer, c.top_k, w.ids, w.ew, c.sigmoid_router != 0); RadRouting r{}; r.expert_ids = (const int32_t*)rad_buf_ptr(c_, w.ids); r.expert_w = nullptr; - const int32_t* const counts = rad_route_counts(c_, layer, c.n_expert); - r.expert_count = counts ? counts : (const int32_t*)rad_buf_ptr(c_, w.ecnt); + r.expert_count = (const int32_t*)rad_buf_ptr(c_, w.ecnt); r.sorted_tok = (const int32_t*)rad_buf_ptr(c_, w.sorted); r.expert_offset = (const int32_t*)rad_buf_ptr(c_, w.eoff); r.top_k = c.top_k; diff --git a/arch/glm5next/glm5next.cpp b/arch/glm5next/glm5next.cpp index 2a9a61fa..39d514ed 100644 --- a/arch/glm5next/glm5next.cpp +++ b/arch/glm5next/glm5next.cpp @@ -246,9 +246,13 @@ static int ffn_buffers(RadBuilder* b, Model& m) { mw.ecnt = decl_b(b, nm.f("expert_counts"), RAD_I32, { 288 }); mw.sorted = decl_b(b, nm.f("expert_sorted"), RAD_I32, { T * 8 }); mw.inv = decl_b(b, nm.f("expert_inverse"), RAD_I32, { T * 8 }); - mw.egu = decl_b(b, nm.f("expert_gate_up"), RAD_BF16, { T * 8, 2 * F }); - mw.eff.x = decl_b(b, nm.f("expert_ff"), RAD_BF16, { T * 8, F }); - RAD_ARCH_TRY(mw.eff.declare_qs(b, nm, g, "expert_ff", F, T * 8)); + // FE: the routed experts' WHOLE intermediate (2048). Under expert parallelism a rank owns + // whole experts, so the routed arm's buffers are full-width at every world size; the SHARED + // expert stays width-sharded (F). The two coincide at world 1. + const int64_t FE = 2048; + mw.egu = decl_b(b, nm.f("expert_gate_up"), RAD_BF16, { T * 8, 2 * FE }); + mw.eff.x = decl_b(b, nm.f("expert_ff"), RAD_BF16, { T * 8, FE }); + RAD_ARCH_TRY(mw.eff.declare_qs(b, nm, g, "expert_ff", FE, T * 8)); mw.edn = decl_b(b, nm.f("expert_down"), RAD_BF16, { T * 8, 4096 }); mw.sgu = bf("shared_gate_up", 2 * F); mw.sff.x = bf("shared_ff", F); @@ -357,9 +361,26 @@ static int layer(RadBuilder* b, Model& m, Layer& lay, Names& nm, int l, bool dra s.router_bias = nm.ckpt("%s.mlp.gate.e_score_correction_bias", pre); s.experts = nm.ckpt("%s.mlp.experts", pre); s.shared = nm.ckpt("%s.mlp.shared_experts", pre); - s.cfg.n_expert = s.cfg.n_expert_all = 288; + // EXPERT PARALLELISM AT TP: each rank owns 288/world WHOLE experts (the contiguous + // window [expert_base, expert_base+n_expert) of the checkpoint's numbering), which the + // helper declares RAD_SHARD_NONE -- unsharded, so an expert unit is file-direct and can + // live in the container's mapping or the file tier + // instead of needing VRAM+pinned-pool residency for all 151 GiB of them. The router + // stays replicated over n_expert_all; moe_scatter/router_topk_scatter drop slots + // outside the window (moe_scatter schema: "which is how EXPERT PARALLELISM is + // expressed"); the shared expert stays width-sharded on its own declaration path; the + // block's all-reduce sums the partials exactly as width TP did. At world 1 this + // degenerates to the old whole-model declaration (n_expert == n_expert_all, no spans). + s.cfg.n_expert_all = 288; s.cfg.top_k = 8; - s.cfg.n_ff_exp = s.cfg.n_ff_shared = GlmTp::expert_width(g.world); + s.cfg.n_expert = 288 / g.world; + // A shape probe may pass rank >= world (arch_test's parallel declares reuse i % 4); + // old code tolerated that because expert_base stayed 0, so clamp to rank 0's window. + const int ep_rank = g.rank < g.world ? g.rank : 0; + s.cfg.expert_base = ep_rank * (288 / g.world); + s.cfg.probed_expert = s.cfg.expert_base; // probe this rank's first owned expert + s.cfg.n_ff_exp = 2048; // whole: whole experts' intermediate + s.cfg.n_ff_shared = 2048 / g.world; s.cfg.rows = g.max_tok; s.cfg.sigmoid_router = 1; s.cfg.route_scale = 2.5; @@ -367,9 +388,14 @@ static int layer(RadBuilder* b, Model& m, Layer& lay, Names& nm, int l, bool dra s.cfg.shared_ungated = 1; s.cfg.router_fuse = 1; s.cfg.swiglu_quant = 1; - for (int q = 0; q < 2; ++q) { - s.cfg.ff_lo[q] = g.rank * GlmTp::expert_width(g.world); - s.cfg.ff_hi[q] = (g.rank + 1) * GlmTp::expert_width(g.world); + // At world 1 the width-slice declaration IS the whole model: keep the spans so the + // graph stays the pinned one. At world > 1 expert parallelism is in force and whole + // experts need no width spans (split && ep is refused by the helper). + if (g.world == 1) { + for (int q = 0; q < 2; ++q) { + s.cfg.ff_lo[q] = g.rank * (2048 / g.world); + s.cfg.ff_hi[q] = (g.rank + 1) * (2048 / g.world); + } } RAD_ARCH_TRY(MoeFP8::probe(b, nm, l, s, &s.cfg)); RAD_ARCH_TRY(lay.moe.declare(b, nm, g, l, w, s, false, draft)); diff --git a/tests/glm5next_test.cpp b/tests/glm5next_test.cpp index 819d4c77..bd645b67 100644 --- a/tests/glm5next_test.cpp +++ b/tests/glm5next_test.cpp @@ -1612,10 +1612,14 @@ TEST(glm53_is_the_pinned_graph_and_steps) { bool heads_only = false; /* the default: the graph before the row form */ }; /* Pins from this test's own `pin:` printout: the head form (heads_only, which declares the - * streamed ops) and the row form, at one and eight ranks. */ + * streamed ops) and the row form, at one and eight ranks. Re-recorded after every graph + * change -- the merge re-recorded them for main's builder plus this branch's file-direct + * expert ops. */ const Case cases[] = { { 1, 0x8e12725b3ed3548dull, 0x68ffc03d2b640d51ull }, - { 8, 0xe4ba21765f690f70ull, 0x82ed1094dbf4b637ull }, - { 8, 0xf404ab12440b389full, 0x3818eedb4ac10d1bull, true } }; + /* World 8 is this branch's expert-parallel graph (288/world whole + * experts, file-direct), so its pins differ from main's. */ + { 8, 0x6232b56d6f971d61ull, 0x1b80576ba8f1ecc7ull }, + { 8, 0x3e4b89352f01b57bull, 0xed8cc595e8eeb593ull, true } }; for (const Case& cs : cases) { RadBuilder b; b.kv_block = 4;