Core
The full block, assembled
A decoder Transformer block alternates normalized causal communication with a nonlinear feature transformation, adding both into one residual stream.
Updated
1
Concept
The pieces now fit into one repeated interface. A decoder-only, pre-norm Transformer block receives a tensor with shape and returns a tensor of the same shape. Everything inside may expand, split into heads, and form square attention matrices, but the boundary stays stable so dozens or hundreds of blocks can stack.
Conceptually, the block is two equations:
Dropout or residual scaling may appear on the update branches. Post-norm architectures move normalization after each sum. The interface remains an existing state plus a learned update.
Open the attention branch. Normalized features are projected into , , and , reshaped into heads, and compared with scaled dot products. A causal mask removes future edges. Softmax turns each query row into routing weights, which mix values. Heads are concatenated, an output projection restores , and the result is written into the residual stream.
Open the FFN branch. The updated stream is normalized, expanded into a wider dimension, transformed by an activation or gate, and projected back to . This operation is shared across positions but does not exchange information between them. It recognizes and transforms feature combinations delivered by the stream.
The alternation is the heart of the architecture. Attention is token mixing: each position can receive information from other allowed positions. The FFN is channel mixing: each position can recombine its own features nonlinearly. Repeating the pair allows routes and transformations to compose. A first layer may collect a local syntactic cue; a later FFN can encode it; another head can carry that feature to a distant position.
Embeddings sit before the stack. Token IDs become vectors, and the model supplies position through an absolute embedding, RoPE, a relative bias, or another scheme. A final normalization often follows the stack. The language-model head then maps each final position to vocabulary logits. Weight tying may reuse the token embedding matrix for this output projection.
Shape tracing prevents many mistakes. With , , , and 12 heads, each usual head has width 64. Scores have shape . After value mixing, the head tensor returns to , then concatenates to . The FFN may expand the final dimension, but must project back before its residual addition.
Memory use does not follow parameter count alone. The attention score or its backward intermediates scale with in conventional implementations. FFN activations scale with . Training must retain or recompute information for gradients, while decoding caches keys and values across generated positions. The same block stresses hardware differently in pretraining, prefill, and token-by-token decode.
A useful anatomy view should preserve causality and ownership: the residual stream is central; normalization reads it; attention and FFN are branches that return updates. Drawing the stream as if it flows through attention and loses its prior state obscures why residual learning works.
The assembled block is surprisingly compact. Its power comes from learned width, depth, data, and repeated composition rather than a long list of operations. Understanding the interface lets you read real implementations without mistaking fused kernels or renamed variables for different mathematics.
2
Explain it like I am five
A parcel travels on a conveyor through two stations. At the communications station, clerks inspect its normalized manifest and retrieve permitted notes from earlier parcels. The notes are added to the parcel without discarding its contents. At the workshop station, another normalized manifest drives a wide bank of tools, and that result is added too. One block is this two-station module; depth means repeating it with new clerks and tools.
3
Teach it back
Trace one tensor through a pre-norm decoder block, naming shapes, masks, residual additions, and the distinction between token mixing and feature mixing.
Minimum: 80 characters and 15 words. Your text stays only in this browser.
Saved only on this device.
Show a model answer
A B×T×d_model residual tensor is normalized and projected into multi-head Q, K, and V. Causal attention mixes information across allowed token positions, heads are concatenated and projected, and that update is added to the original stream. A second normalization feeds a wide FFN that mixes feature channels independently at each position, projects back to d_model, and adds another update. Shapes return to B×T×d_model after each branch so blocks can stack.
4
Check your understanding
Complete the teach-back and answer the quiz correctly to finish this lesson.
Sources
- Ashish Vaswani et al. (2017). Attention Is All You Need.