Skip to content
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

Fix scheduler inference steps error with power of 3 #466

Merged
merged 5 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 4 additions & 3 deletions src/diffusers/schedulers/scheduling_ddim.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ def set_timesteps(self, num_inference_steps: int, offset: int = 0):
offset (`int`): TODO
"""
self.num_inference_steps = num_inference_steps
self.timesteps = np.arange(
0, self.config.num_train_timesteps, self.config.num_train_timesteps // self.num_inference_steps
)[::-1].copy()
step_ratio = self.config.num_train_timesteps // self.num_inference_steps
# creates integer timesteps by multiplying by ratio
# casting to int to avoid issues when num_inference_step is power of 3
self.timesteps = (np.arange(0, num_inference_steps) * step_ratio).astype(int)[::-1].copy()
self.timesteps += offset
self.set_format(tensor_format=self.tensor_format)

Expand Down
7 changes: 4 additions & 3 deletions src/diffusers/schedulers/scheduling_pndm.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ def set_timesteps(self, num_inference_steps: int, offset: int = 0) -> torch.Floa
offset (`int`): TODO
"""
self.num_inference_steps = num_inference_steps
self._timesteps = list(
range(0, self.config.num_train_timesteps, self.config.num_train_timesteps // num_inference_steps)
)
step_ratio = self.config.num_train_timesteps // self.num_inference_steps
# creates integer timesteps by multiplying by ratio
# casting to int to avoid issues when num_inference_step is power of 3
self._timesteps = (np.arange(0, num_inference_steps) * step_ratio).astype(int).tolist()
self._offset = offset
self._timesteps = np.array([t + self._offset for t in self._timesteps])

Expand Down
17 changes: 16 additions & 1 deletion tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def test_time_indices(self):

def test_inference_steps(self):
for t, num_inference_steps in zip([1, 10, 50], [10, 50, 500]):
self.check_over_forward(num_inference_steps=num_inference_steps)
self.check_over_forward(time_step=t, num_inference_steps=num_inference_steps)

def test_eta(self):
for t, eta in zip([1, 10, 49], [0.0, 0.5, 1.0]):
Expand Down Expand Up @@ -621,6 +621,21 @@ def test_inference_steps(self):
for t, num_inference_steps in zip([1, 5, 10], [10, 50, 100]):
self.check_over_forward(time_step=t, num_inference_steps=num_inference_steps)

def test_pow_of_3_inference_steps(self):
num_inference_steps = 27

for scheduler_class in self.scheduler_classes:
sample = self.dummy_sample
residual = 0.1 * sample

scheduler_config = self.get_scheduler_config()
scheduler = scheduler_class(**scheduler_config)

scheduler.set_timesteps(num_inference_steps)

for i, t in enumerate(scheduler.prk_timesteps):
sample = scheduler.step_prk(residual, t, sample).prev_sample

def test_inference_plms_no_past_residuals(self):
with self.assertRaises(ValueError):
scheduler_class = self.scheduler_classes[0]
Expand Down