Forward-Diffuse a Batch to Timestep t
Problem Statement
Apply the closed-form forward process to a batch of clean samples at possibly different timesteps, the exact operation inside a training step. Each sample gets noised with its own t and its own noise vector.
Background
The forward marginal is q(x_t | x_0) = N(sqrt(alpha_bar_t) x_0, (1 - alpha_bar_t) I), so with a standard-normal noise eps:
xtβ=Ξ±Λtββx0β+1βΞ±ΛtββΞ΅
In a training batch each row i has its own timestep t_i (drawn uniformly) and its own noise, so the per-row scale factors are gathered from the alpha_bar table.
Your Task
Implement:
def forward_diffuse(x0, eps, alpha_bar, t):
- x0, eps: B x D nested lists (clean samples and noise).
- alpha_bar: the full schedule (list).
- t: list of B integer timesteps, one per row.
Return the B x D noised batch as a nested list rounded to 4 decimals.
Input Format
- x0, eps: B x D.
- alpha_bar: list of cumulative products.
- t: list of B indices into alpha_bar.
Output Format
- A B x D nested list rounded to 4 decimals.
Sample
x0 = [[1.0, 1.0]]
eps = [[1.0, -1.0]]
print(forward_diffuse(x0, eps, [0.99, 0.5], [1]))
Output:
[[1.4142, 0.0]]
Example:
x0 = [[1.0, 1.0]] eps = [[1.0, -1.0]] print(forward_diffuse(x0, eps, [0.99, 0.5], [1]))
[[1.4142, 0.0]]
- Look up the noise schedule value for the given timestep: with t=[1], we index into
alpha_barat position 1 to get Ξ±Λ=0.5. - Compute the signal and noise scale factors for this timestep: the signal weight is a=0.5ββ0.7071 and the noise weight is b=1β0.5β=0.5ββ0.7071.
- Apply the forward diffusion formula xtβ=aβ
x0β+bβ
Ο΅ element-wise to the single sample:
- For the first dimension: 0.7071β 1.0+0.7071β 1.0=1.4142.
- For the second dimension: 0.7071β 1.0+0.7071β (β1.0)=0.0.
- The final output is
[[1.4142, 0.0]]
Constraints:
x0andepsshare shapeB x D;len(t) == B.- Gather
alpha_bar[t_i]per row; scales aresqrt(alpha_bar)andsqrt(1 - alpha_bar). - Round to 4 decimals; avoid
-0.0.
1. Background Knowledge
In denoising diffusion probabilistic models (DDPMs), the forward process gradually adds Gaussian noise to a clean data sample x0β over T discrete timesteps. The key insight is that although the process is defined as a Markov chain, the marginal distribution q(xtββ£x0β) has a closed-form solution: it is a Gaussian with mean Ξ±Λtββx0β and covariance (1βΞ±Λtβ)I. This means you can jump directly from x0β to any xtβ in one step, without simulating all intermediate timesteps.
The quantity Ξ±Λtβ is the cumulative product of the per-step signal retention factors Ξ±iβ=1βΞ²iβ, where Ξ²iβ is the noise variance at step i. As t increases, Ξ±Λtβ decreases toward zero, meaning the signal is progressively drowned out by noise. At t=0, Ξ±Λ0β=1 (pure signal); at large t, Ξ±Λtββ0 (pure noise).
In a training batch, each sample is noised at a different randomly sampled timestep tiβ. This is essential for the model to learn to denoise at all noise levels simultaneously. The per-sample scale factors Ξ±Λtiβββ and 1βΞ±Λtiβββ are gathered from the precomputed schedule and applied row-wise.
2. Algorithm Approach
This is a row-wise element-wise operation with per-row scalar coefficients. The pattern is:
- For each row i in the batch, look up Ξ±Λtiββ from the schedule.
- Compute the two scalar coefficients: siβ=Ξ±Λtiβββ and niβ=1βΞ±Λtiβββ.
- Apply the linear combination xtβ[i]=siββ x0β[i]+niββ Ξ΅[i] element-wise across the feature dimension D.
No loops over timesteps are neededβjust a single pass over the batch.
3. Step-by-Step Strategy
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.