Feed-forward network (FFN)

A feed-forward network, or multilayer perceptron, is a stack of neuron layers that passes data in one direction; in a transformer it is the block after attention that processes each token on its own and holds most of the parameters.

A feed-forward network is the simplest kind of neural network. Data enters at one end and flows through a series of layers to the other, with no loops. Each layer multiplies its input by a grid of learned weights, adds a bias and passes the result through a non-linear activation function. A network of this kind with one or more hidden layers is also called a multilayer perceptron, or MLP, and the two names are used almost interchangeably.

Inside a transformer, each layer has two main parts. Attention lets every token gather information from the other tokens. The feed-forward network then works on each token's position separately and identically. In the 2017 paper that introduced the transformer, it was two linear transformations with a ReLU activation between them: each token's 512 numbers were expanded to 2,048 and then compressed back to 512. Expanding and compressing like this gives the model room to transform what attention has gathered.

These blocks are where most of a transformer's parameters sit. A 2020 study by Mor Geva and colleagues noted that feed-forward layers make up about two-thirds of a transformer's parameters, and found that they behave like key-value memories: the first layer's units respond to particular patterns in the text, and the second pushes the model towards likely next tokens. Lower layers picked up surface patterns and higher layers more abstract ones, which is one reason researchers studying how models store what they have learned look closely at these blocks.

The design has been refined rather than replaced. In 2020 Noam Shazeer reported that gated variants, which multiply two projections together, improved quality over plain ReLU or GELU activations. Meta's LLaMA models, published in February 2023, used one of these, SwiGLU, with the hidden layer sized at two-thirds of four times the model width.

Feed-forward networks are also the experts in mixture-of-experts models. Mixtral 8x7B, released in December 2023, replaces the single feed-forward block in each layer with eight of them, and a router sends each token to two. That gives it 47 billion parameters in total but only about 13 billion in use for any one token. When news coverage says a model has hundreds of experts, it means hundreds of these blocks.

Common questions

Is a feed-forward network the same as a multilayer perceptron?

In practice, nearly always. A multilayer perceptron is a feed-forward network with at least one hidden layer, and transformer papers use 'MLP block' and 'feed-forward network' for the same component. Strictly, feed-forward describes any network without loops, so it is the broader term.

What does the feed-forward layer do in a transformer?

After attention has mixed information between tokens, the feed-forward layer transforms each token's representation on its own. Research suggests it acts partly as a memory, matching patterns in the input and nudging the model towards likely outputs. It contains most of the model's parameters.

Why does the feed-forward layer hold so many parameters?

Its inner layer is several times wider than the model's main width, four times in the original transformer, and it has two large weight matrices to expand and then compress each token's values. Those matrices add up to about two-thirds of the parameters in a standard transformer.

How are feed-forward networks related to mixture of experts?

A mixture-of-experts model replaces the one feed-forward block in each layer with many smaller ones, called experts, and a router picks a few for each token. The model can then hold far more parameters while using only a fraction of them for any single token.

Finding more

Search for "transformer feed-forward layer", "MLP block transformer" or "SwiGLU" to see how the component is built, and "feed-forward layers key-value memories" for interpretability research on what it does. Original papers on arXiv and the architecture sections of open model technical reports are the most reliable sources. Introductory textbooks and university course notes cover the multilayer perceptron in its classic form.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.