Skip to content

Studio diffusion: the INT8 attention sink path repeats a seed from the first render - #18532

Merged
danielhanchen merged 9 commits into
mainfrom
studio-int8-sink-first-render
Nov 20, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
studio-int8-sink-first-render

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary

On the INT8 attention sink route (FLUX.3, FLUX.2, Z-Image, Qwen-Image-2.1, HunyuanVideo-1.5), a launch plan runs the sink path while it learns over its first 32 calls whether any head clipped V (an attention sink), and races at its 48th call. When a head clipped and the race picked the kernels without the sink path (Qwen-Image-2.1 on L4 and A100), or torch's attention, every call of the next render ran that winner. So the first render of a load was a different image from the next render of the same seed.

On main this only shows when no thrown-away CUDA graph warm-ups use up those 48 calls first: CUDA graphs off, a LoRA scale in attention_kwargs, or a prefix K/V over 1 GiB. Qwen-Image-2.1 runs 31 INT8 attention calls per step, so its decision landed inside step 2.

Change

The race, its call, its inputs and its scope stay as they were: a sink race verdict is per load, never persisted, so every load races on its own inputs (#18156).

  • Every undecided call before the decision (not only the first 32) records its clipped-key counts, and whether its output was used. A call inside a capture's side-stream warm-ups is thrown away; anything else is used.
  • At the decision, a verdict other than the sink path is taken only where no used call clipped on the sink path. Otherwise the plan keeps the sink path for the load, which is what its earlier calls computed, so the next render repeats the seed. The bucket still keeps the race verdict for later plans of the same load.
  • A torch attention verdict does not move routes (floor raise or stock processor) that another plan of the load already ran used INT8 calls on.
  • Main's default path (CUDA graphs on, side-stream warm-ups) is unchanged by these rules: its learning calls are thrown away.

Evidence

Colab L4, torch 2.13.0+cu130, Qwen-Image-2.1 INT8 (Auto), 1024 px, 8 steps, CUDA graphs off, the same prompt and seed twice per process, one VM:

load 1: render 1 load 1: render 2 load 2 (new process, same Studio home): render 1 render 2
main fede6637 b80aa53a
this branch b2d547c3 b2d547c3 b2d547c3 b2d547c3
  • The second load raced again: its plan carries its own race result, and the Studio home's caches hold no sink verdict entry after either load.

  • Second-render s/step: main 1.352, this branch 1.366 and 1.367 (one sample per arm). Peak allocation is unchanged on the first load (17681 MiB in both arms).

  • With CUDA graphs on (the default), the stacked warm-up PR's tree (which contains this change) renders FLUX.2 klein byte-identical to main (6aff2096) in all 8 processes on the same VM.

  • B200, correctness only (earlier revision with the same rule): a toy 8-block DiT on the real INT8 attention kernels with V sinks on the first tokens, eager and torch.compiled, 8 steps, two renders of the same inputs. Main with graphs off differs between render 1 and render 2 for every verdict; this rule gives torch.equal on every step. With every call treated as a thrown-away warm-up (main's rule) the mismatch comes back. No new dynamo graphs, breaks or recompiles.

  • CPU (-o addopts=): the INT8 attention, CUDA graph and probe cache suites, 595 passed on main and 607 here, no failures on either. test_differential.py against the merge base: DISCRIMINATING, 12 fixed, 0 head failures. New tests cover the kept sink path after a used clip, a bucket verdict of the same load after a used clip, thrown-away clips taking the verdict as before, the route guard, the used/thrown-away mark, every undecided call being checked, and a load that kept the sink path persisting nothing (the next load and process race again).

Not covered

  • HunyuanVideo-1.5's known first-render difference may be the same mechanism; it was not run.
  • Only L4 and B200 ran this rule; A100, T4, G4 and Windows did not.

…rst render

A sink-family route (FLUX.3, FLUX.2, Z-Image, Qwen-Image-2.1,
HunyuanVideo-1.5) runs the sink path while its plan learns over its first
32 calls whether a head clipped V, and races at the 48th. Where a head
clipped and the race picked the kernels without the sink path
(Qwen-Image-2.1 on L4 and A100) or torch's attention, every call of the
next render ran that winner, so the first render of a load was a different
image from the next render of the same seed (Qwen-Image-2.1: LPIPS 0.0027 on
L4) whenever no thrown-away CUDA graph warm-ups had used up the 48 calls:
graphs off, a LoRA scale or a long prefix K/V on the step.

The race and its inputs stay as they were. Each learning check now records
whether its call's output was used (not inside a capture's side-stream
warm-ups). A verdict other than the sink path is taken only where no used
call clipped on the sink path; otherwise the plan keeps the sink path for
the load, which is what its earlier calls computed. The verdict is kept for
the bucket and persisted per card, toolchain, sources, DiT class and
checkpoint, so new plans of the load and later loads take it at their first
clipped call: that call's clip count is read right after its quantisers
(one wait per learning call until the first clip, only where a verdict is
known) and the verdict runs before its attention. An out-of-memory race and
a DiT with no known checkpoint persist nothing, as before.
@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T20:08:28.671479Z 4e44d54 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[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 8d216f6104

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

… arrives mid-learning, and key sink verdicts on every install's checkpoint
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…r plan of the load already ran INT8 calls on
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 2303c92744

ℹ️ 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 Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 4f4296fa46

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

…e sink decision, not only the learning window

[skip ci]
The first-render rule stays: a plan whose used calls clipped on the sink
path keeps it for the load, so the same seed repeats. Sink race verdicts
are no longer written to or read from the probe cache, so every load races
on its own inputs again. Drops the per-checkpoint key, the early clip read
that applied a persisted verdict at the first clipped call, and their tests.

[skip ci]
@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: b1feb2da89

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

Comment on lines +1020 to +1023
if verdict is not True and plan.get("live_seen"):
# a render already used this plan's sink-path outputs where V clipped: keep the path for the
# load (the next render repeats the seed); the bucket keeps the verdict for later plans
verdict = True

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 Preserve the race's rejection of non-finite sink outputs

When a used call has clipped and the deciding call produces a non-finite sink-path error, _pick_path explicitly rejects that path in favor of plain INT8 or native attention. This unconditional override selects the rejected sink path anyway and pins it for the load, allowing subsequent renders to continue producing invalid outputs instead of taking the existing numerical-safety fallback. This also contradicts the behavior covered by test_the_sink_race_never_takes_a_path_with_a_non_finite_error. Distinguish numerical rejection from a performance verdict before applying the reproducibility override.

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.

Fixed in the latest commit. The race now records a bucket whose sink-path error was non-finite, and the repeatability override applies only when the sink path was not rejected on accuracy, both for the plan that raced and for later plans of the same bucket within the load. forget_plans() clears the record. New test test_a_sink_path_rejected_on_accuracy_is_never_kept_for_repeatability covers both cases and fails with the guard removed.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 4e44d54d0d

ℹ️ 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 added a commit that referenced this pull request Nov 20, 2026
…rming layers (#18688)

## Summary

`Repo tests (CPU, rest)` fails intermittently on current `main`. It is
seen on #18650 and #18532, both based on recent `main`, and both runs
include #18607 and #18641:

```
FAILED tests/test_gc_skip_replay_mlp_down.py::test_only_the_post_norm_free_layer_arms
E   AssertionError: assert ['LlamaDecode...e_at_514_178'] == ['LlamaDecode...fast_forward']
E     Left contains one more item: '__resume_at_514_178'
```

## Cause

#18607 added this test. It collects every module-level function in
`unsloth.models.{llama, gemma, ...}` whose source calls
`arm_mlp_output_unread(` and expects exactly
`LlamaDecoderLayer_fast_forward`.

When an earlier test in the same xdist worker compiles that layer and
hits a graph break, Dynamo parks the continuation function
(`__resume_at_<offset>_<n>`) in the compiled function's module globals.
That puts it in `unsloth.models.llama`. Its code object points at
`LlamaDecoderLayer_fast_forward`'s lines, so `inspect.getsource` returns
the same source, and it counts as a second arming layer. The test passes
alone and fails only when such a compile ran first in the worker. The
product code is right; the test's view of the module namespace was not.

## Change

`tests/test_gc_skip_replay_mlp_down.py`: only count a function when its
code name matches the name it is bound under (`fn.__code__.co_name ==
fn_name`). Dynamo's resume functions are bound as `__resume_at_*` but
named `torch_dynamo_resume_in_*`, so they drop out. Every function
defined in the module with `def` still matches.

## Testing

- `tests/test_gc_skip_replay_mlp_down.py`: 18 passed, 6 skipped (the six
need CUDA).
- Reproduced the CI failure with a conftest that plants a resume
function exactly as Dynamo does: same code with `co_name =
"torch_dynamo_resume_in_LlamaDecoderLayer_fast_forward_at_514"`, bound
as `__resume_at_514_178` in `unsloth.models.llama`. The test from `main`
fails with `Left contains one more item: '__resume_at_514_178'`; this
version passes.
- The guard still does its job: planting a real second arming function
in `unsloth.models.gemma` still fails it (`Left contains one more item:
'GemmaDecoderLayer_arming'`).
@danielhanchen
danielhanchen merged commit 49343ca into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the studio-int8-sink-first-render branch November 20, 2026 21:59
danielhanchen added a commit that referenced this pull request Nov 20, 2026
## Summary

Stacked on #18532 (the INT8 attention sink path repeats a seed from the
first render); merge that first.

Today a resident denoiser's step CUDA graph runs three warm-up forwards
on a side stream for each graph key, on the first render of every load,
and throws them away before recording. With this change the key's first
three real steps are the warm-ups: they run eagerly on copies laid out
like the capture statics, and the fourth step records. The first render
of a load runs three fewer denoiser forwards per graph key.
`UNSLOTH_DIFFUSION_GRAPH_REAL_WARMUP=0` keeps the side-stream warm-ups.

Real-step warm-ups are used only where the key is sure to reach its
recording step inside the render: Studio passes the render's denoise
steps (after img2img strength) to the graph handle before the pipeline
call, and a key takes real warm-ups only when

- the render has more than 3 steps,
- the pipeline has one denoiser (experts that split the steps keep the
side-stream path), and
- the key follows no step-dependent branch (NVFP4 per-step precision, a
pre-hook's branch flag), since such a key may appear on only 1 to 3
steps.

Every other key records on its first call with side-stream warm-ups,
exactly as on main. So SDXL-Turbo's 1-step default, any 1 to 3 step
render, and low-strength img2img or inpaint replay from their first
render as before. A caller that names no step count (anything outside
Studio's render path) also gets main's behaviour.

Before its first real warm-up, a key does what the capture does before
its warm-ups: its NVFP4 GEMMs are tuned at the step's row counts, and
NVFP4 activation scales that are still calibrating go to the capture's
refusal (eager for the load), as before.

## Evidence

Colab L4, torch 2.13.0+cu130, one VM per run, base = merge base
`0b278200d2`, head = this stack, the same prompt and seed for every
render of a process.

FLUX.2 klein (4 steps, so real warm-ups apply), INT8 by Auto, 1024 px,
processes m h | h m m h h m (the first two cold, then race files shared
and warm restarts):

| | first-render denoise, warm restarts (median of 3, range) | cold
first render | second-render denoise (median) | graph engagement, render
1 | images |
|---|---|---|---|---|---|
| base | 4.777 s (4.770-4.784) | 10.63 s | 0.793 s | 1 capture, 4
replays | `6aff2096` in all 4 |
| head | 3.739 s (3.713-3.780), -21.7% | 10.16 s | 0.782 s | 3 real
warm-ups, 1 capture, 1 replay | `6aff2096` in all 4 |

From render 2 on both arms replay all 4 steps; 0 graph fallbacks.

SDXL-Turbo, Studio's 1-step default, 512 px, default (deferred) speed
profile: graphs engage once the background compile finishes
(`UNSLOTH_DIFFUSION_BG_COMPILE_YIELD=0` in every arm, so image 4 waits
for it), so images 4, 5 and 6 of the process are the first three graphed
renders. Replays per render:

| | renders 1-3 after the graphs engage |
|---|---|
| base | 1, 1, 1 (recorded on the first call) |
| this PR's previous head | 0, 1, 1 (one real warm-up, then a
side-stream recording) |
| head | 1, 1, 1 (recorded on the first call, 0 real warm-ups) |

All 6 renders of each process are byte-identical across the three trees.

The earlier L4 run of the same real warm-up path (median of 3 warm
restarts, first-render denoise): Qwen-Image-2.1 12.72 -> 10.30 s,
Z-Image-Turbo 9.92 -> 7.89 s, MOSS-Image 16.82 -> 13.20 s, FLUX.3 klein
about equal; second renders and images unchanged (MOSS-Image varies VM
to VM on main itself). All of these run 8 or more steps on one denoiser,
so the step-count rule leaves them on real warm-ups.

- B200, correctness only: Studio's MOSS transformer (tiny random config,
compiled, bf16, true CFG with two graph keys, 12 steps, two renders) is
bit-identical to the side-stream path on all 24 calls; a toy DiT on the
real INT8 attention kernels is `torch.equal` with 16 executed forwards
instead of 19.
- CPU: the CUDA graph, offload graph, block graph and INT8 attention
suites pass on main (595), on #18532 (607) and here (635), no failures.
`test_differential.py` against #18532's head is API only (every new test
names the new switch or `note_render_steps`); reverting the switch alone
fails the 11 real warm-up tests, and removing only the step-count gate
fails the 10 tests that pin main's behaviour for 1 to 3 step renders,
branch keys, several denoisers and a caller with no step count. Against
this PR's previous head, the no-step-count 1-step test is FIXED (replays
per render `[0, 1, 1, 1, 1]` before, `[1, 1, 1, 1, 1]` now, as main).

## Not covered

- Only FLUX.2 klein and SDXL-Turbo ran on this head; Qwen-Image-2.1,
Z-Image-Turbo, MOSS-Image and FLUX.3 klein numbers are from the previous
head, whose path is unchanged for them. A100, T4, G4, a LoRA change,
low-strength img2img on GPU and Windows did not run.
- A key that is not a branch key but first appears in the last 3 steps
of a render (for example a short second stage inside one pipeline call;
no current Studio pipeline was found doing this) still records on the
next render's first call, not on its own first call.
- Video families (graphs opt-in) render through a path that passes no
step count, so they keep main's side-stream warm-ups.
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