Skip to content

fix(gemma4): make batched MTP verify decode-exact on row-wise geometries - #2203

Merged
inureyes merged 7 commits into
mainfrom
fix/issue-2190-gemma4-batched-mtp-verify
Oct 7, 2026
Merged

inureyes merged 7 commits into
mainfrom
fix/issue-2190-gemma4-batched-mtp-verify

Conversation

@inureyes

@inureyes inureyes commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Summary

The batched Gemma 4 MTP adapter never set mtp_verify, so on the row-wise geometries (31B everywhere, 12B on CUDA) it missed the #2185 decode-exact path: b1_batched_baseline_probe failed on main (31B rows 1, 2, 3 at tokens 1, 9, 17). Batched MTP is now byte-identical to per-sequence classic decode at B=1, 2 and 4, and on CUDA the 31B serves those windows because they measured 53 to 77% faster than classic batched decode.

What changed

  • The batched adapter marks verify forwards only. On the row-wise geometries each row is prefilled on its own cache over the classic partition (chunks plus history boundary, last-row LM head) and the caches are stacked with history first and a zero tail, so a short or ragged row is a row lagging the shared offset; no left padding, no shifted RoPE frame.
  • A B>1 verify runs each row as its own [1, K] call (Q/K/V, row-wise RoPE at the row's offset, attend_verify_rows on keys physically cut to the row's history plus block, per-row ring cursor, o_proj, MLP, LM head). Only the cache write stays batched, so finalize, rollback and drafter slabs are unchanged. A lagging row that lost sliding keys to shared-offset compaction makes the verify refuse the round.
  • run_mtp_burst_batched replaces the unconditional row-wise decline with RowWiseBatchedWindow: CUDA only (Metal and ROCm builds keep declining), B <= 4, dense FP16 caches, prompts within the sliding window, and max_prompt_len + K * (max_tokens + 1) <= window + 32, so compaction cannot happen mid-burst. Windows carry per-row history boundaries. Still behind MLXCEL_ENABLE_MTP_BATCH.
  • In-process parity tests apply the server's MLX graph/SDPA cache sizes; the new test crossed MLX's lifetime-miss abort at the bare default.

