Inside a transformer layer: residual paths, the per-token network and the cost
10 questions on residual connections, layer normalisation, pre-norm, the per-token MLP, why attention alone is limited, parameter counts and how cost scales with length.
- 10 questions
- Stopwatch
- Instant feedback
Sample questions
Play now ↑Answers stay hidden. Start the quiz to play them.
Sample 1
A sequence has 100 tokens of 100 features each. A fully connected mapping of all inputs to all outputs would have about 100 million parameters. The attention matrices are shared across tokens and amount to roughly 100 squared parameters. About how many times fewer parameters is that?
- About 100 times
- About 10,000 times
- About 10 times
How it works
- One question per screen. An answer is final once sent.
- You see right away whether you were right, with a short explanation.
- The stopwatch runs: on equal scores, the faster player ranks first.
- Only your best attempt counts, so replaying is never risky.
About this quiz
A transformer is a tall stack of identical-looking layers, and once you understand one of them you understand the whole thing. A layer has two halves. In the first, tokens exchange information through multi-head attention. In the second, each token is processed on its own by a small ordinary neural network. Around each half, a shortcut adds the input back onto the output and a normalisation keeps the numbers well scaled. This quiz opens up a layer and explains each part, then asks what the whole arrangement costs in parameters and in computation.
You will first see why the attention half is not enough by itself. Its outputs are blends of its inputs, so they can never leave the space spanned by those inputs, and the only nonlinearity is in the weights. The second half repairs that by adding a nonlinear network that is applied to every token, using the same weights each time, which has the useful side effect that sequences of any length can be handled. You will see the two common ways of arranging the normalisation, after the block or before it, and why the layers in a stack have the same structure but do not share their weights.
The second half of the quiz is about cost, and it is arithmetic with a message. A fully connected network that mapped all the input values to all the output values would need a number of parameters that grows with the square of the sequence length times the square of the token size. An attention layer uses shared matrices and needs only a number that grows with the square of the token size, though its dot products cost an amount that grows with the square of the sequence length. You will compare the two terms, and see which dominates for short and for long sequences.
Frequent confusions include thinking that the per-token network mixes information between tokens, that the layers of a transformer share one set of weights, and that attention cost is linear in sequence length. It is quadratic, which is why very long inputs are expensive.
The quiz suits learners who have met multi-head attention and want to see the whole layer, and practitioners who need a feel for how transformers scale. It uses round numbers. There are 10 questions, each with an explanation, and it takes about four minutes. Knowing what a layer contains and what it costs lets you reason about real systems. When someone says that a model's cost rises steeply with the length of its input, or that doubling the width of the model quadruples a part of its parameters, you can see why from the formulas in a layer. The same reasoning guides choices about how long a context to allow, how many layers to stack and where to spend a limited computing budget, which is why these simple counts are among the most practical in the field.
Questions and answers
›How long does the quiz take?
About 3 to 4 minutes for 10 questions, at roughly 20 seconds each. An explanation follows each answer, with the cost comparisons worked out.
›Do I need to know multi-head attention?
Yes, in outline. The quizzes on attention basics and on scaled and multi-head attention cover it. Here it is treated as a building block, and the questions recall what it does as they use it.
›What are the two stages of a transformer layer?
The first stage is attention, which mixes the features of different tokens, working across the columns of the table of tokens. The second stage acts on each row independently, transforming the features within each token vector through a small neural network. Together they map a table of tokens to another of the same shape.
›Why add a neural network after attention?
The outputs of attention are linear combinations of linear functions of the inputs, so they lie in the space spanned by the inputs, which limits what a layer can express. Nonlinearity enters only through the attention weights. A multilayer network applied to each token adds flexible nonlinear processing, and it uses the same weights for every token so that the length of the sequence can vary.
›What are residual connections and layer normalisation doing here?
A residual connection adds a block's input to its output, which helps training of deep stacks and needs the input and output to have the same size. Layer normalisation then rescales each token's features, which improves training efficiency. Both are applied around the attention block and again around the per-token network.
›What is pre-norm?
It is a variation in which the normalisation is applied before the attention or per-token network, inside the residual branch, instead of after the addition. It can make optimisation more effective. The layer still maps a table of tokens to a table of the same shape.
›Do the layers of a transformer share weights?
No. The layers usually have identical structures, but each has its own weights and biases. What is shared is the parameters across the tokens within a layer, which is what lets one layer process sequences of different lengths and apply the same computation to every token.
›How does the cost compare with a fully connected network?
A fully connected network mapping all the input numbers to all the output numbers would need a number of parameters, and a number of calculations, growing with the square of the sequence length times the square of the token size. A transformer layer shares its matrices across tokens, so it has far fewer parameters, and is much cheaper to compute.
›Which part of the cost dominates?
The dot products of attention cost an amount that grows with the square of the sequence length times the token size. The per-token network costs an amount that grows with the sequence length times the square of the token size. For long sequences with small tokens, attention dominates, and for short sequences with large tokens, the per-token network does.