Skip to content

Fix vae dtype #11228

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

Closed
wants to merge 8 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,8 @@ def prepare_latents(

batch_size = batch_size * num_images_per_prompt

vae_original_dtype = self.vae.dtype

if image.shape[1] == 4:
init_latents = image

Expand Down Expand Up @@ -742,7 +744,7 @@ def prepare_latents(
init_latents = retrieve_latents(self.vae.encode(image), generator=generator)

if self.vae.config.force_upcast:
self.vae.to(dtype)
self.vae.to(vae_original_dtype)

init_latents = init_latents.to(dtype)
if latents_mean is not None and latents_std is not None:
Expand Down Expand Up @@ -1459,10 +1461,13 @@ def denoising_value_valid(dnv):
if needs_upcasting:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)
elif latents.dtype != self.vae.dtype:

if latents.dtype != self.vae.dtype:
if torch.backends.mps.is_available():
# some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272
self.vae = self.vae.to(latents.dtype)
else:
latents = latents.to(self.vae.dtype)

# unscale/denormalize the latents
# denormalize with the mean and std if available and not None
Expand Down