Skip to content

Commit 4e61e94

Browse files
committed
enable varlen full cudagraph
1 parent 9ccbb57 commit 4e61e94

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

torchtitan/experiments/rl/models/attention.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
from torchtitan.tools.logging import warn_once
2020
from torchtitan.tools.utils import has_cuda_capability
2121
from vllm.model_executor.layers.attention import Attention
22-
from vllm.v1.attention.backend import AttentionType
22+
from vllm.v1.attention.backend import AttentionCGSupport, AttentionType
2323
from vllm.v1.attention.backends.flash_attn import (
2424
FlashAttentionBackend,
2525
FlashAttentionImpl,
2626
FlashAttentionMetadata,
27+
FlashAttentionMetadataBuilder,
2728
)
2829
from vllm.v1.attention.backends.registry import AttentionBackendEnum, register_backend
2930

@@ -53,6 +54,13 @@ def get_name():
5354
def get_impl_cls():
5455
return PyTorchVarlenAttentionImpl
5556

57+
@staticmethod
58+
def get_builder_cls():
59+
class PyTorchVarlenAttentionMetadataBuilder(FlashAttentionMetadataBuilder):
60+
_cudagraph_support = AttentionCGSupport.ALWAYS
61+
62+
return PyTorchVarlenAttentionMetadataBuilder
63+
5664

5765
class PyTorchVarlenAttentionImpl(FlashAttentionImpl):
5866
"""
@@ -146,7 +154,9 @@ def forward(
146154

147155
cu_seqlens_q = attn_metadata.query_start_loc
148156
seqused_k = attn_metadata.seq_lens
149-
max_seqlen_q = attn_metadata.max_query_len
157+
# Pin max_seqlen_q to the total token count for safe cudagraph capture and use
158+
# the linearize FA3 combine kernel to preserve performance.
159+
max_seqlen_q = num_actual_tokens
150160
max_seqlen_k = attn_metadata.max_seq_len
151161
block_table = attn_metadata.block_table
152162

0 commit comments

Comments
 (0)