让代码生成按行进行,提升长期一致性与质量
From Token to Line: Enhancing Code Generation with a Long-Term Perspective
- 以行为单位逐行生成,利用注意力机制优化生成路径
- 在三个基准上超越当前最优方法,提升代码准确率
- 适合需要高质量、长序列代码生成的研究与开发场景
大语言模型的兴起显著推动了代码生成任务的发展,但现有研究常因冗余输出和短期模式过拟合而受限。尽管已有方法尝试通过多标记预测缓解问题,但对生成长度的选择关注不足。分析发现,大模型生成过程中,注意力分数高峰通常出现在行末,提示应以行为基本处理单元,按行顺序生成。受此启发,我们提出LSR-MCTS算法,利用蒙特卡洛树搜索实现逐行决策并选择最优路径;同时在每个节点引入自修正机制,通过错误纠正提升多样性与生成质量。在三个公开代码基准上的大量实验与分析表明,该方法显著优于现有最先进方法。
原文摘要 · Abstract (English)
The emergence of large language models (LLMs) has significantly promoted the development of code generation task, sparking a surge in pertinent literature. Current research is hindered by redundant generation results and a tendency to overfit local patterns in the short term. Although existing studies attempt to alleviate the issue by adopting a multi-token prediction strategy, there remains limited focus on choosing the appropriate processing length for generations. By analyzing the attention between tokens during the generation process of LLMs, it can be observed that the high spikes of the attention scores typically appear at the end of lines. This insight suggests that it is reasonable to treat each line of code as a fundamental processing unit and generate them sequentially. Inspired by this, we propose the LSR-MCTS algorithm, which leverages MCTS to determine the code line-by-line and select the optimal path. Further, we integrate a self-refine mechanism at each node to enhance diversity and generate higher-quality programs through error correction. Extensive experiments and comprehensive analyses on three public coding benchmarks demonstrate that our method outperforms the state-of-the-art performance approaches.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。