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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
+
from scipy import integrate
|
| 4 |
+
import torch
|
| 5 |
+
from torch import nn
|
| 6 |
+
from torchdiffeq import odeint
|
| 7 |
+
import torchsde
|
| 8 |
+
from tqdm.auto import trange, tqdm
|
| 9 |
+
|
| 10 |
+
from . import utils
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def append_zero(x):
|
| 14 |
+
return torch.cat([x, x.new_zeros([1])])
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def get_sigmas_karras(n, sigma_min, sigma_max, rho=7., device='cpu'):
|
| 18 |
+
"""Constructs the noise schedule of Karras et al. (2022)."""
|
| 19 |
+
ramp = torch.linspace(0, 1, n)
|
| 20 |
+
min_inv_rho = sigma_min ** (1 / rho)
|
| 21 |
+
max_inv_rho = sigma_max ** (1 / rho)
|
| 22 |
+
sigmas = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
|
| 23 |
+
return append_zero(sigmas).to(device)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def get_sigmas_exponential(n, sigma_min, sigma_max, device='cpu'):
|
| 27 |
+
"""Constructs an exponential noise schedule."""
|
| 28 |
+
sigmas = torch.linspace(math.log(sigma_max), math.log(sigma_min), n, device=device).exp()
|
| 29 |
+
return append_zero(sigmas)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def get_sigmas_polyexponential(n, sigma_min, sigma_max, rho=1., device='cpu'):
|
| 33 |
+
"""Constructs an polynomial in log sigma noise schedule."""
|
| 34 |
+
ramp = torch.linspace(1, 0, n, device=device) ** rho
|
| 35 |
+
sigmas = torch.exp(ramp * (math.log(sigma_max) - math.log(sigma_min)) + math.log(sigma_min))
|
| 36 |
+
return append_zero(sigmas)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def get_sigmas_vp(n, beta_d=19.9, beta_min=0.1, eps_s=1e-3, device='cpu'):
|
| 40 |
+
"""Constructs a continuous VP noise schedule."""
|
| 41 |
+
t = torch.linspace(1, eps_s, n, device=device)
|
| 42 |
+
sigmas = torch.sqrt(torch.exp(beta_d * t ** 2 / 2 + beta_min * t) - 1)
|
| 43 |
+
return append_zero(sigmas)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def to_d(x, sigma, denoised):
|
| 47 |
+
"""Converts a denoiser output to a Karras ODE derivative."""
|
| 48 |
+
return (x - denoised) / utils.append_dims(sigma, x.ndim)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def get_ancestral_step(sigma_from, sigma_to, eta=1.):
|
| 52 |
+
"""Calculates the noise level (sigma_down) to step down to and the amount
|
| 53 |
+
of noise to add (sigma_up) when doing an ancestral sampling step."""
|
| 54 |
+
if not eta:
|
| 55 |
+
return sigma_to, 0.
|
| 56 |
+
sigma_up = min(sigma_to, eta * (sigma_to ** 2 * (sigma_from ** 2 - sigma_to ** 2) / sigma_from ** 2) ** 0.5)
|
| 57 |
+
sigma_down = (sigma_to ** 2 - sigma_up ** 2) ** 0.5
|
| 58 |
+
return sigma_down, sigma_up
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def default_noise_sampler(x):
|
| 62 |
+
return lambda sigma, sigma_next: torch.randn_like(x)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class BatchedBrownianTree:
|
| 66 |
+
"""A wrapper around torchsde.BrownianTree that enables batches of entropy."""
|
| 67 |
+
|
| 68 |
+
def __init__(self, x, t0, t1, seed=None, **kwargs):
|
| 69 |
+
t0, t1, self.sign = self.sort(t0, t1)
|
| 70 |
+
w0 = kwargs.get('w0', torch.zeros_like(x))
|
| 71 |
+
if seed is None:
|
| 72 |
+
seed = torch.randint(0, 2 ** 63 - 1, []).item()
|
| 73 |
+
self.batched = True
|
| 74 |
+
try:
|
| 75 |
+
assert len(seed) == x.shape[0]
|
| 76 |
+
w0 = w0[0]
|
| 77 |
+
except TypeError:
|
| 78 |
+
seed = [seed]
|
| 79 |
+
self.batched = False
|
| 80 |
+
self.trees = [torchsde.BrownianTree(t0, w0, t1, entropy=s, **kwargs) for s in seed]
|
| 81 |
+
|
| 82 |
+
@staticmethod
|
| 83 |
+
def sort(a, b):
|
| 84 |
+
return (a, b, 1) if a < b else (b, a, -1)
|
| 85 |
+
|
| 86 |
+
def __call__(self, t0, t1):
|
| 87 |
+
t0, t1, sign = self.sort(t0, t1)
|
| 88 |
+
w = torch.stack([tree(t0, t1) for tree in self.trees]) * (self.sign * sign)
|
| 89 |
+
return w if self.batched else w[0]
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
class BrownianTreeNoiseSampler:
|
| 93 |
+
"""A noise sampler backed by a torchsde.BrownianTree.
|
| 94 |
+
|
| 95 |
+
Args:
|
| 96 |
+
x (Tensor): The tensor whose shape, device and dtype to use to generate
|
| 97 |
+
random samples.
|
| 98 |
+
sigma_min (float): The low end of the valid interval.
|
| 99 |
+
sigma_max (float): The high end of the valid interval.
|
| 100 |
+
seed (int or List[int]): The random seed. If a list of seeds is
|
| 101 |
+
supplied instead of a single integer, then the noise sampler will
|
| 102 |
+
use one BrownianTree per batch item, each with its own seed.
|
| 103 |
+
transform (callable): A function that maps sigma to the sampler's
|
| 104 |
+
internal timestep.
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
def __init__(self, x, sigma_min, sigma_max, seed=None, transform=lambda x: x):
|
| 108 |
+
self.transform = transform
|
| 109 |
+
t0, t1 = self.transform(torch.as_tensor(sigma_min)), self.transform(torch.as_tensor(sigma_max))
|
| 110 |
+
self.tree = BatchedBrownianTree(x, t0, t1, seed)
|
| 111 |
+
|
| 112 |
+
def __call__(self, sigma, sigma_next):
|
| 113 |
+
t0, t1 = self.transform(torch.as_tensor(sigma)), self.transform(torch.as_tensor(sigma_next))
|
| 114 |
+
return self.tree(t0, t1) / (t1 - t0).abs().sqrt()
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
@torch.no_grad()
|
| 118 |
+
def sample_euler(model, x, sigmas, extra_args=None, callback=None, disable=None, s_churn=0., s_tmin=0., s_tmax=float('inf'), s_noise=1.):
|
| 119 |
+
"""Implements Algorithm 2 (Euler steps) from Karras et al. (2022)."""
|
| 120 |
+
extra_args = {} if extra_args is None else extra_args
|
| 121 |
+
s_in = x.new_ones([x.shape[0]])
|
| 122 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 123 |
+
gamma = min(s_churn / (len(sigmas) - 1), 2 ** 0.5 - 1) if s_tmin <= sigmas[i] <= s_tmax else 0.
|
| 124 |
+
eps = torch.randn_like(x) * s_noise
|
| 125 |
+
sigma_hat = sigmas[i] * (gamma + 1)
|
| 126 |
+
if gamma > 0:
|
| 127 |
+
x = x + eps * (sigma_hat ** 2 - sigmas[i] ** 2) ** 0.5
|
| 128 |
+
denoised = model(x, sigma_hat * s_in, **extra_args)
|
| 129 |
+
d = to_d(x, sigma_hat, denoised)
|
| 130 |
+
if callback is not None:
|
| 131 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigma_hat, 'denoised': denoised})
|
| 132 |
+
dt = sigmas[i + 1] - sigma_hat
|
| 133 |
+
# Euler method
|
| 134 |
+
x = x + d * dt
|
| 135 |
+
return x
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
@torch.no_grad()
|
| 139 |
+
def sample_euler_ancestral(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None):
|
| 140 |
+
"""Ancestral sampling with Euler method steps."""
|
| 141 |
+
extra_args = {} if extra_args is None else extra_args
|
| 142 |
+
noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler
|
| 143 |
+
s_in = x.new_ones([x.shape[0]])
|
| 144 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 145 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 146 |
+
sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1], eta=eta)
|
| 147 |
+
if callback is not None:
|
| 148 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 149 |
+
d = to_d(x, sigmas[i], denoised)
|
| 150 |
+
# Euler method
|
| 151 |
+
dt = sigma_down - sigmas[i]
|
| 152 |
+
x = x + d * dt
|
| 153 |
+
if sigmas[i + 1] > 0:
|
| 154 |
+
x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * s_noise * sigma_up
|
| 155 |
+
return x
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
@torch.no_grad()
|
| 159 |
+
def sample_heun(model, x, sigmas, extra_args=None, callback=None, disable=None, s_churn=0., s_tmin=0., s_tmax=float('inf'), s_noise=1.):
|
| 160 |
+
"""Implements Algorithm 2 (Heun steps) from Karras et al. (2022)."""
|
| 161 |
+
extra_args = {} if extra_args is None else extra_args
|
| 162 |
+
s_in = x.new_ones([x.shape[0]])
|
| 163 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 164 |
+
gamma = min(s_churn / (len(sigmas) - 1), 2 ** 0.5 - 1) if s_tmin <= sigmas[i] <= s_tmax else 0.
|
| 165 |
+
eps = torch.randn_like(x) * s_noise
|
| 166 |
+
sigma_hat = sigmas[i] * (gamma + 1)
|
| 167 |
+
if gamma > 0:
|
| 168 |
+
x = x + eps * (sigma_hat ** 2 - sigmas[i] ** 2) ** 0.5
|
| 169 |
+
denoised = model(x, sigma_hat * s_in, **extra_args)
|
| 170 |
+
d = to_d(x, sigma_hat, denoised)
|
| 171 |
+
if callback is not None:
|
| 172 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigma_hat, 'denoised': denoised})
|
| 173 |
+
dt = sigmas[i + 1] - sigma_hat
|
| 174 |
+
if sigmas[i + 1] == 0:
|
| 175 |
+
# Euler method
|
| 176 |
+
x = x + d * dt
|
| 177 |
+
else:
|
| 178 |
+
# Heun's method
|
| 179 |
+
x_2 = x + d * dt
|
| 180 |
+
denoised_2 = model(x_2, sigmas[i + 1] * s_in, **extra_args)
|
| 181 |
+
d_2 = to_d(x_2, sigmas[i + 1], denoised_2)
|
| 182 |
+
d_prime = (d + d_2) / 2
|
| 183 |
+
x = x + d_prime * dt
|
| 184 |
+
return x
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
@torch.no_grad()
|
| 188 |
+
def sample_dpm_2(model, x, sigmas, extra_args=None, callback=None, disable=None, s_churn=0., s_tmin=0., s_tmax=float('inf'), s_noise=1.):
|
| 189 |
+
"""A sampler inspired by DPM-Solver-2 and Algorithm 2 from Karras et al. (2022)."""
|
| 190 |
+
extra_args = {} if extra_args is None else extra_args
|
| 191 |
+
s_in = x.new_ones([x.shape[0]])
|
| 192 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 193 |
+
gamma = min(s_churn / (len(sigmas) - 1), 2 ** 0.5 - 1) if s_tmin <= sigmas[i] <= s_tmax else 0.
|
| 194 |
+
eps = torch.randn_like(x) * s_noise
|
| 195 |
+
sigma_hat = sigmas[i] * (gamma + 1)
|
| 196 |
+
if gamma > 0:
|
| 197 |
+
x = x + eps * (sigma_hat ** 2 - sigmas[i] ** 2) ** 0.5
|
| 198 |
+
denoised = model(x, sigma_hat * s_in, **extra_args)
|
| 199 |
+
d = to_d(x, sigma_hat, denoised)
|
| 200 |
+
if callback is not None:
|
| 201 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigma_hat, 'denoised': denoised})
|
| 202 |
+
if sigmas[i + 1] == 0:
|
| 203 |
+
# Euler method
|
| 204 |
+
dt = sigmas[i + 1] - sigma_hat
|
| 205 |
+
x = x + d * dt
|
| 206 |
+
else:
|
| 207 |
+
# DPM-Solver-2
|
| 208 |
+
sigma_mid = sigma_hat.log().lerp(sigmas[i + 1].log(), 0.5).exp()
|
| 209 |
+
dt_1 = sigma_mid - sigma_hat
|
| 210 |
+
dt_2 = sigmas[i + 1] - sigma_hat
|
| 211 |
+
x_2 = x + d * dt_1
|
| 212 |
+
denoised_2 = model(x_2, sigma_mid * s_in, **extra_args)
|
| 213 |
+
d_2 = to_d(x_2, sigma_mid, denoised_2)
|
| 214 |
+
x = x + d_2 * dt_2
|
| 215 |
+
return x
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
@torch.no_grad()
|
| 219 |
+
def sample_dpm_2_ancestral(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None):
|
| 220 |
+
"""Ancestral sampling with DPM-Solver second-order steps."""
|
| 221 |
+
extra_args = {} if extra_args is None else extra_args
|
| 222 |
+
noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler
|
| 223 |
+
s_in = x.new_ones([x.shape[0]])
|
| 224 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 225 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 226 |
+
sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1], eta=eta)
|
| 227 |
+
if callback is not None:
|
| 228 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 229 |
+
d = to_d(x, sigmas[i], denoised)
|
| 230 |
+
if sigma_down == 0:
|
| 231 |
+
# Euler method
|
| 232 |
+
dt = sigma_down - sigmas[i]
|
| 233 |
+
x = x + d * dt
|
| 234 |
+
else:
|
| 235 |
+
# DPM-Solver-2
|
| 236 |
+
sigma_mid = sigmas[i].log().lerp(sigma_down.log(), 0.5).exp()
|
| 237 |
+
dt_1 = sigma_mid - sigmas[i]
|
| 238 |
+
dt_2 = sigma_down - sigmas[i]
|
| 239 |
+
x_2 = x + d * dt_1
|
| 240 |
+
denoised_2 = model(x_2, sigma_mid * s_in, **extra_args)
|
| 241 |
+
d_2 = to_d(x_2, sigma_mid, denoised_2)
|
| 242 |
+
x = x + d_2 * dt_2
|
| 243 |
+
x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * s_noise * sigma_up
|
| 244 |
+
return x
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def linear_multistep_coeff(order, t, i, j):
|
| 248 |
+
if order - 1 > i:
|
| 249 |
+
raise ValueError(f'Order {order} too high for step {i}')
|
| 250 |
+
def fn(tau):
|
| 251 |
+
prod = 1.
|
| 252 |
+
for k in range(order):
|
| 253 |
+
if j == k:
|
| 254 |
+
continue
|
| 255 |
+
prod *= (tau - t[i - k]) / (t[i - j] - t[i - k])
|
| 256 |
+
return prod
|
| 257 |
+
return integrate.quad(fn, t[i], t[i + 1], epsrel=1e-4)[0]
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
@torch.no_grad()
|
| 261 |
+
def sample_lms(model, x, sigmas, extra_args=None, callback=None, disable=None, order=4):
|
| 262 |
+
extra_args = {} if extra_args is None else extra_args
|
| 263 |
+
s_in = x.new_ones([x.shape[0]])
|
| 264 |
+
sigmas_cpu = sigmas.detach().cpu().numpy()
|
| 265 |
+
ds = []
|
| 266 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 267 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 268 |
+
d = to_d(x, sigmas[i], denoised)
|
| 269 |
+
ds.append(d)
|
| 270 |
+
if len(ds) > order:
|
| 271 |
+
ds.pop(0)
|
| 272 |
+
if callback is not None:
|
| 273 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 274 |
+
cur_order = min(i + 1, order)
|
| 275 |
+
coeffs = [linear_multistep_coeff(cur_order, sigmas_cpu, i, j) for j in range(cur_order)]
|
| 276 |
+
x = x + sum(coeff * d for coeff, d in zip(coeffs, reversed(ds)))
|
| 277 |
+
return x
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
@torch.no_grad()
|
| 281 |
+
def log_likelihood(model, x, sigma_min, sigma_max, extra_args=None, atol=1e-4, rtol=1e-4):
|
| 282 |
+
extra_args = {} if extra_args is None else extra_args
|
| 283 |
+
s_in = x.new_ones([x.shape[0]])
|
| 284 |
+
v = torch.randint_like(x, 2) * 2 - 1
|
| 285 |
+
fevals = 0
|
| 286 |
+
def ode_fn(sigma, x):
|
| 287 |
+
nonlocal fevals
|
| 288 |
+
with torch.enable_grad():
|
| 289 |
+
x = x[0].detach().requires_grad_()
|
| 290 |
+
denoised = model(x, sigma * s_in, **extra_args)
|
| 291 |
+
d = to_d(x, sigma, denoised)
|
| 292 |
+
fevals += 1
|
| 293 |
+
grad = torch.autograd.grad((d * v).sum(), x)[0]
|
| 294 |
+
d_ll = (v * grad).flatten(1).sum(1)
|
| 295 |
+
return d.detach(), d_ll
|
| 296 |
+
x_min = x, x.new_zeros([x.shape[0]])
|
| 297 |
+
t = x.new_tensor([sigma_min, sigma_max])
|
| 298 |
+
sol = odeint(ode_fn, x_min, t, atol=atol, rtol=rtol, method='dopri5')
|
| 299 |
+
latent, delta_ll = sol[0][-1], sol[1][-1]
|
| 300 |
+
ll_prior = torch.distributions.Normal(0, sigma_max).log_prob(latent).flatten(1).sum(1)
|
| 301 |
+
return ll_prior + delta_ll, {'fevals': fevals}
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
class PIDStepSizeController:
|
| 305 |
+
"""A PID controller for ODE adaptive step size control."""
|
| 306 |
+
def __init__(self, h, pcoeff, icoeff, dcoeff, order=1, accept_safety=0.81, eps=1e-8):
|
| 307 |
+
self.h = h
|
| 308 |
+
self.b1 = (pcoeff + icoeff + dcoeff) / order
|
| 309 |
+
self.b2 = -(pcoeff + 2 * dcoeff) / order
|
| 310 |
+
self.b3 = dcoeff / order
|
| 311 |
+
self.accept_safety = accept_safety
|
| 312 |
+
self.eps = eps
|
| 313 |
+
self.errs = []
|
| 314 |
+
|
| 315 |
+
def limiter(self, x):
|
| 316 |
+
return 1 + math.atan(x - 1)
|
| 317 |
+
|
| 318 |
+
def propose_step(self, error):
|
| 319 |
+
inv_error = 1 / (float(error) + self.eps)
|
| 320 |
+
if not self.errs:
|
| 321 |
+
self.errs = [inv_error, inv_error, inv_error]
|
| 322 |
+
self.errs[0] = inv_error
|
| 323 |
+
factor = self.errs[0] ** self.b1 * self.errs[1] ** self.b2 * self.errs[2] ** self.b3
|
| 324 |
+
factor = self.limiter(factor)
|
| 325 |
+
accept = factor >= self.accept_safety
|
| 326 |
+
if accept:
|
| 327 |
+
self.errs[2] = self.errs[1]
|
| 328 |
+
self.errs[1] = self.errs[0]
|
| 329 |
+
self.h *= factor
|
| 330 |
+
return accept
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
class DPMSolver(nn.Module):
|
| 334 |
+
"""DPM-Solver. See https://arxiv.org/abs/2206.00927."""
|
| 335 |
+
|
| 336 |
+
def __init__(self, model, extra_args=None, eps_callback=None, info_callback=None):
|
| 337 |
+
super().__init__()
|
| 338 |
+
self.model = model
|
| 339 |
+
self.extra_args = {} if extra_args is None else extra_args
|
| 340 |
+
self.eps_callback = eps_callback
|
| 341 |
+
self.info_callback = info_callback
|
| 342 |
+
|
| 343 |
+
def t(self, sigma):
|
| 344 |
+
return -sigma.log()
|
| 345 |
+
|
| 346 |
+
def sigma(self, t):
|
| 347 |
+
return t.neg().exp()
|
| 348 |
+
|
| 349 |
+
def eps(self, eps_cache, key, x, t, *args, **kwargs):
|
| 350 |
+
if key in eps_cache:
|
| 351 |
+
return eps_cache[key], eps_cache
|
| 352 |
+
sigma = self.sigma(t) * x.new_ones([x.shape[0]])
|
| 353 |
+
eps = (x - self.model(x, sigma, *args, **self.extra_args, **kwargs)) / self.sigma(t)
|
| 354 |
+
if self.eps_callback is not None:
|
| 355 |
+
self.eps_callback()
|
| 356 |
+
return eps, {key: eps, **eps_cache}
|
| 357 |
+
|
| 358 |
+
def dpm_solver_1_step(self, x, t, t_next, eps_cache=None):
|
| 359 |
+
eps_cache = {} if eps_cache is None else eps_cache
|
| 360 |
+
h = t_next - t
|
| 361 |
+
eps, eps_cache = self.eps(eps_cache, 'eps', x, t)
|
| 362 |
+
x_1 = x - self.sigma(t_next) * h.expm1() * eps
|
| 363 |
+
return x_1, eps_cache
|
| 364 |
+
|
| 365 |
+
def dpm_solver_2_step(self, x, t, t_next, r1=1 / 2, eps_cache=None):
|
| 366 |
+
eps_cache = {} if eps_cache is None else eps_cache
|
| 367 |
+
h = t_next - t
|
| 368 |
+
eps, eps_cache = self.eps(eps_cache, 'eps', x, t)
|
| 369 |
+
s1 = t + r1 * h
|
| 370 |
+
u1 = x - self.sigma(s1) * (r1 * h).expm1() * eps
|
| 371 |
+
eps_r1, eps_cache = self.eps(eps_cache, 'eps_r1', u1, s1)
|
| 372 |
+
x_2 = x - self.sigma(t_next) * h.expm1() * eps - self.sigma(t_next) / (2 * r1) * h.expm1() * (eps_r1 - eps)
|
| 373 |
+
return x_2, eps_cache
|
| 374 |
+
|
| 375 |
+
def dpm_solver_3_step(self, x, t, t_next, r1=1 / 3, r2=2 / 3, eps_cache=None):
|
| 376 |
+
eps_cache = {} if eps_cache is None else eps_cache
|
| 377 |
+
h = t_next - t
|
| 378 |
+
eps, eps_cache = self.eps(eps_cache, 'eps', x, t)
|
| 379 |
+
s1 = t + r1 * h
|
| 380 |
+
s2 = t + r2 * h
|
| 381 |
+
u1 = x - self.sigma(s1) * (r1 * h).expm1() * eps
|
| 382 |
+
eps_r1, eps_cache = self.eps(eps_cache, 'eps_r1', u1, s1)
|
| 383 |
+
u2 = x - self.sigma(s2) * (r2 * h).expm1() * eps - self.sigma(s2) * (r2 / r1) * ((r2 * h).expm1() / (r2 * h) - 1) * (eps_r1 - eps)
|
| 384 |
+
eps_r2, eps_cache = self.eps(eps_cache, 'eps_r2', u2, s2)
|
| 385 |
+
x_3 = x - self.sigma(t_next) * h.expm1() * eps - self.sigma(t_next) / r2 * (h.expm1() / h - 1) * (eps_r2 - eps)
|
| 386 |
+
return x_3, eps_cache
|
| 387 |
+
|
| 388 |
+
def dpm_solver_fast(self, x, t_start, t_end, nfe, eta=0., s_noise=1., noise_sampler=None):
|
| 389 |
+
noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler
|
| 390 |
+
if not t_end > t_start and eta:
|
| 391 |
+
raise ValueError('eta must be 0 for reverse sampling')
|
| 392 |
+
|
| 393 |
+
m = math.floor(nfe / 3) + 1
|
| 394 |
+
ts = torch.linspace(t_start, t_end, m + 1, device=x.device)
|
| 395 |
+
|
| 396 |
+
if nfe % 3 == 0:
|
| 397 |
+
orders = [3] * (m - 2) + [2, 1]
|
| 398 |
+
else:
|
| 399 |
+
orders = [3] * (m - 1) + [nfe % 3]
|
| 400 |
+
|
| 401 |
+
for i in range(len(orders)):
|
| 402 |
+
eps_cache = {}
|
| 403 |
+
t, t_next = ts[i], ts[i + 1]
|
| 404 |
+
if eta:
|
| 405 |
+
sd, su = get_ancestral_step(self.sigma(t), self.sigma(t_next), eta)
|
| 406 |
+
t_next_ = torch.minimum(t_end, self.t(sd))
|
| 407 |
+
su = (self.sigma(t_next) ** 2 - self.sigma(t_next_) ** 2) ** 0.5
|
| 408 |
+
else:
|
| 409 |
+
t_next_, su = t_next, 0.
|
| 410 |
+
|
| 411 |
+
eps, eps_cache = self.eps(eps_cache, 'eps', x, t)
|
| 412 |
+
denoised = x - self.sigma(t) * eps
|
| 413 |
+
if self.info_callback is not None:
|
| 414 |
+
self.info_callback({'x': x, 'i': i, 't': ts[i], 't_up': t, 'denoised': denoised})
|
| 415 |
+
|
| 416 |
+
if orders[i] == 1:
|
| 417 |
+
x, eps_cache = self.dpm_solver_1_step(x, t, t_next_, eps_cache=eps_cache)
|
| 418 |
+
elif orders[i] == 2:
|
| 419 |
+
x, eps_cache = self.dpm_solver_2_step(x, t, t_next_, eps_cache=eps_cache)
|
| 420 |
+
else:
|
| 421 |
+
x, eps_cache = self.dpm_solver_3_step(x, t, t_next_, eps_cache=eps_cache)
|
| 422 |
+
|
| 423 |
+
x = x + su * s_noise * noise_sampler(self.sigma(t), self.sigma(t_next))
|
| 424 |
+
|
| 425 |
+
return x
|
| 426 |
+
|
| 427 |
+
def dpm_solver_adaptive(self, x, t_start, t_end, order=3, rtol=0.05, atol=0.0078, h_init=0.05, pcoeff=0., icoeff=1., dcoeff=0., accept_safety=0.81, eta=0., s_noise=1., noise_sampler=None):
|
| 428 |
+
noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler
|
| 429 |
+
if order not in {2, 3}:
|
| 430 |
+
raise ValueError('order should be 2 or 3')
|
| 431 |
+
forward = t_end > t_start
|
| 432 |
+
if not forward and eta:
|
| 433 |
+
raise ValueError('eta must be 0 for reverse sampling')
|
| 434 |
+
h_init = abs(h_init) * (1 if forward else -1)
|
| 435 |
+
atol = torch.tensor(atol)
|
| 436 |
+
rtol = torch.tensor(rtol)
|
| 437 |
+
s = t_start
|
| 438 |
+
x_prev = x
|
| 439 |
+
accept = True
|
| 440 |
+
pid = PIDStepSizeController(h_init, pcoeff, icoeff, dcoeff, 1.5 if eta else order, accept_safety)
|
| 441 |
+
info = {'steps': 0, 'nfe': 0, 'n_accept': 0, 'n_reject': 0}
|
| 442 |
+
|
| 443 |
+
while s < t_end - 1e-5 if forward else s > t_end + 1e-5:
|
| 444 |
+
eps_cache = {}
|
| 445 |
+
t = torch.minimum(t_end, s + pid.h) if forward else torch.maximum(t_end, s + pid.h)
|
| 446 |
+
if eta:
|
| 447 |
+
sd, su = get_ancestral_step(self.sigma(s), self.sigma(t), eta)
|
| 448 |
+
t_ = torch.minimum(t_end, self.t(sd))
|
| 449 |
+
su = (self.sigma(t) ** 2 - self.sigma(t_) ** 2) ** 0.5
|
| 450 |
+
else:
|
| 451 |
+
t_, su = t, 0.
|
| 452 |
+
|
| 453 |
+
eps, eps_cache = self.eps(eps_cache, 'eps', x, s)
|
| 454 |
+
denoised = x - self.sigma(s) * eps
|
| 455 |
+
|
| 456 |
+
if order == 2:
|
| 457 |
+
x_low, eps_cache = self.dpm_solver_1_step(x, s, t_, eps_cache=eps_cache)
|
| 458 |
+
x_high, eps_cache = self.dpm_solver_2_step(x, s, t_, eps_cache=eps_cache)
|
| 459 |
+
else:
|
| 460 |
+
x_low, eps_cache = self.dpm_solver_2_step(x, s, t_, r1=1 / 3, eps_cache=eps_cache)
|
| 461 |
+
x_high, eps_cache = self.dpm_solver_3_step(x, s, t_, eps_cache=eps_cache)
|
| 462 |
+
delta = torch.maximum(atol, rtol * torch.maximum(x_low.abs(), x_prev.abs()))
|
| 463 |
+
error = torch.linalg.norm((x_low - x_high) / delta) / x.numel() ** 0.5
|
| 464 |
+
accept = pid.propose_step(error)
|
| 465 |
+
if accept:
|
| 466 |
+
x_prev = x_low
|
| 467 |
+
x = x_high + su * s_noise * noise_sampler(self.sigma(s), self.sigma(t))
|
| 468 |
+
s = t
|
| 469 |
+
info['n_accept'] += 1
|
| 470 |
+
else:
|
| 471 |
+
info['n_reject'] += 1
|
| 472 |
+
info['nfe'] += order
|
| 473 |
+
info['steps'] += 1
|
| 474 |
+
|
| 475 |
+
if self.info_callback is not None:
|
| 476 |
+
self.info_callback({'x': x, 'i': info['steps'] - 1, 't': s, 't_up': s, 'denoised': denoised, 'error': error, 'h': pid.h, **info})
|
| 477 |
+
|
| 478 |
+
return x, info
|
| 479 |
+
|
| 480 |
+
|
| 481 |
+
@torch.no_grad()
|
| 482 |
+
def sample_dpm_fast(model, x, sigma_min, sigma_max, n, extra_args=None, callback=None, disable=None, eta=0., s_noise=1., noise_sampler=None):
|
| 483 |
+
"""DPM-Solver-Fast (fixed step size). See https://arxiv.org/abs/2206.00927."""
|
| 484 |
+
if sigma_min <= 0 or sigma_max <= 0:
|
| 485 |
+
raise ValueError('sigma_min and sigma_max must not be 0')
|
| 486 |
+
with tqdm(total=n, disable=disable) as pbar:
|
| 487 |
+
dpm_solver = DPMSolver(model, extra_args, eps_callback=pbar.update)
|
| 488 |
+
if callback is not None:
|
| 489 |
+
dpm_solver.info_callback = lambda info: callback({'sigma': dpm_solver.sigma(info['t']), 'sigma_hat': dpm_solver.sigma(info['t_up']), **info})
|
| 490 |
+
return dpm_solver.dpm_solver_fast(x, dpm_solver.t(torch.tensor(sigma_max)), dpm_solver.t(torch.tensor(sigma_min)), n, eta, s_noise, noise_sampler)
|
| 491 |
+
|
| 492 |
+
|
| 493 |
+
@torch.no_grad()
|
| 494 |
+
def sample_dpm_adaptive(model, x, sigma_min, sigma_max, extra_args=None, callback=None, disable=None, order=3, rtol=0.05, atol=0.0078, h_init=0.05, pcoeff=0., icoeff=1., dcoeff=0., accept_safety=0.81, eta=0., s_noise=1., noise_sampler=None, return_info=False):
|
| 495 |
+
"""DPM-Solver-12 and 23 (adaptive step size). See https://arxiv.org/abs/2206.00927."""
|
| 496 |
+
if sigma_min <= 0 or sigma_max <= 0:
|
| 497 |
+
raise ValueError('sigma_min and sigma_max must not be 0')
|
| 498 |
+
with tqdm(disable=disable) as pbar:
|
| 499 |
+
dpm_solver = DPMSolver(model, extra_args, eps_callback=pbar.update)
|
| 500 |
+
if callback is not None:
|
| 501 |
+
dpm_solver.info_callback = lambda info: callback({'sigma': dpm_solver.sigma(info['t']), 'sigma_hat': dpm_solver.sigma(info['t_up']), **info})
|
| 502 |
+
x, info = dpm_solver.dpm_solver_adaptive(x, dpm_solver.t(torch.tensor(sigma_max)), dpm_solver.t(torch.tensor(sigma_min)), order, rtol, atol, h_init, pcoeff, icoeff, dcoeff, accept_safety, eta, s_noise, noise_sampler)
|
| 503 |
+
if return_info:
|
| 504 |
+
return x, info
|
| 505 |
+
return x
|
| 506 |
+
|
| 507 |
+
|
| 508 |
+
@torch.no_grad()
|
| 509 |
+
def sample_dpmpp_2s_ancestral(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None):
|
| 510 |
+
"""Ancestral sampling with DPM-Solver++(2S) second-order steps."""
|
| 511 |
+
extra_args = {} if extra_args is None else extra_args
|
| 512 |
+
noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler
|
| 513 |
+
s_in = x.new_ones([x.shape[0]])
|
| 514 |
+
sigma_fn = lambda t: t.neg().exp()
|
| 515 |
+
t_fn = lambda sigma: sigma.log().neg()
|
| 516 |
+
|
| 517 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 518 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 519 |
+
sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1], eta=eta)
|
| 520 |
+
if callback is not None:
|
| 521 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 522 |
+
if sigma_down == 0:
|
| 523 |
+
# Euler method
|
| 524 |
+
d = to_d(x, sigmas[i], denoised)
|
| 525 |
+
dt = sigma_down - sigmas[i]
|
| 526 |
+
x = x + d * dt
|
| 527 |
+
else:
|
| 528 |
+
# DPM-Solver++(2S)
|
| 529 |
+
t, t_next = t_fn(sigmas[i]), t_fn(sigma_down)
|
| 530 |
+
r = 1 / 2
|
| 531 |
+
h = t_next - t
|
| 532 |
+
s = t + r * h
|
| 533 |
+
x_2 = (sigma_fn(s) / sigma_fn(t)) * x - (-h * r).expm1() * denoised
|
| 534 |
+
denoised_2 = model(x_2, sigma_fn(s) * s_in, **extra_args)
|
| 535 |
+
x = (sigma_fn(t_next) / sigma_fn(t)) * x - (-h).expm1() * denoised_2
|
| 536 |
+
# Noise addition
|
| 537 |
+
if sigmas[i + 1] > 0:
|
| 538 |
+
x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * s_noise * sigma_up
|
| 539 |
+
return x
|
| 540 |
+
|
| 541 |
+
|
| 542 |
+
@torch.no_grad()
|
| 543 |
+
def sample_dpmpp_sde(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None, r=1 / 2):
|
| 544 |
+
"""DPM-Solver++ (stochastic)."""
|
| 545 |
+
sigma_min, sigma_max = sigmas[sigmas > 0].min(), sigmas.max()
|
| 546 |
+
noise_sampler = BrownianTreeNoiseSampler(x, sigma_min, sigma_max) if noise_sampler is None else noise_sampler
|
| 547 |
+
extra_args = {} if extra_args is None else extra_args
|
| 548 |
+
s_in = x.new_ones([x.shape[0]])
|
| 549 |
+
sigma_fn = lambda t: t.neg().exp()
|
| 550 |
+
t_fn = lambda sigma: sigma.log().neg()
|
| 551 |
+
|
| 552 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 553 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 554 |
+
if callback is not None:
|
| 555 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 556 |
+
if sigmas[i + 1] == 0:
|
| 557 |
+
# Euler method
|
| 558 |
+
d = to_d(x, sigmas[i], denoised)
|
| 559 |
+
dt = sigmas[i + 1] - sigmas[i]
|
| 560 |
+
x = x + d * dt
|
| 561 |
+
else:
|
| 562 |
+
# DPM-Solver++
|
| 563 |
+
t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i + 1])
|
| 564 |
+
h = t_next - t
|
| 565 |
+
s = t + h * r
|
| 566 |
+
fac = 1 / (2 * r)
|
| 567 |
+
|
| 568 |
+
# Step 1
|
| 569 |
+
sd, su = get_ancestral_step(sigma_fn(t), sigma_fn(s), eta)
|
| 570 |
+
s_ = t_fn(sd)
|
| 571 |
+
x_2 = (sigma_fn(s_) / sigma_fn(t)) * x - (t - s_).expm1() * denoised
|
| 572 |
+
x_2 = x_2 + noise_sampler(sigma_fn(t), sigma_fn(s)) * s_noise * su
|
| 573 |
+
denoised_2 = model(x_2, sigma_fn(s) * s_in, **extra_args)
|
| 574 |
+
|
| 575 |
+
# Step 2
|
| 576 |
+
sd, su = get_ancestral_step(sigma_fn(t), sigma_fn(t_next), eta)
|
| 577 |
+
t_next_ = t_fn(sd)
|
| 578 |
+
denoised_d = (1 - fac) * denoised + fac * denoised_2
|
| 579 |
+
x = (sigma_fn(t_next_) / sigma_fn(t)) * x - (t - t_next_).expm1() * denoised_d
|
| 580 |
+
x = x + noise_sampler(sigma_fn(t), sigma_fn(t_next)) * s_noise * su
|
| 581 |
+
return x
|
| 582 |
+
|
| 583 |
+
|
| 584 |
+
@torch.no_grad()
|
| 585 |
+
def sample_dpmpp_2m(model, x, sigmas, extra_args=None, callback=None, disable=None):
|
| 586 |
+
"""DPM-Solver++(2M)."""
|
| 587 |
+
extra_args = {} if extra_args is None else extra_args
|
| 588 |
+
s_in = x.new_ones([x.shape[0]])
|
| 589 |
+
sigma_fn = lambda t: t.neg().exp()
|
| 590 |
+
t_fn = lambda sigma: sigma.log().neg()
|
| 591 |
+
old_denoised = None
|
| 592 |
+
|
| 593 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 594 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 595 |
+
if callback is not None:
|
| 596 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 597 |
+
t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i + 1])
|
| 598 |
+
h = t_next - t
|
| 599 |
+
if old_denoised is None or sigmas[i + 1] == 0:
|
| 600 |
+
x = (sigma_fn(t_next) / sigma_fn(t)) * x - (-h).expm1() * denoised
|
| 601 |
+
else:
|
| 602 |
+
h_last = t - t_fn(sigmas[i - 1])
|
| 603 |
+
r = h_last / h
|
| 604 |
+
denoised_d = (1 + 1 / (2 * r)) * denoised - (1 / (2 * r)) * old_denoised
|
| 605 |
+
x = (sigma_fn(t_next) / sigma_fn(t)) * x - (-h).expm1() * denoised_d
|
| 606 |
+
old_denoised = denoised
|
| 607 |
+
return x
|
| 608 |
+
|
| 609 |
+
|
| 610 |
+
@torch.no_grad()
|
| 611 |
+
def sample_dpmpp_2m_sde(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None, solver_type='midpoint'):
|
| 612 |
+
"""DPM-Solver++(2M) SDE."""
|
| 613 |
+
|
| 614 |
+
if solver_type not in {'heun', 'midpoint'}:
|
| 615 |
+
raise ValueError('solver_type must be \'heun\' or \'midpoint\'')
|
| 616 |
+
|
| 617 |
+
sigma_min, sigma_max = sigmas[sigmas > 0].min(), sigmas.max()
|
| 618 |
+
noise_sampler = BrownianTreeNoiseSampler(x, sigma_min, sigma_max) if noise_sampler is None else noise_sampler
|
| 619 |
+
extra_args = {} if extra_args is None else extra_args
|
| 620 |
+
s_in = x.new_ones([x.shape[0]])
|
| 621 |
+
|
| 622 |
+
old_denoised = None
|
| 623 |
+
h_last = None
|
| 624 |
+
|
| 625 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 626 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 627 |
+
if callback is not None:
|
| 628 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 629 |
+
if sigmas[i + 1] == 0:
|
| 630 |
+
# Denoising step
|
| 631 |
+
x = denoised
|
| 632 |
+
else:
|
| 633 |
+
# DPM-Solver++(2M) SDE
|
| 634 |
+
t, s = -sigmas[i].log(), -sigmas[i + 1].log()
|
| 635 |
+
h = s - t
|
| 636 |
+
eta_h = eta * h
|
| 637 |
+
|
| 638 |
+
x = sigmas[i + 1] / sigmas[i] * (-eta_h).exp() * x + (-h - eta_h).expm1().neg() * denoised
|
| 639 |
+
|
| 640 |
+
if old_denoised is not None:
|
| 641 |
+
r = h_last / h
|
| 642 |
+
if solver_type == 'heun':
|
| 643 |
+
x = x + ((-h - eta_h).expm1().neg() / (-h - eta_h) + 1) * (1 / r) * (denoised - old_denoised)
|
| 644 |
+
elif solver_type == 'midpoint':
|
| 645 |
+
x = x + 0.5 * (-h - eta_h).expm1().neg() * (1 / r) * (denoised - old_denoised)
|
| 646 |
+
|
| 647 |
+
if eta:
|
| 648 |
+
x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * sigmas[i + 1] * (-2 * eta_h).expm1().neg().sqrt() * s_noise
|
| 649 |
+
|
| 650 |
+
old_denoised = denoised
|
| 651 |
+
h_last = h
|
| 652 |
+
return x
|
| 653 |
+
|
| 654 |
+
|
| 655 |
+
@torch.no_grad()
|
| 656 |
+
def sample_dpmpp_3m_sde(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None):
|
| 657 |
+
"""DPM-Solver++(3M) SDE."""
|
| 658 |
+
|
| 659 |
+
sigma_min, sigma_max = sigmas[sigmas > 0].min(), sigmas.max()
|
| 660 |
+
noise_sampler = BrownianTreeNoiseSampler(x, sigma_min, sigma_max) if noise_sampler is None else noise_sampler
|
| 661 |
+
extra_args = {} if extra_args is None else extra_args
|
| 662 |
+
s_in = x.new_ones([x.shape[0]])
|
| 663 |
+
|
| 664 |
+
denoised_1, denoised_2 = None, None
|
| 665 |
+
h_1, h_2 = None, None
|
| 666 |
+
|
| 667 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 668 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 669 |
+
if callback is not None:
|
| 670 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 671 |
+
if sigmas[i + 1] == 0:
|
| 672 |
+
# Denoising step
|
| 673 |
+
x = denoised
|
| 674 |
+
else:
|
| 675 |
+
t, s = -sigmas[i].log(), -sigmas[i + 1].log()
|
| 676 |
+
h = s - t
|
| 677 |
+
h_eta = h * (eta + 1)
|
| 678 |
+
|
| 679 |
+
x = torch.exp(-h_eta) * x + (-h_eta).expm1().neg() * denoised
|
| 680 |
+
|
| 681 |
+
if h_2 is not None:
|
| 682 |
+
r0 = h_1 / h
|
| 683 |
+
r1 = h_2 / h
|
| 684 |
+
d1_0 = (denoised - denoised_1) / r0
|
| 685 |
+
d1_1 = (denoised_1 - denoised_2) / r1
|
| 686 |
+
d1 = d1_0 + (d1_0 - d1_1) * r0 / (r0 + r1)
|
| 687 |
+
d2 = (d1_0 - d1_1) / (r0 + r1)
|
| 688 |
+
phi_2 = h_eta.neg().expm1() / h_eta + 1
|
| 689 |
+
phi_3 = phi_2 / h_eta - 0.5
|
| 690 |
+
x = x + phi_2 * d1 - phi_3 * d2
|
| 691 |
+
elif h_1 is not None:
|
| 692 |
+
r = h_1 / h
|
| 693 |
+
d = (denoised - denoised_1) / r
|
| 694 |
+
phi_2 = h_eta.neg().expm1() / h_eta + 1
|
| 695 |
+
x = x + phi_2 * d
|
| 696 |
+
|
| 697 |
+
if eta:
|
| 698 |
+
x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * sigmas[i + 1] * (-2 * h * eta).expm1().neg().sqrt() * s_noise
|
| 699 |
+
|
| 700 |
+
denoised_1, denoised_2 = denoised, denoised_1
|
| 701 |
+
h_1, h_2 = h, h_1
|
| 702 |
+
return x
|
| 703 |
+
|
| 704 |
+
@torch.no_grad()
|
| 705 |
+
def sample_dpmpp_2m_alt(model, x, sigmas, extra_args=None, callback=None, disable=None):
|
| 706 |
+
"""DPM-Solver++(2M)."""
|
| 707 |
+
extra_args = {} if extra_args is None else extra_args
|
| 708 |
+
s_in = x.new_ones([x.shape[0]])
|
| 709 |
+
sigma_fn = lambda t: t.neg().exp()
|
| 710 |
+
t_fn = lambda sigma: sigma.log().neg()
|
| 711 |
+
old_denoised = None
|
| 712 |
+
|
| 713 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 714 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 715 |
+
if callback is not None:
|
| 716 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 717 |
+
t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i + 1])
|
| 718 |
+
h = t_next - t
|
| 719 |
+
|
| 720 |
+
t_min = min(sigma_fn(t_next), sigma_fn(t))
|
| 721 |
+
t_max = max(sigma_fn(t_next), sigma_fn(t))
|
| 722 |
+
|
| 723 |
+
if old_denoised is None or sigmas[i + 1] == 0:
|
| 724 |
+
x = (t_min / t_max) * x - (-h).expm1() * denoised
|
| 725 |
+
else:
|
| 726 |
+
h_last = t - t_fn(sigmas[i - 1])
|
| 727 |
+
|
| 728 |
+
h_min = min(h_last, h)
|
| 729 |
+
h_max = max(h_last, h)
|
| 730 |
+
r = h_max / h_min
|
| 731 |
+
|
| 732 |
+
h_d = (h_max + h_min) / 2
|
| 733 |
+
denoised_d = (1 + 1 / (2 * r)) * denoised - (1 / (2 * r)) * old_denoised
|
| 734 |
+
x = (t_min / t_max) * x - (-h_d).expm1() * denoised_d
|
| 735 |
+
|
| 736 |
+
old_denoised = denoised
|
| 737 |
+
return x
|
| 738 |
+
|
| 739 |
+
@torch.no_grad()
|
| 740 |
+
def sample_dpmpp_2m_v1(model, x, sigmas, extra_args=None, callback=None, disable=None):
|
| 741 |
+
"""DPM-Solver++(2M)."""
|
| 742 |
+
extra_args = {} if extra_args is None else extra_args
|
| 743 |
+
s_in = x.new_ones([x.shape[0]])
|
| 744 |
+
sigma_fn = lambda t: t.neg().exp()
|
| 745 |
+
t_fn = lambda sigma: sigma.log().neg()
|
| 746 |
+
old_denoised = None
|
| 747 |
+
|
| 748 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 749 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 750 |
+
if callback is not None:
|
| 751 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 752 |
+
t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i + 1])
|
| 753 |
+
h = t_next - t
|
| 754 |
+
if old_denoised is None or sigmas[i + 1] == 0:
|
| 755 |
+
x = (sigma_fn(t_next) / sigma_fn(t)) * x - (-h).expm1() * denoised
|
| 756 |
+
else:
|
| 757 |
+
h_last = t - t_fn(sigmas[i - 1])
|
| 758 |
+
r = h_last / h
|
| 759 |
+
denoised_d = (1 + 1 / (2 * r)) * denoised - (1 / (2 * r)) * old_denoised
|
| 760 |
+
x = (sigma_fn(t_next) / sigma_fn(t)) * x - (-h).expm1() * denoised_d
|
| 761 |
+
sigma_progress = i / len(sigmas)
|
| 762 |
+
adjustment_factor = 1 + (0.15 * (sigma_progress * sigma_progress))
|
| 763 |
+
old_denoised = denoised * adjustment_factor
|
| 764 |
+
return x
|
| 765 |
+
|
| 766 |
+
@torch.no_grad()
|
| 767 |
+
def sample_dpmpp_2m_test(model, x, sigmas, extra_args=None, callback=None, disable=None):
|
| 768 |
+
"""DPM-Solver++(2M)."""
|
| 769 |
+
extra_args = {} if extra_args is None else extra_args
|
| 770 |
+
s_in = x.new_ones([x.shape[0]])
|
| 771 |
+
sigma_fn = lambda t: t.neg().exp()
|
| 772 |
+
t_fn = lambda sigma: sigma.log().neg()
|
| 773 |
+
old_denoised = None
|
| 774 |
+
|
| 775 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 776 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 777 |
+
if callback is not None:
|
| 778 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 779 |
+
t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i + 1])
|
| 780 |
+
h = t_next - t
|
| 781 |
+
|
| 782 |
+
t_min = min(sigma_fn(t_next), sigma_fn(t))
|
| 783 |
+
t_max = max(sigma_fn(t_next), sigma_fn(t))
|
| 784 |
+
|
| 785 |
+
if old_denoised is None or sigmas[i + 1] == 0:
|
| 786 |
+
x = (t_min / t_max) * x - (-h).expm1() * denoised
|
| 787 |
+
else:
|
| 788 |
+
h_last = t - t_fn(sigmas[i - 1])
|
| 789 |
+
|
| 790 |
+
h_min = min(h_last, h)
|
| 791 |
+
h_max = max(h_last, h)
|
| 792 |
+
r = h_max / h_min
|
| 793 |
+
|
| 794 |
+
h_d = (h_max + h_min) / 2
|
| 795 |
+
denoised_d = (1 + 1 / (2 * r)) * denoised - (1 / (2 * r)) * old_denoised
|
| 796 |
+
x = (t_min / t_max) * x - (-h_d).expm1() * denoised_d
|
| 797 |
+
|
| 798 |
+
old_denoised = denoised
|
| 799 |
+
return x
|
| 800 |
+
|
| 801 |
+
import torch
|
| 802 |
+
from torch import nn
|
| 803 |
+
import torchsde
|
| 804 |
+
from tqdm import trange, tqdm
|
| 805 |
+
from scipy import integrate
|
| 806 |
+
import math
|
| 807 |
+
from modules import shared
|
| 808 |
+
import k_diffusion.sampling
|
| 809 |
+
|
| 810 |
+
#Citing from K-Diffusion
|
| 811 |
+
#===================================================================================
|
| 812 |
+
def append_zero(x):
|
| 813 |
+
return torch.cat([x, x.new_zeros([1])])
|
| 814 |
+
|
| 815 |
+
def append_dims(x, target_dims):
|
| 816 |
+
"""Appends dimensions to the end of a tensor until it has target_dims dimensions."""
|
| 817 |
+
dims_to_append = target_dims - x.ndim
|
| 818 |
+
if dims_to_append < 0:
|
| 819 |
+
raise ValueError(f'input has {x.ndim} dims but target_dims is {target_dims}, which is less')
|
| 820 |
+
return x[(...,) + (None,) * dims_to_append]
|
| 821 |
+
|
| 822 |
+
def to_d(x, sigma, denoised):
|
| 823 |
+
"""Converts a denoiser output to a Karras ODE derivative."""
|
| 824 |
+
return (x - denoised) / append_dims(sigma, x.ndim)
|
| 825 |
+
|
| 826 |
+
def get_sigmas_karras(n, sigma_min, sigma_max, rho=7., device='cpu'):
|
| 827 |
+
"""Constructs the noise schedule of Karras et al. (2022)."""
|
| 828 |
+
ramp = torch.linspace(0, 1, n)
|
| 829 |
+
min_inv_rho = sigma_min ** (1 / rho)
|
| 830 |
+
max_inv_rho = sigma_max ** (1 / rho)
|
| 831 |
+
sigmas = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
|
| 832 |
+
return append_zero(sigmas).to(device)
|
| 833 |
+
|
| 834 |
+
|
| 835 |
+
def get_sigmas_exponential(n, sigma_min, sigma_max, device='cpu'):
|
| 836 |
+
"""Constructs an exponential noise schedule."""
|
| 837 |
+
sigmas = torch.linspace(math.log(sigma_max), math.log(sigma_min), n, device=device).exp()
|
| 838 |
+
return append_zero(sigmas)
|
| 839 |
+
|
| 840 |
+
|
| 841 |
+
def get_sigmas_polyexponential(n, sigma_min, sigma_max, rho=1., device='cpu'):
|
| 842 |
+
"""Constructs an polynomial in log sigma noise schedule."""
|
| 843 |
+
ramp = torch.linspace(1, 0, n, device=device) ** rho
|
| 844 |
+
sigmas = torch.exp(ramp * (math.log(sigma_max) - math.log(sigma_min)) + math.log(sigma_min))
|
| 845 |
+
return append_zero(sigmas)
|
| 846 |
+
|
| 847 |
+
|
| 848 |
+
def get_sigmas_vp(n, beta_d=19.9, beta_min=0.1, eps_s=1e-3, device='cpu'):
|
| 849 |
+
"""Constructs a continuous VP noise schedule."""
|
| 850 |
+
t = torch.linspace(1, eps_s, n, device=device)
|
| 851 |
+
sigmas = torch.sqrt(torch.exp(beta_d * t ** 2 / 2 + beta_min * t) - 1)
|
| 852 |
+
return append_zero(sigmas)
|
| 853 |
+
|
| 854 |
+
def get_ancestral_step(sigma_from, sigma_to, eta=1.):
|
| 855 |
+
"""Calculates the noise level (sigma_down) to step down to and the amount
|
| 856 |
+
of noise to add (sigma_up) when doing an ancestral sampling step."""
|
| 857 |
+
if not eta:
|
| 858 |
+
return sigma_to, 0.
|
| 859 |
+
sigma_up = min(sigma_to, eta * (sigma_to ** 2 * (sigma_from ** 2 - sigma_to ** 2) / sigma_from ** 2) ** 0.5)
|
| 860 |
+
sigma_down = (sigma_to ** 2 - sigma_up ** 2) ** 0.5
|
| 861 |
+
return sigma_down, sigma_up
|
| 862 |
+
|
| 863 |
+
def default_noise_sampler(x):
|
| 864 |
+
return lambda sigma, sigma_next: torch.randn_like(x)
|
| 865 |
+
|
| 866 |
+
def linear_multistep_coeff(order, t, i, j):
|
| 867 |
+
if order - 1 > i:
|
| 868 |
+
raise ValueError(f'Order {order} too high for step {i}')
|
| 869 |
+
def fn(tau):
|
| 870 |
+
prod = 1.
|
| 871 |
+
for k in range(order):
|
| 872 |
+
if j == k:
|
| 873 |
+
continue
|
| 874 |
+
prod *= (tau - t[i - k]) / (t[i - j] - t[i - k])
|
| 875 |
+
return prod
|
| 876 |
+
return integrate.quad(fn, t[i], t[i + 1], epsrel=1e-4)[0]
|
| 877 |
+
|
| 878 |
+
class DPMSolver(nn.Module):
|
| 879 |
+
"""DPM-Solver. See https://arxiv.org/abs/2206.00927."""
|
| 880 |
+
|
| 881 |
+
def __init__(self, model, extra_args=None, eps_callback=None, info_callback=None):
|
| 882 |
+
super().__init__()
|
| 883 |
+
self.model = model
|
| 884 |
+
self.extra_args = {} if extra_args is None else extra_args
|
| 885 |
+
self.eps_callback = eps_callback
|
| 886 |
+
self.info_callback = info_callback
|
| 887 |
+
|
| 888 |
+
def t(self, sigma):
|
| 889 |
+
return -sigma.log()
|
| 890 |
+
|
| 891 |
+
def sigma(self, t):
|
| 892 |
+
return t.neg().exp()
|
| 893 |
+
|
| 894 |
+
def eps(self, eps_cache, key, x, t, *args, **kwargs):
|
| 895 |
+
if key in eps_cache:
|
| 896 |
+
return eps_cache[key], eps_cache
|
| 897 |
+
sigma = self.sigma(t) * x.new_ones([x.shape[0]])
|
| 898 |
+
eps = (x - self.model(x, sigma, *args, **self.extra_args, **kwargs)) / self.sigma(t)
|
| 899 |
+
if self.eps_callback is not None:
|
| 900 |
+
self.eps_callback()
|
| 901 |
+
return eps, {key: eps, **eps_cache}
|
| 902 |
+
|
| 903 |
+
def dpm_solver_1_step(self, x, t, t_next, eps_cache=None):
|
| 904 |
+
eps_cache = {} if eps_cache is None else eps_cache
|
| 905 |
+
h = t_next - t
|
| 906 |
+
eps, eps_cache = self.eps(eps_cache, 'eps', x, t)
|
| 907 |
+
x_1 = x - self.sigma(t_next) * h.expm1() * eps
|
| 908 |
+
return x_1, eps_cache
|
| 909 |
+
|
| 910 |
+
def dpm_solver_2_step(self, x, t, t_next, r1=1 / 2, eps_cache=None):
|
| 911 |
+
eps_cache = {} if eps_cache is None else eps_cache
|
| 912 |
+
h = t_next - t
|
| 913 |
+
eps, eps_cache = self.eps(eps_cache, 'eps', x, t)
|
| 914 |
+
s1 = t + r1 * h
|
| 915 |
+
u1 = x - self.sigma(s1) * (r1 * h).expm1() * eps
|
| 916 |
+
eps_r1, eps_cache = self.eps(eps_cache, 'eps_r1', u1, s1)
|
| 917 |
+
x_2 = x - self.sigma(t_next) * h.expm1() * eps - self.sigma(t_next) / (2 * r1) * h.expm1() * (eps_r1 - eps)
|
| 918 |
+
return x_2, eps_cache
|
| 919 |
+
|
| 920 |
+
def dpm_solver_3_step(self, x, t, t_next, r1=1 / 3, r2=2 / 3, eps_cache=None):
|
| 921 |
+
eps_cache = {} if eps_cache is None else eps_cache
|
| 922 |
+
h = t_next - t
|
| 923 |
+
eps, eps_cache = self.eps(eps_cache, 'eps', x, t)
|
| 924 |
+
s1 = t + r1 * h
|
| 925 |
+
s2 = t + r2 * h
|
| 926 |
+
u1 = x - self.sigma(s1) * (r1 * h).expm1() * eps
|
| 927 |
+
eps_r1, eps_cache = self.eps(eps_cache, 'eps_r1', u1, s1)
|
| 928 |
+
u2 = x - self.sigma(s2) * (r2 * h).expm1() * eps - self.sigma(s2) * (r2 / r1) * ((r2 * h).expm1() / (r2 * h) - 1) * (eps_r1 - eps)
|
| 929 |
+
eps_r2, eps_cache = self.eps(eps_cache, 'eps_r2', u2, s2)
|
| 930 |
+
x_3 = x - self.sigma(t_next) * h.expm1() * eps - self.sigma(t_next) / r2 * (h.expm1() / h - 1) * (eps_r2 - eps)
|
| 931 |
+
return x_3, eps_cache
|
| 932 |
+
|
| 933 |
+
def dpm_solver_fast(self, x, t_start, t_end, nfe, eta=0., s_noise=1., noise_sampler=None):
|
| 934 |
+
noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler
|
| 935 |
+
if not t_end > t_start and eta:
|
| 936 |
+
raise ValueError('eta must be 0 for reverse sampling')
|
| 937 |
+
|
| 938 |
+
m = math.floor(nfe / 3) + 1
|
| 939 |
+
ts = torch.linspace(t_start, t_end, m + 1, device=x.device)
|
| 940 |
+
|
| 941 |
+
if nfe % 3 == 0:
|
| 942 |
+
orders = [3] * (m - 2) + [2, 1]
|
| 943 |
+
else:
|
| 944 |
+
orders = [3] * (m - 1) + [nfe % 3]
|
| 945 |
+
|
| 946 |
+
for i in range(len(orders)):
|
| 947 |
+
eps_cache = {}
|
| 948 |
+
t, t_next = ts[i], ts[i + 1]
|
| 949 |
+
if eta:
|
| 950 |
+
sd, su = get_ancestral_step(self.sigma(t), self.sigma(t_next), eta)
|
| 951 |
+
t_next_ = torch.minimum(t_end, self.t(sd))
|
| 952 |
+
su = (self.sigma(t_next) ** 2 - self.sigma(t_next_) ** 2) ** 0.5
|
| 953 |
+
else:
|
| 954 |
+
t_next_, su = t_next, 0.
|
| 955 |
+
|
| 956 |
+
eps, eps_cache = self.eps(eps_cache, 'eps', x, t)
|
| 957 |
+
denoised = x - self.sigma(t) * eps
|
| 958 |
+
if self.info_callback is not None:
|
| 959 |
+
self.info_callback({'x': x, 'i': i, 't': ts[i], 't_up': t, 'denoised': denoised})
|
| 960 |
+
|
| 961 |
+
if orders[i] == 1:
|
| 962 |
+
x, eps_cache = self.dpm_solver_1_step(x, t, t_next_, eps_cache=eps_cache)
|
| 963 |
+
elif orders[i] == 2:
|
| 964 |
+
x, eps_cache = self.dpm_solver_2_step(x, t, t_next_, eps_cache=eps_cache)
|
| 965 |
+
else:
|
| 966 |
+
x, eps_cache = self.dpm_solver_3_step(x, t, t_next_, eps_cache=eps_cache)
|
| 967 |
+
|
| 968 |
+
x = x + su * s_noise * noise_sampler(self.sigma(t), self.sigma(t_next))
|
| 969 |
+
|
| 970 |
+
return x
|
| 971 |
+
|
| 972 |
+
def dpm_solver_adaptive(self, x, t_start, t_end, order=3, rtol=0.05, atol=0.0078, h_init=0.05, pcoeff=0., icoeff=1.,
|
| 973 |
+
dcoeff=0., accept_safety=0.81, eta=0., s_noise=1., noise_sampler=None):
|
| 974 |
+
noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler
|
| 975 |
+
if order not in {2, 3}:
|
| 976 |
+
raise ValueError('order should be 2 or 3')
|
| 977 |
+
forward = t_end > t_start
|
| 978 |
+
if not forward and eta:
|
| 979 |
+
raise ValueError('eta must be 0 for reverse sampling')
|
| 980 |
+
h_init = abs(h_init) * (1 if forward else -1)
|
| 981 |
+
atol = torch.tensor(atol)
|
| 982 |
+
rtol = torch.tensor(rtol)
|
| 983 |
+
s = t_start
|
| 984 |
+
x_prev = x
|
| 985 |
+
accept = True
|
| 986 |
+
pid = PIDStepSizeController(h_init, pcoeff, icoeff, dcoeff, 1.5 if eta else order, accept_safety)
|
| 987 |
+
info = {'steps': 0, 'nfe': 0, 'n_accept': 0, 'n_reject': 0}
|
| 988 |
+
|
| 989 |
+
while s < t_end - 1e-5 if forward else s > t_end + 1e-5:
|
| 990 |
+
eps_cache = {}
|
| 991 |
+
t = torch.minimum(t_end, s + pid.h) if forward else torch.maximum(t_end, s + pid.h)
|
| 992 |
+
if eta:
|
| 993 |
+
sd, su = get_ancestral_step(self.sigma(s), self.sigma(t), eta)
|
| 994 |
+
t_ = torch.minimum(t_end, self.t(sd))
|
| 995 |
+
su = (self.sigma(t) ** 2 - self.sigma(t_) ** 2) ** 0.5
|
| 996 |
+
else:
|
| 997 |
+
t_, su = t, 0.
|
| 998 |
+
|
| 999 |
+
eps, eps_cache = self.eps(eps_cache, 'eps', x, s)
|
| 1000 |
+
denoised = x - self.sigma(s) * eps
|
| 1001 |
+
|
| 1002 |
+
if order == 2:
|
| 1003 |
+
x_low, eps_cache = self.dpm_solver_1_step(x, s, t_, eps_cache=eps_cache)
|
| 1004 |
+
x_high, eps_cache = self.dpm_solver_2_step(x, s, t_, eps_cache=eps_cache)
|
| 1005 |
+
else:
|
| 1006 |
+
x_low, eps_cache = self.dpm_solver_2_step(x, s, t_, r1=1 / 3, eps_cache=eps_cache)
|
| 1007 |
+
x_high, eps_cache = self.dpm_solver_3_step(x, s, t_, eps_cache=eps_cache)
|
| 1008 |
+
delta = torch.maximum(atol, rtol * torch.maximum(x_low.abs(), x_prev.abs()))
|
| 1009 |
+
error = torch.linalg.norm((x_low - x_high) / delta) / x.numel() ** 0.5
|
| 1010 |
+
accept = pid.propose_step(error)
|
| 1011 |
+
if accept:
|
| 1012 |
+
x_prev = x_low
|
| 1013 |
+
x = x_high + su * s_noise * noise_sampler(self.sigma(s), self.sigma(t))
|
| 1014 |
+
s = t
|
| 1015 |
+
info['n_accept'] += 1
|
| 1016 |
+
else:
|
| 1017 |
+
info['n_reject'] += 1
|
| 1018 |
+
info['nfe'] += order
|
| 1019 |
+
info['steps'] += 1
|
| 1020 |
+
|
| 1021 |
+
if self.info_callback is not None:
|
| 1022 |
+
self.info_callback(
|
| 1023 |
+
{'x': x, 'i': info['steps'] - 1, 't': s, 't_up': s, 'denoised': denoised, 'error': error,
|
| 1024 |
+
'h': pid.h, **info})
|
| 1025 |
+
|
| 1026 |
+
return x, info
|
| 1027 |
+
|
| 1028 |
+
class BatchedBrownianTree:
|
| 1029 |
+
"""A wrapper around torchsde.BrownianTree that enables batches of entropy."""
|
| 1030 |
+
|
| 1031 |
+
def __init__(self, x, t0, t1, seed=None, **kwargs):
|
| 1032 |
+
t0, t1, self.sign = self.sort(t0, t1)
|
| 1033 |
+
w0 = kwargs.get('w0', torch.zeros_like(x))
|
| 1034 |
+
if seed is None:
|
| 1035 |
+
seed = torch.randint(0, 2 ** 63 - 1, []).item()
|
| 1036 |
+
self.batched = True
|
| 1037 |
+
try:
|
| 1038 |
+
assert len(seed) == x.shape[0]
|
| 1039 |
+
w0 = w0[0]
|
| 1040 |
+
except TypeError:
|
| 1041 |
+
seed = [seed]
|
| 1042 |
+
self.batched = False
|
| 1043 |
+
self.trees = [torchsde.BrownianTree(t0, w0, t1, entropy=s, **kwargs) for s in seed]
|
| 1044 |
+
|
| 1045 |
+
@staticmethod
|
| 1046 |
+
def sort(a, b):
|
| 1047 |
+
return (a, b, 1) if a < b else (b, a, -1)
|
| 1048 |
+
|
| 1049 |
+
def __call__(self, t0, t1):
|
| 1050 |
+
t0, t1, sign = self.sort(t0, t1)
|
| 1051 |
+
w = torch.stack([tree(t0, t1) for tree in self.trees]) * (self.sign * sign)
|
| 1052 |
+
return w if self.batched else w[0]
|
| 1053 |
+
|
| 1054 |
+
class BrownianTreeNoiseSampler:
|
| 1055 |
+
"""A noise sampler backed by a torchsde.BrownianTree.
|
| 1056 |
+
|
| 1057 |
+
Args:
|
| 1058 |
+
x (Tensor): The tensor whose shape, device and dtype to use to generate
|
| 1059 |
+
random samples.
|
| 1060 |
+
sigma_min (float): The low end of the valid interval.
|
| 1061 |
+
sigma_max (float): The high end of the valid interval.
|
| 1062 |
+
seed (int or List[int]): The random seed. If a list of seeds is
|
| 1063 |
+
supplied instead of a single integer, then the noise sampler will
|
| 1064 |
+
use one BrownianTree per batch item, each with its own seed.
|
| 1065 |
+
transform (callable): A function that maps sigma to the sampler's
|
| 1066 |
+
internal timestep.
|
| 1067 |
+
"""
|
| 1068 |
+
|
| 1069 |
+
def __init__(self, x, sigma_min, sigma_max, seed=None, transform=lambda x: x):
|
| 1070 |
+
self.transform = transform
|
| 1071 |
+
t0, t1 = self.transform(torch.as_tensor(sigma_min)), self.transform(torch.as_tensor(sigma_max))
|
| 1072 |
+
self.tree = BatchedBrownianTree(x, t0, t1, seed)
|
| 1073 |
+
|
| 1074 |
+
def __call__(self, sigma, sigma_next):
|
| 1075 |
+
t0, t1 = self.transform(torch.as_tensor(sigma)), self.transform(torch.as_tensor(sigma_next))
|
| 1076 |
+
return self.tree(t0, t1) / (t1 - t0).abs().sqrt()
|
| 1077 |
+
|
| 1078 |
+
#===================================================================================
|
| 1079 |
+
|
| 1080 |
+
@torch.no_grad()
|
| 1081 |
+
def sample_skip(model, x, sigmas, extra_args=None, callback=None, disable=None, s_churn=0., s_tmin=0., s_tmax=float('inf'), s_noise=1.):
|
| 1082 |
+
"""Implements Algorithm 2 (Euler steps) from Karras et al. (2022)."""
|
| 1083 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1084 |
+
return x
|
| 1085 |
+
|
| 1086 |
+
@torch.no_grad()
|
| 1087 |
+
def sample_euler(model, x, sigmas, extra_args=None, callback=None, disable=None, s_churn=0., s_tmin=0., s_tmax=float('inf'), s_noise=1.):
|
| 1088 |
+
"""Implements Algorithm 2 (Euler steps) from Karras et al. (2022)."""
|
| 1089 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1090 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1091 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1092 |
+
gamma = min(s_churn / (len(sigmas) - 1), 2 ** 0.5 - 1) if s_tmin <= sigmas[i] <= s_tmax else 0.
|
| 1093 |
+
eps = torch.randn_like(x) * s_noise
|
| 1094 |
+
sigma_hat = sigmas[i] * (gamma + 1)
|
| 1095 |
+
if gamma > 0:
|
| 1096 |
+
x = x + eps * (sigma_hat ** 2 - sigmas[i] ** 2) ** 0.5
|
| 1097 |
+
denoised = model(x, sigma_hat * s_in, **extra_args)
|
| 1098 |
+
d = to_d(x, sigma_hat, denoised)
|
| 1099 |
+
if callback is not None:
|
| 1100 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigma_hat, 'denoised': denoised})
|
| 1101 |
+
dt = sigmas[i + 1] - sigma_hat
|
| 1102 |
+
# Euler method
|
| 1103 |
+
x = x + d * dt
|
| 1104 |
+
return x
|
| 1105 |
+
|
| 1106 |
+
@torch.no_grad()
|
| 1107 |
+
def sample_euler_ancestral(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None):
|
| 1108 |
+
"""Ancestral sampling with Euler method steps."""
|
| 1109 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1110 |
+
noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler
|
| 1111 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1112 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1113 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 1114 |
+
sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1], eta=eta)
|
| 1115 |
+
if callback is not None:
|
| 1116 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 1117 |
+
d = to_d(x, sigmas[i], denoised)
|
| 1118 |
+
# Euler method
|
| 1119 |
+
dt = sigma_down - sigmas[i]
|
| 1120 |
+
x = x + d * dt
|
| 1121 |
+
if sigmas[i + 1] > 0:
|
| 1122 |
+
x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * s_noise * sigma_up
|
| 1123 |
+
return x
|
| 1124 |
+
|
| 1125 |
+
@torch.no_grad()
|
| 1126 |
+
def sample_heun(model, x, sigmas, extra_args=None, callback=None, disable=None, s_churn=0., s_tmin=0., s_tmax=float('inf'), s_noise=1.):
|
| 1127 |
+
"""Implements Algorithm 2 (Heun steps) from Karras et al. (2022)."""
|
| 1128 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1129 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1130 |
+
s_end = sigmas[-1]
|
| 1131 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1132 |
+
gamma = min(s_churn / (len(sigmas) - 1), 2 ** 0.5 - 1) if s_tmin <= sigmas[i] <= s_tmax else 0.
|
| 1133 |
+
eps = torch.randn_like(x) * s_noise
|
| 1134 |
+
sigma_hat = sigmas[i] * (gamma + 1)
|
| 1135 |
+
if gamma > 0:
|
| 1136 |
+
x = x + eps * (sigma_hat ** 2 - sigmas[i] ** 2) ** 0.5
|
| 1137 |
+
denoised = model(x, sigma_hat * s_in, **extra_args)
|
| 1138 |
+
d = to_d(x, sigma_hat, denoised)
|
| 1139 |
+
if callback is not None:
|
| 1140 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigma_hat, 'denoised': denoised})
|
| 1141 |
+
dt = sigmas[i + 1] - sigma_hat
|
| 1142 |
+
if sigmas[i + 1] == s_end:
|
| 1143 |
+
# Euler method
|
| 1144 |
+
x = x + d * dt
|
| 1145 |
+
else:
|
| 1146 |
+
# Heun's method
|
| 1147 |
+
x_2 = x + d * dt
|
| 1148 |
+
denoised_2 = model(x_2, sigmas[i + 1] * s_in, **extra_args)
|
| 1149 |
+
d_2 = to_d(x_2, sigmas[i + 1], denoised_2)
|
| 1150 |
+
d_prime = (d + d_2) / 2
|
| 1151 |
+
x = x + d_prime * dt
|
| 1152 |
+
|
| 1153 |
+
return x
|
| 1154 |
+
|
| 1155 |
+
@torch.no_grad()
|
| 1156 |
+
def sample_heunpp2(model, x, sigmas, extra_args=None, callback=None, disable=None, s_churn=0., s_tmin=0., s_tmax=float('inf'), s_noise=1.):
|
| 1157 |
+
"""Implements Algorithm 2 (Heun steps) from Karras et al. (2022)."""
|
| 1158 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1159 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1160 |
+
s_end = sigmas[-1]
|
| 1161 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1162 |
+
gamma = min(s_churn / (len(sigmas) - 1), 2 ** 0.5 - 1) if s_tmin <= sigmas[i] <= s_tmax else 0.
|
| 1163 |
+
eps = torch.randn_like(x) * s_noise
|
| 1164 |
+
sigma_hat = sigmas[i] * (gamma + 1)
|
| 1165 |
+
if gamma > 0:
|
| 1166 |
+
x = x + eps * (sigma_hat ** 2 - sigmas[i] ** 2) ** 0.5
|
| 1167 |
+
denoised = model(x, sigma_hat * s_in, **extra_args)
|
| 1168 |
+
d = to_d(x, sigma_hat, denoised)
|
| 1169 |
+
if callback is not None:
|
| 1170 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigma_hat, 'denoised': denoised})
|
| 1171 |
+
dt = sigmas[i + 1] - sigma_hat
|
| 1172 |
+
if sigmas[i + 1] == s_end:
|
| 1173 |
+
# Euler method
|
| 1174 |
+
x = x + d * dt
|
| 1175 |
+
elif sigmas[i + 2] == s_end:
|
| 1176 |
+
|
| 1177 |
+
# Heun's method
|
| 1178 |
+
x_2 = x + d * dt
|
| 1179 |
+
denoised_2 = model(x_2, sigmas[i + 1] * s_in, **extra_args)
|
| 1180 |
+
d_2 = to_d(x_2, sigmas[i + 1], denoised_2)
|
| 1181 |
+
|
| 1182 |
+
w = 2 * sigmas[0]
|
| 1183 |
+
w2 = sigmas[i+1]/w
|
| 1184 |
+
w1 = 1 - w2
|
| 1185 |
+
|
| 1186 |
+
d_prime = d * w1 + d_2 * w2
|
| 1187 |
+
|
| 1188 |
+
|
| 1189 |
+
x = x + d_prime * dt
|
| 1190 |
+
|
| 1191 |
+
else:
|
| 1192 |
+
# Heun++
|
| 1193 |
+
x_2 = x + d * dt
|
| 1194 |
+
denoised_2 = model(x_2, sigmas[i + 1] * s_in, **extra_args)
|
| 1195 |
+
d_2 = to_d(x_2, sigmas[i + 1], denoised_2)
|
| 1196 |
+
dt_2 = sigmas[i + 2] - sigmas[i + 1]
|
| 1197 |
+
|
| 1198 |
+
x_3 = x_2 + d_2 * dt_2
|
| 1199 |
+
denoised_3 = model(x_3, sigmas[i + 2] * s_in, **extra_args)
|
| 1200 |
+
d_3 = to_d(x_3, sigmas[i + 2], denoised_3)
|
| 1201 |
+
|
| 1202 |
+
w = 3 * sigmas[0]
|
| 1203 |
+
w2 = sigmas[i + 1] / w
|
| 1204 |
+
w3 = sigmas[i + 2] / w
|
| 1205 |
+
w1 = 1 - w2 - w3
|
| 1206 |
+
|
| 1207 |
+
d_prime = w1 * d + w2 * d_2 + w3 * d_3
|
| 1208 |
+
x = x + d_prime * dt
|
| 1209 |
+
return x
|
| 1210 |
+
|
| 1211 |
+
@torch.no_grad()
|
| 1212 |
+
def sample_lms(model, x, sigmas, extra_args=None, callback=None, disable=None, order=4):
|
| 1213 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1214 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1215 |
+
sigmas_cpu = sigmas.detach().cpu().numpy()
|
| 1216 |
+
ds = []
|
| 1217 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1218 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 1219 |
+
d = to_d(x, sigmas[i], denoised)
|
| 1220 |
+
ds.append(d)
|
| 1221 |
+
if len(ds) > order:
|
| 1222 |
+
ds.pop(0)
|
| 1223 |
+
if callback is not None:
|
| 1224 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 1225 |
+
cur_order = min(i + 1, order)
|
| 1226 |
+
coeffs = [linear_multistep_coeff(cur_order, sigmas_cpu, i, j) for j in range(cur_order)]
|
| 1227 |
+
x = x + sum(coeff * d for coeff, d in zip(coeffs, reversed(ds)))
|
| 1228 |
+
|
| 1229 |
+
return x
|
| 1230 |
+
|
| 1231 |
+
@torch.no_grad()
|
| 1232 |
+
def sample_dpm_2(model, x, sigmas, extra_args=None, callback=None, disable=None, s_churn=0., s_tmin=0., s_tmax=float('inf'), s_noise=1.):
|
| 1233 |
+
"""A sampler inspired by DPM-Solver-2 and Algorithm 2 from Karras et al. (2022)."""
|
| 1234 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1235 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1236 |
+
s_end = sigmas[-1]
|
| 1237 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1238 |
+
gamma = min(s_churn / (len(sigmas) - 1), 2 ** 0.5 - 1) if s_tmin <= sigmas[i] <= s_tmax else 0.
|
| 1239 |
+
eps = torch.randn_like(x) * s_noise
|
| 1240 |
+
sigma_hat = sigmas[i] * (gamma + 1)
|
| 1241 |
+
if gamma > 0:
|
| 1242 |
+
x = x + eps * (sigma_hat ** 2 - sigmas[i] ** 2) ** 0.5
|
| 1243 |
+
denoised = model(x, sigma_hat * s_in, **extra_args)
|
| 1244 |
+
d = to_d(x, sigma_hat, denoised)
|
| 1245 |
+
if callback is not None:
|
| 1246 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigma_hat, 'denoised': denoised})
|
| 1247 |
+
if sigmas[i + 1] == s_end:
|
| 1248 |
+
# Euler method
|
| 1249 |
+
dt = sigmas[i + 1] - sigma_hat
|
| 1250 |
+
x = x + d * dt
|
| 1251 |
+
else:
|
| 1252 |
+
# DPM-Solver-2
|
| 1253 |
+
sigma_mid = sigma_hat.log().lerp(sigmas[i + 1].log(), 0.5).exp()
|
| 1254 |
+
dt_1 = sigma_mid - sigma_hat
|
| 1255 |
+
dt_2 = sigmas[i + 1] - sigma_hat
|
| 1256 |
+
x_2 = x + d * dt_1
|
| 1257 |
+
denoised_2 = model(x_2, sigma_mid * s_in, **extra_args)
|
| 1258 |
+
d_2 = to_d(x_2, sigma_mid, denoised_2)
|
| 1259 |
+
x = x + d_2 * dt_2
|
| 1260 |
+
return x
|
| 1261 |
+
|
| 1262 |
+
@torch.no_grad()
|
| 1263 |
+
def sample_dpm_2_ancestral(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None):
|
| 1264 |
+
"""Ancestral sampling with DPM-Solver second-order steps."""
|
| 1265 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1266 |
+
noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler
|
| 1267 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1268 |
+
s_end = sigmas[-1]
|
| 1269 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1270 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 1271 |
+
sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1], eta=eta)
|
| 1272 |
+
if callback is not None:
|
| 1273 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 1274 |
+
d = to_d(x, sigmas[i], denoised)
|
| 1275 |
+
if sigma_down == s_end:
|
| 1276 |
+
# Euler method
|
| 1277 |
+
dt = sigma_down - sigmas[i]
|
| 1278 |
+
x = x + d * dt
|
| 1279 |
+
else:
|
| 1280 |
+
# DPM-Solver-2
|
| 1281 |
+
sigma_mid = sigmas[i].log().lerp(sigma_down.log(), 0.5).exp()
|
| 1282 |
+
dt_1 = sigma_mid - sigmas[i]
|
| 1283 |
+
dt_2 = sigma_down - sigmas[i]
|
| 1284 |
+
x_2 = x + d * dt_1
|
| 1285 |
+
denoised_2 = model(x_2, sigma_mid * s_in, **extra_args)
|
| 1286 |
+
d_2 = to_d(x_2, sigma_mid, denoised_2)
|
| 1287 |
+
x = x + d_2 * dt_2
|
| 1288 |
+
x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * s_noise * sigma_up
|
| 1289 |
+
return x
|
| 1290 |
+
|
| 1291 |
+
@torch.no_grad()
|
| 1292 |
+
def sample_dpmpp_2s_ancestral(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None):
|
| 1293 |
+
"""Ancestral sampling with DPM-Solver++(2S) second-order steps."""
|
| 1294 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1295 |
+
noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler
|
| 1296 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1297 |
+
sigma_fn = lambda t: t.neg().exp()
|
| 1298 |
+
t_fn = lambda sigma: sigma.log().neg()
|
| 1299 |
+
s_end = sigmas[-1]
|
| 1300 |
+
|
| 1301 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1302 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 1303 |
+
sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1], eta=eta)
|
| 1304 |
+
if callback is not None:
|
| 1305 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 1306 |
+
if sigma_down == s_end:
|
| 1307 |
+
# Euler method
|
| 1308 |
+
d = to_d(x, sigmas[i], denoised)
|
| 1309 |
+
dt = sigma_down - sigmas[i]
|
| 1310 |
+
x = x + d * dt
|
| 1311 |
+
else:
|
| 1312 |
+
# DPM-Solver++(2S)
|
| 1313 |
+
t, t_next = t_fn(sigmas[i]), t_fn(sigma_down)
|
| 1314 |
+
r = 1 / 2
|
| 1315 |
+
h = t_next - t
|
| 1316 |
+
s = t + r * h
|
| 1317 |
+
x_2 = (sigma_fn(s) / sigma_fn(t)) * x - (-h * r).expm1() * denoised
|
| 1318 |
+
denoised_2 = model(x_2, sigma_fn(s) * s_in, **extra_args)
|
| 1319 |
+
x = (sigma_fn(t_next) / sigma_fn(t)) * x - (-h).expm1() * denoised_2
|
| 1320 |
+
# Noise addition
|
| 1321 |
+
if sigmas[i + 1] > 0:
|
| 1322 |
+
x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * s_noise * sigma_up
|
| 1323 |
+
return x
|
| 1324 |
+
|
| 1325 |
+
@torch.no_grad()
|
| 1326 |
+
def sample_dpmpp_sde(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None, r=1 / 2):
|
| 1327 |
+
"""DPM-Solver++ (stochastic)."""
|
| 1328 |
+
sigma_min, sigma_max = sigmas[sigmas > 0].min(), sigmas.max()
|
| 1329 |
+
noise_sampler = BrownianTreeNoiseSampler(x, sigma_min, sigma_max) if noise_sampler is None else noise_sampler
|
| 1330 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1331 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1332 |
+
sigma_fn = lambda t: t.neg().exp()
|
| 1333 |
+
t_fn = lambda sigma: sigma.log().neg()
|
| 1334 |
+
s_end = sigmas[-1]
|
| 1335 |
+
|
| 1336 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1337 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 1338 |
+
if callback is not None:
|
| 1339 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 1340 |
+
if sigmas[i + 1] == s_end:
|
| 1341 |
+
# Euler method
|
| 1342 |
+
d = to_d(x, sigmas[i], denoised)
|
| 1343 |
+
dt = sigmas[i + 1] - sigmas[i]
|
| 1344 |
+
x = x + d * dt
|
| 1345 |
+
else:
|
| 1346 |
+
# DPM-Solver++
|
| 1347 |
+
t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i + 1])
|
| 1348 |
+
h = t_next - t
|
| 1349 |
+
s = t + h * r
|
| 1350 |
+
fac = 1 / (2 * r)
|
| 1351 |
+
|
| 1352 |
+
# Step 1
|
| 1353 |
+
sd, su = get_ancestral_step(sigma_fn(t), sigma_fn(s), eta)
|
| 1354 |
+
s_ = t_fn(sd)
|
| 1355 |
+
x_2 = (sigma_fn(s_) / sigma_fn(t)) * x - (t - s_).expm1() * denoised
|
| 1356 |
+
x_2 = x_2 + noise_sampler(sigma_fn(t), sigma_fn(s)) * s_noise * su
|
| 1357 |
+
denoised_2 = model(x_2, sigma_fn(s) * s_in, **extra_args)
|
| 1358 |
+
|
| 1359 |
+
# Step 2
|
| 1360 |
+
sd, su = get_ancestral_step(sigma_fn(t), sigma_fn(t_next), eta)
|
| 1361 |
+
t_next_ = t_fn(sd)
|
| 1362 |
+
denoised_d = (1 - fac) * denoised + fac * denoised_2
|
| 1363 |
+
x = (sigma_fn(t_next_) / sigma_fn(t)) * x - (t - t_next_).expm1() * denoised_d
|
| 1364 |
+
x = x + noise_sampler(sigma_fn(t), sigma_fn(t_next)) * s_noise * su
|
| 1365 |
+
return x
|
| 1366 |
+
|
| 1367 |
+
@torch.no_grad()
|
| 1368 |
+
def sample_dpmpp_2m(model, x, sigmas, extra_args=None, callback=None, disable=None):
|
| 1369 |
+
"""DPM-Solver++(2M)."""
|
| 1370 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1371 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1372 |
+
sigma_fn = lambda t: t.neg().exp()
|
| 1373 |
+
t_fn = lambda sigma: sigma.log().neg()
|
| 1374 |
+
old_denoised = None
|
| 1375 |
+
s_end = sigmas[-1]
|
| 1376 |
+
|
| 1377 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1378 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 1379 |
+
if callback is not None:
|
| 1380 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 1381 |
+
t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i + 1])
|
| 1382 |
+
h = t_next - t
|
| 1383 |
+
if old_denoised is None or sigmas[i + 1] == s_end:
|
| 1384 |
+
x = (sigma_fn(t_next) / sigma_fn(t)) * x - (-h).expm1() * denoised
|
| 1385 |
+
else:
|
| 1386 |
+
h_last = t - t_fn(sigmas[i - 1])
|
| 1387 |
+
r = h_last / h
|
| 1388 |
+
denoised_d = (1 + 1 / (2 * r)) * denoised - (1 / (2 * r)) * old_denoised
|
| 1389 |
+
x = (sigma_fn(t_next) / sigma_fn(t)) * x - (-h).expm1() * denoised_d
|
| 1390 |
+
old_denoised = denoised
|
| 1391 |
+
return x
|
| 1392 |
+
|
| 1393 |
+
@torch.no_grad()
|
| 1394 |
+
def sample_dpmpp_2m_sde(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None, solver_type='midpoint'):
|
| 1395 |
+
"""DPM-Solver++(2M) SDE."""
|
| 1396 |
+
|
| 1397 |
+
if solver_type not in {'heun', 'midpoint'}:
|
| 1398 |
+
raise ValueError('solver_type must be \'heun\' or \'midpoint\'')
|
| 1399 |
+
|
| 1400 |
+
sigma_min, sigma_max = sigmas[sigmas > 0].min(), sigmas.max()
|
| 1401 |
+
noise_sampler = BrownianTreeNoiseSampler(x, sigma_min, sigma_max) if noise_sampler is None else noise_sampler
|
| 1402 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1403 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1404 |
+
s_end = sigmas[-1]
|
| 1405 |
+
|
| 1406 |
+
old_denoised = None
|
| 1407 |
+
h_last = None
|
| 1408 |
+
|
| 1409 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1410 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 1411 |
+
if callback is not None:
|
| 1412 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 1413 |
+
if sigmas[i + 1] == s_end:
|
| 1414 |
+
# Denoising step
|
| 1415 |
+
x = denoised
|
| 1416 |
+
else:
|
| 1417 |
+
# DPM-Solver++(2M) SDE
|
| 1418 |
+
t, s = -sigmas[i].log(), -sigmas[i + 1].log()
|
| 1419 |
+
h = s - t
|
| 1420 |
+
eta_h = eta * h
|
| 1421 |
+
|
| 1422 |
+
x = sigmas[i + 1] / sigmas[i] * (-eta_h).exp() * x + (-h - eta_h).expm1().neg() * denoised
|
| 1423 |
+
|
| 1424 |
+
if old_denoised is not None:
|
| 1425 |
+
r = h_last / h
|
| 1426 |
+
if solver_type == 'heun':
|
| 1427 |
+
x = x + ((-h - eta_h).expm1().neg() / (-h - eta_h) + 1) * (1 / r) * (denoised - old_denoised)
|
| 1428 |
+
elif solver_type == 'midpoint':
|
| 1429 |
+
x = x + 0.5 * (-h - eta_h).expm1().neg() * (1 / r) * (denoised - old_denoised)
|
| 1430 |
+
|
| 1431 |
+
if eta:
|
| 1432 |
+
x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * sigmas[i + 1] * (-2 * eta_h).expm1().neg().sqrt() * s_noise
|
| 1433 |
+
|
| 1434 |
+
old_denoised = denoised
|
| 1435 |
+
h_last = h
|
| 1436 |
+
return x
|
| 1437 |
+
|
| 1438 |
+
@torch.no_grad()
|
| 1439 |
+
def sample_dpmpp_3m_sde(model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1., s_noise=1., noise_sampler=None):
|
| 1440 |
+
"""DPM-Solver++(3M) SDE."""
|
| 1441 |
+
|
| 1442 |
+
sigma_min, sigma_max = sigmas[sigmas > 0].min(), sigmas.max()
|
| 1443 |
+
noise_sampler = BrownianTreeNoiseSampler(x, sigma_min, sigma_max) if noise_sampler is None else noise_sampler
|
| 1444 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1445 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1446 |
+
s_end = sigmas[-1]
|
| 1447 |
+
|
| 1448 |
+
denoised_1, denoised_2 = None, None
|
| 1449 |
+
h_1, h_2 = None, None
|
| 1450 |
+
|
| 1451 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1452 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 1453 |
+
if callback is not None:
|
| 1454 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 1455 |
+
if sigmas[i + 1] == s_end:
|
| 1456 |
+
# Denoising step
|
| 1457 |
+
x = denoised
|
| 1458 |
+
else:
|
| 1459 |
+
t, s = -sigmas[i].log(), -sigmas[i + 1].log()
|
| 1460 |
+
h = s - t
|
| 1461 |
+
h_eta = h * (eta + 1)
|
| 1462 |
+
|
| 1463 |
+
x = torch.exp(-h_eta) * x + (-h_eta).expm1().neg() * denoised
|
| 1464 |
+
|
| 1465 |
+
if h_2 is not None:
|
| 1466 |
+
r0 = h_1 / h
|
| 1467 |
+
r1 = h_2 / h
|
| 1468 |
+
d1_0 = (denoised - denoised_1) / r0
|
| 1469 |
+
d1_1 = (denoised_1 - denoised_2) / r1
|
| 1470 |
+
d1 = d1_0 + (d1_0 - d1_1) * r0 / (r0 + r1)
|
| 1471 |
+
d2 = (d1_0 - d1_1) / (r0 + r1)
|
| 1472 |
+
phi_2 = h_eta.neg().expm1() / h_eta + 1
|
| 1473 |
+
phi_3 = phi_2 / h_eta - 0.5
|
| 1474 |
+
x = x + phi_2 * d1 - phi_3 * d2
|
| 1475 |
+
elif h_1 is not None:
|
| 1476 |
+
r = h_1 / h
|
| 1477 |
+
d = (denoised - denoised_1) / r
|
| 1478 |
+
phi_2 = h_eta.neg().expm1() / h_eta + 1
|
| 1479 |
+
x = x + phi_2 * d
|
| 1480 |
+
|
| 1481 |
+
if eta:
|
| 1482 |
+
x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * sigmas[i + 1] * (-2 * h * eta).expm1().neg().sqrt() * s_noise
|
| 1483 |
+
|
| 1484 |
+
denoised_1, denoised_2 = denoised, denoised_1
|
| 1485 |
+
h_1, h_2 = h, h_1
|
| 1486 |
+
return x
|
| 1487 |
+
|
| 1488 |
+
@torch.no_grad()
|
| 1489 |
+
def lcm_sampler(model, x, sigmas, extra_args=None, callback=None, disable=None, noise_sampler=None):
|
| 1490 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1491 |
+
noise_sampler = default_noise_sampler(x) if noise_sampler is None else noise_sampler
|
| 1492 |
+
s_end = sigmas[-1]
|
| 1493 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1494 |
+
for i in trange(len(sigmas) - 1, disable=disable):
|
| 1495 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 1496 |
+
if callback is not None:
|
| 1497 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 1498 |
+
|
| 1499 |
+
x = denoised
|
| 1500 |
+
if sigmas[i + 1] > s_end:
|
| 1501 |
+
x += sigmas[i + 1] * noise_sampler(sigmas[i], sigmas[i + 1])
|
| 1502 |
+
return x
|
| 1503 |
+
|
| 1504 |
+
@torch.no_grad()
|
| 1505 |
+
def restart_sampler(model, x, sigmas, extra_args=None, callback=None, disable=None, s_noise=1., restart_list=None):
|
| 1506 |
+
"""Implements restart sampling in Restart Sampling for Improving Generative Processes (2023)
|
| 1507 |
+
Restart_list format: {min_sigma: [ restart_steps, restart_times, max_sigma]}
|
| 1508 |
+
If restart_list is None: will choose restart_list automatically, otherwise will use the given restart_list
|
| 1509 |
+
"""
|
| 1510 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1511 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1512 |
+
step_id = 0
|
| 1513 |
+
s_end = sigmas[-1]
|
| 1514 |
+
|
| 1515 |
+
def heun_step(x, old_sigma, new_sigma, second_order=True):
|
| 1516 |
+
nonlocal step_id, s_end
|
| 1517 |
+
denoised = model(x, old_sigma * s_in, **extra_args)
|
| 1518 |
+
d = to_d(x, old_sigma, denoised)
|
| 1519 |
+
if callback is not None:
|
| 1520 |
+
callback({'x': x, 'i': step_id, 'sigma': new_sigma, 'sigma_hat': old_sigma, 'denoised': denoised})
|
| 1521 |
+
dt = new_sigma - old_sigma
|
| 1522 |
+
if new_sigma == s_end or not second_order:
|
| 1523 |
+
# Euler method
|
| 1524 |
+
x = x + d * dt
|
| 1525 |
+
else:
|
| 1526 |
+
# Heun's method
|
| 1527 |
+
x_2 = x + d * dt
|
| 1528 |
+
denoised_2 = model(x_2, new_sigma * s_in, **extra_args)
|
| 1529 |
+
d_2 = to_d(x_2, new_sigma, denoised_2)
|
| 1530 |
+
d_prime = (d + d_2) / 2
|
| 1531 |
+
x = x + d_prime * dt
|
| 1532 |
+
step_id += 1
|
| 1533 |
+
return x
|
| 1534 |
+
|
| 1535 |
+
steps = sigmas.shape[0] - 1
|
| 1536 |
+
if restart_list is None:
|
| 1537 |
+
if steps >= 20:
|
| 1538 |
+
restart_steps = 9
|
| 1539 |
+
restart_times = 1
|
| 1540 |
+
if steps >= 36:
|
| 1541 |
+
restart_steps = steps // 4
|
| 1542 |
+
restart_times = 2
|
| 1543 |
+
sigmas = get_sigmas_karras(steps - restart_steps * restart_times, sigmas[-2].item(), sigmas[0].item(), device=sigmas.device)
|
| 1544 |
+
restart_list = {0.1: [restart_steps + 1, restart_times, 2]}
|
| 1545 |
+
else:
|
| 1546 |
+
restart_list = {}
|
| 1547 |
+
|
| 1548 |
+
restart_list = {int(torch.argmin(abs(sigmas - key), dim=0)): value for key, value in restart_list.items()}
|
| 1549 |
+
|
| 1550 |
+
step_list = []
|
| 1551 |
+
for i in range(len(sigmas) - 1):
|
| 1552 |
+
step_list.append((sigmas[i], sigmas[i + 1]))
|
| 1553 |
+
if i + 1 in restart_list:
|
| 1554 |
+
restart_steps, restart_times, restart_max = restart_list[i + 1]
|
| 1555 |
+
min_idx = i + 1
|
| 1556 |
+
max_idx = int(torch.argmin(abs(sigmas - restart_max), dim=0))
|
| 1557 |
+
if max_idx < min_idx:
|
| 1558 |
+
sigma_restart = get_sigmas_karras(restart_steps, sigmas[min_idx].item(), sigmas[max_idx].item(), device=sigmas.device)[:-1]
|
| 1559 |
+
while restart_times > 0:
|
| 1560 |
+
restart_times -= 1
|
| 1561 |
+
step_list.extend([(old_sigma, new_sigma) for (old_sigma, new_sigma) in zip(sigma_restart[:-1], sigma_restart[1:])])
|
| 1562 |
+
|
| 1563 |
+
last_sigma = None
|
| 1564 |
+
for old_sigma, new_sigma in tqdm(step_list, disable=disable):
|
| 1565 |
+
if last_sigma is None:
|
| 1566 |
+
last_sigma = old_sigma
|
| 1567 |
+
elif last_sigma < old_sigma:
|
| 1568 |
+
x = x + k_diffusion.sampling.torch.randn_like(x) * s_noise * (old_sigma ** 2 - last_sigma ** 2) ** 0.5
|
| 1569 |
+
x = heun_step(x, old_sigma, new_sigma)
|
| 1570 |
+
last_sigma = new_sigma
|
| 1571 |
+
|
| 1572 |
+
return x
|
| 1573 |
+
|
| 1574 |
+
@torch.no_grad()
|
| 1575 |
+
def sample_dpmpp_2m_tm(model, x, sigmas, extra_args=None, callback=None, disable=None):
|
| 1576 |
+
"""DPM-Solver++(2M)."""
|
| 1577 |
+
extra_args = {} if extra_args is None else extra_args
|
| 1578 |
+
s_in = x.new_ones([x.shape[0]])
|
| 1579 |
+
sigma_fn = lambda t: t.neg().exp()
|
| 1580 |
+
t_fn = lambda sigma: sigma.log().neg()
|
| 1581 |
+
old_denoised = None
|
| 1582 |
+
|
| 1583 |
+
for i in tqdm.tqdm(self.steps, disable=disable):
|
| 1584 |
+
i -= 1
|
| 1585 |
+
|
| 1586 |
+
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
| 1587 |
+
if callback is not None:
|
| 1588 |
+
callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
| 1589 |
+
t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i + 1])
|
| 1590 |
+
h = t_next - t
|
| 1591 |
+
if old_denoised is None or sigmas[i + 1] == 0:
|
| 1592 |
+
x = (sigma_fn(t_next) / sigma_fn(t)) * x - (-h).expm1() * denoised
|
| 1593 |
+
else:
|
| 1594 |
+
h_last = t - t_fn(sigmas[i - 1])
|
| 1595 |
+
r = h_last / h
|
| 1596 |
+
denoised_d = (1 + 1 / (2 * r)) * denoised - (1 / (2 * r)) * old_denoised
|
| 1597 |
+
x = (sigma_fn(t_next) / sigma_fn(t)) * x - (-h).expm1() * denoised_d
|
| 1598 |
+
old_denoised = denoised
|
| 1599 |
+
return x
|