Correctly keep vae in `float16` when using PyTorch 2 or xFormers (#4019) · larme/diffusers@d6b8614 · GitHub
Skip to content

Commit d6b8614

Browse files
pcuencaxiaohu2015
andauthored
Correctly keep vae in float16 when using PyTorch 2 or xFormers (huggingface#4019)
* Update pipeline_stable_diffusion_xl.py fix a bug * Update pipeline_stable_diffusion_xl_img2img.py * Update pipeline_stable_diffusion_xl_img2img.py * Update pipeline_stable_diffusion_upscale.py * style --------- Co-authored-by: Hu Ye <xiaohuzc@gmail.com>
1 parent e4f6c37 commit d6b8614

3 files changed

Lines changed: 30 additions & 20 deletions

File tree

‎src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py‎

Lines changed: 10 additions & 6 deletions

‎src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -786,15 +786,18 @@ def __call__(
786786
# make sure the VAE is in float32 mode, as it overflows in float16
787787
self.vae.to(dtype=torch.float32)
788788

789-
use_torch_2_0_or_xformers = self.vae.decoder.mid_block.attentions[0].processor in [
790-
AttnProcessor2_0,
791-
XFormersAttnProcessor,
792-
LoRAXFormersAttnProcessor,
793-
LoRAAttnProcessor2_0,
794-
]
789+
use_torch_2_0_or_xformers = isinstance(
790+
self.vae.decoder.mid_block.attentions[0].processor,
791+
(
792+
AttnProcessor2_0,
793+
XFormersAttnProcessor,
794+
LoRAXFormersAttnProcessor,
795+
LoRAAttnProcessor2_0,
796+
),
797+
)
795798
# if xformers or torch_2_0 is used attention block does not need
796799
# to be in float32 which can save lots of memory
797-
if not use_torch_2_0_or_xformers:
800+
if use_torch_2_0_or_xformers:
798801
self.vae.post_quant_conv.to(latents.dtype)
799802
self.vae.decoder.conv_in.to(latents.dtype)
800803
self.vae.decoder.mid_block.to(latents.dtype)

‎src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py‎

Lines changed: 10 additions & 7 deletions

0 commit comments

Comments
 (0)