Skip to content

fix(server): paged backend declines every partial prefix adoption, and APC+paged loses even multiturn reuse #225

Closed

Description

@inureyes

Measurement context

Found during a cross-version server memory-footprint measurement (v0.1.4 vs main, 2026-06-12) using scripts/bench_memory_footprint.py (commit 18c88c1). Method: one fresh mlxcel serve process per (config, scenario), fixed HTTP workloads at temperature 0, sampling the process phys-footprint via /usr/bin/footprint at 2.5 s (RSS does not see MLX Metal buffers at all). Model: llama-3.2-1b-4bit on M1 Ultra, --max-batch-size 8. Scenarios: idle, shared_prefix_burst (8 concurrent requests sharing a ~3.7k-token system prompt), seqshare (same prefix, 8 sequential requests), multiturn (one conversation, 8 turns), churn (32 distinct prompts). Run-to-run noise: idle/multiturn within 0.5 percent, burst/seqshare/churn peaks within 10 to 25 percent.

Problem

try_adopt_cached_prefix (src/server/batch/scheduler.rs, paged branch around line 1290) declines paged adoption whenever matched_len < paged_seq_len, with partial adoption marked as deferred. Measured consequences over HTTP:

  • Divergent sibling requests sharing a long system prompt never adopt on the default backend (decode_storage auto resolves to paged when batching): burst and seqshare report cached=0 for every request.
  • With --apc-enabled, APC clamps matched_len to a block boundary, so matched_len < entry_len holds for virtually every lookup. Result: paged plus APC adopts nothing at all, including strict multiturn continuation that works without APC (measured: multiturn cached tokens 11067 without APC, 0 with APC).

Fix sketch

Implement partial adoption for paged detached sets: floor matched_len to the pool block boundary, adopt the covered block chain (reusing the existing refcount/pin machinery), release the tail blocks, set the logical length and per-layer offsets, and let the prefill loop re-prefill the divergent tail. Release pins on every decline path. Mind the known trim_tokens front/back asymmetry.

Acceptance criteria

  • APC + paged shows cached>0 for burst, seqshare, and multiturn over HTTP.
  • Outputs byte-identical to a cold prefill (extend the real-model paged_prefix_share_parity style test to a partial match).
  • seqshare footprint for APC+paged at or below APC+dense on the bench harness.

Related

Filed together from the same measurement: #224, #226, #227, #228.

Metadata

Metadata

Assignees

Labels

area:inferencepriority:highstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions