[论文解读] CodeEditor: Learning to Edit Source Code with Pre-trained Models
本文提出 CodeEditor,一种预训练代码编辑模型,通过一种新颖的预训练任务学习将变异代码版本纠正为真实版本,该任务聚焦于真实世界的编辑模式。通过在自动生成的多样化代码变异上进行训练,CodeEditor 在微调、少样本和零样本设置下均达到最先进性能,在基准数据集上的表现优于先前模型高达 26.6%。
Developers often perform repetitive code editing activities for various reasons (e.g., code refactoring) during software development. Pre-trained code editing models have achieved the state-of-the-art (SOTA) results. Pre-trained models are first pre-trained with pre-training tasks and fine-tuned with the code editing task. Existing pre-training tasks mainly are code infilling tasks (e.g., masked language modeling), which are derived from the natural language processing field and are not designed for automatic code editing. This paper proposes a novel pre-training task specialized in code editing and presents an effective pre-trained code editing model named CodeEditor. Our pre-training task further improves the performance and generalization ability of code editing models. Specifically, we collect lots of real-world code snippets as the ground truth and use a powerful generator to rewrite them into mutated versions. Then, we pre-train our CodeEditor to edit mutated versions into the corresponding ground truth, to learn edit patterns. We conduct experiments on four code editing datasets and evaluate the pre-trained CodeEditor in three settings. (1) In the fine-tuning setting, we train the pre-trained CodeEditor with four datasets and evaluate it on the test data. CodeEditor outperforms the SOTA baselines by 15%, 25.5%, and 9.4% and 26.6% on four datasets. (2) In the few-shot setting, we train the pre-trained CodeEditor with limited data and evaluate it on the test data. CodeEditor substantially performs better than all baselines. (3) In the zero-shot setting, CodeEditor correctly edits 1,113 programs while the SOTA baselines can not work.
研究动机与目标
- 解决现有代码编辑预训练任务的局限性,这些任务源自自然语言处理(NLP),未针对真实代码编辑模式进行优化。
- 通过从真实、多样的代码变异中学习,提升代码编辑模型的性能与泛化能力。
- 设计一种预训练任务,通过使用真实代码作为真实版本,并利用强大生成器生成合理变异版本,更好地捕捉有意义的编辑模式。
- 在真实场景中评估模型,包括少样本和零样本场景,以证明其鲁棒性与泛化能力。
- 提供一种更实用、高效的自动代码编辑方法,无需编辑位置标注。
提出的方法
- 从开源代码仓库(如 GitHub)中收集真实世界的代码片段作为真实版本代码,确保其经过审查且可投入生产使用。
- 使用强大的神经生成器将真实版本代码自动重写为合理、变异的版本,这些版本在语法或语义上有所不同,但仍保持语法正确。
- 设计一种新颖的预训练任务,使模型学习将变异代码编辑回原始真实版本,从而端到端学习编辑模式。
- 使用序列到序列框架在编辑-变异对数据上预训练 CodeEditor 模型,使其能够识别并纠正代码更改,而无需显式的位置标注。
- 在下游代码编辑数据集上对预训练的 CodeEditor 进行微调,涵盖标准、少样本和零样本设置,以评估性能与泛化能力。
- 确保变异样本始终与原始代码不同,以避免产生平凡或误导性的预训练样本,与 ELECTRA 或 SSR 等方法不同。
实验结果
研究问题
- RQ1是否一种专为代码编辑设计的预训练任务能优于通用的代码掩码填充任务(如掩码语言建模)?
- RQ2在真实、多样的代码变异上进行预训练,是否能提升模型对未见编辑模式的泛化能力?
- RQ3CodeEditor 是否能在无需在目标数据集上微调的情况下,在零样本和少样本设置中实现强大性能?
- RQ4在不同代码编辑基准测试中,CodeEditor 与最先进模型相比,在编辑准确率和鲁棒性方面表现如何?
- RQ5通过学习从变异到原始代码的编辑对,是否能比标准预训练目标带来更优的编辑模式理解?
主要发现
- 在微调设置下,CodeEditor 在四个不同的代码编辑数据集上分别比最先进基线模型高出 15%、25.5%、9.4% 和 26.6%。
- 在少样本设置下,CodeEditor 超过了在完整训练数据上微调的最先进模型,展现出更强的数据效率与泛化能力。
- 在零样本设置下,CodeEditor 在未进行任何微调的情况下成功编辑了 1,113 个程序,而最先进基线模型完全失败,证明其具备强大的归纳偏置与泛化能力。
- 所提出的预训练任务通过聚焦于真实、多样的编辑模式而非随机掩码,显著提升了模型性能与泛化能力。
- CodeEditor 能够自动定位并纠正代码编辑,而无需编辑位置标注,使其在实际应用中更具实用性。
- 该方法避免了平凡的预训练样本(如输入与输出完全相同),减少了模型混淆,提升了学习效率。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。