Skip to content

Probe xformers support on sm_120 instead of disabling it by version - #6828

Merged
oobabooga merged 12 commits into
unslothai:mainfrom
oobabooga:xformers-blackwell-probe
Jul 14, 2026
Merged

oobabooga merged 12 commits into
unslothai:mainfrom
oobabooga:xformers-blackwell-probe

Conversation

@oobabooga

@oobabooga oobabooga commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

Unsloth blanket-disables xformers on any GPU with compute capability >= 12 (RTX 50-series, sm_120) and falls back to SDPA. On packed training that wastes a large amount of attention memory: the SDPA fallback materializes a dense block-diagonal mask while xformers does not.

Closes #4631

Problem

The guard added in #4342 disables xformers by GPU version alone:

if _cc[0] >= 12:
    HAS_XFORMERS = False

The comment claims xformers kernels "only support compute capability <= 9.0". That is too strong. The shipped wheels have no native sm_120 kernel, but memory_efficient_attention still runs there when its cutlass kernel JIT-compiles forward from the 8.0+PTX build target. On builds where it runs, disabling it throws away a real memory saving. The load-time guard in models/_utils.py (broken FA3 dispatch on xformers <= 0.0.32.post2) already blocks the genuinely broken versions, so this version check is redundant.

Fix

Replace the version guess with a runtime probe, gated to sm_120+ so nothing changes for any other GPU. At sm_120+ it runs one tiny memory_efficient_attention forward and keeps xformers only if that succeeds. Otherwise it falls back to SDPA exactly as before. The worst case on a broken sm_120 build is unchanged behavior, never a new crash.

Below sm_120 the probe never runs, so there is no import-time cost or behavior change for Ada, Hopper, B200, and so on.

Hardenings from the review rounds:

  • The probe picks its dtype from SUPPORTS_BFLOAT16 (bf16 on sm_80+, else fp16), so pre-Ampere GPUs with a working fp16 xformers are not misreported as broken.
  • torch.cuda.synchronize() after the probe call, so an async kernel failure is caught here instead of surfacing as a false success.
  • The probe is skipped when FlashAttention is installed: flash always wins backend selection and nothing downgrades flash to xformers, so the probe would be a wasted kernel launch there.

One companion fix rides along. On sm_100+ xformers' fp32-capable cutlass op is capability-rejected and only its fp16/bf16 flash-2 op exists, so the existing #1013 fp32 DoRA downcast now also applies to the xformers backend there. Without it, fp32 Q/K/V raises No operator found for memory_efficient_attention_forward on those GPUs.

Verification

Verified on the exact hardware class the report is about: an RTX 5090 (sm_120, Ubuntu 24.04, driver 580) with the environment Unsloth ships (torch 2.10.0, xformers 0.0.35 from the pinned cu128 wheel, no flash-attn).

The gate itself, main vs this branch, on the same box:

main:      HAS_XFORMERS = False, backend sdpa      (blanket disable)
this PR:   probe returns True in ~1 ms, backend xformers
           import time unchanged (6.9 s vs 7.7 s on main, no JIT penalty)

Packed forward and backward through the real run_attention (8 GQA heads, head_dim 128, bf16):

4 x 4096 tokens:  xformers   324 MiB peak
                  SDPA       OOM on the 32 GiB card (math backend under the dense packed mask)
4 x 1024 tokens:  xformers    82 MiB peak
                  SDPA      1624 MiB peak    (94.9% saving)
                  max diff 1.6e-2, normal bf16 cross-kernel noise

So on sm_120 the fallback this PR removes is not just slower: it cannot run a packed 4 x 4096 batch at all at this head count.

The fp32 downcast was exercised live on the same GPU. With it, fp32 Q/K/V runs and matches the bf16 run exactly. With the flag forced off, the same call raises No operator found for memory_efficient_attention_forward.

Earlier measurement on a B200 (sm_100, same PTX forward-compat path) agrees: 539 MB vs 1242 MB peak, max diff 3.9e-3.

Added tests/utils/test_xformers_capability_gate.py: the decision logic across Ada, Hopper, B200, sm_120-works, and sm_120-broken, the probe dtype selection, the synchronize behavior, plus a real-GPU check that the probe shapes are valid so a malformed probe cannot silently disable xformers everywhere. Also added xformers cases to tests/utils/test_attention_dispatch_dora_dtype.py for the fp32 downcast. Both files pass on the 5090 (13 passed, 1 skipped: the real-GPU check skips on sm_120+, where False is a legitimate probe answer) and on an Ada/Ampere box (14 passed).

@oobabooga
oobabooga requested a review from danielhanchen as a code owner July 2, 2026 21:27

@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 replaces the hardcoded compute-capability check for disabling xformers on sm_120+ GPUs with a dynamic probe that verifies if the xformers kernel can actually run on the device. It also adds a regression test suite to validate this behavior. The review feedback points out that the probe uses torch.bfloat16 unconditionally, which will cause test failures on older GPUs (like Turing or Volta) that do not support bfloat16 but have a working xformers build. It is recommended to dynamically select the data type based on hardware support.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread unsloth/utils/attention_dispatch.py Resolved

@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: 888bb39cf1

ℹ️ 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".

Comment thread unsloth/utils/attention_dispatch.py Resolved
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Jul 8, 2026
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@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: 819a1d217f

ℹ️ 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".

Comment thread unsloth/utils/attention_dispatch.py Resolved
Comment thread unsloth/utils/attention_dispatch.py Resolved
Comment thread unsloth/utils/attention_dispatch.py Resolved
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@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: 819a1d217f

ℹ️ 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".

Comment thread unsloth/utils/attention_dispatch.py Resolved
Comment thread unsloth/utils/attention_dispatch.py Resolved
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@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: 7b1bb3136e

ℹ️ 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".

Comment thread unsloth/utils/attention_dispatch.py Resolved
Comment thread unsloth/utils/attention_dispatch.py Resolved
Comment thread unsloth/utils/attention_dispatch.py Resolved
Comment thread unsloth/utils/attention_dispatch.py Resolved
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 1c584112de

ℹ️ 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".

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@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: 677bea28e4

ℹ️ 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".

Comment thread unsloth/utils/attention_dispatch.py Resolved
Comment thread unsloth/utils/attention_dispatch.py Resolved
@oobabooga
oobabooga merged commit ed42702 into unslothai:main Jul 14, 2026
47 checks passed
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.

sm_120 arch forces SDPA attention, even with functional xformers

1 participant