Classifier-Free Guidance

Volume III, Chapter 10 — Part III. Conditioning diffusion models without a classifier: joint training, guided score derivation, guidance scale effects, and the quality-diversity tradeoff.

Advanced

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Notation
  4. Core Intuition
  5. Classifier Guidance: Background
  6. Classifier-Free Guidance: Formulation
  7. Joint Training Procedure
  8. Score Function Derivation
  9. The Guidance Scale
  10. Quality-Diversity Tradeoff
  11. Computational Cost
  12. Worked Examples
  13. Connection to the Broader Curriculum
  14. Common Pitfalls and Misconceptions
  15. Research Perspective
  16. Summary of Takeaways
  17. Exercises

Learning Objectives

After reading this chapter, you should be able to:

  1. State classifier guidance via Bayes' rule on the score function.
  2. Derive classifier-free guidance (CFG) as implicit classifier amplification without a separate model.
  3. Explain joint training with random condition dropout.
  4. Prove the equivalence between guided noise prediction and modified score logp(xtc)+wlogp(cxt)\nabla \log p(\mathbf{x}_t \mid c) + w \nabla \log p(c \mid \mathbf{x}_t).
  5. Analyze the quality-diversity tradeoff as a function of guidance scale ww.
  6. Compute the 2× inference overhead of CFG.

Prerequisites


Notation

  • cc — Conditioning variable (class, text, etc.)
  • ϵθ(xt,t,c)\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, c) — Conditional noise prediction
  • ϵθ(xt,t)\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) — Unconditional prediction
  • ww — Guidance scale
  • \varnothing — Null / dropped condition

Core Intuition

Conditional generation requires sampling from p(xc)p(\mathbf{x} \mid c) — e.g., images given text prompts. Classifier guidance (Dhariwal & Nichol, 2021) modifies the score using a separately trained classifier p(cxt)p(c \mid \mathbf{x}_t) at every noise level — expensive and limited to tasks with classifiers.

Classifier-free guidance (Ho & Salimans, 2022) trains a single noise predictor in both conditional and unconditional modes, then extrapolates between them at inference. No separate classifier needed — yet equivalent to amplifying an implicit classifier gradient.

CFG is the standard technique in Stable Diffusion, DALL·E 2, and Imagen, enabling high-fidelity text-to-image generation.

Series context. Volume III, Chapter 10, Part III.

Classifier-Free Guidance

xε̃ᵤε̃ᶜε̃ = ε̃ᵤ + w(ε̃ᶜ−ε̃ᵤ)w = 7.5strong guidance
Scale w
7.50
UnconditionalConditionalGuided
Explore: CFG amplifies the conditional direction. w=1 is no guidance; w≈7.5 is typical. High w produces sharper, more prompt-faithful but less diverse samples.

Classifier Guidance: Background

Theorem 1 (Bayesian Score Decomposition).

xtlogp(xtc)=xtlogp(xt)+xtlogp(cxt).(1)\nabla_{\mathbf{x}_t} \log p(\mathbf{x}_t \mid c) = \nabla_{\mathbf{x}_t} \log p(\mathbf{x}_t) + \nabla_{\mathbf{x}_t} \log p(c \mid \mathbf{x}_t). \tag{1}

Definition 1 (Classifier-Guided Score).

~xtlogp(xtc)=xtlogp(xt)+wCGxtlogp(cxt),(2)\tilde{\nabla}_{\mathbf{x}_t} \log p(\mathbf{x}_t \mid c) = \nabla_{\mathbf{x}_t} \log p(\mathbf{x}_t) + w_{\text{CG}} \nabla_{\mathbf{x}_t} \log p(c \mid \mathbf{x}_t), \tag{2}

where wCG>1w_{\text{CG}} > 1 amplifies conditioning.

Limitation. Requires training p(cxt)p(c \mid \mathbf{x}_t) on noisy inputs at all tt — cumbersome for text conditioning.


Classifier-Free Guidance: Formulation

Definition 2 (CFG Noise Prediction).

ϵ~θ(xt,t,c)=(1+w)ϵθ(xt,t,c)wϵθ(xt,t,),(3)\tilde{\boldsymbol{\epsilon}}_\theta(\mathbf{x}_t, t, c) = (1 + w)\,\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, c) - w\,\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, \varnothing), \tag{3}

equivalently:

ϵ~=ϵθ(xt,t,)+(1+w)[ϵθ(xt,t,c)ϵθ(xt,t,)].(4)\tilde{\boldsymbol{\epsilon}} = \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, \varnothing) + (1 + w)\left[\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, c) - \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, \varnothing)\right]. \tag{4}

ww is the guidance scale; \varnothing is null condition (empty prompt).


Joint Training Procedure

Definition 3 (Condition Dropout). During training, with probability puncondp_{\text{uncond}} (typically 0.1–0.2), replace condition cc with \varnothing:

ctrain={cwith prob. 1puncondwith prob. puncond(5)c_{\text{train}} = \begin{cases} c & \text{with prob. } 1 - p_{\text{uncond}} \\ \varnothing & \text{with prob. } p_{\text{uncond}} \end{cases} \tag{5}

Proposition 1. The same network ϵθ(xt,t,ctrain)\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, c_{\text{train}}) learns:

  • ϵθ(xt,t,c)\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, c) — conditional denoising
  • ϵθ(xt,t,)\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, \varnothing) — unconditional denoising

Using standard Reverse Process loss (13).


Score Function Derivation

