4.5.15 · HinglishGenerative Models

Latent diffusion (Stable Diffusion)

2,717 words12 min readRead in English

4.5.15 · AI-ML › Generative Models


Yeh Kaunsa Problem Solve Karta Hai?

Pixel-space diffusion ki problem: Ek 512×512 RGB image mein 786,432 dimensions hote hain. Is par kaafi saare timesteps ke liye U-Net denoising chalana computationally expensive hai, khaaskar self-attention layers.

Latent diffusion ka solution:

  1. Images compress karne ke liye ek Variational Autoencoder (VAE) train karo: jahan
  2. Diffusion sirf latent space mein chalao
  3. Final ko VAE decoder se wapas image mein decode karo

Stable Diffusion downsampling use karta hai: 512×512 → 64×64, channels ke saath → 98% dimension reduction.


Architecture Components

1. The VAE (Autoencoder)

VAE kyun, aur simple autoencoder kyun nahi? VAE ka KL term latent space ko continuous aur smooth banane par majboor karta hai—nearby latents similar images mein decode hote hain. Diffusion ko noisy aur clean latents ke beech interpolate karne ke liye yeh smoothness chahiye.

Stable Diffusion specifics:

  • Downsampling factor : 512→64, 1024→128
  • Latent channels (learned, RGB nahi)
  • Massive image datasets (LAION) par alag se train kiya gaya
  • L2 pixel reconstruction loss + KL regularization ke saath train kiya gaya (released SD VAE yahi use karta hai; note: standard KL-autoencoder recipe mein VGG/perceptual loss nahi hota)
  • Diffusion training ke dauran frozen—sirf encode/decode ke liye use hota hai

2. The Diffusion U-Net (Latent Space Mein)

Pixels denoise karne ki jagah, hum latents denoise karte hain:

Reverse process (denoising):

Yeh kyun kaam karta hai:

  • 64×64×4 hai 512×512×3 ki jagah → U-Net ~2% original dimensions par operate karta hai
  • Latents semantic structure capture karte hain, isliye U-Net "object shapes" seekhta hai na ki "pixel noise patterns"
  • Text conditioning (CLIP se) denoising guide karta hai: "fur texture add karo" vs "metal texture add karo"

3. Text Conditioning (CLIP Embeddings)

Example: Prompt "a cat wearing sunglasses"

  • Head area ke corresponding latent region ka "cat" + "sunglasses" tokens par attention
  • Body ke latent region ka "cat" token par attention
  • U-Net is information ko use karta hai denoising ke dauran appropriate features inject karne ke liye
Figure — Latent diffusion (Stable Diffusion)

Step-by-Step: Stable Diffusion Image Kaise Generate Karta Hai


Latent Space Kyun? First Principles Se Derivation

Question: Pixel space se latent space mein denoise karna zyada efficient kyun hai?

Computational cost per denoising step (schematically): aur self-attention part specifically spatial locations ki sankhya mein quadratically scale karta hai:

Pixel-space ke liye (DDPM):

  • Spatial locations:
  • Channels: 3 (RGB)
  • Total dim:

Latent-space ke liye (Stable Diffusion):

  • Spatial locations:
  • Channels: 4
  • Total dim:

Dimension ratio: kam dimensions.

Kyunki attention cost scale karta hai, spatial locations mein se tak ki reduction ( mein drop) roughly yeh attention-cost reduction deta hai: jabki element-wise convolutional work linearly scale karta hai ke saath (~ cheaper). Practical end-to-end speedup is baat par depend karta hai ki U-Net kitna attention-heavy hai; empirically yeh large (roughly an order of magnitude ya usse zyada) hai, aur yahi reason hai ki latent diffusion ne consumer GPUs par high-res text-to-image generation feasible banaya.

Lekin quality loss kyun nahi hota?

VAE ko images reconstruct karne ke liye train kiya jaata hai (L2 loss) KL regularization ke saath. Yeh imperceptible high-frequency detail aur spatial redundancy (neighboring pixels ka almost identical hona) discard karta hai jabki semantic content—object shapes, colors, composition—preserve karta hai. Generative tasks ke liye hum semantic correctness care karte hain ("kya yeh cat jaisa dikhta hai?"), exact pixel values nahi, isliye yeh compression essentially perceptually harmless hai.




Key Hyperparameters aur Design Choices

Parameter Stable Diffusion v1.5 Yeh Value Kyun?
Latent channels 4 Balance: 3 undercomplete hai (info lose karta hai), 8 redundant hai. 4 structure achha capture karta hai.
Downsampling 8 → abhi bhi large latents (slow), → bahut lossy (VAE artifacts). 8 sweet spot hai.
U-Net timesteps 1000 (training) Standard DDPM schedule. Higher = smoother noise schedule lekin slower training.
Sampling steps 20–50 (inference) DDIM/other samplers steps skip karte hain. ~50 steps ≈ near-max quality (iske aage diminishing returns).
CFG scale 7.5 Classifier-Free Guidance scale. Higher = zyada prompt adherence, lower = zyada diversity.

Classifier-Free Guidance (CFG): Sampling ke dauran, hum conditional aur unconditional predictions ke beech interpolate karte hain: jahan guidance scale hai (7.5), null prompt hai. Yeh text conditioning ko amplify karta hai.



Recall Ek 12 Saal Ke Bacche Ko Samjhao

Imagine karo tum ek super detailed tasveer banana chahte ho, lekin tumhare paas sirf ek chhota notepad hai. Seedha saari details draw karna forever le jaata!

Yeh hai woh trick jo Stable Diffusion use karta hai:

  1. Tasveer shrink karo – Apni badi tasveer lo aur use ek chhote "summary" mein compress karo (jaise apne phone par photo shrink karna). Yeh summary saari important cheezein rakhti hai—cat ka face, colors—lekin chhoti details jaise individual baal throw away kar deti hai.
  2. Chhoti jagah mein draw karo – Ab ek bade canvas par draw karne ki jagah, tum is chhoti summary par draw karte ho. Bahut faster hai! Tum cat sketch kar sakte ho, sunglasses add kar sakte ho, galtiyan theek kar sakte ho—sab kuch super quick.
  3. Wapas bada karo – Jab ho jao, chhoti summary ko wapas ek badi, detailed tasveer mein "un-compress" karo. Magic!

Yeh kyun kaam karta hai? Kyunki kisi bhi tasveer ka zyaatar hissa repetitive hota hai—blue sky pixels sab similar hain, grass sirf repeated texture hai. "Summary" unique parts rakhti hai (cat ki aankhein, sunglasses shape) aur baad mein repetitive parts recreate karne deti hai. Tum tasveer ki meaning par kaam kar rahe ho, na ki har ek pixel par.



Connections

  • Variational Autoencoders (VAE) – compression backbone
  • Denoising Diffusion Probabilistic Models (DDPM) – latent diffusion DDPM ka noise schedule latent space mein apply karta hai
  • U-Net Architecture – denoising model (conditioning ke liye attention layers ke saath)
  • CLIP Text Encoder – prompts ko cross-attention ke liye embeddings mein convert karta hai
  • Classifier-Free Guidance – text conditioning strengthen karne ki technique (woh CFG scale slider)
  • DDIM Sampling – faster sampling algorithm (isliye 20–50 steps, 1000 nahi)
  • Attention Mechanisms – text image generation ko kaise condition karta hai
  • Perceptual Loss – related idea; note karo ki SD KL-autoencoder L2+KL use karta hai, VGG nahi

#flashcards/ai-ml

Latent diffusion models ki key innovation kya hai? :: Diffusion process (noise addition aur denoising) ko ek low-dimensional latent space mein chalana jo VAE se produce hota hai, seedhe pixel space mein nahi, isse large speedup milta hai negligible quality loss ke saath.

Latent diffusion ke liye VAE (with KL) kyun use karte hain, plain autoencoder kyun nahi?
KL term latent distribution ko ek smooth Gaussian-like prior ki taraf push karta hai, "holes" prevent karta hai jahan interpolation break ho jaata hai. Diffusion ko latent space mein continuously traverse karna padta hai—smoothness ke bina, denoising artifacts produce karta hai.
Stable Diffusion v1.5 mein dimension reduction kitna hai?
512×512×3 (786,432 dimensions) → 64×64×4 (16,384 dimensions), f=8 downsampling VAE ke zariye ~98% reduction.
Latent diffusion forward process equation kya hai?
jahan encoded image hai aur noise hai.

Latent diffusion ke liye training objective kya hai? :: — U-Net ko train karo noise predict karne ke liye diye gaye noisy latent , timestep , aur text conditioning ke liye.

DDPM reverse-step update kya hai?
, jahan aur .
Stable Diffusion mein text conditioning kaise kaam karta hai?
Text CLIP se encode hokar embedding banta hai, phir U-Net mein cross-attention ke zariye inject hota hai. Query image features se aata hai, key/value text se, jisse har spatial region relevant prompt tokens par attend kar sake.
Stable Diffusion VAE ko kaunsa loss train karta hai?
Pixel-level L2 reconstruction loss + ek KL divergence regularization term (standard KL-autoencoder recipe); us recipe mein VGG/perceptual loss nahi hota.
Self-attention pixel-space diffusion ko itna expensive kyun banata hai?
Attention cost spatial tokens ki sankhya mein scale karta hai. 512² par 262,144 tokens hain; 64² par sirf 4,096, isliye attention cost drop ho jaata hai.
Stable Diffusion generation ke chaar steps kya hain?
1) CLIP se text encode karo → , 2) Noise sample karo , 3) U-Net use karke T steps denoise karo, 4) VAE decoder se ko image mein decode karo.
Classifier-Free Guidance (CFG) kya hai?
. Scale prompt adherence vs diversity control karta hai.
SD VAE space mein diffusion latent manifold se "fall off" kyun nahi karta?
KL regularization latents ko near-Gaussian aur smooth banata hai, isliye saare points valid images mein interpolate hote hain—denoising ke dauran koi "holes" nahi hote jisme girna pade.

Concept Map

compressed by

produces

removed by

forward process

denoised by

conditions

predicts noise to recover

reconstructed by

yields

98% smaller so

Image 512x512x3

VAE Encoder

Latent 64x64x4

Add Gaussian noise

Diffusion U-Net

Text embedding c

Clean latent z0

VAE Decoder

Output image

Pixel redundancy