Skip to content

[rl] GDN layer: run steps with drafts from TorchTitan's builder - #6433

Closed
felipemello1 wants to merge 3 commits into
felipemello1/mtp-uniform-verifyfrom
felipemello1/gdn-layer-drafts
Closed

felipemello1 wants to merge 3 commits into
felipemello1/mtp-uniform-verifyfrom
felipemello1/gdn-layer-drafts

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Agent details

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. this PR
  4. "[rl] MTP speculative decoding in the vLLM generator"
  5. "[rl] MTP verify steps: run verify rows on FA4's decode kernel"
  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: VLLMInnerGDN runs steps with MTP drafts from the GDN builder's metadata (PR 1). Every kernel runs every row, and rows it doesn't own get the null slot, so one graph per token count replays any mix of rows. The layer still refuses drafts in __init__: the MTP PR (4) lifts that refusal when the engine owns speculative_config.

The GDN layer runs verify rows on vLLM's verify GDN kernel, which keeps the state after each draft in its own slot so rejected drafts leave no trace, and one 32-token packed CUDA graph replays 4 row mixes bitwise equal to eager.

VLLMInnerGDN._forward_speculative, by the builder's execution path:
  VERIFY  every row verifies (or is a 1-token row after context): vLLM's verify conv + GDN kernels
  PACKED  any other step: Attention Gym's prefill conv + chunk kernels on every token,
          then vLLM's verify kernels on the first S = (1 + k) x max_num_seqs tokens;
          each token keeps the output of the kernels that own its row

