TL;DR
Standard decoding strategies for code generation treat all tokens uniformly, often leading to irreversible logic errors at critical decision points. This paper introduces AdaDec, an adaptive decoding framework that uses Shannon entropy to dynamically detect high-uncertainty steps and trigger a lookahead-based reranking mechanism. By selectively pausing and evaluating future trajectories only when needed, AdaDec significantly boosts code generation accuracy while maintaining high computational efficiency.
What Problem Does This Paper Address?
When Large Language Models (LLMs) generate code, they do so token by token. Standard decoding strategies like greedy decoding always pick the most probable next token. However, code generation is highly sensitive to critical decision points (e.g., choosing between a for loop or a return statement at the start of a line). A single misstep here causes "logic drift," where the model completely diverges into an incorrect program structure.
Prior attempts to fix this have relied on coarse heuristics, such as increasing sampling temperature or triggering reasoning chains exclusively at the beginning of lines (e.g., AdapT or UnCert-CoT). The authors find two major gaps in these approaches:
- In-line errors are ignored: Their empirical study reveals that 13.04% of logic drifts actually occur within lines, meaning line-initial heuristics miss a significant portion of errors.
- Static thresholds fail: Different models exhibit unique uncertainty profiles, making a universal, fixed uncertainty threshold highly suboptimal.
Methods
AdaDec introduces a token-level pause-then-rerank mechanism driven dynamically by token uncertainty. The framework operates in two main phases:
- Entropy-based Pause Checking: At each decoding step $t$, AdaDec calculates the Shannon entropy of the model's next-token probability distribution. To determine if this uncertainty warrants intervention, the framework compares it against a model-specific threshold ($\tau^{LM}$). Instead of hand-tuning this threshold, AdaDec learns it using a data-driven logistic regression model trained on next-token prediction data. If the entropy is below the threshold, decoding proceeds normally (e.g., greedily). If it exceeds it, decoding pauses.
- Lookahead-based Token Reranking: Once paused, AdaDec selects the top-$K$ candidate tokens. For each candidate, it simulates a short future trajectory of length $L$ using greedy decoding. It then scores each candidate by calculating the geometric mean of the token log-probabilities across the candidate and its simulated future. The candidate with the highest trajectory score is appended to the sequence, and normal decoding resumes.
Datasets
- Threshold Training: BigCodeBench, filtered using SBERT semantic similarity to strictly eliminate any data contamination with the evaluation benchmarks.
- Evaluation Benchmarks:
- HumanEval+: An enhanced version of the classic HumanEval dataset featuring 81x more test cases across 164 Python problems to rigorously evaluate functional correctness.
- MBPP+: A refined subset of 378 problems from the Mostly Basic Python Problems (MBPP) dataset with 35x larger unit test suites.
- DevEval: A repository-level benchmark consisting of real-world software engineering scenarios. The authors randomly sampled 238 problems for evaluation.
Key Insights and Ideas
- Entropy is a High-Fidelity Signal for Logic Drift: The authors' empirical analysis demonstrates that tokens at logic drift points exhibit drastically higher Shannon entropy (median of 1.26) compared to non-drift steps (median of 0.03).
- The Correct Token is Usually in the Top-K: At high-uncertainty steps, the model rarely misses the correct token entirely; rather, it simply ranks it second or third instead of first. This validates why a local lookahead reranking strategy is highly effective.
- Model-Aware Thresholds are Essential: As shown in the paper's percentile analysis, different models exhibit varying sensitivities to entropy thresholds. Learning a model-specific threshold ensures that the framework balances accuracy gains with computational overhead across diverse model architectures and sizes.
Key Results
- Substantial Accuracy Boosts: AdaDec consistently outperforms greedy decoding, Beam Search ($B=3$), and AdapT. It improves Pass@1 accuracy by up to 20.9% in absolute terms (on MBPP+ using QW3-0.6B) and achieves average absolute improvements of +3.59% on HumanEval+, +7.61% on MBPP+, and +4.53% on DevEval.
- High Efficiency via Selective Pausing: AdaDec concentrates its computational budget only where it matters. It triggers the lookahead mechanism sparingly, maintaining a low average pause rate of 6.75% across all configurations.
- Counter-Intuitive Latency Reductions: While lookahead decoding typically adds overhead, for certain models (like QW3-0.6B and QW3-1.7B on HumanEval+), AdaDec actually decreased generation latency compared to greedy decoding. By correcting errors early, AdaDec prevents models from falling into repetitive loops or generating verbose, nonsensical code blocks.
Limitations and Future Work
- Reliance on Shannon Entropy: Shannon entropy provides a relatively coarse measure of model uncertainty. The authors note that incorporating richer contextual signals—such as token-level gradient norms or higher-order statistics—could improve detection accuracy.
- Simplicity of Threshold Learning: The threshold estimation relies on a simple logistic regression classifier. While highly interpretable, more expressive models like small neural networks might better capture complex uncertainty feature interactions.
- Fixed Lookahead Depth: AdaDec currently utilizes a fixed lookahead length ($L=5$). For complex, repository-level tasks (like DevEval), a short, static lookahead window might fail to capture long-range semantic dependencies, indicating a need for adaptive, context-aware lookahead depths.
Summary updated