#233The 4× MLP hidden dimensionEasyTransformersNeural NetworksLLMs
The 4× MLP hidden dimension
Background
GPT-2's feed-forward block widens the embedding to a hidden dimension 4× larger, applies a non-linearity, then projects back:
The 4× expansion (d_ff = 4·C) has been the default since the 2017 Transformer — wide enough for rich per-token feature mixing, not so wide it wastes parameters.
Problem statement
Implement mlp_hidden_dim(n_embd, expansion=4) returning the hidden width.
Input
n_embd— embedding dimC.expansion— width multiplier (default4).
Output
Returns an int: expansion * n_embd.
Examples
Example 1 — GPT-2 small
Input: n_embd = 768
Output: 3072
Example 2 — 8× variant
Input: n_embd = 512, expansion = 8
Output: 4096
Constraints
- Return
expansion * n_embd. - Default
expansion = 4.
Notes
- The hidden layer projects onto many feature directions before the non-linearity recombines them — the per-token "compute" tier attention can't provide.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example GPT-2 small 768 -> 3072
- •Reference explicit 8x on 512 -> 4096
- •Sample default multiplier 100 -> 400