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:
Images compress karne ke liye ek Variational Autoencoder (VAE) train karo: x∈RH×W×3→z∈Rh×w×c jahan h,w≪H,W
Diffusion sirf latent space z mein chalao
Final z0 ko VAE decoder se wapas image mein decode karo
Stable Diffusion f=8 downsampling use karta hai: 512×512 → 64×64, c=4 channels ke saath → 98% dimension reduction.
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 f=8: 512→64, 1024→128
Latent channels c=4 (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
Question: Pixel space se latent space mein denoise karna zyada efficient kyun hai?
Computational cost per denoising step (schematically):
cost∝(number of spatial locations)×(channels)×(per-token work)
aur self-attention part specifically spatial locations ki sankhya N=h⋅w mein quadratically scale karta hai:
attention cost∝N2=(hw)2
Pixel-space ke liye (DDPM):
Spatial locations: 512×512=262,144
Channels: 3 (RGB)
Total dim:262,144×3=786,432
Latent-space ke liye (Stable Diffusion):
Spatial locations: 64×64=4,096
Channels: 4
Total dim:4,096×4=16,384
Dimension ratio:16,384786,432=48× kam dimensions.
Kyunki attention cost N2 scale karta hai, spatial locations mein 262,144 se 4,096 tak ki reduction (N mein 64× drop) roughly yeh attention-cost reduction deta hai:
Nlat2Npix2=(4,096262,144)2=642=4096×
jabki element-wise convolutional work linearly scale karta hai N ke saath (~64× 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.
Classifier-Free Guidance (CFG): Sampling ke dauran, hum conditional aur unconditional predictions ke beech interpolate karte hain:
ϵ^=ϵθ(zt,t,∅)+s⋅(ϵθ(zt,t,c)−ϵθ(zt,t,∅))
jahan s 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:
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.
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.
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.
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.
Latent diffusion forward process equation kya hai?
zt=αˉtz0+1−αˉtϵ jahan z0=E(x) encoded image hai aur ϵ∼N(0,I) noise hai.
Latent diffusion ke liye training objective kya hai? :: L=Ez0,t,ϵ,c[∥ϵ−ϵθ(zt,t,c)∥2] — U-Net ko train karo noise ϵ predict karne ke liye diye gaye noisy latent zt, timestep t, aur text conditioning c ke liye.
Stable Diffusion mein text conditioning kaise kaam karta hai?
Text CLIP se encode hokar embedding c 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 N=hw mein N2 scale karta hai. 512² par 262,144 tokens hain; 64² par sirf 4,096, isliye attention cost (262144/4096)2=4096× drop ho jaata hai.
Stable Diffusion generation ke chaar steps kya hain?
1) CLIP se text encode karo → c, 2) Noise sample karo zT∼N(0,I), 3) U-Net ϵθ(zt,t,c) use karke T steps denoise karo, 4) VAE decoder D(z0) se z0 ko image mein decode karo.
Classifier-Free Guidance (CFG) kya hai?
ϵ^=ϵθ(zt,t,∅)+s(ϵθ(zt,t,c)−ϵθ(zt,t,∅)). Scale s 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.