From 1beb12d872d79842ceaf33a6a4eb6f64cbae7768 Mon Sep 17 00:00:00 2001 From: Yuta Hayashibe Date: Tue, 4 Oct 2022 20:08:59 +0900 Subject: [PATCH 1/3] Avoid negative strides for tensors --- .../stable_diffusion/pipeline_stable_diffusion_img2img.py | 5 ++++- .../stable_diffusion/pipeline_stable_diffusion_inpaint.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py index c8f02b5896d6..f1ee217fa5ae 100644 --- a/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +++ b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py @@ -284,7 +284,10 @@ def __call__( # Some schedulers like PNDM have timesteps as arrays # It's more optimzed to move all timesteps to correct device beforehand - timesteps_tensor = torch.tensor(self.scheduler.timesteps[t_start:], device=self.device) + if torch.is_tensor(self.scheduler.timesteps): + timesteps_tensor = torch.tensor(self.scheduler.timesteps[t_start:], device=self.device) + else: + timesteps_tensor = torch.tensor(self.scheduler.timesteps.copy()[t_start:], device=self.device) for i, t in enumerate(self.progress_bar(timesteps_tensor)): t_index = t_start + i diff --git a/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py index 21490d975730..9db2ad9ec5e2 100644 --- a/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +++ b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py @@ -318,7 +318,10 @@ def __call__( # Some schedulers like PNDM have timesteps as arrays # It's more optimzed to move all timesteps to correct device beforehand - timesteps_tensor = torch.tensor(self.scheduler.timesteps[t_start:], device=self.device) + if torch.is_tensor(self.scheduler.timesteps): + timesteps_tensor = torch.tensor(self.scheduler.timesteps[t_start:], device=self.device) + else: + timesteps_tensor = torch.tensor(self.scheduler.timesteps.copy()[t_start:], device=self.device) for i, t in tqdm(enumerate(timesteps_tensor)): t_index = t_start + i From 7f567eceb6027f5b72d8be9670355ebdca0db2dd Mon Sep 17 00:00:00 2001 From: Yuta Hayashibe Date: Tue, 4 Oct 2022 22:46:34 +0900 Subject: [PATCH 2/3] Changed not to make torch.tensor --- .../pipeline_stable_diffusion_img2img.py | 10 ++-------- .../pipeline_stable_diffusion_inpaint.py | 10 ++-------- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py index f1ee217fa5ae..01f79b818bc7 100644 --- a/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +++ b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py @@ -281,15 +281,9 @@ def __call__( latents = init_latents t_start = max(num_inference_steps - init_timestep + offset, 0) + timesteps = self.scheduler.timesteps[t_start:] - # Some schedulers like PNDM have timesteps as arrays - # It's more optimzed to move all timesteps to correct device beforehand - if torch.is_tensor(self.scheduler.timesteps): - timesteps_tensor = torch.tensor(self.scheduler.timesteps[t_start:], device=self.device) - else: - timesteps_tensor = torch.tensor(self.scheduler.timesteps.copy()[t_start:], device=self.device) - - for i, t in enumerate(self.progress_bar(timesteps_tensor)): + for i, t in enumerate(self.progress_bar(timesteps)): t_index = t_start + i # expand the latents if we are doing classifier free guidance diff --git a/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py index 9db2ad9ec5e2..ea15331000ab 100644 --- a/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +++ b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py @@ -315,15 +315,9 @@ def __call__( latents = init_latents t_start = max(num_inference_steps - init_timestep + offset, 0) + timesteps = self.scheduler.timesteps.copy()[t_start:] - # Some schedulers like PNDM have timesteps as arrays - # It's more optimzed to move all timesteps to correct device beforehand - if torch.is_tensor(self.scheduler.timesteps): - timesteps_tensor = torch.tensor(self.scheduler.timesteps[t_start:], device=self.device) - else: - timesteps_tensor = torch.tensor(self.scheduler.timesteps.copy()[t_start:], device=self.device) - - for i, t in tqdm(enumerate(timesteps_tensor)): + for i, t in tqdm(enumerate(timesteps)): t_index = t_start + i # expand the latents if we are doing classifier free guidance latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents From dca59b810d2488289672180e11657b26dc29acf8 Mon Sep 17 00:00:00 2001 From: Yuta Hayashibe Date: Wed, 5 Oct 2022 10:05:00 +0900 Subject: [PATCH 3/3] Removed a needless copy --- .../stable_diffusion/pipeline_stable_diffusion_inpaint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py index ea15331000ab..197a6ae3f0b8 100644 --- a/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +++ b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py @@ -315,7 +315,7 @@ def __call__( latents = init_latents t_start = max(num_inference_steps - init_timestep + offset, 0) - timesteps = self.scheduler.timesteps.copy()[t_start:] + timesteps = self.scheduler.timesteps[t_start:] for i, t in tqdm(enumerate(timesteps)): t_index = t_start + i