Foundations to Frontier
Tests
H. Generative Models › H2 Autoregressive Models

Image GPT, VQ-based autoregressive transformers

A GPT-style causal transformer can model images just as it models text: turn the image into a sequence of discrete tokens, then predict each token from the ones before it. Image GPT (Chen et al., 2020) used raw pixels, with colours reduced to a 512-entry palette. It generated coherent images and its features made strong linear probes, but attention over every pixel limited it to 64×64. VQ-based models first compress the image with a learned codebook (VQ-VAE, VQGAN; a 256×256 image becomes 16×16 = 256 tokens) and then fit a transformer prior over the codes. DALL·E, Parti, LlamaGen, VAR and token-based multimodal models are built this way. This page covers tokenisation, the two-stage recipe, sampling, and the trade-off between sequence length, codebook size and reconstruction quality. In the exercises you cut images into patches, vector-quantise them and count code bits. You then build four tokenisers for 8×8 grey digits and fit the same small causal transformer to each. Pixels with an 8-level palette needed 64 tokens, 92.0 bits per image and had reconstruction MSE 0.0006. 2×2 VQ with 16 codes needed 16 tokens and 22.4 bits but had MSE 0.0035. 4×4 VQ needed only 4 tokens and 10.0 bits, at MSE 0.0060. Shorter, coarser codes were cheaper to model but lost detail.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Writing an image as a sentence

A language model writes text one token at a time. To make it write images, you first need an alphabet for pictures. Image GPT used one token per pixel colour; modern systems first learn a codebook of small image pieces (like a set of jigsaw tiles), so a picture becomes a short sentence of tile numbers. A transformer then learns to write those sentences, and a decoder turns the tiles back into pixels. The demo shows how many tokens an image needs with and without such compression, and what that does to the cost of attention.

Generative models lab
A 256×256 image as raw pixels is 65,536 tokens; with a VQ encoder that downsamples by 16, it is 256 tokens, so full attention is 65,536 times cheaper. Image GPT worked at 32×32 to 64×64 pixels; VQGAN with f = 16 makes a 256×256 image 256 tokens.
Why engineers care

Tokenised imagery lets one transformer handle text, maps and sensor images together, the basis of multimodal assistants that can read and produce satellite or inspection images.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Image GPT predicts…
  2. 2. A VQ codebook turns an image into…
  3. 3. Fewer tokens per image make attention…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Image GPT (Chen et al., 2020): GPT-2 architecture on pixel sequences at 32×32 to 64×64; colours clustered by k-means into a 9-bit (512-entry) palette, one token per pixel; trained by next-pixel prediction (and BERT-style masking for comparison). Linear probes on its features reached 96.3% on CIFAR-10, showing generative pretraining learns useful representations, but compute limited resolution.
  • Two-stage recipe: (1) a tokenizer, an autoencoder with a vector-quantised bottleneck (VQ-VAE, van den Oord et al., 2017; VQGAN, Esser et al., 2021, with perceptual and adversarial losses), maps an image to a grid of code indices (downsampling f = 8 or 16, codebooks of 1024 to 16384); (2) an autoregressive transformer models the code sequence; samples are decoded back to pixels.
  • Systems: VQ-VAE-2 (2019, hierarchical codes with PixelCNN priors); DALL·E (2021, 32×32 dVAE tokens after 256 text tokens, 12B transformer); Parti (2022, ViT-VQGAN, up to 20B); LlamaGen (2024, plain Llama on VQGAN codes, class- and text-conditional); VAR (2024, predicts coarse-to-fine scales instead of raster order); Chameleon and Emu3 (2024, one model for text and image tokens).
  • Sampling: temperature, top-k / top-p, and classifier-free guidance (mixing conditional and unconditional logits) control quality vs diversity.
  • Alternatives: masked token models (MaskGIT, 2022) generate many tokens in parallel; continuous-token autoregression with a diffusion head (MAR, 2024) avoids quantisation; diffusion models remain the main competitor.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Image GPT reduced colours with…
  2. 2. VQGAN adds to VQ-VAE…
  3. 3. VAR predicts tokens…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Patches, codebooks and code length

