Skip to content

Studio diffusion: TF32 matmuls on the default speed tier, not only max - #16762

Merged
danielhanchen merged 4 commits into
mainfrom
studio-diffusion-tf32-default-tier
Nov 5, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-diffusion-tf32-default-tier

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

The default speed tier now enables TF32 matmuls on CUDA. Until now only max did.

  • cuDNN TF32 is already torch's own default. The only new thing is torch.backends.cuda.matmul.allow_tf32 = True on the default tier.
  • It touches fp32 matmuls only, such as an fp32 VAE decode or fp32 embedders. bf16 and fp16 compute is unchanged.
  • off and eager keep strict fp32. Nothing changes on the bit-exact tier.
  • The setting is process-global, as it already is for max. snapshot_backend_flags / restore_backend_flags already restore it on unload.
  • UNSLOTH_DIFFUSION_TF32=0 keeps strict fp32 matmuls on both compiled tiers. The compile cache keys on that switch.
  • The QKV fuse, max-autotune and the compiled VAE decode stay max-only. See below for why.

Evidence (A100-SXM4-40GB, Colab, torch 2.14.1)

The test decodes a 128x128 latent (1024x1024 output) with stabilityai/sdxl-vae in fp32. Each arm was timed in 5 interleaved rounds of 10 decodes with the arm order rotated, and strict fp32 was timed twice for A/A.

arm median ms vs strict
strict fp32 matmul (current default tier) 223.79 -
TF32 matmul (this PR) 222.55 0.56% faster
strict fp32, A/A repeat 223.69 0.04% spread
comparison LPIPS (alex) max pixel diff (8-bit)
TF32 vs strict fp32 matmul 0.00000046 1
strict fp32 matmul vs fully strict (cuDNN TF32 off too) 0.0000016 2

The gain is small, because a bf16 model step does not use fp32 matmuls. The output change is below what torch's own default cuDNN TF32 already causes, so this falls under the "faster or equal, fp-order level" rule.

Also measured in the same run, as the reason it stays max-only: the DiT QKV fuse on the default tier. On a FLUX.1-shaped model (2 double + 4 single blocks, regional compile, bf16), the fused projections ran 36.33 vs 36.58 ms (0.7%, inside the per-round spread). For DiTs the unfused projections are kept beside the fused ones, which costs +453 MB peak on just those 6 blocks. No change for it.

Tests

  • test_speed_default_dense_falls_back_to_regional_compile now expects TF32 on the default tier.
  • New: test_the_tf32_kill_switch (default and max) and test_the_eager_tier_leaves_tf32_off.
  • The compile-cache switch test covers UNSLOTH_DIFFUSION_TF32 automatically.
  • fixtures/diffusion_autoopt_parity.json: tf32 is added to the applied set of exactly the 188 default-tier CUDA cases, and nothing else in the fixture changes. Both parity tests pass.
  • Base vs head over test_diffusion_speed.py, test_diffusion_autoopt* and test_diffusion_compile_cache*:
    • head: 423 passed.
    • base with the head tests: 4 failed. These are the two parity tests, the updated default-tier test, and the kill switch on max.

Not covered here

  • Only an A100 was timed. L4, H100 and Blackwell were not.
  • No end-to-end Studio render timing. The VAE decode above is the main fp32 matmul consumer on CUDA loads.

The default (compiled) tier now sets matmul TF32 on CUDA like max does; cuDNN TF32 is
already torch's default. It only touches fp32 matmuls (bf16 / fp16 compute is unchanged):
on an A100 fp32 SDXL VAE decode it is 0.56% faster (223.8 -> 222.6 ms, A/A 0.04%) at
LPIPS 5e-7 and at most 1/255 per pixel. The off and eager tiers keep strict fp32.

UNSLOTH_DIFFUSION_TF32=0 keeps strict fp32 matmuls on both compiled tiers; the compile
cache keys on it. The autoopt parity fixture gains tf32 on the 188 default-tier CUDA cases.
@danielhanchen danielhanchen self-assigned this Nov 5, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 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-11-05T10:50:50.893666Z bd9fa74 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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: bd9fa7498a

ℹ️ 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 7d0c095 into main Nov 5, 2026
@danielhanchen
danielhanchen deleted the studio-diffusion-tf32-default-tier branch November 5, 2026 17:38
danielhanchen added a commit that referenced this pull request Nov 5, 2026
…er rows (#16838)

## Summary

`seam-guard` (`studio/backend/tests/test_diffusion_autoopt_parity.py`)
fails on main and on every PR that runs it, for example the staging runs
for #16711 and #16799:

```
FAILED tests/test_diffusion_autoopt_parity.py::test_autoopt_resolves_the_same_optimizations_as_before
FAILED tests/test_diffusion_autoopt_parity.py::test_legacy_sequence_still_matches_fixture
E   AssertionError: {'image|chroma|default|bfloat16|offload=0|gguf=0': {'before': {'applied': [['compiled', 'cuda_graph', 'cudnn_benchmark', 'fp16_accum']], ...
```

Bisected on main: `7d0c0952a0` (#16762, "TF32 matmuls on the default
speed tier, not only max") is the first failing commit; its parent
passes. #16762 added `tf32` to the default tier and regenerated
`fixtures/diffusion_autoopt_parity.json`, but its branch predates
#16710, which added the Anima and Chroma families. Those 16 rows (2
families x 2 dtypes x offload x GGUF on the default tier) kept their
pre-#16762 values, so both parity tests now see them change. The code is
right; the fixture is stale for the two families that landed in between.

## Change

`studio/backend/tests/fixtures/diffusion_autoopt_parity.json` only. The
16 `image|anima|default|...` and `image|chroma|default|...` rows are
rewritten from the current resolver, in the file's existing
one-row-per-line format. In every one of them the only change is `tf32`
appended to `applied`, the same change #16762 made to the other
families' default-tier rows. No other row moves, and both
`autoopt_entry` and `legacy_entry` produce the new rows.

## Testing

- `tests/test_diffusion_autoopt_parity.py` and
`tests/test_diffusion_speed.py`: main 2 failed; this branch 236 passed.
- `7d0c0952a0~1`: the two parity tests pass; `7d0c0952a0`: they fail,
which is the bisect above.
- A script check over the diff: 16 rows changed, and in each the only
difference is `tf32` added to `applied`.
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