Test plan (GB10, CUDA, release)

  • speculative_parity --ignored --test-threads=1 (full, no env): 9 passed, 0 failed: b1_batched_baseline_probe (now 31B and 12B, all rows identical), divergent_hidden_probe_31b, gemma4_batched::greedy_parity_mtp_gemma4_batched_matches_classic (new: B=2/B=4, equal and ragged, both pairs, 24/24 rows identical to classic, divergent rounds in every case), greedy_parity_dflash_qwen35_4b, greedy_parity_mtp_gemma4_31b, greedy_parity_mtp_gemma4_batched_b4_matches_b1, greedy_parity_mtp_gemma4_batched_b4_ragged_matches_b1, greedy_parity_mtp_gemma4_unified_12b, mtp_gemma4_ragged_throughput_probe. Main: 7 of 8, probe failing.
  • Lib gemma4 speculative_burst: 327 passed. New hermetic tests for verify marking (AC-2), per-row verify vs B=1 linear verify (fails under a shared-cursor mutation and with row mode disabled), cache stacking, the window gate.
  • Clippy -D warnings (lib, tests), cargo fmt --check.
  • After rebasing onto d54b209b (refactor: one post-sample finish step for every CLI and server decode site #2201, refactor: one prefill plan and one trim hook for the CLI and the server #2205) the lib selectors (327 passed), the full suite (9 passed, 0 failed) and one served B=2 window (batched, bytes identical to classic) were rerun on the pushed head.
  • Throughput, 3 interleaved rounds, null arm, identical bytes: 31B B=2 +75 to 77%, B=4 +53 to 55%, null -2.3 to +3.1%. Shipped binary round: batched windows engaged, bytes identical. See docs/benchmark_results/gemma4-mtp-batched-gb10-2026-10-07.md, which also records why default serving never forms a window (classic batched prefill, --ignore-eos, unseeded requests) and that the 12B serves single-slot.

Not verified on this host

Metal. Metal 31B is row-wise too; changes reaching it are listed on #2158 (verify marking, per-row prefill and stacking, per-row verify and LM head at B>1, finish_layer extraction). The window gate keeps Metal declined.

Closes #2190

inureyes added a commit that referenced this pull request Oct 7, 2026
@inureyes inureyes added status:done Completed type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:linux Linux (CUDA / packaging) specific labels Oct 7, 2026
@inureyes
inureyes force-pushed the fix/issue-2190-gemma4-batched-mtp-verify branch from 4b2192c to d523512 Compare October 7, 2026 07:33
inureyes added a commit that referenced this pull request Oct 7, 2026
The batched Gemma 4 MTP adapter never set `mtp_verify`, so its verify skipped the #2185 row-wise path and B=1 batched runs drifted from the B=1 linear adapter and classic decode on the 31B and the CUDA 12B.

The batched adapter now marks verify forwards (prefill stays unmarked). On the row-wise geometries every row is prefilled on its own cache over the classic chunk partition and the caches are stacked with history first and a zero tail, so a short row is a row whose valid end lags the shared offset. A B>1 verify runs each row as its own [1, K] call through the B=1 helpers (Q/K/V, RoPE, attention, o_proj, MLP, LM head) and keeps only the cache write batched, cutting each row's stale gap out of its keys and using the ring cursor for the row's own length. A lagging row that lost sliding keys to shared-offset compaction is reported as an error instead of producing inexact tokens.

Refs #2190
Every shipped binary raises MLX's CUDA graph and cuDNN SDPA cache capacities at startup (#818, #1799), but the in-process model tests ran MLX's bare defaults. The new batched-vs-classic test crossed the graph cache's lifetime-miss abort (800 misses at the default 400) partway through its 12B half; at the server's 2000 it passes. A shared helper now applies the same defaults before each in-process test initializes MLX.

The batched-vs-classic test also counts only rounds in which every row was still generating when it checks for a divergent accept round.

Refs #2190
Batched MTP on the 31B beat classic batched decode on GB10 by 75% at B=2 and 53% at B=4 (null arm within 3%, identical bytes in every arm), so the unconditional row-wise decline in run_mtp_burst_batched becomes a gate. A row-wise window now runs only on CUDA, at B <= 4, with dense FP16 caches, prompts within the sliding window, and max_prompt_len + K * (max_tokens + 1) within the window plus the rollback buffer; anything else declines to classic before drafter IO. The last bound keeps the shared sliding ring from compacting under a lagging row, where the verify would refuse the round mid-burst; a finished row still advances up to K positions a round, hence K * max_tokens.

Batched windows now carry each row's classic history-boundary split into the per-row prefill, so prompt-cache serving builds the same prompt KV classic does. The path stays behind MLXCEL_ENABLE_MTP_BATCH.

Refs #2190
…ment

Adds the GB10 record for issue #2190 with its harness and raw data: exactness evidence for the batched adapter, three interleaved rounds per width with a classic null arm (31B B=2 +75 to 77%, B=4 +53 to 55%, null -2.3 to +3.1%, identical bytes in every arm), a graph-cache budget check, a shipped-binary round, and the decision per pair and width. It also records why a batched window does not form in default serving (classic batched prefill, --ignore-eos token bias, unseeded requests) and that the 12B serves single-slot.

CONTINUOUS_BATCHING.md and the MLXCEL_ENABLE_MTP_BATCH row describe when the row-wise geometries now serve a batched window.

Refs #2190
The 12B is a row-wise MTP geometry on CUDA only. On Metal its batched verify is #1986's subject, so the new test logs and skips a pair whose target is not row-wise instead of failing an assertion.

Refs #2190
The batched-window gate admitted any backend where MLX reports its GPU backend available, which a ROCm build can also satisfy. The admission was measured on CUDA only, so ROCm builds now decline row-wise windows like Metal does.

Refs #2190
@inureyes
inureyes force-pushed the fix/issue-2190-gemma4-batched-mtp-verify branch from d523512 to 21f0067 Compare October 7, 2026 09:43
@inureyes
inureyes merged commit a20ac93 into main Oct 7, 2026
26 checks passed
@inureyes
inureyes deleted the fix/issue-2190-gemma4-batched-mtp-verify branch October 7, 2026 10:05
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:linux Linux (CUDA / packaging) specific priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Bring Gemma 4 batched MTP verify onto the #2185 per-row path

1 participant