Byte BPE Tokenization as an Inverse string Homomorphism

Saibo Geng, Sankalp Gambhir, Chris Wendler, Robert WestarXiv 2024

Abstract

Tokenization is an important preprocessing step in the training and inference of large language models (LLMs). While there has been extensive research on the expressive power of the neural architectures used in LLMs, the impact of tokenization has not been well understood. In this work, we demonstrate that tokenization, irrespective of the algorithm used, acts as an inverse homomorphism between strings and tokens. This suggests that the character space of the source language and the token space of the tokenized language are homomorphic, preserving the structural properties of the source language. Additionally, we explore the concept of proper tokenization, which refers to an unambiguous tokenization returned from the tokenizer. Our analysis reveals that the expressiveness of neural architectures in recognizing context-free languages is not affected by tokenization.

Introduction

Tokenization is the foundational step in any Large Language Model (LLM) pipeline, yet it is frequently treated as a heuristic "black box." As models like GPT-4 and LLaMA process increasingly complex data—from mathematical proofs to source code—researchers have questioned whether the arbitrary nature of Byte Pair Encoding (BPE) destroys the underlying syntax of the language. If a tokenizer fragments a structured string into a sequence of seemingly random IDs, does the resulting "token language" still retain the formal complexity of the original character-based language?

This paper provides a mathematical proof that byte-level tokenization preserves the structural complexity of a language. By framing detokenization as a string homomorphism, we demonstrate that if a language is regular or context-free, its tokenized counterpart remains just as structured. This ensures that neural architectures like Transformers are theoretically capable of recognizing these patterns despite the fragmented nature of sub-word splitting.

Theoretical Framework: Tokenization as an Inverse Homomorphism

The core of our analysis lies in applying Automata Theory and Formal Language Theory to the tokenization process. We define tokenization ($f_{tok}$) as a mapping from characters to token IDs and detokenization ($f_{detok}$) as its inverse.

While tokenization itself is not a homomorphism—since the token for a string like "[]" is often a single unique ID rather than the concatenation of the tokens for "[" and "]"—the process of detokenization is a clear homomorphism. It maps a sequence of discrete IDs back to a sequence of strings, preserving the concatenation operation. Consequently, tokenization can be mathematically treated as an inverse homomorphism.

Construction of a token-space PDA M' that recognizes tokenized sequences by simulating a character-level PDA M using a buffer.

A fundamental result in computer science is that Regular and Context-Free Languages (CFLs) are closed under inverse homomorphism. This means that if the original character language is a CFL, the set of all its possible tokenizations is guaranteed to remain a CFL. To prove this constructively, we describe a method to build a Pushdown Automaton (PDA) that recognizes token sequences. As shown in the figure above, this PDA ($M'$) uses a buffer to reconstruct characters from the incoming token stream, feeding them into an underlying character-based PDA ($M$).

Case Study: The Dyck Language and Structural Preservation

To illustrate these theoretical findings, we analyze the language of balanced parentheses, known as the Dyck language. This is a primary example of a context-free language that mirrors the nested structures found in programming and mathematics.

BPE tokenization of balanced parentheses strings at different depths, showing how structure is fragmented into token IDs.

As seen in the table above, BPE tokenizers (such as those used in GPT-2 or LLaMA) split nested parentheses into different token IDs based on the depth of nesting and the specific merge rules learned during training. Despite this fragmentation, the underlying context-free structure is preserved. Our proof shows that a model does not need to see raw characters to understand the grammar; the structural properties are inherent in the token sequence itself.

Key Insights and Results

  • The Detokenization Perspective: The critical insight is focusing on the detokenizer rather than the tokenizer. Because detokenization simply glues strings together, it is a mathematically "clean" homomorphism, allowing the use of established closure theorems.
  • Unicode Compatibility: Modern tokenizers operate at the byte level. This means Unicode characters, which may span multiple bytes, are integrated into the same formal framework without losing structural properties.
  • Expressivity: The formal proof that structural properties are preserved implies that the expressivity of neural architectures in recognizing complex grammars is not fundamentally limited by the tokenization process. If a Transformer can recognize a character-level CFL, it can theoretically recognize its tokenized version.

Discussion and Limitations

While we prove that the set of all possible tokenizations (Extended Tokenization) is well-behaved, a gap remains regarding "Proper Tokenization"—the specific path chosen by the BPE algorithm. BPE uses a multi-pass "unmerge-remerge" logic that is more complex than simple finite-state transitions. Furthermore, practical heuristics like "leading space" handling in models like LLaMA can technically break the homomorphism property, though these can often be addressed by adjusting the language definition.

Finally, while we prove what is theoretically possible for a model to learn, future work is needed to determine if current training objectives, such as next-token prediction, allow models to discover these underlying homomorphic structures efficiently in practice.

Summary updated

BibTeX
@misc{geng2024byte,
  author = {Saibo Geng and Sankalp Gambhir and Chris Wendler and Robert West},
  title = {{Byte BPE Tokenization as an Inverse string Homomorphism}},
  year = {2024},
  eprint = {2412.03160},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2412.03160},
}
Figure