Repository navigation
[rl] GDN builder: stage steps with drafts in fixed buffers - #6432
felipemello1 wants to merge 3 commits into
Conversation
TorchTitanLinearAttentionMetadataBuilder builds GDN metadata for steps with MTP drafts: verify rows (1 + k tokens with context) go to GDN's verify kernels, the rest to the prefill kernels, in fixed buffers that FULL graphs can replay. - Rows of at most 1 + k tokens lead every batch (reorder threshold 1 + k); the runner checks it in calculate_reorder_batch_threshold. - The runner tells the builders which graph a step replays (packed or uniform) and whether a dummy run stages a uniform batch (null slots, accepted counts of 1). - Accepted counts come from the runner's buffer, which vLLM refreshes on every step. - Rows with drafts but no context raise; so does a uniform graph that meets another row. No caller sets drafts yet: the GDN layer runs these rows in the next PR, and MTP turns them on after it.
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 6m 12s —— View job Needs discussion -- review posted (correctness depends on vLLM kernel and runner behavior I couldn't read; the subclass TODO needs an upstream vLLM issue link). |
There was a problem hiding this comment.
Verdict: Needs discussion -- the staging logic holds up on its own terms, but whether it is correct depends on vLLM kernel and runner behavior I couldn't read (vLLM isn't installed here). One TODO also needs an upstream link.
What this PR does. When MTP drafts are on (use_spec_decode), TorchTitan's GDN builder takes a new path, _build_with_drafts. It sorts rows on the CPU into verify-kernel rows (rows with drafts, and 1-token rows after context) and prefill-kernel rows. It writes both into buffers sized for the largest step, with the null slot 0 on rows a kernel doesn't own. That keeps every tensor shape fixed, so one FULL graph (packed or uniform verify) can replay any mix of rows. The runner now also tells the builders whether a uniform graph is being replayed and whether a dummy run is uniform, and hands them its accepted-count buffer. Without drafts, nothing changes. Nothing sets drafts until #6287.
Example (from the docstring: k = 3, capacity 5, 24 padded tokens, so S = 20):
rows verify 4 | verify 4 | 1-token resume | prefill 13 | pad
spec_query_start_loc [0, 4, 8, 9, 20, 20, 20] -> row 3 gets 11 tokens on the verify kernels, null slot
prefill_query_start_loc [0, 9, 9, 9, 22, 24, 24] -> rows 0-2 merged into one null interval
before: vLLM's GDN builder makes mix-dependent tensors -> no FULL graph for this step
after: fixed buffers -> packed FULL graph replays it
Must fix
linear_attention.py:88-- the subclass is a workaround for vLLM V1'sisinstancegate, and its TODO links a torchtitan PR, not a vLLM issue. It also makes every KDA engine run GDN's__init__. Details inline.
Should consider
worker.py:199-- readforce_uniform_decodefromkwargsinstead of changing the signature and re-passingNoneto vLLM. Details inline.
Questions
- Null-slot rows in the verify kernels. In the example, row 3 hands the verify kernels an 11-token interval but only 4 slot columns. Do
fused_sigmoid_gating_delta_rule_updateandcausal_conv1d_updatereturn early on a row whose slot is 0, before any per-tokenssm_state_indices[row, t]load or store? If they only checkidx >= 0per token, rows longer than 1 + k inside S read the next rows' slots, and late rows read pastspec_state_indices_tensor. The conv update could also overwrite the prefill conv's output for those tokens. If the skip isn't guaranteed, clampspec_query_start_locat the end of the last verify row (known on the CPU) instead of at S. That gives the later non-verify rows zero-length intervals, with no shape change. - Does the V2 runner in #5956 pass
num_accepted_tokens/num_decode_draft_tokens_cputo non-GDN builders? If so, the subclass has a known end date. If not, an upstream issue is the only exit.
Details
- Overlap search: "GDN", "MTP", "linear_attention.py". Found the rest of this stack (#6433, #6402, #6287, #6364, #6403, #6365) and #5956 (V2 runner), which rewrites
_determine_batch_execution_and_paddingand the builder flags. Per the description, #5956 rebases on this stack, so they are coordinated. - Value: the stack's measurement is in #6403: +6.3% / +4.4% output tok/s for FULL over piecewise (Qwen3.8-27B, one run per arm, on an earlier version of the stack). That's acceptable for a stack that lands together. Citing it in this PR would help a reader.
- Checked: the buffer sizes for PACKED (
num_rows = capacity + 1, which fits thenum_reqs_capacity + 2offsets), the run-merging inprefill_query_start_loc, dummy-run staging, the order in which paths are picked, and thatupdate_block_tablekeeps the captured buffers. No D2H syncs: classification uses*_cputensors andasync_tensor_h2d. _update_block_table_with_draftsworks out which rows are real from!= 0on the first group's slots. That's sound only because slot 0 is vLLM's null block and is never handed out.- The FA4 builder moves from
reorder_batch_threshold = 1to_init_reorder_batch_threshold(1, supports_spec_as_decode=True). Without drafts this is the same. With drafts, the leading single-token split in FA4 no longer fires behind verify rows (the comment atattention.py:266says so). #6364 addresses that.
Not verified: the vLLM behaviors listed above, that vLLM's runner refreshes num_accepted_tokens.gpu in batch order on every step, and that None is the parent's default for force_uniform_decode. These depend on vLLM source, which isn't available here.
Before merge: fill in the human-written summary.
| # TODO: with the move to vLLM's V2 model runner (pytorch/torchtitan#5956), drop the subclass if V2 | ||
| # passes draft counts to any builder that asks. V1 passes them only to known builder classes | ||
| # (GPUModelRunner._build_attention_metadata). |
There was a problem hiding this comment.
Must fix. This subclass exists only to get past vLLM V1's isinstance(builder, GDNAttentionMetadataBuilder) gate in GPUModelRunner._build_attention_metadata. That is a workaround for a vLLM gap, and CLAUDE.md principle 3 asks for a TODO that links an upstream issue. This TODO links a torchtitan PR instead (#5956, which is the open V2-runner PR, not an issue). The workaround also has a cost on main: every KDA engine now runs vLLM's GDN __init__ (spec buffers that get replaced, plus a GDN prefill-backend lookup that reads hf_text_config). The tests have to stub model_config for it (test_gdn_graph_metadata.py:705).
Fix: open a vLLM issue asking the runner to pass draft and accepted counts to any builder that declares it wants them (for example a class attribute), and link it here. #5956 is already open, so the TODO can also say whether V2 passes these counts today. If it does, the condition can be dropped.
There was a problem hiding this comment.
Reply from claude: Fixed in 869745f: the TODO says what vLLM must change, and that both of vLLM's runners gate the counts by builder class. A vLLM feature request is drafted for Felipe; the TODO links it once it's open.
V2 gates the same way, so the move to V2 alone doesn't end the subclass. MambaHybridAttnMetadata.get_extra_attn_kwargs passes the counts only to vLLM's 4 builder classes (vLLM main 63bcaee474, vllm/v1/worker/gpu/model_states/mamba_hybrid.py:54-66).
KDA engines still start with the GDN parent __init__: TestBitwiseParityKimiK3DebugVarlen (Kimi K3 debug model, its KDA layers on TorchTitan's builder) passes 4 of 4 at the stack's top, on one GB300.
# TODO: drop the subclass once vLLM passes the draft and accepted counts to any builder that
# asks for them. Both of vLLM's runners pass them only to vLLM's own builder classes: V1 in
# GPUModelRunner._build_attention_metadata, V2 in MambaHybridAttnMetadata.get_extra_attn_kwargs
# (vLLM main). No vLLM issue filed yet.There was a problem hiding this comment.
Reply from claude: Still open, on purpose. The PR's author reviews each vLLM feature request on his own fork before he files it on vllm-project/vllm. This request is drafted but not open yet. The TODO gets its link once it is open.
|
Reply from claude: Both questions, answered from vLLM 9f03b510c (our pin).
|
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 1m 52s —— View job Needs discussion -- the subclass TODO still has no vLLM issue link, the cited vLLM behavior still needs someone to check it at the pin, and there's a new question about out-of-bounds gathers in "align" mode: review posted. |
There was a problem hiding this comment.
Verdict: Needs discussion -- one must-fix is still open: the vLLM workaround's TODO has no upstream issue link. The PR is also only correct if vLLM's kernels and runner behave as the author's citations say, and someone who can read vLLM at the pin has to confirm that. I couldn't (vLLM isn't available here).
What this PR does. When MTP drafts are on, the GDN/KDA metadata builder takes a new path, _build_with_drafts. On the CPU it splits rows into verify-kernel rows (rows with drafts, and 1-token rows after context) and prefill-kernel rows. It writes both into buffers sized for the largest step, and gives each kernel the null slot 0 on rows it doesn't own. Tensor shapes then don't depend on the mix of rows, so one packed or uniform FULL graph can replay any step. The runner now tells the builders whether a uniform graph replays and whether a dummy run is uniform, and hands them its accepted-count buffer. Steps without drafts run the same code as on main. Nothing turns drafts on until #6287.
Example (k = 3, capacity 5, 24 padded tokens, S = 20):
rows verify 4 | verify 4 | 1-token resume | prefill 13 | pad
spec_query_start_loc [0, 4, 8, 9, 20, 20, 20] verify kernels; rows 3+ get null slots
prefill_query_start_loc [0, 9, 9, 9, 22, 24, 24] rows 0-2 merged into one null interval
before: vLLM's GDN builder builds tensors sized by the row mix -> no FULL graph for this step
after: fixed buffers -> the packed FULL graph replays it
Since the last review (869745f)
- Resolved: the null-slot question. The author cites both verify kernels returning on slot 0 before any per-token load or store (
causal_conv1d.py:834-839,fused_sigmoid_gating.py:103-115at 9f03b510c). Unverified by me. - Resolved: the V2 runner question. V2 also gates the counts by builder class (
mamba_hybrid.py:54-66), so moving to V2 doesn't remove the subclass. The TODO now says so. - Resolved (should-consider):
worker.py:233, the namedforce_uniform_decodeparameter. #6402 sets this value on real steps, so the override has to pass its own value to vLLM. The author also cites vLLM's default beingNone.
Must fix
- Still open:
linear_attention.py:401-- the TODO for the subclass workaround says "No vLLM issue filed yet." CLAUDE.md principle 3 asks a workaround for an upstream gap to link an upstream issue. File the drafted vLLM feature request and put its URL in the TODO before merging. This is a mechanical edit.
Questions
linear_attention.py:423-426(and:579-582): the gather reads block-table columnsstate_block_column .. state_block_column + kfor every row, prefill rows included, beforetorch.wheremasks them. In "align" mode, a row whose running-state column is among the lastkcolumns would gather out of bounds, which is a device-side assert. Themax_seq_lencheck at:201only bounds the running column. Does vLLM's mamba block table withnum_speculative_blocksalways leavekcolumns past the last running column in "align" mode (unverified)? If not, the smallest fix is to clamp the index atblock_table.shape[1] - 1, since non-verify rows are masked anyway. A verify row that hits the clamp would then be a real bug to raise on. Before this stack, "align" + drafts couldn't be reached, so no existing test covers it. The new test's tables (8 columns, last running column 4) don't reach the edge.
Details
- Commits since the last full review: 869745f (the TODO text only) and a merge of
main. The merge doesn't touch the PR's logic (gh pr diffagainstmainis otherwise unchanged). - Overlap search: "linear_attention", "GDN builder". Found the rest of this stack (#6433, #6402, #6403, #6287, #6364, #6365), #5956 (V2 runner; the description says it rebases on this stack), #6415 (SGLang, a separate generator), and #6294 (
models/common/attention/linear_attention.py, not the RL builder). No conflicting rewrite. - Re-checked at HEAD: PACKED buffer sizes (
num_rows = capacity + 1againstquery_start_loc'scapacity + 2), dummy-run staging (all rows go to the prefill kernels with dummy slots and accepted counts of 1), the uniform-graph raise (a uniform descriptor needs every row at 1 + k tokens, so a step whose drafts were all trimmed can't get there), and D2H syncs (classification uses*_cpuand numpy; H2D goes throughasync_tensor_h2d). - KDA engines now run vLLM's GDN
__init__as well. The author reportsTestBitwiseParityKimiK3DebugVarlen4/4 at the stack top.
Not verified: the vLLM source lines cited in the replies (kernel early returns, the V1/V2 builder-class gates, num_accepted_tokens.gpu refreshed in batch order on every step), and the block-table width in "align" mode with drafts.
Before merge: fill in the human-written summary.
|
Reply from claude: I checked every vLLM behavior this PR cites at TorchTitan's pin, and each one holds. The "align" gather stays in bounds, so the code doesn't change. Two lines of the PR text were wrong, and I fixed them. The pin: 1. The cited behaviors
2. "align" gathers stay in bounds. vLLM gives each mamba block-table row k spare columns past the last running column:
vLLM's own GDN builder makes the same gather, on every row, in "align" mode (gdn_attn.py#L222-L227, which calls utils.py#L1155-L1180). A CPU check built a real You're right that the 3. PR text Still open: the TODO's upstream link (see its thread). |
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 1m 8s —— View job Needs discussion: the subclass TODO still needs its vLLM issue link, and someone who can read vLLM at the pin should check the author's citations. Review posted. |
There was a problem hiding this comment.
Verdict: Needs discussion -- no code has changed since my last review (869745f). The one must-fix left is the TODO's missing vLLM issue link. The vLLM behavior the PR depends on is now cited with permalinks at the pin, but I still can't read vLLM here, so someone who can should check those links before this merges.
What this PR does. When MTP drafts are on, the GDN/KDA metadata builder uses _build_with_drafts. On the CPU it sorts rows into verify-kernel rows and prefill-kernel rows. It writes both into buffers sized for the largest step, and gives the null slot 0 to every row a kernel doesn't own. One packed or uniform FULL graph can then replay any mix of rows. Steps without drafts run main's code.
Example (k = 3, capacity 5, 24 padded tokens, S = 20):
rows verify 4 | verify 4 | 1-token resume | prefill 13 | pad
spec_query_start_loc [0, 4, 8, 9, 20, 20, 20] verify kernels; rows 3+ get null slots
prefill_query_start_loc [0, 9, 9, 9, 22, 24, 24] rows 0-2 merged into one null interval
before: vLLM's GDN builder builds tensors sized by the row mix -> no FULL graph for this step
after: fixed buffers -> the packed FULL graph replays it
Since the last review
- No new commits. The author replied with vLLM permalinks at 9f03b510c and fixed two lines of the description.
- Answered: the question about out-of-bounds gathers in "align" mode. The argument holds, given the cited facts. A mamba block-table row is
cdiv(max_model_len, block_size) + kcolumns wide, and a GDN layer'snum_speculative_blocksis k. The scheduler keeps every request withinmax_model_len, and the runner caps its dummy runs there too (worker.py:191-197). So the last column read is(max_model_len - 1) // block_size + k, the row's last column. The arithmetic checks out. The vLLM facts it rests on are unverified by me.
Must fix
- Still open:
linear_attention.py:88-91. The TODO for the subclass workaround says "No vLLM issue filed yet." It's a mechanical edit: link the issue once it's filed. Filing it later isn't an argument against the finding, so it stays a must-fix until the link is in the TODO.
Should consider
linear_attention.py:201-211(existing code, not in the diff): themax_seq_lencheck bounds only the running column, while the new draft gather reads k more columns. With drafts, checking against(block_table_tensor.shape[1] - self.num_spec) * block_sizewould guard the bound the gather actually relies on. A wrong layout would then raise a clearValueErrorinstead of a device-side assert. This is a one-line change.
Details
- I tried to read vLLM at 9f03b510c to check the citations. The vLLM package isn't installed in this job, and reading other repos isn't permitted here.
- The overlap search from the last review still applies: the rest of this stack (#6433, #6402, #6403, #6287, #6364, #6365) and #5956 (V2 runner, which rebases on this stack). No conflicting rewrite.
Not verified: the vLLM lines the author cited. These are the kernels' early return on slot 0, the V1/V2 builder-class gates, num_accepted_tokens being refreshed in batch order on every step, and the mamba block-table width in "align" mode.
Before merge: fill in the human-written summary.
|
Superseded by #6473 |
Summary (human-written)
Agent details
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: TorchTitan's GDN metadata builder can now stage steps that carry MTP draft tokens. It writes them into fixed buffers, so FULL CUDA graphs can replay any mix of rows. No caller sets drafts yet: the GDN layer PR (3) runs these rows, and the MTP PR (4) turns drafts on. Steps without drafts run main's code.
Problem
kMTP drafts, each decode step verifies 1 + k tokens per request ("verify rows"). A step can also hold 1-token rows and prefill rows.Solution
TorchTitanLinearAttentionMetadataBuildersubclasses vLLM'sGDNAttentionMetadataBuilder. vLLM's V1 runner passes draft and accepted counts only to known builder classes (GPUModelRunner._build_attention_metadata)._build_with_draftsstages each step with drafts in buffers sized for the largest step. A kernel gets the null slot (0) on rows it doesn't own, so no tensor size depends on the mix.update_block_tablereuses one GDN group's metadata for the next, drafts included.TorchTitanGPUModelRunner.calculate_reorder_batch_thresholdraises if they disagree.num_accepted_tokensbuffer, which vLLM refreshes on every step. vLLM passes them to builders only on steps with drafts.VLLMEngine.add_requestsrejects prompts under 2 tokens with drafts (this builder's raise is the backstop), and drafts at generator DP > 1 raise at engine start.buildruns main's code.__init__now starts with vLLM's GDN builder__init__(the new parent) in every GDN and KDA engine: 8 small buffers (the largest holds up tomax_num_seqs * (1 + k)rows of1 + kint32 slots) and a GDN prefill-backend lookup that readshf_text_config.Results
You may ask: why a subclass of vLLM's builder? vLLM's V1 runner hands draft counts only to known builder classes. The TODO at the class says to drop it once vLLM passes the counts to any builder that asks. vLLM's V2 runner gates them by class too.
NOTE:
test_gdn_graph_metadata.pyruns in CI's RL 1 GPU job (A10G) when the PR has theciflow/rllabel. That job was green at the old head of "[rl] MTP on GDN models: FULL CUDA graphs for steps with prefill" (tagciflow/rl/6403), which had these cases. There they skip only without CUDA or the model path, which the workflow sets, so they most likely ran. Its log was not read. Not run there at this head yet. The newtest_draft_builder_rejects_rows_it_cannot_stageonly regroups three raises those cases already checked.Test plan