# How Do Diffusion Models Work, Basically?

> How diffusion models learn to remove noise, then make pictures from static, with the cost trade-off explained simply for your AI interview.

**In short:** A diffusion model learns to remove noise. In training it damages real photos a little at a time and learns to undo each step. To make a new picture, it starts from random static and cleans it up step by step, guided by your text prompt.

AI and LLM interview question Q444, Multimodal and newer topics. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-do-diffusion-models-work-basically

## Say it in 30 seconds

A diffusion model learns to remove noise. In training, I take real photos, add noise in small steps until they are pure static, and teach the network to guess the noise at each step. To make a new picture, it starts from random static and removes the noise step by step, guided by a text prompt. It works very well, but each picture takes many network runs, so teams work hard to make those runs cheaper.

## Say it in 2 minutes

A diffusion model is a network that learns to remove noise. Noise is random graininess, like the speckle on a photo taken in a dark room. A model that is good at cleaning noise can start from pure static and clean its way to a picture. Ho and colleagues described this in 2020 as a chain of small steps, each one undoing a bit of damage.

Training runs the damage first. A real photo gets a little noise added, again and again, until nothing of the picture is left. The network sees a noisy version and a step number, and learns to guess the noise that was added. Nothing new is painted during training. The network only learns to undo one small step at a time.

Making a picture starts from random static and runs the clean-up many times. A text prompt is turned into numbers and fed into the network at each step, so the clean-up is steered toward the words. The catch is cost. Each image needs many network runs, one after another. Latent diffusion helps by doing those runs on a smaller, compressed version of the image.

So my short answer is that the model learns the reverse of a fixed damage process. Where it is strong, the images are very good. Where it is weak, it is slow and expensive, so cost is a real design question for any team that uses it at scale.

## What is a diffusion model, in plain words?

A diffusion model is a network that learns to remove noise. Noise is random graininess, like the speckle on a photo taken in a dark room. Once a model is good at cleaning noise, it can start from pure static and clean its way to a picture.

Think of a document conservator at a records office. She has seen thousands of water-damaged files. Shown a smudged page, she learns to guess what the page looked like before the damage, one layer at a time. A diffusion model learns the same kind of repair, but with pixels instead of ink.

Now a real case. A retailer's catalogue team needs a plain studio backdrop behind thousands of product shots. A diffusion model can build each backdrop from static, guided by a text prompt that describes the look.

- **Noise:** Random speckle added to an image, like grain on a photo taken in the dark.
- **Clean-up step:** One pass where the model removes a little of the noise.
- **Latent space:** A compressed, smaller version of the image, where each step is cheaper to run.
- **Text prompt:** The words you give the model, which steer what the picture shows.
- **Sampling:** Making a new image by running the clean-up steps from random static.

## How does training work, step by step?

Training never paints a picture. It damages real photos on purpose, then teaches the network to undo one small step of the damage.

*Diagram, How a diffusion model is trained: Real photos get a little noise, step by step, and the network learns to guess the noise it was given. The damage recipe is fixed. Only the network learns.*
1. A real photo is damaged a little on purpose, one step at a time.
2. The network sees the damaged picture and the step number.
3. Its guess is compared with the noise that was really added.

1. **Take a real photo:** A picture from the training set, the kind of image the model should learn to match.
2. **Add a little noise:** A fixed recipe adds a small amount of random noise. Repeat it until the photo is pure static. Nothing is learned in this part.
3. **Ask the network to guess:** It sees the noisy picture and the step number, and guesses the noise that was added.
4. **Correct the guess:** The guess is compared with the real noise. The network's settings (its weights) are nudged so the next guess is closer.

Here is the damage part on its own, in a few lines of Python. A made-up strip of 8 pixels gets more noise at each of 6 steps. Each step keeps a little less of the photo and mixes in fresh random noise.

