-
Notifications
You must be signed in to change notification settings - Fork 6.6k
[wip] [core] introduce pipeline-specific mixins to hold common methods #12820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sayakpaul
wants to merge
80
commits into
main
Choose a base branch
from
pipeline-specific-mixins
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,446
−12,106
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
|
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. |
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?
As discussed internally, this PR introduces pipeline-specific Mixin classes that hold common methods shared across different task-specific pipelines. The actual pipelines (such as
StableDiffusionPipeline,QwenImagePipeline,QwenImageImg2ImgPipeline, etc.) would then subclass from these mixins.Examples of such common methods vary from pipeline to pipeline, but some examples include
encode_prompt(), properties such asguidance_scale, etc.Additionally, we have a couple of methods like
retrieve_latents()andretrieve_timesteps()originated instable_diffusionand we copied them over to other pipelines that use these methods. Same forcalculate_shiftthat originated influx. This PR also considers this situation and follows a reasonable approach to treat it (more below).I know we also discussed moving the specific LoraLoaderMixins to their respective pipeline modules. The PR only does that for Stable Diffusion to gather feedback. Please keep on reading.
Note
This PR introduces Mixin classes for four popular pipelines: Flux, Qwen, SDXL, and SD. Once the PR is merged, we could work with the community to do this for other influential pipelines.
Guiding principles
A sample structure for
src/diffusers/pipelines/flux:pipeline_flux_utils.pyhere has the Mixin class, holding the common methods. It additionally has thecalculate_shift()method, which is imported by the other pipelines undersrc/diffusers/pipelines/flux. Those pipelines won't have to maintain# Copied from ...versions of thecalculate_shift()method anymore. They can just import likefrom .pipeline_flux_utils import calculate_shift. This is how we reduce LOCs, further on an intra-pipeline basis.However, if a new pipeline (other than Flux), wants to leverage
calculate_shift(), it should still follow# Copied from ...versions of thecalculate_shift()instead of a direct import. This way, we can let individual pipeline-level methods evolve independently without introducing complex dependency patterns. For example,pipeline_flux_utils.pyuses the# Copied from ...versions of methods likeretrieve_latents().LoRA
For LoRA, I have limited the changes to only Flux. Once we settle on that, I can propagate that change for other pipelines tackled in this PR.
Notes
encode_prompt()varying while unifying the_get_qwen_prompt_embeds()method inQwenImageMixin. Comments are in line.prepare_image()is shared byQwenImageControlNetInpaintPipelineandQwenImageControlNetPipelinebut not others. We can choose to move them topipeline_qwen_utils.pybut I am not strongly opinionated on this. The same applies to methods likeprepare_mask_latents(). My preference here is to keep them as is for now.We can take similar principles for Flux, too. Flux Control and ControlNet pipelines have a
prepare_image()method. So, we could have something likeFluxControlMixin(FluxMixin)and includeprepare_image()there. But I felt like it was getting complex so, decided to skip.Additional questions
We have
diffusers/src/diffusers/pipelines/pipeline_utils.py
Line 2175 in 6bf668c
This PR also has a similar Mixin class but without the methods from the above Mixin. To avoid confusion, I have named it
SDMixin.WDYT about collating the two classes (
StableDiffusionMixinandSDMixin(introduced in this PR)) into one?