Skip to main content
QUICK REVIEW

[论文解读] NatGen: Generative pre-training by "Naturalizing" source code

Saikat Chakraborty, Toufique Ahmed|arXiv (Cornell University)|Jun 15, 2022
Software Engineering Research被引用 9
一句话总结

NatGen 提出了一种新颖的预训练目标——'自然化'代码,即模型学习将语义等价但不自然的代码转换为类人、自然的代码。通过在真实代码上应用六种语义保持的变换,并在代码生成、翻译和优化任务上进行微调,NatGen 在零样本和少样本设置下均取得了最先进性能,优于 CodeT5 等模型。

ABSTRACT

Pre-trained Generative Language models (e.g. PLBART, CodeT5, SPT-Code) for source code yielded strong results on several tasks in the past few years, including code generation and translation. These models have adopted varying pre-training objectives to learn statistics of code construction from very large-scale corpora in a self-supervised fashion; the success of pre-trained models largely hinges on these pre-training objectives. This paper proposes a new pre-training objective, "Naturalizing" of source code, exploiting code's bimodal, dual-channel (formal & natural channels) nature. Unlike natural language, code's bimodal, dual-channel nature allows us to generate semantically equivalent code at scale. We introduce six classes of semantic preserving transformations to introduce un-natural forms of code, and then force our model to produce more natural original programs written by developers. Learning to generate equivalent, but more natural code, at scale, over large corpora of open-source code, without explicit manual supervision, helps the model learn to both ingest & generate code. We fine-tune our model in three generative Software Engineering tasks: code generation, code translation, and code refinement with limited human-curated labeled data and achieve state-of-the-art performance rivaling CodeT5. We show that our pre-trained model is especially competitive at zero-shot and few-shot learning, and better at learning code properties (e.g., syntax, data flow).

研究动机与目标

  • 解决代码生成及其他生成式软件工程任务中高质量标注数据稀缺的问题。
  • 利用代码的双通道特性(形式化语言与自然语言)构建一种自监督预训练目标,以提升模型的理解与生成能力。
  • 开发一种可扩展的无监督方法,通过将自然代码转化为不自然形式并要求恢复原始形式,在大规模开源代码上预训练模型。
  • 通过强调代码自然性与语义保真度的预训练任务,提升模型在低资源设置(零样本和少样本)下的泛化能力与性能。
  • 证明学习‘编辑’代码——即自然化代码——相比标准去噪目标,在下游生成任务中能带来更好的性能。

提出的方法

  • 对真实、自然的源代码应用六类语义保持的变换,生成‘不自然’的变体,保留语义的同时改变语法与结构。
  • 将原始自然代码作为目标,将变换后的代码作为输入,以序列到序列的方式训练模型重建原始形式。
  • 将预训练目标设计为去噪自编码器任务,使模型学习将不自然代码映射到其自然等价形式,从而需要深入理解语义与句法。
  • 使用此自然化目标在大规模开源代码语料上对基于 Transformer 的模型(NatGen)进行无监督预训练。
  • 在三个下游生成任务(代码生成、代码翻译、代码优化)上对预训练的 NatGen 模型进行微调,仅使用有限的人工标注数据。
  • 采用类似 CodeT5 的编码器-解码器架构,但将其预训练目标替换为自然化任务,以提升生成质量与迁移能力。

实验结果

研究问题

  • RQ1基于自然化代码的预训练目标——即将不自然代码转换为自然、开发者编写的代码——是否能提升模型在下游生成式软件工程任务中的性能?
  • RQ2在低资源设置下,自然化预训练目标与标准去噪目标(如掩码标记预测)相比,其下游性能如何?
  • RQ3自然化任务在多大程度上增强了模型学习代码句法与语义特性(如数据流与结构)的能力?
  • RQ4代码的双通道特性(形式化语言与自然语言)是否能提供比传统方法更有效的自监督预训练目标?
  • RQ5在未经过大量微调的情况下,经自然化预训练的模型是否能有效泛化至零样本和少样本设置?

主要发现

  • NatGen 在代码生成、代码翻译和代码优化任务上均达到最先进性能,与或超越 CodeT5 的表现。
  • 模型在零样本和少样本学习设置中表现出色,表明自然化预训练目标具有强大的泛化能力。
  • 与采用标准去噪目标预训练的模型相比,NatGen 生成的代码在句法与语义上更接近人类编写的代码。
  • 自然化预训练任务有效提升了模型理解与生成具有更好结构与语义保真度代码的能力,这一点在数据流等代码属性上的性能提升中得到验证。
  • 该方法具有可扩展性且为自监督,预训练阶段无需人工标注数据,仅依赖对现有代码的自动化变换。
  • 在有限标注数据上微调 NatGen 即可获得优异结果,证明了该预训练目标在低数据场景下的有效性。

更好的研究,从现在开始

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

无需绑定信用卡

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