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

Add K-LMS scheduler from k-diffusion #185

Merged
merged 11 commits into from
Aug 16, 2022
Merged

Add K-LMS scheduler from k-diffusion #185

merged 11 commits into from
Aug 16, 2022

Conversation

anton-l
Copy link
Member

@anton-l anton-l commented Aug 16, 2022

This PR adds the K-LMS sampler from k-diffusion by Katherine Crowson.

At the moment it only supports discrete beta-schedules (specifically the one for Stable Diffusion) but it will be extended to support continuous sigma-schedules in a follow-up PR.

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Aug 16, 2022

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

Comment on lines 128 to 131
if isinstance(self.scheduler, LmsDiscreteScheduler):
latents = self.scheduler.step(noise_pred, i, latents, **extra_step_kwargs)["prev_sample"]
else:
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs)["prev_sample"]
Copy link
Member Author

Choose a reason for hiding this comment

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

This can be made cleaner by removing the branch with t->i, but for now I'm not touching the timesteps from k-lms for ease of debugging.

Comment on lines 115 to 117
if isinstance(self.scheduler, LmsDiscreteScheduler):
sigma = self.scheduler.sigmas[i]
latent_model_input = latent_model_input / ((sigma**2 + 1) ** 0.5)
Copy link
Member Author

Choose a reason for hiding this comment

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

We'll probably need a mechanism to specify custom coefficients for inputs in the future, since the Karras scheduler needs it too.

@anton-l anton-l changed the title [WIP] Add K-LMS scheduler from k-diffusion Add K-LMS scheduler from k-diffusion Aug 16, 2022
@anton-l anton-l marked this pull request as ready for review August 16, 2022 14:06
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.

Just wondering about the i vs. sigma API since in karras_ve we pass a sigma:

src/diffusers/__init__.py Outdated Show resolved Hide resolved
@@ -118,7 +125,10 @@ def __call__(
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

# compute the previous noisy sample x_t -> x_t-1
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs)["prev_sample"]
if isinstance(self.scheduler, LmsDiscreteScheduler):
latents = self.scheduler.step(noise_pred, i, latents, **extra_step_kwargs)["prev_sample"]
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we maybe pass the sigma here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok we need it anyway - good to leave as is for me!

Copy link
Member Author

Choose a reason for hiding this comment

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

The i is needed inside the step for other calculations too, and we can't reverse the sigma into i, so we'll need it like this until a more full refactor.

@anton-l anton-l merged commit d7b6920 into main Aug 16, 2022
@anton-l anton-l deleted the lms-scheduler branch August 17, 2022 12:35
@rossli0102
Copy link

prompt = ["a logo of Knicks and championship "]

height = 512 # default height of Stable Diffusion
width = 512 # default width of Stable Diffusion

num_inference_steps = 100 # Number of denoising steps

guidance_scale = 7.5 # Scale for classifier-free guidance

generator = torch.manual_seed(32) # Seed generator to create the inital latent noise

batch_size = 1

yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
* test LMS with LDM

* test LMS with LDM

* Interchangeable sigma and timestep. Added dummy objects

* Debug

* cuda generator

* Fix derivatives

* Update tests

* Rename Lms->LMS
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.

4 participants