sd_schedulers.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import dataclasses
  2. import torch
  3. import k_diffusion
  4. import numpy as np
  5. from scipy import stats
  6. from modules import shared
  7. def to_d(x, sigma, denoised):
  8. """Converts a denoiser output to a Karras ODE derivative."""
  9. return (x - denoised) / sigma
  10. k_diffusion.sampling.to_d = to_d
  11. @dataclasses.dataclass
  12. class Scheduler:
  13. name: str
  14. label: str
  15. function: any
  16. default_rho: float = -1
  17. need_inner_model: bool = False
  18. aliases: list = None
  19. def uniform(n, sigma_min, sigma_max, inner_model, device):
  20. return inner_model.get_sigmas(n).to(device)
  21. def sgm_uniform(n, sigma_min, sigma_max, inner_model, device):
  22. start = inner_model.sigma_to_t(torch.tensor(sigma_max))
  23. end = inner_model.sigma_to_t(torch.tensor(sigma_min))
  24. sigs = [
  25. inner_model.t_to_sigma(ts)
  26. for ts in torch.linspace(start, end, n + 1)[:-1]
  27. ]
  28. sigs += [0.0]
  29. return torch.FloatTensor(sigs).to(device)
  30. def get_align_your_steps_sigmas(n, sigma_min, sigma_max, device):
  31. # https://research.nvidia.com/labs/toronto-ai/AlignYourSteps/howto.html
  32. def loglinear_interp(t_steps, num_steps):
  33. """
  34. Performs log-linear interpolation of a given array of decreasing numbers.
  35. """
  36. xs = np.linspace(0, 1, len(t_steps))
  37. ys = np.log(t_steps[::-1])
  38. new_xs = np.linspace(0, 1, num_steps)
  39. new_ys = np.interp(new_xs, xs, ys)
  40. interped_ys = np.exp(new_ys)[::-1].copy()
  41. return interped_ys
  42. if shared.sd_model.is_sdxl:
  43. sigmas = [14.615, 6.315, 3.771, 2.181, 1.342, 0.862, 0.555, 0.380, 0.234, 0.113, 0.029]
  44. else:
  45. # Default to SD 1.5 sigmas.
  46. sigmas = [14.615, 6.475, 3.861, 2.697, 1.886, 1.396, 0.963, 0.652, 0.399, 0.152, 0.029]
  47. if n != len(sigmas):
  48. sigmas = np.append(loglinear_interp(sigmas, n), [0.0])
  49. else:
  50. sigmas.append(0.0)
  51. return torch.FloatTensor(sigmas).to(device)
  52. def kl_optimal(n, sigma_min, sigma_max, device):
  53. alpha_min = torch.arctan(torch.tensor(sigma_min, device=device))
  54. alpha_max = torch.arctan(torch.tensor(sigma_max, device=device))
  55. step_indices = torch.arange(n + 1, device=device)
  56. sigmas = torch.tan(step_indices / n * alpha_min + (1.0 - step_indices / n) * alpha_max)
  57. return sigmas
  58. def simple_scheduler(n, sigma_min, sigma_max, inner_model, device):
  59. sigs = []
  60. ss = len(inner_model.sigmas) / n
  61. for x in range(n):
  62. sigs += [float(inner_model.sigmas[-(1 + int(x * ss))])]
  63. sigs += [0.0]
  64. return torch.FloatTensor(sigs).to(device)
  65. def normal_scheduler(n, sigma_min, sigma_max, inner_model, device, sgm=False, floor=False):
  66. start = inner_model.sigma_to_t(torch.tensor(sigma_max))
  67. end = inner_model.sigma_to_t(torch.tensor(sigma_min))
  68. if sgm:
  69. timesteps = torch.linspace(start, end, n + 1)[:-1]
  70. else:
  71. timesteps = torch.linspace(start, end, n)
  72. sigs = []
  73. for x in range(len(timesteps)):
  74. ts = timesteps[x]
  75. sigs.append(inner_model.t_to_sigma(ts))
  76. sigs += [0.0]
  77. return torch.FloatTensor(sigs).to(device)
  78. def ddim_scheduler(n, sigma_min, sigma_max, inner_model, device):
  79. sigs = []
  80. ss = max(len(inner_model.sigmas) // n, 1)
  81. x = 1
  82. while x < len(inner_model.sigmas):
  83. sigs += [float(inner_model.sigmas[x])]
  84. x += ss
  85. sigs = sigs[::-1]
  86. sigs += [0.0]
  87. return torch.FloatTensor(sigs).to(device)
  88. def beta_scheduler(n, sigma_min, sigma_max, inner_model, device):
  89. # From "Beta Sampling is All You Need" [arXiv:2407.12173] (Lee et. al, 2024) """
  90. alpha = shared.opts.beta_dist_alpha
  91. beta = shared.opts.beta_dist_beta
  92. timesteps = 1 - np.linspace(0, 1, n)
  93. timesteps = [stats.beta.ppf(x, alpha, beta) for x in timesteps]
  94. sigmas = [sigma_min + (x * (sigma_max-sigma_min)) for x in timesteps]
  95. sigmas += [0.0]
  96. return torch.FloatTensor(sigmas).to(device)
  97. schedulers = [
  98. Scheduler('automatic', 'Automatic', None),
  99. Scheduler('uniform', 'Uniform', uniform, need_inner_model=True),
  100. Scheduler('karras', 'Karras', k_diffusion.sampling.get_sigmas_karras, default_rho=7.0),
  101. Scheduler('exponential', 'Exponential', k_diffusion.sampling.get_sigmas_exponential),
  102. Scheduler('polyexponential', 'Polyexponential', k_diffusion.sampling.get_sigmas_polyexponential, default_rho=1.0),
  103. Scheduler('sgm_uniform', 'SGM Uniform', sgm_uniform, need_inner_model=True, aliases=["SGMUniform"]),
  104. Scheduler('kl_optimal', 'KL Optimal', kl_optimal),
  105. Scheduler('align_your_steps', 'Align Your Steps', get_align_your_steps_sigmas),
  106. Scheduler('simple', 'Simple', simple_scheduler, need_inner_model=True),
  107. Scheduler('normal', 'Normal', normal_scheduler, need_inner_model=True),
  108. Scheduler('ddim', 'DDIM', ddim_scheduler, need_inner_model=True),
  109. Scheduler('beta', 'Beta', beta_scheduler, need_inner_model=True),
  110. ]
  111. schedulers_map = {**{x.name: x for x in schedulers}, **{x.label: x for x in schedulers}}