Inferensys

Glossary

Ancestral Sampling

Ancestral sampling is the fundamental stochastic procedure used in diffusion models to generate new data by iteratively denoising a sample of pure noise, following the learned reverse Markov chain.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
DIFFUSION MODELS

What is Ancestral Sampling?

Ancestral sampling is the standard stochastic sampling procedure used in diffusion models to generate new data by iteratively denoising pure noise.

Ancestral sampling is the step-by-step generative procedure in a Denoising Diffusion Probabilistic Model (DDPM). Starting from pure Gaussian noise, the model applies a learned reverse process over multiple timesteps. At each step, it predicts and removes a portion of the noise, while also injecting a small, stochastic amount of new noise defined by the model's fixed variance schedule. This process follows the exact reverse of the Markov chain defined during training.

This method is called 'ancestral' because each new sample is directly descended from the previous one in the chain. The injected noise at each step is crucial, as it ensures the sampling trajectory explores the full probabilistic distribution learned by the model, rather than taking a deterministic path. While accurate, this iterative nature makes ancestral sampling computationally intensive compared to newer, faster samplers like DDIM or Consistency Models.

DIFFUSION MODELS

Key Characteristics of Ancestral Sampling

Ancestral sampling is the fundamental stochastic procedure for generating data from a trained diffusion model. It follows the learned reverse Markov chain, step-by-step, to denoise a sample of pure Gaussian noise into a coherent data point.

01

Stochastic, Stepwise Denoising

Ancestral sampling is an iterative process where each step is inherently stochastic. Starting from pure noise x_T ~ N(0, I), the model predicts a slightly denoised sample x_{t-1}. Crucially, this prediction is not deterministic; a new, small amount of Gaussian noise is added at each step according to the learned reverse process variance. This stochasticity is essential for generating diverse samples and exploring the data distribution.

  • Process: x_T → x_{T-1} → ... → x_0
  • Core Operation: x_{t-1} = μ_θ(x_t, t) + σ_t * z, where z ~ N(0, I)
  • Outcome: A single, unique sample from the model's learned distribution.
02

Follows the Reverse Markov Chain

The procedure strictly adheres to the reverse Markov chain defined by the trained model's parameters. Each step conditions only on the immediate previous state (x_t), not the full history. This Markov property is a direct inversion of the forward diffusion process and is encoded in the model's learned denoising function.

  • Mathematical Basis: p_θ(x_{0:T}) = p(x_T) ∏{t=1}^{T} p_θ(x{t-1} | x_t)
  • Conditional Distribution: p_θ(x_{t-1} | x_t) = N(x_{t-1}; μ_θ(x_t, t), Σ_θ(x_t, t))
  • Contrast with DDIM: Unlike deterministic samplers (DDIM), it does not skip steps or use a non-Markovian process.
03

Parameterized by Learned Variances

The amount of new noise injected at each denoising step is not arbitrary; it is governed by the model's learned variance schedule Σ_θ(x_t, t). In foundational models like DDPM, this is often fixed to a schedule derived from the forward process betas. However, the model can also learn to predict these variances, allowing the sampling process to adaptively control stochasticity.

  • Common Practice (DDPM): Σ_θ(x_t, t) = σ_t² I, where σ_t² is a fixed scalar from the noise schedule.
  • Learned Variances: More advanced models parameterize Σ_θ(x_t, t) with the neural network, potentially improving sample quality.
  • Impact: The variance controls the trade-off between sample diversity and fidelity at each step.
04

Connection to Score-Based Models

Ancestral sampling in variance-preserving diffusion models is mathematically equivalent to sampling via Langevin dynamics using a learned score function. The model's noise prediction ϵ_θ(x_t, t) is directly proportional to the score (gradient of the log data density). Each sampling step can be viewed as moving towards higher data density (via the score) while adding noise for exploration.

  • Equivalence: ϵ_θ(x_t, t) ∝ -score(x_t, t)
  • Langevin Step: x_{t-1} = x_t + (α * score(x_t, t)) + (√(2α) * z)
  • Foundation: This bridges Denoising Diffusion Probabilistic Models (DDPM) and Score Matching generative models.
