Fifty Steps or Five

Diffusion ModelsHard+30 XP+30

Fifty Steps or Five

The number of sampling steps is yours to choose, which means a sampler has to be able to jump from step 60 straight to step 40 without visiting anything in between. Doing that correctly is the whole trick.

Write αˉt\bar{\alpha}_tαˉt​ for the signal remaining at step ttt, with αˉ0=1\bar{\alpha}_0 = 1αˉ0​=1 (nothing destroyed). One jump, from step ttt down to an earlier step ppp, is three moves:

  1. Predict the noise ε^\hat{\varepsilon}ε^ in the current latent xtx_txt​.
  2. Strip all of it out, giving the denoiser's guess at the clean image:

x^0=xt−1−αˉt  ε^αˉt\hat{x}_0 = \frac{x_t - \sqrt{1 - \bar{\alpha}_t}\; \hat{\varepsilon}}{\sqrt{\bar{\alpha}_t}}x^0​=αˉt​​xt​−1−αˉt​​ε^​

  1. Put back exactly as much noise as step ppp is supposed to have:

xp=αˉp  x^0  +  1−αˉp  ε^x_p = \sqrt{\bar{\alpha}_p}\; \hat{x}_0 \; + \; \sqrt{1 - \bar{\alpha}_p}\; \hat{\varepsilon}xp​=αˉp​​x^0​+1−αˉp​​ε^

Move 3 is what makes a jump of any size legal: the latent is rebuilt at whatever noise level you asked for, instead of being nudged down by one.

The denoiser here is a toy one, so the arithmetic is reproducible: at step ttt it predicts the noise to be c[t] times whatever latent it is shown, elementwise.

Task: write denoise(x, abar, c, steps), returning the finished result at step 0 as a plain list, rounded to 4 decimal places — and rounded only there, never between jumps.

  • x is the starting latent, sitting at step steps[0].
  • abar[t] is the signal remaining at step t, and abar[0] is 1.0.
  • c[t] is the toy denoiser's multiplier at step t.
  • steps is strictly decreasing and every entry is at least 1. Visit those steps in order; after the last one, the next stop is step 0.

The two visible tests are the same latent and the same schedule, differing only in which steps get visited — and they do not agree. Skipping is an approximation, and the gap between those two answers is the speed-versus-quality dial every diffusion tool puts in front of you.

Examples

x = [0.8, -0.4]
abar = [1.0, 0.9, 0.75, 0.6, 0.4, 0.2, 0.05]
c = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
steps = [6, 4, 2]
returns [1.5604, -0.7802]
x = [0.8, -0.4]
abar = [1.0, 0.9, 0.75, 0.6, 0.4, 0.2, 0.05]
c = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
steps = [6, 5, 4, 3, 2, 1]
returns [1.5878, -0.7939]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run