[论文解读] GraphCodeBERT: Pre-training Code Representations with Data Flow
GraphCodeBERT 通过引入数据流结构来预训练代码表示,提出两种结构感知任务,并在四个代码相关下游任务上实现最先进的结果。
Pre-trained models for programming language have achieved dramatic empirical improvements on a variety of code-related tasks such as code search, code completion, code summarization, etc. However, existing pre-trained models regard a code snippet as a sequence of tokens, while ignoring the inherent structure of code, which provides crucial code semantics and would enhance the code understanding process. We present GraphCodeBERT, a pre-trained model for programming language that considers the inherent structure of code. Instead of taking syntactic-level structure of code like abstract syntax tree (AST), we use data flow in the pre-training stage, which is a semantic-level structure of code that encodes the relation of "where-the-value-comes-from" between variables. Such a semantic-level structure is neat and does not bring an unnecessarily deep hierarchy of AST, the property of which makes the model more efficient. We develop GraphCodeBERT based on Transformer. In addition to using the task of masked language modeling, we introduce two structure-aware pre-training tasks. One is to predict code structure edges, and the other is to align representations between source code and code structure. We implement the model in an efficient way with a graph-guided masked attention function to incorporate the code structure. We evaluate our model on four tasks, including code search, clone detection, code translation, and code refinement. Results show that code structure and newly introduced pre-training tasks can improve GraphCodeBERT and achieves state-of-the-art performance on the four downstream tasks. We further show that the model prefers structure-level attentions over token-level attentions in the task of code search.
研究动机与目标
- 通过利用超越标记序列的代码结构来提升对代码的理解。
- 提出一种面向编程语言的语义结构感知预训练框架。
- 研究数据流作为对 AST 的可扩展替代,用于对代码语义的编码。
- 展示结构感知型预训练在多种代码相关任务中的益处。
提出的方法
- 使用一个 Transformer 主干,并扩展以将数据流作为图输入进行融合来表示代码。
- 引入图引导的掩码注意力,选择性地关注数据流节点和代码标记。
- 增加两个结构感知的预训练任务:数据流边预测和代码与数据流之间的节点对齐。
- 将这些与标准的掩码语言模型结合,以学习联合的代码和结构表示。
- 在 CodeSearchNet 上进行 GraphCodeBERT 的预训练,并在四个下游任务上进行评估。
实验结果
研究问题
- RQ1将数据流作为语义代码结构的引入,是否能在超越标记序列的方面改善学习得到的表示?
- RQ2两个结构感知的预训练任务(边预测和节点对齐)是否能提升跨任务的代码理解?
- RQ3GraphCodeBERT 与仅基于标记的预训练模型在代码搜索、克隆检测、翻译和 refinement 上的表现有何差异?
- RQ4在实践中模型是否比对原始代码标记更关注数据流节点?
主要发现
- GraphCodeBERT 在四个下游任务上实现了最先进的性能:代码搜索、克隆检测、代码翻译和代码 refinement。
- 在消融研究中,去除数据流或两种结构感知任务会降低性能,验证了它们的有效性。
- 注意力分析表明,在代码搜索中模型更偏好结构层面的(数据流)注意力,而非纯标记层面的注意力。
- 案例研究表明数据流有助于消解变量语义的歧义,提升代码理解。
- 在各任务中,利用代码结构相对于包括 CodeBERT 和 RoBERTa 变体的基线模型有显著提升。
更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。