Skip to content

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

Merged
danielhanchen merged 3 commits into
mainfrom
fix/cuda-spoof-supplies-the-raw-stream
Sep 19, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix/cuda-spoof-supplies-the-raw-stream

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Dispatching the notebooks smoke matrix on main after #11325 and #11326 took it from 0/8 to 7/8. The one leg still failing, nb/Llama3.1_(8B)-GRPO.ipynb, dies in Verify imports under spoof:

File "unsloth/kernels/utils.py", line 248, in <module>
    torch._C._cuda_getCurrentRawStream(index)
AttributeError: module 'torch._C' has no attribute '_cuda_getCurrentRawStream'

Why

unsloth snapshots each device's raw stream at import, inside if DEVICE_COUNT > 0 -- a branch this spoof makes true by answering device_count() with 1. A CPU-only torch wheel does not export that symbol.

The spoof already knew. Its own comment says so, and it imports bitsandbytes before flipping is_available() precisely because bitsandbytes reads it:

Its __init__ does if torch.cuda.is_available(): from .backends.cuda import ops, and that module reads torch._C._cuda_getCurrentRawStream at import. On a CPU-only wheel that attribute is absent [...]

It worked around that one importer and never supplied the symbol, so anything reading it afterwards still died.

Why only this leg

From comparing the install sets of the failing leg against a passing one: the GRPO cell is the one pulling vLLM and the CUDA userspace packages -- cuda-python, cuda-bindings, cuda-core, flashinfer-python, compressed-tensors among 85 packages the Alpaca leg never installs. That is what carries unsloth into the DEVICE_COUNT > 0 branch on a CPU-only torch.

The fix

Supply the symbol, and only when absent, so a real CUDA build keeps its own handle. 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.

Verification

Staged the absence and evaluated the expression unsloth evaluates:

result
symbol deleted, no spoof AttributeError: module 'torch._C' has no attribute '_cuda_getCurrentRawStream'
symbol deleted, spoof applied ctypes.c_void_p(...) yields the null pointer

Three tests: both spoofs supply it (_zoo_rocm_spoof delegates to this one, so it is covered too), and a real handle is left alone. Removing the stub fails both spoof rows. Run in a subprocess, because applying a spoof mutates the interpreter's torch for good and this host's torch exports the symbol for real.

Not verified here

This box cannot run the smoke matrix, so the leg itself is still unproven. The failure and the install-set comparison both come from the dispatched run on main (35434257014). The matrix is cron/dispatch only and will not run on this PR; I will dispatch it on main once this lands.

One related observation from that run, not addressed here: unsloth now logs torchcodec 0.0.0 is incompatible with torch and disables it. That is #11325's placeholder being correctly detected as unusable -- non-fatal, and on every leg including the seven that pass.

Dispatching the notebooks smoke matrix on main took it from 0/8 to 7/8. The one
leg still failing, nb/Llama3.1_(8B)-GRPO.ipynb, dies in Verify imports under
spoof:

    File "unsloth/kernels/utils.py", line 248, in <module>
        torch._C._cuda_getCurrentRawStream(index)
    AttributeError: module 'torch._C' has no attribute '_cuda_getCurrentRawStream'

unsloth snapshots each device's raw stream at import, inside
`if DEVICE_COUNT > 0` -- a branch this spoof makes true by answering
device_count() with 1. A CPU-only torch wheel does not export that symbol. The
spoof already knew: it imports bitsandbytes before flipping is_available()
precisely because bitsandbytes reads it. But it worked around that one importer
and never supplied the symbol, so anything reading it afterwards still died.

Why only this leg, from comparing the install sets: GRPO's install cell is the
one pulling vLLM and the CUDA userspace packages (cuda-python, cuda-bindings,
flashinfer, 85 packages the Alpaca leg never installs), which is what carries
unsloth into the DEVICE_COUNT > 0 branch on a CPU-only torch.

Supply it, and only when absent so a real CUDA build keeps its own handle. 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 naming no stream is the honest value.

