Skip to content

[rl] remove duplicated transpose around VllmAttention in generator - #2709

Merged
wwwjn merged 7 commits into
mainfrom
rl-attention-transpose
Mar 30, 2026
Merged

wwwjn merged 7 commits into
mainfrom
rl-attention-transpose

Conversation

@wwwjn

@wwwjn wwwjn commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

As titled

  • Previous Standard GQAttention + VLLMInnerAttention flow: (bs,seq,heads,dim) → transpose → (bs,heads,seq,dim) → VLLMInnerAttention transpose back → clone → (bs*seq,heads,dim) → vllm_attn → reshape → transpose → contiguous (4 layout ops)
Screenshot 2026-03-30 at 1 53 29 PM
  • New VLLMGQAttention subclass: (bs,seq,heads,dim) → reshape → (bs*seq,heads,dim) (1 zero-copy op)→ vllm_attn → view → (bs,seq,hidden)
Screenshot 2026-03-30 at 1 14 26 PM

From Claude's summary of trace before vs. after:

  • aten::transpose: 28,090 → 15,486 (-12,604 calls), the redundant transpose round-trip is gone
  • aten::view: 54,508 → 51,707 (-2,801 calls), fewer intermediate views

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 24, 2026
layer.attention.inner_attention = vllm_attn

# Swap GQAttention to VLLMGQAttention for zero-copy forward path
layer.attention.__class__ = VLLMGQAttention

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.

changing __class__ sounds suspicious -- is it replacing attention with a newly created module?

@wwwjn
wwwjn requested review from fegin and wconstab as code owners March 25, 2026 19:54
@wwwjn
wwwjn force-pushed the rl-attention-transpose branch from 2876e15 to 769efb0 Compare March 26, 2026 02:35

@tianyu-l tianyu-l 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.

please address remaining comments

Comment thread torchtitan/models/common/attention.py Resolved
wwwjn added 6 commits March 30, 2026 08:25
Replace post-build class swapping (replace_with_vllm_attention) with
config-time VLLMGQAttention.Config so the model builds with vLLM-compatible
attention directly.

- VLLMGQAttention gets a proper Config subclass of GQAttention.Config
  that creates VLLMInnerAttention during __init__ using vLLM's runtime
  config for tp_degree
- vllm_wrapper replaces GQAttention.Config with VLLMGQAttention.Config
  before model.build(), removing the post-hoc __class__ swap
- Remove replace_with_vllm_attention function
- Remove duplicated __call__ from VLLMInnerAttention, relying on
  LocalMapAttention's base __call__ with relaxed Shard dim assertion
- LocalMapAttention now accepts any Shard placement (not just Shard(1))
  with a guard that all placements use the same shard dim
- Use attn_backend="varlen" instead of "vllm" to correctly describe the
  attention mechanism (vLLM is KV cache management, not a backend)
- Remove hacky __post_init__ override and dataclasses.replace workaround
  that bypassed parent config validation
- Move _layer_counter from VLLMGQAttention to VLLMInnerAttention where
  it's actually needed for vLLM prefix generation
- Remove layer_name parameter, use counter-based layer_id directly
- Add TODO noting counter breaks with pipeline parallelism
@wwwjn
wwwjn force-pushed the rl-attention-transpose branch from a518908 to d5c6e99 Compare March 30, 2026 15:29
@wwwjn
wwwjn merged commit 52ebfc6 into main Mar 30, 2026
23 of 32 checks passed
@tianyu-l
tianyu-l deleted the rl-attention-transpose branch March 30, 2026 22:08
acisseJZhong pushed a commit that referenced this pull request Mar 31, 2026
…2709)

As titled


- Previous Standard GQAttention + VLLMInnerAttention flow:
(bs,seq,heads,dim) → transpose → (bs,heads,seq,dim) → VLLMInnerAttention
transpose back → clone → (bs*seq,heads,dim) → vllm_attn → reshape →
transpose → contiguous (4 layout ops)
<img width="1480" height="363" alt="Screenshot 2026-03-30 at 1 53 29 PM"
src="https://github.com/user-attachments/assets/c5566510-48e2-4f68-bb3f-456ba17b001b"
/>



- New VLLMGQAttention subclass: (bs,seq,heads,dim) → reshape →
(bs*seq,heads,dim) (1 zero-copy op)→ vllm_attn → view → (bs,seq,hidden)
<img width="1480" height="363" alt="Screenshot 2026-03-30 at 1 14 26 PM"
src="https://github.com/user-attachments/assets/97ad664b-e965-4f85-b4ae-7da6d02572c0"
/>


From Claude's summary of trace before vs. after:
- aten::transpose: 28,090 → 15,486 (-12,604 calls), the redundant
transpose round-trip is gone
- aten::view: 54,508 → 51,707 (-2,801 calls), fewer intermediate views
tianyu-l added a commit that referenced this pull request Mar 31, 2026
for several things:
- Save a pair of transpose for varlen attention, by moving the transpose
for sdpa / flex inside their own module. I had to change
`_ContextParallel(seq_dim=2, ...)` to `seq_dim=1` -> I also had to
change flux model impl to adapt.
- The above change follows
#2709 which is only for the
generator. With this PR I'm able to remove `VLLMGQAttention` module
added in that PR.
- Make `inner_attention` a python config, instead of string
`attn_backend` which is now used in qwen3 `model_registry` as a
convenient way to switch config.
- Thanks to that, we can support FlexAttention with configurable
options, including `block_size` and `kernel_options` to enable Flash
attention backend #2171. Added
a config `qwen3_debugmodel_flex_flash` to the registry.
- Move `annotate_flex_attention_for_regional_inductor` from general
attention file to `graph_trainer` where it's used.
- remove outdated artificial restrictions on CP / CP + TP
- some naming improvement

