Skip to content
Merged
Changes from 1 commit
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
15 changes: 7 additions & 8 deletions stream_attention/core/flashattention_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,14 @@ def forward(
if _use_flash_sdpa() and q.device.type == "cuda":
try:
# Prefer the newer torch.nn.attention API when available
sdpa_ctx = torch.nn.attention.sdpa_kernel(SDPBackend.FLASH_ATTENTION)
sdpa_ctx = torch.nn.attention.sdpa_kernel(
SDPBackend.FLASH_ATTENTION
)
except (AttributeError, TypeError):
try:
# Older PyTorch versions expose the context in torch.backends
sdpa_ctx = torch.backends.cuda.sdp_kernel(
enable_math=False, enable_flash=True, enable_mem_efficient=False
)
except (AttributeError, RuntimeError):
sdpa_ctx = nullcontext()
# Fallback for older PyTorch releases
sdpa_ctx = torch.backends.cuda.sdp_kernel(
Copy link
Contributor

Choose a reason for hiding this comment

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

Calling torch.backends.cuda.sdp_kernel without its own error handling can raise and crash on environments where the kernel is absent, removing the prior graceful fallback.

Prompt for AI agents
Address the following comment on stream_attention/core/flashattention_v3.py at line 88:

<comment>Calling torch.backends.cuda.sdp_kernel without its own error handling can raise and crash on environments where the kernel is absent, removing the prior graceful fallback.</comment>

<file context>
@@ -80,15 +80,14 @@ def forward(
         if _use_flash_sdpa() and q.device.type == &quot;cuda&quot;:
             try:
                 # Prefer the newer torch.nn.attention API when available
-                sdpa_ctx = torch.nn.attention.sdpa_kernel(SDPBackend.FLASH_ATTENTION)
+                sdpa_ctx = torch.nn.attention.sdpa_kernel(
+                    SDPBackend.FLASH_ATTENTION
+                )
             except (AttributeError, TypeError):
-                try:
</file context>

enable_math=False, enable_flash=True, enable_mem_efficient=False
)

with sdpa_ctx:
out = F.scaled_dot_product_attention(
Expand Down