Exercise 1.

  • Cut images into p×p patches and reassemble them.
  • Vector-quantise patches against a codebook.
  • Count the code bits of a token grid under a uniform prior.

Practice.

  • Use a pretrained tokenizer (VQGAN, the LlamaGen or Open-MAGVIT tokenizers) and train only the prior, unless the domain (for example SAR or multispectral) needs its own.
  • Watch codebook usage: dead codes waste capacity; use k-means initialisation, codebook reset, low-dimensional or ℓ2-normalised codes, or lookup-free / finite scalar quantisation (LFQ, FSQ).
  • Report reconstruction quality (rFID, PSNR) of the tokenizer separately from generation quality (FID) of the prior.
  • Raster order is standard; add 2-D positional embeddings.
  • Use classifier-free guidance and top-k sampling for conditional generation.
import torch
# Stage 1: tokenise with a pretrained VQ model (e.g. from the LlamaGen or taming-transformers repos)
codes = vq_model.encode_to_indices(images)          # (B, 16, 16) integers in [0, 16384)
seq = codes.flatten(1)                              # raster order, 256 tokens
# Stage 2: next-token prediction with any decoder-only transformer
logits = gpt(torch.cat([class_token[:, None], seq[:, :-1]], 1))
loss = torch.nn.functional.cross_entropy(logits.transpose(1, 2), seq)
🐍 Exercise 1: patches, vector quantisation and code length
Tokenise images into patches, vector-quantise them against a codebook and compute the uniform-prior code length.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Dead codes are fixed by…
  2. 2. Report tokenizer quality with…
  3. 3. FSQ and LFQ are alternatives to…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Two-stage likelihood. With a deterministic encoder z = E(x) and decoder x̂ = D(z), the prior gives the exact probability of the code sequence, p(z) = Π_t p(z_t | z_before t), so −log₂ p(z) is the code length of the lossy representation; image quality is bounded by the tokenizer’s distortion ‖x − D(E(x))‖. This is rate–distortion: fewer or coarser tokens lower the rate (bits) and the cost of the prior, but raise the distortion.

Cost. Attention costs O(n²) for n tokens: 65536 pixels of a 256×256 image vs 256 VQ tokens with f = 16 is a factor of 65536 in attention scores, which is why Image GPT stopped at 64×64.

Vector quantisation. VQ-VAE trains with a straight-through gradient through argmin, a codebook loss ‖sg[E(x)] − e‖² and a commitment loss β‖E(x) − sg[e]‖²; codebook vectors can also be updated as exponential moving averages of their assigned encodings (k-means online).

model year tokens for a 256² image prior
Image GPT 2020 65536 (palette pixels; used only up to 64²) GPT-2
VQ-VAE-2 2019 32² + 64² hierarchical PixelCNN
DALL·E 2021 32² = 1024 12B transformer
VQGAN + transformer 2021 16² = 256 GPT-2-like
LlamaGen 2024 16² = 256 Llama
📝 Quick check (Level 3)
0/3 answered
  1. 1. The prior gives the exact likelihood of…
  2. 2. VQ-VAE's gradient through argmin uses…
  3. 3. Coarser codes lower the rate but raise…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How do pixel tokens and VQ tokens of different sizes trade sequence length, model bits and reconstruction quality?

History.

  • M. Chen et al., “Generative pretraining from pixels” (Image GPT, ICML 2020).
  • A. van den Oord, O. Vinyals and K. Kavukcuoglu, “Neural discrete representation learning” (VQ-VAE, 2017); A. Razavi et al., VQ-VAE-2 (2019).
  • P. Esser, R. Rombach and B. Ommer, “Taming transformers for high-resolution image synthesis” (VQGAN, CVPR 2021); A. Ramesh et al., “Zero-shot text-to-image generation” (DALL·E, 2021).
  • P. Sun et al., “Autoregressive model beats diffusion: Llama for scalable image generation” (LlamaGen, 2024); K. Tian et al., “Visual autoregressive modeling” (VAR, NeurIPS 2024).

