Adaptive CFG Scheduling over the Trajectory
Problem Statement
A single fixed guidance scale is suboptimal: strong guidance early adds detail, strong guidance late over-saturates. Implement a linearly interpolated CFG schedule and apply it across a batch of per-step noise predictions.
Background
Given K sampling steps ordered from first (high noise) to last (low noise), the guidance scale ramps linearly from w_start at step 0 to w_end at step K-1:
wk​=wstart​+K−1k​(wend​−wstart​)
(with w_0 = w_start when K == 1). At each step apply CFG with that step's scale:
ε^k​=εkuncond​+wk​(εkcond​−εkuncond​)
Your Task
Implement:
def scheduled_cfg(eps_uncond, eps_cond, w_start, w_end):
- eps_uncond[k], eps_cond[k]: the per-step predictions (each a list of length D), K steps total.
Return the list of K guided predictions, each a list rounded to 4 decimals.
Input Format
- eps_uncond, eps_cond: K x D nested lists.
- w_start, w_end (float).
Output Format
- A K x D nested list rounded to 4 decimals.
Sample
u = [[0.0], [0.0], [0.0]]
c = [[1.0], [1.0], [1.0]]
print(scheduled_cfg(u, c, 10.0, 2.0))
Output:
[[10.0], [6.0], [2.0]]
Example:
u = [[0.0], [0.0], [0.0]] c = [[1.0], [1.0], [1.0]] print(scheduled_cfg(u, c, 10.0, 2.0))
[[10.0], [6.0], [2.0]]
- Determine the number of steps K=3 and compute the linearly interpolated guidance scales wk​ for k∈{0,1,2} using wk​=10.0+2k​(2.0−10.0), yielding w0​=10.0, w1​=6.0, and w2​=2.0.
- Calculate the difference between conditional and unconditional predictions for each step: εkcond​−εkuncond​=1.0−0.0=1.0 for all k.
- Apply the CFG formula ε^k​=0.0+wk​(1.0) at step 0 to get ε^0​=10.0⋅1.0=10.0.
- Apply the formula at step 1 to get ε^1​=6.0⋅1.0=6.0.
- Apply the formula at step 2 to get ε^2​=2.0⋅1.0=2.0.
- The final output is [[10.0], [6.0], [2.0]]
Constraints:
len(eps_uncond) == len(eps_cond) == K,K >= 1; rows share lengthD.w_kramps linearly fromw_start(k=0) tow_end(k=K-1);K==1usesw_start.- Per step:
eps_uncond + w_k*(eps_cond - eps_uncond); round to 4 decimals; avoid-0.0.
1. Background Knowledge
Classifier-Free Guidance (CFG) is a technique used in latent diffusion models to steer the denoising process toward the conditioning signal (e.g., a text prompt) without requiring a separate classifier. At each sampling step, the model makes two noise predictions: one conditioned on the prompt (εcond) and one unconditioned (εuncond). The guided prediction is a linear extrapolation:
ε^=εuncond+w(εcond−εuncond)
where w is the guidance scale. A larger w pushes the sample further from the unconditioned manifold, increasing fidelity to the prompt but risking over-saturation or artifacts.
A single fixed w is suboptimal because the noise level changes dramatically across the K sampling steps. Early steps operate at high noise, where strong guidance helps establish coarse structure and detail. Late steps operate at low noise, where strong guidance over-saturates colors and textures. An adaptive schedule addresses this by interpolating w linearly from wstart​ (step 0) to wend​ (step K−1):
wk​=wstart​+K−1k​(wend​−wstart​)
This is a simple linear interpolation (lerp) between two endpoints. When K=1, the fraction K−1k​ is undefined, so the convention is w0​=wstart​.
2. Algorithm Approach
The problem is a straightforward element-wise vector operation combined with a linear schedule computation:
- Compute the schedule: For each step index k∈{0,…,K−1}, calculate wk​ via linear interpolation.
- Apply CFG per step: For each step k, compute the guided prediction by combining the unconditioned and conditioned noise vectors using the scalar wk​.
- Round and return: Round each component to 4 decimal places and assemble the result.
No iterative refinement, sorting, or complex data structures are needed. The core pattern is a nested loop (or vectorized operation) over steps and dimensions.
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.