Compute Min-SNR loss weights

Why it matters Min-SNR weighting speeds up Stable Diffusion style training convergence.

Min-SNR weighting stops easy, low-noise timesteps from dominating diffusion training.

Write min_snr_weights(alphas_cumprod, t, gamma=5.0). SNR(t) = abar_t / (1 - abar_t). For epsilon prediction, the weight is min(SNR, gamma) / SNR. Return the weights for the timestep indices t.

Signaturedef min_snr_weights(alphas_cumprod, t, gamma=5.0)

Example

with abar [0.9, 0.5] and t [0, 1], SNR is [9, 1] and weights are [5/9, 1].

Understand the idea

Diffusion models train on many noise levels at once. Timesteps with little noise are easy, and their losses can dominate training. Min-SNR weighting caps how much each timestep counts. The signal-to-noise ratio is SNR = abar / (1 - abar), where abar is the cumulative alpha product. For epsilon prediction the weight is min(SNR, gamma) / SNR, so steps with SNR above gamma are scaled down and the rest keep weight 1. Worked example: abar [0.99, 0.8, 0.2], t [0, 1, 2], gamma 5. SNR = 0.99 / 0.01 = 99, 0.8 / 0.2 = 4 and 0.2 / 0.8 = 0.25. Weights: 5 / 99 = 0.0505, 4 / 4 = 1 and 0.25 / 0.25 = 1. Rule: gather abar at the indices t; SNR = abar / (1 - abar); weight = min(SNR, gamma) / SNR. Returning the clipped SNR without dividing is the usual mistake; that is the weight for x0 prediction, not epsilon prediction. Used in: faster and steadier training of image diffusion models, with the weight multiplying the per-sample MSE loss.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 3.

  • Sample 1: examplemin_snr_weights([0.9, 0.5], [0, 1])
  • Sample 2: schedulemin_snr_weights(np.linspace(0.999, 0.01, 10), [0, 3, 9])
  • Test 3: gamma 1min_snr_weights([0.99, 0.8, 0.2], [0, 1, 2], 1.0)