Skip to content

[Refactor] refactor loaders.py to make it cleaner and leaner. - #5771

Merged
patrickvonplaten merged 26 commits into
mainfrom
refactor/loaders
Nov 14, 2023
Merged

[Refactor] refactor loaders.py to make it cleaner and leaner.#5771
patrickvonplaten merged 26 commits into
mainfrom
refactor/loaders

Conversation

@sayakpaul

@sayakpaul sayakpaul commented Nov 12, 2023

Copy link
Copy Markdown
Member

What does this PR do?

Fixes: #5669

I had to move some import statements to not cause circular dependencies.

@sayakpaul sayakpaul changed the title [WIP][Refactor] refactor loaders.py to make it cleaner and leaner. [WIP][Refactor] refactor loaders.py to make it cleaner and leaner. Nov 13, 2023
Comment thread examples/text_to_image/train_text_to_image_flax.py Outdated
Comment thread examples/text_to_image/train_text_to_image_lora_sdxl.py Outdated
@@ -0,0 +1,201 @@
# Copyright 2023 The HuggingFace Team. All rights reserved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we remove this file and put everything in the corresponding classes? I think most things here should go into lora.py

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just kept it because of AttnProcLayers as it is used by custom diffusion.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok

Comment thread src/diffusers/loaders/lora.py Outdated
set_adapter_layers,
set_weights_and_activate_adapters,
)
from .utils import PatchedLoraProjection, text_encoder_attn_modules, text_encoder_mlp_modules

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's define all these classes directly here. The classes are only used in lora.py anyways no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

text_encoder_lora_state_dict() is used by some training scripts. I moved it to lora.py as well and made necessary changes. Is that too aggressive?

@patrickvonplaten patrickvonplaten left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me! Let's make sure we run all slow tests related to text inv, lora and single file and unet2D. Especially the slow tests of:

Comment thread src/diffusers/loaders.py Outdated
@patrickvonplaten

Copy link
Copy Markdown
Contributor

Very cool refactor - let's make sure we don't braek

Looks good to me! Let's make sure we run all slow tests related to text inv, lora and single file and unet2D. Especially the slow tests of:

Once all these slow tests run correctly we can merge this IMO

@sayakpaul
sayakpaul marked this pull request as ready for review November 13, 2023 14:47
@sayakpaul sayakpaul changed the title [WIP][Refactor] refactor loaders.py to make it cleaner and leaner. [Refactor] refactor loaders.py to make it cleaner and leaner. Nov 14, 2023
@HuggingFaceDocBuilderDev

HuggingFaceDocBuilderDev commented Nov 14, 2023

Copy link
Copy Markdown

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

@sayakpaul

Copy link
Copy Markdown
Member Author

@patrickvonplaten I have run all the tests you asked for, and they pass :=)

I'd appreciate it very much if you could go through the PR one more time rigorously and let me know your feedback.

Comment thread src/diffusers/models/unet_2d_condition.py Outdated
Comment thread src/diffusers/models/unet_3d_condition.py Outdated
Comment thread src/diffusers/models/autoencoder_kl.py Outdated
@patrickvonplaten

Copy link
Copy Markdown
Contributor

Added two functions to src/diffusers/loaders/__init__.py to make sure the imports are backwards compatible for imports from example scripts. Apart from this everything looks good to me! Let's try to get this in asap to avoid merge conflicts.

@sayakpaul

Copy link
Copy Markdown
Member Author

Thanks! Will merge once the CI is green then!

@patrickvonplaten
patrickvonplaten merged commit ded93f7 into main Nov 14, 2023
@stevhliu stevhliu mentioned this pull request Nov 15, 2023
1 task
@kashif
kashif deleted the refactor/loaders branch December 5, 2023 08:59
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
…ingface#5771)

* refactor loaders.py to make it cleaner and leaner.

* refactor loaders init

* inits.

* textual inversion to the init.

* inits.

* remove certain modules from the main init.

* AttnProcsLayers

* fix imports

* avoid circular import.

* fix circular import pt 2.

* address PR comments

* imports

* fix: imports.

* remove from main init for avoiding circular deps.

* remove spurious deps.

* fix-copies.

* fix imports.

* more debug

* more debug

* Apply suggestions from code review

* Apply suggestions from code review

---------

Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
…ingface#5771)

* refactor loaders.py to make it cleaner and leaner.

* refactor loaders init

* inits.

* textual inversion to the init.

* inits.

* remove certain modules from the main init.

* AttnProcsLayers

* fix imports

* avoid circular import.

* fix circular import pt 2.

* address PR comments

* imports

* fix: imports.

* remove from main init for avoiding circular deps.

* remove spurious deps.

* fix-copies.

* fix imports.

* more debug

* more debug

* Apply suggestions from code review

* Apply suggestions from code review

---------

Co-authored-by: Patrick von Platen <patrick.v.platen@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.

[Refactor] Decouple the different loaders and create a separate loaders module

3 participants