Skip to content

Input type (c10::Half) and bias type (float) mismatch in pipeline_stable_diffusion_xl.py #4752

@Yukun-Huang

Description

@Yukun-Huang

Describe the bug

I'm using the official example and trying to call StableDiffusionXLPipeline (fp16) twice. But in the second call, an error message appeared:

RuntimeError: Input type (c10::Half) and bias type (float) should be the same.

There are similar issues like #4619 and #4478 but don't seem to apply in my case.

I found the reason for this error is from the following code:

# 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":
image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]
else:
image = latents
return StableDiffusionXLPipelineOutput(images=image)

For the second call of pipe, self.vae.dtype is already float32, so the type conversion in L840-L842 is skipped, which makes latents still be float16 and conflicts with float32 vae.

Reproduction

import torch
from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16,
                                         use_safetensors=True, variant="fp16")
pipe.to("cuda")

prompt = "An astronaut riding a green horse"

pipe(prompt=prompt).images[0].save('1.jpg')

pipe(prompt=prompt).images[0].save('2.jpg')
# RuntimeError: Input type (c10::Half) and bias type (float) should be the same

Logs

No response

System Info

  • diffusers version: 0.21.0.dev0
  • Platform: Linux-5.4.0-122-generic-x86_64-with-glibc2.17
  • Python version: 3.8.15
  • PyTorch version (GPU?): 1.13.1 (True)
  • Huggingface_hub version: 0.16.2
  • Transformers version: 4.30.2
  • Accelerate version: 0.18.0
  • xFormers version: not installed
  • Using GPU in script?: Yes
  • Using distributed or parallel set-up in script?: No

Who can help?

@williamberman, @patrickvonplaten, and @sayakpaul

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions