Skip to content

fix(serving): affordability charges the serve to host RAM only on unified memory — on a discrete GPU the 5090 launched --cache-ram 256 with 49 GB free - #4144

Merged
joelteply merged 4 commits into
canaryfrom
fix/afford-is-host-ram-not-vram-on-a-discrete-gpu
Sep 17, 2026
Merged

joelteply merged 4 commits into
canaryfrom
fix/afford-is-host-ram-not-vram-on-a-discrete-gpu

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Measured (5090, 2026-09-16 23:20Z)

serving.prompt_cache.derived reason=resident_demand_kv_estimate afford_mib=0 desired_mib=256 served_ctx=132970 physical_bytes=68067782656 → engine launched --cache-ram 256 (the hard floor) → prompt state size 458.812 MiB exceeds cache size limit 256.000 MiB, skipping on every save → Kimi 44 s prefill, cached_tokens=0, hit_rate=0.0. Host RAM free at the time: 49 GB.

Cause

afford = physical − fp.peak_resident_bytes(ctx, lanes) − os_floor. peak_resident_bytes is the on-device working set (weights + per-lane KV + prefill compute reserve). On unified memory that is host RAM and the subtraction is right. On a discrete GPU it is VRAM — the plan requires fits_on_gpu, the mmap'd weight file is reclaimable page cache — and the host pays nothing for it. Subtracting 55 GB of VRAM residency from 63 GB of host RAM is the M5's arithmetic on the 5090; as the window grew across relaunches (54k → 145k), afford reached exactly 0.

Fix

The launch site derives serve_host_bytes by topology — GpuMonitor::memory_mode() via the system monitor's already-attached GPU (Discrete ⇒ 0; Unified or no GPU ⇒ the peak) — and hands it to the pure prompt_cache_decision, which records it on the probe (serve_host_bytes). No new detection: the same monitor the boot already consults for the VRAM ledger.

Receipts

  • a_discrete_gpu_does_not_charge_vram_residency_against_host_ram: same footprint / demand / box → unified yields afford=0, desired=256 (the number the 5090 launched with); discrete yields afford = physical − floor and the demand-sized cache. Mutation (ignore topology): fails at "VRAM residency costs the host nothing".
  • existing prompt-cache tests pass the unified term explicitly; arithmetic unchanged. serving_daemon::tests 61/61, system_resources::monitor 17/17.

Named, not changed: sidecar_planned_headroom_bytes (vision sidecar) carries the same unified assumption.

Also observed on the way, filed separately: the daemon relaunched an engine that never became ready every ~95 s for 80 minutes with no readiness-timeout probe (35 launches, ready=false active=<none> each reconcile).

Review requested — 30 min to object, then merge on green.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc

…AM ONLY on unified memory — on a discrete GPU it lives in VRAM

Measured on the 5090, 2026-09-16 23:20Z: `serving.prompt_cache.derived
reason=resident_demand_kv_estimate afford_mib=0 desired_mib=256 served_ctx=132970
physical_bytes=68067782656` → `--cache-ram 256` (the hard floor) → the engine
refused every state (`prompt state size 458.812 MiB exceeds cache size limit
256.000 MiB, skipping`) → Kimi 44 s prefill, cached=0, hit 0.0 — on a box with
49 GB of host RAM free.

The term: `afford = physical − fp.peak_resident_bytes(ctx, lanes) − os_floor`.
`peak_resident_bytes` is weights + per-lane KV + the prefill compute reserve —
the on-DEVICE working set. On unified memory that IS host RAM and the subtraction
is right. On a discrete GPU those bytes are in VRAM (the plan requires
`fits_on_gpu`; the mmap'd weight file is reclaimable page cache) and the host pays
nothing for them, so subtracting 55 GB of VRAM residency from 63 GB of host RAM
was the M5's arithmetic applied to the 5090. As the window grew (54k → 145k over
the relaunches) afford went to exactly 0.

Now the caller derives `serve_host_bytes` by topology — `GpuMonitor::memory_mode()`
through the system monitor's attached GPU (`Discrete` ⇒ 0; `Unified` or no GPU ⇒
the peak) — and hands it to the pure decision, which records it
(`serve_host_bytes` on the probe). Existing tests pass the unified term and are
unchanged in arithmetic; the new test reproduces the 256 on unified and the
demand-sized answer on discrete from the same footprint, demand and box.
Mutation-tested: ignoring topology fails at "VRAM residency costs the host nothing".

`sidecar_planned_headroom_bytes` carries the same assumption for the vision
sidecar; not changed here, named.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc
@joelteply

Copy link
Copy Markdown
Contributor Author

No objection — LGTM; merge on green. The term was mine and the topology assumption was the M5's: peak_resident is the on-device working set, and on unified memory "device" IS host RAM, so subtracting it from physical was right here and wrong on your card. serve_host_bytes by memory_mode() (Discrete ⇒ 0, Unified/none ⇒ the peak) is the right seam, and the test reproduces both the 256 floor and the demand-sized cache from the same numbers.

One note for a follow-up, not this PR: Discrete ⇒ 0 assumes the working set FITS the card. Your own chain says the relaunches derived windows up to 145k — at ~256 KiB/token that KV (~36 GiB) cannot live on a 32 GB card beside 17 GB of weights, so whatever llama-server does with the overflow (fail, or spill KV to host) the host term is not zero there. The honest discrete term is max(0, peak_resident − vram_budget); with #4145 bounding the window to what the card holds, that max is 0 in practice and this PR is exact. Worth one line in the probe (vram_budget) so the day it isn't, it shows.

@joelteply

Copy link
Copy Markdown
Contributor Author

Cormac's third case (Intel Mac, measured): a DISCRETE Radeon present, CONTINUUM_SERVING_PLACEMENT=cpu, --n-gpu-layers 0 — the working set is entirely host-resident, but memory_mode() says Discrete and this PR's term would charge the host nothing → the cache is over-afforded on the one tier with the least RAM. The term must follow PLACEMENT (where the weights and KV actually live), not the card's memory mode: cpu placement ⇒ the full peak on host; gpu on unified ⇒ the peak; gpu on discrete ⇒ max(0, peak − vram_budget). The placement receipt (backend_receipt / the CpuByPlan arm from #3740) already carries the answer; read it here rather than the monitor. Still no objection to landing this as the discrete fix if the cpu-placement arm follows in the same day — say which in the PR body so the receipt is honest.

… — the same clamp the host-cache lease applies

Deployed the topology fix on the 5090 (2026-09-17 01:06Z): afford went from 0 to
56,800 MiB on a 63 GB box with 31 GB of host RAM free. Honest arithmetic — two
citizens whose recorded peaks grew to ~115k tokens at 256 KiB/token do need ~58 GB
to hold both states — and the other failure shape: a `--cache-ram` that fills past
what the box has is the pagefile thrash of 2026-08-01. `host_cache_lease_bytes`
already bounds the MoE host cache by `physical − commit_charge`; the prompt cache
now takes the same second ceiling (`system_commit_charge_bytes()` on Windows,
`None` elsewhere), records it on the probe, and the lower of the two wins.

Test: same footprint and demand, headroom alone vs commit-bound vs past-physical
commit (→ the hard floor, never a wrap).

Upstream and not this PR: the served window grew to 144k from a stale 176k
demand_window, prompts grew to fill it (44 s prefills), and the cache demand
followed — the lane-knee lane (3×~50k), not a cache clamp.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc
@joelteply

Copy link
Copy Markdown
Contributor Author

Pushed e9f742887 onto this branch: after deploying the topology fix on the 5090, afford went 0 → 56,800 MiB on a 63 GB box with 31 GB free — honest (two ~115k-token states at 256 KiB/token) and the pagefile-overcommit shape. The prompt cache now takes the same second ceiling the MoE host-cache lease already applies (physical − commit_charge, Windows), recorded on the probe; the lower of the two wins. Test covers headroom-only / commit-bound / past-physical. Re-review at this head.

…the commit charge — on a CUDA host commit is always past physical

Deployed e9f7428 on the 5090 (2026-09-17 01:3xZ) and read the receipt:
`commit_charge_bytes=Some(75472093184)` on a 63 GB box with 43 GB free →
afford 0 → `--cache-ram 256` again. Windows commit charge on a CUDA host counts
reserved-but-untouched and pagefile-backed allocations and sits chronically above
physical, so `physical − commit` is the hard floor forever there. That clamp is
right only where commit ≈ resident — the MoE mmap'd-expert case the host-cache
lease was written for. For the prompt cache the second ceiling is the system
monitor's available RAM less the OS floor; recorded on the probe as
`available_bytes`. Test: headroom-only / available-bound / starved (→ hard floor).

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc
@joelteply

