Skip to content

[LoRA] support kohya and xlabs loras for flux. - #9295

Merged
sayakpaul merged 8 commits into
mainfrom
support-kohya-kora
Aug 29, 2024
Merged

[LoRA] support kohya and xlabs loras for flux.#9295
sayakpaul merged 8 commits into
mainfrom
support-kohya-kora

Conversation

@sayakpaul

@sayakpaul sayakpaul commented Aug 28, 2024

Copy link
Copy Markdown
Member

What does this PR do?

Fixes #9291.
Fixes #9292 (potentially).

@apolinario does this work for you?

I decided to reuse https://github.com/kohya-ss/sd-scripts/blob/sd3/networks/convert_flux_lora.py (with appropriate credits to @kohya-ss) because the conversion utility won't have been too different if we were to write it anyway.

@kohya-ss could I get your commit email address so that we could add you as a co-author of this PR? @Leommm-byte same for you as well.

About tests, I think it might make sense to add a bit later to give the community a chance to play with it.

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@apolinario

Copy link
Copy Markdown
Collaborator

The Kohya LoRA seem to be working!

import torch
from diffusers import DiffusionPipeline

device = "cuda" if torch.cuda.is_available() else "cpu"

pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
pipe = pipe.to(device)

pipe.load_lora_weights("Norod78/brain-slug-flux")
generator = torch.Generator(device="cpu").manual_seed(0)
prompt = "The girl with a brain slug earring"
image = pipe(prompt, generator=generator).images[0]
image

download copy

@sayakpaul
sayakpaul marked this pull request as draft August 28, 2024 10:32
@kohya-ss

Copy link
Copy Markdown

@kohya-ss could I get your commit email address so that we could add you as a co-author of this PR? @Leommm-byte same for you as well.

Thank you for considering me as a co-author. I appreciate the credit, but there's no need to add me as a co-author to the PR. Please feel free to use the code as you see fit. I'm glad it could be helpful for your project.

@sayakpaul

Copy link
Copy Markdown
Member Author

Sure, anyway, I have made a comment about the code I reused from sd-scripts. Thank you!

@apolinario

Copy link
Copy Markdown
Collaborator

XLabs LoRAs seem to be working, however they seem to need a higher scale to really shine/showcase their effect (also, using the guidance_scale of 3.5 instead of the default 7.0, as the super high guidance seems to outshine the effect of the LoRA)

A blue jay standing on a large basket of rainbow macarons, disney style (disney_lora.safetensors)
No LoRA disney_lora.safetensors (scale 1.0) disney_lora.safetensors (scale 1.75)
The portrait of a brazilian person (XLabs-AI/flux-RealismLora)
No LoRA flux-RealismLora (scale 1.0) flux-RealismLora (scale 1.75)
import torch
from diffusers import DiffusionPipeline

device = "cuda" if torch.cuda.is_available() else "cpu"

pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
pipe = pipe.to(device)

prompt = "A blue jay standing on a large basket of rainbow macarons, disney style"
#prompt = "The portrait of a brazilian person"

generator = torch.Generator(device="cpu").manual_seed(42)
image = pipe(prompt, generator=generator, guidance_scale=3.5).images[0]
image.save("no_lora.png")

pipe.load_lora_weights("XLabs-AI/flux-lora-collection", weight_name="disney_lora.safetensors")
#pipe.load_lora_weights("XLabs-AI/flux-RealismLora")

generator = torch.Generator(device="cpu").manual_seed(42)
image = pipe(prompt, generator=generator, joint_attention_kwargs={"scale": 1}, guidance_scale=3.5).images[0]
image.save("lora_1_0.png")

generator = torch.Generator(device="cpu").manual_seed(42)
image = pipe(prompt, generator=generator, joint_attention_kwargs={"scale": 1.75}, guidance_scale=3.5).images[0]
image.save("lora_1_75.png")

@apolinario

apolinario commented Aug 28, 2024

Copy link
Copy Markdown
Collaborator

One other thing, some LoRAs in this format have a diffusion_model prepend before the double_blocks, as in

diffusion_model.double_blocks.0.img_attn.proj.lora_down.weight

For example check this out

Probably worth to consider this possibility too in the conversion

@sayakpaul

Copy link
Copy Markdown
Member Author

One other thing, some LoRAs in this format have a diffusion_model prepend before the double_blocks, as in

@apolinario thanks! Does d3c60e5 work for you?

Comment thread src/diffusers/loaders/lora_conversion_utils.py Outdated
Comment thread src/diffusers/loaders/lora_conversion_utils.py Outdated
Co-authored-by: apolinário <joaopaulo.passos@gmail.com>
@sayakpaul sayakpaul changed the title [LoRA] support kohya lora in flux. [LoRA] support kohya and xlabs loras for flux. Aug 28, 2024
Comment thread src/diffusers/loaders/lora_pipeline.py

@apolinario apolinario left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's go!

@sayakpaul
sayakpaul marked this pull request as ready for review August 28, 2024 14:06
@sayakpaul
sayakpaul requested review from DN6 and yiyixuxu August 28, 2024 14:07
@Leommm-byte

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #9291. Fixes #9292 (potentially).

@apolinario does this work for you?

I decided to reuse https://github.com/kohya-ss/sd-scripts/blob/sd3/networks/convert_flux_lora.py (with appropriate credits to @kohya-ss) because the conversion utility won't have been too different if we were to write it anyway.

@kohya-ss could I get your commit email address so that we could add you as a co-author of this PR? @Leommm-byte same for you as well.

About tests, I think it might make sense to add a bit later to give the community a chance to play with it.

Ohh, I'm so glad I could be helpful with this challenge. I'm also honored to be considered a co-author.

If needed, here's my commit email address: leom20031@gmail.com

I'll test the adaptation as soon as I get the chance. Great work guys.

@yiyixuxu yiyixuxu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

thanks!

sayakpaul and others added 2 commits August 29, 2024 07:29
Co-authored-by: Leommm-byte <leom20031@gmail.com>
@sayakpaul
sayakpaul merged commit 2a3fbc2 into main Aug 29, 2024
@sayakpaul
sayakpaul deleted the support-kohya-kora branch August 29, 2024 02:11
@duonglegiang

Copy link
Copy Markdown

Do you have plans to support ControlNet and IP Adapter from XLabs-AI in future updates?
ControlNet: https://huggingface.co/XLabs-AI/flux-controlnet-canny
IP-Adapter: https://huggingface.co/XLabs-AI/flux-ip-adapter

@sayakpaul

Copy link
Copy Markdown
Member Author

For ControlNet, #9301.

For IP-Adapters, cc: @fabiorigano @asomoza @yiyixuxu.

@Oguzhanercan

Copy link
Copy Markdown

High, thanks for your work. I wonder that, using trained loras-controlnets with 4-bit quantized DiTs(either nf4, raw int4 or SVD4Bit) is okey? Or should we quantize them also? As I see, flux-labs scripts are using float32 dtype. Loading and fusing them to 4bit dit is not throws an error. But I am not sure that loras trained with these scripts works with diffusers beacuse the output does not change at all.

@sayakpaul

@sayakpaul

Copy link
Copy Markdown
Member Author

You need to keep some things in mind when fusing LoRAs into a quantized base model:
https://github.com/huggingface/diffusers/tree/main/examples/research_projects/flux_lora_quantization#inference

@Oguzhanercan

Copy link
Copy Markdown

You need to keep some things in mind when fusing LoRAs into a quantized base model:

https://github.com/huggingface/diffusers/tree/main/examples/research_projects/flux_lora_quantization#inference

Thank you

@dancasas

dancasas commented Nov 19, 2024

Copy link
Copy Markdown

I am using the provided code example to load a LoRA I trained with x-flux from X-Labs, but there seems to be some new keys that the current implementation of lora_conversion_utils.py does not parse. I get this error after pipe.load_lora_weights("/tmp/my_lora.safetensors")

ValueError: old_state_dict should be at this point but has: ['single_blocks.1.processor.qkv_lora.down.weight', 'single_blocks.1.processor.qkv_lora.up.weight', 'single_blocks.2.processor.qkv_lora.down.weight', 'single_blocks.2.processor.qkv_lora.up.weight', 'single_blocks.3.processor.qkv_lora.down.weight', 'single_blocks.3.processor.qkv_lora.up.weight', 'single_blocks.4.processor.qkv_lora.down.weight', 'single_blocks.4.processor.qkv_lora.up.weight'].

Does anyone know how to fix it? Thanks!

@sayakpaul

Copy link
Copy Markdown
Member Author

Refer to #9915.

@dancasas

Copy link
Copy Markdown

Thanks! I can confirm that this PR #9915 worked for me to :)

sayakpaul added a commit that referenced this pull request Dec 23, 2024
* support kohya lora in flux.

* format

* support xlabs

* diffusion_model prefix.

* Apply suggestions from code review

Co-authored-by: apolinário <joaopaulo.passos@gmail.com>

* empty commit.

Co-authored-by: Leommm-byte <leom20031@gmail.com>

---------

Co-authored-by: apolinário <joaopaulo.passos@gmail.com>
Co-authored-by: Leommm-byte <leom20031@gmail.com>
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.

X-Labs FLUX LoRA keys not compatible with diffusers kohya-ss FLUX LoRA keys not compatible with diffusers

9 participants