Negative Prompt as the Unconditional Branch
Problem Statement
In practice the "unconditional" branch of CFG is replaced by a negative prompt prediction, steering away from unwanted content. Implement guidance with an explicit negative branch and report the resulting noise plus how far it moved from the plain conditional prediction.
Background
With a negative-prompt prediction eps_neg in place of the unconditional one, the guided noise is
The larger the gap between eps_cond and eps_neg, the more the guidance pushes. Report the guided prediction and the L2 distance between it and eps_cond (how much guidance changed the base prediction).
Your Task
Implement:
def negative_prompt_guidance(eps_cond, eps_neg, w):
Return a dict with "eps" (list rounded to 4 decimals) and "shift" (L2 distance from eps_cond, rounded to 4 decimals).
Input Format
- eps_cond, eps_neg: lists of equal length D.
- w (float): guidance scale.
Output Format
- A dict with a list and a float.
Sample
print(negative_prompt_guidance([1.0, 0.0], [0.0, 0.0], 2.0))
Output:
{'eps': [2.0, 0.0], 'shift': 1.0}
Example
print(negative_prompt_guidance([1.0, 0.0], [0.0, 0.0], 2.0))
{'eps': [2.0, 0.0], 'shift': 1.0}-
Compute the guided noise vector using the formula :
- For the first element:
- For the second element:
- Resulting vector:
-
Calculate the difference between the guided noise and the original conditional prediction to determine the shift:
-
Compute the L2 distance (shift) by taking the square root of the sum of squared differences:
-
Round the results to 4 decimal places as required:
-
The final output is
{'eps': [2.0, 0.0], 'shift': 1.0}
Constraints
len(eps_cond) == len(eps_neg).eps = eps_neg + w*(eps_cond - eps_neg).shift = ||eps - eps_cond||_2; round both to 4 decimals; avoid-0.0.