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
17 changes: 14 additions & 3 deletions src/diffusers/schedulers/scheduling_euler_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,14 +887,25 @@ def get_velocity(self, sample: torch.Tensor, noise: torch.Tensor, timesteps: tor
schedule_timesteps = self.timesteps.to(sample.device)
timesteps = timesteps.to(sample.device)

step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timesteps]
alpha_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timesteps]
if self.config.timestep_type != "continuous" or self.config.prediction_type != "v_prediction":
# Only discrete training timesteps directly index alphas_cumprod.
training_indices = timesteps.long()
is_training_timestep = (
(timesteps == training_indices)
& (training_indices >= 0)
& (training_indices < len(self.alphas_cumprod))
)
alpha_indices = torch.where(
is_training_timestep, training_indices, training_indices.new_tensor(alpha_indices)
)
alphas_cumprod = self.alphas_cumprod.to(sample)
sqrt_alpha_prod = alphas_cumprod[step_indices] ** 0.5
sqrt_alpha_prod = alphas_cumprod[alpha_indices] ** 0.5
sqrt_alpha_prod = sqrt_alpha_prod.flatten()
while len(sqrt_alpha_prod.shape) < len(sample.shape):
sqrt_alpha_prod = sqrt_alpha_prod.unsqueeze(-1)

sqrt_one_minus_alpha_prod = (1 - alphas_cumprod[step_indices]) ** 0.5
sqrt_one_minus_alpha_prod = (1 - alphas_cumprod[alpha_indices]) ** 0.5
sqrt_one_minus_alpha_prod = sqrt_one_minus_alpha_prod.flatten()
while len(sqrt_one_minus_alpha_prod.shape) < len(sample.shape):
sqrt_one_minus_alpha_prod = sqrt_one_minus_alpha_prod.unsqueeze(-1)
Expand Down
47 changes: 46 additions & 1 deletion tests/schedulers/test_scheduler_euler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import torch

from diffusers import EulerDiscreteScheduler
from diffusers import DDIMScheduler, EulerDiscreteScheduler

from ..testing_utils import torch_device
from .test_schedulers import SchedulerCommonTest
Expand Down Expand Up @@ -37,6 +37,51 @@ def test_prediction_type(self):
for prediction_type in ["epsilon", "v_prediction"]:
self.check_over_configs(prediction_type=prediction_type)

def test_get_velocity_matches_ddim(self):
config = self.get_scheduler_config(
num_train_timesteps=1000,
beta_start=0.00085,
beta_end=0.012,
beta_schedule="scaled_linear",
prediction_type="v_prediction",
)
scheduler = EulerDiscreteScheduler(**config)
ddim = DDIMScheduler(**config)
sample = torch.tensor([[1.0, -2.0], [0.5, 3.0]])
noise = torch.tensor([[0.25, 0.5], [-0.75, 1.0]])
timesteps = torch.tensor([999.0, 500.0])
expected = ddim.get_velocity(sample, noise, timesteps.long())

with self.subTest(schedule="training"):
torch.testing.assert_close(scheduler.get_velocity(sample, noise, timesteps), expected)

scheduler.set_timesteps(timesteps=[999, 500])
with self.subTest(schedule="custom"):
torch.testing.assert_close(scheduler.get_velocity(sample, noise, timesteps), expected)

def test_get_velocity_with_fractional_timestep(self):
scheduler = EulerDiscreteScheduler(num_train_timesteps=6)
ddim = DDIMScheduler(num_train_timesteps=6)
scheduler.set_timesteps(3)

sample = torch.tensor([[1.0]])
noise = torch.tensor([[0.25]])
timesteps = scheduler.timesteps[1:2]
self.assertEqual(timesteps.item(), 2.5)
expected = ddim.get_velocity(sample, noise, torch.tensor([1]))
torch.testing.assert_close(scheduler.get_velocity(sample, noise, timesteps), expected)

def test_get_velocity_with_continuous_timesteps(self):
# A continuous timestep can be exactly zero when its sigma is one.
config = {"num_train_timesteps": 2, "trained_betas": [0.5, 0.25], "prediction_type": "v_prediction"}
scheduler = EulerDiscreteScheduler(**config, timestep_type="continuous")
ddim = DDIMScheduler(**config)
sample = torch.tensor([[1.0], [0.5]])
noise = torch.tensor([[0.25], [-0.75]])
self.assertEqual(scheduler.timesteps[1].item(), 0.0)
expected = ddim.get_velocity(sample, noise, torch.tensor([0, 1]))
torch.testing.assert_close(scheduler.get_velocity(sample, noise, scheduler.timesteps), expected)

def test_timestep_type(self):
timestep_types = ["discrete", "continuous"]
for timestep_type in timestep_types:
Expand Down