Skip to content

feat: serve the loaded window and K/V format to clients (int8 KV S2) - #342

Merged
Brooooooklyn merged 13 commits into
mainfrom
feat/kv-int8-s2-surfaces
Nov 12, 2026
Merged

Brooooooklyn merged 13 commits into
mainfrom
feat/kv-int8-s2-surfaces

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Contributor

Stage S2 of the Splash int8 KV alignment. This PR is stacked on #336 (S1). Merge #336 first.

What changes

Clients now see the real window and the K/V format after the model loads. Before this PR, /v1/models showed the trained window (262,144). The agent never compacted, and turns stopped with "output limit".

before:  /v1/models ── trained 262,144 ──► agent never compacts ──► turn hits the real window
after:   load ──► window of the default text lane ──► /v1/models, /health, agent env

The served window is the window of the lane that the default text turn takes. Splash does the same (server/server.py:1830-1841, ReadyEvent.max_context_tokens).

Load (M3 Pro inputs, 27B) Lane Window shown kv.format
DFlash2 draft, int8 (default) DFlash2 139,257 int8
DFlash2 draft, --kv-format bf16 DFlash2 smaller (bf16 rows) bf16
no draft (paged) pool pool window bf16
no contextLimits() family none trained omitted

Parts

  • Server. /v1/models and /health show kv[model] = {maximum_context_tokens, format, quantization, scale_type}. The context_window field stays, because grok reads it.
  • CLI. --kv-format on serve and agent.
  • qwen3_5. Qwen3_5ContextLimits has kv_format and speculative_kv_format. The DFlash2 format comes from the same function that prices the window, so the two cannot disagree.
  • Eval. The report shows the resolved K/V format.
  • Agent launch. The agent starts after the model preload. mlx launch claude sets CLAUDE_CODE_MAX_CONTEXT_TOKENS and CLAUDE_CODE_AUTO_COMPACT_WINDOW from the served window.
  • Signals. The launcher installs its SIGINT, SIGTERM and SIGHUP handlers before it spawns serve. A signal during the preload wait now stops the serve child.

Tests

  • yarn typecheck, vp lint, vp fmt on the touched files: pass.
  • vp test --run on 19 files: 898 passed.
  • cargo clippy --all-targets -- -D warnings, cargo fmt --check: pass.
  • cargo test -p mlx-core --lib (qwen3_5, qwen3_5_moe, quality, engine): 1097 passed.
  • New subprocess test: SIGTERM during the preload wait stops serve. Mutation (old handler placement) turns it red.

🤖 Generated with Claude Code

@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: 8954f84a-e796-410d-aac4-3d580612208e

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:46:24.149216Z 9189763 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.

@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s2-surfaces branch from 25576c1 to 3ed1075 Compare November 8, 2026 09:18
@Brooooooklyn
Brooooooklyn added this pull request to stack #344 November 8, 2026 13:54
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s1-window branch from aabe08e to cfcf4d2 Compare November 12, 2026 13:41
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s2-surfaces branch from f50c82f to 493942d Compare November 12, 2026 13:41
@Brooooooklyn
Brooooooklyn removed this pull request from stack #344 November 12, 2026 15:43
Base automatically changed from feat/kv-int8-s1-window to main November 12, 2026 15:43
Brooooooklyn and others added 13 commits November 12, 2026 23:44
… /health

The registry reads contextLimits() once when a model gets its binding and
keeps a JS copy for the binding's life. servedContext(name) picks the lane
a default text turn takes (contextLane, the rule the ChatSession clamp
uses): the DFlash2 window when MTP is on by default and a speculative
window exists, else the pool window.

/v1/models carries that window as context_window, max_model_len and
context_length once the model is loaded, and the trained window before
load and after eviction. The authenticated /health body gains kv[model] =
{maximum_context_tokens, format, quantization, scale_type} (Splash /status
names); the unauthenticated minimal body is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`mlx serve --kv-format int8|bf16` sets the flat K/V cache format of
qwen3_5 / qwen3_5_moe loads; `mlx agent --kv-format` lifts the flag out of
the grok argv and passes it to the spawned serve. The host hands kvFormat
only to families whose family-data row sets acceptsKvFormat (the same flag
the loader now checks), so a gemma4 load on the same host is unaffected.
The block-paged pool ignores the option and stays bf16 (said in the help).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qwen3_5ContextLimits gains kv_format (the pool lane: bf16 on a paged load,
since the block-paged pool stores BF16 whatever kv_format says; the
configured flat format otherwise) and speculative_kv_format (the DFlash2
lane, from dflash2_target_kv_format, the same function its window is
priced with). Dense and MoE snapshots both fill them.

EvalBackend reports the K/V format its teacher-forced prefill writes, and
EvalReport carries it, so `mlx eval` can name the resolved format instead
of guessing from the flag.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`mlx eval score` prints `kv_format` from the native report, and `--json`
takes kvFormat from it too. Before, an unset flag on dense 27B labelled an
int8 run "bf16". The help names both families and the real default.

Docs: draftModelPath and kvFormat cover qwen3_5_moe; the block-paged pool
stays bf16 whatever kvFormat says; contextLimits() names each lane's
format; `mlx serve` / `mlx agent --kv-format`; /v1/models window fields
before and after load; the authenticated /health kv block; FP8 KV is
dormant (every loader passes use_fp8_cache: Some(false)).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Grok reads /v1/models once at boot and caches it, so `mlx agent` now polls
the spawned serve's authenticated /health until the preload is resident
(or its load failed) before it starts grok; a serve exit during the wait
fails the launch. No timeout: a cold load can take minutes, and Splash's
launcher also starts a client only on a ready server.

`mlx launch claude` loads the default model before Claude Code starts and
sets CLAUDE_CODE_MAX_CONTEXT_TOKENS and CLAUDE_CODE_AUTO_COMPACT_WINDOW to
the served window, as Splash install/clients.py does. A failed load is
printed and Claude Code starts without them.

This changes startup timing of both launchers, so it is its own commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
mlx agent waited on /health with no timeout until the preloaded model was
resident or its load failed. A preload that serve rejects before the load
bracket (a model its own scan no longer finds) records no lastLoad, so the
agent waited forever. Serve enters the bracket in the tick that prints its
banner, so nothing loading and nothing loaded after it means no load will run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The load-then-read wiring had no test: skipping the load or reading the
window before it settles left every test green. loadServedWindow holds it
and is tested through the real host and registry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The named mutation (restore `kvFormat ?? 'bf16'` in the --json line) stayed
green: `...report` comes after it and wins. Name the two that go red.

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

