From 2c67e93342150cf83fdcc20413d3c88e4ca2b76d Mon Sep 17 00:00:00 2001 From: John McDonough <18016200+JohnCMcDonough@users.noreply.github.com> Date: Mon, 19 Oct 2026 15:45:09 +0200 Subject: [PATCH] engine: the one-sequence KV floor is the live plan's inverse, its state slots included The floor a KV pool must keep for one --max-model-len request came from a separate plan at max_seqs 1. A hybrid model's recurrent and conv slots are fixed per configured sequence and stay allocated while one request is active, so that plan undercounted and the pool it allowed could not hold max_ctx. The floor is now pool_mib_for_tokens over the real plan's groups. mem_test (hybrid_context_floor_accounts_for_all_configured_state_slots). --- core/engine_bringup.cpp | 19 +++++++------------ core/mem/vram_budget.h | 2 +- tests/mem_test.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 0f84c216..55252355 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -1373,18 +1373,6 @@ int Engine::plan() { ngram_bytes = std::max(ngram_bytes, wf.ngram); ngram_device = ngram_device || wf.ngram_device; ngram_host = ngram_host || wf.ngram_host; - /* One --max-model-len sequence's cache: the least a KV pool can hold and still serve - * every acceptable request (configure_kv warns below it). --ngram-placement auto puts - * the table on the card only if the pool stays above it. */ - { - Config one = cfg_; - one.max_seqs = 1; - KVManager single; - if (single.plan_groups(r->program.kv_groups, one) == RAD_OK) { - kv_floor = std::max(kv_floor, single.ceiling(ctx)); - f.kv_one = single.ceiling(ctx); - } - } /* The staging region is sized only where the arena lends its top: without smaller * levels nothing returns it to the slab at decode, and a deterministic plan moves * nothing. */ @@ -1397,6 +1385,13 @@ int Engine::plan() { ck_slot += probe.checkpoint_bytes(); f.kv_ceiling = probe.ceiling(ctx); f.kv_fixed = probe.fixed_bytes(); + /* One --max-model-len sequence's pool, as the inverse of the real plan: its + * recurrent and conv slots stay allocated with one request active, which a + * separate one-sequence plan would leave out. */ + const int64_t MiB = 1ll << 20; + const int64_t need = probe.pool_mib_for_tokens(ctx, (f.kv_ceiling + MiB - 1) / MiB); + if (need >= 0) f.kv_one = need * MiB; + kv_floor = std::max(kv_floor, f.kv_one); } /* The step batch's device staging, reserved by the scheduler after this split: block * tables for max_seqs sequences at max_ctx, media staging for max_tok encoder rows. diff --git a/core/mem/vram_budget.h b/core/mem/vram_budget.h index 55f8cc46..48e548c8 100644 --- a/core/mem/vram_budget.h +++ b/core/mem/vram_budget.h @@ -75,7 +75,7 @@ struct VramFacts { /* Experts the pinned host pool cannot hold, plus the slab's spare slots: the expert slab's * floor when the cache lends (expert_vram_floor, core/place/planner.h). */ int64_t expert_floor = 0; - int64_t kv_one = 0; /* the cache for one --max-model-len sequence: the least it may get */ + int64_t kv_one = 0; /* all configured fixed state plus pages for one full-length context */ /* The cache lends idle blocks to the expert slab: a placement that moves units (expert_tiered * or auto), not --deterministic, on a device with virtual memory management. A lending cache * can be sized for what live requests could need; a non-lending one would hold it empty. */ diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index e2eeb1bf..d5eca921 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -385,6 +385,31 @@ TEST(the_cache_keeps_one_full_length_sequence_even_against_the_expert_floor) { CHECK_EQ(b2.experts, b2.elastic - g.kv_ceiling); } +/* 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. */ +TEST(hybrid_context_floor_accounts_for_all_configured_state_slots) { + Config c = small_config(); + c.max_seqs = 4; + c.max_ctx = 32768; + c.checkpoint_slots = 0; + auto groups = hybrid_groups(); + KVManager actual, single; + CHECK_OK(actual.plan_groups(groups, c)); + Config one = c; + one.max_seqs = 1; + CHECK_OK(single.plan_groups(groups, one)); + // A one-sequence declaration drops fixed state that the live four-slot plan owns. + CHECK(actual.paged_tokens(single.ceiling(c.max_ctx)) < c.max_ctx); + constexpr int64_t MiB = 1ll << 20; + const int64_t ceiling_mib = (actual.ceiling(c.max_ctx) + MiB - 1) / MiB; + const int64_t floor_mib = actual.pool_mib_for_tokens(c.max_ctx, ceiling_mib); + CHECK(floor_mib > 0); + CHECK(actual.paged_tokens(floor_mib * MiB) >= c.max_ctx); + CHECK((floor_mib - 1) * MiB < actual.fixed_bytes() || + actual.paged_tokens((floor_mib - 1) * MiB) < c.max_ctx); +} + TEST(neither_side_is_given_more_than_it_could_use) { /* A model that fits: 4 GiB of experts and a cache that can address 1.2 GiB cannot use a * 32 GiB card, so the remainder is reported unusable rather than allocated to an idle pool. */