diffusion

**Diffusion models** generate data by **learning to reverse a gradual noising process** — progressively adding Gaussian noise to data during training, then learning to denoise step-by-step during generation, producing high-quality images, audio, and video that rival or exceed GANs. **What Are Diffusion Models?** - **Definition**: Generative models based on denoising process. - **Training**: Learn to reverse gradual corruption by noise. - **Generation**: Start from pure noise, iteratively denoise. - **Examples**: Stable Diffusion, DALL-E, Midjourney, Sora. **Why Diffusion Works** - **Stable Training**: No adversarial dynamics (unlike GANs). - **Quality**: State-of-the-art image generation. - **Flexibility**: Conditional generation, inpainting, editing. - **Theory**: Strong mathematical foundation. **Forward Process (Noising)** **Gradual Corruption**: ``` x_0 → x_1 → x_2 → ... → x_T (data) (pure noise) At each step: x_t = √(α_t) × x_{t-1} + √(1-α_t) × ε Where ε ~ N(0, I) is Gaussian noise α_t follows a schedule (typically 0.9999 to 0.0001) ``` **Closed Form to Any Step**: ``` x_t = √(ᾱ_t) × x_0 + √(1-ᾱ_t) × ε Where ᾱ_t = Π_{s=1}^t α_s (cumulative product) ``` **Visual**: ```svg Diffusion Models — Denoising Score Matching learn to reverse gradual noise addition — generate images by iterative denoising Forward Process q(x_t | x_{t-1}) — Add Noise (fixed, no learning) x₀ clean x₁ x_t ... x_T pure noise q(x_t|x₀) = N(√ᾱ_t · x₀, (1-ᾱ_t)I) T = 1000 steps (DDPM) closed-form: sample x_t directly from x₀ Reverse Process p_θ(x_{t-1} | x_t) — Denoise (learned) x_T -ε_θ ... x_t x₁ x₀ generated! U-Net (or DiT) predicts noise ε_θ(x_t, t) loss = ||ε - ε_θ(x_t, t)||² 50 steps (DDIM) or 1–4 (distilled) CFG: classifier-free guidance (scale=7.5) Architecture: U-Net → DiT U-Net (SD 1.5, SDXL): encoder-decoder + skip connections + cross-attn text conditioning via CLIP text encoder DiT (SD 3, FLUX, Sora): pure transformer on latent patches scales better with compute, uses MM-DiT blocks Diffusion Model Family DALL·E 2 (2022) CLIP + diffusion prior Stable Diffusion (2022) latent space diffusion (VAE) SDXL / SD 3 (2024) DiT + flow matching FLUX (2024) rectified flow + T5 text enc Sora (2024) video DiT, spacetime patches consistency models 1-step generation (distilled) Diffusion models replaced GANs for image generation — iterative refinement beats adversarial training. ``` **Reverse Process (Denoising)** **Learning to Denoise**: ``` Train neural network ε_θ to predict noise: Loss = ||ε - ε_θ(x_t, t)||² Given noisy image x_t and timestep t, predict the noise ε that was added. ``` **Generation** (Sampling): ``` Start: x_T ~ N(0, I) (pure noise) For t = T, T-1, ..., 1: Predict noise: ε̂ = ε_θ(x_t, t) Compute x_{t-1} using ε̂ Return: x_0 (generated sample) ``` **Implementation Sketch** **Training Loop**: ```python import torch import torch.nn.functional as F def train_step(model, x_0, noise_scheduler): # Sample random timesteps t = torch.randint(0, T, (batch_size,)) # Sample noise noise = torch.randn_like(x_0) # Add noise to get x_t x_t = noise_scheduler.add_noise(x_0, noise, t) # Predict noise predicted_noise = model(x_t, t) # MSE loss loss = F.mse_loss(predicted_noise, noise) return loss ``` **Sampling Loop**: ```python @torch.no_grad() def sample(model, noise_scheduler, shape): # Start from pure noise x = torch.randn(shape) # Iteratively denoise for t in reversed(range(T)): # Predict noise predicted_noise = model(x, t) # Compute previous step x = noise_scheduler.step(predicted_noise, t, x) return x ``` **Key Architectures** **U-Net (Standard)**: ```svg ┌─────────────────────────────────────────────────────────┐ Noisy Image + t └─────────────────────────────────────────────────────────┘ ┌───▼───┐ Encoder (downsampling) Conv └───┬───┘ │──────────────────┐ ┌───▼───┐ Conv └───┬───┘ │─────────────┐ ┌───▼───┐ Bottom └───┬───┘ ┌───▼───┐←────────┘ Decoder (upsampling) Conv skip └───┬───┘ ┌───▼───┐←─────────────┘ Conv skip └───┬───┘ Predicted Noise ``` **DiT (Diffusion Transformer)**: ``` Modern alternative using transformers instead of U-Net: - Used in Sora, recent SOTA models - Better scaling properties - Patch-based processing ``` **Conditional Generation** **Text-to-Image**: ```python # Classifier-free guidance def guided_sample(model, prompt, guidance_scale=7.5): text_embeddings = encode_text(prompt) for t in reversed(range(T)): # Conditional prediction noise_cond = model(x, t, text_embeddings) # Unconditional prediction noise_uncond = model(x, t, null_embedding) # Guided prediction noise = noise_uncond + guidance_scale * (noise_cond - noise_uncond) x = denoise_step(x, noise, t) return x ``` **Popular Models** ``` Model | Type | Open Source -------------------|----------------|------------ Stable Diffusion | Text-to-image | Yes DALL-E 3 | Text-to-image | No Midjourney | Text-to-image | No Sora | Text-to-video | No Runway Gen-2 | Text-to-video | No AudioLDM | Text-to-audio | Yes ``` Diffusion models are **the dominant paradigm for generative AI** — their stable training, high quality outputs, and flexibility for conditioning have made them the foundation of modern image, video, and audio generation systems. ```svg Diffusion Forward Process (q) Data is progressively corrupted by Gaussian noise over timesteps 0..T x₀ (clean) data manifold xₜ (mid noise) xₜ' (high noise) x_T (Gaussian) β₁ βₜ β_T Closed-form noising relation xₜ = √(ᾱₜ) x₀ + √(1-ᾱₜ) ε, ε ~ N(0, I) αₜ = 1-βₜ, ᾱₜ = Πᵢ₌₁ᵗ αᵢ As t increases: signal-to-noise ratio decreases monotonically Training samples random t and learns to predict ε from noisy xₜ ``` ```svg Reverse Denoising Process (pθ) Generation starts from noise and iteratively removes it using a learned UNet x_T pure Gaussian noise UNet εθ(xₜ,t,c) predict noise conditioned on prompt c Sampler Step DDPM / DDIM / DPM-Solver xₜ₋₁ less noisy sample repeat until x₀ loop t = T..1 Classifier-free guidance: ε̂ = ε_uncond + w(ε_cond - ε_uncond) Sampling quality/speed trade-off DDPM: higher fidelity, more steps DDIM: deterministic, fewer steps DPM-Solver: fast high-quality ODE Final x₀ becomes generated image/audio/video sample ``` ```svg Latent Diffusion Pipeline (Text-to-Image) Compress to latent space, denoise there, decode back to pixels Prompt "silicon wafer in cleanroom" Text Encoder CLIP / T5 context embeddings Latent UNet cross-attention denoising steps VAE Decoder latent z → image x pixel space Why latent space? 4x-16x fewer spatial elements than pixel space → faster sampling and lower memory Training objective Predict added noise ε with MSE loss in latent space Inference controls guidance scale, sampler, step count, seed trade-off: diversity vs prompt fidelity Stable Diffusion class models are latent diffusion systems with text-conditioned denoising. ```

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account