Skip to content

feat(kv-int8): a thinking budget ends thinking with the template close - #365

Merged
Brooooooklyn merged 1 commit into
mainfrom
feat/kv-int8-s31-thinking-close
Nov 12, 2026
Merged

Brooooooklyn merged 1 commit into
mainfrom
feat/kv-int8-s31-thinking-close

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Contributor

Stage S31 of the Splash int8 KV alignment (stack #344). Base: S8 (#364).

A thinking-budget cut now ends thinking with the chat template's own close, not with </think> alone. The re-rendered history then has the same tokens as the reply, so the next turn's paged prefix hit goes through the forced close.

before:  ...thinking (budget reached) ──► </think>             re-render: "\n</think>" ──► prefix hit stops at the cut
after:   ...thinking (budget reached) ──► "\n" </think>        re-render: "\n</think>" ──► prefix hit covers the close

Changes

  • Probe at load (Splash thinking_end_tokens / _thinking_end): every tokenizer renders a 4-message conversation, slices from the end of the reasoning through the first </think>, and encodes it. The close must end with </think>, have no other added token, and be at most 64 tokens, or the load fails. A template with no close falls back to </think> alone, as Splash does. from_tokenizer now returns Result.
  • One tracker rule for every decode loop (Splash NativeBackend._thinking_budget, commitSelected): the end advances when a token is observed. A forced token is handed out only when no forced token is in flight. While thinking ends, the unforced budget is 0, with no drafts and no pipelining. A token past the budget is a debug_assert, because Splash throws there.
  • Paths: AR, decode_loop!, MTP, DSpark/DFlash2, the qwen3 scheduler, hybrid scheduler AR rows and scheduled speculation, the qwen3_5 and MoE whole-turn cores, and every family with a thinking budget. Speculative paths commit each forced token through its own one-row cycle, so their tokens and KV are the same as AR.
  • Deleted: forced_token_id, should_force_think_end, force_think_end_pending, end_scheduled, from_setup_multi.

Probe survey (local templates, tokenizer only)

close families
"\n</think>" Qwen3, 3.5, 3.6, 3.8, clef, ornith, agents, ternary, and their GGUFs
"</think>" nemotron, lfm2.5, qianfan, qwen3-asr, deepseek
none (nothing forced) gemma4, muse, k2, privacy-filter

No local template fails the load. Qwen3 also moves to "\n</think>". This is deliberate.

Gaps

  • Splash forces the close inside one verify. Here MTP and DSpark spend one 1-row cycle for each close token, which is 2 on Qwen.
  • No also_ends rule: a <tool_call> opened inside a cut still gets the close after it. This was true before S31 too. There is no --thinking-end-text flag.
  • A cut right after a space or a line break still misses the cache at the cut, because the template trims the reasoning. Splash has the same gap.
  • A cut turn reports one more reasoning token (budget + 1), which is Splash's count.
  • The MoE whole-turn and vision cores have no test of their own. The tracker test covers their risk.
  • docs/vllm_reasoning_control_flow_spec_for_mlx_node.md still describes the old single-token design (historical spec).

Tests

  • 14 named mutations, each red when on and green when off (M1–M13, M5 = force </think> alone and the re-rendered history splits at the cut).
  • New tests include tracker_ends_thinking_with_the_whole_close, probe_finds_the_template_close (Qwen3.8 + a new Qwen3.6 template fixture), probe_falls_back_and_refuses_a_bad_close, budget_cut_rerender_hits_through_the_close, scheduled_rows_end_thinking_with_the_whole_close, legacy_dense_cores_end_thinking_with_the_whole_close and decode_loop_ends_thinking_with_the_whole_close.
  • cargo fmt --check, cargo clippy --all-targets -D warnings, yarn build:native, yarn typecheck, vp lint / vp fmt on the touched files: clean. mlx-core --lib (RUST_TEST_THREADS=1): 4,762 passed, 0 failed.
  • After the rebase onto S8 ef025f7: fmt, clippy, yarn build:native, yarn typecheck clean; mlx-core --lib 4,764 passed, 0 failed

Not in this PR (live, not run)

  • L1 AR, L2 DFlash2, L3 MTP on qwen3.8-27b with a thinking budget of 64. Each runs a second turn on a re-rendered history and records the prefix-hit tokens, which must cover the forced close.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Nov 11, 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: c020aeb4-8341-4939-8c71-a8709a488bf0

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 11, 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:54:38.363851Z 42c28ea 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 11, 2026 13:55
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s8-dflash2-pool branch from ef025f7 to b93e8d6 Compare November 12, 2026 13:41
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s31-thinking-close branch from 736aa42 to 48f3c52 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-s8-dflash2-pool branch from b93e8d6 to 1fb67c0 Compare November 12, 2026 15:52
Base automatically changed from feat/kv-int8-s8-dflash2-pool to main November 12, 2026 15:52
At load the tokenizer probes what the chat template writes after a
reply's reasoning through its close (Qwen: "\n</think>"), as Splash's
chat_templates._thinking_end and backend.thinking_end_tokens do. A close
that holds another special token or more than 64 tokens fails the load;
a template with no close ends thinking with the close token alone.

Every decode path that cuts thinking (AR, decode_loop!, the qwen3_5 and
MoE whole-turn cores, MTP, DSpark/DFlash2 verify, the hybrid scheduler
and its speculation, the qwen3 scheduler) now forces that whole sequence
one token per step, so the next request's re-render of the reply hits
the paged prefix through the close. The single forced_token_id path is
gone; reasoning_tokens counts budget + len(end) - 1 as Splash does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s31-thinking-close branch from 48f3c52 to 42c28ea Compare November 12, 2026 15:53
@Brooooooklyn
Brooooooklyn merged commit 72c6ca6 into main Nov 12, 2026
1 check failed
@Brooooooklyn
Brooooooklyn deleted the feat/kv-int8-s31-thinking-close branch November 12, 2026 15:53
Brooooooklyn added a commit that referenced this pull request Nov 12, 2026
Stage S32 of the Splash int8 KV alignment (stack #344). Base: S31
(#365).

Models with gqa 16 (Qwen3.6-122B: 32 query / 2 KV heads, D 256) now
default to int8 KV pages. Before this PR, `default_for_geometry` allowed
int8 only up to gqa 8.

```
before:  gqa <= 8 and whole groups ──► int8      gqa 16 ──► bf16
after:   whole groups               ──► int8      (122B window at 96 GiB: 118,393 ──► 262,137 tokens)
```

## Why no tile parts
Splash `SPLASH_ATTENTION_TILE_PARTS` splits the rows of one tile that
packs 8 rows × every query head of a group, so that each part holds at
most 64 packed (row, head) entries. The mlx-node pool kernels run one
threadgroup for each query head and row. By Splash's own formula, each
of them is already one part. The paged int8 reads map query head h to KV
head h / gqa at any gqa, so the gqa ≤ 8 limit had no kernel reason.

## Changes
- `default_for_geometry` (`transformer/kv_cache.rs`) needs only whole
GQA groups.
- The flat int8 cache (loads with no pool, MoE `generate_sync`,
calibration prefills, training generation): `forward_int8_cached` takes
the segmented kernels only for a block they can split (`rows <= 2 ×
max_q(gqa)`, new `segmented_int8_covers`). Any other block takes a
dequantize route (below). Before, a 5..8-row block at gqa 16 threw
"unsupported segmented SDPA verify block". The cache stays int8.
- A 5..8-row verify that the segmented kernels cannot split (flat MTP at
gqa 16, depth 4 or 5) dequantizes the cache's own rows `[0, P+T)`, so
its new rows are read int8, as a 1-row decode reads them (Splash
`PagedAttention::addVerify`: store, then attend). Prefill chunks keep
the bf16 own-rows read; S10 owns that.
- Doc text, generated TS types, CLI help and docs no longer say "GQA up
to 8".
- No Metal, C++ or metallib source changed. gqa ≤ 8 results stay
bit-identical.

## Tests
| test | clean | mutation (red) |
|---|---|---|
| `paged_int8_attention_gqa16` (32/2 at 300 / 600 / 17,000 / 33,000
tokens; 1, 2, 8, 64 rows) vs f64 reference | max ≤ 1.03, mean ≤ 0.13
bf16 ulps | G16 (heads 8+ read the next KV head): all 11 rows, 151–374
ulps; gqa ≤ 8 tests stay green |
| `int8_adapter_reads_serve_gqa_16` (D256, 32/2) | max diff 0.125 | G16:
diff 16.0 |
| `eager_forward_over_an_int8_cache_tracks_the_bf16_cache` (adds 32x2) |
worst 8.6e-3 | covers-check removed: the C++ throw |
| `flat_int8_verify_at_gqa_16_reads_like_sequential_int8_decode` (32x2,
4..8 rows vs 1-row int8 decode, incl. V = [127, 0.5]) | ≤ 2 bf16 ulps |
F1 (own rows read as bf16): verify 0.25 vs decode 0 |
| 122B default, unset-format 32/2 rows, 122B estimate | green | D1 (keep
`gqa <= 8`): red |

- `cargo fmt --check`, `cargo clippy --all-targets -D warnings`, `cargo
test -p mlx-paged-attn`, `yarn build:native`, `yarn typecheck`: clean.
mlx-core `--lib` (`RUST_TEST_THREADS=1`): 4,767 passed, 0 failed.

## Gaps
- At gqa 16 the pool reads run 16 threadgroups per KV head per decode
row (128 per 8-row verify). Splash runs `_kv2_g16` as 2 parts. Its speed
gain for parts is not captured here; it belongs to the S10 prefill split
kernel and the compiled paged verify.
- The grouped int8 route is enabled for 24/4 and 16/2, not 32/2.
- Splash uses 32-token pages; mlx-node uses 16.
- No gqa-16 checkpoint is on disk. The 122B window numbers come from a
config-only fixture with Splash's Qwen3.5-122B-A10B geometry.
- Out of scope, reported only: the MoE loader pins sym8 after the KV
format. It matters only for a page size other than 16 or 32.

## Not in this PR (live, not run)
- L0 download a 4-bit Qwen3.6-122B; L1 teacher-forced KL / top-1, int8
vs bf16 pages; L2 decode and verify speed, int8 vs bf16.

🤖 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