MCPcopy Create free account
hub / github.com/294coder/Dif-PAN / singlestep_dpm_solver_third_update

Method singlestep_dpm_solver_third_update

solver/dpm_solver.py:683–802  ·  view source on GitHub ↗

Singlestep solver DPM-Solver-3 from time `s` to time `t`. Args: x: A pytorch tensor. The initial value at time `s`. s: A pytorch tensor. The starting time, with the shape (1,). t: A pytorch tensor. The ending time, with the shape (1,).

(self, x, s, t, r1=1./3., r2=2./3., model_s=None, model_s1=None, return_intermediate=False, solver_type='dpmsolver')

Source from the content-addressed store, hash-verified

681 return x_t
682
683 def singlestep_dpm_solver_third_update(self, x, s, t, r1=1./3., r2=2./3., model_s=None, model_s1=None, return_intermediate=False, solver_type='dpmsolver'):
684 """
685 Singlestep solver DPM-Solver-3 from time `s` to time `t`.
686
687 Args:
688 x: A pytorch tensor. The initial value at time `s`.
689 s: A pytorch tensor. The starting time, with the shape (1,).
690 t: A pytorch tensor. The ending time, with the shape (1,).
691 r1: A `float`. The hyperparameter of the third-order solver.
692 r2: A `float`. The hyperparameter of the third-order solver.
693 model_s: A pytorch tensor. The model function evaluated at time `s`.
694 If `model_s` is None, we evaluate the model by `x` and `s`; otherwise we directly use it.
695 model_s1: A pytorch tensor. The model function evaluated at time `s1` (the intermediate time given by `r1`).
696 If `model_s1` is None, we evaluate the model at `s1`; otherwise we directly use it.
697 return_intermediate: A `bool`. If true, also return the model value at time `s`, `s1` and `s2` (the intermediate times).
698 solver_type: either 'dpmsolver' or 'taylor'. The type for the high-order solvers.
699 The type slightly impacts the performance. We recommend to use 'dpmsolver' type.
700 Returns:
701 x_t: A pytorch tensor. The approximated solution at time `t`.
702 """
703 if solver_type not in ['dpmsolver', 'taylor']:
704 raise ValueError("'solver_type' must be either 'dpmsolver' or 'taylor', got {}".format(solver_type))
705 if r1 is None:
706 r1 = 1. / 3.
707 if r2 is None:
708 r2 = 2. / 3.
709 ns = self.noise_schedule
710 lambda_s, lambda_t = ns.marginal_lambda(s), ns.marginal_lambda(t)
711 h = lambda_t - lambda_s
712 lambda_s1 = lambda_s + r1 * h
713 lambda_s2 = lambda_s + r2 * h
714 s1 = ns.inverse_lambda(lambda_s1)
715 s2 = ns.inverse_lambda(lambda_s2)
716 log_alpha_s, log_alpha_s1, log_alpha_s2, log_alpha_t = ns.marginal_log_mean_coeff(s), ns.marginal_log_mean_coeff(s1), ns.marginal_log_mean_coeff(s2), ns.marginal_log_mean_coeff(t)
717 sigma_s, sigma_s1, sigma_s2, sigma_t = ns.marginal_std(s), ns.marginal_std(s1), ns.marginal_std(s2), ns.marginal_std(t)
718 alpha_s1, alpha_s2, alpha_t = torch.exp(log_alpha_s1), torch.exp(log_alpha_s2), torch.exp(log_alpha_t)
719
720 if self.algorithm_type == "dpmsolver++":
721 phi_11 = torch.expm1(-r1 * h)
722 phi_12 = torch.expm1(-r2 * h)
723 phi_1 = torch.expm1(-h)
724 phi_22 = torch.expm1(-r2 * h) / (r2 * h) + 1.
725 phi_2 = phi_1 / h + 1.
726 phi_3 = phi_2 / h - 0.5
727
728 if model_s is None:
729 model_s = self.model_fn(x, s)
730 if model_s1 is None:
731 x_s1 = (
732 (sigma_s1 / sigma_s) * x
733 - (alpha_s1 * phi_11) * model_s
734 )
735 model_s1 = self.model_fn(x_s1, s1)
736 x_s2 = (
737 (sigma_s2 / sigma_s) * x
738 - (alpha_s2 * phi_12) * model_s
739 + r2 / r1 * (alpha_s2 * phi_22) * (model_s1 - model_s)
740 )

Callers 2

dpm_solver_adaptiveMethod · 0.95

Calls 5

model_fnMethod · 0.95
marginal_lambdaMethod · 0.80
inverse_lambdaMethod · 0.80
marginal_stdMethod · 0.80

Tested by

no test coverage detected