Transformer Feed-Forward Networks as Key-Value Memories: How First-Layer Keys and Second-Layer Values Store Knowledge
In modern autoregressive Transformers, the division of labor between attention heads and multi-layer perceptron (MLP) blocks is often summarized through a clean functional split: attention routes information across sequence positions, while feed-forward networks (FFNs) process information per position. Yet for years, the exact mechanism by which FFNs process that information remained a black box.
Foundational interpretability research by Geva et al. (2021) demonstrated that Transformer feed-forward networks operate mathematically as unnormalized associative key-value memories. Under this formulation:
- The first linear layer acts as a bank of pattern-matching keys () that detect textual, syntactic, or semantic triggers in the input representation.
- The non-linear activation function serves as a thresholding and routing filter.
- The second linear layer acts as a bank of values () whose corresponding vectors are retrieved, weighted, and written directly into the model's residual stream.
Understanding this key-value memory formulation explains why FFNs account for roughly two-thirds of a Transformer's total non-embedding parameter budget, how factual knowledge is localized across layers, and how techniques like model editing and Mixture of Experts scale LLM capacity.
The Mathematical Isomorphism: FFN as Associative Lookup
In a standard Transformer block, the feed-forward sublayer processes an incoming hidden vector from the residual stream. The standard two-layer MLP computation is defined as:
where , , , , and is a non-linear activation function (such as GELU or ReLU). The intermediate dimension is typically set to in standard architectures (or in gated variants such as SwiGLU).

