Skip to content

Commit deb1738

Browse files
authored
docs: improve docstring scheduling_sde_ve.py (#14172)
Improve docstring scheduling sde ve
1 parent 0196914 commit deb1738

1 file changed

Lines changed: 49 additions & 14 deletions

File tree

src/diffusers/schedulers/scheduling_sde_ve.py

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ScoreSdeVeScheduler(SchedulerMixin, ConfigMixin):
5050
methods the library implements for all schedulers such as loading and saving.
5151
5252
Args:
53-
num_train_timesteps (`int`, defaults to 1000):
53+
num_train_timesteps (`int`, defaults to 2000):
5454
The number of diffusion steps to train the model.
5555
snr (`float`, defaults to 0.15):
5656
A coefficient weighting the step from the `model_output` sample (from the network) to the random noise.
@@ -85,7 +85,7 @@ def __init__(
8585

8686
self.set_sigmas(num_train_timesteps, sigma_min, sigma_max, sampling_eps)
8787

88-
def scale_model_input(self, sample: torch.Tensor, timestep: int = None) -> torch.Tensor:
88+
def scale_model_input(self, sample: torch.Tensor, timestep: int | None = None) -> torch.Tensor:
8989
"""
9090
Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
9191
current timestep.
@@ -102,7 +102,9 @@ def scale_model_input(self, sample: torch.Tensor, timestep: int = None) -> torch
102102
"""
103103
return sample
104104

105-
def set_timesteps(self, num_inference_steps: int, sampling_eps: float = None, device: str | torch.device = None):
105+
def set_timesteps(
106+
self, num_inference_steps: int, sampling_eps: float | None = None, device: str | torch.device | None = None
107+
):
106108
"""
107109
Sets the continuous timesteps used for the diffusion chain (to be run before inference).
108110
@@ -120,7 +122,11 @@ def set_timesteps(self, num_inference_steps: int, sampling_eps: float = None, de
120122
self.timesteps = torch.linspace(1, sampling_eps, num_inference_steps, device=device)
121123

122124
def set_sigmas(
123-
self, num_inference_steps: int, sigma_min: float = None, sigma_max: float = None, sampling_eps: float = None
125+
self,
126+
num_inference_steps: int,
127+
sigma_min: float | None = None,
128+
sigma_max: float | None = None,
129+
sampling_eps: float | None = None,
124130
):
125131
"""
126132
Sets the noise scales used for the diffusion chain (to be run before inference). The sigmas control the weight
@@ -129,11 +135,11 @@ def set_sigmas(
129135
Args:
130136
num_inference_steps (`int`):
131137
The number of diffusion steps used when generating samples with a pre-trained model.
132-
sigma_min (`float`, optional):
138+
sigma_min (`float`, *optional*):
133139
The initial noise scale value (overrides value given during scheduler instantiation).
134-
sigma_max (`float`, optional):
140+
sigma_max (`float`, *optional*):
135141
The final noise scale value (overrides value given during scheduler instantiation).
136-
sampling_eps (`float`, optional):
142+
sampling_eps (`float`, *optional*):
137143
The final timestep value (overrides value given during scheduler instantiation).
138144
139145
"""
@@ -147,7 +153,21 @@ def set_sigmas(
147153
self.discrete_sigmas = torch.exp(torch.linspace(math.log(sigma_min), math.log(sigma_max), num_inference_steps))
148154
self.sigmas = torch.tensor([sigma_min * (sigma_max / sigma_min) ** t for t in self.timesteps])
149155

150-
def get_adjacent_sigma(self, timesteps, t):
156+
def get_adjacent_sigma(self, timesteps: torch.Tensor, t: torch.Tensor) -> torch.Tensor:
157+
"""
158+
Returns the sigma value corresponding to the previous timestep index. When the timestep is zero (no previous
159+
timestep), returns zero.
160+
161+
Args:
162+
timesteps (`torch.Tensor`):
163+
The tensor of discrete timestep indices.
164+
t (`torch.Tensor`):
165+
The tensor of continuous timestep values.
166+
167+
Returns:
168+
`torch.Tensor`:
169+
The sigma values for the adjacent (previous) timestep index.
170+
"""
151171
return torch.where(
152172
timesteps == 0,
153173
torch.zeros_like(t.to(timesteps.device)),
@@ -241,12 +261,12 @@ def step_correct(
241261
generator (`torch.Generator`, *optional*):
242262
A random number generator.
243263
return_dict (`bool`, *optional*, defaults to `True`):
244-
Whether or not to return a [`~schedulers.scheduling_sde_ve.SdeVeOutput`] or `tuple`.
264+
Whether or not to return a [`~schedulers.scheduling_utils.SchedulerOutput`] or `tuple`.
245265
246266
Returns:
247-
[`~schedulers.scheduling_sde_ve.SdeVeOutput`] or `tuple`:
248-
If return_dict is `True`, [`~schedulers.scheduling_sde_ve.SdeVeOutput`] is returned, otherwise a tuple
249-
is returned where the first element is the sample tensor.
267+
[`~schedulers.scheduling_utils.SchedulerOutput`] or `tuple`:
268+
If return_dict is `True`, [`~schedulers.scheduling_utils.SchedulerOutput`] is returned, otherwise a
269+
tuple is returned where the first element is the sample tensor.
250270
251271
"""
252272
if self.timesteps is None:
@@ -283,7 +303,22 @@ def add_noise(
283303
noise: torch.Tensor,
284304
timesteps: torch.Tensor,
285305
) -> torch.Tensor:
286-
# Make sure sigmas and timesteps have the same device and dtype as original_samples
306+
"""
307+
Add noise to the original samples according to the noise schedule at the specified timesteps. This is the
308+
forward diffusion process.
309+
310+
Args:
311+
original_samples (`torch.Tensor`):
312+
The original samples to which noise will be added.
313+
noise (`torch.Tensor`):
314+
The noise tensor to add to the original samples.
315+
timesteps (`torch.Tensor`):
316+
The timesteps at which to add noise, determining the noise level from the schedule.
317+
318+
Returns:
319+
`torch.Tensor`:
320+
The noisy samples with added noise scaled according to the timestep schedule.
321+
"""
287322
timesteps = timesteps.to(original_samples.device)
288323
sigmas = self.discrete_sigmas.to(original_samples.device)[timesteps]
289324
noise = (
@@ -294,5 +329,5 @@ def add_noise(
294329
noisy_samples = noise + original_samples
295330
return noisy_samples
296331

297-
def __len__(self):
332+
def __len__(self) -> int:
298333
return self.config.num_train_timesteps

0 commit comments

Comments
 (0)