Combine a conditional and an unconditional model prediction with classifier-free guidance, optionally rescaling the result per sample.
g=u+w(c−u)Here c is cond_pred, u is uncond_pred, both of shape (B, ...), and w is guidance_scale. When guidance_rescale ϕ is 0, return g.
When ϕ>0, compute for every sample b the population standard deviation (dividing by the number of elements, with no Bessel correction) over all of its non-batch elements, written s(⋅), and blend
rb=gbs(gb)s(cb),resultb=ϕrb+(1−ϕ)gb.Each sample uses its own two standard deviations. The value ϕ=1 returns the fully rescaled prediction.
Return a PyTorch tensor with the same shape, dtype and device as cond_pred.
Input: cond_pred = [[[[0.6,-0.2],[1.0,0.4]]],[[[0.1,0.3],[-0.5,0.2]]]], uncond_pred = [[[[0.4,0.0],[0.5,0.3]]],[[[0.2,0.1],[-0.1,0.0]]]], guidance_scale = 3.0, guidance_rescale = 0.0
Output: [[[[1.0,-0.6],[2.0,0.6]]],[[[-0.1,0.7],[-1.3,0.6]]]]
Explanation: With guidance_scale = 3 each value moves three times the gap away from the unconditional prediction, so the first pixel becomes 0.4 + 3 (0.6 - 0.4) = 1.0. A rescale of 0 leaves this guided result unchanged.
Input: cond_pred = [[[[0.6,-0.2],[1.0,0.4]]],[[[0.1,0.3],[-0.5,0.2]]]], uncond_pred = [[[[0.4,0.0],[0.5,0.3]]],[[[0.2,0.1],[-0.1,0.0]]]], guidance_scale = 3.0, guidance_rescale = 1.0
Output: [[[[0.464907,-0.278944],[0.929814,0.278944]]],[[[-0.039001,0.273009],[-0.507017,0.234008]]]]
Input: cond_pred = [[1.0,2.0,0.0,-1.0],[0.5,-0.5,0.5,-0.5]], uncond_pred = [[0.5,1.0,0.5,0.0],[0.0,0.0,0.0,0.0]], guidance_scale = 7.5, guidance_rescale = 0.7
Output: [[1.807397,3.614794,-1.382127,-3.189524],[1.475,-1.475,1.475,-1.475]]
tuple(range(1, g.ndim)) lists every non-batch dimension for any input rank.
g.std(dim=dims, keepdim=True, correction=0) gives a population std per sample; the default divides by n - 1 and will not match.
keepdim=True keeps a (B, 1, ...) shape that broadcasts back over the sample.
Accepts: array
Accepts: array
Accepts: number
Accepts: number
Combine a conditional and an unconditional model prediction with classifier-free guidance, optionally rescaling the result per sample.
g=u+w(c−u)Here c is cond_pred, u is uncond_pred, both of shape (B, ...), and w is guidance_scale. When guidance_rescale ϕ is 0, return g.
When ϕ>0, compute for every sample b the population standard deviation (dividing by the number of elements, with no Bessel correction) over all of its non-batch elements, written s(⋅), and blend
rb=gbs(gb)s(cb),resultb=ϕrb+(1−ϕ)gb.Each sample uses its own two standard deviations. The value ϕ=1 returns the fully rescaled prediction.
Return a PyTorch tensor with the same shape, dtype and device as cond_pred.
Input: cond_pred = [[[[0.6,-0.2],[1.0,0.4]]],[[[0.1,0.3],[-0.5,0.2]]]], uncond_pred = [[[[0.4,0.0],[0.5,0.3]]],[[[0.2,0.1],[-0.1,0.0]]]], guidance_scale = 3.0, guidance_rescale = 0.0
Output: [[[[1.0,-0.6],[2.0,0.6]]],[[[-0.1,0.7],[-1.3,0.6]]]]
Explanation: With guidance_scale = 3 each value moves three times the gap away from the unconditional prediction, so the first pixel becomes 0.4 + 3 (0.6 - 0.4) = 1.0. A rescale of 0 leaves this guided result unchanged.
Input: cond_pred = [[[[0.6,-0.2],[1.0,0.4]]],[[[0.1,0.3],[-0.5,0.2]]]], uncond_pred = [[[[0.4,0.0],[0.5,0.3]]],[[[0.2,0.1],[-0.1,0.0]]]], guidance_scale = 3.0, guidance_rescale = 1.0
Output: [[[[0.464907,-0.278944],[0.929814,0.278944]]],[[[-0.039001,0.273009],[-0.507017,0.234008]]]]
Input: cond_pred = [[1.0,2.0,0.0,-1.0],[0.5,-0.5,0.5,-0.5]], uncond_pred = [[0.5,1.0,0.5,0.0],[0.0,0.0,0.0,0.0]], guidance_scale = 7.5, guidance_rescale = 0.7
Output: [[1.807397,3.614794,-1.382127,-3.189524],[1.475,-1.475,1.475,-1.475]]
tuple(range(1, g.ndim)) lists every non-batch dimension for any input rank.
g.std(dim=dims, keepdim=True, correction=0) gives a population std per sample; the default divides by n - 1 and will not match.
keepdim=True keeps a (B, 1, ...) shape that broadcasts back over the sample.
Accepts: array
Accepts: array
Accepts: number
Accepts: number