Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 101 additions & 13 deletions src/diffusers/schedulers/scheduling_cosine_dpmsolver_multistep.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ def set_begin_index(self, begin_index: int = 0):

# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler.precondition_inputs
def precondition_inputs(self, sample, sigma):
"""
Precondition the input sample by scaling it according to the EDM formulation.

Args:
sample (`torch.Tensor`):
The input sample tensor to precondition.
sigma (`float` or `torch.Tensor`):
The current sigma (noise level) value.

Returns:
`torch.Tensor`:
The scaled input sample.
"""
c_in = self._get_conditioning_c_in(sigma)
scaled_sample = sample * c_in
return scaled_sample
Expand All @@ -155,7 +168,27 @@ def precondition_noise(self, sigma):
return sigma.atan() / math.pi * 2

# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler.precondition_outputs
def precondition_outputs(self, sample, model_output, sigma):
def precondition_outputs(
self,
sample: torch.Tensor,
model_output: torch.Tensor,
sigma: Union[float, torch.Tensor],
) -> torch.Tensor:
"""
Precondition the model outputs according to the EDM formulation.

Args:
sample (`torch.Tensor`):
The input sample tensor.
model_output (`torch.Tensor`):
The direct output from the learned diffusion model.
sigma (`float` or `torch.Tensor`):
The current sigma (noise level) value.

Returns:
`torch.Tensor`:
The denoised sample computed by combining the skip connection and output scaling.
"""
sigma_data = self.config.sigma_data
c_skip = sigma_data**2 / (sigma**2 + sigma_data**2)

