Skip to content

Add check to disable xformers on newer GPUs - #4342

Merged
danielhanchen merged 1 commit into
unslothai:mainfrom
pluesclues:patch_sm12
Mar 17, 2026
Merged

danielhanchen merged 1 commit into
unslothai:mainfrom
pluesclues:patch_sm12

Conversation

@pluesclues

Copy link
Copy Markdown
Contributor

Disable xformers for GPUs with compute capability >= 12 to ensure compatibility with newer hardware.

Disable xformers for GPUs with compute capability >= 12 to ensure compatibility with newer hardware.
@pluesclues
pluesclues requested a review from mmathew23 as a code owner March 17, 2026 04:53
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 enhances the stability and compatibility of the attention dispatch mechanism by introducing a safeguard for newer GPU architectures. It prevents potential issues with xformers on devices with compute capability 12 or greater, ensuring that the system gracefully defaults to SDPA for optimal performance and reliability on cutting-edge hardware.

Highlights

  • Xformers Compatibility: Implemented a check to disable xformers for GPUs with compute capability 12 or higher, ensuring compatibility with newer hardware.
  • Fallback Mechanism: When xformers is disabled due to high compute capability, the system will automatically fall back to Scaled Dot-Product Attention (SDPA).
Changelog
  • unsloth/utils/attention_dispatch.py
    • Added logic to check GPU compute capability and conditionally disable xformers.
Activity
  • No specific activity has been recorded for this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request adds a check to disable xformers on GPUs with a compute capability of 12.0 or higher, falling back to SDPA. This is a good forward-looking change to ensure compatibility with future hardware. My only feedback is to update the accompanying comment to more accurately reflect the code's logic and avoid confusion about supported hardware versions, as the current comment is inconsistent with the implementation.

Comment on lines +37 to +38
# xformers kernels (FA3, FA2, cutlass) only support compute capability <= 9.0.
# Disable xformers on newer GPUs (e.g. RTX 5070 Ti / sm_120) and fall back to SDPA.

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.

medium

The comment here is a bit confusing and seems to contradict the code. The first line states that xformers only supports compute capability <= 9.0, but the code only disables it for >= 12.0, leaving a gap for versions 10.x and 11.x where it would remain enabled. Additionally, the example RTX 5070 Ti refers to the Blackwell architecture, which has a compute capability of 10.0, so the check _cc[0] >= 12 would not trigger for it.

To avoid confusion, I suggest updating the comment to align with the code's logic and the PR's description, which is to disable xformers for compute capability >= 12.0.

Suggested change
# xformers kernels (FA3, FA2, cutlass) only support compute capability <= 9.0.
# Disable xformers on newer GPUs (e.g. RTX 5070 Ti / sm_120) and fall back to SDPA.
# Xformers is not yet supported on compute capability >= 12.0.
# Disable xformers on such GPUs and fall back to SDPA.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dbf82583a7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

# Disable xformers on newer GPUs (e.g. RTX 5070 Ti / sm_120) and fall back to SDPA.
if HAS_XFORMERS and torch.cuda.is_available():
_cc = torch.cuda.get_device_capability()
if _cc[0] >= 12:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Disable xformers on all unsupported SM versions

The new guard only disables xformers when major >= 12, but the comment directly above says xformers kernels are only supported through compute capability 9.0; this leaves SM 10.x/11.x GPUs on the xformers path instead of the intended SDPA fallback. On those newer non-9.x devices, backend selection can still choose xformers and trigger runtime kernel-dispatch failures rather than the compatibility fallback this commit is trying to enforce.

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member

This PR addresses a real problem -- RTX 50-series users (SM 12.0) hitting xformers crashes like "requires device with capability <= (9, 0) but your GPU has capability (12, 0) (too new)" (see #4200, #3870, #3018). However there are a few issues with the current implementation:

1. Already handled in _utils.py

unsloth/models/_utils.py lines 981-993 already has a version-aware guard:

if (f"{major_version}.{minor_version}" in ("10.0", "11.0", "12.0")) and (
    Version(xformers_version) <= Version("0.0.32.post2")
):
    raise NotImplementedError(...)

This runs at import time before attention_dispatch.py is loaded. For the broken case (old xformers + new GPU), xformers is already set to None, so HAS_XFORMERS is already False by the time this new check would run.

2. Blanket disable breaks working xformers >= 0.0.33

xformers 0.0.33.post2 works on SM 12.0 via FA2 -- flash.FwOp has CUDA_MAXIMUM_COMPUTE_CAPABILITY = None (no upper bound), and the wheel is built with 12.0a/12.1a targets. The unconditional _cc[0] >= 12 check would silently downgrade these users to SDPA, losing performance for no reason.

3. Comment/code mismatch

The comment says "xformers kernels (FA3, FA2, cutlass) only support compute capability <= 9.0" but only FA3 and cutlass cap at SM 9.0 -- FA2 has no upper cap. The threshold >= 12 also leaves SM 10.x (B200/B300) uncovered, contradicting the comment.

4. Minor issues

  • get_device_capability() with no args only checks torch.cuda.current_device() (GPU 0), which is unsafe in heterogeneous multi-GPU setups
  • _cc leaks into module namespace (should del _cc)
  • llama.py has its own independent HAS_XFORMERS that is not patched

Suggestion

Since _utils.py already covers the broken case, this check is not needed. If an extra safeguard is desired, a runtime try/except in run_attention() that catches the specific NotImplementedError ("too new") and retries with SDPA would be more robust and forward-compatible than an import-time capability gate.

@danielhanchen
danielhanchen merged commit f5e1f52 into unslothai:main Mar 17, 2026
shibizhao pushed a commit to shibizhao/unsloth-npu that referenced this pull request Apr 7, 2026
Disable xformers for GPUs with compute capability >= 12 to ensure compatibility with newer hardware.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants