Practical and Asymptotically Exact Conditional Sampling in Diffusion Models

Luhuan Wu, Brian L. Trippe, Christian A Naesseth, David Blei, John Patrick Cunningham

Overview

Diffusion models are powerful tools for conditional generation, but they typically require expensive task-specific training or rely on mathematically flawed heuristics. This paper introduces the Twisted Diffusion Sampler (TDS), a Sequential Monte Carlo (SMC) algorithm that allows for mathematically rigorous, asymptotically exact conditional sampling from unconditional diffusion models. By cleverly combining heuristic guidance with particle filtering, TDS achieves state-of-the-art results in complex tasks like protein motif-scaffolding without needing specialized retraining.

The Challenge of Conditional Generation

Imagine you have a generative model trained on millions of images, and you want to sample an image that satisfies a specific condition—like "contains a cat" or "fills in this missing square." Or, in biology, you have a model of protein structures and want to generate a protein that contains a specific functional active site (a "motif").

Currently, there are two main ways to achieve this:

  1. Conditional Training: Train a brand-new model (or fine-tune an existing one) specifically for that condition. This is expensive, requires paired datasets, and is highly inflexible.
  2. Heuristic Approximations: Take an unconditional model and use tricks (like Gradient Guidance or replacement methods) to steer the generation process at inference time. While flexible, these heuristics are mathematically inexact. They often produce artifacts in images or, worse, chemically impossible structures in protein design.

The authors tackle this gap by asking: Can we perform flexible conditional sampling using an unconditional diffusion model, but with mathematical guarantees that we are actually sampling from the true conditional distribution?

Introducing the Twisted Diffusion Sampler (TDS)

The authors frame the reverse diffusion process as a sequence of probabilistic steps, making it a perfect candidate for Sequential Monte Carlo (SMC). SMC works by simulating a swarm of "particles" (candidate data points) that are iteratively proposed, weighted, and resampled based on how well they fit the desired condition.

The core innovation is the use of Twisting, a technique from the SMC literature. Standard SMC can be highly inefficient if particles blindly wander into low-probability areas. "Optimal twisting" would perfectly guide particles toward the target, but it requires looking into the future, which is computationally intractable.

To solve this, TDS uses a tractable twisting function. It leverages the diffusion model's own denoising network (its best guess of the final clean data at any given step) to approximate the optimal guidance.

  • The clever trick: TDS uses existing heuristics (like gradient guidance) as the proposal distribution to steer particles, but then uses SMC weighting to correct the mathematical errors of those heuristics.
  • Extensions: The authors adapt TDS to work on Riemannian manifolds (necessary for 3D protein backbone generation) and introduce a way to handle "degrees of freedom" (e.g., when you know a protein needs a specific motif, but you don't care where it is located in the sequence).

Key Insights and Ideas

  • Heuristics make great proposals: Heuristics like Gradient Guidance might fail to produce exact samples on their own, but they are fantastic at guessing the general direction a sample should go. By using them as SMC proposals, TDS gets the efficiency of heuristics with the exactness of Monte Carlo methods.
  • A computational-statistical trade-off: TDS allows users to dial in their desired accuracy. If you are in a hurry, running TDS with just 2 particles often beats standard heuristics. If you need absolute precision (like in molecular design), you can crank up the number of particles to drive the approximation error to zero.
Mean Square Error vs. Number of Particles for various sampling methods. TDS demonstrates a clear computational-statistical trade-off, achieving lower error as particle count increases compared to standard heuristics.
  • Amortization isn't always necessary: You don't always need to spend massive compute upfront to train a specialized conditional model. If you have a good unconditional model and a differentiable condition (like a classifier), you can achieve state-of-the-art results entirely at inference time.

Experimental Results

The authors evaluated TDS across synthetic data, image datasets (MNIST, CIFAR-10, ImageNet), and protein structures.

  • Image Generation: On MNIST class-conditional generation, TDS with 64 particles achieved near-perfect classification accuracy, visually eliminating the artifacts that plagued Gradient Guidance. It also successfully scaled to high-dimensional ImageNet (256x256) generation.
  • Protein Design: This is the standout result. The authors applied TDS to FrameDiff (an unconditional protein model) to solve motif-scaffolding tasks. TDS matched or outperformed RFdiffusion (a state-of-the-art model specifically trained for conditional motif scaffolding) on 11 out of 24 benchmark tasks. TDS was particularly dominant on shorter protein scaffolds (<100 residues).

Limitations and Future Work

While powerful, TDS comes with a few trade-offs:

  • Computational Cost: Because TDS simulates $K$ particles, the compute cost scales linearly with $K$. Furthermore, computing the twisting function requires backpropagating through the denoising network at every step, making it noticeably slower than unconditional generation.
  • Particle Collapse: In the final few steps of the diffusion process (as noise goes to zero), the effective sample size (ESS) can suddenly drop, meaning most particles die off and diversity is lost. The authors sometimes had to use a "truncation" hack to avoid this.
  • Designing Twisting Functions: While the authors provide twisting functions for classifiers and inpainting, formulating mathematically sound twisting functions for arbitrary, complex constraints remains an open challenge for future work.

Summary updated

BibTeX
@inproceedings{wu2023practical,
  author = {Luhuan Wu and Brian L. Trippe and Christian A Naesseth and David Blei and John Patrick Cunningham},
  title = {{Practical and Asymptotically Exact Conditional Sampling in Diffusion Models}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=eWKqr1zcRv},
}
Figure