Retrofitting Large Language Models with Dynamic Tokenization

Darius Feher, Ivan Vulic, Benjamin Minixhofer

Introduction

Modern Large Language Models (LLMs) typically rely on static subword tokenizers, such as BPE or WordPiece, which are fixed after the initial training phase. While these tokenizers are standard, they impose a "tokenizer tax" that manifests in several ways. In non-English languages, tokenizers often over-segment text into numerous small fragments, leading to significantly longer sequences for the same semantic content. This increases inference costs, latency, and creates a performance disparity across languages. Furthermore, static vocabularies cannot easily adapt to new domains or evolving terminology without expensive retraining.

This paper introduces a method to "retrofit" existing LLMs with dynamic tokenization. By allowing the model to adjust token boundaries on-the-fly, the researchers aim to improve inference efficiency and cross-lingual fairness without the need for full model retraining.

Methodology: Dynamic Merging and Hypernetworks

The proposed retrofitting process enables dynamic tokenization through two primary mechanisms:

  1. Dynamic Merging: For any given batch of text, the system optimizes tokenization locally. It begins with standard subword tokens and applies a merging algorithm to identify frequent adjacent pairs within that specific batch. These pairs are merged into longer "dynamic tokens," effectively compressing the input sequence before it is processed by the transformer layers.
  2. Embedding Prediction via Hypernetworks: A core challenge is that these newly created dynamic tokens do not exist in the model's original vocabulary and thus lack pre-defined embeddings. To solve this, the authors utilize a Hypernetwork (HN)—a compact auxiliary model trained to predict the embedding of any arbitrary string based on its constituent characters or subwords.

For encoder models like XLM-R, a small LoRA adapter is integrated to help the model handle these coarser tokens. For decoder models like Mistral-7B, this approach is applied to the "prefilling" (prompt processing) and "scoring" stages. In the context of autoregressive generation, the vocabulary is expanded to 1 million tokens, and an Approximate Nearest Neighbor (ANN) search is used to efficiently select the next token.

Figure 1: Overview of the dynamic tokenization architecture using a Hypernetwork to predict embeddings for merged tokens.

Experimental Setup

The researchers evaluated their framework across a variety of multilingual and general benchmarks to test both sentence-level and token-level performance:

  • XNLI (Cross-lingual Natural Language Inference): Evaluated across 13 languages to measure multilingual sentence understanding.
  • UNER (Universal Named Entity Recognition): Tested on 4 languages for token-level precision.
  • MMLU-lite: Used for zero-shot evaluation of decoder models across 5 languages.
  • MT-Bench: Assessed conversational and generative capabilities of instruction-tuned models.

Key Results and Insights

The empirical results demonstrate that the limitations of a fixed tokenizer can be effectively bypassed. By using hypernetworks to "hallucinate" valid embeddings for longer, merged sequences, the model achieves significant compression with minimal impact on quality.

  • Sequence Compression: For encoder models, the method reduced sequence lengths by an average of 22.5% on XNLI and 26.4% on UNER.
  • Performance Retention: This compression came at a minimal cost, with less than a 2% drop in accuracy for XNLI and a 1.7% drop in F1-score for UNER.
  • Efficiency Gains: In decoder models like Mistral-7B, sequence length during prefilling was reduced by up to 17%. The computational overhead of the hypernetwork remained negligible, typically under 3.1% of total FLOPs.
Figure 4: Performance trade-offs on XNLI. (a) Mean accuracies across 13 languages and (b) accuracies for English as a function of sequence length reduction.

Beyond raw metrics, the study highlights that dynamic tokenization can improve fairness. By reducing sequence lengths in languages like Swahili or Turkish more aggressively than in English, the method levels the computational cost per sentence. Interestingly, the authors found that the slight inaccuracies introduced by ANN search during generation could act as a form of regularization, sometimes improving overall generation quality.

Limitations and Future Work

While promising, the method has dependencies and areas for improvement. It relies on the availability of high-quality pre-trained hypernetworks for specific LLMs. Additionally, while prefilling and scoring work well, full autoregressive generation with an unbounded vocabulary still exhibits a performance gap compared to using original embeddings. Managing the embedding cache (LRU cache) required for efficiency also adds engineering complexity. The authors suggest that the ultimate goal is "true" dynamic tokenization, where models learn to propose their own token boundaries during the initial training phase.

Summary updated

BibTeX
@inproceedings{feher2025retrofitting,
  author = {Darius Feher and Ivan Vulic and Benjamin Minixhofer},
  title = {{Retrofitting Large Language Models with Dynamic Tokenization}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure