Phase 1: Diffusion Model Fundamentals · 55 min · Python · Hugging Face Diffusers · ComfyUI
The Concept
Sampling methods
The sampler determines how the model traverses the reverse diffusion process — from noise to image. Different samplers take different paths, with different speed/quality trade-offs.
DDPM (Denoising Diffusion Probabilistic Models)
The original sampler from the 2020 paper.
- Steps needed: 50-1000 (typically 100+)
- Behavior: Stochastic — adds noise at each step, producing diverse outputs
- Quality: High, but requires many steps
- Use case: Research, when you need maximum diversity
- Speed: Slow (many steps)
DDIM (Denoising Diffusion Implicit Models)
A deterministic alternative to DDPM.
- Steps needed: 20-50 (much fewer than DDPM)
- Behavior: Deterministic — same seed + same steps = same output
- Quality: Good, close to DDPM with fewer steps
- Use case: When you need reproducibility, faster than DDPM
- Speed: Medium (fewer steps than DDPM)
DPM++ 2M Karras
The production standard for SDXL.
- Steps needed: 20-30
- Behavior: Deterministic, optimized step schedule
- Quality: Excellent — best quality per step
- Use case: Production image generation with SDXL
- Speed: Fast (fewer steps, optimized math)
Euler a (Ancestral)
Fast and creative.
- Steps needed: 15-25
- Behavior: Stochastic — adds noise, producing creative variation
- Quality: Good, slightly less precise than DPM++
- Use case: Creative exploration, fast iteration
- Speed: Fast
Comparison Table
| Sampler | Steps (typical) | Behavior | Quality | Speed | Best For |
|---|
| DDPM | 100+ | Stochastic | High | Slow | Research, max diversity |
| DDIM | 20-50 | Deterministic | Good | Medium | Reproducibility |
| DPM++ 2M Karras | 20-30 | Deterministic | Excellent | Fast | Production (SDXL) |
| Euler a | 15-25 | Stochastic | Good | Fast | Creative exploration |
| UniPC | 10-20 | Deterministic | Good | Very Fast | Speed-critical |
| LMS | 20-30 | Deterministic | Good | Fast | Alternative to DPM++ |
from diffusers import StableDiffusionXLPipeline
import torch
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
# Different samplers
pipe.scheduler = diffusers.DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) # DPM++
# pipe.scheduler = diffusers.DDIMScheduler.from_config(pipe.scheduler.config) # DDIM
# pipe.scheduler = diffusers.EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config) # Euler a
Classifier-free guidance (CFG)
CFG is the single most important parameter for controlling generation behavior. It controls how closely the model adheres to the text prompt vs. how much creative freedom it takes.
Guidance Scale = 1.0: Model ignores prompt (maximum creativity, minimum adherence)
Guidance Scale = 7.5: Balanced (default for most models)
Guidance Scale = 15+: Strong prompt adherence (can cause artifacts, oversaturation)
How CFG Works
At each diffusion step, the model runs twice:
- Conditional: with the text prompt embedding
- Unconditional: with a null/empty prompt
The final prediction extrapolates away from the unconditional:
ε_guided = ε_unconditional + scale × (ε_conditional - ε_unconditional)
- Low scale (1-3): small difference between conditional and unconditional → creative, less prompt-faithful
- High scale (10-15): large difference → strong prompt adherence, but can cause color artifacts and oversaturation
# Effect of guidance scale
for scale in [1.0, 7.5, 15.0]:
image = pipe(
prompt="a red sports car on a mountain road",
guidance_scale=scale, # 1.0 = creative, 7.5 = balanced, 15.0 = strict
num_inference_steps=25,
).images[0]
What happens when you set guidance_scale=1.0?
At guidance_scale=1.0, the conditional and unconditional predictions are weighted equally, so the text prompt has minimal influence. The model produces creative, diverse outputs that may not match the prompt well. This is useful for exploration but not for production where prompt adherence matters.
Steps vs quality
The number of inference steps has diminishing returns:
Steps: 5 10 15 20 25 30 50 100
Quality: 30% 60% 75% 85% 90% 93% 97% 99%
Time: 0.5s 1s 1.5s 2s 2.5s 3s 5s 10s
- 4 steps: FLUX Schnell (distilled model, 4 steps = full quality)
- 15-20 steps: Good enough for most production use with DPM++
- 25-30 steps: Sweet spot for SDXL with DPM++ 2M Karras
- 50+ steps: Diminishing returns — quality barely improves, cost doubles
More steps ≠ proportionally better quality
Going from 20 to 100 steps gives maybe 5% quality improvement but 5x cost. Always benchmark quality at different step counts and pick the knee of the curve — typically 20-30 steps for SDXL with DPM++.
FLUX schnell: the exception
FLUX.1 Schnell is a distilled model — it's trained to produce full-quality output in only 4 steps. This is fundamentally different from SDXL:
| Property | SDXL + DPM++ (30 steps) | FLUX Schnell (4 steps) |
|---|
| Time | ~3s | ~2s |
| Quality | High | High (comparable) |
| Guidance | 7.5 (CFG needed) | 0.0 (no CFG needed) |
| Cost | $0.025/image (fal.ai Dev) | $0.003/image (fal.ai Schnell) |
FLUX Schnell doesn't need CFG (guidance_scale=0.0) because it was trained with a different objective. This is a major speed advantage — no double forward pass per step.
Production optimization
# Production-optimized SDXL config
PRODUCTION_CONFIG = {
"scheduler": "DPM++ 2M Karras",
"steps": 25, # Sweet spot
"guidance_scale": 7.0, # Slightly below default for less artifacts
"cfg_rescale": 0.7, # Rescale CFG to reduce oversaturation
"use_fp16": True, # Half precision for speed
"enable_xformers": True, # Memory-efficient attention
"batch_size": 4, # Generate 4 at once for throughput
}
# Production-optimized FLUX Schnell config
FLUX_CONFIG = {
"steps": 4, # Only 4 steps
"guidance_scale": 0.0, # No CFG needed
"use_fp16": True,
}