Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:AUTOMATIC1111 Stable diffusion webui KDiffusionSampler sample

From Leeroopedia


Knowledge Sources
Domains Diffusion Models, Sampling, Stochastic Processes
Last Updated 2026-02-08 00:00 GMT

Overview

Concrete tool for executing the latent diffusion sampling (denoising) loop using k-diffusion library samplers with classifier-free guidance, provided by the AUTOMATIC1111 stable-diffusion-webui repository.

Description

KDiffusionSampler is a wrapper class that bridges the WebUI's processing pipeline with the k-diffusion library's sampling functions. It handles:

  1. Sigma computation -- The get_sigmas() method computes the noise schedule based on the selected scheduler (Karras, Exponential, Beta, Uniform, or Automatic) and the number of steps. It supports overriding min/max sigmas and discarding the penultimate sigma.
  1. Initial noise scaling -- The input noise tensor is scaled by the initial sigma value. Optionally applies SGM noise multiplier scaling: x = x * sqrt(1 + sigma_0^2).
  1. Sampler dispatch -- Delegates to the appropriate k-diffusion sampling function (e.g., k_diffusion.sampling.sample_euler, k_diffusion.sampling.sample_dpmpp_2m) with the CFG denoiser as the model.
  1. CFG denoising -- The CFGDenoiser (at modules/sd_samplers_cfg_denoiser.py, lines 33-312) wraps the UNet to implement classifier-free guidance. At each denoising step, it:
    • Receives the noisy latent and current sigma
    • Runs the UNet once with the positive prompt conditioning and once with the negative prompt conditioning
    • Combines the predictions using the CFG formula: denoised = uncond + cfg_scale * (cond - uncond)
    • Handles prompt scheduling, conditional padding, and s_min_uncond optimization

The class also implements sample_img2img() for image-to-image sampling (used by hires fix), which starts from a partially noised version of the input image rather than pure noise.

Usage

KDiffusionSampler is instantiated by sd_samplers.create_sampler() when a k-diffusion sampler is selected. Its sample() method is called by StableDiffusionProcessingTxt2Img.sample() during the first pass, and sample_img2img() is called during the hires fix second pass.

Code Reference

Source Location

  • Repository: stable-diffusion-webui
  • File: modules/sd_samplers_kdiffusion.py
  • Lines: 67-234
  • CFGDenoiser: modules/sd_samplers_cfg_denoiser.py, Lines: 33-312

Signature

class KDiffusionSampler(sd_samplers_common.Sampler):
    def __init__(self, funcname, sd_model, options=None):
        ...

    def get_sigmas(self, p, steps):
        """Compute the noise schedule (sigma sequence) for the given steps and scheduler."""
        ...

    def sample(self, p, x, conditioning, unconditional_conditioning, steps=None, image_conditioning=None):
        """
        Run the full txt2img sampling loop.

        Args:
            p: StableDiffusionProcessing instance with generation parameters.
            x: Initial noise tensor of shape (B, 4, H/8, W/8).
            conditioning: Positive prompt conditioning from CLIP.
            unconditional_conditioning: Negative prompt conditioning from CLIP.
            steps: Number of denoising steps (defaults to p.steps).
            image_conditioning: Optional image conditioning for inpainting models.

        Returns:
            Denoised latent tensor of shape (B, 4, H/8, W/8).
        """
        ...

    def sample_img2img(self, p, x, noise, conditioning, unconditional_conditioning, steps=None, image_conditioning=None):
        """
        Run the img2img sampling loop starting from a partially noised latent.
        Used by hires fix second pass.
        """
        ...

Import

from modules.sd_samplers_kdiffusion import KDiffusionSampler

I/O Contract

Inputs

Name Type Required Description
p StableDiffusionProcessing Yes The processing object containing generation parameters (cfg_scale, scheduler, sampler_name, etc.)
x torch.Tensor Yes Initial noise tensor of shape (B, 4, H/8, W/8) where B is batch size, H and W are image dimensions
conditioning ScheduledPromptConditioning Yes Positive prompt conditioning tensor from the CLIP text encoder
unconditional_conditioning ScheduledPromptConditioning Yes Negative prompt (unconditional) conditioning tensor from the CLIP text encoder
steps int or None No Number of sampling steps; defaults to p.steps if None
image_conditioning torch.Tensor or None No Additional image conditioning for inpainting/unclip models; None for standard txt2img

Outputs

Name Type Description
return torch.Tensor Denoised latent tensor of shape (B, 4, H/8, W/8) ready for VAE decoding

Usage Examples

Basic Usage

from modules import sd_samplers

# Create a sampler (typically done inside StableDiffusionProcessingTxt2Img.sample())
sampler = sd_samplers.create_sampler("DPM++ 2M", shared.sd_model)

# Generate initial noise
x = rng_instance.next()  # shape: (1, 4, 64, 64) for 512x512

# Run sampling
samples = sampler.sample(
    p=processing_instance,
    x=x,
    conditioning=positive_cond,
    unconditional_conditioning=negative_cond,
    image_conditioning=img_cond,
)
# samples.shape: (1, 4, 64, 64)

Sigma Schedule Inspection

# Inspect the computed noise schedule
sigmas = sampler.get_sigmas(p=processing_instance, steps=30)
# sigmas is a 1D tensor of length steps+1, monotonically decreasing
# e.g., tensor([14.6146, 10.7892, ..., 0.0292, 0.0000])

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment