Skip to content

fix(studio): evict a torch-mismatched xFormers after the Linux repair - #11559

Closed
InfoSage05 wants to merge 4 commits into
unslothai:mainfrom
InfoSage05:fix/11545-repair-xformers-torch-mismatch
Closed

InfoSage05 wants to merge 4 commits into
unslothai:mainfrom
InfoSage05:fix/11545-repair-xformers-torch-mismatch

Conversation

@InfoSage05

Copy link
Copy Markdown
Contributor

Summary

On Linux, Unsloth Desktop's Repair Installation re-selects torchao when the repair moves torch (fixed for #10493), but never re-checked xFormers. xFormers links its compiled extension against one exact (torch, CUDA) pair; beside any other pair its ops go mute or raise instead of failing cleanly at import, and diffusers/peft import xFormers eagerly — so a resident xFormers built for a torch that Repair Installation just replaced surfaces as an unrelated-looking aten::_flash_attention_forward schema crash the first time an image/diffusion model loads, well after the repair itself reported success.

This PR reuses _evict_xformers_built_for_another_torch() — the exact eviction primitive already proven on the Windows-on-ARM64 wheelhouse path (#10282) — in the Linux final-repair step, right alongside the existing torchao re-selection call.

Root Cause

The root cause is studio/install_python_stack.py's Linux "step 13" final torch repair (install_python_stack(), around the if _torch_after_repair and _torch_after_repair != _torch_before_repair: block) re-selecting torchao for the newly-repaired torch but never re-checking xFormers, because _resync_torch_coupled_packages() — the function that already does both — is only reached from the Windows torch-flavor-repair path (_ensure_expected_torch_flavor), and #10493's fix added a standalone torchao-only call to Linux's step 13 without adding the equivalent for xFormers.

This matches issue #11545 exactly: repairing a GTX 960M (Maxwell, compute capability 5.0) moved torch from a CPU-only build to 2.6.0+cu124 (correctly landed via the existing GPU-architecture-aware torch selection), but left a resident xFormers 0.0.35 built for torch 2.10.0+cu128, Python 3.10.19 completely untouched — not even from this venv's own install history — which then crashed diffusers/peft's import chain the first time an image/diffusion model was loaded.

Changes

  • studio/install_python_stack.py:
    • Linux step 13: after _install_torchao_for_torch(_torch_after_repair), call _evict_xformers_built_for_another_torch() under the same "torch actually moved during this repair" guard.
    • Generalized _evict_xformers_built_for_another_torch()'s note text, which hardcoded "windows on arm:" even though the function itself has no platform dependency — it's now reused from a general Linux path too.
  • tests/studio/install/test_cuda_repair.py: two new tests following this file's own established inspect.getsource(...) wiring-verification idiom (see TestTheWindowsXpuTritonSwapReachesADirectRun for precedent) — one confirms the eviction call is wired into step 13's torch-moved guard in the right order relative to the torchao call, the other confirms the reused eviction function's note text no longer claims "windows on arm".

No new elaborate mocking harness was built for the deeply-nested procedural install_python_stack() entry point itself — the eviction logic (_evict_xformers_built_for_another_torch, _resident_xformers_build_torch) is already extensively covered by existing tests in test_windows_arm64_wheelhouse_tags.py and test_cuda_repair.py's TestThePackagesTiedToTheTorchReleaseAreResettled, which I did not touch.

Investigation

Verified the actual "Repair Installation" desktop-button code path end-to-end rather than assuming: frontend desktop-repair-control.tsx → useTauriRepairController().repairInstall() → Tauri command start_managed_repair (force_installer: true) → install::run_install_for_repair → resolve_install_script picks install.sh on Linux → install.sh runs unsloth studio update, which (per this file's own Windows-path docstring: "unsloth studio update runs setup.ps1 and this script [install_python_stack.py]") is what actually executes install_python_stack.py's repair steps on Linux.

Testing

python -m py_compile studio/install_python_stack.py
python -m pytest tests/studio/install/test_cuda_repair.py -q
  • Compile check: clean.
  • test_cuda_repair.py: 268/268 passed (both new tests included).
  • Also ran the full surrounding suite (test_cross_platform_parity.py, test_e2e_no_torch_sandbox.py, test_flash_attn_install_python_stack.py, test_install_python_stack.py, test_no_torch_filtering.py, test_torchcodec_torch_compat.py, test_windows_arm64_requirement_markers.py, test_windows_arm64_wheelhouse_tags.py, test_windows_no_torch_setup.py): 1667 passed, 9 failed, 7 skipped. All 9 failures reproduce identically against the unmodified upstream baseline (confirmed via git stash) — they come from a broken WSL2 sandbox in this specific CI/dev environment (Failed to attach disk '...ext4.vhdx' to WSL2), unrelated to this change.

Checklist

  • Tests added (targeted, following the file's own wiring-verification idiom — not a new mocking harness)
  • All tests directly exercising this change pass (268/268 in test_cuda_repair.py)
  • Pre-existing, environment-only failures in adjacent files confirmed unrelated via baseline comparison
  • Linked to the issue (Fixes #11545)

Fixes #11545

The final torch repair (step 13) already re-selects torchao when it
moves torch to fix the venv's flavor (unslothai#10493), but left xFormers
alone. xFormers links its extension against one exact (torch, CUDA)
pair; beside any other its ops go mute or raise instead of failing at
import, and diffusers/peft import xFormers eagerly -- so a resident
xFormers built for a torch that is no longer installed (often not
even from this venv's own install history) surfaces as an unrelated-
looking aten::_flash_attention_forward schema crash the first time an
image/diffusion model loads, well after Repair Installation reported
success.

Reuse _evict_xformers_built_for_another_torch(), already proven on
the Windows-on-ARM64 wheelhouse path (unslothai#10282), instead of adding a
new check: it already does exactly this eviction, just was never
wired into the general Linux path. Generalized its note text, which
hardcoded "windows on arm" even though the function itself has no
platform dependency.

Fixes unslothai#11545
@InfoSage05
InfoSage05 marked this pull request as ready for review September 22, 2026 16:29
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the Linux repair in install_python_stack.py re-selects torchao when torch moves but never checks xFormers, and calling the existing _evict_xformers_built_for_another_torch there looks right for #11545. Will get this reviewed.

@oobabooga

Copy link
Copy Markdown
Member

Thanks for this. The xFormers requirement mismatch reported in #11545 is covered by #11847, which is now merged and checks even when torch stays unchanged during the final repair.

Closing as superseded, but your diagnosis of the missing xFormers check was right.

@oobabooga oobabooga closed this Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] LLM CUDA works, image model import fails after Repair.

3 participants