Skip to content

fix: reclaim paged KV blocks during decode under budget pressure - #2077

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-1982-decode-block-reclaim
Sep 30, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-1982-decode-block-reclaim

Conversation

@inureyes

@inureyes inureyes commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

With a paged block budget (the default --kv-cache-budget auto), a decoding row that crossed a block boundary while the pool was full hit "block budget exhausted" inside the model forward. write_paged expects that write to succeed, so the worker panicked and every request returned 503.

What changed

  • Decode reserves the blocks a tick will mint before the forward. The admission reclaim loop moved to scheduler/block_reclaim.rs, and both paths now use it: cold prompt-cache prefixes by LRU, then preemption. Decode uses a preemption floor of 1. A row that still cannot grow is deferred when only a parked chunked prefill's reservation blocks it, and otherwise ends with a "KV cache budget exhausted" error instead of panicking. With room, nothing changes.
  • CachePool::paged_blocks_to_append in mlxcel-core counts per-layer tail blocks plus copy-on-write forks.
  • Deviation from the issue's "existing preemption policy": block reclaim evicts the least-progressed row (priority first, then fewest generated tokens, then newest id). On GB10, longest-first preemption livelocked: 179 preemptions and one completion in ten minutes, with every victim near 250 tokens. Slot preemption keeps its configured policy.
  • GB10 validation exposed four more budget failures, fixed here as well. Equal-priority admissions preempted each other after every prefill (over 1,600 preemptions), so admission now preempts only lower-priority rows. A deferred head spun without decoding (98% CPU stall), so the deferring tick now runs a decode step. Batched prefill had no budget gate and panicked. Decode consumed blocks that a parked chunked prefill still needed, and the next chunk panicked.
  • A separate commit removes a stale cfg(not(feature = "cuda")) gate in sanitize_tests.rs. cargo test --features cuda --lib does not compile on main without it.

Test plan

  • New unit tests: LRU eviction lets a boundary-crossing row proceed (the control shows the append fails without reclaim), no eviction when the pool has room, preemption fallback, shedding with no panic, deferral, the admission priority limit, the chunked-prefill reservation, the batched-window budget, and the copy-on-write count
  • server::batch server::prompt_cache models::sanitize_tests: 674 passed; mlxcel-core cache::paged: 132 passed; clippy -D warnings and fmt are clean
  • GB10, llama-3.1-8b-4bit, 4444-block budget: three concurrent 3-turn conversations with 600-token generations completed all 9 turns (16 reclaim passes, 12 preemptions, no errors). A 2200-token generation completed after decode evicted 3 cached prefixes with no preemption
  • Default auto budget: turns 2 and 3 hit the prompt cache (1376 and 1696 cached tokens) with zero reclaim lines

Not covered: per-layer free lists can diverge from the global free count on non-uniform layouts. An admission watermark to reduce recompute under tight budgets is left as a follow-up.

Closes #1982

load_and_sanitize_weights_dense_repack_env_keeps_non_cuda_affine_rollback now gates on the runtime backend instead of the cuda feature, but EnvRestore::set_with_clear kept its cfg(not(feature = "cuda")) gate, so cargo test --features cuda --lib no longer compiled on main. The helper is needed on every build now.
With a paged block budget, a decoding row that crossed a block boundary while cached prefixes and other rows filled the pool hit "block budget exhausted" inside the model forward, which panics the worker (write_paged expects the pool write to succeed).

Decode now reserves the blocks a tick will mint before the forward: the admission reclaim loop moved to scheduler/block_reclaim.rs and serves both paths (cold prompt-cache prefixes by LRU, then preemption), with a preemption floor of 1 for decode. Rows that still cannot grow are deferred when only a parked chunked prefill's reservation blocks them, and otherwise finished with a "KV cache budget exhausted" error. mlxcel-core gains paged_blocks_to_append to count per-layer tail blocks plus copy-on-write forks.

Validating on GB10 under a 4444-block budget exposed further failures that this change also fixes: longest-first preemption livelocked (179 preemptions, one completion in ten minutes), so block reclaim now evicts the least-progressed row; equal-priority admissions preempted each other after every prefill, so admission only preempts lower priority; a deferred head spun without decoding; batched prefill had no budget gate; and decode consumed blocks a parked chunked prefill still needed.

Unit tests cover LRU eviction, the no-pressure path, preemption fallback, shedding without panic, deferral, and the priority limit. On GB10 three concurrent 600-token multi-turn conversations complete with no errors, and a default-budget run still hits the prompt cache with no reclaim.

Closes #1982
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority labels Sep 30, 2026
@inureyes
inureyes merged commit 929c80a into main Sep 30, 2026
24 of 25 checks passed
@inureyes
inureyes deleted the fix/issue-1982-decode-block-reclaim branch September 30, 2026 07:02
inureyes added a commit that referenced this pull request Oct 2, 2026
Per-layer accounting: a freed paged block stays on its layer's free list and acquire reuses only that list before minting under the pool-wide cap, so `budget - live` over-reported once the lists diverged and a layer could run dry while the figure was positive. `free_block_budget` now reports the largest uniform per-layer step every layer can acquire (own free blocks plus a share of the mint headroom), capped at `budget - live`. Admission, decode reservation and the lookahead prime all read it.

Chunk reservation: `continue_chunked_prefill` reserves its chunk's blocks (tile padding included) against the pool figure before the forward. The set-aside figure saturates at 0, so once the pool fell below a parked prefill's reservation nothing restored it and the chunk could hit an exhausted pool. The reservation evicts cold prefixes and then preempts with floor 0, and covers both the MixedStep tick and the #1011 grant. The reservation itself now counts the next chunk's padding.

Admission watermark: `--kv-admission-watermark` (env `MLXCEL_KV_ADMISSION_WATERMARK`) keeps a fraction of the block budget free at admission while a decode batch is live, evicting cold prefixes toward it before deferring. The batched window and the single-sequence gate charge against the same figure.

The GB10 pressure sweep at higher watermarks exposed two #2077 accounting gaps, fixed here: admission charged an adopted request for its whole prompt although the adopted blocks were already live (a prefix over half the budget wedged the queue with an empty batch), and blocks pinned by queued adoptions were unreachable by reclaim (a lone row was shed). Admission now charges the suffix, and decode and chunk reclaim drop a queued request's adoption after cold prefixes and before preempting.

Refs #2088
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Reclaim paged blocks during decode when the pool budget is exhausted

1 participant