Repository navigation
fix: reclaim paged KV blocks during decode under budget pressure - #2077
Merged
Merged
Conversation
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
This was referenced Oct 1, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_pagedexpects that write to succeed, so the worker panicked and every request returned 503.What changed
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_appendin mlxcel-core counts per-layer tail blocks plus copy-on-write forks.cfg(not(feature = "cuda"))gate insanitize_tests.rs.cargo test --features cuda --libdoes not compile on main without it.Test plan
server::batch server::prompt_cache models::sanitize_tests: 674 passed; mlxcel-corecache::paged: 132 passed; clippy-D warningsand fmt are cleanNot 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