Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Give the CUDA spoof the raw-stream handle unsloth reads at import (#11328)

Daniel Han9c97883176eb
diff --git a/tests/_zoo_aggressive_cuda_spoof.py b/tests/_zoo_aggressive_cuda_spoof.py
index 44b4f58b1b0..c0172cdeefb 100644
--- a/tests/_zoo_aggressive_cuda_spoof.py
+++ b/tests/_zoo_aggressive_cuda_spoof.py
@@ -41,6 +41,25 @@ def apply() -> None:
torch.cuda.is_bf16_supported = lambda *a, **k: True
torch.cuda._is_in_bad_fork = lambda *a, **k: False # type: ignore[attr-defined]
+ # The raw-stream handle, which a CPU-only wheel does not export. This module already
+ # knows that -- the bitsandbytes import above exists because of it -- but only worked
+ # around it for bitsandbytes and never supplied the symbol, so anything that reads it
+ # AFTER is_available() flips still dies. unsloth/kernels/utils.py does, at import:
+ #
+ # torch._C._cuda_getCurrentRawStream(index)
+ #
+ # under `if DEVICE_COUNT > 0`, which this spoof makes true. The notebooks smoke matrix
+ # showed it on the one leg whose install cell pulls vLLM and the CUDA userspace packages
+ # (cuda-python, cuda-bindings, flashinfer): seven legs passed and Llama3.1-(8B)-GRPO
+ # failed with `AttributeError: module 'torch._C' has no attribute
+ # '_cuda_getCurrentRawStream'`.
+ #
+ # 0 is the null (default) stream. Callers wrap it in ctypes.c_void_p and no kernel is
+ # ever launched under the spoof, so a handle that names no stream is the honest value --
+ # and set only when absent, so a real CUDA build keeps its own.
+ if not hasattr(torch._C, "_cuda_getCurrentRawStream"):
+ torch._C._cuda_getCurrentRawStream = lambda index = 0: 0 # type: ignore[attr-defined]
+
class _Props:
name = "NVIDIA A100-SPOOFED"
major = 8
diff --git a/tests/python/test_accelerator_skip_guards.py b/tests/python/test_accelerator_skip_guards.py
index 8c6d2ed40b0..7fa04cab198 100644
--- a/tests/python/test_accelerator_skip_guards.py
+++ b/tests/python/test_accelerator_skip_guards.py
@@ -67,6 +67,10 @@ _SPOOFED_CALLS = {
# meta-test below can insist every newly spoofed name lands in one bucket or the other.
_SPOOFED_NON_PREDICATES = {
("torch", "Tensor", "is_pinned"),
+ # A stream handle, not a question: unsloth/kernels/utils.py reads it at import under
+ # `if DEVICE_COUNT > 0` and wraps it in ctypes.c_void_p. Nothing branches on it, so no
+ # skip guard could read it.
+ ("torch", "_C", "_cuda_getCurrentRawStream"),
("torch", "Tensor", "pin_memory"),
("torch", "cuda", "Event"),
("torch", "cuda", "Stream"),
diff --git a/tests/test_cuda_spoof_supplies_the_raw_stream.py b/tests/test_cuda_spoof_supplies_the_raw_stream.py
new file mode 100644
index 00000000000..fa695fe5093
--- /dev/null
+++ b/tests/test_cuda_spoof_supplies_the_raw_stream.py
@@ -0,0 +1,111 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""A CUDA spoof has to supply `torch._C._cuda_getCurrentRawStream`.
+
+`unsloth/kernels/utils.py` snapshots each device's raw stream AT IMPORT, inside
+`if DEVICE_COUNT > 0` -- a branch the spoof makes true by answering
+`torch.cuda.device_count()` with 1:
+
+ torch._C._cuda_getCurrentRawStream(index)
+
+A CPU-only torch wheel does not export that symbol. The spoof already knew as much -- it
+imports bitsandbytes before flipping `is_available()` precisely because bitsandbytes reads
+it -- but it only worked around the one importer and never supplied the symbol, so anything
+reading it afterwards still died.
+
+The notebooks smoke matrix showed it on exactly one leg: seven passed and
+`nb/Llama3.1_(8B)-GRPO.ipynb` failed with
+
+ AttributeError: module 'torch._C' has no attribute '_cuda_getCurrentRawStream'
+
+That leg's install cell is the one pulling vLLM and the CUDA userspace packages
+(cuda-python, cuda-bindings, flashinfer), which is what carried unsloth into the
+`DEVICE_COUNT > 0` branch on a CPU-only torch.
+
+Run in a subprocess: applying a spoof mutates the interpreter's torch for good, and this
+host's torch exports the symbol for real, so the absence has to be staged somewhere
+disposable.
+"""
+
+from __future__ import annotations
+
+import pathlib
+import subprocess
+import sys
+import textwrap
+
+import pytest
+
+_TESTS = pathlib.Path(__file__).resolve().parent
+_SPOOFS = ("_zoo_aggressive_cuda_spoof.py", "_zoo_rocm_spoof.py")
+
+
+def _probe(spoof_module: str) -> str:
+ """Delete the symbol, apply the spoof, then evaluate what unsloth evaluates."""
+ script = textwrap.dedent(
+ f"""
+ import ctypes, sys
+ sys.path.insert(0, {str(_TESTS)!r})
+ import torch
+
+ # Stand in for a CPU-only wheel, which simply does not carry it.
+ if hasattr(torch._C, "_cuda_getCurrentRawStream"):
+ delattr(torch._C, "_cuda_getCurrentRawStream")
+ try:
+ torch._C._cuda_getCurrentRawStream(0)
+ print("PRE unexpectedly-present")
+ except AttributeError:
+ print("PRE absent")
+
+ import {spoof_module} as spoof
+ spoof.apply()
+
+ # unsloth/kernels/utils.py, verbatim in shape.
+ try:
+ handle = ctypes.c_void_p(torch._C._cuda_getCurrentRawStream(0))
+ except AttributeError as e:
+ print("POST AttributeError", e)
+ else:
+ print("POST ok", handle.value)
+ """
+ )
+ out = subprocess.run([sys.executable, "-c", script], capture_output = True, text = True)
+ assert (
+ "PRE absent" in out.stdout
+ ), f"the probe never staged the absence: {out.stdout}\n{out.stderr}"
+ return out.stdout
+
+
[email protected]("filename", _SPOOFS)
+def test_the_spoof_supplies_the_raw_stream_handle(filename):
+ spoof = filename[: -len(".py")]
+ if not (_TESTS / filename).is_file():
+ pytest.skip(f"{filename} is gone")
+ printed = _probe(spoof)
+ assert "POST AttributeError" not in printed, (
+ f"{filename} leaves torch._C._cuda_getCurrentRawStream absent, so importing unsloth "
+ f"under it dies the way the GRPO smoke leg did: {printed}"
+ )
+ # The null stream. Callers wrap it in ctypes.c_void_p, where 0 reads back as None.
+ assert "POST ok None" in printed, printed
+
+
+def test_the_spoof_leaves_a_real_raw_stream_alone():
+ """Only supplied when absent: a machine with CUDA keeps the handle its driver gives."""
+ script = textwrap.dedent(
+ f"""
+ import sys
+ sys.path.insert(0, {str(_TESTS)!r})
+ import torch
+ sentinel = object()
+ torch._C._cuda_getCurrentRawStream = lambda index = 0: 4242
+ import _zoo_aggressive_cuda_spoof as spoof
+ spoof.apply()
+ print("VALUE", torch._C._cuda_getCurrentRawStream(0))
+ """
+ )
+ out = subprocess.run([sys.executable, "-c", script], capture_output = True, text = True)
+ assert (
+ "VALUE 4242" in out.stdout
+ ), f"the spoof overwrote a raw-stream handle that was already there: {out.stdout}\n{out.stderr}"