Repository navigation
fix(amd): turn on PyTorch's fast ROCm attention kernels when Unsloth Studio/Desktop launches, so a 16 GB card stops asking for 66 GiB - #8323
Conversation
…ing for 66 GiB unslothai#8233 made the math-only fallback visible and drew the wrong conclusion from it. The card is not missing a fused kernel: torch has one and hides it behind TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL. With the gate closed every sub-quadratic backend declines, SDPA falls through to MATH, and peak VRAM grows with the SQUARE of the token count -- unslothai#8225, a 3.4 GB Q4_K_M asking a 16 GB card for one 66.54 GiB allocation. Measured on an RX 9060 XT (gfx1200, torch 2.11.0+rocm7.13.0), B=1 H=16 N=9408 D=64: math 387.2 ms peak 12.14 GiB flash 17.3 ms peak 0.18 GiB max|d| vs math = 1.221e-04 mem_efficient 24.6 ms peak 0.20 GiB max|d| vs math = 1.221e-04 67x the memory and 22x the time for a result that agrees to 1.221e-04, which is 2**-13, fp16 epsilon. The gate is not buying accuracy, it is only costing the kernel. Opened in run.py before any import that can pull torch, because torch reads the var once while loading its C++ extension and a later set is dead code. A pre-existing value always wins, including "0", which is the opt-out for anyone who hits an AOTriton bug. Set unconditionally rather than only on ROCm: knowing the build means importing torch, and by then the read has happened. Non-ROCm torch never looks at a TORCH_ROCM_* var. Not routed through select_attention_backend. This does not choose a backend, it changes which backends exist, so auto still returns None and the speed_mode=off bit-identical guarantee is untouched. unslothai#8233's warning is unchanged and now fires only where the card really is math-only.
|
@codex review |
|
To use Codex here, create a Codex account and connect to github. |
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Confirmed studio/backend/run.py never opened TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL before torch loads, so a Studio launch outside install.sh stays on the quadratic math path, and setting it early with the "0" opt-out preserved looks right. Could you take a look at the three Core jobs and the pip scan-packages hf-stack check, which are green on main but red here? |
studio/ still ships on the 3.9 floor, where `env: dict | None = None` is evaluated at def time and raises. tests/test_python39_compatibility.py ratchets on the count, and this file took it from 35 to 36, which is what turned the three Core jobs and Repo tests (CPU) red. Before: 1 failed, 'studio\backend\utils\rocm_attention.py' named as the sole addition. After: 8 passed in test_python39_compatibility.py, and the 6 tests in test_rocm_attention.py still pass.
|
Merged main ( The six red checks at The Core failures at the Aug 14 head (
Run locally on the merged head, and on
The 65 are the same suites on both trees (release_desktop integrity and notarization, which need a Linux or mac host), so they are the box, not the change. The Backend CI Repo tests step and the Core legs need a full unsloth_zoo install and were not run here. One hardware row added, and the body narrowed to match. On the Radeon AI PRO R9700 (gfx1201, Windows 11, ROCm 7.1, the same torch 2.11.0+rocm7.13.0 build the gfx1200 rows used), @codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f766d4121f
ℹ️ 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".
…torch latches it uvicorn main:app never imports run.py, so main.py now makes the same stdlib-only call above its route and hardware imports, with a test pinning the order. The docstrings and the run.py comment no longer claim torch reads the variable at extension load: it is a function-local static latched at the first SDPA capability check. The 1.221e-04 agreement is one fp16 ulp at the output magnitude, not fp16 epsilon.
The stub-order guard treats every utils import before the stub installers as one that can reach torch. utils.rocm_attention imports only os and seeds an env var torch reads, the same shape as utils.cpu_threads, so it joins the allowlist next to it.
|
Pushed
The comment in
Local, Windows 11, Python 3.12, on the merged head: @codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Pushed |
|
Confirmed main only exports TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL from install.sh and the WSL script, so a Studio launch from elsewhere still lands on the quadratic math path, and the run.py plus main.py placement covers both entry points. This does opt our processes into kernels AMD still labels experimental on those arches, so could you confirm the three Core legs, which are green on main but red here, before I review? |
|
Closing this in favour of #8821, which sets the same variable with the same What each PR reaches
Your own description here says #8821 reaches Studio through Evidence that #8821 does the jobMeasured on the self-hosted gfx1151 Strix Halo runner, Linux/ROCm, torch 2.11.0+rocm7.13.0: at the base both flash and mem-efficient SDPA raise Nothing here was wrong. The helper and its tests are a clean shape, and if you would like the |
Fixes #8225.
On some AMD cards PyTorch ships fast attention kernels (AOTriton) but leaves them switched off until an environment variable says otherwise. Nothing in Studio set it, so attention ran on the slow fallback kernel, whose memory grows with the square of the token count. That is how a 3.4 GB video model on a 16 GB card ends up asking for 66 GiB. This PR sets the variable when Studio launches. Same output to within fp16 rounding, up to 67x less memory at attention.
Cards it changes: RX 9060 XT / 9060 (gfx1200), RX 7800 XT / 7700 XT (gfx1101), the RX 7600 family (gfx1102), Radeon 780M/760M (gfx1103) and the RDNA 3.5 laptop chips (gfx1150/1152/1153). On these, video stops asking for tens of GB at attention, high-res image generation needs less VRAM, chat on safetensors models takes long prompts about 22x faster, and training fits longer sequences.
Cards it leaves alone: RX 9070 XT / 9070 / R9700 (gfx1201), the RX 7900 series (gfx1100), Strix Halo (gfx1151, since AOTriton 0.12), MI200/MI300, and anything NVIDIA or Apple. The fast kernels are already on there. GGUF models go through llama.cpp, not PyTorch, so nothing changes for them either.
How it works
The variable is
TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL. PyTorch only reads it after it has already confirmed AOTriton has a kernel for this GPU and that the GPU is on AOTriton's "experimental" list (sdp_utils.cpp). It reads it once, the first time attention runs, and keeps the answer for the life of the process.run.pysets it before torch is imported, so it is always in time, andmain.pymakes the same call for a backend started directly withuvicorn main:app.That experimental list is compiled into the AOTriton binary inside the torch wheel (
v3src/util.cc) and changes between releases (0.12 dropped gfx1151, for one), so this PR keeps no card list of its own. PyTorch does the filtering; the variable just says yes where PyTorch asks. It is set withsetdefault, so a value you already have wins, and"0"is the opt-out if you ever hit an AOTriton bug. It does not pick a backend either:select_attention_backendstill returns None onauto,speed_mode=offis still bit-for-bit, and #8233's "no fused kernel" warning correctly stops firing once the kernels are available.#8821 sets the same variable for
import unsloth, and instudio/backend/main.py, whichrun.pyimports on every launch anduvicorn main:appruns directly, so it reaches Studio too. This PR's write inrun.pyis just the earliest one. #8821 also removes the host-wide export the WSL installers used to write. Every write issetdefault, so whichever runs first wins.Measured
RX 9060 XT (gfx1200), Windows 11, torch 2.11.0+rocm7.13.0, at
4451f6a. Nothing above the variable has changed in the merges since.flash and mem_efficient agree with math to 1.221e-04, which is fp16 rounding. 66.54 GiB is exactly what math needs for about 33k tokens at D=128, so the allocation in the issue is the attention score matrix, not weights or the VAE.
Seven tests in
studio/backend/tests/test_rocm_attention.py: the variable turns on when unset, any preset value including"0"survives, calling it twice is harmless, and therun.pyandmain.pyordering is pinned so nobody can move either call below a torch import later.Test matrix
CI can't show this one. The org's hosted runners have no AMD GPU, the AMD runner we do have is a Strix Halo (gfx1151) which is not affected by this pr.
4451f6a: math only with the variable off; flash and mem_efficient on, matching math to fp16 roundingaotriton_report.py, 2026-09-03: 8 of 8 forced cases pass with the variable at"0"and at"1", same peak VRAMSolid: the gfx1200 numbers, and the off-list prediction holding on the R9700. Biggest gap: no end-to-end generation, and no Linux run on any card the PR changes.