-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
[LoRA refactor 2] feat: add support for load_lora(). #5958
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7358d91
feat: add support for load_lora().
sayakpaul a6d931a
style
sayakpaul dc6e343
add: test
sayakpaul fd7620c
change unet init dict.
sayakpaul 11e80d2
change to torch.allclose
sayakpaul 2f090e8
init lora weights.
sayakpaul c291601
check for peft layer addition
sayakpaul 861e83f
replicate training in the adapter state dict.
sayakpaul c829a53
UNet2DConditionModelLoRATests should need require_peft_backend
sayakpaul fc4cc8b
robustify tests
sayakpaul a62b32e
Merge branch 'main' into refactor/lora-peft-part-ii
sayakpaul b016980
Merge branch 'main' into refactor/lora-peft-part-ii
sayakpaul 4c06247
bin -> safetensors.
sayakpaul 4e0567e
debug
sayakpaul e2dfacb
conditional checking of files.
sayakpaul 511739b
handle bare except
sayakpaul ea0dd25
Merge branch 'main' into refactor/lora-peft-part-ii
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 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 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
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.
@BenjaminBossan @pacman100 @younesbelkada @apolinario - We need to discuss this a bit. I'm all in favor of supporting
adapter_config.json
mid-term, but we have some peculiarities here:=> As a consequence the main "loading" function of LoRA is part of the pipeline:
However, it's also very important that we allow loading LoRAs just from the unet or the text encoder:
So the question here is, what format and serialization should we use for PEFT that is both:
unet
object when no pipeline is loaded)pipeline.load_lora_weights
)Can we allow
adapter.json
to have a config for either just a text encoder, just a unet, both text encoder and unetCan
adapter.safetensors
have both formats?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.
Not sure if I grasped the problem completely, but the 2nd part (separate LoRAs for text encoder and unet) would be the easier one, right? We would just point to different folders, each with their own
adapter_config.json
etc.For the combined adapter, we would need to find a new solution, it could be some kind of convention (sub-folder names) or we would need to have a single adapter that takes care of both, but that seems unwieldy and more error prone.
Another use case I'd like to bring up, not sure if it's relevant at this moment: Having a pipeline load multiple different LoRA adapters (or even other type of adapters).
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.
Yes, it'd def be cleaner as a serialization format, but it's very much not the convention right now. At the moment, all LoRAs always only have a single safetensors file. So if we introduce a new 4 file convention (text_encoder's adapter_config.json, text_encoder's adapter_model.safetensors unet's adapter_config.json and unet's adapter_model.safetensors) it might be quite confusing for the community. I'm not too happy about creating a new folder structure as this wasn't super well perceived in the first place.
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 prefer the file format with subfolders for unet/text-encoder each having a
adapter_model.safetensors
andadapter_config.json
. This is exactly mimicing the folder structure of full finetuning/pretrained SD/SDXL in Diffusers just that the config/weights are only for adapters. So, that would be clearly following the standard persistence format of DiffusersThere 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.
For portability, I had written a conversion script which would put both the unet and text-encoder weights and their config's in a single
safetensors
file. The file is this: https://github.com/pacman100/peft-dreambooth/blob/master/peft_utils/merge_peft_sd_ckpt_to_single_safetensors.pyThere 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.
This way, current paradigm of single safetensors for LoRA would be achieved for PEFT and users are happy. But, this single safetensors file won't work in Auto1111 as it requires changing the state dict keys which is also implemented here https://github.com/pacman100/peft-sd-webui-additional-networks/blob/main/scripts/peft_lora.py
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.
Thanks for your thoughts. But the thing is the diffusion community very much prefers the single file-format at least for LoRAs.
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.
then these comments https://github.com/huggingface/diffusers/pull/5958/files#r1410149384 and https://github.com/huggingface/diffusers/pull/5958/files#r1410163690 would be the recommended way to have Diffusers specific utils.
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.
Another thing to keep in mind is that the utils need to generic. For example, A PR to add OFT in PEFT is close to being merged. It follows the standard format of subfolders with adapter+config files. The utils need to be generic to convert both LoRA or OFT into single safetensors format.
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.
Having a utility to output a single file seems to be the best choice here without compromising too much.