Skip to content

Text to image: DALL·E's tokens and unCLIP's prior

Two routes from a prompt to a picture: write the image as discrete tokens after the text (DALL·E), or turn the caption's CLIP embedding into an image embedding and decode it with diffusion (unCLIP).

Advanced ~14 min8 steps

What runs in CodeSplash

You will be able to

  • Explain how a discrete image tokenizer lets a Transformer generate images
  • Compute the negative log-likelihood of image tokens given a text prefix
  • Describe unCLIP's prior and decoder and what each one generates
  • Judge generated images on fidelity, diversity, prompt adherence and failure rate

Read first

01

Images as a second language

Read

A Transformer generates text one token at a time. To generate images the same way, an image must become tokens. A discrete image tokenizerDiscrete image tokensAn image encoded as a grid of codebook indices, so a Transformer can generate it token by token like text., such as a discrete VAE or VQ-VAEVQ-VAEAn autoencoder whose code snaps to the nearest of a fixed set of learned vectors, giving discrete codes instead of Gaussian ones., compresses an image into a small grid of codebook indices and can decode such a grid back to pixels.

The original DALL·E used a 32 × 32 grid with 8,192 possible codes for a 256 × 256 image. A caption followed by its image is then one long sequence of tokens.

02

DALL·E: text, then image tokens

Read
Reference · MM019 · Runs as a notebookDALL·E: original autoregressive text-to-image modelModels text and discrete image tokens in one autoregressive stream. The original DALL·E is a historically important bridge from language-style sequence modeling to text-conditioned image generation, distinct from later…

DALL·E trained a large Transformer to predict the next token of caption-plus-image sequences. To generate, it reads the prompt, samples image tokens one at a time, decodes the grid to pixels, and makes many candidates that CLIP then reranks by how well they match the prompt.

Training minimizes the negative log-likelihood of each image token given the text and the tokens before it. In the program, three image tokens follow the prefix "a red cube"; their summed negative log-likelihood is 1.091.

03

Sampling tokens like words

Explore

Each image token is drawn from a probability distribution, exactly as a word is. Temperature and top-p trade variety against safety in the same way, and one unlucky early token shapes everything after it:

DExplorable · Temperature and top-p

What do the sampling settings on a language model actually change?

With these settings, how likely is the single most likely token?

0.50
How sure?
1.00

Below 1 sharpens the distribution; above 1 flattens it.

1.00

Keep the likeliest tokens until they hold this much probability.

12
200

“The p-value tells you how extreme”

surprising0.364unusual0.221likely0.148extreme0.099compatible0.066rare0.045strong0.024much0.016big0.010true0.005purple0.001sandwich0.000
  • With these settings
  • Default (temperature 1, no cut-off)

Twelve sampled tokens: extreme · surprising · surprising · unusual · rare · rare · unusual · surprising · rare · extreme · surprising · likely

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

04

How long is the sequence?

Predict

Predict first

A tokenizer turns a 256 × 256 image into a 32 × 32 grid of codes. How many image tokens must the Transformer generate, one at a time?

2561,02465,536
Reveal

1,024. Every one of them is a full forward pass, which is why autoregressive image generation is slow, and why the tokenizer compresses so hard: generating 65,536 pixels directly would be far worse.

05

unCLIP: a prior and a decoder

Read
Reference · MM020 · Explained hereDALL·E 2 / unCLIPSeparates text-to-image generation into a prior over CLIP image embeddings and a decoder that generates pixels from those embeddings.

DALL·E 2, or unCLIP, works in CLIP's embedding space. A priorunCLIP priorA model that turns a caption's CLIP text embedding into a plausible CLIP image embedding, which a decoder then draws. turns the caption's CLIP text embedding into a plausible CLIP image embedding; a diffusion decoder then draws an image that has that embedding, and upsamplers enlarge it.

In the program the text embedding plus prior noise, normalized, gives the image embedding (0.994, 0.110), which a toy linear decoder expands into three features. Splitting the work allows image variations: keep one image embedding, sample the decoder again.

How a diffusion decoder turns noise into samples, and what guidance does.

DExplorable · Diffusion: noise schedule, sampler steps, guidance

How does a diffusion model turn noise back into data, and what do the schedule, the steps and guidance change?

With this sampler and number of steps, will the samples match the data?

How sure?

β schedule

250

The forward process at this step: how much signal is left.

Sampler

DDPM adds fresh noise every step; DDIM is deterministic.

50
0.0

0 = unconditional, 1 = plain conditional, above 1 = guided.

Class to guide toward

400
  • Noised samples
  • Exact q(xₜ)
  • Samples
  • Data

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

06

Judging generated images

Read

There is no reference image, so judge four things separately: fidelity (do they look real?), diversity (do prompts give varied outputs?), prompt adherence (counts, colors, relations, text), and failure rate (how often is an output unusable or harmful?). FIDFréchet inception distance (FID)Distance between Gaussians fit to real and generated images' features; lower is closer, and it depends on the feature network. covers fidelity and diversity across many images; CLIPScoreCLIPScore2.5 × max(cosine of an image and its caption, 0): how well a caption fits its image, without reference captions. covers rough adherence, with CLIP's blind spots for counting and negation. Neither replaces a human look at a sample.

DExplorable · Scoring generators: FID, precision and recall

Can one number say how good generated samples are?

How will this generator's precision and recall come out?

How sure?

Generator

The real data is a ring of 8 clusters. Each generator gets something different wrong.

Feature extractor

FID and the k-NN scores compare features, not raw samples. Real FID uses a vision network; here, four simple maps.

200

How many real and how many generated samples are compared.

3

Each sample's ball reaches its k-th nearest neighbor in its own set.

Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

07

Check: text to image

Checkpoint

Check yourself

Why does DALL·E need a discrete image tokenizer?

Show the answer

Answer: a

Next-token prediction needs tokens. The tokenizer also shortens the sequence enormously compared with raw pixels.

Check yourself

After a text prefix, a model gives two image tokens probabilities 1 and 0.5. What is their summed negative log-likelihood?

Show the answer

Answer: 0.6931

−log 1 − log 0.5 = 0 + 0.693 = 0.693. Each token's surprise adds up over the sequence.

Check yourself

DALL·E generated many candidates per prompt. How were the shown images chosen?

Show the answer

Answer: a

Reranking with a separate image–text model raised apparent quality, and also hides how often single samples fail.

Check yourself

In unCLIP, what does the prior generate?

Show the answer

Answer: a

The prior works in embedding space; the diffusion decoder turns the image embedding into pixels.

Check yourself

How does unCLIP make several variations of one image?

Show the answer

Answer: a

The embedding fixes the meaning and style; decoder noise varies the details.

Check yourself

A new generator improves FID and CLIPScore. Which question do those two numbers still leave open?

Show the answer

Answer: b

FID compares distributions; CLIPScore inherits CLIP's blind spots. Targeted prompts and human review check the rest.

08

Recap

Recap
  • A discrete tokenizer turns images into codebook grids; DALL·E predicts them after the text, one token at a time.
  • Training minimizes the image tokens' negative log-likelihood; sampling settings shape the output as for words.
  • unCLIP's prior makes a CLIP image embedding from the caption; a diffusion decoder draws it.
  • Judge generators on fidelity, diversity, prompt adherence and failure rate, with humans in the loop.

Concepts in this lesson