AnyLearn
All lessons
AIadvanced

Guidance and samplers: steering diffusion and making it fast

How raw denoisers become text-to-image systems: conditioning, classifier-free guidance and the CFG scale, latent diffusion, the sampler zoo from DDPM to DDIM and beyond, and the distillation techniques that cut a thousand steps down to a few.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 7

From denoiser to product: two missing pieces

The previous lesson built a model that turns static into plausible images. A product needs two more things: the image must match a prompt, and it must arrive in seconds, not minutes.

Both problems live in the sampling loop, and both were solved without changing the core training idea:

  • Steering. The denoiser must be told what to draw, and, crucially, told how strongly to obey. The mechanism, classifier-free guidance, is arguably the single most consequential trick in applied diffusion.
  • Speed. Naive sampling runs the network once per timestep, up to a thousand forward passes. The fixes stack: run in a compressed latent space (each pass gets cheaper), use smarter solvers (fewer passes needed), and finally distill (a student network jumps in one to four passes).

A useful frame for the whole lesson: the trained model defines a field of arrows from noise toward data (the score, from lesson 1). Everything here is about bending those arrows toward what the user asked for, and following them in fewer, larger, smarter steps.

Full lesson text

All 7 steps on one page, for reading, reference, and search.

Show

1. From denoiser to product: two missing pieces

The previous lesson built a model that turns static into plausible images. A product needs two more things: the image must match a prompt, and it must arrive in seconds, not minutes.

Both problems live in the sampling loop, and both were solved without changing the core training idea:

  • Steering. The denoiser must be told what to draw, and, crucially, told how strongly to obey. The mechanism, classifier-free guidance, is arguably the single most consequential trick in applied diffusion.
  • Speed. Naive sampling runs the network once per timestep, up to a thousand forward passes. The fixes stack: run in a compressed latent space (each pass gets cheaper), use smarter solvers (fewer passes needed), and finally distill (a student network jumps in one to four passes).

A useful frame for the whole lesson: the trained model defines a field of arrows from noise toward data (the score, from lesson 1). Everything here is about bending those arrows toward what the user asked for, and following them in fewer, larger, smarter steps.

2. Conditioning: telling the model what to draw

Making a denoiser conditional is architecturally straightforward: feed a representation of the condition into the network alongside the noisy image and timestep, ϵθ(xt,t,c)\epsilon_\theta(x_t, t, c).

For text-to-image, cc is produced by a text encoder, a frozen language model or a contrastively trained image-text encoder, whose token embeddings enter the backbone through cross-attention: every spatial location of the image representation attends over the prompt tokens and pulls in what is relevant locally. This is how "a red cube on a blue sphere" routes red to one region and blue to another. Class labels, depth maps, sketches, or reference images enter the same way, or by simple concatenation for spatially aligned conditions.

Training barely changes: the same noise-prediction loss, now with the condition attached to each example.

The catch shows up at generation time: a conditional model trained this way treats the prompt as one input among many, and the unconditional pull toward "any plausible image" often dominates. Samples come out generic, loosely related to the text. Conditioning gets the information in; it does not by itself make the model obey. That requires guidance.

3. Classifier-free guidance: the volume knob for obedience

Classifier-free guidance (CFG) solves obedience with an elegant two-for-one. During training, randomly drop the condition (replace the prompt with a null token, typically ~10% of the time). The single network then knows both a conditional prediction ϵθ(xt,c)\epsilon_\theta(x_t, c) and an unconditional one ϵθ(xt,)\epsilon_\theta(x_t, \varnothing).

At sampling time, run both and extrapolate past the conditional one:

ϵ~=ϵθ(xt,)+w(ϵθ(xt,c)ϵθ(xt,))\tilde\epsilon = \epsilon_\theta(x_t, \varnothing) + w \cdot \big(\epsilon_\theta(x_t, c) - \epsilon_\theta(x_t, \varnothing)\big)

The difference in parentheses is a direction in noise space: "what the prompt changes about the prediction." The guidance scale ww amplifies it. w=1w = 1 is plain conditioning; w=0w = 0 ignores the prompt; typical products run w5w \approx 5 to 88.

The trade-off is real and visible: higher ww buys prompt fidelity and sharper, more saturated images, but overcooks at the extreme (burned colors, reduced diversity, distorted anatomy), because you are pushing samples off the learned data manifold. That one scalar, exposed in every image tool as "CFG scale," is the practical dial between creativity and obedience. Cost note: vanilla CFG doubles compute (two forward passes per step), one more reason the speed section matters.

4. Latent diffusion: shrink the canvas, keep the picture

The first speed lever attacks the cost of each step. Pixels are a wasteful place to diffuse: a 1024x1024 RGB image is ~3 million values, most encoding texture detail that a generative model does not need to reason about step by step.

Latent diffusion (the architecture behind the Stable Diffusion family) inserts a pretrained autoencoder: an encoder compresses the image, typically 8x per side, into a latent tensor ~48x smaller; a decoder reconstructs pixels nearly losslessly. Diffusion then happens entirely in latent space: noise latents, denoise latents, and only at the very end decode once to pixels.

The wins compound:

  • Every network pass processes a far smaller tensor, so the same compute buys a bigger, smarter backbone.
  • The autoencoder absorbs perceptual detail (skin texture, grain), letting the diffusion model spend capacity on composition and semantics.
  • Training datasets can be pre-encoded once, shrinking storage and I/O.

The gotcha inherited by every latent system: the autoencoder is a ceiling. Whatever it cannot reconstruct, no amount of diffusion quality can restore; historically that meant small text and fine faces suffered first. Modern systems raised the latent channel count largely to lift this ceiling.

5. The sampler zoo: fewer, smarter steps

The second lever reduces how many steps you take. Recall from lesson 1 that a trained model defines a differential equation from noise to data; a "sampler" is just a numerical solver for it, and solvers differ enormously in how large a step they can take accurately.

  • DDPM (ancestral) sampling: the original stochastic chain, one small noisy step per timestep. Faithful but needs hundreds of steps.
  • DDIM reinterprets the same trained model as a deterministic ODE: same start point, same endpoint distribution, no injected noise along the way. Steps can be much larger; 20-50 suffice. Determinism also gives reproducibility and meaningful latent interpolation.
  • Higher-order solvers (Heun-style, DPM-Solver++ and family) use multiple function evaluations or history to cancel discretization error, pushing decent quality to ~10-20 steps.
SamplerTypeTypical steps
DDPM ancestralstochastic250-1000
DDIMdeterministic20-50
DPM-Solver++ classdeterministic, higher-order10-20

The key conceptual point: none of these require retraining. The model is fixed; only the integration scheme changes. Below ~10 steps, however, solver cleverness hits a wall, the arrows genuinely curve, and big straight steps miss. Getting to 1-4 steps needs training-time interventions.

6. Distillation: one to four steps

To go below the solver wall, change the model, not the solver. The family of techniques is distillation: train a student to reproduce, in one big jump, what the teacher achieves in many small ones.

  • Progressive distillation: the student learns to match two teacher steps with one of its own; halve repeatedly (1000 to 500 to 250 ... to 4). Each round is cheap supervised regression.
  • Consistency models: train a network mapping any point on a noise trajectory directly to that trajectory's endpoint, with a self-consistency loss (any two points on the same trajectory must map to the same result). One step generation, a few steps to refine.
  • Adversarial distillation (used by "turbo"-style checkpoints): add a discriminator so the student's single-step outputs stay sharp; crisper than pure regression students at very low step counts.

Costs, stated honestly: distilled students trade a little diversity and prompt fidelity for speed, guidance often has to be baked in (the CFG knob may be fixed or limited), and each distillation is an extra training run. Interactive products accept those trades happily, and this is how "real-time" image generation actually works.

One stone remains unturned: if big straight jumps are what we ultimately want, why train along curved paths at all? Making straightness the training objective itself is the idea behind flow matching, the next lesson.

7. The full production stack, assembled

Put the pieces together and you can read any modern text-to-image system as one pipeline:

prompt
  └─ text encoder ──────────────► token embeddings
noise latent (e.g. 128x128x C)
  └─ DiT / U-Net backbone ◄──── cross-attention on embeddings
       │  repeated N times (N = 4-50 by product tier)
       │  each step: eps_cond, eps_uncond → CFG mix (scale w)
       ▼
clean latent ── autoencoder decoder ──► 1024x1024 image

And the knobs you now understand mechanically, not just as sliders:

  • Steps: which solver regime you are in (or whether the model is distilled).
  • CFG scale: extrapolation strength between unconditional and conditional predictions.
  • Seed: the initial noise latent; with a deterministic sampler, same seed + prompt = same image.
  • Negative prompt: literally swaps what fills the unconditional slot in the CFG formula, so guidance pushes away from it. A direct corollary of the math, not a separate feature.

Every item traces back to two ideas: bend the arrows (guidance), and follow them cheaply (latents, solvers, distillation).

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. Why does conditioning alone (without guidance) produce images that only loosely follow the prompt?
    • Cross-attention cannot carry text information into the image
    • Text encoders are too small to represent prompts
    • The prompt is one input among many, and the model's pull toward generic plausible images often dominates
    • The noise schedule erases the condition at high timesteps
  2. In classifier-free guidance, what does the guidance scale w multiply?
    • The difference between the conditional and unconditional noise predictions
    • The learning rate of the sampler
    • The number of denoising steps
    • The variance of the initial noise
  3. What ceiling does every latent diffusion system inherit from its autoencoder?
    • The maximum CFG scale it can use
    • The number of sampling steps required
    • The maximum batch size during training
    • Details the autoencoder cannot reconstruct can never appear correctly, regardless of diffusion quality
  4. What makes DDIM able to use far fewer steps than ancestral DDPM sampling, without retraining?
    • It uses a smaller network for early steps
    • It reinterprets sampling as a deterministic ODE, allowing larger accurate steps along the same trajectory field
    • It skips the classifier-free guidance computation
    • It compresses the image into a latent space
  5. How do consistency models achieve one-step generation?
    • They increase the CFG scale until one step suffices
    • They use a discriminator to replace the denoiser
    • They train a network to map any point on a noise trajectory directly to its endpoint, enforcing self-consistency
    • They run the ODE solver at higher order

Related lessons

AI
advanced

When Generating Data Beats Collecting It

Synthetic data is roughly three orders of magnitude cheaper than human annotation, and cheapness is the least interesting thing about it. This lesson establishes what generation can and cannot manufacture: it produces coverage and format, never information the generator lacks, and the one exception is verifiable domains, where a checker turns generation into search.

10 steps·~15 min
AI
advanced

EBMs as a Unifying Lens

Why LeCun treats energy as the common language of machine learning. This lesson shows how classification, generative models, self-supervised learning, JEPA, and diffusion all read as energy-based models, ties the contrastive-versus-regularized split back to self-supervised learning, and gives an honest account of where explicit EBMs help and where they do not.

8 steps·~12 min
AI
advanced

Flow matching: straightening the path from noise to data

The reframing that took over frontier image generation: learn a velocity field that transports noise to data along direct paths. Conditional flow matching, rectified flow, why straight trajectories mean fewer sampling steps, and how diffusion becomes a special case.

7 steps·~11 min
AI
advanced

Diffusion models: learning to create by learning to denoise

How diffusion models generate images and more: the forward process that destroys data with noise, the reverse process that learns to undo it, the surprisingly simple training objective, and the network backbones (U-Net, DiT) that make it work.

8 steps·~12 min