Verified by staging the absence and evaluating the expression unsloth evaluates:
deleting the symbol reproduces the AttributeError exactly, and after the spoof
ctypes.c_void_p(...) yields the null pointer. Three tests, covering both spoofs
(_zoo_rocm_spoof delegates to this one) and that a real handle is left alone.
Removing the stub fails both spoof rows.

Not verified here: this box cannot run the smoke matrix, so the leg itself is
still unproven. The failure and the install-set comparison both come from the
dispatched run on main (35434257014).
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-19T10:01:06.895370Z 2c45505 Manual request
🔒 Security Review ✅ Completed 2026-09-19T09:33:39.256249Z c4ba9c8 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: c4ba9c8b8d

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

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: c4ba9c8b8d

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Repo tests (CPU, python) caught what I missed:

    FAILED tests/python/test_accelerator_skip_guards.py::test_the_spoofed_probe_list_keeps_up_with_the_spoof
    AssertionError: the spoof patches these and this file does not say what they are
      torch._C._cuda_getCurrentRawStream

That guard requires every name the spoof patches to be classified: _SPOOFED_CALLS
if a skip guard could read it, _SPOOFED_NON_PREDICATES if none would. I added a
patch without declaring it.

It belongs in the non-predicates. It is 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, and nothing branches on the value, so no skip guard
could consult it.

Removing the declaration fails the guard again, naming the symbol.
test_accelerator_skip_guards, test_cuda_spoof_supplies_the_raw_stream and
test_cuda_spoof_reports_free_memory together: 29 passed.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 2c45505fd9

ℹ️ 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 9c97883 into main Sep 19, 2026
29 checks passed
@danielhanchen
danielhanchen deleted the fix/cuda-spoof-supplies-the-raw-stream branch September 19, 2026 10:20
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…slothai#11328)

* Give the CUDA spoof the raw-stream handle unsloth reads at import

Dispatching the notebooks smoke matrix on main took it from 0/8 to 7/8. The one
leg still failing, nb/Llama3.1_(8B)-GRPO.ipynb, dies in Verify imports under
spoof:

    File "unsloth/kernels/utils.py", line 248, in <module>
        torch._C._cuda_getCurrentRawStream(index)
    AttributeError: module 'torch._C' has no attribute '_cuda_getCurrentRawStream'

unsloth snapshots each device's raw stream at import, inside
`if DEVICE_COUNT > 0` -- a branch this spoof makes true by answering
device_count() with 1. A CPU-only torch wheel does not export that symbol. The
spoof already knew: it imports bitsandbytes before flipping is_available()
precisely because bitsandbytes reads it. But it worked around that one importer
and never supplied the symbol, so anything reading it afterwards still died.

Why only this leg, from comparing the install sets: GRPO's install cell is the
one pulling vLLM and the CUDA userspace packages (cuda-python, cuda-bindings,
flashinfer, 85 packages the Alpaca leg never installs), which is what carries
unsloth into the DEVICE_COUNT > 0 branch on a CPU-only torch.

Supply it, and only when absent so a real CUDA build keeps its own handle. 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 naming no stream is the honest value.

Verified by staging the absence and evaluating the expression unsloth evaluates:
deleting the symbol reproduces the AttributeError exactly, and after the spoof
ctypes.c_void_p(...) yields the null pointer. Three tests, covering both spoofs
(_zoo_rocm_spoof delegates to this one) and that a real handle is left alone.
Removing the stub fails both spoof rows.

Not verified here: this box cannot run the smoke matrix, so the leg itself is
still unproven. The failure and the install-set comparison both come from the
dispatched run on main (35434257014).

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Declare the raw-stream handle in the spoofed-probe inventory

Repo tests (CPU, python) caught what I missed:

    FAILED tests/python/test_accelerator_skip_guards.py::test_the_spoofed_probe_list_keeps_up_with_the_spoof
    AssertionError: the spoof patches these and this file does not say what they are
      torch._C._cuda_getCurrentRawStream

