Skip to content

tests: import bitsandbytes before the CUDA/ROCm spoof so Repo tests (CPU) is green again - #7461

Closed
danielhanchen wants to merge 1 commit into
unslothai:mainfrom
danielhanchen:fix/rocm-routing-tests-cpu-runner
Closed

danielhanchen wants to merge 1 commit into
unslothai:mainfrom
danielhanchen:fix/rocm-routing-tests-cpu-runner

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

Backend CI / Repo tests (CPU) is red on main, so every open PR inherits a red check. The last three completed runs on main fail identically (runs 30189939987, 30183620518, 30180331443): 2716 passed, 72 skipped, 12 errors, with all 12 errors being setup errors in one file, tests/studio/install/test_rocm_rdna_routing.py:

ERROR at setup of test_detect_gpu_target[gfx1030 .. gfx1201]
ERROR at setup of test_device_type_is_hip
AssertionError: child produced no result.

Root cause

The routed module fixture runs a child process that applies tests/_zoo_rocm_spoof, imports unsloth_zoo.device_type / unsloth_zoo.llama_cpp, and prints a RESULT {json} line the parent parses. The child dies before printing it.

_zoo_aggressive_cuda_spoof.apply() (which _zoo_rocm_spoof.apply() delegates to) sets torch.cuda.is_available() to True, and the ROCm spoof then layers torch.version.hip on top. bitsandbytes picks its backend at import time:

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

and bitsandbytes/backends/cuda/ops.py binds torch._C._cuda_getCurrentRawStream, which the CPU-only torch build on the runner does not have. The child imports unsloth_zoo, whose temporary_patches.moe_utils does import bitsandbytes as bnb, that import takes the CUDA/ROCm branch under the spoof, and the child dies with:

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
AttributeError: module 'torch._C' has no attribute '_cuda_getCurrentRawStream'

The libhipblas.so.2: cannot open shared object file line and the rocminfo "no such file" line that appear earlier in the same stderr are logged by bitsandbytes and are not fatal: bitsandbytes catches that failure and installs a mock native library. They are the visible symptom of the spoofed HIP identity, not the cause of the crash.

The job started failing without a code change on our side because it installs bitsandbytes>=0.45, which now resolves to 0.50.0, and that release imports the CUDA backend ops module eagerly at package import.

Fix

tests/conftest.py already gets the safe ordering for free: it imports unsloth at collection time, before any fixture spoofs torch, so in-process tests always see a bitsandbytes that was resolved against honest device probes. Subprocess children that apply the spoof standalone do not.

So the spoof itself now does the same thing: _zoo_aggressive_cuda_spoof.apply() imports bitsandbytes once, before it flips torch.cuda.is_available(). That pins the CPU backend and every later import bitsandbytes reuses the cached module. The import is guarded, so a host without bitsandbytes (or with a broken build) is unaffected.

This keeps the coverage intact. No test is deleted, xfailed, skipped, or weakened. The child still resolves get_device_type() to "hip", is_hip() to True, and lc._detect_gpu_target() to ("rocm", gfx) for all 11 RDNA2 / RDNA3 / RDNA3.5 / RDNA4 arches, against real unsloth_zoo code.

The fixture assertion also now reports the child's exit code alongside stdout and stderr, so the next breakage of this shape is obvious from the log.

Verification

