sd_schedulers.py 3.9 KB

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