Fix: Make tokenizer_2 and text_encoder_2 truly optional in StableDiffusionXLPipeline #11606
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.
Background:
I am working on an SDXL varient that exclusively has only (long)CLIP-L.
cf: https://huggingface.co/opendiffusionai/sdxl-longcliponly
Problem that this PR fixes
I would LIKE to be able to just use StableDiffusionXLPipeline.
The text_encoder_2 component is supposedly already marked as optional. However...
Previously, the StableDiffusionXLPipeline did not correctly handle cases where
tokenizer_2andtext_encoder_2were None, despite them being listed in_optional_components. This could lead to errors during prompt encoding.This commit addresses the issue by:
encode_promptto dynamically build lists of tokenizers and text encoders, only including those that are not None.prompt_2andnegative_prompt_2to only use them if the correspondingtokenizer_2andtext_encoder_2are present.text_encoder_projection_dimin the__call__method to correctly use available text encoder configs or fallback to pooled embedding dimensions iftext_encoder_2is not present.Unit tests have been added to verify the pipeline's behavior when:
tokenizerandtext_encoderare present.tokenizer_2andtext_encoder_2are present.What does this PR do?
Fixes # (issue)
Before submitting
documentation guidelines, and
here are tips on formatting docstrings.
Core library: