Skip to content

fix(z-image): handle missing unified mask in regional guidance forward - #9614

Closed
Zhuoxi2000 wants to merge 1 commit into
invoke-ai:mainfrom
Zhuoxi2000:fix-z-image-regional-none-mask
Closed

Zhuoxi2000 wants to merge 1 commit into
invoke-ai:mainfrom
Zhuoxi2000:fix-z-image-regional-none-mask

Conversation

@Zhuoxi2000

Copy link
Copy Markdown

Summary

Bug fix. Z-Image Turbo generations with a Regional Guidance layer fail with AttributeError: 'NoneType' object has no attribute 'bool' at invokeai/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_sequence returns attn_mask=None when every batch item has the same unified length:

if all(seq == max_seqlen for seq in unified_seqlens):
    attn_mask = None

z_image_denoise always calls the transformer with a single item (cap_feats=[pos_prompt_embeds]), so inside the patched regional forward unified_mask is always None and unified_mask.bool() raises on every regional guidance run. With 0.39 the single-item mask was all True.

How: when unified_mask is None, build the base padding mask as all-valid (torch.ones((bsz, unified_seqlen), dtype=torch.bool)), which is what None means in diffusers and matches the 0.39 behaviour. The odd layers already pass unified_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):

pytest -q tests/backend/z_image/test_z_image_transformer_patch.py
  • 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 (no SEQ_MULTI_OF padding). 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):

  • on main (a51e26f): 2 failed, both with AttributeError: 'NoneType' object has no attribute 'bool' at z_image_transformer_patch.py:160
  • with this PR: 3 passed for tests/backend/z_image/
  • ruff check and ruff format --check pass on both files

Manual 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

  • The PR has a short but descriptive title, suitable for a changelog
  • Tests added / updated (if applicable)
  • ❗Changes to a redux slice have a corresponding migration
  • Documentation added / updated (if applicable)
  • Updated What's New copy (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.

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
@github-actions github-actions Bot added python PRs that change python files backend PRs that change backend files python-tests PRs that change python tests labels Oct 1, 2026
@Zhuoxi2000

Copy link
Copy Markdown
Author

Closing: this is already fixed on main by d6a54b6 ("fix(z-image): regional prompting at batch size 1"), which landed with #9613. Thanks!

@Zhuoxi2000 Zhuoxi2000 closed this Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend PRs that change backend files python PRs that change python files python-tests PRs that change python tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: Z-Image turbo regional guidance.

1 participant