Skip to content

TransformersLesson 5 of 10

Stacking blocks and keeping their shape

Residual additions, normalization, and matrix shapes

One block hands back the same shape it got. That makes its output a valid input to the next block. Different blocks have different learned weights. The number of blocks is a model choice; 96 is an example, not a requirement.

Each sublayer computes an update and adds it to its input. The vectors passed along through those additions form the residual stream. Both terms of each addition must have the same shape.

The number of entries stays fixed. Their magnitude need not: additions may reinforce or cancel. Residual addition alone does not bound the vector’s length.

These chosen updates demonstrate addition and cancellation. Their bounded values are a feature of this example, not a guarantee about trained models.

In the pre-normalized arrangement shown here, a sublayer reads a normalized version of each token row. Layer normalization subtracts that row’s mean, divides by variance+ε\sqrt{\text{variance}+\varepsilon}, then applies learned scale and offset parameters. The positive ε prevents division by zero. The residual stream itself still receives the additive update; normalizing its input does not prove the stream stays bounded.

Change the input scale and compare the original and normalized entries.

A decoder model repeats the attention → add → feed-forward → add block, using different parameters in each block. Token and position lookups happen before the stack; final normalization and vocabulary scoring happen after it.

X0=token vectors+position vectors,X+1=Block(X).X_0=\text{token vectors}+\text{position vectors},\qquad X_{\ell+1}=\operatorname{Block}_{\ell}(X_\ell).

Here ℓ labels the block. Every XX_\ell has one row per token and the same feature width.

Matrix multiplication performs much of the arithmetic, but its inputs matter:

OperationWhat it combines
A projection, such as Q = XW + bInput-dependent values and fixed learned parameters
QKᵀQuery and key values that both depend on the input text
AVAttention weights and value rows, both computed from the input
Normalization, softmax, ReLU, and residual additionOther operations that change the result; they are not matrix products

The nonlinear operations matter. A complete Transformer is not equivalent to multiplying the original input by one fixed matrix.

Follow the tokens through a complete calculation

Section titled “Follow the tokens through a complete calculation”

The explorer computes one small decoder block with a single attention head. Start with “the cat sat”: three token rows, each with four features. Its fixed, invented weights let us check the calculation; they have not learned language.

Select a token to follow its row through the whole block. Select any number to see the inputs that produced it. For a matrix product, that means one input row multiplied by one weight column. Attention also uses products whose two inputs both depend on the text: Q × Kᵀ computes position-to-position scores, and A × V mixes value rows.

Select a token row, then a number. The calculation shows which inputs and weights produced it.

Open the explorer on its own for more room.

Try the causal mask: select the first position, then open “Change the input” and change the final token. The first row’s result stays unchanged because it cannot use future positions. Select the final position to see a row that can use the whole input.

Try the matrix product: select a query entry. Its explanation shows four input values, four entries from one weight column, their products, and the added bias. Now inspect a mixed-value entry: its terms come from different token positions. The arithmetic is similar, but the axes mean different things.

Appending the highest-probability token uses the final position’s output and runs the calculation again with the same weights. Training would be a separate operation that changes those weights.

What if several passes reused the same block parameters? Recurrence explains that variation.

Next: how that last vector turns into a word.

Sources · 7
  1. Jurafsky, Daniel, and James H. Martin. Speech and Language Processing (3rd ed. draft, 2026), Chapter 8: “Transformers.”
  2. He, Kaiming, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. “Deep Residual Learning for Image Recognition.” CVPR, 2016. arXiv:1512.03385.
  3. Elhage, Nelson, et al. “A Mathematical Framework for Transformer Circuits.” Transformer Circuits Thread, 2021.
  4. Ba, Jimmy Lei, Jamie Ryan Kiros, and Geoffrey E. Hinton. “Layer Normalization.” 2016. arXiv:1607.06450.
  5. Xiong, Ruibin, et al. “On Layer Normalization in the Transformer Architecture.” ICML, 2020. arXiv:2002.04745.
  6. Zhang, Biao, and Rico Sennrich. “Root Mean Square Layer Normalization.” NeurIPS, 2019. arXiv:1910.07467.
  7. Brown, Tom B., et al. “Language Models are Few-Shot Learners.” NeurIPS, 2020. arXiv:2005.14165.

Full bibliography →

Definition

Read the full glossary entry →