Skip to content

23. Autoregressive Generation

Autoregressive Image Generation

Diffusion models have dominated image generation since 2020 β€” but there is a radically different approach that has gained traction: treating images as sequences of discrete tokens and generating them the same way language models generate text.

This is the approach behind native image generation in Gemini, GPT-4o, and models like Chameleon (Meta) and LlamaGen.


The Problem: How to Tokenize an Image?

Text is naturally discrete (words, subwords). Images are continuous β€” pixels in \([0,255]^3\). To use autoregressive generation, we need a visual vocabulary.

The solution: VQ-GAN (Vector Quantization GAN)1 learns a codebook of \(K\) vectors. The encoder maps any image patch to the nearest vector in the codebook β€” converting the image into a grid of integer indices.


Autoregressive Token Generation

With a trained codebook, we can represent any image as a sequence of \(N\) integer indices. We then generate this sequence exactly like an LLM generates text:

\[ p(t_1, t_2, \ldots, t_N) = \prod_{i=1}^{N} p(t_i \mid t_1, \ldots, t_{i-1}, \text{prompt}) \]

Each token is generated one at a time, conditioned on all previous ones and the text prompt.


MaskGIT: Parallel Generation via Masking

Purely autoregressive generation is slow: 1024 tokens = 1024 model passes. MaskGIT2 accelerates this with iterative parallel generation:

  1. Start with all tokens masked [MASK]
  2. At each iteration, predict all tokens simultaneously (bidirectional!)
  3. "Reveal" only the tokens with highest confidence
  4. Repeat with fewer masked tokens

In just 8–12 iterations, it generates 1024 tokens β€” versus 1024 iterations for pure AR.


Any-to-Any: Gemini, GPT-4o, and Chameleon

The final step is removing the distinction between text and image tokens. Any-to-any models treat everything as a token sequence:

[TEXT: "a photo of"] [IMG_TOK_3742] [IMG_TOK_891] ... [IMG_TOK_5531] [TEXT: "cat"]

The standard Transformer model processes this mixed sequence naturally.

How Each Model Implements This

Model Visual tokenizer Generation Training
Chameleon (Meta) VQ-VAE (8192 codes) Pure autoregressive Text + image together from the start
Gemini 2.0 (Google) Proprietary tokenizer AR + diffusion decoder Native multimodal
GPT-4o (OpenAI) Discrete visual tokens AR + diffusion decoder Native multimodal
LlamaGen VQGAN (16384 codes) AR with LLaMA Initializes from pre-trained LLaMA

AR vs. Diffusion: When to Use Each?

Autoregressive Generation
  • Unifies text and image in the same architecture
  • Best for multimodal any-to-any
  • Leverages the entire LLM infrastructure
  • Scales well with more data
  • Slow: 1 token at a time
Diffusion (DDPM / Flow Matching)
  • Best standalone image quality
  • Coherent global generation
  • More control (guidance, cfg scale)
  • Faster per image than AR
  • Does not natively unify with text

The current trend: hybrids β€” an autoregressive LLM backbone for understanding and reasoning, with a diffusion decoder to render the final image at high quality. This is exactly what GPT-4o does.


Implementation: VQ-GAN + Autoregressive Transformer

import torch
import torch.nn as nn

# 1. Vector quantizer
class VectorQuantizer(nn.Module):
    def __init__(self, n_codes, d_code):
        super().__init__()
        self.codebook = nn.Embedding(n_codes, d_code)

    def forward(self, z):
        # z: (B, H, W, d_code) β€” encoder latents
        flat = z.view(-1, z.shape[-1])
        # Distances to codebook
        dists = torch.cdist(flat, self.codebook.weight)
        indices = dists.argmin(dim=-1)            # index of nearest code
        quantized = self.codebook(indices).view_as(z)
        # Straight-through estimator for backprop
        quantized_st = z + (quantized - z).detach()
        return quantized_st, indices.view(z.shape[:3])

# 2. Autoregressive generation with GPT-like model
class ImageGPT(nn.Module):
    def __init__(self, n_codes, seq_len, d_model, n_heads, n_layers):
        super().__init__()
        self.tok_emb = nn.Embedding(n_codes + 1, d_model)  # +1 for BOS token
        self.pos_emb = nn.Embedding(seq_len + 1, d_model)
        encoder_layer = nn.TransformerEncoderLayer(d_model, n_heads, d_model*4, batch_first=True)
        self.transformer = nn.TransformerEncoder(encoder_layer, n_layers)
        self.head = nn.Linear(d_model, n_codes)

    def forward(self, tokens):
        B, T = tokens.shape
        pos = torch.arange(T, device=tokens.device).unsqueeze(0)
        x = self.tok_emb(tokens) + self.pos_emb(pos)
        # Causal mask
        mask = torch.triu(torch.ones(T, T, device=tokens.device), diagonal=1).bool()
        x = self.transformer(x, mask=mask)
        return self.head(x)  # logits over n_codes

    @torch.no_grad()
    def generate(self, prompt_tokens, n_new, temperature=1.0, top_k=2048):
        tokens = prompt_tokens.clone()
        for _ in range(n_new):
            logits = self(tokens)[:, -1, :] / temperature
            if top_k: logits[logits < logits.topk(top_k)[0][:,-1:]] = -float('inf')
            probs = logits.softmax(-1)
            next_tok = torch.multinomial(probs, 1)
            tokens = torch.cat([tokens, next_tok], dim=1)
        return tokens


The 2026 picture

Autoregressive image generation looked like a dead end in 2022 β€” VQ-GAN was good, and diffusion was better and faster. It came back for a reason that has nothing to do with image quality: it is the only formulation in which a picture and a sentence are the same kind of object.

Three lines of work matter now, and they disagree with each other in an instructive way.

  • VAR β€” predict the next scale


    Raster order is an arbitrary and bad choice: pixel 500 is not "after" pixel 499 in any meaningful sense. VAR6 replaces it with coarse-to-fine β€” predict a \(1{\times}1\) token map, then \(2{\times}2\), then \(4{\times}4\), up to full resolution.

    That is a causal order the data actually has, it needs \(O(\log N)\) steps instead of \(O(N)\), and it was the first autoregressive model to beat a comparable diffusion model on ImageNet.

  • MAR β€” drop the quantizer


    The codebook is where autoregressive image models lose fidelity: everything between codebook entries is unrepresentable. MAR5 removes it and predicts continuous tokens, using a small per-token diffusion head to model \(p(x_i \mid x_{<i})\).

    Autoregressive in structure, diffusion in the per-token distribution. It is a good demonstration that "autoregressive versus diffusion" was never the right axis.

  • Any-to-any β€” one sequence, every modality


    Chameleon4 and its successors interleave text and image tokens in one sequence with one Transformer and one next-token loss. No adapters, no separate image head, no cross-attention.

    This is what the whole approach is for: interleaved reasoning and generation in the same forward pass β€” describing an image, editing it, and explaining the edit, without leaving the model.

The axis is not what people think it is

"Autoregressive versus diffusion" sounds like a choice of model family. It is really two independent choices: what order do you generate in (raster, coarse-to-fine, random-masked, all-at-once) and what distribution do you put on each element (categorical over a codebook, or continuous via a diffusion head). MAR is autoregressive with a diffusion head; MaskGIT is parallel with a categorical one; a diffusion model is all-at-once and continuous. Once you see the two axes, the taxonomy stops being tribal.

The same idea is now running in the other direction: diffusion language models generate text by iterative denoising over a whole sequence instead of left to right, trading the KV cache for parallel decoding.

Where each one is used

Task What to reach for Why
Text-to-image, quality first Diffusion / flow (chapter 21) Best fidelity per unit of compute, and continuous latents lose nothing to a codebook
Interleaved text and images in one model Autoregressive One sequence, one loss, one model β€” nothing else does this cleanly
Image editing driven by a conversation Autoregressive or a hybrid The edit and the reasoning about it live in the same context
Fast sampling on discrete tokens MaskGIT-style parallel decoding \(O(\log N)\) rounds instead of \(O(N)\)
Exact likelihoods, compression Autoregressive It is the only family here with a tractable exact likelihood

Key takeaways

  1. Autoregressive image generation needs a tokenizer: a VQ-VAE/VQ-GAN turns an image into a grid of integers, and the codebook is both the enabling trick and the fidelity ceiling.
  2. Cost is one forward pass per token, so it runs on ~1024 latent tokens, never on pixels. This is the same accounting as chapter 16.
  3. Raster order is arbitrary. MaskGIT generates in parallel rounds; VAR generates coarse-to-fine and needs \(O(\log N)\) steps.
  4. MAR shows the quantizer is optional: continuous tokens with a small diffusion head per token.
  5. The real axes are generation order and per-element distribution, not "autoregressive versus diffusion".
  6. The reason the approach came back is unification: text and images as one sequence, one loss, one model β€” which is what any-to-any systems need.



  1. Esser, P. et al. (2021). Taming Transformers for High-Resolution Image Synthesis (VQ-GAN). β†©

  2. Chang, H. et al. (2022). MaskGIT: Masked Generative Image Transformer. β†©

  3. Sun, P., et al. (2024). Autoregressive Model Beats Diffusion: Llama for Scalable Image Generation. LlamaGen β€” a plain Llama architecture over VQ tokens, with no vision-specific inductive bias. β†©

  4. Team, C. et al. (2024). Chameleon: Mixed-Modal Early-Fusion Foundation Models. β†©

  5. Li, J. et al. (2024). MAR: Autoregressive Image Generation without Vector Quantization. β†©

  6. Tian, K., Jiang, Y., Yuan, Z., Peng, B., & Wang, L. (2024). Visual Autoregressive Modeling: Scalable Image Generation via Next-Scale Prediction β€” NeurIPS (best paper). Coarse-to-fine instead of raster order. β†©