Skip to main content
QUICK REVIEW

[论文解读] SPT-Code: Sequence-to-Sequence Pre-Training for Learning Source Code Representations

Changan Niu, Chuanyi Li|arXiv (Cornell University)|Jan 5, 2022
Software Engineering Research被引用 11
一句话总结

SPT-Code 提出了一种用于源代码表示学习的序列到序列预训练模型,通过三种新颖的自监督任务——代码重建、基于抽象语法树(AST)遍历的代码结构建模,以及自然语言描述预测——联合预训练编码器和解码器,且无需双语平行数据。该方法在五个下游代码相关任务中实现了最先进(SOTA)的性能,包括代码摘要生成、代码补全和错误修复。

ABSTRACT

Recent years have seen the successful application of large pre-trained models to code representation learning, resulting in substantial improvements on many code-related downstream tasks. But there are issues surrounding their application to SE tasks. First, the majority of the pre-trained models focus on pre-training only the encoder of the Transformer. For generation tasks that are addressed using models with the encoder-decoder architecture, however, there is no reason why the decoder should be left out during pre-training. Second, many existing pre-trained models, including state-of-the-art models such as T5-learning, simply reuse the pre-training tasks designed for natural languages. Moreover, to learn the natural language description of source code needed eventually for code-related tasks such as code summarization, existing pre-training tasks require a bilingual corpus composed of source code and the associated natural language description, which severely limits the amount of data for pre-training. To this end, we propose SPT-Code, a sequence-to-sequence pre-trained model for source code. In order to pre-train SPT-Code in a sequence-to-sequence manner and address the aforementioned weaknesses associated with existing pre-training tasks, we introduce three pre-training tasks that are specifically designed to enable SPT-Code to learn knowledge of source code, the corresponding code structure, as well as a natural language description of the code without relying on any bilingual corpus, and eventually exploit these three sources of information when it is applied to downstream tasks. Experimental results demonstrate that SPT-Code achieves state-of-the-art performance on five code-related downstream tasks after fine-tuning.

研究动机与目标

  • 解决现有代码预训练模型仅预训练编码器而使解码器未优化的局限性。
  • 通过设计无需在预训练阶段使用标注自然语言描述的预训练任务,克服对小规模双语代码-文档语料库的依赖。
  • 通过无外部监督的方式,联合学习源代码结构(通过 AST)和自然语言语义,以改善代码表示。
  • 实现在完全无标签代码数据上的端到端编码器-解码器模型预训练,以提升下游软件工程任务的性能。

提出的方法

  • 提出三种自监督预训练任务:(1) 通过掩码标记预测进行代码重建,(2) 使用改进的 AST 遍历方法(X-SBT)进行代码结构建模,(3) 使用代码标记作为输入进行自然语言描述预测。
  • 设计一种混合输入表示,结合源代码标记、通过 X-SBT 的 AST 遍历以及自然语言嵌入,以增强结构和语义信号。
  • 使用序列到序列 Transformer 架构端到端训练 SPT-Code 模型,对大规模无标签代码语料库中的编码器和解码器进行联合预训练。
  • 使用 CodeSearchNet 作为预训练数据集,不进行重复数据删除,以确保与基线方法的公平比较。
  • 在五个下游任务上微调预训练的 SPT-Code 模型:代码摘要生成、代码补全、错误修复(BFP)、代码搜索以及 Java 代码摘要生成(JCSD)。
  • 提出 X-SBT,一种增强的 AST 遍历方法,通过在遍历过程中保留控制流和数据流语义,更好地捕捉代码结构。

实验结果

研究问题

  • RQ1是否一种联合训练编码器和解码器的序列到序列预训练模型,能在代码表示学习中超越仅训练编码器的模型?
  • RQ2是否不依赖双语代码-文档对的预训练任务,仍能有效学习代码的自然语言描述?
  • RQ3在多大程度上,结合 AST 结构和自然语言信号能提升下游代码任务的性能?
  • RQ4所提出的三种预训练任务在不同下游任务中各自对模型性能的贡献如何?
  • RQ5使用改进的 AST 遍历方法(X-SBT)是否能比标准遍历技术带来更好的代码结构建模效果?

主要发现

  • SPT-Code 在五个下游代码相关任务中实现了最先进性能:代码摘要生成(BLEU 提升 +0.88 超过 SOTA)、代码补全(Acc@1 提升 +0.03)、错误修复(BFP 中等准确率提升 +0.07)。
  • 消融实验表明,所有三种预训练任务均产生积极影响,其中基于 AST 的任务对代码结构理解的提升作用最强。
  • 同时包含 AST 和自然语言输入组件显著提升了模型性能,表明结构信号与语义信号具有互补优势。
  • 即使在从 CodeSearchNet 中移除测试集重复数据后,SPT-Code 的性能在任一指标上下降均不足 0.07,表明其对数据泄露问题具有鲁棒性。
  • 包含 AST 信息的模型(如 SPT-Code、GraphCodeBERT)生成的摘要比仅依赖数据流或代码标记的模型更准确且更具可读性。
  • X-SBT 遍历方法在捕捉代码结构方面优于标准 AST 遍历方法,表现为摘要质量更高且结构一致性更强。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。