Introduction
Tokenization serves as the foundational entry point for Natural Language Processing (NLP) pipelines. Modern transformer models, most notably BERT, rely on WordPiece tokenization to decompose text into sub-word units (e.g., "tokenization" becomes ["token", "##iza", "##tion"]). Traditionally, WordPiece employs a greedy Maximum Matching (MaxMatch) strategy: it identifies the longest prefix in the vocabulary, "pops" it, and repeats the process for the remaining string.
While conceptually simple, this approach historically suffered from a time complexity of $O(nm)$, where $n$ is the input length and $m$ is the maximum token length in the vocabulary. This "multiplicative tax" becomes a significant bottleneck when processing massive datasets or deploying models on resource-constrained edge devices. This paper introduces LinMaxMatch, the first algorithm to achieve true $O(n)$ linear-time complexity for WordPiece tokenization.
The LinMaxMatch Algorithm
The core innovation of LinMaxMatch lies in eliminating backtracking by precomputing state transitions, drawing inspiration from the Aho-Corasick algorithm for multi-pattern matching.
Trie-Based Representation and Failure Links
The algorithm represents the vocabulary as a trie (prefix tree). To achieve linear time, the authors introduce two critical precomputed components:
- Failure Link ($f(v)$): If the algorithm reaches a node $v$ and the next character in the input does not match any outgoing edge, the failure link directs the search to the next appropriate node in the trie without re-reading any input characters.
- Failure Pop ($F(v)$): Because WordPiece is greedy, jumping via a failure link might bypass valid tokens that should have been emitted. The "failure pop" is a precomputed list of tokens that the algorithm outputs immediately when a failure link is traversed.
End-to-End (E2E) Tokenization
Standard tokenization pipelines typically involve a two-step process: splitting text by whitespace/punctuation followed by WordPiece. LinMaxMatch extends to an "End-to-End" version that handles punctuation and whitespace directly within the trie traversal. This allows for processing an entire sentence in a single, unified linear pass.
Key Insights: Tokenization as a Finite State Transducer
The authors demonstrate that LinMaxMatch can be formally viewed as a Finite State Transducer (FST). By precalculating all possible transitions, the tokenization process is reduced to a simple state-machine transition for every input character. This deterministic behavior ensures that the "longest-match-first" logic is preserved without the need for repeated scanning.
As shown in the trace above, the algorithm maintains a constant flow through the input string, emitting tokens like [a, ##b, ##c, ##dz] through a series of transitions and failure link jumps, never revisiting a character once it has been read.
Experimental Results
The algorithm was evaluated using the official BERT-Base, Multilingual Cased vocabulary (104 languages) against a multilingual Wikipedia dataset.
- Performance Gains: LinMaxMatch is 8.2x faster than the Rust-based HuggingFace Tokenizers and 5.1x faster than the C++ TensorFlow Text implementation.
- Linear Scaling: Empirical tests confirm that the runtime grows perfectly linearly with input length, providing predictable latency even for long or complex sequences.
- Compatibility: The algorithm is a drop-in replacement, producing identical token IDs to the original WordPiece implementation.
Conclusion and Future Work
LinMaxMatch effectively breaks the $O(nm)$ barrier for WordPiece tokenization, offering a significant speedup for NLP infrastructure. While it requires an offline precomputation step and a slightly larger memory footprint for the trie, the benefits for high-throughput or low-latency applications are substantial. Future research may explore extending these linear-time techniques to other tokenization methods like Byte-Pair Encoding (BPE) or Unigram models.
Summary updated