Skip to content

[Bug] AMD: pip-installed unsloth leaves the ROCm AOTriton gate shut, so SDPA falls to MATH and finetuning OOMs at a fraction of the context the card holds #8819

Closed

Description

@LeoBorcherding

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 0

librocdxg.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

Activity

  1. LeoBorcherding commented on Aug 14, 2026

    @LeoBorcherding
    CollaboratorAuthor

    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 no
    attention kernel at all (cross-entropy, RMSNorm, RoPE, SwiGLU, GEGLU, LoRA and MoE are custom
    Triton; attention is delegated):

    • Unsloth's own attention forward calls it directly: unsloth/models/llama.py:550 and :559,
      plus cohere.py:421, granite.py:409, falcon_h1.py:363.
    • The loader seeds it explicitly: unsloth/models/llama.py:2449 and :2459,
      attn_implementation = "sdpa".

    Where it does not apply.

    model path on ROCm AOTriton gate helps?
    most LLMs (llama, qwen, mistral, …) SDPA yes
    gemma3, gemma3_text, shieldgemma2 flex_attention (_FLEX_PREFERRED_MODELS, _utils.py:399) no
    gpt_oss, deepseek_v4 eager (_SDPA_EXCLUDED_MODELS, _utils.py:400) no
    flash-attn built from source flash already linear

    A second quadratic path worth separating out. flex_attention is not automatically better.
    _utils.py:458 records that below the capability floor the flex HOP "runs its eager sdpa_dense
    fallback", 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=partial for 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.

  2. LeoBorcherding commented on Aug 14, 2026

    @LeoBorcherding
    CollaboratorAuthor

    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, hip 7.13.99004,
    rocm-sdk-libraries-gfx120X-all 7.13.0.

    The wheel carries the kernels:

    torch/lib/aotriton_v2.dll
    torch/lib/aotriton_v2.lib
    torch/lib/aotriton.images/amd-gfx120x/
    

    Torch's own flags claim the backends are on:

    flash_sdp_enabled      True
    mem_eff_sdp_enabled    True
    

    They are not. With the variable unset, every forced-backend call fails:

    dtype backend direction causal unset set to 1
    fp16 flash fwd no No available kernel pass
    fp16 flash bwd no No available kernel pass
    fp16 mem-eff fwd no No available kernel pass
    fp16 mem-eff bwd no No available kernel pass
    fp16 flash fwd yes No available kernel pass
    fp16 flash bwd yes No available kernel pass
    fp16 mem-eff fwd yes No available kernel pass
    fp16 mem-eff bwd yes No available kernel pass
    bf16 all eight of the above No available kernel pass

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

    Mem Efficient attention on Current AMD GPU is still experimental.
    Enable it with TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL=1
      sdp_utils.cpp:384
    Flash attention has been runtime disabled.
      sdp_utils_cpp.h:540
    

    Accuracy against the math backend at that shape, fp16 causal: max abs diff 0.00195. Larger than
    the 1.221e-04 measured at B=1 H=16 N=9408 D=64 in #8225, and expected to be: shorter sequences
    and a different reduction order move fp16 rounding around. Both are noise, neither is a kernel
    disagreement.

    Two things this pins down:

    • Windows is in scope. Earlier I could not tell whether repo.amd.com/rocm/windows shipped the
      library at all.
    • flash_sdp_enabled() and mem_efficient_sdp_enabled() are not a usable probe on ROCm. They
      report the user-facing toggle, not whether a kernel exists. Anything checking AMD attention
      support has to attempt a call.

    gfx1201 shares the amd-gfx120x image family so it should behave the same, but that is inference
    from 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 0 and gets the old behaviour back; the helper never
    overwrites an existing value. If AMD's own confidence in RDNA4 backward shifts, the default is one
    line to flip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions