Skip to content

Commit d4df4b7

Browse files
don't error when not calling + doc string
1 parent 0acab1c commit d4df4b7

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

bitsandbytes/cextension.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,19 @@ class BNBNativeLibrary:
4545
def __init__(self, lib: ct.CDLL):
4646
self._lib = lib
4747

48-
def __getattr__(self, item):
49-
return getattr(self._lib, item)
48+
def __getattr__(self, name):
49+
def throw_on_call(*args, **kwargs):
50+
if hasattr(self._lib, name):
51+
return getattr(self._lib, name)(*args, **kwargs)
52+
raise RuntimeError(
53+
f"Method '{name}' not available in CPU-only version of bitsandbytes.\n"
54+
"Reinstall with GPU support or use CUDA-enabled hardware."
55+
)
56+
57+
return throw_on_call
5058

5159
def __getitem__(self, item):
52-
return getattr(self._lib, item)
60+
return self.__getattr__(item)
5361

5462

5563
class CudaBNBNativeLibrary(BNBNativeLibrary):
@@ -221,8 +229,12 @@ def _format_dependency_error(self) -> str:
221229
)
222230

223231
def __getattr__(self, name):
224-
"""Raise error with detailed message when any attribute is accessed"""
225-
raise RuntimeError(f"{self.formatted_error}Native code method attempted to access: lib.{name}()")
232+
"""Return a dummy function that throws when called, rather than on attribute access"""
233+
234+
def throw_on_call(*args, **kwargs):
235+
raise RuntimeError(f"{self.formatted_error}Native code method attempted to call: lib.{name}()")
236+
237+
return throw_on_call
226238

227239
def __getitem__(self, name):
228240
return self.__getattr__(name)

0 commit comments

Comments
 (0)