Skip to content

[Spec] Windowed draft-decode attention for built-in EAGLE / MTP drafts - #32673

Merged
BBuf merged 1 commit into
sgl-project:mainfrom
avalliappan-nvidia:feat/windowed-mtp-draft-decode
Sep 22, 2026
Merged

BBuf merged 1 commit into
sgl-project:mainfrom
avalliappan-nvidia:feat/windowed-mtp-draft-decode

Conversation

@avalliappan-nvidia

@avalliappan-nvidia avalliappan-nvidia commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

[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-size in #24664, but that flag reaches
the drafter through the model file (llama_eagle3.py sets sliding_window_size per layer)
and the backend's per-layer SWA path, so it applies to LlamaForCausalLMEagle3
checkpoints. 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

  1. generate_draft_decode_kv_indices gains window_size and sink_size tl.constexpr
    params, both defaulting to 0. With window_size > 0 a draft-decode step gathers
    [first sink_size tokens] + [most recent window_size tokens] instead of the whole
    prefix, then appends the draft tree tokens unwindowed as before. window_size == 0
    collapses every added op to the identity, so the default path and the kernel's other
    callers (AITER, MLA) are unchanged.
  2. --speculative-draft-window-size is now also honored by the built-in EAGLE / MTP
    draft-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__.
  3. New --speculative-draft-sink-size for the attention sink, since a pure recent window
    drops 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 default sink=0 is
    min(seq_len, window), the same expression as _compute_compact_draft_seq_lens, with
    no -1. Tree tokens are never capped, so the existing
    window_size >= num_draft_tokens check still holds.
  4. A draft model with a sliding window of its own keeps it, and this windowing stays off.
    That covers an MTP head with SWA in its config and a LlamaForCausalLMEagle3 drafter,
    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 the
variable-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_mha target, EAGLE steps=6 topk=1 draft_tokens=7, W=4032 S=64 (4096 prefix entries per draft step), batch 1, RULER
niah_multiquery_enum, 512 output tokens. window vs native on the same draft backend;
the target is identical in every arm. T_iter is per-iteration wall time — the mechanism,
without acceptance mixed in — and TPOT is T_iter / AL.

context draft backend TPOT native → window speedup T_iter native → window AL native → window
32K triton 3.895 → 3.068 ms 1.27× 12.67 → 10.05 ms (−21%) 3.25 → 3.28
64K triton 4.414 → 3.264 ms 1.35× 19.06 → 13.48 ms (−29%) 4.32 → 4.13
128K triton 7.047 → 4.568 ms 1.54× 31.65 → 20.02 ms (−37%) 4.49 → 4.38
256K triton 10.448 → 6.093 ms 1.71× 57.35 → 33.45 ms (−42%) 5.49 → 5.49
32K flashinfer 2.199 → 2.142 ms 1.03× 7.20 → 7.01 ms (−3%) 3.28 → 3.28
64K flashinfer 1.728 → 1.738 ms 0.99× 7.46 → 7.18 ms (−4%) 4.32 → 4.13
128K flashinfer 1.799 → 1.696 ms 1.06× 8.08 → 7.44 ms (−8%) 4.49 → 4.38
256K flashinfer 1.735 → 1.499 ms 1.16× 9.36 → 8.05 ms (−14%) 5.39 → 5.37

Dense (no speculation) TPOT at those four contexts, for reference: 2.997 / 3.158 / 3.320
/ 3.698 ms.

  • T_iter drops at every point on both backends and the drop grows with context, which
    is the mechanism doing what it should: the draft's read is capped while native's grows.
  • TPOT also depends on acceptance, so the 64K FlashInfer point is a wash — a 4%
    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.
  • Acceptance never beats native in this sweep. It does past 512K in the paper, but that
    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 on
one GPU. Every expectation comes from a closed-form oracle over seq_lens, never from the
kernel's own packing, so a bug can't be mirrored on both sides.

  • window_size == 0 reproduces the full-KV read plan, offsets and gathered slots, for
    single- and multi-request batches at topk 1 and 2. This is the off-by-default
    regression guard.
  • Windowed kv_indptr lengths are min(seq_len, sink + window) + tree, and the gather
    really is [sink] + [recent] + [tree], parametrized past 256K/512K/1M.
  • Tree drafts (topk > 1): each (request, branch) slot gets its own windowed base list
    plus its own branch at the oracle offset. Covered at page_size == 1 and on the
    page_size > 1 and topk > 1 paged branch, where tree tokens are read from the uncapped
    prefix end but written at the capped offset.
  • resolve_draft_decode_window, including the own-window opt-out and exactly when it
    warns, 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

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@avalliappan-nvidia

Copy link
Copy Markdown
Contributor Author

@Qiaolin-Yu @hnyls2002 would you be able to take a look and kick off CI for this one?

@avalliappan-nvidia

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@ishandhanani

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Jul 30, 2026
@avalliappan-nvidia
avalliappan-nvidia force-pushed the feat/windowed-mtp-draft-decode branch from 6db3a94 to 7408215 Compare July 30, 2026 17:55
@avalliappan-nvidia

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@avalliappan-nvidia
avalliappan-nvidia force-pushed the feat/windowed-mtp-draft-decode branch 2 times, most recently from 580e8cf to 9f720aa Compare August 3, 2026 16:37
@avalliappan-nvidia

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@avalliappan-nvidia

Copy link
Copy Markdown
Contributor Author

@Qiaolin-Yu @hnyls2002 @ch-wan — would appreciate a look when you have a moment.

@avalliappan-nvidia

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@avalliappan-nvidia
avalliappan-nvidia force-pushed the feat/windowed-mtp-draft-decode branch from 9f720aa to f55ae7f Compare August 6, 2026 20:30
@avalliappan-nvidia

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@avalliappan-nvidia
avalliappan-nvidia force-pushed the feat/windowed-mtp-draft-decode branch from f55ae7f to 7c2e90f Compare August 12, 2026 17:09
@avalliappan-nvidia

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@trevor-m trevor-m left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@avalliappan-nvidia

avalliappan-nvidia commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor Author

@BBuf @Fridge003 @hnyls2002 — would appreciate if someone could take a look?
Happy to split the PR or refine it if that's easier to review. Thanks!

@avalliappan-nvidia
avalliappan-nvidia force-pushed the feat/windowed-mtp-draft-decode branch 2 times, most recently from 9b35889 to 3e186e4 Compare August 24, 2026 16:17
@avalliappan-nvidia
avalliappan-nvidia force-pushed the feat/windowed-mtp-draft-decode branch 2 times, most recently from 7350e87 to 9eb7898 Compare August 31, 2026 23:58
@avalliappan-nvidia

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@avalliappan-nvidia
avalliappan-nvidia force-pushed the feat/windowed-mtp-draft-decode branch 2 times, most recently from 0d98ba3 to 8dfa742 Compare September 8, 2026 18:19
@avalliappan-nvidia
avalliappan-nvidia force-pushed the feat/windowed-mtp-draft-decode branch from 8dfa742 to d16b2da Compare September 12, 2026 02:37
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.
@avalliappan-nvidia
avalliappan-nvidia force-pushed the feat/windowed-mtp-draft-decode branch from d16b2da to 28ef9f4 Compare September 12, 2026 04:24
@avalliappan-nvidia

Copy link
Copy Markdown
Contributor Author

/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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

@BBuf
BBuf merged commit 61d0cf2 into sgl-project:main Sep 22, 2026
243 of 278 checks passed
kevin-mii added a commit to Jacob0226/sglang that referenced this pull request Sep 22, 2026
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]>
Leoyzen pushed a commit to Leoyzen/sglang that referenced this pull request Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit-kernel run-ci CI: run the baseline test suite on this PR speculative-decoding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants