utils: handle flash_attn missing from importlib packages_distributions without crashing#45524
Open
SAY-5 wants to merge 1 commit intohuggingface:mainfrom
Open
utils: handle flash_attn missing from importlib packages_distributions without crashing#45524SAY-5 wants to merge 1 commit intohuggingface:mainfrom
SAY-5 wants to merge 1 commit intohuggingface:mainfrom
Conversation
…not in the distribution map
is_flash_attn_2_available / _3 / _4 / _greater_or_equal 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 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. On Python 3.13 with ComfyUI setups
(huggingface#45520), and in any environment where the 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 same
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.
Fixes huggingface#45520
Member
|
cc @vasqu |
vasqu
reviewed
Apr 23, 2026
Contributor
vasqu
left a comment
There was a problem hiding this comment.
Thank you, seems valid in either case. Can you
- Update the same in modeling flash attn utils
transformers/src/transformers/modeling_flash_attention_utils.py
Lines 73 to 109 in 57f9936
- Add a small test similar to
transformers/tests/utils/test_modeling_utils.py
Line 2840 in 57f9936
3 tasks
This was referenced Apr 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #45520.
is_flash_attn_2_available,is_flash_attn_3_available,is_flash_attn_4_available, andis_flash_attn_greater_or_equalall do two checks:Step 1 uses
importlib.util.find_spec, which returns a spec if anyflash_attnimport 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 inimportlib.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_attnimport is resolvable via a non-pip source,packages_distributions()has no"flash_attn"key. Because the list comprehension is evaluated before theinoperator, short-circuit evaluation of the outeranddoes not protect us — theKeyErrorfires duringtransformersimport 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 answeris_flash_attn_*_available()would have returned anyway — instead of raising. The inner helper_is_package_availablealready wraps the same subscript in atry/except, so we are only making the outer call sites match that contract.