Abstract Byte-Pair Encoding (BPE) is the standard tokenization method for modern large language models, yet it has historically been treated as a heuristic rather than a formal mathematical process. This paper provides a rigorous theoretical framework for BPE, formalizing it as a submodular optimization problem. We prove that the standard greedy BPE algorithm provides a provable approximation of the optimal merge sequence. Furthermore, we introduce a new data structure that improves BPE training complexity from $O(NM)$ to $O(N \log M)$, enabling significant speedups for large-scale datasets.
Introduction
Byte-Pair Encoding (BPE) is the foundational method for transforming raw text into the discrete tokens processed by neural networks like GPT-4 and Llama. Despite its ubiquity, BPE's performance guarantees and theoretical limits have remained largely unexamined. Originally designed for data compression in the 1990s, the NLP community adopted BPE's greedy approach: iteratively merging the most frequent adjacent character pairs. This paper moves BPE from a heuristic "it just works" status into the realm of combinatorial optimization, offering the first theoretical guarantees for its performance.
Theoretical Framework: Submodularity and Curvature
The authors define a "compression utility" function, $\kappa$, which quantifies the characters saved by a specific sequence of merges. A central contribution of this work is the proof that $\kappa$ is monotone and submodular. Specifically, they identify a property termed "hierarchical sequence submodularity," which formalizes the law of diminishing returns in tokenization: a merge operation performed early in the process typically provides more utility than the same merge performed later, after the string has already been simplified.
To quantify the gap between the standard greedy approach and a theoretical optimum, the authors utilize the concept of "total backward curvature" ($\sigma$). This allows for the derivation of a provable lower bound: the greedy algorithm is a $\frac{1}{\sigma(\mu^\star)}(1-e^{-\sigma(\mu^\star)})$-approximation of the optimal sequence. Empirically, for natural languages like English, this suggests that greedy BPE captures between 37% and 43% of the maximum possible compression utility.
Algorithmic Efficiency
Beyond theoretical bounds, the paper addresses the computational bottleneck of BPE training. Traditional implementations scale linearly with both the corpus length ($N$) and the number of merges ($M$), resulting in $O(NM)$ complexity. This becomes prohibitive as training sets reach trillions of tokens.
The authors propose an optimized algorithm that utilizes a doubly linked list to represent the string and a max-heap (priority queue) to track pair frequencies. As shown in the visualization above, this structure allows the algorithm to update only the local segments of the string affected by a merge, rather than re-scanning the entire corpus. This shift reduces the complexity to $O(N \log M)$, a substantial improvement for large-scale NLP applications.
Greedy vs. Optimal Performance
While the greedy algorithm is efficient, the authors explored how it differs from an absolute optimal search. By employing a memoized Depth First Search (DFS), they were able to find optimal vocabularies for smaller datasets.
As illustrated in the table above, the greedy approach can sometimes prioritize pairs that lead to less efficient long-term compression compared to an optimal sequence. However, the authors found that for natural language, many different merge sequences are functionally equivalent, resulting in the same final bracketing of the text. This redundancy suggests that the greedy heuristic is remarkably robust for linguistic data.
Conclusion
This formalization provides the necessary tools to evaluate BPE across different linguistic contexts. By proving submodularity and providing an $O(N \log M)$ implementation, the authors have grounded a critical piece of the modern AI stack in rigorous theory. Future research can now use this framework to investigate whether BPE's optimality varies across morphologically diverse languages or different data modalities.
Summary updated