Skip to content

Studio FLUX.3 Klein on T4: run edits, and no slower step CUDA graph over int8 linears - #17949

Merged
danielhanchen merged 6 commits into
mainfrom
studio-flux3-fp16-edit-and-speed
Nov 16, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-flux3-fp16-edit-and-speed

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Summary

Two FLUX.3 Klein problems on GPUs without bfloat16 (T4 class), found running FLUX.3 Klein and Klein Base on a real Colab T4:

  1. Every Klein and Klein Base edit was refused with "needs about 8.50 GB of working memory" on a 15 GB T4. The generate guard priced the tiled VAE decode with the flat per-tile estimate, which charges a 1024 px DiT VAE tile like a U-Net's whole render. The measured peak of the edit is 9.1 GB of the 15 GB card.
  2. Speed Default made FLUX.3 about 25% slower on fp16 cards (42.5 to 43.5 s per image vs 34 s with speed off, same pixels). On those cards the denoiser is not compiled, so Studio wraps the step in a CUDA graph; the native int8 W8A8 linears run as a compiled function that never runs inside a capture, so the captured step baked their slower eager kernels.

Changes

  • diffusion_memory.py: estimate_tiled_image_runtime_mib takes the measured activation row the placement was sized from and caps the flat one-tile VAE figure with the row's measured tiled decode, scaled by tile area. placement_measured_activation(pipe) reads that row from the load's recorded reserve.
  • diffusion.py: the generate guard passes that row; when the load kept no measured reserve on a calibrated placement, it reads the same row the plan used. Only calibrated placements change; unknown keeps the flat estimate.
  • diffusion_speed.py / diffusion_native_quant.py: on fp16 cards, the step CUDA graph is declined over an uncompiled denoiser whose native int8 W8A8 linears are served by the compiled function (fused_w8a8_serves: CUDA, not ROCm, inductor available, not disabled, not failed on that card). bf16 loads keep their graph unchanged. UNSLOTH_DIFFUSION_STEP_GRAPH_NATIVE_W8A8=1 keeps the graph.
  • The measured tiled decode is scaled against the tile side each family's row was measured with (512 px for Qwen-Image-2.1, read from the family token of the load's variant hint), and only lowers the flat estimate, never raises it.

Results

Real Colab T4, FLUX.3 Klein int8, 1024 x 1024:

main this PR
Klein edit refused ("needs about 8.50 GB") renders, about 96 s
Klein Base edit refused renders, about 130 s
Klein text-to-image, Speed Default 42.5 to 43.5 s / image 35.5 to 38.1 s, identical pixels
Klein Base, Speed Default, 12 steps 68 s 54 s

bf16 card (Colab RTX PRO 6000), main and this PR alternated twice: text-to-image is bit-identical, 1.45 s per image both ways, the same 3 compiled graphs and no recompiles; edit differs by LPIPS 0.0011 to 0.0013, the same as two main runs differ from each other (0.0011), 3.24 s both ways. A second bf16 card (A100): all four images (two text-to-image, two edits) byte-identical between main and this PR, 2.54 vs 2.55 s per image.

A100 with bf16 disabled (Studio sees a T4), Klein 1024, 16 steps: Speed Off 3.79 s, Speed Default on main 5.77 s, Speed Default / Max with this PR 3.70 / 3.71 s, identical pixels across all four.

Guard matrix

570,240 guard inputs (11 families x sizes x batch x free memory x tile side x slicing x references x ControlNet x speed tier x compute width x calibrated or not), main vs this branch in separate processes: 4,190 cells move from refuse to tile, none the other way; uncalibrated placements and uncalibrated families never change; no family is priced below its own measured tiled decode.

Tests

  • New tests/test_diffusion_measured_tiled_decode_guard.py: the FLUX.3 edit on a T4 budget is admitted with the measured row and still refused when it truly does not fit; non-calibrated placements keep the flat estimate.
  • New tests/test_diffusion_step_graph_native_w8a8.py: the graph is declined only for an uncompiled denoiser with served W8A8 linears; compiled denoisers, ROCm, disabled or failed fused W8A8, and the env override keep it.
  • Both files fail on main and pass here (74 tests); related diffusion memory and speed suites pass with the same 3 pre-existing failures as main.

Not covered

  • No AMD or Windows GPU run: the step-graph change is gated off on ROCm, and the memory change is device neutral.
  • T4 speed figures are mostly single runs; T4 renders spread up to about 3 s run to run.

…he measured row, so a FLUX.3 Klein edit runs on a T4

On a T4 (fp16) FLUX.3 Klein loads int8 with the transformer resident from the
measured activation row. A reference edit at the default 1248x832 is conditioned,
so the generate guard skips the default-resolution exemption and the tiled
estimate charged the one 1024 px VAE tile at the flat 8192 MiB per megapixel
(6.5 GiB + 2 GiB overhead vs 5.7 GiB usable) and refused an edit that runs at
8.6 GiB allocated on the 15 GiB card. The guard now caps that tile at the
family's measured tiled decode (same speed tier and compute width the load
placed from, read from _unsloth_measured_reserve). It never prices higher than
before; uncalibrated placements and families without a measured row are
unchanged.
… when the load kept no measured reserve

FLUX.3 Klein Base on a T4 places from the measured row but records no
_unsloth_measured_reserve (no phase seed), so its 1248x832 edit still met the flat
VAE tile price and was refused. The generate guard now reads the same row the plan
read (_calibrated_activation with the fp32 scaling the load applies) when the
placement is calibrated and no reserve exists.
…ative int8 linears run compiled

The compiled W8A8 function (rotate, quantise, int_mm, rescale in one inductor
graph) never runs inside a CUDA-graph capture, so a captured step bakes the eager
kernels instead. On fp16 cards the whole-model compile is declined for FLUX.3, so
Speed default / max captured the step over the eager path: a T4 rendered FLUX.3
Klein int8 at 42.5 s per image against 34 s at Speed off, same pixels. The step
graph is now declined there (status says why);
UNSLOTH_DIFFUSION_STEP_GRAPH_NATIVE_W8A8=1 keeps it. A compiled denoiser and
loads without native act-int8 linears are unchanged.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 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-16T09:55:53.720427Z e322951 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: 55e56a376c

ℹ️ 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 +9370 to +9371
tile_scale = max(0.25, min(w, side) * min(h, side) * vae_batch / megapixel)
vae = min(vae, max(1024, int(measured.tiled_decode_mib * tile_scale)))

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 Normalize decode scaling to the family's measured tile size

For Qwen-Image-2.1, the calibration table explicitly records tiled decode using 512 px tiles, but this calculation scales that already-tiled measurement by another 512²/1024². For example, a 1152×1024 request with 512 px tiles now estimates only 1152 MiB of tiled runtime, below the row's 2036 MiB measured decode even though the output is larger than the calibration canvas and the tile size is unchanged. Previously this request was priced at 2048 MiB. This can admit requests under tight memory budgets that the guard previously refused. Scale relative to the tile dimensions used for each family's calibration rather than assuming every measured decode used a 1024 px tile.

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 ebd98da: the measured tiled decode now scales against the tile side each family's row was measured with (512 px for Qwen-Image-2.1, the default otherwise), so a 512 px Qwen tile is priced at the measured row, not a quarter of it. New test test_qwen_tiles_are_priced_against_the_512_px_tile_they_were_measured_with fails before and passes after.

Comment on lines +465 to +466
if _FUSED_STATE["failed"] is not None or int8_fused_disabled():
return 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.

P2 Badge Check device-specific fused failures before declining graphs

After a fused W8A8 compilation or execution failure on a CUDA card, _fused_w8a8_failed() records the failure in _FUSED_STATE['devices'], not _FUSED_STATE['failed']. This helper only checks the latter, so subsequent model loads on that card still report that fused W8A8 serves the denoiser and disable its step graph, while _fused_w8a8() permanently returns None for that device. Those loads therefore lose graph acceleration without receiving the compiled-linear acceleration that justified disabling it. Include the relevant device's failure state in this eligibility check.

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 ebd98da: fused_w8a8_serves now reads the per-card failure in _FUSED_STATE['devices'] for the cards the native int8 linears sit on (the current card for offloaded weights); a card whose compiled linear failed keeps its step graph. New test test_a_card_whose_compiled_linear_failed_keeps_its_graph fails before and passes after.

…ed tile side; a card whose compiled W8A8 failed keeps its step graph

[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. You're on a roll.

Reviewed commit: ebd98da0fa

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

…ured tile side; decline the step graph over eager W8A8 on fp16 cards only

[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. Nice work!

Reviewed commit: e322951d49

ℹ️ 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 584edff into main Nov 16, 2026
@danielhanchen
danielhanchen deleted the studio-flux3-fp16-edit-and-speed branch November 16, 2026 10:58
danielhanchen added a commit that referenced this pull request Nov 16, 2026
…fusion red on main) (#17974)

## Summary

`(Python 3.13, diffusion)` is red on main (seen on the staging runs of
#17966 and #17971):

```
studio/backend/tests/test_diffusion_flux3_fp32_calibrated.py::test_only_the_load_plan_reads_the_scaled_table
E   assert 2 == 1
```

The test pinned that the scaled (float32) activation row,
`_calibrated_activation(..., fp32_scaled = True)`, is read in exactly
one place in `DiffusionBackend`: the load plan. #17949 (FLUX.3 Klein on
T4) added a second read in the generation guard:

```python
guard_measured = placement_measured_activation(state.pipe)
if guard_measured is None and getattr(state, "calibrated_placement", False):
    guard_measured = _calibrated_activation(fam, guard_target, fp32_scaled = True)
```

That read re-uses the same row the calibrated placement was sized from,
so the guard and the plan agree. It is intended, and it keeps what the
test protects (an uncalibrated or post-load reader never sees the scaled
row). The string count was just too blunt.

## Change

Test only, `test_only_the_load_plan_reads_the_scaled_table`, now walks
the AST instead of counting a substring:

- exactly two `fp32_scaled=True` reads in `DiffusionBackend`;
- one is the load plan's `calibrated_activation=` argument (still pinned
verbatim);
- the other must sit under an `if` whose test checks
`calibrated_placement`.

## Testing

- `test_diffusion_flux3_fp32_calibrated.py`: 10 passed. On main the test
fails as above.
- Sabotage: dropping the `calibrated_placement` condition from the guard
fails it; adding a third scaled read fails it.
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