The adaptive step size solver based on singlestep DPM-Solver. Args: x: A pytorch tensor. The initial value at time `t_T`. order: A `int`. The (higher) order of the solver. We only support order == 2 or 3. t_T: A `float`. The starting time of the
(self, x, order, t_T, t_0, h_init=0.05, atol=0.0078, rtol=0.05, theta=0.9, t_err=1e-5, solver_type='dpmsolver')
| 962 | raise ValueError("Solver order must be 1 or 2 or 3, got {}".format(order)) |
| 963 | |
| 964 | def dpm_solver_adaptive(self, x, order, t_T, t_0, h_init=0.05, atol=0.0078, rtol=0.05, theta=0.9, t_err=1e-5, solver_type='dpmsolver'): |
| 965 | """ |
| 966 | The adaptive step size solver based on singlestep DPM-Solver. |
| 967 | |
| 968 | Args: |
| 969 | x: A pytorch tensor. The initial value at time `t_T`. |
| 970 | order: A `int`. The (higher) order of the solver. We only support order == 2 or 3. |
| 971 | t_T: A `float`. The starting time of the sampling (default is T). |
| 972 | t_0: A `float`. The ending time of the sampling (default is epsilon). |
| 973 | h_init: A `float`. The initial step size (for logSNR). |
| 974 | atol: A `float`. The absolute tolerance of the solver. For image data, the default setting is 0.0078, followed [1]. |
| 975 | rtol: A `float`. The relative tolerance of the solver. The default setting is 0.05. |
| 976 | theta: A `float`. The safety hyperparameter for adapting the step size. The default setting is 0.9, followed [1]. |
| 977 | t_err: A `float`. The tolerance for the time. We solve the diffusion ODE until the absolute error between the |
| 978 | current time and `t_0` is less than `t_err`. The default setting is 1e-5. |
| 979 | solver_type: either 'dpmsolver' or 'taylor'. The type for the high-order solvers. |
| 980 | The type slightly impacts the performance. We recommend to use 'dpmsolver' type. |
| 981 | Returns: |
| 982 | x_0: A pytorch tensor. The approximated solution at time `t_0`. |
| 983 | |
| 984 | [1] A. Jolicoeur-Martineau, K. Li, R. Piché-Taillefer, T. Kachman, and I. Mitliagkas, "Gotta go fast when generating data with score-based models," arXiv preprint arXiv:2105.14080, 2021. |
| 985 | """ |
| 986 | ns = self.noise_schedule |
| 987 | s = t_T * torch.ones((1,)).to(x) |
| 988 | lambda_s = ns.marginal_lambda(s) |
| 989 | lambda_0 = ns.marginal_lambda(t_0 * torch.ones_like(s).to(x)) |
| 990 | h = h_init * torch.ones_like(s).to(x) |
| 991 | x_prev = x |
| 992 | nfe = 0 |
| 993 | if order == 2: |
| 994 | r1 = 0.5 |
| 995 | lower_update = lambda x, s, t: self.dpm_solver_first_update(x, s, t, return_intermediate=True) |
| 996 | higher_update = lambda x, s, t, **kwargs: self.singlestep_dpm_solver_second_update(x, s, t, r1=r1, solver_type=solver_type, **kwargs) |
| 997 | elif order == 3: |
| 998 | r1, r2 = 1. / 3., 2. / 3. |
| 999 | lower_update = lambda x, s, t: self.singlestep_dpm_solver_second_update(x, s, t, r1=r1, return_intermediate=True, solver_type=solver_type) |
| 1000 | higher_update = lambda x, s, t, **kwargs: self.singlestep_dpm_solver_third_update(x, s, t, r1=r1, r2=r2, solver_type=solver_type, **kwargs) |
| 1001 | else: |
| 1002 | raise ValueError("For adaptive step size solver, order must be 2 or 3, got {}".format(order)) |
| 1003 | while torch.abs((s - t_0)).mean() > t_err: |
| 1004 | t = ns.inverse_lambda(lambda_s + h) |
| 1005 | x_lower, lower_noise_kwargs = lower_update(x, s, t) |
| 1006 | x_higher = higher_update(x, s, t, **lower_noise_kwargs) |
| 1007 | delta = torch.max(torch.ones_like(x).to(x) * atol, rtol * torch.max(torch.abs(x_lower), torch.abs(x_prev))) |
| 1008 | norm_fn = lambda v: torch.sqrt(torch.square(v.reshape((v.shape[0], -1))).mean(dim=-1, keepdim=True)) |
| 1009 | E = norm_fn((x_higher - x_lower) / delta).max() |
| 1010 | if torch.all(E <= 1.): |
| 1011 | x = x_higher |
| 1012 | s = t |
| 1013 | x_prev = x_lower |
| 1014 | lambda_s = ns.marginal_lambda(s) |
| 1015 | h = torch.min(theta * h * torch.float_power(E, -1. / order).float(), lambda_0 - lambda_s) |
| 1016 | nfe += order |
| 1017 | print('adaptive solver nfe', nfe) |
| 1018 | return x |
| 1019 | |
| 1020 | def add_noise(self, x, t, noise=None): |
| 1021 | """ |
no test coverage detected