Singlestep DPMSolver
Overview
Original paper can be found here and the improved version. The original implementation can be found here.
DPMSolverSinglestepScheduler
class diffusers.DPMSolverSinglestepScheduler
< source >( num_train_timesteps: int = 1000 beta_start: float = 0.0001 beta_end: float = 0.02 beta_schedule: str = 'linear' trained_betas: typing.Optional[numpy.ndarray] = None solver_order: int = 2 prediction_type: str = 'epsilon' thresholding: bool = False dynamic_thresholding_ratio: float = 0.995 sample_max_value: float = 1.0 algorithm_type: str = 'dpmsolver++' solver_type: str = 'midpoint' lower_order_final: bool = True )
Parameters

num_train_timesteps (
int
) — number of diffusion steps used to train the model. 
beta_start (
float
) — the startingbeta
value of inference. 
beta_end (
float
) — the finalbeta
value. 
beta_schedule (
str
) — the beta schedule, a mapping from a beta range to a sequence of betas for stepping the model. Choose fromlinear
,scaled_linear
, orsquaredcos_cap_v2
. 
trained_betas (
np.ndarray
, optional) — option to pass an array of betas directly to the constructor to bypassbeta_start
,beta_end
etc. 
solver_order (
int
, default2
) — the order of DPMSolver; can be1
or2
or3
. We recommend to usesolver_order=2
for guided sampling, andsolver_order=3
for unconditional sampling. 
prediction_type (
str
, defaultepsilon
) — indicates whether the model predicts the noise (epsilon), or the data /x0
. One ofepsilon
,sample
, orvprediction
. 
thresholding (
bool
, defaultFalse
) — whether to use the “dynamic thresholding” method (introduced by Imagen, https://arxiv.org/abs/2205.11487). For pixelspace diffusion models, you can set bothalgorithm_type=dpmsolver++
andthresholding=True
to use the dynamic thresholding. Note that the thresholding method is unsuitable for latentspace diffusion models (such as stablediffusion). 
dynamic_thresholding_ratio (
float
, default0.995
) — the ratio for the dynamic thresholding method. Default is0.995
, the same as Imagen (https://arxiv.org/abs/2205.11487). 
sample_max_value (
float
, default1.0
) — the threshold value for dynamic thresholding. Valid only whenthresholding=True
andalgorithm_type="dpmsolver++
. 
algorithm_type (
str
, defaultdpmsolver++
) — the algorithm type for the solver. Eitherdpmsolver
ordpmsolver++
. Thedpmsolver
type implements the algorithms in https://arxiv.org/abs/2206.00927, and thedpmsolver++
type implements the algorithms in https://arxiv.org/abs/2211.01095. We recommend to usedpmsolver++
withsolver_order=2
for guided sampling (e.g. stablediffusion). 
solver_type (
str
, defaultmidpoint
) — the solver type for the secondorder solver. Eithermidpoint
orheun
. The solver type slightly affects the sample quality, especially for small number of steps. We empirically find thatmidpoint
solvers are slightly better, so we recommend to use themidpoint
type. 
lower_order_final (
bool
, defaultTrue
) — whether to use lowerorder solvers in the final steps. For singlestep schedulers, we recommend to enable this to use up all the function evaluations.
DPMSolver (and the improved version DPMSolver++) is a fast dedicated highorder solver for diffusion ODEs with the convergence order guarantee. Empirically, sampling by DPMSolver with only 20 steps can generate highquality samples, and it can generate quite good samples even in only 10 steps.
For more details, see the original paper: https://arxiv.org/abs/2206.00927 and https://arxiv.org/abs/2211.01095
Currently, we support the singlestep DPMSolver for both noise prediction models and data prediction models. We
recommend to use solver_order=2
for guided sampling, and solver_order=3
for unconditional sampling.
We also support the “dynamic thresholding” method in Imagen (https://arxiv.org/abs/2205.11487). For pixelspace
diffusion models, you can set both algorithm_type="dpmsolver++"
and thresholding=True
to use the dynamic
thresholding. Note that the thresholding method is unsuitable for latentspace diffusion models (such as
stablediffusion).
~ConfigMixin takes care of storing all config attributes that are passed in the scheduler’s __init__
function, such as num_train_timesteps
. They can be accessed via scheduler.config.num_train_timesteps
.
SchedulerMixin provides general loading and saving functionality via the SchedulerMixin.save_pretrained() and
from_pretrained() functions.
convert_model_output
< source >(
model_output: FloatTensor
timestep: int
sample: FloatTensor
)
→
torch.FloatTensor
Parameters

model_output (
torch.FloatTensor
) — direct output from learned diffusion model. 
timestep (
int
) — current discrete timestep in the diffusion chain. 
sample (
torch.FloatTensor
) — current instance of sample being created by diffusion process.
Returns
torch.FloatTensor
the converted model output.
Convert the model output to the corresponding type that the algorithm (DPMSolver / DPMSolver++) needs.
DPMSolver is designed to discretize an integral of the noise prediction model, and DPMSolver++ is designed to discretize an integral of the data prediction model. So we need to first convert the model output to the corresponding type to match the algorithm.
Note that the algorithm type and the model type is decoupled. That is to say, we can use either DPMSolver or DPMSolver++ for both noise prediction model and data prediction model.
dpm_solver_first_order_update
< source >(
model_output: FloatTensor
timestep: int
prev_timestep: int
sample: FloatTensor
)
→
torch.FloatTensor
Parameters

model_output (
torch.FloatTensor
) — direct output from learned diffusion model. 
timestep (
int
) — current discrete timestep in the diffusion chain. 
prev_timestep (
int
) — previous discrete timestep in the diffusion chain. 
sample (
torch.FloatTensor
) — current instance of sample being created by diffusion process.
Returns
torch.FloatTensor
the sample tensor at the previous timestep.
One step for the firstorder DPMSolver (equivalent to DDIM).
See https://arxiv.org/abs/2206.00927 for the detailed derivation.
get_order_list
< source >( num_inference_steps: int )
Computes the solver order at each time step.
scale_model_input
< source >(
sample: FloatTensor
*args
**kwargs
)
→
torch.FloatTensor
Ensures interchangeability with schedulers that need to scale the denoising model input depending on the current timestep.
set_timesteps
< source >( num_inference_steps: int device: typing.Union[str, torch.device] = None )
Sets the timesteps used for the diffusion chain. Supporting function to be run before inference.
singlestep_dpm_solver_second_order_update
< source >(
model_output_list: typing.List[torch.FloatTensor]
timestep_list: typing.List[int]
prev_timestep: int
sample: FloatTensor
)
→
torch.FloatTensor
Parameters

model_output_list (
List[torch.FloatTensor]
) — direct outputs from learned diffusion model at current and latter timesteps. 
timestep (
int
) — current and latter discrete timestep in the diffusion chain. 
prev_timestep (
int
) — previous discrete timestep in the diffusion chain. 
sample (
torch.FloatTensor
) — current instance of sample being created by diffusion process.
Returns
torch.FloatTensor
the sample tensor at the previous timestep.
One step for the secondorder singlestep DPMSolver.
It computes the solution at time prev_timestep
from the time timestep_list[2]
.
singlestep_dpm_solver_third_order_update
< source >(
model_output_list: typing.List[torch.FloatTensor]
timestep_list: typing.List[int]
prev_timestep: int
sample: FloatTensor
)
→
torch.FloatTensor
Parameters

model_output_list (
List[torch.FloatTensor]
) — direct outputs from learned diffusion model at current and latter timesteps. 
timestep (
int
) — current and latter discrete timestep in the diffusion chain. 
prev_timestep (
int
) — previous discrete timestep in the diffusion chain. 
sample (
torch.FloatTensor
) — current instance of sample being created by diffusion process.
Returns
torch.FloatTensor
the sample tensor at the previous timestep.
One step for the thirdorder singlestep DPMSolver.
It computes the solution at time prev_timestep
from the time timestep_list[3]
.
singlestep_dpm_solver_update
< source >(
model_output_list: typing.List[torch.FloatTensor]
timestep_list: typing.List[int]
prev_timestep: int
sample: FloatTensor
order: int
)
→
torch.FloatTensor
Parameters

model_output_list (
List[torch.FloatTensor]
) — direct outputs from learned diffusion model at current and latter timesteps. 
timestep (
int
) — current and latter discrete timestep in the diffusion chain. 
prev_timestep (
int
) — previous discrete timestep in the diffusion chain. 
sample (
torch.FloatTensor
) — current instance of sample being created by diffusion process. 
order (
int
) — the solver order at this step.
Returns
torch.FloatTensor
the sample tensor at the previous timestep.
One step for the singlestep DPMSolver.
step
< source >(
model_output: FloatTensor
timestep: int
sample: FloatTensor
return_dict: bool = True
)
→
~scheduling_utils.SchedulerOutput
or tuple
Parameters

model_output (
torch.FloatTensor
) — direct output from learned diffusion model. 
timestep (
int
) — current discrete timestep in the diffusion chain. 
sample (
torch.FloatTensor
) — current instance of sample being created by diffusion process. 
return_dict (
bool
) — option for returning tuple rather than SchedulerOutput class
Returns
~scheduling_utils.SchedulerOutput
or tuple
~scheduling_utils.SchedulerOutput
if return_dict
is
True, otherwise a tuple
. When returning a tuple, the first element is the sample tensor.
Step function propagating the sample with the singlestep DPMSolver.