Kformer: Knowledge Injection in Transformer Feed-Forward Layers

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

Introduction

Pre-trained Language Models (PTMs) like RoBERTa excel at many NLP tasks but often falter when faced with "knowledge-driven" questions requiring specific factual or commonsense reasoning. Traditional methods inject knowledge by concatenating retrieved text to the input, but this is limited by sequence length constraints and ignores the architectural reality that factual knowledge is primarily stored in Feed-Forward Networks (FFNs), not self-attention layers. Kformer addresses this by treating FFNs as "knowledge neurons" and injecting external information directly into these layers.

The Kformer Architecture

The Kformer framework operates in three stages: retrieval, embedding, and injection. After retrieving relevant facts using a hybrid BM25 and dense retrieval approach, these facts are embedded into the model's latent space. Interestingly, the authors initialize these embeddings using the PTM’s own embedding layer to ensure compatibility.

The core innovation lies in the Knowledge Injection phase. Instead of modifying the input sequence, Kformer expands the FFN's linear transformations. It projects retrieved knowledge into new "key" and "value" vectors, which are then concatenated to the existing FFN weight matrices. This effectively adds specialized neurons that activate when the input hidden state aligns with the injected external facts.

The Kformer architecture expands the Feed-Forward Network (FFN) by projecting external knowledge into additional key-value neurons within the fusion layer.

Experimental Evaluation

Kformer was evaluated on two distinct benchmarks:

  1. SocialIQA: Focused on commonsense reasoning using the ATOMIC knowledge graph.
  2. MedQA-USMLE: A medical exam dataset utilizing standard medical textbooks as the knowledge source.

Results and Key Insights

Kformer consistently outperformed standard RoBERTa baselines and other sophisticated fusion techniques like MCQueen and attention-based (ATT) methods. For instance, on the MedQA-USMLE dataset, Kformer achieved 30.01% accuracy, significantly higher than the base RoBERTa (27.41%). On the SocialIQA dataset, the Large model variant reached a test accuracy of 78.68%.

Performance comparison on the SocialIQA dataset, showing Kformer's superiority in both Base and Large model configurations.

Beyond raw accuracy, several key insights emerged:

  • The "Goldilocks Zone": Performance peaks at a specific number of retrieved facts (typically around 15-20). Beyond this point, "knowledge noise" begins to degrade the model's reasoning capabilities.
  • Layer Sensitivity: Knowledge injection is most effective in the top layers (near the output) or bottom layers (near the input), with the top layers showing the greatest benefit for semantic integration.
  • Efficiency and Interpretability: By baking knowledge into the FFN weights, Kformer bypasses the 512-token limit of standard Transformers. Furthermore, the model's decisions can be interpreted by visualizing which "knowledge neurons" fire during a specific query.

Conclusion

Kformer demonstrates that aligning knowledge injection with the internal "memory" structure of Transformers—the FFN—is more effective than simple input concatenation. Future work may involve integrating this FFN-based fusion during the pre-training phase to build inherently more knowledgeable models from the ground up.

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