In-Context Pretraining: Language Modeling Beyond Document Boundaries

Weijia Shi, Sewon Min, Maria Lomeli, Chunting Zhou, Margaret Li, Xi Victoria Lin, Noah A. Smith, Luke Zettlemoyer, Wen-tau Yih, Mike Lewis

TL;DR

Instead of pretraining language models on random "packs" of unrelated documents, In-Context Pretraining reorders the training data so that semantically related documents are placed together in the same context window. This simple, data-centric change forces models to reason across document boundaries during pretraining, leading to massive gains in reading comprehension (+15%), faithfulness (+16%), and retrieval-augmented generation (+9%).

The Problem: Context as Noise

Modern Large Language Models (LLMs) are trained on massive context windows (e.g., 8,192 tokens), but most individual documents in a training set are much shorter than that. To maximize efficiency, developers "pack" multiple documents into a single training sequence.

The catch? These documents are usually chosen at random. When a model is halfway through a training sequence and hits the boundary between a Wikipedia article about giraffes and a recipe for sourdough, the preceding text provides zero signal for predicting the next token. This teaches the model that "context" is often just noise to be ignored. Consequently, models often struggle with tasks that require reasoning across multiple pieces of information, such as multi-hop question answering or staying faithful to a provided reference text (RAG).

In-Context vs Standard Pretraining

Methods: From Random Shuffle to Coherent Narrative

The authors propose a two-step pipeline to transform pretraining from a random shuffle into a coherent narrative. Crucially, this method requires no changes to the model architecture or the underlying training objective. It is purely a preprocessing step that changes the order in which the model sees the data.

  1. Finding Related Documents at Scale: To handle billions of documents, the researchers used a dense retrieval model (Contriever) to map every document into an embedding space. They then used FAISS (an efficient similarity search library) to find the top-10 nearest neighbors for every single document in their 306-billion-token corpus.
  2. Document Graph Traversal: They framed the ordering of these documents as a Maximum Traveling Salesman Problem. The goal is to find a path through the documents that visits each one exactly once while maximizing the similarity between adjacent documents. Since this is NP-hard, they used a greedy algorithm: start with a document, move to its most similar unvisited neighbor, and if you get stuck, jump to a new "starting" point with the fewest neighbors.
Document Graph Traversal and Packing

Datasets

  • Pretraining: A subset of the English CommonCrawl dataset (235 million documents, totaling 306 billion tokens).
  • Evaluation:
    • Language Modeling: Wikipedia, Arxiv, and Books.
  • Classification: 7 datasets including SST2 (sentiment) and Agnews (topic).
  • Reading Comprehension: RACE, SQuAD, BoolQ, DROP, and HotpotQA.
  • Factuality/Faithfulness: NQ-Swap and MemoTrap (testing if a model follows context even when it contradicts its internal "knowledge").
  • Long Context: The SCROLLS benchmark.

Key Insights and Ideas

  • Context as a Signal, Not Noise: By placing related documents together, the model can actually use the first document to help predict the second. For example, if Doc A mentions a specific date and Doc B discusses an event on that date, the model learns to "look back" to find the answer.
  • The "Aha" Moment on Faithfulness: One of the most striking findings was the improvement in faithfulness. Standard models often ignore their context if it contradicts what they "memorized" during training. In-Context Pretraining teaches the model that the provided context is highly relevant, making it 16% more likely to stick to the provided facts.
  • Deduplication is Non-Negotiable: The authors found that if they didn't strictly deduplicate the data, the model would learn to simply "copy-paste" from one document to the next (since the nearest neighbor is often a near-duplicate). Proper semantic deduplication was essential to force actual reasoning rather than just memorization.

Key Results

The researchers trained models ranging from 300M to 7B parameters. The "In-Context" models (ICLM) consistently outperformed standard baselines:

  • Reading Comprehension: A massive 15% average improvement across 8 tasks.
  • In-Context Learning: An 8% boost in few-shot performance, suggesting the model is better at "learning how to learn" from examples.
  • Retrieval Augmentation (RAG): When given 10 retrieved documents from Wikipedia to answer a question, the ICLM showed a 9% gain over the standard model.
  • Efficiency: The ICLM achieved lower training loss (perplexity) faster than the baseline, meaning it learned more effectively from the same amount of data.

Limitations and Future Work

  • Computational Overhead: While the authors used efficient approximate algorithms, finding the nearest neighbors for billions of documents is still a significant one-time preprocessing cost.
  • The "Copying" Risk: If the retrieval/deduplication pipeline isn't perfect, the model might over-rely on looking back for exact string matches rather than semantic reasoning.
  • Future Directions: The authors suggest this could be expanded to multilingual settings (linking documents on the same topic in different languages) or code (linking files within the same repository) to create even more powerful cross-boundary signals.

Summary updated

BibTeX
@inproceedings{shi2024context,
  author = {Weijia Shi and Sewon Min and Maria Lomeli and Chunting Zhou and Margaret Li and Xi Victoria Lin and Noah A. Smith and Luke Zettlemoyer and Wen-tau Yih and Mike Lewis},
  title = {{In-Context Pretraining: Language Modeling Beyond Document Boundaries}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=LXVswInHOo},
}
Figure