Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 11 additions & 5 deletions examples/community/lpw_stable_diffusion_xl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,13 +1184,19 @@ def __call__(
if callback is not None and i % callback_steps == 0:
callback(i, t, latents)

# make sure the VAE is in float32 mode, as it overflows in float16
if self.vae.dtype == torch.float16 and self.vae.config.force_upcast:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

if not output_type == "latent":
# make sure the VAE is in float32 mode, as it overflows in float16
needs_upcasting = self.vae.dtype == torch.float16 and self.vae.config.force_upcast

if needs_upcasting:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]

# cast back to fp16 if needed
if needs_upcasting:
self.vae.to(dtype=torch.float16)
else:
image = latents
return StableDiffusionXLPipelineOutput(images=image)
Expand Down
16 changes: 11 additions & 5 deletions examples/community/stable_diffusion_xl_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,13 +772,19 @@ def hacked_UpBlock2D_forward(self, hidden_states, res_hidden_states_tuple, temb=
if callback is not None and i % callback_steps == 0:
callback(i, t, latents)

# make sure the VAE is in float32 mode, as it overflows in float16
if self.vae.dtype == torch.float16 and self.vae.config.force_upcast:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

if not output_type == "latent":
# make sure the VAE is in float32 mode, as it overflows in float16
needs_upcasting = self.vae.dtype == torch.float16 and self.vae.config.force_upcast

if needs_upcasting:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]

# cast back to fp16 if needed
if needs_upcasting:
self.vae.to(dtype=torch.float16)
else:
image = latents
return StableDiffusionXLPipelineOutput(images=image)
Expand Down
16 changes: 11 additions & 5 deletions src/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,13 +1183,19 @@ def __call__(
self.controlnet.to("cpu")
torch.cuda.empty_cache()

# make sure the VAE is in float32 mode, as it overflows in float16
if self.vae.dtype == torch.float16 and self.vae.config.force_upcast:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

if not output_type == "latent":
# make sure the VAE is in float32 mode, as it overflows in float16
needs_upcasting = self.vae.dtype == torch.float16 and self.vae.config.force_upcast

if needs_upcasting:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]

# cast back to fp16 if needed
if needs_upcasting:
self.vae.to(dtype=torch.float16)
else:
image = latents
return StableDiffusionXLPipelineOutput(images=image)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,15 +751,20 @@ def __call__(
if callback is not None and i % callback_steps == 0:
callback(i, t, latents)

# 10. Post-processing
# make sure the VAE is in float32 mode, as it overflows in float16
if self.vae.dtype == torch.float16 and self.vae.config.force_upcast:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

# post-processing
if not output_type == "latent":
# make sure the VAE is in float32 mode, as it overflows in float16
needs_upcasting = self.vae.dtype == torch.float16 and self.vae.config.force_upcast

if needs_upcasting:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]

# cast back to fp16 if needed
if needs_upcasting:
self.vae.to(dtype=torch.float16)

image, has_nsfw_concept, _ = self.run_safety_checker(image, device, prompt_embeds.dtype)
else:
image = latents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,13 +870,19 @@ def __call__(
if callback is not None and i % callback_steps == 0:
callback(i, t, latents)

# make sure the VAE is in float32 mode, as it overflows in float16
if self.vae.dtype == torch.float16 and self.vae.config.force_upcast:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

if not output_type == "latent":
# make sure the VAE is in float32 mode, as it overflows in float16
needs_upcasting = self.vae.dtype == torch.float16 and self.vae.config.force_upcast

if needs_upcasting:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]

# cast back to fp16 if needed
if needs_upcasting:
self.vae.to(dtype=torch.float16)
else:
image = latents
return StableDiffusionXLPipelineOutput(images=image)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,13 +1027,19 @@ def denoising_value_valid(dnv):
if callback is not None and i % callback_steps == 0:
callback(i, t, latents)

# make sure the VAE is in float32 mode, as it overflows in float16
if self.vae.dtype == torch.float16 and self.vae.config.force_upcast:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

if not output_type == "latent":
# make sure the VAE is in float32 mode, as it overflows in float16
needs_upcasting = self.vae.dtype == torch.float16 and self.vae.config.force_upcast

if needs_upcasting:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]

# cast back to fp16 if needed
if needs_upcasting:
self.vae.to(dtype=torch.float16)
else:
image = latents
return StableDiffusionXLPipelineOutput(images=image)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1333,13 +1333,19 @@ def denoising_value_valid(dnv):
if callback is not None and i % callback_steps == 0:
callback(i, t, latents)

# make sure the VAE is in float32 mode, as it overflows in float16
if self.vae.dtype == torch.float16 and self.vae.config.force_upcast:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

if not output_type == "latent":
# make sure the VAE is in float32 mode, as it overflows in float16
needs_upcasting = self.vae.dtype == torch.float16 and self.vae.config.force_upcast

if needs_upcasting:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]

# cast back to fp16 if needed
if needs_upcasting:
self.vae.to(dtype=torch.float16)
else:
return StableDiffusionXLPipelineOutput(images=latents)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,13 +908,19 @@ def __call__(
if callback is not None and i % callback_steps == 0:
callback(i, t, latents)

# make sure the VAE is in float32 mode, as it overflows in float16
if self.vae.dtype == torch.float16 and self.vae.config.force_upcast:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

if not output_type == "latent":
# make sure the VAE is in float32 mode, as it overflows in float16
needs_upcasting = self.vae.dtype == torch.float16 and self.vae.config.force_upcast

if needs_upcasting:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)

image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]

# cast back to fp16 if needed
if needs_upcasting:
self.vae.to(dtype=torch.float16)
else:
image = latents
return StableDiffusionXLPipelineOutput(images=image)
Expand Down