Skip to content

Tests: import bitsandbytes before the GPU-free harness spoofs CUDA - #7582

Merged
danielhanchen merged 2 commits into
mainfrom
fix/conftest-bnb-preimport
Jul 29, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix/conftest-bnb-preimport

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

Core is red on main, all three matrix legs, and takes every open PR with it:

unsloth/kernels/utils.py:155: in <module>
    get_ptr = bnb.functional.get_ptr
E   AttributeError: module 'bitsandbytes' has no attribute 'functional'

The wheel is fine. In the same CPU-only venv, a plain import bitsandbytes gives 0.50.0 with functional, functional.lib and all five ctypes symbols present. What breaks it is our own test harness.

Cause

tests/conftest.py patches torch.cuda.is_available to return True so device_type.py's cache captures "cuda" on a GPU-less runner. bitsandbytes/__init__.py reads that same flag:

if torch.cuda.is_available():
    from .backends.cuda import ops as cuda_ops

and bitsandbytes/backends/cuda/ops.py:69 reads torch._C._cuda_getCurrentRawStream, which a CPU-only torch build does not expose. A bitsandbytes import landing inside the spoof window therefore raises:

!!! EXCEPTION during import of bitsandbytes : AttributeError module 'torch._C' has no attribute '_cuda_getCurrentRawStream'
  File ".../bitsandbytes/__init__.py", line 36, in <module>
    from .backends.cuda import ops as cuda_ops
  File ".../bitsandbytes/backends/cuda/ops.py", line 69, in <module>
    _get_raw_stream = torch._C._cuda_getCurrentRawStream

The failure is not recoverable inside the process. Python drops bitsandbytes from sys.modules but leaves bitsandbytes.functional and the rest of the submodules cached, so the next import re-executes __init__ against those cached submodules, re-binds nothing, and returns a module with no .functional. An import trace of the failing run:

<-- DONE  bitsandbytes | bnb.functional=None     first import failed, module dropped
--> IMPORT bitsandbytes
<-- DONE  bitsandbytes | bnb.functional=False    re-import, permanently broken

_gpu_init.py:304-309 swallows the original AttributeError with a bare except: and prints "bitsandbytes is not installed", which is why the real cause never surfaced.

Clearing sys.modules is not a way out either: re-executing bitsandbytes._ops raises RuntimeError: Tried to register an operator (bitsandbytes::int8_mixed_scaled_mm) ... multiple times. The import has to not fail in the first place.

Fix

Import bitsandbytes once before the spoof window, so it binds against the real CPU-only torch, stays on its CPU backend, and remains fully usable. A genuinely missing or broken wheel still falls through to unsloth's own degradation path.

This keeps bitsandbytes working in CI. Declaring it absent would make the CPU matrix green while silently running with 4-bit disabled.

Verification

Reproduced first, in a venv matching the failing job (torch==2.10.0+cpu, bitsandbytes 0.50.0, no GPU), then re-run with the fix:

before:  no tests collected, 2 errors      (the CI-named files)
after:   39 tests collected, 0 errors
full tests/ collection:  696 collected, zero "has no attribute 'functional'" errors
get_ptr is bnb.functional.get_ptr:  True
ALLOW_BITSANDBYTES:                 True

That last pair matters: bitsandbytes is genuinely active, not degraded to the _bnb_required stub.

tests/python/test_conftest_bitsandbytes_preimport.py guards the ordering: 3 passed here, 3 failed against main's conftest. It is source-level on purpose. Reproducing the runtime failure needs a CPU-only torch build, so a behavioural assertion would pass vacuously on any machine with CUDA torch installed.

Separately confirmed that this is CI-only and not user-facing: Llama3.2_(1B_and_3B)-Conversational.ipynb with load_in_4bit = True on bitsandbytes 0.50.0 runs clean end to end on a B200 (193 Linear4bit modules, NF4 round-trip verified, 60/60 steps, generation and adapter save all fine).

Note on #7578

#7578 guards the same crash by treating a half-loaded wheel as absent. Worth keeping as defence-in-depth, but on its own it would turn the CPU matrix green with 4-bit silently off, so I would land this as the actual repair. Two things to add there: it probes bnb.functional.get_ptr but not bnb.functional.lib, which is the third hazard at kernels/utils.py:262-273; and device_type.py:125-130 derives ALLOW_BITSANDBYTES from a probe catching only import exceptions, so on a half-loaded module the flag stays True and the failure moves to the first forward pass instead of degrading to 16-bit.

The CPU test harness patches torch.cuda.is_available to return True so
device_type.py's cache captures "cuda" on a GPU-less runner. bitsandbytes
reads the same flag at import time to decide whether to load its CUDA
backend, and that backend reads torch._C._cuda_getCurrentRawStream, which
a CPU-only torch build does not expose. An import landing inside the spoof
window therefore raises, Python drops bitsandbytes from sys.modules while
leaving its submodules cached, and every later import returns a module with
no .functional, so unsloth/kernels/utils.py dies at module scope.

Import bitsandbytes before the window so it stays on its CPU backend and
remains fully usable, rather than being degraded to unavailable.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 761746ed27

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 0064663 into main Jul 29, 2026
16 checks passed
@danielhanchen
danielhanchen deleted the fix/conftest-bnb-preimport branch July 29, 2026 01:52
vineethsaivs added a commit to vineethsaivs/unsloth that referenced this pull request Jul 30, 2026
Picks up the bitsandbytes test-collection fixes (unslothai#7578, unslothai#7580, unslothai#7582), which
landed after the last run on this branch and are what the red Core jobs were
failing on.

Conflict was the Bucket-A file list only: main appended
tests/test_raw_text_json_loading.py where this branch appended
tests/test_map_eos_token.py. Kept both.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants