Skip to content

Commit 57a021d

Browse files
authored
[fix] FreeInit step index out of bounds (#8969)
* fix step index out of bounds * add test for free_init with different schedulers * add test to vid2vid and pia
1 parent 1168eaa commit 57a021d

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

src/diffusers/pipelines/free_init_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ def _apply_free_init(
180180
num_inference_steps = max(
181181
1, int(num_inference_steps / self._free_init_num_iters * (free_init_iteration + 1))
182182
)
183+
184+
if num_inference_steps > 0:
183185
self.scheduler.set_timesteps(num_inference_steps, device=device)
184186

185187
return latents, self.scheduler.timesteps

tests/pipelines/animatediff/test_animatediff.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
AnimateDiffPipeline,
1111
AutoencoderKL,
1212
DDIMScheduler,
13+
DPMSolverMultistepScheduler,
14+
LCMScheduler,
1315
MotionAdapter,
1416
StableDiffusionPipeline,
1517
UNet2DConditionModel,
@@ -353,6 +355,52 @@ def test_free_init(self):
353355
"Disabling of FreeInit should lead to results similar to the default pipeline results",
354356
)
355357

358+
def test_free_init_with_schedulers(self):
359+
components = self.get_dummy_components()
360+
pipe: AnimateDiffPipeline = self.pipeline_class(**components)
361+
pipe.set_progress_bar_config(disable=None)
362+
pipe.to(torch_device)
363+
364+
inputs_normal = self.get_dummy_inputs(torch_device)
365+
frames_normal = pipe(**inputs_normal).frames[0]
366+
367+
schedulers_to_test = [
368+
DPMSolverMultistepScheduler.from_config(
369+
components["scheduler"].config,
370+
timestep_spacing="linspace",
371+
beta_schedule="linear",
372+
algorithm_type="dpmsolver++",
373+
steps_offset=1,
374+
clip_sample=False,
375+
),
376+
LCMScheduler.from_config(
377+
components["scheduler"].config,
378+
timestep_spacing="linspace",
379+
beta_schedule="linear",
380+
steps_offset=1,
381+
clip_sample=False,
382+
),
383+
]
384+
components.pop("scheduler")
385+
386+
for scheduler in schedulers_to_test:
387+
components["scheduler"] = scheduler
388+
pipe: AnimateDiffPipeline = self.pipeline_class(**components)
389+
pipe.set_progress_bar_config(disable=None)
390+
pipe.to(torch_device)
391+
392+
pipe.enable_free_init(num_iters=2, use_fast_sampling=False)
393+
394+
inputs = self.get_dummy_inputs(torch_device)
395+
frames_enable_free_init = pipe(**inputs).frames[0]
396+
sum_enabled = np.abs(to_np(frames_normal) - to_np(frames_enable_free_init)).sum()
397+
398+
self.assertGreater(
399+
sum_enabled,
400+
1e1,
401+
"Enabling of FreeInit should lead to results different from the default pipeline results",
402+
)
403+
356404
@unittest.skipIf(
357405
torch_device != "cuda" or not is_xformers_available(),
358406
reason="XFormers attention is only available with CUDA and `xformers` installed",

tests/pipelines/animatediff/test_animatediff_video2video.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
AnimateDiffVideoToVideoPipeline,
1111
AutoencoderKL,
1212
DDIMScheduler,
13+
DPMSolverMultistepScheduler,
14+
LCMScheduler,
1315
MotionAdapter,
1416
StableDiffusionPipeline,
1517
UNet2DConditionModel,
@@ -380,3 +382,49 @@ def test_free_init(self):
380382
1e-4,
381383
"Disabling of FreeInit should lead to results similar to the default pipeline results",
382384
)
385+
386+
def test_free_init_with_schedulers(self):
387+
components = self.get_dummy_components()
388+
pipe: AnimateDiffVideoToVideoPipeline = self.pipeline_class(**components)
389+
pipe.set_progress_bar_config(disable=None)
390+
pipe.to(torch_device)
391+
392+
inputs_normal = self.get_dummy_inputs(torch_device)
393+
frames_normal = pipe(**inputs_normal).frames[0]
394+
395+
schedulers_to_test = [
396+
DPMSolverMultistepScheduler.from_config(
397+
components["scheduler"].config,
398+
timestep_spacing="linspace",
399+
beta_schedule="linear",
400+
algorithm_type="dpmsolver++",
401+
steps_offset=1,
402+
clip_sample=False,
403+
),
404+
LCMScheduler.from_config(
405+
components["scheduler"].config,
406+
timestep_spacing="linspace",
407+
beta_schedule="linear",
408+
steps_offset=1,
409+
clip_sample=False,
410+
),
411+
]
412+
components.pop("scheduler")
413+
414+
for scheduler in schedulers_to_test:
415+
components["scheduler"] = scheduler
416+
pipe: AnimateDiffVideoToVideoPipeline = self.pipeline_class(**components)
417+
pipe.set_progress_bar_config(disable=None)
418+
pipe.to(torch_device)
419+
420+
pipe.enable_free_init(num_iters=2, use_fast_sampling=False)
421+
422+
inputs = self.get_dummy_inputs(torch_device)
423+
frames_enable_free_init = pipe(**inputs).frames[0]
424+
sum_enabled = np.abs(to_np(frames_normal) - to_np(frames_enable_free_init)).sum()
425+
426+
self.assertGreater(
427+
sum_enabled,
428+
1e1,
429+
"Enabling of FreeInit should lead to results different from the default pipeline results",
430+
)

tests/pipelines/pia/test_pia.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from diffusers import (
1010
AutoencoderKL,
1111
DDIMScheduler,
12+
DPMSolverMultistepScheduler,
13+
LCMScheduler,
1214
MotionAdapter,
1315
PIAPipeline,
1416
StableDiffusionPipeline,
@@ -360,6 +362,52 @@ def test_free_init(self):
360362
"Disabling of FreeInit should lead to results similar to the default pipeline results",
361363
)
362364

365+
def test_free_init_with_schedulers(self):
366+
components = self.get_dummy_components()
367+
pipe: PIAPipeline = self.pipeline_class(**components)
368+
pipe.set_progress_bar_config(disable=None)
369+
pipe.to(torch_device)
370+
371+
inputs_normal = self.get_dummy_inputs(torch_device)
372+
frames_normal = pipe(**inputs_normal).frames[0]
373+
374+
schedulers_to_test = [
375+
DPMSolverMultistepScheduler.from_config(
376+
components["scheduler"].config,
377+
timestep_spacing="linspace",
378+
beta_schedule="linear",
379+
algorithm_type="dpmsolver++",
380+
steps_offset=1,
381+
clip_sample=False,
382+
),
383+
LCMScheduler.from_config(
384+
components["scheduler"].config,
385+
timestep_spacing="linspace",
386+
beta_schedule="linear",
387+
steps_offset=1,
388+
clip_sample=False,
389+
),
390+
]
391+
components.pop("scheduler")
392+
393+
for scheduler in schedulers_to_test:
394+
components["scheduler"] = scheduler
395+
pipe: PIAPipeline = self.pipeline_class(**components)
396+
pipe.set_progress_bar_config(disable=None)
397+
pipe.to(torch_device)
398+
399+
pipe.enable_free_init(num_iters=2, use_fast_sampling=False)
400+
401+
inputs = self.get_dummy_inputs(torch_device)
402+
frames_enable_free_init = pipe(**inputs).frames[0]
403+
sum_enabled = np.abs(to_np(frames_normal) - to_np(frames_enable_free_init)).sum()
404+
405+
self.assertGreater(
406+
sum_enabled,
407+
1e1,
408+
"Enabling of FreeInit should lead to results different from the default pipeline results",
409+
)
410+
363411
@unittest.skipIf(
364412
torch_device != "cuda" or not is_xformers_available(),
365413
reason="XFormers attention is only available with CUDA and `xformers` installed",

0 commit comments

Comments
 (0)