Finding Optimal Tokenizers

Open original

The author frames tokenizer design as an integer linear program, relaxes it to a continuous LP, and uses cutting planes to close the gap between fractional and integral solutions. They automate cut discovery with Codex, apply cycle constraints on overlapping token edges, and report provably optimal tokenizers on small pretokenized datasets.

  • BPE, despite being greedy, actually gets within 1% of the true optimal tokenizer, as revealed by casting tokenization as an ILP with a provable lower bound.
  • The LP relaxation plus rounding gives a feasible upper bound, and cutting planes (constraints violated by fractional solutions but satisfied by all integral ones) tighten the gap between the two.
  • Codex was used to automatically discover these cutting planes by brute-forcing small word pairs/triplets and solving auxiliary LPs to find maximally violated constraints, rather than deriving them by hand.
  • Results are only provably optimal under a fixed pretokenization scheme, since dropping that assumption would make the LP intractably large.

Researchers tackled the problem of finding an optimal tokenizer—a vocabulary that minimizes token count for a fixed size. Normally, byte-pair encoding (BPE) does this greedily, but it can fall within 1% of optimal only. Tempus et al. recast tokenization as an integer linear program: one binary “color” variable per possible substring (whether it’s in the vocab), edge variables for each occurrence, plus flow constraints to ensure a valid parse. They relax the ILP to a continuous LP, solve it, then round the color variables to get a feasible but suboptimal tokenizer, using the LP’s objective as a lower bound and the rounded solution as an upper bound. Building on TSP solvers’ cutting‐plane methods, the author asked OpenAI’s Codex to generate valid cuts for the tokenization LP. A cut is a linear constraint that all integral solutions satisfy but the current fractional solution violates. Instead of manually crafting cuts, they brute‐forced small projections: pick pairs or triplets of words where color variables were fractional, set up auxiliary LPs to maximize violation, then add those constraints back into the main LP. This raised the lower bound and improved the rounded tokenizer’s performance. Pretokenization (splitting text into words and merging repeats) remains a limiting assumption. It slashes variable count—crucial for tractability—but means the solution is only optimal under that pretokenizer. Removing it could yield true global optimality but would balloon the LP’s size. Future work might explore cutting planes that work on raw byte sequences or find more efficient heuristics to guide Codex’s search for violated constraints.

Ask about this article