Lab 22

Learn to wreck a picture with noise, and you have learnt how to make one

A diffusion model is trained on a job that sounds useless: take a photograph, mix in a measured amount of static, and guess which static was added. Repeat a few hundred million times and you own a machine that can start from pure noise and subtract its way to a picture. Here you can drive a real noise schedule in both directions, then train a small diffusion model β€” forward pass and backpropagation, written out in this page β€” until it sculpts a shape out of random points.

t = 0
t = 0the clean image, xβ‚€. Drawn by code on this page β€” no image files anywhere.
t = 1000every trace of the picture gone. This is the destination, and it has to be.
xt = √ᾱtΒ·xβ‚€ + √(1βˆ’αΎ±t)Β·Ξ΅
Ξ΅ ~ N(0, 1) Β· one fixed draw of static

0
0 Β· clean1000 Β· static
1.000αΎ±t Β· signal power
∞ dBSignal-to-noise
100%√ᾱ · image left
0%√(1βˆ’αΎ±) Β· static in

The reverse sweep replays the same schedule backwards using the noise we already know we added. Nothing here is predicting anything β€” it is the destination the next section's network has to reach without being told the answer.

A model that actually learns

Train a diffusion model in two seconds

Pictures are too big to train on in a browser tab, so this works in two dimensions instead, where the whole idea fits on screen. The target is a cloud of points in a shape. A small neural network β€” two hidden layers, 4,994 weights, trained by backpropagation written out below this page's controls β€” learns one thing only: given a noisy point and the timestep it came from, which way was the noise pushing? Then it starts from pure Gaussian points and undoes its own answer, over and over.

Starting up
β€”
Targetβ€”

The training data. The network never sees this directly β€” only noisy versions of it.

Untrained0 steps

Sampled with the weights straight after random initialisation. A shapeless blob, as it should be.

Trainedβ€”

The same sampler, the same starting noise, after training. Only the weights changed.

2500
0.006
50
β€”Loss (MSE on Ξ΅)
0Steps trained
4,994Weights
β€”Training time

Loss 1.00 is what you get for answering β€œno noise at all” every time, because the noise has variance 1. Anything below that is real knowledge about the shape.

What fewer steps costs you

Each denoising step is a straight-line guess at a curved path. Take few, long steps and you overshoot; take many short ones and you stay on the curve. Same trained network, same starting noise, only the number of steps changes β€” which is exactly why a picture generated at 4 steps looks smeared and the same seed at 50 steps looks clean.

Conditioning

How a sentence gets inside the noise

Nothing above knows about words. Turning a denoiser into something you can talk to takes three extra pieces: a way to turn text into numbers that live in the same space as pictures, a way to let those numbers reach into every denoising step, and a dial for how hard to insist. The diagrams here are drawn to explain the wiring β€” they are not running a text model.

Text and pictures, measured with the same ruler

CLIP trains two encoders at once β€” one for images, one for captions β€” and pushes the matching pair together while pushing every other pair in the batch apart. OpenAI trained it on 400 million image–text pairs with 32,768 pairs in each batch, so each correct pairing was scored against 32,767 wrong ones at a time. Afterwards a sentence and a photograph are points in one shared space, and "close together" means "these are about the same thing".

Cross-attention: the prompt is consulted at every step

Stable Diffusion turns your prompt into 77 token vectors of 768 numbers each. Inside the U-Net, every position in the latent image asks a question of all 77 of them and pulls back a weighted blend. At 64Γ—64 latent resolution that is 4,096 positions Γ— 77 tokens = 315,392 attention weights per head, per layer, recomputed at every one of the 50 steps. The line weights drawn below are illustrative, not measured.

Classifier-free guidance: the volume knob on obedience

The model is run twice at every step β€” once with your prompt and once with an empty one β€” and the difference between the two predictions is the part that is because of the prompt. Guidance scale multiplies that difference before it is applied. The arithmetic below is exactly the formula that runs inside the model; the arrows are two-dimensional stand-ins for a prediction that really has 16,384 numbers in it.

7.5
Applied predictionβ€”
Times longer than Ξ΅uncondβ€”
Extra compute per step2Γ— forward passes

Why the work happens in a smaller space

Denoising 512Γ—512 pixels directly means carrying 786,432 numbers through every step. Stable Diffusion does not. A separately trained autoencoder squeezes the image into a 64Γ—64 grid of 4 channels β€” 16,384 numbers, a 48-fold reduction β€” and the entire diffusion process happens in there. Only at the very end does the decoder turn the finished latent back into pixels. That single choice is why the model runs on a laptop graphics card instead of a data centre.

786,432numbers in a 512Γ—512Γ—3 image
16,384numbers in a 64Γ—64Γ—4 latent
48Γ—fewer numbers to denoise
8Γ—spatial shrink in each direction
Reference

The things worth remembering