Skip to content

fix(inkling2): make speculative verify blocks byte-identical to decode - #3405

Merged
inureyes merged 4 commits into
mainfrom
fix/issue-3280-inkling2-verify-exactness
Nov 24, 2026
Merged

inureyes merged 4 commits into
mainfrom
fix/issue-3280-inkling2-verify-exactness

Conversation

@inureyes

@inureyes inureyes commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Makes an Inkling 2 speculative verify block byte-identical to single-token decode, so the block-versus-chain probe passes and the bundled DSpark drafter runs without MLXCEL_MTP_ALLOW_INEXACT=1. Refs #3280 (the issue also covers the first Inkling generation, so it stays open).

Causes

A per-visit hidden-state comparison of an 8-row verify block against 8 decode steps found five, none of them in the quantized expert path:

  • MLX multiplies one row with gemv and several with gemm, which sum in different orders. This hits every dense product: attention projections (bf16), the f32 router, the message controllers and the f32 LM head.
  • The per-query zero sink column sat after the newest key, at index N+1 in a step but N+K in a block, which changed the SDPA reduction even for row 0.
  • The looped core's message read query @ bank^T is a batched matmul over the block length.
  • Past a sliding layer's 1024-token window, block row r also saw the r oldest keys (masked) that decode step r had dropped. The probe's 32-token prompt never reaches the window, so it passed while long prompts diverged.
  • The DSpark prefill ran the prompt in one forward; classic decode prefills in 2048-token chunks.

Change

  • mlxcel_core::row_invariant: an opt-in Scope/Mode plus dense, linear, per_row and per_position, backed by a new Metal kernel row_invariant_matvec (1 to 16 rows, summation order fixed by K and N). Outside a scope the helpers are plain matmul / forward, so other models are untouched.
  • Inkling 2 enters the scope for every text forward, routes all dense products through it, puts the sink column before the keys, reads messages per position and attends sliding visits row by row from each row's window start inside a verify block, and prefills the drafter prompt in the classic chunks (text and VL targets). With MLXCEL_INKLING2_KERNELS=0 or off Metal a verify block multiplies one row at a time (exact, slower). Dense experts are split per token there too.
  • build.rs now watches the Inkling 2 kernel sources; edits to them were never rebuilt.

Results (M3 Ultra)

  • Probe passes at widths 2, 4, 8 on Mini, Small and Mini 4-bit; the block equals the chain after 1100, 3000 and 8000-token prompts. Drafter-on output equals classic decode byte for byte, also on a 2006-token prompt.
  • Decode with the kernel: Mini +1.4%, Mini 4-bit +0.9% against MLX gemv.
  • DSpark, 256 greedy tokens: Mini 1.04x (prose) and 1.60x (code); Small 0.90x and 1.54x, since a Small verify round costs about 2.3 decode steps.
  • mlx-vlm parity after moving the sink column (Mini 4-bit text trace): 31 / 1024 top-1 differences, 0 / 372 at decided positions (33 and 0 before).
  • Local gate: contracts, clippy, 12817 lib tests serial, 2467 core tests pass.

Details: docs/benchmark_results/inkling2-verify-exactness-m3ultra-2026-11-24.md; report: TECHNICAL_REPORTS/3405-inkling2-verify-exactness-20261125.{en,ko}.md.

The block-versus-chain probe declined the bundled DSpark drafter of Inkling 2 Mini and Small at every width, so the drafter stayed off unless MLXCEL_MTP_ALLOW_INEXACT=1. A per-visit hidden-state comparison found three causes: MLX multiplies one row with gemv and several with gemm, which sum in different orders; the per-query sink column sat after the newest key, so its index differed between a step and a block and moved the SDPA reduction; and the looped core's message read is a batched matmul over the block length.

Add mlxcel_core::row_invariant, an opt-in scope plus dense/linear/per_row/per_position helpers backed by a new Metal matvec (row_invariant_matvec) whose summation order depends on K and N only, for up to 16 rows. Inkling 2 enters the scope for every text forward and routes all dense products through it, places the sink column before the keys, and reads messages per position inside a verify block. With MLXCEL_INKLING2_KERNELS=0 or off Metal a verify block multiplies one row at a time, which stays exact.

The probe now passes at widths 2, 4 and 8 on Mini and Small, drafter-on output equals classic decode, and decode is 1.4% (Mini) and 0.9% (Mini 4-bit) faster with the kernel. build.rs now watches the Inkling 2 kernel sources, which were never rebuilt after an edit.

Refs #3280
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions status:in-progress Currently being worked on type:performance Performance improvements area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:macos macOS (Apple Silicon) specific arch:moe Sparse mixture-of-experts decoder and removed type:bug Bug fixes, error corrections, or issue resolutions labels Nov 24, 2026
Once the context exceeds a sliding layer's 1024-token window, row r of a verify block also saw the r oldest keys, masked out, which a decode step had already dropped. The extra columns shifted the SDPA reduction, so rows 1 and later differed from decode on Mini at 1100, 3000 and 8000 tokens while the probe, which uses a short prompt, still passed. Inside a verify block a sliding visit now attends row by row over the keys from that row's window start. Dense (unquantized) experts are split per token too, since gather_mm also changes kernel with the row count.

