Skip to content

feat(kv-int8): one memory plan sizes the pool, the window and the DFlash2 budget (Splash int8 align S4) - #356

Merged
Brooooooklyn merged 4 commits into
mainfrom
feat/kv-int8-s4-memory-plan
Nov 12, 2026
Merged

Brooooooklyn merged 4 commits into
mainfrom
feat/kv-int8-s4-memory-plan

Conversation

@Brooooooklyn

@Brooooooklyn Brooooooklyn commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

What

Stage S4 of the Splash int8 KV alignment. It stacks on #355 (S28). The reference is Splash 9124ff2c (1.6.0), evaluateEngineMemoryPlan.

One memory plan now sizes the paged pool, both lane windows and the DFlash2 KV budget for qwen3_5 dense and MoE. It replaces three separate budgets (the 0.85 pool probe, DFlash2's 0.95 − 2 GiB then ×0.9, and live_prefill_headroom).

hard    = WS − (15% + 1 GiB), lowered by --max-memory        (S11: Splash max(1 GiB, 2%))
fixed   = MLX active after load + other pools + 64 MiB staging + 256 MiB pipeline + 512 MiB runtime
dynamic = hard − fixed
lane    = lane state + transient(W) + growth copy + KV(W + scratch), in whole extents ≤ dynamic
window  = min(trained, largest W that fits)

⚠️ Window change on a 36 GB M3 Pro (needs your review)

The plan now charges what the old budgets left to the margin: Splash's 768 MiB reserves, the pool-growth copy, and the turn transients (verify partials and paged prefill attention). The margin stays 15% + 1 GiB until the governor (S11), as decided. Together this lowers the windows until S11:

M3 Pro inputs (27 GiB WS, 17,357 MiB active) before S4 S4 with S11's 2% margin
AR / pool window 78,464 48,601 ~120K
DFlash2 window 139,257 61,433 ~176K
pool ceiling (1 MiB blocks) 5,051 3,186 ~7.7K

Prefill chunk ceiling

Every qwen3_5 / MoE paged prefill (text, scheduler, serial, VLM) now runs in chunks of at most 2,048 rows, as Splash ExecutionLimits::prefillTokenBudget does, so the plan prices the chunk the engine runs. MLX_PAGED_PREFILL_CHUNK_SIZE above 2,048 is capped, and 0 (one shot) is gone for these families. CLI launchers already set 2,048.

The pool budget subtracts the lane state first (Splash availableForOneRequestKv = dynamic − laneStateBytes). The scheduler still charges the GDN cell inside the pool too, so the AR window pays it twice versus Splash (50,585 if paid once); that is shared scheduler code and stays a labelled gap.

Deleted (no compatibility path)

MLX_KV_MEMORY_UTILIZATION, MLX_KV_SAFETY_MARGIN_BYTES, DFLASH2_KV_HEADROOM_RESERVE_BYTES, MLX_PAGED_CACHE_INITIAL_MB / paged_cache_initial_memory_mb, the qwen3_5 one-sequence pool cap, qwen3_5/MoE reads of MLX_PAGED_PER_SEQ_CTX, the TS overlay's 16,384 / 2,048 MiB defaults and MLX_PAGED_CACHE_MEMORY_MB. MLX_PAGED_PER_SEQ_CTX stays for the families outside this work.

New

  • --max-memory SIZE (lower only) on mlx serve and mlx agent, through the host to Qwen35LoadOptions.max_memory_bytes. A value above the automatic budget is ignored with Splash's log line.
  • Load errors that name the limit: the plan cannot hold one lane and the runway; an explicit pool above the plan (16,384 MiB on the M3 Pro inputs → "at most 3,186 MiB"); the startup audit finds memory over the plan.
  • /v1/models before load: estimateQwen35ContextWindow plans from file sizes without loading.

Splash gaps (labelled)

  • 27B lane: 355,217,408 B vs Splash 356,122,624 B (−905,216): no 4-bit draft window, a 2,311-row ring, no 16 KiB conv alignment.
  • Flash Next is FN1 (index plane, E1 64K allowance, E9 PLE share).
  • --max-memory reaches qwen3_5/MoE loads only.

Tests

Plan module: 18 tests, 25 named mutations. Wiring: 17 mutations. TS: new flag and overlay tests with mutations. Golden: WS 25,769,803,776 → hard 20,830,591,385 with mlx-node's margin (Splash's 24,696,061,952 recorded for S11; mutation "Splash margin" → red).

🤖 Generated with Claude Code

