Token Weighting for Long-Range Language Modeling

Falko Helm, Nico Daheim, Iryna Gurevych

Abstract

Standard Large Language Model (LLM) training employs a next-token prediction objective that assigns equal weight to every token in the loss function. This approach fails to distinguish between trivial local predictions and critical long-range dependencies (LRDs), often leading to the "lost in the middle" phenomenon in long-context tasks. This paper introduces a Token Weighting framework that prioritizes tokens based on their contextual importance. By weighting training tokens according to the difference in "surprise" between long-context and short-context models, we demonstrate significant improvements in long-range reasoning and retrieval capabilities without the need for massive new datasets.

Introduction

As LLMs are increasingly deployed to process extensive documents—such as legal contracts, technical manuals, and large codebases—the ability to effectively utilize long-range context becomes paramount. However, standard training objectives dilute the signal required for long-distance reasoning by treating common words (e.g., "the") with the same importance as specific facts mentioned thousands of tokens earlier. This research addresses this imbalance by proposing a non-uniform loss weighting scheme that focuses the model's learning capacity on tokens that truly benefit from extended context.

Methodology: The Token Weighting Framework

The proposed framework consists of two primary stages: Token Scoring and Postprocessing.

1. Token Scoring

The importance of a token is determined by comparing the confidence of two models: a "long-context" model (with access to the full sequence) and a "short-context" model (limited to a small local window). The scoring function is defined by the absolute difference in log-probabilities:

$$Score = | \log p_{short}(token) - \log p_{long}(token) |$$

This metric identifies tokens that are significantly easier to predict with more context (LRDs) or tokens that the model is currently in the process of mastering (learnable tokens). Using the absolute value ensures that the model focuses on both types of critical signals.

2. Postprocessing and Weighting

Once scores are assigned, the training loss is adjusted using one of two schemes:

  • Dense Weighting: All tokens retain a non-zero weight, but those with higher scores receive proportionally more emphasis.
  • Sparse Weighting: Only the top $X\%$ of tokens (the most "surprising" or context-dependent) contribute to the training loss, while the remaining tokens are zeroed out.

To maintain computational efficiency, the researchers found that using a "frozen" version of the base model or even a smaller, cheaper model (weak-to-strong generalization) as a scorer is highly effective, avoiding the need to train two models simultaneously.

The Four-Quadrant Token Map

The authors categorize tokens into four distinct types based on model confidence, which helps explain why uniform weighting is suboptimal:

  1. Easy Tokens: Both models are certain (e.g., common syntax or high-frequency words). These provide little learning signal for long-context reasoning.
  2. Hard Tokens: Both models are uncertain (e.g., random strings or unpredictable transitions). These often represent noise.
  3. Learnable Tokens: Both models are uncertain, but the model is beginning to show improvement. These are valuable for general language acquisition.
  4. Long-Range Dependencies (LRD): The long-context model is certain, while the short-context model is lost. This is the "sweet spot" for teaching a model to utilize its extended context window.

Experimental Results

The framework was evaluated by continually pre-training Llama-3 8B and Phi-2 2.7B on the PG-19 corpus (Project Gutenberg books). Performance was measured across several benchmarks, including RULER (synthetic long-context tasks), Longbench (real-world QA and summarization), and standard benchmarks like MMLU to monitor general knowledge retention.

Key Findings

  • Performance Gains: Non-uniform token weighting consistently outperformed standard uniform training. On the RULER benchmark (32k context), the "Sparse Frozen" method achieved a score of 88.84, significantly higher than the 87.38 of uniform weighting and the 66.60 of the un-tuned baseline.
  • Retrieval Specialization: Sparse weighting proved exceptionally effective for "needle-in-a-haystack" tasks, forcing the model to ignore the "noise" of easy tokens and focus on rare, context-dependent signals.
  • Sparsity Trade-offs: While sparse weighting (ignoring up to 80% of tokens) creates a specialist in long-range retrieval, it can lead to a slight decrease in general short-context knowledge (e.g., MMLU scores). Dense weighting offers a more balanced profile for general-purpose use.

Conclusion and Future Work

Token weighting provides a scalable and efficient method for context extension, allowing models to better navigate the complexities of long-form text. While the current study focused on context windows up to 32k tokens, the results suggest that steering the model's attention during training is more effective than simply increasing the volume of data. Future research will explore the application of this method to extreme context scales (128k+ tokens) and investigate how token weighting interacts with instruction finetuning and Reinforcement Learning from Human Feedback (RLHF).

Summary updated

BibTeX
@inproceedings{helm2025token,
  author = {Falko Helm and Nico Daheim and Iryna Gurevych},
  title = {{Token Weighting for Long-Range Language Modeling}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2025},
  doi = {10.18653/v1/2025.findings-naacl.79},
}
Figure