Repository: huggingface/transformers
Pull request: Logging message for ``` is_bitsandbytes_available() ``` 

# What does this PR do?

Fixes [bitsandbytes](https://github.com/bitsandbytes-foundation/bitsandbytes/issues/837)

## Before submitting

- [X] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [X] Was this discussed/approved via a Github issue or the [bnb/issue/837](https://github.com/bitsandbytes-foundation/bitsandbytes/issues/837)? Please add a link
      to it if that's the case.?

## Who can review?

Integrations:
- quantization (bitsandbytes, autogpt): @SunMarc  @MekkCyber





Changed files:
filename: src/transformers/quantizers/quantizer_bnb_4bit.py
patch: @@ -72,10 +72,23 @@ def validate_environment(self, *args, **kwargs):
             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 @@ def validate_environment(self, *args, **kwargs):
                     "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

filename: src/transformers/quantizers/quantizer_bnb_8bit.py
patch: @@ -69,10 +69,23 @@ def validate_environment(self, *args, **kwargs):
             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

filename: src/transformers/utils/import_utils.py
patch: @@ -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