diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index c28fa5a8..913c1890 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -3087,22 +3087,50 @@ int Engine::size_checkpoint_store(int64_t slot, const char* what) { int64_t still = cfg_.prefix_cache_mib * ((int64_t)1 << 20); for (const auto& r : ranks_) still += arena_host_bytes(r->program); - std::string limit; - int64_t room = lockable_bytes(still, host_headroom_bytes(), &limit); - if (!device_is_host()) { - bool lifted = false; - if (const int64_t cap = amdgpu_pin_cap(&lifted); cap > 0) { - int64_t pinned = 0, mapped = 0; - rad_dev_alloc_totals(RAD_MEM_HOST_PINNED, &pinned, nullptr); - rad_dev_alloc_totals(RAD_MEM_HOST_MAPPED, &mapped, nullptr); - const int64_t left = std::max(cap - pinned - mapped - still, 0); - if (left < room) { - room = left; - limit = fmt("the amdgpu driver's pin cap of %s, of which this process holds %s " - "and still pins %s", humanb(cap).c_str(), - humanb(pinned + mapped).c_str(), humanb(still).c_str()); + /* The fewest snapshots the store means to hold, in bytes: the count below which prefix reuse + * is not worth the pinning. It is what the reserve below must not decide away, and it scales + * with the model's own state -- any hybrid's linear layers, any KV encoding's mirror -- since + * it is measured from this rank's slot and the host's own spare memory. */ + const int64_t floor_slots = + std::min(checkpoint_slots_want(*pol, cfg_.max_seqs, ctx, interval), + kCheckpointSlotsFloor); + const int64_t floor_bytes = + slot > INT64_MAX / std::max(floor_slots, 1) ? INT64_MAX : floor_slots * slot; + const int64_t asked = host_headroom_bytes(); + int64_t margin = asked; + std::string limit, gave_way; + /* What the host can pin with `marg` held free: spare memory (MemAvailable and every memory + * cgroup), then the driver's own pin cap. Both are the host's own measurement, so a wider + * mirror, a bigger pool or a larger slot never needs a second rule here. */ + auto room_with = [&](int64_t marg, std::string* why) { + int64_t r = lockable_bytes(still, marg, why); + if (!device_is_host()) { + bool lifted = false; + if (const int64_t cap = amdgpu_pin_cap(&lifted); cap > 0) { + int64_t pinned = 0, mapped = 0; + rad_dev_alloc_totals(RAD_MEM_HOST_PINNED, &pinned, nullptr); + rad_dev_alloc_totals(RAD_MEM_HOST_MAPPED, &mapped, nullptr); + const int64_t left = std::max(cap - pinned - mapped - still, 0); + if (left < r) { + r = left; + *why = fmt("the amdgpu driver's pin cap of %s, of which this process holds %s " + "and still pins %s", humanb(cap).c_str(), + humanb(pinned + mapped).c_str(), humanb(still).c_str()); + } } } + return r; + }; + int64_t room = room_with(margin, &limit); + if (const int64_t keep = host_reserve_for_floor(floor_bytes, room, margin); keep < margin) { + /* The host has the bytes, but the reserve stood between them and a store that works. + * Take back exactly the shortfall and say so; a host that cannot pin the floor even + * then still gets the honest warning below. */ + const int64_t wider = room_with(keep, &limit); + gave_way = fmt("; the host reserve gave back %s of its %s for the floor of %lld " + "snapshot(s), holding %s", humanb(margin - keep).c_str(), + humanb(asked).c_str(), (long long)floor_slots, humanb(keep).c_str()); + room = wider; } const int64_t total = meminfo_bytes("MemTotal"); const int64_t share = total > 0 ? total / 8 : room; @@ -3122,19 +3150,19 @@ int Engine::size_checkpoint_store(int64_t slot, const char* what) { else if (c.bound == CheckpointSlots::Bound::Room) bound = fmt("; %lld wanted, held to what the host can pin: %s", (long long)c.want, limit.c_str()); - decide("--checkpoint-slots", std::to_string(c.slots), need + bound); + decide("--checkpoint-slots", std::to_string(c.slots), need + bound + gave_way); if (c.slots == 0) { - RAD_WARN("checkpoint slots: auto finds no room for one %s snapshot of %s (%s), so every " - "prefix hit on this model prefills from the start. Free host memory, or lower " - "--host-pool-mib or --prefix-cache-mib", what, humanb(slot).c_str(), - limit.c_str()); + RAD_WARN("checkpoint slots: the host cannot pin one %s snapshot of %s: %s%s. Every prefix " + "hit on this model prefills from the start. Free host memory, or lower " + "--host-pool-mib, --prefix-cache-mib or --host-headroom-mib", what, + humanb(slot).c_str(), limit.c_str(), gave_way.c_str()); return RAD_OK; } const std::string line = fmt("checkpoint slots: auto: %lld %s snapshot(s) of %s in pinned host memory (%s), none of " - "it on the card; %s%s", (long long)c.slots, what, humanb(slot).c_str(), - humanb(slot * c.slots).c_str(), need.c_str(), bound.c_str()); - if (c.bound == CheckpointSlots::Bound::Room) RAD_WARN("%s", line.c_str()); + "it on the card; %s%s%s", (long long)c.slots, what, humanb(slot).c_str(), + humanb(slot * c.slots).c_str(), need.c_str(), bound.c_str(), gave_way.c_str()); + if (c.slots < floor_slots || c.bound == CheckpointSlots::Bound::Room) RAD_WARN("%s", line.c_str()); else RAD_INFO("%s", line.c_str()); return RAD_OK; } @@ -3265,7 +3293,14 @@ int Engine::configure_kv() { }; if (!sized) return refuse("no host has"); std::string why; - if (!lockable_room(slot * n, 0, host_headroom_bytes(), false, &why)) + /* The same reserve rule the sizing used, or a count that auto could fit by giving the + * reserve back would be refused a few lines after it was chosen. */ + std::string guard_why; + const int64_t want_bytes = slot * n; + const int64_t marg = host_reserve_for_floor( + want_bytes, lockable_bytes(0, host_headroom_bytes(), &guard_why), + host_headroom_bytes()); + if (!lockable_room(want_bytes, 0, marg, false, &why)) return refuse("does not fit: " + why); if (ck_store_.open(slot, slot * n) != RAD_OK) return refuse(std::string("the host did not pin: ") + rad_dev_last_error()); diff --git a/core/mem/prefix.cpp b/core/mem/prefix.cpp index d73906c7..b103ceb6 100644 --- a/core/mem/prefix.cpp +++ b/core/mem/prefix.cpp @@ -122,8 +122,7 @@ CheckpointSlots checkpoint_slots_auto(const CheckpointPolicy& policy, int64_t ma int64_t share_bytes, int64_t room_bytes) { CheckpointSlots c; c.per_seq = policy.held(ctx, interval) + 1; - const int64_t seqs = std::max(max_seqs, 1); - c.want = seqs > INT64_MAX / c.per_seq ? INT64_MAX : seqs * c.per_seq; + c.want = checkpoint_slots_want(policy, max_seqs, ctx, interval); if (slot_bytes <= 0) return c; const int64_t share = std::max(share_bytes, 0) / slot_bytes; const int64_t room = std::max(room_bytes, 0) / slot_bytes; @@ -136,6 +135,24 @@ CheckpointSlots checkpoint_slots_auto(const CheckpointPolicy& policy, int64_t ma return c; } +int64_t checkpoint_slots_want(const CheckpointPolicy& policy, int64_t max_seqs, int64_t ctx, + int64_t interval) { + const int64_t per = policy.held(ctx, interval) + 1; + const int64_t seqs = std::max(max_seqs, 1); + return seqs > INT64_MAX / per ? INT64_MAX : seqs * per; +} + +int64_t host_reserve_for_floor(int64_t need_bytes, int64_t room_bytes, int64_t margin_bytes) { + if (need_bytes <= 0 || margin_bytes <= 0 || room_bytes >= need_bytes) return margin_bytes; + /* Whole MiB, the unit --host-headroom-mib takes, and the shortfall rounding up: the reserve + * gives back at least what the floor asked for, so the store is never a byte short of it. */ + const int64_t mib = (int64_t)1 << 20; + const int64_t shortfall = (need_bytes - room_bytes + mib - 1) / mib; + const int64_t margin = margin_bytes / mib; + const int64_t keep = margin - shortfall; + return std::min(margin, std::max(kHostReserveFloor / mib, keep)) * mib; +} + /* ------------------------------------------------------------------ configure */ int PrefixCache::configure(const Config& cfg, KVManager* kv, int64_t checkpoint_interval) { diff --git a/core/mem/prefix.h b/core/mem/prefix.h index 792dc7a3..a8ab500e 100644 --- a/core/mem/prefix.h +++ b/core/mem/prefix.h @@ -174,6 +174,9 @@ std::unique_ptr make_checkpoint_policy(const std::string& name * the room is a hard bound. */ constexpr int64_t kCheckpointSlotsFloor = 8; +/* The least --host-headroom-mib is reduced to for a store's floor (host_reserve_for_floor). */ +constexpr int64_t kHostReserveFloor = (int64_t)1 << 30; + struct CheckpointSlots { int64_t per_seq = 0; /* snapshots a sequence is given */ int64_t want = 0; /* --max-num-seqs x per_seq */ @@ -181,10 +184,28 @@ struct CheckpointSlots { enum class Bound { Want, Share, Room } bound = Bound::Want; }; +/* What --checkpoint-slots auto wants before any byte budget applies: the policy's hold for one + * --max-model-len session plus one for its running turn, for each of --max-num-seqs. */ +int64_t checkpoint_slots_want(const CheckpointPolicy& policy, int64_t max_seqs, int64_t ctx, + int64_t interval); + CheckpointSlots checkpoint_slots_auto(const CheckpointPolicy& policy, int64_t max_seqs, int64_t ctx, int64_t interval, int64_t slot_bytes, int64_t share_bytes, int64_t room_bytes); +/* The reserve to charge a store whose floor `need_bytes` must fit. `room_bytes` is what the host + * can pin with `margin_bytes` (--host-headroom-mib) held free; it is what the engine measures, not + * what a model or a KV encoding is assumed to cost. + * + * The reserve is a courtesy to the system -- the process's unpooled allocations, the load's + * staging windows, the page cache -- and a store that turns prefix reuse on or off outranks it: a + * store too small for one snapshot re-prefills the whole conversation on every turn, while the + * reserve it displaced is memory the host would merely have kept. So the reserve gives back + * exactly the shortfall, never below kHostReserveFloor, and never above the margin asked for. + * It answers `margin_bytes` unchanged whenever the floor already fits, so a host with room to + * spare never sees the reserve move. */ +int64_t host_reserve_for_floor(int64_t need_bytes, int64_t room_bytes, int64_t margin_bytes); + /* ------------------------------------------------------------------ a release */ /* What PrefixCache::release_session took out and what it left. Blocks are left for three * reasons, counted apart so a caller can tell a half-shared conversation from a busy one: the diff --git a/docs/GUIDE.md b/docs/GUIDE.md index 58abf901..a08087f8 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1589,6 +1589,23 @@ none of it on the card; 10 a sequence for --max-num-seqs 8: geometric-backoff ke 200000-token session at a 2048-token interval, and one more for a running turn ``` +**The floor outranks the reserve.** A store one snapshot short of its floor is not a smaller store: +it is every prefix hit prefilling from the start. `--host-headroom-mib` is a courtesy to the system +— the unpooled allocations, the load's staging, the page cache — and it may not, alone, decide the +feature off. So when the floor does not fit with the reserve held free, the reserve gives back +exactly the shortfall, never below 1 GiB and never above the margin asked for, and the startup line +says how much it gave: + +``` +checkpoint slots: auto: 8 linear-state snapshot(s) of 112.73 MiB ... ; 16 wanted, held to what the +host can pin: ... ; the host reserve gave back 835.00 MiB of its 4.00 GiB for the floor of 8 +snapshot(s), holding 3.19 GiB +``` + +It is a byte rule over the host's own measurement, so it needs no second case for a wider slot (a +model with more linear state), a wider streaming mirror (any KV encoding), or a larger `--tp`: a +host that can pin the floor gets it, and one that cannot says what it is short. + A line that says `held to an eighth of host memory`, or (a warning) `held to what the host can pin`, means the store is smaller than the policy wants; `--checkpoint-slots N` sets the count outright. An agent client with subagents keeps more @@ -1939,7 +1956,7 @@ by 0.002-0.003 nats. | `--attn-prefill-fp8 MODE` | off | `off`, `qk`, `pv` or `both`: the prefill attention's Q·Kᵀ, P·V or both in E4M3 over an `fp8`, `fp8_v4` or `k6_v4` cache; lossy, Qwen3.8-27B (§4.4) | | `--host-pool-mib N` | 0 | pinned host tier (weights) | | `--kv-resident N` | 0 (off) | stream the sparse attention group's KV from pinned host memory, keeping a ring of N tokens on the card. Raised to 32 768 and to one decode step's selection; a ring that covers the context streams nothing. Ignored with a warning on an architecture that cannot stream (§4.4.1, [KV-STREAMING.md](KV-STREAMING.md)) | -| `--host-headroom-mib N` | 4096 | host RAM left free for the system when startup pins host memory — kept for the unpooled allocations, staging and page cache (§4.3) | +| `--host-headroom-mib N` | 4096 | host RAM left free for the system when startup pins host memory — kept for the unpooled allocations, staging and page cache (§4.3). A snapshot store's floor takes back the shortfall from it, down to 1 GiB, rather than come up with none (`--checkpoint-slots`, §4.8) | | `--embedding-placement WHERE` | ram | `ram` or `vram`: the token embedding pinned in host memory beside `--host-pool-mib` and read in place, or on the card (§4.3) | | `--ngram-placement WHERE` | auto | `auto`, `vram`, `ram` or `disk`: an n-gram table on the card, as a copy in RAM made at load and read by the card, or read from the container (§4.3) | | `--vision-tower-placement WHERE` | vram | `vram` or `ram`: the encoder towers (vision, and audio where a model has one) on the card, or pinned in host memory beside `--host-pool-mib` and read in place once an image (§4.3) | @@ -1970,7 +1987,7 @@ by 0.002-0.003 nats. | `--deterministic` | off | pin placement to the loader's plan, so the resident set cannot change under the heat engine | | `--checkpoint-interval N` | 2048 | linear-attention state checkpoint interval, in tokens | | `--prefill-span N` | 1 | checkpoint intervals one step may carry of a prefill alone in its step, from the prompt's second interval on; needs `--max-num-batched-tokens` of N intervals. Every other step is planned at the budget less the extra intervals, and the checkpoints inside a span are not written (§4.5) | -| `--checkpoint-slots N` | auto | linear-state (hybrid) or sliding-window snapshots the server may hold at once, in total, not per sequence. They live in pinned host memory, allocated at startup: one slot is a snapshot of every rank's shard (113 MB on Flash-Next at `--tp 2`, 144 MiB on Qwen3.8-27B; on Gemma 4 31B the 1024-token window of every sliding layer, 406 MiB at `--tp 2`). `auto` gives each of `--max-num-seqs` what `--checkpoint-policy` keeps for a `--max-model-len` session plus one (10 at 200K tokens under geometric backoff), within an eighth of host memory and what the host can pin; the startup line and the report say what it chose and why (§4.8). A full store retires its least recently used snapshot. A count is taken as given: a store the host memory cannot hold or pin stops the start, and a table placed in host memory leaves room for it; `0` serves with none, every prefix hit then prefilling from the start | +| `--checkpoint-slots N` | auto | linear-state (hybrid) or sliding-window snapshots the server may hold at once, in total, not per sequence. They live in pinned host memory, allocated at startup: one slot is a snapshot of every rank's shard (113 MB on Flash-Next at `--tp 2`, 144 MiB on Qwen3.8-27B; on Gemma 4 31B the 1024-token window of every sliding layer, 406 MiB at `--tp 2`). `auto` gives each of `--max-num-seqs` what `--checkpoint-policy` keeps for a `--max-model-len` session plus one (10 at 200K tokens under geometric backoff), within an eighth of host memory and what the host can pin; a floor of 8 (or what the policy wants, if fewer) outranks `--host-headroom-mib`, which gives back exactly the shortfall down to 1 GiB rather than take the store to none, and the startup line says so. The startup line and the report say what it chose and why (§4.8). A full store retires its least recently used snapshot. A count is taken as given: a store the host memory cannot hold or pin stops the start, and a table placed in host memory leaves room for it; `0` serves with none, every prefix hit then prefilling from the start | | `--checkpoint-policy NAME` | geometric-backoff | also `tip-only`, `keep-all` | | `--prefix-resume MODE` | exact | `exact`: a prefix hit resumes only where an idle server's cold prefill of the prompt starts a chunk, and a prompt's tail snapshot is taken where its last chunk starts, so a hit answers bit-identically to a cold run; an interval past the chunk is rounded up to a multiple of it. `nearest`: the deepest snapshot or block, up to a chunk less re-prefilled a turn | | `--no-prefix-cache` | prefix cache on | disable prefix caching | diff --git a/spec.md b/spec.md index 04f8e7cf..9ff1e7e2 100644 --- a/spec.md +++ b/spec.md @@ -931,7 +931,7 @@ utilisation fraction: |---|---| | `--gpu-headroom-mib` | VRAM left unclaimed on every card (default 96) | | `--host-pool-mib` | the pinned host tier | -| `--host-headroom-mib` | host RAM left free for the system at startup (default 4096) | +| `--host-headroom-mib` | host RAM left free for the system at startup (default 4096); a snapshot store's floor takes back the shortfall from it, down to 1 GiB | | `--weights-disk-tier` | the file tier for weights, read from the container; needs `O_DIRECT` | | `--max-model-len`, `--max-num-seqs` | the KV cache's bound | | `--kv-resident` | stream the sparse attention group's KV from pinned host memory, keeping a ring of N tokens on the card; off by default. Raised to 32 768 and to one decode step's selection; a ring that covers the context streams nothing; ignored with a warning on an architecture that cannot stream (docs/KV-STREAMING.md) | diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index 94aa2592..b38a8ee2 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -3416,6 +3416,39 @@ TEST(a_sessions_standing_share_is_what_retention_keeps) { CHECK_EQ((int64_t)most, 9ll); } +/* The reserve --host-headroom-mib charges a store's floor: it gives back exactly what the floor is + * short, never below a gigabyte, never above what was asked for, and stays put when the floor + * already fits. A byte rule, so a wider slot (any hybrid model's linear state) or a wider mirror + * (any KV encoding) needs no second case. */ +TEST(the_host_reserve_gives_way_to_a_stores_floor_and_no_further) { + const int64_t gib = (int64_t)1 << 30, mib = (int64_t)1 << 20; + const int64_t slot = 113 * mib, floor8 = 8 * slot, reserve = 4 * gib; + + /* The floor fits: the reserve never moves, so a host with room to spare sees nothing. */ + CHECK_EQ(host_reserve_for_floor(floor8, 20 * floor8, reserve), reserve); + CHECK_EQ(host_reserve_for_floor(floor8, floor8, reserve), reserve); + /* Short: the shortfall comes back in whole MiB, rounded up so the store is never a byte + * short of its floor, and the rest of the reserve stays. */ + CHECK_EQ(host_reserve_for_floor(floor8, slot / 2, reserve), reserve - 848 * mib); + CHECK_EQ(host_reserve_for_floor(floor8, floor8 - 1, reserve), reserve - 1 * mib); + CHECK_EQ(host_reserve_for_floor(floor8, floor8 - mib, reserve), reserve - 1 * mib); + CHECK_EQ(host_reserve_for_floor(floor8, floor8 - mib - 1, reserve), reserve - 2 * mib); + /* A floor beyond both the spare memory and the reserve: down to the floor, never below. */ + CHECK_EQ(host_reserve_for_floor(64 * gib, 0, reserve), gib); + /* Never above the margin asked for, so a small --host-headroom-mib is still a real bound. */ + CHECK_EQ(host_reserve_for_floor(floor8, 0, 512 * mib), 512 * mib); + /* Nothing to protect. */ + CHECK_EQ(host_reserve_for_floor(0, 0, reserve), reserve); + CHECK_EQ(host_reserve_for_floor(floor8, 0, 0), 0); + + /* The count a floor is taken from: the policy's hold plus a running turn, per sequence. */ + const GeometricBackoff g(2048); + CHECK_EQ(checkpoint_slots_want(g, 8, 200000, 2048), 80ll); + CHECK_EQ(checkpoint_slots_want(g, 1, 200000, 2048), 10ll); + CHECK_EQ(checkpoint_slots_want(g, 0, 200000, 2048), 10ll); + CHECK_EQ(checkpoint_slots_want(KeepTipOnly(), 1, 200000, 2048), 2ll); +} + /* --checkpoint-slots auto: the policy's share plus one for each sequence, held to the share of * memory but not below the floor, and to the room the host has. */ TEST(the_auto_store_gives_every_sequence_its_sessions_share) {