Skip to content

Commit 6133d98

Browse files
authored
[IF| add set_begin_index for all IF pipelines (#7577)
add set_begin_index for all if pipelines
1 parent 1c60e09 commit 6133d98

7 files changed

+23
-7
lines changed

src/diffusers/pipelines/deepfloyd_if/pipeline_if.py

+3
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,9 @@ def __call__(
691691
self.scheduler.set_timesteps(num_inference_steps, device=device)
692692
timesteps = self.scheduler.timesteps
693693

694+
if hasattr(self.scheduler, "set_begin_index"):
695+
self.scheduler.set_begin_index(0)
696+
694697
# 5. Prepare intermediate images
695698
intermediate_images = self.prepare_intermediate_images(
696699
batch_size * num_images_per_prompt,

src/diffusers/pipelines/deepfloyd_if/pipeline_if_img2img.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -633,12 +633,15 @@ def numpy_to_pt(images):
633633

634634
return image
635635

636+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.StableDiffusionImg2ImgPipeline.get_timesteps
636637
def get_timesteps(self, num_inference_steps, strength):
637638
# get the original timestep using init_timestep
638639
init_timestep = min(int(num_inference_steps * strength), num_inference_steps)
639640

640641
t_start = max(num_inference_steps - init_timestep, 0)
641-
timesteps = self.scheduler.timesteps[t_start:]
642+
timesteps = self.scheduler.timesteps[t_start * self.scheduler.order :]
643+
if hasattr(self.scheduler, "set_begin_index"):
644+
self.scheduler.set_begin_index(t_start * self.scheduler.order)
642645

643646
return timesteps, num_inference_steps - t_start
644647

src/diffusers/pipelines/deepfloyd_if/pipeline_if_img2img_superresolution.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -714,13 +714,15 @@ def preprocess_image(self, image: PIL.Image.Image, num_images_per_prompt, device
714714

715715
return image
716716

717-
# Copied from diffusers.pipelines.deepfloyd_if.pipeline_if_img2img.IFImg2ImgPipeline.get_timesteps
717+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.StableDiffusionImg2ImgPipeline.get_timesteps
718718
def get_timesteps(self, num_inference_steps, strength):
719719
# get the original timestep using init_timestep
720720
init_timestep = min(int(num_inference_steps * strength), num_inference_steps)
721721

722722
t_start = max(num_inference_steps - init_timestep, 0)
723-
timesteps = self.scheduler.timesteps[t_start:]
723+
timesteps = self.scheduler.timesteps[t_start * self.scheduler.order :]
724+
if hasattr(self.scheduler, "set_begin_index"):
725+
self.scheduler.set_begin_index(t_start * self.scheduler.order)
724726

725727
return timesteps, num_inference_steps - t_start
726728

src/diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -723,13 +723,15 @@ def preprocess_mask_image(self, mask_image) -> torch.Tensor:
723723

724724
return mask_image
725725

726-
# Copied from diffusers.pipelines.deepfloyd_if.pipeline_if_img2img.IFImg2ImgPipeline.get_timesteps
726+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.StableDiffusionImg2ImgPipeline.get_timesteps
727727
def get_timesteps(self, num_inference_steps, strength):
728728
# get the original timestep using init_timestep
729729
init_timestep = min(int(num_inference_steps * strength), num_inference_steps)
730730

731731
t_start = max(num_inference_steps - init_timestep, 0)
732-
timesteps = self.scheduler.timesteps[t_start:]
732+
timesteps = self.scheduler.timesteps[t_start * self.scheduler.order :]
733+
if hasattr(self.scheduler, "set_begin_index"):
734+
self.scheduler.set_begin_index(t_start * self.scheduler.order)
733735

734736
return timesteps, num_inference_steps - t_start
735737

src/diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting_superresolution.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -800,13 +800,15 @@ def preprocess_mask_image(self, mask_image) -> torch.Tensor:
800800

801801
return mask_image
802802

803-
# Copied from diffusers.pipelines.deepfloyd_if.pipeline_if_img2img.IFImg2ImgPipeline.get_timesteps
803+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.StableDiffusionImg2ImgPipeline.get_timesteps
804804
def get_timesteps(self, num_inference_steps, strength):
805805
# get the original timestep using init_timestep
806806
init_timestep = min(int(num_inference_steps * strength), num_inference_steps)
807807

808808
t_start = max(num_inference_steps - init_timestep, 0)
809-
timesteps = self.scheduler.timesteps[t_start:]
809+
timesteps = self.scheduler.timesteps[t_start * self.scheduler.order :]
810+
if hasattr(self.scheduler, "set_begin_index"):
811+
self.scheduler.set_begin_index(t_start * self.scheduler.order)
810812

811813
return timesteps, num_inference_steps - t_start
812814

src/diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.py

+3
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,9 @@ def __call__(
775775
self.scheduler.set_timesteps(num_inference_steps, device=device)
776776
timesteps = self.scheduler.timesteps
777777

778+
if hasattr(self.scheduler, "set_begin_index"):
779+
self.scheduler.set_begin_index(0)
780+
778781
# 5. Prepare intermediate images
779782
num_channels = self.unet.config.in_channels // 2
780783
intermediate_images = self.prepare_intermediate_images(

src/diffusers/pipelines/pipeline_utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,7 @@ def enable_model_cpu_offload(self, gpu_id: Optional[int] = None, device: Union[t
998998

999999
all_model_components = {k: v for k, v in self.components.items() if isinstance(v, torch.nn.Module)}
10001000

1001+
self._all_hooks = []
10011002
hook = None
10021003
for model_str in self.model_cpu_offload_seq.split("->"):
10031004
model = all_model_components.pop(model_str, None)

0 commit comments

Comments
 (0)