Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions torchtitan/experiments/rl/models/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
from torchtitan.tools.logging import warn_once
from torchtitan.tools.utils import has_cuda_capability
from vllm.model_executor.layers.attention import Attention
from vllm.v1.attention.backend import AttentionType
from vllm.v1.attention.backend import AttentionCGSupport, AttentionType
from vllm.v1.attention.backends.flash_attn import (
FlashAttentionBackend,
FlashAttentionImpl,
FlashAttentionMetadata,
FlashAttentionMetadataBuilder,
)
from vllm.v1.attention.backends.registry import AttentionBackendEnum, register_backend

Expand Down Expand Up @@ -53,6 +54,13 @@ def get_name():
def get_impl_cls():
return PyTorchVarlenAttentionImpl

@staticmethod
def get_builder_cls():
class PyTorchVarlenAttentionMetadataBuilder(FlashAttentionMetadataBuilder):
_cudagraph_support = AttentionCGSupport.ALWAYS

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.

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.

yes


return PyTorchVarlenAttentionMetadataBuilder


class PyTorchVarlenAttentionImpl(FlashAttentionImpl):
"""
Expand Down Expand Up @@ -146,7 +154,9 @@ def forward(

cu_seqlens_q = attn_metadata.query_start_loc
seqused_k = attn_metadata.seq_lens
max_seqlen_q = attn_metadata.max_query_len
# Pin max_seqlen_q to the total token count for safe cudagraph capture and use
# the linearize FA3 combine kernel to preserve performance.

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.

Suggested change
# the linearize FA3 combine kernel to preserve performance.
# the linearized FA3 combine kernel to preserve performance.

max_seqlen_q = num_actual_tokens
max_seqlen_k = attn_metadata.max_seq_len
block_table = attn_metadata.block_table

Expand Down
Loading