Attention Head Embeddings with Trainable Deep Kernels for Hallucination Detection in LLMs

Alexey Zaytsev, Rodion Oblovatny, Alexandra Bazarova, Alexey Zaytsev 0002 · DOI

TL;DR

This paper introduces a novel method for detecting hallucinations in Large Language Models (LLMs), particularly in Retrieval-Augmented Generation (RAG) systems. By analyzing the statistical divergence between prompt and response hidden states using Maximum Mean Discrepancy (MMD), the approach achieves state-of-the-art performance and offers a fully unsupervised solution.

What Problem Does This Paper Address?

Large Language Models (LLMs) are increasingly used in various applications, but they often generate "hallucinations" – fabricated or incorrect content. This is especially problematic in Retrieval-Augmented Generation (RAG) systems, where LLMs rely on retrieved information. Existing hallucination detection methods often overlook the RAG context or analyze hallucinations in isolation. This paper addresses the critical need for effective hallucination detection in RAG settings by proposing a method that explicitly analyzes the relationship between the LLM's generation and its provided context. The authors hypothesize that hallucinated responses will exhibit greater deviations in the LLM's internal "understanding" (represented by hidden states) compared to grounded responses.

Methods

The core of the proposed method lies in analyzing the probabilistic distances between the hidden state distributions of prompt tokens and response tokens. Specifically, they employ Maximum Mean Discrepancy (MMD), a statistical measure that quantifies the difference between two probability distributions.

Key novelties include:

  • Attention Head Embeddings: Instead of using standard layer-wise embeddings, the method extracts hidden states from specific attention heads. Ablation studies show this provides finer granularity and improves detection quality, as factuality signals are less "blurred" at the head level.
  • Wild Bootstrap for Null Distribution: To obtain reliable p-values for the MMD statistic, the authors use a wild bootstrap procedure. This method is chosen over label shuffling because it better accounts for the sequential and autoregressive nature of text token embeddings.
  • Unsupervised Hallucination Score: The method constructs a hallucination score based on the p-value derived from the MMD statistic and wild bootstrap. This score is normalized between 0 and 1, making it comparable across different sequence lengths and enabling an unsupervised approach.
  • Head Selection: A procedure is introduced to select the most discriminative attention heads based on their "separation capability" on an annotated probe dataset, similar to prior work but adapted for MMD.

Datasets

The proposed approach was evaluated on four datasets:

  • RAGTruth: Contains manually annotated answers for LLMs in RAG settings, with subsets for Question Answering (QA) and Summarization (Summary). This dataset is noted for its longer contexts and responses, making it representative of real-world applications.
  • CoQA: A conversational question answering dataset.
  • SQuAD: A question answering dataset.
  • XSum: A dataset for abstractive summarization.

The CoQA and SQuAD datasets were used in versions where answers were generated by an LLM and then annotated using GPT-4.

Key Insights and Ideas

  • Hidden states encode factuality: The paper strongly supports the idea that the internal representations (hidden states) of LLMs contain discriminative patterns for hallucinations.
  • Geometric structure matters: Analyzing the "geometric structure" of token hidden states, specifically their distributional divergence, is a powerful way to detect factual inconsistencies.
  • Attention heads are key: Focusing on individual attention heads, rather than aggregated layer embeddings, provides a more precise signal for hallucination detection. This suggests that specialized heads capture specific linguistic or semantic patterns crucial for factuality.
  • NLI and Hallucination Detection are related: The task of hallucination detection in RAG is shown to be similar to Natural Language Inference (NLI), where a contradiction arises. This similarity allows for transferability and enables an unsupervised approach by leveraging NLI datasets for head selection.
  • Bootstrap is crucial for robustness: The wild bootstrap method for approximating the null distribution of MMD is shown to be superior to simply using the raw MMD statistic, leading to more stable and normalized scores.

Key Results

  • The proposed MMD-based method achieves state-of-the-art or competitive performance across various datasets and LLMs, often outperforming existing methods, especially on larger models.
  • For instance, on the SQuAD dataset, the head-level MMD approach achieved ROC AUC scores of 0.93±0.01 for Mistral-7B and 0.94±0.02 for LLaMA-2-13B, which are among the highest reported.
  • The method demonstrates strong and stable correlation with NLI scores, confirming its ability to detect deviations from given premises.
  • The approach shows good transferability from NLI tasks to hallucination detection, enabling its use even when labeled hallucination data is unavailable.
  • The head-wise embedding approach consistently outperforms layer-wise embeddings, with significant gains on datasets like SQuAD.

Limitations and Future Work

  • Dataset Selection: The paper notes that the choice of NLI dataset for head selection can impact results, suggesting that domain-specific NLI datasets might be beneficial for certain hallucination detection tasks. Further research is needed to explore optimal dataset selection.
  • Probe Set Size: While the method is shown to be robust to probe set size, the optimal size might still vary.
  • Number of Heads: The study indicates that the method's performance is not highly sensitive to the exact number of heads (Nmax), but a balance is needed to avoid signal noise or insufficient robustness.
  • Future Work: The authors suggest exploring optimal NLI dataset selection for specific domains and further investigating the relationship between NLI and hallucination detection. The paper also implies that further research into the geometric properties of LLM hidden states could yield more insights.

Summary updated

BibTeX
@misc{zaytsev2025attention,
  author = {Alexey Zaytsev and Rodion Oblovatny and Alexandra Bazarova and Alexey Zaytsev},
  title = {{Attention Head Embeddings with Trainable Deep Kernels for Hallucination Detection in LLMs}},
  year = {2025},
  eprint = {2506.09886},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2506.09886},
}
Figure