Skip to content

Say when xformers is installed but cannot load, and how to fix it - #13410

Merged
danielhanchen merged 4 commits into
mainfrom
fix-4381-xformers-broken-message
Oct 12, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
fix-4381-xformers-broken-message

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

When xformers is installed but its CUDA extension cannot load, Unsloth falls back to PyTorch attention without saying why. xformers' own warning is silenced right before the import, and Unsloth's explanation only prints under UNSLOTH_ENABLE_LOGGING, so all the user gets is FA [Xformers = None. FA2 = False] in the banner.

That is what #4381 reports: torch 2.10.0+cu130 on Windows next to xformers 0.0.35 from PyPI. PyPI's xformers wheels are cu128 builds (cpp_lib.json: torch 2.10.0+cu128, cuda 1208) and their _C links the CUDA 12 runtime (libcudart.so.12 / cudart64_12.dll). A cu130 torch only ships the CUDA 13 runtime, so torch.ops.load_library fails.

Now, on CUDA and outside UNSLOTH_ENABLE_LOGGING, Unsloth prints one message that names the torch the wheel was built for and the fix:

Unsloth: xformers is installed but cannot load with torch 2.13.0+cu130 (it was built for 2.10.0+cu128), so PyTorch attention is used instead.
To install a matching xformers, run `unsloth install-kernels xformers`, or `pip uninstall xformers` to hide this. UNSLOTH_ENABLE_LOGGING=1 shows the full error.

unsloth install-kernels xformers (shipped in 2026.10.3) already replaces a non-loading xformers with the wheel from the matching CUDA index. For the reporter's setup (win_amd64, torch 2.10.0+cu130) it picks https://download.pytorch.org/whl/cu130/xformers-0.0.34-cp39-abi3-win_amd64.whl (HTTP 200).

Nothing changes when xformers loads, when it is not installed, under UNSLOTH_ENABLE_LOGGING, or on ROCm / XPU / CPU.

Fixes #4381

Verification

To reproduce the reporter's loader failure on Linux, I copied the PyPI xformers 0.0.35 package and repointed its _C.so dependency from libcudart.so.12 to a missing name (patchelf --replace-needed). That is what a cu130-only machine sees. Then I loaded hf-internal-testing/tiny-random-LlamaForCausalLM with FastLanguageModel:

main this PR
broken xformers no message, banner Xformers = None message above, banner Xformers = None
working xformers banner Xformers = 0.0.35, no message same, no message
  • The cu130 xformers 0.0.35 wheel links libcudart.so.13 and loads with torch 2.13.0+cu130. Its cpp_lib.json says torch 2.10.0+cu130, cuda 1300.
  • New tests/utils/test_xformers_broken_message.py: it raises xformers' own xFormersInvalidLibException from _register_extensions in a subprocess, so the parsed text comes from the installed package. It also checks that a working xformers prints nothing. 2 passed on this branch. On main, test_broken_xformers_is_reported fails and the quiet-path test passes.

@danielhanchen danielhanchen self-assigned this Oct 12, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-12T11:34:46.200303Z 50c15a1 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-12T12:01:36.047450Z 2d83951 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

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: 50c15a12ec

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

Comment thread unsloth/models/_utils.py
Comment on lines +3852 to +3856
"Unsloth: xformers is installed but cannot load with torch "
f"{torch_version}{f' (it was built for {_built_for.group(1)})' if _built_for else ''}, "
"so PyTorch attention is used instead.\n"
"To install a matching xformers, run `unsloth install-kernels xformers`, "
"or `pip uninstall xformers` to hide this. UNSLOTH_ENABLE_LOGGING=1 shows the full error."

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 Preserve the Blackwell-specific repair instructions

On SM 10/11/12 GPUs with xformers <= 0.0.32.post2 (for example, an RTX 50-series GPU with torch 2.8), this handler receives the deliberate compatibility exception raised above rather than a loader failure. The generic recommendation is ineffective: studio/backend/utils/wheel_utils.py maps torch 2.8 back to xformers 0.0.32.post2, and install_kernel returns “already installed and loads” because _register_extensions() succeeds, leaving xformers disabled. Preserve the original “upgrade to >= 0.0.33 or build from source” guidance for this exception, and use this message only for actual extension-load failures.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in ab5f698: Unsloth's own version guards (Blackwell FA3 dispatch, old torch) now print their own fix; the install-kernels hint is only for extension load failures. Covered by test_version_guard_keeps_its_own_fix.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 12, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

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: ab5f698e62

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

Comment thread unsloth/models/_utils.py
"Unsloth: xformers is installed but cannot load with torch "
f"{torch_version}{f' (it was built for {_built_for.group(1)})' if _built_for else ''}, "
"so PyTorch attention is used instead.\n"
"To install a matching xformers, run `unsloth install-kernels xformers`, "

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 Avoid recommending an installer with no matching wheel

On CUDA platforms without a published prebuilt wheel, such as Linux aarch64/Jetson, this advertised fix cannot install xformers. The CLI help explicitly says install-kernels is prebuilt-only and skips kernels when no wheel matches; studio/backend/utils/wheel_utils.py:303-307 only maps linux_x86_64 and win_amd64, while studio/backend/utils/kernel_install.py:349-351 returns without installing when resolution fails. Users on these supported CUDA systems therefore remain on the fallback after following the message; gate this recommendation by platform/wheel availability or provide source-build guidance.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not changing this: on a platform with no prebuilt wheel, install-kernels prints that none matches this torch and stays on the fallback, and the same message offers pip uninstall xformers. The common broken case (PyPI wheel beside a cu130 torch on win_amd64 / linux_x86_64) is covered.



def test_broken_xformers_is_reported():
out = _run(_BREAK_XFORMERS + _IMPORT_UNSLOTH)

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 Make the generic tests bypass the Blackwell version guard

On an SM 10/11/12 host with xformers <= 0.0.32.post2, this subprocess reaches the existing compatibility guard before the injected _register_extensions failure, so it prints the FA3-dispatch message instead and the generic-message assertions fail. test_working_xformers_prints_nothing fails similarly because the same guard deliberately sets u.xformers to None. Force a non-Blackwell capability or otherwise neutralize the version guard in the two generic tests, leaving only test_version_guard_keeps_its_own_fix to exercise that path.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 2d83951: the generic tests pin xformers.version past the Blackwell guard, so only test_version_guard_keeps_its_own_fix exercises it.

out = _run(_BREAK_XFORMERS + _IMPORT_UNSLOTH)
assert "XFORMERS_IS_NONE True" in out
assert "xformers is installed but cannot load with torch" in out
assert "(it was built for " in out

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 Allow xformers installations without build metadata

On a CUDA host with a pure-Python or no-extension xformers installation, xformers._cpp_lib._build_metadata is None; the injected xFormersInvalidLibException then reports only that xformers was built for a different PyTorch/Python version and contains no PyTorch ... with CUDA ... text for the production regex to extract. The module-level marker still enables this test, so this unconditional assertion fails even though the diagnostic correctly omits the unavailable build version. Either require non-None build metadata in the skip condition or make the parsed-version assertion conditional.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 2d83951: the built-for assertion now follows whether xformers has build metadata.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 2d83951484

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

@danielhanchen

Copy link
Copy Markdown
Member Author

pre-commit.ci run

@danielhanchen
danielhanchen merged commit ff64a01 into main Oct 12, 2026
@danielhanchen
danielhanchen deleted the fix-4381-xformers-broken-message branch October 12, 2026 14:10
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.

[Bug] No xformers and Flash-attention under Windows?

1 participant