Skip to content

[MultiControlNet] Allow save and load #3747

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

Merged
merged 7 commits into from
Jun 12, 2023

Conversation

patrickvonplaten
Copy link
Contributor

@patrickvonplaten patrickvonplaten commented Jun 12, 2023

This PR allows local saving and loading of multiple controlnets. The following code snippet now works:

#!/usr/bin/env python3
import diffusers
import torch
import os

hugging_face_token = "<insert hugging face token>"
controlnet_dir = f"./edit-anything-v0-4-sd15/"
controlnet_inpaint_dir = f"./control_v11p_sd15_inpaint/"
sd_dir = f"./stable-diffusion-v1-5/"

edit_anything_controlnet = diffusers.ControlNetModel.from_pretrained(
    "shgao/edit-anything-v0-4-sd15",
    cache_dir=controlnet_dir,
    torch_dtype=torch.float16,
)
edit_anything_controlnet.save_pretrained(controlnet_dir, safe_serialization=True)

inpaint_controlnet = diffusers.ControlNetModel.from_pretrained(
    "lllyasviel/control_v11p_sd15_inpaint",
    cache_dir=controlnet_inpaint_dir,
    torch_dtype=torch.float16,
)

inpaint_controlnet.save_pretrained(controlnet_inpaint_dir, safe_serialization=True)

pipe = diffusers.StableDiffusionControlNetPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    controlnet=[edit_anything_controlnet, inpaint_controlnet], # this turns the controlnet into MultiControlNet
    cache_dir=sd_dir,
    torch_dtype=torch.float16,
    use_auth_token=hugging_face_token,
)
pipe.scheduler = diffusers.UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.save_pretrained(sd_dir, safe_serialization=True)

pipe = diffusers.StableDiffusionControlNetPipeline.from_pretrained(sd_dir)

This should fix the following issue: #3683

cc @DJBen let me know if this works for you

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

Comment on lines +98 to +99
variant (`str`, *optional*):
If specified, weights are saved in the format pytorch_model.<variant>.bin.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe an example of what a variant should be?

Comment on lines 181 to 183
raise ValueError(
"No ControlNets found under {os.path.dirname(pretrained_model_path)}. Expected at least {pretrained_model_path + '_0'}."
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f"" missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice change! LGTM.

@patrickvonplaten patrickvonplaten merged commit 34d14d7 into main Jun 12, 2023
@patrickvonplaten patrickvonplaten deleted the multi_controlnet_allow_save_and_load branch June 12, 2023 16:30
@DJBen
Copy link

DJBen commented Jun 17, 2023

Thank you for the change!

yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
* [MultiControlNet] Allow save and load

* Correct more

* [MultiControlNet] Allow save and load

* make style

* Apply suggestions from code review
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
* [MultiControlNet] Allow save and load

* Correct more

* [MultiControlNet] Allow save and load

* make style

* Apply suggestions from code review
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants