Skip to content
AgenticAIWiki
  1. Learn
  2. Part 1
  3. 8. Multimodal and voice
  4. 8.7

Lesson 8.7 · 7 of 9 · 6 min read · Reviewed 24 Nov 2026

Diffusion and flow matching

By the end, you can

  • Describe what a diffusion model learns during training.
  • Explain how flow matching differs from diffusion.
  • Relate steps, guidance and seeds to speed and quality.

Lesson 8.4 gave the picture: start from noise, remove a little at a time, steered by the prompt. This lesson looks at what the network actually learns, and at flow matching, the newer objective behind several recent image and video models.

Diffusion: learn to predict the noise

Training a diffusion model is a simple loop:

1. Take a training image x (usually its latent, see below).
2. Pick a random noise level t between "almost clean" and "pure noise".
3. Mix: noisy = a(t) x image + s(t) x random_noise
4. Ask the network: given noisy, t and the caption, what was the noise?
5. Loss = how far its guess is from the real noise. Update the weights.

The schedule a(t) and s(t) decides how fast the image fades. At generation time the model runs this backwards: start from pure noise, predict the noise, remove part of it, and repeat. Classic samplers took hundreds or a thousand steps; better samplers brought that down to tens.

Flow matching: learn the direction

Flow matching changes the question. Draw a straight line between a noise sample and an image:

x(t) = (1 - t) x noise + t x image        t goes from 0 to 1

The velocity along that line is simply image - noise. The network is trained to predict this velocity at any point x(t). To generate, start at noise and follow the predicted velocity in steps, like walking with a compass.

Diffusion (noise prediction) Flow matching (velocity prediction)
Model predicts The noise in the input Which way to move, and how fast
Path from noise to image Curved, set by the noise schedule Straight lines in training, so close to straight
Typical steps Tens with a good sampler Often fewer for the same quality
Seen in Many earlier image models Several recent image and video models, such as Stable Diffusion 3

The two are closely related; with the right schedule, one can be rewritten as the other. The practical gain is that straighter paths tolerate bigger steps.

Latent space

Running either process on full-size pixels is slow. Latent diffusion first trains an autoencoder that squeezes an image into a much smaller grid (for example 8 times smaller per side), runs the denoising there, and decodes to pixels at the end. That’s the “decode from latent space” step in 7.4.

The three knobs you meet

Knob What it does Typical effect
Steps How many updates from noise to image Too few: blurry or broken; past a point: no gain, just slower
Guidance scale How strongly the prompt pulls each step (classifier-free guidance) Low: loose, varied; high: literal, harsh colours, less variety
Seed Which starting noise you use Same seed, prompt and settings give the same image

Classifier-free guidance runs the model twice per step, once with the prompt and once without, then pushes the result further in the prompt’s direction. That doubles the work per step, which is one reason step count matters.

A worked example

A product team wants 40 banner drafts, then one polished final.

  • Drafts: fewer steps, moderate guidance, 40 different seeds. Fast, varied.
  • Pick: a person chooses two; keep their seeds.
  • Final: same seed and prompt, more steps, then a small prompt tweak. The composition stays because the starting noise is the same.

Common mistakes

  • Raising guidance to fix a bad prompt. It exaggerates what the prompt says; it doesn’t add missing detail.
  • Comparing settings with different seeds. Fix the seed when you test steps or guidance, or you’re comparing luck.
  • Assuming more steps always helps. Find the knee of the curve for your model and stop there.

Key points

  • A diffusion model is trained to predict the noise that was added to an image at a random noise level.
  • Flow matching trains a model to predict a velocity: which way to move from noise towards an image. Straighter paths need fewer steps.
  • Both generate by starting from random noise and taking many small steps; most work in a compressed latent space.
  • Classifier-free guidance strengthens the prompt at each step; too much makes images harsh and less varied.
Hands-on labRuns in the Playground

Follow a flow from noise to data

See why straighter paths from noise to data need fewer generation steps.

You will have: A small Euler sampler that follows a straight and a curved velocity field, with the final error at 1, 2, 4 and 16 steps.

Steps

  1. Run the starter: the sampler never moves, so the error is large for every step count.
  2. Fill in euler(): from t = 0 to 1 in n equal steps, add velocity x step size each time.
  3. Run it on the straight field (flow matching style) and the curved field.
  4. Print the distance from the target after 1, 2, 4 and 16 steps for both.
  5. Explain why the straight path is exact even with a single step.

Done when

  • The straight field reaches the target with error 0.000 at every step count.
  • The curved field's error shrinks as steps increase.
  • At 1 step the curved field misses by more than 1.0.

Open in the Playground No API key needed.

Starter code (Python)
import math
NOISE = (0.0, 0.0)   # starting point, a sample of noise
DATA = (3.0, 1.0)    # the "image" we want to reach
BEND = 1.0           # how curved the second path is

def straight_v(x, t):
    return (DATA[0] - NOISE[0], DATA[1] - NOISE[1])

def curved_v(x, t):
    # derivative of a path that bows sideways and comes back: noise + t*(data-noise) + BEND*sin(pi t) sideways
    side = BEND * math.pi * math.cos(math.pi * t)
    return (DATA[0] - NOISE[0] - side * 0.3, DATA[1] - NOISE[1] + side)

def euler(v, n):
    x = list(NOISE)
    # TODO: for i in range(n): t = i / n; dx = v(x, t); x += dx * (1 / n)
    return x

for name, v in [("straight", straight_v), ("curved", curved_v)]:
    for n in [1, 2, 4, 16]:
        x = euler(v, n)
        print(name, n, "steps, error", round(math.dist(x, DATA), 3))
Show a solution
import math
NOISE = (0.0, 0.0)
DATA = (3.0, 1.0)
BEND = 1.0

def straight_v(x, t):
    return (DATA[0] - NOISE[0], DATA[1] - NOISE[1])

def curved_v(x, t):
    side = BEND * math.pi * math.cos(math.pi * t)
    return (DATA[0] - NOISE[0] - side * 0.3, DATA[1] - NOISE[1] + side)

def euler(v, n):
    x = list(NOISE)
    for i in range(n):
        t = i / n
        dx = v(x, t)
        x = [x[0] + dx[0] / n, x[1] + dx[1] / n]
    return x

for name, v in [("straight", straight_v), ("curved", curved_v)]:
    errors = [f"{math.dist(euler(v, n), DATA):.3f}" for n in [1, 2, 4, 16]]
    print(f"{name:8} error at 1/2/4/16 steps: {' / '.join(errors)}")
Quick checkWhy can flow matching models often make a good image in fewer steps than classic diffusion?
How sure are you?
Pick an answer and how sure you are