Skip to content

perf(cache): release superseded FP16 KV buffers in every model's prefill - #3096

Merged
inureyes merged 3 commits into
mainfrom
update/issue-3092-kv-growth-release
Nov 15, 2026
Merged

inureyes merged 3 commits into
mainfrom
update/issue-3092-kv-growth-release

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

A chunked-prefill piece grows every layer's dense FP16 KVCache, and each superseded buffer sat in MLX's allocator cache until the engine flushed after the piece (#672). #3091 released those buffers for DeepSeek V2 only, through explicit KVCache::reserve_rows calls. The release now runs inside the shared FP16 growth in KVCache::update_fp16, so every model on the dense cache gets it without per-model calls.

  • A multi-token growth of an existing buffer empties the allocator cache (returning what earlier layers' growths released), builds the grown buffer with the same rule as before, and queues its copy with async_eval. Growth is still per layer at append time; reserving every layer at forward start was rejected in perf(models): release superseded KV buffers in DeepSeek V2 prefill #3091.
  • First fills, decode rows, appends that fit, and the INT8/Turbo, rotating and pool-backed layouts do not take this path. The grown buffer is the one the lazy growth built, so outputs are byte-identical.
  • DeepSeek V2's two reserve_rows calls and reserve_rows itself are removed; its decompressed and latent caches both reach update_fp16, and its peak is unchanged against main.

Measurements (M5 Max, 28K-token prompt, 2048-token pieces, order-reversed pairs)

model peak, main peak, branch prefill change output
qwen3-4b-4bit 10.94 GB 7.26 GB -1.1% identical
meta-llama-3.1-8b-instruct-4bit 10.39 GB 8.09 GB +2.9% identical
gemma-3-4b-it-4bit 5.25 GB 4.86 GB +2.7% identical
deepseek-v2-lite-chat-4bit-mlx 20.20 GB 20.20 GB +0.2% identical

Single pass is unchanged in peak and output for all three families. Gemma's smaller saving is expected: only its 5 global layers use KVCache. Details and raw rows are in docs/benchmark_results/kv-growth-release-all-models-m5max-2026-11-15.md. Arithmetic is unchanged (same buffers, same writes), so no logit trace applies; greedy output is byte-identical in every cell.

Test plan

  • cache::fp16_growth_tests (growth across pieces, trim, no-growth, decode, first fill, INT8, pool-backed), cache::kv_trim_tests, mla::, causal-mask roster test
  • tests/kv_growth_release.rs: the releasing piece parks 151 MB against 403 MB for the lazy-growth control; with the release disabled it reads 403 MB and fails
  • Narrow clippy for mlxcel-core (lib and tests) and mlxcel, cargo fmt --check, env-var docs, benchmark raw-log and kernel dtype-key checks

Closes #3092

@inureyes inureyes added type:performance Performance improvements priority:high High priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) status:review Under review labels Nov 15, 2026
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 15, 2026
A chunked-prefill piece grows every layer's dense FP16 KVCache, and each superseded buffer lands in MLX's allocator cache, where no later request in the piece can reuse it, until the engine flushes after the piece (#672). #3091 fixed this for DeepSeek V2 only, through explicit KVCache::reserve_rows calls.

The release now lives in the shared FP16 growth inside update_fp16: a multi-token growth of an existing buffer empties the allocator cache, builds the grown buffer, and queues its copy with async_eval. First fills, decode rows, appends that fit, and the quantized, rotating and pool-backed layouts are unchanged. The grown buffer is the one the lazy growth built, so outputs are byte-identical. DeepSeek V2's explicit calls and reserve_rows are removed.

Unit tests cover growth, trim, no-growth, decode, INT8 and pool-backed caches; tests/kv_growth_release.rs guards the allocator-cache effect against a lazy-growth control (and fails with the release disabled).

Refs #3092
Main against branch on M5 Max, 28K-token prompt, 2048-token pieces (order-reversed pairs) and single pass for qwen3-4b-4bit, meta-llama-3.1-8b-instruct-4bit and gemma-3-4b-it-4bit, plus DeepSeek V2 for consistency with #3091. Adds the CHANGELOG entry.

Refs #3092
@inureyes
inureyes force-pushed the update/issue-3092-kv-growth-release branch from f548e12 to d854eb7 Compare November 15, 2026 20:13
@inureyes
inureyes marked this pull request as ready for review November 15, 2026 20:13
@inureyes
inureyes merged commit af1ac45 into main Nov 15, 2026
@inureyes
inureyes deleted the update/issue-3092-kv-growth-release branch November 15, 2026 20:13
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(cache): reserve KV cache growth per layer for every model during chunked prefill

1 participant