Repository navigation
[Spec] Windowed draft-decode attention for built-in EAGLE / MTP drafts - #32673
Conversation
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
|
@Qiaolin-Yu @hnyls2002 would you be able to take a look and kick off CI for this one? |
|
/rerun-failed-ci |
|
/tag-and-rerun-ci |
6db3a94 to
7408215
Compare
|
/rerun-failed-ci |
580e8cf to
9f720aa
Compare
|
/rerun-failed-ci |
|
@Qiaolin-Yu @hnyls2002 @ch-wan — would appreciate a look when you have a moment. |
|
/rerun-failed-ci |
9f720aa to
f55ae7f
Compare
|
/rerun-failed-ci |
f55ae7f to
7c2e90f
Compare
|
/rerun-failed-ci |
|
@BBuf @Fridge003 @hnyls2002 — would appreciate if someone could take a look? |
9b35889 to
3e186e4
Compare
7350e87 to
9eb7898
Compare
|
/rerun-failed-ci |
0d98ba3 to
8dfa742
Compare
8dfa742 to
d16b2da
Compare
Add an optional sliding-window + attention-sink KV read for the MTP/EAGLE draft decode step so the draft's per-step attention no longer scans the full context. The new server args speculative_draft_window_size / speculative_draft_sink_size are honored by both the Triton and FlashInfer multi-step draft backends through the shared generate_draft_decode_kv_indices kernel, so one kernel change covers both. window_size=0 preserves the exact full-KV read plan, and the target still verifies every proposed token at full attention, so windowing can only change which tokens are proposed, never which are committed. resolve_draft_decode_window() is the single place mapping the server args to the kernel's (window_size, sink_size). It returns (0, 0) when the flag is unset and also when the draft model ships its own sliding window: the index builder emits one KV list shared by every draft layer, so it cannot express a per-layer window, and a checkpoint's window is authoritative over a server flag. Adds a unit test for the KV-index builder covering the off-by-default identity, windowed lengths and gathered contents up to ~1M, tree drafts at topk > 1 in both the simple and paged extend branches, window resolution including the native-sliding-window opt-out, and a CPU guard that both backends wire the resolver through to the kernel.
d16b2da to
28ef9f4
Compare
|
/rerun-failed-ci |
| self.assertIn("resolve_draft_decode_window", init_src) | ||
| self.assertIn("self.draft_window_size", init_src) | ||
| self.assertIn("self.draft_sink_size", init_src) | ||
| tmpl_src = inspect.getsource(cls.common_template) |
There was a problem hiding this comment.
Non-blocking test suggestion: could we add a window-enabled CUDA Graph capture/replay regression for the actual Triton and FlashInfer draft backends? The kernel tests cover the packed indices, while this wiring test only inspects the Python source.
Replaying the same captured batch shape with updated seq_lens/positions, including lengths below and above sink_size + window_size, would check that the compact KV metadata and backend planning/split state stay consistent across replays. Comparing the attention outputs against eager windowed execution, with a nonzero sink and topk > 1 where supported, would cover the integration beyond the index builder. I have not identified a correctness mismatch here; this is additional regression coverage.
There was a problem hiding this comment.
@BBuf Thanks for the suggestion. I scoped the existing CUDA-graph test infrastructure and this would extend:
python/sglang/test/kits/attention_unittest/runner_modes/speculative_draft_runner.py
test/registered/attention/unittests/dense/test_triton.py
test/registered/attention/unittests/dense/test_flashinfer.py
The test would capture once, then replay with changed seq_lens / positions and verify windowed graph output and compact KV metadata against eager execution for both backends.
Would you prefer that I add it to this PR, or keep this PR focused and follow up with the graph test separately?
check_registered_tests.py requires kernel tests to live under
test/registered/kernels/{ops,benchmark}/<group>/. sgl-project#32673 added this one at
test/registered/kernel/spec/, which fails the taxonomy check and turns the
lint job red on every open PR, this one included.
Move it beside the other draft/spec kernel tests in kernels/ops/speculative/.
Nothing references the old path, and register_cuda_ci(stage="base-b-kernel-unit")
is declared in the file rather than derived from it, so CI dispatch is unchanged.
Co-authored-by: Cursor <[email protected]>
sgl-project#32673) (cherry picked from commit 61d0cf2)
[Spec] Windowed draft-decode attention for built-in EAGLE / MTP drafts
Motivation
In arXiv:2607.21535 we show that a built-in MTP draft
head's full-attention KV read grows with the prefix until it dominates the decode step,
and that restricting the draft's attention to a sink plus recent window cuts the
per-decode-step cost by +28% to +44% at 1M context across three architecture families at
near-unchanged acceptance, with the target still verifying every token at full attention.
The same idea motivated
--speculative-draft-window-sizein #24664, but that flag reachesthe drafter through the model file (
llama_eagle3.pysetssliding_window_sizeper layer)and the backend's per-layer SWA path, so it applies to
LlamaForCausalLMEagle3checkpoints. This PR applies the window one level down, in the shared draft-decode index
builder, so the built-in EAGLE / MTP (NEXTN) draft-decode path gets it on the Triton and
FlashInfer draft backends with no model-file change and no second attention wrapper — the
draft simply gets a shorter KV list. Only the windowing mechanism is upstreamed here; the
paper's ring-buffer reclaim of the unread draft KV is possible follow-up work.
Modifications
generate_draft_decode_kv_indicesgainswindow_sizeandsink_sizetl.constexprparams, both defaulting to
0. Withwindow_size > 0a draft-decode step gathers[first sink_size tokens] + [most recent window_size tokens]instead of the wholeprefix, then appends the draft tree tokens unwindowed as before.
window_size == 0collapses every added op to the identity, so the default path and the kernel's other
callers (AITER, MLA) are unchanged.
--speculative-draft-window-sizeis now also honored by the built-in EAGLE / MTPdraft-decode path on the Triton and FlashInfer draft backends.
spec_utils.resolve_draft_decode_window()is the single mapping from server args to(window_size, sink_size); both*MultiStepDraftBackends call it once in__init__.--speculative-draft-sink-sizefor the attention sink, since a pure recent windowdrops the initial tokens attention reliably parks on. The window keeps DFLASH's unit
and anchor — KV entries, counted back from the end of the prefix — so a step reads
min(seq_len, window + sink)prefix entries, which at the defaultsink=0ismin(seq_len, window), the same expression as_compute_compact_draft_seq_lens, withno
-1. Tree tokens are never capped, so the existingwindow_size >= num_draft_tokenscheck still holds.That covers an MTP head with SWA in its config and a
LlamaForCausalLMEagle3drafter,whose window comes from this same flag. One KV list is shared by every draft layer, so
the index builder cannot express a per-layer window, and a checkpoint's own window
should win over a server flag either way. We warn only when the resulting window
differs from the requested one, so the EAGLE-3 case stays quiet.
CUDA graphs are safe: the per-request kept length rides in
kv_indptr, exactly like thevariable-seqlen path the graph already replays. Same launch grid, no host sync, no dynamic
shapes.
Speed Tests and Profiling
Qwen3.6-35B-A3B bf16, one B200,
trtllm_mhatarget, EAGLEsteps=6 topk=1 draft_tokens=7,W=4032 S=64(4096 prefix entries per draft step), batch 1, RULERniah_multiquery_enum, 512 output tokens.windowvsnativeon the same draft backend;the target is identical in every arm.
T_iteris per-iteration wall time — the mechanism,without acceptance mixed in — and TPOT is
T_iter / AL.Dense (no speculation) TPOT at those four contexts, for reference: 2.997 / 3.158 / 3.320
/ 3.698 ms.
T_iterdrops at every point on both backends and the drop grows with context, whichis the mechanism doing what it should: the draft's read is capped while native's grows.
per-iteration saving against a 4% acceptance dip. The window is a clear win on
FlashInfer from ~128K up and at every point on Triton, whose full-prefix draft read is
far more expensive to begin with.
needs RoPE extension beyond this checkpoint's 256K native context, so we leave it out
of the PR's numbers.
Testing
test/registered/unit/spec/test_draft_decode_window.py— 13 tests / 28 subtests, 16 s onone GPU. Every expectation comes from a closed-form oracle over
seq_lens, never from thekernel's own packing, so a bug can't be mirrored on both sides.
window_size == 0reproduces the full-KV read plan, offsets and gathered slots, forsingle- and multi-request batches at
topk1 and 2. This is the off-by-defaultregression guard.
kv_indptrlengths aremin(seq_len, sink + window) + tree, and the gatherreally is
[sink] + [recent] + [tree], parametrized past 256K/512K/1M.topk > 1): each(request, branch)slot gets its own windowed base listplus its own branch at the oracle offset. Covered at
page_size == 1and on thepage_size > 1 and topk > 1paged branch, where tree tokens are read from the uncappedprefix end but written at the capped offset.
resolve_draft_decode_window, including the own-window opt-out and exactly when itwarns, plus a CPU-only guard that both draft backends resolve through it.
Checklist
CI States
Latest PR Test (Base): ✅ Run #34672938539
Latest PR Test (Extra): ❌ Run #34672938427
Latest PR Test (AMD ROCm 10): ❌ Run #34672938503