Repository navigation
Description
Summary
On a ROCm build, torch keeps its AOTriton flash and memory-efficient SDPA kernels behind
TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL, which it reads once, while loading its C++
extension. With the gate shut every sub-quadratic backend declines, SDPA falls through to MATH,
and MATH materialises the whole B x H x N x N score matrix. Peak memory then grows with the
square of the context.
Nothing on the library path opens that gate. A user who runs pip install unsloth and starts a
finetune on bare-metal Linux or Windows AMD gets the O(N²) attention path.
Why finetuning pays twice
Inference materialises the scores once per forward. Training needs them again for the backward
pass, so they are either held or recomputed per layer. For a 32-head model in fp16 the score
tensor alone, before the softmax output:
| context | score matrix |
|---|---|
| 2,048 | 0.27 GB |
| 4,096 | 1.07 GB |
| 8,192 | 4.3 GB |
| 16,384 | 17.2 GB |
On a 16 GB card that is the difference between a 4-8k context and a 32k one.
Where the gate is opened today, and where it is not
| Path | Gate |
|---|---|
ROCm-on-WSL via install.sh |
open |
| Studio backend process | open once #8323 merges |
bare-metal Linux via install.sh |
shut |
any Windows install (install.ps1) |
shut |
pip install unsloth + a script or notebook |
shut |
| containers / CI | shut |
install.sh sets it in two places, both inside _persist_rocm_wsl_dropin():
_persist_rocm_wsl_dropin() {
[ -e /opt/rocm/lib/librocdxg.so ] || [ -e /opt/rocm/lib64/librocdxg.so ] || return 0librocdxg.so is the ROCm-on-WSL shim, so the function returns immediately anywhere else. It is
called only from the WSL bootstrap (install.sh:4117, :4139). install.ps1 never sets the
variable: grep -c AOTRITON install.ps1 returns 0.
Why SDPA is the path that matters on AMD
unsloth/models/_utils.py:2176, the DEVICE_TYPE == "hip" branch, does try flash-attn. But the
prebuilt resolver is CUDA-only: studio/backend/utils/wheel_utils.py:147 direct_wheel_url()
returns None unless env["cuda_major"] is set, and the filename template is
+cu{major}torch{mm}. That is the No compatible flash-attn prebuilt wheel found line users see
(for example in #8322's install log). Unless someone builds flash-attn from source, AMD finetuning
runs on SDPA, and SDPA without the gate runs on MATH.
Measured
RX 9060 XT (gfx1200), torch 2.11.0+rocm7.13.0, B=1 H=16 N=9408 D=64, from #8225 / #8323:
| kernel | time | peak VRAM | max abs diff vs math |
|---|---|---|---|
| math | 387.2 ms | 12.14 GiB | reference |
| flash | 17.3 ms | 0.18 GiB | 1.221e-04 |
| mem_efficient | 24.6 ms | 0.20 GiB | 1.221e-04 |
1.221e-04 is 2**-13, fp16 epsilon. The gate is not buying accuracy, only costing the kernel.
Training-shaped before/after numbers (forward plus backward, several context lengths) to follow.
Open question: does AOTriton cover Windows?
Worth settling before anyone assumes the scope. AOTriton has historically been a Linux artifact,
and the ROCm-on-Windows wheels come from repo.amd.com/rocm/windows, a much newer pipeline. If
those wheels do not ship the aotriton library, then on Windows the variable opens onto nothing:
SDPA still declines to MATH and the fix helps only Linux and WSL.
One command answers it on any ROCm host:
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')"The measured table above names an RX 9060 XT on torch 2.11.0+rocm7.13.0, which is a Windows
build, so the Windows answer is probably yes. Confirming rather than assuming.
Proposed fix
Open the gate at the top of unsloth/__init__.py, before anything can import torch, honouring any
pre-existing value (including "0" as the opt-out). #8323 does the same thing inside the Studio
backend process; this is the library-path counterpart.
Related
- [Bug] Studio video: Wan2.2-TI2V-5B requests 71 GB on a 16 GB card: SDPA falls back to the math kernel on ROCm gfx1200 #8225 the user-visible form: a 3.4 GB Q4_K_M asking a 16 GB card for one 66.54 GiB allocation
- 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 opens the same gate in the Studio backend
Activity
LeoBorcherding commentedon Aug 14, 2026
Scope correction: which models this actually reaches
The issue above says "SDPA is the path" without qualifying it. That is the majority case, not the
whole picture, so pinning down the exceptions before anyone sizes the fix.
Why SDPA is the majority path. Two independent reasons, and
unsloth/kernels/contains noattention kernel at all (cross-entropy, RMSNorm, RoPE, SwiGLU, GEGLU, LoRA and MoE are custom
Triton; attention is delegated):
unsloth/models/llama.py:550and:559,plus
cohere.py:421,granite.py:409,falcon_h1.py:363.unsloth/models/llama.py:2449and:2459,attn_implementation = "sdpa".Where it does not apply.
gemma3,gemma3_text,shieldgemma2_FLEX_PREFERRED_MODELS,_utils.py:399)gpt_oss,deepseek_v4_SDPA_EXCLUDED_MODELS,_utils.py:400)A second quadratic path worth separating out. flex_attention is not automatically better.
_utils.py:458records that below the capability floor the flex HOP "runs its eagersdpa_densefallback", which materialises the scores just like MATH does.
_flex_attention_gpu_is_supported()deliberately leaves ROCm alone, so flex stays selectable on AMD, and #4109 sets
UNSLOTH_COMPILE_DISABLE=partialfor Gemma3 on RDNA to dodge a NaN.So Gemma3 on an AMD card plausibly gets O(N²) attention by a completely different route, one this
fix cannot touch. If that reproduces it deserves its own issue rather than being folded in here.
Nothing above changes the fix in the linked PR: the gate is still shut for every pip-installed AMD
user, and opening it is still correct for the models that reach SDPA. It narrows the claim from
"AMD finetuning" to "AMD finetuning on the models that resolve to SDPA", which is most of them but
not Gemma3 and not gpt-oss.
LeoBorcherding commentedon Aug 14, 2026
Windows answered: the wheel ships AOTriton, and the variable is the only thing holding it shut
RX 9060 XT (gfx1200), Windows,
torch 2.11.0+rocm7.13.0, hip7.13.99004,rocm-sdk-libraries-gfx120X-all 7.13.0.The wheel carries the kernels:
Torch's own flags claim the backends are on:
They are not. With the variable unset, every forced-backend call fails:
No available kernelNo available kernelNo available kernelNo available kernelNo available kernelNo available kernelNo available kernelNo available kernelNo available kernel16 of 16 fail unset, 16 of 16 pass set.
B=2 H=8 S=512 D=64.Torch names the variable itself in the warning it emits on the way past:
Accuracy against the math backend at that shape, fp16 causal: max abs diff
0.00195. Larger thanthe
1.221e-04measured atB=1 H=16 N=9408 D=64in #8225, and expected to be: shorter sequencesand a different reduction order move fp16 rounding around. Both are noise, neither is a kernel
disagreement.
Two things this pins down:
repo.amd.com/rocm/windowsshipped thelibrary at all.
flash_sdp_enabled()andmem_efficient_sdp_enabled()are not a usable probe on ROCm. Theyreport the user-facing toggle, not whether a kernel exists. Anything checking AMD attention
support has to attempt a call.
gfx1201 shares the
amd-gfx120ximage family so it should behave the same, but that is inferencefrom the directory name, not a measurement. Untested.
On "experimental"
The gating is AMD's, not a formality, and RDNA4 backward is the least-exercised path in it. Worth
stating plainly given this PR opens the gate by default.
The comparison is not "experimental kernel versus safe kernel". It is "experimental kernel versus
MATH", and MATH puts a 32-head fp16 model at 4.3 GB of attention scores at 8k tokens and 17.2 GB at
16k, which is an OOM on a 16 GB card before any weights are loaded. Against that, a 0.00195 fp16
delta on a path that passes forward and backward in both dtypes is the better default.
Anyone who hits trouble sets the variable to
0and gets the old behaviour back; the helper neveroverwrites an existing value. If AMD's own confidence in RDNA4 backward shifts, the default is one
line to flip.