Uh oh!
There was an error while loading. Please reload this page.
[Bug] Fix QwenImageEditPlus Series on NPU - #13017
Conversation
…n and refining mask checks.
…alidation and conversion logic.
zhangtao0408
commented
Jan 22, 2026
…e performance and validation.
| if ( | ||
| attn_mask is not None | ||
| and torch.all(attn_mask != 0).item() | ||
| ): |
There was a problem hiding this comment.
| if ( | |
| attn_maskisnotNone | |
| andtorch.all(attn_mask!=0).item() | |
| ): | |
| ifattn_maskisnotNoneandtorch.all(attn_mask!=0): |
Won't it work?
There was a problem hiding this comment.
Won't it work?
diffusers/src/diffusers/models/attention_dispatch.py
Lines 1131 to 1136 in 5c92a77
Thanks for the reply!
Since NPU FA does not support the [B, Seq_len_kv] mask shape passed by QwenImageEditPlus, and the unsqueeze/expand operations slow down execution, I added logic to bypass these steps when the mask is all 1s. This optimization significantly improves speed in context parallel, as shown in the test results below:
| Stage | Card | End to End Time(s) |
|---|---|---|
Skip expand mask (set to None) | 1 | 108.22 |
Skip expand mask (set to None) | 4 | 49.83 |
| Expand mask | 1 | 108.62 |
| Expand mask | 4 | 57.74 |
There was a problem hiding this comment.
That's fine. I am asking if this condition would work (i.e., no item()):if attn_mask is not None and torch.all(attn_mask != 0):
There was a problem hiding this comment.
Thanks, that worked. I've removed item() and pushed the update.
| # Skip Attention Mask if all values are 1, `None` mask can speedup the computation | ||
| if ( | ||
| attn_mask is not None | ||
| and torch.all(attn_mask != 0).item() |
| per_sample_len = torch.where(has_active, active_positions.max(dim=1).values + 1, torch.as_tensor(text_seq_len)) | ||
| per_sample_len = torch.where( | ||
| has_active, | ||
| active_positions.max(dim=1).values + 1, | ||
| torch.as_tensor(text_seq_len, device=encoder_hidden_states.device) | ||
| ) |
There was a problem hiding this comment.
Seems like an unrelated change? If so, could you undo it?
| if ( | ||
| attn_mask is not None | ||
| and attn_mask.ndim == 2 | ||
| and attn_mask.shape[0] == query.shape[0] | ||
| and attn_mask.shape[1] == key.shape[1] | ||
| ): | ||
| B, Sq, Skv = attn_mask.shape[0], query.shape[1], key.shape[1] | ||
| attn_mask = ~attn_mask.to(torch.bool) | ||
| attn_mask = attn_mask.unsqueeze(1).expand(B, Sq, Skv).unsqueeze(1).contiguous() |
There was a problem hiding this comment.
Would it make sense to have a small utlity named _maybe_modify_attn_mask_npu() so that it can be reused in the two places (here and above)?
There was a problem hiding this comment.
Thanks for your suggestion! I've updated the _maybe_modify_attn_mask_npu() method.
diffusers/src/diffusers/models/attention_dispatch.py
Lines 1114 to 1135 in 020a232
HuggingFaceDocBuilderDev
commented
Jan 23, 2026
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
zhangtao0408
commented
Feb 4, 2026
Hi @sayakpaul , thanks for reviewing! Could you let me know what’s needed to get this PR merged? I’m happy to make any adjustments. Thanks! |
sayakpaul
commented
Feb 4, 2026
@bot /style |
Style bot fixed some files and pushed the changes. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.


What does this PR do?
Fixes: #13015
Fixes: #13016
Test Codes:
Results in comment
Before submitting
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
cc @yiyixuxu@sayakpaul@asomoza@DN6