Skip to content

feat(qwen3_5): one Splash 1.6 take order for KV, states and idle owners (S18b1) - #346

Merged
Brooooooklyn merged 7 commits into
mainfrom
feat/kv-int8-s18b1-reclaim
Nov 12, 2026
Merged

Brooooooklyn merged 7 commits into
mainfrom
feat/kv-int8-s18b1-reclaim

Conversation

@Brooooooklyn

@Brooooooklyn Brooooooklyn commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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

@Brooooooklyn
Brooooooklyn added this pull request to stack #344 November 8, 2026 20:57
@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:49:20.111960Z 8099193 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.

@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: 56b1f018-f1ca-4070-a54f-f8eb83e4b37e

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 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: e425884b9d

ℹ️ 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".

@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: 257114e7c8

ℹ️ 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 force-pushed the feat/kv-int8-s18a-allocator branch from bff5ad6 to fecd770 Compare November 12, 2026 13:41
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s18b1-reclaim branch from f7d0dc8 to 3374b50 Compare November 12, 2026 13:41
@Brooooooklyn
Brooooooklyn removed this pull request from stack #344 November 12, 2026 15:43
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s18a-allocator branch from fecd770 to be2afca Compare November 12, 2026 15:45
Base automatically changed from feat/kv-int8-s18a-allocator to main November 12, 2026 15:46
Brooooooklyn and others added 7 commits November 12, 2026 23:46
Every refused KV reservation on qwen3_5 dense and MoE now goes through
one loop, `engine::reclaim::free_for_kv`, after Splash
`MemoryLedger::free` and `Engine::allocate`: clear the MLX pool, sync
marks, a need met in place takes nothing, a count that cannot cover the
need takes nothing, else units go oldest first (a state on a tie, as
`Cache::nextUnit`) until the need is met. A unit that frees nothing is
passed over (`MemoryLedger::take`). `MetBefore` is Splash's `met` list:
the same need after a take or a Met stops the retry.

What changes:
- One clock (`CacheRecency`) for the block allocator, the DFlash2
  store, the GDN ladder and history entries and idle owners. A parked
  live state keeps its turn-end stamp (`insert_at`).
- Ladder entries mark their chain in production, so `allocate` never
  takes a chain a state restores through. Marks sync at prune, at
  `clear_reuse_state`, at `drop_gdn_state` and before every count.
- InUse: the replay point a running turn restored from (text and VLM
  prepares, dense and MoE) is no unit until its use ends (Splash
  `StateCache::useState` / `unuse`); the use end stamps it after the KV
  tail and the history write (`Engine::release`).
- Refusal record per sequence in the adapter (Splash
  `MemoryLedger::refused`), kept across a failed prepare's own release
  with the hit blocks it hands back. `kv_need_met` is the met check;
  the grow probe's room is read live.
- Path A: a blocked scheduled step takes states and idle owners by
  their own stamps (`SchedReclaim`, Splash `Engine::judge`). Its count
  is what releasing every idle owner frees together
  (`BlockAllocator::released_blocks`): a cached block two idle tables
  share counts once, as Splash's bound counts the whole take. An idle
  owner the scheduler never finished goes by its table's turn-end
  stamp. `capacity_exhausted` says what the take order tried.
- Path A-serial: `reclaiming` wraps the serial paged steps, the six
  prefix prepares, the two VLM prepares, the two cold restarts and the
  two MTP gates.
- Path A-admit: admission grow (at the cap too, `cap_shortfall_blocks`),
  prompt grow and admission charges leave the prompt's own marked hit
  out (`VictimQuery::kept`). `try_grow_pool_by`: a `Short` take, or a
  need `MetBefore` saw, is not grown.
- Path B: the flat DFlash2 lane takes free extents, a store compaction,
  dead leaves, then one unit by stamp (store step, ladder or history
  entry, idle table offered only while an extent cut is in reach,
  Splash `leadsToReach`), behind a count (`flat_kv_in_reach`,
  `Cache::takeReclaim`) that gates on the KV alone. A take it lets
  through goes on toward the KV plus its transients until nothing is
  left, so the prefill keeps its transient headroom: the transients have
  no fixed reservation here, unlike Splash's scratch.
- The scheduler snapshot counts only takeable blocks (marked chains
  out); the draft fit adds them back.
- Deleted: `release_one_for_pool_grow`,
  `evict_one_parked_dflash2_for_pool_grow`, `POOL_GROW_EVICTIONS`,
  the per-owner `releasable_blocks_for`.

mlx-node choices (labelled):
- At the block cap only tip rungs are ladder units and store and
  history entries are none: they sit on no block, so taking them there
  only loses cache (Splash takes the oldest state either way).
- At the cap the met check can return Met for a page need.
- Store compaction runs first among byte units: it loses nothing.
- Idle owners' kept tables (path A) and idle tables (path B) are units
  by turn-end stamp; Splash has no table unit.

Gaps (v6 §11): no `Growth` claim, priority tiers or `Pending`; the count
is coarse (blocks plus bytes rounded up plus live room, not one ledger);
the refusal record is per step, not per request claim; `metBefore` is
per retry loop; the kept chain is prefix-matched ladder/history entries,
not `RestoredChains`; admission's marked hit matches tokens, not keys;
the serial MTP per-cycle lookahead and the flat transient loop keep no
take order; vision entries and the draft need are not units yet; a
serial turn that fails before its history write keeps its InUse record
until the next prepare.

Per family: qwen3_5 dense and MoE take in one stamp order at and below
the cap (`reclaims_in_order`). gemma4, qwen3, lfm2, nemotron_h,
muse_glimmer, k2_horizon and qwen4_exp keep their owner loop and one
`try_grow_pool`; they see only the refusal record, the snapshot fed
from evictable blocks (equal to reusable with no marks) and inert trait
defaults.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…store

- A prompt with block keys keeps a ladder rung only when the rung's block
  hashes start the prompt's own salted, keyed chain (Splash
  RestoredChains, Walk::keepsState by hash). Before, the take side kept
  any rung whose tokens prefixed the prompt, while the reach count read
  the hit by hash, so a rung on another cache_salt or image-key chain was
  counted as takeable but never taken and the prepare failed
  context_length_exceeded at the cap. Keep::hit_chain computes the chain
  once for marked_hit_blocks and the new KeptPoints; Keep now flows
  through gdn_unit_excluded, oldest_gdn_state_unit, gdn_state_bytes,
  state_reach_bytes and oldest_state_unit (dense and MoE). Without keys
  the token test stays; the history entry stays by tokens.
- At the cap a mark in no other mark's chain is a unit when its chain
  holds any cache-only block, not only when the mark itself is
  cache-only. A rung whose tip is a held verified-duplicate tail under a
  released ancestor was filtered out, so the cap reclaim had no unit and
  failed with recoverable blocks (Splash Cache::takeOldest takes the
  oldest state either way).
- paged_kv_cache_adapter imports HashSet unconditionally: the reclaim
  code uses it outside the macOS cfg, so the aarch64 Linux build broke.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… its replay-point use

A VLM prepare that skips the lookup (reuse_cache=false) and the cold
restart after a GDN miss now keep no replay point, as Splash keeps the
restored chain only when the request does not skip the cache and retries
a failed admission with skipCache. ensure_paged_pool_for_prompt is split
into ensure_paged_pool_keeping(target, prompt, keep); the dense and MoE
VLM prepares pass Keep::default() when !reuse_cache, and the cold-restart
reclaim always does.

The paged finalize reads the turn's sequence before it releases the
request, and save_paged_history ends that sequence's replay-point use on
the reuse_cache=false return and after the history write, so a released
serial turn (reuse off, or a reuse turn whose reconcile failed) no longer
leaves its rung InUse (Splash Engine::release). Error and abort paths
already clear gdn_in_use through clear_reuse_state.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…in-use replay points

- kv_need_met recounts takeable blocks after a passing grow: a grow
  clamped at the pool max can add less than the shortfall (Splash
  KvPool::takeFreePages). free_for_kv re-reads the cap after each met
  miss, so units that free no block at the cap are passed over.
- A lone blocked turn's need reaches the replay points other turns use,
  suspended or not, after every ordinary unit (Splash Engine::growthClass,
  ReclaimClass::InUse); its count includes their bytes (Splash
  StateCache::reclaims). A yielding lane, admission, serial and DFlash2
  keep them. The use survives preemption and a take.
- Tests that finalize a paged turn hold the sidecar counter lock.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ished shrink bytes

Keep::points tests ladder and history entries against the prompt without
its last token, as the lookup does (Splash Cache::extendMatch): a rung at
the prompt's own block-aligned end is no replay point of it, so an
admission at the max pool may take it instead of rejecting. A resumed
turn charges and keeps by its replay tokens (prompt plus committed
output, Splash exactTokens), the tokens its lookup uses.

release_reach_bytes adds the layer bytes above the pool's block count
that a cut or grow which failed part way left behind (Splash
MemoryLedger::takeUnsized), so a flat DFlash2 growth at the runway sees
them in reach and its first take finishes the cut instead of failing with
context_length_exceeded.

Scheduled turns never skip the cache (reuse_cache=false is rejected
before a scheduled turn is prepared), so admission keeps are unchanged
for skipCache.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A grow capped by the pool max or by headroom can end inside an extent;
the cut takes that partial extent down to the runway, so
release_reach_bytes rounds the extent count up.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s18b1-reclaim branch from 3374b50 to 8099193 Compare November 12, 2026 15:46
@Brooooooklyn
Brooooooklyn merged commit fca6187 into main Nov 12, 2026
0 of 5 checks passed
@Brooooooklyn
Brooooooklyn deleted the feat/kv-int8-s18b1-reclaim branch November 12, 2026 15:46
Brooooooklyn added a commit that referenced this pull request Nov 12, 2026
…e order (S18b2) (#347)

## What

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

It adds the two units that S18b1 left out of the take order:

```
free_for_kv (KV need)                         free_for_draft (draft need, new)
  met in place? → count covers? → take:         met in place (live shortfall)? → count covers? → take:
    states / idle owners by stamp                  idle owners by stamp (tables + parked rows)
    then ONE unheld image entry (Rebuild stage,
    below the block cap only)
```

## What changes

- **Rebuild stage.** A vision cache entry that nothing outside the cache
holds (`Arc::strong_count == 1`) is a unit. The oldest goes first, and
only after the state and owner walk is empty (Splash `Order::Stage`
Rebuild, `TakeRights::rebuiltPools`). It never meets a pure page need
(`Lacking::meets`).
- **Image hold.** A VLM turn holds its image features until the prefill
that reads them is evaluated, then drops them before decode (Splash
`Runtime::prefillAsync`). This applies to dense and MoE, in both paged
VLM turns.
- **Draft need as its own claim.** A short draft fit records a
`DraftRefusal`, next to the KV refusal (`MemoryLedger::refused`).
`free_for_draft` reads the shortfall live and takes idle owners by
stamp.
- **One met list.** A single `MetBefore<(SeqId, StepNeed)>` serves both
kinds of need. A blocked qwen3_5 step with no record takes nothing, so
the old lowest-SeqId owner loop is gone.

## mlx-node choices and gaps (labelled)

- **Re-encode cost.** A dropped image entry is re-encoded on later turns
that send the image, even when the restored KV covers it. Splash
releases covered images and never needs them again.
- **Locked cache.** A locked vision cache counts as unknown
(`u64::MAX`), so the count can over-count but never under-count.
- **Draft need.** Images, states and store entries are not draft units,
because the draft fit counts only pool bytes. Splash's rebuild pools do
serve a byte need.

## Families

- **qwen3_5 dense and MoE:** these get the change.
- **Other families:** they see inert defaults, and the draft map is
cleared every step.

## Tests

- 27 new or changed tests. Each has a named mutation that went red, then
green after the revert (31 mutation runs).
- An adversarial verify step found 4 items. All 4 are fixed or labelled.
- Gates:
- `cargo fmt --check` and `cargo clippy --all-targets -D warnings` pass.
  - `cargo test -p mlx-paged-attn --lib` passes: 213 tests.
- `cargo test -p mlx-core --lib`, run over every touched family and
module, passes: 2635 tests.
- Not run: a live two-turn image chat.

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

## Review round 1 (2026-11-09)

`837e50b54`: `Scheduler::preempt_running_at` clears both reservation
flags before `settle_step` reclaims for the parked turn, while its
caches stay resident. The parked turn now gets both flags back before
the reclaim, as `reinstate_running` does, so its draft state and
bookings count. Tests run through the real `preempt_running` (victim
refused, peer refused); 4 named mutations.

🤖 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