UniPCMultistepScheduler · Hugging Face (original) (raw)

UniPCMultistepScheduler is a training-free framework designed for fast sampling of diffusion models. It was introduced in UniPC: A Unified Predictor-Corrector Framework for Fast Sampling of Diffusion Models by Wenliang Zhao, Lujia Bai, Yongming Rao, Jie Zhou, Jiwen Lu.

It consists of a corrector (UniC) and a predictor (UniP) that share a unified analytical form and support arbitrary orders. UniPC is by design model-agnostic, supporting pixel-space/latent-space DPMs on unconditional/conditional sampling. It can also be applied to both noise prediction and data prediction models. The corrector UniC can be also applied after any off-the-shelf solvers to increase the order of accuracy.

The abstract from the paper is:

Diffusion probabilistic models (DPMs) have demonstrated a very promising ability in high-resolution image synthesis. However, sampling from a pre-trained DPM is time-consuming due to the multiple evaluations of the denoising network, making it more and more important to accelerate the sampling of DPMs. Despite recent progress in designing fast samplers, existing methods still cannot generate satisfying images in many applications where fewer steps (e.g., <10) are favored. In this paper, we develop a unified corrector (UniC) that can be applied after any existing DPM sampler to increase the order of accuracy without extra model evaluations, and derive a unified predictor (UniP) that supports arbitrary order as a byproduct. Combining UniP and UniC, we propose a unified predictor-corrector framework called UniPC for the fast sampling of DPMs, which has a unified analytical form for any order and can significantly improve the sampling quality over previous methods, especially in extremely few steps. We evaluate our methods through extensive experiments including both unconditional and conditional sampling using pixel-space and latent-space DPMs. Our UniPC can achieve 3.87 FID on CIFAR10 (unconditional) and 7.51 FID on ImageNet 256×256 (conditional) with only 10 function evaluations. Code is available at this https URL.

Tips

It is recommended to set solver_order to 2 for guide sampling, and solver_order=3 for unconditional sampling.

Dynamic thresholding from Imagen is supported, and for pixel-space diffusion models, you can set both predict_x0=True and thresholding=True to use dynamic thresholding. This thresholding method is unsuitable for latent-space diffusion models such as Stable Diffusion.

UniPCMultistepScheduler

class diffusers.UniPCMultistepScheduler

< source >

( num_train_timesteps: int = 1000 beta_start: float = 0.0001 beta_end: float = 0.02 beta_schedule: str = 'linear' trained_betas: numpy.ndarray | list[float] | None = None solver_order: int = 2 prediction_type: typing.Literal['epsilon', 'sample', 'v_prediction', 'flow_prediction'] = 'epsilon' thresholding: bool = False dynamic_thresholding_ratio: float = 0.995 sample_max_value: float = 1.0 predict_x0: bool = True solver_type: typing.Literal['bh1', 'bh2'] = 'bh2' lower_order_final: bool = True disable_corrector: list = [] solver_p: SchedulerMixin = None use_karras_sigmas: bool = False use_exponential_sigmas: bool = False use_beta_sigmas: bool = False use_flow_sigmas: bool = False flow_shift: float = 1.0 timestep_spacing: typing.Literal['linspace', 'leading', 'trailing'] = 'linspace' steps_offset: int = 0 final_sigmas_type: typing.Literal['zero', 'sigma_min'] = 'zero' rescale_betas_zero_snr: bool = False use_dynamic_shifting: bool = False time_shift_type: typing.Literal['exponential'] = 'exponential' sigma_min: float | None = None sigma_max: bool | None = None shift_terminal: bool | None = None )

Parameters

UniPCMultistepScheduler is a training-free framework designed for the fast sampling of diffusion models.

This model inherits from SchedulerMixin and ConfigMixin. Check the superclass documentation for the generic methods the library implements for all schedulers such as loading and saving.

add_noise

< source >

( original_samples: Tensor noise: Tensor timesteps: IntTensor ) → torch.Tensor

Parameters

The noisy samples.

Add noise to the original samples according to the noise schedule at the specified timesteps.

convert_model_output

< source >

( model_output: Tensor *args sample: Tensor = None **kwargs ) → torch.Tensor

Parameters

The converted model output.

Convert the model output to the corresponding type the UniPC algorithm needs.

index_for_timestep

< source >

( timestep: int | torch.Tensor schedule_timesteps: torch.Tensor | None = None ) → int

Parameters

The index of the timestep in the schedule.

Find the index for a given timestep in the schedule.

multistep_uni_c_bh_update

< source >

( this_model_output: Tensor *args last_sample: Tensor = None this_sample: Tensor = None order: int = None **kwargs ) → torch.Tensor

Parameters

The corrected sample tensor at the current timestep.

One step for the UniC (B(h) version).

multistep_uni_p_bh_update

< source >

( model_output: Tensor *args sample: Tensor = None order: int = None **kwargs ) → torch.Tensor

Parameters

The sample tensor at the previous timestep.

One step for the UniP (B(h) version). Alternatively, self.solver_p is used if is specified.

scale_model_input

< source >

( sample: Tensor *args **kwargs ) → torch.Tensor

Parameters

A scaled input sample.

Ensures interchangeability with schedulers that need to scale the denoising model input depending on the current timestep.

set_begin_index

< source >

( begin_index: int = 0 )

Parameters

Sets the begin index for the scheduler. This function should be run from pipeline before the inference.

set_timesteps

< source >

( num_inference_steps: int | None = None device: str | torch.device = None sigmas: list[float] | None = None mu: bool | None = None )

Parameters

Sets the discrete timesteps used for the diffusion chain (to be run before inference).

step

< source >

( model_output: Tensor timestep: int | torch.Tensor sample: Tensor return_dict: bool = True ) → SchedulerOutput or tuple

Parameters

Returns

SchedulerOutput or tuple

If return_dict is True, SchedulerOutput is returned, otherwise a tuple is returned where the first element is the sample tensor.

Predict the sample from the previous timestep by reversing the SDE. This function propagates the sample with the multistep UniPC.

time_shift

< source >

( mu: float sigma: float t: Tensor ) → torch.Tensor

Parameters

The time-shifted timesteps.

Apply time shifting to the sigmas.

SchedulerOutput

class diffusers.schedulers.scheduling_utils.SchedulerOutput

< source >

( prev_sample: Tensor )

Parameters

Base class for the output of a scheduler’s step function.

Update on GitHub