Skip to content

debug EMAModel.from_pretrained() #9809

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

Closed
wants to merge 2 commits into from
Closed

debug EMAModel.from_pretrained() #9809

wants to merge 2 commits into from

Conversation

chenguolin
Copy link
Contributor

@chenguolin chenguolin commented Oct 30, 2024

What does this PR do?

model_cls.load_config() is modified to model_cls.from_config().

When setting return_unused_kwargs=True, for model_cls.load_config(), the returned unused kwargs are referred to as unused input args, instead of unused args for model_cls initialization. So the returned ema_kwargs are always empty ({}).

model_cls.from_config() will return the real unused args for model initialization, which are the expected ema_kwargs.

Before submitting

Who can review?

General functionalities: @sayakpaul @yiyixuxu @DN6

`model_cls.load_config()` is modified to **`model_cls.from_config()`**.

When setting `return_unused_kwargs=True`, for `model_cls.load_config()`, the returned unused kwargs are referred to as **unused input args**, instead of unused args for `model_cls` initialization.
So the returned `ema_kwargs` are always empty (`{}`).

**`model_cls.from_config()`** will return the real **unused args for model initialization**, which are the expected `ema_kwargs`.
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.

@chenguolin
Copy link
Contributor Author

Could you add a test for this here? https://github.com/huggingface/diffusers/blob/main/tests/others/test_ema.py

Hi @sayakpaul , how can I add a test here? I'm not familiar with this.

@sayakpaul
Copy link
Member

Add a test case like

def test_serialization(self):

that accounts for the changes you introduced in this PR?

@chenguolin
Copy link
Contributor Author

This PR only affects training and resuming with EMAModel and also requires loading a JSON file, so this bug might not be that obvious and easy to write a test case.

Let's say you have a file named config.json:

{
    "decay": 0.9999,
    "inv_gamma": 1.0
}

You can run:

from diffusers import UNet2DConditionModel

model_cls = UNet2DConditionModel
path = "config.json"

_, ema_kwargs = model_cls.from_config(path, return_unused_kwargs=True)
print(ema_kwargs)  # Output: {"decay": 0.9999, "inv_gamma": 1.0}: ✅

_, ema_kwargs = model_cls.load_config(path, return_unused_kwargs=True)
print(ema_kwargs)  # Output: {}: ❌

@sayakpaul
Copy link
Member

Okay, we can add this as a test case then maybe?

@chenguolin
Copy link
Contributor Author

OK, thank you 😊. You could consider merging this PR then. I have tested it in my own training process, and this bug has confused me for quite some time.

@sayakpaul
Copy link
Member

Sorry I think you misunderstood. I am suggesting you to add a test case to demonstrate the use case you mentioned in #9809 (comment).

@chenguolin
Copy link
Contributor Author

OK. I tried.

But I found it's not that easy to write a test_from_pretrained() for EMAModelTests, as EMAModel.from_pretrained() requires loading both a model checkpoint and the corresponding config json file including EMA args (such as decay and optimization_step), which are not available in "hf-internal-testing/tiny-stable-diffusion-pipe".

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@SahilCarterr
Copy link
Contributor

SahilCarterr commented Oct 30, 2024

i already opened a PR for this #9779 with the test case @sayakpaul @chenguolin

@chenguolin
Copy link
Contributor Author

i already opened a PR for this #9779 with the test case @sayakpaul @chenguolin

It looks good :)

@chenguolin chenguolin closed this by deleting the head repository Nov 4, 2024
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.

4 participants