|
|
|
@ -2,8 +2,9 @@ import math
|
|
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
from scipy import integrate
|
|
|
|
|
from torch import nn
|
|
|
|
|
from torchdiffeq import odeint
|
|
|
|
|
from tqdm.auto import trange
|
|
|
|
|
from tqdm.auto import tqdm, trange
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def append_zero(x):
|
|
|
|
@ -39,12 +40,16 @@ def to_d(x, sigma, denoised):
|
|
|
|
|
return (x - denoised) / sigma
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_ancestral_step(sigma_from, sigma_to):
|
|
|
|
|
def get_ancestral_step(sigma_from, sigma_to, eta=1.0):
|
|
|
|
|
"""Calculates the noise level (sigma_down) to step down to and the amount
|
|
|
|
|
of noise to add (sigma_up) when doing an ancestral sampling step."""
|
|
|
|
|
sigma_up = (
|
|
|
|
|
sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2
|
|
|
|
|
) ** 0.5
|
|
|
|
|
if not eta:
|
|
|
|
|
return sigma_to, 0.0
|
|
|
|
|
sigma_up = min(
|
|
|
|
|
sigma_to,
|
|
|
|
|
eta
|
|
|
|
|
* (sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5,
|
|
|
|
|
)
|
|
|
|
|
sigma_down = (sigma_to**2 - sigma_up**2) ** 0.5
|
|
|
|
|
return sigma_down, sigma_up
|
|
|
|
|
|
|
|
|
@ -95,14 +100,14 @@ def sample_euler(
|
|
|
|
|
|
|
|
|
|
@torch.no_grad()
|
|
|
|
|
def sample_euler_ancestral(
|
|
|
|
|
model, x, sigmas, extra_args=None, callback=None, disable=None
|
|
|
|
|
model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1.0
|
|
|
|
|
):
|
|
|
|
|
"""Ancestral sampling with Euler method steps."""
|
|
|
|
|
extra_args = {} if extra_args is None else extra_args
|
|
|
|
|
s_in = x.new_ones([x.shape[0]])
|
|
|
|
|
for i in trange(len(sigmas) - 1, disable=disable):
|
|
|
|
|
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
|
|
|
|
sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1])
|
|
|
|
|
sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1], eta=eta)
|
|
|
|
|
if callback is not None:
|
|
|
|
|
callback(
|
|
|
|
|
{
|
|
|
|
@ -211,27 +216,32 @@ def sample_dpm_2(
|
|
|
|
|
"denoised": denoised,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
# Midpoint method, where the midpoint is chosen according to a rho=3 Karras schedule
|
|
|
|
|
sigma_mid = ((sigma_hat ** (1 / 3) + sigmas[i + 1] ** (1 / 3)) / 2) ** 3
|
|
|
|
|
dt_1 = sigma_mid - sigma_hat
|
|
|
|
|
dt_2 = sigmas[i + 1] - sigma_hat
|
|
|
|
|
x_2 = x + d * dt_1
|
|
|
|
|
denoised_2 = model(x_2, sigma_mid * s_in, **extra_args)
|
|
|
|
|
d_2 = to_d(x_2, sigma_mid, denoised_2)
|
|
|
|
|
x = x + d_2 * dt_2
|
|
|
|
|
if sigmas[i + 1] == 0:
|
|
|
|
|
# Euler method
|
|
|
|
|
dt = sigmas[i + 1] - sigma_hat
|
|
|
|
|
x = x + d * dt
|
|
|
|
|
else:
|
|
|
|
|
# DPM-Solver-2
|
|
|
|
|
sigma_mid = sigma_hat.log().lerp(sigmas[i + 1].log(), 0.5).exp()
|
|
|
|
|
dt_1 = sigma_mid - sigma_hat
|
|
|
|
|
dt_2 = sigmas[i + 1] - sigma_hat
|
|
|
|
|
x_2 = x + d * dt_1
|
|
|
|
|
denoised_2 = model(x_2, sigma_mid * s_in, **extra_args)
|
|
|
|
|
d_2 = to_d(x_2, sigma_mid, denoised_2)
|
|
|
|
|
x = x + d_2 * dt_2
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@torch.no_grad()
|
|
|
|
|
def sample_dpm_2_ancestral(
|
|
|
|
|
model, x, sigmas, extra_args=None, callback=None, disable=None
|
|
|
|
|
model, x, sigmas, extra_args=None, callback=None, disable=None, eta=1.0
|
|
|
|
|
):
|
|
|
|
|
"""Ancestral sampling with DPM-Solver inspired second-order steps."""
|
|
|
|
|
extra_args = {} if extra_args is None else extra_args
|
|
|
|
|
s_in = x.new_ones([x.shape[0]])
|
|
|
|
|
for i in trange(len(sigmas) - 1, disable=disable):
|
|
|
|
|
denoised = model(x, sigmas[i] * s_in, **extra_args)
|
|
|
|
|
sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1])
|
|
|
|
|
sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1], eta=eta)
|
|
|
|
|
if callback is not None:
|
|
|
|
|
callback(
|
|
|
|
|
{
|
|
|
|
@ -243,15 +253,20 @@ def sample_dpm_2_ancestral(
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
d = to_d(x, sigmas[i], denoised)
|
|
|
|
|
# Midpoint method, where the midpoint is chosen according to a rho=3 Karras schedule
|
|
|
|
|
sigma_mid = ((sigmas[i] ** (1 / 3) + sigma_down ** (1 / 3)) / 2) ** 3
|
|
|
|
|
dt_1 = sigma_mid - sigmas[i]
|
|
|
|
|
dt_2 = sigma_down - sigmas[i]
|
|
|
|
|
x_2 = x + d * dt_1
|
|
|
|
|
denoised_2 = model(x_2, sigma_mid * s_in, **extra_args)
|
|
|
|
|
d_2 = to_d(x_2, sigma_mid, denoised_2)
|
|
|
|
|
x = x + d_2 * dt_2
|
|
|
|
|
x = x + torch.randn_like(x, device="cpu").to(x.device) * sigma_up
|
|
|
|
|
if sigma_down == 0:
|
|
|
|
|
# Euler method
|
|
|
|
|
dt = sigma_down - sigmas[i]
|
|
|
|
|
x = x + d * dt
|
|
|
|
|
else:
|
|
|
|
|
# DPM-Solver-2
|
|
|
|
|
sigma_mid = sigmas[i].log().lerp(sigma_down.log(), 0.5).exp()
|
|
|
|
|
dt_1 = sigma_mid - sigmas[i]
|
|
|
|
|
dt_2 = sigma_down - sigmas[i]
|
|
|
|
|
x_2 = x + d * dt_1
|
|
|
|
|
denoised_2 = model(x_2, sigma_mid * s_in, **extra_args)
|
|
|
|
|
d_2 = to_d(x_2, sigma_mid, denoised_2)
|
|
|
|
|
x = x + d_2 * dt_2
|
|
|
|
|
x = x + torch.randn_like(x, device="cpu").to(x.device) * sigma_up
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -329,3 +344,336 @@ def log_likelihood(
|
|
|
|
|
torch.distributions.Normal(0, sigma_max).log_prob(latent).flatten(1).sum(1)
|
|
|
|
|
)
|
|
|
|
|
return ll_prior + delta_ll, {"fevals": fevals}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PIDStepSizeController:
|
|
|
|
|
"""A PID controller for ODE adaptive step size control."""
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self, h, pcoeff, icoeff, dcoeff, order=1, accept_safety=0.81, eps=1e-8
|
|
|
|
|
):
|
|
|
|
|
self.h = h
|
|
|
|
|
self.b1 = (pcoeff + icoeff + dcoeff) / order
|
|
|
|
|
self.b2 = -(pcoeff + 2 * dcoeff) / order
|
|
|
|
|
self.b3 = dcoeff / order
|
|
|
|
|
self.accept_safety = accept_safety
|
|
|
|
|
self.eps = eps
|
|
|
|
|
self.errs = []
|
|
|
|
|
|
|
|
|
|
def limiter(self, x):
|
|
|
|
|
return 1 + math.atan(x - 1)
|
|
|
|
|
|
|
|
|
|
def propose_step(self, error):
|
|
|
|
|
inv_error = 1 / (float(error) + self.eps)
|
|
|
|
|
if not self.errs:
|
|
|
|
|
self.errs = [inv_error, inv_error, inv_error]
|
|
|
|
|
self.errs[0] = inv_error
|
|
|
|
|
factor = (
|
|
|
|
|
self.errs[0] ** self.b1 * self.errs[1] ** self.b2 * self.errs[2] ** self.b3
|
|
|
|
|
)
|
|
|
|
|
factor = self.limiter(factor)
|
|
|
|
|
accept = factor >= self.accept_safety
|
|
|
|
|
if accept:
|
|
|
|
|
self.errs[2] = self.errs[1]
|
|
|
|
|
self.errs[1] = self.errs[0]
|
|
|
|
|
self.h *= factor
|
|
|
|
|
return accept
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DPMSolver(nn.Module):
|
|
|
|
|
"""DPM-Solver. See https://arxiv.org/abs/2206.00927."""
|
|
|
|
|
|
|
|
|
|
def __init__(self, model, extra_args=None, eps_callback=None, info_callback=None):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.model = model
|
|
|
|
|
self.extra_args = {} if extra_args is None else extra_args
|
|
|
|
|
self.eps_callback = eps_callback
|
|
|
|
|
self.info_callback = info_callback
|
|
|
|
|
|
|
|
|
|
def t(self, sigma):
|
|
|
|
|
return -sigma.log()
|
|
|
|
|
|
|
|
|
|
def sigma(self, t):
|
|
|
|
|
return t.neg().exp()
|
|
|
|
|
|
|
|
|
|
def eps(self, eps_cache, key, x, t, *args, **kwargs):
|
|
|
|
|
if key in eps_cache:
|
|
|
|
|
return eps_cache[key], eps_cache
|
|
|
|
|
sigma = self.sigma(t) * x.new_ones([x.shape[0]])
|
|
|
|
|
eps = (
|
|
|
|
|
x - self.model(x, sigma, *args, **self.extra_args, **kwargs)
|
|
|
|
|
) / self.sigma(t)
|
|
|
|
|
if self.eps_callback is not None:
|
|
|
|
|
self.eps_callback()
|
|
|
|
|
return eps, {key: eps, **eps_cache}
|
|
|
|
|
|
|
|
|
|
def dpm_solver_1_step(self, x, t, t_next, eps_cache=None):
|
|
|
|
|
eps_cache = {} if eps_cache is None else eps_cache
|
|
|
|
|
h = t_next - t
|
|
|
|
|
eps, eps_cache = self.eps(eps_cache, "eps", x, t)
|
|
|
|
|
x_1 = x - self.sigma(t_next) * h.expm1() * eps
|
|
|
|
|
return x_1, eps_cache
|
|
|
|
|
|
|
|
|
|
def dpm_solver_2_step(self, x, t, t_next, r1=1 / 2, eps_cache=None):
|
|
|
|
|
eps_cache = {} if eps_cache is None else eps_cache
|
|
|
|
|
h = t_next - t
|
|
|
|
|
eps, eps_cache = self.eps(eps_cache, "eps", x, t)
|
|
|
|
|
s1 = t + r1 * h
|
|
|
|
|
u1 = x - self.sigma(s1) * (r1 * h).expm1() * eps
|
|
|
|
|
eps_r1, eps_cache = self.eps(eps_cache, "eps_r1", u1, s1)
|
|
|
|
|
x_2 = (
|
|
|
|
|
x
|
|
|
|
|
- self.sigma(t_next) * h.expm1() * eps
|
|
|
|
|
- self.sigma(t_next) / (2 * r1) * h.expm1() * (eps_r1 - eps)
|
|
|
|
|
)
|
|
|
|
|
return x_2, eps_cache
|
|
|
|
|
|
|
|
|
|
def dpm_solver_3_step(self, x, t, t_next, r1=1 / 3, r2=2 / 3, eps_cache=None):
|
|
|
|
|
eps_cache = {} if eps_cache is None else eps_cache
|
|
|
|
|
h = t_next - t
|
|
|
|
|
eps, eps_cache = self.eps(eps_cache, "eps", x, t)
|
|
|
|
|
s1 = t + r1 * h
|
|
|
|
|
s2 = t + r2 * h
|
|
|
|
|
u1 = x - self.sigma(s1) * (r1 * h).expm1() * eps
|
|
|
|
|
eps_r1, eps_cache = self.eps(eps_cache, "eps_r1", u1, s1)
|
|
|
|
|
u2 = (
|
|
|
|
|
x
|
|
|
|
|
- self.sigma(s2) * (r2 * h).expm1() * eps
|
|
|
|
|
- self.sigma(s2)
|
|
|
|
|
* (r2 / r1)
|
|
|
|
|
* ((r2 * h).expm1() / (r2 * h) - 1)
|
|
|
|
|
* (eps_r1 - eps)
|
|
|
|
|
)
|
|
|
|
|
eps_r2, eps_cache = self.eps(eps_cache, "eps_r2", u2, s2)
|
|
|
|
|
x_3 = (
|
|
|
|
|
x
|
|
|
|
|
- self.sigma(t_next) * h.expm1() * eps
|
|
|
|
|
- self.sigma(t_next) / r2 * (h.expm1() / h - 1) * (eps_r2 - eps)
|
|
|
|
|
)
|
|
|
|
|
return x_3, eps_cache
|
|
|
|
|
|
|
|
|
|
def dpm_solver_fast(self, x, t_start, t_end, nfe, eta=0.0, s_noise=1.0):
|
|
|
|
|
if not t_end > t_start and eta:
|
|
|
|
|
raise ValueError("eta must be 0 for reverse sampling")
|
|
|
|
|
|
|
|
|
|
m = math.floor(nfe / 3) + 1
|
|
|
|
|
ts = torch.linspace(t_start, t_end, m + 1, device=x.device)
|
|
|
|
|
|
|
|
|
|
if nfe % 3 == 0:
|
|
|
|
|
orders = [3] * (m - 2) + [2, 1]
|
|
|
|
|
else:
|
|
|
|
|
orders = [3] * (m - 1) + [nfe % 3]
|
|
|
|
|
|
|
|
|
|
for i in range(len(orders)):
|
|
|
|
|
eps_cache = {}
|
|
|
|
|
t, t_next = ts[i], ts[i + 1]
|
|
|
|
|
if eta:
|
|
|
|
|
sd, su = get_ancestral_step(self.sigma(t), self.sigma(t_next), eta)
|
|
|
|
|
t_next_ = torch.minimum(t_end, self.t(sd))
|
|
|
|
|
su = (self.sigma(t_next) ** 2 - self.sigma(t_next_) ** 2) ** 0.5
|
|
|
|
|
else:
|
|
|
|
|
t_next_, su = t_next, 0.0
|
|
|
|
|
|
|
|
|
|
eps, eps_cache = self.eps(eps_cache, "eps", x, t)
|
|
|
|
|
denoised = x - self.sigma(t) * eps
|
|
|
|
|
if self.info_callback is not None:
|
|
|
|
|
self.info_callback(
|
|
|
|
|
{"x": x, "i": i, "t": ts[i], "t_up": t, "denoised": denoised}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if orders[i] == 1:
|
|
|
|
|
x, eps_cache = self.dpm_solver_1_step(
|
|
|
|
|
x, t, t_next_, eps_cache=eps_cache
|
|
|
|
|
)
|
|
|
|
|
elif orders[i] == 2:
|
|
|
|
|
x, eps_cache = self.dpm_solver_2_step(
|
|
|
|
|
x, t, t_next_, eps_cache=eps_cache
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
x, eps_cache = self.dpm_solver_3_step(
|
|
|
|
|
x, t, t_next_, eps_cache=eps_cache
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
x = x + su * s_noise * torch.randn_like(x)
|
|
|
|
|
|
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
def dpm_solver_adaptive(
|
|
|
|
|
self,
|
|
|
|
|
x,
|
|
|
|
|
t_start,
|
|
|
|
|
t_end,
|
|
|
|
|
order=3,
|
|
|
|
|
rtol=0.05,
|
|
|
|
|
atol=0.0078,
|
|
|
|
|
h_init=0.05,
|
|
|
|
|
pcoeff=0.0,
|
|
|
|
|
icoeff=1.0,
|
|
|
|
|
dcoeff=0.0,
|
|
|
|
|
accept_safety=0.81,
|
|
|
|
|
eta=0.0,
|
|
|
|
|
s_noise=1.0,
|
|
|
|
|
):
|
|
|
|
|
if order not in {2, 3}:
|
|
|
|
|
raise ValueError("order should be 2 or 3")
|
|
|
|
|
forward = t_end > t_start
|
|
|
|
|
if not forward and eta:
|
|
|
|
|
raise ValueError("eta must be 0 for reverse sampling")
|
|
|
|
|
h_init = abs(h_init) * (1 if forward else -1)
|
|
|
|
|
atol = torch.tensor(atol)
|
|
|
|
|
rtol = torch.tensor(rtol)
|
|
|
|
|
s = t_start
|
|
|
|
|
x_prev = x
|
|
|
|
|
accept = True
|
|
|
|
|
pid = PIDStepSizeController(
|
|
|
|
|
h_init, pcoeff, icoeff, dcoeff, 1.5 if eta else order, accept_safety
|
|
|
|
|
)
|
|
|
|
|
info = {"steps": 0, "nfe": 0, "n_accept": 0, "n_reject": 0}
|
|
|
|
|
|
|
|
|
|
while s < t_end - 1e-5 if forward else s > t_end + 1e-5:
|
|
|
|
|
eps_cache = {}
|
|
|
|
|
t = (
|
|
|
|
|
torch.minimum(t_end, s + pid.h)
|
|
|
|
|
if forward
|
|
|
|
|
else torch.maximum(t_end, s + pid.h)
|
|
|
|
|
)
|
|
|
|
|
if eta:
|
|
|
|
|
sd, su = get_ancestral_step(self.sigma(s), self.sigma(t), eta)
|
|
|
|
|
t_ = torch.minimum(t_end, self.t(sd))
|
|
|
|
|
su = (self.sigma(t) ** 2 - self.sigma(t_) ** 2) ** 0.5
|
|
|
|
|
else:
|
|
|
|
|
t_, su = t, 0.0
|
|
|
|
|
|
|
|
|
|
eps, eps_cache = self.eps(eps_cache, "eps", x, s)
|
|
|
|
|
denoised = x - self.sigma(s) * eps
|
|
|
|
|
|
|
|
|
|
if order == 2:
|
|
|
|
|
x_low, eps_cache = self.dpm_solver_1_step(x, s, t_, eps_cache=eps_cache)
|
|
|
|
|
x_high, eps_cache = self.dpm_solver_2_step(
|
|
|
|
|
x, s, t_, eps_cache=eps_cache
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
x_low, eps_cache = self.dpm_solver_2_step(
|
|
|
|
|
x, s, t_, r1=1 / 3, eps_cache=eps_cache
|
|
|
|
|
)
|
|
|
|
|
x_high, eps_cache = self.dpm_solver_3_step(
|
|
|
|
|
x, s, t_, eps_cache=eps_cache
|
|
|
|
|
)
|
|
|
|
|
delta = torch.maximum(atol, rtol * torch.maximum(x_low.abs(), x_prev.abs()))
|
|
|
|
|
error = torch.linalg.norm((x_low - x_high) / delta) / x.numel() ** 0.5
|
|
|
|
|
accept = pid.propose_step(error)
|
|
|
|
|
if accept:
|
|
|
|
|
x_prev = x_low
|
|
|
|
|
x = x_high + su * s_noise * torch.randn_like(x_high)
|
|
|
|
|
s = t
|
|
|
|
|
info["n_accept"] += 1
|
|
|
|
|
else:
|
|
|
|
|
info["n_reject"] += 1
|
|
|
|
|
info["nfe"] += order
|
|
|
|
|
info["steps"] += 1
|
|
|
|
|
|
|
|
|
|
if self.info_callback is not None:
|
|
|
|
|
self.info_callback(
|
|
|
|
|
{
|
|
|
|
|
"x": x,
|
|
|
|
|
"i": info["steps"] - 1,
|
|
|
|
|
"t": s,
|
|
|
|
|
"t_up": s,
|
|
|
|
|
"denoised": denoised,
|
|
|
|
|
"error": error,
|
|
|
|
|
"h": pid.h,
|
|
|
|
|
**info,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return x, info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@torch.no_grad()
|
|
|
|
|
def sample_dpm_fast(
|
|
|
|
|
model,
|
|
|
|
|
x,
|
|
|
|
|
sigma_min,
|
|
|
|
|
sigma_max,
|
|
|
|
|
n,
|
|
|
|
|
extra_args=None,
|
|
|
|
|
callback=None,
|
|
|
|
|
disable=None,
|
|
|
|
|
eta=0.0,
|
|
|
|
|
s_noise=1.0,
|
|
|
|
|
):
|
|
|
|
|
"""DPM-Solver-Fast (fixed step size). See https://arxiv.org/abs/2206.00927."""
|
|
|
|
|
if sigma_min <= 0 or sigma_max <= 0:
|
|
|
|
|
raise ValueError("sigma_min and sigma_max must not be 0")
|
|
|
|
|
with tqdm(total=n, disable=disable) as pbar:
|
|
|
|
|
dpm_solver = DPMSolver(model, extra_args, eps_callback=pbar.update)
|
|
|
|
|
if callback is not None:
|
|
|
|
|
dpm_solver.info_callback = lambda info: callback(
|
|
|
|
|
{
|
|
|
|
|
"sigma": dpm_solver.sigma(info["t"]),
|
|
|
|
|
"sigma_hat": dpm_solver.sigma(info["t_up"]),
|
|
|
|
|
**info,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@torch.no_grad()
|
|
|
|
|
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.0,
|
|
|
|
|
icoeff=1.0,
|
|
|
|
|
dcoeff=0.0,
|
|
|
|
|
accept_safety=0.81,
|
|
|
|
|
eta=0.0,
|
|
|
|
|
s_noise=1.0,
|
|
|
|
|
return_info=False,
|
|
|
|
|
):
|
|
|
|
|
"""DPM-Solver-12 and 23 (adaptive step size). See https://arxiv.org/abs/2206.00927."""
|
|
|
|
|
if sigma_min <= 0 or sigma_max <= 0:
|
|
|
|
|
raise ValueError("sigma_min and sigma_max must not be 0")
|
|
|
|
|
with tqdm(disable=disable) as pbar:
|
|
|
|
|
dpm_solver = DPMSolver(model, extra_args, eps_callback=pbar.update)
|
|
|
|
|
if callback is not None:
|
|
|
|
|
dpm_solver.info_callback = lambda info: callback(
|
|
|
|
|
{
|
|
|
|
|
"sigma": dpm_solver.sigma(info["t"]),
|
|
|
|
|
"sigma_hat": dpm_solver.sigma(info["t_up"]),
|
|
|
|
|
**info,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
if return_info:
|
|
|
|
|
return x, info
|
|
|
|
|
return x
|
|
|
|
|