Part C · Chapter 13 · The layer

13. The position-wise feed-forward network

After attention has mixed the words together, each word goes on its own through a small two-layer network: widened to four times its size, the negatives cut to zero, narrowed back. The same network for every word.

Widen, cut, narrow
Sentence
Playback speed

One word's vector after the attention of the encoder's first layer: 64 numbers drawn as bars, up for positive and down for negative. W₁ widens it to 256 numbers; the ReLU sets every negative one to 0, and the clip counts them; W₂ narrows the rest back to 64. Last, the same two matrices on every word of the sentence, with how many of each word's 256 numbers the ReLU set to 0.

The idea

Attention (chapters 9 to 11) is where words exchange information: each word's new vector is a blend of the others'. What attention cannot do is much thinking about one word on its own: it only mixes. So every layer of the transformer follows its attention with a second step, the feed-forward network, which works on each word separately.

It is the simplest network there is, in three moves. A matrix W₁ widens the word's 64 numbers into 256. The ReLU keeps every positive number and replaces every negative one by 0. A second matrix W₂ narrows the result back to 64. Widening gives room for many small detectors, each a column of W₁; the ReLU lets each detector either fire or stay silent; W₂ adds up what fired.

“Position-wise” means the same network for every position: the word at place 1 and the word at place 9 go through exactly the same W₁ and W₂, one at a time, without looking at each other. The words already looked at each other in the attention just before.

A worked example: “it”

In “Anna has a small dog . She likes it .”, take “it” after the attention of the encoder's first layer. Its 64 numbers begin [0.11, −0.07, 0.74, 0.42, …]. W₁ and b₁ widen them to 256; of those, 247 are negative.

The ReLU sets all 247 to 0, so only 9 of the 256 numbers are left standing. W₂ and b₂ turn them back into 64 numbers, beginning [0.53, −0.30, 1.51, −0.63, …]: the network's output, which the next step adds back to the word's vector (chapter 14).

Every word of the sentence goes through the same two matrices. How many of their 256 numbers the ReLU zeroes, word by word: Anna 241, has 240, a 251, small 249, dog 250, . 240, She 250, likes 244, it 247, . 237, </s> 250. Most of the detectors stay silent for any one word; which few fire is what the network has to say about it.

The formula

\operatorname{FFN}(x) = \max(0,\ xW_1 + b_1)\,W_2 + b_2
\(x\)
one word's vector, d_model numbers: 64 in our model, 512 in the paper
\(W_1,\ b_1\)
the first linear layer: d_model by d_ff, and d_ff biases; it widens the vector
\(\max(0,\ \cdot)\)
the ReLU, number by number: a positive number stays, a negative one becomes 0
\(W_2,\ b_2\)
the second linear layer: d_ff by d_model, and d_model biases; it narrows the vector back
\(d_{ff}\)
the inner size: 2048 in the paper, 256 in ours, four times d_model in both

One network has d_model × d_ff + d_ff + d_ff × d_model + d_model weights: 33,088 in each of our layers, against 16,384 for the four matrices of the attention before it (W^Q, W^K, W^V and W^O, without biases in our model). The feed-forward networks hold most of each layer's weights.

Why it matters

Because these two matrices are the biggest in every layer, they are where most of a model's parameters, and most of its arithmetic per token, go. When a provider quotes a model's size, or when a bigger model costs more per token, a large share of that is feed-forward weights, used once for every token of your prompt and every token of the answer.

That the network runs on each token alone also makes it easy to spread across hardware: no token waits for another, which is part of why long prompts can be read in one parallel pass.

Where it fails

  • No word sees another here. All the exchange between words happens in attention; a layer without attention could not tell which word came before which.
  • Its detectors are hard to name. The 256 columns of W₁ are learned, not designed; a few may track something recognisable, most do not.
  • A detector can die. If a column's sum is negative for every word, the ReLU always gives 0 and that detector never helps; later models often use smoother functions such as GELU (chapter 22).
Remember After attention, each word goes alone through the same small network: widened by W₁ to four times its size, its negatives cut to 0 by the ReLU, narrowed back by W₂. FFN(x) = max(0, xW₁ + b₁)W₂ + b₂.

Exercises

Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.

What the ReLU does

What does the ReLU do to a negative number?

Count the zeros

In the clip's first sentence, how many of the 256 numbers of “it” does the ReLU of the encoder's first layer set to 0?

Your answer
A whole number

The paper's feed-forward network

The paper uses d_model = 512 and d_ff = 2048. How many weights does one of its feed-forward networks have, biases included?

Your answer
A whole number

Position-wise

Within one layer, which matrices does the feed-forward network use for the different words of a sentence?

In the paper

Section 3.3: “each of the layers in our encoder and decoder contains a fully connected feed-forward network, which is applied to each position separately and identically. This consists of two linear transformations with a ReLU activation in between.” It gives the formula above, with d_model = 512 and an inner size d_ff = 2048, and notes that each layer has its own parameters: “Another way of describing this is as two convolutions with kernel size 1.”

In the paper: section 3.3

Open the Theory tab
An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.