Guidance Rescaling to Fix Over-Exposure
Problem Statement
High CFG scales inflate the predicted-noise magnitude, over-exposing images. Lin et al.'s guidance rescale trick renormalizes the guided prediction back toward the conditional prediction's standard deviation. Implement it.
Background
Let eps_cfg be the guided prediction and eps_cond the plain conditional one. Compute their standard deviations (over all elements, population std), rescale the guided prediction to the conditional's std, then blend by a factor phi:
εrescaled​=εcfg​⋅std(εcfg​)std(εcond​)​,εfinal​=ϕεrescaled​+(1−ϕ)εcfg​
phi = 0 disables the fix; phi = 1 fully rescales. If std(eps_cfg) is 0, skip the rescale (return eps_cfg).
Your Task
Implement:
def guidance_rescale(eps_cfg, eps_cond, phi):
Return the final prediction as a list rounded to 4 decimals.
Input Format
- eps_cfg, eps_cond: lists of equal length D.
- phi (float) in [0, 1].
Output Format
- A list of D floats rounded to 4 decimals.
Sample
print(guidance_rescale([2.0, -2.0], [1.0, -1.0], 1.0))
Output:
[1.0, -1.0]
Example:
print(guidance_rescale([2.0, -2.0], [1.0, -1.0], 1.0))
[1.0, -1.0]
- Compute the population standard deviation of the guided prediction
eps_cfg = [2.0, -2.0]. The mean is 0, so std(εcfg​)=2(2.0)2+(−2.0)2​​=4​=2.0. - Compute the population standard deviation of the conditional prediction
eps_cond = [1.0, -1.0]. The mean is 0, so std(εcond​)=2(1.0)2+(−1.0)2​​=1​=1.0. - Since std(εcfg​)î€ =0, calculate the rescaled prediction by scaling
eps_cfgby the ratio of the standard deviations: εrescaled​=[2.0,−2.0]⋅2.01.0​=[1.0,−1.0]. - Blend the rescaled prediction with the original guided prediction using ϕ=1.0. This fully applies the rescaling: εfinal​=1.0⋅[1.0,−1.0]+(1−1.0)⋅[2.0,−2.0]=[1.0,−1.0].
- The final output is [1.0, -1.0]
Constraints:
len(eps_cfg) == len(eps_cond),0 <= phi <= 1.- Use population std (ddof=0) over all elements.
- If
std(eps_cfg) == 0, returneps_cfgunchanged. - Round to 4 decimals; avoid
-0.0.
1. Background Knowledge
In classifier-free guidance (CFG), the model makes two noise predictions per step: an unconditional one εuncond​ and a conditional one εcond​. The guided prediction is formed as εcfg​=εuncond​+w(εcond​−εuncond​), where w is the guidance scale. As w grows, the magnitude of εcfg​ inflates, which in latent space translates to over-exposed or saturated images.
The guidance rescale trick (Lin et al.) observes that the direction of the guided prediction is useful, but its scale is not. By renormalizing εcfg​ so that its standard deviation matches that of εcond​, you keep the guidance direction while restoring a sane noise magnitude. A blending factor ϕ∈[0,1] lets you interpolate between the raw guided prediction (ϕ=0) and the fully rescaled one (ϕ=1).
The standard deviation used here is the population standard deviation (divide by N, not N−1), computed over all elements of the vector. This is a simple element-wise scaling operation followed by a convex combination.
2. Algorithm Approach
This is a vector normalization and blending problem. The core pattern is:
- Compute a scalar statistic (std) from each input vector.
- Use the ratio of the two scalars as a multiplicative scale factor.
- Apply the scale to one vector, then take a weighted average with the original.
No loops over dimensions are needed beyond the final element-wise operations. The entire computation reduces to a handful of scalar arithmetic steps plus one pass over the vectors for the std calculations and the final blend.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.