diff --git a/abi/rad_abi.h b/abi/rad_abi.h index 48738105..adfe5e1a 100644 --- a/abi/rad_abi.h +++ b/abi/rad_abi.h @@ -19,7 +19,7 @@ extern "C" { * version is refused at load with both versions printed. Each also exports the layout's fingerprint * (rad_abi_layout.h), so two layouts given one number are refused as well. A new optional export * (e.g. rad_plugin_reference) is not a change of shape: a plugin without it gets the default. */ -#define RAD_ABI_VERSION 36u +#define RAD_ABI_VERSION 37u /* ================================================================== plugin identity */ /* One value per plugin, not flags (QUANT is 3). RAD_PLUGIN_QUANT is a quantiser plugin: diff --git a/abi/rad_abi_layout_rows.h b/abi/rad_abi_layout_rows.h index 9f9c2f8e..f4db7e8d 100644 --- a/abi/rad_abi_layout_rows.h +++ b/abi/rad_abi_layout_rows.h @@ -146,6 +146,7 @@ ABI_FIELD(RadBuildCtx, calib_layer_hi) ABI_FIELD(RadBuildCtx, calib_tokens) ABI_FIELD(RadBuildCtx, gen_max_pixels) ABI_FIELD(RadBuildCtx, gen_max_refs) +ABI_FIELD(RadBuildCtx, expert_file_tier) ABI_STRUCT(RadAudioGenBatch) ABI_FIELD(RadAudioGenBatch, n_lane) ABI_FIELD(RadAudioGenBatch, lane) diff --git a/abi/rad_builder.h b/abi/rad_builder.h index 849b0b84..ec2b42cb 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -295,6 +295,12 @@ typedef struct RadBuildCtx { * fixed buffers ignores them, and the core says so at startup. */ int64_t gen_max_pixels; int64_t gen_max_refs; + + /* ---- appended in ABI 37 (where a routed expert may live) */ + /* --weights-disk-tier: nonzero when weights may stay in the container. An expert read from the + * drive has to be stored whole, so a plugin that can lay its routed experts out either way + * keeps them whole for this; 0 leaves it free to split them across the ranks by width. */ + int expert_file_tier; } RadBuildCtx; /* ================================================================== control vectors */ diff --git a/arch/glm5next/glm5next.cpp b/arch/glm5next/glm5next.cpp index 1b013449..8074689e 100644 --- a/arch/glm5next/glm5next.cpp +++ b/arch/glm5next/glm5next.cpp @@ -50,6 +50,11 @@ struct Model { * form declares none of the row form's full-width weights (1.29 GiB a card at eight ranks); * the row form is opt-in because it is not bit-identical to it (docs/ARCHITECTURES.md). */ GlmMla::Form form = GlmMla::Form::heads; + /* 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. */ + bool whole_experts = false; std::vector layers; rad_kvgroup latent = 0, index = 0, tail = 0, state = 0, conv = 0; /* The streams and the step's stream-wide buffers. */ @@ -252,10 +257,10 @@ static int ffn_buffers(RadBuilder* b, Model& m) { mw.ecnt = decl_b(b, nm.f("expert_counts"), RAD_I32, { 288 }, RAD_BUF_PERSIST); 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 }); - // 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; + // FE: the routed experts' intermediate. Under expert parallelism a rank owns whole experts, + // so the routed arm's buffers are full-width (2048); split by width they are F. The SHARED + // expert stays width-sharded (F) either way. The two coincide at world 1. + const int64_t FE = m.whole_experts ? 2048 : F; 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)); @@ -380,14 +385,19 @@ static int layer(RadBuilder* b, Model& m, Layer& lay, Names& nm, int l, bool dra // 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_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.n_ff_shared = GlmTp::expert_width(g.world); + if (m.whole_experts) { + 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 + } else { + s.cfg.n_expert = 288; + s.cfg.n_ff_exp = GlmTp::expert_width(g.world); + } s.cfg.rows = g.max_tok; s.cfg.sigmoid_router = 1; s.cfg.route_scale = 2.5; @@ -396,12 +406,12 @@ static int layer(RadBuilder* b, Model& m, Layer& lay, Names& nm, int l, bool dra s.cfg.router_fuse = 1; s.cfg.swiglu_quant = 1; // 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) { + // graph stays the pinned one. Whole experts need no width spans (split && ep is refused + // by the helper). + if (!m.whole_experts) { 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); + 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); } } RAD_ARCH_TRY(MoeFP8::probe(b, nm, l, s, &s.cfg)); @@ -494,6 +504,7 @@ 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); RAD_ARCH_TRY(stream_buffers(b, m)); RAD_ARCH_TRY(kda_buffers(b, m)); RAD_ARCH_TRY(mla_buffers(b, m)); diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 3805b070..81d59ce6 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -1028,6 +1028,7 @@ RadBuildCtx Engine::build_ctx(int rank, int64_t max_tok) const { bc.state_dtype = cfg_.state_cache_dtype == "f16" ? RAD_F16 : 0; bc.draft_coupling = cfg_.draft_coupling ? 1 : 0; bc.tp_wire_min_bytes = cfg_.tp_wire_min_kb * 1024; + bc.expert_file_tier = cfg_.weights_disk_tier ? 1 : 0; bc.scope = ""; bc.max_enc_patches = cfg_.mm_max_patches > 0 ? cfg_.mm_max_patches : 0; bc.max_audio_frames = cfg_.mm_audio_frames > 0 ? cfg_.mm_audio_frames : 0; diff --git a/tests/abi_layout_table.h b/tests/abi_layout_table.h index 583850dc..5e9b586d 100644 --- a/tests/abi_layout_table.h +++ b/tests/abi_layout_table.h @@ -1,4 +1,4 @@ -/* abi_layout_table.h -- every abi/ struct's size and field offsets at RAD_ABI_VERSION 36 and +/* abi_layout_table.h -- every abi/ struct's size and field offsets at RAD_ABI_VERSION 37 and * RAD_FORMAT_VER 2, as { type, field, offset, size } rows ("" is the struct itself). A snapshot * of the headers on x86-64 Linux, which abi_layout_test.cpp holds the layout to; regenerate it * from the test's printout only together with a version bump. */ @@ -109,7 +109,7 @@ { "RadBufDecl", "domain", 60, 4 }, { "RadBufDecl", "derive", 64, 8 }, { "RadBufDecl", "kv", 72, 4 }, -{ "RadBuildCtx", "", 0, 200 }, +{ "RadBuildCtx", "", 0, 208 }, { "RadBuildCtx", "rank", 0, 4 }, { "RadBuildCtx", "world_size", 4, 4 }, { "RadBuildCtx", "max_tok", 8, 8 }, @@ -142,6 +142,7 @@ { "RadBuildCtx", "calib_tokens", 176, 8 }, { "RadBuildCtx", "gen_max_pixels", 184, 8 }, { "RadBuildCtx", "gen_max_refs", 192, 8 }, +{ "RadBuildCtx", "expert_file_tier", 200, 4 }, { "RadAudioGenBatch", "", 0, 344 }, { "RadAudioGenBatch", "n_lane", 0, 8 }, { "RadAudioGenBatch", "lane", 8, 8 }, diff --git a/tests/abi_layout_test.cpp b/tests/abi_layout_test.cpp index 361f3783..3a7d78fa 100644 --- a/tests/abi_layout_test.cpp +++ b/tests/abi_layout_test.cpp @@ -8,7 +8,7 @@ * * Checked: a C translation unit (abi_c_test.c, strict C11) lays every struct out as C++ does; every * struct's listed fields account for its size, so the list in rad_abi_layout_rows.h is complete; and - * the layout agrees with the table below, taken at RAD_ABI_VERSION 36 and RAD_FORMAT_VER 2. Under + * the layout agrees with the table below, taken at RAD_ABI_VERSION 37 and RAD_FORMAT_VER 2. Under * those versions a listed field may not move or change size, an ABI struct may only grow by fields * past its old end, and an on-disk struct may not change at all. The table is a snapshot of the * headers: nothing else states the layout. When it disagrees the test prints the live table. @@ -58,8 +58,8 @@ std::vector cpp_rows() { }; } -/* The layout at RAD_ABI_VERSION 36 / RAD_FORMAT_VER 2 (see the file header). */ -constexpr uint32_t kTableAbi = 36, kTableFormat = 2; +/* The layout at RAD_ABI_VERSION 37 / RAD_FORMAT_VER 2 (see the file header). */ +constexpr uint32_t kTableAbi = 37, kTableFormat = 2; const AbiRow kTable[] = { #include "abi_layout_table.h" }; diff --git a/tests/glm5next_test.cpp b/tests/glm5next_test.cpp index df5c5f95..a0cbd160 100644 --- a/tests/glm5next_test.cpp +++ b/tests/glm5next_test.cpp @@ -1788,6 +1788,7 @@ TEST(glm53_is_the_pinned_graph_and_steps) { int world; uint64_t graph, steps; bool heads_only = false; /* the default: the graph before the row form */ + bool file_tier = false; /* --weights-disk-tier: whole experts at four ranks and up */ }; /* 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. Re-recorded after every graph @@ -1795,10 +1796,15 @@ TEST(glm53_is_the_pinned_graph_and_steps) { * expert ops, and the graphs again when the routing counts became RAD_BUF_PERSIST (the * steps did not change). */ const Case cases[] = { { 1, 0xce4368c09bfdbbeeull, 0x19679d4fcc0321ebull }, - /* World 8 is this branch's expert-parallel graph (288/world whole - * experts, file-direct), so its pins differ from main's. */ - { 8, 0x553f81a93cec76bfull, 0x17f0a90d3bed9713ull }, - { 8, 0x9685be82c8d55813ull, 0x75e775f0568636b5ull, true } }; + /* Two ranks keep whole experts; four and eight split them by width. */ + { 2, 0xab04b95724840abeull, 0x76865603b1390635ull }, + { 4, 0xfdee91cd1c985b0aull, 0xde7ebe5b604c3cd2ull }, + { 8, 0x59112e7ad7d77fdeull, 0xb9c29d30003a6df7ull }, + { 8, 0xf1bb904ab21d8813ull, 0xfb28d3dcd8ec5ae1ull, true }, + /* The file tier's whole experts, file-direct, at four and eight. */ + { 4, 0x1918906f91fad4e7ull, 0x07d518b9a0f32986ull, false, true }, + { 8, 0x553f81a93cec76bfull, 0x17f0a90d3bed9713ull, false, true }, + { 8, 0x9685be82c8d55813ull, 0x75e775f0568636b5ull, true, true } }; for (const Case& cs : cases) { RadBuilder b; b.kv_block = 4; @@ -1815,11 +1821,12 @@ TEST(glm53_is_the_pinned_graph_and_steps) { ctx.max_seqs = 8; ctx.max_ctx = 200000; ctx.tp_attn_rows = cs.heads_only ? 0 : 1; + ctx.expert_file_tier = cs.file_tier; REQUIRE_EQ(glm5next::declare(&b, &meta, &ctx), RAD_OK); const uint64_t g = graph_hash(b); const uint64_t s = pinned_steps(b, glm5next::step, 0, cs.world); - fprintf(stderr, " pin: glm5next world %d%s: graph 0x%016llxull, steps 0x%016llxull\n", - cs.world, cs.heads_only ? " heads only" : "", + fprintf(stderr, " pin: glm5next world %d%s%s: graph 0x%016llxull, steps 0x%016llxull\n", + cs.world, cs.heads_only ? " heads only" : "", cs.file_tier ? " file tier" : "", (unsigned long long)g, (unsigned long long)s); CHECK_EQ(g, cs.graph); CHECK_EQ(s, cs.steps);