Skip to content

feat(paged-attn): one recency clock, leaf-only reuse, covered reserve (S18a) - #345

Merged
Brooooooklyn merged 1 commit into
mainfrom
feat/kv-int8-s18a-allocator
Nov 12, 2026
Merged

Brooooooklyn merged 1 commit into
mainfrom
feat/kv-int8-s18a-allocator

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Contributor

What

Stage S18a of the Splash int8 KV alignment. It stacks on #343 (S3). The reference is Splash 9124ff2c (1.6.0).

Every block allocator now orders and reuses its cached blocks the way Splash KvCache does:

before                                   after (Splash KvCache / MemoryLedger)
LinkedHashSet LRU of hashes              one u64 clock (CacheRecency)
victim = oldest hash = chain HEAD        victim = oldest cached LEAF nothing holds
  (a 4-block chain loses block 0,          (A→B→C→D loses D; A→B→C still hits)
   so the prefix hit drops to 0)
reserve: take one, retry, roll back      reserve(n): take nothing unless free + evictable
  (evicts cached blocks, then fails)       cover n, at every pool size; the caller grows first
prefix cache entry cap                   removed (it never fired in production)

The stamp rules:

  • A block is stamped on commit, on the tip of a hit, and on release.
  • A parent that becomes a leaf takes max(own, child) (KvCache::inherit).
  • A refresh stamps nothing.

mlx-node choices, not in Splash (labelled in code)

  • Pinned-interior fallback. When no dead leaf is left, allocate takes the deepest cached block whose cached children are all held. Splash moves a request onto the block it finds. mlx-node's verified-duplicate skip links the new tail under the found block, so without the fallback that block could never be reused.
  • Wave gate. At the max pool, a batched decode wave reserves all of its rows or none. Splash admits one claim at a time; mlx-node records a wave row by row.
  • Turn-end stamp for kept tables. A table kept past its turn is freed at its turn-end stamp.

Families

  • Shared allocator change: all families that use the allocator, which are lfm2, nemotron_h, qwen3, qwen3_5 dense/MoE, qwen3_asr, k2_horizon, gemma4, muse_glimmer and qwen4_exp.
  • No state marks are set yet (S18b does that). So reusable equals evictable, and no admission decision and no qwen3_5 reclaim order changes.
  • Which requests fail does not change. The only difference is that a refused reserve no longer evicts cached blocks before it rolls back.

Tests

  • 19 new tests, each with a named mutation that went red, then green after the revert. Examples:
    • allocate_takes_the_chain_tail_and_keeps_the_prefix_hit
    • parent_takes_its_child_stamp
    • a_hit_touches_its_tip_only
    • reserve_evicts_nothing_it_cannot_cover
    • below_max_refused_reserve_takes_nothing_after_the_grow
    • max_pool_wave_records_nothing_it_cannot_cover (adapter and muse_glimmer)
  • Gates:
    • cargo fmt --check and cargo clippy --all-targets -D warnings pass.
    • cargo test -p mlx-paged-attn --lib passes: 212 tests.
    • cargo test -p mlx-core --lib, run over every touched family and module, passes: 2113 tests.

CI does not run on this PR until its base is main.

🤖 Generated with Claude Code

@Brooooooklyn
Brooooooklyn added this pull request to stack #344 November 8, 2026 17:14
@coderabbitai

coderabbitai Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c200b45f-cd66-47cb-b3e9-4b2233d302fa

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T15:48:20.580651Z be2afca New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fecd770225

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Brooooooklyn
Brooooooklyn removed this pull request from stack #344 November 12, 2026 15:43
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s3-moe-int8 branch from addf0f0 to 6be43ea Compare November 12, 2026 15:45
Base automatically changed from feat/kv-int8-s3-moe-int8 to main November 12, 2026 15:45
Every block allocator now orders and reuses its cached blocks the way
Splash KvCache does.

Recency
- One saturating u64 clock per allocator (`CacheRecency`, as Splash
  `CacheRecency::next`). A block is stamped when it is committed (new or
  a verified duplicate), when a hit ends on it, and when its last
  request lets go of it (as Splash `KvCache::releaseActive` stamps a
  released block with no children). A refresh stamps nothing.