```python
# A simplified forward pass: 8 brightness values (a made-up photo strip) get more noise each step.
import math
import random

random.seed(7)
photo = [0.9, 0.9, 0.1, 0.1, 0.9, 0.1, 0.9, 0.9]  # brightness, 0 (black) to 1 (white)
noise_per_step = [0.05, 0.10, 0.20, 0.35, 0.50, 0.70]  # a made-up schedule: more noise each step
x = photo[:]
kept = 1.0  # how much of the original photo is still in the picture
for step, beta in enumerate(noise_per_step, start=1):
    kept *= math.sqrt(1 - beta)
    x = [math.sqrt(1 - beta) * v + math.sqrt(beta) * random.gauss(0, 1) for v in x]
    print(f"step {step}: photo kept {kept:.2f}, first pixels {[round(v, 2) for v in x[:4]]}")
print(f"original first pixels: {photo[:4]}")
```

Output:

```text
step 1: photo kept 0.97, first pixels [0.82, 0.99, 0.05, 0.03]
step 2: photo kept 0.92, first pixels [1.11, 1.02, 0.17, 0.08]
step 3: photo kept 0.83, first pixels [0.23, 0.13, -0.25, -0.13]
step 4: photo kept 0.67, first pixels [0.37, 0.34, -0.59, 0.91]
step 5: photo kept 0.47, first pixels [0.02, 0.16, 0.03, 0.82]
step 6: photo kept 0.26, first pixels [-0.67, 0.3, 0.37, -0.8]
original first pixels: [0.9, 0.9, 0.1, 0.1]
```

The share of the photo that is left falls from 0.97 after step 1 to 0.26 after step 6. By then the first pixels no longer look like the bright, bright, dark, dark pattern they started as. A real model uses about 1,000 much smaller steps, and the network learns to guess the noise added at each one.

> "which is a Markov chain that gradually adds noise to the data in the opposite direction of sampling until signal is destroyed" (Ho and colleagues, 2020)
>
> "Creating noise from data is easy; creating data from noise is generative modeling." (Song and colleagues, 2020)

A Markov chain is a chain of steps where each step depends only on the one before it. That is what makes each noising step simple to define. The 2020 paper set the number of steps, T, to 1,000 in its experiments.

## How does it make a new picture?

Making a picture starts where the damage ends: from random static. The network removes a little noise, then runs again on its own output, and again. After the last run, the result is the picture. Running these clean-up steps from random static to a finished picture is called sampling. The prompt is turned into numbers, and cross-attention layers (parts of the network that let the picture check those numbers) feed them in at every step, so each clean-up pass is pulled toward the words.

*Diagram, How a new picture is made: Generation starts from random static. The prompt's numbers steer each run of the network, and what is left after the last run is the picture. Many runs in a row make it slow.*
1. Each new picture starts as random static, not as a photo.
2. The words are fed in, so every clean-up run is steered toward them.
3. After many runs in a row, what is left is the picture.

A worked example with made-up numbers. A catalogue team needs 50,000 product shots a month. If each shot takes 1,000 clean-up runs, that is 50 million network runs a month. The latent diffusion paper says the same model architecture must run sequentially for a large number of steps, so one picture needs its steps one after another.

- **The hard part:** Cost and waiting time per picture, multiplied by every picture the team makes.
- **The fix:** Fewer steps cut the network runs each picture needs. A smaller space makes each run cheaper.
- **The check:** Measure speed and quality on real shots before you promise the team a number.

## How does latent diffusion work?

Running every step on full-size pixels is heavy. Latent diffusion first squeezes the picture into a smaller code, using an autoencoder (a model that squeezes an image down and can rebuild it). The clean-up runs on that small code, and the result is rebuilt into a full picture.

*Diagram, Pixel space or latent space: Two ways to run the same clean-up. Pixel space works on every pixel and is heavy. Latent space works on a smaller code, then rebuilds the picture, which is cheaper.*
1. Pixel space runs every step on the full picture, which is heavy.
2. Latent space runs the same steps on a small code, then rebuilds the picture.

> "However, since these models typically operate directly in pixel space, optimization of powerful DMs often consumes hundreds of GPU days and inference is expensive due to sequential evaluations." (Rombach and colleagues, 2022)
>
> "By introducing cross-attention layers into the model architecture, we turn diffusion models into powerful and flexible generators for general conditioning inputs such as text or bounding boxes" (Rombach and colleagues, 2022)

The paper's main claim is about cost. A GPU day is a day of work on one graphics chip, so hundreds of them is a very large bill. The paper sets out to train on limited computing resources while keeping quality, and working in the smaller space cuts the work a lot compared with pixel models.

