TL;DR
Score distillation sampling (SDS) lets you optimise anything that can render an image, such as a 3D scene, so that its renders look right to a pretrained 2D diffusion model. Each step renders an image, adds noise, asks the diffusion model what noise it sees, and uses the gap between that answer and the noise actually added as a gradient for the scene's parameters. The diffusion model is never trained and never backpropagated through.
Builds on: Classifier-Free Guidance, Diffusion Model, Score Function · Related: Score Matching (the training idea behind the residual's target)
Where it's used
- Text-to-3D generation. DreamFusion (2022) optimised a neural radiance field, a 3D scene rendered by volume rendering, from a text prompt alone. Each step rendered the field from a random camera, applied SDS with a text-to-image diffusion model, and updated the field. No 3D training data was needed.
- Editing and stylising images. Any parametric picture, such as a vector drawing, a set of brush strokes or a low-resolution image, can be optimised the same way toward a text prompt, because the only requirement is that the image is a differentiable function of its parameters.
- Distilling a slow generator into a fast one. The same gradient signal can train a one-step or few-step generator to match a multi-step diffusion model, in the spirit of knowledge distillation; this is where the word "distillation" in the name comes from.
How it works
Take the simplest possible case: a picture with one number, and the goal is to move to where a diffusion model would place likely images.
- Render. The generator produces an image from its parameters . In the 1D toy, is just .
- Add noise. Pick a random noise level and add Gaussian noise to get a noisy version . The diffusion model sees only .
- Ask the model for its noise guess. The model returns a noise prediction . Classifier-free guidance, with a weight , makes this prediction point more strongly at the text prompt.
- Compare the guess with the truth. The residual says which way the image should move. A positive residual means the image sits above where the model expects it, so the step pushes it down; a negative one pushes it up.
- Pass the residual back. Use the residual as the gradient of the image, then chain it through the renderer to get a gradient for . Take an ordinary gradient descent step: , where is the step size.
Tiny instance. Let the data be a single value , and take a diffusion model that knows this exactly. At noise level , the model's noise guess for is . The residual is then , which does not depend on the random noise at all. Start with and use step size . Step 1 gives . Step 2 gives . Check: each step halves the distance to , and approaches .
flowchart LR A["Parameters theta"] --> B["Render x = g(theta)"] B --> C["Add noise, level t"] C --> D["Diffusion model guesses noise"] D --> E["Residual guess minus noise"] E --> F["Chain back through renderer"] F --> A
The math
Steps 1 and 2 in symbols: the noisy image
The renderer gives . The noisy version is , where and are fixed numbers set by the noise level (the signal weight and the noise weight). Larger means smaller and larger , so more of the image is replaced by noise.
Step 3 in symbols: the guided noise prediction
Let be the diffusion model's noise prediction for prompt , and let be its prediction with the prompt removed. With guidance weight , the prediction used by SDS is
This is the classifier-free guidance formula from Classifier-Free Guidance. Setting gives the plain conditional prediction.
Steps 4 and 5 in symbols: the SDS gradient
Write with . The diffusion training loss, averaged over and , has gradient with respect to equal to
for a weighting that depends on the noise level. The term would require backpropagating through the whole diffusion network, which is expensive and unnecessary. SDS drops that factor and keeps the residual:
The only part that needs the renderer's derivative is , which the renderer already provides. Because the diffusion model's Jacobian is dropped, this is not the exact gradient of a clearly defined objective. It is a direction that works well in practice.
Relation to the score
The noise prediction and the score function are related by , where is the density of the noisy data at level . So the model's guess points along the score of the noisy distribution, which is the direction of higher density. Samplers such as Langevin dynamics follow the score directly; SDS uses it to pull a rendered image toward higher density, and the toy case moved toward the data value for that reason.
Worked examples
- Let the true noise be , and let the prompt-free and prompt-conditioned predictions be and .
- Take guidance weight . Then .
- The residual is .
- Take . The gradient on the image is .
- Interpret. With no guidance, the model asks for a small move, .
- Use the same numbers, , , , but now take guidance weight .
- Compute the guided prediction. .
- Compute the residual. .
- Compare with Example 1. The residual rose from to , which is about twelve times larger.
- Interpret. Guidance pushes the image much harder toward the prompt. SDS implementations often use a large guidance weight for this reason, and the same weight can also push colours and contrast too far.
Take a RGB image. It has values, and suppose the residual for each value is . The gradient vector in pixel space then has Euclidean norm .
- Compute the square root. .
- Multiply by the per-value residual. .
- Interpret. The step on the image is large in total even though each pixel moves only a little. The renderer then maps this pixel gradient onto the parameters of the scene, so the effect on the scene is averaged over many pixels.
Try it: a RGB render with per-value residual . What is the norm of the pixel gradient?
Answer: values, , and .
Common mistakes and tradeoffs
It is tempting to think SDS samples images from the diffusion model. In fact it produces a gradient that improves a parametric image or scene. It does not return a sample, and the result depends on the optimiser, the renderer and the camera distribution as well as the diffusion model.
It is tempting to think the residual is the exact gradient of a known loss. In fact dropping the diffusion network's Jacobian makes it a biased direction. It works well in practice, but it can over-smooth results and produce saturated colours, and it does not come with the guarantees of an exact gradient.
It is tempting to think a larger guidance weight is always better. In fact large weights strengthen the match to the prompt, as Example 2 shows, but they also cause oversaturated and cartoon-like results. The weight is a trade-off between fidelity to the prompt and realism.
When to use SDS: you have a differentiable renderer or a parametric image, a pretrained diffusion model, and no paired training data. When not to use it: when you can sample directly from the image model, which is cheaper and gives a true sample. Alternatives include optimisation with more careful sampling of noise levels and variants that replace the residual with a learned, per-prompt model; later work built on the basic SDS idea in that direction.
History
Diffusion models for images became strong in 2021 and 2022, and classifier-free guidance made prompt-following practical in 2022. In September 2022 the DreamFusion preprint by Poole, Jain, Barron and Mildenhall showed that a neural radiance field could be optimised from a text prompt using SDS with a large text-to-image diffusion model, producing 3D scenes that could be viewed from any angle. The name borrows the word "distillation" from knowledge distillation, the idea of transferring what a large model knows into a different, optimised representation.
Recap
- SDS turns a pretrained diffusion model into a gradient for any differentiable image generator.
- Each step renders an image, adds noise, takes the guided noise prediction, and uses the residual between the prediction and the true noise as the image gradient.
- The diffusion network's Jacobian is dropped, so the gradient is biased but cheap.
- Guidance weight controls how strongly the result follows the prompt, and too much weight makes results oversaturated.
- Read next: Score Matching for the training idea whose target the residual approximates, and Classifier-Free Guidance for the guidance formula used in Step 3.
Check yourself
QuestionRecall: what does SDS use as the gradient for the image, and which part of the diffusion model is left out?Show answer
QuestionComputation: the true noise is , the prompt-free prediction is , the prompt-conditioned prediction is , and the guidance weight is . What is the residual?Show answer
QuestionWhy: why does the 1D toy with an exact model move toward the data value , even though the added noise is random?Show answer
QuestionApplied: a team uses SDS to optimise a 3D scene and sees oversaturated colours at high guidance weights. Name one change that reduces the problem and say what it costs.Show answer
Sources
- Ben Poole, Ajay Jain, Jonathan T. Barron, Ben Mildenhall, "DreamFusion: Text-to-3D using 2D Diffusion", arXiv:2209.14988. https://arxiv.org/abs/2209.14988
- Jonathan Ho, Tim Salimans, "Classifier-Free Diffusion Guidance", arXiv:2207.12598. https://arxiv.org/abs/2207.12598