Context-Aware Decoding: Forcing LLMs to Trust Evidence Over Intuition
Trusting Your Evidence: Hallucinate Less with Context-aware Decoding
This paper introduces Context-Aware Decoding (CAD), a training-free inference-time method that enhances LLM faithfulness by contrasting output distributions with and without context. CAD achieves state-of-the-art results on summarization (e.g., +14.3% factuality for LLaMA-30B) and significantly mitigates hallucinations.
TL;DR
Language Models are often "stubborn"—they prefer what they learned during pretraining (prior knowledge) over what you tell them right now (context). Context-Aware Decoding (CAD) is a simple, training-free inference trick that uses contrastive logit math to suppress the model's internal biases, resulting in up to 14.3% better factuality and a 2.9x improvement in resolving knowledge conflicts.
The Motivation: The "Internal Memory" Bug
We've all seen LLMs hallucinate. Often, this happens because the model is too "smart" for its own good; its pretraining data is so deeply ingrained that it ignores the provided text.
For example, if you provide a document saying Argentina won 3 World Cups but the model was trained when they had only won 2, the model will likely output "Two." This "Prior Knowledge Dominance" makes LLMs unreliable for tasks like summarization or Retrieval-Augmented Generation (RAG) where the context is the only truth.
Methodology: Subtracting the Bias
The authors propose Context-Aware Decoding (CAD). The intuition is elegant: if we want a model to focus on the context, we should look for tokens that are much more likely when the context is present than when it is absent.
Mathematically, CAD modifies the decoding distribution as follows:
- : The standard probability given context () and query ().
- : The probability given only the query (the model's "blind" guess based on its priors).
- : A hyperparameter (the "trust" level) that determines how much we want to penalize the model's internal prior.
By subtracting the context-free logits from the context-aware ones, the model effectively filters out its internal "prejudices."
Fig 1: CAD amplifies the difference between output probabilities with and without context, ensuring the model picks the evidence-backed answer.
Experiments: Slaying the Hallucination
CAD was tested against standard decoding on datasets including CNN-DM (summarization) and NQ-Swap (knowledge conflicts).
1. Summarization Factuality
In summarization, CAD doesn't just improve the flow; it drastically improves Factuality Metrics (FactKB and BERT-Precision). For LLaMA-30B, ROUGE-L jumped by 21%, showing that the summaries became both more descriptive and more accurate to the source.
2. Knowledge Conflicts
The results on NQ-Swap are staggering. In this dataset, gold answers are swapped with random entities. Standard models fail because they "remember" the original answer. CAD allows the model to "forget" what it knows and read what is on the page, leading to a nearly 3x performance boost.
Table 1: Performance gains across diverse model families (OPT, GPT-Neo, LLaMA, FLAN-T5).
Deep Insight: Scale Makes Models More Stubborn
A fascinating takeaway from the paper is that larger models actually benefit more from CAD. As models grow in size, they store more "facts" in their weights, which makes them more likely to ignore context. CAD scales effectively with these larger models (as seen in the OPT 13B vs 30B comparisons), making it a vital tool for the next generation of LLMs.
Fig 2: Performance remains robust across different adjustment levels (), with serving as a reliable default.
Conclusion & Limitations
CAD is a powerful, zero-cost (aside from a double-pass inference) method to fix one of the most annoying behaviors of LLMs.
Limitations:
- Compute Overhead: Since it requires computing logits twice (once with context, once without), inference time essentially doubles.
- Probability Access: It requires access to the model's raw logits, meaning it cannot be used with "Black Box" APIs like GPT-4 unless the provider exposes log-probabilities.
However, for developers running local models or hosting their own inference servers, CAD is an essential addition to the RAG pipeline to ensure that "Evidence" always beats "Hallucination."
Takeaway: In the battle between a model's memory and your data, CAD gives your data the upper hand.
