-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Error converting LoRA to safetensors/ckpt #2326
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
Comments
I just tried running the newer script
And the output appeared to be the same as
|
Looking at the script itself, I'm seeing why this isn't working. My output directory isn't a model. What do I do with the output in there currently?
|
@williamberman could you take a look here? |
Following |
By other WebUIs was mostly Automatic1111 meant? We should really think about whether we can somehow integrate |
@patrickvonplaten Yes, I am speaking to automatic1111's webui specifically. Though, I want to be able to do this via a python script without having to use a webui. I'll have to go back and make sure I followed the 🤗 docs. I do feel like I followed them closely, which is why I am confused as to how to use the outputted LoRAs and convert them into a safetensors or ckpt. |
You can use LoRA trained embeddings easily with |
Hi, @patrickvonplaten, does diffusers support convert or directly load from Lora+safetensor? I have tried with the converting scripts, but failed. The weight is from civitai. Any help would be appreciated. |
I'll try to allocate time for this. Otherwise maybe @williamberman or @sayakpaul could check it out as well. I'm not 100% sure whether we can convert civitai lora weights |
W.r.t #2363 I think there are a couple of different conversion pathways we're talking about for completness:
@patrickvonplaten am I missing out on something? |
I think the second one has already been ready with The third one should be civitai LoRA weights (in safetensors format) to diffusers. I'm actually working on it by diving into stable-diffusion-webui which supports loading from lora+safetensors format, I will provide my script for your reference once I finish. |
That is amazing! It would be amazing to contribute a PR for that as well! |
@haofanwang |
Should we add Lora related code to research_projects? @sayakpaul |
If it's about conversion, okay to add them to |
@sayakpaul @williamberman I have made a PR for this, if it looks good to you, it should be fine to merge. |
@haofanwang That is awesome - now how can I convert my 🤗 diffuser to a safetensors/ckpt? Is it just a matter if mapping keys? |
@jndietz Yes. Once the PR merged into diffuser, you can just run the convert script! |
@haofanwang thanks a lot for the PR. I have a question related to the discussion AUTOMATIC1111/stable-diffusion-webui#7387 - after we trained lora with dreambooth using diffusers - the .bin file is only about ~3m while the lora models on Civitai.com are around ~150m - how can we convert the 3m file to the file we can use in Automatic1111? I am still learning about this and sorry for asking stupid questions. Thanks again. |
@haofanwang Isn't that script for converting safetensors to diffusers? |
@harrywang Don't worry about the file size, you are on the right way, it can work just as other weights in civitai. This is because the default setting for dimension lora layer is quite small. You can find more info at the end of this tutorial, give us a star if it is helpful. |
@jndietz No, it is not a general converting script. For now, we only handle LoRA weights stored in safetensors format. |
@haofanwang Thanks a lot for the nice reply and tutorial! - I have shared that with our team. |
@haofanwang Another question: How to convert them into safetensors like the ones I downloaded from civitai or huggingface so that I can use this via Automatica1111? Thanks a lot!! |
@harrywang could you open a new issue here? as this issue becomes just like a Q&A, I will take a look soon. |
No problem. I have created an issue haofanwang/Lora-for-Diffusers#1 Thanks! |
@harrywang check out my comment in this discussion, it might help you until an official solution gets released |
Thanks. But when I use https://github.com/huggingface/diffusers/blob/main/examples/text_to_image/train_text_to_image_lora.py to train a model, there is no any idea? |
@harrywang I've just realized that I used a different version of the lora training script which explains the missing file: |
@harrywang so I have just replaced the 'custom_checkpoint_0.pkl' with 'pytorch_model.bin' in the converter script and it works just fine using in automatic1111 |
@ignacfetser I'll give this a shot later, thanks. |
@ignacfetser Worked perfectly. I will close this issue. I used import os;
import re;
import torch;
from safetensors.torch import save_file;
loraName = os.path.split(os.getcwd())[1];
for root, dirs, files in os.walk('.'):
for dir in dirs:
ckptIndex = re.search('^checkpoint\-(\d+)$', dir);
if ckptIndex:
newDict = dict();
checkpoint = torch.load(os.path.join(dir, 'custom_checkpoint_0.pkl'));
for idx, key in enumerate(checkpoint):
newKey = re.sub('\.processor\.', '_', key);
newKey = re.sub('mid_block\.', 'mid_block_', newKey);
newKey = re.sub('_lora.up.', '.lora_up.', newKey);
newKey = re.sub('_lora.down.', '.lora_down.', newKey);
newKey = re.sub('\.(\d+)\.', '_\\1_', newKey);
newKey = re.sub('to_out', 'to_out_0', newKey);
newKey = 'lora_unet_'+newKey;
newDict[newKey] = checkpoint[key];
newLoraName = loraName + '-' + ckptIndex.group(1) + '.safetensors';
print("Saving " + newLoraName);
save_file(newDict, newLoraName); @ignacfetser To satisfy my own curiosity, how did you know what keys needed map, and why? Was there some documentation you read, or did you load the models and inspect the keys manually? |
@jndietz the conversion worked but when I loaded and used the converted lora weights in webui it does not have any effect - do you have the same issue? |
@jndietz I have downloaded an auto1111 'compatible' lora model from civitai and cross referenced the keys, also the auto1111 ui logs out the wrong keys when loading the model and that made it easier. |
@harrywang I'm not seeing any issues with my LoRAs that I created with diffusers. I put them in the automatic1111 |
@jndietz thanks! I tried another lora I trained and it worked - I guess the Lora I used before was not trained well. |
I adapted the script by @ignacfetser by adding the CPU support and a simple argparse: https://github.com/harrywang/finetune-sd/blob/main/convert-to-safetensors.py Thanks again @ignacfetser @jndietz @haofanwang for your guidance and help. Now, I can train the models using diffusers and use them in WebUI, cheers! |
Do these Diffusers to safetensors scripts work for both SD 1.5 and SD 2.1? |
Hi harry, which sd network you using work fine? |
In case anyone else ends up here with a "Failed to match keys when loading Lora" from Automatic1111 WebUI after using one of the above conversion scripts, see #3284. I was able to work around it temporarily by storing the good keys and referencing those when saving the file. |
Related Question: I recently created multiple LoRAs via "LyCORIS extraction from Trained Dreambooth models" (I used "Kohya SS" Utilities tab)... Everything went smoothly (they are the correct 90K size, etc). But I accidentally saved all the LoRAs as .CKPT instead of .safetensors... And they don't seem to be working as .ckpt format... Questions: If not, I could re-extract them from scratch via LyCORIS, but I'd rather NOT if I can avoid that... Any thoughts? :) |
For stable diffusion 2 the following script works: https://gist.github.com/slessans/ac1045b6d4627753743bc2081546733a |
@slessans Maybe you got a solution on how to convert LoRA trained on SDXL Dreambooth .bin to .safetensors? Tried a few scripts, and all of them ended with a key mismatch. |
Generally speaking, I am not clear on what to do with the output of these LoRA python scripts. I don't think the output can be natively used by the webuis. Other LoRAs I've seen online are usually safetensors. Here is what I did...
I used the provided python script in
examples
to generate a LoRA:I successfully (or not?) created a LoRA, and it output the following to
/output
:Then, I tried to run
scripts\convert_diffusers_to_original_stable_diffusion.py
, like so:I received the following error:
Did I miss something when creating the LoRA?
The text was updated successfully, but these errors were encountered: