-
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
Open
lavinal712
wants to merge
44
commits into
huggingface:main
Choose a base branch
from
lavinal712:control-lora
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.
+307
−1
Open
Support for control-lora #10686
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
18de3ad
run control-lora on diffusers
lavinal712 e9d91e1
cannot load lora adapter
lavinal712 9cf8ad7
test
lavinal712 2453e14
1
lavinal712 39b3b84
add control-lora
lavinal712 de61226
1
lavinal712 10daac7
1
lavinal712 523967f
1
lavinal712 dd24464
Merge branch 'huggingface:main' into control-lora
lavinal712 33288e6
Merge branch 'huggingface:main' into control-lora
lavinal712 280cf7f
Merge branch 'huggingface:main' into control-lora
lavinal712 7c25a06
fix PeftAdapterMixin
lavinal712 0719c20
fix module_to_save bug
lavinal712 81eed41
delete json print
lavinal712 2de1505
Merge branch 'main' into control-lora
sayakpaul ce2b34b
Merge branch 'main' into control-lora
lavinal712 6a1ff82
resolve conflits
lavinal712 ab9eeff
Merge branch 'main' into control-lora
lavinal712 6fff794
merged but bug
lavinal712 8f7fc0a
Merge branch 'huggingface:main' into control-lora
lavinal712 63bafc8
change peft.py
lavinal712 c134bca
change peft.py
lavinal712 39e9254
Merge branch 'huggingface:main' into control-lora
lavinal712 d752992
Merge branch 'huggingface:main' into control-lora
lavinal712 0a5bd74
1
lavinal712 53a06cc
delete state_dict print
lavinal712 23cba18
fix alpha
lavinal712 d3a0755
Merge branch 'main' into control-lora
lavinal712 af8255e
Merge branch 'main' into control-lora
lavinal712 c6c13b6
Merge branch 'huggingface:main' into control-lora
lavinal712 4a64d64
Merge branch 'main' into control-lora
lavinal712 59a42b2
Merge branch 'huggingface:main' into control-lora
lavinal712 1c90272
Merge branch 'huggingface:main' into control-lora
lavinal712 a2eff1c
Merge branch 'huggingface:main' into control-lora
lavinal712 00a26cd
Create control_lora.py
lavinal712 1e8221c
Add files via upload
lavinal712 9d94c37
Merge branch 'huggingface:main' into control-lora
lavinal712 dfad056
Merge branch 'huggingface:main' into control-lora
lavinal712 4d1e891
rename
lavinal712 2223722
Merge branch 'huggingface:main' into control-lora
lavinal712 48eeeae
Merge branch 'main' into control-lora
lavinal712 58559ec
no need modify as peft updated
lavinal712 cd71418
add doc
lavinal712 03d1751
Merge branch 'main' into control-lora
sayakpaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| 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) | ||
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
| 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") |
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
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
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
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
Oops, something went wrong.
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.
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.