Skip to content

[2737]: Add DPMSolverMultistepScheduler to CLIP guided community pipeline #2779

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 2 commits into from
Mar 23, 2023

Conversation

nipunjindal
Copy link
Contributor

@nipunjindal nipunjindal commented Mar 22, 2023

Enabled DPMSolverMultistepScheduler in CLIP-guided pipeline.
Issue: #2737

Here is code to test the changes:

import torch
from diffusers import (
    DiffusionPipeline,
    DPMSolverMultistepScheduler,
    LMSDiscreteScheduler,
)
from transformers import CLIPFeatureExtractor, CLIPModel

feature_extractor = CLIPFeatureExtractor.from_pretrained(
    "laion/CLIP-ViT-B-32-laion2B-s34B-b79K"
)
clip_model = CLIPModel.from_pretrained(
    "laion/CLIP-ViT-B-32-laion2B-s34B-b79K", torch_dtype=torch.float16
)


guided_pipeline = DiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    custom_pipeline="/home/njindal/diffusers/examples/community/clip_guided_stable_diffusion.py",
    clip_model=clip_model,
    feature_extractor=feature_extractor,
    torch_dtype=torch.float16,
)
guided_pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
    guided_pipeline.scheduler.config
)

guided_pipeline.enable_attention_slicing()
guided_pipeline = guided_pipeline.to("cuda")

prompt = "fantasy book cover, full moon, fantasy forest landscape, golden vector elements, fantasy magic, dark light night, intricate, elegant, sharp focus, illustration, highly detailed, digital painting, concept art, matte, art by WLOP and Artgerm and Albert Bierstadt, masterpiece"

generator = torch.Generator(device="cuda").manual_seed(0)

image = guided_pipeline(
    prompt=prompt,
    num_inference_steps=10,
    guidance_scale=7.5,
    clip_guidance_scale=100,
    num_cutouts=4,
    use_cutouts=False,
    generator=generator,
).images[0]

display(image)

Within 10 steps able to get good result with new scheduler
Screenshot 2023-03-22 at 2 12 49 PM

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Mar 22, 2023

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

@yuvalkirstain
Copy link

yuvalkirstain commented Mar 22, 2023

@nipunjindal thank you! so it seems like the process for DPMSolverMultistepScheduler is the same as PNDMScheduler.

Do you happen to know what those lines are meant to do?

fac = torch.sqrt(beta_prod_t)
sample = pred_original_sample * (fac) + latents * (1 - fac)

and why does it use the cutouts + spherical loss rather than the loss from the original paper?

Also, it probably worth adding a few side-by-side image of w/o classifier guidance vs w classifier guidance (for different classifier guidance scales).

@pete-finesse
Copy link

What would be the process for including the ancestral samplers as well? I find these work best when using clip guidance using the sdk from stability.ai

Comment on lines -128 to -133
if isinstance(self.scheduler, LMSDiscreteScheduler):
sigma = self.scheduler.sigmas[index]
# the model input needs to be scaled to match the continuous ODE formulation in K-LMS
latent_model_input = latents / ((sigma**2 + 1) ** 0.5)
else:
latent_model_input = latents
Copy link
Member

Choose a reason for hiding this comment

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

Do you mind elaborating on this change?

Copy link
Contributor

Choose a reason for hiding this comment

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

This looks correct! This should not be done in the pipeline :-)

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

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

LGTM, well done 🔥

Could you maybe also include a few lines about this support in the README as well since it improves the results (efficiency-wise at least). Do you think that would make sense?

@patrickvonplaten could you give it a quick review too?

@sayakpaul
Copy link
Member

What would be the process for including the ancestral samplers as well? I find these work best when using clip guidance using the sdk from stability.ai

The process would not differ much. You will need to consider adding an ancestral sampler in the condition (such as this). If it's not immediately compatible, then some changes might be necessary. An example:

# For karras style schedulers the model does classifer free guidance using the

Copy link
Contributor

@patrickvonplaten patrickvonplaten left a comment

Choose a reason for hiding this comment

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

Ok to merge for me

@patrickvonplaten patrickvonplaten merged commit 055c90f into huggingface:main Mar 23, 2023
w4ffl35 pushed a commit to w4ffl35/diffusers that referenced this pull request Apr 14, 2023
…line (huggingface#2779)

[2737]: Add DPMSolverMultistepScheduler to CLIP guided community pipelines

Co-authored-by: njindal <[email protected]>
Co-authored-by: Patrick von Platen <[email protected]>
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
…line (huggingface#2779)

[2737]: Add DPMSolverMultistepScheduler to CLIP guided community pipelines

Co-authored-by: njindal <[email protected]>
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