Repository navigation
perf(cache): release superseded FP16 KV buffers in every model's prefill - #3096
Merged
Merged
Conversation
3 tasks done
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
inureyes
force-pushed
the
update/issue-3092-kv-growth-release
branch
from
November 15, 2026 20:13
f548e12 to
d854eb7
Compare
inureyes
marked this pull request as ready for review
November 15, 2026 20:13
3 tasks done
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
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 explicitKVCache::reserve_rowscalls. The release now runs inside the shared FP16 growth inKVCache::update_fp16, so every model on the dense cache gets it without per-model calls.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.reserve_rowscalls andreserve_rowsitself are removed; its decompressed and latent caches both reachupdate_fp16, and its peak is unchanged against main.Measurements (M5 Max, 28K-token prompt, 2048-token pieces, order-reversed pairs)
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 indocs/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 testtests/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 failsmlxcel-core(lib and tests) andmlxcel,cargo fmt --check, env-var docs, benchmark raw-log and kernel dtype-key checksCloses #3092