-
Notifications
You must be signed in to change notification settings - Fork 6.1k
[Refactor] Remove set_seed #289
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
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a733d72
[Refactor] Remove set_seed and class attributes
patrickvonplaten 4a559e9
apply anton's suggestiosn
patrickvonplaten 9aacfdd
fix
patrickvonplaten 3d093c9
Apply suggestions from code review
patrickvonplaten 59d1e4c
up
patrickvonplaten f223486
Merge branch 'refactors' of https://github.com/huggingface/diffusers …
patrickvonplaten 0d1ca30
update
patrickvonplaten 27be6fc
Merge branch 'refactors' of https://github.com/huggingface/diffusers …
patrickvonplaten 109c90f
make style
patrickvonplaten 6a905e4
Apply suggestions from code review
patrickvonplaten 92829b3
make fix-copies
patrickvonplaten 8d13b9e
make style
patrickvonplaten 761130a
make style and new copies
patrickvonplaten a37da3a
Merge branch 'main' of https://github.com/huggingface/diffusers into …
patrickvonplaten 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 |
---|---|---|
|
@@ -14,8 +14,10 @@ | |
|
||
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch | ||
|
||
import warnings | ||
|
||
# TODO(Patrick, Anton, Suraj) - make scheduler framework indepedent and clean-up a bit | ||
patrickvonplaten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from typing import Union | ||
from typing import Optional, Union | ||
|
||
import numpy as np | ||
import torch | ||
|
@@ -98,6 +100,9 @@ def get_adjacent_sigma(self, timesteps, t): | |
raise ValueError(f"`self.tensor_format`: {self.tensor_format} is not valid.") | ||
|
||
def set_seed(self, seed): | ||
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. We should not work with
|
||
warnings.warn( | ||
"The method `set_seed` is outdated. Please consider passing a generator instead.", DeprecationWarning | ||
patrickvonplaten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
tensor_format = getattr(self, "tensor_format", "pt") | ||
if tensor_format == "np": | ||
np.random.seed(seed) | ||
|
@@ -111,14 +116,14 @@ def step_pred( | |
model_output: Union[torch.FloatTensor, np.ndarray], | ||
timestep: int, | ||
sample: Union[torch.FloatTensor, np.ndarray], | ||
seed=None, | ||
generator: Optional[torch.Generator] = None, | ||
**kwargs, | ||
): | ||
""" | ||
Predict the sample at the previous timestep by reversing the SDE. | ||
""" | ||
if seed is not None: | ||
self.set_seed(seed) | ||
# TODO(Patrick) non-PyTorch | ||
if "seed" in kwargs and kwargs["seed"] is not None: | ||
self.set_seed(kwargs["seed"]) | ||
|
||
if self.timesteps is None: | ||
raise ValueError( | ||
|
@@ -140,7 +145,7 @@ def step_pred( | |
drift = drift - diffusion[:, None, None, None] ** 2 * model_output | ||
|
||
# equation 6: sample noise for the diffusion term of | ||
noise = self.randn_like(sample) | ||
noise = self.randn_like(sample, generator=generator) | ||
prev_sample_mean = sample - drift # subtract because `dt` is a small negative timestep | ||
# TODO is the variable diffusion the correct scaling term for the noise? | ||
prev_sample = prev_sample_mean + diffusion[:, None, None, None] * noise # add impact of diffusion field g | ||
|
@@ -151,14 +156,15 @@ def step_correct( | |
self, | ||
model_output: Union[torch.FloatTensor, np.ndarray], | ||
sample: Union[torch.FloatTensor, np.ndarray], | ||
seed=None, | ||
generator: Optional[torch.Generator] = None, | ||
**kwargs, | ||
): | ||
""" | ||
Correct the predicted sample based on the output model_output of the network. This is often run repeatedly | ||
after making the prediction for the previous timestep. | ||
""" | ||
if seed is not None: | ||
self.set_seed(seed) | ||
if "seed" in kwargs and kwargs["seed"] is not None: | ||
self.set_seed(kwargs["seed"]) | ||
|
||
if self.timesteps is None: | ||
raise ValueError( | ||
|
@@ -167,7 +173,7 @@ def step_correct( | |
|
||
# For small batch sizes, the paper "suggest replacing norm(z) with sqrt(d), where d is the dim. of z" | ||
# sample noise for correction | ||
noise = self.randn_like(sample) | ||
noise = self.randn_like(sample, generator=generator) | ||
|
||
# compute step size from the model_output, the noise, and the snr | ||
grad_norm = self.norm(model_output) | ||
|
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.
let's pass the generator here