05

Computational Cost and Speed

The primary drawback of ancestral sampling is its computational expense. Generating a single sample requires performing a full forward pass of the neural network (e.g., a U-Net) at every timestep in the reverse chain. For high-fidelity image generation, this often means 50 to 1000 sequential evaluations, making it slow compared to single-step generators.

  • Typical Steps: 50 (fast sampling) to 1000 (high quality) neural network evaluations.
  • Bottleneck: Sequential dependency prevents parallelization across timesteps.
  • Driver for Research: This cost motivates faster samplers (DDIM) and distillation methods (Consistency Models).
06

The Foundation for Conditional Generation

Ancestral sampling is the backbone for conditional generation techniques like Classifier-Free Guidance (CFG). In CFG, the core denoising step is modified. The model makes two predictions: one conditional (e.g., on a text prompt) and one unconditional. The final noise prediction is a weighted combination, steering the ancestral sampling trajectory towards regions of the data space that better match the condition.

  • CFG Update: ϵ̃ = ϵ_uncond + guidance_scale * (ϵ_cond - ϵ_uncond)
  • Process: This modified ϵ̃ is used in the standard ancestral sampling update rule.
  • Result: Dramatically improved adherence to the input prompt or class label.
SAMPLING ALGORITHMS

Ancestral Sampling vs. Other Sampling Methods

A comparison of key characteristics between ancestral sampling and other prominent sampling techniques used in diffusion models and generative AI.

Feature / MetricAncestral SamplingDeterministic Sampling (e.g., DDIM)Stochastic Sampling (e.g., DDPM)

Core Mechanism

Adds new noise at each reverse step

Follows a deterministic ODE trajectory

Follows the learned stochastic reverse process

Markovian Property

Yes, follows the learned reverse Markov chain

No, uses a non-Markovian forward process

Yes, follows the learned reverse Markov chain

Output Determinism

Stochastic (different noise seeds produce different outputs)

Deterministic (same seed produces identical output)

Stochastic (different noise seeds produce different outputs)

Sampling Speed (Steps)

Typically requires the full number of training steps (e.g., 1000)

Can use far fewer steps (e.g., 20-50) with minimal quality loss

Typically requires the full number of training steps (e.g., 1000)

Primary Use Case

Baseline sampling, theoretical analysis

Fast inference, latent space interpolation

High-fidelity, diverse sample generation

Noise Injection

Explicitly adds Gaussian noise (σₜ) at each step

No explicit noise addition after the initial sample

Adds noise scaled by the learned variance (Σₜ)

Relation to Training Objective

Directly implements the trained reverse process variances

Derived from the probability flow ODE of the trained model

Directly implements the trained reverse process means and variances

Sample Quality vs. Diversity Trade-off

Balanced, as defined by the trained model

Often higher perceived quality but lower diversity per step count

Balanced, as defined by the trained model

DIFFUSION MODELS

Frequently Asked Questions

Answers to common technical questions about the ancestral sampling procedure used in diffusion models.

Ancestral sampling is the standard, stochastic procedure for generating data from a trained diffusion model. It works by iteratively denoising a sample of pure Gaussian noise, following the learned reverse Markov chain. At each denoising step, the model predicts the noise component, and a new sample is drawn from a Gaussian distribution whose mean is the denoised estimate and whose variance is defined by the model's learned or fixed noise schedule. This injection of fresh noise at each step makes the process stochastic, meaning multiple runs from the same initial noise can yield different outputs.

This method is called "ancestral" because it directly samples from the conditional distributions defined by the model's reverse process, moving step-by-step from a simple prior distribution (noise) to the complex data distribution. It is the foundational sampling algorithm for models like Denoising Diffusion Probabilistic Models (DDPMs).

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.