Classifier-Free Guidance: Buying Prompt Adherence with a Second Forward Pass
Published:
Conditioning is not control
Feeding a text embedding into the denoiser gives you \(\epsilon_\theta(\mathbf{x}_t, t, c)\), a model of \(p(\mathbf{x}\mid c)\). Trained this way it works, but weakly: the prompt nudges the sample rather than governing it. The reason is that the training loss rewards covering the data, and the conditional and unconditional distributions of natural images overlap heavily. A model can score well while largely ignoring \(c\).
What users want is not a sample from \(p(\mathbf{x}\mid c)\). They want a sample from the part of \(p(\mathbf{x}\mid c)\) where the prompt is unambiguously satisfied — a sharpened, mode-seeking version of it.
Classifier guidance, and why it was awkward
Dhariwal and Nichol’s answer was to attach an explicit classifier \(p_\phi(c \mid \mathbf{x}_t)\) and push the sample uphill on it. Because \(\boldsymbol{\epsilon}_\theta = -\sqrt{1-\bar{\alpha}_t}\,\nabla_{\mathbf{x}_t}\log p_t(\mathbf{x}_t)\), Bayes’ rule in score space becomes a simple additive correction on the predicted noise:
Here \(s\) is the guidance scale and \(\bar{\alpha}_t\) the cumulative signal coefficient of the forward process. The correction says: move in the direction that increases the classifier’s confidence in \(c\).
The awkwardness is the classifier itself. It has to accept \(\mathbf{x}_t\), not a clean image, so an off-the-shelf ImageNet classifier is useless — you must train a bespoke one on noisy inputs across all timesteps. That is a second model, a second training pipeline and a second failure mode. Worse, gradients of a classifier on near-pure noise are exactly the adversarial-example regime: the classifier can be made confident by perturbations that do not correspond to real image structure. And for free-form text there is no classifier to train in the first place.
The trick: be your own classifier
Ho and Salimans noticed the correction never needed a classifier explicitly. Applying Bayes’ rule the other way,
so the classifier gradient is the difference between two scores the diffusion model can already produce, provided it can also run unconditionally. Getting that for free costs one line in the data loader: during training, replace \(c\) with a null token \(\varnothing\) with probability around 10%. One network, one loss, two behaviours.
At sampling time, substitute and rearrange, and the guided noise prediction is a straight-line extrapolation:
Read it as a lever. At \(w = 0\) you get the unconditional model and the prompt is ignored. At \(w = 1\) the two terms cancel to \(\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, c)\) — ordinary conditional sampling. At \(w > 1\) you overshoot past the conditional prediction, along the direction that distinguishes “with prompt” from “without prompt”. Typical text-to-image settings sit between 5 and 10. (Some papers write \(\tilde{\boldsymbol{\epsilon}} = (1+s)\boldsymbol{\epsilon}_c - s\,\boldsymbol{\epsilon}_\varnothing\); that is the same equation with \(w = 1+s\).)
The trade-off, stated honestly
| Guidance weight | Prompt adherence | Diversity | Typical artefacts |
|---|---|---|---|
| \(w = 1\) | loose; prompt often partially ignored | highest | washed-out, generic compositions |
| \(w \approx 3\) | reliable on the main subject | good | mild |
| \(w \approx 7\) | strong, including attributes | noticeably reduced | rising saturation and contrast |
| \(w \ge 15\) | over-literal | collapses towards one composition | blown-out colours, cartoon edges, clipping |
Two costs are unavoidable. The first is compute: every sampling step needs both a conditional and an unconditional forward pass, so sampling is about twice as expensive per step (batching the two halves together hides the latency, not the FLOPs). The second is the saturation blow-up. Extrapolating in \(\boldsymbol{\epsilon}\)-space pushes the implied clean image \(\hat{\mathbf{x}}_0\) outside the valid pixel range, and the errors compound over steps. Imagen’s dynamic thresholding — rescaling \(\hat{\mathbf{x}}_0\) by a per-step percentile rather than hard-clipping it — is the standard patch, and it is what makes very high guidance weights usable at all.
Guidance is why text-to-image diffusion works at all in practice, and it composes with everything else: it is orthogonal to where the diffusion happens (latent diffusion), to how the trajectory is integrated (samplers), and to whether the path is stochastic or straight (flow matching).
References
- Dhariwal, P., & Nichol, A. Diffusion Models Beat GANs on Image Synthesis. NeurIPS 2021.
- Ho, J., & Salimans, T. Classifier-Free Diffusion Guidance. NeurIPS 2021 Workshop on Deep Generative Models.
- Nichol, A., Dhariwal, P., Ramesh, A., Shyam, P., Mishkin, P., McGrew, B., Sutskever, I., & Chen, M. GLIDE: Towards Photorealistic Image Generation and Editing with Text-Guided Diffusion Models. ICML 2022.
- Saharia, C., Chan, W., Saxena, S., et al. Photorealistic Text-to-Image Diffusion Models with Deep Language Understanding. NeurIPS 2022.