## Are diffusion models better than GANs?

A GAN (generative adversarial network) trains two networks at once. One makes images and the other judges them, and they push each other. In 2021, Dhariwal and Nichol reported that diffusion models could reach image sample quality superior to the state-of-the-art generative models of the time. Their results were measured on ImageNet with FID, a score where lower is better.

- **GAN: One maker, one judge.** Two networks train against each other. The maker is the part that makes the picture.
- **Diffusion: Many small steps.** Very strong image quality, but each picture needs many network runs, one after another, which costs time and machine power.

So the honest answer is a trade-off. Diffusion gives strong quality, and you pay for it in time and machine power for every image.

### How would you run it for 50,000 product photos a month?

- **Pick the smaller space:** A latent model cuts the cost of each run, so the monthly bill drops.
- **Cut the steps:** Use the fewest steps that still look right on your real product shots.
- **Budget it first:** 50,000 pictures times the cost of one picture is the number finance will ask for.
- **Keep a person in the loop:** A reviewer checks labels and product shape before anything goes live.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: a network that learns to remove noise, then makes pictures by cleaning up static.
- [ ] Training: noise is added to real photos, and the network learns to guess the noise.
- [ ] Generation: start from static, run many steps, guided by the prompt.
- [ ] The cost: many network runs per image, and how latent space helps.
- [ ] The trade-off: image quality against speed, compared with GANs.
- [ ] A real example of where the cost bites at scale.

**Common traps**

- Trap: "The model draws the picture step by step, like a painter." Better: It removes noise from static, guided by the prompt. It learned that by undoing damage in training.
- Trap: "Each new picture is a fresh training run." Better: Training happens once. Making a picture only runs the clean-up steps.
- Trap: "More steps always gives a better picture." Better: Each step costs a full network run. Pick the fewest steps that keep quality on your real shots.

## Can you spot the wrong note?

**Spot the wrong note:** A product team wrote four notes about their image tool. Which note is wrong?

A. Training adds noise to real photos, then the network learns to predict that noise.
B. Generating starts from random noise and removes a little of it at each step.
C. The network is trained once per image, so every new picture needs its own training run.
D. A text prompt is turned into numbers that steer each clean-up step.

**Answer:** C. The network is trained once on many photos. A new picture needs only the clean-up runs, not a new training run. Retraining per image would multiply the cost.

**Quick check:** A team wants 50,000 images a month, each built from 1,000 steps. Which change cuts the cost most safely?

A. Use a bigger network so fewer steps are needed
B. Cut the steps to the fewest that keep quality, and use a latent model
C. Drop the human check on sampled images
D. Double the amount of noise added at the start

**Answer:** B. Fewer steps cut the network runs each image needs, and a smaller space makes each run cheaper. A bigger network costs more on every run, and a new network per photo multiplies the cost.

## Can you answer these from memory?

**Flash cards**

- **What is a diffusion model?** A network that learns to remove noise, then makes pictures by cleaning up random static.
- **What happens in training?** Real photos get noise added step by step. The network learns to guess the noise.
- **How is a new image made?** Start from random static and run the clean-up steps, guided by the prompt.
- **Why latent diffusion?** It runs the steps on a smaller compressed code, which is cheaper than full pixels.
- **Why is it slow?** Each image needs many network runs, one after another.
- **How does a text prompt steer it?** Its words feed into the network through attention layers at each step.

## Likely follow-up questions

- Q443 What is CLIP? https://designervenkat.online/interview/ai/what-is-clip
- Q442 How do images become tokens? https://designervenkat.online/interview/ai/how-do-images-become-tokens
- Q441 What is a vision-language model? https://designervenkat.online/interview/ai/what-is-a-vision-language-model
- Q453 Safety for text-to-image? https://designervenkat.online/interview/ai/safety-for-text-to-image
- Q449 Uses and risks of synthetic data (model collapse)? https://designervenkat.online/interview/ai/uses-and-risks-of-synthetic-data-model-collapse

## Learn it in depth

- [Generating images from text](https://designervenkat.online/learn/ai-engineering/image-generation)
- [GANs: two networks, one game](https://designervenkat.online/learn/machine-learning/generative-adversarial-networks)
