Abstract
Large Language Models (LLMs) typically employ uniform decoding strategies for both natural language and code. However, programming languages possess a rigid syntax where specific structural tokens act as logical anchors. This paper introduces AdapT (Adaptive Temperature) sampling, a decoding-time heuristic that applies high temperature to structural "challenging" tokens to encourage logical exploration and low temperature to "confident" tokens to minimize syntax errors. Evaluated on HumanEval and MBPP, AdapT significantly improves code generation accuracy and reduces common errors without requiring model retraining.
The Challenge of Code Generation
While Large Language Models have revolutionized code generation, they often rely on decoding strategies optimized for natural language (NL). Standard temperature sampling uses a single parameter $T$ to balance predictability and creativity. However, programming languages (PL) are fundamentally different: they are governed by strict, non-forgiving syntax. A single misplaced character can render an entire script non-functional. Current strategies treat every token equally, failing to distinguish between structural "anchors" that define logic and highly predictable boilerplate code.
The authors analyzed the loss distribution of code versus natural language, finding that code exhibits distinct "peaks"—specific tokens that are significantly harder for the model to predict. As shown in Figure 5, code generation involves higher variance in token difficulty compared to the relatively smoother loss curves of natural language.
AdapT Sampling Methodology
Statistical analysis revealed two types of tokens:
- Challenging Tokens: Difficult to predict, often representing the start of a new logical block.
- Confident Tokens: Easily inferred from the preceding context.
The "Start of Block" heuristic identifies that challenging tokens almost always appear at the beginning of a code block (e.g., immediately following an indentation change). Figure 1 illustrates this relationship, where tokens like while and if correspond to sharp peaks in the negative log probability (-logP).
AdapT sampling dynamically adjusts the temperature $T$ based on token position:
- High Temperature ($a$): Applied to the initial token of a code block to allow the model to explore diverse logical paths (e.g., deciding between an
ifstatement or awhileloop). - Low Temperature ($b$): Applied to all other tokens to maintain focus and prevent syntax errors once the block's logic is established.
Experimental Results
The researchers evaluated AdapT on HumanEval and MBPP using models like CodeGeeX-13B and CodeGen-2B.
- Accuracy Gains: On HumanEval, AdapT improved the pass@15 score of CodeGeeX-13B from 36.0% to 40.9%, a 13.6% relative improvement.
- Error Reduction: Qualitative analysis showed a significant drop in
NameErrorandSyntaxError. For CodeGen-2B,NameErrorinstances decreased by 44.3% compared to standard sampling. - Zero-Shot Utility: As a decoding-time heuristic, AdapT requires no retraining or fine-tuning, making it an efficient "plug-and-play" solution for existing LLMs.
Conclusion and Future Work
AdapT sampling demonstrates that treating code tokens differently based on their structural role leads to more robust and accurate generation. By reducing "tail randomness" in predictable segments while maintaining creativity at decision points, the method achieves state-of-the-art performance across multiple benchmarks.
While the current heuristic focuses on block starts, future work may involve learning-based temperature adjustment to identify challenging tokens within complex expressions or across different programming languages with varying syntax structures.
Summary updated