Diffusers documentation

CMStochasticIterativeScheduler

You are viewing v0.24.0 version. A newer version v0.31.0 is available.
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

CMStochasticIterativeScheduler

Consistency Models by Yang Song, Prafulla Dhariwal, Mark Chen, and Ilya Sutskever introduced a multistep and onestep scheduler (Algorithm 1) that is capable of generating good samples in one or a small number of steps.

The abstract from the paper is:

Diffusion models have significantly advanced the fields of image, audio, and video generation, but they depend on an iterative sampling process that causes slow generation. To overcome this limitation, we propose consistency models, a new family of models that generate high quality samples by directly mapping noise to data. They support fast one-step generation by design, while still allowing multistep sampling to trade compute for sample quality. They also support zero-shot data editing, such as image inpainting, colorization, and super-resolution, without requiring explicit training on these tasks. Consistency models can be trained either by distilling pre-trained diffusion models, or as standalone generative models altogether. Through extensive experiments, we demonstrate that they outperform existing distillation techniques for diffusion models in one- and few-step sampling, achieving the new state-of-the-art FID of 3.55 on CIFAR-10 and 6.20 on ImageNet 64x64 for one-step generation. When trained in isolation, consistency models become a new family of generative models that can outperform existing one-step, non-adversarial generative models on standard benchmarks such as CIFAR-10, ImageNet 64x64 and LSUN 256x256.

The original codebase can be found at openai/consistency_models.

CMStochasticIterativeScheduler

class diffusers.CMStochasticIterativeScheduler

< >

( num_train_timesteps: int = 40 sigma_min: float = 0.002 sigma_max: float = 80.0 sigma_data: float = 0.5 s_noise: float = 1.0 rho: float = 7.0 clip_denoised: bool = True )

Parameters

  • num_train_timesteps (int, defaults to 40) — The number of diffusion steps to train the model.
  • sigma_min (float, defaults to 0.002) — Minimum noise magnitude in the sigma schedule. Defaults to 0.002 from the original implementation.
  • sigma_max (float, defaults to 80.0) — Maximum noise magnitude in the sigma schedule. Defaults to 80.0 from the original implementation.
  • sigma_data (float, defaults to 0.5) — The standard deviation of the data distribution from the EDM paper. Defaults to 0.5 from the original implementation.
  • s_noise (float, defaults to 1.0) — The amount of additional noise to counteract loss of detail during sampling. A reasonable range is [1.000, 1.011]. Defaults to 1.0 from the original implementation.
  • rho (float, defaults to 7.0) — The parameter for calculating the Karras sigma schedule from the EDM paper. Defaults to 7.0 from the original implementation.
  • clip_denoised (bool, defaults to True) — Whether to clip the denoised outputs to (-1, 1).
  • timesteps (List or np.ndarray or torch.Tensor, optional) — An explicit timestep schedule that can be optionally specified. The timesteps are expected to be in increasing order.

Multistep and onestep sampling for consistency 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.

get_scalings_for_boundary_condition

< >

( sigma ) tuple

Parameters

  • sigma (torch.FloatTensor) — The current sigma in the Karras sigma schedule.

Returns

tuple

A two-element tuple where c_skip (which weights the current sample) is the first element and c_out (which weights the consistency model output) is the second element.

Gets the scalings used in the consistency model parameterization (from Appendix C of the paper) to enforce boundary condition.

epsilon in the equations for c_skip and c_out is set to sigma_min.

scale_model_input

< >

( sample: FloatTensor timestep: typing.Union[float, torch.FloatTensor] ) torch.FloatTensor

Parameters

  • sample (torch.FloatTensor) — The input sample.
  • timestep (float or torch.FloatTensor) — The current timestep in the diffusion chain.

Returns

torch.FloatTensor

A scaled input sample.

Scales the consistency model input by (sigma**2 + sigma_data**2) ** 0.5.

set_timesteps

< >

( num_inference_steps: typing.Optional[int] = None device: typing.Union[str, torch.device] = None timesteps: typing.Optional[typing.List[int]] = None )

Parameters

  • num_inference_steps (int) — The number of diffusion steps used when generating samples with a pre-trained model.
  • device (str or torch.device, optional) — The device to which the timesteps should be moved to. If None, the timesteps are not moved.
  • timesteps (List[int], optional) — Custom timesteps used to support arbitrary spacing between timesteps. If None, then the default timestep spacing strategy of equal spacing between timesteps is used. If timesteps is passed, num_inference_steps must be None.

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

sigma_to_t

< >

( sigmas: typing.Union[float, numpy.ndarray] ) float or np.ndarray

Parameters

  • sigmas (float or np.ndarray) — A single Karras sigma or an array of Karras sigmas.

Returns

float or np.ndarray

A scaled input timestep or scaled input timestep array.

Gets scaled timesteps from the Karras sigmas for input to the consistency model.

step

< >

( model_output: FloatTensor timestep: typing.Union[float, torch.FloatTensor] sample: FloatTensor generator: typing.Optional[torch._C.Generator] = None return_dict: bool = True ) CMStochasticIterativeSchedulerOutput or tuple

Parameters

  • model_output (torch.FloatTensor) — The direct output from the learned diffusion model.
  • timestep (float) — The current timestep in the diffusion chain.
  • sample (torch.FloatTensor) — A current instance of a sample created by the diffusion process.
  • generator (torch.Generator, optional) — A random number generator.
  • return_dict (bool, optional, defaults to True) — Whether or not to return a CMStochasticIterativeSchedulerOutput or tuple.

Returns

CMStochasticIterativeSchedulerOutput or tuple

If return_dict is True, CMStochasticIterativeSchedulerOutput 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 diffusion process from the learned model outputs (most often the predicted noise).

CMStochasticIterativeSchedulerOutput

class diffusers.schedulers.scheduling_consistency_models.CMStochasticIterativeSchedulerOutput

< >

( prev_sample: FloatTensor )

Parameters

  • prev_sample (torch.FloatTensor of shape (batch_size, num_channels, height, width) for images) — Computed sample (x_{t-1}) of previous timestep. prev_sample should be used as next model input in the denoising loop.

Output class for the scheduler’s step function.