Implementation:AUTOMATIC1111 Stable diffusion webui UniPC Sampler
| Knowledge Sources | |
|---|---|
| Domains | Sampling, Diffusion_Models |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
Implements the UniPC (Unified Predictor-Corrector) sampling algorithm as a wrapper around the diffusion model for high-quality image generation with configurable step scheduling.
Description
The UniPC Sampler module provides the UniPCSampler class that wraps a diffusion model to perform denoising sampling using the UniPC algorithm. The sampler initializes by extracting the cumulative product of alphas from the model and constructs a discrete noise schedule. It supports both SD 1.X ("noise" parameterization) and SD 2.X ("v" parameterization) models. The sample method creates a classifier-free guidance model wrapper, initializes the UniPC solver with configurable variant, skip type, order, and lower-order-final options (all exposed through shared.opts), and runs the multistep sampling process. The sampler supports hook functions (before_sample, after_sample, after_update) that can be registered via set_hooks for external monitoring or modification of the sampling process. Conditioning can be provided as a dict, list, or tensor.
Usage
Use this sampler when the UniPC sampling method is selected in the WebUI. It provides high-quality results with fewer steps compared to some other samplers, supporting both SD 1.X and SD 2.X model architectures.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: modules/models/diffusion/uni_pc/sampler.py
- Lines: 1-101
Signature
class UniPCSampler:
def __init__(self, model, **kwargs) -> None
def register_buffer(self, name: str, attr) -> None
def set_hooks(self, before_sample, after_sample, after_update) -> None
def sample(self, S, batch_size, shape, conditioning=None, callback=None,
normals_sequence=None, img_callback=None, quantize_x0=False,
eta=0., mask=None, x0=None, temperature=1., noise_dropout=0.,
score_corrector=None, corrector_kwargs=None, verbose=True,
x_T=None, log_every_t=100, unconditional_guidance_scale=1.,
unconditional_conditioning=None, **kwargs) -> tuple[torch.Tensor, None]
Import
from modules.models.diffusion.uni_pc.sampler import UniPCSampler
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | object | Yes | The diffusion model with alphas_cumprod, parameterization, and apply_model attributes |
| S | int | Yes | Number of sampling steps |
| batch_size | int | Yes | Number of images to generate simultaneously |
| shape | tuple[int, int, int] | Yes | Latent space dimensions (C, H, W) |
| conditioning | dict, list, or Tensor | No | Positive conditioning embeddings |
| unconditional_conditioning | object | No | Negative/unconditional conditioning embeddings |
| unconditional_guidance_scale | float | No | Classifier-free guidance scale (default 1.0) |
| x_T | torch.Tensor | No | Initial noise tensor; generated randomly if not provided |
Outputs
| Name | Type | Description |
|---|---|---|
| samples | torch.Tensor | The denoised latent samples tensor |
| intermediates | None | Always None (placeholder for API compatibility) |
Usage Examples
from modules.models.diffusion.uni_pc.sampler import UniPCSampler
sampler = UniPCSampler(model)
samples, _ = sampler.sample(
S=20,
batch_size=1,
shape=(4, 64, 64),
conditioning=positive_cond,
unconditional_conditioning=negative_cond,
unconditional_guidance_scale=7.5,
)