2022-07-01 14:16:10 +00:00
|
|
|
import ctypes as ct
|
|
|
|
import os
|
|
|
|
from warnings import warn
|
2022-08-01 10:31:48 +00:00
|
|
|
|
2022-08-02 14:42:27 +00:00
|
|
|
from bitsandbytes.cuda_setup.main import evaluate_cuda_setup
|
2022-07-01 14:16:10 +00:00
|
|
|
|
|
|
|
|
2022-08-01 00:47:44 +00:00
|
|
|
class CUDALibrary_Singleton(object):
|
|
|
|
_instance = None
|
|
|
|
|
|
|
|
def __init__(self):
|
2022-08-01 10:31:48 +00:00
|
|
|
raise RuntimeError("Call get_instance() instead")
|
2022-08-01 00:47:44 +00:00
|
|
|
|
|
|
|
def initialize(self):
|
|
|
|
self.context = {}
|
|
|
|
binary_name = evaluate_cuda_setup()
|
2022-08-01 10:31:48 +00:00
|
|
|
if not os.path.exists(os.path.dirname(__file__) + f"/{binary_name}"):
|
|
|
|
print(f"TODO: compile library for specific version: {binary_name}")
|
|
|
|
print("defaulting to libbitsandbytes.so")
|
|
|
|
self.lib = ct.cdll.LoadLibrary(
|
|
|
|
os.path.dirname(__file__) + "/libbitsandbytes.so"
|
|
|
|
)
|
2022-08-01 00:47:44 +00:00
|
|
|
else:
|
2022-08-01 10:31:48 +00:00
|
|
|
self.lib = ct.cdll.LoadLibrary(
|
|
|
|
os.path.dirname(__file__) + f"/{binary_name}"
|
|
|
|
)
|
2022-08-01 00:47:44 +00:00
|
|
|
|
|
|
|
@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:
|
2022-08-01 10:31:48 +00:00
|
|
|
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
|