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 [core/GLIGEN]: TypeError when iterating over 0-d tensor with In-painting mode when EulerAncestralDiscreteScheduler is used #5305

Merged

Conversation

rchuzh99
Copy link
Contributor

@rchuzh99 rchuzh99 commented Oct 6, 2023

What does this PR do?

Fixes #5216
Re-opens PR #5214

This PR fixes the TypeError caused by trying to directly iterate over a 0-dimension tensor in the denoising stage of GLIGEN In-painting operation.

The error occurs when using diffusion noise schedulers that iterate over timesteps
(e.g. EulerAnchestralDiscreteScheduler, KDPM2AncestralDiscreteScheduler), during in-painting operation with the StableDiffusionGLIGENPipeline and StableDiffusionGLIGENTextImagePipeline .

For further clarification, this operation of the add_noise function 🔽

step_indices = [(schedule_timesteps == t).nonzero().item() for t in timesteps]
in the affected noise schedulers expects the timesteps to be a non-0 dim torch Tensor. However, in the affected pipelines, timesteps is 0-dimension.

This PR references the approach found in the StableDiffusionInpaintingPipeline

if i < len(timesteps) - 1:
noise_timestep = timesteps[i + 1]
init_latents_proper = self.scheduler.add_noise(
init_latents_proper, noise, torch.tensor([noise_timestep])
)
which is to wrap the timestep(t) 0-d tensor in a list to convert to 1-d tensor as follow 🔽

  if gligen_inpaint_image is not None:
      gligen_inpaint_latent_with_noise = (
          self.scheduler.add_noise(
              gligen_inpaint_latent, torch.randn_like(gligen_inpaint_latent), torch.tensor([t])
          )
          .expand(latents.shape[0], -1, -1, -1)
          .clone()
      )

https://github.com/rchuzh99/diffusers/blob/fb82fc4bdcead457e24a780cfb193070227f3e31/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_gligen.py#L799-L806 and https://github.com/rchuzh99/diffusers/blob/fb82fc4bdcead457e24a780cfb193070227f3e31/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_gligen_text_image.py#L960-L967

Affected pipelines

  1. StableDiffusionGLIGENPipeline
  2. StableDiffusionGLIGENTextImagePipeline

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

cc: @sayakpaul , @nikhil-masterful, @tuanh123789

References

  1. StableDiffusionGLIGENPipeline: Add GLIGEN implementation #4441
  2. StableDiffusionGLIGENTextImagePipeline: Add GLIGEN Text Image implementation #4777
  3. StableDiffusionInpaintingPipeline: https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py

zhen-hao.chu and others added 3 commits October 5, 2023 06:49
…ist to convert to 1-d tensor. This avoids the TypeError caused by trying to directly iterate over a 0-dimensional tensor in the denoising stage
@@ -207,7 +207,7 @@ def __init__(
with open(concept["class_prompt"], "r") as f:
class_prompt = f.read().splitlines()

class_img_path = [(x, y) for (x, y) in zip(class_images_path, class_prompt)]
class_img_path = list(zip(class_images_path, class_prompt))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean.

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much!

Will merge once the CI is green

@rchuzh99
Copy link
Contributor Author

rchuzh99 commented Oct 9, 2023

Thanks so much!

Will merge once the CI is green

Thanks for the review @sayakpaul 👍🏻

@sayakpaul sayakpaul merged commit 6bd55b5 into huggingface:main Oct 9, 2023
11 checks passed
@WuyangLuo
Copy link

Very useful ! Thx

yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
…ainting mode when EulerAncestralDiscreteScheduler is used (huggingface#5305)

* fix(gligen_inpaint_pipeline): 🐛 Wrap the timestep() 0-d tensor in a list to convert to 1-d tensor. This avoids the TypeError caused by trying to directly iterate over a 0-dimensional tensor in the denoising stage

* test(gligen/gligen_text_image): unit test using the EulerAncestralDiscreteScheduler

---------

Co-authored-by: zhen-hao.chu <[email protected]>
Co-authored-by: Sayak Paul <[email protected]>
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
…ainting mode when EulerAncestralDiscreteScheduler is used (huggingface#5305)

* fix(gligen_inpaint_pipeline): 🐛 Wrap the timestep() 0-d tensor in a list to convert to 1-d tensor. This avoids the TypeError caused by trying to directly iterate over a 0-dimensional tensor in the denoising stage

* test(gligen/gligen_text_image): unit test using the EulerAncestralDiscreteScheduler

---------

Co-authored-by: zhen-hao.chu <[email protected]>
Co-authored-by: Sayak Paul <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants