Flexibly Scaling Large Language Models Contexts Through Extensible Tokenization

Ninglu Shao, Shitao Xiao, Zheng Liu, Peitian ZhangarXiv 2024 · DOI

Introduction

Large Language Models (LLMs) are fundamentally limited by their context windows, typically capping at 4k to 32k tokens. This constraint hinders applications requiring the processing of extensive data, such as retrieval-augmented generation (RAG) or long-document analysis. While existing solutions like fine-tuning or sparse attention offer improvements, they often come with high computational costs or hardware incompatibilities.

Extensible Tokenization (ET) addresses this by introducing a "plug-and-play" middleware designed to increase information density. Rather than expanding the LLM's internal window, ET compresses raw token embeddings into dense "extensible embeddings" (super tokens), allowing the model to perceive significantly more information within its existing fixed context length.

Methodology

The core of this approach is the Extensible Tokenizer, a standalone module that sits between the input text and the downstream LLM.

Overview of the Extensible Tokenization architecture.

As illustrated in the architecture, the process involves several key steps:

  1. Tokenization: A small, pre-trained model (e.g., the first 8 layers of Llama-2-7B) transforms raw token embeddings into a sequence of hidden states.
  2. Strided Down-scaling: Data is compressed using a scaling factor $k$. For instance, at $k=32$, every 32nd embedding is sampled as a "super token" that represents the cumulative contextual information of that block.
  3. Two-Stream Training: A specialized training approach optimizes the tokenizer while keeping the downstream LLM frozen. This ensures the base model's original capabilities remain intact.
  4. Flexible Inference: The scaling factor $k$ can be adjusted dynamically, allowing users to mix raw tokens for precise instructions with compressed tokens for long background context.

Experimental Setup

The model was trained on 100,000 samples from high-quality sources, including RedPajama and LongAlpaca. Evaluation was conducted using benchmarks designed for long-context understanding:

  • PG19 & Books3: Used to measure next-word prediction (perplexity) over sequences up to 100K tokens.
  • LongBench: A comprehensive suite for tasks like multi-doc QA and summarization.

Results and Discussion

Extensible Tokenization demonstrates superior scaling capabilities and resource efficiency compared to standard context-extension methods.

Performance comparison of Extensible Tokenization against baseline methods.

Massive Context Scaling

The method successfully extended Llama-2-7B's context to over 100K tokens. When applied to LongChat-32K, it reached a context length of 1 million tokens. As shown in the performance plots, ET maintains stable perplexity at extreme lengths where baseline methods like Positional Interpolation (PI) or NTK-Aware RoPE either degrade or fail due to memory limits.

Efficiency and Resource Gains

ET achieves linear time complexity and a massive reduction in GPU memory usage. In tests with a 100K context, standard models encountered Out-of-Memory (OOM) errors, while ET operated efficiently with just 17.54 GB of GPU memory. Furthermore, inference time for a 32K context was significantly faster (0.23s vs. 3.32s for standard methods).

Preservation of Base Capabilities

Because the downstream LLM is never modified, its performance on standard, short-context tasks remains completely preserved. This avoids the common trade-off where extending context windows leads to performance degradation on general NLP tasks.

Conclusion

Extensible Tokenization provides a scalable and modular path for long-context LLM applications without the need for expensive re-training. Future research may explore larger tokenizer models for enhanced compression and the optimization of scaling factors for specific downstream tasks.

Summary updated

BibTeX
@misc{shao2024flexibly,
  author = {Ninglu Shao and Shitao Xiao and Zheng Liu and Peitian Zhang},
  title = {{Flexibly Scaling Large Language Models Contexts Through Extensible Tokenization}},
  year = {2024},
  eprint = {2401.07793},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2401.07793},
}
Figure