Implementation:AUTOMATIC1111 Stable diffusion webui UniPC Solver
| Knowledge Sources | |
|---|---|
| Domains | Diffusion Models, ODE Solvers, Sampling |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
Implements the UniPC (Unified Predictor-Corrector) framework for fast high-order ODE solving of diffusion models, providing noise schedule management and the core multi-step sampling algorithm.
Description
This module contains three main components:
NoiseScheduleVP: Encapsulates forward SDE noise schedules for variance-preserving diffusion processes. Supports discrete (from betas or alphas_cumprod), linear, and cosine schedules. Provides methods for computing alpha_t, sigma_t, lambda_t (half-logSNR), and the inverse lambda function for converting between continuous time labels and noise levels.
model_wrapper: A factory function that adapts various model parameterization types (noise, x_start, v, score) with different guidance modes (unconditional, classifier, classifier-free) into a unified noise prediction interface suitable for ODE solvers. Handles batched conditional/unconditional inference for classifier-free guidance.
UniPC: The core multi-step predictor-corrector ODE solver. Uses Taylor expansions with configurable order (up to order 3), supports both data prediction and noise prediction modes, dynamic thresholding, and two variants (bh1 and bh2). Implements themultistep_uni_pc_updatemethod for individual steps and asamplemethod for the full sampling loop with configurable skip types (time_uniform, logSNR, time_quadratic).
Usage
Use the UniPC solver when you need high-quality sampling results with fewer diffusion steps. It is one of the key sampling methods available in the WebUI, accessible through the sampler dropdown. The solver is particularly effective for generating good results in 10-20 steps compared to traditional DDPM/DDIM samplers that may require 50+ steps.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: modules/models/diffusion/uni_pc/uni_pc.py
- Lines: 1-863
Signature
class NoiseScheduleVP:
def __init__(self, schedule='discrete', betas=None, alphas_cumprod=None,
continuous_beta_0=0.1, continuous_beta_1=20.):
def model_wrapper(model, noise_schedule, model_type="noise",
model_kwargs=None, guidance_type="uncond",
guidance_scale=1., classifier_fn=None, classifier_kwargs=None):
class UniPC:
def __init__(self, model_fn, noise_schedule, predict_x0=True,
thresholding=False, max_val=1., variant='bh1',
condition=None, unconditional_condition=None,
before_sample=None, after_sample=None, after_update=None):
def sample(self, x, steps=20, t_start=None, t_end=None,
order=3, skip_type='time_uniform', method='multistep',
lower_order_final=True, denoise_to_zero=False,
atol=0.0078, rtol=0.05):
Import
from modules.models.diffusion.uni_pc.uni_pc import NoiseScheduleVP, model_wrapper, UniPC
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | torch.Tensor | Yes | Initial noisy latent tensor to denoise |
| steps | int | No | Number of sampling steps (default 20) |
| order | int | No | Order of the solver, 1-3 (default 3) |
| skip_type | str | No | Timestep skip strategy: 'time_uniform', 'logSNR', or 'time_quadratic' |
| method | str | No | Solver method: 'multistep' or 'singlestep' |
Outputs
| Name | Type | Description |
|---|---|---|
| x | torch.Tensor | The denoised latent tensor after the sampling process |
Usage Examples
from modules.models.diffusion.uni_pc.uni_pc import NoiseScheduleVP, model_wrapper, UniPC
# Create a noise schedule from model betas
ns = NoiseScheduleVP('discrete', betas=betas)
# Wrap the model for classifier-free guidance
model_fn = model_wrapper(
model,
ns,
model_type="noise",
guidance_type="classifier-free",
guidance_scale=7.5,
)
# Create the UniPC solver
uni_pc = UniPC(model_fn, ns, predict_x0=True, variant='bh1')
# Sample from noise
x_init = torch.randn(1, 4, 64, 64)
x_denoised = uni_pc.sample(x_init, steps=20, order=3)