-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix diffusion inferers to support diffusers-style schedulers #8870
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
Open
ugbotueferhire
wants to merge
2
commits into
Project-MONAI:dev
Choose a base branch
from
ugbotueferhire:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+157
−12
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
|
|
@@ -313,6 +313,33 @@ | |
| ], | ||
| ] | ||
|
|
||
| TEST_CASES_DIFFUSERS = [TEST_CASES[0]] | ||
|
|
||
|
|
||
| class DiffusersLikeSchedulerOutput: | ||
| def __init__(self, prev_sample: torch.Tensor, pred_original_sample: torch.Tensor) -> None: | ||
| self.prev_sample = prev_sample | ||
| self.pred_original_sample = pred_original_sample | ||
|
|
||
|
|
||
| class DiffusersStyleDDPMScheduler(DDPMScheduler): | ||
| def step( | ||
| self, | ||
| model_output: torch.Tensor, | ||
| timestep: int, | ||
| sample: torch.Tensor, | ||
| generator: torch.Generator | None = None, | ||
| return_dict: bool = True, | ||
| ): | ||
| prev_sample, pred_original_sample = super().step( | ||
| model_output=model_output, timestep=timestep, sample=sample, generator=generator | ||
| ) | ||
| if return_dict: | ||
| return DiffusersLikeSchedulerOutput( | ||
| prev_sample=prev_sample, pred_original_sample=pred_original_sample | ||
| ) | ||
| return prev_sample, pred_original_sample | ||
|
|
||
|
Comment on lines
+319
to
+342
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as comment for test_diffusion_inferer. |
||
|
|
||
| class TestDiffusionSamplingInferer(unittest.TestCase): | ||
| @parameterized.expand(TEST_CASES) | ||
|
|
@@ -414,6 +441,37 @@ def test_sample_shape( | |
| ) | ||
| self.assertEqual(sample.shape, input_shape) | ||
|
|
||
| @parameterized.expand(TEST_CASES_DIFFUSERS) | ||
| @skipUnless(has_einops, "Requires einops") | ||
| def test_diffusers_style_ddpm_sample_shape( | ||
| self, ae_model_type, autoencoder_params, dm_model_type, stage_2_params, input_shape, latent_shape | ||
| ): | ||
| if ae_model_type == "AutoencoderKL": | ||
| stage_1 = AutoencoderKL(**autoencoder_params) | ||
| else: | ||
| stage_1 = VQVAE(**autoencoder_params) | ||
|
|
||
| if dm_model_type == "SPADEDiffusionModelUNet": | ||
| stage_2 = SPADEDiffusionModelUNet(**stage_2_params) | ||
| else: | ||
| stage_2 = DiffusionModelUNet(**stage_2_params) | ||
|
|
||
| device = "cuda:0" if torch.cuda.is_available() else "cpu" | ||
| stage_1.to(device) | ||
| stage_2.to(device) | ||
| stage_1.eval() | ||
| stage_2.eval() | ||
|
|
||
| noise = torch.randn(latent_shape).to(device) | ||
| scheduler = DiffusersStyleDDPMScheduler(num_train_timesteps=1000) | ||
| inferer = LatentDiffusionInferer(scheduler=scheduler, scale_factor=1.0) | ||
| scheduler.set_timesteps(num_inference_steps=10) | ||
|
|
||
| sample = inferer.sample( | ||
| input_noise=noise, autoencoder_model=stage_1, diffusion_model=stage_2, scheduler=scheduler | ||
| ) | ||
| self.assertEqual(sample.shape, input_shape) | ||
|
|
||
| @parameterized.expand(TEST_CASES) | ||
| @skipUnless(has_einops, "Requires einops") | ||
| def test_sample_shape_with_cfg( | ||
|
|
||
Oops, something went wrong.
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.
For a single method, I don't think this class is necessary. We can have this as a method on the main test class. But not 100% sure @ericspod