Topic 4 of 5 · 5 min read
Inside one decoder transformer block
Goal: Label the parts of a decoder-only transformer block and explain the role of the causal mask during generation.
A decoder transformer block is attention plus a feed-forward network, wrapped in residual connections and normalization, with a causal mask so each token can only look backwards. A model such as GPT, Claude, Gemini or Llama is that block repeated many times.
Check yourself · 1 of 3
What does the causal mask do in a decoder model?
Why it matters
When you read a model card ("32 layers, 8 key-value heads") or a bug report about long contexts, you need to know what each part does. The mask also explains why generation is one token at a time.
The parts, in order
Given the vectors for each token position, one block does:
- Normalization. Rescales each vector so values stay in a stable range.
- Masked self-attention. Every token mixes in information from earlier tokens (topics 2 and 3).
- Residual connection. The attention output is added to the original input instead of replacing it. Information can pass through untouched, and training deep stacks becomes much easier.
- Normalization again.
- Feed-forward network (FFN). A small two-layer network applied to each position on its own. Attention moves information between tokens; the FFN processes it within a token. Much of a model's parameter count sits here.
- Residual connection again.
In pseudocode:
def block(x):
x = x + attention(norm(x), causal=True)
x = x + feed_forward(norm(x))
return x
Position encoding
Attention alone ignores order: shuffle the keys and the scores stay the same. So models add position information, either to the input vectors or inside attention (rotary embeddings are a common choice). Without it, "dog bites man" and "man bites dog" would look alike.
The causal mask
During training and generation, a decoder must not see the future. Before the softmax, scores for later positions are set to minus infinity, so their weights become exactly 0.
scores = q @ k.T / math.sqrt(d)
scores[j > i] = -inf # position i cannot read position j later than i
weights = softmax(scores)
At generation time this matches reality: when predicting token 50, tokens 51 onward do not exist yet. During training it lets the model learn next-token prediction at every position in one pass.
Before and after
Weak: "A transformer is attention layers, one after another." Better: "Each block alternates mixing across tokens (attention) with processing inside a token (FFN), with residual paths that keep the original signal."
What goes wrong
- Forgetting the mask. A reimplementation without it lets the model peek at the answer in training and fail at generation.
- Dropping position information. Output becomes order-blind.
- Thinking residuals are optional. Removing them makes deep models hard to train.
Examples
The mask as a grid
For four tokens, rows are the current token and columns are the tokens it may read (1 = allowed):
t1 t2 t3 t4
t1 1 0 0 0
t2 1 1 0 0
t3 1 1 1 0
t4 1 1 1 1
The zeros become minus infinity before the softmax, so token 2 can never see token 3.
Reading a model card
A card lists layers: 32, heads: 32, hidden size: 4096. It means 32 stacked blocks, each with 32 attention heads, working on vectors of 4096 numbers. The feed-forward part of each block is usually the larger share of the parameters.
Key points
- A decoder block is masked self-attention plus a feed-forward network, each with normalization and a residual connection.
- Attention mixes information across tokens; the feed-forward network processes each token on its own.
- Residual connections add a layer's output to its input, which keeps information flowing and makes deep stacks trainable.
- Attention ignores order by itself, so position information is added to tell "dog bites man" from "man bites dog".
- The causal mask sets scores for later positions to minus infinity, so each token can only read earlier tokens.
Common mistakes
- Implementing attention without the causal mask, so training leaks the next token and generation fails.
- Leaving out position information and getting a model that treats text as a bag of words.
- Assuming attention does all the work and ignoring the feed-forward network that holds much of the parameters.
- Treating residual connections as an optional detail rather than the path that lets deep stacks train.
Check yourself · 2 of 3
What job does the feed-forward network in a block do?
Check yourself · 3 of 3
Why does a transformer need position encoding?
Practice items, not counted towards your mastery.
Start here
- The Illustrated Transformer (Jay Alammar) (opens in a new tab)ArticleFree
Step-by-step pictures of queries, keys, values, multi-head attention and the encoder-decoder stack.
- Transformer Explainer (Georgia Tech Polo Club) (opens in a new tab)ToolFree
Runs a small GPT-2 in your browser so you can watch attention weights change as you edit the prompt.
- 3Blue1Brown: Attention in transformers, step-by-step (opens in a new tab)VideoFree
A visual video lesson, with a written version, on queries, keys, values, masking and multi-head attention.