Skip to content

[BugFix]: Fixed add_noise in LMSDiscreteScheduler #253

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 3 commits into from
Aug 29, 2022

Conversation

nicolas-dufour
Copy link
Contributor

The formulation of add noise is LMSDiscreteScheduler is wrong (see crowsonkb/k-diffusion#4). This PR fixes that

Copy link
Contributor

@patil-suraj patil-suraj left a comment

Choose a reason for hiding this comment

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

Ah, good catch! Will let @anton-l take a final look, he knows the best here.

@patil-suraj patil-suraj requested a review from anton-l August 26, 2022 13:00
Copy link
Member

@anton-l anton-l left a comment

Choose a reason for hiding this comment

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

Hi @nicolas-dufour! For this particular sampler, the model needs to be trained similarly to DDPM/DDIM (i.e. with the current add_noise). That's what works for Stable Diffusion, as far as I know (let me know if I misunderstand something!).
But if you find a way to convert the beta schedule closer to the original k-lms, feel free to update the PR :)

@nicolas-dufour
Copy link
Contributor Author

nicolas-dufour commented Aug 26, 2022

Hi @anton-l, I have tried to use the current add_noise with stable diffusion doing some img2img but it did not work and the only way i managed to make it work was to change add_noise to the proposed change.

From what i can see from k-diffusion they also use this noise technique at train time (https://github.com/crowsonkb/k-diffusion/blob/3f93c28088890e4d6bc593072739e1d6e759b392/k_diffusion/layers.py#L28). It's also what is proposed by the original Karras et al paper on which k-diffusion scheduler is based (https://arxiv.org/pdf/2206.00364.pdf Algo 2).

@patrickvonplaten
Copy link
Contributor

patrickvonplaten commented Aug 29, 2022

Hi @anton-l, I have tried to use the current add_noise with stable diffusion doing some img2img but it did not work and the only way i managed to make it work was to change add_noise to the proposed change.

From what i can see from k-diffusion they also use this noise technique at train time (https://github.com/crowsonkb/k-diffusion/blob/3f93c28088890e4d6bc593072739e1d6e759b392/k_diffusion/layers.py#L28). It's also what is proposed by the original Karras et al paper on which k-diffusion scheduler is based (https://arxiv.org/pdf/2206.00364.pdf Algo 2).

Thanks for the links here @nicolas-dufour - from reading the code a bit, I think the sampler can work with both cases, e.g. it can work for inference on a model that was trained on classic DDPM, but as @nicolas-dufour pointed out one should also be able to train the model with the original formula.

Could we maybe allow for both and add a config parameter? Something like: predict_ddpm_criterion=False/True ? What do you think @anton-l ?

@patil-suraj
Copy link
Contributor

Could we maybe allow for both and add a config parameter? Something like: predict_ddpm_criterion=False/True

I'm in favor of this. As @nicolas-dufour said, the current add_noise doesn't work for imag2image, in-painting. So would be important to have this.

Co-authored-by: Anton Lozhkov <aglozhkov@gmail.com>
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Aug 29, 2022

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

@anton-l anton-l mentioned this pull request Aug 29, 2022
@patrickvonplaten
Copy link
Contributor

@anton-l let's merge this and then move onto your follow-up PR?

Copy link
Member

@anton-l anton-l left a comment

Choose a reason for hiding this comment

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

@nicolas-dufour thank you for the fix! Your investigation spawned another PR for img2img and K-LMS compatibility in general, so feel free to join the conversation, if you'd like: #270

@anton-l anton-l merged commit da7d4cf into huggingface:main Aug 29, 2022
@patrickvonplaten
Copy link
Contributor

Awesome addition @nicolas-dufour ❤️

natolambert pushed a commit that referenced this pull request Sep 7, 2022
* Fixed add_noise in LMSDiscreteScheduler

* Linting

* Update src/diffusers/schedulers/scheduling_lms_discrete.py

Co-authored-by: Anton Lozhkov <aglozhkov@gmail.com>

Co-authored-by: Anton Lozhkov <aglozhkov@gmail.com>
@nicolas-dufour nicolas-dufour deleted the fix_lms branch September 16, 2022 16:38
PhaneeshB pushed a commit to nod-ai/diffusers that referenced this pull request Mar 1, 2023
…ngface#253)

* Disable passing of sm_arch to iree-compile CL args by default.

* Fix formatting.
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
* Fixed add_noise in LMSDiscreteScheduler

* Linting

* Update src/diffusers/schedulers/scheduling_lms_discrete.py

Co-authored-by: Anton Lozhkov <aglozhkov@gmail.com>

Co-authored-by: Anton Lozhkov <aglozhkov@gmail.com>
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.

5 participants