Repository navigation
Description
Follow-up to #161 / PR #162, which landed experimental variable-length ("ragged") B>1 batched MTP speculative bursts behind MLXCEL_ENABLE_MTP_BATCH_RAGGED=1. Greedy parity is byte-identical on the real 31B, but the PR review flagged robustness items that were deliberately deferred. Track them here.
Items
MEDIUM — NaN-safe fully-masked padding query rows
For any ragged row with left-padding lp > 0, the prefill query rows q in [0, lp) have an empty causal-AND-padding intersection, so their attention row is all -inf, and softmax over an all--inf row is NaN. Today parity holds only because MLX's fused SDPA hard-overrides additive -inf key columns (so q·k(NaN) on a masked column never propagates, and the NaN stays confined to padding-position outputs that are never consumed as real keys). This is correct in practice (the 31B gate is byte-identical across 35 layers) but silently depends on undocumented kernel masked-column semantics. Make it explicit: zero or skip the fully-masked padding query rows q in [0, lp), or add a NaN-safe assertion / documented invariant, so parity does not rely on SDPA behavior that could change.
MEDIUM — per-row valid-length tail not masked on verify
After divergent (mixed) accepts in a ragged window, each row's per-row kv_valid_len (logical position) diverges while the physical cache offset is the global max. The mask == None verify mask is derived from the global offset, so for the full-attention Cache::Standard path (whose zero_partial_accept_tail is a no-op) a shorter row can attend stale rejected-draft K/V in [lp_r + valid_len_r, global_offset). This is inherited from the pre-existing same-length batched path (validated byte-identical on the 31B with different-content same-length prompts) and is not a regression introduced by #162, but it is a real latent gap. Thread each row's per-row valid end into the verify mask so the tail is excluded.
LOW — off-by-one in the test-only make_drafter_masks wrapper
The drafter SWA refactor derives the SWA query position from effective_valid - 1. For the kv_valid_len == None wrapper path (make_drafter_masks), effective_valid falls back to query_offset, so the position becomes query_offset - 1 vs. the old query_offset. Only reachable from one test (drafter/masks.rs, the all-None fast path, unaffected); all production callers use make_drafter_masks_with_valid_len with Some(kv_valid_len). Harmless today; add a comment or make the wrapper pass query_offset + 1 for symmetry.
Throughput / default-on (the #161 conditional)
Variable-length ragged batching currently measures ~0.94 to 1.13x vs classic batched decode on the 31B (M5 Max), i.e. it fixes the prior serialization (~0.78x) but is not a consistent win, so MLXCEL_ENABLE_MTP_BATCH / MLXCEL_ENABLE_MTP_BATCH_RAGGED stay off by default. Once the ragged path is consistently faster than classic batched (profiling the per-row drafter/verify overhead, the short-row early-retire dynamics, and max_batch_prefill), evaluate flipping it default-on per the #161 conditional goal.
Touchpoints
src/models/gemma4.rs—forward_with_speculative_sinksmask construction; fully-masked padding query rows.src/models/gemma4_mtp_target.rs— per-row valid-length tail in the batched verify mask.src/lib/mlxcel-core/src/drafter/masks.rs—make_drafter_maskswrapper off-by-one.src/server/batch/speculative_burst.rs/scheduler.rs— throughput tuning for the default-on evaluation.
Acceptance
- Ragged batched MTP no longer relies on SDPA masked-column NaN semantics (explicit zero/skip or documented + asserted invariant); greedy parity stays byte-identical on the 31B.
- Per-row valid-length tails are excluded from the verify mask for both ragged and same-length batched bursts; greedy parity preserved.
- The LOW wrapper off-by-one is resolved or documented.
- A throughput study determines whether default-on is justified; if so, flip it (keeping the env vars as overrides).
Activity
test: scope ragged MTP parity gate to the hole-free lockstep prefix
inureyes commentedon Jun 10, 2026
Throughput study (item 4) and real-model verification notes from PR #202
Hardware and setup: M1 Ultra, REACHABLE_PAIRINGS[1] (gemma-4-31b-it-4bit target + gemma-4-31B-it-assistant-bf16 MTP drafter), block_size 4, greedy, 48 new tokens per row, 4 rows, 192 tokens per leg, via the new
mtp_gemma4_ragged_throughput_probe.Reading: the ragged path does fix the serialization penalty it was built for (#161 measured ~0.78x pre-ragged; this probe shows 1.25x in ragged's favor on the variable-length set). But classic batched MTP itself loses to serial B=1 at high accept rates: the equal-length set degenerates into repetitive loops the drafter predicts well, so serial B=1 reaches 13.3 tok/s while B=4 pays per-round batch overhead for 9.9. Batched MTP is therefore still not a consistent win over serial on this hardware, matching the 0.94 to 1.13x range PR #162 measured on an M5 Max.
Decision: default-on is not justified.
MLXCEL_ENABLE_MTP_BATCHandMLXCEL_ENABLE_MTP_BATCH_RAGGEDstay opt-in. Revisit after issue #203 (per-row position holes) lands, since post-divergence behavior also affects accept dynamics.Caveats: prompts are the synthetic token-id sets from
tests/speculative_parity.rs, which produce extreme accept-rate regimes (near-zero accepts on the variable-length set, near-max on the degenerate equal-length loops); treat the ratios, not the absolute tok/s, as the signal.Parity verification: the new ragged gate passes on M1 Ultra over the structurally guaranteed lockstep prefix (prefill bonus + rounds before each row's first sub-max accept; row 3 held 9/24 tokens byte-identical, and beyond the guarantee it tracked B=1 for 17 tokens). The verification also surfaced two pre-existing findings now tracked in #203: per-row position holes block full-stream byte-parity after divergent accepts (the strict equal-length gate is red on current main on M1 Ultra), and the M1 SDPA NaN behavior made main's ragged path emit token id 0 garbage from the very first token, which item 1's diagonal rescue in PR #202 fixes (with the fix, the ragged prefill bonus and lockstep prefix match B=1 on M1).
perf: harden ragged B>1 MTP batching masks and verify tail (#202)
test(turbo): exercise the steel kernel and classify #1712 failures (#…
perf: harden ragged B>1 MTP batching masks and verify tail (lablup#202)