Skip to content

Commit 12c4096

Browse files
Add autoloading for backend packages (#1593)
Co-authored-by: Titus von Koeller <[email protected]>
1 parent 8f88cef commit 12c4096

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

bitsandbytes/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# LICENSE file in the root directory of this source tree.
55

66

7+
import sys
8+
79
import torch
810

911
from . import _ops, research, utils
@@ -32,6 +34,30 @@
3234
if torch.cuda.is_available():
3335
from .backends.cuda import ops as cuda_ops
3436

37+
38+
def _import_backends():
39+
"""
40+
Discover and autoload all available backends installed as separate packages.
41+
Packages with an entrypoint for "bitsandbytes.backends" will be loaded.
42+
Inspired by PyTorch implementation: https://pytorch.org/tutorials/prototype/python_extension_autoload.html
43+
"""
44+
from importlib.metadata import entry_points
45+
46+
if sys.version_info < (3, 10):
47+
extensions = entry_points().get("bitsandbytes.backends", [])
48+
else:
49+
extensions = entry_points(group="bitsandbytes.backends")
50+
51+
for ext in extensions:
52+
try:
53+
entry = ext.load()
54+
entry()
55+
except Exception as e:
56+
raise RuntimeError(f"bitsandbytes: failed to load backend {ext.name}: {e}") from e
57+
58+
59+
_import_backends()
60+
3561
__pdoc__ = {
3662
"libbitsandbytes": False,
3763
"optim.optimizer.Optimizer8bit": False,

0 commit comments

Comments
 (0)