Skip to content

[WIP] [Core] Add support for ControlNet LoRA - #5938

Closed
shauray8 wants to merge 3 commits into
huggingface:mainfrom
shauray8:control-lora
Closed

[WIP] [Core] Add support for ControlNet LoRA #5938
shauray8 wants to merge 3 commits into
huggingface:mainfrom
shauray8:control-lora

Conversation

@shauray8

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds support for ControlNet LoRA. continuing on @sayakpaul design #4899

Fixes#5800

Before submitting

The current design

Load things

fromdiffusersimport (
StableDiffusionXLControlNetPipeline, ControlNetModel, UNet2DConditionModel
)
importtorchpipe_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.float16).to("cuda")
controlnet=ControlNetModel.from_unet(unet).to(device="cuda", dtype=torch.float16)
controlnet.load_lora_weights(lora_id, weight_name=lora_filename, controlnet_config=controlnet.config)

Infer

fromdiffusersimportAutoencoderKLfromdiffusers.utilsimportload_image, make_image_gridfromPILimportImageimportnumpyasnpimportcv2prompt="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=0.5# recommended for good generalizationvae=AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
pipe=StableDiffusionXLControlNetPipeline.from_pretrained(
pipe_id,
unet=unet,
controlnet=controlnet,
vae=vae,
torch_dtype=torch.float16,
).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
).imagesfinal_image= [image] +imagesgrid=make_image_grid(final_image, 1, 5)
grid

Who can review?

@sayakpaul can you provide me with access to the notebook you mentioned

@sayakpaul

Copy link
Copy Markdown
Member

Here you go: https://colab.research.google.com/drive/1S-NDshYL7N4S1ugF9Y86d3SRY-Fe0Qe-?usp=sharing

@George0726

Copy link
Copy Markdown

Hi folks, are there any other updates on the PR?

@andypotato

Copy link
Copy Markdown

+1 for this

@sayakpaul

Copy link
Copy Markdown
Member

We don't have the bandwidth for this at the moment and hence #4899

@github-actions

Copy link
Copy Markdown
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.

@github-actionsgithub-actionsBot added the stale Issues that haven't received updates label Jan 9, 2024
@AbnerCSZ

AbnerCSZ commented Apr 30, 2024

Copy link
Copy Markdown

@sayakpaul@shauray8@George0726@andypotato Requesting assistance from everyone, thank you very much.
controlnet = ControlNetModel.from_unet(unet).to(device="cuda", dtype=torch.float16)
controlnet.load_lora_weights(lora_id, weight_name=lora_filename, controlnet_config=controlnet.config)

The following error occurred during runtime:
AttributeError: 'ControlNetModel' object has no attribute 'load_lora_weights'
Is this due to a version issue? May I know which version you are using? My running version is as follows:
diffusers 0.27.0.dev0
transformers 4.35.2

@sayakpaulsayakpaul reopened this Apr 30, 2024
@sayakpaul

Copy link
Copy Markdown
Member

This feature hasn't been shipped yet.

@github-actionsgithub-actionsBot removed the stale Issues that haven't received updates label Apr 30, 2024
@AbnerCSZ

AbnerCSZ commented May 6, 2024

Copy link
Copy Markdown

This feature hasn't been shipped yet.

@sayakpaul
Is there a test version that has been implemented that can be used? thanks!

@shauray8shauray8 closed this by deleting the head repository May 21, 2024
@bghira

Copy link
Copy Markdown
Contributor

@sayakpaul maybe this can come back sometime

@a-r-r-o-w

Copy link
Copy Markdown
Contributor

Thanks for reminding about this @bghira! I'm going to take a look at this soon considering we now are working on adding support for Flux Control LoRA

@sayakpaul

sayakpaul commented Nov 22, 2024

Copy link
Copy Markdown
Member

Just flagging some differences so that we don't conflate ControlNet LoRA (from SAI) and what BFL has released (Flux Control LoRA):

  1. SAI's version is like so:
    a. Initialize a ControlNet from the weights of the denoiser.
    b. Add LoRA layers.
    c. Train the LoRA layers.
    d. Their pipeline is actually a ControlNet pipeline i.e., during denoising we leverage two things: the ControlNet (with LoRA added) module as well as the denoiser.

  2. OTOH, BFL is doing something different (it's more akin to how regular img2img is done but with differences)
    a. Expand the input dimension of the denoiser ([3072, 64] -> [3072, 128]). First half is pre-trained weights and the rest is initialized to zeros.
    b. Add LoRA layers to the attention layers (and others) including the input embedding layer (img_in) and train them.
    c. During inference, compute the condition of a given image (depth condition, for example), encode it with the VAE's encoder, and then do regular img2img. There is NO separate ControlNet module involved.

Hope that helps clear out the differences a bit.

Sign up for freeto 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.

[Control-LoRA] Picking Control-LoRA up again

7 participants

@shauray8@sayakpaul@George0726@andypotato@AbnerCSZ@bghira@a-r-r-o-w