Uncertainty-Aware Contrastive Decoding (2025)

Abstract: Large language models excel in a wide range of natural language processing tasks, but generating factually accurate and consistent outputs remains a challenge. To improve text reliability, Contrastive Decoding (CD) refines token selection by leveraging differences between an expert and base model, penalizing low-quality token choices. However, CD employs static weighting between models, making it sensitive to variations in model architecture and input characteristics, often resulting in suboptimal token selection and error propagation throughout generation. We propose Uncertainty-Aware Contrastive Decoding (UCD), a method that dynamically adjusts model contributions at each decoding step based on uncertainty. We introduce a cumulative energy function, where uncertainty is quantified as the negative log-sum-exp over logits, and decomposed into entropy and expected logit components. This energy serves as a dynamic confidence signal, guiding adaptive model weighting during generation. We demonstrate through extensive experiments that UCD significantly improves factual accuracy and reliability over existing decoding methods. Finally, we provide a theoretical analysis showing that our energy function serves as a well-defined uncertainty metric capturing model confidence. Our code is available at: https://github.com/MLAI-Yonsei/UCD.

## introduction large language models demonstrated remarkable performance across diverse tasks, ranging from open-ended text generation to structured reasoning @xcite . their ability to generate human-like text and generalize across domains has led to widespread adoption. however, reliability remains a persistant challenge, as llms are prone to repetition, hallucination and * both authors contributed equally to this research. † corresponding author factual inconsistencies @xcite . standard decoding strategies, such as greedy decoding and sampling, struggle to balance coherence, diversity and factuality, often producing deterministic yet incorrect generations @xcite . recent research suggests that relying on a single llm is inherently limiting, as no single model can fully capture the diverse reasoning strategies, domain knowledge, and stylistic variations required for robust text generation @xcite . to address this limitation, multi-llm collaboration techniques such as contrastive objectives and ensemble methods have been explored to leverage the strengths of different models @xcite . one prominent approach, contrastive decoding (cd), improves text generation without additional training by refining token selection at inference time @xcite . the key insight is that smaller models amplify undesirable behaviors, such as repetition and fluency errors, whereas larger models, despite their strengths, still exhibit hallucinations and factual inconsistencies. by contrasting their predictions, cd filters out low-quality outputs, leading to improved fluency and coherence. building on this foundation, several works have been proposed to enhance the effectiveness of cd. @xcite improve adaptation by using a three-model setup (large lm, small lm, and fine-tuned small lm) to simulate fine-tuning effects on the large lm without directly updating its parameters. other works @xcite apply contrastive objective to hallucination-induced models to further enhance factual accuracy. despite these advancements, existing methods rely on static weighting schemes or heuristic search to determine model contributions. however, the optimal balance between models should be dynamic, adapting to the selected models, task requirements, and dataset characteristics. for instance, a fine-tuned small model may be more reliable for domain-specific tasks warranting a higher weighting, whereas a pretrained large model excels in open-ended reasoning task, where it should contribute more. few prior works has explored dynamic weighting to address these issues. fan et al. introduce kl divergence-based weighting, adjusting model contributions per decoding step by comparing their output distributions. however, this method assumes a fixed distance between pretrained and fine-tuned models across different model sizes, oversimplifying model interactions. @xcite optimize model contributions using perplexity, but focus on ensembling models rather than applying contrastive weighting. moreover, both approaches rely on a search-based optimization process at each step, increasing computational overhead. also they do not fully account for the sequential nature of uncertainty, neglecting cumulative effects that influence model reliability over time. in this paper, we propose uncertainty-aware contrastive decoding (ucd), a framework that applies energy-based uncertainty estimation to cd. ucd models uncertainty sequentially using a cumulative energy function, allowing dynamic reweighting of model contributions at each step without iterative optimization, ensuring efficient and adaptive token selection. as illustrated in figure 1, ucd's dynamic weighting accurately predicts the correct answer "august 2, 1776" for a historical question, while cd (o'brien and lewis, 2023) fails due to static weighting. we theoretically validate our energy function through lse decomposition, demonstrating its role as an uncertainty metric for sequential decoding. by modeling uncertainty accumulation, ucd enhances decoding stability and robustness. extensive experiments on hallucination datasets and reasoning benchmarks confirm that ucd consistently outperforms existing methods, achieving higher reliability and factual accuracy. ## experimental setup we conduct three experiments to evaluate our methodology. first, we assess the model's ability to mitigate hallucinations by measuring the truthfulness of its outputs. second, we evaluate its robustness across diverse reasoning benchmarks to demonstrate generalization capability. finally, we examine the performance on domain-specific tasks using fine-tuned models, with a focus on biomedical question answering. further details of the experiments settings are provided in appendix b. ## conclusion we propose uncertainty-aware contrastive decoding (ucd), a framework that introduces cumulative energy as a dynamic uncertainty metric to balance contributions of multiple language models. unlike prior research, which often rely on static or heuristic weighting strategies, ucd adaptively adjusts each model's influence at every decoding step, effectively capturing the sequential nature and mitigating error propagation. empirical results demonstrate that ucd consistently improves factual accuracy and reliability over existing methods across various benchmarks, including domainspecific tasks. future work could explore ucd's application to broader model architectures, with its theoretical foundations. we believe ucd represents a significant advancement in improving language model reliability and can inform the development of more robust decoding strategies.