Skip to content

perf(server): paged KV writes copy the whole slab under lookahead decode #1964

Closed

Description

@inureyes

Problem

With default server settings, paged-capable models (Llama, Qwen3 and others) decode a single request at about half the dense-cache speed. Llama 3.1 8B 4-bit on M1 Ultra, scripts/bench_serving_concurrency.py at concurrency 1, 512 prompt / 128 generated tokens:

Configuration Decode tok/s
Default (paged storage, lookahead pipeline on) 46.5-48.2
MLXCEL_FORCE_SYNC=1 81.5
--kv-cache-budget none 46.5-47.9 (unchanged, so the block budget is not the cause)
--decode-storage-backend dense 104.7-109.5

--ctx-size looked like a fix only because it sets max_kv_size, which makes lookahead_params (src/server/batch/scheduler/decode_tick.rs:350) return None and disables lookahead.

Mechanism

PagedBlockPool::write_block (src/lib/mlxcel-core/src/cache/paged.rs:1283) writes each new K/V row with slice_update on the layer's pool slab and relies on buffer donation. Under lookahead the previous step's command buffer still holds the slab, so donation fails and every write copies the whole slab: 1024 blocks x 32 x 8 x 128 x 2 bytes = 64 MiB for K and again for V, per layer. With lookahead forced on, decode drops from 76-79 tok/s at a 256-block slab to 52.7-54.6 at a 4096-block slab, while sync decode at 4096 blocks stays at 84. Same root cause as #1959 on the dense cache.

Proposed fix

Write in place with inplace_slice_write when the block is held by exactly one sequence (shared blocks are already copied before writes) and already owned its row before the write. First writes to a fresh or recycled row keep the copying path.

Acceptance Criteria

  • Server concurrency-1 decode with default settings measured before and after on Llama 3.1 8B and one more paged-capable family
  • Concurrency 4 not regressed
  • Greedy replies identical with MLXCEL_KV_INPLACE_WRITE on and off, including prompt-cache restores
  • Paged pool tests pass, plus a test that an in-place write never changes another block's rows or a shared block
  • Benchmark record in docs/benchmark_results/

Follow-up

Even sync paged decode (81-87 tok/s) is slower than dense (105-110) at concurrency 1. Measure that gap after this fix lands.

Activity

  1. added
    status:in-progress
    type:performance
    priority:high
    area:core
    platform:macos
    status:review
    and removed
    status:in-progress
    on Sep 24, 2026
  2. added
    status:done
    and removed
    status:review
    on Sep 24, 2026
  3. added a commit that references this issue on Sep 29, 2026
    af6e98d
  4. added this to the 0.8 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:coreplatform:macospriority:highstatus:donetype:performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions