bitsandbytes-rocm/bitsandbytes/cextension.py

48 lines
1.5 KiB
Python
Raw Normal View History

2022-07-01 14:16:10 +00:00
import ctypes as ct
from pathlib import Path
2022-07-01 14:16:10 +00:00
from warnings import warn
from .cuda_setup.main import evaluate_cuda_setup
2022-07-01 14:16:10 +00:00
class CUDALibrary_Singleton(object):
_instance = None
def __init__(self):
raise RuntimeError("Call get_instance() instead")
def initialize(self):
binary_name = evaluate_cuda_setup()
package_dir = Path(__file__).parent
binary_path = package_dir / binary_name
if not binary_path.exists():
2022-08-04 16:16:00 +00:00
print(f"CUDA_SETUP: TODO: compile library for specific version: {binary_name}")
legacy_binary_name = "libbitsandbytes.so"
2022-08-04 16:16:00 +00:00
print(f"CUDA_SETUP: Defaulting to {legacy_binary_name}...")
self.lib = ct.cdll.LoadLibrary(package_dir / legacy_binary_name)
else:
2022-08-04 16:16:00 +00:00
print(f"CUDA_SETUP: Loading binary {binary_path}...")
self.lib = ct.cdll.LoadLibrary(binary_path)
@classmethod
def get_instance(cls):
if cls._instance is None:
cls._instance = cls.__new__(cls)
cls._instance.initialize()
return cls._instance
lib = CUDALibrary_Singleton.get_instance().lib
2022-07-01 14:16:10 +00:00
try:
lib.cadam32bit_g32
2022-07-22 21:41:05 +00:00
lib.get_context.restype = ct.c_void_p
lib.get_cusparse.restype = ct.c_void_p
2022-07-01 14:16:10 +00:00
COMPILED_WITH_CUDA = True
except AttributeError:
warn(
"The installed version of bitsandbytes was compiled without GPU support. "
"8-bit optimizers and GPU quantization are unavailable."
)
2022-07-01 14:16:10 +00:00
COMPILED_WITH_CUDA = False