Import bitsandbytes before the hardware spoof rewrites torch - #7471
Merged
Conversation
tests/studio/install/test_rocm_rdna_routing.py errors out on CPU-only CI,
taking Repo tests (CPU) with it, all 12 cases with
OSError: libhipblas.so.2: cannot open shared object file
AttributeError: module 'torch._C' has no attribute '_cuda_getCurrentRawStream'
The spoof presents torch as a Radeon card, which flips
torch.cuda.is_available() to True and sets torch.version.hip. bitsandbytes
gates its backend on exactly that:
if torch.cuda.is_available():
from .backends.cuda import ops as cuda_ops
so a bitsandbytes imported afterwards walks into the CUDA/ROCm path against a
CPU-only wheel and dies reading torch._C._cuda_getCurrentRawStream. It reaches
the test because unsloth_zoo imports it eagerly, guarded by except ImportError,
which neither OSError nor AttributeError satisfies.
Import it in the spoof instead, while is_available() is still False, so the CPU
path is cached in sys.modules before torch is rewritten. Placed in the shared
apply(), ahead of the first mutation and inside the idempotence guard, so the
ROCm spoof that layers on top gets it too.
danielhanchen
force-pushed
the
fix-bnb-import-under-hardware-spoof
branch
from
July 26, 2026 11:58
8862e76 to
8015771
Compare
This was referenced Jul 26, 2026
Member
Author
|
Validated on staging replications onto main, which run the real
The middle row is this change on its own: the 12 errors go to zero, and the single remaining failure was the font-scale test that #7468 has since fixed. With both in place the job is green: Full batch on the last row: Backend CI, Core, Lint CI and Notebooks CI all success. |
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.
Repo tests (CPU)is red on main for two independent reasons. #7468 covers one. This is the other: all 12 cases intests/studio/install/test_rocm_rdna_routing.pyerror out, so the job exits non-zero even with zero test failures.Why
tests/_zoo_rocm_spoof.pypresents torch as a Radeon card so the hip routing paths are testable without AMD hardware. That flipstorch.cuda.is_available()toTrueand setstorch.version.hip. bitsandbytes gates its backend on exactly that, inbitsandbytes/__init__.py:and
backends/cuda/ops.pyreadstorch._C._cuda_getCurrentRawStreamat module scope. CI installs a CPU-only torch (--index-url https://download.pytorch.org/whl/cpu), where that attribute does not exist. So a bitsandbytes imported after the spoof walks into a backend that cannot work.It gets imported after the spoof because
unsloth_zoopulls it in eagerly (temporary_patches/__init__->gpt_oss->moe_utils), and the test importsunsloth_zooon the next line. That import is guarded, but only byexcept ImportError, and what comes back here isOSErrorandAttributeError.Change
Import bitsandbytes inside the spoof's
apply(), whileis_available()is stillFalse, so the CPU path is resolved and cached insys.modulesbefore torch is rewritten. It sits in the shared_zoo_aggressive_cuda_spoof.apply()ahead of the first mutation and inside the existing idempotence guard, so the ROCm spoof that layers on top is covered by the same three lines. Wrapped inexcept Exceptionso a host with no bitsandbytes at all is unaffected.Verification
Reproduced in a
uv venvmatching the CI install shape (Python 3.12,torch>=2.4,<2.11from the CPU index,bitsandbytes>=0.45from PyPI,unsloth_zoofrom git main). Confirmed CPU-only:torch 2.10.0+cpu,hasattr(torch._C, "_cuda_getCurrentRawStream") == False. The spawned child fails with the identicalAttributeErrorbefore the change and returns itsRESULTline after.tests/studio/install, same venv, before and after:The 3 failures are the same ones on both sides (
test_managed_node_runtime, unrelated to this change and to bitsandbytes).test_rocm_rdna_routing.pyon its own goes from 11 passed + 12 errors to 23 passed.test_rocm_arch_table_parity.py(76 passed) andtest_xpu_spoof_pipeline.pyare unchanged before and after.