ML//diffusion model//CFG

Classifier-free guidance (CFG) is a technique that dramatically improves how well diffusion models follow text prompts, by amplifying the difference between conditioned and unconditioned generation. At each denoising step the model runs twice, once with the text prompt (conditioned) and once without it (unconditioned), and the output is extrapolated away from the unconditioned prediction toward the conditioned one:


Classifier-free guidance (CFG) is a technique that dramatically improves how well diffusion models follow text prompts, by amplifying the difference between conditioned and unconditioned generation. At each denoising step the model runs twice, once with the text prompt (conditioned) and once without it (unconditioned), and the output is extrapolated away from the unconditioned prediction toward the conditioned one:

output=unconditioned+s (conditioned−unconditioned)\text{output} = \text{unconditioned} + s\,(\text{conditioned} - \text{unconditioned})output=unconditioned+s(conditioned−unconditioned)

The guidance scale sss controls the trade-off. At s=1s=1s=1 the generation is normal (the unconditioned prediction is ignored); between 7 and 15 the model follows the prompt more strongly at the cost of diversity; too high, and the images become oversaturated and full of artifacts.

The method is called classifier-free because an earlier method (classifier guidance) used a separate classifier network to steer generation. CFG eliminated that dependency: the model itself provides both predictions during training by randomly dropping the conditioning (text) a fraction of the time.

It is ubiquitous in text-to-image: Stable Diffusion, DALL-E 2/3, Imagen, Flux all use CFG. It's the reason these models can produce images that tightly match complex text descriptions instead of vague approximations.

The computational cost is two forward passes per denoising step instead of one (or a single batched pass with double the batch size). This is why diffusion generation is slow. But the quality improvement is so large that nobody skips it.