You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Merging Stable diffusion pipelines just makes sense #551
It's getting even worse! Now with onnx and the obvious request to have img-to-img and inpainting pipelines with onnx (#510), that would mean that every modification will need to be duplicated 6 times in total, with 6 times the number of tests.
This in untenable...
Reacted by Kevin Turner, Grigory Sizov, Gadi Cohen, Anton Lozhkov, Yuta Hayashibe, João Lages, James Thornton, PaintOnBrush, AJ Otto, 39th president of the United States, probably and 10 more
Hi @leszekhanusz you raise an important question, as the number of Stable Diffusion pipelines is rising, and we don't have a robust copy mechanism like the one in transformers yet.
We can just add copy-mechanism, we have in Transformers.
Quite strongly against creating a unified pipeline as it makes it impossible down the road to define classes like AutoPipelineForText2Image.
Also note that default values cannot be coherently defined anymore. In text2image we use 50 inference steps in image2image just 30 => I don't want to clutter the code with if type == ... statements.
We'll sure not merge onnx and non onnx pipeline because one should not have onnx installed to be able to use the pipeline and also optimization might very well be pipeline specific.
Happy to add a "one-size-fits-it-all" community pipeline which we'll also support in pip install diffusers in 1,2 weeks by mirroring community pipelines an the Hub. If people use this extensively, we can also add this to src/diffusers
User experience is much more important for us than maintenance. We should not write code that is only concerned with improving our maintenance, we should mostly write code that is easy to use and won't have us run into big backwards breaking problems in the future.
I've read your blog post and you make some good points but some of it seems to contradict you:
we decided to adopt a different design principle which we like to call the single model file policy. The single model file policy states that all code necessary for the forward pass of a model is in one and only one file.
Here we have a single model (Stable diffusion) which has 3 different pipelines (ignoring onnx for now) in different files doing more or less the same thing.
We could argue that having all the code in a single pipeline is actually following your single model file policy.
Did you look at what the code looks like in the unified pipeline? You have if init_image is None: and if mask_image is not None: conditions and it's really not complicated to follow those branches.
On the contrary, now it's getting easier to see the difference between img-to-img and inpainting than to try to compare code in different files.
For someone like me which is quite new to diffusion models, what's complicated is to understand the model, what is classifier free guidance, schedulers, torch broadcasting and shapes of tensors. Tuning out a block if mask_image is not None: is a piece of cake in comparison and allow to see more easily the difference between the different usages.
We can just add copy-mechanism, we have in Transformers.
To be honest, from an external eye, this seems like a hack to me.
Quite strongly against creating a unified pipeline as it makes it impossible down the road to define classes like AutoPipelineForText2Image.
I don't see why. If you set init_image and mask_image to None, then the pipeline is the same as the text-to-img pipeline.
Also note that default values cannot be coherently defined anymore. In text2image we use 50 inference steps in image2image just 30 => I don't want to clutter the code with if type == ... statements.
First of all, for most use-cases the user will provide the number of inference steps himself as it is quite an important parameter which he would probably like to make configurable.
But if you want to keep backward compatibility and allow custom default for each pipeline, I propose to use create a StableDiffusionCommonPipeline which will contain the unified pipeline. Then the current StableDiffusionPipeline used only for text-to-image could be replaced by something like:
We'll sure not merge onnx and non onnx pipeline because one should not have onnx installed to be able to use the pipeline and also optimization might very well be pipeline specific.
Sure, I agree.
Happy to add a "one-size-fits-it-all" community pipeline which we'll also support in pip install diffusers in 1,2 weeks by mirroring community pipelines an the Hub. If people use this extensively, we can also add this to src/diffusers
That's great! From the user point of view that is really important. That "one-size-fits-all" pipeline should really be inside src/diffusers so that it could be integrated easily in other projects and automatically updated with an upgrade of diffusers.
User experience is much more important for us than maintenance. We should not write code that is only concerned with improving our maintenance, we should mostly write code that is easy to use and won't have us run into big backwards breaking problems in the future.
That's good to hear, but the user experience could be better right now.
Reacted by Gadi Cohen, João Lages, Hafiidz Hassan, beyonddiana, James Thornton, AJ Otto, kosukekurimoto, Andrei Filipchyk, E.C and randomguy
Another aspect to this from a user experience perspective is that right now naively loading all pipelines via from_pretrained takes three times the GPU memory. If you want to use all three in the same script or gradio interface you have to either load the models separately and init the pipelines via the initializer manually or juggle the pipelines in and out of your GPU memory.
Reacted by kosukekurimoto, Aaron Miller and Zak Buzzard
Very good points here! Thanks a lot for writing all of this down. I still don't agree with most of it here though 😅
Writing my thoughts down a final time before giving in haha.
We could argue that having all the code in a single pipeline is actually following your single model file policy.
Clear no. Single model file policy means exactly to not build one model that fits it all but to have multiple modeling files. This was misunderstood. This was written for transformers so it cannot be applied one-to-one here.
On the contrary, now it's getting easier to see the difference between img-to-img and inpainting than to try to compare code in different files.
Don't agree either. We now cannot set sensible defaults anymore, clutter the __call__ with input args (like strengthlatentsmask_image ....) How would one know what guidance scale should be used for the pipelines, what num inference steps to set? It's not true that most people understand num_inference_steps well enough that it doesn't require a good default. People will mix functionality for different tasks and we'll get issues about I believe. Let's see!
A point that is not taken into account a lot here IMO: Users of diffusers include both "end-users" and "packages".
"packages" are the ones that build on top of diffusers and many good "packages" is more important than direct "end-users". PyTorch is so highly used because every ML package can super easily build on it, it has an intuitive design, ... not because they started adding a stable diffusion model in their core library.
"end-users" usually prefer very much the "one-size-fits-it-all" solutions which makes a lot of sense! Writing the same code every time for your use case is annoying! But note that we will never be able to full fill everyone's use case!
"packages" are libraries that integrate this library into other libraries & Python packages and thus favor a cleaner logical API, having a clear distinction between different tasks, unified API, no backwards breaking problems. Unified API will be broken with this pipeline because it's not a "task" pipeline anymore but a "diffusion model specific" pipeline class. It won't fit in this table: https://github.com/huggingface/diffusers/tree/main/src/diffusers/pipelines#pipelines-summary -> we'd have to give it multiple task names. This will have more consequences in the future. It won't fit in a "AutoText2ImagePipeline" class.
E.g. having all image-to-image, in-painting, and text2image is very stable diffusion specific. Having everything in one pipeline make us prone to future breaking changes. Most arguments here fall away for the "library" users:
I can build a 10-line one size fits it all pipeline that uses no more memory:
Note: {k: v for k,v in vars(pipeline).items() if not k.startswith("_")} is very hacky but we could very well provide a nice function for this in the downstream DiffusionPipeline class (cc @patil-suraj@anton-l ) I think this would be useful anyways.
Why is this better? This way the diffusers repo can focus on making clean task-specific pipelines and doesn't have to put the burden of maintenance higher lever functionality on us. E.g. now image stable diffusion has two more task that can be done with the same model => having an official one-fits-it-all pipeline puts us then in a tough spot: "where do we draw the line", "can we add the new features without making the code unreadable? Do we have to break existing behavior?" => it becomes obvious that the one-size-fits it all will quickly break
I think a big problem is that we don't advertise well enough how diffusers is intended to be used. E.g. it's really not that hard to build something on top of diffusers that doesn't require 3x the memory usage IMO
Nevertheless, I do want to have this library be community driven, but I think the above aspects and the long-term vision for the library is not taken into account enough.
=> So if you want, happy to follow your advice, but added it as a "experimental" pipeline. However I won't maintain the pipeline and ping you quite a bit on issues I think @leszekhanusz haha ;-). Thanks for taking the time to write everything down though - "easy-of-use" for such a young library might indeed be the winning argument in the end of the day.
Think this is a cool discussion, so will also asked the Twitter community on their opinion :-)
I think I'm on team 'three classes'.
If you want to do a quick demo of one application, easy to just load, say, text2img.
If you want multiple your example is very helpful, it'd be nice to document what the sub_models are and how to avoid loading duplicates like you do there.
I also think a lot of people trying more complex things won't be loading three pipelines, they'll be defining their own sampling process anyway - where they can add all the extra features they want.
That being said, I'm not the one having to maintain the code ;)
I understand the '3 classes' arguments but, as @leszekhanusz mentioned, having the code duplicated for all of them is very error-prone.
IMO, the solution proposed by @leszekhanusz of having the backbone class is what I thought of.
Yes! People now need to switch between multiple files CoreStableDiffusionPipeline and StableDiffusionInpaintPipeline . Maintaining different files is really not that big of a problem and much less of a concern in OSS than in industry settings
Yes! People now need to switch between multiple files CoreStableDiffusionPipeline and StableDiffusionInpaintPipeline .
If that's a problem, the CoreStableDiffusionPipeline subclasses could be all in the same file.
This solution is better to avoid adding/removing functionality in one of the pipelines but not in the others, that's the way I see it.
Reacted by Jonatan Kłosko, Hanusz Leszek, Sam Green, Žilvinas Ledas and James Thornton
As you say @patrickvonplaten it is not really difficult to get the pipelines to share the same models now either. The non hack way that already works is to just load each model separately via subdirectory instead of from a pipeline. Maybe a simple example for documentation is enough for most end-users in this regard and merging the pipelines are overkill. It's just that one tends to get spoiled by the "one liner that just works" huggingface experience that we all know and love 😉
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.
Please note that issues that do not follow the contributing guidelines are likely to be ignored.
To me the main concern seems to be about not factoring out shared functionality and code. I also agree that this could help in better understand differences Happy to factor out the code that all pipelines share into a function and then make sure this stays identical for the other pipelines.
Big +1 on @pcuenca that pipelines are rather supposed to be examples. We hope that most downstream application directly make use of schedulers and models instead of the very high level pipelines.
Will focus on two things from this PR:
add a function to retrieve all components
add better docs for this
factor out a function that includes shared common code
add copy mechanism so that the function can stay in the pipeline file but we ensure identical code across pipelines
Closing this issue now as #1269 finishes off the 4 points above.
This PR: Finalize stable diffusion refactor #1269 now factors out all functions of all of the 4 main pipelines we have. This should make the pipelines a lot easier to read/understand. It should also help significantly to compare the pipelines to each other
This PR: Finalize stable diffusion refactor #1269 also adds the copy-mechanism to most of the factored out functions. Most factored-out functions that are identical are "inherited" from text2image and now stay easily in sync since one only has to run "make fix-copies" to copy all the functionality that is shared.
I believe this at least partly resolves this issue. Note that since we started this discussion the "official" in-painting checkpoint has come out which uses a different model architecture than CompVis/stable-diffusion-v1-4 (more latent channels) => so those models just need different code and IMO it's good that they are separated here.
Please leave a comment below if you would still want to have some major changes and don't think this issue is "solved" yet.
Hey @thealanreis yes! Should look very similar to how it's done in the StableDiffusionPipeline. Would you like to take a shot at this ? Happy to help :)
Hello @patrickvonplaten, thanks for the hard work for the community. I love diffusers! What about this PR tho? I would love to have a full compatibility with diffusers-interpret in the latest version and the changes does not seem that huge.
@patrickvonplaten thanks for the explanation. How can I help? Is there a possibility that, opening a feature request for point i and iv, would be implemented?
after this PR changed 78 files just to fix a type error i'm pretty frustrated about this issue still. it would be nice to see movement toward resolving it instead of citing the "philosophy" document whose goal seems to be producing a large quantity of broken / duplicate code.
Activity
leszekhanusz commentedon Sep 18, 2022
I just noticed that in PR #549, the code was not duplicated exactly the same way. Sometimes multiplying by
batch_size, sometimes not.Proof again than duplicating code is never good and introduce bugs.
zledas commentedon Sep 18, 2022
I'm new here, but I also noticed, that most of the code is just duplicated, which is very hard when you want to make changes to all three :(
shirayu commentedon Sep 18, 2022
Good suggestion. I agree with you.
anton-l commentedon Sep 19, 2022
Hi @leszekhanusz you raise an important question, as the number of Stable Diffusion pipelines is rising, and we don't have a robust copy mechanism like the one in
transformersyet.Inviting @patil-suraj @patrickvonplaten @pcuenca to discuss possible solutions here :)
In the meantime, I'll address the mismatching pipeline issues manually, thank you for a thorough compilation!
patrickvonplaten commentedon Sep 19, 2022
We can just add copy-mechanism, we have in Transformers.
Quite strongly against creating a unified pipeline as it makes it impossible down the road to define classes like
AutoPipelineForText2Image.Also note that default values cannot be coherently defined anymore. In text2image we use 50 inference steps in image2image just 30 => I don't want to clutter the code with
if type == ...statements.We'll sure not merge onnx and non onnx pipeline because one should not have onnx installed to be able to use the pipeline and also optimization might very well be pipeline specific.
For more general arguments regarding this design see also here: https://huggingface.co/blog/transformers-design-philosophy
Overall:
pip install diffusersin 1,2 weeks by mirroring community pipelines an the Hub. If people use this extensively, we can also add this tosrc/diffusersWhat do you think @leszekhanusz ?
leszekhanusz commentedon Sep 19, 2022
I've read your blog post and you make some good points but some of it seems to contradict you:
Here we have a single model (Stable diffusion) which has 3 different pipelines (ignoring onnx for now) in different files doing more or less the same thing.
We could argue that having all the code in a single pipeline is actually following your single model file policy.
Did you look at what the code looks like in the unified pipeline? You have
if init_image is None:andif mask_image is not None:conditions and it's really not complicated to follow those branches.On the contrary, now it's getting easier to see the difference between img-to-img and inpainting than to try to compare code in different files.
For someone like me which is quite new to diffusion models, what's complicated is to understand the model, what is classifier free guidance, schedulers, torch broadcasting and shapes of tensors. Tuning out a block
if mask_image is not None:is a piece of cake in comparison and allow to see more easily the difference between the different usages.To be honest, from an external eye, this seems like a hack to me.
I don't see why. If you set
init_imageandmask_imageto None, then the pipeline is the same as the text-to-img pipeline.First of all, for most use-cases the user will provide the number of inference steps himself as it is quite an important parameter which he would probably like to make configurable.
But if you want to keep backward compatibility and allow custom default for each pipeline, I propose to use create a StableDiffusionCommonPipeline which will contain the unified pipeline. Then the current StableDiffusionPipeline used only for text-to-image could be replaced by something like:
Sure, I agree.
That's great! From the user point of view that is really important. That "one-size-fits-all" pipeline should really be inside src/diffusers so that it could be integrated easily in other projects and automatically updated with an upgrade of diffusers.
That's good to hear, but the user experience could be better right now.
okalldal commentedon Sep 20, 2022
Another aspect to this from a user experience perspective is that right now naively loading all pipelines via from_pretrained takes three times the GPU memory. If you want to use all three in the same script or gradio interface you have to either load the models separately and init the pipelines via the initializer manually or juggle the pipelines in and out of your GPU memory.
leszekhanusz commentedon Sep 20, 2022
@okalldal it's possible to merge all the existing pipelines into a single one. Available here.
patrickvonplaten commentedon Sep 22, 2022
Very good points here! Thanks a lot for writing all of this down. I still don't agree with most of it here though 😅
Writing my thoughts down a final time before giving in haha.
A point that is not taken into account a lot here IMO: Users of
diffusersinclude both "end-users" and "packages"."packages" are the ones that build on top of
diffusersand many good "packages" is more important than direct "end-users". PyTorch is so highly used because every ML package can super easily build on it, it has an intuitive design, ... not because they started adding a stable diffusion model in their core library."AutoText2ImagePipeline"class.E.g. having all image-to-image, in-painting, and text2image is very stable diffusion specific. Having everything in one pipeline make us prone to future breaking changes. Most arguments here fall away for the "library" users:
Note:
{k: v for k,v in vars(pipeline).items() if not k.startswith("_")}is very hacky but we could very well provide a nice function for this in the downstreamDiffusionPipelineclass (cc @patil-suraj @anton-l ) I think this would be useful anyways.diffusersrepo can focus on making clean task-specific pipelines and doesn't have to put the burden of maintenance higher lever functionality on us. E.g. now image stable diffusion has two more task that can be done with the same model => having an official one-fits-it-all pipeline puts us then in a tough spot: "where do we draw the line", "can we add the new features without making the code unreadable? Do we have to break existing behavior?" => it becomes obvious that the one-size-fits it all will quickly breakdiffusersis intended to be used. E.g. it's really not that hard to build something on top ofdiffusersthat doesn't require 3x the memory usage IMONevertheless, I do want to have this library be community driven, but I think the above aspects and the long-term vision for the library is not taken into account enough.
=> So if you want, happy to follow your advice, but added it as a "experimental" pipeline. However I won't maintain the pipeline and ping you quite a bit on issues I think @leszekhanusz haha ;-). Thanks for taking the time to write everything down though - "easy-of-use" for such a young library might indeed be the winning argument in the end of the day.
Think this is a cool discussion, so will also asked the Twitter community on their opinion :-)
Also cc @keturn @shirayu @johnowhitaker @jonatanklosko @JoaoLages @pcuenca @patil-suraj @anton-l @kashif @sgrigory very interested in hearing your final thoughts!
patrickvonplaten commentedon Sep 22, 2022
Also here a twitter poll: https://twitter.com/PatrickPlaten/status/1572980566579941381
johnowhitaker commentedon Sep 22, 2022
I think I'm on team 'three classes'.
If you want to do a quick demo of one application, easy to just load, say, text2img.
If you want multiple your example is very helpful, it'd be nice to document what the sub_models are and how to avoid loading duplicates like you do there.
I also think a lot of people trying more complex things won't be loading three pipelines, they'll be defining their own sampling process anyway - where they can add all the extra features they want.
That being said, I'm not the one having to maintain the code ;)
JoaoLages commentedon Sep 22, 2022
I understand the '3 classes' arguments but, as @leszekhanusz mentioned, having the code duplicated for all of them is very error-prone.
IMO, the solution proposed by @leszekhanusz of having the backbone class is what I thought of.
To exemplify:
This way we would still keep the 3 classes separated and the code would be way easier to maintain.
Do you guys see any disadvantages in this solution?
patrickvonplaten commentedon Sep 22, 2022
Yes! People now need to switch between multiple files
CoreStableDiffusionPipelineandStableDiffusionInpaintPipeline. Maintaining different files is really not that big of a problem and much less of a concern in OSS than in industry settingsJoaoLages commentedon Sep 22, 2022
If that's a problem, the
CoreStableDiffusionPipelinesubclasses could be all in the same file.This solution is better to avoid adding/removing functionality in one of the pipelines but not in the others, that's the way I see it.
okalldal commentedon Sep 22, 2022
As you say @patrickvonplaten it is not really difficult to get the pipelines to share the same models now either. The non hack way that already works is to just load each model separately via subdirectory instead of from a pipeline. Maybe a simple example for documentation is enough for most end-users in this regard and merging the pipelines are overkill. It's just that one tends to get spoiled by the "one liner that just works" huggingface experience that we all know and love 😉
15 remaining items
patrickvonplaten commentedon Oct 17, 2022
Hey everybody,
We've now merged the "all-in-one" community pipeline which doesn't require any additional memory to run all pipelines. Please have a look at: https://github.com/huggingface/diffusers/tree/main/examples/community#stable-diffusion-mega
patrickvonplaten commentedon Oct 17, 2022
Will try to tackle the main "factor out functions" PR tomorrow
github-actions commentedon Nov 11, 2022
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.
Please note that issues that do not follow the contributing guidelines are likely to be ignored.
patrickvonplaten commentedon Nov 13, 2022
Closing this issue now as #1269 finishes off the 4 points above.
"make fix-copies"to copy all the functionality that is shared.I believe this at least partly resolves this issue. Note that since we started this discussion the "official" in-painting checkpoint has come out which uses a different model architecture than CompVis/stable-diffusion-v1-4 (more latent channels) => so those models just need different code and IMO it's good that they are separated here.
Please leave a comment below if you would still want to have some major changes and don't think this issue is "solved" yet.
thealanreis commentedon Nov 15, 2022
It's possible to implement enable_xformers_memory_efficient_attention for Mega?
patil-suraj commentedon Nov 15, 2022
Hey @thealanreis yes! Should look very similar to how it's done in the
StableDiffusionPipeline. Would you like to take a shot at this ? Happy to help :)federicotorrielli commentedon Nov 23, 2022
Hello @patrickvonplaten, thanks for the hard work for the community. I love diffusers! What about this PR tho? I would love to have a full compatibility with diffusers-interpret in the latest version and the changes does not seem that huge.
patrickvonplaten commentedon Nov 29, 2022
Hey @federicotorrielli,
Thanks for the nice words!
Regarding "changes does not seem that huge" I must disagree a bit. The intro of the PR is:
In general, for each of these points it'd be nice to open a separate issue/PR.
In more detail:
federicotorrielli commentedon Nov 29, 2022
@patrickvonplaten thanks for the explanation. How can I help? Is there a possibility that, opening a feature request for point i and iv, would be implemented?
patrickvonplaten commentedon Dec 1, 2022
I think iv. from my point of view should definitely be feasible. i) is a big change that will take quite some time. So maybe iv) if you'd like :-)
We'll have to see with the other maintainers of this library though
bghira commentedon Apr 1, 2024
after this PR changed 78 files just to fix a type error i'm pretty frustrated about this issue still. it would be nice to see movement toward resolving it instead of citing the "philosophy" document whose goal seems to be producing a large quantity of broken / duplicate code.