Compute the sample at time `t_end` by DPM-Solver, given the initial `x` at time `t_start`. ===================================================== We support the following algorithms for both noise prediction model and data prediction model: - 'singlestep':
(self, x, steps=20, t_start=None, t_end=None, order=3, skip_type='time_uniform',
method='singlestep', lower_order_final=True, denoise_to_zero=False, solver_type='dpm_solver',
atol=0.0078, rtol=0.05,
)
| 937 | return x |
| 938 | |
| 939 | def sample(self, x, steps=20, t_start=None, t_end=None, order=3, skip_type='time_uniform', |
| 940 | method='singlestep', lower_order_final=True, denoise_to_zero=False, solver_type='dpm_solver', |
| 941 | atol=0.0078, rtol=0.05, |
| 942 | ): |
| 943 | """ |
| 944 | Compute the sample at time `t_end` by DPM-Solver, given the initial `x` at time `t_start`. |
| 945 | ===================================================== |
| 946 | We support the following algorithms for both noise prediction model and data prediction model: |
| 947 | - 'singlestep': |
| 948 | Singlestep DPM-Solver (i.e. "DPM-Solver-fast" in the paper), which combines different orders of singlestep DPM-Solver. |
| 949 | We combine all the singlestep solvers with order <= `order` to use up all the function evaluations (steps). |
| 950 | The total number of function evaluations (NFE) == `steps`. |
| 951 | Given a fixed NFE == `steps`, the sampling procedure is: |
| 952 | - If `order` == 1: |
| 953 | - Denote K = steps. We use K steps of DPM-Solver-1 (i.e. DDIM). |
| 954 | - If `order` == 2: |
| 955 | - Denote K = (steps // 2) + (steps % 2). We take K intermediate time steps for sampling. |
| 956 | - If steps % 2 == 0, we use K steps of singlestep DPM-Solver-2. |
| 957 | - If steps % 2 == 1, we use (K - 1) steps of singlestep DPM-Solver-2 and 1 step of DPM-Solver-1. |
| 958 | - If `order` == 3: |
| 959 | - Denote K = (steps // 3 + 1). We take K intermediate time steps for sampling. |
| 960 | - If steps % 3 == 0, we use (K - 2) steps of singlestep DPM-Solver-3, and 1 step of singlestep DPM-Solver-2 and 1 step of DPM-Solver-1. |
| 961 | - If steps % 3 == 1, we use (K - 1) steps of singlestep DPM-Solver-3 and 1 step of DPM-Solver-1. |
| 962 | - If steps % 3 == 2, we use (K - 1) steps of singlestep DPM-Solver-3 and 1 step of singlestep DPM-Solver-2. |
| 963 | - 'multistep': |
| 964 | Multistep DPM-Solver with the order of `order`. The total number of function evaluations (NFE) == `steps`. |
| 965 | We initialize the first `order` values by lower order multistep solvers. |
| 966 | Given a fixed NFE == `steps`, the sampling procedure is: |
| 967 | Denote K = steps. |
| 968 | - If `order` == 1: |
| 969 | - We use K steps of DPM-Solver-1 (i.e. DDIM). |
| 970 | - If `order` == 2: |
| 971 | - We firstly use 1 step of DPM-Solver-1, then use (K - 1) step of multistep DPM-Solver-2. |
| 972 | - If `order` == 3: |
| 973 | - We firstly use 1 step of DPM-Solver-1, then 1 step of multistep DPM-Solver-2, then (K - 2) step of multistep DPM-Solver-3. |
| 974 | - 'singlestep_fixed': |
| 975 | Fixed order singlestep DPM-Solver (i.e. DPM-Solver-1 or singlestep DPM-Solver-2 or singlestep DPM-Solver-3). |
| 976 | We use singlestep DPM-Solver-`order` for `order`=1 or 2 or 3, with total [`steps` // `order`] * `order` NFE. |
| 977 | - 'adaptive': |
| 978 | Adaptive step size DPM-Solver (i.e. "DPM-Solver-12" and "DPM-Solver-23" in the paper). |
| 979 | We ignore `steps` and use adaptive step size DPM-Solver with a higher order of `order`. |
| 980 | You can adjust the absolute tolerance `atol` and the relative tolerance `rtol` to balance the computatation costs |
| 981 | (NFE) and the sample quality. |
| 982 | - If `order` == 2, we use DPM-Solver-12 which combines DPM-Solver-1 and singlestep DPM-Solver-2. |
| 983 | - If `order` == 3, we use DPM-Solver-23 which combines singlestep DPM-Solver-2 and singlestep DPM-Solver-3. |
| 984 | ===================================================== |
| 985 | Some advices for choosing the algorithm: |
| 986 | - For **unconditional sampling** or **guided sampling with small guidance scale** by DPMs: |
| 987 | Use singlestep DPM-Solver ("DPM-Solver-fast" in the paper) with `order = 3`. |
| 988 | e.g. |
| 989 | >>> dpm_solver = DPM_Solver(model_fn, noise_schedule, predict_x0=False) |
| 990 | >>> x_sample = dpm_solver.sample(x, steps=steps, t_start=t_start, t_end=t_end, order=3, |
| 991 | skip_type='time_uniform', method='singlestep') |
| 992 | - For **guided sampling with large guidance scale** by DPMs: |
| 993 | Use multistep DPM-Solver with `predict_x0 = True` and `order = 2`. |
| 994 | e.g. |
| 995 | >>> dpm_solver = DPM_Solver(model_fn, noise_schedule, predict_x0=True) |
| 996 | >>> x_sample = dpm_solver.sample(x, steps=steps, t_start=t_start, t_end=t_end, order=2, |
no test coverage detected