@@ -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
5563class 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