To see the key-value memory structure, let:
- denote the -th column of (the -th key vector, for ).
- denote the -th row of (the -th value vector).
- denote the -th scalar bias in .
Expanding the matrix multiplication into vector operations yields:
This formulation matches the mathematical definition of an associative key-value memory:
- Key Matching (Addressing): The inner product measures the similarity between the current hidden state and the -th key vector .
- Activation Filtering: The scalar activation determines how strongly memory slot fires.
- Value Retrieval and Superposition: The output is a linear combination of value vectors , where each value vector is injected in proportion to its activation intensity.
Unlike attention-based key-value retrieval, which normalizes coefficients via softmax across tokens in a context window, the FFN memory operates independently per token across discrete memory slots using elementwise activations.
Keys as Pattern Detectors, Values as Vocabulary Concepts
What do individual keys and values actually represent? Subsequent mechanistic work by Geva et al. (2022) and Dai et al. (2022) systematically analyzed the inputs that activate specific neurons and the output distributions induced by their corresponding value vectors.
Keys as Trigger Patterns
By tracking which text prefixes produce the largest inner product , researchers found that individual keys act as specialized pattern detectors:
- Shallow Patterns: Keys in early layers trigger on specific n-grams, capitalization, punctuation formats, or morphological suffixes (such as words ending in "-ing" or capitalized acronyms).
- Semantic Classes: Keys in middle layers trigger on conceptual categories, such as mentions of programming languages, geographical locations, or corporate entities.
- Relational Contexts: Keys in upper-middle layers trigger on specific relational prompts (such as "The capital of [Country] is" or "authored by").
Values as Vocabulary Directions
Because each value vector lives in the same hidden space as the residual stream, it can be directly analyzed by projecting it through the language model's unembedding matrix (often termed the Logit Lens):
When projected into vocabulary space, individual value vectors place high probability mass on a small, semantically coherent cluster of tokens. For instance:
- A key that activates on phrases describing the Eiffel Tower or the Louvre pairs with a value vector that directly boosts the logits for "Paris", "France", and "French".
- A key that detects code definitions pairs with a value vector that promotes programming syntax tokens such as
return,def, orimport.
Feed-forward layers function by querying these memory slots and adding their output directly to the residual stream:
Each layer sequentially modifies the token distribution by promoting or suppressing specific vocabulary candidates.
Layerwise Specialization: The Three-Tier Memory Hierarchy
Across a multi-layer Transformer, the functional role of key-value memories evolves systematically from lower to upper layers:
| Layer Tier | Representation Level | Key Triggers () | Value Vector Output () | | :--- | :--- | :--- | :--- | | Lower Layers (0 to 30%) | Surface & Syntax | Token prefixes, casing, syntax markers, subword concatenations | Syntactic continuations, morphological completions | | Middle Layers (30 to 70%) | Semantics & Knowledge | Entity types, subject-relation tuples, relational contexts | Factual attributes, related entity names, topical terms | | Upper Layers (70 to 100%) | Prediction & Distribution | Task-specific goals, next-token formatting context | Exact next-token candidates, probability mass sharpening |
In research exploring knowledge localization, Meng et al. (2022) demonstrated via causal tracing that factual recall (such as determining the country associated with a landmark) originates in the early-to-middle MLP layers at the subject token position, which is then routed to the final sequence position by late attention layers.
The Parameter Budget and SwiGLU Gating
Feed-forward layers represent the largest parameter footprint in standard language models. In a vanilla Transformer with hidden dimension and :
- Self-Attention Sublayer: account for parameters.
- FFN Sublayer: and account for parameters.
Thus, FFN blocks consume of total transformer layer weights (excluding embeddings).
Standard FFN (2-Layer MLP):
Input x ────────────────────────┬───────────────────────────┐
│ │
▼ │
[ Key Matrix W₁ ] │
│ (x · k_i) │
▼ │
[ Activation σ ] │
│ (m_i) │
▼ │
[ Value Matrix W₂ ] │
│ (Σ m_i · v_i) │
▼ │
( + ) ◄──────────────────────┘ (Residual Stream)
│
OutputSwiGLU Gating Architecture
Modern architectures (such as LLaMA, Mistral, and Qwen) replace the two-matrix MLP with SwiGLU (Shazeer, 2020), which introduces an explicit multiplicative gating branch:
To maintain a comparable parameter budget of , is scaled down to approximately :
Under the key-value interpretation, the gating branch provides a dynamic continuous gate that scales the projection before the linear value recombination in , increasing memory capacity per parameter without increasing inference FLOPs.
Practical Applications: Model Editing and Mixture of Experts
Viewing FFN layers as associative memories is not merely an interpretability framework; it has enabled practical architectural and operational capabilities:
1. Rank-One Model Editing (ROME) and MEMIT
Because the second linear layer serves as a linear value lookup matrix satisfying , researchers can treat factual editing as a constrained linear algebra problem. Meng et al. (2022) and Meng et al. (2023) demonstrated that injecting a new fact (or updating an existing one) can be executed in closed form using a rank-one weight update:
where $k^$ is the key vector for the target subject entity, $v^$ is the desired value vector corresponding to the target property, and is a covariance matrix of unperturbed key activations that prevents catastrophic forgetting.
2. Mixture of Experts (MoE)
The key-value memory perspective explains why Mixture of Experts architectures (such as Mixtral, DeepSeek-V3, and Grok) scale model capacity by replicating FFN layers while keeping attention layers shared:
- FFN parameters store static knowledge and associative facts.
- Attention parameters govern dynamic contextual routing.
By partitioning the FFN key-value memory banks into discrete routed experts (), models can expand total factual memory capacity to hundreds of billions of parameters while only activating top- memory banks per token, keeping active inference latency and FLOPs constant.
Sources
- Geva et al. (2021): Transformer Feed-Forward Layers Are Key-Value Memories
- Geva et al. (2022): Transformer Feed-Forward Layers Build Predictions by Promoting Concepts in the Vocabulary
- Dai et al. (2022): Knowledge Neurons in Pretrained Transformers
- Meng et al. (2022): Locating and Editing Factual Associations in GPT (ROME)
- Meng et al. (2023): Mass-Editing Memory in a Transformer (MEMIT)
- Shazeer (2020): GLU Variants Improve Transformer