Expand All @@ -173,13 +206,13 @@ def precondition_outputs(self, sample, model_output, sigma):
# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler.scale_model_input
def scale_model_input(self, sample: torch.Tensor, timestep: Union[float, torch.Tensor]) -> torch.Tensor:
"""
Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
current timestep. Scales the denoising model input by `(sigma**2 + 1) ** 0.5` to match the Euler algorithm.
Scale the denoising model input to match the Euler algorithm. Ensures interchangeability with schedulers that
need to scale the denoising model input depending on the current timestep.

Args:
sample (`torch.Tensor`):
The input sample.
timestep (`int`, *optional*):
The input sample tensor.
timestep (`float` or `torch.Tensor`):
The current timestep in the diffusion chain.

Returns:
Expand Down Expand Up @@ -242,8 +275,27 @@ def set_timesteps(self, num_inference_steps: int = None, device: Union[str, torc
self.noise_sampler = None

# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler._compute_karras_sigmas
def _compute_karras_sigmas(self, ramp, sigma_min=None, sigma_max=None) -> torch.Tensor:
"""Constructs the noise schedule of Karras et al. (2022)."""
def _compute_karras_sigmas(
self,
ramp: torch.Tensor,
sigma_min: Optional[float] = None,
sigma_max: Optional[float] = None,
) -> torch.Tensor:
"""
Construct the noise schedule of [Karras et al. (2022)](https://huggingface.co/papers/2206.00364).

Args:
ramp (`torch.Tensor`):
A tensor of values in [0, 1] representing the interpolation positions.
sigma_min (`float`, *optional*):
Minimum sigma value. If `None`, uses `self.config.sigma_min`.
sigma_max (`float`, *optional*):
Maximum sigma value. If `None`, uses `self.config.sigma_max`.

Returns:
`torch.Tensor`:
The computed Karras sigma schedule.
"""
sigma_min = sigma_min or self.config.sigma_min
sigma_max = sigma_max or self.config.sigma_max

Expand All @@ -254,10 +306,27 @@ def _compute_karras_sigmas(self, ramp, sigma_min=None, sigma_max=None) -> torch.
return sigmas

# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler._compute_exponential_sigmas
def _compute_exponential_sigmas(self, ramp, sigma_min=None, sigma_max=None) -> torch.Tensor:
"""Implementation closely follows k-diffusion.

def _compute_exponential_sigmas(
self,
ramp: torch.Tensor,
sigma_min: Optional[float] = None,
sigma_max: Optional[float] = None,
) -> torch.Tensor:
"""
Compute the exponential sigma schedule. Implementation closely follows k-diffusion:
https://github.com/crowsonkb/k-diffusion/blob/6ab5146d4a5ef63901326489f31f1d8e7dd36b48/k_diffusion/sampling.py#L26

Args:
ramp (`torch.Tensor`):
A tensor of values representing the interpolation positions.
sigma_min (`float`, *optional*):
Minimum sigma value. If `None`, uses `self.config.sigma_min`.
sigma_max (`float`, *optional*):
Maximum sigma value. If `None`, uses `self.config.sigma_max`.

Returns:
`torch.Tensor`:
The computed exponential sigma schedule.
"""
sigma_min = sigma_min or self.config.sigma_min
sigma_max = sigma_max or self.config.sigma_max
Expand Down Expand Up @@ -354,7 +423,10 @@ def dpm_solver_first_order_update(
`torch.Tensor`:
The sample tensor at the previous timestep.
"""
sigma_t, sigma_s = self.sigmas[self.step_index + 1], self.sigmas[self.step_index]
sigma_t, sigma_s = (
self.sigmas[self.step_index + 1],
self.sigmas[self.step_index],
)
alpha_t, sigma_t = self._sigma_to_alpha_sigma_t(sigma_t)
alpha_s, sigma_s = self._sigma_to_alpha_sigma_t(sigma_s)
lambda_t = torch.log(alpha_t) - torch.log(sigma_t)
Expand Down Expand Up @@ -430,7 +502,9 @@ def multistep_dpm_solver_second_order_update(

# Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.index_for_timestep
def index_for_timestep(
self, timestep: Union[int, torch.Tensor], schedule_timesteps: Optional[torch.Tensor] = None
self,
timestep: Union[int, torch.Tensor],
schedule_timesteps: Optional[torch.Tensor] = None,
) -> int:
"""
Find the index for a given timestep in the schedule.
Expand Down Expand Up @@ -540,7 +614,10 @@ def step(
[g.initial_seed() for g in generator] if isinstance(generator, list) else generator.initial_seed()
)
self.noise_sampler = BrownianTreeNoiseSampler(
model_output, sigma_min=self.config.sigma_min, sigma_max=self.config.sigma_max, seed=seed
model_output,
sigma_min=self.config.sigma_min,
sigma_max=self.config.sigma_max,
seed=seed,
)
noise = self.noise_sampler(self.sigmas[self.step_index], self.sigmas[self.step_index + 1]).to(
model_output.device
Expand Down Expand Up @@ -613,6 +690,17 @@ def add_noise(

# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler._get_conditioning_c_in
def _get_conditioning_c_in(self, sigma):
"""
Compute the input conditioning factor for the EDM formulation.

Args:
sigma (`float` or `torch.Tensor`):
The current sigma (noise level) value.

Returns:
`float` or `torch.Tensor`:
The input conditioning factor `c_in`.
"""
c_in = 1 / ((sigma**2 + self.config.sigma_data**2) ** 0.5)
return c_in

Expand Down
125 changes: 112 additions & 13 deletions src/diffusers/schedulers/scheduling_edm_dpmsolver_multistep.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,36 @@ def set_begin_index(self, begin_index: int = 0):

# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler.precondition_inputs
def precondition_inputs(self, sample, sigma):
"""
Precondition the input sample by scaling it according to the EDM formulation.

Args:
sample (`torch.Tensor`):
The input sample tensor to precondition.
sigma (`float` or `torch.Tensor`):
The current sigma (noise level) value.

Returns:
`torch.Tensor`:
The scaled input sample.
"""
c_in = self._get_conditioning_c_in(sigma)
scaled_sample = sample * c_in
return scaled_sample

# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler.precondition_noise
def precondition_noise(self, sigma):
"""
Precondition the noise level by applying a logarithmic transformation.

Args:
sigma (`float` or `torch.Tensor`):
The sigma (noise level) value to precondition.

Returns:
`torch.Tensor`:
The preconditioned noise value computed as `0.25 * log(sigma)`.
"""
if not isinstance(sigma, torch.Tensor):
sigma = torch.tensor([sigma])

Expand All @@ -190,7 +214,27 @@ def precondition_noise(self, sigma):
return c_noise

# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler.precondition_outputs
def precondition_outputs(self, sample, model_output, sigma):
def precondition_outputs(
self,
sample: torch.Tensor,
model_output: torch.Tensor,
sigma: Union[float, torch.Tensor],
) -> torch.Tensor:
"""
Precondition the model outputs according to the EDM formulation.

Args:
sample (`torch.Tensor`):
The input sample tensor.
model_output (`torch.Tensor`):
The direct output from the learned diffusion model.
sigma (`float` or `torch.Tensor`):
The current sigma (noise level) value.

Returns:
`torch.Tensor`:
The denoised sample computed by combining the skip connection and output scaling.
"""
sigma_data = self.config.sigma_data
c_skip = sigma_data**2 / (sigma**2 + sigma_data**2)

Expand All @@ -208,13 +252,13 @@ def precondition_outputs(self, sample, model_output, sigma):
# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler.scale_model_input
def scale_model_input(self, sample: torch.Tensor, timestep: Union[float, torch.Tensor]) -> torch.Tensor:
"""
Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
current timestep. Scales the denoising model input by `(sigma**2 + 1) ** 0.5` to match the Euler algorithm.
Scale the denoising model input to match the Euler algorithm. Ensures interchangeability with schedulers that
need to scale the denoising model input depending on the current timestep.

Args:
sample (`torch.Tensor`):
The input sample.
timestep (`int`, *optional*):
The input sample tensor.
timestep (`float` or `torch.Tensor`):
The current timestep in the diffusion chain.

Returns:
Expand Down Expand Up @@ -274,8 +318,27 @@ def set_timesteps(self, num_inference_steps: int = None, device: Union[str, torc
self.sigmas = self.sigmas.to("cpu") # to avoid too much CPU/GPU communication

# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler._compute_karras_sigmas
def _compute_karras_sigmas(self, ramp, sigma_min=None, sigma_max=None) -> torch.Tensor:
"""Constructs the noise schedule of Karras et al. (2022)."""
def _compute_karras_sigmas(
self,
ramp: torch.Tensor,
sigma_min: Optional[float] = None,
sigma_max: Optional[float] = None,
) -> torch.Tensor:
"""
Construct the noise schedule of [Karras et al. (2022)](https://huggingface.co/papers/2206.00364).

Args:
ramp (`torch.Tensor`):
A tensor of values in [0, 1] representing the interpolation positions.
sigma_min (`float`, *optional*):
Minimum sigma value. If `None`, uses `self.config.sigma_min`.
sigma_max (`float`, *optional*):
Maximum sigma value. If `None`, uses `self.config.sigma_max`.

Returns:
`torch.Tensor`:
The computed Karras sigma schedule.
"""
sigma_min = sigma_min or self.config.sigma_min
sigma_max = sigma_max or self.config.sigma_max

Expand All @@ -286,10 +349,27 @@ def _compute_karras_sigmas(self, ramp, sigma_min=None, sigma_max=None) -> torch.
return sigmas

# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler._compute_exponential_sigmas
def _compute_exponential_sigmas(self, ramp, sigma_min=None, sigma_max=None) -> torch.Tensor:
"""Implementation closely follows k-diffusion.

def _compute_exponential_sigmas(
self,
ramp: torch.Tensor,
sigma_min: Optional[float] = None,
sigma_max: Optional[float] = None,
) -> torch.Tensor:
"""
Compute the exponential sigma schedule. Implementation closely follows k-diffusion:
https://github.com/crowsonkb/k-diffusion/blob/6ab5146d4a5ef63901326489f31f1d8e7dd36b48/k_diffusion/sampling.py#L26

Args:
ramp (`torch.Tensor`):
A tensor of values representing the interpolation positions.
sigma_min (`float`, *optional*):
Minimum sigma value. If `None`, uses `self.config.sigma_min`.
sigma_max (`float`, *optional*):
Maximum sigma value. If `None`, uses `self.config.sigma_max`.

Returns:
`torch.Tensor`:
The computed exponential sigma schedule.
"""
sigma_min = sigma_min or self.config.sigma_min
sigma_max = sigma_max or self.config.sigma_max
Expand Down Expand Up @@ -433,7 +513,10 @@ def dpm_solver_first_order_update(
`torch.Tensor`:
The sample tensor at the previous timestep.
"""
sigma_t, sigma_s = self.sigmas[self.step_index + 1], self.sigmas[self.step_index]
sigma_t, sigma_s = (
self.sigmas[self.step_index + 1],
self.sigmas[self.step_index],
)
alpha_t, sigma_t = self._sigma_to_alpha_sigma_t(sigma_t)
alpha_s, sigma_s = self._sigma_to_alpha_sigma_t(sigma_s)
lambda_t = torch.log(alpha_t) - torch.log(sigma_t)
Expand Down Expand Up @@ -579,7 +662,9 @@ def multistep_dpm_solver_third_order_update(

# Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.index_for_timestep
def index_for_timestep(
self, timestep: Union[int, torch.Tensor], schedule_timesteps: Optional[torch.Tensor] = None
self,
timestep: Union[int, torch.Tensor],
schedule_timesteps: Optional[torch.Tensor] = None,
) -> int:
"""
Find the index for a given timestep in the schedule.
Expand Down Expand Up @@ -684,7 +769,10 @@ def step(

if self.config.algorithm_type == "sde-dpmsolver++":
noise = randn_tensor(
model_output.shape, generator=generator, device=model_output.device, dtype=model_output.dtype
model_output.shape,
generator=generator,
device=model_output.device,
dtype=model_output.dtype,
)
else:
noise = None
Expand Down Expand Up @@ -758,6 +846,17 @@ def add_noise(

# Copied from diffusers.schedulers.scheduling_edm_euler.EDMEulerScheduler._get_conditioning_c_in
def _get_conditioning_c_in(self, sigma):
"""
Compute the input conditioning factor for the EDM formulation.

Args:
sigma (`float` or `torch.Tensor`):
The current sigma (noise level) value.

Returns:
`float` or `torch.Tensor`:
The input conditioning factor `c_in`.
"""
c_in = 1 / ((sigma**2 + self.config.sigma_data**2) ** 0.5)
return c_in

Expand Down
Loading