Understanding and Mitigating Tokenization Bias in Language Models

Buu Phan, Marton Havasi, Matthew J. Muckley, Karen Ullrich · DOI

TL;DR

Language models use tokenization to process text, but common methods like MPE and BPE introduce a "sampling bias" that skews predictions and can't be fixed by more data. This paper introduces novel algorithms to remove this bias, allowing tokenized models to behave like "token-free" models without retraining.

What Problem Does This Paper Address?

Modern language models (LMs) process text by breaking it down into smaller units called "tokens." This tokenization step is crucial for handling large vocabularies and managing context length. However, popular tokenization schemes like Maximum Prefix Encoding (MPE) and Byte-Pair Encoding (BPE) can introduce a subtle but significant problem: a "sampling bias." This bias means that the model's predictions about the next character are skewed because the tokenization process itself imposes certain constraints. For example, a tokenizer might force a specific sequence of characters to always be represented by a single token, even if other sequences are more probable in a token-free setting. The authors show that this bias persists even with more training data and cannot be fixed by simply fine-tuning the model. This fundamentally limits our understanding of what tokenized LMs truly learn and how they differ from hypothetical "token-free" models.

Methods

The core contribution of this paper is the development of algorithms to mitigate tokenization bias. The authors propose a two-stage approach:

  1. Refactoring: This stage aims to mathematically express the desired token-free character-level probability using the tokenized LM's output. This involves carefully decomposing the probability of the next character based on the tokenized sequence.
  2. Maximum Prefix Correction (MPC) Algorithm: This novel algorithm is designed to compute the refactored probabilities. It works by considering two complementary events: when the next token starts with the target character sequence (the "branch step") and when it contains it (the "pass step"). The MPC algorithm recursively computes these probabilities, effectively simulating token-free behavior.

The paper also introduces a Truncate-Renormalization (TR) process to ensure that even LMs that don't strictly adhere to the theoretical properties of tokenization (due to softmax activations) can be made to comply, guaranteeing lower token-level perplexity. For Byte-Pair Encoding (BPE), a related Byte-Pair Correction (BPC) Algorithm is presented, which efficiently searches for "cover encodings" to achieve the same bias removal.

Datasets

The paper does not explicitly mention training or evaluating on specific large-scale datasets. Instead, it uses a 3rd-order Markov chain as a controlled experimental setup to demonstrate the effectiveness of their methods. This allows for precise control over the ground truth probabilities and a clear isolation of the tokenization bias.

Key Insights and Ideas

  • Tokenization bias is inherent and persistent: Popular tokenization methods like MPE and BPE introduce a sampling bias that is not a bug but a feature of the encoding process. This bias cannot be fixed by simply training on more data or fine-tuning.
  • Tokenized LMs can simulate token-free behavior: The proposed algorithms allow us to extract the implicitly learned token-free probabilities from a tokenized LM. This means we can effectively "undo" the tokenization bias.
  • Theoretical equivalence: The work suggests that tokenized LMs, when corrected, are statistically equivalent to their token-free counterparts. This is a significant theoretical insight.
  • Bias affects more than just next-token prediction: The bias extends to character-level predictions, impacting how LMs understand and generate text.
  • New algorithms for bias correction: The MPC and BPC algorithms provide a principled and theoretically grounded way to remove this bias without requiring model retraining.

Key Results

  • Accurate recovery of ground truth: In experiments with a 3rd-order Markov chain, the proposed method accurately recovered the ground truth transition probabilities, while baseline methods (directly using tokenized LM outputs) exhibited significant sampling bias.
  • Demonstrated bias in BPE and MPE: The paper empirically shows that both MPE and BPE tokenization schemes introduce this bias.
  • Theoretical guarantees: The proposed algorithms are shown to be theoretically sound, providing unbiased estimates and enabling the simulation of token-free behavior.
  • Efficiency: The MPC algorithm's complexity scales linearly with the sequence length for MPE, making it practical.

Limitations and Future Work

  • Theoretical focus: While the paper demonstrates the concept on a Markov chain, the practical application to large, complex language models on real-world datasets is not extensively shown, though the authors claim their method is general.
  • Computational cost: While the MPC algorithm is linear in sequence length for MPE, the BPC algorithm's efficiency for BPE might still be a concern for very long sequences, as it involves searching through cover encodings.
  • Future work: The authors suggest that their method opens the door to simulating the behavior of models trained with different tokenizers without fine-tuning. This implies potential for cross-tokenizer transfer learning and a deeper understanding of how tokenization choices impact model capabilities. They also hint at extending their work to non-optimal LMs.

Summary updated

BibTeX
@misc{phan2024understanding,
  author = {Buu Phan and Marton Havasi and Matthew J. Muckley and Karen Ullrich},
  title = {{Understanding and Mitigating Tokenization Bias in Language Models}},
  year = {2024},
  eprint = {2406.16829},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2406.16829},
}
Figure