arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2510.14972cs.CLcs.AIcs.LGcs.PLcs.SE
收藏

TokDrift:当大语言模型以子词为单位说话,而代码以语法为单位表达时

TokDrift: When LLM Speaks in Subwords but Code Speaks in Grammar

University of Waterloo(滑铁卢大学)

Yinxi Li, Yuntian Deng, Pengyu Nie

AI总结:

针对代码LLM的子词分词与代码语法错位问题,提出TokDrift框架,通过语义保持重写生成变体,发现微小格式变化即引发行为漂移,源于早期嵌入,呼吁语法感知分词。

摘要:

用于代码的大型语言模型(LLMs)依赖子词分词器,例如字节对编码(BPE),这些分词器从混合的自然语言文本和编程语言代码中学习,但由统计驱动而非语法驱动。因此,语义上相同的代码片段可能因空白或标识符命名等表面因素而被不同地分词。为了衡量这种错位的影响,我们引入了TokDrift,一个应用语义保持重写规则来创建仅在分词上有所不同的代码变体的框架。在九个代码LLM中,包括参数超过300亿的大型模型,即使是微小的格式变化也可能导致模型行为的显著变化。逐层分析表明,问题源于早期嵌入,其中子词分割未能捕捉语法词边界。我们的发现将错位的分词识别为可靠代码理解和生成的隐藏障碍,强调了未来代码LLM需要语法感知的分词。

英文摘要:

Large language models (LLMs) for code rely on subword tokenizers, such as byte-pair encoding (BPE), learned from mixed natural language text and programming language code but driven by statistics rather than grammar. As a result, semantically identical code snippets can be tokenized differently depending on superficial factors such as whitespace or identifier naming. To measure the impact of this misalignment, we introduce TokDrift, a framework that applies semantic-preserving rewrite rules to create code variants differing only in tokenization. Across nine code LLMs, including large ones with over 30B parameters, even minor formatting changes can cause substantial shifts in model behavior. Layer-wise analysis shows that the issue originates in early embeddings, where subword segmentation fails to capture grammar token boundaries. Our findings identify misaligned tokenization as a hidden obstacle to reliable code understanding and generation, highlighting the need for grammar-aware tokenization for future code LLMs.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