Repository navigation
Description
Summary
Support variable-length prompts in a single B>1 batched MTP speculative burst. Today the batched burst only groups concurrent requests whose prompt length is identical; a realistic mix of prompt lengths serializes into per-request B=1 bursts that head-of-line-block each other, which makes forced B>1 MTP slower than classic batched decode under realistic load.
Evidence (measured, M5 Max, 31B + bf16 assistant, 4 concurrent, temperature 0)
| Case | classic agg tok/s | B>1 MTP agg tok/s | speedup | parity |
|---|---|---|---|---|
| same-length window (true B>1 burst) | 31.8 | 33.7 | 1.06x | identical |
| variable-length mix (realistic) | 32.0 | 24.8 | 0.78x | identical |
In the variable-length run the four bursts complete at burst_ms ≈ 3800 / 8688 / 25680 / 25687 — i.e. serialized, each blocking the next. In the same-length run all four finish together (burst_ms ≈ 18.9 s each), confirming a single B>1 window formed. See docs/benchmark_results/gemma4-mtp-speculative-decoding.md.
Because of this, B>1 batched MTP stays off by default (MLXCEL_ENABLE_MTP_BATCH=1 forces it). Resolving the variable-length case is the prerequisite to making batched MTP worthwhile.
Root cause
The batched-burst window is formed in src/server/batch/scheduler.rs (~lines 1824–1852). Candidates may join the head's window only if every field matches, including an exact prompt-length equality:
self.prefill_queue.drain_matching_window(head_lane, max_extra, |candidate| {
candidate.prompt_tokens.len() == head_prompt_len // <-- the constraint
&& candidate.max_tokens == head_max_tokens
&& super::speculative_burst::sampling_config_eq(&candidate.sampling, &head_sampling)
&& super::speculative_burst::should_burst_for_sequence(dispatch, candidate)
&& super::speculative_burst::can_join_batched_burst_window(candidate)
});A request whose prompt length differs from the head is left in the prefill queue. It later runs as its own B=1 burst, and (per the server-startup notice) "head-of-line-blocks concurrent classic-decode rows for its full duration." The batched MTP verify path assumes a rectangular [B, prompt_len] window, so ragged prompt lengths are not currently representable in one burst.
Proposed approach
Allow a B>1 batched MTP burst to span rows with different prompt lengths by making the batched verify ragged-aware:
- Relax the window predicate (
scheduler.rs~1835): drop theprompt_tokens.len() == head_prompt_lenequality (keepmax_tokens/ sampling / lane matching, or relax those too as separate steps). Group up tomax_batch_sizeburst-eligible rows regardless of prompt length. - Left-pad the batched prefill/verify to
max(prompt_len)across the window, with a per-row attention mask so padding positions do not attend, and per-row valid lengths threaded intoprefill_and_seed. Gemma 4's sliding-window + K=V + proportional-RoPE attention mask construction must honor the per-row offset. - Per-row positions / RoPE anchors: the batched
MtpTargetalready preserves per-row cache positions and RoPE anchors after mixed accepts (seemtp_batched_burst_enableddocs andGemma4MtpBatchedTargetAdapter). Extend that so each row's initial position reflects its own (unpadded) prompt length, not a shared one. - Per-row finish / rollback: rows in the same window may accept different numbers of draft tokens per round and may hit EOS /
max_tokensat different steps; the batched generator must retire finished rows and continue the rest without collapsing the window to B=1.
Touchpoints
src/server/batch/scheduler.rs— window formation (drain_matching_windowpredicate, ~1834) and the B>1 decline guard.src/server/batch/speculative_burst.rs—can_join_batched_burst_window,sampling_config_eq, the batched burst driver, and the startup notice describing the limitation.src/models/gemma4_mtp_target.rs—Gemma4MtpBatchedTargetAdapter/Gemma4UnifiedMtpBatchedTargetAdapter(batched verify forward, per-row positions).src/lib/mlxcel-core/src/speculative/mtp/—MtpBatchedGenerator(per-row accept/rollback/retire).- Attention-mask construction for batched ragged prefill (Gemma 4 sliding-window / full-attention paths).
Acceptance criteria
- Concurrent requests with different prompt lengths form a single B>1 batched MTP burst (no serialized head-of-line-blocking B=1 bursts), verified from server logs (one batched burst, rows finishing together rather than sequentially).
- On the 31B + bf16 assistant at
temperature 0, a 4-request variable-length window is at least as fast as classic batched decode (target: clearly >1.0x, recovering the same-length ~1.06x or better), measured via the same harness indocs/benchmark_results/gemma4-mtp-speculative-decoding.md. - Greedy parity preserved: every row's
temperature 0output is byte-identical to its classic-decode output. - Once variable-length batched MTP is consistently faster than classic batched decode, evaluate flipping
MLXCEL_ENABLE_MTP_BATCHto default-on (mirroring the B=1 default-on change), keeping the env var as an opt-out.
Notes
- This is the follow-up referenced as "Variable-length-prompt batched bursts are a documented follow-up" in the speculative server-startup notice.
- B=1 MTP already runs by default and is profitable (~1.2–1.4x on the 31B, ~1.87x on the 12B Unified); this issue is specifically about the batched (B>1) concurrent-serving path.
Activity
fix: mask prompt padding in ragged batched MTP verify + drafter SWA
fix: mask resident prompt padding in every ragged MTP verify step
perf: support variable-length prompts in B>1 batched MTP bursts (#162)
perf: support variable-length prompts in B>1 batched MTP bursts (labl…