liveBook · Manning

Autoregressive model

As explained in Build a Text-to-Image Generator (from Scratch)

A process that produces an ordered output sequence one element at a time, conditioning each element on previously generated elements and available conditioning information.

An autoregressive model generates an ordered sequence one token at a time. Each prediction depends on the tokens already generated and, in an encoder–decoder transformer, information supplied by the encoder. The decoder adds its prediction to the sequence, then uses the longer sequence for the next step. Text generation stops when it predicts an end token. section 2.1.2 section 4.1.2

The same loop can produce different kinds of output:

  • For German-to-English translation, tokenize and encode the German sentence. Start the English decoder with BOS, predict its next token, append that token, and repeat until EOS. A subsequent mask is passed to the decoder during generation. section 2.4.2
  • For image captioning, encode the image into embeddings. The decoder uses those embeddings and the caption so far, starting with <start> and stopping at <end>. An image of a skier can yield “a woman skis” through successive token predictions. section 4.1.2
  • For text-to-image generation, encode the prompt and predict a sequence of discrete image tokens. A VQGAN decoder then turns those tokens into image patches and a complete image; the image tokens are not themselves pixels. section 12.1.1 section 12.3.2

To implement the image-token loop, prepare the encoded text and decoder state, then iterate over 256 token positions. At each position, pass the previous token and current state to the decoder, store the predicted token, update the state, and save a copy if you want to inspect the image as it forms. Sampling settings include temperature, top-k, and a factor controlling adherence to the prompt. section 12.3.2

Listing 12.7 Predicting image tokens iteratively
from copy import deepcopy

imgtokens=[]    #1
for i in range(256):    #2
    torch.cuda.empty_cache()                
    with torch.amp.autocast("cuda",dtype=getattr(torch, dtype)):
        image_tokens[:, i + 1], \    #3
        attention_state = decoder.sample_tokens(
            settings=settings,
            attention_mask=attention_mask,
            encoder_state=encoder_state,
            attention_state=attention_state,
            prev_tokens=image_tokens[:, [i]],
            token_index=token_indices[[i]])
        imgtokens.append(deepcopy(image_tokens))

Follow the 256-iteration decoder loop: each call supplies the previous image token and attention state, receives an updated state, and saves the intermediate token sequence. section 12.3.2

Saved token sequences can be decoded into a grid or animation. After 32 tokens, only the top eighth of the image is present; at 128, half is recognizable; at 256, the result is complete. This makes the progression inspectable, unlike a GAN that generates an image in one pass. section 12.4.2

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Traces start tokens, iterative prediction, stopping, and a 256-step decoder loop, including state updates and sampling settings.
Examples
Uses German-to-English translation, a skier caption, and intermediate images from a text-to-image prompt.
What it assumes
Introduces stepwise decoding and start/end tokens while using tokenizers, tensors, encoders, and decoders in the implementations.
What you can do after
Apply it via a transformer decoder to generate output token by token and inspect saved stages of image generation.
How it connects
Connects encoder–decoder transformers, cross-attention, sequence-to-sequence generation, image captioning, and vector-quantized image reconstruction.

Also explained in

Compare how 5 books explain autoregressive model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.