Skip to content
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

add PAG support sd15 controlnet #8820

Merged
merged 7 commits into from
Jul 12, 2024

Conversation

tuanh123789
Copy link
Contributor

What does this PR do?

Adds PAG (Perturbed-Attention Guidance) support for SD 1.5 with controlnet models (StableDiffusionControlNetPAGPipeline)
Continuation of #8710

Before submitting

Who can review?

@yiyixuxu
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.

Sample using SD1.5 controlnet Sample SD1.5 controlnet PAG
test test_10
from diffusers import AutoPipelineForText2Image, ControlNetModel, UniPCMultistepScheduler
from diffusers.utils import load_image
import numpy as np
import torch

import cv2
from PIL import Image

# download an image
image = load_image(
   "https://hf.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/hf-logo.png"
)
image = np.array(image)

# get canny image
image = cv2.Canny(image, 100, 200)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
canny_image = Image.fromarray(image)

# load control net and stable diffusion v1-5
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
pipe = AutoPipelineForText2Image.from_pretrained(
   "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16, enable_pag=True
)

# speed up diffusion process with faster scheduler and memory optimization
# remove following line if xformers is not installed
pipe.enable_xformers_memory_efficient_attention()

pipe.enable_model_cpu_offload()

# generate image
generator = torch.manual_seed(0)
image = pipe(
   "aerial view, a futuristic research complex in a bright foggy jungle, hard lighting",
   guidance_scale=7.5,
   generator=generator,
   image=canny_image,
   pag_scale=10,
).images[0]

@tuanh123789
Copy link
Contributor Author

Hi @a-r-r-o-w I open new PR because I cannot undo deleted pipelines. I have edited it according to your comment, please review again, thank you

Copy link
Member

@a-r-r-o-w a-r-r-o-w left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great. Just small changes remaining and this should be good for merge.

src/diffusers/pipelines/__init__.py Outdated Show resolved Hide resolved
src/diffusers/pipelines/pag/pipeline_pag_controlnet_sd.py Outdated Show resolved Hide resolved
@HuggingFaceDocBuilderDev

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.

@a-r-r-o-w
Copy link
Member

Thank you for addressing the comments! Could we fix the failing tests?

Error logs
...
FAILED tests/pipelines/pag/test_pag_controlnet_sd.py::StableDiffusionControlNetPAGPipelineFastTests::test_pag_cfg - AssertionError: output is different from expected, [0.45505235 0.2785938  0.16334778 0.79689944 0.53095645 0.40135607
   0.7052706  0.69065094 0.41548574]
assert 0.3865616192955017 < 0.001
FAILED tests/pipelines/pag/test_pag_controlnet_sd.py::StableDiffusionControlNetPAGPipelineFastTests::test_pag_uncond - AssertionError: output is different from expected, [0.45127502 0.2797252  0.15970308 0.7993157  0.5414344  0.40160775
   0.7114598  0.69803864 0.4217583 ]
assert 0.39445382411422725 < 0.001
FAILED tests/pipelines/pag/test_pag_controlnet_sd.py::StableDiffusionControlNetPAGPipelineFastTests::test_save_load_optional_components - KeyError: 'tokenizer_2'

Let me know if you need any help. We can merge as soon as these are fixed since everything else looks good to me!

@tuanh123789
Copy link
Contributor Author

Sure can you help me on tests 🤗

@a-r-r-o-w
Copy link
Member

Sure can you help me on tests 🤗

For the first two test failures, you will have to update the expected values to what is the actual value of image_slice is. You can print the values of image_slice and replace expected_slice with that 😜

For the third test, you need to remove SDXLOptionalComponentsTesterMixin in the fast tests class.

Copy link
Collaborator

@yiyixuxu yiyixuxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Comment on lines 534 to 544
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.decode_latents
def decode_latents(self, latents):
deprecation_message = "The decode_latents method is deprecated and will be removed in 1.0.0. Please use VaeImageProcessor.postprocess(...) instead"
deprecate("decode_latents", "1.0.0", deprecation_message, standard_warn=False)

latents = 1 / self.vae.config.scaling_factor * latents
image = self.vae.decode(latents, return_dict=False)[0]
image = (image / 2 + 0.5).clamp(0, 1)
# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
image = image.cpu().permute(0, 2, 3, 1).float().numpy()
return image
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.decode_latents
def decode_latents(self, latents):
deprecation_message = "The decode_latents method is deprecated and will be removed in 1.0.0. Please use VaeImageProcessor.postprocess(...) instead"
deprecate("decode_latents", "1.0.0", deprecation_message, standard_warn=False)
latents = 1 / self.vae.config.scaling_factor * latents
image = self.vae.decode(latents, return_dict=False)[0]
image = (image / 2 + 0.5).clamp(0, 1)
# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
image = image.cpu().permute(0, 2, 3, 1).float().numpy()
return image

I don't think it is used

extra_step_kwargs["generator"] = generator
return extra_step_kwargs

def check_inputs(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a #Copied from here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we have a version of check_inputs with callback_steps removed, and this is the first of its kind

@yiyixuxu
Copy link
Collaborator

feel free to merge once you're happy with it! @a-r-r-o-w

@tuanh123789
Copy link
Contributor Author

@a-r-r-o-w Can you help me with last fail of test 🤗

@a-r-r-o-w
Copy link
Member

a-r-r-o-w commented Jul 12, 2024

@tuanh123789 The failing test is unrelated and nothing needs to be done on your end. I'm happy to merge this once you address YiYi's comment: #8820 (comment)

@tuanh123789
Copy link
Contributor Author

@tuanh123789 The failing test is unrelated and nothing needs to be done on your end. I'm happy to merge this once you address YiYi's comment: #8820 (comment)

:v Sure, will be done right away

Copy link
Member

@a-r-r-o-w a-r-r-o-w left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looking good! One other thing that I just realised - could you update the pipelines doc page with this SD15 Controlnet? https://github.com/huggingface/diffusers/blob/main/docs/source/en/api/pipelines/pag.md. Please see #7944 for an example

@tuanh123789
Copy link
Contributor Author

Thanks, looking good! One other thing that I just realised - could you update the pipelines doc page with this SD15 Controlnet? https://github.com/huggingface/diffusers/blob/main/docs/source/en/api/pipelines/pag.md. Please see #7944 for an example

Done

@a-r-r-o-w a-r-r-o-w merged commit d704b3b into huggingface:main Jul 12, 2024
12 of 15 checks passed
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.

None yet

4 participants