Repository navigation
perf(cache): write paged KV rows in place under lookahead decode - #1965
Merged
Merged
Conversation
With default server settings, paged-capable models decoded a single request at about half the dense-cache speed. PagedBlockPool::write_block wrote each new K/V row with slice_update on the layer's pool slab, relying on donation, but under the default lookahead pipeline the previous step's command buffer still holds the slab, so every write copied the whole slab (64 MiB each for K and V per layer at the default slab size). With lookahead forced on, decode fell from 77 to 54 tok/s as the slab grew from 256 to 4096 blocks, while sync decode stayed at 84. The write now uses inplace_slice_write when the block is held by exactly one sequence and already owned its pool row before the call. Shared blocks are copied to a fresh block before any write, and a first write to a fresh or recycled row keeps the copying path, so a row that another sequence, a snapshot or a finished request's in-flight step can read is never rewritten in place. MLXCEL_KV_INPLACE_WRITE=0 restores the copying path. M1 Ultra, server defaults, bench_serving_concurrency.py 512/128: Llama 3.1 8B concurrency-1 decode 47 to 105 tok/s and concurrency-4 aggregate 98 to 135; Qwen2.5 7B 68 to 108 and 117.5 to 136.5. Replies identical with the switch on and off for concurrent and prompt-cache-restoring requests on both models. A new pool test checks that writes leave other blocks intact and that a shared block copies; dropping the exclusivity check fails it. Closes #1964
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.
Paged KV pool writes now go in place when no one else can see the rows. Under the default lookahead decode,
PagedBlockPool::write_block'sslice_updatecould not donate the layer slab (the previous step's command buffer still held it), so every K/V row write copied the whole slab. This halved single-request decode for paged-capable models with default server settings.inplace_slice_write(from perf(cache): write the decode KV row in place instead of copying the cache #1961) when the block's refcount is 1 and it already owned its pool row before the call.slice_update, so rows another sequence, a snapshot or a finished request's in-flight step can read are never rewritten in place.MLXCEL_KV_INPLACE_WRITE=0restores the copying path; docs updated.Measurement
M1 Ultra,
mlxcel-serverdefaults (paged storage, 4 slots, lookahead on),scripts/bench_serving_concurrency.py --concurrency 1,1,4,4 --prompt-tokens 512 --max-tokens 128, two server starts per arm (ABBA):Diagnosis:
--kv-cache-budget nonedid not change the slow run;MLXCEL_FORCE_SYNC=1and--ctx-size(which disables lookahead) did; with lookahead forced on, decode tracked the slab size (256 blocks 77 tok/s, 4096 blocks 54) while sync did not (84). Record:docs/benchmark_results/paged-lookahead-inplace-write-m1ultra-2026-09-24.md.Validation
make verify-test: 11432 passed, 0 failed. Workspace clippy and fmt pass.Closes #1964