Uh oh!
There was an error while loading. Please reload this page.
StableDiffusionLatentUpscalePipeline - positive/negative prompt embeds support - #8947
Conversation
rootonchair
commented
Jul 23, 2024
Test script fromdiffusersimportStableDiffusionLatentUpscalePipeline, StableDiffusionPipelineimporttorchpipeline=StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
pipeline.to("cuda")
upscaler=StableDiffusionLatentUpscalePipeline.from_pretrained("stabilityai/sd-x2-latent-upscaler", torch_dtype=torch.float16)
upscaler.to("cuda")
prompt="a photo of an astronaut high resolution, unreal engine, ultra realistic"generator=torch.manual_seed(33)
# we stay in latent space! Let's make sure that Stable Diffusion returns the image# in latent spacelow_res_latents=pipeline(prompt, generator=generator, output_type="latent").imagesupscaled_image=upscaler(
prompt=prompt,
image=low_res_latents,
num_inference_steps=20,
guidance_scale=0,
generator=generator,
).images[0]
# Let's save the upscaled image under "upscaled_astronaut.png"upscaled_image.save("astronaut_1024.png")
prompt_embeds, negative_prompt_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds=upscaler.encode_prompt(
prompt=prompt,
device=upscaler._execution_device,
do_classifier_free_guidance=False,
)
upscaled_image=upscaler(
image=low_res_latents,
num_inference_steps=20,
guidance_scale=0,
generator=generator,
prompt_embeds=prompt_embeds,
pooled_prompt_embeds=pooled_prompt_embeds,
).images[0]
upscaled_image.save("embeds_astronaut_1024.png")
# as a comparison: Let's also save the low-res imagewithtorch.no_grad():
image=pipeline.decode_latents(low_res_latents)
image=pipeline.numpy_to_pil(image)[0]
image.save("astronaut_512.png") |
HuggingFaceDocBuilderDev
commented
Jul 24, 2024
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
yiyixuxu
left a comment
There was a problem hiding this comment.
oh thanks! I think it looks really nice!
sayakpaul
left a comment
There was a problem hiding this comment.
LGTM! Could you also share some results stemming from this change?
Let's also add a fast test for this?
rootonchair
commented
Jul 25, 2024
Sure which does not have much different
Should I open another PR for this @sayakpaul ? |
sayakpaul
commented
Jul 25, 2024
We can do this in this PR |
…r/diffusers into latent_upscaler_prompt_embeds
…r/diffusers into latent_upscaler_prompt_embeds
rootonchair
commented
Aug 11, 2024
Hi @sayakpaul, could you help me review this PR? |
…r/diffusers into latent_upscaler_prompt_embeds
| @@ -0,0 +1,218 @@ | |||
| # coding=utf-8 | |||
There was a problem hiding this comment.
oh actually I think the tests for latent upscaler is here https://github.com/huggingface/diffusers/blob/main/tests/pipelines/stable_diffusion_2/test_stable_diffusion_latent_upscale.py
maybe we add new tests there? very sorry for all the additional work to create a new test from scratch
rootonchair
commented
Aug 19, 2024
@yiyixuxu I have just update the existing test. Could you help me review? Btw, adding new test is kinda interesting too. So no worries |
| self.assertEqual(image.shape, (1, 256, 256, 3)) | ||
| expected_slice = np.array( | ||
| [0.47222412, 0.41921633, 0.44717434, 0.46874192, 0.42588258, 0.46150726, 0.4677534, 0.45583832, 0.48579055] | ||
| [0.3970313, 0.3768756, 0.41147298, 0.4716793, 0.5115408, 0.44601366, 0.43763855, 0.46781355, 0.46358708] |
There was a problem hiding this comment.
why do we need to update the expected_slice here? the results of existing tests should not change, no?
| if image.shape[1] == 3: | ||
| # encode image if not in latent-space yet | ||
| image = self.vae.encode(image).latent_dist.sample() * self.vae.config.scaling_factor | ||
| image = retrieve_latents(self.vae.encode(image), generator=generator) * self.vae.config.scaling_factor |
There was a problem hiding this comment.
@yiyixuxu I think it's due to this line. The old code does not take in generator
yiyixuxu
left a comment
There was a problem hiding this comment.
I think we got the order of the negative_prompt_embeds and prompt_embeds reversed, that's why the previous test wasn't passing. Let's make the change, and change the test back and make sure it passes :)
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
…sion_latent_upscale.py Co-authored-by: YiYi Xu <yixu310@gmail.com>
…sion_latent_upscale.py Co-authored-by: YiYi Xu <yixu310@gmail.com>
…t_upscale.py Co-authored-by: YiYi Xu <yixu310@gmail.com>
rootonchair
commented
Aug 21, 2024
@yiyixuxu thanks for the catch! Sorry I did not check it thoroughly |
…s support (#8947) * make latent upscaler accept prompt embeds --------- Co-authored-by: Dhruv Nair <dhruv.nair@gmail.com> Co-authored-by: Sayak Paul <spsayakpaul@gmail.com> Co-authored-by: YiYi Xu <yixu310@gmail.com>


What does this PR do?
Fixes#8895
Before submitting
documentation guidelines, and
here are tips on formatting docstrings.
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.
@yiyixuxu