Foundations to Frontier
Tests
H. Generative Models › H7 Diffusion Models

Classifier-free guidance

Classifier-free guidance (CFG; Ho and Salimans, 2022) gets the benefits of classifier guidance without a classifier. One network is trained both with and without the condition by dropping labels or captions about 10% of the time, and at sampling time the two predictions are extrapolated: ε̃ = ε_u + w(ε_c − ε_u). Weight w = 1 is the conditional model; w > 1 trades diversity for fidelity and is behind almost every text-to-image and text-to-video system (Stable Diffusion's default is 7.5). This page derives CFG as guidance by an implicit classifier, covers the training recipe, the cost of two network evaluations per step, guidance intervals, CFG rescale and distillation, and explains why CFG does not sample the 'tilted' density p(x|y)^w p(x)^(1−w). In the exercises you implement label dropout and the CFG combination. You then sample a six-mode toy problem with exact mixture scores in place of a trained network. Weight 2 cut the ambiguous mode's share from 0.30 to 0.064, although the tilted density keeps 0.294. Weight 8 pushed samples off the modes (spread 0.67 against 0.40). Guidance applied only at low noise gave every sample the right class without changing the mode weights.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Turn up the ‘more like the prompt’ knob

A text-to-image model can make a picture with or without being told what to draw. Classifier-free guidance asks it for both at every step and then exaggerates the difference: take the unprompted guess, look at how the prompted guess differs, and move several times further in that direction. The result follows the prompt much more strongly, at the cost of variety and, if pushed too far, over-cooked images. The demo shows what that exaggeration does to a simple mixture.

Conditional diffusion lab
Green: p(x|B)^w·p(x)^(1−w), the density classifier-free guidance aims at with weight w. w = 1 is the conditional (dashed); w = 0 is unconditional (grey); w > 1 extrapolates and drains the inner mode (inner mass 0.45). A real sampler applies this to the noised densities at every step, which is not quite the same.
Why engineers care

Nearly every deployed image, video and audio diffusion model uses classifier-free guidance, and the guidance weight is one of the few knobs exposed to users. Knowing what it does helps when generated data feed downstream training, for example synthetic satellite scenes for detector training, where too much guidance quietly removes rare cases.

📝 Quick check (Level 0)
0/3 answered
  1. 1. CFG combines predictions…
  2. 2. w = 1 gives…
  3. 3. A large w tends to reduce…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

An implicit classifier

  • Training: one network ε_θ(x_t, t, c); replace c by a null token ∅ with probability p_uncond ≈ 0.1–0.2, so it also learns ε_θ(x_t, t, ∅).
  • Sampling: ε̃ = ε_θ(x_t, ∅) + w (ε_θ(x_t, c) − ε_θ(x_t, ∅)); equivalently (1 + γ) ε_c − γ ε_u with γ = w − 1 (the convention of the original paper).
  • Why it works: ∇log p(c|x_t) = ∇log p(x_t|c) − ∇log p(x_t), so ε_c − ε_u is (up to −√(1−ᾱ)) the gradient of an implicit classifier; CFG with weight w is classifier guidance with that classifier at scale w on the unconditional model, or at scale w − 1 on the conditional model (the paper’s γ).
  • Gaussian case: N(m_c, v_c)^w N(m_u, v_u)^(1−w) is Gaussian with precision w/v_c − (w−1)/v_u; the mean moves beyond m_c, away from m_u, and the variance shrinks.
  • Cost: two network evaluations per step (batched as 2× batch); guidance distillation (Meng et al., 2023) and guidance-embedded models (FLUX.1-dev) fold it into one.
  • Refinements: CFG rescale against over-saturation (Lin et al., 2024); applying guidance only in a limited interval of noise levels (Kynkäänniemi et al., 2024); negative prompts replace ∅ by an unwanted condition; autoguidance uses a weaker model in place of the unconditional one (Karras et al., 2024).
  • Not the tilted density: applying the rule to noised densities at every t does not sample p(x|c)^w p(x)^(1−w) at t = 0 (Chidambaram et al., 2024; Bradley and Nakkiran, 2024).
📝 Quick check (Level 1)
0/3 answered
  1. 1. Typical label-dropout probability:
  2. 2. CFG with weight w equals classifier guidance on the conditional model at scale…
  3. 3. Network evaluations per sampling step with CFG:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

CFG in practice

Exercise 1.

  • Label dropout.
  • The CFG combination of two ε predictions.
  • The exact ε of a noised Gaussian mixture (our stand-in for a network).
  • The tilted log-density CFG is often said to sample.

Practice.

  • In diffusers the weight is guidance_scale (default 7.5 for Stable Diffusion 1.x; SDXL and SD3 commonly 5–7); sweep it and plot FID or human preference against CLIP score.
  • Train with p_uncond of 0.1–0.2 and an explicit null embedding (an empty prompt for text models).
  • Halve sampling cost with guidance distillation, or limit guidance to the middle noise levels.
  • Use guidance_rescale around 0.7 against over-saturated, over-contrasted images, especially with zero-terminal-SNR schedules.
  • Watch for lost diversity: report recall or coverage alongside fidelity, and lower w when generating training data.
import torch
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16).to("cuda")
for w in (1.0, 3.0, 7.5, 15.0):                      # w = 1: no guidance
    img = pipe("an aerial photo of a river delta", guidance_scale=w, num_inference_steps=30).images[0]
    img.save(f"delta_w{w}.png")
# inside the pipeline: eps = eps_u + w * (eps_c - eps_u), computed on a doubled batch
🐍 Exercise 1: label dropout and the CFG combination
Write label dropout, the CFG combination, the exact ε of a noised Gaussian mixture and the tilted log-density.
📝 Quick check (Level 2)
0/3 answered
  1. 1. In diffusers the CFG weight is called…
  2. 2. Over-saturated images at high w are reduced by…
  3. 3. When generating training data, a high w risks…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Implicit classifier. Bayes’ rule gives p(c|x_t) ∝ p(x_t|c)/p(x_t), so ∇log p(c|x_t) = −(ε_c − ε_u)/√(1−ᾱ_t). Substituting into classifier guidance of the conditional model with scale γ gives ε_c + γ(ε_c − ε_u), which is CFG with w = 1 + γ; applied to the unconditional model with scale w it gives ε_u + w(ε_c − ε_u) directly. So the w = 1 and w = 2 rows below coincide with classifier guidance at scales 1 and 2 on the classifier-guidance page’s setup. The ‘classifier’ is never trained; it is whatever difference the network learned between its two modes.

What is actually sampled. At each t the sampler follows the score of p_t(x|c)^w p_t(x)^(1−w). The family of these noised tilted densities is not the forward-noised family of any single clean distribution, so the endpoint is not p(x|c)^w p(x)^(1−w). In particular, at high noise two nearby modes of different classes are indistinguishable and the guidance term pushes samples away from both, a decision that the clean tilted density does not make. This is why CFG removes ambiguous modes more aggressively than the tilted density.

Extrapolation. For w > 1 the combined ε is outside the range of anything the network was trained to output; large w overshoots the data manifold (over-saturated colours in images), which rescaling and limited guidance intervals address.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The implicit classifier's gradient is proportional to…
  2. 2. Does CFG sample p(x|c)^w p(x)^(1−w) exactly?
  3. 3. Large w can move samples…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

What does the guidance weight do to the sample distribution, does CFG sample the tilted density, and at which noise levels does guidance matter?

History.

  • J. Ho and T. Salimans, “Classifier-free diffusion guidance” (NeurIPS workshop 2021; arXiv 2022).
  • A. Nichol et al., “GLIDE” (2022), and R. Rombach et al., “Latent diffusion” (2022): CFG for text-to-image.
  • T. Kynkäänniemi et al., “Applying guidance in a limited interval improves sample and distribution quality” (2024); M. Chidambaram et al., “What does guidance do?” (2024).

