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 ControlNet support for SDXL #675

Merged
merged 31 commits into from
Aug 30, 2024
Merged

Add ControlNet support for SDXL #675

merged 31 commits into from
Aug 30, 2024

Conversation

JingyaHuang
Copy link
Collaborator

@JingyaHuang JingyaHuang commented Aug 6, 2024

What does this PR do?

  • Add ControlNet export support via CLI
  • Regular SDXL
optimum-cli export neuron -m stabilityai/stable-diffusion-xl-base-1.0 --task stable-diffusion-xl --batch_size 1 --height 1024 --width 1024 --controlnet_ids diffusers/controlnet-canny-sdxl-1.0-small --num_images_per_prompt 1 sdxl_neuron_controlnet/
  • Tiny test(compilation failed for tiny on TimestepEmbedding forward pass)
optimum-cli export neuron -m echarlaix/tiny-random-stable-diffusion-xl --task stable-diffusion-xl --batch_size 1 --height 64 --width 64 --controlnet_ids hf-internal-testing/tiny-controlnet-sdxl --num_images_per_prompt 1 sdxl_neuron_tiny_controlnet/
  • Add ControlNet export support via modeling API
from optimum.neuron import NeuronStableDiffusionXLControlNetPipeline

controlnet_id = "diffusers/controlnet-canny-sdxl-1.0-small"
sdxl_id = "stabilityai/stable-diffusion-xl-base-1.0"

input_shapes = {"batch_size": 1, "height": 1024, "width": 1024, "num_images_per_prompt": 1}
compiler_args = {"auto_cast": "matmul", "auto_cast_type": "bf16"}
pipe = NeuronStableDiffusionXLControlNetPipeline.from_pretrained(
    sdxl_id,
    controlnet_ids=controlnet_id,
    export=True,
    **input_shapes,
    **compiler_args,
)
pipe.save_pretrained("sdxl_neuron_controlnet")
  • Add ControlNet pipeline for stable diffusion XL
import cv2
import numpy as np
from diffusers.utils import load_image
from PIL import Image
from optimum.neuron import NeuronStableDiffusionXLControlNetPipeline

# Inputs
prompt = "aerial view, a futuristic research complex in a bright foggy jungle, hard lighting"
negative_prompt = "low quality, bad quality, sketches"

image = load_image(
    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/hf-logo.png"
)
image = np.array(image)
image = cv2.Canny(image, 100, 200)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
image = Image.fromarray(image)

controlnet_conditioning_scale = 0.5  # recommended for good generalization

pipe = NeuronStableDiffusionXLControlNetPipeline.from_pretrained("sdxl_neuron_controlnet")

images = pipe(
    prompt,
    negative_prompt=negative_prompt,
    image=image,
    controlnet_conditioning_scale=controlnet_conditioning_scale,
).images
images[0].save("hug_lab.png")
  • Documentation

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

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

@JingyaHuang JingyaHuang marked this pull request as ready for review August 21, 2024 13:22
Copy link
Member

@michaelbenayoun michaelbenayoun left a comment

Choose a reason for hiding this comment

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

LGTM!

@JingyaHuang JingyaHuang merged commit d857415 into main Aug 30, 2024
10 of 11 checks passed
@JingyaHuang JingyaHuang deleted the controlnet-for-sdxl branch August 30, 2024 12:02
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.

3 participants