PIXELBANKv9.1.0
Menu

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+kK−1 (wend−wstart)w_k = w_{\text{start}} + \frac{k}{K-1}\,(w_{\text{end}} - w_{\text{start}})

(with w_0 = w_start when K == 1). At each step apply CFG with that step's scale:

ε^k=εkuncond+wk (εkcond−εkuncond)\hat{\varepsilon}_k = \varepsilon^{\text{uncond}}_k + w_k\,(\varepsilon^{\text{cond}}_k - \varepsilon^{\text{uncond}}_k)

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:

Input:
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]]
Reasoning:
  • Determine the number of steps K=3K=3 and compute the linearly interpolated guidance scales wkw_k for k∈{0,1,2}k \in \{0, 1, 2\} using wk=10.0+k2(2.0−10.0)w_k = 10.0 + \frac{k}{2}(2.0 - 10.0), yielding w0=10.0w_0 = 10.0, w1=6.0w_1 = 6.0, and w2=2.0w_2 = 2.0.
  • Calculate the difference between conditional and unconditional predictions for each step: εkcond−εkuncond=1.0−0.0=1.0\varepsilon^{\text{cond}}_k - \varepsilon^{\text{uncond}}_k = 1.0 - 0.0 = 1.0 for all kk.
  • Apply the CFG formula ε^k=0.0+wk(1.0)\hat{\varepsilon}_k = 0.0 + w_k(1.0) at step 0 to get ε^0=10.0â‹…1.0=10.0\hat{\varepsilon}_0 = 10.0 \cdot 1.0 = 10.0.
  • Apply the formula at step 1 to get ε^1=6.0â‹…1.0=6.0\hat{\varepsilon}_1 = 6.0 \cdot 1.0 = 6.0.
  • Apply the formula at step 2 to get ε^2=2.0â‹…1.0=2.0\hat{\varepsilon}_2 = 2.0 \cdot 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 length D.
  • w_k ramps linearly from w_start (k=0) to w_end (k=K-1); K==1 uses w_start.
  • Per step: eps_uncond + w_k*(eps_cond - eps_uncond); round to 4 decimals; avoid -0.0.
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.