Reproduced locally in a venv that mirrors the job (Python 3.12, CPU-only torch 2.10.0+cpu, bitsandbytes 0.50.0, unsloth_zoo from git main, pip install -e . --no-deps):

  • before: 11 passed, 12 errors for tests/studio/install/test_rocm_rdna_routing.py, matching CI exactly (the 11 passes are the pure-mapping test_rocm_gfx_family cases, which never needed a child).
  • after: 23 passed.
  • child under the fix now reports DEVICE hip True, TARGET ('rocm', 'gfx1030'), and BNB backend CPU / HIP_ENVIRONMENT False, with the real CPU native library loaded instead of the error mock, so no libhipblas or rocminfo probe is attempted at all.
  • the same file passes on a CUDA torch build too, where it was already green.
  • tests/studio/install/: 1411 passed, 2 skipped (the 3 test_managed_node_runtime.py failures on my box are a pre-existing local-environment artifact, they fail identically on a clean checkout of main).
  • full job command (pytest tests/ with the workflow's ignores, markers and deselects): 2848 passed, 70 skipped, 23 deselected, zero errors. The one failure, tests/studio/test_ui_font_scale_contract.py::test_no_raw_pixel_text_utilities, also fails on a clean checkout of main in this environment and is unrelated.
  • the isolated hardware-spoof step (test_hardware_dispatch_matrix.py, test_is_mlx_dispatch_gate.py, test_xpu_spoof_pipeline.py): 57 passed, 1 skipped.

…survive

Repo tests (CPU) is red on main: the 12 tests in
tests/studio/install/test_rocm_rdna_routing.py all error at fixture setup
with "child produced no result".

tests/_zoo_aggressive_cuda_spoof.apply() sets torch.cuda.is_available() to
True, and tests/_zoo_rocm_spoof.apply() layers torch.version.hip on top of
it. bitsandbytes picks its backend at import time:

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

and bitsandbytes/backends/cuda/ops.py binds
torch._C._cuda_getCurrentRawStream, which the CPU-only torch build on the
runner does not have. The routing test spawns a child that applies the spoof
and then imports unsloth_zoo, whose temporary_patches.moe_utils does
"import bitsandbytes as bnb". Under the spoof that import takes the
CUDA/ROCm branch and dies with

    AttributeError: module 'torch._C' has no attribute '_cuda_getCurrentRawStream'

before the child can print its RESULT line. The libhipblas.so.2 load error
and the missing rocminfo in the same stderr are logged by bitsandbytes, not
fatal.

tests/conftest.py already gets the safe ordering for free, because it
imports unsloth at collection time, before any fixture spoofs torch.
Subprocess children that apply the spoof standalone do not, so do the same
thing in the spoof itself: import bitsandbytes once while the device probes
are still honest. That pins the CPU backend and every later import reuses
the cached module.

The routing assertions are unchanged and still run for real: the child
resolves device_type "hip", is_hip() True, and ("rocm", gfx) for all 11
RDNA2/3/3.5/4 arches.

Also surface the child's exit code in the fixture assertion so the next
breakage of this shape names itself.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 366226e6eb

ℹ️ 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

Copy link
Copy Markdown
Member Author

CI evidence on this PR's own Repo tests (CPU) run (job):

1 failed, 2867 passed, 73 skipped, 23 deselected, 40 subtests passed

The 12 test_rocm_rdna_routing.py setup errors are gone. The one remaining failure, tests/studio/test_ui_font_scale_contract.py::test_no_raw_pixel_text_utilities, is a separate regression on main from the Agents settings tab and is fixed by #7464. With both landed the job is green again.

@danielhanchen

Copy link
Copy Markdown
Member Author

Note on the red Repo tests (CPU) here: this PR fixes the ROCm half of the breakage, and the one remaining failure is the other half, which #7462 fixes.

On this branch the job goes from 12 errors to 1 failure, and passing tests go 2716 -> 2867. All twelve test_rocm_rdna_routing cases now run and pass. What is left is:

FAILED tests/studio/test_ui_font_scale_contract.py::test_no_raw_pixel_text_utilities

which is the raw px utilities in agents-tab.tsx, fixed by #7462. Conversely #7462 is red only on the ROCm errors this PR fixes, so the two are mutually blocking and neither can show a green job until one of them lands. I applied both on top of main locally and the contract test passes, so they compose.

@danielhanchen

Copy link
Copy Markdown
Member Author

Superseded by #7469, which landed the same bitsandbytes pre-import and fixed the ROCm routing errors on main.

That PR puts the import inside this test file's child code; this one put it in the shared _zoo_aggressive_cuda_spoof.apply() so any test using the spoof is covered. With the breakage already fixed on main the difference is only preventive, and the other five users of that spoof pass today, so there is no reason to keep a second PR open for it. Worth revisiting if the same trap shows up in another subprocess test.

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.

1 participant