Skip to content

Studio diffusion: arm Z-Image's prompt-length inputs for an int8 / fp8 compile - #14715

Merged
danielhanchen merged 2 commits into
mainfrom
imgfix-zimage-dynamic-text
Oct 22, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
imgfix-zimage-dynamic-text

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

An int8 Z-Image-Turbo, which is what the default load picks when bf16 would not fit (a 12 GB card here: int8 + group offload), compiles with automatic dynamic (compile_dynamic returns None for torchao weights). Its one block class runs the caption refiner (caption tokens), the noise refiner (image tokens) and the main layers (caption + image), so on main:

  • the first render recompiled the block for each of those lengths: 17 graphs, 12 recompiles (tensor 'x' size mismatch at index 1. expected 4096, actual 32), and
  • the first longer prompt recompiled again (tensor 'x' size mismatch at index 1. expected 32, actual 256: the caption is padded to 32-token buckets).

#14387 classified Z-Image's prompt-length inputs in PROMPT_LENGTH_POLICY but left them out of _FAMILY_SOURCES. This arms them, as FLUX.3 and Qwen-Image already are, so a longer prompt reuses the first compile instead of recompiling.

A bf16 Z-Image (resident, dynamic=True) is unchanged: install arms sources only for an automatic-dynamic compile, and that path already showed 0 recompiles on the same follow-up sequence.

Measured (real weights, Tongyi-MAI/Z-Image-Turbo, defaults on a 12 GiB card, one process, B200, torch 2.13)

Follow-up sequence, main vs this branch, same process layout (speed unset, memory auto, which resolves to int8 + group offload + regional compile + CUDA graphs on this card):

render main this branch
first render 17 graphs, 12 recompiles, 1 capture 13 graphs, 7 recompiles, 1 capture
repeats, guidance, seed change 0 / 0 0 / 0
~190-word prompt 1 recompile (x 32 to 256) 0
new resolution, back to 1024 0 recompiles, 1 capture 0 recompiles, 1 capture

Every image is bit-identical between main and this branch (same sha256 prefixes for the first render, the long prompt, the seed change, the new resolution and the return to 1024).

The 7 first-render recompiles left on this branch are one-time and come from inputs this change does not arm: the refiners pass adaln_input=None where the main layers pass a tensor (a different graph by construction), and freqs_cis reaches the attention processor through cross_attention_kwargs. No later render recompiles.

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

Changes

  • diffusion_dynamic_text._FAMILY_SOURCES: add ZImageTransformer2DModel: _ZIMAGE_SOURCES (the sources Studio diffusion: record a prompt length's CUDA graph on the first render only #14387 already defined).
  • Tests: test_zimage_new_caption_bucket_reuses_the_first_graphs (a Z-Image block stand-in compiled with automatic dynamic, called with caption, image and unified lengths: unarmed it compiles more than once in the first render, armed it compiles once and never again across caption buckets and image sizes). Fails on main (install arms nothing), passes here. The policy test now lists Z-Image 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 sequence on main and on this branch above (B200 under a 12 GiB cap, one process per tree).

Not covered

  • Turning CFG on (guidance 0 to 3.5) on the offloaded int8 load still compiles the batch-2 shape once (Z-Image's CFG split is off when the denoiser streams, by design: two calls would upload it twice per step).
  • FLUX.1 is the same gap and is handled separately.

…8 compile

An int8 Z-Image (the default when bf16 does not fit, e.g. a 12 GB card) compiles with automatic dynamic, so its shared block recompiled for the caption, image and unified lengths inside the first render and again on the first longer prompt. Add ZImageTransformer2DModel to _FAMILY_SOURCES with the sources PROMPT_LENGTH_POLICY already lists.
@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-22T14:44:38.313071Z e6a5708 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. Nice work!

Reviewed commit: e6a5708a6b

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

…-text

# Conflicts:
#	studio/backend/core/inference/diffusion_dynamic_text.py
#	studio/backend/tests/test_diffusion_dynamic_text.py
#	studio/backend/tests/test_diffusion_prompt_length_policy.py

[skip ci]
@danielhanchen
danielhanchen merged commit 725455e into main Oct 22, 2026
@danielhanchen
danielhanchen deleted the imgfix-zimage-dynamic-text branch October 22, 2026 18:15
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