Feed-Forward Networks as Key-Value Memories: How Transformers Store, Retrieve, and Edit Parametric Knowledge
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.

Deconstructing the FFN as an Associative Memory
A standard two-layer feed-forward network in a transformer block processes a token representation from the residual stream using an intermediate hidden dimension (typically in standard architectures or in gated architectures). The canonical layer transformation is defined as:
where , , and 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 denote the -th column of , and let denote the -th row of . The operation becomes:
where the scalar coefficient is computed as:
In this formulation, the FFN operates as an unnormalized key-value associative memory:
- Keys (): Vector acts as a learned pattern detector in the representation space. The inner product measures the degree to which the current token state matches the trigger condition.
- Memory Activations (): The non-linear function thresholds the match score into an activation coefficient , determining how strongly memory slot fires.
- Values (): Vector acts as the stored memory payload. When triggered, it injects a directional update directly into the residual stream.
- 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 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 define the representation update written to the residual stream. By projecting value vectors into the vocabulary space using the language model's unembedding matrix , the direct effect of an individual memory slot on output token probabilities can be computed:
Research by Geva et al. (2022) and Dar et al. (2023) demonstrated that the top logits in correspond to semantically coherent token clusters. When an activation occurs, the value vector 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 . In algorithms such as ROME (Rank-One Model Editing) and MEMIT (Mass-Editing Memory in a Transformer) (Meng et al., 2022):
- The target key representation corresponding to the subject entity is extracted at the critical middle layer.
- The desired target value that maximizes the probability of the new target token is computed.
- The projection matrix is updated via:
where satisfies , 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):
SwiGLU introduces an explicit gating projection that acts as a multiplicative filter over the input key matrix . 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 independent expert FFNs (Fedus et al., 2022):
Each expert represents a specialized memory bank. The routing gate directs tokens to the top- 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 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.
Sources
- Geva, M., Schuster, R., Berant, J., & Levy, O. (2021). Transformer Feed-Forward Layers Are Key-Value Memories. Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing (EMNLP).
- Geva, M., Caciularu, A., Wang, K. R., & Berant, J. (2022). Transformer Feed-Forward Layers Build Predictions by Promoting Concepts in the Vocabulary Space. Proceedings of the 2022 Conference on Empirical Methods in Natural Language Processing (EMNLP).
- Sukhbaatar, S., Grave, E., Bojanowski, P., & Joulin, A. (2019). Augmenting Self-attention with Persistent Memory. arXiv preprint arXiv:1907.01470.
- Dai, D., Dong, L., Hao, Y., Sui, Z., Chang, B., & Wei, F. (2022). Knowledge Neurons in Pretrained Transformers. Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (ACL).
- Dar, G., Geva, M., Gupta, A., & Berant, J. (2023). Analyzing Transformers in Embedding Space. Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (ACL).
- Meng, K., Bau, D., Andonian, A., & Belinkov, Y. (2022). Locating and Editing Factual Associations in GPT. Advances in Neural Information Processing Systems (NeurIPS).
- Meng, K., Sharma, A. S., Andonian, A., Yonchev, Y., Belinkov, Y., & Bau, D. (2022). Mass-Editing Memory in a Transformer. arXiv preprint arXiv:2210.07229.
- Shazeer, N. (2020). GLU Variants Improve Transformer. arXiv preprint arXiv:2002.05202.
- Fedus, W., Zoph, B., & Shazeer, N. (2022). Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity. Journal of Machine Learning Research (JMLR).
- Song, M., et al. (2024). ProSparse: Introducing and Exploiting Activation Sparsity in Large Language Models. arXiv preprint arXiv:2401.04088.



