Skip to content

Add support Karras sigmas for StableDiffusionKDiffusionPipeline #2874

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 11 commits into from
Mar 31, 2023

Conversation

takuma104
Copy link
Contributor

@takuma104 takuma104 commented Mar 28, 2023

This PR adds an option use_karras_sigmas= to apply the Karras sigmas discussed in issues like #1633, #2801 for the StableDiffusionKDiffusionPipeline. The original code is mostly by @Stax124, with some modifications made to apply it to the latest StableDiffusionKDiffusionPipeline. I opened this PR because the topic of DPM++ 2M Karras has come up repeatedly, and I personally wanted to use it.

Usage:

import torch
from diffusers import StableDiffusionKDiffusionPipeline
pipe = StableDiffusionKDiffusionPipeline.from_pretrained('dreamlike-art/dreamlike-anime-1.0', 
    torch_dtype=torch.float16,
    safety_checker = None,
).to('cuda')
pipe.set_scheduler('sample_dpmpp_2m')
image = pipe(prompt='best quality, a tuxedo cat',
    num_inference_steps=15, generator=torch.Generator(device='cuda').manual_seed(0), 
    width=512, height=512, 
    use_karras_sigmas=True).images[0]
image

Effectiveness:

The sigmas seem to change slightly like this.

Default:

[14.6146,  9.6826,  6.6780,  4.7746,  3.5221,  2.6666,  2.0606,  1.6156, 1.2768,  1.0097,  0.7913,  0.6056,  0.4397,  0.2780,  0.0292,  0.0000]

Use Karras option:

[14.6146, 10.8198,  7.9029,  5.6878,  4.0277,  2.8016,  1.9104,  1.2741, 0.8289,  0.5243,  0.3211,  0.1896,  0.1072,  0.0576,  0.0292,  0.0000]

Test:

The integration tests have passed (although there is no coverage for the additional code). For generation test, I couldn't verify pixel matching, but the results seem to be close to those of DPM++ 2M Karras in stable-diffusion-webui.

stable-diffusion-webui with DPM++ 2M Karras option use_karras_sigmas=True use_karras_sigmas=False
webui_00009-0-best quality, a tuxedo diffusers_use_karras diffusers_no_use_karras

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Mar 28, 2023

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

@takuma104
Copy link
Contributor Author

takuma104 commented Mar 28, 2023

For the related topic, I also looked into the question of whether the sample_dpmpp_2m option (without Karras) with StableDiffusionKDiffusionPipeline and the DPMSolverMultistepScheduler with StableDiffusionPipeline are the same.

While they appear quite similar, the results are not identical. Looking at the implementation, DPMSolverMultistepScheduler seems to be written according to the paper. In contrast, the implementation of k-diffusion did not appear to be implemented according to the formulas in the paper.

import torch
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
pipe = StableDiffusionPipeline.from_pretrained('dreamlike-art/dreamlike-anime-1.0', 
    torch_dtype=torch.float16,
    safety_checker = None,
).to('cuda')
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
image = pipe(prompt=prompt, num_inference_steps=15, 
             generator=torch.Generator(device='cuda').manual_seed(0), 
             width=512, height=512).images[0]
image
StableDiffusionKDiffusionPipeline with sample_dpmpp_2m StableDiffusionPipeline with DPMSolverMultistepScheduler
diffusers_no_use_karras dpm_output

@patrickvonplaten
Copy link
Contributor

Very clean!

Could you just run make style to merge this one @takuma104 ? :-)

@takuma104
Copy link
Contributor Author

@patrickvonplaten thanks! I just fixed it.

Comment on lines +461 to +464
use_karras_sigmas (`bool`, *optional*, defaults to `False`):
Use karras sigmas. For example, specifying `sample_dpmpp_2m` to `set_scheduler` will be equivalent to
`DPM++2M` in stable-diffusion-webui. On top of that, setting this option to True will make it `DPM++2M
Karras`.
Copy link
Member

Choose a reason for hiding this comment

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

This is how docstrings should be I think.

Could you maybe just add a link to the paper that introduced Karras sigmas?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sayakpaul Thanks for merging! The so-called Karras Sigmas corresponds to Eq. 5 in the paper. Since I missed the timing, could you please include a citation as a comment when your working on #2905?

Karras, T. (2022, June 1). Elucidating the Design Space of Diffusion-Based Generative Models. arXiv.org. https://arxiv.org/abs/2206.00364

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.

I love everything about this PR.

Changes introduced, PR description, and the considerations given to it.

Well done!

@sayakpaul sayakpaul merged commit 0df4ad5 into huggingface:main Mar 31, 2023
w4ffl35 pushed a commit to w4ffl35/diffusers that referenced this pull request Apr 14, 2023
…ggingface#2874)

* add use_karras_sigmas option

thanks @Stax124

* fix sigma_min/max from scheduler.sigmas

* add docstring

* revert to use k_diffusion_model.sigma, to(device)

* add integration test

* make style
@alexisrolland
Copy link
Contributor

alexisrolland commented May 28, 2023

@takuma104 out of curiosity, why was this functionality implemented in a separate pipeline than StableDiffusionPipeline rather than as a scheduler for StableDiffusionPipeline?

@takuma104
Copy link
Contributor Author

@alexisrolland Currently, there is a use_karras_sigmas=True option in DPMSolverMultistepScheduler (#3001), so please use it if you want to use karras_sigmas in a pipeline other than this one.

@asthinasthi
Copy link

I was looking for all the available samplers. The list is here https://github.com/crowsonkb/k-diffusion/blob/master/k_diffusion/sampling.py

sample_dpm_fast
sample_dpm_adaptive
sample_dpmpp_2s_ancestral
sample_dpmpp_sde
sample_dpmpp_2m
sample_dpmpp_2m_sde

In case anyone is interested.

yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
…ggingface#2874)

* add use_karras_sigmas option

thanks @Stax124

* fix sigma_min/max from scheduler.sigmas

* add docstring

* revert to use k_diffusion_model.sigma, to(device)

* add integration test

* make style
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
…ggingface#2874)

* add use_karras_sigmas option

thanks @Stax124

* fix sigma_min/max from scheduler.sigmas

* add docstring

* revert to use k_diffusion_model.sigma, to(device)

* add integration test

* make style
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