A conditional diffusion model may understand a prompt and still produce samples that only weakly reflect it. During generation, developers therefore often want a way to push the denoising trajectory toward the condition without training a separate classifier for every prompt or label.
Classifier-free guidance (CFG) is a widely used way to do that. At each denoising step, the model is evaluated with the condition and without it. The difference between those predictions gives a direction associated with the condition, and a guidance scale controls how strongly sampling moves along that direction.
The scale is not a generic quality knob. Increasing it strengthens the conditional correction, but can reduce diversity or push sampling into regimes where artifacts become more likely. This article builds the mechanism from a small numerical example, then explains the training requirement, inference cost, and practical limits that matter when using CFG.
Start with two predictions for the same noisy sample
Suppose a text-conditioned diffusion model is denoising a latent representation z_t for the prompt:
a red bicycle beside a stone wallAt one denoising step, evaluate the same noisy latent twice:
conditional: model(z_t, prompt)
unconditional: model(z_t, empty_condition)For a simplified one-dimensional teaching example, imagine those predictions are:
conditional prediction = 0.30
unconditional prediction = 0.50Their difference is:
0.30 - 0.50 = -0.20That difference is the useful signal. It represents how the model’s prediction changes when the condition is present.
A common CFG parameterization is:
guided = unconditional + s * (conditional - unconditional)where s is the guidance scale. With s = 1:
guided = 0.50 + 1 * (0.30 - 0.50)
= 0.30The guided prediction is exactly the conditional prediction. With s = 3:
guided = 0.50 + 3 * (0.30 - 0.50)
= -0.10Now the prediction goes beyond the conditional estimate in the same direction away from the unconditional estimate. That extrapolation is the core idea behind classifier-free guidance.
The numbers above are deliberately scalar. Real diffusion predictions are high-dimensional tensors, but the linear combination is applied element by element in the same way.
The mental model: amplify what the condition changes
It helps to separate the calculation into a baseline and a correction:
baseline = unconditional
correction = conditional - unconditional
guided = baseline + s * correctionThe unconditional prediction asks what the denoiser would predict without the requested condition. The conditional prediction asks what it predicts when the condition is supplied. Subtracting them isolates a direction associated with conditioning, according to the model.
CFG then scales that direction before the sampler uses the result for the next denoising step.
This explains several useful boundary cases:
s = 0 -> unconditional prediction
s = 1 -> ordinary conditional prediction
s > 1 -> extrapolate farther in the conditional directionSome papers and libraries define the guidance parameter differently. For example, the original CFG paper writes an equivalent form in which a parameter w appears as:
guided = (1 + w) * conditional - w * unconditionalSetting s = 1 + w makes the two expressions equivalent. This is why a numeric guidance value should not be compared across implementations until you know which convention the implementation uses.
Why the model can make an unconditional prediction
Classifier-free guidance requires more than changing an inference formula. The model must have learned how to denoise both conditionally and unconditionally.
A standard training idea is to randomly remove the condition for some training examples:
example A -> condition kept
example B -> condition removed
example C -> condition kept
example D -> condition removedThe same denoising network therefore sees both modes during training. For a text-conditioned model, removing the condition may be represented by an empty or null conditioning input. The exact representation is model-specific.
Conceptually, training teaches one network to approximate two related predictions:
model(z_t, condition) -> conditional prediction
model(z_t, null_condition) -> unconditional predictionThis is the “classifier-free” part of the name. Earlier classifier guidance methods use gradients from a separate classifier to steer diffusion sampling. CFG instead obtains its guidance direction from conditional and unconditional predictions of the diffusion model itself.
You cannot assume that an arbitrary conditional diffusion model supports CFG merely because its inference API exposes a numeric scale. The training procedure and model interface must provide a meaningful unconditional or null-conditioned prediction.
Guidance happens at every denoising step
Diffusion sampling repeatedly transforms a noisy state toward a cleaner sample. CFG changes the model prediction used inside that repeated process:
z_T
|
| conditional + unconditional predictions
v
z_(T-1)
|
| conditional + unconditional predictions
v
...
|
v
z_0A simplified loop looks like this:
for each denoising step t:
u = model(z_t, null_condition, t)
c = model(z_t, condition, t)
prediction = u + scale * (c - u)
z_t = sampler_step(z_t, prediction, t)This is pseudo-code, not a production sampler. Diffusion systems can predict different parameterizations, and the sampler determines how a model prediction updates the current state. CFG must combine quantities in the representation expected by that particular model and sampler.
The important invariant is simpler: the conditional and unconditional predictions must refer to the same noisy state and denoising step before their difference is meaningful.
Guidance scale creates a trade-off, not a free improvement
Why use s > 1 at all? The conditional model prediction may not emphasize the condition as strongly as desired during sampling. Extrapolating along the conditional direction can make generated samples adhere more strongly to that conditioning signal.
But stronger extrapolation changes the distribution being sampled. It is therefore incorrect to treat larger guidance values as automatically better.
Consider three conceptual settings:
low guidance -> more influence from the unconditional model
moderate -> stronger pressure toward the condition
very high -> aggressive extrapolationThe useful operating point depends on the model, condition, sampler, number of denoising steps, and the application’s quality criteria. Strong guidance can trade diversity for stronger conditioning, and excessive values can degrade sample quality.
This has an evaluation consequence: do not tune guidance only by inspecting a few attractive outputs. If diversity matters, evaluate it explicitly alongside condition adherence and perceptual or task-specific quality.
CFG can increase inference work
The basic formulation needs a conditional and an unconditional prediction at each denoising step. A naive implementation therefore performs two model evaluations where unguided conditional sampling would require one.
Many implementations combine the two inputs into a batch:
[unconditional input, conditional input] -> one batched model callThis can reduce dispatch overhead and exploit parallel hardware, but it does not make the second prediction computationally free. It generally increases the amount of model work and may increase memory use. The actual latency impact depends on hardware utilization, batch shape, model architecture, and serving implementation.
This matters in production because guidance interacts with another major diffusion cost: the number of denoising steps. A configuration that looks acceptable for one offline image may be too expensive for an interactive or high-throughput service.
Measure the full generation path rather than assuming that a particular batching strategy halves the practical cost.
Keep guidance conventions explicit in application code
A small helper can prevent ambiguity about what a scale means:
function classifier_free_guidance(unconditional, conditional, scale):
return unconditional + scale * (conditional - unconditional)With this definition, scale = 1 means the ordinary conditional prediction. That boundary condition is worth testing.
Useful implementation tests include:
scale = 0 -> output equals unconditional
scale = 1 -> output equals conditional
conditional = unconditional -> scale has no effectThe third test is particularly informative. If the condition does not change the model prediction, multiplying the difference cannot create conditioning information that is not there.
For tensor implementations, also verify shape and batch alignment. Accidentally pairing the unconditional prediction from one sample with the conditional prediction from another produces a valid-looking tensor with invalid semantics.
Common mistakes hide what CFG actually controls
Calling the scale a probability
A guidance scale such as 5 or 7 is not a confidence percentage or a probability that the prompt is correct. It is a coefficient in a linear combination of model predictions.
Assuming the same number means the same behavior everywhere
Different model families, training procedures, samplers, parameter conventions, and APIs can make identical numeric values behave differently. Treat guidance scale as a parameter to validate for a specific generation pipeline.
Tuning on one seed
Diffusion generation is stochastic in many common sampling setups. A scale that looks good on one random seed may reveal reduced diversity or artifacts across a broader sample. Compare settings over a representative prompt set and multiple samples per condition when those variations matter to the application.
Forgetting the unconditional branch during optimization
Caching or reusing work can be valuable, but the unconditional prediction still depends on the current noisy state and denoising step. In the basic CFG procedure, it cannot simply be computed once at the beginning and reused for every step.
Treating CFG as a substitute for a capable model
Guidance amplifies the difference the model has learned between conditional and unconditional behavior. It does not supply missing concepts, repair poor conditioning data, or guarantee correct composition of details in a complex prompt.
When classifier-free guidance is a good fit
CFG is useful when a diffusion model was trained with conditional dropout or an equivalent mechanism, the application benefits from stronger condition adherence, and the additional inference work is acceptable.
It is less attractive when latency or compute is extremely constrained, when the model does not provide a meaningful unconditional path, or when ordinary conditional sampling already meets the application’s requirements. In those cases, adding guidance can introduce cost and tuning complexity without solving a real problem.
It is also worth separating CFG from other controls. Changing the random seed explores different stochastic outcomes. Changing the number or type of sampling steps changes the numerical generation process. Changing the prompt changes the condition itself. Guidance scale specifically changes how strongly the conditional-versus-unconditional prediction difference affects denoising.
Conclusion
Classifier-free guidance is easiest to reason about as a controlled extrapolation. The diffusion model predicts the same noisy state with and without a condition, the difference provides a conditional direction, and the guidance scale determines how far sampling moves along that direction.
That mental model makes the practical trade-offs clearer. A stronger scale can increase conditioning pressure, but it is not guaranteed to improve overall quality and can reduce diversity. The basic method also requires both conditional and unconditional predictions throughout denoising, so guidance has real inference cost.
When implementing or tuning CFG, make the scale convention explicit, test its boundary cases, evaluate more than one quality dimension, and measure the actual serving cost. Those checks turn a commonly exposed generation knob into a parameter you can reason about rather than tune blindly.