NOTE:
- GraphTrainer tests fail on main as well
- Model test on Llama4+PP+compile fails on main as well
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
…ytorch#2709)

As titled


- Previous Standard GQAttention + VLLMInnerAttention flow:
(bs,seq,heads,dim) → transpose → (bs,heads,seq,dim) → VLLMInnerAttention
transpose back → clone → (bs*seq,heads,dim) → vllm_attn → reshape →
transpose → contiguous (4 layout ops)
<img width="1480" height="363" alt="Screenshot 2026-03-30 at 1 53 29 PM"
src="https://github.com/user-attachments/assets/c5566510-48e2-4f68-bb3f-456ba17b001b"
/>



- New VLLMGQAttention subclass: (bs,seq,heads,dim) → reshape →
(bs*seq,heads,dim) (1 zero-copy op)→ vllm_attn → view → (bs,seq,hidden)
<img width="1480" height="363" alt="Screenshot 2026-03-30 at 1 14 26 PM"
src="https://github.com/user-attachments/assets/97ad664b-e965-4f85-b4ae-7da6d02572c0"
/>


From Claude's summary of trace before vs. after:
- aten::transpose: 28,090 → 15,486 (-12,604 calls), the redundant
transpose round-trip is gone
- aten::view: 54,508 → 51,707 (-2,801 calls), fewer intermediate views
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
for several things:
- Save a pair of transpose for varlen attention, by moving the transpose
for sdpa / flex inside their own module. I had to change
`_ContextParallel(seq_dim=2, ...)` to `seq_dim=1` -> I also had to
change flux model impl to adapt.
- The above change follows
pytorch#2709 which is only for the
generator. With this PR I'm able to remove `VLLMGQAttention` module
added in that PR.
- Make `inner_attention` a python config, instead of string
`attn_backend` which is now used in qwen3 `model_registry` as a
convenient way to switch config.
- Thanks to that, we can support FlexAttention with configurable
options, including `block_size` and `kernel_options` to enable Flash
attention backend pytorch#2171. Added
a config `qwen3_debugmodel_flex_flash` to the registry.
- Move `annotate_flex_attention_for_regional_inductor` from general
attention file to `graph_trainer` where it's used.
- remove outdated artificial restrictions on CP / CP + TP
- some naming improvement

NOTE:
- GraphTrainer tests fail on main as well
- Model test on Llama4+PP+compile fails on main as well
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
…ytorch#2709)

As titled


- Previous Standard GQAttention + VLLMInnerAttention flow:
(bs,seq,heads,dim) → transpose → (bs,heads,seq,dim) → VLLMInnerAttention
transpose back → clone → (bs*seq,heads,dim) → vllm_attn → reshape →
transpose → contiguous (4 layout ops)
<img width="1480" height="363" alt="Screenshot 2026-03-30 at 1 53 29 PM"
src="https://github.com/user-attachments/assets/c5566510-48e2-4f68-bb3f-456ba17b001b"
/>



- New VLLMGQAttention subclass: (bs,seq,heads,dim) → reshape →
(bs*seq,heads,dim) (1 zero-copy op)→ vllm_attn → view → (bs,seq,hidden)
<img width="1480" height="363" alt="Screenshot 2026-03-30 at 1 14 26 PM"
src="https://github.com/user-attachments/assets/97ad664b-e965-4f85-b4ae-7da6d02572c0"
/>


From Claude's summary of trace before vs. after:
- aten::transpose: 28,090 → 15,486 (-12,604 calls), the redundant
transpose round-trip is gone
- aten::view: 54,508 → 51,707 (-2,801 calls), fewer intermediate views
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
for several things:
- Save a pair of transpose for varlen attention, by moving the transpose
for sdpa / flex inside their own module. I had to change
`_ContextParallel(seq_dim=2, ...)` to `seq_dim=1` -> I also had to
change flux model impl to adapt.
- The above change follows
pytorch#2709 which is only for the
generator. With this PR I'm able to remove `VLLMGQAttention` module
added in that PR.
- Make `inner_attention` a python config, instead of string
`attn_backend` which is now used in qwen3 `model_registry` as a
convenient way to switch config.
- Thanks to that, we can support FlexAttention with configurable
options, including `block_size` and `kernel_options` to enable Flash
attention backend pytorch#2171. Added
a config `qwen3_debugmodel_flex_flash` to the registry.
- Move `annotate_flex_attention_for_regional_inductor` from general
attention file to `graph_trainer` where it's used.
- remove outdated artificial restrictions on CP / CP + TP
- some naming improvement

NOTE:
- GraphTrainer tests fail on main as well
- Model test on Llama4+PP+compile fails on main as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu 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