As asked
Write a Python class that implements a linear DDPM noise scheduler. It should precompute the alpha_bar values for T timesteps, implement a method to add noise to a batch of images at an arbitrary timestep in a single step using the closed-form formula, and implement a method to compute the posterior mean and variance for one reverse step.
Sample answer outline
A strong implementation precomputes betas as a linspace, then alphas = 1 - betas, then alpha_bars as the cumprod. add_noise uses the reparameterization: x_t = sqrt(alpha_bar_t) * x0 + sqrt(1 - alpha_bar_t) * noise. The reverse step uses the posterior mean formula from the DDPM paper. The candidate should handle tensor shapes correctly and not use loops for the forward process.
Reference implementation (python)
import torch
import torch.nn as nn
class DDPMScheduler:
def __init__(self, num_timesteps: int = 1000,
beta_start: float = 1e-4,
beta_end: float = 0.02):
self.T = num_timesteps
# TODO: compute betas, alphas, alpha_bars
pass
def add_noise(self, x0: torch.Tensor,
noise: torch.Tensor,
t: torch.Tensor) -> torch.Tensor:
# TODO: one-shot forward process q(x_t | x_0)
pass
def step(self, model_output: torch.Tensor,
t: int, x_t: torch.Tensor) -> torch.Tensor:
# TODO: one reverse step p(x_{t-1} | x_t)
passExpect these follow-ups
- How would you modify this to support a cosine schedule?
- What changes are needed to support classifier-free guidance at inference time?