Skip to content

[rl] MTP verify steps: run verify rows on FA4's decode kernel - #6477

Draft
liangel-02 wants to merge 2 commits into
liangel-02/vllm-mtp-spec-decodefrom
liangel-02/mtp-verify-steps
Draft

liangel-02 wants to merge 2 commits into
liangel-02/vllm-mtp-spec-decodefrom
liangel-02/mtp-verify-steps

Conversation

@liangel-02

Copy link
Copy Markdown
Contributor

Supersedes #6364 (originally by felipemello1).

Summary (human-written)

Agent details

Needs retesting on B200 (SM100): every number below is from one GB300 (SM103). FA4's split heuristics are tuned per GPU, so B200 timings can differ.

Stack, bottom to top:

  1. "[rl] GDN builder: stage steps with drafts in fixed buffers"
  2. "[rl] MTP: replay the verify CUDA graph only when every row verifies"
  3. "[rl] GDN layer: run steps with drafts from TorchTitan's builder"
  4. "[rl] MTP speculative decoding in the vLLM generator"
  5. this PR
  6. "[rl] MTP on GDN models: FULL CUDA graphs for steps with prefill"
  7. "[rl] vLLM generator: fp8 KV cache through FA4's fp8 forward, MTP verify steps included"

The stack lands after the vLLM 0.33 PRs ("[rl] generator: vLLM 0.33 API fixes and TP padding in the model wrapper", "[rl] Install vLLM 0.33.0 on the torch nightly"). "[rl] generator: run on vLLM's model runner V2 (vLLM 0.34)" rebases on it.

TLDR: MTP verify rows (1 + k tokens each) now run on SwapAB, FA4's decode kernel for one query token per row, instead of its prefill kernel. On agent loads with long shared prompts, attention per 26-row verify step drops from 25.5 to 14.9 ms, plus 1.2 ms for the fp32 tail's kernels. Qwen3.8-27B, one GB300, 26 multi-turn sessions, MTP-3 with FULL_DECODE_ONLY: 1,008 output tok/s, vs 857 before this PR and 892 without MTP (an earlier version of the stack, NOTE).

With 3 drafts, each verify row's 4 query tokens become extra query heads of one decode row on FA4's SwapAB kernel, and the last 3 keys run in fp32 and merge by logsumexp, so attention per 26-row verify step takes 14.9 ms plus 1.2 ms instead of 25.5 ms, for 1,008 output tok/s vs 857.

verify step, row of seq_len L, k = 3 drafts (4 query tokens):
before:  4-token rows    -> FA4 prefill kernel
after:   keys 0 .. L-4   -> SwapAB, 4x query heads per row, KV splits
         keys L-3 .. L-1 -> fp32, causal among the row's tokens; logsumexp merge with the SwapAB part

Problem

  1. With num_speculative_tokens = k, each decode step verifies 1 + k tokens per request.
  2. FA4 runs SwapAB only for one query token per row, so 4-token verify rows take its prefill kernel.
  3. Cost: 23.9 ms of a 46.4 ms verify step's GPU time (26 rows sharing 3 prompts, profiled engine). MTP-3 ran at 857 output tok/s on a 26-session multi-turn load, 0.96x no MTP (892).

Solution

  1. _verify_rows_as_decode (vllm/attention/attention.py): each row's tokens become extra query heads of one decode row over the keys all of them see. The last k keys are added in fp32, causally, and merged by logsumexp.
  2. It runs only where SwapAB takes the rows: SM100/SM103, head dim 64/96/128/256, query heads per KV head x (1 + k) <= 32. It also needs a uniform verify step of 8+ rows, padded rows included: below that, the tail costs more than SwapAB saves (grid below). A FULL graph fixes the row count, so the check is graph-safe.
  3. A step whose rows all hold the same number of tokens (> 1) makes one FA4 varlen call: it has no 1-token decode row to split off. Before, such steps (verify steps under 8 rows, equal prefill rows) also launched an empty decode call. The remaining call has the same arguments, so the output is unchanged.
  4. The B200 RL workflow runs test_vllm_verify_attention.py.
  5. Unchanged: k = 0, and every graph mode.

Results

Numerics: not bitwise vs the prefill-kernel path (other kernels, other summation order).
  GPU test vs an fp32 causal reference: atol 2e-3, rtol 2e-2, at max_seq_len 262144 (Qwen3.8-27B's max_model_len).
  Graph vs eager, 24 greedy requests of 256 tokens: FULL_DECODE_ONLY vs NONE, 16/24 identical streams,
  mean logprob diff 2.24e-3.

Verify-path grid: forward on a uniform MTP-3 verify step, this PR's path vs the parent's FA4 varlen call (max_seqlen_q 4). Qwen3.5 attention configs (head dim 256), rows 1-64 x context 1k-128k, 128-token pages, max_seq_len 131072, CUDA graphs, FA4 4.0.0b40, one GB300, one run per point:

Heads (q/KV), hd256 KV pages Time vs parent: median (best .. worst) Points faster rel L2 vs parent (max)
24/4 unique per row +57% (+9% .. +153%) 0/35 3.5e-3
16/2 unique per row +73% (+12% .. +135%) 0/35 3.5e-3
16/4 unique per row +50% (+2% .. +126%) 0/35 3.5e-3
24/4 8-row groups share 3/4 +56% (-20% .. +156%) 7/35 3.5e-3
16/2 8-row groups share 3/4 +90% (-16% .. +158%) 5/35 3.5e-3
16/4 8-row groups share 3/4 +57% (-27% .. +151%) 8/35 3.5e-3
  • The path wins when 8+ rows share long prompts: SwapAB reads the shared pages from L2, and FA4's prefill kernel does not. 19 of the 20 faster points have 8+ rows and 64k+ context (0.73-0.99x).
  • Small steps pay a fixed cost: the tail's ~15 small kernels add 53-71 us per layer at 1-4 rows and up to 4k context (the parent's whole call: 44-60 us). So steps under 8 rows keep the varlen call.
  • With unique contexts of one length, the path loses. With mixed lengths, as an engine's rows have, it wins at 32-64 rows x 64k-128k:
24/4, hd256, unique KV per row, max_seq_len 262144, FA4 4.0.0b40, one GB300, CUDA graphs, us per layer:
                   one length per row              lengths 0.25x .. 1.75x of the mean
rows   mean ctx    parent   this PR   ratio        parent   this PR   ratio
32     64k         1,246    1,414     1.13         1,680    1,416     0.84
64     64k         2,466    2,703     1.10         2,871    2,730     0.95
32     128k        2,406    2,686     1.12         3,303    2,696     0.82
64     128k        4,843    5,245     1.08         5,665    5,300     0.94
Qwen3.8-27B, one GB300, TP1, bf16 KV, 26 multi-turn sessions in 3 groups (27k / 55k / 77k shared prompts),
180 s window, output tok/s:
  no MTP, FULL                                     892
  MTP-3, FULL_DECODE_ONLY, before this PR          857     0.96x
  MTP-3, FULL_DECODE_ONLY, this PR                 1,008   1.13x
Engine profile, one verify step (26 rows x 4 tokens): GPU 46.4 -> 37.2 ms; attention (target + drafter)
25.5 -> 14.9 ms, plus 1.2 ms for the verify tail's kernels.

On a unique-context load the path ties: 128 agent sessions in groups of 16 that share only a 4k first prompt, MTP-3, 761 output tok/s vs 763 for the varlen call. Its decode steps are 22% faster (p50 49.7 vs 63.7 ms), but the two arms' prefix-cache hits drifted apart (73% vs 82% of prompt tokens), so they ran different prefill work.

NOTE:

  • The engine numbers ran on a development branch: the MTP PR's earlier GDN path (a port of vLLM's mixed-step GDN code, replaced in this stack by the GDN builder and layer PRs), plus generator changes not in this stack. Its attention file differs from this PR's only in comments, keyword arguments, a head-dim term in the gate and the 8-row cutoff, which the 26-session run predates. The unique-context load ran FULL graphs with piecewise graphs for steps with prefill, in both arms. One run per arm. Not re-measured on this stack.
  • MTP pays only while the KV cache has room. 32 sessions sharing one 27k prompt, then each its own history: MTP-3 1,305 vs 903 tok/s without MTP (1.45x, with the earlier FULL piecewise graphs). At 46 sessions: 210 vs 572 (0.37x); both arms are KV-bound there, and MTP-3 evicts more history. The fp8 KV cache PR (7) fits 2x the tokens.
  • Two open PRs edit vllm/attention/attention.py; the one that lands second rebases: the vLLM V2 model runner PR and the ROCm paged-KV gather PR.

You may ask: why not vLLM's merge_attn_states for the merge? It takes token-major outputs of one dtype: three layout copies and a bf16 tail, for ~5 fewer kernels.

Follow up / TODO (the # TODO is in forward)

  • Drop the fp32 tail once FA4's floor has SwapAB token mode (an open FA4 PR): the plain varlen call then serves verify rows. Keep this path only for shapes token mode refuses, e.g. 6 query heads per KV head at k = 4. If FA4 declines token mode, fuse the tail and the merge into one Triton kernel: its ~15 small kernels per layer add 1.2 ms per verify step (~3%).

Test plan

pytest tests/rl/unit_tests/cpu
pytest tests/rl/unit_tests/gpu/test_vllm_verify_attention.py   # 1 GPU (GB300); also in the B200 RL workflow
pre-commit run --files <changed files>
RL CPU suite   788 passed, 0 failed, 8 skipped on the pinned vLLM (1.0.0.dev20261107; the MTP PR: the same), main
               merged in.
GPU            test_vllm_verify_attention.py: 3 passed (40 s). Verify, few-row and mixed batches through `forward`,
               against an fp32 reference; it counts FA4 calls: one for verify and few-row, two for mixed.
pre-commit     all hooks pass (pyrefly and lychee skipped)

Uniform verify steps of 8 rows or more run on FA4's decode kernel; fewer rows keep one
varlen call. The B200 RL job runs test_vllm_verify_attention.py.
@pytorch-bot pytorch-bot Bot added the ciflow/rl label Dec 1, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants