Kformer: Knowledge Injection in Transformer Feed-Forward Layers

Yunzhi Yao, Shaohan Huang, Li Dong, Furu Wei, Huajun Chen, Ningyu Zhang

TL;DR

Kformer is a novel knowledge injection framework that infuses external information directly into the Feed-Forward Network (FFN) layers of a Transformer. By treating FFNs as "knowledge neurons," Kformer outperforms traditional methods like input concatenation, particularly on knowledge-intensive tasks like medical exams and commonsense reasoning.

What Problem Does This Paper Address?

Pre-trained Language Models (PLMs) like RoBERTa are incredibly powerful, but they have a "knowledge gap." While they store a massive amount of implicit information in their parameters, they often struggle with tasks requiring specific, external factual knowledge—such as answering complex medical questions or navigating nuanced social common sense.

The standard way to fix this is "knowledge injection." Most existing methods either concatenate the knowledge to the input text (which is limited by sequence length) or use complex attention mechanisms to fuse representations. However, recent research suggests that the Transformer's Feed-Forward Network (FFN) layers actually act as "key-value memories" or "knowledge neurons" that store factual information. The authors of Kformer argue that if we want to give a model new knowledge, we should put it exactly where the model already stores its own: in the FFN.

Methods

Kformer follows a three-step pipeline to augment a standard Transformer:

  1. Knowledge Retrieval: For a given question, the system uses a hybrid retrieval approach. It first performs a sparse search (BM25) to find potential candidates and then refines these using dense retrieval to select the top $N$ most relevant knowledge snippets.
  2. Knowledge Embedding: Each retrieved snippet is converted into a dense vector by averaging the embeddings of its constituent tokens.
  3. Knowledge Injection: This is the core innovation. In a standard Transformer, the FFN layer performs two linear transformations. Kformer expands these layers. It projects the external knowledge vectors into the model's hidden space and concatenates them to the existing weight matrices of the FFN.

As shown in the architecture diagram below, the final output of the FFN becomes a sum of the original model's output and the new "knowledge fusion" output.

![Fig.

  1. An illustration of KFormer for knowledge injection.](/figures/art_b0a4326f7a404942a52dc22bc091fdcd)

Datasets

The authors evaluated Kformer on two challenging multiple-choice benchmarks:

  • SocialIQA: A commonsense reasoning task. The knowledge source used was ATOMIC, a large-scale graph of social interactions, with triples converted into natural language sentences.
  • MedQA-USMLE: Questions from medical board exams in the US. The knowledge source consisted of standard medical textbooks.
Table 1. Overall statistics of the two datasets. ‘Know len.’ means the length of knowledge text we extract from the knowledge Graph and TextBook. The question len. of the Social IQA dataset contains the length of the context.

Key Insights and Ideas

  • FFNs are Factual Hubs: The success of Kformer reinforces the theory that FFN layers are the primary site for factual storage in Transformers. Injecting here is more "natural" for the architecture than forcing the model to attend to long, concatenated strings at the input layer.
  • Location Matters: The authors experimented with injecting knowledge into different layers. They found that the top layers (10-12) generally provide the best performance. This aligns with the idea that higher layers capture more complex semantic information. Interestingly, injecting into the middle layers (4-6) actually degraded performance in some cases.
  • The "Goldilocks" Zone of Retrieval: More knowledge isn't always better. As seen in Figure 2, performance peaks at a certain number of retrieved candidates (Top 15 for SocialIQA, Top 20 for MedQA) before declining. Too much information introduces "knowledge noise," which confuses the model.
  • Interpretability: By looking at the activation function's output, the authors could see which specific pieces of injected knowledge the model "clicked on" to answer a question. For example, when asked about a divorce, the model successfully activated knowledge snippets about "finding a new place to live" and "moving on."

Key Results

Kformer consistently outperformed both the base RoBERTa model and other common injection techniques:

  • MedQA-USMLE: Kformer achieved a test accuracy of 30.01%, significantly higher than the standard RoBERTa (27.41%) and even beating the concatenation method (29.77%).
  • SocialIQA: The Kformer-Large model reached 78.68% test accuracy, outperforming other specialized models like RoBERTa+MCQueen.
Table 3. Results of the MedQA-USMLE task.

Limitations and Future Work

While Kformer is effective, the authors note a few areas for improvement:

  • Pre-training Integration: Currently, Kformer is applied during the fine-tuning stage. The authors suggest that integrating this fusion method during the initial pre-training phase could lead to even more robust knowledge representations.
  • Layer Dynamics: The reason why intermediate layers react poorly to injection remains a bit of a mystery and requires further research into the fundamental mechanisms of how Transformers store different types of information across layers.
  • Scaling: While the model is "pluggable," the computational overhead of projecting and concatenating very large numbers of knowledge vectors needs to be managed for production-scale environments.

Summary updated

BibTeX
@inproceedings{yao2022kformer,
  author = {Yunzhi Yao and Shaohan Huang and Li Dong and Furu Wei and Huajun Chen and Ningyu Zhang},
  title = {{Kformer: Knowledge Injection in Transformer Feed-Forward Layers}},
  booktitle = {NLPCC},
  year = {2022},
  doi = {10.1007/978-3-031-17120-8\_11},
}
Figure