That guard requires every name the spoof patches to be classified: _SPOOFED_CALLS
if a skip guard could read it, _SPOOFED_NON_PREDICATES if none would. I added a
patch without declaring it.

It belongs in the non-predicates. It is 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, and nothing branches on the value, so no skip guard
could consult it.

Removing the declaration fails the guard again, naming the symbol.
test_accelerator_skip_guards, test_cuda_spoof_supplies_the_raw_stream and
test_cuda_spoof_reports_free_memory together: 29 passed.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
danielhanchen added a commit that referenced this pull request Oct 17, 2026
#14056)

## Summary

The 2026-10-17 06:39 scheduled `Notebooks CI` run on main (`1018ecbf`,
run 39010742438) failed the `nb/Llama3.1_(8B)-GRPO.ipynb` install-cell
smoke leg, which passed on the previous scheduled run (2026-10-01):

```
unsloth/kernels/utils.py:251: torch._C._cuda_getCurrentRawStream(index)
torch.AcceleratorError: CUDA error: CUDA driver version is insufficient for CUDA runtime version
```

The notebook's non-Colab branch runs `pip install unsloth vllm`, which
now brings in `torch 2.13.0+cu130`.
`tests/_zoo_aggressive_cuda_spoof.py` only supplied
`_cuda_getCurrentRawStream` when the symbol was absent, which is the
CPU-wheel case from #11328. A CUDA wheel on a GPU-less runner has the
symbol, but calling it raises, so `import unsloth` under the spoof died
at that line. The product is fine. The spoof did not cover this wheel.

## Change

- **`tests/_zoo_aggressive_cuda_spoof.py`:** `_cuda_getCurrentRawStream`
is now replaced when it is absent or when calling it raises. A working
handle on a real CUDA machine is still kept.
- **`tests/test_cuda_spoof_supplies_the_raw_stream.py`:** new
`test_the_spoof_replaces_a_raw_stream_that_has_no_driver_behind_it`
stages a raw-stream function that raises the driver error and requires
the spoof to replace it. The existing
`test_the_spoof_leaves_a_real_raw_stream_alone` still passes.

## Testing

- **End to end with the same `torch 2.13.0+cu130` and
`CUDA_VISIBLE_DEVICES=""`:** applying the spoof and then `import
unsloth` fails on main with `AcceleratorError: CUDA error: no
CUDA-capable device is detected`, and passes with this change.
- **Spoof tests:** `test_cuda_spoof_supplies_the_raw_stream.py` and
`test_cuda_spoof_reports_free_memory.py` give 6 passed. With the spoof
reverted, the new test fails.
- **Every test file that imports the spoof:** this change and main fail
on exactly the same test IDs in my local venv, which lacks the pinned
TRL / vLLM versions those tests need. This change adds the new passing
test.
- **`Notebooks CI` with `include_smoke=true` on the first revision of
this branch (run 39015954144):** the GRPO leg passes. That revision also
set `UV_SYSTEM_PYTHON` on the install step. It turned the Nemotron and
Qwen3_5 failures into different ones and broke gpt-oss, so it is not
part of this PR (see below).

## Not in this PR

The Nemotron-3-Nano and Qwen3_5 Vision legs fail for a different reason,
which this PR leaves alone. Their install cells call `!uv pip install`.
Colab sets `UV_SYSTEM_PYTHON=true` (checked on a live
`release-colab-external-images_20261014` runtime), but the smoke job
does not. The job also sets no `COLAB_*` variable, so the notebooks take
their non-Colab branch.

Setting only `UV_SYSTEM_PYTHON` lets the uv lines run, but without the
Colab branch the result is not Colab's environment:
- `transformers==4.57.6` is installed with `--no-deps` next to
`huggingface_hub` 1.29.
- gpt-oss loses `triton`.

Making the job match Colab needs both variables together, and it should
be a separate change with its own smoke run.

The `static + transitive resolve` red (Colab oracle drifted from its
committed snapshot) is also untouched. It has failed on every scheduled
run since at least 2026-09-27.
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