bitsandbytes-cpu-fork 0.50.2.dev4__py3-none-manylinux_2_39_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bitsandbytes/__init__.py +81 -0
- bitsandbytes/__main__.py +4 -0
- bitsandbytes/_cpu_caps.py +110 -0
- bitsandbytes/_ops.py +526 -0
- bitsandbytes/autograd/__init__.py +0 -0
- bitsandbytes/autograd/_functions.py +491 -0
- bitsandbytes/backends/__init__.py +0 -0
- bitsandbytes/backends/cpu/__init__.py +0 -0
- bitsandbytes/backends/cpu/ops.py +805 -0
- bitsandbytes/backends/cuda/__init__.py +0 -0
- bitsandbytes/backends/cuda/ops.py +1212 -0
- bitsandbytes/backends/default/__init__.py +0 -0
- bitsandbytes/backends/default/ops.py +632 -0
- bitsandbytes/backends/hpu/__init__.py +0 -0
- bitsandbytes/backends/hpu/ops.py +53 -0
- bitsandbytes/backends/mps/__init__.py +0 -0
- bitsandbytes/backends/mps/ops.py +277 -0
- bitsandbytes/backends/triton/__init__.py +0 -0
- bitsandbytes/backends/triton/kernels_4bit.py +583 -0
- bitsandbytes/backends/triton/kernels_8bit_quant.py +195 -0
- bitsandbytes/backends/triton/kernels_optim.py +1177 -0
- bitsandbytes/backends/triton/ops.py +304 -0
- bitsandbytes/backends/utils.py +94 -0
- bitsandbytes/backends/xpu/__init__.py +0 -0
- bitsandbytes/backends/xpu/ops.py +305 -0
- bitsandbytes/cextension.py +447 -0
- bitsandbytes/cli.py +50 -0
- bitsandbytes/consts.py +12 -0
- bitsandbytes/cuda_specs.py +111 -0
- bitsandbytes/detect_cpu.py +189 -0
- bitsandbytes/diagnostics/__init__.py +0 -0
- bitsandbytes/diagnostics/cuda.py +193 -0
- bitsandbytes/diagnostics/main.py +134 -0
- bitsandbytes/diagnostics/utils.py +12 -0
- bitsandbytes/disk_balancer.py +1276 -0
- bitsandbytes/efst.py +777 -0
- bitsandbytes/functional.py +1836 -0
- bitsandbytes/gdn_cpu.py +616 -0
- bitsandbytes/libbitsandbytes_cpu.so +0 -0
- bitsandbytes/libbitsandbytes_cpu_avx512.so +0 -0
- bitsandbytes/libbitsandbytes_cpu_generic.so +0 -0
- bitsandbytes/nn/__init__.py +19 -0
- bitsandbytes/nn/modules.py +1220 -0
- bitsandbytes/nn/parametrize.py +206 -0
- bitsandbytes/optim/__init__.py +22 -0
- bitsandbytes/optim/adagrad.py +187 -0
- bitsandbytes/optim/adam.py +346 -0
- bitsandbytes/optim/adamw.py +337 -0
- bitsandbytes/optim/ademamix.py +410 -0
- bitsandbytes/optim/lamb.py +190 -0
- bitsandbytes/optim/lars.py +259 -0
- bitsandbytes/optim/lion.py +266 -0
- bitsandbytes/optim/optimizer.py +756 -0
- bitsandbytes/optim/rmsprop.py +170 -0
- bitsandbytes/optim/sgd.py +152 -0
- bitsandbytes/py.typed +0 -0
- bitsandbytes/torch_cpu_kit.py +468 -0
- bitsandbytes/utils.py +208 -0
- bitsandbytes_cpu_cli.py +1012 -0
- bitsandbytes_cpu_fork-0.50.2.dev4.dist-info/METADATA +452 -0
- bitsandbytes_cpu_fork-0.50.2.dev4.dist-info/RECORD +67 -0
- bitsandbytes_cpu_fork-0.50.2.dev4.dist-info/WHEEL +5 -0
- bitsandbytes_cpu_fork-0.50.2.dev4.dist-info/entry_points.txt +3 -0
- bitsandbytes_cpu_fork-0.50.2.dev4.dist-info/licenses/LICENSE +21 -0
- bitsandbytes_cpu_fork-0.50.2.dev4.dist-info/sboms/auditwheel.cdx.json +1 -0
- bitsandbytes_cpu_fork-0.50.2.dev4.dist-info/top_level.txt +2 -0
- bitsandbytes_cpu_fork.libs/libgomp-645478cb.so.1.0.0 +0 -0
bitsandbytes/__init__.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
|
2
|
+
#
|
|
3
|
+
# This source code is licensed under the MIT license found in the
|
|
4
|
+
# LICENSE file in the root directory of this source tree.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
import importlib
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
|
|
12
|
+
from . import _ops, utils
|
|
13
|
+
from .autograd._functions import (
|
|
14
|
+
MatmulLtState,
|
|
15
|
+
matmul,
|
|
16
|
+
matmul_4bit,
|
|
17
|
+
)
|
|
18
|
+
from .backends.cpu import ops as cpu_ops
|
|
19
|
+
from .backends.default import ops as default_ops
|
|
20
|
+
from .nn import modules
|
|
21
|
+
from .optim import adam
|
|
22
|
+
|
|
23
|
+
# This is a signal for integrations with transformers/diffusers.
|
|
24
|
+
# Eventually we may remove this but it is currently required for compatibility.
|
|
25
|
+
features = {"multi_backend"}
|
|
26
|
+
supported_torch_devices = {
|
|
27
|
+
"cpu",
|
|
28
|
+
"cuda", # NVIDIA/AMD GPU
|
|
29
|
+
"xpu", # Intel GPU
|
|
30
|
+
"hpu", # Intel Gaudi
|
|
31
|
+
"npu", # Ascend NPU
|
|
32
|
+
"mps", # Apple Silicon
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if torch.cuda.is_available():
|
|
36
|
+
from .backends.cuda import ops as cuda_ops
|
|
37
|
+
|
|
38
|
+
if hasattr(torch, "xpu") and torch.xpu.is_available():
|
|
39
|
+
from .backends.xpu import ops as xpu_ops
|
|
40
|
+
|
|
41
|
+
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
|
|
42
|
+
from .backends.mps import ops as mps_ops
|
|
43
|
+
|
|
44
|
+
if importlib.util.find_spec("habana_frameworks") and importlib.util.find_spec("habana_frameworks.torch"):
|
|
45
|
+
# In case not automatically imported
|
|
46
|
+
import habana_frameworks.torch
|
|
47
|
+
|
|
48
|
+
if hasattr(torch, "hpu") and torch.hpu.is_available():
|
|
49
|
+
from .backends.hpu import ops as hpu_ops
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _import_backends():
|
|
53
|
+
"""
|
|
54
|
+
Discover and autoload all available backends installed as separate packages.
|
|
55
|
+
Packages with an entrypoint for "bitsandbytes.backends" will be loaded.
|
|
56
|
+
Inspired by PyTorch implementation: https://pytorch.org/tutorials/prototype/python_extension_autoload.html
|
|
57
|
+
"""
|
|
58
|
+
from importlib.metadata import entry_points
|
|
59
|
+
|
|
60
|
+
extensions = entry_points(group="bitsandbytes.backends")
|
|
61
|
+
|
|
62
|
+
for ext in extensions:
|
|
63
|
+
try:
|
|
64
|
+
entry = ext.load()
|
|
65
|
+
entry()
|
|
66
|
+
except Exception as e:
|
|
67
|
+
raise RuntimeError(f"bitsandbytes: failed to load backend {ext.name}: {e}") from e
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
_import_backends()
|
|
71
|
+
|
|
72
|
+
__pdoc__ = {
|
|
73
|
+
"libbitsandbytes": False,
|
|
74
|
+
"optim.optimizer.Optimizer8bit": False,
|
|
75
|
+
"optim.optimizer.MockArgs": False,
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
__version__ = "0.50.2.dev4"
|
|
79
|
+
|
|
80
|
+
# 挂载 gdn_cpu(GDN 融合内核 / patch_transformers 入口),让 bnb.gdn_cpu 可直接访问
|
|
81
|
+
from . import gdn_cpu # noqa: E402,F401
|
bitsandbytes/__main__.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""CPU feature queries used to choose between the AVX2 and AVX-512 CPU libraries.
|
|
2
|
+
|
|
3
|
+
Why a separate module: two places need the same answer -- `cextension` (the main native
|
|
4
|
+
library loader) and `gdn_cpu` (the fused Gated DeltaNet wrapper). Importing one from the
|
|
5
|
+
other would drag the whole native library in at import time, so the query lives here and
|
|
6
|
+
imports nothing but the standard library; torch is imported lazily, only when it is already
|
|
7
|
+
present in the process.
|
|
8
|
+
|
|
9
|
+
The AVX-512 backend is a separate artifact because it is NOT safe on AVX2-only CPUs
|
|
10
|
+
(and it is compiled with AVX512-BF16, so it is also unsafe on AVX-512 CPUs without BF16):
|
|
11
|
+
building it with `-mavx512f` is a translation-unit-wide switch, and GCC auto-vectorises
|
|
12
|
+
ordinary kernels (dequantise, optimizer, rmsnorm) with zmm instructions. The library must
|
|
13
|
+
therefore only be loaded after the CPU and OS are known to support AVX-512. On Linux the
|
|
14
|
+
`/proc/cpuinfo` flags are authoritative; on Windows the query goes through torch, which
|
|
15
|
+
already performs the full CPUID + XGETBV check (and exists in any process that can use this
|
|
16
|
+
package). If neither source can answer, the answer is "no" -- refusing to load the AVX-512
|
|
17
|
+
library is always safe, guessing is not.
|
|
18
|
+
"""
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import os
|
|
22
|
+
|
|
23
|
+
# avx512_bf16 is required too: the AVX-512 artifact is compiled with
|
|
24
|
+
# -mavx512bf16, so it contains bf16 instructions (auto-vectorised) even outside
|
|
25
|
+
# the explicitly guarded bf16 kernels. CPUs with AVX-512 but no BF16
|
|
26
|
+
# (Skylake-X, Cascade Lake) would SIGILL on them.
|
|
27
|
+
_AVX512_FLAGS = frozenset({"avx512f", "avx512bw", "avx512dq", "avx512vl", "avx512_bf16"})
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _env_flag(name: str) -> bool:
|
|
31
|
+
value = os.environ.get(name)
|
|
32
|
+
if value is None:
|
|
33
|
+
return False
|
|
34
|
+
return value.strip().lower() not in ("", "0", "false", "no", "off")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _torch_capability() -> str | None:
|
|
38
|
+
"""What torch already knows about this CPU ('AVX2', 'AVX512', 'DEFAULT', ...)."""
|
|
39
|
+
try:
|
|
40
|
+
import torch
|
|
41
|
+
except Exception: # pragma: no cover - torch is a hard dependency in practice
|
|
42
|
+
return None
|
|
43
|
+
backend = getattr(torch, "backends", None)
|
|
44
|
+
cpu = getattr(backend, "cpu", None)
|
|
45
|
+
query = getattr(cpu, "get_cpu_capability", None)
|
|
46
|
+
if query is None:
|
|
47
|
+
return None
|
|
48
|
+
try:
|
|
49
|
+
return str(query()).upper()
|
|
50
|
+
except Exception: # pragma: no cover - defensive: a broken torch must not crash import
|
|
51
|
+
return None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _read_cpuinfo_flags(path: str = "/proc/cpuinfo") -> set[str] | None:
|
|
55
|
+
"""First ``flags`` line of /proc-style cpuinfo, or None when unreadable (non-Linux)."""
|
|
56
|
+
try:
|
|
57
|
+
with open(path, "r", encoding="utf-8", errors="replace") as handle:
|
|
58
|
+
for line in handle:
|
|
59
|
+
if line.startswith("flags"):
|
|
60
|
+
return set(line.split(":", 1)[1].split())
|
|
61
|
+
except OSError:
|
|
62
|
+
return None
|
|
63
|
+
return None
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _proc_cpuinfo_supports_avx2(path: str = "/proc/cpuinfo") -> bool:
|
|
67
|
+
flags = _read_cpuinfo_flags(path)
|
|
68
|
+
return flags is not None and {"avx2", "fma", "f16c"} <= flags
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _proc_cpuinfo_supports_avx512(path: str = "/proc/cpuinfo") -> bool:
|
|
72
|
+
flags = _read_cpuinfo_flags(path)
|
|
73
|
+
return flags is not None and _AVX512_FLAGS <= flags
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def avx512_supported() -> bool:
|
|
77
|
+
"""True only when the CPU + OS can execute the AVX-512 libraries' instructions.
|
|
78
|
+
|
|
79
|
+
BNB_CPU_DISABLE_AVX512=1 forces the AVX2 library even on capable hardware (useful when
|
|
80
|
+
comparing the two backends); BNB_CPU_FORCE_AVX512=1 forces the AVX-512 path (only safe on
|
|
81
|
+
a machine that really supports it -- it exists so the selection logic can be tested).
|
|
82
|
+
"""
|
|
83
|
+
if _env_flag("BNB_CPU_DISABLE_AVX512"):
|
|
84
|
+
return False
|
|
85
|
+
if _env_flag("BNB_CPU_FORCE_AVX512"):
|
|
86
|
+
return True
|
|
87
|
+
flags = _read_cpuinfo_flags()
|
|
88
|
+
if flags is not None:
|
|
89
|
+
# /proc/cpuinfo knows about BF16, so it is the authoritative answer on Linux.
|
|
90
|
+
return _AVX512_FLAGS <= flags
|
|
91
|
+
# torch reports only the ISA level ("AVX512"), not BF16. Since the shipped AVX-512
|
|
92
|
+
# library contains bf16 instructions, an ISA-level "yes" alone is not safe there.
|
|
93
|
+
capability = _torch_capability()
|
|
94
|
+
if capability is not None:
|
|
95
|
+
return False
|
|
96
|
+
return False
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def avx2_supported() -> bool:
|
|
100
|
+
"""True when the CPU can execute the regular (AVX2+FMA) artifact.
|
|
101
|
+
|
|
102
|
+
BNB_CPU_FORCE_GENERIC=1 forces the portable artifact (it runs on any x86-64, so this is
|
|
103
|
+
safe and makes the three-way selection testable on an AVX2 machine).
|
|
104
|
+
"""
|
|
105
|
+
if _env_flag("BNB_CPU_FORCE_GENERIC"):
|
|
106
|
+
return False
|
|
107
|
+
capability = _torch_capability()
|
|
108
|
+
if capability is not None:
|
|
109
|
+
return capability in ("AVX2", "AVX512")
|
|
110
|
+
return _proc_cpuinfo_supports_avx2()
|