Classifier-Free Guidance: Uncond/Cond Mixing
Problem Statement
Classifier-free guidance mixes the conditional and unconditional noise predictions to steer generation toward the prompt. Implement the standard linear combination.
Background
Given the unconditional prediction eps_uncond and the conditional prediction eps_cond, the guided noise at guidance scale w is
ε^=εuncond​+w(εcond​−εuncond​)
At w = 1 this is just the conditional prediction; larger w extrapolates further in the prompt direction (sharper adherence, less diversity).
Your Task
Implement:
def cfg_combine(eps_uncond, eps_cond, w):
Return the guided noise as a list rounded to 4 decimals.
Input Format
- eps_uncond, eps_cond: lists of length D.
- w (float): guidance scale.
Output Format
- A list of D floats rounded to 4 decimals.
Sample
print(cfg_combine([0.0, 0.0], [1.0, 2.0], 7.5))
Output:
[7.5, 15.0]
Example:
print(cfg_combine([0.0, 0.0], [1.0, 2.0], 7.5))
[7.5, 15.0]
- Identify the input vectors and guidance scale: the unconditional prediction is εuncond​=[0.0,0.0], the conditional prediction is εcond​=[1.0,2.0], and the scale is w=7.5.
- Compute the difference between the conditional and unconditional predictions element-wise to determine the direction of the prompt: [1.0−0.0,2.0−0.0]=[1.0,2.0].
- Scale this difference by the guidance weight w to amplify the conditional signal: 7.5×[1.0,2.0]=[7.5,15.0].
- Add the scaled difference to the unconditional baseline to obtain the guided noise: [0.0,0.0]+[7.5,15.0]=[7.5,15.0].
- Round the resulting values to 4 decimal places, which leaves them unchanged as they are already exact: [7.5,15.0].
- The final output is [7.5, 15.0]
Constraints:
len(eps_uncond) == len(eps_cond).eps = eps_uncond + w*(eps_cond - eps_uncond).- Round to 4 decimals; avoid
-0.0.
1. Background Knowledge
Classifier-free guidance (CFG) is a technique used in diffusion models to steer generation toward a text prompt without requiring a separate classifier. During training, the model is trained to predict noise both with and without the conditioning signal (e.g., a text embedding). At inference time, the model is run twice: once with the prompt (yielding εcond​) and once without (yielding εuncond​). The final noise estimate is a linear extrapolation from the unconditional prediction in the direction of the conditional prediction.
The guiding principle is that the difference εcond​−εuncond​ captures the "direction" in latent space that moves the sample toward the prompt. By scaling this difference by a weight w and adding it back to the unconditional baseline, you control how strongly the generation adheres to the prompt. At w=1, the formula reduces to the pure conditional prediction. For w>1, the model extrapolates beyond the conditional prediction, producing sharper, more prompt-aligned outputs at the cost of diversity. For w<1, the result is a blend closer to the unconditional (unprompted) generation.
This linear combination is mathematically equivalent to:
ε^=(1−w)εuncond​+wεcond​
Both forms are algebraically identical; the first form makes the "extrapolation" interpretation more transparent, while the second form is a standard convex combination (or its extension) of two vectors.
2. Algorithm Approach
The problem is a straightforward element-wise vector operation. You are given two vectors of equal length D and a scalar w. For each index i, compute:
ε^i​=εuncond,i​+w⋅(εcond,i​−εuncond,i​)
This is a per-element affine transformation. No loops over multiple iterations, no matrix multiplications, no iterative refinement — just a single pass over the vectors. The key insight is recognizing that this is a linear interpolation/extrapolation between two points in RD.
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.