Skip to content

Studio diffusion: arm FLUX.1 prompt-length dynamic shapes so int8 does not recompile on a long prompt or new size - #14689

Merged
danielhanchen merged 1 commit into
mainfrom
imgfix-flux1-dynamic-text
Oct 22, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
imgfix-flux1-dynamic-text

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On the default speed tier FLUX.1 (schnell / dev / Krea-dev, and Kontext, which uses the same FluxTransformer2DModel) recompiles both repeated blocks on follow-up prompts:

  • the first prompt longer than 256 T5 tokens (Studio pads T5 to 256, or 512 for a longer prompt), and
  • the first render at a new resolution.

The default load picks int8 for FLUX.1, and a torchao denoiser compiles with automatic dynamic (compile_dynamic returns None), so the first compile is static and the first new text length and the first new image size each pay a generalising recompile. #14387 classified FLUX.1's prompt-length inputs in PROMPT_LENGTH_POLICY but left them out of _FAMILY_SOURCES ("a new length there can still recompile. That is the dynamic-text compile change, not this PR"). This is that change for FLUX.1: its block inputs are armed from the first forward, as FLUX.3 and Qwen-Image already are.

Measured (real weights, unsloth/FLUX.1-schnell, default settings, one process, B200, torch 2.13)

Follow-up sequence on the Images defaults (speed unset, memory auto, which resolves to int8 + regional compile + CUDA graphs):

render main this PR
first render (compile + capture) 2 graphs, 1 capture 2 graphs, 1 capture
repeat, seed change, guidance change 0 / 0 0 / 0
prompt past 256 T5 tokens 2 recompiles, 31.6 s 0 recompiles, 3.0 s, same image bytes as main
new resolution 768x1280 2 recompiles + 1 capture, 60.3 s 0 recompiles + 1 capture (new size, by design), 23.4 s, same image bytes as main
batch 2 2 recompiles + 1 capture (batch 1 is specialised) unchanged (batch size is not a prompt-length input)

Recompile reasons on main: tensor 'image_rotary_emb[0]' size mismatch at index 0. expected 4352, actual 4608 (text 256 to 512) and tensor 'hidden_states' size mismatch at index 1. expected 4096, actual 3840 (new resolution).

Images: the long-prompt, new-resolution and batch-2 renders are byte-identical to main's (main ran the same generalised graph after recompiling). The first render now runs that dynamic graph from the start instead of main's static first graph: against the speed-off bf16 render of the same seed, LPIPS-alex 0.054 here vs 0.035 on main (one image; main's own long-prompt render is 0.119 and its new-resolution render 0.082 against bf16, so this is inside the int8 default's spread). Renders whose prompt length matches the first one differ from main's by LPIPS 0.01-0.03 on simple scenes and up to 0.22 on a dense night-market scene, same composition (kernel numerics of the static vs dynamic graph).

The host was heavily loaded (load average above 1000), so the wall times above are what one user saw on that host, not a clean benchmark; the recompile and capture counts are the result.

Changes

  • diffusion_dynamic_text._FAMILY_SOURCES: add FluxTransformer2DModel: _FLUX1_SOURCES (the sources Studio diffusion: record a prompt length's CUDA graph on the first render only #14387 already defined). Only automatic-dynamic compiles arm them (install with dynamic=None), so a bf16 FLUX.1 (dynamic=True) and a static compile are unchanged.
  • Tests: test_flux1_new_prompt_length_and_resolution_reuse_the_first_graphs (a two-block FLUX.1 stand-in compiled with automatic dynamic: unarmed it compiles again on a new text length and on a new image length; armed, the first compile covers both). Fails on main (install arms nothing), passes here. The policy test now lists FLUX.1 among the armed classes.

Testing

  • pytest tests/test_diffusion_dynamic_text.py tests/test_diffusion_prompt_length_policy.py: 33 passed (the new test fails on main).
  • Real-weights follow-up sequence above, main vs this branch, same host, same seeds.

Not covered

  • Z-Image is still classified but not armed: its bf16 default compiles with dynamic=True and did not recompile in the same sequence; an int8 Z-Image (bf16 does not fit) was not run here.
  • FLUX.1-dev and Krea-dev were not run with real weights (same transformer class and block code as schnell).

…or new size does not recompile

An int8 / fp8 FLUX.1 (the default load) compiles with automatic dynamic, so the first prompt past 256 T5 tokens and the first new resolution each recompiled both repeated blocks. Add FluxTransformer2DModel to _FAMILY_SOURCES with the sources PROMPT_LENGTH_POLICY already lists, as FLUX.3 and Qwen-Image are.
@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-22T13:18:22.611719Z a8b55bd 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: a8b55bda05

ℹ️ 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 6f0ca18 into main Oct 22, 2026
@danielhanchen
danielhanchen deleted the imgfix-flux1-dynamic-text branch October 22, 2026 18:13
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