TensorTonic
Problems
Classifier-Free Guidance
Diffusion ModelsSamplingConditional Generation
Medium

Combine a conditional and an unconditional model prediction with classifier-free guidance, optionally rescaling the result per sample.

g=u+w (c−u)g = u + w\,(c - u)g=u+w(c−u)

Here ccc is cond_pred, uuu is uncond_pred, both of shape (B, ...), and www is guidance_scale. When guidance_rescale ϕ\phiϕ is 0, return ggg.

When ϕ>0\phi > 0ϕ>0, compute for every sample bbb the population standard deviation (dividing by the number of elements, with no Bessel correction) over all of its non-batch elements, written s(⋅)s(\cdot)s(⋅), and blend

rb=gb s(cb)s(gb),resultb=ϕ rb+(1−ϕ) gb.r_b = g_b \, \frac{s(c_b)}{s(g_b)}, \qquad \text{result}_b = \phi\, r_b + (1 - \phi)\, g_b .rb​=gb​s(gb​)s(cb​)​,resultb​=ϕrb​+(1−ϕ)gb​.

Each sample uses its own two standard deviations. The value ϕ=1\phi = 1ϕ=1 returns the fully rescaled prediction.

Return a PyTorch tensor with the same shape, dtype and device as cond_pred.

Examples

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]]

Hint 1

tuple(range(1, g.ndim)) lists every non-batch dimension for any input rank.

Hint 2

g.std(dim=dims, keepdim=True, correction=0) gives a population std per sample; the default divides by n - 1 and will not match.

Hint 3

keepdim=True keeps a (B, 1, ...) shape that broadcasts back over the sample.

Requirements

  • Use PyTorch tensor operations.
  • Leave all input tensors unchanged.

Constraints

  • cond_pred and uncond_pred are floating-point PyTorch tensors of the same shape (B, ...) with B at least 1 and at least two elements per sample.
  • guidance_scale is a Python float, usually between 1 and 12, and it may be 0 or negative.
  • guidance_rescale is a Python float in [0, 1].
  • Whenever guidance_rescale is above 0, every sample of the guided prediction has a standard deviation above 0.
  • Supported dtypes are float32 and float64. All values are finite.
Try Similar Problems
DDPM Ancestral StepMediumDDIM StepHardKarras Euler SamplerHardAnnealed Langevin DynamicsMediumToy DDPM SamplerHardGaussian Mixture ScoreMediumCosine Noise ScheduleMediumRescale to Zero Terminal SNRMediumTime-Conditioned Residual BlockMediumSpatial Self-Attention BlockMediumPosterior Mean and VarianceMediumPrediction ParameterizationMedium

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.

Classifier-Free Guidance

Diffusion ModelsSamplingConditional Generation
Medium

Combine a conditional and an unconditional model prediction with classifier-free guidance, optionally rescaling the result per sample.

g=u+w (c−u)g = u + w\,(c - u)g=u+w(c−u)

Here ccc is cond_pred, uuu is uncond_pred, both of shape (B, ...), and www is guidance_scale. When guidance_rescale ϕ\phiϕ is 0, return ggg.

When ϕ>0\phi > 0ϕ>0, compute for every sample bbb the population standard deviation (dividing by the number of elements, with no Bessel correction) over all of its non-batch elements, written s(⋅)s(\cdot)s(⋅), and blend

rb=gb s(cb)s(gb),resultb=ϕ rb+(1−ϕ) gb.r_b = g_b \, \frac{s(c_b)}{s(g_b)}, \qquad \text{result}_b = \phi\, r_b + (1 - \phi)\, g_b .rb​=gb​s(gb​)s(cb​)​,resultb​=ϕrb​+(1−ϕ)gb​.

Each sample uses its own two standard deviations. The value ϕ=1\phi = 1ϕ=1 returns the fully rescaled prediction.

Return a PyTorch tensor with the same shape, dtype and device as cond_pred.

Examples

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]]

Hint 1

tuple(range(1, g.ndim)) lists every non-batch dimension for any input rank.

Hint 2

g.std(dim=dims, keepdim=True, correction=0) gives a population std per sample; the default divides by n - 1 and will not match.

Hint 3

keepdim=True keeps a (B, 1, ...) shape that broadcasts back over the sample.

Requirements

  • Use PyTorch tensor operations.
  • Leave all input tensors unchanged.

Constraints

  • cond_pred and uncond_pred are floating-point PyTorch tensors of the same shape (B, ...) with B at least 1 and at least two elements per sample.
  • guidance_scale is a Python float, usually between 1 and 12, and it may be 0 or negative.
  • guidance_rescale is a Python float in [0, 1].
  • Whenever guidance_rescale is above 0, every sample of the guided prediction has a standard deviation above 0.
  • Supported dtypes are float32 and float64. All values are finite.
Try Similar Problems
DDPM Ancestral StepMediumDDIM StepHardKarras Euler SamplerHardAnnealed Langevin DynamicsMediumToy DDPM SamplerHardGaussian Mixture ScoreMediumCosine Noise ScheduleMediumRescale to Zero Terminal SNRMediumTime-Conditioned Residual BlockMediumSpatial Self-Attention BlockMediumPosterior Mean and VarianceMediumPrediction ParameterizationMedium

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
Problems
Loading...
1 / 1