Skip to main content
QUICK REVIEW

[论文解读] StructCoder: Structure-Aware Transformer for Code Generation

Sindhu Tipirneni, Ming Zhu|arXiv (Cornell University)|Jun 10, 2022
Software Engineering Research被引用 7
一句话总结

StructCoder 提出了一种结构感知的 Transformer 编码器-解码器模型,用于代码生成,通过在编码器和解码器中联合建模抽象语法树(AST)和数据流图(DFG),提升了语法和语义的保真度。它在解码过程中引入了新颖的辅助任务——AST 路径预测和 DFG 边预测,在 CodeXGLUE 和 APPS 基准测试中实现了最先进性能。

ABSTRACT

There has been a recent surge of interest in automating software engineering tasks using deep learning. This paper addresses the problem of code generation, where the goal is to generate target code given source code in a different language or a natural language description. Most state-of-the-art deep learning models for code generation use training strategies primarily designed for natural language. However, understanding and generating code requires a more rigorous comprehension of the code syntax and semantics. With this motivation, we develop an encoder-decoder Transformer model where both the encoder and decoder are explicitly trained to recognize the syntax and data flow in the source and target codes, respectively. We not only make the encoder structure-aware by leveraging the source code's syntax tree and data flow graph, but we also support the decoder in preserving the syntax and data flow of the target code by introducing two novel auxiliary tasks: AST (Abstract Syntax Tree) paths prediction and data flow prediction. To the best of our knowledge, this is the first work to introduce a structure-aware Transformer decoder that models both syntax and data flow to enhance the quality of generated code. The proposed StructCoder model achieves state-of-the-art performance on code translation and text-to-code generation tasks in the CodeXGLUE benchmark, and improves over baselines of similar size on the APPS code generation benchmark. Our code is publicly available at https://github.com/reddy-lab-code-research/StructCoder/.

研究动机与目标

  • 解决现有代码生成模型将代码视为自然语言处理的局限,忽略语法和语义结构的问题。
  • 通过在编码器中显式建模源代码的语法(通过 AST)和数据流(通过 DFG),提升代码生成质量。
  • 通过训练解码器预测目标代码中的根到叶 AST 路径和 DFG 边,提升输出的结构正确性。
  • 在保持模型效率的前提下,在代码翻译和文本到代码生成任务中实现最先进结果。
  • 通过在编码器和解码器中整合结构归纳偏置,实现更可靠且语义准确的代码生成。

提出的方法

  • 将源代码 AST 的根到叶路径和 DFG 节点作为编码器输入的增强内容,使用结构感知自注意力机制建模依赖关系。
  • 引入一种改进的自注意力机制,以保留代码标记、AST 节点和 DFG 变量之间的结构关系。
  • 在解码过程中引入两个辅助任务:预测目标 AST 中的所有根到叶路径,以及预测目标代码中的 DFG 边。
  • 使用基于结构的去噪自编码器(DAE)任务进行预训练,以提升表征学习能力。
  • 在微调过程中采用多任务学习目标,结合掩码语言建模、AST 路径预测和 DFG 边预测。
  • 实现高效的注意力掩码机制,以在 GPU 上高效处理代码标记、DFG 变量和 AST 叶子之间的稀疏注意力模式。

实验结果

研究问题

  • RQ1在编码器和解码器中联合建模 AST 和 DFG 结构,是否能相比标准 Transformer 更好地提升代码生成质量?
  • RQ2训练解码器预测 AST 路径和 DFG 边,是否能生成更具语法和语义正确性的代码?
  • RQ3结构组件的引入如何影响推理效率和可扩展性?
  • RQ4结构感知设计在代码翻译和文本到代码生成基准测试中,性能提升的幅度有多大?
  • RQ5结构感知预训练和辅助任务能否在不同编程语言和代码生成任务中实现泛化?

主要发现

  • 在 CodeXGLUE 基准测试中,StructCoder 在代码翻译和文本到代码生成任务中均实现了最先进性能。
  • 在 APPS 基准测试中,StructCoder 超过了同等规模的基线模型,表现出更强的泛化能力和正确性。
  • 模型在 Go 语言的代码摘要任务中性能提升了 4.62 BLEU 分(从 19.56 提升至 24.18),表明其具备更广泛的应用潜力。
  • 尽管输入长度增加了 75–83%,但添加 AST 和 DFG 组件后,推理时间仅增加 28–29%,表明 GPU 利用效率高。
  • 解码器的辅助任务(AST 路径和 DFG 预测)显著提升了结构保真度,且未增加推理成本。
  • 结构感知自注意力机制能够更好地建模代码的语义和语法,尤其在复杂转换任务中表现更优。

更好的研究,从现在开始

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

无需绑定信用卡

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