-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Support for control-lora #10686
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
base: main
Are you sure you want to change the base?
Support for control-lora #10686
Changes from 40 commits
18de3ad
e9d91e1
9cf8ad7
2453e14
39b3b84
de61226
10daac7
523967f
dd24464
33288e6
280cf7f
7c25a06
0719c20
81eed41
2de1505
ce2b34b
6a1ff82
ab9eeff
6fff794
8f7fc0a
63bafc8
c134bca
39e9254
d752992
0a5bd74
53a06cc
23cba18
d3a0755
af8255e
c6c13b6
4a64d64
59a42b2
1c90272
a2eff1c
00a26cd
1e8221c
9d94c37
dfad056
4d1e891
2223722
48eeeae
58559ec
cd71418
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Control-LoRA inference example | ||
|
|
||
| Control-LoRA is introduced by Stability AI in [stabilityai/control-lora](https://huggingface.co/stabilityai/control-lora) by adding low-rank parameter efficient fine tuning to ControlNet. This approach offers a more efficient and compact method to bring model control to a wider variety of consumer GPUs. | ||
|
|
||
| ## Installing the dependencies | ||
|
|
||
| Before running the scripts, make sure to install the library's training dependencies: | ||
|
|
||
| **Important** | ||
|
|
||
| To make sure you can successfully run the latest versions of the example scripts, we highly recommend **installing from source** and keeping the install up to date as we update the example scripts frequently and install some example-specific requirements. To do this, execute the following steps in a new virtual environment: | ||
| ```bash | ||
| git clone https://github.com/huggingface/diffusers | ||
| cd diffusers | ||
| pip install . | ||
| ``` | ||
|
|
||
| Then cd in the example folder and run | ||
| ```bash | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| And initialize an [🤗Accelerate](https://github.com/huggingface/accelerate/) environment with: | ||
|
|
||
| ```bash | ||
| accelerate config | ||
| ``` | ||
|
|
||
| ## Inference on SDXL | ||
|
|
||
| [stabilityai/control-lora](https://huggingface.co/stabilityai/control-lora) provides a set of Control-LoRA weights for SDXL. Here we use the `canny` condition to generate an image from a text prompt and a reference image. | ||
|
|
||
| ```bash | ||
| python control_lora.py | ||
| ``` | ||
|
|
||
| ## Acknowledgements | ||
|
|
||
| - [stabilityai/control-lora](https://huggingface.co/stabilityai/control-lora) | ||
| - [comfyanonymous/ControlNet-v1-1_fp16_safetensors](https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors) | ||
| - [HighCWu/control-lora-v2](https://github.com/HighCWu/control-lora-v2) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import cv2 | ||
| import numpy as np | ||
| from PIL import Image | ||
| import torch | ||
|
|
||
| from diffusers import ( | ||
| StableDiffusionXLControlNetPipeline, | ||
| ControlNetModel, | ||
| UNet2DConditionModel, | ||
| ) | ||
| from diffusers import AutoencoderKL | ||
| from diffusers.utils import load_image, make_image_grid | ||
|
|
||
| pipe_id = "stabilityai/stable-diffusion-xl-base-1.0" | ||
| lora_id = "stabilityai/control-lora" | ||
| lora_filename = "control-LoRAs-rank128/control-lora-canny-rank128.safetensors" | ||
|
|
||
| unet = UNet2DConditionModel.from_pretrained(pipe_id, subfolder="unet", torch_dtype=torch.bfloat16).to("cuda") | ||
| controlnet = ControlNetModel.from_unet(unet).to(device="cuda", dtype=torch.bfloat16) | ||
| controlnet.load_lora_adapter(lora_id, weight_name=lora_filename, prefix=None, controlnet_config=controlnet.config) | ||
|
|
||
| prompt = "aerial view, a futuristic research complex in a bright foggy jungle, hard lighting" | ||
| negative_prompt = "low quality, bad quality, sketches" | ||
|
|
||
| image = load_image("https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/hf-logo.png") | ||
|
|
||
| controlnet_conditioning_scale = 1.0 # recommended for good generalization | ||
|
|
||
| vae = AutoencoderKL.from_pretrained("stabilityai/sdxl-vae", torch_dtype=torch.bfloat16) | ||
| pipe = StableDiffusionXLControlNetPipeline.from_pretrained( | ||
| pipe_id, | ||
| unet=unet, | ||
| controlnet=controlnet, | ||
| vae=vae, | ||
| torch_dtype=torch.bfloat16, | ||
| safety_checker=None, | ||
| ).to("cuda") | ||
|
|
||
| image = np.array(image) | ||
| image = cv2.Canny(image, 100, 200) | ||
| image = image[:, :, None] | ||
| image = np.concatenate([image, image, image], axis=2) | ||
| image = Image.fromarray(image) | ||
|
|
||
| images = pipe( | ||
| prompt, negative_prompt=negative_prompt, image=image, | ||
| controlnet_conditioning_scale=controlnet_conditioning_scale, | ||
| num_images_per_prompt=4 | ||
| ).images | ||
|
|
||
| final_image = [image] + images | ||
| grid = make_image_grid(final_image, 1, 5) | ||
| grid.save("hf-logo_canny.png") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -182,7 +182,7 @@ def get_peft_kwargs( | |
| else: | ||
| lora_alpha = set(network_alpha_dict.values()).pop() | ||
|
|
||
| target_modules = list({name.split(".lora")[0] for name in peft_state_dict.keys()}) | ||
| target_modules = list({name.split(".lora")[0] for name in peft_state_dict.keys() if "lora" in name}) | ||
|
||
| use_dora = any("lora_magnitude_vector" in k for k in peft_state_dict) | ||
| # for now we know that the "bias" keys are only associated with `lora_B`. | ||
| lora_bias = any("lora_B" in k and k.endswith(".bias") for k in peft_state_dict) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can create an official documentation for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think adding it to API > Pipelines > Image would be good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add a description of how to use this LoRA in the ControlNet documentation.