DDIM with Stochasticity (general eta)
Problem Statement
Implement the general DDIM update with the interpolation parameter eta sweeping from deterministic (eta=0) to full DDPM (eta=1).
Background
σt​=η1−αˉt​1−αˉt′​​​1−αˉt′​αˉt​​​
x^0​=αˉt​​xt​−1−αˉt​​ε^​,xt′​=αˉt′​​x^0​+1−αˉt′​−σt2​​ε^+σt​z
Clamp the direction-term square root argument at 0. Add sigma_t * z only when eta > 0.
Your Task
Implement:
def ddim_step(x_t, eps, alpha_bar_t, alpha_bar_prev, eta=0.0, z=None):
Return x_{t'} as a list rounded to 4 decimals.
Input Format
- x_t, eps: lists of length D; z: list or None.
- alpha_bar_t, alpha_bar_prev (float); eta (float).
Output Format
- A list of D floats rounded to 4 decimals.
Sample
print(ddim_step([0.4, -0.6], [0.1, 0.2], 0.5, 0.8, 0.0))
Output:
[0.4612, -0.8484]
Example:
print(ddim_step([0.4, -0.6], [0.1, 0.2], 0.5, 0.8, 0.0))
[0.4612, -0.8484]
- Compute the noise scale σt​: Since η=0.0, the stochastic term vanishes, so σt​=0.
- Estimate the initial state x^0​ by removing the noise component from xt​: Using 1−αˉt​​=0.5​≈0.7071 and αˉt​​=0.5​≈0.7071, we get x^0​=0.7071xt​−0.7071⋅ε^​. For the first element: 0.70710.4−0.7071(0.1)​≈0.4596; for the second: 0.7071−0.6−0.7071(0.2)​≈−0.8485.
- Calculate the deterministic direction term: The coefficient is 1−αˉt′​−σt2​​=1−0.8−0​=0.2​≈0.4472. Multiplying by ε^ gives [0.0447,0.0894].
- Combine the scaled prediction and direction term to find xt′​: Multiply x^0​ by αˉt′​​=0.8​≈0.8944 to get [0.4111,−0.7589], then add the direction term: [0.4111+0.0447,−0.7589+0.0894]=[0.4558,−0.6695]. Correction: Re-evaluating the exact algebraic simplification for η=0 where xt′​=αˉt′​​x^0​+1−αˉt′​​ε^:
- Element 1: 0.8​(0.5​0.4−0.5​(0.1)​)+0.2​(0.1)≈0.4612
- Element 2: 0.8​(0.5​−0.6−0.5​(0.2)​)+0.2​(0.2)≈−0.8484
- The final output is [0.4612, -0.8484]
Constraints:
len(x_t) == len(eps); alpha-bar values in(0, 1].- Clamp
1 - alpha_bar_prev - sigma^2at 0 inside the square root. - Add
sigma*zonly wheneta > 0; round to 4 decimals; avoid-0.0.
1. Background Knowledge
DDPM (Denoising Diffusion Probabilistic Models) defines a forward noising process where Gaussian noise is gradually added to data, and a reverse denoising process where a neural network predicts the noise ε^ to recover the clean signal. The standard DDPM reverse step is stochastic and follows a specific Gaussian transition kernel.
DDIM (Denoising Diffusion Implicit Models) generalizes the reverse process by introducing a deterministic path. When the interpolation parameter η=0, the update becomes fully deterministic, meaning the same noise prediction ε^ always leads to the same xt′​. This allows for faster sampling (fewer steps) and enables techniques like classifier guidance.
The general DDIM update bridges these two extremes. By varying η from 0 to 1, you interpolate between the deterministic DDIM path and the stochastic DDPM path. The key insight is that the "noise" added at each step is controlled by σt​, which depends on η and the ratio of cumulative noise schedules (αˉ). When η=1, σt​ recovers the standard DDPM noise level.
2. Algorithm Approach
The implementation follows a direct formulaic approach:
- Compute σt​: Use the provided formula involving η, αˉt​, and αˉt′​ (which is alpha_bar_prev in the function signature).
- Predict x^0​: Use the current noisy sample xt​ and the predicted noise ε^ to estimate the clean data.
- Reconstruct xt′​: Combine the predicted clean data x^0​, the predicted noise ε^, and the stochastic term σt​z using the specific coefficients derived from the DDIM derivation.
- Handle Edge Cases: Ensure numerical stability by clamping square root arguments and conditionally adding the stochastic term.
3. Step-by-Step Strategy
- Calculate σt​:
- Compute the term inside the first square root: 1−αˉt​1−αˉt′​​.
- Compute the term inside the second square root: 1−αˉt′​αˉt​​.
- Multiply these by η2 (since σt​=η…​, so σt2​=η2…).
- Take the square root to get σt​.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.