Repository navigation
[rl] MTP verify steps: run verify rows on FA4's decode kernel - #6364
felipemello1 wants to merge 2 commits into
Conversation
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 6m 11s —— View job Approve -- please address the items before merging (empty human-written summary; link the upstream issues in the torch and vLLM TODOs). Review |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. The two problems are real on the parent (FULL + drafts raises; verify rows take FA4's prefill kernel), the fix stays in the vLLM generator, and the remaining items are small edits. A maintainer still needs to review.
What this PR does.
- A verify step's attention runs as one decode row per request when every row has 1 + k tokens and there are 8 or more rows. The row's tokens become extra query heads over the keys all of them see. The last k keys are added in fp32 and merged by logsumexp.
cuda_graph.mode="FULL"with drafts now maps to vLLM'sFULL_AND_PIECEWISEinstead of raising. The speculative GDN call becomes an eager graph break.
Example. One row, seq_len L = 1000, k = 3 (tokens at positions 996..999):
before: FA4 varlen, max_seqlen_q 4 -> prefill kernel
after: FA4 decode, 4 x 24 query heads over keys 0..996 (seqused_k = 997)
fp32: token j also sees keys 997..996+j (token 0: none, lse -inf)
out = exp(lse_a - lse) * out_a + exp(lse_b - lse) * out_b
cuda_graph.mode="FULL", k=3:
before: ValueError in VLLMEngine.Config.__post_init__
after: uniform verify steps -> FULL graph; steps with prefill and the drafter -> piecewise, attention + GDN eager
I checked the causal split against the reference: token j at position L-n+j sees shared keys 0..L-n plus tail keys i < j. It is correct for any uniform n-token row whose queries are the row's last n positions, so a prefill step whose rows all happen to have n tokens is also handled correctly.
Must fix
- PR description: "Summary (human-written)" is empty. The author needs to fill it in. Agents don't write it.
attention.py:294-296,engine.py:157-158-- the upstream-workaround TODOs say "no torch issue filed yet" and "no vLLM issue filed yet". File both issues and link them (inline).
Should consider
attention.py:376-- use shape suffixes in_verify_rows_as_decode(inline).test_vllm_verify_attention.py:152-- the secondmax_seq_lenvalue doubles the B200 cases. Keep it only if it guards a distinct FA4 split path (inline).
Questions
- Your grid times this path slower at all 35 unique-context points, including 1.08-1.14x at the unique-context engine load's shapes, yet that load's decode steps were 22% faster. The cutoff counts only rows, so every 8+-row verify step takes this path whether or not its rows share pages. Can one of the four listed differences be checked before landing? Otherwise we keep a dispatch rule whose own microbenchmark disagrees with it, and the cause is unexplained (CLAUDE.md principle 1). GRPO groups usually share prompts, so the default is plausibly right for RL. The ask is to confirm it, not to change it.
_enable_breakable_cuda_graphsraises if anything imported vLLM's attention module beforeVLLMEngine.__init__. The only engine runs are on the dev branch, and no test builds aVLLMEnginewith FULL + k > 0. Was the engine built end to end at this commit with the env var unset at process start (e.g. throughtorchtitan/rl/generate.py)?FULL_AND_PIECEWISEcaptures piecewise graphs for every capture size, up tomax_num_batched_tokensin FULL mode, on top of the FULL uniform-decode graphs. What do capture time and graph memory cost vs. FULL_DECODE_ONLY on Qwen3.8-27B?
Details
- Gate: both problems reproduce on the parent: the
__post_init__raise, andmax_seqlen_q > 1sending verify rows to FA4's prefill kernel. The layer is right: TorchTitan's CUSTOM backend owns these FA4 calls, and the kernel-side fix is upstream (Dao-AILab/flash-attention#3217) with a TODO. No overlapping PR changes the dispatch; the V2-runner and ROCm PRs only conflict textually, as the description says. - Graph safety: inside a FULL graph the branch is decided at capture from Python ints (
max_query_len,num_actual_tokens,seq_lens.shape[0]). Under piecewise replay, attention runs eagerly and re-reads live metadata (get_attention_context,attention.py:224), so mixed steps fall back to the varlen call. No host syncs in the new path. - Padded rows (
seq_len0): the shared part hasseqused_k0. The tail reads block-table column 0, a valid page id, and the merge'snan_to_num_gives finite garbage for those rows. Their output is discarded, so this is fine. - Model coverage: only Qwen3.5-architecture drafts are accepted (
worker.py:262). KDA rejects speculative decoding (kda.py:91), andout_transform(sinks) is excluded by the gate, so there is no other attention/linear-attention layer that would need a graph break underFULL_AND_PIECEWISE. mode="FULL"now means "FULL where vLLM's GDN builder allows it". That is documented onVLLMCudaGraphConfig, so it isn't a silent fallback. The alternative is a separateFULL_AND_PIECEWISEmode value, which would add a knob only MTP uses. Keeping the mapping seems right.
Not verified: vLLM's eager_break_during_capture / BreakableCUDAGraphCapture semantics, and whether vLLM accepts FULL_AND_PIECEWISE with CompilationMode.NONE. vLLM isn't installed in this runner, so I'm relying on the PR's GPU tests and engine runs.
|
Reply from claude: Round 1 fixes are in e85f0c0. The two issue links wait on the author (see their threads). The questions are answered below.
Q1: grid vs engine. Checked. The grid gave every row one context length, and the engine's rows have mixed lengths. With the same mean but rows spread from 0.25x to 1.75x of it, this path takes 0.82-0.95x the parent's time, vs 1.08-1.13x with one length. The parent's prefill kernel gets 16-37% slower on uneven rows. This path stays within 1%. The table is in the body. The 8-row cutoff stays. Q2: an engine built with the variable unset? Not at this exact commit. An RL run on a development branch did it through the RL launcher, and nothing set Q3: capture cost. Qwen3.8-27B on one GB300, now in the body: So with drafts, FULL costs ~28 s and ~3.2 GiB more than FULL_DECODE_ONLY, and no more than FULL without MTP. |
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 1m 3s —— View job Approve -- please address the items before merging (round 1 is resolved except the human-written summary and the torch/vLLM issue links in the two TODOs). Review |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. Round 1 is resolved except for the two upstream-issue links and the human-written summary, and e85f0c0 adds no new problems. A maintainer still needs to review.
What this PR does. Verify steps with 8 or more uniform rows of 1 + k tokens run their attention on FA4's decode kernel: each row's tokens act as extra query heads over the keys they all see, and the last k keys are added in fp32 and merged by logsumexp. cuda_graph.mode="FULL" with drafts now maps to vLLM's FULL_AND_PIECEWISE and no longer raises. The speculative GDN call becomes an eager graph break.
Example. One row, L = 1000, k = 3:
before: FA4 varlen, max_seqlen_q 4 -> prefill kernel
after: FA4 decode, 4 x 24 heads over keys 0..996; fp32 tail keys 997..999, token j sees the first j
FULL + k=3: before ValueError; after verify-only steps -> FULL graph, steps with prefill and the drafter -> piecewise
Round 1, resolved
- Shape suffixes in
_verify_rows_as_decode: done, legend in the docstring. I checked the rename against the old code. The views and permutes are unchanged, andshared_out_RKJGVis allocated in 5-D and viewed to 3-D for FA4, the same memory as before. The oldrikd->rkjgdvalue einsum is now correctly labelledrikv->rkjgv. - The second
max_seq_lenvalue was dropped, which halves the B200 cases. - Q1 (the grid disagreed with the engine load): answered. Mixed row lengths flip the grid to 0.82-0.95x, and that table is now in the body. The 8-row cutoff is justified.
- Q2 (an engine built with the env var unset): answered with an RL-launcher run on the dev branch, whose
_enable_breakable_cuda_graphsmatches this one. - Q3 (capture cost): answered. FULL with drafts costs ~28 s and ~3.2 GiB more than FULL_DECODE_ONLY, and no more than FULL without MTP.
- The
mixedcase is now[1, 4, 4]. It exercises the leading-single-token split inforward(decode call for row 0, varlen call for rows 1-2), which the old[4, 4, 1]didn't.
Must fix (still open)
- PR description: "Summary (human-written)" is still empty. The author fills it in.
attention.py:294-296,engine.py:157-158: the upstream-workaround TODOs still say "no torch issue filed yet" / "no vLLM issue filed yet". File both issues and link them (CLAUDE.md principle 3). Per the replies, this waits on the author.
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.
dc7ca75 to
974dde2
Compare
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 2m 11s —— View job Approve: every earlier must-fix item is resolved at 974dde2, and the reworked commit adds no new problems. One open question: this PR's TODO and the one in #6366 give different removal conditions. Review |
There was a problem hiding this comment.
Verdict: Approve -- every earlier must-fix item is resolved at 974dde2, and the reworked commit adds no new problems. A maintainer still needs to review.
What this PR does. A uniform verify step runs its attention on FA4's SwapAB decode kernel. Every row must have 1 + k tokens, there must be 8 or more rows (padded rows count), and the layer must pass the SM100/SM103, head-dim and heads x tokens <= 32 gate. Each row's tokens become extra query heads over the keys they all see. The last k keys are added in fp32 and merged by logsumexp. Any step whose rows all hold the same number of tokens now makes one FA4 call instead of a real call plus an empty decode call. The FULL_AND_PIECEWISE / breakable-graph change is gone from this PR (it now lives in #6403), so this diff no longer touches engine.py.
Example. 26 rows, k = 3, one row with L = 1000:
before: FA4 varlen, max_seqlen_q 4 -> prefill kernel (decode call empty)
after: FA4 decode, 4 x 24 heads, seqused_k 997 -> SwapAB
fp32 tail: keys 997..999, token j sees the first j; logsumexp merge
3 rows (< 8): one varlen call, as before minus the empty decode call
[1, 4, 4] rows: decode call for row 0 + varlen call for rows 1-2 (unchanged)
Earlier items
- Upstream-workaround TODO (
attention.py:303-307): resolved. It now links Dao-AILab/flash-attention#3217 and gives a removal condition, which is FA4 token mode serving the plain varlen call. The vLLM TODO left with theengine.pychange. - Shape suffixes, the second
max_seq_lenvalue, and the[1, 4, 4]mixed case: still in place. - Q2/Q3 (breakable graphs,
FULL_AND_PIECEWISEcapture cost): no longer apply here. They belong to #6403.
Questions
- The stacked fp8 PR #6366 still carries the old TODO ("once torch's varlen API passes FA4's query_len_in_heads"). That PR passes the knob explicitly to
_flash_attn_fwd. This PR's TODO says the plain varlen call will serve verify rows once FA4 has token mode, with no torch change. Which removal condition is right? Make the two TODOs agree when #6366 rebases.
Details
- Gate: the problem is still real on the parent.
max_seqlen_q > 1sends verify rows to FA4's prefill kernel. The fix is in the layer that owns these FA4 calls (TorchTitan's CUSTOM backend), and the kernel-side fix is upstream with a link. is_uniform(attention.py:302): each row holds at mostmax_query_lentokens, sonum_actual_tokens == max_query_len * num_rowsholds only if every row is full. In a FULL graph the decision uses Python ints fixed at capture. Under eager/piecewise it uses the live metadata. Skipping the split for uniform steps drops only the call whoseseqused_kis all zeros, so output is unchanged.- Uniform prefill steps: a prefill step of >= 8 rows that each happen to have 1 + k tokens also takes the decode path. That is still correct, because vLLM's queries are always the last n positions of each row.
- Padded rows (
seq_len0): the shared part hasseqused_k0, the tail reads block-table column 0, andnan_to_num_keeps the result finite. The output is discarded. - DiffKV:
head_dim_vcomes fromvalue_cache.shape[-1], so the path handles it. No MTP-capable model reaches DiffKV today: Qwen3.5 full attention has equal K/V head dims, and KDA refuses drafts. - Overlap search: "MTP", "SwapAB", "verify attention.py". Found the author's own stack (#6287, #6365, #6366, #6402, #6403, #6432, #6433) and #5956 (V2 runner, a textual conflict only).
Not verified: FA4 #3217's token-mode behavior, and the GPU test and timings (no GPU or vLLM in this runner).
Before merge: fill in the human-written summary.
|
Reply from claude: This PR's condition is the right one: with FA4 token mode, the plain varlen call serves verify rows, with no torch change. If the stacked fp8 verify-rows PR stays open, its TODO gets the same condition when it rebases. |
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 1m 0s —— View job Approve: the PR's diff is unchanged since my full review at 974dde2, and the base-branch merge doesn't touch the verify path. Review |
There was a problem hiding this comment.
Verdict: Approve -- the PR's diff at fa1ea64 matches the one I reviewed in full at 974dde2, line for line. The only new commit is a merge of the base branch, and it doesn't touch the verify path. A maintainer still needs to review.
What this PR does. A uniform verify step runs its attention on FA4's SwapAB decode kernel. Every row must have 1 + k tokens, there must be 8 or more rows, and the layer must pass the SM100/SM103, head-dim and heads x tokens <= 32 gate. Each row's tokens become extra query heads over the keys they all see. The last k keys are added in fp32 and merged by logsumexp. A step whose rows all have the same token count makes one FA4 varlen call instead of a real call plus an empty decode call.
Example. 26 rows, k = 3, one row with L = 1000:
before: FA4 varlen, max_seqlen_q 4 -> prefill kernel (decode call empty)
after: FA4 decode, 4 x 24 heads, seqused_k 997 -> SwapAB
fp32 tail: keys 997..999, token j sees the first j; logsumexp merge
3 rows (< 8): one varlen call
[1, 4, 4] rows: decode call for row 0 + varlen call for rows 1-2 (unchanged)
Since the last review
- Earlier must-fix items: all were resolved at 974dde2 and are still in place.
- The question about the TODO in #6366: answered. The removal condition in this PR is FA4 token mode serving the plain varlen call, and #6366 takes the same wording when it rebases. Nothing more is needed here.
- New commits: c8c6e08 and the merge fa1ea64 come from the base branch. They change worker.py, registry.py, the GDN builder's TODO, and the KV-offload and Mooncake code. None of these files is in this PR's diff, and none changes what
forwardor_verify_rows_as_decodereceives.
Details
- Diff identity: compared the added and removed lines of
git diff 9265203 974dde2withgh pr diff 6364at HEAD. They are identical. - Overlap search: searched "verify attention.py" and "SwapAB". Found only the author's own stack (#6365, #6366, #6432) and the PRs already named in the description (#5956, #4866), which conflict only in text.
Not verified: the GPU test and the timings (this runner has no GPU and no vLLM).
Before merge: fill in the human-written summary.
|
Superseded by #6477 |
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:
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).
Problem
num_speculative_tokens = k, each decode step verifies 1 + k tokens per request.Solution
_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.test_vllm_verify_attention.py.Results
Verify-path grid:
forwardon 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: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:
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_statesfor 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
# TODOis inforward)Test plan