[论文解读] What does Transformer learn about source code?
本文提出了聚合注意力分数和聚合注意力图,以自动从预训练的Transformer模型(如CodeBERT和GraphCodeBert)中提取结构信息,例如语法、控制流和数据流。该方法实现了无需规则的程序表示图的有效构建,其在Variable Misuse任务上的性能达到或超过人工设计的图结构,证明了Transformer模型在源代码中学习到丰富且语义有意义的结构模式。
In the field of source code processing, the transformer-based representation models have shown great powerfulness and have achieved state-of-the-art (SOTA) performance in many tasks. Although the transformer models process the sequential source code, pieces of evidence show that they may capture the structural information (\eg, in the syntax tree, data flow, control flow, \etc) as well. We propose the aggregated attention score, a method to investigate the structural information learned by the transformer. We also put forward the aggregated attention graph, a new way to extract program graphs from the pre-trained models automatically. We measure our methods from multiple perspectives. Furthermore, based on our empirical findings, we use the automatically extracted graphs to replace those ingenious manual designed graphs in the Variable Misuse task. Experimental results show that the semantic graphs we extracted automatically are greatly meaningful and effective, which provide a new perspective for us to understand and use the information contained in the model.
研究动机与目标
- 探究Transformer模型学习到的源代码结构信息,特别是超越线性序列建模的结构信息。
- 解决代码专用Transformer模型中结构模式缺乏有效解释方法的问题。
- 开发一种自动化的、数据驱动的方法,从注意力机制中提取程序表示图,而无需依赖手工规则。
- 评估自动提取的图是否能在下游代码智能任务中达到或超越人工设计图的性能。
提出的方法
- 提出聚合注意力分数,作为量化并聚合所有头和层注意力权重的方法,以揭示源代码中的结构依赖关系。
- 使用Chu-Liu/Edmonds(CLE)算法将聚合注意力分数转换为有向无环图(DAG),形成聚合注意力图。
- 将该方法应用于CodeBERT和GraphCodeBert等预训练模型,从注意力模式中提取语义和结构关系。
- 通过依赖解析基准上的精确率、召回率和F1等定量指标衡量图的质量。
- 在Variable Misuse任务中,用自动提取的聚合注意力图替代人工设计的图,并使用标准指标评估性能。
实验结果
研究问题
- RQ1基于Transformer的模型学习到的源代码结构信息(如语法、控制流/数据流)是什么类型?
- RQ2在未进行显式结构预训练的情况下,CodeBERT和GraphCodeBert等预训练模型的注意力机制是否能隐式编码程序结构?
- RQ3与人工设计的程序图相比,聚合注意力图在捕捉有意义的程序关系方面有多有效?
- RQ4从注意力分数中自动提取的图是否能在下游代码智能任务中实现具有竞争力的性能?
主要发现
- 聚合注意力分数成功捕捉了源代码中的句法和语义依赖关系,例如if-else结构和变量使用关系。
- 通过CLE算法提取的聚合注意力图保持了较强的结构保真度,并在Variable Misuse任务上实现了与人工设计图相当的性能。
- 在VarMisuse任务中,从CodeBERT提取的图达到77.33%的位置和表示准确率,从GraphCodeBert提取的图达到77.81%,性能匹配或超过基线水平。
- 该方法优于基于序列和基于原始输入的图基线模型,证明注意力机制编码了丰富且与任务相关的结构信息。
- 结果验证了Transformer模型即使未显式预训练于此类特征,也能学习并保留复杂的程序结构。
- 该方法提供了一种新颖、可扩展且可解释的方式,用于从预训练代码模型中提取和利用结构知识,而无需依赖外部工具或基于规则的系统。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。