transformers icon indicating copy to clipboard operation
transformers copied to clipboard

SDPA launch failures on ROCm Windows surface as misleading async errors with no fallback — propose error translation or startup probe

Open AIwork4me opened this issue 1 month ago • 3 comments

Summary

On torch builds where a fused SDPA backend launches kernels that fail with hipErrorInvalidValue (Windows ROCm gfx1100, torch 2.12.0+rocm7.14.1 — root-caused upstream in ROCm/TheRock#7315, AOTriton runtime/images mismatch), a plain F.scaled_dot_product_attention call raises:

torch.AcceleratorError: CUDA error: invalid argument
Search for `hipErrorInvalidValue' in ...rocm docs...
CUDA kernel errors might be asynchronously reported at some other API call

Two user-facing problems:

  1. Misleading attribution: the error surfaces at a later, benign op (an elementwise add) because it is asynchronous. Users bisect the wrong layer. (Our first report blamed the residual add; transformers/models/qwen3_vl/modeling_qwen3_vl.py:587 was innocent.)
  2. No recovery path: auto-selection commits to flash/mem-efficient and the whole forward dies, even though the math backend works.

The ComfyUI contrast (probe-and-fallback works)

ComfyUI on the same broken stack probes at startup — construct SDPAParams, launch one tiny flash SDPA, synchronize(), then a dummy tensor.add_(1).item() to flush the async error — catches it, logs Could not run flash attention, disabling it, disables the backend, and everything downstream works (sub-quadratic kernel). Pattern: comfy/model_management.py aotriton_supported() (ComfyUI 0.43.0 b5a4321, lines ~497-527). End users see a working product on a stack where diffusers/transformers pipelines hard-fail.

Proposal (two options, prefer the smaller one first)

Option A — actionable error translation (small, no behavior change): when an SDPA call inside a text-encoder/DiT forward raises an accelerator error mentioning invalid argument/hipErrorInvalidValue, re-raise with a hint: "a fused SDPA backend (flash/mem-efficient) may be failing on this build; try torch.nn.attention.sdpa_kernel(SDPBackend.MATH) or disable the failing backend". One helper + one catch-site per integration (transformers/integrations/sdpa_attention.py, diffusers/models/attention_dispatch.py).

Option B — probe-and-fallback (bigger): one cheap startup probe per process (the ComfyUI pattern, ~10 lines) that downgrades backend priority when a fused backend fails at launch; opt-out via env var. This is what saves ComfyUI users today; doing it in the libraries would fix every HF pipeline on this class of broken builds.

Happy to PR either (A first) — the probe pattern is already field-tested in ComfyUI and we have a 192-cell weights-free repro matrix (evidence) showing exactly which backends fail on which shapes.

Environment: Windows 11 26300, RX 7900 XTX (gfx1100), torch 2.12.0+rocm7.14.1 (repo.amd.com whl-multi-arch), transformers 5.22.1, diffusers 0.42.1; failing models: Qwen3-VL text encoder (GQA) and Qwen-Image-2.1 DiT (dense, bool-4D segment masks).

AIwork4me avatar Nov 01 '26 06:11 AIwork4me

cc @Abdennacer-Badaoui for AMD

Rocketknight1 avatar Nov 02 '26 15:11 Rocketknight1

Hi @AIwork4me,

I've been looking into this issue. The distinction between the asynchronous reporting of a fused SDPA kernel failure and the later operation where the error surfaces is particularly important here. It can make debugging misleading, while the absence of a fallback prevents users from recovering even when the math backend is available.

I'd be interested in helping investigate or validate a focused solution. My initial thought is that actionable error translation could be a lower-risk first step, while a startup probe would need careful consideration of synchronization overhead, backend-specific behavior, and whether a probe reliably predicts failures for different shapes and masks.

Would you recommend starting with Option A, and is there a particular area where an additional contributor could help with testing or implementation?

Thanks!

Spartan2588 avatar Nov 03 '26 18:11 Spartan2588

Hey @AIwork4me, thanks for the detailed write-up and the repro matrix. We'd rather not carry this in transformers. The root cause is the aotriton runtime/image pin mismatch in the ROCm wheel (ROCm/TheRock#7315), so the fix belongs there or in PyTorch's SDPA dispatcher.

Option A also wouldn't fire as written: the failed launch only surfaces at a later op, so catching it at our SDPA call would need a sync after every attention layer. Option B means a library flipping process-global torch backend flags plus a new env var, which we'd have to keep supporting long after the wheel is fixed.

Until a fixed wheel ships, calling torch.backends.cuda.enable_flash_sdp(False) and torch.backends.cuda.enable_mem_efficient_sdp(False) at startup gives you the ComfyUI behavior, or load with attn_implementation="eager".

qgallouedec avatar Nov 25 '26 09:11 qgallouedec