From f713f449c20405b6d9acd43d068bbd05d7c71e72 Mon Sep 17 00:00:00 2001 From: AnimusNull Date: Mon, 2 Nov 2026 15:14:03 +0000 Subject: [PATCH] glm5next: the width split is for all-MXFP4 experts The layout rule chose the width split at four ranks and up before it knew the weights' format. MoeFP8 refuses to slice a plain bf16 expert, so a container of bf16 experts, or of MXFP4 with protected bf16 ones, that main serves at TP4 was refused unless --weights-disk-tier was set. Read the container's encodings first, on every rank alike, and take the width split only when every routed expert is MXFP4. The check declares no name map, so no pinned graph moves. --- arch/glm5next/glm5next.cpp | 23 +++++++++++++-- docs/GUIDE.md | 4 +-- tests/glm5next_test.cpp | 60 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/arch/glm5next/glm5next.cpp b/arch/glm5next/glm5next.cpp index 8074689e..4599aff9 100644 --- a/arch/glm5next/glm5next.cpp +++ b/arch/glm5next/glm5next.cpp @@ -53,7 +53,8 @@ struct Model { /* Routed experts whole on their rank (288/world of them) instead of split by width across * every rank. Whole experts can be read from the drive, which two cards need; at four and * eight the width split is the shape the MXFP4 kernels are tuned for (prefill runs about three - * times faster), so it stays unless --weights-disk-tier asks for the drive. */ + * times faster), so all-MXFP4 experts take it unless --weights-disk-tier asks for the drive. + * Anything else stays whole: a plain bf16 expert cannot be sliced at all. */ bool whole_experts = false; std::vector layers; rad_kvgroup latent = 0, index = 0, tail = 0, state = 0, conv = 0; @@ -81,6 +82,23 @@ struct Model { static std::unique_ptr models[MAX_RANKS]; static thread_local std::unique_ptr probe_model; +/* Whether every routed expert is MXFP4, asked before any buffer is sized and of the same experts on + * every rank, so all ranks reach the same layout. It reads the container's entries and declares no + * name map, so it leaves the graph as it was. One expert in another format settles it: a protected + * bf16 expert is served only whole. No answer means no container entry, and what is not known to + * be MXFP4 stays whole. */ +static bool experts_are_mxfp4(RadBuilder* b, Names& nm) { + for (int l = 3; l < 45; ++l) + for (int ge = 0; ge < 288; ++ge) + for (const char* fmt : { "blk.%d.ffn_gate_up_exps.%d.weight", + "blk.%d.ffn_down_exps.%d.weight" }) { + RadEncoding e{}; + if (!weight_enc(b, nm.f(fmt, l, ge), &e) || MoeFP8::format_of(e) != 8) + return false; + } + return true; +} + static int64_t cfg(const RadModelMeta* m, const char* key, int64_t fallback) { char nested[160]; std::snprintf(nested, sizeof nested, "text_config.%s", key); @@ -504,7 +522,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c auto& nm = m.nm; m.form = ctx->tp_attn_rows ? GlmMla::Form::rows : GlmMla::Form::heads; RAD_ARCH_TRY(geometry(g, meta, ctx, world)); - m.whole_experts = world == 2 || (world > 2 && ctx->expert_file_tier); + m.whole_experts = world == 2 || (world > 2 && (ctx->expert_file_tier || + !experts_are_mxfp4(b, nm))); RAD_ARCH_TRY(stream_buffers(b, m)); RAD_ARCH_TRY(kda_buffers(b, m)); RAD_ARCH_TRY(mla_buffers(b, m)); diff --git a/docs/GUIDE.md b/docs/GUIDE.md index 3d04d47a..12b7f7f7 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -887,8 +887,8 @@ as shipped, to calibrate or compare against. The bf16 Qwen3.8-Flash-Next (335 Gi cards at about 28 s a 32K-token step, so use large prefill steps: GLM-5.3-Flash lays its routed experts out by what the deployment needs. At four and eight cards each -expert is split by width across every card, the shape its MXFP4 kernels are tuned for, and every expert -must then fit in VRAM and the host pool. `--weights-disk-tier` keeps them whole on one card each, so they +MXFP4 expert is split by width across every card, the shape its kernels are tuned for, and every expert +must then fit in VRAM and the host pool. A container with any expert in another format stays whole. `--weights-disk-tier` keeps them whole on one card each, so they can stay on the drive, at about a third of the prefill speed and two thirds of the decode speed on 4 x R9700. Two cards always keep them whole. diff --git a/tests/glm5next_test.cpp b/tests/glm5next_test.cpp index a0cbd160..33d0cb0d 100644 --- a/tests/glm5next_test.cpp +++ b/tests/glm5next_test.cpp @@ -1833,6 +1833,66 @@ TEST(glm53_is_the_pinned_graph_and_steps) { } } +/* The width split is for all-MXFP4 experts alone: anything else keeps whole experts at four ranks + * and up, because a plain bf16 expert, even one protected among MXFP4 ones, cannot be sliced. */ +TEST(glm53_lays_out_its_experts_by_their_format) { + const char* keys[] = { "num_nextn_predict_layers", "hc_mult", "n_routed_experts", + "index_topk" }; + const char* vals[] = { "1", "4", "288", "2048" }; + RadModelMeta meta{}; + meta.n_layers = 45; + meta.n_embd = 4096; + meta.n_vocab = 154880; + meta.rms_eps = 1e-5f; + meta.n_kv = 4; + meta.kv_key = keys; + meta.kv_val = vals; + const auto mx = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32); + const auto fp8 = rad_enc_affine(RAD_F8E4M3, RAD_F32, 128, 128); + const auto bf = rad_enc_plain(RAD_BF16); + const auto declare = [&](const RadEncoding& first, const RadEncoding& rest, bool tier, + int world, uint64_t* graph) { + RadBuilder b; + b.kv_block = 4; + b.encs = { { "mtp.blk.45.ffn_gate_up_exps", fp8 }, + { "mtp.blk.45.ffn_down_exps", fp8 }, + { "blk.3.ffn_gate_up_exps.0.", first }, + { "blk.3.ffn_down_exps.0.", first }, + { "blk.3.ffn_gate_up_exps.1.", first }, + { "blk.3.ffn_down_exps.1.", first }, + { "ffn_gate_up_exps", rest }, + { "ffn_down_exps", rest }, + { "ffn_gate_up_shexp", rest }, + { "ffn_down_shexp", rest }, + { "ffn_gate_up.weight", fp8 }, + { "ffn_down.weight", fp8 } }; + RadBuildCtx ctx = spec_ctx(0, world, 3); + ctx.max_tok = 4096; + ctx.max_seqs = 8; + ctx.max_ctx = 200000; + ctx.expert_file_tier = tier; + const int rc = glm5next::declare(&b, &meta, &ctx); + if (rc == RAD_OK) *graph = graph_hash(b); + return rc; + }; + for (const int world : { 4, 8 }) { + uint64_t whole = 0, g = 0; + REQUIRE_EQ(declare(bf, bf, true, world, &whole), RAD_OK); + REQUIRE_EQ(declare(bf, bf, false, world, &g), RAD_OK); /* was refused: slices every expert */ + CHECK_EQ(g, whole); + REQUIRE_EQ(declare(fp8, fp8, true, world, &whole), RAD_OK); + REQUIRE_EQ(declare(fp8, fp8, false, world, &g), RAD_OK); + CHECK_EQ(g, whole); + REQUIRE_EQ(declare(mx, mx, true, world, &whole), RAD_OK); + uint64_t width = 0; + REQUIRE_EQ(declare(mx, mx, false, world, &width), RAD_OK); + CHECK(width != whole); + REQUIRE_EQ(declare(bf, mx, true, world, &whole), RAD_OK); + REQUIRE_EQ(declare(bf, mx, false, world, &g), RAD_OK); /* a protected pair */ + CHECK_EQ(g, whole); + } +} + /* ==================================================================== GLM-5.3's vision tower */ namespace {