arXiv:2411.14611cs.SEcs.LG2024-11被引 3

用多代码视图增强Transformer,让小模型更懂代码语义。

CodeSAM: Source Code Representation Learning by Infusing Self-Attention with Multi-Code-View Graphs

  • 通过自注意力掩码融合抽象语法树等多代码视图
  • 在代码搜索等任务上超越CodeBERT等模型
  • 适合资源受限场景下的高效代码理解应用

软件工程中的机器学习因能显著提升各类应用性能而备受关注,这主要得益于可泛化的源代码表示方法,它们能有效捕捉代码的语法和语义特征。近年来,受自然语言处理启发的预训练Transformer模型在软件工程任务中表现突出。然而,源代码的结构与语义特性嵌入其语法中,可通过抽象语法树(AST)、数据流图(DFG)和控制流图(CFG)等结构化代码视图提取。这些视图可补充传统NLP方法,进一步提升软件工程任务表现。但目前缺乏灵活框架将任意代码视图有效融入现有Transformer模型。为此,本文提出CodeSAM——一种新型可扩展框架,通过构建自注意力掩码将多种代码视图注入Transformer模型。我们使用CodeSAM微调CodeBERT等小型语言模型(SLM),应用于语义代码搜索、代码克隆检测和程序分类任务。实验表明,相比GraphCodeBERT和CodeBERT等模型,使用单一或组合代码视图进行微调后,下游任务性能均获得提升。结果表明,CodeSAM等技术有助于构建紧凑且高性能的代码小型语言模型,适用于资源受限环境。

原文摘要 · Abstract (English)

Machine Learning (ML) for software engineering (SE) has gained prominence due to its ability to significantly enhance the performance of various SE applications. This progress is largely attributed to the development of generalizable source code representations that effectively capture the syntactic and semantic characteristics of code. In recent years, pre-trained transformer-based models, inspired by natural language processing (NLP), have shown remarkable success in SE tasks. However, source code contains structural and semantic properties embedded within its grammar, which can be extracted from structured code-views like the Abstract Syntax Tree (AST), Data-Flow Graph (DFG), and Control-Flow Graph (CFG). These code-views can complement NLP techniques, further improving SE tasks. Unfortunately, there are no flexible frameworks to infuse arbitrary code-views into existing transformer-based models effectively. Therefore, in this work, we propose CodeSAM, a novel scalable framework to infuse multiple code-views into transformer-based models by creating self-attention masks. We use CodeSAM to fine-tune a small language model (SLM) like CodeBERT on the downstream SE tasks of semantic code search, code clone detection, and program classification. Experimental results show that by using this technique, we improve downstream performance when compared to SLMs like GraphCodeBERT and CodeBERT on all three tasks by utilizing individual code-views or a combination of code-views during fine-tuning. We believe that these results are indicative that techniques like CodeSAM can help create compact yet performant code SLMs that fit in resource constrained settings.

代码表示Transformer多视图融合小模型

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。