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

[PEFT / LoRA] Fix civitai bug when network alpha is an empty dict #5608

Merged
merged 8 commits into from
Nov 1, 2023

Conversation

younesbelkada
Copy link
Contributor

What does this PR do?

Fixes: #5606

The fix is simply to ignore the processing of network_alpha_dict in case that dictionary is empty.

Also confirmed that I got the same generation with that checkpoint on main vs with this branch, hence added a slow test for it

cc @sayakpaul @patrickvonplaten

@@ -129,7 +129,7 @@ def get_peft_kwargs(rank_dict, network_alpha_dict, peft_state_dict, is_unet=True
rank_pattern = dict(filter(lambda x: x[1] != r, rank_dict.items()))
rank_pattern = {k.split(".lora_B.")[0]: v for k, v in rank_pattern.items()}

if network_alpha_dict is not None:
if network_alpha_dict is not None and len(network_alpha_dict) > 0:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if network_alpha_dict is not None and len(network_alpha_dict) > 0:
if network_alpha_dict:

Wouldn't this be sufficient?

Copy link
Contributor Author

@younesbelkada younesbelkada Nov 1, 2023

Choose a reason for hiding this comment

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

Yeah in practice it should be sufficient, but I think that we should avoid python magic and make sure we test explicit checks :D

Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't consider this "magic", even PEP8 recommends it:

For sequences, (strings, lists, tuples), use the fact that empty sequences are false:

# Correct:
if not seq:
if seq:

# Wrong:
if len(seq):
if not len(seq):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see ok ! happy to add that, I know in transformers we usually avoid these things, I will let diffusers maintainers comment on this

Copy link
Member

Choose a reason for hiding this comment

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

Would like to stay as explicit as possible here following the transformers approach.

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

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

Thanks much!

@patrickvonplaten
Copy link
Contributor

Nice fix!

@patrickvonplaten patrickvonplaten merged commit 02ba50c into huggingface:main Nov 1, 2023
11 checks passed
@younesbelkada younesbelkada deleted the fix-lora-issue branch November 1, 2023 21:14
kashif pushed a commit to kashif/diffusers that referenced this pull request Nov 11, 2023
…huggingface#5608)

* fix civitai bug

* add test

* up

* fix test

* added slow test.

* style

* Update src/diffusers/utils/peft_utils.py

Co-authored-by: Benjamin Bossan <[email protected]>

* Update src/diffusers/utils/peft_utils.py

---------

Co-authored-by: Benjamin Bossan <[email protected]>
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
…huggingface#5608)

* fix civitai bug

* add test

* up

* fix test

* added slow test.

* style

* Update src/diffusers/utils/peft_utils.py

Co-authored-by: Benjamin Bossan <[email protected]>

* Update src/diffusers/utils/peft_utils.py

---------

Co-authored-by: Benjamin Bossan <[email protected]>
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
…huggingface#5608)

* fix civitai bug

* add test

* up

* fix test

* added slow test.

* style

* Update src/diffusers/utils/peft_utils.py

Co-authored-by: Benjamin Bossan <[email protected]>

* Update src/diffusers/utils/peft_utils.py

---------

Co-authored-by: Benjamin Bossan <[email protected]>
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.

Bug when loading LoRA with no alpha value
4 participants