Exercise 2. 2000 training and 500 test grey-level 8×8 digits (blurred seven-segment glyphs with noise of std 0.03).

  • Tokenisers: an 8-level per-pixel palette by k-means (Image GPT style, 64 tokens); k-means codebooks of 2×2 patches with 16 or 64 codes (16 tokens); 4×4 patches with 64 codes (4 tokens). k-means stands in for a learned VQ encoder.
  • Prior: the same one-block causal transformer (d = 32, single head, ReLU MLP, start token, learned positions) for every tokeniser; 200 Adam steps, batch 16; backpropagation by hand, checked by finite differences.
  • Scores: reconstruction MSE of the decoded codes; test code length in bits per image under the transformer and under independent token frequencies.
tokeniser tokens attention pairs recon MSE independent tokens (bits) transformer prior (bits)
pixels, 8-level palette 64 2080 0.0006 174.4 92.0
VQ 2×2, K 16 16 136 0.0035 55.4 22.4
VQ 2×2, K 64 16 136 0.0014 85.4 40.4
VQ 4×4, K 64 4 10 0.0060 21.2 10.0
  • The transformer learned the structure: every prior needed under 60% of the bits of independent tokens (from 40% for VQ 2×2 K 16 to 53% for pixels).
  • Rate–distortion: each step to fewer or coarser tokens cut the bits to model (92.0 → 22.4 → 10.0) and the attention cost (2080 → 136 → 10 pairs), and raised the reconstruction error (0.0006 → 0.0035 → 0.0060).
  • Codebook size at fixed length: 64 instead of 16 codes for 2×2 patches cut the error by 60% at the price of 18 more bits.
  • Seeds 1 and 2: pixels 77.5 and 80.9 bits; VQ 2×2 K 16 21.4 and 21.7; VQ 4×4 K 64 10.5 and 10.2; reconstruction errors within 0.0005 of seed 0.
  • Caveat: tiny images; k-means instead of a learned encoder with a perceptual decoder; the pixel prior was the least trained relative to its task (64 positions with the same 200 steps). Bits of lossy codes are not comparable with lossless bits/dim.
🐍 Exercise 2 (research): how do pixel tokens and VQ tokens of different sizes trade sequence length, model bits and reconstruction quality?
Tokenise grey digits as palette pixels or as vector-quantised patches, fit the same causal transformer prior to each, and compare tokens, bits per image and reconstruction error.

Image GPT and VQ transformers in AI, robotics and aerospace

  • Earth observation: tokenizers trained on multispectral or SAR imagery let transformers generate, inpaint and compress scenes and share one model with text.
  • World models for robots: video tokenizers plus autoregressive transformers predict future camera frames for planning.
  • Compression: learned codebooks with an entropy-coded AR prior form neural image codecs for low-bandwidth downlinks.

References

  1. M. Chen et al. (2020). “Generative pretraining from pixels.” ICML.
  2. A. van den Oord, O. Vinyals and K. Kavukcuoglu (2017). “Neural discrete representation learning.” NeurIPS.
  3. P. Esser, R. Rombach and B. Ommer (2021). “Taming transformers for high-resolution image synthesis.” CVPR.
  4. A. Ramesh et al. (2021). “Zero-shot text-to-image generation.” ICML.
  5. P. Sun et al. (2024). “Autoregressive model beats diffusion: Llama for scalable image generation.” arXiv:2406.06525.
  6. K. Tian, Y. Jiang, Z. Yuan, B. Peng and L. Wang (2024). “Visual autoregressive modeling: scalable image generation via next-scale prediction.” NeurIPS.