Skip to content

Commit

Permalink
Merge pull request #15751 from LoganBooker/LoganBooker-Add-AlignYourS…
Browse files Browse the repository at this point in the history
…teps-Scheduler

Add Align Your Steps to available schedulers
  • Loading branch information
AUTOMATIC1111 committed Jun 8, 2024
2 parents 3c7384a + 1d74482 commit 46bcfbe
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions modules/sd_schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import k_diffusion

import numpy as np

from modules import shared

@dataclasses.dataclass
class Scheduler:
Expand All @@ -30,6 +33,33 @@ def sgm_uniform(n, sigma_min, sigma_max, inner_model, device):
sigs += [0.0]
return torch.FloatTensor(sigs).to(device)

def get_align_your_steps_sigmas(n, sigma_min, sigma_max, device='cpu'):
# https://research.nvidia.com/labs/toronto-ai/AlignYourSteps/howto.html
def loglinear_interp(t_steps, num_steps):
"""
Performs log-linear interpolation of a given array of decreasing numbers.
"""
xs = np.linspace(0, 1, len(t_steps))
ys = np.log(t_steps[::-1])

new_xs = np.linspace(0, 1, num_steps)
new_ys = np.interp(new_xs, xs, ys)

interped_ys = np.exp(new_ys)[::-1].copy()
return interped_ys

if shared.sd_model.is_sdxl:
sigmas = [14.615, 6.315, 3.771, 2.181, 1.342, 0.862, 0.555, 0.380, 0.234, 0.113, 0.029]
else:
# Default to SD 1.5 sigmas.
sigmas = [14.615, 6.475, 3.861, 2.697, 1.886, 1.396, 0.963, 0.652, 0.399, 0.152, 0.029]

if n != len(sigmas):
sigmas = np.append(loglinear_interp(sigmas, n), [0.0])
else:
sigmas.append(0.0)

return torch.FloatTensor(sigmas).to(device)

schedulers = [
Scheduler('automatic', 'Automatic', None),
Expand All @@ -38,6 +68,7 @@ def sgm_uniform(n, sigma_min, sigma_max, inner_model, device):
Scheduler('exponential', 'Exponential', k_diffusion.sampling.get_sigmas_exponential),
Scheduler('polyexponential', 'Polyexponential', k_diffusion.sampling.get_sigmas_polyexponential, default_rho=1.0),
Scheduler('sgm_uniform', 'SGM Uniform', sgm_uniform, need_inner_model=True, aliases=["SGMUniform"]),
Scheduler('align_your_steps', 'Align Your Steps', get_align_your_steps_sigmas),
]

schedulers_map = {**{x.name: x for x in schedulers}, **{x.label: x for x in schedulers}}

0 comments on commit 46bcfbe

Please sign in to comment.