Skip to content

feat(qwen3_5_moe): default to int8 KV where the int8 kernels serve the geometry (S3) - #343

Merged
Brooooooklyn merged 5 commits into
mainfrom
feat/kv-int8-s3-moe-int8
Nov 12, 2026
Merged

Brooooooklyn merged 5 commits into
mainfrom
feat/kv-int8-s3-moe-int8

Conversation

@Brooooooklyn

@Brooooooklyn Brooooooklyn commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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 perf(qwen3.5, kquant): Splash-parity work — tiled K-quant layout, all ggml types, int8 KV, fused verify kernels #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

@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: aaa2afba-2ca2-4533-8d64-aa188f3d234d

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:50.673198Z 6be43ea 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 added this pull request to stack #344 November 8, 2026 13:54
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s3-moe-int8 branch from 1e6662a to addf0f0 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-s2-surfaces branch from 493942d to 9189763 Compare November 12, 2026 15:44
Base automatically changed from feat/kv-int8-s2-surfaces to main November 12, 2026 15:45
Brooooooklyn and others added 5 commits November 12, 2026 23:45
…e geometry

Splash defaults every target's KV to int8 (RuntimeResourcesConfig
kvFormat = kv::Format::Int8, runtime/engine/RuntimeResources.hpp:103)
and applies that format to the target layout without a per-family
override (RuntimeResources.mm:727-728). Drop the MoE-only bf16 override
in Qwen3_5MoeConfig::to_dense_config, so MoE takes the same geometry
default as dense qwen3_5.

KvFormat::default_for_geometry now also needs a GQA factor of 1..=8:
the int8 two-pass verify runs 32 x gqa x rows threads (<= 1024), so an
8-row block (DFlash2, MTP depth >= 4) needs 4-row chunks. Splash serves
the 122B (32Q/2KV/D256, gqa 16) with its own compiled kernel
(PagedAttention.cpp:61-67: {24,4,256}, {16,2,256,_kv2_g8},
{32,2,256,_kv2_g16}, ...). mlx-node has no gqa-16 int8 verify kernel,
so at gqa 16 every flat int8 verify block of 5 to 8 rows would error.
The 122B therefore stays bf16 by default; closing that gap needs a
gqa-16 int8 verify kernel like Splash's _kv2_g16.

The DFlash2 window prices the format the cache will use, the MoE paged
parity tests pin their flat side to bf16, and the napi docs and CLI help
state the new default.

Tests:
- kv_format_default_needs_an_int8_verify_kernel (27B/35B int8, 122B,
  non-divisible, D128 bf16)
- int8_verify_chunks_bound_the_default_gqa
- moe_flat_cache_precision_follows_the_geometry_default_and_explicit_format
- moe_dflash2_window_prices_the_default_format
- eager_forward_over_an_int8_cache_tracks_the_bf16_cache over every
  served gqa

Co-Authored-By: Claude Opus 5.5 <[email protected]>
docs/models.md, docs/cli.md and docs/research/splash-qwen35-moe.md
state that dense and MoE Qwen default to int8 KV on Metal at head 256
and GQA up to 8, following Splash (RuntimeResources.hpp:103,
RuntimeResources.mm:727-728), and record the Qwen3.6-122B gap: Splash
serves 32Q/2KV/D256 with its _kv2_g16 kernel (PagedAttention.cpp:61-67);
mlx-node has no gqa-16 int8 verify kernel, so the 122B stays bf16.

The 27B / 35B tail KL and top-1 slots and the gate table's commit and
device cells are placeholders until the teacher-forced quality run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Teacher-forced vs a bf16-KV teacher (view dir with kv_format bf16),
8 x 8,192 tokens, top-k 1024, M5 Max 128 GB, 1 trial per case.

35B-A3B bf16 control: kvFormat bf16, KL 0, top-1 1 (exact).
35B-A3B default (no flag): kvFormat int8, KL 0.00467 / top-1 0.97746 raw;
tail past the first 2,048-token chunk KL 0.00623 / top-1 0.96994
(1,477 of 49,144; 0.97 allows 1,474, 0.07 SE under). Raw top-1 and both
KL figures pass; the default stays int8.

27B: teacher captured, the default score run failed with a GPU timeout
while materializing weights; the bf16 control was not run (budget).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An f16-compute load (mlx convert --dtype float16) got int8 flat K/V at head
256 / GQA <= 8: the int8 segmented SDPA verify takes BF16 queries only, so
the MoE compiled DFlash2 verify failed the turn and dense fell back to the
eager int8 read (whole-prefix dequantize, f32 SDPA). Splash is BF16-only
(bfloat queries in its int8 kernels; f16 weights narrowed at image write).

KvFormat::default_for_geometry now takes the compute dtype, and both loaders
pin the format after the weights give it (the embedding's output dtype,
after the MoE affine-sidecar alignment). An explicit kvFormat still wins.
This also closes the dense hole open since #183.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The int8 segmented SDPA takes BF16 queries only. A MoE load with f32 layer
norms (the MoE loader installs norms as stored) or an explicit int8 format on
an f16 load gave f32/f16 queries, the compiled DFlash2 verify threw at the
validator, and MoE turns that error into a failed turn. The Int8 arm of
forward_verify_impl now casts the queries to BF16, as Splash's int8 kernels
take bfloat queries; a no-op on every BF16 load.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s3-moe-int8 branch from addf0f0 to 6be43ea Compare November 12, 2026 15:45
@Brooooooklyn
Brooooooklyn merged commit 14d43ea into main Nov 12, 2026
0 of 5 checks passed
@Brooooooklyn
Brooooooklyn deleted the feat/kv-int8-s3-moe-int8 branch November 12, 2026 15:45
Brooooooklyn added a commit that referenced this pull request Nov 12, 2026
… (S18a) (#345)

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