Skip to content

feat: unify radix prompt cache with paged block pool (sub-step b of #121) - #168

Merged
inureyes merged 1 commit into
mainfrom
feat/121-radix-pool-unification
Jun 7, 2026
Merged

inureyes merged 1 commit into
mainfrom
feat/121-radix-pool-unification

Conversation

@inureyes

@inureyes inureyes commented Jun 7, 2026 •

Copy link
Copy Markdown
Member

Summary

Part of #121 — sub-step (b) of three. Sub-step (a) (pool-backed KVCache for paged Fp16 sequences) is already merged.

Unifies the cross-request radix prompt-prefix cache with the paged block pool so two requests sharing a prompt prefix store that prefix's KV once in the pool and the second request reuses it via paged adopt/donate instead of re-prefilling. The try_adopt_cached_prefix Paged guard is removed.

Changes

Store entry union (src/server/prompt_cache/entry.rs)

Replaces the dense-only DetachedHolder with an enum DetachedKvSet { Dense(DetachedCacheSet), Paged(DetachedPagedCacheSet) } plus a DetachedKvSetHolder Send/Sync newtype. CacheEntry::new / take_detached now carry either variant; size_bytes snapshots DetachedKvSet::nbytes() so byte-budget eviction accounting is backend-agnostic. The variant-aware DetachedKvSet::is_empty() gates a paged set on its block table / visible length (its dense per-layer handles are empty by design) rather than on the dense caches. store.rs/lookup.rs are unchanged (they work off size_bytes / has_detached). Added PromptCacheStore::min_prefix_tokens() for the donate-side screen below.

Scheduler wiring (src/server/batch/scheduler.rs)

  • try_adopt_cached_prefix: drops the DecodeStorageBackend::Paged early return and dispatches adoption on the union — Dense => cache_pool.adopt, Paged => cache_pool.adopt_paged. Skips (cold prefill) on a backend/variant mismatch, and on any skip path that took paged block pins it releases them via release_detached_paged (a paged set dropped on the floor would leak its pins). The empty check is variant-aware.
  • donate_finished_sequence_cache: drops the dense-only rejection and detaches Dense => detach, Paged => detach_paged, still skipping ModelOwned. Short paged prefixes are length-screened against min_prefix_tokens() before detaching so the store never takes block pins it would only have to release.

adopt_paged pool-backed reconstruction (src/lib/mlxcel-core/src/cache/paged_detach.rs) — key correctness change

adopt_paged_preserving previously rebuilt dense caches from the detached handles. For a pool-backed sequence those handles are empty (K/V lives in the pool), so the adopted sequence read garbage. It now decides pool-backing with the same gate CachePool::allocate_with_layout uses (model.sequence_state_layout().backend == DenseKvCache && paged_layout.cache_mode == Fp16); when pool-backed it rebuilds the per-layer caches with KVCache::new_paged sharing the adopted block-table state_rc + the active pool (the block table already points at the pinned, refcounted pool pages, so the prefix is read with no copy), and restores each cache's monotonic offset from its detached handle so post-adopt RoPE positions continue from the cached prefix length. Non-Fp16 / model-owned paged sets keep the dense-from-handles path.

Copy-on-write

Concurrent prefix-share COW is already provided by the pool (from #118/#120): once an adopted sequence shares blocks (refcount>1), the first write past the shared prefix forks the block. No new COW code — the adopt path just shares the blocks correctly.

Tests

  • cache::paged_detach::tests::adopt_paged_reconstructs_pool_backed_caches_and_shares_prefix — detach→park→adopt of a pool-backed sequence: adopted caches are pool-backed, restored offset == prefix length, shared block ids, live_blocks not doubled, gather_visible equals the original, and a suffix allocates only its own blocks.
  • cache::paged_detach::tests::pool_backed_sharers_cow_keeps_other_prefix_intact — two live pool-backed sequences sharing a partial-tail prefix; a divergent suffix on one forks the shared tail (COW) while the other's prefix stays bit-identical.
  • server::batch::scheduler_prompt_cache_tests::paged_detached_set_round_trips_through_cache_entry / empty_paged_detached_set_reports_empty — a real DetachedKvSet::Paged round-trips through CacheEntry::new / take_detached (one-shot) and nbytes() / is_empty() dispatch to the paged arm.
  • tests/paged_prefix_share_parity.rs (#[ignore], qwen3-0.6b-4bit, soft-skip) — sequence A stores its prefix in a real PromptCacheStore; sequence B looks it up, adopt_pageds it (reusing A's prefix blocks, refcount asserted across the handoff), prefills only its suffix and decodes identically to a cold no-cache run.

Deferred (not in this sub-step)

Validation

  • cargo check (both crates, --tests), cold cargo clean -p mlxcel-core && cargo clippy -p mlxcel-core --tests --features metal,accelerate -- -D warnings, and root cargo clippy --tests --features metal,accelerate -- -D warnings are clean; cargo fmt --check passes.
  • The metal-linked test binaries cannot be built in this sandbox (Metal toolchain absent), so the unit tests above and the real-model parity test are intended to be run by the orchestrator:
    • cargo test --lib -p mlxcel-core --features metal,accelerate cache::paged_detach::tests
    • cargo test -p mlxcel --lib --features metal,accelerate server::batch::scheduler_prompt_cache_tests
    • cargo test --test paged_prefix_share_parity --release --features metal,accelerate -- --ignored --nocapture

Orchestrator verification (all green, M1 Ultra, metal+accelerate): cold clippy clean (both crates); cache::paged_detach 21 passed (incl. adopt_paged_reconstructs_pool_backed_caches_and_shares_prefix + COW + offset); scheduler_prompt_cache 15 passed; full core regression 834 passed; and the real-model DoD paged_prefix_share_matches_cold_run PASSED — sequence B reused sequence A's 2-block prefix from the radix store and decoded 16 tokens identically to a cold no-cache run.

Lets two requests sharing a prompt prefix store its KV once in the paged pool and reuse it via paged adopt/donate instead of re-prefilling, removing the `try_adopt_cached_prefix` Paged guard (part of #121 sub-step b).

Replace the prompt-cache store's dense-only `DetachedHolder` with a `DetachedKvSet { Dense(DetachedCacheSet), Paged(DetachedPagedCacheSet) }` union (plus a `DetachedKvSetHolder` Send/Sync newtype). `CacheEntry::new`/`take_detached` now carry either variant and `size_bytes` snapshots `DetachedKvSet::nbytes()` so byte-budget eviction accounting is backend-agnostic. The variant-aware `DetachedKvSet::is_empty()` gates a paged set on its block table / visible length (its dense handles are empty by design) rather than the dense caches.

Wire the scheduler for both backends: `try_adopt_cached_prefix` drops the Paged backend guard and dispatches adoption on the union (`Dense => cache_pool.adopt`, `Paged => cache_pool.adopt_paged`), skipping on a backend/variant mismatch and releasing any paged block pins it declines to use; `donate_finished_sequence_cache` drops its dense-only rejection and detaches dense via `detach`, paged via `detach_paged`, still skipping `ModelOwned`. Short paged prefixes are length-screened before detaching so the store never takes block pins it would only have to release.

Fix `adopt_paged_preserving` to reconstruct POOL-BACKED caches: for a dense-natural Fp16 sequence (the same gate `allocate_with_layout` uses) the adopted per-layer caches are rebuilt with `KVCache::new_paged` sharing the adopted block-table state and the active pool — the prefix is read straight from the pinned, refcounted pool pages with no copy — and each cache's monotonic offset is restored from its detached handle so post-adopt RoPE positions continue from the cached prefix length. The previous dense-from-handles rebuild handed pool-backed sequences empty buffers. Non-Fp16 / model-owned paged sets keep the dense-from-handles path.

Concurrent prefix-share copy-on-write is already provided by the pool (#118/#120): once an adopted sequence shares blocks (refcount>1) the first write past the shared prefix forks the block.

Add a `min_prefix_tokens()` accessor to `PromptCacheStore` for the donate-side length screen.

Tests: CachePool-level `adopt_paged_reconstructs_pool_backed_caches_and_shares_prefix` (pool-backed adopt, shared block ids, restored offset, no double allocation, gather equality) and `pool_backed_sharers_cow_keeps_other_prefix_intact` (two live sharers, divergent-suffix COW fork, other prefix intact); a root-crate `DetachedKvSet::Paged` round-trip through `CacheEntry`; and an ignored real-model `paged_prefix_share_parity` proving B reuses A's stored prefix blocks and decodes identically to a cold run.

Deferred: APC block-hash matching for paged entries; releasing store-declined/evicted paged pins under pool block-budget pressure (#122); distributed serde of `DetachedPagedCacheSet` (#125).
@inureyes
inureyes merged commit 029a7aa into main Jun 7, 2026
4 checks passed
@inureyes
inureyes deleted the feat/121-radix-pool-unification branch June 7, 2026 05:14
@inureyes inureyes added this to the 0.2 milestone Jun 21, 2026
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes added status:done Completed type:enhancement New features, capabilities, or significant additions labels Sep 6, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…ng (lablup#168)

Lets two requests sharing a prompt prefix store its KV once in the paged pool and reuse it via paged adopt/donate instead of re-prefilling, removing the `try_adopt_cached_prefix` Paged guard (part of lablup#121 sub-step b).

Replace the prompt-cache store's dense-only `DetachedHolder` with a `DetachedKvSet { Dense(DetachedCacheSet), Paged(DetachedPagedCacheSet) }` union (plus a `DetachedKvSetHolder` Send/Sync newtype). `CacheEntry::new`/`take_detached` now carry either variant and `size_bytes` snapshots `DetachedKvSet::nbytes()` so byte-budget eviction accounting is backend-agnostic. The variant-aware `DetachedKvSet::is_empty()` gates a paged set on its block table / visible length (its dense handles are empty by design) rather than the dense caches.

Wire the scheduler for both backends: `try_adopt_cached_prefix` drops the Paged backend guard and dispatches adoption on the union (`Dense => cache_pool.adopt`, `Paged => cache_pool.adopt_paged`), skipping on a backend/variant mismatch and releasing any paged block pins it declines to use; `donate_finished_sequence_cache` drops its dense-only rejection and detaches dense via `detach`, paged via `detach_paged`, still skipping `ModelOwned`. Short paged prefixes are length-screened before detaching so the store never takes block pins it would only have to release.

Fix `adopt_paged_preserving` to reconstruct POOL-BACKED caches: for a dense-natural Fp16 sequence (the same gate `allocate_with_layout` uses) the adopted per-layer caches are rebuilt with `KVCache::new_paged` sharing the adopted block-table state and the active pool — the prefix is read straight from the pinned, refcounted pool pages with no copy — and each cache's monotonic offset is restored from its detached handle so post-adopt RoPE positions continue from the cached prefix length. The previous dense-from-handles rebuild handed pool-backed sequences empty buffers. Non-Fp16 / model-owned paged sets keep the dense-from-handles path.

Concurrent prefix-share copy-on-write is already provided by the pool (lablup#118/lablup#120): once an adopted sequence shares blocks (refcount>1) the first write past the shared prefix forks the block.

Add a `min_prefix_tokens()` accessor to `PromptCacheStore` for the donate-side length screen.

Tests: CachePool-level `adopt_paged_reconstructs_pool_backed_caches_and_shares_prefix` (pool-backed adopt, shared block ids, restored offset, no double allocation, gather equality) and `pool_backed_sharers_cow_keeps_other_prefix_intact` (two live sharers, divergent-suffix COW fork, other prefix intact); a root-crate `DetachedKvSet::Paged` round-trip through `CacheEntry`; and an ignored real-model `paged_prefix_share_parity` proving B reuses A's stored prefix blocks and decodes identically to a cold run.

Deferred: APC block-hash matching for paged entries; releasing store-declined/evicted paged pins under pool block-budget pressure (lablup#122); distributed serde of `DetachedPagedCacheSet` (lablup#125).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant