4.5 The transformer block
1. Learn
Section titled “1. Learn”- Understanding Deep Learning (Prince)§12.4–12.5 Transformer layers and Transformers for NLP (pp. 215–219)
- Vaswani et al., Attention Is All You Needsections 3.1–3.5 and figure 1
- Stanford CS336: Language Modeling from Scratchlecture 3 "Architectures, hyperparameters" (April 8, slides)
2. Bridge
Section titled “2. Bridge”The residual stream. A transformer is a stack of identical blocks, each of the form , then . Attention moves information between positions; the MLP processes each position on its own. Both only add to the stream, so, as with ResNets in 3.7, gradients have a direct path through every layer. Putting the norm before each sublayer (pre-norm) rather than after the addition, as the original paper did, trains deep stacks without careful warmup (Xiong et al. 2020). UDL §12.4 and the original paper’s figure 1 draw the post-norm order, so expect the readings to differ from the block you build here (UDL §12.4).
Multiple heads. One attention pattern per layer is limiting: a token may need to look at the previous word and at the subject of the sentence at once. Multi-head attention splits the query, key and value features into groups of , runs attention in each group independently, concatenates the results and mixes them with an output matrix . The cost and parameter count are the same as for one head of width (Vaswani et al. 2017, §3.2.2).
Modern components. Current open models such as LLaMA replace LayerNorm by RMSNorm, which only rescales and skips the mean (Zhang & Sennrich 2019). They replace the ReLU MLP by SwiGLU, where one projection, passed through SiLU, gates another elementwise (Shazeer 2020). And they encode positions by rotating queries and keys instead of adding a position vector to the input (CS336 lecture 3).
Rotary embeddings. Attention alone sees a set (4.4). RoPE splits each query and key into pairs of features and rotates pair by the angle , where is the position and . Rotating both vectors changes their dot product only through the difference of their angles, so the score depends on the relative position and the token contents, never on absolute positions (Su et al. 2021).
3. Explore
Section titled “3. Explore”stubs/m4_5_block.pyLoading starter file…
Prefer your own editor? Edit exercises/stubs/m4_5_block.py and run mlc check 4.5. The tests are identical; import the results on the progress page.
Then check your block against PyTorch (locally; needs pip install torch, CPU is fine):
cd exercises && python projects/block_vs_pytorch.pyIt builds the same block from PyTorch’s rms_norm, scaled_dot_product_attention and silu, with RoPE written in the complex-number form of Meta’s LLaMA code. The reference solution agrees to about 10⁻¹⁵.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
Learner log: time spent and points of confusion
Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.