Skip to content

Block CUDA graphs: only release the cudagraph pool for tree functions the block backend compiled - #15777

Merged
danielhanchen merged 4 commits into
mainfrom
dh/graph-pool-foreign-ids
Oct 28, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
dh/graph-pool-foreign-ids

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

With UNSLOTH_BLOCK_CUDAGRAPHS=1, going past the shape cap turns block graphs off and releases the cudagraph tree pool via reset_cudagraph_trees(), but only when every tree function belongs to a block compile (#15352). Ownership was taken as every tree function created during a graphed block call (ids_to_funcs before / after delta in _graphed_call).

A third-party torch.compile(mode="reduce-overhead") function that is first called inside a block (behind a graph break, e.g. through a torch.compiler.disable helper) registers its tree function during that same call, so it was counted as the block's. Past the cap the release then reset the shared tree, and the next call of that function failed with AssertionError: Running CUDAGraph after shutdown.

Change

The graphs-on block compile now uses a subclass of the stock inductor backend (torch._TorchCompileInductorWrapper, same options) that records CompileContext.current_compile_id() for every graph it compiles. A foreign region compiles through its own backend, so its compile ids are never recorded. _graphed_call only counts new tree functions whose compile id the block backend recorded; anything else makes the release verdict "foreign" and the pool is kept, as already happens for regions created before the first block call. The backward tree function carries the forward's compile id, so a block-only tree is still released. If the backend cannot be built, nothing counts as the block's and the pool is kept. The graphs-off copy and runs without UNSLOTH_BLOCK_CUDAGRAPHS=1 are unchanged.

Tests

tests/test_block_cudagraphs_clone.py:

  • test_foreign_function_first_called_inside_a_block_is_not_ours: a mocked tree manager gets a block and a foreign tree function during one _graphed_call; only the block one is ours and the verdict is "foreign". Fails on main.
  • test_block_backend_notes_the_compile_ids_it_builds, test_only_the_graphs_on_compile_gets_the_owning_backend: the backend records the compile id and keeps the block options; only the graphs-on compile gets it.
  • test_graphed_call_records_the_compile_ids_it_created updated for the recorded ids.

CPU: test_block_cudagraphs_clone.py, test_block_compile.py, test_block_compile_discovery.py, test_legacy_block_compile_install.py, test_fsdp2_native_block_cudagraphs*_gloo.py: 138 passed, 6 skipped, 1 failed (test_sm_count_is_read_outside_the_trace needs a CUDA device and fails the same way on main).

GPU (RTX PRO 6000 Blackwell, torch 2.14.1+cu130): one decoder layer whose forward calls a reduce-overhead helper through a torch.compiler.disable boundary, sequence lengths 40, 48, 56 so the third shape passes the cap of 2:

foreign helper inside the block block only
main pool released, then Running CUDAGraph after shutdown at step 8 pool released
this change pool kept, every step and a later standalone helper call succeed pool released

Not covered

torch 2.7 to 2.13 on GPU were not run (the backend class and CompileContext.current_compile_id() exist across that range). Multi GPU runs were not run.

…ackend compiled it

The pool release counted every cudagraph tree function created during a graphed block
call as the block's. A third-party torch.compile(mode="reduce-overhead") region first
called inside a block (behind a graph break) was counted too, so going past the shape cap
reset the shared tree and its next call failed with "Running CUDAGraph after shutdown".
The graphs-on block compile now uses an inductor backend that notes the compile ids it
builds; only those count as ours, anything else keeps the pool.
@danielhanchen danielhanchen self-assigned this Oct 28, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-10-28T17:48:25.444740Z 8d07bc6 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 review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 8d07bc6780

ℹ️ 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 dda083b into main Oct 28, 2026
@danielhanchen
danielhanchen deleted the dh/graph-pool-foreign-ids branch October 28, 2026 19:24
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