Skip to content

feat(kv-int8): paged attention reads the int8 pool - #361

Merged
Brooooooklyn merged 2 commits into
mainfrom
feat/kv-int8-s6-int8-reads
Nov 12, 2026
Merged

Brooooooklyn merged 2 commits into
mainfrom
feat/kv-int8-s6-int8-reads

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Contributor

Stage S6 of the Splash int8 KV alignment (stack #344). Base: S5 (#360).

Paged attention reads the int8 pool, with Splash's op order (splash_attention_page_softmax). Only tests build an int8 pool; S7 makes the pool follow kv_format.

score = (dot(q, k_int8) · Ks[t]) · scale          key scale first
m, l  = softmax max / sum over the unscaled weights
P     = bf16(valid ? p · Vs[t] : 0)                select, so a NaN scale in an unwritten slot never leaks
out   = Σ P · v_int8  (f32 products, f32 v2 partials, combine in order, one bf16 round)

Changes

  • paged_attention.metal: int8 generic and varlen kernels (D128/D256, bs16/bs32), int8 reducers, and a grouped D256 bs16 int8 kernel. Scale planes use buffers 6 and 7. The Qwen3.5 grouped gate accepts Int8.
  • mlx_paged_ops.cpp: one validate_kv_scales for the write and both reads. The S5 read refusals are deleted.
  • Adapter: every paged read entry serves Int8. The raw decode route sends int8 through the graph op. gather_kv_dense_range and read_kv_range unpack x=16 and call the flat dequantize_kv_rows (now templated on D 128/256). A qwen3_5 cache-hit prefill on an int8 pool takes PagedVarlen.
  • Budgeted bind (Splash PageStorage::writeTable): bind_pool_plane binds K, V and the scale planes with set_input_array_with_budget(live blocks). The live-block count is deduplicated, so a shared prefix counts once. This changes the commit points of every paged family, bf16 included. Values do not change.

Measured (synthetic, Metal)

What Measured Bound
Paged int8 vs f64 reference, worst of 17 cases (ctx 300–17,000; 1/2/3/8 rows) max 0.91, mean 0.092 bf16 ulps max 3, mean 0.35 (the flat kernel's own)
Paged vs flat int8 kernel max 6.3e-3 of output magnitude int8-vs-bf16 noise 5.3e-3 to 1.0e-2
Dense gather and read_kv_range vs flat dequantize 0 differing bits bit-exact
Size commits, 4,096-block pool, 1 live block, one read per command buffer 0 0 (was 1 per plane)

The grouped route fires at context 9,000 (2 rows) and 17,000 (1 and 2 rows).

Gaps vs Splash

  • Fork patch 10 gives the full charge back when one pool is bound twice in one command buffer (4 commits per 8 reads). No decode or verify graph does this today.
  • Batched lanes and the 8-row verify always take the generic kernel (the grouped route needs num_seqs == 1). S8 measures.
  • Gathers dequantize to bf16 in memory (S10 removes that route). Bit parity with Splash is S17.

Tests

  • 12 named mutations, each red when on and green when off (K1–K3 kernels, M1 validator, M2–M4 gathers and adapter, B1/D1/D2 bind).
  • cargo fmt --check, cargo clippy --all-targets -D warnings, yarn build:native, yarn typecheck: clean.
  • mlx-paged-attn: all 15 binaries green (lib 209, paged_ops_smoke 86, paged_int8_read 2, paged_attention_budget 2). mlx-core (RUST_TEST_THREADS=1): paged_int8_tests 4, adapter 187, qwen4_exp 231, qwen3_5 690, engine:: 559, memory_plan 32, paged 471.

Not in this PR (live, not run)

  • L1: teacher-forced KL on qwen3.8-27b, paged int8 vs flat int8 vs flat bf16. It can use the real path from S7.
  • L2: bf16 decode A/B of the budgeted bind (stop if more than 3% slower).

🤖 Generated with Claude Code

@Brooooooklyn
Brooooooklyn added this pull request to stack #344 November 10, 2026 20:57
@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:52:57.599465Z 244ed11 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 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: 7530ae14-6897-4652-a62a-ddef5cdfc1cd

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

@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s6-int8-reads branch from 4addf95 to a45a2e7 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-s5-int8-store branch from 7613a32 to f2e9d03 Compare November 12, 2026 15:50
Base automatically changed from feat/kv-int8-s5-int8-store to main November 12, 2026 15:50
Brooooooklyn and others added 2 commits November 12, 2026 23:51
The paged attention kernels (v1, v2, varlen, and the grouped Qwen3.5 D256
route) read int8 pages with their per-row scales in Splash order: key
scale then Scale on the score, P = bf16(p * value scale) with masked
tokens exactly zero, f32 PV products and f32 partials combined in
partition order. The read validators take the scale planes and the
int8 refusals are gone. The dense gather and read_kv_range dequantize
int8 rows with the flat cache's op (now head size 128 or 256); the raw
decode route sends int8 through the graph op; an int8 pool always takes
the varlen cache-hit prefill. Pool planes are bound with a budget of the
distinct blocks the tables reach, so a big pool no longer commits after
every read. The default pool stays bf16.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A dense int8 gather that covers one page reshapes V without a copy, so the
rows reach the dequantize kernel with a head-dimension stride of the block
size. The kernel read src[j] and mixed neighbouring tokens; its validator
checked the strides of a lazy array, which are always row-major. The kernel
now reads at q_strides[3], so every caller is correct without an extra copy.

Tests: a strided-input op test (D 128 and 256, lazy and evaluated) and
one-page dense gather parity for block sizes 16 and 32 against the flat
int8 rows. Mutation U1 (stride 1 reads) turns both red.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the feat/kv-int8-s6-int8-reads branch from a45a2e7 to 244ed11 Compare November 12, 2026 15:51
@Brooooooklyn
Brooooooklyn merged commit e693110 into main Nov 12, 2026
1 check failed
@Brooooooklyn
Brooooooklyn deleted the feat/kv-int8-s6-int8-reads branch November 12, 2026 15:51
Brooooooklyn added a commit that referenced this pull request Nov 12, 2026
Stage S7 of the Splash int8 KV alignment (stack #344). Base: S6 (#361).

The qwen3_5 and qwen3_5_moe paged pools take their page format from
`kv_format`, as Splash `--kv-format` does. The default is int8 where the
kernels serve the model.

```
kv_format unset  ──► int8 if head 128/256, block 16/32, BF16 compute, else bf16
kv_format int8   ──► int8, or the load fails (Splash compiledAttention refuses a layout with no kernel)
kv_format bf16   ──► bf16
```

## Changes
- **Pool format:** the pool, the memory plan (layout, bytes per block,
transient) and the load-free estimate follow the format. The runway uses
Splash `kvRunwayPages`: 275 blocks for bf16 (unchanged), 546 for int8.
- **Int8 varlen prefill tiles its query rows** (rows × partitions ≤
2,048), as Splash `PagedAttention::prefillWorkspace` does. Each row's
math is unchanged. This removes the 87,040-token cap (130,560 on the
35B) and the host `read_kv_range` fallback past it. An int8 pool returns
errors instead of taking the legacy write or the host read.
- **GDN sidecar:** the sidecar is labelled with the activation dtype,
not the pool dtype. Before this, an int8 pool turned off the whole cold
tier with no log. Int8 blocks round-trip the cold tier with their scales
(S5 blob layout, no `CACHE_ABI` change).
- **One window:** every non-DFlash2 turn takes the pool window. A load
with MTP heads prices the MTP state into it once (`PoolInput.state`).
DFlash2 keeps its own window until S8.
- **`mlx eval` reads pages** on a paged load
(`paged_teacher_forced_hidden`, dense and MoE), so the live KL gate runs
the production path.
- **Paged FP8 is deleted** (Rust, C++, Metal, tests, docs). `kv_dtype`
code 2 is refused. `Int8` keeps code 3.
- `kv_format` is never saved to a config.
- The load-free context estimate reads the compute dtype from the
checkpoint header (safetensors header or GGUF import types), with the
same rule as the load. An f16 checkpoint no longer shows an int8-sized
window that its load (bf16 pages) cannot serve.

## Window (27B, M3 Pro inputs)
| case | window (tokens) |
|---|---|
| bf16 pool, no MTP (before) | 48,601 |
| int8 pool, no MTP | 102,393 |
| int8 pool, MTP, serial | 55,207 |
| int8 pool, MTP, `MLX_CONTINUOUS_BATCHING=1` | 20,473 |
| DFlash2 lane (unchanged) | 32,761 |

## Gaps
- The serial paged-MTP prompt-hidden capture is not priced or capped.
Only `held_bytes(W)` is.
- With continuous batching, the MTP state is charged both outside and
inside the pool (S11).
- The int8 prefill tile count grows with context (S10).
- The first prefill chunk (no prefix) attends over the in-flight bf16
K/V, not its own int8 pages (S10, Splash `QwenTarget::addPrefillMixer`).
So `mlx eval` on rows that fit one chunk scores int8 like bf16.
- No MoE twins for the pool-format, sidecar and eval tests. The sidecar
test covers capture only.

## Tests
- 15 named mutations, each red when on and green when off (D1–D3, S1,
W1, W2, B1, K1, V1, C1, G1, P1, O1, O2, E1).
- `cargo fmt --check`, `cargo clippy --all-targets -D warnings`, `yarn
build:native`, `yarn typecheck`: clean.
- mlx-paged-attn: 15 binaries + doctests, 323 passed. mlx-core `--lib`
(`RUST_TEST_THREADS=1`): 4,879 passed, 0 failed. `vp test` cli eval /
serve / agent-args: 122 passed.

## Not in this PR (live, not run)
- L1: teacher-forced KL on qwen3.8-27b, int8 pages vs bf16 pages,
through `mlx eval`.
- L2: native-MTP acceptance. L3: speed at 8K and 32K. L4: an image turn.
L5: a 35B turn. L6: a prompt past the old 87,040 cap.

🤖 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