ling32 tier planner: pool mode keeps the budget a hard cap, tests follow the pool default

- pool mode no longer adds a free-standing bonus to the pool: with an auto budget it uses a 256 MiB reserve instead of 600 MiB, a 1 % margin instead of 2.5 %
  and an activation estimate 512 MiB lower (all three measured on 4 cards: 1.7-2.1 GiB were left free), so vram_total_planned <= vram_budget holds by construction;
  an explicit --vram-budget-gib stays a hard cap.
- the MTP sidecar bytes (+200 MiB of buffers) are taken out of the pool's memory.
- ling32_tier_planner_test: the 24 GiB case asserts the pool default (no static prefix, many slots, planned <= budget), the old layout is checked with --tier-pool off,
  and a 1 GiB sidecar must shrink the pool.
This commit is contained in:
Niko1221 committed 2026-12-01 19:59:16 +01:00
1 parent 967ac817e3
commit f720d382d3
2 files changed
+32 -7

No files matched your search

@@ -165,7 +165,14 @@ inline TierPlan plan_tier(const TierHardware& hw, const TierRequestOpts& rq, con
const uint64_t kda_state = 2ull * 35 * 2 * MiB;
const uint64_t act = static_cast<uint64_t>(sz.chunk) * (static_cast<uint64_t>(sz.vocab) * 4 + 1200ull * 1024) + 64 * MiB;
p.vram_fixed = 450 * MiB + 100 * MiB + sz.trunk_bytes + sz.latent_bytes + kda_state + act + (rq.prefetch ? sz.lookahead_bytes : 0);
const uint64_t margin = p.vram_budget / 40; // 2.5 %: allocator slack
// pool mode (see below) uses the card more tightly: measured at the end of the start-up on a 4090, an A4000, a 3060 and a 3060 Ti, 1.7 - 2.1 GiB of the card were still free with the
// plan above (the 600 MiB reserve, the 2.5 % margin, and an activation estimate that is 0.55 - 0.8 GiB high). So: 256 MiB reserve (auto budget only), 1 % margin, fixed - 512 MiB.
const bool pool_wanted = rq.pool == 1 || (rq.pool == -1 && p.vram_budget <= 30 * GiB);
if (pool_wanted) {
if (!rq.vram_budget) p.vram_budget = vram_total > 256 * MiB ? vram_total - 256 * MiB : 0;
p.vram_fixed -= std::min<uint64_t>(p.vram_fixed, 512 * MiB);
}
const uint64_t margin = p.vram_budget / (pool_wanted ? 100 : 40); // 2.5 % (pool mode 1 %): allocator slack
const uint64_t vram_free_for_experts = p.vram_budget > p.vram_fixed + margin ? p.vram_budget - p.vram_fixed - margin : 0;
// ---- RAM
@@ -200,14 +207,13 @@ inline TierPlan plan_tier(const TierHardware& hw, const TierRequestOpts& rq, con
uint64_t dyn_target = std::min<uint64_t>(4096 * MiB, p.vram_budget / 8); // a bigger LRU pool beat a bigger static prefix on a 5090 (decode +5..20 %, prefill with prefetch +7 %)
int slots = static_cast<int>(dyn_target / std::max<uint64_t>(1, rt.max_slab));
slots = std::max(24, std::min(slots, 800));
const bool pool_mode = rq.pool == 1 || (rq.pool == -1 && p.vram_budget <= 30 * GiB); // measured on a 4090 (24 GB): one preloaded LRU pool decodes +34 % over prefix + 500 slots, same tokens
const bool pool_mode = pool_wanted; // measured on a 4090 (24 GB): one preloaded LRU pool decodes +34 % over prefix + 500 slots, same tokens
uint64_t dyn_bytes_planned = 0;
uint64_t pool_free = vram_free_for_experts;
if (pool_mode) {
// measured at the end of the start-up (4090 / A4000 / 3060 / 3060 Ti): 1.7 - 2.1 GiB of the card were still free with the plan above (the 600 MiB reserve, the 2.5 % margin and an
// activation estimate that is ~0.5 GiB high). The pool takes 1.1 GiB of that, so about 0.6 GiB stay free; the MTP sidecar (0.92 GiB) and its buffers come out of the same slack.
const uint64_t bonus = 1100 * MiB, mtp_cost = rq.mtp_bytes ? rq.mtp_bytes + 200 * MiB : 0;
pool_free += bonus > mtp_cost ? bonus - mtp_cost : 0;
// the MTP sidecar's resident bytes (and its buffers) come out of the pool's memory
const uint64_t mtp_cost = rq.mtp_bytes ? rq.mtp_bytes + 200 * MiB : 0;
pool_free = pool_free > mtp_cost ? pool_free - mtp_cost : 0;
slots = static_cast<int>(pool_free / std::max<uint64_t>(1, rt.max_slab));
slots = std::max(24, std::min(slots, 8192));
p.pool_mode = true;
@@ -86,9 +86,28 @@ int main(int argc, char** argv) {
hw.vram_bytes = 24 * GiB; hw.ram_bytes = 128 * GiB;
p = plan_tier(hw, rq, rt, sz);
std::printf("%s\n", p.describe().c_str());
CHECK(p.ok && p.mode == TierMode::B && p.lookahead_k == 4 && p.vram_ranks > 0 && p.vram_ranks < n_exp);
// the default for cards up to 30 GiB is the pool layout: no static prefix, every card byte for experts is a slot
CHECK(p.ok && p.mode == TierMode::B && p.lookahead_k == 4 && p.pool_mode && p.vram_ranks == 0 && p.dyn_slots > 1000);
CHECK(p.vram_prefix_bytes == 0 && p.ram_prefix_bytes == rt.total && p.ssd_bytes == 0 && p.ring_slots == 0);
CHECK(p.vram_total_planned <= p.vram_budget);
// the old layout (hot prefix + a small pool) is --tier-pool off
rq.pool = 0;
p = plan_tier(hw, rq, rt, sz);
std::printf("%s\n", p.describe().c_str());
CHECK(p.ok && p.mode == TierMode::B && p.lookahead_k == 4 && !p.pool_mode && p.vram_ranks > 0 && p.vram_ranks < n_exp);
CHECK(p.vram_prefix_bytes + p.ram_prefix_bytes == rt.total && p.ssd_bytes == 0 && p.ring_slots == 0);
CHECK(p.vram_total_planned <= p.vram_budget);
rq.pool = -1;
// an MTP sidecar takes its bytes out of the pool
{
const int s0 = p.dyn_slots;
(void)s0;
TierPlan q0 = plan_tier(hw, rq, rt, sz);
rq.mtp_bytes = 1ull * GiB;
TierPlan q1 = plan_tier(hw, rq, rt, sz);
rq.mtp_bytes = 0;
CHECK(q1.dyn_slots < q0.dyn_slots && q1.vram_total_planned <= q1.vram_budget);
}
// 8 GiB card + 16 GiB RAM budget: Mode C, ring, SSD share
hw.vram_bytes = 8 * GiB; hw.ram_bytes = 64 * GiB;
rq.ram_budget = 16 * GiB;