Combine a conditional and an unconditional noise prediction into the guided prediction that every text-to-image sampler actually uses.
Classifier-free guidance (Ho & Salimans, 2022) drops the conditioning at random during training so one network learns both ε^θ(xt,t,c) and ε^θ(xt,t,∅). At sampling time the two are extrapolated:
ε^guided=ε^θ(xt,t,∅)+w(ε^θ(xt,t,c)−ε^θ(xt,t,∅))
The bracket is (proportional to) the score of the implicit classifier p(c∣xt): the direction in noise space that makes the sample more like the prompt. Guidance walks along it.
Read off the boundary cases, because they are the whole intuition: w=0 gives the unconditional model, w=1 gives the plain conditional model (no guidance at all), and w>1 extrapolates past the conditional prediction. The usual Stable Diffusion default of guidance_scale = 7.5 is therefore a 7.5x overshoot, trading diversity for prompt fidelity. Note that w = 1 meaning "off" is exactly why some codebases skip the second forward pass when guidance_scale <= 1.
Implement:
def cfg_combine(eps_uncond, eps_cond, w):
Return the guided noise prediction, shaped like the inputs.
A NumPy array shaped like the inputs.
u = np.array([0.1, -0.2])
c = np.array([0.3, 0.2])
print(np.round(cfg_combine(u, c, 7.5), 4).tolist())
The difference is [0.2, 0.4], so the result is [0.1 + 7.50.2, -0.2 + 7.50.4] = [1.6, 2.8].
u = np.array([0.1, -0.2]) c = np.array([0.3, 0.2]) print(np.round(cfg_combine(u, c, 7.5), 4).tolist())
[1.6, 2.8]
The conditional direction is c - u = [0.2, 0.4]. Guidance walks 7.5 times along it from the unconditional base: 0.1 + 7.5*0.2 = 1.6 and -0.2 + 7.5*0.4 = 2.8. Both components are pushed well past the conditional prediction itself -- that overshoot is exactly what a guidance scale above 1 means.
w may be any float, including 0.0 and values below 1.w = 1.0 must return eps_cond exactly.