Skip to content

fix(core): evict prefix chain tails so leading pages stay reusable - #217

Open
HowardYangAI wants to merge 1 commit into
InferMatrixLab:mainfrom
HowardYangAI:fix/prefix-chain-eviction-order
Open

HowardYangAI wants to merge 1 commit into
InferMatrixLab:mainfrom
HowardYangAI:fix/prefix-chain-eviction-order

Conversation

@HowardYangAI

@HowardYangAI HowardYangAI commented Nov 24, 2026 •

Copy link
Copy Markdown

Required human contributor answers

1. What outcome are you trying to achieve?

Once the cache fills up, the shared part of a prompt should still be usable, instead of having to compute it again while its pages are still cached.

2. What is the highest-leverage variable for that outcome?

Which page eviction drops. Drop a chain's first page and nothing behind it can be found; drop the last one and the pages before it still work.

3. How will this work move that variable rather than create busywork?

Eviction now drops the page that has gone unused the longest and has nothing chained after it, which is a chain's last page. That page is the least useful one at the time, so the same pool saves more repeated work.

4. Who is the customer or user, and what do they actually need?

Anyone running with enable_prefix_caching=True. When several requests share a long prompt, they need that shared part to still be usable once the pool is busy, which is when skipping the work matters most.

PR necessity and bugfix evidence

A PR is needed: the chaining and the eviction order both live in KVCacheManager, so no configuration avoids this. Reported and reproduced in #216: with 90 cached pages, 20 entries survive eviction and the prompt that published them reuses 0 tokens. Reproduced locally on CPU, and each case below has a regression test.

Purpose

Prefix keys are chained: a page's key is computed from every page before it, so a lookup walks from the first page and stops at the first gap. Eviction picked the least recently used entry, and the first page is the one published earliest, so it went first and left every page behind it cached but impossible to find.

  • Each entry records which page comes before it and how many follow, and eviction takes the entry that has gone unused the longest with nothing chained after it.
  • Tails are queued by their own use time, so a hit refreshes the pages it reached and exposing a new tail does not reset its time. Eviction no longer rescans the cache for every page it drops.
  • Queued records that a later hit or a new successor replaces are pruned once they outnumber the cache, so repeated hits on the same cache do not make the queue grow.

vLLM gets the same order without this bookkeeping: a request frees its blocks last page first, so a chain's tail joins the free queue ahead of its head. Our prefix table is filled when a prefill publishes pages and is not tied to when a request finishes, so there is no free step to order, and this PR tracks the tails directly.

Addresses #216.

Test Plan

vllm-rlt Version / Commit: main 4821d47, head 24624ad.

Test Result

Check Result
Pre-commit (Ruff lint, format, CPU regressions) Passed
Full CPU suite 1314 passed, 916 skipped
GPU suites, L20 290 passed, 12 failed (unrelated)

Reproducer from #216, re-requesting a 35-token prompt after a second prompt forced eviction:

Pool pages Entries left Pages found, before → after Tokens reused, before → after
90 20 0 → 3 0 → 6
110 25 0 → 8 0 → 16
140 33 0 → 16 0 → 32

The same entries stay cached either way; the difference is whether they can be found.

Six regression tests in tests/test_prefix_growth.py cover eviction taking tails, a hit's first page outliving its own tail, a hit chain outliving a newer one, repeated hits not growing the queue, draining a long chain without rescanning, and the engine-level re-request. Each one fails if the ordering it checks is reverted.

The unrelated GPU failures, all of which fail the same way on 4821d47:

  • 6 FlashAttention-4 and packed-prefill cases in test_prefix_growth.py; the pinned flash-attn-4 release does not support SM89.
  • 6 test_pd_prefix_reuse_reduces_transfers_and_preserves_outputs cases, which fail with a stage worker error on this host.

Prefix keys are chained: a page's key is computed from every page before
it, so a lookup walks from the first page and stops at the first gap.
Eviction picked the least recently used entry, and the first page is the
one published earliest, so it went first and left every page behind it
cached but impossible to find.

Each entry now records which page comes before it and how many follow,
and eviction takes the entry that has gone unused the longest with
nothing chained after it. Tails are queued by their own use time, so a
hit refreshes the pages it reached and eviction no longer rescans the
cache for every page it drops. Queued records that a later hit or a new
successor replaces are pruned once they outnumber the cache.

Caching a 35-token prompt and letting a second prompt fill the cache, a
re-request reused nothing before and now reuses 6, 16 and 32 tokens in
90-, 110- and 140-page pools.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant