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 Z-Image's prompt-length inputs for an int8 / fp8 compile (#14715)

Daniel Han725455e1ae59
diff --git a/studio/backend/core/inference/diffusion_dynamic_text.py b/studio/backend/core/inference/diffusion_dynamic_text.py
index b7a4143de46..849cd062f4d 100644
--- a/studio/backend/core/inference/diffusion_dynamic_text.py
+++ b/studio/backend/core/inference/diffusion_dynamic_text.py
@@ -121,11 +121,13 @@ def prompt_length_varies(transformer: Any) -> bool:
return isinstance(PROMPT_LENGTH_POLICY.get(cls.__name__), tuple)
-# 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.
+# What the compile arms. int8 / fp8 FLUX.1 and Z-Image (torchao weights compile with automatic dynamic) recompiled on
+# new text lengths: FLUX.1 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); Z-Image's shared block on the caption, image and unified lengths (12 recompiles in the
+# first render) and again on the first longer prompt. Armed, the first compile covers every text length and image size.
_FAMILY_SOURCES: dict[str, tuple[str, ...]] = {
"FluxTransformer2DModel": _FLUX1_SOURCES,
+ "ZImageTransformer2DModel": _ZIMAGE_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 7df70cf4ff1..c5d257a433d 100644
--- a/studio/backend/tests/test_diffusion_dynamic_text.py
+++ b/studio/backend/tests/test_diffusion_dynamic_text.py
@@ -543,6 +543,52 @@ def test_slice_identity_fix_is_probe_gated_and_reversible(monkeypatch):
assert SliceVariable.__dict__.get("get_real_python_backed_value") is stock
+class ZImageTransformer2DModel(torch.nn.Module):
+ """Z-Image's one block class runs the caption refiner (caption tokens), the noise refiner (image tokens) and the
+ main layers (caption + image), positionally as (x, attn_mask, freqs_cis); the caption is padded to 32-token buckets."""
+
+ def __init__(self):
+ super().__init__()
+ self.block = torch.compile(self._block, backend = "eager", dynamic = None)
+
+ @staticmethod
+ def _block(x, attn_mask, freqs_cis):
+ return (x * freqs_cis).masked_fill(~attn_mask[..., None], 0.0) + x
+
+ def forward(self, caption_len, image_len):
+ def run(n):
+ x = torch.randn(1, n, 8)
+ return self.block(x, torch.ones(1, n, dtype = torch.bool), torch.randn(1, n, 8))
+
+ run(caption_len)
+ run(image_len)
+ return run(caption_len + image_len)
+
+
+def test_zimage_new_caption_bucket_reuses_the_first_graphs():
+ """An automatic-dynamic Z-Image recompiled its block for each new length (caption, image, unified) inside the
+ first render, then again on the first longer prompt; armed, the first compile covers every length."""
+ from torch._dynamo.utils import counters
+
+ def graphs_per_render(install):
+ torch._dynamo.reset()
+ counters.clear()
+ m = ZImageTransformer2DModel()
+ if install:
+ assert dt.install(m)
+ seen = []
+ for caption_len, image_len in ((32, 64), (32, 64), (96, 64), (32, 48)):
+ before = counters["stats"]["unique_graphs"]
+ m(caption_len, image_len)
+ seen.append(counters["stats"]["unique_graphs"] - before)
+ dt.uninstall(m)
+ torch._dynamo.reset()
+ return seen
+
+ assert graphs_per_render(False)[0] > 1
+ assert graphs_per_render(True) == [1, 0, 0, 0]
+
+
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."""
diff --git a/studio/backend/tests/test_diffusion_prompt_length_policy.py b/studio/backend/tests/test_diffusion_prompt_length_policy.py
index d3cfd0856ce..5b40d3e2b9b 100644
--- a/studio/backend/tests/test_diffusion_prompt_length_policy.py
+++ b/studio/backend/tests/test_diffusion_prompt_length_policy.py
@@ -62,9 +62,10 @@ def test_prompt_length_varies_reads_the_policy():
def test_the_compile_arms_only_its_own_sources():
- # Z-Image is classified but compiles as before
+ # every classified family is armed
assert set(dt._FAMILY_SOURCES) == {
"FluxTransformer2DModel",
+ "ZImageTransformer2DModel",
"Flux3Transformer2DModel",
"QwenImage21Transformer2DModel",
"QwenImageTransformer2DModel",