Skip to main content

The Transformer in a GPT: architecture and computation

From stacked blocks to logits

Lesson objective. Trace representations to logits, explain what a Transformer block repeats, and distinguish network computation from generation of the next token.

Lesson skeleton
  • Initial hidden states → Model forward pass · Trace how the blocks transform the states

See the path

Review this lesson

A GPT is a language model built around a decoder-only Transformer. The Transformer does not refer to a processing step placed between other components: it is a neural-network architecture. In a decoder-only GPT, its core is a stack of Transformer blocks that transform token representations. From these representations, the network computes logits — one score for every token in the vocabulary.

This page first shows what that architecture looks like, then follows a forward pass through its blocks to the logits. It stops at the network output: converting logits into probabilities and selecting the next token belong to decoding, not to the Transformer’s internal computation.

TipThe Transformer in one minute

A decoder-only GPT is a Transformer network made of repeated blocks. Within each block, self-attention mixes information across permitted positions, the MLP transforms each position, residual connections provide a direct path, and normalization helps control activation scale. The stack produces final representations and then logits; choosing the next token belongs to decoding.

1. Represented input 2. Transformer block 3. Final state 4. Logits

The diagram below offers two levels of view: the complete GPT, to locate the data flow, and an enlarged block, to understand the repeated operations. We will then follow this flow from the network input to the logits, without retelling tokenization or the generation loop.

The Transformer: the network at the heart of a GPT

At the top, the complete network: token IDs, embeddings and positional information, a stack of N Transformer blocks, final normalization, vocabulary projection and logits. At the bottom, one pre-LN block: normalization, causal multi-head self-attention, a residual connection, then normalization, a position-wise MLP and a second residual connection.
Figure 1: Architecture of a decoder-only GPT: input representations pass through a stack of Transformer blocks, then a final normalization and output head produce logits. The lower panel expands one typical block.

The stack of blocks is the Transformer in the GPT shown here. The expanded view shows a pre-LN arrangement: each sublayer is preceded by its own normalization. This is a common layout; sublayer order and normalization variants differ across models.

In a decoder-only GPT, blocks are stacked: the output of one becomes the input to the next. Each block transforms the sequence of representations; it does not choose the next token itself. After the final block, a final normalization and a vocabulary projection produce the logits.

After following the forward pass, you can compare this GPT with the 2017 diagram.

This diagram represents the decoder-only family used by autoregressive GPT models. Transformers are not limited to this architecture: the Transformer architecture comparison shows what changes in encoder-only and encoder-decoder models.

Placing the network in the generation process

Complete pipeline: text, tokenization, embeddings and positional information, repeated Transformer blocks, logits, softmax, next-token selection, then addition to the context.

Simplified flow of an autoregressive GPT, from tokens to next-token selection.

This second diagram answers a different question: it places the network in the complete process. The stack of blocks in the middle is the Transformer shown above; upstream operations prepare its input, while softmax, token selection, and feedback happen downstream. The decoding loop takes place after the network output.

1. From input IDs to the first block

The previous lessons established how text becomes a sequence of IDs, then how those IDs become vectors to which the architecture adds positional information. The first block therefore receives a sequence of representations, not raw text. In the project GPT with additive absolute positional embeddings, token and position embeddings are added before the first block. This is not a universal rule: with RoPE, positional information acts on queries and keys after projection, inside attention. The details remain in the lessons on embeddings, positional information, and contextualized representations.

token IDs → initial representations → first-block input

The prefix “It is a good situation, scribe?” then passes through the block stack before the model produces the next-token logits. The continuation is built through the generation loop; it is not written in advance in the prompt.

2. Transformer blocks

The stack consists of blocks of the same general type, repeated in depth. Each block transforms the sequence of representations while preserving its length and width: it combines two complementary sublayers.

  • Multi-head self-attention: each position aggregates information from positions permitted by the mask. In GPT, causal masking prevents access to future tokens. The projections that feed this computation are covered in Q/K/V, followed by score computation and aggregation in scaled dot-product attention.
  • MLP (feed-forward network): transforms each position independently, using parameters shared across positions. It provides a local nonlinear transformation without directly mixing positions.

Each sublayer is integrated into the residual stream: its correction is added to the flowing representation, and normalization accompanies the transformations. The exact order varies across architectures (pre-LN, post-LN, RMSNorm, and others). This lesson introduces their role; the deep dive on contextualized representations explains the sublayers, residual connections, and normalization in detail. The block walkthrough, by contrast, focuses on the Python implementation.

Parallel training, sequential generation

During training, the tokens in a sequence are known: the network can compute representations for multiple positions in parallel, while the causal mask prevents each position from using future tokens. This removes the position-by-position recurrent dependency found in RNNs.

During generation, the next token depends on tokens already produced, so decoding steps remain autoregressive. The key-value cache (KV cache) avoids recomputing some past keys and values, but does not remove this sequential dependency. The generation lesson distinguishes initial prompt processing (prefill) from incremental production (decode).

  • Training: t₁ t₂ t₃ t₄ → one masked pass; positions are computed in parallel.
  • Generation: t₁ → t₂ → t₃ → … each step depends on the previous token.

Depth and width: two architectural dimensions

  • Depth: the number of Transformer blocks stacked in the network.
  • Width: the dimension (d_{}) of the representations flowing through the residual stream.

These two dimensions describe the overall structure but are not sufficient to predict performance. Internal dimensions (d_{}) and (d_{}), tensor shapes, and GPT-2 reference values are gathered in the Tensors page; the GPT configuration walkthrough shows how they are used in the project’s model.

Three levels that should not be conflated

  1. The attention sublayer exchanges information across permitted positions.
  2. The Transformer block combines attention with an MLP, residual connections, and normalization.
  3. The complete GPT assembles input representations, a stack of blocks, then a final normalization and output head.

Attention is part of a block; the block is the repeated unit; the Transformer alone is not the complete GPT.

3. From representations to logits

The output of the final block is the network’s final state. The final normalization and the output head then produce the logits: one unnormalized score for every vocabulary token. The output head is a simple linear projection: each d_model-dimensional representation becomes a vector of vocab_size scores (50,257 for GPT-2).

final block output → final normalization → vocabulary projection → logits

Downstream, outside the network: logits → softmax → probabilities → chosen token.

For our example prefix with GPT-2’s dimensions: each position leaves the final block as a vector of 768 numbers, which becomes 50,257 scores. To extend an unpadded prefix, generation uses the logits at its last position; the causal mask guarantees that they depend only on the tokens already known. The logits lesson explains what these scores represent; the output probabilities lesson shows how softmax converts them into a distribution. The network stops at these scores: choosing a token, appending it to the context, and running the model again belong to autoregressive generation.

Language: Français · English

Practice

The questions and exercises for this lesson are collected on its dedicated practice page.

Further exploration

Supplementary resources Explore the optional interactive workshop and further-reading resources for this lesson.

Optional interactive exploration

The GPT-2 exploration workshop lets you observe these mechanisms on a real model: run controlled experiments and examine the limits of the visualization.

Further reading

For visualizations and implementation resources, see the Transformer resources in Deepen. The guided reading of the original 2017 diagram is in A history of Transformers. For a complementary first overview, see Understanding Transformer: The Architecture Behind Modern AI.

For the original architecture diagram

The guided reading of the 2017 encoder–decoder Transformer has moved to A history of Transformers, alongside the research timeline and context.