fix: pass attention_mask to T5/CLIP encoder in HFEmbedder#507
Open
Mr-Neutr0n wants to merge 1 commit intoblack-forest-labs:mainfrom
Open
fix: pass attention_mask to T5/CLIP encoder in HFEmbedder#507Mr-Neutr0n wants to merge 1 commit intoblack-forest-labs:mainfrom
Mr-Neutr0n wants to merge 1 commit intoblack-forest-labs:mainfrom
Conversation
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
HFEmbedder.forward()tokenizes text withpadding="max_length", which pads short sequences tomax_lengthwith padding tokens. However, it then passesattention_mask=Noneto the underlying T5/CLIP model.For T5 in particular, this is problematic because T5 uses bidirectional (fully visible) self-attention. When
attention_mask=None, the encoder treats every token — including padding tokens — as real input and attends to them. This means padding tokens actively participate in self-attention computations, polluting the text embeddings especially for short prompts that have many padding tokens.The tokenizer already produces the correct
attention_maskinbatch_encoding, marking real tokens as1and padding tokens as0. This PR simply passes that mask through to the model instead of discarding it.Changes
src/flux/modules/conditioner.py: Replaceattention_mask=Nonewithattention_mask=batch_encoding["attention_mask"].to(self.hf_module.device)inHFEmbedder.forward()Impact
max_lengthare unaffected (no padding tokens to mask)