transformers - ✅(Solved) Fix KeyError: 'flash_attn' in import_utils.py when running on Python 3.13 [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#45520Fetched 2026-04-20 11:58:48
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
0
Participants
Timeline (top)
cross-referenced ×1labeled ×1referenced ×1

Fix Action

Fixed

PR fix notes

PR #45524: utils: handle flash_attn missing from importlib packages_distributions without crashing

Description (problem / solution / changelog)

Fixes #45520.

is_flash_attn_2_available, is_flash_attn_3_available, is_flash_attn_4_available, and is_flash_attn_greater_or_equal all do two checks:

is_available, _ = _is_package_available("flash_attn", return_version=True)
is_available = is_available and "flash-attn" in [
    pkg.replace("_", "-") for pkg in PACKAGE_DISTRIBUTION_MAPPING["flash_attn"]
]

Step 1 uses importlib.util.find_spec, which returns a spec if any flash_attn import is findable — an editable install, a namespace package, a bundled shim, or a stub module sitting under another project. Step 2 then assumes that every findable import name also has an entry in importlib.metadata.packages_distributions().

That assumption does not hold in practice. On Python 3.13 with ComfyUI setups (as reported in #45520), and more generally in any environment where the flash_attn import is resolvable via a non-pip source, packages_distributions() has no "flash_attn" key. Because the list comprehension is evaluated before the in operator, short-circuit evaluation of the outer and does not protect us — the KeyError fires during transformers import and takes down the whole process before any model is loaded.

Swap the four raising subscripts for .get(name, []). If the name is missing from the distribution map we simply conclude that the requested flash-attention flavour is not properly installed — which is the answer is_flash_attn_*_available() would have returned anyway — instead of raising. The inner helper _is_package_available already wraps the same subscript in a try/except, so we are only making the outer call sites match that contract.

Changed files

  • src/transformers/utils/import_utils.py (modified, +4/-4)

Code Example

Using Python 3.13.12 environment at: ComfyUI-ROCm/.venv
Name: accelerate
Version: 1.13.0
Location: /home/laichiaheng/ComfyUI-ROCm/.venv/lib/python3.13/site-packages
Requires: huggingface-hub, numpy, packaging, psutil, pyyaml, safetensors, torch
Required-by: peft
---
Name: diffusers
Version: 0.37.1
Location: /home/laichiaheng/ComfyUI-ROCm/.venv/lib/python3.13/site-packages
Requires: filelock, httpx, huggingface-hub, importlib-metadata, numpy, pillow, regex, requests, safetensors
Required-by:
---
Name: torch
Version: 2.13.0.dev20260418+rocm7.2
Location: /home/laichiaheng/ComfyUI-ROCm/.venv/lib/python3.13/site-packages
Requires: filelock, fsspec, jinja2, networkx, setuptools, sympy, triton-rocm, typing-extensions
Required-by: accelerate, kornia, peft, rotary-embedding-torch, spandrel, torchsde, torchvision
---
Name: transformers
Version: 5.5.4
Location: /home/laichiaheng/ComfyUI-ROCm/.venv/lib/python3.13/site-packages
Requires: huggingface-hub, numpy, packaging, pyyaml, regex, safetensors, tokenizers, tqdm, typer
Required-by: comfyui-manager, peft

  Name:                    AMD Ryzen 7 3700X 8-Core Processor 
  Marketing Name:          AMD Ryzen 7 3700X 8-Core Processor 


Python 3.13.12
Linux laichiaheng 7.0.0-1-cachyos #1 SMP PREEMPT Tue, 14 Apr 2026 17:12:24 +0000 x86_64 GNU/Linux
RAW_BUFFERClick to expand / collapse

System Info

Using Python 3.13.12 environment at: ComfyUI-ROCm/.venv
Name: accelerate
Version: 1.13.0
Location: /home/laichiaheng/ComfyUI-ROCm/.venv/lib/python3.13/site-packages
Requires: huggingface-hub, numpy, packaging, psutil, pyyaml, safetensors, torch
Required-by: peft
---
Name: diffusers
Version: 0.37.1
Location: /home/laichiaheng/ComfyUI-ROCm/.venv/lib/python3.13/site-packages
Requires: filelock, httpx, huggingface-hub, importlib-metadata, numpy, pillow, regex, requests, safetensors
Required-by:
---
Name: torch
Version: 2.13.0.dev20260418+rocm7.2
Location: /home/laichiaheng/ComfyUI-ROCm/.venv/lib/python3.13/site-packages
Requires: filelock, fsspec, jinja2, networkx, setuptools, sympy, triton-rocm, typing-extensions
Required-by: accelerate, kornia, peft, rotary-embedding-torch, spandrel, torchsde, torchvision
---
Name: transformers
Version: 5.5.4
Location: /home/laichiaheng/ComfyUI-ROCm/.venv/lib/python3.13/site-packages
Requires: huggingface-hub, numpy, packaging, pyyaml, regex, safetensors, tokenizers, tqdm, typer
Required-by: comfyui-manager, peft

  Name:                    AMD Ryzen 7 3700X 8-Core Processor 
  Marketing Name:          AMD Ryzen 7 3700X 8-Core Processor 


Python 3.13.12
Linux laichiaheng 7.0.0-1-cachyos #1 SMP PREEMPT Tue, 14 Apr 2026 17:12:24 +0000 x86_64 GNU/Linux

Who can help?

When running transformers on Python 3.13 (specifically within a ComfyUI environment on Arch Linux with ROCm 7.2), a KeyError is triggered during the module import process. This happens when the library attempts to check for Flash Attention availability.

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

  1. Install ComfyUI_SeedVR2_VideoUpsacler
  2. Try to load the SeedVR2 templates.

Expected behavior

I can open it like before.

extent analysis

TL;DR

The KeyError triggered during the module import process when running transformers on Python 3.13 within a ComfyUI environment on Arch Linux with ROCm 7.2 may be resolved by ensuring compatibility between the transformers library and the specific Python and ROCm versions.

Guidance

  • Verify that the transformers library version 5.5.4 is compatible with Python 3.13.12 and ROCm 7.2, as the issue might stem from version incompatibilities.
  • Check the official documentation or release notes of the transformers library for any known issues or compatibility problems with the specified environment.
  • Attempt to reproduce the issue with an official example script to isolate if the problem lies within the library itself or the custom scripts.
  • Consider testing with a different Python version or environment to see if the issue persists, helping to narrow down the cause.

Notes

The provided information does not specify the exact line of code or the full error message of the KeyError, which could offer more insight into the problem. Additionally, the compatibility of the transformers library with Python 3.13 and ROCm 7.2 is not explicitly stated, making it a potential point of investigation.

Recommendation

Apply workaround: Given the potential for version incompatibilities and the lack of explicit compatibility statements, attempting to use a different Python version or environment that is known to be compatible with the transformers library might offer a temporary solution until the compatibility issue is resolved.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

I can open it like before.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING