[论文解读] What Do They Capture? -- A Structural Analysis of Pre-Trained Language Models for Source Code
本文通过注意力对齐、词嵌入探查和语法树归纳,对预训练代码语言模型(CodeBERT 和 GraphCodeBERT)进行结构分析,揭示其如何捕捉代码结构。研究发现注意力机制与代码语法高度对齐,中间表示保留了句法结构,且模型能够归纳出有效的抽象语法树(AST),表明在预训练过程中引入语法信息可增强代码表征学习。
Recently, many pre-trained language models for source code have been proposed to model the context of code and serve as a basis for downstream code intelligence tasks such as code completion, code search, and code summarization. These models leverage masked pre-training and Transformer and have achieved promising results. However, currently there is still little progress regarding interpretability of existing pre-trained code models. It is not clear why these models work and what feature correlations they can capture. In this paper, we conduct a thorough structural analysis aiming to provide an interpretation of pre-trained language models for source code (e.g., CodeBERT, and GraphCodeBERT) from three distinctive perspectives: (1) attention analysis, (2) probing on the word embedding, and (3) syntax tree induction. Through comprehensive analysis, this paper reveals several insightful findings that may inspire future studies: (1) Attention aligns strongly with the syntax structure of code. (2) Pre-training language models of code can preserve the syntax structure of code in the intermediate representations of each Transformer layer. (3) The pre-trained models of code have the ability of inducing syntax trees of code. Theses findings suggest that it may be helpful to incorporate the syntax structure of code into the process of pre-training for better code representations.
研究动机与目标
- 理解预训练代码语言模型为何在代码智能任务中表现优异。
- 探究这些模型捕捉的结构性特征,特别是句法结构。
- 分析注意力、词嵌入和模型表征是否保留或诱导代码句法。
- 评估预训练模型在无显式训练的情况下重建抽象语法树(AST)的能力。
- 提供可解释性洞见,以指导未来代码表征模型的设计。
提出的方法
- 通过可视化自注意力并将其与AST结构对齐,评估注意力对句法成分的关注程度。
- 设计一种结构探查方法,测试每个Transformer层中的中间表示是否编码了句法结构。
- 应用自顶向下递归的二叉树归纳算法,从模型表征中恢复AST。
- 在树构建中引入右偏倚以提升与AST的对齐性,该方法源自自然语言解析。
- 使用标准树相似度度量评估归纳树与真实AST的匹配程度。
- 在大规模代码仓库中的代码片段上分析模型,以确保泛化能力。
实验结果
研究问题
- RQ1预训练代码模型中的注意力头在多大程度上与源代码的句法结构对齐?
- RQ2每个Transformer层中的中间表示在多大程度上保留了句法结构?
- RQ3预训练代码模型能否在无显式训练的情况下,从代码表征中归纳出有效的抽象语法树(AST)?
- RQ4在预训练中引入语法信息(如通过GraphCodeBERT)如何影响表征中的结构保留?
- RQ5注意力模式、词嵌入与代码模型中的句法结构之间存在何种关系?
主要发现
- 预训练代码模型中的注意力头与句法成分(尤其是函数调用、控制结构和变量声明)表现出强烈对齐。
- 所有Transformer层的中间表示均保留了句法结构,且深层网络表现出更精细的句法感知能力。
- 预训练代码模型能够从未显式训练的代码嵌入中归纳出合理的抽象语法树,树相似度的F1分数表明结构恢复具有实际意义。
- 像GraphCodeBERT这类在预训练中引入数据流图的模型,相比标准的CodeBERT,表现出更强的句法结构保留能力。
- 归纳出的语法树结构连贯,反映了代码的层次特性,表明句法结构被隐式编码于表征之中。
- 本研究揭示,句法结构是预训练代码模型捕捉的关键归纳偏置,支持在未来预训练目标中整合句法信息。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。