FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

refactor DPMSolverMultistepScheduler using sigmas by yiyixuxu · Pull Request #4986 · huggingface/diffusers · GitHub

refactor DPMSolverMultistepScheduler using sigmas - #4986

Merged
yiyixuxu merged 39 commits into
mainfrom
dpm-mstep-sigma-2
Sep 19, 2023
Merged

refactor DPMSolverMultistepScheduler using sigmas#4986
yiyixuxu merged 39 commits into
mainfrom
dpm-mstep-sigma-2

Conversation

yiyixuxu commented Sep 12, 2023
edited
Loading

Copy link
Copy Markdown
Collaborator

I think I overcomplicated things with this #4690 by trying to follow k-diffusion implementation.

trying it again with a simpler approach here: we simply calculate lambda_t, sigma_t and alpha_t from sigma

to-do

  • dpmsolver_multistep
  • dpmsolver_singlestep
  • DEIS
  • UniPC

testing

Notes:
I compared the results against the current implementation (for dpm multistep only). There is some slight numerical difference when using k-sigmas; for example, in the last testing examples below on the 4th row (sde-dpmsolver++, use_karras_sigma=True), you can visually see the outputs are slightly different.
Also, I had to loosen a test here #4986 (comment).
However, the new implementation is more accurate by using the sigma directly.

import torch
from diffusers import StableDiffusionKDiffusionPipeline, DPMSolverMultistepScheduler, StableDiffusionPipeline
import gc

# make sure to update your branch name 
branch = 'dpm-mstep-sigma-2'
#branch = "main"


# test 1: dpmsolver_+++, use_karras_sigma=False 
seed = 33

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
pipe = pipe.to("cuda")
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
    pipe.scheduler.config, use_karras_sigmas=False
)

prompt = "an astronaut riding a horse on mars"

generator = torch.Generator(device="cuda").manual_seed(seed)
image = pipe(prompt, generator=generator, num_inference_steps=20).images[0]

image.save(f"[{branch}]_test_dpmsolver++.png")


# test 2: dpmsolver_+++, use_karras_sigma=True 
seed = 33

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
pipe = pipe.to("cuda")
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
    pipe.scheduler.config, use_karras_sigmas=True
)

prompt = "an astronaut riding a horse on mars"

generator = torch.Generator(device="cuda").manual_seed(seed)
image = pipe(prompt, generator=generator, num_inference_steps=20).images[0]

image.save(f"[{branch}]_test_dpmsolver++_k_sigma.png")

# # test 3: sde-dpmsolver++, use_karras_sigma=False 

seed = 33

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
pipe = pipe.to("cuda")
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
    pipe.scheduler.config, use_karras_sigmas=False, algorithm_type="sde-dpmsolver++"
)

prompt = "an astronaut riding a horse on mars"

generator = torch.Generator(device="cuda").manual_seed(seed)
image = pipe(prompt, generator=generator, num_inference_steps=20).images[0]

image.save(f"[{branch}]_test_sde-dpmsolver++.png")

## test 4: sde-dpmsolver++, use_karras_sigma=True
seed = 33

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
pipe = pipe.to("cuda")
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
    pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++"
)

prompt = "an astronaut riding a horse on mars"

generator = torch.Generator(device="cuda").manual_seed(seed)
image = pipe(prompt, generator=generator, num_inference_steps=20).images[0]
image.save(f"[{branch}]_test_sde-dpmsolver++_k_sigma.png")
main this PR


output, new_output = sample, sample
for t in range(time_step, time_step + scheduler.config.solver_order + 1):
t = scheduler.timesteps[t]

yiyixuxu Sep 13, 2023
edited
Loading

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We used 0,1,2 directly as timestep index here, but they are not even in the self.timestepes. In the previous implementation, it will default to the last timesteps when it is outside of the timesteps range, but I don't think it is intended. I changed it here, I think it makes more sense this way. Let me know if it's not the case


assert sample.dtype == torch.float16

def test_unique_timesteps(self, **config):

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

don't need this test anymore because we allow duplicated timesteps now

Copy link
Copy Markdown
Collaborator Author

@patrickvonplaten another review please :)


scheduler.set_timesteps(scheduler.config.num_train_timesteps)
assert len(scheduler.timesteps.unique()) == scheduler.num_inference_steps
assert len(scheduler.timesteps) == scheduler.num_inference_steps

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

nice!

patrickvonplaten left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Very nice!

rvorias commented Sep 22, 2023
edited
Loading

Copy link
Copy Markdown

@yiyixuxu This refactor has broken stable diffusion (1.5) img2img for unipc

both use denoising strength = 0.6

Using unipc code before this commit:

Using unipc after this commit:

ljk1291 commented Sep 22, 2023
edited
Loading

Copy link
Copy Markdown

@yiyixuxu This refactor has broken stable diffusion (1.5) img2img for unipc

both use denoising strength = 0.6

Using unipc code before this commit:

Using unipc after this commit:

Same with DPMSolverMultistepScheduler using Karras sigmas. Just returns noise with anything under 1.0 denoising strength. Havent tested it without Karras sigmas.

Copy link
Copy Markdown
Collaborator Author

@ljk1291
oh, thanks for reporting! I'm looking into it now!
if you have a reproducible example and can open a new issue - that would be great!

Copy link
Copy Markdown
Contributor

it seems like this bug still resurfaces somehow when using the legacy inpainting pipeline for some reason

Copy link
Copy Markdown
Collaborator Author

Hi @burgalon

can you be more specific? could you maybe open a bug report and provide an example?

thanks!

YiYi

burgalon commented Nov 26, 2023
edited
Loading

Copy link
Copy Markdown
Contributor

@yiyixuxu
oops... happened to reply here #4631 (comment)

yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
---------

Co-authored-by: yiyixuxu <yixu310@gmail,com>
Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
yiyixuxu mentioned this pull request Jan 8, 2024
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
---------

Co-authored-by: yiyixuxu <yixu310@gmail,com>
Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL