From 71f73859423f12888dcd460ae4ff1fcbb73d5625 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=B5=E0=A5=87=E0=A4=A6=E0=A4=BE=E0=A4=82=E0=A4=A4?= <146507396+ved1beta@users.noreply.github.com> Date: Tue, 10 Jun 2025 15:45:01 +0530 Subject: [PATCH] Logging message for ``` is_bitsandbytes_available() ``` (#38528) * bnb import log * bnb import log * log mesage change * moved error issue in qunatizer_bnb_4_bit.py * ruff * arg added for bnb check * required changes --------- Co-authored-by: Marc Sun <57196510+SunMarc@users.noreply.github.com> --- .../quantizers/quantizer_bnb_4bit.py | 21 ++++++++++++------- .../quantizers/quantizer_bnb_8bit.py | 15 ++++++++++++- src/transformers/utils/import_utils.py | 10 +++++++-- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/transformers/quantizers/quantizer_bnb_4bit.py b/src/transformers/quantizers/quantizer_bnb_4bit.py index 8beabfa7346..b25d61cdb49 100644 --- a/src/transformers/quantizers/quantizer_bnb_4bit.py +++ b/src/transformers/quantizers/quantizer_bnb_4bit.py @@ -72,10 +72,23 @@ class Bnb4BitHfQuantizer(HfQuantizer): raise ImportError( f"Using `bitsandbytes` 4-bit quantization requires Accelerate: `pip install 'accelerate>={ACCELERATE_MIN_VERSION}'`" ) - if not is_bitsandbytes_available(): + if not is_bitsandbytes_available(check_library_only=True): raise ImportError( "Using `bitsandbytes` 4-bit quantization requires the latest version of bitsandbytes: `pip install -U bitsandbytes`" ) + if not is_torch_available(): + raise ImportError( + "The bitsandbytes library requires PyTorch but it was not found in your environment. " + "You can install it with `pip install torch`." + ) + # `bitsandbytes` versions older than 0.43.1 eagerly require CUDA at import time, + # so those versions of the library are practically only available when CUDA is too. + if version.parse(importlib.metadata.version("bitsandbytes")) < version.parse("0.43.1"): + if not torch.cuda.is_available(): + raise ImportError( + "The installed version of bitsandbytes (<0.43.1) requires CUDA, but CUDA is not available. " + "You may need to install PyTorch with CUDA support or upgrade bitsandbytes to >=0.43.1." + ) from ..integrations import validate_bnb_backend_availability from ..utils import is_bitsandbytes_multi_backend_available @@ -110,12 +123,6 @@ class Bnb4BitHfQuantizer(HfQuantizer): "for more details. " ) - if version.parse(importlib.metadata.version("bitsandbytes")) < version.parse("0.39.0"): - raise ValueError( - "You have a version of `bitsandbytes` that is not compatible with 4bit inference and training" - " make sure you have the latest version of `bitsandbytes` installed" - ) - def adjust_target_dtype(self, target_dtype: "torch.dtype") -> "torch.dtype": if version.parse(importlib.metadata.version("accelerate")) > version.parse("0.19.0"): from accelerate.utils import CustomDtype diff --git a/src/transformers/quantizers/quantizer_bnb_8bit.py b/src/transformers/quantizers/quantizer_bnb_8bit.py index e0b5811fc7f..f88035459f8 100644 --- a/src/transformers/quantizers/quantizer_bnb_8bit.py +++ b/src/transformers/quantizers/quantizer_bnb_8bit.py @@ -69,10 +69,23 @@ class Bnb8BitHfQuantizer(HfQuantizer): raise ImportError( f"Using `bitsandbytes` 8-bit quantization requires Accelerate: `pip install 'accelerate>={ACCELERATE_MIN_VERSION}'`" ) - if not is_bitsandbytes_available(): + if not is_bitsandbytes_available(check_library_only=True): raise ImportError( "Using `bitsandbytes` 8-bit quantization requires the latest version of bitsandbytes: `pip install -U bitsandbytes`" ) + if not is_torch_available(): + raise ImportError( + "The bitsandbytes library requires PyTorch but it was not found in your environment. " + "You can install it with `pip install torch`." + ) + # `bitsandbytes` versions older than 0.43.1 eagerly require CUDA at import time, + # so those versions of the library are practically only available when CUDA is too. + if version.parse(importlib.metadata.version("bitsandbytes")) < version.parse("0.43.1"): + if not torch.cuda.is_available(): + raise ImportError( + "The installed version of bitsandbytes (<0.43.1) requires CUDA, but CUDA is not available. " + "You may need to install PyTorch with CUDA support or upgrade bitsandbytes to >=0.43.1." + ) from ..integrations import validate_bnb_backend_availability from ..utils import is_bitsandbytes_multi_backend_available diff --git a/src/transformers/utils/import_utils.py b/src/transformers/utils/import_utils.py index 0d412406d9f..5e024e5857e 100644 --- a/src/transformers/utils/import_utils.py +++ b/src/transformers/utils/import_utils.py @@ -995,8 +995,14 @@ def is_torch_xpu_available(check_device=False): @lru_cache() -def is_bitsandbytes_available(): - if not is_torch_available() or not _bitsandbytes_available: +def is_bitsandbytes_available(check_library_only=False) -> bool: + if not _bitsandbytes_available: + return False + + if check_library_only: + return True + + if not is_torch_available(): return False import torch