Skip to content

Studio diffusion: CUDA graph re-check fixes (eager OOM retry on originals, planned negative length records on the first render) - #14682

Merged
danielhanchen merged 7 commits into
mainfrom
cuda_graph_recheck_fixes
Oct 28, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
cuda_graph_recheck_fixes

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Summary

True CFG on Qwen-Image-2.1 with a negative prompt of another text length now records the negative branch's CUDA graph on the first render. Found while re-checking #14387.

The problem

The planned whole-step graph keys on the prompt length. After the first capture, each new length spends one real step as its warm-up and records on the next step. With true CFG and a negative prompt of a different length, each step makes two calls:

  • The positive key records on the first decode step.
  • The negative key warms up on that same step.
  • On the next step the positive key replays, and that replay seals the image scope for new prompt lengths.
  • The negative key then hit the sealed check (length_eager) and ran ungraphed for the rest of that render and every render after.

The default guidance of 1 runs no negative branch, so it never hits this. Any guidance above 1 always does, because the default negative prompt is empty and so has a different text length from the positive one.

The fix

A planned key that was already warmed before the seal can still record. Only warm-ups from the current render count: the set is cleared at the end of each render (seal_lengths) and on reset(). A length first seen after the first render, or one that warmed only on an earlier render or before a reset, still runs ungraphed, as before.

Results

Qwen-Image-2.1, true CFG with guidance above 1 and an empty negative, two renders on an L4:

graphs recorded length_eager new graphs on render 2
base 1 5 0
this PR 2 0 0

Frames are bit-identical between base and this PR.

Tests

  • test_a_planned_negative_of_another_length_records_on_the_first_render (CPU stub): two planned keys of different lengths over two renders give 2 captures, 1 shape warm-up and 0 length_eager, and a later new length still runs ungraphed. On main it gives 1 capture and fails.
  • test_a_warmup_from_before_a_reset_does_not_reopen_the_sealed_scope and test_a_warmup_from_an_earlier_render_does_not_reopen_the_sealed_scope (CPU stub): a length that warmed before a reset(), or only on an earlier render, still runs ungraphed once the scope is sealed. They guard the fix from reopening more than the current render's warm-ups.
  • test_true_cfg_negative_of_another_length_records_on_the_first_render (CUDA, tiny random Qwen-Image-2.1 transformer, real planned graphs): two true-CFG renders, positive text length 9 and negative 13, bit-identical to the stock forward. With the fix there are 2 captures and 0 length_eager. On main there is 1 capture and the test fails. The UNSLOTH_DIFFUSION_CUDA_GRAPH_EVERY_LENGTH=1 control passes on both.

Known failures on main (not fixed here)

On a CUDA host, three existing tests in tests/test_diffusion_qwenimage21_graph.py fail on main and with this PR alike:

  • test_evicting_the_only_graph_does_not_reuse_its_dead_pool
  • test_a_new_prompt_length_recaptures_and_the_oldest_graph_is_evicted
  • test_a_new_prompt_length_warms_on_tensors_laid_out_like_the_capture

Each one renders a new prompt length after the first render and expects it to record. Since #14387, that length runs ungraphed by design unless UNSLOTH_DIFFUSION_CUDA_GRAPH_EVERY_LENGTH=1 is set.

…pies OOMs

The ungraphed step (a sealed prompt length, a call over the graph cap)
copies inference-mode inputs to normal tensors so compiled blocks keep
their dispatch keys. Only making the copies was guarded against OOM: the
step then ran with the originals and the copies alive, so a near-capacity
resident model could fail a render the plain call fitted. On an OOM there,
free the copies and run the step on the caller's own tensors.
…irst render

A planned whole-step graph warms each new prompt length for one real step
and records it on the next. With true CFG and a negative prompt of another
length, the positive key's first replay sealed the image scope while the
negative key was still in that warm-up, so the negative branch ran
ungraphed on every later step and render. A planned key warmed before the
seal still records; a length first seen after it still runs ungraphed.
@danielhanchen danielhanchen self-assigned this Oct 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-28T02:46:53.511532Z df918cd 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 311c989de1

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

