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

Fuente: arXiv
Gespeichert in:
Bibliographische Detailangaben
Hauptverfasser: Li, Yinxi, Deng, Yuntian, Nie, Pengyu
Format: Preprint
Veröffentlicht: 2025
Schlagworte:
Online-Zugang:
Tags: Tag hinzufügen
Keine Tags, Fügen Sie den ersten Tag hinzu!
_version_ 1866918161995005952
author Li, Yinxi
Deng, Yuntian
Nie, Pengyu
author_facet Li, Yinxi
Deng, Yuntian
Nie, Pengyu
contents 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.
format Preprint
id arxiv_https___arxiv_org_abs_2510_14972
institution arXiv
publishDate 2025
record_format arxiv
spellingShingle TokDrift: When LLM Speaks in Subwords but Code Speaks in Grammar
Li, Yinxi
Deng, Yuntian
Nie, Pengyu
Computation and Language
Artificial Intelligence
Machine Learning
Programming Languages
Software Engineering
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.
title TokDrift: When LLM Speaks in Subwords but Code Speaks in Grammar
topic Computation and Language
Artificial Intelligence
Machine Learning
Programming Languages
Software Engineering
url https://arxiv.org/abs/2510.14972