Search

Search papers and lessons

Scaling Autoregressive Models for Content-Rich Text-to-Image Generation

Jiahui Yu, Yuanzhong Xu, Jing Yu Koh et al. · 2022

Parti writes an image as 1024 discrete tokens and generates them one at a time from the text prompt, and scaling that model to 20 billion parameters gives state-of-the-art finetuned FID on MS-COCO.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Architectures
  • ReadReady to readNot yet

TL;DR

Parti is a text-to-image model that treats a picture as a sentence. A learned image tokenizer turns each 256 by 256 image into 1024 discrete tokens, and a Transformer reads the text prompt and writes those tokens one at a time, the way a language model writes words. The authors scale this encoder-decoder up to 20 billion parameters. The result is photorealistic images that follow complicated prompts, with a finetuned FID of 3.22 on MS-COCO.

Builds on: Autoregressive Model, Encoder-Decoder Architecture, Transformer, Vector Quantization, Classifier-Free Guidance · Leads to: none in the graph yet

Why it mattered

Parti showed that the plain recipe of language modelling works for images when the image is first turned into tokens. Before it, the strongest text-to-image results came from diffusion models such as GLIDE, DALL-E 2 and Imagen. Parti reached Imagen's zero-shot score on MS-COCO (7.23 against 7.27) with a model that generates by next-token prediction, which is the same training objective used for large language models. That made it evidence that the scaling behaviour seen in text models, where bigger models with more data keep improving, carries over to images. The paper also introduced PartiPrompts, a benchmark of more than 1600 English prompts sorted into 12 categories and 11 challenge types, so that later models could be compared on hard compositional prompts and not only on MS-COCO captions.

The problem

By 2022 there were two ways to make images from text. Diffusion models start from noise and refine it into an image, and they gave the best sample quality. Autoregressive models, such as DALL-E and CogView, predict a sequence of discrete image tokens, which lets them reuse the machinery of language models, but their quality lagged. The earlier autoregressive systems were also small. Their image tokens came from weaker tokenizers, and nobody had shown how the approach behaves when the model is made much larger.

Two things had to be solved. First, the image tokenizer had to keep enough detail that a 1024-token picture still looks sharp, and it had to use its whole codebook, so that the model does not waste its vocabulary on a few codes. Second, the model had to be scaled, which means handling training runs that do not fit on one device. The paper addresses both.

How it works

The pipeline has two stages, a tokenizer and a generator, and a few add-ons. In the order the system runs at inference:

  1. The text prompt is split into subword tokens. The paper caps the prompt at 128 text tokens.
  2. The text tokens enter the encoder, a Transformer that reads the whole prompt at once and produces a representation for each text token.
  3. The decoder, a larger Transformer, produces image token 1 out of 1024. It sees the encoder output and nothing else yet. Its output is a probability for each of the 8192 image codes.
  4. One code is sampled. It is appended to the decoder's input, and the decoder predicts image token 2. This repeats until all 1024 tokens exist. The decoder is causal, so token k can only look at tokens before it.
  5. The 1024 codes form a 32 by 32 grid. The ViT-VQGAN decoder looks up each code's vector in the codebook and turns the grid back into a 256 by 256 picture.
  6. Optionally, a super-resolution network enlarges the picture to 512 by 512 or 1024 by 1024.
  7. The paper samples several candidate images for each prompt, scores each one against the prompt with a CoCa image-text model, and keeps the best one.

Here is a toy version with 4 image tokens and a codebook of 8 codes, so the mechanics fit on a line. Suppose the decoder gives these probabilities, each conditioned on the text and on the tokens already chosen:

  • token 1: code 3 with probability 0.4
  • token 2 given token 1: code 7 with probability 0.5
  • token 3 given the first two: code 7 with probability 0.9
  • token 4 given the first three: code 1 with probability 0.25

Sampling picks codes 3, 7, 7, 1. The grid is then decoded into a tiny picture. The probability the model assigned to this whole sequence is the product of the four step probabilities, 0.4 times 0.5 times 0.9 times 0.25, which is 0.045. Parti does the same thing with 1024 steps and 8192 codes.

The training signal is the same at every step. The model is shown real image tokens and asked to predict each one from the text and the earlier real tokens, and the loss is the cross-entropy between its prediction and the true code.

flowchart LR
  A["Prompt text"] --> B["Text tokens, up to 128"]
  B --> C["Encoder"]
  C --> D["Decoder predicts image token 1 to 1024"]
  D --> E["32 by 32 grid of codes"]
  E --> F["ViT-VQGAN decoder"]
  F --> G["256 by 256 image"]
  G --> H["Optional super-resolution"]
  D -. "several samples per prompt" .-> I["CoCa reranker keeps the best"]

Three further choices matter in practice. Parti uses Classifier-Free Guidance at inference, which makes the picture follow the prompt more closely. The text encoder is pretrained before the generator is trained, so the generator starts from text representations that already carry the meaning of the prompt. Finally, the 20 billion model is too large for one device, so training splits its weights across many accelerators (model parallelism) and repeats that split across data-parallel replicas, as described in the Scaling section.

The math

Definition (the tokenizer). A ViT-VQGAN encoder maps an image x to a grid of vectors. Each vector z is replaced by its nearest entry in a learned codebook of K = 8192 vectors e_1 to e_K. The chosen index is the image token. Writing k for that index, k = argmin over j of the distance between z and e_j. The paper normalises the codes to unit length so the nearest-entry choice is stable, and it uses factorised codes to keep the codebook in use. The result is a sequence of 1024 integers, each between 1 and 8192.

Step 2 in symbols (the generator). Let c be the text condition and x_1 to x_1024 the image tokens. The autoregressive factorisation writes the probability of an image as a product of next-token probabilities:

p(x1,…,x1024∣c)=∏i=11024p(xi∣x1,…,xi−1,c)p(x_1, \ldots, x_{1024} \mid c) = \prod_{i=1}^{1024} p(x_i \mid x_1, \ldots, x_{i-1}, c)

Each factor is one decoder step. The left side says "the probability of this whole token grid, given the prompt". The right side says "multiply the probability of each token, given the prompt and the tokens before it". The toy example above is this formula with four factors.

Step 3 in symbols (training). Training minimises the average negative log-probability of the true tokens:

L=−11024∑i=11024log⁡p(xi∣x<i,c)L = -\frac{1}{1024} \sum_{i=1}^{1024} \log p(x_i \mid x_{\lt i}, c)

Here x<ix_{\lt i} is shorthand for all tokens before position ii. Dividing by 1024 is only a normalisation, so the loss size does not depend on the grid length. This is the softmax Cross-Entropy Loss of language modelling with a vocabulary of 8192 image codes.

Step 6 in symbols (classifier-free guidance). Let G(z) be the model's prediction with the prompt dropped and G(z, c) its prediction with the prompt. Guidance combines them as

I=G(z)+λ(G(z,c)−G(z))I = G(z) + \lambda \left( G(z, c) - G(z) \right)

with weight lambda, a tunable number; the examples below use lambda = 1.2. Worked through on a single number, if G(z) = 0.2 and G(z, c) = 0.6, then 0.2 + 1.2 times (0.6 minus 0.2) = 0.2 + 0.48 = 0.68. The conditional prediction is pushed further from the unconditional one, which sharpens the match to the prompt.

Scaling. The four model sizes in the paper's Table 1 are:

ModelEncoder layersDecoder layersModel dimTotal params
Parti-350M12121024350M
Parti-750M12361024750M
Parti-3B123620483B
Parti (20B)1664409620B

The decoder grows faster than the encoder. The paper reports that its 3B model has 3 times as many decoder layers as encoder layers, and its 20B model has 4 times. Going from 3B to 20B is about a 6.7-fold increase in parameters.

Results

The paper evaluates with FID, the Fréchet Inception Distance, which compares the statistics of generated and real image features, so lower is better (see Fréchet Inception Distance). The main comparison is on MS-COCO, with zero-shot numbers (no training on the benchmark's data) and finetuned numbers:

  • MS-COCO zero-shot: Parti 7.23, Imagen 7.27, DALL-E 2 10.39, GLIDE 12.24, Make-A-Scene 11.84.
  • MS-COCO finetuned: Parti 3.22, against 7.55 for Make-A-Scene, the previous best autoregressive finetuned result.

FID is a useful summary but an imperfect one: it compares feature statistics, so an image set can score well while individual images look worse to people. That is why the paper also runs human side-by-side evaluations on PartiPrompts, where annotators compared the 20B model's images with the 3B model's on how well they match the prompt and how realistic they look. The 20B model was preferred on both.

The paper's limitations come near the end. The most common errors are color bleeding (an unspecified object takes the colour of a described one) and feature blending (two similar objects fuse into one). Text rendering, counting and some spatial relations are also imperfect. The most common errors are color bleeding (an unspecified object takes the colour of a described one) and feature blending (two similar objects fuse into one). Text rendering, counting and some spatial relations are also imperfect. The paper notes that the training data is web-scale and only partly filtered, and it discusses the risks that follow from that.

Reading guide

Read the paper in this order, with the lessons each part needs:

Check yourself

QuestionA Parti image is 256 by 256 pixels and the tokenizer maps it to a 32 by 32 grid. How many image tokens does the decoder generate for one image, and how many decoder steps does that take?Show answer

The grid has 32 times 32 = 1024 cells, and each cell is one code. The decoder is autoregressive, so it generates one token per step, which takes 1024 decoder steps.

QuestionTwo sampled tokens have probabilities 0.5 and 0.2 under the model. What is the probability of the two-token sequence under the autoregressive factorisation?Show answer

The factorisation multiplies the step probabilities, so the answer is 0.5 times 0.2 = 0.1. (This assumes the 0.2 is already conditioned on the first token, as the formula requires.)

QuestionWhy does Parti give the decoder more layers than the encoder, and why does this matter for the text prompt?Show answer

The decoder runs once for every image token, 1024 times per image, so it carries most of the compute for generating the picture. The encoder runs once per prompt and its output is reused at every decoder step through cross-attention. The table shows the decoder growing faster than the encoder, which follows from that split of work.

QuestionUsing the guidance formula with lambda = 1.2, a conditional prediction of 0.9 and an unconditional prediction of 0.5 at one point, what combined value does the formula give?Show answer

0.5 + 1.2 times (0.9 minus 0.5) = 0.5 + 1.2 times 0.4 = 0.5 + 0.48 = 0.98.

Sources

Read the full paper

Jiahui Yu, Yuanzhong Xu, Jing Yu Koh et al., 2022. arXiv:2206.10789