Exercise 2. The six-mode toy from the classifier-guidance page: class 0 on the left, class 1 on the right, with two overlapping central modes. We use the exact ε of the noised mixture, conditional and unconditional, in place of a trained network, so all effects come from the guidance rule. Deterministic DDIM, 50 steps, target class 1, 2000 samples.

  • Metrics: accuracy (exact clean posterior above 0.5), central share (true 1/3), TV of mode weights, and spread, the RMS distance to the nearest mode centre.
  • Tilted density: the central share of p(x|y)^w p(x)^(1−w), integrated on a grid.
  • Intervals: w = 4 inside a window of timesteps t (out of 1000), w = 1 outside.
guidance accuracy central share TV spread
w = 0 (unconditional) 0.487 0.163 0.512 0.402
w = 1 (conditional) 0.975 0.30 0.035 0.397
w = 2 1.0 0.064 0.27 0.395
w = 4 1.0 0.0 0.333 0.457
w = 8 1.0 0.0 0.333 0.67
w = 4 for t ≥ 500 only 1.0 0.034 0.299 0.44
w = 4 for 200 ≤ t ≤ 600 1.0 0.0 0.333 0.39
w = 4 for t ≤ 200 only 1.0 0.316 0.018 0.403
  • Diversity loss is fast: already at w = 2 the ambiguous mode kept only 0.064 of the samples.
  • Not the tilted density: the clean tilted density keeps a central share of 0.294 at w = 2 and 0.273 at w = 4, so CFG removed the mode far more than the density it is often described as sampling.
  • Off-manifold at large w: spread grew from 0.397 to 0.67 at w = 8; samples were pushed past the modes, the toy version of over-saturation.
  • Where guidance acts: the mode choice was made at moderate to high noise (windows covering t ≥ 200 removed the mode). Guidance only at t ≤ 200 gave accuracy 1.0 with correct mode weights: it fixed the few ambiguous samples without starving the mode. Unlike Kynkäänniemi et al.’s ImageNet finding, in this toy the useful window was the low-noise end, because the two central modes only become distinguishable at small noise.
  • Seeds 1 and 2: w = 2 central share 0.065 and 0.066; low-noise-only TV 0.013 and 0.012; w = 8 spread 0.67 and 0.675.
  • Caveat: exact scores, two classes, 2-D; with a learned network, the ε_c − ε_u difference is noisier and guidance also amplifies its errors.
🐍 Exercise 2 (research): what does the guidance weight do to the sample distribution, does CFG sample the tilted density, and at which noise levels does guidance matter?
Sample the six-mode toy with exact mixture ε and classifier-free guidance at several weights and noise windows, and compare with the tilted density.

Classifier-free guidance in AI, robotics and aerospace

  • Text-to-image and video: Stable Diffusion, Imagen, DALL·E 2 and 3, Sora-style video models and audio models all rely on CFG or a distilled version of it.
  • Robot policies: Diffusion Policy variants and goal-conditioned planners use CFG over goals or language instructions to trade exploration for goal adherence.
  • Synthetic data: generators of rare-event imagery (wildfires, flooded runways, debris) need modest weights; high weights erase the long tail the data were meant to supply.

References

  1. J. Ho and T. Salimans (2022). “Classifier-free diffusion guidance.” arXiv:2207.12598.
  2. A. Nichol et al. (2022). “GLIDE: towards photorealistic image generation and editing with text-guided diffusion models.” ICML.
  3. S. Lin, B. Liu, J. Li and X. Yang (2024). “Common diffusion noise schedules and sample steps are flawed.” WACV.
  4. T. Kynkäänniemi, M. Aittala, T. Karras, S. Laine, T. Aila and J. Lehtinen (2024). “Applying guidance in a limited interval improves sample and distribution quality in diffusion models.” NeurIPS.
  5. M. Chidambaram, K. Gatmiry, S. Chen, H. Lee and J. Lu (2024). “What does guidance do? A fine-grained analysis in a simple setting.” NeurIPS.
  6. C. Meng et al. (2023). “On distillation of guided diffusion models.” CVPR.