GPT-2 › Worked Example: Tracing a Forward Pass
Language AI HandbookAll chapters GPT-2 › Worked Example: Tracing a Forward PassOne section · Sections in this chapter7 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To solidify your understanding of how GPT-2 processes text, let's trace a minimal forward pass step by step with concrete dimensions. We'll use GPT-2 Small as our example, with hidden size , 12 layers, and 12 attention heads.
Suppose the input is the sequence "The cat sat", which tokenizes to 3 tokens with indices, say, [464, 3797, 3332].
Step 1: Token Embedding Lookup. We look up each token index in the token embedding table, which has shape . For 3 tokens, we retrieve a matrix of shape . Each row is the 768-dimensional representation of that token.
Step 2: Position Embedding Lookup. We look up positions 0, 1, 2 in the position embedding table, also of shape . We retrieve another matrix. Adding token embeddings and position embeddings gives the initial hidden state of shape .
Step 3: Pre-Norm Attention in Layer 1. We apply LayerNorm to , creating of shape with mean 0 and variance 1 per position. We then project through the combined projection matrix of shape to get a matrix, which we split into three matrices for , , and .
Step 4: Multi-Head Attention Split. We reshape each of , , and from into by splitting the 768 dimensions across 12 heads of 64 dimensions each. For head , the query matrix is of shape , and similarly for and .
Step 5: Scaled Dot-Product Attention. For each head , we compute attention scores and weighted values:
where:
- has shape : a score matrix where entry measures how much position should attend to position
- : the scaling factor preventing large dot products from saturating the softmax
- The causal mask zeroes out the upper triangle: position 0 only attends to itself, position 1 attends to positions 0 and 1, position 2 attends to all three
The output has shape .
Step 6: Concatenate and Project. We concatenate the 12 head outputs to get a matrix, then project through the output projection of shape , giving a attention output.
Step 7: First Residual Addition. We add the attention output to the original : . Both have shape . The residual connection preserves the original information while the attention output adds what the attention mechanism learned.
Step 8: Pre-Norm Feed-Forward. We apply LayerNorm to , then pass through the feed-forward network. The first linear layer expands from 768 to 3072 dimensions, GELU activation is applied, and the second linear layer compresses back to 768. The result has shape .
Step 9: Second Residual Addition. We add the feed-forward output to : . This completes one transformer block.
Steps 10 through 21: Repeat for layers 2 through 12. The hidden state passes through 11 more identical blocks. Each adds its learned transformations via residual connections, progressively enriching each token's representation with broader context.
Step 22: Final LayerNorm. After all 12 layers, we apply the final layer normalization to the output.
Step 23: Logit Projection. We multiply by the transposed token embedding matrix of shape , creating logits of shape . For next-token prediction, we take the last position's logits (shape ) and sample from this distribution to generate the 4th token.
This entire forward pass computes all three positions simultaneously during training, with the causal mask so each position only uses information from prior positions. The efficiency comes from this parallel computation: rather than running the model three times sequentially to train on positions 1, 2, and 3, we train on all positions in a single forward pass.