Skip to main content
Interactive 3D/Feed-Forward Network (FFN) Sub-layer
Feed-Forward Sub-layer (FFN)
Every transformer block has a 2-layer MLP after attention. Click a step to inspect it.
FFN(x) = GELU(xW₁ + b₁) · W₂ + b₂
Input
[seq, 512]
Linear₁
512 → 2048
GELU
[seq, 2048]
Linear₂
2048 → 512
Output
[seq, 512]
d_model
512
d_ff
2048
Expansion
Linear₁ params
1.0M
Linear₂ params
1.0M
Total FFN params
2.1M
Parameter breakdown
Linear₁1.0M (50%)
Linear₂1.0M (50%)
Controls
d_model512
2562048
Expansion ratio4×
GPT-style uses 4×
Activation
FFN is ~2/3 of transformer params. The expansion ratio trades compute for capacity. SwiGLU (Shazeer, 2020) is now standard in LLaMA, PaLM, Gemma.
d_ff rule of thumb:
GPT-2: 4× d_model
LLaMA: 8/3 × d_model (SwiGLU)
Common: 4096 for 7B models

Feed-Forward Network (FFN) Sub-layer - Interactive Visualization

The feed-forward sub-layer in a transformer block is a two-layer MLP applied independently to each token position. It expands token representations from d_model to d_ff (typically 4×) using a nonlinear activation like GELU or SwiGLU, then projects back down. This expansion-contraction pattern stores factual knowledge and accounts for roughly two-thirds of all transformer parameters.

  • Step-through animation of the FFN forward pass: Linear1 → activation → Linear2
  • Configurable d_model (256–2048) and expansion ratio (1×–8×) with live parameter count
  • Compare GELU, ReLU, and SwiGLU activations - including SwiGLU gate mechanism used in LLaMA
  • Parameter breakdown bars showing how FFN dominates total model size

Part of the EngineersOfAI Interactive 3D - free interactive visualizations covering every major concept in machine learning and AI engineering. Hover any element for a plain-English explanation. No code required.