Foundation Models: Tokens, Attention, And Generation · Lesson 7

Inside A Transformer: Layers And Connections

A defined transformer block combines attention, learned feedforward transformations and separate residual/normalization operations. Trace the actual order and input to each path to explain what the block computes.

Study And Save Progress

Watch The Lesson

7 min 4 sec

Read the video transcript

A diagram of a language model sometimes shows tokens entering attention and an answer coming out. Follow the package sentence from the last lessons instead. Attention has combined information from allowed positions. What happens to that new vector before it becomes the input to another layer?

We will follow parallel attention heads, the 1st addition and normalization, a further learned transformation, and the 2nd addition and normalization. Leaving out these connections changes the computation we are trying to explain. Use the encoder block in the original Transformer paper, first released in June 2017, as an inspectable historical design.

Its input is a sequence of vectors containing information about the input and its positions. Call the vector at 1 position x. The whole sequence enters the attention calculation, while a separate path carries x around that calculation to an addition. Keeping that path visible will let

us identify exactly what is combined. Multi-head attention performs several attention calculations with different learned projections. Each head computes its own queries, keys and values, combines allowed source-position values, and produces an output. The original paper uses 8 heads. Their outputs are placed together,

then another learned projection combines them into the attention sublayer's output. The model does not simply choose the winning head and discard the others. Different learned projections allow different relationships to affect the representation. That does not give each head a guaranteed human job.

Calling a head the grammar expert or the arithmetic expert requires evidence about its behavior. For our package sentence, draw different computed contributions without inventing a discovered linguistic role. The useful question is which inputs, learned transformations and allowed connections produced the result. Now return to the path around attention.

Add the attention sublayer's output to its own input x, coordinate by coordinate. As a small supplied arithmetic fragment, let x be (2,1) and the attention output be (0.5,negative 0.5). Their sum is (2.5,0.5). This fragment illustrates the addition only; it is not a measured head

output or the complete block calculation. The incoming representation contributes directly alongside the computed change. In this original design, layer normalization follows that addition. It adjusts the coordinates at a position using their mean and variance, a small numerical stabilizer,

and learned scale and offset settings. This changes numerical behavior while preserving the vector's role in the continuing computation. We will label its output y rather than pretend that our addition-only example has already calculated normalization. Later model designs can move normalization before a transformation, so their

diagrams must show their own order. The state y then enters a position-wise feedforward network. Position-wise means that the same learned transformation is applied separately to each sequence position at this stage. In the original paper it contains a learned linear transformation, a nonlinear function,

and another learned linear transformation. Attention mixed information across positions; this network transforms the resulting information within each position. Its weights are learned, while its computed activations depend on the current input. The nonlinear function in the original block is called ReLU. It passes positive values through

and replaces negative values with 0. For a supplied intermediate pair (negative 1,2), its result is (0,2). Without a nonlinear operation between these learned transformations, the pair could be collapsed into 1 transformation with weights and a bias. The nonlinear step lets the composition express a wider set of relationships.

This small pair illustrates that operation, not an observed model feature. A 2nd path carries y around the feedforward network. Add the feedforward output to y, then apply another layer normalization. Notice the reference point: this residual path starts at the feedforward input y.

It does not return to the original block input x. The original encoder block therefore contains 2 separate addition-and-normalization sites, each attached to its own sublayer. The resulting sequence of vectors becomes input to the next block. Repeating blocks repeatedly transforms the representation;

the original encoder stacks 6. Weights can differ between these blocks even though the architectural pattern repeats. A final hidden state has passed through more than an embedding lookup and an attention mixture. The later output projection and vocabulary scores remain separate parts of the complete model.

Pause and trace the original post-normalized encoder block from x. Where do the 2 residual paths begin, and does the feedforward transformation happen before or after the 1st normalization? Explain the order without using a single box labelled attention as your whole answer. The 1st path starts at

x and joins the attention output. Normalization produces y. The feedforward network transforms y, while the 2nd residual path also starts at y. Their sum passes through the 2nd normalization. The feedforward transformation therefore follows the 1st addition and normalization. Both sites belong in

this historical block diagram. Try a different diagram. It shows attention, then feedforward, then 1 final addition of the original input and 1 normalization. A colleague says the missing connections are merely drawing shortcuts. Identify the computation that

this diagram fails to preserve, and say what evidence would justify labelling 1 of its heads the arithmetic expert. The diagram removes the 1st attention addition and normalization and gives the later residual the wrong starting state for the original architecture. Restore attention plus x, normalize to y,

then feedforward plus y and normalize again. A head label needs a defined behavioral test and evidence; its number or a suggestive arrow is insufficient. This diagnosis concerns the original post-normalized design. Another architecture requires its own explicit specification. Architecture also matters when planning a service. Head arrangements and key/value dimensions affect

temporary cache storage; feedforward shapes affect matrix work and hardware use. A model name alone does not provide those quantities. You can now inspect the connections inside a defined block and ask which configuration is actually being served. Next we will use that discipline

to compare model families by which directions information can travel and what each family is trained to produce.

Main Point

A defined transformer block combines attention, learned feedforward transformations and separate residual/normalization operations. Trace the actual order and input to each path to explain what the block computes.

Key Points

1. Follow a defined architecture

Use the encoder in Vaswani and colleagues’ 2017 Transformer paper as the historical worked architecture. Its input is a sequence of vectors with input and positional information. At a position, call its incoming vector x. Attention processes the sequence while a separate path preserves x for the subsequent addition.

Later pre-normalized architectures arrange normalization differently. An architectural claim needs the diagram and specification for the particular model being discussed.

2. Combine multiple heads

The original design uses 8 attention heads. Each has learned query, key and value projections and computes its own allowed-position value mixture. The head outputs are concatenated: their coordinates are placed together. A learned output projection then combines that larger representation into the attention sublayer’s output.

This is a defined combination of computed vectors. It does not select 1 winning head. Different projections can make different relationships available, but the name “arithmetic expert” needs behavioral evidence rather than a suggestive diagram label.

3. Add the attention residual, then normalize

The 1st residual connection carries x around attention. Add corresponding coordinates of x and the attention output. For the supplied addition-only illustration:

`x=(2,1)`

`attention output=(0.5,−0.5)`

`sum=(2.5,0.5)`

These are classroom values, not a recorded attention head output. The sum is not yet the normalized state. Layer normalization then uses a position’s coordinate mean and variance, a numerical stabilizer and learned scale/offset parameters to adjust the representation. Call the resulting state y.

`y=LayerNorm(x+Attention(x))`

This notation summarizes the defined sublayer order; training details such as dropout are outside this simplified inference-oriented trace. We have not supplied normalization parameters or computed y numerically.

4. Transform each position through feedforward

The same learned feedforward network applies separately to each position’s y. In the original block it has a linear transformation, ReLU and another linear transformation. ReLU passes positive values through and replaces negative values with 0. A supplied intermediate pair `(−1,2)` therefore becomes `(0,2)` at this operation.

The learned matrices and biases are model parameters; the intermediate values are input-dependent activations. Attention mixes information across allowed positions. This feedforward stage transforms information within each position. Without an intervening nonlinearity, the pair could collapse into 1 transformation with weights and a bias.

5. Add the feedforward residual, then normalize

Carry y around the feedforward network. Add its output to y and apply the 2nd layer normalization:

`z=LayerNorm(y+FFN(y))`

The 2nd path begins at y, not at the block’s earlier input x. The complete simplified sequence is:

`x → attention → add x → normalize to y → feedforward → add y → normalize to z`

Keep both additions and both normalization sites visible. The connection is part of the computation rather than a decorative arrow.

6. Repeat blocks and inspect configuration

The original encoder stacks 6 blocks of this architectural form. Repeating the form does not require their learned parameters to be identical. The final sequence has undergone repeated mixing and transformation; later output projections and vocabulary scores have their own functions.

For serving, head arrangements and key/value dimensions affect cache storage. Feedforward matrix shapes affect computational work and hardware use. Inspect the actual configuration before estimating those quantities from a model name. This lesson supplies no measured memory requirement or deployment result.

7. Closing

You can now explain what enters each sublayer, what its residual path carries and which state leaves. That lets you detect a missing operation without assigning invented human jobs to heads. Next, compare model families by their permitted information paths and training tasks.

8. Learning Checks

Attempt the questions before opening their explanations. The worksheet has blank space for your own trace.

M14-L07-Q01

Trace the original post-normalized encoder block from x. Where do its 2 residual paths begin? Does feedforward occur before or after the 1st normalization?

Your explanation: ____________________

Show explanation after your attempt

The 1st residual path starts at x. Add the attention output to x, then normalize to obtain y. The feedforward network transforms y, and the 2nd residual path also starts at y. Add that network’s output to y, then normalize again. Feedforward follows the 1st addition and normalization. There are 2 distinct residual paths around 2 sublayers in this original post-normalized design.

M14-L07-P01

A proposed diagram shows attention, then feedforward, then 1 final addition of the original input and 1 normalization. A colleague calls the omitted connections drawing shortcuts and labels head 3 the arithmetic expert. Diagnose the diagram and identify evidence needed for that head label.

Your explanation: ____________________

Show explanation after your attempt

The diagram omits the attention sublayer’s own addition and normalization, so it does not preserve the original computation. It also uses x where the feedforward residual should use y, the state after the 1st normalization. Restore y=LayerNorm(x+Attention(x)), followed by z=LayerNorm(y+FFN(y)). Head 3’s number establishes no arithmetic role. A defined behavioral test and evidence about that head’s contribution are needed before using that functional label.

Sources

Learner Materials

Download Lesson 7 Learner Materials

Use the supplied fictional cases and templates for practice.

Practice

Write your answer, compare it with the model answer and decide what to revisit. These written checkpoints are self-assessed.

Trace the original post-normalized encoder block from x. Where do its 2 residual paths begin? Does feedforward occur before or after the 1st normalization? Your explanation: ____________________

Show The Model Answer

The 1st residual path starts at x. Add the attention output to x, then normalize to obtain y. The feedforward network transforms y, and the 2nd residual path also starts at y. Add that network’s output to y, then normalize again. Feedforward follows the 1st addition and normalization. There are 2 distinct residual paths around 2 sublayers in this original post-normalized design.

Compare your reasoning with the model answer. These written checks are self-assessed.

A proposed diagram shows attention, then feedforward, then 1 final addition of the original input and 1 normalization. A colleague calls the omitted connections drawing shortcuts and labels head 3 the arithmetic expert. Diagnose the diagram and identify evidence needed for that head label. Your explanation: ____________________

Show The Model Answer

The diagram omits the attention sublayer’s own addition and normalization, so it does not preserve the original computation. It also uses x where the feedforward residual should use y, the state after the 1st normalization. Restore y=LayerNorm(x+Attention(x)), followed by z=LayerNorm(y+FFN(y)). Head 3’s number establishes no arithmetic role. A defined behavioral test and evidence about that head’s contribution are needed before using that functional label.

Compare your reasoning with the model answer. These written checks are self-assessed.

Open Practice And Save Your Work