arXiv:2608.15412cs.LGcs.AI2026-08

通过代码变换增强训练,提升编码模型对语法变体的鲁棒性。

Invariant Pretraining for Robust Code Representations

论文配图:Invariant Pretraining for Robust Code Representations
图 1 · 摘自论文原文
  • 用语义不变的代码变换构建多正样本对比学习
  • 在克隆检测上提升11个百分点,代码分类提升19个百分点
  • 仅需代码数据,适合部署在资源受限的代码分析场景

基于编码器的代码表示模型广泛用于克隆检测和代码分类等判别任务,其小体积和低推理成本是关键优势。然而,其鲁棒性脆弱:在语义等价但语法不同的代码(即不变程序)上,学习到的表示性能显著下降,尽管程序行为未变。本文对四种编码器基线、两个下游任务和四个数据集进行了实证研究,提出一种仅使用代码的持续预训练方法——不变预训练(InvPT)。该方法对语料库应用语义保持变换,并结合掩码语言建模与多正样本监督对比学习,将同一函数的所有变换版本视为正例,混合自对比对(相同代码不同掩码)与不变对比对(变换代码),以应对不同难度的正例。与以往对比学习方法不同,InvPT无需配对的自然语言数据。在评估中,InvPT在变换测试集上的鲁棒性提升达11个百分点(克隆检测)和19个百分点(代码分类),同时保持或优于标准准确率;消融实验表明,多正样本不变对比是主要增益来源。目标并非提出新目标,而是精准定位编码器鲁棒性失效点,并验证简单、纯代码的方案可恢复多大程度的鲁棒性。

原文摘要 · Abstract (English)

Encoder-based code representation models remain widely deployed for discriminative tasks such as clone detection and code classification, where their small size and low inference cost are decisive. Their robustness, however, is fragile: under invariant programs, semantically equivalent code written in different syntactic forms, learned representations degrade substantially even though program behavior is unchanged. We present an empirical study of this robustness gap across four encoder baselines, two downstream tasks, and four datasets, together with a minimal code-only continued pretraining recipe that closes much of it. Our method, invariant pretraining (InvPT), applies semantic-preserving transformations to the corpus and combines masked language modeling with multi-positive supervised contrastive learning that treats all augmentations of the same source function as positives, mixing self-contrast pairs (same code, different masks) with invariant-contrast pairs (transformed code) for positives of varying difficulty. Unlike prior contrastive code encoders, InvPT does not require paired natural-language data. Across our evaluation, InvPT improves robustness on transformed test sets by up to 11 percentage points on clone detection and 19 on code classification while matching or improving standard accuracy, and our ablations isolate multi-positive invariant contrast as the main source of the gains. Our aim is not a new objective but a careful measurement of where encoder robustness breaks and how far a simple, code-only recipe can recover it.

代码表示对比学习鲁棒性预训练

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