Inferensys

Glossary

Feed-Forward Network (FFN)

A position-wise, fully connected network within a Transformer block that applies a non-linear transformation to each token's representation independently, typically consisting of two linear layers with an activation function.
Enterprise console with connected nodes and monitoring panels for orchestrated systems.
TRANSFORMER ARCHITECTURE

What is Feed-Forward Network (FFN)?

A position-wise, fully connected sub-layer within a Transformer block that applies a non-linear transformation to each token's representation independently.

A Feed-Forward Network (FFN) is a position-wise, fully connected network that applies an identical non-linear transformation to every token's representation independently within a Transformer block. It typically consists of two linear projections separated by an activation function, such as ReLU or GELU, enabling the model to introduce non-linearity and increase representational capacity.

Unlike the self-attention mechanism, which mixes information between tokens, the FFN operates on each position in isolation. This component acts as a per-token feature processor, expanding the hidden dimension before projecting it back, and is essential for the model to learn complex, non-linear functions of the data.

POSITION-WISE TRANSFORMATION

Key Characteristics of the FFN

The Feed-Forward Network (FFN) is the primary locus of non-linear processing and memory storage within a Transformer block. It applies an identical transformation to every token independently, expanding and then contracting the model's hidden dimension.

01

Position-Wise Independence

Unlike the self-attention sub-layer, which mixes information across token positions, the FFN processes each token's representation in isolation. This means the exact same weights are applied to every vector in the sequence, treating each position identically. This design allows the attention mechanism to handle the routing of information between tokens, while the FFN focuses on refining what is stored within each token's hidden state.

02

The Two-Layer Sandwich

The standard FFN consists of two linear projections separated by a non-linear activation function:

  • Up-Projection: The first layer expands the input from the model's hidden dimension d_model to a larger intermediate dimension d_ff (often 4x larger).
  • Activation: A non-linear function like ReLU, GELU, or SwiGLU is applied.
  • Down-Projection: The second layer projects the expanded representation back down to d_model. This bottleneck structure forces the network to learn a compressed, expressive representation.
03

Key-Value Memory Analogy

A leading interpretation views the FFN as a massive, static key-value memory bank. The first linear layer acts as a set of keys, detecting specific patterns in the input. The activation function selectively fires for matching patterns, and the second linear layer retrieves the corresponding values—factual knowledge or linguistic rules—and adds them to the residual stream. This is why factual recall is often localized to FFN weights.

04

SwiGLU Activation Variant

Modern architectures like LLaMA and PaLM replace the simple ReLU with the SwiGLU activation function. This uses a gating mechanism:

  • The input is projected into two separate matrices.
  • One is passed through a Swish (SiLU) activation.
  • The result is multiplied element-wise with the other matrix. This gated linear unit provides a smoother gradient flow and has been empirically shown to improve model quality, though it requires an additional weight matrix.
05

The Sparse Mixture-of-Experts (MoE) Extension

To scale model capacity without proportionally increasing compute, the single FFN can be replaced with a Mixture-of-Experts (MoE) layer. This contains multiple parallel FFN 'experts' and a trainable router network.

  • Sparsity: For each token, the router selects only the top-k experts (e.g., 2 out of 8) to activate.
  • Conditional Compute: This massively increases total parameters while keeping the FLOPs per token roughly constant, as inactive experts are not computed.
06

Computational Dominance

Despite its conceptual simplicity, the FFN dominates the parameter count and computational budget of a Transformer. In a standard architecture with d_ff = 4 * d_model, the two linear layers contain roughly two-thirds of the total model parameters. Optimizing these matrix multiplications is therefore the primary target for inference acceleration techniques like quantization and pruning.

TRANSFORMER BLOCK COMPONENTS

FFN vs. Self-Attention: Complementary Roles

A functional comparison of the two primary sub-layers within a Transformer block, highlighting their distinct computational roles in token processing.

FeatureFeed-Forward Network (FFN)Self-Attention

Primary Function

Position-wise non-linear transformation

Token mixing via pairwise interactions

Interaction Scope

Per-token independently

Across all tokens in the sequence

Core Operation

Two linear projections with activation

Scaled dot-product of Q, K, V vectors

Information Flow

Vertical (depth-wise feature refinement)

Horizontal (contextual aggregation)

Complexity Class

O(n * d_model^2)

O(n^2 * d_model)

Parameter Count

~67% of block parameters

~33% of block parameters

Role in Architecture

Knowledge storage and pattern memory

Dynamic routing of contextual information

Activation Function

FEED-FORWARD NETWORK CLARIFIED

Frequently Asked Questions

Clear answers to common questions about the position-wise feed-forward network, its role in the Transformer block, and its impact on model capacity.

A Feed-Forward Network (FFN) is a position-wise, fully connected sub-layer within a Transformer block that applies an identical non-linear transformation to every token's representation independently. Unlike the self-attention mechanism, which mixes information between tokens, the FFN processes each token's vector in isolation. It typically consists of two linear transformations with a non-linear activation function, such as ReLU, GELU, or SwiGLU, in between. The standard formulation is FFN(x) = W_2 * Activation(W_1 * x + b_1) + b_2, where the first layer expands the hidden dimension (e.g., by a factor of 4) and the second layer projects it back to the model's original dimension. This architecture acts as a per-token feature processor, introducing the non-linearity essential for the model to learn complex functions beyond simple weighted averages.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.