if (
scope in getattr(self, "_sealed_scopes", ())
and (self.placement is None or key not in self._recorded)
and not (self.plan is not None and key in self._warmed)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Do not let pre-reset warmups reopen sealed scopes

When reset_all() runs after a planned key has warmed but before that key captures—for example, after a true-CFG render where the negative branch warmed—GraphedForward.reset() clears the graphs and scope seals but leaves _warmed intact. If the same image scope is then sealed by the first render with the new weights, a later render using that old negative length passes this new exception and performs a capture or eviction even though the length was not part of the post-reset first render. The seal bypass must distinguish warmups pending in the current recording window from stale warmups retained across a reset.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, this was real. reset() clears the graphs and the scope seals but keeps _warmed, and _warmed also lives on across renders. So a length that warmed before a LoRA change (or warmed on an earlier render without recording) passed the sealed-scope exception, and recorded after the new first render had sealed the scope.

Fixed: the exception now checks a separate _open_warmups set. A planned key joins it when it takes its warm-up step, and reset() and seal_lengths() clear it. Only a key warmed earlier in the same render, which is the true-CFG negative caught in its warm-up when the positive replay seals the scope, still gets to record. _warmed keeps its existing job of skipping a second warm-up.

New tests in tests/test_diffusion_cuda_graph.py:

  • test_a_warmup_from_before_a_reset_does_not_reopen_the_sealed_scope: negative warms, reset(), a new first render seals the scope, then the old negative length runs ungraphed (length_eager) with no new capture.
  • test_a_warmup_from_an_earlier_render_does_not_reopen_the_sealed_scope: same check across a render end.

Both fail before this change and pass with it. The original true-CFG test still passes: in a multi-step render the negative warms on one decode step and records on the next.

pre-commit-ci Bot and others added 2 commits October 22, 2026 13:52
…h scope

The sealed-scope exception for planned keys checked _warmed, which
outlives both reset() and the end of a render. A length warmed before new
weights were loaded could then record after the new first render sealed
its scope. Track the warm-ups of the current recording window separately
and clear them on reset() and in seal_lengths().

[skip ci]
The copies are a few MB and the only repro needed a synthetic 2 GiB input
under a 4 GiB cap, so the retry is not worth its code. Restore the plain
step after the copies, as on main, and drop its test.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bec03e27a1

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

if (
scope in getattr(self, "_sealed_scopes", ())
and (self.placement is None or key not in self._recorded)
and not (self.plan is not None and key in self._open_warmups)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clear pending warmups when a render is cancelled or fails

If a true-CFG render is cancelled after the negative key warms but before it captures, _open_warmups survives into the next render: diffusion.py:14224-14225 raises on cancellation, and its exception path rethrows before seal_lengths() at line 14258. The next render can therefore replay the positive graph, seal the scope, and still capture or evict for the previous render's negative length through this exception. Unlike the reset and successful-render cases covered by the new tests, cancelled and failed renders never close this recording window. Clear pending warmups on those exits as well so stale keys cannot bypass the seal.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked this, and I am leaving it as is. A warm-up can only open on a scope that is not sealed yet, since a sealed scope sends a new length to length_eager before the warm-up branch. So a stale entry only survives a cancelled or failed first render. The next render then finishes recording the length that first render started, and only if it uses that same length: it is still the first render that completes, so that is the recording this change exists for.

On the stub, after a first render cancelled with the negative warmed: the next render with the same negative records it (2 captures, length_eager 0). The render after that, with a negative of another length, stays eager (2 captures, length_eager 2, 0 evictions). Clearing the window on cancel would leave that load's negative ungraphed for good, because its key is already in _warmed and never warms again.

@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. Hooray!

Reviewed commit: bec03e27a1

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

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: df918cd83d

ℹ️ 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 c519609 into main Oct 28, 2026
@danielhanchen
danielhanchen deleted the cuda_graph_recheck_fixes branch October 28, 2026 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant