Fix Qwen attention mask crash with diffusers >=0.37#748
Merged
Conversation
diffusers v0.37 (PR #12987) optimizes all-ones attention masks to None in encode_prompt() when there is no padding. This breaks ai-toolkit's Qwen extensions which call .to() on the mask unconditionally. Fix: reconstruct the all-ones mask at the boundary (get_prompt_embeds) right after encode_prompt() returns. This keeps the rest of the code unchanged and works with both old and new diffusers versions. Also removes redundant duplicate mask assignments in qwen_image_edit and qwen_image_edit_plus. Fixes ostris#740
There was a problem hiding this comment.
Pull request overview
Fixes a training-time crash in the Qwen image model extensions when used with diffusers>=0.37, where encode_prompt() may return None for an all-ones attention mask (no padding).
Changes:
- Reconstruct an all-ones
prompt_embeds_masktensor whenencode_prompt()returnsNonein all Qwen variants (base/edit/edit_plus). - Remove redundant re-assignment of
prompt_embeds_maskfromtext_embeddings.attention_maskin the edit/edit_plus noise prediction paths. - Ensure the mask passed into the transformer is a tensor (and in
qwen_image_edit.py, passed as a detached tensor).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
extensions_built_in/diffusion_models/qwen_image/qwen_image.py |
Switches prompt embedding extraction back to public encode_prompt() and guards against None masks by recreating an all-ones mask. |
extensions_built_in/diffusion_models/qwen_image/qwen_image_edit.py |
Adds the same None-mask reconstruction and removes redundant mask reassignment; passes a detached mask into the transformer. |
extensions_built_in/diffusion_models/qwen_image/qwen_image_edit_plus.py |
Adds the same None-mask reconstruction and removes redundant mask reassignment in noise prediction. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
|
Thank you! @Rasaboun |
mocliamg1
pushed a commit
to mocliamg1/ai-toolkit
that referenced
this pull request
Apr 23, 2026
* Fix Qwen Image mask handling * Fix Qwen attention mask crash with diffusers >=0.37 diffusers v0.37 (PR #12987) optimizes all-ones attention masks to None in encode_prompt() when there is no padding. This breaks ai-toolkit's Qwen extensions which call .to() on the mask unconditionally. Fix: reconstruct the all-ones mask at the boundary (get_prompt_embeds) right after encode_prompt() returns. This keeps the rest of the code unchanged and works with both old and new diffusers versions. Also removes redundant duplicate mask assignments in qwen_image_edit and qwen_image_edit_plus. Fixes ostris#740
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.
Problem
diffusersv0.37 (PR #12987) optimizes all-ones attention masks toNoneinencode_prompt()when there is no padding. This causes anAttributeError: 'NoneType' object has no attribute 'to'crash in all three Qwen image extensions during training.The recent fix in
295094baddressesqwen_image.pyby switching to the private_get_qwen_prompt_embeds()API, butqwen_image_edit.pyandqwen_image_edit_plus.pystill crash.Fix
Reconstruct the all-ones mask right after
encode_prompt()returnsNone, inget_prompt_embeds()of all three Qwen variants:This approach:
encode_prompt()API (not_get_qwen_prompt_embeds)Also removes redundant duplicate mask assignments in
qwen_image_edit.pyandqwen_image_edit_plus.py.Fixes #740