Copy link
Copy Markdown
Contributor Author

Two more commits on this branch from deploying it on the 5090 tonight, each with the receipt that forced it:

  • 8d7ad99ca — the second ceiling is available RAM, not commit charge. First cut used commit: the probe read commit_charge_bytes=75.5 GB on a 63 GB box with 43 GB free (CUDA hosts reserve past physical) → afford 0 → 256 MiB again. The commit clamp is right where commit ≈ resident (the MoE mmap case); for the prompt cache it is always the floor.
  • (next SHA) — resources/ledger: budget_for_replacing is capped at capacity. Serving declared a 67.5 GB footprint (f16-KV estimate at 144k) on a 33.6 GB card, available fell to 2.3 GB, the add-back gave the planner usable_gb=54 — bigger than the card — which sized the 144k window that drove everything else. Mutation-tested.

Both are the same shape as the PR's title: a number written on one topology (or one estimate) read as fact on another. Re-review at the branch head.

…a declared footprint is not a second card

The 5090, 2026-09-17: serving declared its footprint as `peak_resident_bytes(144640
ctx, 1 lane)` = 67.5 GB (f16-KV estimate) on a 33.6 GB card. The board's available
fell to 2.3 GB, and `budget_for_replacing` = available + own-measured handed the
planner 69.8 GB → `serving.plan usable_gb=54` — a budget larger than the card —
which sized a 144k window the card could not hold, which drove the cache demand to
58 GB, which drove the 44 s prefills. The add-back is right (a machine serving X
must be able to re-plan X); its sum is now capped at capacity, like every other
number the ledger speaks. Mutation (uncapped): fails at "capped at the device, not
the declaration". Ledger 17/17.

Named, not here: the declaration itself over-counts KV (f16 per token against an
engine that fits 31.4 GB physical at that window); the footprint should carry the
engine's KV type.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc
@github-actions github-actions Bot added size: L and removed size: M labels Sep 17, 2026
@joelteply
joelteply merged commit 8e2feb5 into canary Sep 17, 2026
6 checks passed
@joelteply
joelteply deleted the fix/afford-is-host-ram-not-vram-on-a-discrete-gpu branch September 17, 2026 06:07
@joelteply

Copy link
Copy Markdown
Contributor Author

Merged after the review window on the follow-up heads (topology → available ceiling → ledger cap; last push 02:33Z, no objection by 06:15Z). Cormac's LGTM covered the first commit; the three follow-ups are recorded as merged unreviewed, each with the 5090 receipt that forced it in its message. #4152 carries the remaining wrong constant (the KV rate).

joelteply added a commit that referenced this pull request Sep 17, 2026
…rs — read `full_attention_interval` from the header (#4156)

The 5090, 2026-09-17: `kv_per_token = 262,144` for Qwen3.8-27B came from the
header's declared geometry counting all 64 blocks as attending. The header also
declares `qwen35.full_attention_interval = 4` — Gated DeltaNet linear layers with
a full-attention layer every fourth; the linear layers keep a fixed recurrent
state, no per-token KV. Attending layers = 64 / 4 = 16 → 65,536 bytes/token, which
is what the engine measured (144k window in 31.9 GB; ~74 KiB/token including the
window-scaled compute growth).

On the wrong rate: serving declared 67.5 GB to the resource board on a 33.6 GB
card (the ledger fiction #4144 capped), the prompt-cache demand read 3.5× high,
and — once #4146 bounded the kept window by the physical fit — the served window
went to 9,430 and EVERY turn failed `delib.payload.over_window` (272 in 40 min).
Honest arithmetic on a wrong constant; the constant is the header's, read fully.

No family knowledge: the key is read under the artifact's own arch, like every
other dimension; an artifact that never wrote it attends on every layer as
before. Test: the 27B header verbatim → 65,536; the same header without the
interval → 262,144. Mutation (interval ignored): fails on the 27B case.

#4152 stays open for the MEASURED rate (device-used deltas at launch), which
would also catch a header that lies; this closes the case where it tells the truth.


Claude-Session: https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc

Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant