Skip flex_attention on pre-Ampere GPUs (T4, V100)#4302
Conversation
flex_attention Triton kernels require sm80+ (Ampere). On older GPUs the dense Python fallback runs instead, but sdpa_dense_backward has a dtype mismatch under fp16 autocast -- the matmul at line 904 of torch/_higher_order_ops/flex_attention.py does softmax_scores.to(query.dtype) @ grad_out where query.dtype is Half and grad_out is Float, producing "expected scalar type Float but found Half". This affected Ministral-3B/8B on T4 GPUs (issue #4295) and potentially any model using flex_attention on pre-Ampere hardware. Fix: check torch.cuda.get_device_capability() >= (8, 0) before enabling flex_attention. Falls back to sdpa on older GPUs.
for more information, see https://pre-commit.ci
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical issue where Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly adds a check to skip flex_attention on older GPUs. My feedback includes a small refactoring to improve the code's conciseness and readability by removing a redundant import and simplifying the conditional logic.
| import torch | ||
|
|
||
| if torch.cuda.is_available(): | ||
| major, _ = torch.cuda.get_device_capability() | ||
| if major < 8: | ||
| return None | ||
| else: |
There was a problem hiding this comment.
This block can be made more concise and readable. The import torch is redundant since it's already imported at the top of the file, and the conditional logic can be simplified by checking for the negative case first and returning early. This flattens the code structure.
| import torch | |
| if torch.cuda.is_available(): | |
| major, _ = torch.cuda.get_device_capability() | |
| if major < 8: | |
| return None | |
| else: | |
| # Check for CUDA availability and compute capability. | |
| # Return early if not supported to avoid nested ifs. | |
| if not torch.cuda.is_available(): | |
| return None | |
| major, _ = torch.cuda.get_device_capability() | |
| if major < 8: | |
| return None |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 45feffde6f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| import torch | ||
|
|
||
| if torch.cuda.is_available(): | ||
| major, _ = torch.cuda.get_device_capability() |
There was a problem hiding this comment.
Evaluate compute capability across all visible GPUs
This check only reads torch.cuda.get_device_capability() for the current CUDA device, but prefer_flex_attn_if_supported sets one global attention implementation for the whole model. In mixed-GPU runs (for example, device_map="auto" with both Ampere and pre-Ampere cards), if the current device is sm80+ and another shard lands on sm70/sm75, flex_attention will still be enabled and the same backward dtype mismatch this patch aims to avoid can still occur on the older shard.
Useful? React with 👍 / 👎.
|
We should find a way to combine #4210 |
|
Any updates on this? |
Summary
flex_attentionon GPUs with compute capability < 8.0 (pre-Ampere: T4/sm75, V100/sm70)sdpa_dense_backwardwhich has a dtype mismatch under fp16 autocast (Half @ Floatmatmul at line 904 offlex_attention.py)sdpainstead, which works correctly on all GPU architecturesRoot cause
prefer_flex_attn_if_supported()only checkstorch >= 2.5.0and the model's_supports_flex_attnflag, but not GPU compute capability. The flex_attention Triton kernels require sm80+.Reproduction
Ministral-3B on T4 with
transformers >= 5.0:Crashes during backward with:
at
torch/_higher_order_ops/flex_attention.py:904insdpa_dense_backward.Test plan
prefer_flex_attn_if_supportedreturnsNoneon T4 (sm75)sdpaattention instead offlex_attentionFixes #4295