fix: graceful fallback when attention backends fail to import #13060
+84
−20
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
External attention backends (flash_attn, xformers, sageattention, etc.) may be installed but fail to import at runtime due to ABI mismatches. For example, when
flash_attnis compiled against PyTorch 2.4 but used with PyTorch 2.8, the import fails with:The current code uses
importlib.util.find_spec()to check if packages exist, but this only verifies the package is installed—not that it can actually be imported. When the import fails, diffusers crashes instead of falling back to native PyTorch attention.Solution
Wrap all external attention backend imports in try-except blocks that catch
ImportErrorandOSError. On failure:_CAN_USE_*flag toFalseNoneThis allows diffusers to gracefully degrade to PyTorch's native SDPA (
scaled_dot_product_attention) instead of crashing.Affected backends
flash_attn(Flash Attention)flash_attn_3(Flash Attention 3)aiter(AMD Instinct)sageattention(SageAttention)flex_attention(PyTorch Flex Attention)torch_npu(Huawei NPU)torch_xla(TPU/XLA)xformers(Meta xFormers)Testing
Tested with PyTorch 2.8.0 and flash_attn 2.7.4.post1 (compiled for PyTorch 2.4).
from diffusers import ...with undefined symbol errorExample warning output