Skip to main content
QUICK REVIEW

[Paper Review] Linear-Time WordPiece Tokenization.

Xinying Song, Alex Salcianu|arXiv (Cornell University)|Dec 31, 2020
Natural Language Processing TechniquesComputer Science14 references5 citations
TL;DR

This paper introduces LinMaxMatch, a linear-time algorithm for WordPiece tokenization using optimized trie structures with additional linkages inspired by the Aho-Corasick algorithm. It achieves 3x faster inference than HuggingFace and TensorFlow Text systems, with 4.5x speedup on long-tail inputs, providing both practical efficiency and theoretical optimality for the MaxMatch problem.

ABSTRACT

WordPiece tokenization is a subword-based tokenization schema adopted by BERT: it segments the input text via a longest-match-first tokenization strategy, known as Maximum Matching or MaxMatch. To the best of our knowledge, all published MaxMatch algorithms are quadratic (or higher). In this paper, we propose LinMaxMatch, a novel linear-time algorithm for MaxMatch and WordPiece tokenization. Inspired by the Aho-Corasick algorithm, we introduce additional linkages on top of the trie built from the vocabulary, allowing smart transitions when the trie matching cannot continue. Experimental results show that our algorithm is 3x faster on average than two production systems by HuggingFace and TensorFlow Text. Regarding long-tail inputs, our algorithm is 4.5x faster at the 95 percentile. This work has immediate practical value (reducing inference latency, saving compute resources, etc.) and is of theoretical interest by providing an optimal complexity solution to the decades-old MaxMatch problem.

Motivation & Objective

  • Address the long-standing inefficiency of existing MaxMatch algorithms, which are quadratic or worse, despite their widespread use in BERT and related models.
  • Develop a provably optimal linear-time solution to the MaxMatch problem in subword tokenization, a decades-old challenge in NLP.
  • Reduce inference latency and computational cost in real-world NLP systems by enabling faster, scalable tokenization.
  • Provide theoretical insight by achieving optimal time complexity for the MaxMatch problem, closing a long-standing open problem in NLP engineering.

Proposed method

  • Construct a trie from the vocabulary to represent all possible subword units used in WordPiece tokenization.
  • Augment the trie with additional failure links—inspired by the Aho-Corasick algorithm—to enable efficient transitions when current path matching fails.
  • Use a smart state transition mechanism that leverages these links to avoid backtracking and maintain linear-time performance.
  • Integrate the augmented trie into a longest-match-first (MaxMatch) tokenization pipeline, ensuring maximal subword segmentation at each step.
  • Design the algorithm to process input tokens in a single pass, maintaining O(n) time complexity where n is the input length.
  • Optimize for both average-case and long-tail input performance by minimizing redundant trie traversals through intelligent linkage propagation.

Experimental results

Research questions

  • RQ1Can a linear-time algorithm be designed for MaxMatch-based WordPiece tokenization, breaking the quadratic complexity barrier of prior approaches?
  • RQ2How does the performance of the proposed LinMaxMatch algorithm compare to production systems like HuggingFace and TensorFlow Text in terms of speed and scalability?
  • RQ3To what extent does the algorithm improve performance on long-tail inputs, such as rare or complex sequences?
  • RQ4Is it possible to achieve optimal time complexity for MaxMatch using a trie-based structure enhanced with Aho-Corasick-style failure links?
  • RQ5What is the practical impact of this algorithm on inference latency and compute resource usage in real-world NLP deployments?

Key findings

  • LinMaxMatch achieves linear time complexity O(n) for MaxMatch tokenization, providing the first optimal solution to this decades-old problem.
  • On average, LinMaxMatch is 3x faster than HuggingFace and TensorFlow Text production tokenizers across diverse input distributions.
  • At the 95th percentile of long-tail inputs, LinMaxMatch is 4.5x faster than the baseline systems, demonstrating superior scalability on complex sequences.
  • The algorithm maintains high efficiency across both short and long inputs, with consistent performance gains in real-world inference workloads.
  • The use of failure links on the trie enables smart, backtracking-free transitions, which is key to achieving linear-time performance.
  • The proposed method is both theoretically optimal and practically deployable, offering immediate benefits in low-latency NLP systems.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.