From 9e0e28315d167a0b7e593953a339f75469b647f6 Mon Sep 17 00:00:00 2001 From: John McDonough <18016200+JohnCMcDonough@users.noreply.github.com> Date: Mon, 19 Oct 2026 15:45:29 +0200 Subject: [PATCH] vram_budget: the prefill staging region is paid by expert residency before the cache With a lending cache the staging region (two buffers of one layer's off-card experts) always came out of the cache, which left a large-context MoE model short of its configured coverage. Expert residency above the host pool's floor now pays first; the cache pays only when the experts are at their floor, and an expert with no other home is never evicted for it. The startup report names whichever paid. mem_test (prefill_staging_*). --- core/mem/vram_budget.cpp | 16 ++++++++++---- core/mem/vram_budget.h | 5 +++-- tests/mem_test.cpp | 46 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/core/mem/vram_budget.cpp b/core/mem/vram_budget.cpp index 5e613167..844cca7e 100644 --- a/core/mem/vram_budget.cpp +++ b/core/mem/vram_budget.cpp @@ -190,21 +190,29 @@ static int resolve(Config& cfg, const std::vector& per_rank, VramBudg * * The share is what the slab alone leaves off, the most a step can see: KV and arena loans only * add residency. A layer larger than its share stages what fits and streams the rest. */ + bool stage_from_experts = false; if (layer_max > 0 && b.expert_ceiling > b.experts && cfg.placement != "all_vram" && cfg.placement != "layer_offload") { const double f = (double)(b.expert_ceiling - b.experts) / (double)b.expert_ceiling; const int64_t one = ((int64_t)std::ceil(f * (double)layer_max) + RAD_ALIGN_UNIT - 1) / RAD_ALIGN_UNIT * RAD_ALIGN_UNIT; if (b.lends) { - /* With a lending cache the cache pays: experts are at their floor, and the region is - * lent to the slab in small steps, as the cache would have lent it. */ - if (b.kv - 2 * one >= kv_one) { + /* Expert residency above its host-pool floor pays first, so the cache keeps its + * configured coverage; otherwise the cache pays. Either way the region is lent to the + * slab in small steps. */ + if (b.experts - 2 * one >= b.expert_floor) { + b.stage = 2 * one; + b.experts -= b.stage; + stage_from_experts = true; + b.weights = b.statics + b.experts; + } else if (b.kv - 2 * one >= kv_one) { b.stage = 2 * one; b.kv -= b.stage; } } else if (2 * one < b.experts) { b.stage = 2 * one; b.experts -= b.stage; + stage_from_experts = true; b.weights = b.statics + b.experts; } } @@ -312,7 +320,7 @@ static int resolve(Config& cfg, const std::vector& per_rank, VramBudg "non-resident experts,\n" " at the arena's top and lent back to the " "slab while steps are small\n", humanb(b.stage).c_str(), - b.lends ? "cache" : "experts").c_str() + stage_from_experts ? "experts" : "cache").c_str() : "", humanb(cfg.vram_weights_mib * kMiB).c_str(), "static + experts", humanb(cfg.vram_kv_mib * kMiB).c_str(), "the cache's share", diff --git a/core/mem/vram_budget.h b/core/mem/vram_budget.h index 48e548c8..21f80092 100644 --- a/core/mem/vram_budget.h +++ b/core/mem/vram_budget.h @@ -104,8 +104,9 @@ struct VramBudget { int64_t experts = 0, kv = 0; /* the two shares, each capped at its ceiling */ int64_t unusable = 0; /* elastic neither side could take -- both were full */ /* Prefill staging region: two buffers, each one layer's share of the experts left off the - * card, appended to the top of the activation arena (core/place/stager.h). Zero when every - * expert is resident or the placement cannot move any. */ + * card, funded by cache slack or expert residency above the host-pool floor, appended to the + * top of the activation arena (core/place/stager.h). Zero when every expert is resident or the + * placement cannot move any. */ int64_t stage = 0; int64_t weights = 0; /* statics + experts: what vram_weights is set to */ std::string report; /* the block the engine prints at startup */ diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index d5eca921..31279fcd 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -385,6 +385,52 @@ TEST(the_cache_keeps_one_full_length_sequence_even_against_the_expert_floor) { CHECK_EQ(b2.experts, b2.elastic - g.kv_ceiling); } +/* With a lending cache at its floor, the prefill staging region is paid by expert residency + * above the host pool's floor, and never by an expert that has no other home. */ +TEST(prefill_staging_can_borrow_expert_slack_when_the_cache_is_small) { + VramFacts f = big_moe_facts(); + f.lends = true; + f.kv_one = f.kv_ceiling = 64LL << 20; + f.expert_floor = 16LL << 30; + f.expert_layer_max = 2LL << 30; + Config c = derive_config(); + VramBudget b; + CHECK_OK(vram_budget_resolve(c, { f }, &b)); + CHECK(b.stage > 0); + CHECK(b.report.find("out of the experts") != std::string::npos); + CHECK_EQ(b.kv, f.kv_one); + CHECK(b.experts >= f.expert_floor); + CHECK_EQ(b.weights, b.statics + b.experts); + CHECK_EQ(b.experts + b.kv + b.stage, b.elastic); + + // With no host-pool slack, staging must not evict an expert that has no other home. + f.expert_floor = b.elastic - f.kv_one; + Config tight = derive_config(); + VramBudget no_ring; + CHECK_OK(vram_budget_resolve(tight, { f }, &no_ring)); + CHECK_EQ(no_ring.stage, 0ll); + CHECK_EQ(no_ring.kv, f.kv_one); + CHECK_EQ(no_ring.experts, f.expert_floor); +} + +/* When experts can pay, the staging region leaves the cache at its full coverage. */ +TEST(prefill_staging_preserves_cache_coverage_when_experts_can_pay) { + VramFacts f = big_moe_facts(); + f.lends = true; + f.kv_ceiling = 2LL << 30; + f.kv_one = 768LL << 20; + f.expert_floor = 16LL << 30; + f.expert_layer_max = 2LL << 30; + Config c = derive_config(); + VramBudget b; + CHECK_OK(vram_budget_resolve(c, { f }, &b)); + CHECK(b.stage > 0); + CHECK_EQ(b.kv, f.kv_ceiling); + CHECK(b.experts >= f.expert_floor); + CHECK(b.report.find("out of the experts") != std::string::npos); + CHECK_EQ(b.experts + b.kv + b.stage, b.elastic); +} + /* A hybrid model's recurrent and conv slots stay allocated with one request active, so the * one-sequence floor is the inverse of the live plan, not a separate one-sequence plan: that plan * drops the fixed state and falls short of max_ctx. */