Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
bitsandbytes
Commits
1f2a970e
Commit
1f2a970e
authored
Apr 28, 2025
by
Titus von Koeller
Browse files
C lib loading: add fallback with sensible error msg
parent
c244e983
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
12 deletions
+47
-12
bitsandbytes/cextension.py
bitsandbytes/cextension.py
+47
-12
No files found.
bitsandbytes/cextension.py
View file @
1f2a970e
...
@@ -61,7 +61,37 @@ class CudaBNBNativeLibrary(BNBNativeLibrary):
...
@@ -61,7 +61,37 @@ class CudaBNBNativeLibrary(BNBNativeLibrary):
lib
.
cget_managed_ptr
.
restype
=
ct
.
c_void_p
lib
.
cget_managed_ptr
.
restype
=
ct
.
c_void_p
class
MockBNBNativeLibrary
(
BNBNativeLibrary
):
"""
Mock BNBNativeLibrary that raises an error when trying to use native library functionality without successfully loading the library.
Any method or attribute access will raise a RuntimeError with a message that points to the original error and provides troubleshooting steps.
"""
def
__init__
(
self
,
error_msg
:
str
):
self
.
error_msg
=
error_msg
def
__getattr__
(
self
,
name
):
base_msg
=
"Attempted to use bitsandbytes native library functionality but it's not available.
\n\n
"
original_error
=
f
"Original error:
{
self
.
error_msg
}
\n\n
"
if
self
.
error_msg
else
""
troubleshooting
=
(
"This typically happens when:
\n
"
"1. BNB doesn't ship with a pre-compiled binary for your CUDA version
\n
"
"2. The library wasn't compiled properly during installation
\n
"
"3. Missing CUDA dependencies
\n
"
"4. PyTorch/bitsandbytes version mismatch
\n\n
"
"Run 'python -m bitsandbytes' for diagnostics."
)
raise
RuntimeError
(
base_msg
+
original_error
+
troubleshooting
)
def
__getitem__
(
self
,
name
):
return
self
.
__getattr__
(
name
)
def
get_native_library
()
->
BNBNativeLibrary
:
def
get_native_library
()
->
BNBNativeLibrary
:
"""
Load CUDA library XOR CPU, as the latter contains a subset of symbols of the former.
"""
binary_path
=
PACKAGE_DIR
/
f
"libbitsandbytes_cpu
{
DYNAMIC_LIBRARY_SUFFIX
}
"
binary_path
=
PACKAGE_DIR
/
f
"libbitsandbytes_cpu
{
DYNAMIC_LIBRARY_SUFFIX
}
"
cuda_specs
=
get_cuda_specs
()
cuda_specs
=
get_cuda_specs
()
if
cuda_specs
:
if
cuda_specs
:
...
@@ -86,17 +116,22 @@ def get_native_library() -> BNBNativeLibrary:
...
@@ -86,17 +116,22 @@ def get_native_library() -> BNBNativeLibrary:
try
:
try
:
lib
=
get_native_library
()
lib
=
get_native_library
()
except
Exception
as
e
:
except
Exception
as
e
:
lib
=
None
error_msg
=
f
"Could not load bitsandbytes native library:
{
e
}
"
logger
.
error
(
f
"Could not load bitsandbytes native library:
{
e
}
"
,
exc_info
=
True
)
logger
.
error
(
error_msg
,
exc_info
=
True
)
if
torch
.
cuda
.
is_available
():
logger
.
warning
(
"""
CUDA Setup failed despite CUDA being available. Please run the following command to get more information:
python -m bitsandbytes
diagnostic_help
=
""
if
torch
.
cuda
.
is_available
():
Inspect the output of the command and see if you can locate CUDA libraries. You might need to add them
diagnostic_help
=
(
to your LD_LIBRARY_PATH. If you suspect a bug, please take the information from python -m bitsandbytes
"CUDA Setup failed despite CUDA being available. "
and open an issue at: https://github.com/bitsandbytes-foundation/bitsandbytes/issues
"Please run the following command to get more information:
\n\n
"
"""
,
"python -m bitsandbytes
\n\n
"
"Inspect the output of the command and see if you can locate CUDA libraries. "
"You might need to add them to your LD_LIBRARY_PATH. "
"If you suspect a bug, please take the information from the command and open an issue at:
\n\n
"
"https://github.com/bitsandbytes-foundation/bitsandbytes/issues
\n\n
"
"If you are using a custom CUDA version, you might need to set the BNB_CUDA_VERSION "
"environment variable to the correct version."
)
)
# create a mock with error messaging as fallback
lib
=
MockBNBNativeLibrary
(
diagnostic_help
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment