Repository navigation
fix(cache): avoid whole-cache copies when long requests share a batch - #4312
Merged
Merged
Conversation
When a long cached request joined a running decode batch, the batch KV cache briefly needed several times its own size, which could push usage past the memory guard. - Batch KV caches keep the stock logical layout inside a larger buffer that grows on a ratio ladder, so decode steps, merges, joins and filters no longer copy the whole bank. The logical K/V match the stock classes bit for bit. - Batch joins are materialized one layer at a time. - A request drops its restored prompt cache once the batch generator owns it. - Emergency aborts use the stable physical cap. The dynamic ceiling still drives admission and soft/hard pressure. Refs #4213 Co-Authored-By: josh aune <luken@omner.org>
yoyo930021
added a commit
to yoyo930021/omlx
that referenced
this pull request
Oct 11, 2026
BatchQSAKVCache concatenated the whole raw indexer-key bank and its position ids on every append. Each step left the previous exact-width bank in MLX's pool, where it can never satisfy a later (larger) request. Raw keys and positions now live in a buffer on the batch KV capacity ladder of jundot#4312 (minimum grain 1024 columns): - appends write in place; the rank promotion of _append_indexer_positions runs ahead of the write, and a dtype promotion falls back to concatenate - a ragged finalize rolls only the logical prefix - trim moves index_offset and keeps the buffer - extend and filter land their result on the ladder in one copy index_keys / index_position_ids still read and assign as exact-width arrays, and the completed-block bank (jundot#4070) is carried as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
yoyo930021
added a commit
to yoyo930021/omlx
that referenced
this pull request
Oct 11, 2026
qwen4_exp carries its own BatchKVCache class, which the stock-class capacity patch (jundot#4312) does not reach, so the KV bank under BatchQSAKVCache still regrew by concatenation: every 256-token step, merge, join and filter copied the whole bank. install_capacity_methods() installs jundot#4312's methods on one class; apply_batch_kv_capacity_patch() uses it for the two stock classes and the qwen4_exp package calls it for its own. No second copy of the methods. The lifecycle tests of jundot#4312 now also run against the vendored class, compared with a pristine reload of it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 tasks
jundot
pushed a commit
that referenced
this pull request
Oct 19, 2026
…er, and stop extracted rows doubling (#3963) * fix(qwen4_exp): back the BatchQSAKVCache indexer with a capacity buffer BatchQSAKVCache concatenated the whole raw indexer-key bank and its position ids on every append. Each step left the previous exact-width bank in MLX's pool, where it can never satisfy a later (larger) request. Raw keys and positions now live in a buffer on the batch KV capacity ladder of #4312 (minimum grain 1024 columns): - appends write in place; the rank promotion of _append_indexer_positions runs ahead of the write, and a dtype promotion falls back to concatenate - a ragged finalize rolls only the logical prefix - trim moves index_offset and keeps the buffer - extend and filter land their result on the ladder in one copy index_keys / index_position_ids still read and assign as exact-width arrays, and the completed-block bank (#4070) is carried as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(qwen4_exp): keep extracted batch rows from doubling on first append BatchQSAKVCache.extract returns an exact-width, caller-sized row copy but left it marked for geometric growth, unlike QSAKVCache.extract. The row's first append then grew it to max(step, 2 * capacity). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
jundot
pushed a commit
that referenced
this pull request
Oct 20, 2026
…4030) qwen4_exp carries its own BatchKVCache class, which the stock-class capacity patch (#4312) does not reach, so the KV bank under BatchQSAKVCache still regrew by concatenation: every 256-token step, merge, join and filter copied the whole bank. install_capacity_methods() installs #4312's methods on one class; apply_batch_kv_capacity_patch() uses it for the two stock classes and the qwen4_exp package calls it for its own. No second copy of the methods. The lifecycle tests of #4312 now also run against the vendored class, compared with a pristine reload of it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
When a long cached request joins a running decode batch, for example an agent session resuming a 200k-token context while another request is still decoding, the batch KV cache could briefly need far more memory than it holds, and the memory guard stopped both requests. Addresses the long-context batch case in #4213.
This carries the core memory changes from #4081 by @luken. The capacity layout is adapted from #4030.
What changed
Validation
128GB M5 Max emulated on M3 Ultra (dynamic ceiling 91.8GB), Qwen3.8-27B-oQ8e-mtp,
balanced, SSD cache. A 14k-token request is decoding when a 202k-token request restored from the prefix cache joins it.Decode throughput is unchanged (768 tokens, main/PR/PR/main order): 1 request 40.7, 41.1 vs 44.1, 41.2 tok/s; 2 requests 56.0, 58.3 vs 56.5, 57.5 tok/s.