Skip to content

fix(cache): avoid whole-cache copies when long requests share a batch - #4312

Merged
jundot merged 1 commit into
mainfrom
fix/batch-join-kv-memory
Oct 6, 2026
Merged

jundot merged 1 commit into
mainfrom
fix/batch-join-kv-memory

Conversation

@jundot

@jundot jundot commented Oct 6, 2026

Copy link
Copy Markdown
Owner

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

  • 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 cache. 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.

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.

  • Before: the memory guard aborted both requests at 102.9GB.
  • After: both requests completed, with a 84.1GB peak, below the 87.2GB hard watermark.

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.

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>
@jundot
jundot merged commit 5db725c into main Oct 6, 2026
1 check passed
@jundot
jundot deleted the fix/batch-join-kv-memory branch October 6, 2026 06:29
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>
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant