Skip to content

Commit e1c7acf

Browse files
committed
Propagate changes using 'make fix-copies'
1 parent 3f49eb3 commit e1c7acf

11 files changed

+43
-77
lines changed

src/diffusers/schedulers/scheduling_ddim.py

Lines changed: 17 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DDIMSchedulerOutput(BaseOutput):
3838
prev_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
3939
Computed sample `(x_{t-1})` of previous timestep. `prev_sample` should be used as next model input in the
4040
denoising loop.
41-
pred_original_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images, *optional*):
41+
pred_original_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images):
4242
The predicted denoised sample `(x_{0})` based on the model output from the current timestep.
4343
`pred_original_sample` can be used to preview progress or for guidance.
4444
"""
@@ -49,36 +49,36 @@ class DDIMSchedulerOutput(BaseOutput):
4949

5050
# Copied from diffusers.schedulers.scheduling_ddpm.betas_for_alpha_bar
5151
def betas_for_alpha_bar(
52-
num_diffusion_timesteps: int,
53-
max_beta: float = 0.999,
54-
alpha_transform_type: Literal["cosine", "exp"] = "cosine",
55-
) -> torch.Tensor:
52+
num_diffusion_timesteps,
53+
max_beta=0.999,
54+
alpha_transform_type="cosine",
55+
):
5656
"""
5757
Create a beta schedule that discretizes the given alpha_t_bar function, which defines the cumulative product of
5858
(1-beta) over time from t = [0,1].
5959
6060
Contains a function alpha_bar that takes an argument t and transforms it to the cumulative product of (1-beta) up
6161
to that part of the diffusion process.
6262
63+
6364
Args:
64-
num_diffusion_timesteps (`int`):
65-
The number of betas to produce.
66-
max_beta (`float`, defaults to 0.999):
67-
The maximum beta to use; use values lower than 1 to avoid numerical instability.
68-
alpha_transform_type (`Literal["cosine", "exp"]`, defaults to `"cosine"`):
69-
The type of noise schedule for `alpha_bar`. Must be one of `"cosine"` or `"exp"`.
65+
num_diffusion_timesteps (`int`): the number of betas to produce.
66+
max_beta (`float`): the maximum beta to use; use values lower than 1 to
67+
prevent singularities.
68+
alpha_transform_type (`str`, *optional*, default to `cosine`): the type of noise schedule for alpha_bar.
69+
Choose from `cosine` or `exp`
7070
7171
Returns:
72-
`torch.Tensor`: The betas used by the scheduler to step the model outputs.
72+
betas (`np.ndarray`): the betas used by the scheduler to step the model outputs
7373
"""
7474
if alpha_transform_type == "cosine":
7575

76-
def alpha_bar_fn(t: float) -> float:
76+
def alpha_bar_fn(t):
7777
return math.cos((t + 0.008) / 1.008 * math.pi / 2) ** 2
7878

7979
elif alpha_transform_type == "exp":
8080

81-
def alpha_bar_fn(t: float) -> float:
81+
def alpha_bar_fn(t):
8282
return math.exp(t * -12.0)
8383

8484
else:
@@ -281,21 +281,13 @@ def _get_variance(self, timestep: int, prev_timestep: int) -> torch.Tensor:
281281
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMScheduler._threshold_sample
282282
def _threshold_sample(self, sample: torch.Tensor) -> torch.Tensor:
283283
"""
284-
Dynamic thresholding: At each sampling step we set s to a certain percentile absolute pixel value in xt0 (the
284+
"Dynamic thresholding: At each sampling step we set s to a certain percentile absolute pixel value in xt0 (the
285285
prediction of x_0 at timestep t), and if s > 1, then we threshold xt0 to the range [-s, s] and then divide by
286286
s. Dynamic thresholding pushes saturated pixels (those near -1 and 1) inwards, thereby actively preventing
287287
pixels from saturation at each step. We find that dynamic thresholding results in significantly better
288-
photorealism as well as better image-text alignment, especially when using very large guidance weights.
289-
290-
See https://huggingface.co/papers/2205.11487
288+
photorealism as well as better image-text alignment, especially when using very large guidance weights."
291289
292-
Args:
293-
sample (`torch.Tensor`):
294-
The sample to threshold.
295-
296-
Returns:
297-
`torch.Tensor`:
298-
The thresholded sample.
290+
https://huggingface.co/papers/2205.11487
299291
"""
300292
dtype = sample.dtype
301293
batch_size, channels, *remaining_dims = sample.shape
@@ -509,24 +501,6 @@ def add_noise(
509501
noise: torch.Tensor,
510502
timesteps: torch.IntTensor,
511503
) -> torch.Tensor:
512-
"""
513-
Add noise to the original samples according to the noise magnitude at each timestep.
514-
515-
This implements the forward diffusion process using the formula: `noisy_sample = sqrt(alpha_prod) *
516-
original_sample + sqrt(1 - alpha_prod) * noise`
517-
518-
Args:
519-
original_samples (`torch.Tensor`):
520-
The original clean samples to which noise will be added.
521-
noise (`torch.Tensor`):
522-
The noise tensor to add, typically sampled from a Gaussian distribution.
523-
timesteps (`torch.IntTensor`):
524-
The timesteps indicating the noise level from the diffusion schedule.
525-
526-
Returns:
527-
`torch.Tensor`:
528-
The noisy samples with noise added according to the timestep schedule.
529-
"""
530504
# Make sure alphas_cumprod and timestep have same device and dtype as original_samples
531505
# Move the self.alphas_cumprod to device to avoid redundant CPU to GPU data movement
532506
# for the subsequent add_noise calls
@@ -549,27 +523,6 @@ def add_noise(
549523

550524
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMScheduler.get_velocity
551525
def get_velocity(self, sample: torch.Tensor, noise: torch.Tensor, timesteps: torch.IntTensor) -> torch.Tensor:
552-
"""
553-
Compute the velocity prediction for v-prediction models.
554-
555-
The velocity is computed using the formula: `velocity = sqrt(alpha_prod) * noise - sqrt(1 - alpha_prod) *
556-
sample`
557-
558-
This is used in v-prediction models where the model directly predicts the velocity instead of the noise or the
559-
sample. See section 2.4 of [Imagen Video](https://huggingface.co/papers/2210.02303) paper.
560-
561-
Args:
562-
sample (`torch.Tensor`):
563-
The input sample (x_t) at the current timestep.
564-
noise (`torch.Tensor`):
565-
The noise tensor corresponding to the sample.
566-
timesteps (`torch.IntTensor`):
567-
The timesteps at which to compute the velocity.
568-
569-
Returns:
570-
`torch.Tensor`:
571-
The velocity prediction computed from the sample and noise at the given timesteps.
572-
"""
573526
# Make sure alphas_cumprod and timestep have same device and dtype as sample
574527
self.alphas_cumprod = self.alphas_cumprod.to(device=sample.device)
575528
alphas_cumprod = self.alphas_cumprod.to(dtype=sample.dtype)

src/diffusers/schedulers/scheduling_ddim_inverse.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ def rescale_zero_terminal_snr(betas):
9595
"""
9696
Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)
9797
98-
9998
Args:
10099
betas (`torch.Tensor`):
101100
the betas that the scheduler is being initialized with.

src/diffusers/schedulers/scheduling_ddim_parallel.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def rescale_zero_terminal_snr(betas):
9797
"""
9898
Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)
9999
100-
101100
Args:
102101
betas (`torch.Tensor`):
103102
the betas that the scheduler is being initialized with.
@@ -194,17 +193,17 @@ def __init__(
194193
num_train_timesteps: int = 1000,
195194
beta_start: float = 0.0001,
196195
beta_end: float = 0.02,
197-
beta_schedule: str = "linear",
196+
beta_schedule: Literal["linear", "scaled_linear", "squaredcos_cap_v2"] = "linear",
198197
trained_betas: Optional[Union[np.ndarray, List[float]]] = None,
199198
clip_sample: bool = True,
200199
set_alpha_to_one: bool = True,
201200
steps_offset: int = 0,
202-
prediction_type: str = "epsilon",
201+
prediction_type: Literal["epsilon", "sample", "v_prediction"] = "epsilon",
203202
thresholding: bool = False,
204203
dynamic_thresholding_ratio: float = 0.995,
205204
clip_sample_range: float = 1.0,
206205
sample_max_value: float = 1.0,
207-
timestep_spacing: str = "leading",
206+
timestep_spacing: Literal["leading", "trailing", "linspace"] = "leading",
208207
rescale_betas_zero_snr: bool = False,
209208
):
210209
if trained_betas is not None:
@@ -324,6 +323,11 @@ def set_timesteps(self, num_inference_steps: int, device: Union[str, torch.devic
324323
Args:
325324
num_inference_steps (`int`):
326325
The number of diffusion steps used when generating samples with a pre-trained model.
326+
device (`Union[str, torch.device]`, *optional*):
327+
The device to use for the timesteps.
328+
329+
Raises:
330+
ValueError: If `num_inference_steps` is larger than `self.config.num_train_timesteps`.
327331
"""
328332

329333
if num_inference_steps > self.config.num_train_timesteps:

src/diffusers/schedulers/scheduling_ddpm.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ def rescale_zero_terminal_snr(betas):
9494
"""
9595
Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)
9696
97-
9897
Args:
9998
betas (`torch.Tensor`):
10099
the betas that the scheduler is being initialized with.

src/diffusers/schedulers/scheduling_ddpm_parallel.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ def rescale_zero_terminal_snr(betas):
9696
"""
9797
Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)
9898
99-
10099
Args:
101100
betas (`torch.Tensor`):
102101
the betas that the scheduler is being initialized with.

src/diffusers/schedulers/scheduling_dpmsolver_multistep.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def rescale_zero_terminal_snr(betas):
8080
"""
8181
Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)
8282
83-
8483
Args:
8584
betas (`torch.Tensor`):
8685
the betas that the scheduler is being initialized with.

src/diffusers/schedulers/scheduling_euler_ancestral_discrete.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def rescale_zero_terminal_snr(betas):
9797
"""
9898
Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)
9999
100-
101100
Args:
102101
betas (`torch.Tensor`):
103102
the betas that the scheduler is being initialized with.

src/diffusers/schedulers/scheduling_euler_discrete.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ def rescale_zero_terminal_snr(betas):
100100
"""
101101
Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)
102102
103-
104103
Args:
105104
betas (`torch.Tensor`):
106105
the betas that the scheduler is being initialized with.

src/diffusers/schedulers/scheduling_lcm.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ def rescale_zero_terminal_snr(betas: torch.Tensor) -> torch.Tensor:
9999
"""
100100
Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)
101101
102-
103102
Args:
104103
betas (`torch.Tensor`):
105104
the betas that the scheduler is being initialized with.

src/diffusers/schedulers/scheduling_tcd.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ def rescale_zero_terminal_snr(betas: torch.Tensor) -> torch.Tensor:
9898
"""
9999
Rescales betas to have zero terminal SNR Based on https://huggingface.co/papers/2305.08891 (Algorithm 1)
100100
101-
102101
Args:
103102
betas (`torch.Tensor`):
104103
the betas that the scheduler is being initialized with.
@@ -316,6 +315,24 @@ def scale_model_input(self, sample: torch.Tensor, timestep: Optional[int] = None
316315

317316
# Copied from diffusers.schedulers.scheduling_ddim.DDIMScheduler._get_variance
318317
def _get_variance(self, timestep, prev_timestep):
318+
"""
319+
Computes the variance of the noise added at a given diffusion step.
320+
321+
For a given `timestep` and its previous step, this method calculates the variance as defined in DDIM/DDPM
322+
literature:
323+
var_t = (beta_prod_t_prev / beta_prod_t) * (1 - alpha_prod_t / alpha_prod_t_prev)
324+
where alpha_prod and beta_prod are cumulative products of alphas and betas, respectively.
325+
326+
Args:
327+
timestep (`int`):
328+
The current timestep in the diffusion process.
329+
prev_timestep (`int`):
330+
The previous timestep in the diffusion process. If negative, uses `final_alpha_cumprod`.
331+
332+
Returns:
333+
`torch.Tensor`:
334+
The variance for the current timestep.
335+
"""
319336
alpha_prod_t = self.alphas_cumprod[timestep]
320337
alpha_prod_t_prev = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.final_alpha_cumprod
321338
beta_prod_t = 1 - alpha_prod_t

0 commit comments

Comments
 (0)