Skip to content

Studio diffusion: hold cudnn.benchmark off for FLUX.2 klein so a seed decodes the same image in every process - #14770

Merged
danielhanchen merged 6 commits into
mainfrom
studio-diffusion-offload-cfg-determinism
Oct 23, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-diffusion-offload-cfg-determinism

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Summary

FLUX.2 klein (4B, 9B, base 4B, base 9B), FLUX.1 Kontext, Qwen-Image-Edit and Qwen-Image-Layered now hold cudnn.benchmark off. FLUX.2-dev, FLUX.1 and Qwen-Image already do for the same VAEs. With the benchmark on, every Studio process times the VAE convolutions once and can settle on a different algorithm, so one seed renders different pixels depending on which process served it.

What was happening

An offloaded FLUX.2-klein-base-4B render with CFG batching forced gave two different image digests on two runs of the same tree. CFG batching and the offload prefetch path were the first suspects. They are not the cause:

  • The latent handed to vae.decode was bitwise equal in all 12 fresh processes (CFG batching forced, CFG batching off, cudnn benchmark on and off). The CFG batched step is bit-exact against the plain pair for this load.
  • Images from those same 12 processes: with the benchmark on, 9 processes gave 6 distinct digests (CFG on and off alike). With it off (monkeypatched), all 3 matched.
  • Inside a single process every repeat was identical, so the split happens once per process. That matches a benchmark pick, not a race.

