TL;DR
DALL-E generates an image from a text caption by treating both as one sequence of tokens. A discrete autoencoder first turns each 256 by 256 image into a 32 by 32 grid of codes, and a 12-billion-parameter transformer then learns to predict the caption tokens and image codes one at a time. On MS-COCO captions, which it was never trained on, human raters judged its sample the more realistic image 90% of the time against the prior model DF-GAN.
Builds on: Variational Autoencoder, Transformer, Autoregressive Model, Subword Tokenization, Gumbel-Softmax Trick · Leads to: CLIP, which the paper uses to rerank its own samples
Why it mattered
The paper showed that text-to-image generation, a task that had been studied with GANs and small datasets such as MS-COCO and CUB-200, could be handled by the same kind of large autoregressive transformer used for text. The model was trained on 250 million image-text pairs collected from the internet, and the paper reports MS-COCO results without training on that dataset's captions. Its reranking step uses a contrastive model to pick the best of many samples.
The paper also reports effects that were not designed in. It says the model can render text in images, appear to handle some variable binding in prompts, and do limited image-to-image translation from a text instruction. The authors describe these as emerging from scale rather than from a special training procedure.
The problem
Before DALL-E, text-to-image models were usually GANs or energy-based models trained on small datasets such as MS-COCO and CUB-200, often with extra conditioning information beyond the caption. The paper's question is whether a simpler setup, a transformer trained as a language model over both text and images, can work when data and model size are scaled up.
The obstacle is the length of the image. A raw 256 by 256 RGB image has 196,608 values, and a transformer over that many positions is too costly. The paper also notes that likelihood training over raw pixels spends its capacity on short-range detail, such as the exact texture of a pixel, rather than on the structure that makes an image recognisable. The fix is a two-stage design: compress first, then model the compressed tokens together with the text.
How it works
Take a tiny instance: a caption of two text tokens and an image of three image tokens, with a made-up probability for each next token. Follow one training example from start to finish.
-
Compress the image (stage one). A discrete variational autoencoder, the dVAE, maps the 256 by 256 image to a 32 by 32 grid of codes. Each code is one of 8,192 entries in a learned codebook, so the image is now 1,024 tokens. This cuts the context size of the transformer by a factor of 192, and the paper reports no large loss in visual quality, though fine details such as fur texture or small writing can be distorted (see Variational Autoencoder for the continuous version this builds on).
-
Tokenise the caption. The lower-cased caption is split into at most 256 subword tokens with byte-pair encoding (see Subword Tokenization). The paper uses a text vocabulary of 16,384 entries.
-
Concatenate. The text tokens come first, then the 1,024 image tokens, forming one sequence. For the tiny example, the stream is t1, t2, i1, i2, i3.
-
Predict in order. A decoder-only transformer predicts each token from the earlier tokens its attention mask allows (see Autoregressive Model). Text tokens attend only to earlier text, the standard causal rule. Each image token attends to every text token and to a sparse pattern of earlier image tokens (a row, a column or a convolutional neighbourhood), which keeps the 1,024-token image part affordable.
-
Score the example. Here the model gives probabilities 0.5 for t1, 0.4 for t2 given t1, 0.1 for i1 given the text, 0.2 for i2 given the text and i1, and 0.5 for i3 given everything before it. The probability of the whole stream is the product, 0.5 times 0.4 times 0.1 times 0.2 times 0.5 = 0.002. The training loss for this example is minus the natural log of that product, which is about 6.21.
-
Generate. At test time the caption is fed in, and the model samples image tokens one by one. The sampled grid is decoded by the dVAE decoder into pixels. The paper draws many samples and keeps the ones that a separate contrastive model scores as the best match to the caption (see Learning Transferable Visual Models From Natural Language Supervision (CLIP)).
flowchart LR C[caption] --> BPE[subword tokens, up to 256] IM[image 256x256] --> DV[dVAE encoder] DV --> G[32x32 grid of codes] BPE --> S[one token stream] G --> S S --> TR[transformer predicts next token] TR -->|sample image codes| DD[dVAE decoder] DD --> OUT[image]
Worked examples
- Use the probabilities from the walkthrough: 0.5, 0.4, 0.1, 0.2 and 0.5.
- Multiply them in order. 0.5 times 0.4 is 0.2. Then 0.2 times 0.1 is 0.02. Then 0.02 times 0.2 is 0.004. Then 0.004 times 0.5 is 0.002.
- The natural log of 0.002 is ln 2 minus 3 times ln 10, which is 0.693 minus 6.908, or about -6.21.
- The loss for this example is 6.21.
- Interpretation: each small probability adds to the loss. A model that predicts each token at 0.5 would have a loss of 5 times ln 2, about 3.47, so this example is one the model finds less likely than that baseline does.
- A raw 256 by 256 RGB image has 256 times 256 pixels, times 3 colour values, which is 196,608 numbers.
- The dVAE output is a 32 by 32 grid, which is 1,024 tokens.
- The ratio of raw values to tokens is 196,608 divided by 1,024, which is 192.
- The paper states the same factor: the transformer's context is reduced by a factor of 192.
- Each token takes one of 8,192 values, which needs 13 bits, since 2 to the power 13 is 8,192. So the whole image is 1,024 times 13 bits, or 13,312 bits.
- Interpretation: the compressed image is far shorter than the raw one, which is what makes a transformer over image tokens affordable. The dVAE decoder must recover the picture from these codes, and the paper notes that some fine details are lost.
- The paper trains the 12-billion-parameter model for 430,000 updates, with a total batch size of 1,024.
- Each update sees 1,024 caption-image pairs, so the run sees 430,000 times 1,024 pairs.
- That is 440,320,000 pairs, or about 440 million.
- The dataset has 250 million pairs. So the run sees the data about 440 divided by 250, which is 1.76 times over.
- Interpretation: the model is trained for less than two passes over its data, so the scale comes from the amount of data and parameters, not from many repeated epochs.
- Try it: the paper trains on 1,024 GPUs with 16 gigabytes each. If the model stored in 16-bit precision takes about 24 gigabytes, how many GPUs' memory would a single copy need, and why is this a problem? Answer: 24 divided by 16 is 1.5, so a single copy needs more than one GPU's memory, which is why the paper uses parameter sharding across GPUs.
The math
The paper's objective is an evidence lower bound. Let x be the image, y the caption and z the 32 by 32 grid of image codes. The model factorises the joint distribution as p(x, y, z) = p_θ(x | y, z) p_ψ(y, z), where p_θ is the dVAE decoder and p_ψ is the transformer. The bound is
Here q_φ is the dVAE encoder, which gives a distribution over the codes for an image, and D_KL is the Kullback–Leibler Divergence. The paper notes that the bound holds only for β = 1, but that larger values help in practice. The first term says the image must be reconstructable from the codes; the second says the codes must be predictable from the caption.
Step 1 in symbols: the dVAE
In stage one the paper fixes the prior p_ψ to be uniform over the K = 8192 codebook vectors, and trains the encoder and decoder with the ELB. Because the codes are discrete, the paper uses the Gumbel-softmax relaxation (see Gumbel-Softmax Trick) with a temperature τ annealed to 1/16. The KL weight is set to β = 6.6, which the paper reports leads to better codebook use and a smaller reconstruction error at the end.
Step 2 in symbols: the joint token stream
In stage two the dVAE is frozen, and the transformer maximises the ELB over the joint sequence of text tokens and image tokens. The training loss is a weighted sum of two cross-entropies, one for the text tokens and one for the image tokens:
The image term has most of the weight because the paper is primarily interested in image modelling. Each cross-entropy is normalised by the number of tokens of its kind in the batch.
Step 5 in symbols: the chain rule
For the tiny example, the chain rule for an autoregressive model gives the stream probability as a product of conditionals:
Taking the negative natural log of this product gives the loss for the example. Each conditional is one token prediction from the transformer.
Results
The paper's numbers below are copied from its text and figure captions.
- Human evaluation, MS-COCO, against DF-GAN: in a best-of-five vote, the model's sample was chosen as the most realistic 90.0% of the time, and as the image that best matched a shared caption 93.3% of the time (Figure 7).
- FID on MS-COCO: the paper says the model's FID is within 2 points of the best prior approach, even though it was never trained on MS-COCO captions. The paper finds that about 21% of the MS-COCO validation images are in its training data, using its de-duplication procedure, and reports FID both with and without those images, finding no significant change.
- CUB: on CUB the paper reports a gap of nearly 40 points in FID to the leading prior approach, and says the zero-shot approach is less likely to compare well on specialised datasets.
- Reranking: FID and Inception Score on MS-COCO improve as the number of samples for reranking grows, with diminishing returns after 32 samples (Figure 9c). Figure 3 uses the best of 512.
- Scale: the 12-billion-parameter model was trained on 250 million pairs, against 3.3 million for the preliminary models up to 1.2 billion parameters.
The limits are clear in the paper. Human evaluation is run on 1,000 captions, and the comparisons are against prior models on MS-COCO and CUB, not on every benchmark. The compression to 32 by 32 tokens loses high-frequency detail, and the paper says so. The CUB result shows the model does not win on every dataset. Finally, the reranking step uses a separate model, so the sample quality is a property of the system, not of the transformer alone.
Reading guide
The paper's sections, in order, and the lessons each needs:
- 1 Introduction: Autoregressive Model for the idea of predicting a sequence step by step, and Transformer for the architecture that does it.
- 2.1 Stage One: Learning the Visual Codebook: Variational Autoencoder for the encoder and decoder, Gumbel-Softmax Trick for the relaxation of discrete codes, and Kullback–Leibler Divergence for the KL term.
- 2.2 Stage Two: Learning the Prior: Subword Tokenization for the text tokens, Cross-Entropy Loss for the loss, Sparse Attention for the attention masks, and Adam Optimizer for the optimiser.
- 2.3 Data Collection and 2.4 Mixed-Precision Training: Mixed-Precision Training for the 16-bit training and the gradient scaling the paper describes.
- 2.5 Distributed Optimization: the paper's gradient compression is an engineering detail; read it after the method.
- 3 Experiments: the human and FID comparisons, and Learning Transferable Visual Models From Natural Language Supervision (CLIP) for the reranking model used in Figure 6 and the later sections.
Check yourself
QuestionRecall: what does the dVAE turn a 256 by 256 image into, and how many possible values does each token have?Show answer
It turns the image into a 32 by 32 grid of codes, so 1,024 tokens. Each token takes one of 8,192 codebook values.
QuestionCompute: a caption has two text tokens and the image has two image tokens. The conditional probabilities are 0.5, 0.2, 0.4 and 0.25 in order. What is the negative log-likelihood of the stream?Show answer
The product is 0.5 times 0.2 times 0.4 times 0.25, which is 0.01. The natural log of 0.01 is about -4.605, so the negative log-likelihood is about 4.61.
QuestionWhy: the paper compresses images with a dVAE before training the transformer. Why not train the transformer on raw pixels?Show answer
A raw image has 196,608 values, which makes a transformer over pixels too costly. Pixel-level likelihood also spends its capacity on short-range detail, such as fine texture, rather than on the structure that makes an image recognisable. The dVAE reduces the sequence by a factor of 192 while keeping the main features, so the transformer can model the global structure of the caption and the image together.
QuestionApplied: you want to reproduce the paper's reranking step with a model that scores only 4 samples per caption. Using the paper's sample-size result, what should you expect, and what should you change?Show answer
The paper reports that quality improves as the number of samples grows, with diminishing returns after 32. With 4 samples you will get a real but smaller benefit than the paper's best setting. You should raise the sample count, since the gain from reranking grows with it up to about 32, and check the reranking model's score on the captions before judging the result.
Sources
- Ramesh et al. Zero-Shot Text-to-Image Generation. arXiv:2102.12092. https://arxiv.org/abs/2102.12092
- Radford et al. Learning Transferable Visual Models From Natural Language Supervision (CLIP). arXiv:2103.00020. https://arxiv.org/abs/2103.00020