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: compile FLUX.3 Klein once for every prompt length (#14211)

Daniel Han4d353f589bcf
diff --git a/studio/backend/core/inference/diffusion_dynamic_text.py b/studio/backend/core/inference/diffusion_dynamic_text.py
index dbeddf02d7e..f86c3de79a8 100644
--- a/studio/backend/core/inference/diffusion_dynamic_text.py
+++ b/studio/backend/core/inference/diffusion_dynamic_text.py
@@ -51,7 +51,22 @@ _MINIMAX_H3_SOURCES: tuple[str, ...] = (
"L['temb']",
)
+# FLUX.3 Klein (distilled and base): the pipeline pads the prompt to a multiple of 80 tokens, so every new bucket was
+# a new shape. The per-mode block runs the text stream dense and the image stream int8 (two graphs by weight type),
+# and the joint block takes the text beside the image tokens; both recompiled on the first longer prompt (two graphs,
+# ~12-20 s). Arming the block inputs makes the first compile cover every length. Same images bit for bit on a B200,
+# no recompile from 5 to 138 words, no slower steady state.
+_FLUX3_SOURCES: tuple[str, ...] = (
+ "L['hidden_states']",
+ "L['temb_mod']",
+ "L['encoder_hidden_states']",
+ "L['temb_mod_context']",
+ "L['image_rotary_emb'][0]",
+ "L['image_rotary_emb'][1]",
+)
+
_FAMILY_SOURCES: dict[str, tuple[str, ...]] = {
+ "Flux3Transformer2DModel": _FLUX3_SOURCES,
"QwenImage21Transformer2DModel": _QWEN_IMAGE_21_SOURCES,
"QwenImageTransformer2DModel": _QWEN_IMAGE_SOURCES,
"MiniMaxH3Transformer3DModel": _MINIMAX_H3_SOURCES,
diff --git a/studio/backend/tests/test_diffusion_dynamic_text.py b/studio/backend/tests/test_diffusion_dynamic_text.py
index 46353320deb..6cbadfcfeea 100644
--- a/studio/backend/tests/test_diffusion_dynamic_text.py
+++ b/studio/backend/tests/test_diffusion_dynamic_text.py
@@ -155,6 +155,41 @@ def test_compile_cache_key_changes_only_for_armed_family():
assert "dynamic_text" not in q21_default
+class Flux3Transformer2DModel(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.seen = None
+
+ def forward(self, x):
+ self.seen = _cfg().dynamic_sources
+ return x
+
+
+def test_flux3_block_inputs_are_armed():
+ """Both repeated blocks see the prompt length: the per-mode block's text call and the joint block's text stream."""
+ m = Flux3Transformer2DModel()
+ assert dt.install(m) is True
+ m(torch.zeros(1))
+ seen = m.seen.split(",")
+ for name in (
+ "L['hidden_states']",
+ "L['temb_mod']",
+ "L['encoder_hidden_states']",
+ "L['temb_mod_context']",
+ "L['image_rotary_emb'][0]",
+ "L['image_rotary_emb'][1]",
+ ):
+ assert name in seen
+ # No slice bound among them, so the slice identity patch is not needed for this family.
+ assert not dt._arms_slice_bound(dt.sources_for(m))
+ # Automatic dynamic only: a blanket dynamic=True compile arms nothing, a static one nothing either.
+ assert (
+ dt.fingerprint(m, None)
+ and dt.fingerprint(m, True) is None
+ and dt.fingerprint(m, False) is None
+ )
+
+
class QwenImageTransformer2DModel(torch.nn.Module):
def __init__(self):
super().__init__()