A VAE-only check (one fixed latent, 5 fresh processes per arm, Studio's own channels-last and fused-norm VAE layer):

cudnn.benchmark fused VAE norms distinct outputs / 5 processes
on on 3
on off 4
off on 1
off off 1

Other families on the same VAEs

Same check for the VAEs loaded by FLUX.1 Kontext (FLUX.1 AutoencoderKL), Qwen-Image-Edit 2511 (AutoencoderKLQwenImage) and Qwen-Image-Layered (the RGBA AutoencoderKLQwenImage): one fixed latent, 5 fresh processes per arm.

VAE (family) benchmark on, 768 px benchmark on, 1024 px benchmark off, 768 and 1024 px
FLUX.1 (flux.1-kontext) 4 / 5 1 / 5 1 / 5
Qwen-Image (qwen-image-edit) 2 / 5 2 / 5 1 / 5
Qwen-Image RGBA (qwen-image-layered) 2 / 5 1 / 5 1 / 5

(distinct outputs over 5 processes)

Change

  • diffusion_families.py: cudnn_benchmark = False on flux.2-klein, flux.1-kontext, qwen-image-edit and qwen-image-layered.
  • Tests: test_split_vae_families_hold_cudnn_benchmark_off checks that every repo whose VAE was shown to split (FLUX.2-dev, the klein variants, Kontext, Edit 2511, Layered, plus the unsloth mirrors) resolves to its family with the benchmark off. On main it fails for every repo except FLUX.2-dev, and it passes here. The opt-out set test, the family data freeze fixture and the autoopt parity fixture are updated. The parity fixture diff only drops cudnn_benchmark from applied for the 96 CUDA cases of these four families, which matches a live base-vs-head snapshot exactly.

Cost

The transformer has no convolutions, so s/step is unchanged. Only the VAE decode moves. Warm decode on an exclusive B200, 3 fresh processes per arm, two interleaved arms each (median of 10 decodes, untiled):

latent benchmark on benchmark off
768x768 9.9 to 12.4 ms (median 10.4) 10.2 to 11.8 ms (median 10.8)
1024x1024 15.8 to 16.5 ms (median 15.9) 17.1 to 18.2 ms (median 17.8)
VAE at 1024x1024, 5 processes per arm benchmark on benchmark off
FLUX.1 (Kontext) 15.2 to 16.1 ms (median 15.3) 17.0 to 17.4 ms (median 17.1)
Qwen-Image (Edit) 8.9 to 9.1 ms (median 9.0) 14.7 to 15.0 ms (median 14.8)
Qwen-Image RGBA (Layered) 8.9 to 10.1 ms (median 9.0) 14.7 to 14.9 ms (median 14.7)

For FLUX.2 that is about 2 ms per 1024 px image, under 0.1% of a guided base render and under 0.5% of a 4-step distilled one. FLUX.2-dev already pays the same trade. The Qwen VAEs lose about 6 ms per 1024 px decode, which is small next to a 20B DiT edit render and the same cost Qwen-Image already pays.

Testing

  • pytest tests/test_diffusion_speed.py tests/test_diffusion_family_data_freeze.py tests/test_diffusion_autoopt_core.py tests/test_diffusion_autoopt_parity.py tests/test_diffusion_qwenimage21_vae_layout.py: pass.
  • I also ran every backend test file that mentions cudnn, on main and on this branch. Both fail the same set: test_native_audio (missing optional modules) and test_mirrored_base_still_trips_the_flux2_shape_guard. Nothing new fails.
  • Offloaded FLUX.2-klein-base-4B with Studio defaults (CFG batching on auto), 3 fresh processes on this branch: 3 of 3 identical. Z-Image-Turbo with guidance 4 (offloaded, defaults), 3 fresh processes: 3 of 3 identical, unchanged by this PR.

…atch

FLUX.2 klein decodes with the same VAE as FLUX.2-dev, which already holds
cudnn.benchmark off. With it on, each process times the VAE convs once and
can pick another algorithm, so the same seed decodes to different images in
different Studio processes. On B200 (FLUX.2-klein-base-4B, 768x768, 12
steps, CFG 4, offloaded) the denoised latent was bitwise equal in all 12
fresh processes, CFG batching on or off, but 9 processes with the benchmark
on gave 6 distinct images. With it off, 6 of 6 matched. The transformer has
no convolutions, so only the VAE decode changes: warm decode 10.4 -> 10.8 ms
at 768 and 15.9 -> 17.8 ms at 1024 on an exclusive B200.
@danielhanchen danielhanchen self-assigned this Oct 22, 2026
…and Qwen-Image-Layered

These families decode with VAEs whose cudnn.benchmark pick already split
renders for flux.1 and qwen-image, but they kept the benchmark on. Decoding
one fixed latent with Studio's VAE layer in 5 fresh B200 processes, with the
benchmark on, gave 4 images for the FLUX.1 VAE (Kontext), 2 for the
Qwen-Image VAE (Edit 2511) and 2 for the RGBA Qwen-Image VAE (Layered) at
768 px. With it off, each gave 1 image in 5 processes at both 768 and
1024 px. Warm 1024 px decode on an exclusive B200: Kontext 15.3 -> 17.1 ms,
Edit 8.95 -> 14.7 ms, Layered 8.96 -> 14.7 ms.

The FLUX.2 test now covers every family whose VAE was shown to split.

[skip ci]
@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-22T23:35:01.890037Z b62ccc2 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.

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

filter_reduction_configs_archs = ((10, 0),),
# Same VAE as FLUX.2-dev: cudnn.benchmark's per-process conv pick decoded one latent into 6 images over 9 fresh
# B200 processes (base 4B, 12 steps, CFG 4); with it off all 3 matched, the denoised latent equal in all 12.
cudnn_benchmark = False,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Actually hold cuDNN benchmarking off during VAE decode

When torch.backends.cudnn.benchmark is already true—such as when a co-resident optimized pipeline enabled this process-global flag—this field does not make it false: apply_speed_optims merely skips _enable_cudnn_benchmark, and _cudnn_off_post only disables it when applied["cudnn_benchmark"] is true, which cannot happen for an opted-out family. A later co-resident load can likewise re-enable it before decode. Thus these newly opted-out families can still produce the per-process image variants this change is intended to prevent; the VAE decode needs a scoped disable or equivalent enforcement rather than only suppressing this load's enable call.

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.

Agreed, the family flag only skipped this load's enable. Unloading restores the flag snapshot, so a sequential model switch was already fine, but a pipeline co-resident in the same process that had set the flag would leave an opted-out family's VAE on a per-process conv pick. Renders of an opted-out family now run inside cudnn_benchmark_scope(False), wrapped around the whole pipeline call, encode and decode included. A write from another load during the render goes to the scope's saved value instead of the live flag, and the prior value comes back when the render ends. New tests: the render of an opted-out family enters the scope around the pipeline call, and the scope forces the flag off when it was already on, leaves benchmark-keeping families alone and does nothing off CUDA.

FLUX.1 already pins inductor's reduction-config filter on sm80, sm89 and
sm120, where cold-cache servers benchmarked near-tied norm reduction
configs per process and rendered other images for one seed. B200 was left
out as measured deterministic, but FLUX.1-schnell int8 under a 12 GiB cap
with Studio defaults and a fresh inductor cache denoised one seed into 3
different latents over 15 fresh B200 processes (8, 5 and 2). With the filter
engaged all 12 processes matched bitwise. cudnn.benchmark is already off
for FLUX.1, and its VAE decode is stable across processes without it.

Kontext runs the same DiT, so it pins on B200 too.
danielhanchen added a commit that referenced this pull request Oct 22, 2026
pre-commit.ci fails `ruff-format-with-kwargs` on every open PR (#14806,
#14805, #14803, #14790, #14785, #14781, #14780, #14777, #14776, #14775,
#14774, #14773, #14772, #14770, #14769, #14768, and others): "files were
modified by this hook". pre-commit.ci runs the hooks on all files, and
20 files on main (26 by the time this lands) are not in the formatter's
output, so every PR is reformatted outside its own diff. Where
pre-commit.ci can push, its autofix commit drags those unrelated files
into the PR (for example #14790's touches `tests/test_block_compile.py`,
`tests/test_bnb_integration_compile.py` and `unsloth/models/_utils.py`).

The drift came in with PRs merged today, among them #14702, #14751,
#14759, #14784, #14786, #14787, #14788, #14791 and the FP8 block decode,
GRPO routing replay, 4-bit decode, compiled blocks and MoE LoRA target
changes.

### Change

`python scripts/run_ruff_format.py` (the hook's own entry point,
`ruff==0.6.9` as pinned in `.pre-commit-config.yaml`) applied to those
20 files, plus 6 more from #14739, #14779 and #14806, which merged while
this was open. Formatting only.

### Verification

- Every changed file parses to an identical AST before and after.
- A second run of the hook over the same files changes nothing (fixed
point), and over all tracked Python files on this branch it changes
nothing.
- `ruff check` passes on the changed files;
`tests/test_formatter_fixed_point.py` passes (16).

Open PRs pick this up on their next pre-commit.ci run after merging
main.
…milies

cudnn.benchmark is process-wide. A family with cudnn_benchmark = False only
skipped turning it on at load, so a pipeline that was co-resident in the same
process and had set it left the VAE convs on a per-process pick anyway.
Sequential loads were fine (unload restores the snapshot), but a co-resident
one was not. The render of an opted-out family now runs inside
cudnn_benchmark_scope(False), which also keeps a write from another load in
the meantime from reaching the flag, and the previous value comes back after
the render.

[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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: b62ccc2be6

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

# Conflicts:
#	studio/backend/tests/test_diffusion_backend.py
@danielhanchen
danielhanchen merged commit 5c6673a into main Oct 23, 2026
@danielhanchen
danielhanchen deleted the studio-diffusion-offload-cfg-determinism branch October 23, 2026 01:41
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