KG-Attention: Knowledge Graph-Guided Attention at Test-Time via Bidirectional Information Aggregation

Songlin Zhai, Guilin Qi, Yuan Meng · OpenReview

Abstract

Knowledge graphs (KGs) are the cornerstone of the semantic web, offering up-to-date representations of real-world entities and relations. Yet large language models (LLMs) remain largely static after pre-training, causing their internal knowledge to become outdated and limiting their utility in time-sensitive web applications. To bridge this gap between dynamic knowledge and static models, a prevalent approach is to enhance LLMs with KGs. However, prevailing methods typically rely on parameter-invasive fine-tuning, which risks catastrophic forgetting and often degrades LLMs' general capabilities. Moreover, their static integration frameworks cannot keep pace with the continuous evolution of real-world KGs, hindering their deployment in dynamic web environments. To bridge this gap, we introduce KGA (Knowledge Graph-guided Attention), a novel framework that dynamically integrates external KGs into LLMs exclusively at inference-time without any parameter modification. Inspired by research on neuroscience, we rewire the self-attention module by innovatively introducing two synergistic pathways: a bottom-up knowledge fusion pathway and a top-down attention guidance pathway. The bottom-up pathway dynamically integrates external knowledge into input representations via input-driven KG fusion, which is akin to the stimulus-driven attention process in the human brain. Complementarily, the top-down pathway aims to assess the contextual relevance of each triple through a goal-directed verification process, thereby suppressing task-irrelevant signals and amplifying knowledge-relevant patterns. By synergistically combining these two pathways, our method supports real-time knowledge fusion. Extensive experiments on four benchmarks verify KGA's strong fusion performance and efficiency.

Introduction

Large Language Models (LLMs) are powerful but essentially "frozen" snapshots of the world at the time of their training. As real-world information evolves, the internal parametric memory of these models becomes outdated. Traditional solutions like fine-tuning are "parameter-invasive," often leading to catastrophic forgetting where the model loses general reasoning skills while acquiring new facts. Alternatively, In-Context Learning (ICL) or Retrieval-Augmented Generation (RAG) avoids parameter changes but introduces significant computational overhead due to the quadratic scaling of self-attention. Furthermore, LLMs often struggle with the "attention sink" phenomenon, failing to utilize information buried in long contexts.

To address these challenges, we propose KGA (Knowledge Graph-guided Attention). KGA is a parameter-preserving framework that rewires the internal self-attention mechanism of LLMs at inference time to dynamically inject structured facts from external Knowledge Graphs (KGs).

Methodology: Bidirectional Information Aggregation

Inspired by the dual-pathway nature of human attention in cognitive neuroscience, KGA introduces two synergistic pathways within the self-attention module:

  1. Bottom-Up Knowledge Fusion: This pathway simulates "stimulus-driven" attention. It uses the LLM’s existing projection matrices ($W^Q, W^K, W^V$) to map KG triples into the same semantic space as the input text. This allows the model to "pull" relevant information from the KG based on the input stimulus.
  2. Top-Down Attention Guidance: This pathway simulates "goal-directed" verification. It evaluates the contextual relevance of each KG triple through a three-stage process: Clue Extraction (scanning text with triples), Consolidation (merging clues), and Relevance Scoring. This results in an adaptive weight ($\alpha_Z$) that amplifies relevant signals and suppresses noise.
The KGA framework architecture showing the bottom-up and top-down pathways.

By combining these pathways, KGA performs adaptive weighted fusion, allowing the model to integrate external knowledge without altering a single parameter.

Experimental Evaluation

We evaluated KGA across four diverse benchmarks to test its reasoning, knowledge updating, and efficiency:

  • SimpleQuestions: Standard KG Question Answering (KGQA).
  • PathQuestion (2-hop): Multi-step reasoning over a graph.
  • ZsRE (Zero-Shot Relation Extraction): Testing consecutive knowledge editing.
  • CounterFact: Testing the model's ability to prioritize new external facts over conflicting internal memories.

Results and Discussion

KGA demonstrates significant improvements over standard ICL baselines across various model architectures, including Llama and Qwen.

Performance comparison of KGA against ICL across various LLMs and benchmarks.

Key Findings:

  • Superior Accuracy: On the PathQuestion task, KGA achieved a Hit@1 of 98.3%, significantly outperforming the ICL baseline of 73.7%.
  • Effective Knowledge Injection: On the CounterFact dataset, KGA improved Llama3-8B’s efficacy from 0.55% to 65.45%, showing a 100-fold increase in the model's ability to adopt new, potentially conflicting facts.
  • Zero Forgetting: In model editing tasks (ZsRE), KGA achieved 100% Locality, meaning it integrated new facts without any degradation of existing knowledge.
  • Computational Efficiency: KGA scales linearly with the number of triples, unlike ICL which scales quadratically. For 100 triples, KGA required ~33 GB of VRAM (close to the base model), while ICL spiked to ~78 GB.

Conclusion

KGA offers a robust, efficient, and parameter-preserving method for integrating dynamic knowledge into LLMs. By mimicking the bidirectional attention processes of the human brain, it enables models to stay current with evolving Knowledge Graphs at test-time, overcoming the limitations of static pre-training and the inefficiencies of standard RAG approaches.

Summary updated

BibTeX
@misc{zhai2025kg,
  author = {Songlin Zhai and Guilin Qi and Yuan Meng},
  title = {{KG-Attention: Knowledge Graph-Guided Attention at Test-Time via Bidirectional Information Aggregation}},
  year = {2025},
  eprint = {2507.08704},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2507.08704},
}
Figure