From Reverse Process: ϵθxtlogp(xt)\boldsymbol{\epsilon}_\theta \propto -\nabla_{\mathbf{x}_t} \log p(\mathbf{x}_t).

Proposition 2 (Implicit Classifier Gradient).

xtlogp(cxt)=xtlogp(xtc)xtlogp(xt).(6)\nabla_{\mathbf{x}_t} \log p(c \mid \mathbf{x}_t) = \nabla_{\mathbf{x}_t} \log p(\mathbf{x}_t \mid c) - \nabla_{\mathbf{x}_t} \log p(\mathbf{x}_t). \tag{6}

In noise form:

ϵimplicitϵθ(xt,t,)ϵθ(xt,t,c).(7)\boldsymbol{\epsilon}_{\text{implicit}} \propto \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, \varnothing) - \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, c). \tag{7}

Theorem 2 (CFG as Amplified Classifier Guidance). CFG (3) corresponds to modified score:

~logp(xtc)=logp(xt)+(1+w)[logp(xtc)logp(xt)].(8)\tilde{\nabla} \log p(\mathbf{x}_t \mid c) = \nabla \log p(\mathbf{x}_t) + (1 + w)\left[\nabla \log p(\mathbf{x}_t \mid c) - \nabla \log p(\mathbf{x}_t)\right]. \tag{8}

Equivalent to classifier guidance with scale (1+w)(1 + w) without explicit classifier.

Proof. Substitute (6) into guided score; rearrange to (3). \blacksquare


The Guidance Scale

  • 0 — Standard conditional sampling
  • 1–4 — Moderate enhancement
  • 7.5 — Stable Diffusion default
  • 1\gg 1 — High fidelity, low diversity, artifacts

Proposition 3. At w=0w = 0: ϵ~=ϵθ(xt,t,c)\tilde{\boldsymbol{\epsilon}} = \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, c) — pure conditional.


Quality-Diversity Tradeoff

Theorem 3 (Sharpened Distribution). CFG samples from effective distribution:

p~(xc)p(xc)1+wp(x)w.(9)\tilde{p}(\mathbf{x} \mid c) \propto p(\mathbf{x} \mid c)^{1+w} \cdot p(\mathbf{x})^{-w}. \tag{9}

Proof sketch. From (8): logp~=(1+w)logp(xc)wlogp(x)+C\log \tilde{p} = (1+w)\log p(\mathbf{x}|c) - w\log p(\mathbf{x}) + C. \blacksquare

Consequences:

  • High ww: concentrates on high p(cx)p(c \mid \mathbf{x}) regions — sharp, mode-collapsed
  • Low ww: closer to true conditional — diverse but less aligned
  • CLIP score increases with ww; FID has optimal ww

Computational Cost

Proposition 4. Each sampling step requires two forward passes: conditional and unconditional. Total inference cost 2×\approx 2\times baseline.

Both passes can be batched: stack (xt,xt)(\mathbf{x}_t, \mathbf{x}_t) with conditions (c,)(c, \varnothing).


Worked Examples

Example 1: w=7.5w = 7.5

ϵ~=6.5ϵ+7.5ϵc\tilde{\boldsymbol{\epsilon}} = -6.5\,\boldsymbol{\epsilon}_\varnothing + 7.5\,\boldsymbol{\epsilon}_c — strong push toward conditional.

Example 2: Identical Predictions

If ϵc=ϵ\boldsymbol{\epsilon}_c = \boldsymbol{\epsilon}_\varnothing, CFG has no effect regardless of ww.


Connection to the Broader Curriculum

  • Reverse Process — base sampling
  • Bayes' Theorem — score decomposition
  • Text conditioning via cross-attention (future article)
  • RAG — conditioning in LLM domain

Common Pitfalls and Misconceptions

Pitfall 1: w=0w = 0 is not unconditional — it's conditional without guidance.

Pitfall 2: Too-high ww causes oversaturation and artifacts.

Pitfall 3: Forgetting condition dropout during training makes ϵ\boldsymbol{\epsilon}_\varnothing undefined.

Pitfall 4: Confusing ww in (3) vs. wCGw_{\text{CG}} in (2) — related by offset.


Research Perspective

CFG (Ho & Salimans, 2022). Applied at scale in Stable Diffusion, Imagen. Research: guidance distillation (single-pass), autoguidance, negative prompting as cnegc_{\text{neg}}.


Summary of Takeaways

  • CFGϵ~=(1+w)ϵcwϵ\tilde{\boldsymbol{\epsilon}} = (1+w)\boldsymbol{\epsilon}_c - w\boldsymbol{\epsilon}_\varnothing
  • Implicit classifierϵϵc\boldsymbol{\epsilon}_\varnothing - \boldsymbol{\epsilon}_c
  • Effective distributionp~p(xc)1+w/p(x)w\tilde{p} \propto p(\mathbf{x} — c)^{1+w}/p(\mathbf{x})^w
  • Training — Drop condition with prob. puncondp_{\text{uncond}}
  • Cost — 2× forward passes

Next: Tokenization (Volume III, LLMs)


Exercises

Exercise 1. Derive (8) from (3) using score–noise identity.

Exercise 2. Prove (9) from modified score.

Exercise 3. What happens as ww \to \infty?

Exercise 4. Design training schedule for puncondp_{\text{uncond}}.

Exercise 5. Compare CFG to Logistic Regression classifier guidance conceptually.

Exercise 6. Batch CFG: write combined forward pass dimensions.

Exercise 7. Negative prompting as alternative condition.

Exercise 8. Why does CFG improve CLIP score but harm diversity?