Inference Engineering · Lesson 6 · Inside a Transformer BlockHome · Glossary · Your Lab

Inside a Transformer Block

The complete repeated unit, not attention alone.

Commit a prediction before revealing the model.
Today's win: trace one token through pre-norm, attention, residual addition, MLP/SwiGLU, and the final LM head and explain where inference memory and FLOPs go.
encoder you are here decoder

This lesson opens up a single decoder block: its residual stream, attention, MLP (the feed-forward network, or FFN, the big per-token math block in each layer), and final LM head (the output, or LM, head is the final layer that scores every token in the vocabulary). To see where this one block sits in the whole model, zoom back out to Lesson 7 · the original Transformer →

The setup

One kitchen station has two specialists: a researcher who gathers context (attention) and a prep cook who transforms each item independently (MLP). The residual stream is the shared worktop both add their results back onto.

1 · The residual stream is the main path

Each token begins as a vector on the residual stream. A modern decoder block usually applies RMSNorm or LayerNorm (normalization rescales each vector to a controlled size so a deep stack of layers stays numerically stable; pre-norm does it before each sub-layer) before a sublayer, computes a change, then adds that change back. Residual connections preserve information and make many blocks trainable.

2 · Attention mixes information across tokens

Normalized token states form Q, K, and V. Causal attention lets each position gather information from earlier positions. Its KV states are the part cached across decode steps.

3 · The MLP transforms each token independently

After another norm, the feed-forward network (the MLP and the FFN are the same per-token math block) expands each token into a wider intermediate dimension and projects it back. Qwen/Llama-style blocks commonly use gated SwiGLU, which adds a learned "gate" that scales the intermediate activation (the intermediate vector the layer outputs). These large matrices hold a major share of dense-model parameters and decode weight traffic.

4 · The LM head closes the stack

After dozens of blocks, a final norm and vocabulary projection produce one logit per token (logits = the raw per-token scores, before softmax turns them into probabilities; see Lesson 8). Sampling chooses the next ID; the same stack runs again for the next decode step.

On YOUR cluster live-tested · course lab

Qwen3.6-27B's served config has 64 layers, hidden size 5,120, FFN size 17,408, and a 248,320-token vocabulary. Those dimensions make the repeated block matrices, not the tokenizer, the dominant weight footprint.

Study next: sources & lab companionTransformer · RMSNorm · GLU variants

Final check

← Lesson 5Lesson 7 →
References

Transformer · RMSNorm · GLU variants

Inside a Transformer Block

The complete repeated unit, not attention alone.

Today's win: trace one token through pre-norm, attention, residual addition, MLP/SwiGLU, and the final LM head and explain where inference memory and FLOPs go.
encoder you are here decoder

This lesson opens up a single decoder block: its residual stream, attention, MLP (the feed-forward network, or FFN, the big per-token math block in each layer), and final LM head (the output, or LM, head is the final layer that scores every token in the vocabulary). To see where this one block sits in the whole model, zoom back out to Lesson 7 · the original Transformer →

The picture

One kitchen station has two specialists: a researcher who gathers context (attention) and a prep cook who transforms each item independently (MLP). The residual stream is the shared worktop both add their results back onto.

The residual stream is the main pathEach token begins as a vector on the residual stream
Attention mixes information across tokensNormalized token states form Q, K, and V
The MLP transforms each token independentlyAfter another norm, the feed-forward network expands each token into a wider intermediate dimension and projects it back
The LM head closes the stackAfter dozens of blocks, a final norm and vocabulary projection produce one logit per token

1 · The residual stream is the main path

Each token begins as a vector on the residual stream. A modern decoder block usually applies RMSNorm or LayerNorm (normalization rescales each vector to a controlled size so a deep stack of layers stays numerically stable; pre-norm does it before each sub-layer) before a sublayer, computes a change, then adds that change back. Residual connections preserve information and make many blocks trainable.

RESIDUAL STREAM · the block's shared information highway block input block output RMSNorm before attention causal attention mix earlier tokens + add branch result back RMSNorm before MLP SwiGLU MLP transform each token + add branch result back pre-norm: normalize each branch, preserve the spine
Notice the amber spine never disappears: attention and the MLP compute useful changes on side branches, then add them back. This residual path matters because information and gradients can travel through many stacked blocks without being replaced at every step.

2 · Attention mixes information across tokens

Normalized token states form Q, K, and V. Causal attention lets each position gather information from earlier positions. Its KV states are the part cached across decode steps.

ATTENTION · cross-token mixing MLP · the same transform runs per token the pod crashed mixed meaning a token can use relevant earlier tokens token 1 token 2 token 3 SwiGLUMLP SwiGLUMLP SwiGLUMLP new 1new 2new 3 no sideways arrows: tokens transform independently
Notice the sideways blue connections only on the attention side: attention lets a token gather context from other tokens. The red MLP applies the same learned transformation to each token separately, which matters when reasoning about sequence communication versus per-token compute.

3 · The MLP transforms each token independently

After another norm, the feed-forward network (the MLP and the FFN are the same per-token math block) expands each token into a wider intermediate dimension and projects it back. Qwen/Llama-style blocks commonly use gated SwiGLU, which adds a learned "gate" that scales the intermediate activation (the intermediate vector the layer outputs). These large matrices hold a major share of dense-model parameters and decode weight traffic.

DENSE WEIGHTS · fixed for every request Qwen MLP dimensions: 5,120 → 17,408 → 5,120 5,120 token width up + gate matrices 5,120 × 17,408 each very large weight reads down matrix 17,408 × 5,120 another large read 5,120 out Wide rectangles = many learned numbers stored once, then streamed from memory for inference. KV STATE · starts small, grows as tokens arrive one K/V entry per token → request memory grows with context
Notice the huge red 5,120 × 17,408 matrices compared with one token vector: these fixed dense weights dominate parameter count and must be read repeatedly, so they drive weight traffic. The separate blue KV state grows with every context token, which matters because long or concurrent requests consume increasing memory even though model weights stay fixed.

4 · The LM head closes the stack

After dozens of blocks, a final norm and vocabulary projection produce one logit per token (logits = the raw per-token scores, before softmax turns them into probabilities; see Lesson 8). Sampling chooses the next ID; the same stack runs again for the next decode step.

THE FULL FORWARD PASS · repeat, score, choose transformer block × 64 same shape, new weights final RMSNorm LM head vocabulary scores 248,320 logits one per token ID one token sampled the current token representation is refined through all 64 blocks sampling turns the final distribution into exactly one next token; decode then repeats the whole pass
Notice that the model does not choose a word inside each block: all 64 blocks first refine the representation, then the LM head produces 248,320 candidate logits and sampling selects one token. This matters because generating every next token requires another full trip through the entire stack.

On YOUR cluster live-tested · course lab

Qwen3.6-27B's served config has 64 layers, hidden size 5,120, FFN size 17,408, and a 248,320-token vocabulary. Those dimensions make the repeated block matrices, not the tokenizer, the dominant weight footprint.

Check yourself

← Lesson 5Lesson 7 →
References

Transformer · RMSNorm · GLU variants