From 73eed19ce4b3cbf11366f8f433614a3be8f30254 Mon Sep 17 00:00:00 2001 From: Tai An Date: Thu, 11 Jun 2026 08:53:13 -0700 Subject: [PATCH] fix(_utils): coerce _is_package_available tuple to bool for flash_attn/vllm checks (#6168) transformers >= 5.x makes _is_package_available always return a (exists, version) tuple, which is truthy even when the package is absent. The flash_attn and vLLM availability checks treated the result as a bool, so they always entered the "package present" branch: - flash_attn (CUDA + HIP): the inner import raises when flash-attn is not installed, printing a false "Flash Attention 2 installation seems to be broken" warning before falling back to xformers. - is_vLLM_available(): always reported vLLM as installed. Add a small _package_available() helper that normalises the result to a bool (handling both the new tuple and the legacy bool return), and route the three call sites through it. Fixes #6155 Co-authored-by: Lee Jackson <130007945+Imagineer99@users.noreply.github.com> --- unsloth/models/_utils.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py index 1a86ff8eb6..a2ab3149f1 100644 --- a/unsloth/models/_utils.py +++ b/unsloth/models/_utils.py @@ -1264,6 +1264,18 @@ if is_openai_available(): from transformers import AutoTokenizer from transformers.utils.import_utils import _is_package_available + +def _package_available(pkg_name: str) -> bool: + # transformers >= 5.x makes `_is_package_available` always return a + # `(exists, version)` tuple, which is truthy even when the package is + # absent; older versions returned a plain bool. Normalise to a bool so + # callers don't take "package present" branches for missing packages. + result = _is_package_available(pkg_name) + if isinstance(result, tuple): + return bool(result[0]) + return bool(result) + + SUPPORTS_BFLOAT16 = False HAS_FLASH_ATTENTION = False HAS_FLASH_ATTENTION_SOFTCAPPING = False @@ -1274,7 +1286,7 @@ if DEVICE_TYPE == "cuda": if major_version >= 8: SUPPORTS_BFLOAT16 = True - if _is_package_available("flash_attn"): + if _package_available("flash_attn"): # Check for CUDA linking errors "undefined symbol: _ZNK3c106SymIntltEl" try: try: @@ -1319,7 +1331,7 @@ if DEVICE_TYPE == "cuda": HAS_FLASH_ATTENTION = False elif DEVICE_TYPE == "hip": SUPPORTS_BFLOAT16 = True - if _is_package_available("flash_attn"): + if _package_available("flash_attn"): # Check for CUDA linking errors "undefined symbol: _ZNK3c106SymIntltEl" try: try: @@ -1981,7 +1993,7 @@ def is_bfloat16_supported(): def is_vLLM_available(): - return _is_package_available("vllm") + return _package_available("vllm") # Patches models to add RoPE Scaling