- A hit stamps its tip only: Splash `Cache::lookup` touches the tip and
  the attach after it calls `KvCache::retainActive` on the chain's last
  block alone.
- A parent that becomes a leaf takes max(own, child) when nothing holds
  it (Splash `KvCache::inherit`).

Reuse
- `allocate` takes a free id, else the oldest cached leaf nothing holds
  and no state mark protects (Splash `KvCache::orders`: a block enters a
  leaf order only with no resident children and no users).
- `reserve(n)` takes nothing unless free plus evictable blocks cover n,
  at every pool size (Splash `MemoryLedger::free` counts with `bound`
  and calls `take` only when the count covers the need). Blocks the pool
  has not grown yet do not count: the caller grows first, as Splash
  `KvPool::takeFreePages` grows an extent before the claim reaches the
  ledger. A refused reserve now evicts nothing; which requests fail is
  unchanged.
- Telemetry reports reclaimable blocks (marked chains included) apart
  from evictable ones.
- The hashlink LRU, the prefix-cache entry cap and
  `evict_oldest_cache_only_leaf` are removed. With no state marks set,
  reusable equals evictable, so no admission decision and no qwen3_5
  reclaim order changes.

mlx-node choices, not in Splash
- Pinned-interior fallback: when no dead leaf is left, `allocate` takes
  the deepest cache-only block whose cached children are all held,
  outside every marked chain. Splash moves a request onto the block it
  finds when it publishes, so it never has one; mlx-node's
  verified-duplicate skip keeps the request on its own block and links
  its new tail under the found one, and without the fallback such a
  block could never be reused.
- Wave gate: at the max pool, `check_wave_fits` checks that free plus
  evictable blocks cover the whole wave before any row of a batched
  decode records, so a wave reserves all or nothing. Splash admits one
  claim at a time through the ledger; mlx-node records a batched wave
  row by row.
- Turn-end stamp for kept tables: a table kept past its turn records the
  stamp at its turn end, and `release_request` frees its blocks at that
  stamp (`free_at`), so its tip is stamped as if the request had ended
  then. Splash has no table that outlives its request; it stamps at
  `KvCache::releaseActive` when the request ends.

Per family
- lfm2, nemotron_h, qwen3_5 dense/MoE, qwen3_asr, k2_horizon and the
  qwen3 batched decode: `record_decode_wave` runs the wave gate.
- qwen3 ragged step: the wave gate over each row's query length.
- gemma4: the batched step runs the gate across both KV groups
  (`check_wave_fits_all`); `reserve_rows_all` admits with free plus
  evictable blocks.
- Muse-Glimmer: the batched step runs the gate across its KV groups
  (`check_wave_fits_all`).
- qwen3_5 DFlash2 prompt growth counts evictable blocks as cached.
- Sliding restore and the cold-tier batch restore take their blocks with
  `reserve`.

The prefix-cache hit-walk bench is renamed prefix_cache_hit_walk_bench.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s18a-allocator branch from fecd770 to be2afca Compare November 12, 2026 15:45
@Brooooooklyn
Brooooooklyn merged commit 92cede3 into main Nov 12, 2026
@Brooooooklyn
Brooooooklyn deleted the feat/kv-int8-s18a-allocator branch November 12, 2026 15:46
Brooooooklyn added a commit that referenced this pull request Nov 12, 2026
…rs (S18b1) (#346)

## What

Stage S18b1 of the Splash int8 KV alignment. It stacks on #345 (S18a).
The reference is Splash `9124ff2c` (1.6.0).

On qwen3_5 dense and MoE, every refused KV reservation now goes through
one loop, `engine::reclaim::free_for_kv`. The loop follows Splash
`MemoryLedger::free` and `Engine::allocate`:

```
refused reserve(n)
  → clear MLX pool, sync marks
  → need met in place?            yes → take nothing
  → count (bound) covers need?    no  → take nothing
  → take units oldest first, one clock, tie → state
      (DFlash2 store states · GDN ladder/history entries · idle owners)
      a unit that frees nothing is passed over
  → until the need is met
```

## What changes

- **One clock** (`CacheRecency`) is shared by the block allocator, the
DFlash2 store, the GDN ladder and history entries, and the idle owners.
- **Ladder entries mark their chain.** `allocate` never takes a chain
that a state restores through.
- **InUse.** The replay point that a running turn restored from is not a
unit until its use ends (Splash `StateCache::useState` / `unuse`).
- **Refusal record per sequence.** It survives a failed prepare's own
release.
- **Paths:**
  - blocked scheduled step (A);
- serial steps, prefix, VLM, cold-restart and MTP-gate prepares
(A-serial);
- admission and prompt grow, where the prompt's own marked hit is left
out of the need (A-admit);
- the flat DFlash2 lane: extents, then compaction, then dead leaves,
then one unit by stamp (B).
- **Shared blocks.** The count for idle owners is what releasing all of
them frees together, so a block shared by two idle tables counts once.
- **Deleted:** `release_one_for_pool_grow`,
`evict_one_parked_dflash2_for_pool_grow` and `POOL_GROW_EVICTIONS`.

## mlx-node choices (labelled in code)

- At the block cap, only tip rungs are ladder units. Store and history
entries are not units there, because they sit on no block.
- At the cap, the met check can return Met for a page need.
`settle_step` frees tables between the refusal and the reclaim.
- Store compaction runs first among the byte units, because it loses
nothing.
- Idle tables are units, ordered by their turn-end stamp. Splash has no
table unit.

## Gaps (left to S11 and S18b2)

- There is no single byte ledger, and there are no claim tiers,
`Pending` or holds.
- Vision entries and the draft need are not units yet. That is S18b2.
- The serial MTP per-cycle lookahead and the flat transient loop keep no
take order.

## Families

- **qwen3_5 dense and MoE:** they take in one stamp order, at the cap
and below it.
- **gemma4, qwen3, lfm2, nemotron_h, muse_glimmer, k2_horizon and
qwen4_exp:** they keep their owner loop and see only inert defaults.

## Tests

- 80 new tests. Each has a named mutation that went red, then green
after the revert (104 mutation runs).
- An adversarial verify step found 6 items, including a regression where
a lone turn failed when two idle owners shared blocks. All 6 are fixed
and confirmed.
- Gates:
- `cargo fmt --check` and `cargo clippy --all-targets -D warnings` pass.
  - `cargo test -p mlx-paged-attn --lib` passes: 212 tests.
- `cargo test -p mlx-core --lib`, run over every touched family and
module, passes: 2598 tests.

CI does not run on this PR until its base is `main`.

## Review rounds 3-6 (2026-11-09)

| Commit | Fix | Splash rule |
|---|---|---|
| `63c7af826` | "Met" means covered: after a grow clamped at the pool
max, the takeable blocks are counted again. The walk re-reads the cap
after each miss. | `KvPool::takeFreePages`, `MemoryLedger::take` |
| `63c7af826` | A lone blocked turn can take a suspended turn's in-use
replay point, last. A yielding lane and admission still cannot.
Preemption keeps the use ("suspended or not"). | `Engine::growthClass`,
`takeRights` |
| `1b6f5d0be` | A kept prompt leaves out its last token; a resume keeps
by its replay tokens. | `Cache::extendMatch`, `restoredChain` |
| `1b6f5d0be` | The reach counts layer bytes left above the pool after a
part-way shrink or grow. | `MemoryLedger::takeUnsized` |
| `257114e7c` | A waiting admission below the cap grows and reclaims
before it waits, sized for the watermark at the grown pool. |
`Engine::allocate` -> `governor.free` |
| `f7d0dc845` | The reach counts a partial last extent (a grow capped by
the pool max or headroom can stop inside an extent). | `KvPool` keeps
whole extents only |

Each fix has tests with named mutations (red, then green). Round 6 also
flagged token-only admission keys across cache salts: that is gap 34
(admission keys and salt), planned for S11, and not part of this PR.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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