[论文解读] Unveiling Code Pre-Trained Models: Investigating Syntax and Semantics Capacities
本论文通过一组新颖的探针任务,研究了CodeBERT和GraphCodeBERT在句法与语义学习方面的能力。研究利用抽象语法树(AST)设计句法特定任务,利用控制流、数据依赖和控制依赖图设计语义任务,结果表明,两种模型在句法学习方面表现优异,且GraphCodeBERT在捕捉控制流和数据依赖语义方面优于CodeBERT。
Past research has examined how well these models grasp code syntax, yet their understanding of code semantics still needs to be explored. We extensively analyze seven code models to investigate how code models represent code syntax and semantics. This includes four prominent code pre-trained models (CodeBERT, GraphCodeBERT, CodeT5, and UnixCoder) and three large language models (StarCoder, CodeLlama, and CodeT5+). We have developed four probing tasks to evaluate the models' abilities to learn code syntax and semantics. These tasks focus on reconstructing code syntax and semantic structures-such as AST, CFG, CDG, and DDG - within the models' representation spaces. These structures are fundamental to understanding code. Additionally, we explore the role of syntax tokens in each token representation and the extended dependencies among code tokens. Furthermore, we examine the distribution of attention weights concerning code semantic structures. Through detailed analysis, our results emphasize the strengths and weaknesses of various code models in mastering code syntax and semantics. The findings reveal that these models are proficient in grasping code syntax, effectively capturing the relationships and roles of syntax tokens. However, their ability to encode code semantics shows more variability. This study enriches our understanding of the capabilities of code models in analyzing syntax and semantics. Our findings offer valuable insights for future code model enhancements, helping optimize their application across a range of code-related tasks.
研究动机与目标
- 探究类似CodeBERT和GraphCodeBERT的代码预训练模型是否能有效学习程序句法与语义。
- 解决先前研究依赖高维空间中距离度量方法所面临的维度灾难问题。
- 系统分析预训练模型中编码的语义知识,尤其关注控制流、数据依赖与控制依赖。
- 探究注意力头与层在语义信息编码中的作用,为模型可解释性提供洞见。
- 通过AST和静态分析图验证探针任务的外部有效性,确保其真实反映程序句法与语义。
提出的方法
- 设计两项句法探针任务:(1) 基于AST结构的句法节点对预测,(2) 令牌句法标签预测,以评估语法规则的编码能力。
- 为语义探针任务构建控制流图(CFG)、数据依赖图(DDG)和控制依赖图(CDG)。
- 设计三项语义探针任务:(1) 语义关系预测,(2) 语义传播预测(inGraph),(3) 类型推断。
- 在CodeBERT与GraphCodeBERT的上下文表征上训练线性分类器,以评估其恢复句法与语义事实的能力。
- 使用Spearman等级相关系数(ρ)分析模型与层之间行为的一致性。
- 对注意力权重在头与层之间的分布进行统计分析,以识别其在语义信息编码中的作用。
实验结果
研究问题
- RQ1Code预训练模型如CodeBERT与GraphCodeBERT是否能有效学习程序句法?其在不同句法结构上的表现如何比较?
- RQ2CodeBERT与GraphCodeBERT在多大程度上能学习程序语义,特别是控制流、数据依赖与控制依赖信息?
- RQ3注意力头与层在这些模型中对语义知识编码的贡献有何差异?
- RQ4探针任务是否具有外部有效性,能否真实反映程序句法与语义?是否避免了高维距离度量的缺陷?
- RQ5CodeBERT与GraphCodeBERT在不同层中表示的一致性如何,其在捕捉句法与语义事实方面表现是否稳定?
主要发现
- CodeBERT与GraphCodeBERT在句法学习方面均表现出强大能力,体现在句法节点对预测与令牌标签预测任务中表现优异。
- GraphCodeBERT在控制流与数据依赖语义学习方面优于CodeBERT,这在CFG与DDG探针任务中表现更优。
- 两种模型在控制依赖信息学习方面能力相当,CDG相关探针任务中无显著性能差异。
- 两种模型的最终层表征均包含丰富的语义信息,但不同注意力头与层在编码各类语义事实中扮演不同角色。
- 注意力权重的统计分析表明,特定头与层对不同语义类型具有专业化特征,表明语义理解依赖异质性注意力机制。
- 探针任务具有外部有效性,因其基于AST与静态分析图构建,避免了高维距离度量方法的缺陷。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。