The tiny model's head_dim goes from 8 to 64 so its attention takes the fused SDPA kernel real checkpoints use; with it the tiny probe test passes. The per-visit diagnostic gains INKLING2_DIAG_PROMPT and a tiny-model variant.

Refs #3280
The DSpark path prefilled the whole prompt in one forward while classic decode prefills in MLXCEL_PREFILL_CHUNK (2048) token chunks, so a longer prompt left caches that differ in the last bits and the drafter-on text left classic decode within the first sentences on a 2006-token prompt. Both the text and the VL speculative targets now prefill through Inkling2Model::prefill_speculative, which runs the classic chunks, evaluates each, keeps the last chunk's logits and concatenates the captures. That prompt now gives byte-identical text with and without the drafter.

The benchmark record gains the window and prefill causes, the long-prompt run, and the mlx-vlm parity re-measurement after moving the sink column (Mini 4-bit text trace: 31 / 1024 top-1 differences, 0 / 372 at decided positions, against 33 and 0 before).

Refs #3280
Adds the bilingual technical report for the Inkling 2 verify exactness work and its CHANGELOG entry, and fixes three clippy findings (a redundant rebinding and two needless borrows) left by moving the chunked prefill into Inkling2Model::prefill_speculative.

Refs #3280
@inureyes
inureyes marked this pull request as ready for review November 24, 2026 16:50
@inureyes
inureyes merged commit 5111fd0 into main Nov 24, 2026
33 checks passed
@inureyes
inureyes deleted the fix/issue-3280-inkling2-verify-exactness branch November 24, 2026 16:50
@inureyes inureyes added status:done Completed and removed status:in-progress Currently being worked on labels Nov 24, 2026
inureyes added a commit that referenced this pull request Nov 25, 2026
## Summary

- Inkling-Small MTP verify is now byte-identical to classic decode at every width the gate admits. The probe on `inkling-small-mlx-4bit` passed at widths 2 to 8 but declined at 10 (the MTP default) from layer 0: past 8 queries MLX's fused SDPA leaves the per-query kernel a decode step runs. A verify block now attends in groups of at most 8 queries. The probe passes at widths 2, 3, 4, 8, 10 and 16, after short, 600- and 2100-token prompts.
- A partial accept keeps the verify block's own state (KV tail trim, convolution states from the block's recorded input windows) instead of restoring a snapshot and replaying the kept prefix. The replay ran outside the verify scope, so it did not rebuild classic decode's state (the old width-3 prose stream left classic), and it was a second target forward per round. The snapshot/replay path remains for blocks outside the row-invariant scope.
- The Inkling 2 half of the issue was finished by #3405 and #3417; Inkling 2 code is untouched.

## Measured (M3 Ultra, 128 greedy tokens, ABAB)

| Mode | Prose before | Prose after | Code before | Code after |
|---|---|---|---|---|
| classic | | 45.1 | | 44.4 |
| width 2 | 25.9 | 39.7 | 27.0 | 41.6 |
| width 3 | 23.6 (differs) | 34.6 | 24.4 | 34.9 |
| width 10 (adaptive) | 10.4 (override, differs) | 12.7 | 10.6 (override, differs) | 12.7 |

Mean accepted length is 1.43 to 1.63, nearly all from the first draft. The drafter matches mlx-vlm 466069f5 in chain indexing, hidden-state source and logit path (upstream: 1.59). An experiment starting post-seed drafts at block 1 left acceptance unchanged. No width beats classic, so MTP stays opt-in (`--draft-model`). The #3417 adaptive width belongs to the DFlash loop and does not apply to the MTP loop. Details: `docs/benchmark_results/inkling-small-mtp-m3ultra-2026-11-25.md`.

## Test plan

- [x] New `mtp_partial_accept_keeps_the_single_token_chain_state_on_metal` and widths 10, 16 in `the_verify_block_matches_the_chain_on_metal`: both fail on the previous code, pass now
- [x] `survey_block_vs_chain` on `inkling-small-mlx-4bit`, widths 2, 3, 4, 8, 10, 16; ignored real-checkpoint probe test (now with width 10) on the bf16 0.6B and the 4-bit Small checkpoints
- [x] Narrow selectors: `models::inkling` (covers `inkling2`), `coverage_tests`, `inkling_mtp`, `speculative_exactness`, `speculative_burst`, mlxcel-core `inkling` and `row_invariant`
- [x] fmt, both clippy gates, and the contract scripts; the broad gate is left to CI

Closes #3280
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch:moe Sparse mixture-of-experts decoder area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata platform:macos macOS (Apple Silicon) specific status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant