From Tokens to Files: Multi-Granularity Detection and Localization of AI-Involved Code

📄 arXiv: 2610.27863v1 📥 PDF

作者: Yue Xu, Xiyang Wang, Hui Liu, Bolei He

分类: cs.SE, cs.CL, cs.LG

发布日期: 2026-10-20

备注: 29 pages


💡 一句话要点

提出多粒度检测与定位方法以区分AI参与的代码

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 代码生成 AI作者识别 多粒度检测 机器学习 自然语言处理

📋 核心要点

  1. 现有方法通常将代码文件压缩为单一评分,无法有效区分仅少量块涉及AI的情况,导致证据丢失。
  2. 本文提出了一种基于token级别的框架,通过使用低秩适配器和轻量级头部,精确定位AI作者身份。
  3. 在多个基准测试中,本文方法在假阳性率保持低于1%的情况下,达到了72.4的宏F1值,显著提升了检测性能。

📝 摘要(中文)

随着大型语言模型在代码生成中的广泛应用,亟需区分纯人类编写的代码与AI参与的代码。本文提出了一种新框架,能够在标记文件的同时,精确定位由AI生成或修改的代码行或块。现有方法通常将文件压缩为单一评分,忽略了仅有少量块涉及AI的证据。我们的方法在token级别建模AI作者身份,并在行、块和文件级别进行预测。通过对混合作者文件的训练,我们的方法在多个基准测试中表现出色,达到了72.4的宏F1值,且在真实人类代码中保持较低的假阳性率。

🔬 方法详解

问题定义:本文旨在解决如何有效区分人类编写的代码与AI生成或修改的代码的问题。现有方法往往将文件压缩为单一评分,导致在仅有少量块涉及AI时,证据被平均化,无法准确定位AI的贡献。

核心思路:我们的方法在token级别建模AI作者身份,通过计算每个token的对数似然比来提供局部证据,并在行、块和文件级别进行预测。这种设计能够更细致地捕捉AI的影响,避免信息的丢失。

技术框架:整体架构包括一个冻结的代码语言模型,配备两个低秩适配器,分别在机器生成和人类编写的文件上进行训练。轻量级头部用于在行和块内聚合证据,并最终汇总为文件级决策。

关键创新:最重要的创新在于将AI作者身份建模提升到token级别,利用局部上下文信息进行更精确的定位。这与现有方法的全局评分机制形成鲜明对比。

关键设计:在模型设计中,使用了低秩适配器来处理不同类型的文件,损失函数设计为优化token级别的对数似然比,确保模型能够有效学习到人类与AI的写作风格差异。

🖼️ 关键图片

img_0
img_1
img_2

📊 实验亮点

在内部测试集中,本文方法在11,773个AI参与文件与2,541个人类文件的对比中,达到了72.4的宏F1值,假阳性率保持在0.55%至0.81%之间,显示出优异的性能。此外,在HybridCodeAuthorship基准上,未经过适配的情况下,线级F1达到了62.5,超越了已有的最佳结果56。

🎯 应用场景

该研究的潜在应用领域包括代码审查、软件开发工具以及教育领域,能够帮助开发者识别和理解AI生成的代码,提升代码质量和安全性。未来,该方法还可以扩展到其他类型的文本生成任务,推动人机协作的研究与实践。

📄 摘要(原文)

The increasing use of large language models for code generation calls for methods that distinguish purely human-written code from AI-involved code: files in which any part, however small, was generated or substantially modified by a model. Beyond flagging such files, we aim to localize the lines or blocks attributable to AI. Existing approaches compress a file into a single score, from surface stylistic features or a global code representation, which averages away the evidence when only a few blocks are AI-involved. We propose a framework that models AI authorship at the token level and predicts at the line, block, and file levels. A frozen code language model is equipped with two low-rank adapters, trained separately on machine-written and human-written files; their per-token log-likelihood ratio provides local evidence of AI authorship. Lightweight heads aggregate this evidence within lines and blocks, using the surrounding code context, and their local predictions are aggregated into a file-level decision. We train on mixed-authorship files built from human originals and their AI-generated or AI-modified counterparts, including continuation and fill-in-the-middle scenarios. On an internal test set of 11,773 AI-involved files across six generation regimes against 2,541 human files, our method reaches 72.4 macro F1 at a 1% FPR operating point. With no adaptation, it reaches 62.5 line-level F1 on HybridCodeAuthorship, an external human-AI co-writing benchmark with no training split, above the 56 its own paper reports as the best line-level result. On 19,445 human-written files from repositories created before 2022, the false-positive rate stays between 0.55% and 0.81% under the transported 1% threshold, so the operating point holds on real human code.