Autoregressive transformers
A causal transformer as a PixelCNN without a blind spot, image tokens from a palette and from VQGAN, temperature, top-k and top-p, and text-to-image with DALL·E and Parti.
Images as text
The topic Autoregressive models left us an exact likelihood and a slow sampler. The chain rule turns an image into a product of one-value conditionals, and a PixelCNN learns them all in one parallel pass. But its masked convolutions see only a local window of the past, with a blind spot inside it, and sampling takes one network pass per value.
Language models solve the same problem for text. A GPT predicts each token from all earlier tokens and is trained on billions of them. Can images be generated the way text is?
The obstacle is length. A colour image read one value at a time is a sequence of
values, while GPT-2 reads at most tokens. Self-attention compares every position with every earlier one, so its cost per layer grows with the square of the length (the topic Latent diffusion hit the same wall in pixel space).
So we change what a token is. There are two answers, and this topic follows both:
- a colour from a small palette, one per pixel of a small image (ImageGPT);
- a code of a learned discrete autoencoder — the VQ-VAE of β-VAE and VQ-VAE, made sharper (VQGAN, DALL·E, Parti).
Once an image is a short sequence of codes, text-to-image generation becomes continuing a caption.
A PixelCNN without a blind spot
A decoder-only transformer is a stack of blocks. Each block has a self-attention layer and then a small network applied to every position separately, with residual connections around both. Self-attention is the mechanism from Latent diffusion, except that queries, keys and values all come from the sequence itself.
The causal mask sets the attention scores of later positions to before the softmax, so their weights are exactly zero. The output at position then depends only on positions . The per-position networks mix nothing across positions, so this holds in every layer.
A sequence of tokens from a vocabulary of codes gets the chain-rule model
where each conditional is a softmax over logits, like PixelCNN's 256-way softmax. An grid of codes is read row by row, so .
Training is one parallel pass with the true tokens as input (teacher forcing) and a cross-entropy loss at every position; the sum is exactly . One detail mirrors mask A of PixelCNN. The input is shifted by one: , with a start token BOS. The output at position has seen BOS and , and predicts . The mask hides the future; the shift hides the target.
There is no blind spot: every layer can attend to every earlier position. The price is cost. Position attends to positions, so one layer compares
pairs. This quadratic count decides how many tokens an image can afford.
Sampling and the KV cache
Sampling is as sequential as in PixelCNN. Draw from , append it, draw , and so on: network passes for tokens.
A naive sampler reruns the whole network on the prefix at every step. Generating token then costs pairs per layer, and the whole sample costs
pairs per layer. The sum follows from the telescoping identity .
Most of that work is repeated. Because of the causal mask, the keys and values of earlier positions never see later tokens, so appending a token does not change them. A KV cache stores them, one key and one value vector per layer and position. Each step then computes attention only for the new query against the stored keys, and the whole sample costs
pairs per layer — the same as one training pass.
The cache cuts the work, not the number of steps. A grid of codes still needs passes, one after another.
Pixels as tokens: ImageGPT
ImageGPT (Chen et al., 2020) applies GPT-2 to images with as little change as possible. The context is the first problem. A image has values, and attending over all of them would cost times more than GPT-2's context.
So ImageGPT shrinks the image twice:
- it downsamples it to (or );
- it replaces the three channel values of each pixel by the nearest of 512 colours, a palette found by k-means on training pixels.
A image becomes tokens over a vocabulary of 512. In the language of β-VAE and VQ-VAE, the palette is the simplest possible tokenizer: a codebook of 512 colours, with the nearest-colour lookup as the quantizer, and no encoder or decoder around it. The largest model, iGPT-L, is a GPT-2 with 48 layers and about 1.4 billion parameters.
The paper's main result is about representations, not samples. A network trained only to predict the next pixel learns features that reach 96.3% accuracy on CIFAR-10 with a linear classifier on top, and 99.0% after fine-tuning.
As a generator it stays small. The palette turns three values into one token, but it never shrinks the grid: every pixel is still a token. Larger images need a tokenizer that also shrinks the grid.
Learned tokens: VQGAN
That tokenizer is the two-stage recipe of the VQ-VAE. Train a discrete autoencoder, freeze it, encode every image into a grid of codes and fit an autoregressive model to the grids. With a downsampling factor , an image becomes an grid, and the transformer never sees a pixel. The tokenizer handles texture and local detail; the transformer handles composition. It is the same division of labour as in Latent diffusion.
The catch: a plain VQ-VAE trained with squared error gets blurry under heavy compression. VQGAN (Esser, Rombach and Ommer, 2021) keeps the quantizer, the codebook and the commitment terms and replaces the squared error by two losses: a perceptual loss on features of a pretrained network, and a patch discriminator that calls small patches of the reconstruction real or fake. If they sound familiar, the autoencoder of latent diffusion inherited them from VQGAN.
The two losses live on very different scales, so the adversarial one gets an adaptive weight. With the decoder's last layer,
and the decoder minimizes , where includes the perceptual term.
The token budget. With and codes, a image becomes a grid: 256 tokens of bits each, bits in all. The raw pixels take bits.
The second stage is a GPT-2-sized transformer over the 256 tokens. A condition simply goes in front as extra tokens: a class label, or the codes of a segmentation map from a second VQGAN. For megapixel images the transformer samples with a sliding window, each token predicted from a neighbourhood of codes.
Temperature, top-k and top-p
At each step the transformer outputs logits , one per code. Sampling straight from their softmax reproduces the model's distribution, tail and all. In practice the distribution is reshaped first, in one of three ways.
Temperature divides the logits before the softmax:
At this is the model itself; sharpens it toward the likeliest code, and flattens it. ( is not the step count of diffusion.)
Top-k keeps the likeliest codes. Top-p, or nucleus sampling, keeps the smallest set of likeliest codes whose probabilities sum to at least . Both renormalize what is left.
A worked example. Take the logits .
| setting | probabilities |
|---|---|
| none | 0.463, 0.281, 0.170, 0.063, 0.023 |
| 0.555, 0.272, 0.133, 0.032, 0.008 | |
| top-k, | 0.506, 0.307, 0.186, 0, 0 |
| top-p, | 0.622, 0.378, 0, 0, 0 |
Top-p keeps two codes here: the first alone holds , the first two hold .
All three cut the low-probability tail. A grid of codes gives a rare bad code chances to appear, so cutting the tail makes samples cleaner and more typical, and less diverse. It is the same trade as the guidance weight in Latent diffusion: fidelity for diversity.
Text to image: DALL·E and Parti
DALL·E (Ramesh et al., 2021) is the recipe at scale. A discrete VAE turns a image into a grid over codes. The caption becomes at most 256 text tokens. Text tokens and image tokens are joined into one stream, and one decoder-only transformer with 12 billion parameters, using sparse attention, learns that stream on 250 million image–caption pairs.
Generation is continuing the caption: feed the text tokens, sample the image tokens one by one, decode the grid. Then reranking: DALL·E draws 512 candidates, and CLIP, a model trained to score how well an image matches a caption, keeps the best. On MS-COCO captions, which DALL·E was never trained on, people preferred its samples over those of DF-GAN, a GAN trained on MS-COCO, 90.0% of the time for realism and 93.3% for matching the caption.
Parti (Yu et al., 2022) treats text-to-image as translation. An encoder reads the caption; a decoder generates tokens of a ViT-VQGAN, a VQGAN whose encoder and decoder are vision transformers, with codes. A small super-resolution network takes the decoded image to . From 350 million to 20 billion parameters, image quality and text alignment keep improving.
Parti borrows classifier-free guidance from Latent diffusion. It works on logits just as it works on noise predictions. The model is trained with the caption sometimes replaced by an empty one , and at every step it combines
before sampling from the softmax of . It also reranks, keeping the best of 16 samples per prompt.
Where it breaks and what we have
Two weak spots are built into the recipe.
The tokenizer caps quality. Every sample is a decoder output. In VQGAN with , one token stands for a patch: bits of pixels described by 10 bits of code. Detail the codebook cannot express — small text, faces in a crowd — is missing from every sample, however good the transformer.
A drawn token is never revised. An early mistake in the top-left corner shapes everything after it. A diffusion model refines the whole latent over 20–50 steps and can correct itself; an autoregressive model takes passes for a grid and cannot.
Otherwise the families have converged: a separately trained autoencoder, text through attention, classifier-free guidance. Autoregressive transformers add an exact likelihood over tokens and all the tooling of language models. (The extension Audio generation: speech and music takes the same recipe to sound.)
So the course now has every family: VAEs, GANs, flows, energy-based models, diffusion and autoregressive models. Which one is good? Likelihood cannot settle it. A GAN has none. A token likelihood depends on the tokenizer, so two codebooks give numbers that do not compare. And sampling knobs change the samples without touching the model: on class-conditional ImageNet, Esser et al. cut a standard quality score, FID (two topics from now; lower is better), from 29.2 to 16.0 just by lowering top-k from 973 to 250. The samples became more typical, not the model better.
We need a score computed from samples alone. The next topic, Inception Score, asks a pretrained image classifier to judge them.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.