mlx agent installed its SIGTERM/SIGHUP teardown only after the spawned serve
was ready and its preload had settled. The preload wait has no timeout, so a
signal during a cold load killed the launcher by the default disposition and
left mlx serve running with the model resident. The handlers now go in before
serve spawns and stay through readiness, preload and the agent run; a SIGINT
before grok exists tears serve down and re-raises instead of forwarding.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Serve's graceful shutdown awaits in-flight model loads with no deadline,
so a signal during the preload wait left serve alive (holding the model)
after the launcher died. Teardown now signals serve, waits up to 10 s for
it to exit, SIGKILLs it if it has not, and only then re-raises. The
launcher's finally awaits the teardown so a startup failure during the
grace cannot let the caller exit first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A signal during the preload wait starts the teardown while no agent
exists yet. If the preload then settles inside the serve grace, the
launcher went on to spawn grok, which the teardown never signals, and
grok outlived the launcher. The launcher now checks the latched teardown
right before spawning the agent, awaits it, and never starts grok.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The launcher's SIGINT/SIGTERM/SIGHUP handlers were process.once, and the
finally removed them before awaiting the teardown. A second signal while
serve used its stop grace took the default disposition and left serve
running. The handlers now stay installed until the re-raise; a repeated
signal SIGKILLs serve (and the agent), the teardown still waits for serve
to exit, then re-raises the first signal.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
DELEGATE_VALUE_FLAGS lacked --kv-format, so `mlx delegate --kv-format bf16
'task'` folded bf16 into the prompt and the agent saw a bare --kv-format.
The approval scan now uses the same set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s2-surfaces branch from 493942d to 9189763 Compare November 12, 2026 15:44
@Brooooooklyn
Brooooooklyn merged commit fdc570d into main Nov 12, 2026
0 of 5 checks passed
@Brooooooklyn
Brooooooklyn deleted the feat/kv-int8-s2-surfaces branch November 12, 2026 15:45
Brooooooklyn added a commit that referenced this pull request Nov 12, 2026
…e geometry (S3) (#343)

## What

Stage 3 of the Splash int8 KV alignment. It is stacked on #342 (S2),
which is stacked on #336 (S1).

The flat full-attention K/V of `qwen3_5_moe` now takes the same geometry
default as dense `qwen3_5`. The old MoE-only BF16 override in
`Qwen3_5MoeConfig::to_dense_config` is removed.

```
KvFormat::default_for_geometry(head_dim, q_heads, kv_heads)
  Metal && head 256 && gqa in 1..=8  -> Int8
  else                               -> Bf16

Qwen3.8-27B  dense  24/4  gqa 6   int8 (unchanged)
Qwen3.6-35B  MoE    16/2  gqa 8   int8 (was BF16)   <- this PR
Qwen3.6-122B MoE    32/2  gqa 16  BF16 (gap)
```

Splash (`031a25a6`) uses int8 for every model
(`RuntimeResources.hpp:103`, `RuntimeResources.mm:727-728`). It has one
kernel per geometry, including `_kv2_g16` for 32/2/256
(`PagedAttention.cpp:59-65`).

## Gap: 122B stays BF16

mlx-node has no gqa-16 int8 verify kernel for wide blocks:

```
two-pass stage 1 = 32 x gqa x rows threads <= 1024
gqa 8  -> 4-row chunks -> 8-row verify = 4 + 4      ok
gqa 16 -> 2-row chunks -> 5-8 row verify blocks     error (int8 has no fallback)
```

So the default adds the bound `gqa in 1..=8`. `kvFormat: 'bf16'` (load
option, `--kv-format bf16`, or `kv_format` in `config.json`) opts out on
any model. Lift: a gqa-16 int8 verify kernel, like Splash `_kv2_g16`.

## DFlash2 window (35B-A3B, M3 Pro inputs)

| Case | Row bytes | Window |
|---|---|---|
| 27B int8 (S1) | 33,280 | 139,257 |
| 35B BF16 | 20,480 | 244,217 |
| 35B int8 | 10,400 | **262,144** (trained cap; the pool term is
475,129) |

## Quality (teacher-forced, int8 vs BF16 K/V, 8 x 8192 tokens, M5 Max)

| Model | Run | kvFormat | mean KL | top-1 | KL after 2048 | top-1 after
2048 |
|---|---|---|---|---|---|---|
| 35B-A3B | bf16 control | bf16 | 0 | 1 | - | - |
| 35B-A3B | default (no flag) | int8 | 0.004670 | 0.977460 | 0.006226 |
0.969945 |

- The stop band is top-1 < 0.97 or KL > 0.05. The whole-run numbers
pass. The tail top-1 is 3 positions out of 49,144 under 0.97 (0.07 SE),
so it is noise. NLL moves +0.0013 nats.
- 27B was not measured in this PR. Its score run failed during the
weight load with a GPU timeout. The 27B default does not change here.
- DFlash2 acceptance on 35B was not run, because no local 35B draft
exists.

## BF16 compute only (review fixes)

```
default_for_geometry(head, q_heads, kv_heads, compute)
  Int8 only if Metal && compute == BF16 && head 256 && gqa in 1..=8
load: probe the compute dtype -> pin kv_format -> build caches   (dense and MoE)
an explicit kvFormat still wins
```

- `1bc057823`: an FP16 MoE load (`mlx convert --dtype float16`) got int8
K/V. Its compiled DFlash2 verify then failed the turn, because
`segmented_sdpa_int8` takes only BF16 queries. Now an f16 or f32 load
keeps BF16 K/V. Dense had the same hole since #183 (it fell back to a
slow eager path), and the same rule closes it.
- `1e6662ae1`: the int8 compiled verify casts non-BF16 queries to BF16,
as Splash does (its int8 kernels take `bfloat` queries). No real
checkpoint makes that mix, but no turn can abort now.
- Tests: `kv_format_default_needs_an_int8_verify_kernel`,
`moe_load_pins_the_kv_format_for_the_compute_dtype`,
`dense_load_pins_the_kv_format_for_the_compute_dtype`,
`int8_compiled_verify_takes_f32_queries`. Each one went red under its
named mutation.

## Tests

- `kv_format_default_needs_an_int8_verify_kernel` and
`int8_verify_chunks_bound_the_default_gqa` pin the gqa bound.
- `eager_forward_over_an_int8_cache_tracks_the_bf16_cache` now covers
24x4 and 16x2, and it counts int8 kernel reads.
-
`moe_flat_cache_precision_follows_the_geometry_default_and_explicit_format`
and `moe_dflash2_window_prices_the_default_format` cover the MoE flip
and the window.
- The two MoE paged-vs-flat parity tests pin the flat side to BF16.
- Each new test went red under its named mutation and green after the
revert.
- Gates: `cargo clippy --all-targets -D warnings`, `cargo test -p
mlx-core --lib` on the touched modules (686 passed), `yarn typecheck`,
and the CLI/loader vitest files (232 passed).

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

🤖 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