liveBook · Manning

Decoder-only transformer

As explained in Deep Learning with PyTorch, Second Edition

A transformer variant that uses only the decoding stack to generate outputs autoregressively from prior context.

A decoder-only transformer generates a sequence of tokens from an input sequence using only the transformer’s decoding stack. Unlike the original encoder–decoder design, it does not use a separate encoder to provide representations. GPT-style models, including GPT, Claude, and Llama, use this architecture for coherent text generation. section 9.12 section 9.6

A decoder block combines several mechanisms:

  • Multiheaded attention uses parallel query, key, and value projections so different heads can capture different relationships in the sequence. section 9.6.1
  • Residual connections let gradients flow around layers, while layer normalization stabilizes the block. section 9.6.1
  • Feed-forward layers extend the attention block before repeated decoder blocks transform the sequence representation. section 9.6.1

A small GPT-style model

A PyTorch implementation embeds characters and positions, passes their sum through repeated TransformerBlock instances, and projects the result to vocabulary logits. Training the model on names can produce samples such as “journe,” “huxson,” and “bailanie.”

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Builds a GPT-style decoder in PyTorch with embeddings, repeated blocks, attention projections, normalization, and output projection.
Examples
Demonstrates character-level name generation using a compact PyTorch model and displays generated sample names.
What it assumes
Assumes familiarity with attention MLP blocks and token or character representations, while developing the complete decoder incrementally.
What you can do after
Build it from scratch in PyTorch and generate short text samples with a GPT-style decoder.
How it connects
Connects transformers with attention, residual connections, layer normalization, tokenizers, and encoder–decoder architectures.

Also explained in

Compare how 8 books explain decoder-only transformer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.