Skip to content

fix(rocm): open the AOTriton SDPA gate before torch loads on the library path - #8922

Closed
yzxcj797 wants to merge 4 commits into
unslothai:mainfrom
yzxcj797:fix/8819-aotriton-gate
Closed

yzxcj797 wants to merge 4 commits into
unslothai:mainfrom
yzxcj797:fix/8819-aotriton-gate

Conversation

@yzxcj797

Copy link
Copy Markdown
Contributor

Fixes the library-path half of #8819.

What

Set TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL=1 at the top of unsloth/__init__.py, before anything below can import torch, via os.environ.setdefault so a pre-existing value — including an explicit "0" opt-out — always wins. Guarded on torch not already being imported, which is the only window where the variable can still take effect (torch reads it once while loading its C++ extension).

Why

Per the issue's matrix, only install.sh's ROCm-on-WSL drop-in opens the gate (confirmed: grep -r TORCH_ROCM_AOTRITON hits only install.sh and scripts/install_rocm_wsl_strixhalo.sh). A user who runs pip install unsloth on bare-metal Linux or Windows AMD gets the O(N^2) MATH attention path: SDPA declines, MATH materialises the full B x H x N x N score matrix, and finetuning memory grows with the square of the context — the issue measures 387.2 ms / 12.14 GiB vs 17.3 ms / 0.18 GiB for flash at N=9408, with 1.221e-04 (fp16 epsilon) accuracy difference.

This is the library-path counterpart of what #8323 does inside the Studio backend process. Mirrors the file's existing pattern for USE_TF/USE_FLAX (environment variables that must land before their reader's import). Harmless on non-ROCm hosts, which never read the variable.

Validation

  • python -c "import ast; ast.parse(...)" on the patched file
  • Behavioural check: python -c "import unsloth, os; print(os.environ['TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL'])" prints 1; with TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL=0 python -c ... prints 0 (opt-out honoured); on ROCm hosts torch.backends.cuda.sdp_kernel selection then admits flash/mem_efficient — no ROCm hardware in this sandbox, per the issue's own one-liner for verification:
    python -c "import torch,os;d=os.path.join(os.path.dirname(torch.__file__),'lib');print([f for f in os.listdir(d) if 'aotriton' in f.lower()] or 'NO AOTRITON IN THIS WHEEL')"

AI-assisted development disclosure

ZCode assisted with implementing the fix from the issue's proposed remedy. I reviewed the final diff, ran the validation above, and take responsibility for the submitted change.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the gate is only opened by the WSL drop-in in install.sh, so a pip install leaves SDPA on MATH, and setting it at the top of unsloth/init.py before torch loads is the right place. Could you take a look at the Security audit pip scan-packages hf-stack check, which is green on main but red here.

@danielhanchen

Copy link
Copy Markdown
Member

@LeoBorcherding could you take a look at this one?

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 478e95024f

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

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 9909a0f66b

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

@oobabooga

Copy link
Copy Markdown
Member

Pushed 0bbf30c, which removes the if "torch" not in sys.modules: guard.

Torch does not read TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL while loading its C++ extension. It reads it into a function-local static const bool inside check_flash_attention_hardware_support and check_mem_efficient_hardware_support (aten/src/ATen/native/transformers/cuda/sdp_utils.cpp, identical in 2.8, 2.9, 2.10, 2.11 and main), so the value is fixed at the first ROCm SDPA capability probe, not at import. import torch followed by import unsloth is therefore still early enough, but the guard skipped the write in exactly that case. At 9909a0f, with the variable initially unset: a clean import unsloth gave "1", and import torch; import unsloth left it unset.

That ordering is not hypothetical. studio/backend/core/training/trainer.py imports torch near the top and Unsloth about twenty lines later, so the Studio training process was one of the callers the guard excluded, along with any script or notebook that touches torch first.

The write is now unconditional. setdefault still leaves an existing value alone, so TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL=0 remains a working opt-out, and c10::utils::check_env reads "0" as false and "1" as true. tests/test_rocm_aotriton_gate.py covers both import orderings and the opt-out; its two ordering cases fail against the guarded version and pass now.

The rest of the change stands as written. One thing worth editing: the description still says torch reads the variable while loading its C++ extension, which is where the guard came from.

On the red checks at 9909a0f, none of which come from this branch: Repo tests (CPU) and workflow-trigger lint both fail on tests/studio/test_workflow_guards_run_unfiltered.py (assert not ['test_pip_cache_naming.py']), and (Python 3.13) fails on test_control_markup_neutralize_7066.py. All three reproduce on plain main. The Windows Unsloth API & Auth Tests row was a job that hung in the test step until its 30 minute timeout; the same workflow is green on other PRs on the same base, and this branch passed it at 478e950.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 0bbf30c884

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

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: c99258a719

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

@oobabooga

Copy link
Copy Markdown
Member

CI has settled on c99258a. Four red checks, all of which reproduce on plain main and none of which come from this branch:

  • Repo tests (CPU) and workflow-trigger lint: tests/studio/test_workflow_guards_run_unfiltered.py, assert not ['test_pip_cache_naming.py']
  • (Python 3.13): tests/test_control_markup_neutralize_7066.py, AssertionError: llama_cpp.py
  • Core (HF=latest + TRL=latest): the two mxfp4 drift tests in tests/test_temporary_patches_exhaustive.py, which track the newest transformers release and fail against 5.16.1

The Windows Unsloth API & Auth Tests row that was red before is green now. That job had hung in its test step until the 30 minute timeout.

Repo tests (CPU) moved from 14254 to 14258 passed and 350 to 353 skipped, which is the new test file: the four source-level cases run everywhere, and the three that import Unsloth in a subprocess skip where it is not importable.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed main still only opens the gate in install.sh's WSL drop-in, so a pip install leaves ROCm SDPA on MATH, and the unconditional setdefault at the top of unsloth/init.py is the right place now that the sys.modules guard is gone. The Security audit pip scan-packages hf-stack check is still red here and green on main, could you take a look at that one.

@danielhanchen

Copy link
Copy Markdown
Member

Heads up that #8821 opens the same AOTriton SDPA gate before torch loads and it landed independently. I asked there which of the two to keep; I will take one rather than merge both, so please hold off on further work here until that is settled.

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Closing in favour of #8821, which has the same setdefault line in unsloth/__init__.py and also covers a Studio backend started with uvicorn main:app and removes the installers' host-wide export. The read-timing correction from this thread (torch reads the variable at the first SDPA probe, not at extension load, so the sys.modules guard had to go) fixed both PRs and is credited in #8821. Thanks for the fix.

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.

4 participants