-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Allow low precision vae sd xl #4083
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -542,8 +542,9 @@ def prepare_latents( | |
|
|
||
| else: | ||
| # make sure the VAE is in float32 mode, as it overflows in float16 | ||
| image = image.float() | ||
| self.vae.to(dtype=torch.float32) | ||
| if self.vae.config.force_upcast: | ||
| image = image.float() | ||
| self.vae.to(dtype=torch.float32) | ||
|
|
||
| if isinstance(generator, list) and len(generator) != batch_size: | ||
| raise ValueError( | ||
|
|
@@ -559,9 +560,10 @@ def prepare_latents( | |
| else: | ||
| init_latents = self.vae.encode(image).latent_dist.sample(generator) | ||
|
|
||
| self.vae.to(dtype) | ||
| init_latents = init_latents.to(dtype) | ||
| if self.vae.config.force_upcast: | ||
| self.vae.to(dtype) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to possibly move it back here since for the decoder not all layers are upcasted (so we should move it back to fp16 here) |
||
|
|
||
| init_latents = init_latents.to(dtype) | ||
| init_latents = self.vae.config.scaling_factor * init_latents | ||
|
|
||
| if batch_size > init_latents.shape[0] and batch_size % init_latents.shape[0] == 0: | ||
|
|
@@ -624,6 +626,26 @@ def _get_add_time_ids( | |
|
|
||
| return add_time_ids, add_neg_time_ids | ||
|
|
||
| # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_upscale.StableDiffusionUpscalePipeline.upcast_vae | ||
| def upcast_vae(self): | ||
| dtype = self.vae.dtype | ||
| self.vae.to(dtype=torch.float32) | ||
| use_torch_2_0_or_xformers = isinstance( | ||
| self.vae.decoder.mid_block.attentions[0].processor, | ||
| ( | ||
| AttnProcessor2_0, | ||
| XFormersAttnProcessor, | ||
| LoRAXFormersAttnProcessor, | ||
| LoRAAttnProcessor2_0, | ||
| ), | ||
| ) | ||
| # if xformers or torch_2_0 is used attention block does not need | ||
| # to be in float32 which can save lots of memory | ||
| if use_torch_2_0_or_xformers: | ||
| self.vae.post_quant_conv.to(dtype) | ||
| self.vae.decoder.conv_in.to(dtype) | ||
| self.vae.decoder.mid_block.to(dtype) | ||
|
|
||
| @torch.no_grad() | ||
| @replace_example_docstring(EXAMPLE_DOC_STRING) | ||
| def __call__( | ||
|
|
@@ -932,25 +954,9 @@ def __call__( | |
| callback(i, t, latents) | ||
|
|
||
| # make sure the VAE is in float32 mode, as it overflows in float16 | ||
| self.vae.to(dtype=torch.float32) | ||
|
|
||
| use_torch_2_0_or_xformers = isinstance( | ||
| self.vae.decoder.mid_block.attentions[0].processor, | ||
| ( | ||
| AttnProcessor2_0, | ||
| XFormersAttnProcessor, | ||
| LoRAXFormersAttnProcessor, | ||
| LoRAAttnProcessor2_0, | ||
| ), | ||
| ) | ||
| # if xformers or torch_2_0 is used attention block does not need | ||
| # to be in float32 which can save lots of memory | ||
| if use_torch_2_0_or_xformers: | ||
| self.vae.post_quant_conv.to(latents.dtype) | ||
| self.vae.decoder.conv_in.to(latents.dtype) | ||
| self.vae.decoder.mid_block.to(latents.dtype) | ||
| else: | ||
| latents = latents.float() | ||
| 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": | ||
| image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0] | ||
|
|
||
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.
cc @sayakpaul every config has force_upcast