Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

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

Daniel Han6f0ca187d58c
diff --git a/studio/backend/core/inference/diffusion_dynamic_text.py b/studio/backend/core/inference/diffusion_dynamic_text.py
index bbf8541c470..b7a4143de46 100644
--- a/studio/backend/core/inference/diffusion_dynamic_text.py
+++ b/studio/backend/core/inference/diffusion_dynamic_text.py
@@ -121,8 +121,11 @@ def prompt_length_varies(transformer: Any) -> bool:
return isinstance(PROMPT_LENGTH_POLICY.get(cls.__name__), tuple)
-# What the compile arms (FLUX.1 and Z-Image are classified above but not armed here).
+# What the compile arms (Z-Image is classified above but not armed here). FLUX.1 int8 / fp8 (torchao weights compile
+# with automatic dynamic) recompiled both blocks on the first prompt past 256 T5 tokens and again on the first new
+# resolution (31 s and 60 s on a loaded B200); armed, the first compile covers every text length and image size.
_FAMILY_SOURCES: dict[str, tuple[str, ...]] = {
+ "FluxTransformer2DModel": _FLUX1_SOURCES,
"Flux3Transformer2DModel": _FLUX3_SOURCES,
"QwenImage21Transformer2DModel": _QWEN_IMAGE_21_SOURCES,
"QwenImageTransformer2DModel": _QWEN_IMAGE_SOURCES,
diff --git a/studio/backend/tests/test_diffusion_dynamic_text.py b/studio/backend/tests/test_diffusion_dynamic_text.py
index d5bfcb4f782..7df70cf4ff1 100644
--- a/studio/backend/tests/test_diffusion_dynamic_text.py
+++ b/studio/backend/tests/test_diffusion_dynamic_text.py
@@ -541,3 +541,64 @@ def test_slice_identity_fix_is_probe_gated_and_reversible(monkeypatch):
finally:
dt.uninstall_slice_identity_fix()
assert SliceVariable.__dict__.get("get_real_python_backed_value") is stock
+
+
+class FluxTransformer2DModel(torch.nn.Module):
+ """FLUX.1's two compiled blocks: the double block sees the text stream beside the image tokens, the single block
+ concatenates them, and the rotary tables span text + image."""
+
+ def __init__(self):
+ super().__init__()
+ self.seen = None
+ self.double = torch.compile(self._double, backend = "eager", dynamic = None)
+ self.single = torch.compile(self._single, backend = "eager", dynamic = None)
+
+ @staticmethod
+ def _double(hidden_states, encoder_hidden_states, temb, image_rotary_emb):
+ joint = (
+ torch.cat([encoder_hidden_states, hidden_states], dim = 1) * image_rotary_emb[0]
+ + image_rotary_emb[1]
+ )
+ text = encoder_hidden_states.shape[1]
+ return joint[:, text:] + temb[:, None], joint[:, :text]
+
+ @staticmethod
+ def _single(hidden_states, encoder_hidden_states, temb, image_rotary_emb):
+ joint = torch.cat([encoder_hidden_states, hidden_states], dim = 1)
+ out = joint * image_rotary_emb[0] + image_rotary_emb[1] + temb[:, None]
+ return out[:, encoder_hidden_states.shape[1] :]
+
+ def forward(self, text_len, image_len):
+ self.seen = _cfg().dynamic_sources
+ hidden_states = torch.randn(1, image_len, 8)
+ encoder_hidden_states = torch.randn(1, text_len, 8)
+ temb = torch.randn(1, 8)
+ rotary = (torch.randn(text_len + image_len, 8), torch.randn(text_len + image_len, 8))
+ hidden_states, encoder_hidden_states = self.double(
+ hidden_states, encoder_hidden_states, temb, rotary
+ )
+ return self.single(hidden_states, encoder_hidden_states, temb, rotary)
+
+
+def test_flux1_new_prompt_length_and_resolution_reuse_the_first_graphs():
+ """T5 pads to 256 or 512 tokens; a torchao FLUX.1 compiles with automatic dynamic, which recompiled both blocks on
+ the first 512-token prompt and again on the first new resolution."""
+ from torch._dynamo.utils import counters
+
+ def graphs_per_render(install):
+ torch._dynamo.reset()
+ counters.clear()
+ m = FluxTransformer2DModel()
+ if install:
+ assert dt.install(m)
+ seen = []
+ for text_len, image_len in ((16, 64), (16, 64), (32, 64), (16, 48), (32, 80)):
+ before = counters["stats"]["unique_graphs"]
+ m(text_len, image_len)
+ seen.append(counters["stats"]["unique_graphs"] - before)
+ dt.uninstall(m)
+ torch._dynamo.reset()
+ return seen
+
+ assert graphs_per_render(False)[2:] != [0, 0, 0]
+ assert graphs_per_render(True) == [2, 0, 0, 0, 0]
diff --git a/studio/backend/tests/test_diffusion_prompt_length_policy.py b/studio/backend/tests/test_diffusion_prompt_length_policy.py
index efec7bf2b34..d3cfd0856ce 100644
--- a/studio/backend/tests/test_diffusion_prompt_length_policy.py
+++ b/studio/backend/tests/test_diffusion_prompt_length_policy.py
@@ -62,8 +62,9 @@ def test_prompt_length_varies_reads_the_policy():
def test_the_compile_arms_only_its_own_sources():
- # the classification adds no compile sources: FLUX.1 and Z-Image compile as before
+ # Z-Image is classified but compiles as before
assert set(dt._FAMILY_SOURCES) == {
+ "FluxTransformer2DModel",
"Flux3Transformer2DModel",
"QwenImage21Transformer2DModel",
"QwenImageTransformer2DModel",