cextension.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import ctypes as ct
  2. from pathlib import Path
  3. from warnings import warn
  4. from .cuda_setup.main import evaluate_cuda_setup
  5. class CUDALibrary_Singleton(object):
  6. _instance = None
  7. def __init__(self):
  8. raise RuntimeError("Call get_instance() instead")
  9. def initialize(self):
  10. binary_name = evaluate_cuda_setup()
  11. package_dir = Path(__file__).parent
  12. binary_path = package_dir / binary_name
  13. if not binary_path.exists():
  14. print(f"CUDA SETUP: TODO: compile library for specific version: {binary_name}")
  15. legacy_binary_name = "libbitsandbytes.so"
  16. print(f"CUDA SETUP: Defaulting to {legacy_binary_name}...")
  17. binary_path = package_dir / legacy_binary_name
  18. if not binary_path.exists():
  19. print('CUDA SETUP: CUDA detection failed. Either CUDA driver not installed, CUDA not installed, or you have multiple conflicting CUDA libraries!')
  20. print('CUDA SETUP: If you compiled from source, try again with `make CUDA_VERSION=DETECTED_CUDA_VERSION` for example, `make CUDA_VERSION=113`.')
  21. raise Exception('CUDA SETUP: Setup Failed!')
  22. # self.lib = ct.cdll.LoadLibrary(binary_path)
  23. self.lib = ct.cdll.LoadLibrary(str(binary_path)) # $$$
  24. else:
  25. print(f"CUDA SETUP: Loading binary {binary_path}...")
  26. # self.lib = ct.cdll.LoadLibrary(binary_path)
  27. self.lib = ct.cdll.LoadLibrary(str(binary_path)) # $$$
  28. @classmethod
  29. def get_instance(cls):
  30. if cls._instance is None:
  31. cls._instance = cls.__new__(cls)
  32. cls._instance.initialize()
  33. return cls._instance
  34. lib = CUDALibrary_Singleton.get_instance().lib
  35. try:
  36. lib.cadam32bit_g32
  37. lib.get_context.restype = ct.c_void_p
  38. lib.get_cusparse.restype = ct.c_void_p
  39. COMPILED_WITH_CUDA = True
  40. except AttributeError:
  41. warn(
  42. "The installed version of bitsandbytes was compiled without GPU support. "
  43. "8-bit optimizers and GPU quantization are unavailable."
  44. )
  45. COMPILED_WITH_CUDA = False