Subscribe
AI
7 min read

Feed-Forward Networks as Key-Value Memories: How Transformers Store, Retrieve, and Edit Parametric Knowledge

Mechanistic analysis reveals that feed-forward networks operate as unnormalized, key-value associative memories, enabling precise storage and editing of parametric knowledge in transformer models.

Diagram illustrating Feed-Forward Networks functioning as key-value associative memories within a Transformer architecture
Diagram illustrating Feed-Forward Networks functioning as key-value associative memories within a Transformer architecture

Feed-forward networks in transformers function as unnormalized, key-value associative memories that store factual associations by mapping input keys to output value vectors. This mechanism allows the network to retrieve stored parametric knowledge and enable precise model editing through closed-form weight updates.

In transformer language models, multi-head self-attention and feed-forward networks (FFNs) perform distinct computational roles. While self-attention mechanisms route contextual information dynamically across sequence positions, feed-forward layers account for roughly two-thirds of total model parameters in standard architectures. Despite early interpretations characterizing FFNs simply as generic non-linear projections, mechanistic analysis shows that feed-forward layers operate as unnormalized, key-value associative memories.

Understanding the mechanics of FFN memory retrieval explains how neural networks store factual associations, how representation updates accumulate across network depth, and why model editing methods can rewrite stored facts through closed-form weight updates.

Feed-Forward Networks as Key-Value Memories

Deconstructing the FFN as an Associative Memory

A standard two-layer feed-forward network in a transformer block processes a token representation x∈Rdx \in \mathbb{R}^d from the residual stream using an intermediate hidden dimension dmd_m (typically 4d4d in standard architectures or 83d\frac{8}{3}d in gated architectures). The canonical layer transformation is defined as:

FFN(x)=σ(xWin+bin)Wout+bout\text{FFN}(x) = \sigma(x W_{\text{in}} + b_{\text{in}}) W_{\text{out}} + b_{\text{out}}

where Win∈Rd×dmW_{\text{in}} \in \mathbb{R}^{d \times d_m}, Wout∈Rdm×dW_{\text{out}} \in \mathbb{R}^{d_m \times d}, and σ\sigma is a non-linear activation function such as ReLU, GeLU, or Swish.

As demonstrated by Geva et al. (2021) and Sukhbaatar et al. (2019), this matrix multiplication can be decomposed into an explicit summation over individual parameter vectors. Let ki∈Rdk_i \in \mathbb{R}^d denote the ii-th column of WinW_{\text{in}}, and let vi∈Rdv_i \in \mathbb{R}^d denote the ii-th row of WoutW_{\text{out}}. The operation becomes:

FFN(x)=∑i=1dmmi(x)vi+bout\text{FFN}(x) = \sum_{i=1}^{d_m} m_i(x) v_i + b_{\text{out}}

where the scalar coefficient mi(x)m_i(x) is computed as:

mi(x)=σ(x⊤ki+bi,in)m_i(x) = \sigma(x^\top k_i + b_{i,\text{in}})

In this formulation, the FFN operates as an unnormalized key-value associative memory:

  1. Keys (kik_i): Vector kik_i acts as a learned pattern detector in the representation space. The inner product x⊤kix^\top k_i measures the degree to which the current token state matches the trigger condition.
  2. Memory Activations (mim_i): The non-linear function σ\sigma thresholds the match score into an activation coefficient mi(x)m_i(x), determining how strongly memory slot ii fires.
  3. Values (viv_i): Vector viv_i acts as the stored memory payload. When triggered, it injects a directional update directly into the residual stream.
  4. Aggregation: The layer output is a linear combination of retrieved value vectors weighted by their respective activation coefficients.

Unlike multi-head self-attention, which constructs dynamic keys and values from the input sequence at inference time and normalizes attention weights via softmax across sequence positions, feed-forward layers store static keys and values directly in model weights learned during pre-training.


What Keys and Values Encode

Mechanistic analysis of trained language models reveals distinct semantic structures in the input keys and output values.

Input Keys as Pattern Detectors

Empirical evaluations across models such as GPT-2, RoBERTa, and Llama show that keys kik_i correlate with specific textual, syntactic, and conceptual triggers. Research by Dai et al. (2022) on knowledge neurons and Geva et al. (2021) demonstrates that individual keys respond selectively to:

  • Lexical and syntactic n-grams: Specific prefixes, punctuation structures, or recurring word sequences (e.g., phrases ending with prepositions or numerical constructs).
  • Entity and domain categories: Representations indicating specific entity types, such as programming syntax tokens, geographical entities, or medical terminology.
  • Relational contexts: Prompt configurations that bind a subject to a relation (e.g., "The capital of [Entity] is").

Output Values as Concept and Vocabulary Promoters

While keys detect patterns, value vectors viv_i define the representation update written to the residual stream. By projecting value vectors into the vocabulary space using the language model's unembedding matrix WU∈Rd×∣V∣W_U \in \mathbb{R}^{d \times |V|}, the direct effect of an individual memory slot on output token probabilities can be computed:

E(vi)=viWUE(v_i) = v_i W_U

Research by Geva et al. (2022) and Dar et al. (2023) demonstrated that the top logits in E(vi)E(v_i) correspond to semantically coherent token clusters. When an activation mi(x)>0m_i(x) > 0 occurs, the value vector viv_i directly boosts the probability of specific tokens in vocabulary space, acting as an additive contribution to the model's final token prediction.


Hierarchical Memory Distribution Across Layers

Parametric memory is not distributed uniformly across transformer layers. Instead, language models organize key-value retrieval hierarchically by network depth:

  • Lower Layers (1 to 1/3 depth): Keys trigger on subword n-grams, local syntactic markers, and shallow structural patterns. Value vectors apply baseline corrections to token representations, grounding lexical properties and positional context.
  • Middle Layers (1/3 to 2/3 depth): Keys match specific subject-relation bindings, entity types, and domain topics. Value vectors inject factual attributes and relational knowledge into the residual stream (for example, associating an entity with its attributes or geographic facts).
  • Upper Layers (2/3 to final depth): Keys detect contextual task framing, high-level intent, and constraint states. Value vectors directly push probability mass onto candidate output tokens in the vocabulary space.

Causal Localization and Model Editing

The key-value memory formulation provides the theoretical foundation for model editing and localized weight intervention.

Using causal tracing across autoregressive models, Meng et al. (2022) demonstrated that factual recall tasks (e.g., answering "The Eiffel Tower is located in...") exhibit localized causal states concentrated in the MLP layers at the final token of the subject entity.

Because the MLP functions as a linear associative memory mapping key states to value updates, factual editing can be formulated as a rank-one matrix update to WoutW_{\text{out}}. In algorithms such as ROME (Rank-One Model Editing) and MEMIT (Mass-Editing Memory in a Transformer) (Meng et al., 2022):

  1. The target key representation k∗k_* corresponding to the subject entity is extracted at the critical middle layer.
  2. The desired target value v∗v_* that maximizes the probability of the new target token is computed.
  3. The projection matrix WoutW_{\text{out}} is updated via:

Woutnew=Wout+ΔWW_{\text{out}}^{\text{new}} = W_{\text{out}} + \Delta W where ΔW\Delta W satisfies ΔWk∗=v∗−Woutk∗\Delta W k_* = v_* - W_{\text{out}} k_*, while minimizing interference with previously learned associations using the uncentered covariance matrix of input activations.

This confirms that factual associations reside within the linear weight mappings of feed-forward networks rather than being diffused arbitrarily throughout attention projections.


Modern Extensions: Gating, MoE, and Activation Sparsity

Modern LLM architectures build on the key-value memory paradigm with several structural refinements:

SwiGLU Gating

Architectures such as Llama, Gemma, and Mistral replace standard FFNs with Gated Linear Units (Shazeer, 2020):

FFNSwiGLU(x)=(Swish(xWgate)⊙(xWin))Wout\text{FFN}_{\text{SwiGLU}}(x) = \left( \text{Swish}(x W_{\text{gate}}) \odot (x W_{\text{in}}) \right) W_{\text{out}}

SwiGLU introduces an explicit gating projection WgateW_{\text{gate}} that acts as a multiplicative filter over the input key matrix WinW_{\text{in}}. This dual-projection design tightens the selectivity of memory retrieval, ensuring that memory values are retrieved only when both the gate and the key conditions align.

Mixture-of-Experts (MoE) Memory Scaling

Scaling parametric capacity without increasing per-token compute is achieved by multiplying the number of FFN memory banks. In MoE architectures such as DeepSeek and Mixtral, the single FFN module is replaced by EE independent expert FFNs (Fedus et al., 2022):

MoE(x)=∑e∈TopK(g(x))ge(x)FFNe(x)\text{MoE}(x) = \sum_{e \in \text{TopK}(g(x))} g_e(x) \text{FFN}_e(x)

Each expert FFNe\text{FFN}_e represents a specialized memory bank. The routing gate g(x)g(x) directs tokens to the top-kk relevant memory partitions, scaling total available key-value parameters to hundreds of billions while activating only a sparse subset during inference.

Natural Activation Sparsity

Empirical profiling of transformer FFNs indicates substantial activation sparsity during standard generation. Across ReLU and thresholded GeLU/SwiGLU models, typically between 80% and 90% of intermediate activations mi(x)m_i(x) evaluate to zero or near-zero for any individual token (Song et al., 2024).

This property enables dynamic neuron pruning, activation caching, and speculative memory loading in specialized inference engines, demonstrating that transformer generation relies on sparse lookups from a massive dictionary of parameter-stored knowledge.


Update (Oct 19, 2026)

Corrections: Analyzing Transformers in Embedding Space (Dar et al.) now links to arXiv:2209.02535, and ProSparse (Song et al., 2024) now links to arXiv:2402.13516. Both previously pointed to unrelated papers.

Sources

Frequently asked questions

How do feed-forward networks store information?

They operate as unnormalized key-value associative memories where input keys act as pattern detectors and output values serve as stored memory payloads that are injected into the residual stream.

What is the role of keys and values in FFNs?

Keys measure how well the current token state matches trigger conditions, while values define the representation update written to the residual stream to boost specific token probabilities.

How can factual knowledge be edited in LLMs?

Factual editing can be formulated as a rank-one matrix update to the W_out projection matrix, as demonstrated in algorithms like ROME and MEMIT.

What is activation sparsity in transformer FFNs?

Typically between 80% and 90% of intermediate activations evaluate to zero or near-zero, enabling dynamic neuron pruning and sparse lookups during inference.

AILLMArchitectureMechanistic InterpretabilityTransformersExplainers