Skip to content
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

Support convert LoRA safetensors into diffusers format #2403

Merged
merged 4 commits into from
Mar 6, 2023

Conversation

haofanwang
Copy link
Contributor

This PR hanldes #2363 and part of #2326.

In summary, given a LoRA weight in safetensors format, we can merge it into a stable diffusion model in diffusers. It is quite easy to use as below. I have tested it with civitai models.

python convert_lora_safetensor_to_diffusers.py --base_model_path runwayml/stable-diffusion-v1-5 --checkpoint_path xxx.safetensors --dump_path ./outputs

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Feb 17, 2023

The documentation is not available anymore as the PR was closed or merged.

@haofanwang
Copy link
Contributor Author

I have run quality checker on locally, it should be fine to merge.

@garyhxfang
Copy link

@haofanwang Hi, master. If I would like to bake 2 LoRA into model, can I just run the script two times?:
first time bake the LoRA X into model A to generate model B,
and the second time bake the LoRA Y into model B to get model C.

@haofanwang
Copy link
Contributor Author

@garyhxfang Yes, I did try this using two different LoRA (as style insertion), it would work.

@jndietz
Copy link

jndietz commented Feb 18, 2023

@haofanwang This is awesome.

This is kind of a loaded question, and not really related to the PR, but how did you "know" what to code for this PR? How can I learn more about the structure of pytorch, safetensors, and diffusers binaries and use them effectively?

@garyhxfang
Copy link

@haofanwang Hi, I try the script, and it work well currently. Thanks a lot for your great work!
Only a small mistake
parser.add_argument("--alpha", default=0.75, type=int, help="The merging ratio in W = W0 + alpha * deltaW")
Think the type of alpha should be float? I can run after I change it to float.

@haofanwang
Copy link
Contributor Author

haofanwang commented Feb 20, 2023

@garyhxfang I'm glad to know it is helpful to you! I have fixed this typo, thanks.

@patrickvonplaten Could you take a look for this PR so that we merge it ASAP. It seems that many users are waiting for it.

if len(state_dict[pair_keys[0]].shape) == 4:
weight_up = state_dict[pair_keys[0]].squeeze(3).squeeze(2).to(torch.float32)
weight_down = state_dict[pair_keys[1]].squeeze(3).squeeze(2).to(torch.float32)
curr_layer.weight.data += alpha * torch.mm(weight_up, weight_down).unsqueeze(2).unsqueeze(3)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't fully understand this - why do are we updating the layer weight here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see! Smart! Ok cool, let's go for it

Copy link

Choose a reason for hiding this comment

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

excuse,me. when i run curr_layer.weight.data += alpha * torch.mm(weight_up, weight_down).unsqueeze(2).unsqueeze(3)` it shows error like "'Modulist' object has no attribute 'weight'",May I ask if you know how to solve it?

@patrickvonplaten patrickvonplaten merged commit 63805f8 into huggingface:main Mar 6, 2023
mengfei25 pushed a commit to mengfei25/diffusers that referenced this pull request Mar 27, 2023
)

* add lora convertor

* Update convert_lora_safetensor_to_diffusers.py

* Update README.md

* Update convert_lora_safetensor_to_diffusers.py
w4ffl35 pushed a commit to w4ffl35/diffusers that referenced this pull request Apr 14, 2023
)

* add lora convertor

* Update convert_lora_safetensor_to_diffusers.py

* Update README.md

* Update convert_lora_safetensor_to_diffusers.py
@AlexxxStarkkk
Copy link

AlexxxStarkkk commented Jun 19, 2023

@haofanwang Hi!
I used the “convert_lora_safetensor_to_diffusers.py” to convert the [one-piece-wano-saga-style-lora]--“wanostyle_2_offset.safesensor” to a folder(size about 5G)。
then, i used it:
`import torch, os
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
from diffusers.models import AutoencoderKL

os.environ["CUDA_VISIBLE_DEVICES"] = "0"

model_id = r".\sd_model\stable-diffusion-v1-5" # local sd model
vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse", cache_dir=r"D:\sd310\vae_model")

pipe = StableDiffusionPipeline.from_pretrained(model_id)

lora_model_path = r'D:\sd310\lora_model\diffusers_format\one_piece_diffusers_model'
pipe.unet.load_attn_procs(lora_model_path)

pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)

pipe = pipe.to("cuda")

prompt = "1boy, wanostyle, monkey d luffy, smiling"

image = pipe(prompt, height=504, width=680).images[0]
image.save("./001_images_test/one_piece1.png")`

but there is an error:
Traceback (most recent call last): File "D:\sd310\main_test.py", line 69, in <module> pipe.unet.load_attn_procs(lora_model_path) File "E:\programs\Anoconda3\envs\sd310\lib\site-packages\diffusers\loaders.py", line 204, in load_attn_procs model_file = _get_model_file( File "E:\programs\Anoconda3\envs\sd310\lib\site-packages\diffusers\utils\hub_utils.py", line 275, in _get_model_file raise EnvironmentError( OSError: Error no file named pytorch_lora_weights.bin found in directory D:\sd310\lora_model\diffusers_format\one_piece_diffusers_model.

then i use this code:https://github.com/haofanwang/Lora-for-Diffusers/blob/main/format_convert.py
i create a "onepiece.bin", then load it——— "pipe.unet.load_attn_procs(r'D:\sd310\lora_model\diffusers_format\onepiece.bin')"

but still not work

how can i fix it?

@leebory
Copy link

leebory commented Nov 5, 2023

excuse,me. when i run curr_layer.weight.data += alpha * torch.mm(weight_up, weight_down).unsqueeze(2).unsqueeze(3)` it shows error like "'Modulist' object has no attribute 'weight'",May I ask if you know how to solve it?

AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
)

* add lora convertor

* Update convert_lora_safetensor_to_diffusers.py

* Update README.md

* Update convert_lora_safetensor_to_diffusers.py
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.

7 participants