@Brooooooklyn
Brooooooklyn added this pull request to stack #344 November 9, 2026 17:55
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 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:51:16.865811Z 19da873 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 9, 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: dd895e86-b171-447c-9e15-70147e102b5f

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: 8549389c74

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

Comment thread packages/server/src/host/index.ts Resolved

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

ℹ️ 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-s28-verify-stall-cap branch from 0b664fb to eb36d46 Compare November 12, 2026 13:41
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s4-memory-plan branch from 7ee7dbf to 254506b 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-s28-verify-stall-cap branch from eb36d46 to 0f3c603 Compare November 12, 2026 15:48
Base automatically changed from feat/kv-int8-s28-verify-stall-cap to main November 12, 2026 15:48
Brooooooklyn and others added 4 commits November 12, 2026 23:48
…ash2 budget

Qwen3.5 dense and MoE loads now build one memory plan in the shape of
Splash EngineMemoryPlan (evaluateEngineMemoryPlan): a hard budget below
the Metal working set, the fixed bytes the load measured (weights, other
pools, restore staging, Splash's 256 MiB pipeline and 512 MiB runtime
reserves), and per lane the longest window whose state, transients and
KV in whole Splash extents fit the dynamic rest.

- Margin: 15% of the working set plus 1 GiB (hard = WS - margin), until
  the memory governor moves it to Splash max(1 GiB, 2%).
- --max-memory (mlx serve, mlx agent; napi maxMemoryBytes) only lowers
  the hard budget, as Splash hardBudgetBytes; Splash memoryBound names it
  in the errors.
- The pool starts at the runway on every load and grows by extents to
  the plan's ceiling; an explicit paged_cache_memory_mb above the plan
  fails the load. A load whose lane and runway do not fit fails with
  Splash's wording; a startup audit (Splash auditActualMemory) refuses a
  footprint over the hard budget.
- DFlash2 growth, the paged prefill headroom and pool growth probe the
  plan's ceiling; the 2 GiB reserve and the 9/10 rule are gone.
- /v1/models shows a load-free estimate from the same plan
  (estimateQwen35ContextWindow) until the model loads.

Deleted:
- MLX_PAGED_CACHE_INITIAL_MB and paged_cache_initial_memory_mb
- MLX_KV_MEMORY_UTILIZATION and MLX_KV_SAFETY_MARGIN_BYTES
- MLX_PAGED_PER_SEQ_CTX for qwen3_5 / qwen3_5_moe (other families keep it)
- the TS overlay's 16,384 MiB MLX_PAGED_CACHE_MEMORY_MB floor and 2,048 MiB
  initial size

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…; estimate the loaded config

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PagedAttentionConfig::validate refuses gpu_memory_mb under 256, but the
Qwen3.5 pool allocates by blocks. The plan can approve a pool under that
floor (231 MiB for the 27B at the lane minimum), and the load then failed
to construct the KV pool. Splash has no pool floor
(evaluateEngineMemoryPlan refuses only KvPoolDoesNotFit).

- mlx-paged-attn exports the floor as MIN_GPU_MEMORY_MB, used by validate.
- PoolPlan::config_mib() = max_mib().max(MIN_GPU_MEMORY_MB); dense and MoE
  initialize_paged_adapter pass it. Allocation stays bounded by num_blocks
  and max_num_blocks from the plan; validate is unchanged for other families.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The DFlash2 lane transient counts the verify cycle plus the free pool
MLX keeps of it (decode_cache_limit, twice the cycle), the base S28
term: MLX collects that pool only at min(0.95*WS, its memory limit) and
no load lowers that limit to the plan. The plan, turn eviction and the
Decode growth delta read the one function. M3 Pro DFlash2 window
61,433 -> 32,761; --max-memory 20G 27,241 -> 12,936; the pool lane is
unchanged.

A Qwen3.5 dense or MoE load with neither pool nor draft is planned on a
flat lane (one GDN cell, flat K/V in the load's format, a per-layer grow
copy, its last prefill chunk or one decode step), as Splash plans every
start. maxMemoryBytes now bounds and can refuse such a load, the load is
audited, and effectiveWindowTokens is the flat window with an omitted
budget meaning the rest of it. 27B sym8 estimate on a 27 GiB working set
with 16,000 MiB of weights: 262,144 -> 126,976.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s4-memory-plan branch from 254506b to 19da873 Compare November 12, 2026 15:48
@Brooooooklyn
Brooooooklyn merged commit 9f3a39c into main Nov 12, 2026
@Brooooooklyn
Brooooooklyn deleted the feat/kv-int8-s4-memory-plan branch November 12, 2026 15:48
Brooooooklyn added a commit that referenced this pull request Nov 12, 2026
… memory plan (#357)

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](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