Transformer Feed-Forward Layers Are Key-Value Memories
Mor Geva, Roei Schuster, Jonathan Berant, Omer Levy
Introduction
Transformer-based language models Vaswani et al. (2017) are at the core of state-of-the-art natural language processing Devlin et al. (2019); Brown et al. (2020), largely due to the success of self-attention. While much literature has been devoted to analyzing the function of self-attention layers Voita et al. (2019); Clark et al. (2019); Vig and Belinkov (2019), they account for only a third of a typical transformer’s parameters ( per layer, where is the model’s hidden dimension). Most of the parameter budget is spent on position-wise feed-forward layers ( per layer), yet their role remains under-explored. What, if so, is the function of feed-forward layers in a transformer language model?
We show that feed-forward layers emulate neural memories Sukhbaatar et al. (2015), where the first parameter matrix in the layer corresponds to keys, and the second parameter matrix to values. Figure 1 shows how the keys (first parameter matrix) interact with the input to produce coefficients, which are then used to compute a weighted sum of the values (second parameter matrix) as the output. While the theoretical similarity between feed-forward layers and key-value memories has previously been suggested by Sukhbaatar et al. (2019), we take this observation one step further, and analyze the “memories” that the feed-forward layers store.
We find that each key correlates with a specific set of human-interpretable input patterns, such as -grams or semantic topics. For example, in Figure 1 is triggered by inputs that describe a period of time and end with “a”. Simultaneously, we observe that each value can induce a distribution over the output vocabulary, and that this distribution correlates with the next-token distribution of the corresponding keys in the upper layers of the model. In the above example, the corresponding value represents a distribution that puts most of its probability mass on the word “while”.
Lastly, we analyze how the language model, as a whole, composes its final prediction from individual memories. We observe that each layer combines hundreds of active memories, creating a distribution that is qualitatively different from each of its component memories’ values. Meanwhile, the residual connection between layers acts as a refinement mechanism, gently tuning the prediction at each layer while retaining most of the residual’s information.
In conclusion, our work sheds light on the function of feed-forward layers in transformer-based language models. We show that feed-forward layers act as pattern detectors over the input across all layers, and that the final output distribution is gradually constructed in a bottom-up fashion.The code for reproducing our experiments is available at https://github.com/mega002/ff-layers/.
Feed-Forward Layers as Unnormalized Key-Value Memories
Neural memory
With matrix notation, we arrive at a more compact formulation:
Feed-forward layers emulate neural memory
Comparing equations 1 and 2 shows that feed-forward layers are almost identical to key-value neural memories; the only difference is that neural memory uses softmax as the non-linearity , while the canonical transformer does not use a normalizing function in the feed-forward layer. The hidden dimension is essentially the number of memories in the layer, and the activation , commonly referred to as the hidden layer, is a vector containing an unnormalized non-negative coefficient for each memory. We refer to each as the memory coefficient of the th memory cell.
Sukhbaatar et al. (2019) make an analogous observation, and incorporate the parameters of the feed-forward layers as persistent memory cells in the self-attention layers. While this reparameterization works in practice, the experiment does not tell us much about the role of feed-forward layers in the canonical transformer. If transformer feed-forward layers are indeed key-value memories, then what memories do they store?
We conjecture that each key vector captures a particular pattern (or set of patterns) in the input sequence (Section 3), and that its corresponding value vector represents the distribution of tokens that follows said pattern (Section 4).
Keys Capture Input Patterns
We posit that the key vectors in feed-forward layers act as pattern detectors over the input sequence, where each individual key vector corresponds to a specific pattern over the input prefix . To test our claim, we analyze the keys of a trained language model’s feed-forward layers. We first retrieve the training examples (prefixes of a sentence) most associated with a given key, that is, the input texts where the memory coefficient is highest. We then ask humans to identify patterns within the retrieved examples. For almost every key in our sample, a small set of well-defined patterns, recognizable by humans, covers most of the examples associated with the key.
We conduct our experiment over the language model of Baevski and Auli (2019), a 16-layer transformer language model trained on WikiText-103 Merity et al. (2017). This model defines and , and has a total of potential keys to analyze. We randomly sample 10 keys per layer (160 in total).
Pattern analysis
We let human experts (NLP graduate students) annotate the top-25 prefixes retrieved for each key, and asked them to (a) identify repetitive patterns that occur in at least 3 prefixes (which would strongly indicate a connection to the key, as this would unlikely happen if sentences were drawn at random) (b) describe each recognized pattern, and (c) classify each recognized pattern as “shallow” (e.g. recurring n-grams) or “semantic” (recurring topic). Each key and its corresponding top-25 prefixes were annotated by one expert. To assure that every pattern is grounded in at least 3 prefixes, we instruct the experts to specify, for each of the top-25 prefixes, which pattern(s) it contains. A prefix may be associated with multiple (shallow or semantic) patterns.
Table 1 shows example patterns. A fully-annotated example of the top-25 prefixes from a single memory key is shown in Appendix A.
2 Results
Experts were able to identify at least one pattern for every key, with an average of 3.6 identified patterns per key. Furthermore, the vast majority of retrieved prefixes (65%-80%) were associated with at least one identified pattern (Figure 2). Thus, the top examples triggering each key share clear patterns that humans can recognize.
Shallow layers detect shallow patterns
Comparing the amount of prefixes associated with shallow patterns and semantic patterns (Figure 2), the lower layers (layers 1-9) are dominated by shallow patterns, often with prefixes that share the last word (e.g. in Table 1). In contrast, the upper layers (layers 10-16) are characterized by more semantic patterns, with prefixes from similar contexts but without clear surface-form similarities (e.g. in Table 1). This observation corroborates recent findings that lower (upper) layers in deep contextualized models encode shallow (semantic) features of the inputs Peters et al. (2018); Jawahar et al. (2019); Liu et al. (2019).
To further test this hypothesis, we sample 1600 random keys (100 keys per layer) and apply local modifications to the top-50 trigger examples of every key. Specifically, we remove either the first, last, or a random token from the input, and measure how this mutation affects the memory coefficient. Figure 3 shows that the model considers the end of an example as more salient than the beginning for predicting the next token. In upper layers, removing the last token has less impact, supporting our conclusion that upper-layer keys are less correlated with shallow patterns.
Values Represent Distributions
Value predictions follow key patterns in upper layers.
Detecting predictive values.
Discussion.
When viewed as distributions over the output vocabulary, values in the upper layers tend to assign higher probability to the next-token of examples triggering the corresponding keys. This suggests that memory cells often store information on how to directly predict the output (the distribution of the next word) from the input (patterns in the prefix). Conversely, the lower layers do not exhibit such clear correlation between the keys’ patterns and the corresponding values’ distributions. A possible explanation is that the lower layers do not operate in the same embedding space, and therefore, projecting values onto the vocabulary using the output embeddings does not produce distributions that follow the trigger examples. However, our results imply that some intermediate layers do operate in the same or similar space to upper layers (exhibiting some agreement), which in itself is non-trivial. We leave further exploration of this phenomenon to future work.
Aggregating Memories
So far, our discussion has been about the function of a single memory cell in feed-forward layers. How does the information from multiple cells in multiple layers aggregate to form a model-wide prediction? We show that every feed-forward layer combines multiple memories to produce a distribution that is qualitatively different from each of its component memories’ value distributions (Section 5.1). These layer-wise distributions are then combined via residual connections in a refinement process, where each feed-forward layer updates the residual’s distribution to finally form the model’s output (Section 5.2).
The feed-forward layer’s output can be defined as the sum of value vectors weighted by their memory coefficients, plus a bias term:
We first measure the fraction of “active” memories (cells with a non-zero coefficient). Figure 7 shows that a typical example triggers hundreds of memories per layer (10%-50% of 4096 dimensions), but the majority of cells remain inactive. Interestingly, the number of active memories drops towards layer 10, which is the same layer in which semantic patterns become more prevalent than shallow patterns, according to expert annotations (see Section 3, Figure 2).
While there are cases where a single memory cell dominates the output of a layer, the majority of outputs are clearly compositional. We count the number of instances where the feed-forward layer’s top prediction is different from all of the memories’ top predictions. Formally, we denote:
as a generic shorthand for the top prediction from the vocabulary distribution induced by the vector , and compute the number of examples where the following condition holds:
Figure 8 shows that, for any layer in the network, the layer’s final prediction is different than every one of the memories’ predictions in at least 68% of the examples. Even in the upper layers, where the memories’ values are more correlated with the output space (Section 4), the layer-level prediction is typically not the result of a single dominant memory cell, but a composition of multiple memories.
We further analyze cases where at least one memory cell agrees with the layer’s prediction, and find that (a) in 60% of the examples the target token is a common stop word in the vocabulary (e.g. “the” or “of”), and (b) in 43% of the cases the input prefix has less than 5 tokens. This suggests that very common patterns in the training data might be “cached” in individual memory cells, and do not require compositionality.
2 Inter-Layer Prediction Refinement
While a single feed-forward layer composes its memories in parallel, a multi-layer model uses the residual connection to sequentially compose predictions to produce the model’s final output:The residual propagates information from previous layers, including the transformer’s self-attention layers.
We hypothesize that the model uses the sequential composition apparatus as a means to refine its prediction from layer to layer, often deciding what the prediction will be at one of the lower layers.
Figure 9 shows that roughly a third of the model’s predictions are determined in the bottom few layers. This number grows rapidly from layer 10 onwards, implying that the majority of “hard” decisions occur before the final layer.
Figure 10 shows a similar trend, but emphasizes that it is not only the top prediction’s identity that is refined as we progress through the layers, it is also the model’s confidence in its decision.
Figure 11 shows the breakdown of different cases per layer. In the vast majority of examples, the residual’s top prediction ends up being the model’s prediction (residual+agreement). In most of these cases, the feed forward layer predicts something different (residual). Perhaps surprisingly, when the residual’s prediction does change (composition+ffn), it rarely changes to the feed-forward layer’s prediction (ffn). Instead, we observe that composing the residual’s distribution with that of the feed-forward layer produces a “compromise” prediction, which is equal to neither (composition). This behavior is similar to the intra-layer composition we observe in Section 5.1. A possible conjecture is that the feed-forward layer acts as an elimination mechanism to “veto” the top prediction in the residual, and thus shifts probability mass towards one of the other candidate predictions in the head of the residual’s distribution.
Finally, we manually analyze 100 random cases of last-layer composition, where the feed-forward layer modifies the residual output in the final layer. We find that in most cases (66 examples), the output changes to a semantically distant word (e.g., “people” “same”) and in the rest of the cases (34 examples), the feed-forward layer’s output shifts the residual prediction to a related word (e.g. “later” “earlier” and “gastric” “stomach”). This suggests that feed-forward layers tune the residual predictions at varying granularity, even in the last layer of the model.
Related Work
Considerable attention has been given to demystifying the operation of neural NLP models. An extensive line of work targeted neuron functionality in general, extracting the properties that neurons and subsets of neurons capture Durrani et al. (2020); Dalvi et al. (2019); Rethmeier et al. (2020); Mu and Andreas (2020); Vig et al. (2020), regardless of the model architecture or neurons’ position in it. Jacovi et al. (2018) analyzed CNN architectures in text classification and showed that they extract key n-grams from the inputs.
The study of the transformer architecture has focused on the role and function of self-attention layers Voita et al. (2019); Clark et al. (2019); Vig and Belinkov (2019) and on inter-layer differences (i.e. lower vs. upper layers) Tenney et al. (2019); Jawahar et al. (2019). Previous work also highlighted the importance of feed-forward layers in transformers Press et al. (2020); Pulugundla et al. (2021); Xu et al. (2020). Still, to date, the role of feed-forward layers remains under-explored.
Also related are interpretability methods that explain predictions Han et al. (2020); Wiegreffe and Pinter (2019), however, our focus is entirely different: we do not interpret individual predictions, but aim to understand the mechanism of transformers.
Characterizing the functionality of memory cells based on examples that trigger maximal activations has been used previously in NLP Rethmeier et al. (2020) and vision Erhan et al. (2009).
Discussion and Conclusion
Understanding how and why transformers work is crucial to many aspects of modern NLP, including model interpretability, data security, and development of better models. Feed-forward layers account for most of a transformer’s parameters, yet little is known about their function in the network.
In this work, we propose that feed-forward layers emulate key-value memories, and provide a set of experiments showing that: (a) keys are correlated with human-interpretable input patterns; (b) values, mostly in the model’s upper layers, induce distributions over the output vocabulary that correlate with the next-token distribution of patterns in the corresponding key; and (c) the model’s output is formed via an aggregation of these distributions, whereby they are first composed to form individual layer outputs, which are then refined throughout the model’s layers using residual connections.
Our findings open important research directions:
Layer embedding space. We observe a correlation between value distributions over the output vocabulary and key patterns, that increases from lower to upper layers (Section 4). Is this because the layer’s output space transforms across layers? If so, how? We note that this possible transformation cannot be explained solely by the function of feed-forward layers: if the model only did a series of key-value look-ups and value-distribution aggregation via weighted addition, then a single, unifying embedding space would appear more natural. Thus, the transformation might have to do with the interplay between feed-forward layers and self-attention layers.
Beyond language modeling. Our formulation of feed-forward networks as key-value memories generalizes to any transformer model, e.g. BERT encoders and neural translation models. We thus expect our qualitative empirical observations to hold across diverse settings, and leave verification of this for future work.
Practical implications. A better understanding of feed-forward layers has many implications in NLP. For example, future studies may offer interpretability methods by automating the pattern-identification process; memory cells might affect training-data privacy as they could facilitate white-box membership inference Nasr et al. (2019); and studying cases where a correct pattern is identified but then suppressed during aggregation may guide architectural novelties.
Thus, by illuminating the role of feed-forward layers, we move towards a better understanding of the inner workings of transformers, and open new research threads on modern NLP models.
Acknowledgements
We thank Shimi Salant and Tal Schuster for helpful feedback. This work was supported in part by the Yandex Initiative for Machine Learning, the Blavatnik Interdisciplinary Cyber Research Center (ICRC), the Alon Scholarship, and Intel Corporation. Roei Schuster is a member of the Check Point Institute of Information Technology. This work was completed in partial fulfillment for the Ph.D degree of Mor Geva.
References
Appendix A Pattern Analysis
Table 3 provides a fully-annotated example of 25 prefixes from the memory cell .
Appendix B Implementation details
In this section, we provide further implementation details for reproducibility of our experiments.
For all our experiments, we used the language model of Baevski and Auli (2019) (247M parameters) trained on WikiText-103 Merity et al. (2017). Specifically, we used the model transformer_lm.wiki103.adaptive trained with the fairseq toolkithttps://github.com/pytorch/fairseq.
WikiText-103https://blog.einstein.ai/the-wikitext-long-term-dependency-language-modeling-dataset/ is a well known language modeling dataset and a collection of over 100M tokens extracted from Wikipedia. We used spaCyhttps://spacy.io/ to split examples into sentences (Section 3).