Problem

  1. With k drafts, a verify row reads the state after its last accepted token. It must write one state per draft position, so a rejected draft leaves the accepted state intact.
  2. Attention Gym's GDN kernels keep one state per request, so main's layer refuses drafts.
  3. vLLM has verify kernels that keep one state per draft position (causal_conv1d_update, FLA's fused_sigmoid_gating_delta_rule_update).

Solution

  1. _forward_speculative runs the VERIFY or PACKED kernels above. No tensor size or Python branch depends on the mix of rows. Rows no sequence owns get zeros.
  2. _verify_gdn runs vLLM's verify GDN kernel over every row, from the builder's spec_* fields.
  3. Prefill rows run Attention Gym's paged conv and chunk kernels, as steps without drafts do.
  4. Unchanged: without drafts, _forward runs main's code. __init__ keeps main's refusal of speculative decoding.

Results

Numerics: verify rows run vLLM's GDN kernels; MTP off and the trainer run Attention Gym's.
GPU, one GB300, test_gdn_graph_metadata.py: 38 passed (27 from PR 1 + 11 here). Qwen3.5 GDN layer shapes, k = 3,
random weights:
  test_verify_rows_match_single_token_decodes        j = 1, 2, 4 accepted: outputs and kept states match j
                                                     single-token decodes without drafts
  test_speculative_step_matches_step_without_drafts  6 row mixes (decode, verify, prefill, trimmed drafts,
                                                     prefill with context): each row's output, SSM and conv
                                                     states match the same request in a step without drafts
  test_draft_steps_replay_one_packed_graph           one packed graph, 4 row mixes: bitwise vs eager
                                                     (output, conv state, SSM state)
  test_draft_verify_graph_follows_the_dummy_key      the uniform verify graph: bitwise vs eager; padding rows zero

NOTE:

  • No consumer in the tree until the MTP PR (4). The stack lands together.
  • test_gdn_graph_metadata.py runs in CI's RL 1 GPU job (A10G) when the PR has the ciflow/rl label. That job was green at the old head of "[rl] MTP on GDN models: FULL CUDA graphs for steps with prefill" (tag ciflow/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.

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

  • Try vLLM's fused CUDA verify op, which also applies the gated norm: 3.27 ms per Qwen3.8-27B verify step in stock vLLM, vs 4.0 ms for this Triton kernel alone (GB300). Untested with this layer's layouts.

Test plan

pytest tests/rl/unit_tests/cpu
pytest tests/rl/unit_tests/gpu/test_gdn_graph_metadata.py tests/rl/unit_tests/gpu/test_gdn_cudagraph.py   # 1 GPU (GB300)
pre-commit run --files <changed files>
RL CPU suite         776 passed, 0 failed, 8 skipped on the pinned vLLM (9f03b510c), main merged in (PR 2: the same).
GPU                  test_gdn_graph_metadata.py: 38 passed
                     test_gdn_cudagraph.py: 4 passed (398 s), with "[models] GDN/KDA chunk kernels: always fused"
                     applied (GDN prefill on the fused kernels, which CI's A10G also takes)
pre-commit           all hooks pass (pyrefly and lychee skipped)

VLLMInnerGDN runs the builder's draft metadata: VERIFY steps on vLLM's verify kernels,
PACKED steps with verify rows first and prefill rows after, both inside FULL graphs.
The layer still refuses speculative decoding at construction; the MTP PR lifts that
refusal when the engine owns speculative_config. GPU tests compare verify rows with
single-token decodes and a step with drafts with one without, eager and replayed.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 28, 2026
@felipemello1
felipemello1 added this pull request to stack #6434 November 28, 2026 17:50
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@claude

claude Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 5m 5s —— View job


Verdict: Approve, with two optional cleanups. See the review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: Approve -- the layer uses vLLM's own GDN verify kernels on the builder's fixed buffers from #6432. Without drafts the path is unchanged, and I found no correctness problem. The remaining items are optional cleanups.

What this PR does. With num_speculative_tokens > 0, VLLMInnerGDN._forward sends each step to _forward_speculative.

  • VERIFY runs vLLM's causal_conv1d_update and FLA's fused_sigmoid_gating_delta_rule_update on every row. A verify row reads the state its last step accepted and writes one state per draft position.
  • PACKED first runs Attention Gym's paged conv and chunk kernels on every token. It then runs the verify kernels on the first S tokens and uses spec_token_mask to keep, for each token, the output of the kernel that owns its row.

__init__ still refuses drafts, so nothing in the tree reaches this code until #6287 removes that check.

Example. k = 3, max_num_seqs = 4 (so S <= 16). The step has rows [verify: 4 tokens, context 40, its last step accepted 2; prefill: 13 tokens].

before  __init__ raises; _forward asserted spec_sequence_masks is None
after   PACKED, 17 tokens, S = 16
        prefill conv + chunk kernels on all 17 tokens; the verify row's interval has the
          null slot, so tokens 0-3 get zeros
        verify conv + GDN on tokens 0-15: row 0 reads conv rows 1-3 and the SSM state in
          its 2nd slot, then writes 4 SSM states to its slots 1-4; the prefill tokens
          4-15 have the null slot
        output tokens 0-3 come from the verify kernels, tokens 4-16 from the chunk kernel

Should consider

  • torchtitan/rl/generator/vllm/attention/gdn.py:398-412: the VERIFY and PACKED branches repeat the same causal_conv1d_update call. One sequence can cover both (inline).
  • torchtitan/rl/generator/vllm/attention/gdn.py:68-69 (and lines 10-11): the class and module docstrings now say verify rows run vLLM's kernels. But __init__ (lines 92-95) still raises "does not support speculative decoding", and its comment gives a reason that is no longer true: "Attention Gym currently mutates one slot". Until #6287 lands, change that comment to say the check stays only until the engine wiring lands, e.g. # TODO: lifted by #6287.
Details
  • Gate: this enables MTP on GDN models. #6287 is the consumer and carries the numbers: 1.66-2.14x decode, and trainer-vs-generator k3 2.61e-4 with MTP-3 vs 2.65e-4 with MTP off. It also rejects debug.batch_invariant together with drafts, which this layer's speculative path doesn't honor. The layer reuses the kernels vLLM's native GDN runs instead of adding new ones. The SGLang counterpart (#6414) does the same with SGLang's verify kernels.
  • Overlap search: gh pr list --search "MTP speculative" found only this stack (#6432, #6402, #6287, #6364, #6403, #6365, #6366) and the SGLang stack (#6414). None of them rewrites this function.
  • In-place conv in VERIFY: mixed_qkv is not read after inner_attention (models/common/attention/gdn.py:675-698), so writing over it is safe in eager mode.
  • Tests: each new GPU case guards something the others don't cover: rejected drafts (j < 4), row mixes against a step without drafts, packed-graph replay, and the uniform graph with zeroed padding. They are small (16 heads x 128).

Not verified: vLLM's source isn't available here. Two contracts rest on the author's GB300 run (38 passed): causal_conv1d_update and the FLA verify kernel skip rows whose slot is the null slot 0, and causal_conv1d_update accepts out=. CI has not run at this head.

Before merge: fill in the human-written summary.

Comment on lines +398 to +412
num_verify_tokens = spec_token_mask.shape[0]
# Writes only the verify rows' tokens, where the prefill conv wrote zeros.
causal_conv1d_update(
mixed_qkv[:num_verify_tokens],
conv_state,
conv_weight,
None,
"silu",
conv_state_indices=spec_slots[:, 0],
num_accepted_tokens=metadata.num_accepted_tokens,
query_start_loc=metadata.spec_query_start_loc,
max_query_len=spec_slots.shape[1],
validate_data=False,
out=conv_output[:num_verify_tokens],
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should consider: this causal_conv1d_update call is the VERIFY branch's call (lines 373-384) again, with the same 9 arguments plus out=. The two branches differ only in what fills conv_output and output before the verify kernels run. In VERIFY, num_verify_tokens == num_tokens (the builder picks VERIFY only when num_tokens <= max_verify_tokens), so one sequence covers both:

num_verify_tokens = spec_token_mask.shape[0]
if metadata.execution_path is LinearAttentionExecutionPath.VERIFY:
    conv_output = mixed_qkv  # the conv update writes over its input
else:
    conv_output = paged_causal_conv1d(...).squeeze(0)
causal_conv1d_update(mixed_qkv[:num_verify_tokens], ..., out=conv_output[:num_verify_tokens])
output = (
    conv_output.new_zeros(num_tokens, self.local_num_v_heads, self.head_v_dim)
    if metadata.execution_path is LinearAttentionExecutionPath.VERIFY
    else self._forward_gdn(conv_output, a, b, ...)
)
verify_output = self._verify_gdn(conv_output[:num_verify_tokens], ...)
output[:num_verify_tokens] = torch.where(spec_token_mask[:, None, None], verify_output, output[:num_verify_tokens])

That removes about 15 lines and leaves one place that encodes the verify-kernel arguments. Before adopting it, check one thing (unverified, I couldn't read vLLM's source here): passing out= aliased to the input x must behave like the default in-place write.

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.

Reply from claude: Kept the two calls. VERIFY runs on every decode step with drafts. The shared sequence would cost it 2 more kernels per GDN layer: a zeros buffer for output, and a masked copy back into it.

# VERIFY today: one torch.where
output = self._verify_gdn(conv_output, a, b, A_log, dt_bias, metadata)
return torch.where(spec_token_mask[:, None, None], output, 0)

# VERIFY with the shared sequence: new_zeros, where, and a copy into output[:num_verify_tokens]
output = conv_output.new_zeros(num_tokens, self.local_num_v_heads, self.head_v_dim)
verify_output = self._verify_gdn(conv_output[:num_verify_tokens], ...)
output[:num_verify_tokens] = torch.where(spec_token_mask[:, None, None], verify_output, output[:num_verify_tokens])

@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: Fixed in cfaf638: the drafts refusal in __init__ now says why it stays. The conv cleanup is kept; the reason is in its thread.

# before
# vLLM speculative decoding retains one state per draft position and
# commits the accepted state; Attention Gym currently mutates one slot.
# after
# TODO: allow drafts once VLLMEngine sets them up (the MTP drafter and the runner's
# accepted counts); `_forward_speculative` already runs their steps.

@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@claude

claude Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 1m 7s —— View job


Verdict: Approve -- see the review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: Approve -- since my last full review the only change in this PR's files is the __init__ comment fix (cfaf638), and both earlier items are settled.

What this PR does. With num_speculative_tokens > 0, VLLMInnerGDN._forward sends each step to _forward_speculative. That function runs one of two paths:

  • VERIFY runs vLLM's verify conv and GDN kernels on every row.
  • PACKED runs Attention Gym's prefill conv and chunk kernels on every token. It then runs the verify kernels on the first S tokens and keeps, for each token, the output of the kernel that owns its row.

__init__ still refuses drafts, so nothing in the tree reaches this path until #6287.

Example. k = 3, max_num_seqs = 4, rows [verify: 4 tokens, context 40, its last step accepted 2; prefill: 13 tokens].

before  __init__ raises; _forward asserted spec_sequence_masks is None
after   PACKED, 17 tokens, S = 16: tokens 0-3 come from the verify kernels (they read slot 2
        and write slots 1-4); tokens 4-16 come from the chunk kernel

Earlier items

  • gdn.py:92-93 stale reason for the refusal: resolved. It is now a TODO that names what still has to happen (the engine wiring).
  • gdn.py:398-412 duplicated causal_conv1d_update call: the author's reason holds. Sharing the sequence would add a new_zeros and a masked slice copy per GDN layer to VERIFY, which runs on every decode step with drafts. Keeping the two calls costs about 12 lines and saves those kernels on the hot path. Not re-raised.
Details
  • Re-review scope: git diff f676d62 9101c96 over this PR's three files is empty. The merge only brings in base-branch changes (test_kv_offload.py). cfaf638 changes only the two comment lines.
  • Gate, overlap search, and test value are unchanged from the earlier review. The consumer and the numbers are in #6287. The layer reuses vLLM's own verify kernels instead of adding new ones.

Not verified: vLLM's source still isn't available here. Two kernel contracts rest on the author's GB300 run (38 passed): rows with null slot 0 are skipped, and causal_conv1d_update accepts out=. CI has not run at this head.

Before merge: fill in the human-written summary.

@liangel-02

Copy link
Copy Markdown
Contributor

Superseded by #6475

@liangel-02 liangel-02 closed this Dec 1, 2026
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