Type a sentence, wait a few seconds, and get a photorealistic image that never existed. Behind that trick sits one core idea: teach a neural network to remove noise, then run it on pure static until a picture emerges. This guide breaks down the math, the architecture, and the code, from forward noising to latent-space sampling and classifier-free guidance.
Quick Takeaways
- Diffusion models learn to reverse a gradual noising process. They generate images by denoising random Gaussian noise step by step.
- Modern systems (Stable Diffusion, SDXL, FLUX, DALL·E 3) run diffusion in a compressed latent space, cutting compute by roughly 48x versus pixel space.
- Text prompts steer generation through CLIP or T5 embeddings and classifier-free guidance (CFG), typically set between 5 and 9.
- Newer architectures replace the U-Net with Diffusion Transformers (DiT) and train with flow matching for faster, cleaner sampling.
| Approach | Core Idea | Sample Quality | Speed | Training Stability |
|---|---|---|---|---|
| Diffusion | Iterative denoising | Excellent | Slow (20–50 steps) | Stable |
| GAN | Generator vs. discriminator | Sharp, less diverse | Fast (1 pass) | Unstable |
| VAE | Encode/decode latent | Often blurry | Fast | Stable |
| Autoregressive | Predict patches/tokens | Strong | Slow | Stable |
What Is a Diffusion Model?
A diffusion model is a generative model that learns the structure of images by learning to destroy and then rebuild them.
The idea comes from non-equilibrium thermodynamics. Think of a drop of ink in water. The ink spreads until the water is uniformly cloudy. Diffusion models run that movie in reverse: from uniform cloud back to the ink drop.
Two processes define the framework:
- Forward process (fixed): Add small amounts of Gaussian noise to a real image over T steps (typically T = 1,000) until only noise remains.
- Reverse process (learned): Train a neural network to predict and remove the noise at each step.
The landmark paper, Denoising Diffusion Probabilistic Models (DDPM, Ho et al., 2020), made this practical and set off the current wave of image generation.
The Forward Process: Adding Noise
At each step t, the model mixes the image with Gaussian noise according to a noise schedule β₁…β_T. Values usually rise from β₁ = 0.0001 to β_T = 0.02 in the original linear schedule.
The key formula lets you jump to any step t directly:
x_t = √(ᾱ_t) · x_0 + √(1 − ᾱ_t) · ε, where ε ~ N(0, I)
- x₀ is the clean image.
- ᾱ_t is the cumulative product of (1 − β) up to step t.
- ε is the random noise sample.
This shortcut matters for training. You never simulate 1,000 steps. You pick a random t, add the matching noise in one operation, and train on that.
Noise Schedules
| Schedule | Behavior | Used In |
|---|---|---|
| Linear | Steady noise growth | Original DDPM |
| Cosine | Slower early corruption, preserves detail | Improved DDPM |
| Scaled-linear | Tuned for latent space | Stable Diffusion 1.x/2.x |
| Zero-terminal SNR | Ends at true pure noise | SDXL variants, v-prediction models |
The Reverse Process: Learning to Denoise
The network takes a noisy image x_t, the timestep t, and optionally a text embedding. It predicts the noise ε that was added.
The training loss is simple mean squared error:
L = || ε − ε_θ(x_t, t, c) ||²
That is the entire objective. No adversarial game, no complex likelihood bound in practice. The network guesses the noise, and you penalize the error.
At inference, you start with pure noise x_T and loop:
- Predict the noise with the network.
- Subtract a scaled portion of it.
- Add a small amount of fresh noise (in stochastic samplers).
- Repeat until t = 0.
Different samplers (DDIM, Euler, DPM++ 2M Karras, UniPC) change how step 2 and 3 are computed. Good samplers cut the required steps from 1,000 down to 20–30 with little quality loss.
Latent Diffusion: Why Stable Diffusion Is Fast
Running diffusion on a 512×512×3 image means processing about 786,000 values per step. Latent diffusion (Rombach et al., 2022) avoids this.
A variational autoencoder (VAE) compresses the image into a small latent tensor first. For Stable Diffusion 1.5, that is 64×64×4 = 16,384 values, a 48x reduction. Diffusion happens entirely there. A VAE decoder converts the final latent back to pixels.
The pipeline has three parts:
| Component | Role | SD 1.5 Spec |
|---|---|---|
| VAE | Compress/decompress images | 8x spatial downsampling, 4 latent channels |
| Denoiser (U-Net) | Predict noise in latent space | ~860M parameters |
| Text encoder | Turn prompts into vectors | CLIP ViT-L/14, 77 tokens, 768-dim |
How Text Prompts Steer the Image
The model does not read words. It reads embeddings.
- A text encoder (CLIP, OpenCLIP, or T5) converts your prompt into a sequence of vectors.
- Cross-attention layers inside the denoiser let image features attend to those text vectors at every step.
- The network learns that noise patterns consistent with “red fox in snow” look different from those for “city skyline at night.”
Classifier-Free Guidance (CFG)
CFG is the dial that controls prompt adherence. During training, the text condition is randomly dropped (about 10% of the time), so the model learns both conditional and unconditional prediction. At inference, it computes both and extrapolates:
ε_final = ε_uncond + s · (ε_cond − ε_uncond)
Here s is the guidance scale.
| CFG Scale | Effect |
|---|---|
| 1–3 | Creative, loose, may ignore the prompt |
| 5–9 | Balanced; the usual default range |
| 12+ | Oversaturated, burned, artifact-prone |
CFG doubles the compute per step, since two predictions run each time. Many modern distilled models bake guidance in to avoid this cost.
Architecture Evolution: U-Net to Diffusion Transformers
| Generation | Backbone | Example Models | Notes |
|---|---|---|---|
| Gen 1 | U-Net with attention | SD 1.5, SD 2.x | Convolutional, strong inductive bias |
| Gen 2 | Larger U-Net, dual encoders | SDXL (~2.6B U-Net) | Better text rendering and composition |
| Gen 3 | Diffusion Transformer (DiT) | SD3, FLUX | Scales better with data and compute |
| Gen 4 | DiT + flow matching | FLUX, SD3 | Straighter sampling paths, fewer steps |
Flow matching (rectified flow) replaces noise prediction with learning a velocity field that moves noise to data along near-straight lines. Straighter paths mean fewer sampling steps and more stable training.
Hands-On: Generate an Image with Python
Install the dependencies:
pip install diffusers transformers accelerate torch safetensors
Run a text-to-image pipeline with Hugging Face diffusers:
import torch
from diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler
# Load SDXL in half precision (fp16) to fit ~8 GB VRAM
pipe = StableDiffusionXLPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
variant="fp16",
use_safetensors=True,
).to("cuda")
# Swap in a faster sampler (DPM++ 2M with Karras sigmas)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
pipe.scheduler.config, use_karras_sigmas=True
)
image = pipe(
prompt="a lighthouse on a cliff at dawn, volumetric fog, 35mm photo",
negative_prompt="blurry, low quality, extra limbs, watermark",
num_inference_steps=28, # sampler steps
guidance_scale=7.0, # CFG scale
height=1024,
width=1024,
generator=torch.Generator("cuda").manual_seed(42), # reproducibility
).images[0]
image.save("lighthouse.png")
Annotations:
torch.float16halves memory with minimal quality loss.num_inference_steps=28is a good speed/quality tradeoff for DPM++.manual_seed(42)fixes the starting noise, so identical settings produce identical images.negative_promptfeeds the unconditional branch of CFG, pushing results away from unwanted traits.
For low VRAM, add pipe.enable_model_cpu_offload() or pipe.enable_attention_slicing().
Controlling Output: Beyond the Prompt
| Technique | What It Does | Typical Use |
|---|---|---|
| img2img | Starts from a noised input image, not pure noise | Restyle a sketch or photo |
| Inpainting | Denoises only a masked region | Remove or replace objects |
| ControlNet | Adds spatial conditioning (edges, depth, pose) | Exact composition control |
| LoRA | Small low-rank weight adapters (often 10–200 MB) | Custom styles, characters |
| IP-Adapter | Conditions on a reference image | Style or identity transfer |
In img2img, the strength parameter (0–1) sets how much noise is added to the input. At 0.3, structure is preserved. At 0.8, the model reimagines most of it.
Model Comparison: Major Image Generators
Specifications and pricing change often. Verify against each vendor’s current documentation before committing to a pipeline.
| Model | Access | Architecture | Strength | Trade-off |
|---|---|---|---|---|
| Stable Diffusion (SD 1.5/SDXL) | Open weights | Latent U-Net | Huge ecosystem of LoRAs and tools | Weaker prompt following |
| FLUX | Open and hosted variants | DiT, flow matching | Strong text rendering, detail | Heavier VRAM needs |
| DALL·E 3 | Closed API | Diffusion, caption-enhanced | Excellent prompt adherence | No local control, fixed policies |
| Midjourney | Closed service | Proprietary | Distinct aesthetic quality | No public API weights or fine-tuning |
| Imagen family | Closed | Cascaded diffusion | Photorealism, language understanding | Limited access |
Real-World Use Cases and Workflows
Product Photography at Scale
E-commerce teams train a LoRA on 15–30 product photos, then generate lifestyle backgrounds. Pair it with inpainting to keep the product pixel-accurate while the scene changes.
Game and Concept Art Pipelines
Artists sketch rough compositions, then use ControlNet (depth or scribble) to lock layout while the model fills in rendering. Iteration drops from hours to minutes.
Advanced Prompt Template
Use a consistent structure for predictable results:
[Subject], [action/pose], [environment], [lighting], [camera/lens], [style/medium], [quality modifiers]
Example:
"An elderly clockmaker repairing a pocket watch, workshop interior,
warm tungsten lamp light, 85mm lens, shallow depth of field,
editorial photograph, highly detailed"
Tips that work:
- Put the subject first. Earlier tokens carry more weight in CLIP-based encoders.
- Use negative prompts for artifacts, not for concepts the model never considered.
- Lock the seed, then change one variable at a time.
- For T5-based models like FLUX, natural sentences outperform comma-separated tags.
Limitations and Failure Modes
- Hands, counting, and spatial relations remain error-prone, though newer DiT models improve them.
- Text rendering depends on the encoder; T5-based models handle it far better than CLIP-only ones.
- Training data bias shows up in outputs, so audit results for sensitive uses.
- Copyright and provenance questions are unsettled legally. Many platforms now attach C2PA metadata or invisible watermarks to generated images.
- Compute cost: each image requires dozens of full network passes, which is why distillation (LCM, SDXL Turbo, Lightning) targets 1–4 step generation.
FAQ
How do diffusion models create images from text?
They start with random noise and remove it over 20–50 steps. A text encoder converts the prompt into embeddings, and cross-attention plus classifier-free guidance push each denoising step toward an image that matches the prompt.
What is the difference between diffusion models and GANs?
GANs train a generator against a discriminator and produce an image in one pass, but training is unstable and diversity can suffer. Diffusion models train with a simple noise-prediction loss, giving better stability and diversity, at the cost of slower, multi-step generation.
Why is Stable Diffusion called a latent diffusion model?
It runs the denoising process in a compressed latent space created by a VAE, not on raw pixels. For SD 1.5, this shrinks a 512×512 image to a 64×64×4 latent, cutting compute by about 48x.
What does the CFG scale do in AI image generation?
CFG scale controls how strongly the output follows your prompt. Values of 5–9 balance accuracy and natural look. Very high values often cause oversaturation and visual artifacts.




