Hang et al. (2023) speed up diffusion training by re-weighting the loss of each timestep with a clamped signal-to-noise ratio. Implement the weights for the three common prediction targets and a weighted batch loss.
snr(alpha_bar)
alpha_bar is a float or an array of values in . Return with the same shape.min_snr_weights(alpha_bar, gamma, prediction="eps")
alpha_bar, expressed as a weight on the network's own loss:
"x0": "eps": "v": ValueError for any other prediction string.min_snr_loss(pred, target, alpha_bar_t, gamma, prediction="eps")
pred and target have shape (n, ...); alpha_bar_t has shape (n,) and holds of the timestep drawn for each sample."weights" (list of the n weights rounded to 6 decimals), "per_sample" (list of the n unweighted losses rounded to 6 decimals), "weighted_loss" (mean over the batch of weight times per-sample loss, a float rounded to 6 decimals) and "uniform_loss" (the plain mean of the per-sample losses, rounded to 6 decimals).np.minimum(snr, gamma) clamps element-wise.(n, -1) before taking the mean so any trailing shape works.gamma very large the "x0" weights become the raw SNR, which is why the clamp matters.