Page 24 - 《软件学报》2024年第4期
P. 24
1602 软件学报 2024 年第 35 卷第 4 期
deep learning-based code intelligence. Traditional handcrafted code representation methods mainly rely on domain expert annotations,
which are time-consuming and labor-intensive. Moreover, the obtained code representations are task-specific and not easily reusable for
specific downstream tasks, which contradicts the green and sustainable development concept. To this end, many large-scale pretraining
models for source code representation have shown remarkable success in recent years. These methods utilize massive source code for
self-supervised learning to obtain universal code representations, which are then easily fine-tuned for various downstream tasks. Based on
the abstraction levels of programming languages, code representations have four level features: text level, semantic level, functional level,
and structural level. Nevertheless, current models for code representation treat programming languages merely as ordinary text sequences
resembling natural language. They overlook the functional-level and structural-level features, which bring performance inferior. To
overcome this drawback, this study proposes a representation enhanced contrastive multimodal pretraining (REcomp) framework for code
representation pretraining. REcomp has developed a novel semantic-level to structure-level feature fusion algorithm, which is employed
for serializing abstract syntax trees. Through a multi-modal contrastive learning approach, this composite feature is integrated with both
the textual and functional features of programming languages, enabling a more precise semantic modeling. Extensive experiments are
conducted on three real-world public datasets. Experimental results clearly validate the superiority of REcomp.
Key words: code representation; pre-trained model; multimodal; contrastive learning
由于软件的无处不在和软件版本的快速迭代, GitHub, StackOverflow 等开源代码仓库中的源代码爆发式
[1]
[2]
增长, 代码大数据时代 已经来临. 据 Evans Data Corporation 报告预计, 2024 年, 专业开发人员数会将增加
至 2 870 万. 因此, 如何有效利用海量的源代码相关数据来提高专业开发人员的工作效率, 进而推动代码相关
任务的发展(称作代码智能), 已经成为软件工程领域(SE)的研究热点. 要实现代码智能, 代码表征是关键. 代
[3]
码表征 是对源代码的语义和语法信息进行表征, 得到源代码的特征向量, 并将其应用在不同的下游任务上,
[5]
[4]
例如理解型 任务(代码克隆检测、语义代码检索等). 最初, 传统的基于手工的方法 利用统计学原理表征代
[7]
[6]
[8]
码 . 后来, 大量的机器学习算法 被运用到代码表征中, 但仍然需要手工从源代码中提取代码特征 . 无论
是传统的手工方法还是基于机器学习的方法都依赖领域专家的标注, 繁重耗时, 且无法灵活地应用于特定下
游任务, 这与绿色低碳的发展理念极不相符. 为解决这个问题, 近年来, 有学者开始将基于深度学习的模型,
[9]
如循环神经网络(recurrent neural network, RNN) 、卷积神经网络(convolutional neural network, CNN) [10−13] 和图
神经网络(graph neural network, GNN) [14,15] 等引入到代码表征中, 试图表征隐藏在源代码中的更深层次的复杂
特征, 进一步提高代码表征的准确性. 目前, 基于深度学习的方法已经广泛应用于各种代码智能任务.
根据源代码的抽象层次从低到高划分 [16] , 代码具有以下特征: 文本级特征是以自然语言方式概括函数功
能的语句, 如代码注释; 语义级特征是经过词法分析的字符序列, 如代码字符; 功能级特征是以编程语言方
式概括函数功能的序列, 如函数方法名和应用程序接口(application programming interface, API); 结构级特征
是表示代码句法信息的树形或者图形结构, 如抽象语法树(abstract syntax tree, AST)、数据流图(data flow graph,
DFG)、控制流图(control flow graph, CFG)和程序依赖图等. 由于抽象程度越高, 能够提取的信息越多, 所以大
部分研究更关注结构级特征. 近年来, 对结构级特征的研究主要集中在 AST 上. 因为 AST 含有代码完整的结
构信息, 而其他形式的结构表示, 如 DFG 和 CFG 都是从 AST 中提取的. 此外, 如果对代码只做了语义级的字
符改变(如修改变量名称或者改变代码风格), 而不改变程序的执行结构, 那么修改后的代码与原始代码在结
构信息上的表征是不变的. 因此, 可以利用这一点来实现跨语言表征, 有效避免不同编程语言的语法规则差
异问题. 然而, AST 的树形结构不适用于通用的 Seq2Seq 模型, 因此, AST 的序列化算法成为研究 AST 的主要
方向. 早期, TBCNN [10] 使用基于树结构的 CNN [17] 来表征抽象语法树. 为解决 CNN 的长序列学习依赖问题,
Code2tree [18] 则使用 GRU 编码器对 AST 序列进行编码, 并在解码阶段引入注意力机制以提高代码注释的质量.
CDLH [19] 框架引入了基于树结构的 LSTM [20] 来挖掘词法信息和句法信息. Code2vec [21] 将 AST 分解成一组路径,
并通过简单的全连接层学习如何聚合路径的原子表示. 后来, mrncs [22] 提出了一种基于结构遍历的简化 AST 的
方法, 保留关键句法信息的同时, 得到简化的 AST(称作 ISBT). 但是, 现有的大多数预训练模型对 AST 的提
取并不充分, 存在一些问题, 如图 1 所示. 这些问题包括结点信息的冗余, 即所有结点均出现不止一次, 例如
结点 A 出现了 2 次; 还有在序列化过程中额外开销的引入, 即序列中用大量的括号来表示一棵子树, 例如子树
B 的完整表示是“(B(D)D(E)E(F)F)”.