Skip to content

feat(qwen4_exp): the Flash Next window and expert store come from the memory plan - #357

Merged
Brooooooklyn merged 2 commits into
mainfrom
feat/kv-int8-fn1-flash-next-plan
Nov 12, 2026
Merged

Brooooooklyn merged 2 commits into
mainfrom
feat/kv-int8-fn1-flash-next-plan

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Contributor

Stage FN1 of the Splash int8 KV alignment (stack #344). Base: S4 (#356).

The Flash Next (qwen4_exp) context window and expert store now come from the one memory plan, the same as Splash planStreamedMemory + evaluateEngineMemoryPlan for a target that cannot spill.

load: descriptors → price fixed weights + experts + row-cache share
      → KV allowance first (every lane: min(trained, 64K))
      → experts whole if they leave allowance + row share, else stream with the rest as cap
      → floors over the hard budget refuse before any payload read
after load: re-plan from what is resident → pool grows from a 96-block runway to the plan max

Changes

  • The KV allowance comes first; the store cap is what is left. The host only gates allocations (a refusal gives partial slots; the plan keeps its cap).
  • The pool starts at a 96-block runway and grows by extents under the plan ceiling. Rows, MTP draft state and the transient share the dynamic budget.
  • The page prices the indexer block keys. The cell is the real storage (115,651,584 B; Splash cellBytes 115,802,112).
  • --max-memory reaches qwen4_exp loads. /v1/models shows a load-free estimate (estimateQwen4ExpContextWindow, descriptors only).
  • Deleted: effective_context_limit (the .min(32_768) cap and its 2 GiB f32 estimate), the fixed 1 GiB pool, the host-sized weight budget, the 2 GiB prefill scratch cap, and the 4 GiB WORKING_BYTES share.

Plan numbers

Input Store cap Window
36 GB (WS 30,150,672,384) 14,459,363,158 AR 65,536 (was 32,768)
36 GB + MTP weights 13,180,462,934 AR 95,225 / MTP 65,655
128 GiB working set experts whole 262,144

Tests

  • Rust (one build): memory_plan load_plan qwen4_exp hybrid_scheduler engine paged_kv_cache_adapter — 974 passed, 0 failed, 12 ignored (real-checkpoint tests).
  • Each new test has a named mutation that went red (MUTATIONS.md, 30 mutations).
  • cargo clippy --all-targets -D warnings, cargo fmt --check, yarn typecheck (tsc -b --force), vp lint --type-aware --type-check, vp fmt --check: clean.
  • vp test: 12 files, 252 passed.

Not in this PR

  • The live load gate on a real Flash Next checkpoint is not run.
  • On a 36 GB Mac the host check can halve the 1,024-token prefill chunk at load (known gap 17).
  • The MTP lane's draft state is booked for the whole turn at admission, not grown per step as in Splash.

🤖 Generated with Claude Code

@Brooooooklyn
Brooooooklyn added this pull request to stack #344 November 10, 2026 07:40
@coderabbitai

coderabbitai Bot commented Nov 10, 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: 7f4a3a78-6077-4e0d-9d2e-1cc710be2d3e

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 10, 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:53:21.147757Z b3f945c 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: 63b270953b

ℹ️ 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-fn1-flash-next-plan branch from 63b2709 to b43b26a Compare November 10, 2026 09:31
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-fn1-flash-next-plan branch from b43b26a to 6bf14f1 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-s4-memory-plan branch from 254506b to 19da873 Compare November 12, 2026 15:48
Base automatically changed from feat/kv-int8-s4-memory-plan to main November 12, 2026 15:48
Brooooooklyn and others added 2 commits November 12, 2026 23:49
… memory plan

Splash planStreamedMemory + evaluateEngineMemoryPlan for a target that
cannot spill. Before any payload read the load prices the fixed weights
(hot set less the routed experts, MTP draft banks, vision tower), the
routed experts (all, and a top-k floor) and the row cache's 128K-token
share from the tensor descriptors.

- The KV allowance comes first: the dynamic bytes at which every lane the
  load serves (AR, and MTP with MTP weights) holds min(trained, 64K). The
  experts load whole only where they leave the allowance and the row
  share; otherwise they stream with a cap of what is left. Floors over the
  hard budget refuse before any read.
- The host only gates allocations: a refusal gives partial slots of what
  it admits while the plan keeps the store at its cap (hostHeldBackBytes).
- After the load the engine plan is evaluated from what is resident; a
  shortfall under the allowance comes back from a streamed cap before any
  slot exists. The pool starts at a 96-block runway and grows by extents
  to the plan's maximum under its ceiling, with its registration kept
  current. Rows, MTP draft state and the transient share the dynamic budget.
- The page prices the indexer's block keys; the cell is the real storage
  (115,651,584 B; Splash cellBytes 115,802,112).
- --max-memory reaches qwen4_exp loads, and /v1/models shows a load-free
  estimate (estimateQwen4ExpContextWindow, descriptors only).

36 GB input (Splash's M5 Max, WS 30,150,672,384; hard 24,554,329,702):
allowance 4,468,093,952, row share 1,744,830,464, store cap
14,459,363,158, AR window 65,536 (2,080 pages from a 96-page runway).
With MTP weights: allowance 5,746,994,176, store 13,180,462,934, AR 95,225,
MTP 65,655. A 128 GiB working set loads the experts whole: 262,144.

Deleted: effective_context_limit (the .min(32,768) cap and its 2 GiB f32
estimate), the fixed 1 GiB pool, the host-sized weight budget (plan,
automatic_budget, refresh_plan, direct_plan, DIRECT_LRU_BYTES), the 2 GiB
prefill scratch cap and the 4 GiB WORKING_BYTES wired/state share.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t gates real allocations

- The expert inventory counts transformers `layers.N.mlp.experts.*` as routed
  experts beside GGUF `_exps`, so an HF checkpoint streams under the plan
  instead of refusing its floors (Splash `targetWeights` / `streamedExperts`).
- A refused scheduled-state need gives free pool extents back, then idle
  owners, before it errors (Splash `Cache::takeReclaim`): an MTP start after a
  long AR turn no longer finds the grown pool charged against it.
- The load admits the fixed banks it builds, not the store's whole cap; the
  cap stays a plan number held back (Splash `ExpertStore`, `hostHeldBackBytes`).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-fn1-flash-next-plan branch from 6bf14f1 to b3f945c Compare November 12, 2026 15:49
@Brooooooklyn
Brooooooklyn merged commit af646e8 into main Nov 12, 2026
@Brooooooklyn
Brooooooklyn deleted the feat/kv-int8-fn1-flash-next-plan branch November 12, 2026 15:49
Brooooooklyn added a commit that referenced this pull request Nov 12, 2026
…indow (#358)

Stage S22 of the Splash int8 KV alignment (stack #344). Base: FN1
(#357).

`mlx serve` and `mlx agent` take `--max-context N`, as Splash 1.6 does.
Past the native window, the target RoPE uses static YaRN.

```
--max-context N  (K = 1024, M = 1,000,000; 1..=1,010,000; unset = auto)
  N ≤ native (262,144) → caps every window; RoPE unchanged (bit for bit)
  N > native           → YaRN factor N/native on the target RoPE; the draft keeps its own
  N > default-lane plan window → the load fails (Splash ServingRuntime::start text)
families: qwen3_5, qwen3_5_moe, qwen4_exp; others refuse the option
```

## Splash rules ported
- YaRN math as Splash `rotaryInverseFrequencies` / `attentionFactor`:
beta_fast 32, beta_slow 1, rotated q/k dims × `0.1·ln(f)+1`. YaRN is on
only where native is 262,144.
- The plan prices the served window. Flash Next's KV allowance is N.
- The default lane (DFlash2 with a draft, else the pool, else the flat
lane) refuses an N above its plan window. The other lanes clamp to N.
- A checkpoint whose RoPE is already scaled is refused (`rope_type` /
GGUF `rope.scaling.type`).
- The cold-tier key gets `rope yarn <%.17g> <native>` only with YaRN.
The saved `config.json` never carries the factor. Training refuses a
YaRN load.
- Deleted: the four draft-window caps and the draft's
`max_position_embeddings` accessors (Splash caps by the target only).

## Tests
- 400K: factor 1.5625, attention factor `0x3f85b665`, and Splash's table
bit for bit. A 100K cap publishes 102,400; 400K plans 409,600. A flat
load whose 4 GiB plan holds 45,056 rows refuses 45,057.
- 28 Rust and 15 TS named mutations, each red when on and green when
off.
- `cargo fmt --check`, `cargo clippy --all-targets -D warnings`, `yarn
typecheck`, `vp lint`, `vp fmt --check`: clean. Rust filters `nn::rope
engine:: paddleocr_vl qwen3_5 qwen4_exp functional hybrid_scheduler`:
all pass (`RUST_TEST_THREADS=1`).
- One TS test fails, `bge-native`: the reranker drift that #348 fixes.
#348 is not in this stack's base.

## Not in this PR
- No live run past 262,144 tokens. The live check (100K / 400K on
qwen3.8-27b, and T=0 output unchanged with no flag) is not run.
- Splash's prefill `attentionPairs` cap is not ported. Fused
`mlx_qk_norm_rope` steps aside under YaRN, so decode past native is
slower.
- YaRN angles match Splash within f32 tolerance, not bit for bit (MLX
inverts the periods on the GPU).

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

Co-authored-by: Claude Opus 5.5 <[email protected]>

@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: b3f945c92b

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


/// `indexBytesPerLayerPage`.
fn index_bytes_per_layer_page(&self) -> u64 {
KV_PAGE_TOKENS / ATTENTION_BLOCK_TOKENS * self.index_dimension * 2

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Price index keys using the checkpoint compression ratio

For an accepted checkpoint with indexer_compress_ratio other than 4, this prices a different number of index keys than the decoder stores. Config::validate permits these ratios, and attention_window.rs creates one index key per c.indexer_compress_ratio tokens. For example, a ratio of 2 requires twice the index bytes charged here. Because both the planned window and the scheduled-state pool charge use this layout, such a checkpoint can exceed its planned memory budget. Carry the checkpoint's compression ratio into the layout calculation, or explicitly reject unsupported ratios.

Useful? React with 👍 / 👎.

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