Skip to content

perf: harden ragged B>1 MTP batching (NaN-safe masked rows, per-row valid-length verify mask) #163

Closed

Description

@inureyes

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_sinks mask 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_masks wrapper 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

  1. added
    type:performance
    area:models
    priority:low
    status:in-progress
    status:review
    and removed
    status:in-progress
    on Jun 5, 2026
  2. inureyes commented on Jun 10, 2026

    @inureyes
    MemberAuthor

    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.

    Leg tok/s Ratio
    serial B=1, variable-length prompts 2.5 baseline
    ragged B=4, same prompts 3.2 1.25x vs serialization
    classic B=4, equal-length prompts 9.9 0.74x vs classic serial
    classic serial B=1, equal-length prompts 13.3 baseline

    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_BATCH and MLXCEL_ENABLE_MTP_BATCH_RAGGED stay 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).

  3. added
    status:done
    and removed
    status:review
    on Jun 10, 2026
  4. added this to the 0.2 milestone on Jun 21, 2026
  5. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:modelspriority:lowstatus:donetype:performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions