Skip to content

Merging Stable diffusion pipelines just makes sense #551

Closed

Description

@leszekhanusz

Following the Philosophy, it has been decided to keep different pipelines for Stable Diffusion for txt-to-img, img-to-img and inpainting.

Here is the result:

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...

Activity

  1. leszekhanusz commented on Sep 18, 2022

    @leszekhanusz
    ContributorAuthor

    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.

  2. zledas commented on Sep 18, 2022

    @zledas
    Contributor

    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 :(

  3. shirayu commented on Sep 18, 2022

    @shirayu
    Contributor

    Good suggestion. I agree with you.

  4. anton-l commented on Sep 19, 2022

    @anton-l
    Member

    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.

    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!

  5. patrickvonplaten commented on Sep 19, 2022

    @patrickvonplaten
    Contributor

    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:

    • 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.

    What do you think @leszekhanusz ?

  6. leszekhanusz commented on Sep 19, 2022

    @leszekhanusz
    ContributorAuthor

    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:

    class StableDiffusionPipeline(StableDiffusionCommonPipeline):
    
        def __call__(                                                                                                       
            self,                                                                                                           
            prompt: Union[str, List[str]],                                                                                  
            height: Optional[int] = 512,                                                                                    
            width: Optional[int] = 512,                                                                                     
            num_inference_steps: Optional[int] = 50,                                                                        
            guidance_scale: Optional[float] = 7.5,                                                                          
            eta: Optional[float] = 0.0,                                                                                     
            generator: Optional[torch.Generator] = None,                                                                    
            latents: Optional[torch.FloatTensor] = None,                                                                    
            output_type: Optional[str] = "pil",                                                                             
            return_dict: bool = True,                                                                                       
            **kwargs,                                                                                                       
        ): 
        	super().__call__(
        		prompt=prompt,
        		height=height,
        		...
        		init_image=None,
        		mask_image=None,
        		...
        	)

    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.

  7. okalldal commented on Sep 20, 2022

    @okalldal
    Contributor

    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.

  8. leszekhanusz commented on Sep 20, 2022

    @leszekhanusz
    ContributorAuthor
  9. patrickvonplaten commented on Sep 22, 2022

    @patrickvonplaten
    Contributor

    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 strength latents mask_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:
    from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, StableDiffusionInpaintPipeline
    from diffusers import DiffusionPipeline
    
    stable_diffusion = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
    sub_models = {k: v for k,v in vars(pipeline).items() if not k.startswith("_")}
    
    text2img = StableDiffusionPipeline(**sub_models)
    img2img = StableDiffusionImg2ImgPipeline(**sub_models)
    inpaint = StableDiffusionInpaintPipeline(**sub_models)
    
    def all_in_one(type, *args, **kwargs):
        if type == "text2img":
             return text2img(*args, **kwargs)
        ...

    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 :-)

    Also cc @keturn @shirayu @johnowhitaker @jonatanklosko @JoaoLages @pcuenca @patil-suraj @anton-l @kashif @sgrigory very interested in hearing your final thoughts!

  10. johnowhitaker commented on Sep 22, 2022

    @johnowhitaker
    Contributor

    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 ;)

  11. JoaoLages commented on Sep 22, 2022

    @JoaoLages

    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:

    class CoreStableDiffusionPipeline:
        ...
        def _forward(prompt, init_image: Optional = None, mask_image: Optional = None, ...):
            ....
    
    class StableDiffusionPipeline(CoreStableDiffusionPipeline):
        ...
        def __call__(prompt, ...):
            return self._forward(prompt, ...)
    
    class StableDiffusionImg2ImgPipeline(CoreStableDiffusionPipeline):
        ...
        def __call__(prompt, init_image, ...):
            assert init_image is not None # avoid incorrect class usage
            return self._forward(prompt, init_image, ...)
    
    class StableDiffusionInpaintPipeline(CoreStableDiffusionPipeline):
        ...
        def __call__(prompt, init_image, mask_image, ...):
            assert init_image is not None and mask_image is not None  # avoid incorrect class usage
            return self._forward(prompt, init_image, mask_image, ...)

    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?

  12. patrickvonplaten commented on Sep 22, 2022

    @patrickvonplaten
    Contributor

    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

  13. JoaoLages commented on Sep 22, 2022

    @JoaoLages

    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.

  14. okalldal commented on Sep 22, 2022

    @okalldal
    Contributor

    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. 15 remaining items

  16. patrickvonplaten commented on Oct 17, 2022

    @patrickvonplaten
    Contributor

    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

  17. patrickvonplaten commented on Oct 17, 2022

    @patrickvonplaten
    Contributor

    Will try to tackle the main "factor out functions" PR tomorrow

  18. github-actions commented on Nov 11, 2022

    @github-actions
    Contributor

    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.

  19. added
    stale
    on Nov 11, 2022
  20. patrickvonplaten commented on Nov 13, 2022

    @patrickvonplaten
    Contributor

    Very nice ideas in this thread here!

    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.

    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.

  21. thealanreis commented on Nov 15, 2022

    @thealanreis

    It's possible to implement enable_xformers_memory_efficient_attention for Mega?

  22. patil-suraj commented on Nov 15, 2022

    @patil-suraj
    Contributor

    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 :)

  23. federicotorrielli commented on Nov 23, 2022

    @federicotorrielli

    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.

  24. patrickvonplaten commented on Nov 29, 2022

    @patrickvonplaten
    Contributor

    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:

      1. Having the option to run DiffusionPipeline.call while calculating gradients;
      1. Having a output_latents flag (similar to output_scores/output_attentions/etc from transformers) that adds a latents attribute to the output;
      1. Deactivating safety checker; removed this option (12ca969)
      1. Passing text_embeddings directly instead of the text string;
      1. Gradient checkpointing (this was already a feature in transformers too).

    In general, for each of these points it'd be nice to open a separate issue/PR.

    In more detail:

      1. and 5. are solved by now.
      1. is a big change, but we could discuss this in a "feature request" issue
      1. This will probably soon be possible (we need this change for latent upscaling)
      1. also a bigger change that would warrant a feature request issue
  25. federicotorrielli commented on Nov 29, 2022

    @federicotorrielli

    @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?

  26. patrickvonplaten commented on Dec 1, 2022

    @patrickvonplaten
    Contributor

    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

  27. bghira commented on Apr 1, 2024

    @bghira
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Good Example PRstale

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions