Skip to content

fix a bug of prompt embeds in sdxl #4099

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

Merged
merged 24 commits into from
Jul 24, 2023

Conversation

xiaohu2015
Copy link
Contributor

@xiaohu2015 xiaohu2015 commented Jul 14, 2023

What does this PR do?

Fixes the bug in #3995
when num_images_per_prompt > 1, it give a error

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jul 14, 2023

The documentation is not available anymore as the PR was closed or merged.

@pcuenca
Copy link
Member

pcuenca commented Jul 14, 2023

Hi @xiaohu2015! Could you please explain the bug this solves, and how to to reproduce it?

Many thanks!

@xiaohu2015
Copy link
Contributor Author

Hi @xiaohu2015! Could you please explain the bug this solves, and how to to reproduce it?

Many thanks!

Fixes the bug in #3995
when num_images_per_prompt > 1, it give a error

@patrickvonplaten
Copy link
Contributor

Hey @xiaohu2015,

Thanks for the PR. I can't reproduce the problem with num_images_per_prompt when using the following code snippet:

from diffusers import DiffusionPipeline, EulerDiscreteScheduler, StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMScheduler
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline, AutoencoderKL
import time
from pytorch_lightning import seed_everything
import os
from huggingface_hub import HfApi
# from compel import Compel
import torch
import sys
from pathlib import Path
import requests
from PIL import Image
from io import BytesIO

api = HfApi()
start_time = time.time()

use_refiner = True

vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16, force_upcast=True)
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-0.9", vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True, local_files_only=True)
pipe.to("cuda")

refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-0.9", vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
refiner.to("cuda")


prompt = "An astronaut riding a green horse on Mars"
steps = 20

image = pipe(prompt=prompt, num_inference_steps=steps, num_images_per_prompt=2, output_type="pil").images[0]
image = refiner(prompt=prompt, num_inference_steps=steps - 10, num_images_per_prompt=2, image=image).images[0]

@patrickvonplaten
Copy link
Contributor

Could you maybe add a code snippet that shows the error?

@xiaohu2015
Copy link
Contributor Author

xiaohu2015 commented Jul 18, 2023

from diffusers import DiffusionPipeline, EulerDiscreteScheduler, StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMScheduler
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline, AutoencoderKL
import time
from pytorch_lightning import seed_everything
import os
from huggingface_hub import HfApi
# from compel import Compel
import torch
import sys
from pathlib import Path
import requests
from PIL import Image
from io import BytesIO

api = HfApi()
start_time = time.time()

use_refiner = True

vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16, force_upcast=True)
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-0.9", vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True, local_files_only=True)
pipe.to("cuda")

refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-0.9", vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
refiner.to("cuda")


prompt = "An astronaut riding a green horse on Mars"
negative_prompt = ""
steps = 20

prompt_embeds, negative_prompt_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds = pipe.encode_prompt(prompt, "cuda", 1, True, negative_prompt)

image = pipe(prompt_embeds=prompt_embeds,
        negative_prompt_embeds=negative_prompt_embeds,
        pooled_prompt_embeds=pooled_prompt_embeds,
        negative_pooled_prompt_embeds=negative_pooled_prompt_embeds, num_inference_steps=steps, num_images_per_prompt=2, output_type="pil").images[0]
#image = refiner(prompt=prompt, num_inference_steps=steps - 10, num_images_per_prompt=2, image=image).images[0]

@patrickvonplaten

@patrickvonplaten
Copy link
Contributor

Great, thanks for the reproducible codesnippet! I can indeed confirm that this causes a bug! Your PR looks very nice - could you maybe just add one test here: https://github.com/huggingface/diffusers/blob/main/tests/pipelines/stable_diffusion_xl/test_stable_diffusion_xl.py ?

@patrickvonplaten
Copy link
Contributor

Also can you run make style and make fix-copies so that your changes are also reflected in the pipeline_stable_diffusion_inpaint.py file?

@patrickvonplaten
Copy link
Contributor

Hey @xiaohu2015,

It seems like the changes break the following use case at the moment:

FAILED tests/pipelines/stable_diffusion_xl/test_stable_diffusion_xl_inpaint.py::StableDiffusionXLInpaintPipelineFastTests::test_num_images_per_prompt - RuntimeError: Sizes of tensors must match except in dimension 0. Expected size 128 but got size 64 for tensor number 1 in the list.

Could we try to make sure that the changes both work with the previous test and fix the new use case?

@xiaohu2015
Copy link
Contributor Author

Hey @xiaohu2015,

It seems like the changes break the following use case at the moment:

FAILED tests/pipelines/stable_diffusion_xl/test_stable_diffusion_xl_inpaint.py::StableDiffusionXLInpaintPipelineFastTests::test_num_images_per_prompt - RuntimeError: Sizes of tensors must match except in dimension 0. Expected size 128 but got size 64 for tensor number 1 in the list.

Could we try to make sure that the changes both work with the previous test and fix the new use case?

thanks, it seems all work well

@xiaohu2015 xiaohu2015 changed the title fix bug in sdxl fix bug of prompt embeds in sdxl Jul 21, 2023
@xiaohu2015 xiaohu2015 changed the title fix bug of prompt embeds in sdxl fix a bug of prompt embeds in sdxl Jul 21, 2023
@patrickvonplaten
Copy link
Contributor

Great! Can we now in a final step add some new tests that show that this PR fixes a bug? :-)

@patrickvonplaten patrickvonplaten merged commit 8e5921c into huggingface:main Jul 24, 2023
@xiaohu2015 xiaohu2015 deleted the patch-3 branch July 24, 2023 10:24
orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
* fix bug in sdxl

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_controlnet_sd_xl.py

* Update pipeline_controlnet_sd_xl.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update test_stable_diffusion_xl.py

* Update test_stable_diffusion_xl.py

* Update test_stable_diffusion_xl.py

add test on prompt_embeds

* add test on prompt_embeds

---------

Co-authored-by: Patrick von Platen <[email protected]>
orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
* fix bug in sdxl

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_controlnet_sd_xl.py

* Update pipeline_controlnet_sd_xl.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update test_stable_diffusion_xl.py

* Update test_stable_diffusion_xl.py

* Update test_stable_diffusion_xl.py

add test on prompt_embeds

* add test on prompt_embeds

---------

Co-authored-by: Patrick von Platen <[email protected]>
orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
* fix bug in sdxl

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_controlnet_sd_xl.py

* Update pipeline_controlnet_sd_xl.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update test_stable_diffusion_xl.py

* Update test_stable_diffusion_xl.py

* Update test_stable_diffusion_xl.py

add test on prompt_embeds

* add test on prompt_embeds

---------

Co-authored-by: Patrick von Platen <[email protected]>
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
* fix bug in sdxl

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_controlnet_sd_xl.py

* Update pipeline_controlnet_sd_xl.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update test_stable_diffusion_xl.py

* Update test_stable_diffusion_xl.py

* Update test_stable_diffusion_xl.py

add test on prompt_embeds

* add test on prompt_embeds

---------

Co-authored-by: Patrick von Platen <[email protected]>
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
* fix bug in sdxl

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_controlnet_sd_xl.py

* Update pipeline_controlnet_sd_xl.py

* Update pipeline_stable_diffusion_xl.py

* Update pipeline_stable_diffusion_xl_img2img.py

* Update pipeline_stable_diffusion_xl_inpaint.py

* Update test_stable_diffusion_xl.py

* Update test_stable_diffusion_xl.py

* Update test_stable_diffusion_xl.py

add test on prompt_embeds

* add test on prompt_embeds

---------

Co-authored-by: Patrick von Platen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants