Skip to content

perf(cache): write paged KV rows in place under lookahead decode - #1965

Merged
inureyes merged 1 commit into
mainfrom
update/issue-1964-paged-inplace-write
Sep 24, 2026
Merged

inureyes merged 1 commit into
mainfrom
update/issue-1964-paged-inplace-write

Conversation

@inureyes

Copy link
Copy Markdown
Member

Paged KV pool writes now go in place when no one else can see the rows. Under the default lookahead decode, PagedBlockPool::write_block's slice_update could 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.

  • The write uses 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.
  • Shared blocks are copied to a fresh block before any write (existing COW), and a first write to a fresh or recycled row keeps 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=0 restores the copying path; docs updated.

Measurement

M1 Ultra, mlxcel-server defaults (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):

Model Arm conc 1 decode conc 4 aggregate
Llama 3.1 8B 4-bit copying 46.4 to 47.9 98.0 to 98.2
Llama 3.1 8B 4-bit in-place 100.7 to 105.1 135.0 to 135.7
Qwen2.5 7B 4-bit copying 67.2 to 69.5 117.4 to 117.6
Qwen2.5 7B 4-bit in-place 103.4 to 109.0 132.1 to 137.4

Diagnosis: --kv-cache-budget none did not change the slow run; MLXCEL_FORCE_SYNC=1 and --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

  • Replies identical with the switch on and off on both models: four concurrent greedy prompts (two rounds) plus seven sequential requests with prompt-cache restores.
  • New pool test: an in-place write leaves another block's rows intact, and a write into a shared block copies so an existing slab view keeps its bytes; dropping the exclusivity check fails it.
  • make verify-test: 11432 passed, 0 failed. Workspace clippy and fmt pass.
  • Not measured: CUDA, ROCm, concurrency above 4.

Closes #1964

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
@inureyes inureyes added status:review Under review type:performance Performance improvements priority:high High priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:macos macOS (Apple Silicon) specific labels Sep 24, 2026
@inureyes
inureyes merged commit af6e98d into main Sep 24, 2026
20 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Sep 24, 2026
@inureyes
inureyes deleted the update/issue-1964-paged-inplace-write branch October 6, 2026 12:12
@inureyes inureyes added this to the 0.8 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 platform:macos macOS (Apple Silicon) specific 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(server): paged KV writes copy the whole slab under lookahead decode

1 participant