fix(ernie-image): pass attn_mask=None when text is unpadded#13802
Open
Ace3Z wants to merge 1 commit into
Open
Conversation
4bf7bcc to
5f37368
Compare
5f37368 to
fb49e96
Compare
ErnieImageTransformer2DModel.forward built a bool attention mask from
text_lens on every call, including the common case where every sample
already has full-length text. flash-attn 2 rejects any non-None
attn_mask, so set_attention_backend('flash') crashed even though the
all-True mask was effectively a no-op. Z-Image's _prepare_for_attention
takes the same shortcut.
Closes huggingface#13801
fb49e96 to
84ae4b4
Compare
Author
|
Friendly ping on this one. @yiyixuxu @sayakpaul, when you have a moment, would you mind taking a look? It's a small 10 line change to |
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.
What does this PR do?
Fixes #13801.
If you call
pipeline.transformer.set_attention_backend("flash")on anErnieImagePipeline, it crashes:The same code works fine on
ZImagePipelineandFlux2KleinPipeline.Root cause
ErnieImageTransformer2DModel.forwardalways builds a boolean attention mask fromtext_lens, even when every sample already has the full text length. In the common single-prompt case (whereTmax = lens.max()in_pad_text), every position is valid and the mask is just all True. flash-attn 2 rejects any non-Noneattn_mask, so the call fails before any attention runs.Z-Image already handles this. In
_prepare_for_attention(transformer_z_image.py:792-797) it does:Fix
Do the same thing in Ernie's forward. If at least one sample is padded, build the mask. Otherwise pass
None.The previous all-True bool mask was a no-op on sdpa, cudnn and native paths (no positions get masked, no
-infrows), so behavior on those backends doesn't change. I verified that numerically: with the same seed and inputs, baseline and fix produce bit-identical output in both the uniform (text_lens=[16,16]) and padded (text_lens=[16,12]) cases. Norm, mean, std, and per-element samples match to 7 decimal places.flash-attn and its variants are now usable when the batch is uniform.
Test
Wires the existing
AttentionBackendTesterMixininto the Ernie test class (mirrors the Flux pattern). Native_cudnn backend tests pass; flash variants skip cleanly whenkernelsisn't installed, and will catch the regression when it is.Before submitting
docs/source/en/.AttentionBackendTesterMixinfor Ernie.Who can review?
@yiyixuxu @sayakpaul