-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
SDXL Inpainting VAE Normalization #7225
Conversation
@@ -308,6 +308,25 @@ def retrieve_timesteps( | |||
return timesteps, num_inference_steps | |||
|
|||
|
|||
def requires_vae_latents_normalization(vae): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can definitely do this in place without having to delegate it to a method. As that way the readability of the code stays linear and the reader doesn't have to refer to another method to see what's going on.
def normalize_vae_latents(latents, latents_mean, latents_std): | ||
latents_mean = latents_mean.to(device=latents.device, dtype=latents.dtype) | ||
latents_std = latents_std.to(device=latents.device, dtype=latents.dtype) | ||
latents = (latents - latents_mean) / latents_std | ||
return latents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
def denormalize_vae_latents(latents, latents_mean, latents_std): | ||
latents_mean = latents_mean.to(device=latents.device, dtype=latents.dtype) | ||
latents_std = latents_std.to(device=latents.device, dtype=latents.dtype) | ||
latents = latents * latents_std + latents_mean | ||
return latents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have some results for us to see how the Playground v2.5 checkpoint plays out with inpainting.
I am okay with that changes you're introducing given the comments are addressed.
Cc: @patil-suraj as well.
@sayakpaul Thank for commenting! Don't you think that the code should be DRY (Don't Repeat Yourself)? Overall these 3 functions should be used in any place of SDXL pipeline with this VAE. Maybe it is reasonable to move it to "utils"/"common" or in base SDXL pipeline. Don't have checkpoint yet. I trained one but figured out this normalisation too late... |
Please refer to https://huggingface.co/docs/diffusers/en/conceptual/philosophy. It's okay to trade away DRY in the interest of readability as our pipelines are also read for educational purposes. |
Addressed all the comments. Please let me know if I can be helpful. Btw while you are replying, if it is possible by any chance to share training args used for this PR to train inpainting model: This would be very helpful, thank you! |
Will defer that to the training ninja @patil-suraj here :) |
@@ -939,6 +939,17 @@ def _encode_vae_image(self, image: torch.Tensor, generator: torch.Generator): | |||
else: | |||
image_latents = retrieve_latents(self.vae.encode(image), generator=generator) | |||
|
|||
has_latents_mean = hasattr(self.vae.config, "latents_mean") and self.vae.config.latents_mean is not None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need it here?
we didn't normalize the latents in SDXL tex to image
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be needed because here we are encoding the input image. This is what we do in the SDXL DreamBooth LoRA training script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I agree that #7132 tackles it from a comprehensive perspective.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I re-opene it and I'm happy to be proven wrong
once we merge #7132, you can update the branch, and you can then test it out and show some results with and without the normalization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me!
just saw this PR #7132 |
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread. Please note that issues that do not follow the contributing guidelines are likely to be ignored. |
@yiyixuxu could you give this another look? |
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread. Please note that issues that do not follow the contributing guidelines are likely to be ignored. |
@yiyixuxu a gentle ping. |
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread. Please note that issues that do not follow the contributing guidelines are likely to be ignored. |
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread. Please note that issues that do not follow the contributing guidelines are likely to be ignored. |
Safe to close it now. |
What does this PR do?
Since the release of Playground V2.5 with "custom" VAE we should normalize and denormalize latents.
This is already implemented in
StableDiffusionXLPipeline
:diffusers/src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py
Lines 1230 to 1243 in 687bc27
Who can review?
cc: @sayakpaul @patil-suraj