Repository navigation
Tests: import bitsandbytes before the GPU-free harness spoofs CUDA - #7582
Merged
Merged
Conversation
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.
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
This was referenced Jul 29, 2026
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.
This was referenced Jul 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Coreis red onmain, all three matrix legs, and takes every open PR with it:The wheel is fine. In the same CPU-only venv, a plain
import bitsandbytesgives0.50.0withfunctional,functional.liband all five ctypes symbols present. What breaks it is our own test harness.Cause
tests/conftest.pypatchestorch.cuda.is_availableto returnTruesodevice_type.py's cache captures"cuda"on a GPU-less runner.bitsandbytes/__init__.pyreads that same flag:and
bitsandbytes/backends/cuda/ops.py:69readstorch._C._cuda_getCurrentRawStream, which a CPU-only torch build does not expose. A bitsandbytes import landing inside the spoof window therefore raises:The failure is not recoverable inside the process. Python drops
bitsandbytesfromsys.modulesbut leavesbitsandbytes.functionaland 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:_gpu_init.py:304-309swallows the original AttributeError with a bareexcept:and prints "bitsandbytes is not installed", which is why the real cause never surfaced.Clearing
sys.modulesis not a way out either: re-executingbitsandbytes._opsraisesRuntimeError: 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:That last pair matters: bitsandbytes is genuinely active, not degraded to the
_bnb_requiredstub.tests/python/test_conftest_bitsandbytes_preimport.pyguards the ordering: 3 passed here, 3 failed againstmain'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.ipynbwithload_in_4bit = Trueon bitsandbytes 0.50.0 runs clean end to end on a B200 (193Linear4bitmodules, 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_ptrbut notbnb.functional.lib, which is the third hazard atkernels/utils.py:262-273; anddevice_type.py:125-130derivesALLOW_BITSANDBYTESfrom a probe catching only import exceptions, so on a half-loaded module the flag staysTrueand the failure moves to the first forward pass instead of degrading to 16-bit.