-
Notifications
You must be signed in to change notification settings - Fork 6.5k
[Tests] Adds a test suite for EMAModel
#2530
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
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
31183ee
ema test cases.
sayakpaul 313414a
debugging maessages.
sayakpaul f47487f
debugging maessages.
sayakpaul 2d0f7b5
add: tests for ema.
sayakpaul 17994e5
fix: optimization_step arg,
sayakpaul 49aed34
handle device placement.
sayakpaul 258bbe1
Apply suggestions from code review
sayakpaul f4c4e0c
remove del and gc.
sayakpaul 8850275
address PR feedback.
sayakpaul d6241d2
add: tests for serialization.
sayakpaul aee2846
fix: typos.
sayakpaul b1a19d9
Merge branch 'main' into tests/ema
patrickvonplaten 0ead038
Merge branch 'main' into tests/ema
sayakpaul 8f2a75f
skip_mps to serialization.
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 hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| # coding=utf-8 | ||
| # Copyright 2023 HuggingFace Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import tempfile | ||
| import unittest | ||
|
|
||
| import torch | ||
|
|
||
| from diffusers import UNet2DConditionModel | ||
| from diffusers.training_utils import EMAModel | ||
| from diffusers.utils.testing_utils import skip_mps, torch_device | ||
|
|
||
|
|
||
| class EMAModelTests(unittest.TestCase): | ||
| model_id = "hf-internal-testing/tiny-stable-diffusion-pipe" | ||
| batch_size = 1 | ||
| prompt_length = 77 | ||
| text_encoder_hidden_dim = 32 | ||
| num_in_channels = 4 | ||
| latent_height = latent_width = 64 | ||
| generator = torch.manual_seed(0) | ||
|
|
||
| def get_models(self, decay=0.9999): | ||
| unet = UNet2DConditionModel.from_pretrained(self.model_id, subfolder="unet", device=torch_device) | ||
| ema_unet = UNet2DConditionModel.from_pretrained(self.model_id, subfolder="unet") | ||
| ema_unet = EMAModel( | ||
| ema_unet.parameters(), decay=decay, model_cls=UNet2DConditionModel, model_config=ema_unet.config | ||
| ) | ||
| return unet, ema_unet | ||
|
|
||
| def get_dummy_inputs(self): | ||
| noisy_latents = torch.randn( | ||
| self.batch_size, self.num_in_channels, self.latent_height, self.latent_width, generator=self.generator | ||
| ).to(torch_device) | ||
| timesteps = torch.randint(0, 1000, size=(self.batch_size,), generator=self.generator).to(torch_device) | ||
| encoder_hidden_states = torch.randn( | ||
| self.batch_size, self.prompt_length, self.text_encoder_hidden_dim, generator=self.generator | ||
| ).to(torch_device) | ||
| return noisy_latents, timesteps, encoder_hidden_states | ||
|
|
||
| def simulate_backprop(self, unet): | ||
| updated_state_dict = {} | ||
| for k, param in unet.state_dict().items(): | ||
| updated_param = torch.randn_like(param) + (param * torch.randn_like(param)) | ||
| updated_state_dict.update({k: updated_param}) | ||
| unet.load_state_dict(updated_state_dict) | ||
| return unet | ||
|
|
||
| def test_optimization_steps_updated(self): | ||
| unet, ema_unet = self.get_models() | ||
| # Take the first (hypothetical) EMA step. | ||
| ema_unet.step(unet.parameters()) | ||
| assert ema_unet.optimization_step == 1 | ||
|
|
||
| # Take two more. | ||
| for _ in range(2): | ||
| ema_unet.step(unet.parameters()) | ||
| assert ema_unet.optimization_step == 3 | ||
|
|
||
| def test_shadow_params_not_updated(self): | ||
| unet, ema_unet = self.get_models() | ||
| # Since the `unet` is not being updated (i.e., backprop'd) | ||
| # there won't be any difference between the `params` of `unet` | ||
| # and `ema_unet` even if we call `ema_unet.step(unet.parameters())`. | ||
| ema_unet.step(unet.parameters()) | ||
| orig_params = list(unet.parameters()) | ||
| for s_param, param in zip(ema_unet.shadow_params, orig_params): | ||
| assert torch.allclose(s_param, param) | ||
|
|
||
| # The above holds true even if we call `ema.step()` multiple times since | ||
| # `unet` params are still not being updated. | ||
| for _ in range(4): | ||
| ema_unet.step(unet.parameters()) | ||
| for s_param, param in zip(ema_unet.shadow_params, orig_params): | ||
| assert torch.allclose(s_param, param) | ||
|
|
||
| def test_shadow_params_updated(self): | ||
| unet, ema_unet = self.get_models() | ||
| # Here we simulate the parameter updates for `unet`. Since there might | ||
| # be some parameters which are initialized to zero we take extra care to | ||
| # initialize their values to something non-zero before the multiplication. | ||
| unet_pseudo_updated_step_one = self.simulate_backprop(unet) | ||
|
|
||
| # Take the EMA step. | ||
| ema_unet.step(unet_pseudo_updated_step_one.parameters()) | ||
|
|
||
| # Now the EMA'd parameters won't be equal to the original model parameters. | ||
| orig_params = list(unet_pseudo_updated_step_one.parameters()) | ||
| for s_param, param in zip(ema_unet.shadow_params, orig_params): | ||
| assert ~torch.allclose(s_param, param) | ||
|
|
||
| # Ensure this is the case when we take multiple EMA steps. | ||
| for _ in range(4): | ||
| ema_unet.step(unet.parameters()) | ||
| for s_param, param in zip(ema_unet.shadow_params, orig_params): | ||
| assert ~torch.allclose(s_param, param) | ||
|
|
||
| def test_consecutive_shadow_params_updated(self): | ||
| # If we call EMA step after a backpropagation consecutively for two times, | ||
| # the shadow params from those two steps should be different. | ||
| unet, ema_unet = self.get_models() | ||
|
|
||
| # First backprop + EMA | ||
| unet_step_one = self.simulate_backprop(unet) | ||
| ema_unet.step(unet_step_one.parameters()) | ||
| step_one_shadow_params = ema_unet.shadow_params | ||
|
|
||
| # Second backprop + EMA | ||
| unet_step_two = self.simulate_backprop(unet_step_one) | ||
| ema_unet.step(unet_step_two.parameters()) | ||
| step_two_shadow_params = ema_unet.shadow_params | ||
|
|
||
| for step_one, step_two in zip(step_one_shadow_params, step_two_shadow_params): | ||
| assert ~torch.allclose(step_one, step_two) | ||
|
|
||
| def test_zero_decay(self): | ||
| # If there's no decay even if there are backprops, EMA steps | ||
| # won't take any effect i.e., the shadow params would remain the | ||
| # same. | ||
| unet, ema_unet = self.get_models(decay=0.0) | ||
| unet_step_one = self.simulate_backprop(unet) | ||
| ema_unet.step(unet_step_one.parameters()) | ||
| step_one_shadow_params = ema_unet.shadow_params | ||
|
|
||
| unet_step_two = self.simulate_backprop(unet_step_one) | ||
| ema_unet.step(unet_step_two.parameters()) | ||
| step_two_shadow_params = ema_unet.shadow_params | ||
|
|
||
| for step_one, step_two in zip(step_one_shadow_params, step_two_shadow_params): | ||
| assert torch.allclose(step_one, step_two) | ||
|
|
||
| @skip_mps | ||
| def test_serialization(self): | ||
| unet, ema_unet = self.get_models() | ||
| noisy_latents, timesteps, encoder_hidden_states = self.get_dummy_inputs() | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| ema_unet.save_pretrained(tmpdir) | ||
| loaded_unet = UNet2DConditionModel.from_pretrained(tmpdir, model_cls=UNet2DConditionModel) | ||
|
|
||
| # Since no EMA step has been performed the outputs should match. | ||
| output = unet(noisy_latents, timesteps, encoder_hidden_states).sample | ||
| output_loaded = loaded_unet(noisy_latents, timesteps, encoder_hidden_states).sample | ||
|
|
||
| assert torch.allclose(output, output_loaded) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.