stable-diffusion-webui/modules/sd_schedulers.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.7 KiB
Python
Raw Normal View History

2024-03-20 00:17:11 -06:00
import dataclasses
import torch
import k_diffusion
@dataclasses.dataclass
class Scheduler:
name: str
label: str
function: any
default_rho: float = -1
need_inner_model: bool = False
aliases: list = None
2024-03-20 01:27:32 -06:00
def uniform(n, sigma_min, sigma_max, inner_model, device):
return inner_model.get_sigmas(n)
2024-03-20 00:17:11 -06:00
def sgm_uniform(n, sigma_min, sigma_max, inner_model, device):
start = inner_model.sigma_to_t(torch.tensor(sigma_max))
end = inner_model.sigma_to_t(torch.tensor(sigma_min))
sigs = [
inner_model.t_to_sigma(ts)
for ts in torch.linspace(start, end, n + 1)[:-1]
2024-03-20 00:17:11 -06:00
]
sigs += [0.0]
return torch.FloatTensor(sigs).to(device)
2024-04-22 22:09:43 -06:00
def kl_optimal(n, sigma_min, sigma_max, device):
alpha_min = torch.arctan(torch.tensor(sigma_min, device=device))
alpha_max = torch.arctan(torch.tensor(sigma_max, device=device))
sigmas = torch.empty((n+1,), device=device)
for i in range(n+1):
sigmas[i] = torch.tan((i/n) * alpha_min + (1.0-i/n) * alpha_max)
return sigmas
2024-03-20 00:17:11 -06:00
schedulers = [
Scheduler('automatic', 'Automatic', None),
2024-03-20 01:27:32 -06:00
Scheduler('uniform', 'Uniform', uniform, need_inner_model=True),
2024-03-20 00:17:11 -06:00
Scheduler('karras', 'Karras', k_diffusion.sampling.get_sigmas_karras, default_rho=7.0),
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"]),
2024-04-22 22:09:43 -06:00
Scheduler('kl_optimal', 'KL Optimal', kl_optimal),
2024-03-20 00:17:11 -06:00
]
schedulers_map = {**{x.name: x for x in schedulers}, **{x.label: x for x in schedulers}}