Abstract
Traditional code generation in Large Language Models (LLMs) relies on token-by-token decoding, a method that often lacks the long-term structural perspective required for complex programming tasks. This paper presents LSR-MCTS (Line-level Self-Refine Monte Carlo Tree Search), a novel decoding strategy that shifts the unit of generation from individual tokens to complete lines of code. By identifying "summary tokens" at line breaks and integrating a self-refine mechanism within a Monte Carlo Tree Search framework, LSR-MCTS significantly improves logical coherence and performance across major Python benchmarks.
Introduction
Most current LLMs generate code sequentially, predicting the next token based on preceding context. While effective for short snippets, this "short-sighted" approach often leads to logical inconsistencies, redundant code, or structural hallucinations in longer programs. Programming languages possess a strict logical structure where a single line typically represents a discrete functional step. Existing decoding strategies, such as greedy search or token-level MCTS, struggle to maintain global coherence as program complexity increases.
The "Summary Token" and Line-Level Decoding
A key insight driving this research is the discovery of "summary tokens." By analyzing attention maps during code generation, researchers observed that attention scores spike significantly at the end of lines, specifically at the \n character.
This phenomenon suggests that the model naturally uses line breaks to consolidate and summarize information from the preceding line. LSR-MCTS leverages this biological-like justification to make the "line" the basic unit of its search tree, allowing for a shallower search space that can explore further into the program's potential future without the exponential complexity of token-level search.
The LSR-MCTS Framework
The proposed Line-level Self-Refine Monte Carlo Tree Search algorithm follows a four-phase process adapted specifically for structured code generation:
- Selection: The algorithm uses the Upper Confidence Bound (UCT) to navigate the tree and select the most promising partial code path.
- Expansion: The LLM generates multiple potential "next lines." To facilitate testing, a "supplement" is generated to complete the code block.
- Evaluation: The completed code is executed against public test cases. The resulting pass/fail status provides a reward score.
- Backpropagation: This reward is propagated back up the tree to update the value of ancestor nodes, informing future selections.
Crucially, the framework includes a Self-Refine mechanism. If a generated line is found to be suboptimal during the expansion or evaluation phase, the LLM is prompted to "think again" and correct the line. This synergy between search and refinement is more effective at the line level than the token level, as the line provides sufficient context for the model to recognize and rectify its own errors.
Experimental Results
The researchers evaluated LSR-MCTS on three Python benchmarks: HumanEval, MBPP, and Code Contests. The method consistently outperformed traditional decoding methods (Beam-search, Top-p) and existing token-level MCTS approaches.
- Complex Algorithmic Reasoning: The most significant gains were observed in the Code Contests benchmark, which features difficult competitive programming problems. Using the aiXcoder-7B model, LSR-MCTS achieved a 12.4% improvement in the
pass@5metric over the baseline. - Model Generalizability: The performance boost was consistent across various architectures, including CodeLlama, Llama3-8B, and GPT-4.
- Ablation Studies: Removing either the line-level search or the self-refine module resulted in a performance drop, confirming that the integration of both components is vital for the system's success.
Conclusion and Future Work
LSR-MCTS demonstrates that shifting to a line-level perspective allows LLMs to generate more coherent and logically sound code. While the method is computationally more expensive than greedy decoding due to multiple rollouts and the requirement for test cases, it sets a new state-of-the-art for complex code generation. Future research will focus on developing neural discriminators to replace the dependency on pre-written test cases and expanding the framework to other programming languages like C++.
Summary updated