Fast constrained sampling in pre-trained diffusion models

Alexandros Graikos, Nebojsa Jojic, Dimitris Samaras

Large denoising diffusion models, such as Stable Diffusion, have been trained on billions of image-caption pairs and excel at text-conditioned image generation. However, using these pre-trained models for downstream tasks where the output must satisfy specific constraints—such as inpainting missing regions or super-resolution—is surprisingly difficult and computationally expensive.

This research introduces a training-free algorithm that uses an approximation of Newton’s optimization method to perform constrained sampling in seconds rather than minutes. By replacing expensive backpropagation with simple forward passes through the denoiser, the method achieves state-of-the-art image quality with a fraction of the computational cost and memory.

The Challenge of Constrained Generation

When attempting to generate an image under new constraints (e.g., filling in the missing half of a face), existing approaches generally fall into two categories, both with significant drawbacks:

  1. Backpropagation-based methods: These calculate how to change the noisy input to satisfy the constraint by differentiating through the entire neural network. While mathematically sound and capable of maintaining global consistency, this approach is incredibly slow and memory-intensive.
  2. Local/Naive methods: These enforce constraints only on specific pixels. While fast, they often fail to capture long-range correlations. For example, the model might generate a blue eye on the missing side of a face even if the visible eye is brown, because the local constraint doesn't "see" the other side of the image.

The authors sought a middle ground: a method that maintains the global consistency of backpropagation but runs at the speed of standard, unconstrained generation.

The Symmetry Shortcut

The core innovation lies in how the algorithm updates the image at each denoising step. Standard gradient-descent methods use the transpose of the Jacobian matrix ($J^T$) to find the update direction, which requires heavy backpropagation.

However, the authors observed that in an ideal, theoretically optimal denoiser, the Jacobian matrix is actually symmetric ($J = J^T$). Because real-world models are "almost" symmetric, the authors replace the exact transpose with an inexact Newton step approximated by the Jacobian matrix itself ($J$).

To accelerate this further, they compute the Jacobian-vector product using finite differences. This requires just two forward passes through the model:

  1. One pass to see what the model predicts normally.
  2. A second pass where the input is slightly nudged in the direction of the error.

By comparing these two passes, the model can determine how a change in one part of the image affects the rest. This effectively "inverts" the constraint without ever needing to run a backward pass through the heavy U-Net architecture, turning a hard optimization problem into a cheap arithmetic one.

Key Results

The researchers evaluated their method across several standard benchmarks, including ImageNet (for free-form inpainting and 8× super-resolution), LAION, WikiArt, and FFHQ.

Comparison of inpainting methods showing the proposed method achieves high quality in just 15 seconds.
  • Speed and Quality: For inpainting tasks, the proposed method takes just 15 seconds, compared to 8 minutes for PSLD or 6 minutes for LDPS. Despite the speed, it maintains high visual fidelity and global consistency, avoiding the blurry or inconsistent artifacts common in other fast training-free approaches.
  • Super-Resolution: In 8× super-resolution tasks, the method achieved a Fréchet Inception Distance (FID) of 60.99, outperforming several slower baselines.
  • Memory Efficiency: Because the method avoids backpropagation entirely, it uses significantly less GPU memory (~9GB vs. ~17GB for backprop methods), making high-quality constrained sampling accessible on consumer-grade hardware.
  • Style Guidance: The method proved highly effective at minimizing "style error" in style-guided generation tasks while staying faithful to the original text prompt.

Limitations and Future Work

While highly effective, the authors note a few limitations:

  • High Guidance Sensitivity: The method struggles when the classifier-free guidance weight is very high ($w > 10$), as extreme guidance alters the mathematical properties of the Jacobian.
  • Distilled Models: On distilled models designed to generate images in just 1–4 steps, the algorithm requires more internal iterations to converge, which diminishes the relative speed benefits.

Looking forward, the authors suggest this approach could be highly valuable for medical imaging, such as reconstructing high-resolution CT or MRI scans from low-dose, constrained data.

An implementation of the algorithm is available at GitHub.

Summary updated

BibTeX
@inproceedings{graikos2025fast,
  author = {Alexandros Graikos and Nebojsa Jojic and Dimitris Samaras},
  title = {{Fast constrained sampling in pre-trained diffusion models}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=3kVM0m60Q5},
}
Figure