Fast WordPiece Tokenization

Xinying Song, Alex Salcianu, Yang Song, Dave Dopson, Denny Zhou

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.

The algorithm represents the vocabulary as a trie (prefix tree). To achieve linear time, the authors introduce two critical precomputed components:

  1. 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.
  2. 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.
The vocabulary trie and its corresponding precomputed failure links and pops.

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.

A step-by-step trace of the LinMaxMatch algorithm processing an input string.

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

BibTeX
@inproceedings{song2021fast,
  author = {Xinying Song and Alex Salcianu and Yang Song and Dave Dopson and Denny Zhou},
  title = {{Fast WordPiece Tokenization}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2021},
  doi = {10.18653/v1/2021.emnlp-main.160},
}
Figure