From 7b068090b2aece8a3ee7fe98959ec59a9d6051a0 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Tue, 28 Jul 2026 21:18:05 -0700 Subject: [PATCH] Fix bitsandbytes zombie module breaking test collection on CPU runners (#7580) * Fix bitsandbytes zombie module breaking test collection A partially failed `import bitsandbytes` leaves the package half-imported: CPython evicts only the parent from sys.modules and keeps every submodule it had already loaded. The next import re-executes __init__ but every `from .x import y` is served from cache, so the submodule attributes are never rebound. The package imports "successfully" while `bnb.functional` is gone. Bind the submodule via `import bitsandbytes.functional as bnb_functional`, which reads sys.modules directly and survives that state, and import bitsandbytes in tests/conftest.py on the real CPU path before torch.cuda.is_available() is mocked, so the half-imported state is never created in the first place. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- unsloth/_gpu_init.py | 10 ++++++++-- unsloth/kernels/utils.py | 23 +++++++++++++++-------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/unsloth/_gpu_init.py b/unsloth/_gpu_init.py index 682f3ae6c6..7e8f9ced46 100644 --- a/unsloth/_gpu_init.py +++ b/unsloth/_gpu_init.py @@ -303,13 +303,19 @@ if DEVICE_TYPE == "cuda": # Try loading bitsandbytes and triton try: import bitsandbytes as bnb + + # Bind the submodule by name: a half-imported bitsandbytes leaves the parent + # without a `functional` attribute, which would otherwise be misreported below + # as a CUDA linking failure. See unsloth/kernels/utils.py. + import bitsandbytes.functional as bnb_functional except: print( "Unsloth: `bitsandbytes` is not installed - 4bit QLoRA unallowed, but 16bit and full finetuning works!" ) bnb = None + bnb_functional = None try: - cdequantize_blockwise_fp32 = bnb.functional.lib.cdequantize_blockwise_fp32 + cdequantize_blockwise_fp32 = bnb_functional.lib.cdequantize_blockwise_fp32 libcuda_dirs() except: if hasattr(os, "geteuid") and os.geteuid() == 0: @@ -351,7 +357,7 @@ if DEVICE_TYPE == "cuda": pass else: from triton.common.build import libcuda_dirs - cdequantize_blockwise_fp32 = bnb.functional.lib.cdequantize_blockwise_fp32 + cdequantize_blockwise_fp32 = bnb_functional.lib.cdequantize_blockwise_fp32 libcuda_dirs() except: warnings.warn( diff --git a/unsloth/kernels/utils.py b/unsloth/kernels/utils.py index fd73984a38..839eb9db84 100644 --- a/unsloth/kernels/utils.py +++ b/unsloth/kernels/utils.py @@ -136,11 +136,18 @@ def calculate_settings( HAS_CUDA_STREAM = False try: import bitsandbytes as bnb + + # If an earlier `import bitsandbytes` died inside __init__, CPython evicts only + # the parent from sys.modules and keeps its submodules, so this retry re-executes + # __init__ without rebinding `bnb.functional`. `import x.y as z` reads sys.modules + # directly and survives that, plain attribute access does not. + import bitsandbytes.functional as bnb_functional except Exception: # device_type.py already degrades to 16bit/full finetuning when bnb is missing # (e.g. gfx906, whose generic wheel has no kernels). Keep the import working and # fail only if a 4bit path is actually entered. bnb = None + bnb_functional = None def _bnb_required(*args, **kwargs): @@ -153,7 +160,7 @@ def _bnb_required(*args, **kwargs): if bnb is not None: # https://github.com/bitsandbytes-foundation/bitsandbytes/pull/1330/files HAS_CUDA_STREAM = Version(bnb.__version__) > Version("0.43.3") - get_ptr = bnb.functional.get_ptr + get_ptr = bnb_functional.get_ptr else: get_ptr = _bnb_required @@ -263,18 +270,18 @@ if bnb is None or not native_kernels_ready(bnb, DEVICE_TYPE): cgemm_4bit_inference_naive_fp16 = _bnb_required cgemm_4bit_inference_naive_bf16 = _bnb_required else: - cdequantize_blockwise_fp32 = bnb.functional.lib.cdequantize_blockwise_fp32 - cdequantize_blockwise_fp16_nf4 = bnb.functional.lib.cdequantize_blockwise_fp16_nf4 - cdequantize_blockwise_bf16_nf4 = bnb.functional.lib.cdequantize_blockwise_bf16_nf4 + cdequantize_blockwise_fp32 = bnb_functional.lib.cdequantize_blockwise_fp32 + cdequantize_blockwise_fp16_nf4 = bnb_functional.lib.cdequantize_blockwise_fp16_nf4 + cdequantize_blockwise_bf16_nf4 = bnb_functional.lib.cdequantize_blockwise_bf16_nf4 if DEVICE_TYPE == "xpu": # https://github.com/bitsandbytes-foundation/bitsandbytes/blob/c3b8de268fdb55a88f92feada23fc811a1e6877a/bitsandbytes/backends/xpu/ops.py#L115 # for xpu, inference gemv using above link - cgemm_4bit_inference_naive_fp16 = bnb.functional.lib.cgemv_4bit_inference_fp16 - cgemm_4bit_inference_naive_bf16 = bnb.functional.lib.cgemv_4bit_inference_bf16 + cgemm_4bit_inference_naive_fp16 = bnb_functional.lib.cgemv_4bit_inference_fp16 + cgemm_4bit_inference_naive_bf16 = bnb_functional.lib.cgemv_4bit_inference_bf16 else: - cgemm_4bit_inference_naive_fp16 = bnb.functional.lib.cgemm_4bit_inference_naive_fp16 - cgemm_4bit_inference_naive_bf16 = bnb.functional.lib.cgemm_4bit_inference_naive_bf16 + cgemm_4bit_inference_naive_fp16 = bnb_functional.lib.cgemm_4bit_inference_naive_fp16 + cgemm_4bit_inference_naive_bf16 = bnb_functional.lib.cgemm_4bit_inference_naive_bf16 torch_device_stream = (