utils: handle flash_attn missing from importlib packages_distributions without crashing (#45524)
* utils: stop crashing with KeyError when flash_attn is importable but 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
(#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 #45520
* utils: fix same KeyError in modeling_flash_attention_utils and add regression tests
Signed-off-by: say <say.apm35@gmail.com>
* style: fix ruff formatting in flash_attn fix and tests
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
* test: move flash_attn distribution-map regression test to test_modeling_utils
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
* revert this
* is gh alright?
---------
Signed-off-by: say <say.apm35@gmail.com>
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
Co-authored-by: SAY-5 <SAY-5@users.noreply.github.com>
Co-authored-by: vasqu <antonprogamer@gmail.com>
Co-authored-by: Anton Vlasjuk <73884904+vasqu@users.noreply.github.com>