fix(z-image): handle missing unified mask in regional guidance forward - #9614
Closed
Zhuoxi2000 wants to merge 1 commit into
Closed
Zhuoxi2000 wants to merge 1 commit into
Zhuoxi2000 wants to merge 1 commit into
Conversation
diffusers 0.40 returns attn_mask=None from _build_unified_sequence when all batch items have the same length, which is always the case for the single-item call in z_image_denoise. The regional forward called unified_mask.bool() and crashed on every Z-Image regional guidance generation. Treat a None mask as all tokens valid. Closes invoke-ai#9612
Zhuoxi2000
requested review from
JPPhoto,
Pfannkuchensack,
blessedcoolant,
dunkeroni and
lstein
as code owners
October 1, 2026 15:37
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Bug fix. Z-Image Turbo generations with a Regional Guidance layer fail with
AttributeError: 'NoneType' object has no attribute 'bool'atinvokeai/backend/z_image/z_image_transformer_patch.py:160.Why: #9550 bumped diffusers from 0.39 to 0.40. In 0.40,
ZImageTransformer2DModel._build_unified_sequencereturnsattn_mask=Nonewhen every batch item has the same unified length:z_image_denoisealways calls the transformer with a single item (cap_feats=[pos_prompt_embeds]), so inside the patched regional forwardunified_maskis alwaysNoneandunified_mask.bool()raises on every regional guidance run. With 0.39 the single-item mask was allTrue.How: when
unified_maskisNone, build the base padding mask as all-valid (torch.ones((bsz, unified_seqlen), dtype=torch.bool)), which is whatNonemeans in diffusers and matches the 0.39 behaviour. The odd layers already passunified_mask(None) straight to the layers, so only the float-mask construction needed the change. +8/-1 in the patch file.Related Issues / Discussions
Closes #9612
QA Instructions
Unit tests (CPU, tiny randomly initialised
ZImageTransformer2DModel, no weights needed):test_regional_forward_single_item_batch: single item, 16 image + 5 text tokens, regional mask that blocks part of the image from the prompt. Regression test for [bug]: Z-Image turbo regional guidance. #9612.test_regional_forward_single_item_batch_matches_unpatched_without_padding: 64 image + 32 text tokens (noSEQ_MULTI_OFpadding). An all-True regional mask gives the same output as the unpatched diffusers forward, and a restrictive mask changes the output, so the regional mask still takes effect.Results with diffusers 0.40.0 (the pinned version):
main(a51e26f):2 failed, both withAttributeError: 'NoneType' object has no attribute 'bool'atz_image_transformer_patch.py:1603 passedfortests/backend/z_image/ruff checkandruff format --checkpass on both filesManual check for reviewers (I have only run the unit tests above, not a full generation with Z-Image Turbo weights): Canvas, Z-Image Turbo model, add one Regional Guidance layer with a prompt and a painted region, Invoke. Before this PR the generation fails with the error above; with it, generation should complete with the regional prompt applied to the painted area.
Merge Plan
N/A
Checklist
What's Newcopy (if doing a release after this PR)AI assistance: this change was drafted with an AI coding assistant (Claude) and verified locally with the tests above.