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

[Schedulers] Add beta sigmas / beta noise schedule #9509

Merged
merged 1 commit into from
Sep 25, 2024

Conversation

hlky
Copy link
Collaborator

@hlky hlky commented Sep 24, 2024

What does this PR do?

This PR adds beta sigmas from Beta Sampling is All You Need. Added only to Euler for now, after initial review we will add it to other schedulers. Documentation will be updated for this scheduler and any others implemented in a subsequent PR after the initial review and after they've been added to other schedulers.

The implementation is quite simple, we add use_beta_sigmas parameter and _convert_to_beta function.

Beta sigmas depends on scipy so we add a check with is_scipy_available().

The check for self.config.use_exponential_sigmas and self.config.use_karras_sigmas is moved to schedule instantiation and changed to use sum([self.config.use_beta_sigmas, self.config.use_exponential_sigmas, self.config.use_karras_sigmas]) > 1, any future noise schedules can be added to the list to ensure only one can be selected.

As a note for reviewers: alpha and beta in _convert_to_beta are set to the default values used by Forge, should we make these configurable so we can set e.g. beta_sigmas_alpha=1.0, beta_sigmas_beta=1.0? I don't know how common it is to change these values. This is also relevant to future polyexponential sigmas, polyexponential produces the same as exponential unless the rho value is changed, again I don't know how common it is to change this value, if we're not going to make the values configurable we don't need to add polyexponential. However it would be nice to have 1:1 compatibility with the webuis.

Example usage:

from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler
import torch

pipeline: StableDiffusionXLPipeline = StableDiffusionXLPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    variant="fp16",
    torch_dtype=torch.float16,
)
pipeline.scheduler = EulerDiscreteScheduler.from_config(
    pipeline.scheduler.config, timestep_spacing="linspace", use_beta_sigmas=True
)

Note that we set timestep_spacing="linspace" in addition to use_beta_sigmas=True, this is because XL's scheduler config uses timestep_spacing="leading" by default and the intent is to match results from the webuis where these noise schedules are currently used. We may wish to override timestep_spacing to linspace when use_beta_sigmas=True or add a warning.

Tested against Forge (note that cuda device for generator is essential to match Forge results):

generator = torch.Generator("cuda").manual_seed(49136503742430)

image = pipeline(
    prompt="enormous kirby. space background",
    num_inference_steps=20,
    guidance_scale=5.0,
    generator=generator,
).images[0]
image

output

and with the same settings on Forge we get the same result:
00001-49136503742430

The output is similar yet better than exponential #9499 note the shape of Kirby's feet and the planets.

Both beta and exponential are better than the default noise schedule with XL:
output2

#9490

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

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

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.

left a feedback! thanks!

src/diffusers/schedulers/scheduling_euler_discrete.py Outdated Show resolved Hide resolved
src/diffusers/schedulers/scheduling_euler_discrete.py Outdated Show resolved Hide resolved
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!

@yiyixuxu
Copy link
Collaborator

we need make style now

@hlky
Copy link
Collaborator Author

hlky commented Sep 25, 2024

I've applied make style.

@yiyixuxu yiyixuxu merged commit 1c6ede9 into huggingface:main Sep 25, 2024
15 checks passed
leisuzz pushed a commit to leisuzz/diffusers that referenced this pull request Oct 11, 2024
sayakpaul pushed a commit that referenced this pull request Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants