TensorTonic
Problems
DDIM Step
Diffusion ModelsSamplingGenerative Models
Hard

Perform one DDIM update that moves the whole batch from timestep t to timestep t_prev, and return the new sample together with the predicted clean sample.

Let αˉt\bar\alpha_tαˉt​ be alpha_bars at index t and αˉprev\bar\alpha_{\text{prev}}αˉprev​ be alpha_bars at index t_prev, or exactly 1 when t_prev is -1. With x_t, the noise prediction ϵ^\hat\epsilonϵ^ (predicted_noise) and fresh noise zzz (noise),

x^0=xt−1−αˉt ϵ^αˉt,σ=η1−αˉprev1−αˉt1−αˉtαˉprev\hat x_0 = \frac{x_t - \sqrt{1 - \bar\alpha_t}\, \hat\epsilon}{\sqrt{\bar\alpha_t}}, \qquad \sigma = \eta \sqrt{\frac{1 - \bar\alpha_{\text{prev}}}{1 - \bar\alpha_t}} \sqrt{1 - \frac{\bar\alpha_t}{\bar\alpha_{\text{prev}}}}x^0​=αˉt​​xt​−1−αˉt​​ϵ^​,σ=η1−αˉt​1−αˉprev​​​1−αˉprev​αˉt​​​ xprev=αˉprev x^0+1−αˉprev−σ2 ϵ^+σzx_{\text{prev}} = \sqrt{\bar\alpha_{\text{prev}}}\, \hat x_0 + \sqrt{1 - \bar\alpha_{\text{prev}} - \sigma^2}\, \hat\epsilon + \sigma zxprev​=αˉprev​​x^0​+1−αˉprev​−σ2​ϵ^+σz

Here η\etaη is eta. When clip_sample is true, first clamp x^0\hat x_0x^0​ to [-1, 1], then recompute ϵ^=(xt−αˉt x^0)/1−αˉt\hat\epsilon = (x_t - \sqrt{\bar\alpha_t}\, \hat x_0) / \sqrt{1 - \bar\alpha_t}ϵ^=(xt​−αˉt​​x^0​)/1−αˉt​​ from the clamped value, and use both in the update. Clamp the term under the direction square root at 0 before taking the root.

Return a dict with the keys "x_prev" and "pred_x0" (the possibly clamped x^0\hat x_0x^0​), each a PyTorch tensor with the shape, dtype and device of x_t.

Examples

Input: x_t = [[[[0.5,-1.2],[0.8,0.1]]]], predicted_noise = [[[[0.3,-0.7],[1.1,0.2]]]], t = 3, t_prev = 1, alpha_bars = [0.9,0.7,0.5,0.3,0.1], eta = 0.0, noise = [[[[0.4,1.0],[-0.5,-0.2]]]], clip_sample = False

Output: {"x_prev":[[[[0.544674,-1.321823],[0.418694,0.006693]]]],"pred_x0":[[[[0.454613,-1.121623],[-0.219684,-0.122931]]]]}

Explanation: With alpha_bars[3] = 0.3 the first pixel gives pred_x0 = (0.5 - sqrt(0.7) 0.3) / sqrt(0.3), about 0.4546. Since eta = 0 there is no noise, and x_prev = sqrt(0.7) pred_x0 + sqrt(0.3) predicted_noise, about 0.5447.

Input: x_t = [[[[0.5,-1.2],[0.8,0.1]]]], predicted_noise = [[[[0.3,-0.7],[1.1,0.2]]]], t = 3, t_prev = 1, alpha_bars = [0.9,0.7,0.5,0.3,0.1], eta = 1.0, noise = [[[[0.4,1.0],[-0.5,-0.2]]]], clip_sample = True

Output: {"x_prev":[[[[0.648727,-0.524795],[-0.173025,-0.154878]]]],"pred_x0":[[[[0.454613,-1.0],[-0.219684,-0.122931]]]]}

Input: x_t = [[0.9,-0.3,0.2]], predicted_noise = [[0.5,0.1,-0.4]], t = 0, t_prev = -1, alpha_bars = [0.8,0.5,0.2], eta = 0.5, noise = [[1.0,-1.0,0.5]], clip_sample = False

Output: {"x_prev":[[0.756231,-0.38541,0.423607]],"pred_x0":[[0.756231,-0.38541,0.423607]]}

Hint 1

Read alpha_bars[t_prev] only when t_prev >= 0; otherwise use 1.

Hint 2

torch.clamp(pred_x0, -1.0, 1.0) clips the clean estimate.

Hint 3

.clamp_min(0) keeps a square-root argument non-negative.

Requirements

  • Use PyTorch tensor operations.
  • Use one integer t and one integer t_prev for every sample in the batch.
  • Return a dict with no keys other than "x_prev" and "pred_x0".
  • Leave all input tensors unchanged.

Constraints

  • x_t, predicted_noise and noise are floating-point PyTorch tensors of the same shape (B, ...) with B at least 1 and at least one non-batch dimension.
  • alpha_bars is a 1-D tensor of length T between 3 and 30 with the same dtype as x_t, strictly decreasing, with every value strictly between 0 and 1.
  • t is a Python int with 0 <= t <= T - 1, and t_prev is a Python int with -1 <= t_prev < t.
  • eta is a Python float in [0, 1], and clip_sample is a Python bool.
  • Supported dtypes are float32 and float64. All values are finite.
Try Similar Problems
DDPM Ancestral StepMediumToy DDPM SamplerHardKarras Euler SamplerHardClassifier-Free GuidanceMediumPosterior Mean and VarianceMediumPrediction ParameterizationMediumAnnealed Langevin DynamicsMediumReparameterized Gaussian SampleEasyDiagonal Gaussian KL DivergenceEasyGAN Training StepHardAutoencoder Forward PassMediumVariational Autoencoder Forward PassMedium

Accepts: array

Accepts: array

Accepts: integer

Accepts: integer

Accepts: array

Accepts: number

Accepts: array

You must run your code first.

DDIM Step

Diffusion ModelsSamplingGenerative Models
Hard

Perform one DDIM update that moves the whole batch from timestep t to timestep t_prev, and return the new sample together with the predicted clean sample.

Let αˉt\bar\alpha_tαˉt​ be alpha_bars at index t and αˉprev\bar\alpha_{\text{prev}}αˉprev​ be alpha_bars at index t_prev, or exactly 1 when t_prev is -1. With x_t, the noise prediction ϵ^\hat\epsilonϵ^ (predicted_noise) and fresh noise zzz (noise),

x^0=xt−1−αˉt ϵ^αˉt,σ=η1−αˉprev1−αˉt1−αˉtαˉprev\hat x_0 = \frac{x_t - \sqrt{1 - \bar\alpha_t}\, \hat\epsilon}{\sqrt{\bar\alpha_t}}, \qquad \sigma = \eta \sqrt{\frac{1 - \bar\alpha_{\text{prev}}}{1 - \bar\alpha_t}} \sqrt{1 - \frac{\bar\alpha_t}{\bar\alpha_{\text{prev}}}}x^0​=αˉt​​xt​−1−αˉt​​ϵ^​,σ=η1−αˉt​1−αˉprev​​​1−αˉprev​αˉt​​​ xprev=αˉprev x^0+1−αˉprev−σ2 ϵ^+σzx_{\text{prev}} = \sqrt{\bar\alpha_{\text{prev}}}\, \hat x_0 + \sqrt{1 - \bar\alpha_{\text{prev}} - \sigma^2}\, \hat\epsilon + \sigma zxprev​=αˉprev​​x^0​+1−αˉprev​−σ2​ϵ^+σz

Here η\etaη is eta. When clip_sample is true, first clamp x^0\hat x_0x^0​ to [-1, 1], then recompute ϵ^=(xt−αˉt x^0)/1−αˉt\hat\epsilon = (x_t - \sqrt{\bar\alpha_t}\, \hat x_0) / \sqrt{1 - \bar\alpha_t}ϵ^=(xt​−αˉt​​x^0​)/1−αˉt​​ from the clamped value, and use both in the update. Clamp the term under the direction square root at 0 before taking the root.

Return a dict with the keys "x_prev" and "pred_x0" (the possibly clamped x^0\hat x_0x^0​), each a PyTorch tensor with the shape, dtype and device of x_t.

Examples

Input: x_t = [[[[0.5,-1.2],[0.8,0.1]]]], predicted_noise = [[[[0.3,-0.7],[1.1,0.2]]]], t = 3, t_prev = 1, alpha_bars = [0.9,0.7,0.5,0.3,0.1], eta = 0.0, noise = [[[[0.4,1.0],[-0.5,-0.2]]]], clip_sample = False

Output: {"x_prev":[[[[0.544674,-1.321823],[0.418694,0.006693]]]],"pred_x0":[[[[0.454613,-1.121623],[-0.219684,-0.122931]]]]}

Explanation: With alpha_bars[3] = 0.3 the first pixel gives pred_x0 = (0.5 - sqrt(0.7) 0.3) / sqrt(0.3), about 0.4546. Since eta = 0 there is no noise, and x_prev = sqrt(0.7) pred_x0 + sqrt(0.3) predicted_noise, about 0.5447.

Input: x_t = [[[[0.5,-1.2],[0.8,0.1]]]], predicted_noise = [[[[0.3,-0.7],[1.1,0.2]]]], t = 3, t_prev = 1, alpha_bars = [0.9,0.7,0.5,0.3,0.1], eta = 1.0, noise = [[[[0.4,1.0],[-0.5,-0.2]]]], clip_sample = True

Output: {"x_prev":[[[[0.648727,-0.524795],[-0.173025,-0.154878]]]],"pred_x0":[[[[0.454613,-1.0],[-0.219684,-0.122931]]]]}

Input: x_t = [[0.9,-0.3,0.2]], predicted_noise = [[0.5,0.1,-0.4]], t = 0, t_prev = -1, alpha_bars = [0.8,0.5,0.2], eta = 0.5, noise = [[1.0,-1.0,0.5]], clip_sample = False

Output: {"x_prev":[[0.756231,-0.38541,0.423607]],"pred_x0":[[0.756231,-0.38541,0.423607]]}

Hint 1

Read alpha_bars[t_prev] only when t_prev >= 0; otherwise use 1.

Hint 2

torch.clamp(pred_x0, -1.0, 1.0) clips the clean estimate.

Hint 3

.clamp_min(0) keeps a square-root argument non-negative.

Requirements

  • Use PyTorch tensor operations.
  • Use one integer t and one integer t_prev for every sample in the batch.
  • Return a dict with no keys other than "x_prev" and "pred_x0".
  • Leave all input tensors unchanged.

Constraints

  • x_t, predicted_noise and noise are floating-point PyTorch tensors of the same shape (B, ...) with B at least 1 and at least one non-batch dimension.
  • alpha_bars is a 1-D tensor of length T between 3 and 30 with the same dtype as x_t, strictly decreasing, with every value strictly between 0 and 1.
  • t is a Python int with 0 <= t <= T - 1, and t_prev is a Python int with -1 <= t_prev < t.
  • eta is a Python float in [0, 1], and clip_sample is a Python bool.
  • Supported dtypes are float32 and float64. All values are finite.
Try Similar Problems
DDPM Ancestral StepMediumToy DDPM SamplerHardKarras Euler SamplerHardClassifier-Free GuidanceMediumPosterior Mean and VarianceMediumPrediction ParameterizationMediumAnnealed Langevin DynamicsMediumReparameterized Gaussian SampleEasyDiagonal Gaussian KL DivergenceEasyGAN Training StepHardAutoencoder Forward PassMediumVariational Autoencoder Forward PassMedium

Accepts: array

Accepts: array

Accepts: integer

Accepts: integer

Accepts: array

Accepts: number

Accepts: array

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