TRAMS: Training-free Memory Selection for Long-range Language Modeling

Haofei Yu, Cunxiang Wang, Yue Zhang, Wei Bi

Abstract

The Transformer architecture is crucial for numerous AI models, but it still faces challenges in long-range language modeling. Though several specific transformer architectures have been designed to tackle issues of long-range dependencies, existing methods like Transformer-XL are plagued by a high percentage of ineffective memories. In this study, we present a plug-and-play strategy, known as TRAining-free Memory Selection (TRAMS), that selects tokens participating in attention calculation based on one simple metric. This strategy allows us to keep tokens that are likely to have a high attention score with the current queries and ignore the other ones. We have tested our approach on the word-level benchmark (WikiText-103) and the character-level benchmark (enwik8), and the results indicate an improvement without having additional training or adding additional parameters.

Introduction

Standard Transformers face a significant "quadratic bottleneck": the computational cost of calculating attention grows with the square of the sequence length ($O(N^2)$). To mitigate this and handle long documents, architectures like Transformer-XL utilize a "memory" mechanism to cache hidden states from previous segments. However, a major inefficiency persists: a large portion of the information stored in these memories is never actually utilized during attention calculation.

Initial investigations revealed that up to 50% of memories with the lowest attention scores could be discarded without impacting performance. The core challenge lies in identifying these "high quality" memories before performing the expensive query-key multiplication. TRAMS addresses this by introducing a heuristic-based selection method that filters ineffective memories using a query-independent metric.

Conceptual comparison between Oracle memory selection (ideal) and the TRAMS heuristic (practical).

Methodology

The TRAMS strategy relies on Attention Reformulation. By mathematically rearranging the attention terms, the authors apply all learnable projection matrices ($W_Q, W_K$) to the memory side, creating a "reformulated key" ($K'$) that encapsulates the parametric information required for attention.

Based on empirical observations, the authors derived a selection metric using two primary components:

  1. Norm: Keys with a larger L2 norm ($||K'||$) are statistically more likely to produce high attention scores.
  2. Orientation: Tokens in Transformer hidden spaces are often clustered. Keys oriented closer to the $\vec{1}$ vector (which is roughly orthogonal to the query space) tend to yield higher scores.
Spearman correlation of norm and orientation components with attention scores, providing empirical support for the TRAMS metric.

The final TRAMS metric is defined as: $s = \cos \langle K', \vec{1} \rangle ||K'||$. During inference, the model ranks a pool of memories ($M$) using this score and selects only the top $m$ tokens for the attention mechanism.

Experimental Results

TRAMS was evaluated on two standard benchmarks, demonstrating consistent improvements over the Transformer-XL baseline without any retraining:

  • WikiText-103: Achieved a 0.19 reduction in perplexity (PPL).
  • enwik8: Achieved a 0.017 reduction in bits-per-character (BPC).
  • Efficiency: By selecting the top 25% of memories from a larger pool, the model achieved a substantial drop in perplexity, confirming that focusing on high-probability tokens is more effective than using a standard memory of the same size.
  • Attention Probability: Quantitatively, TRAMS improved the average attention probability by 24.25% compared to Transformer-XL for the same memory budget.

Discussion and Insights

One of the most significant findings is that Transformer-XL's memory is inherently cluttered with ineffective tokens. TRAMS proves that query-independent selection—ranking keys based on their own properties relative to the global hidden space—is a viable path for optimization. Furthermore, the authors observed that memory selection is most impactful in deeper layers (Layer 13 and above), suggesting that high-level semantic integration is where memory quality matters most.

Despite its performance gains, TRAMS introduces approximately 50% additional inference latency due to the overhead of calculating norms and sorting memories. Future work may involve integrating trainable parameters into the selection mechanism to further optimize the trade-off between computational speed and model performance.

Summary updated

BibTeX
@inproceedings{yu2023trams,
  author = {Haofei Yu and Cunxiang Wang and Yue Zhang and Wei Bi},
  title = {{TRAMS: Training-free Memory Selection for Long-range Language Modeling}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  url = {https://openreview.net/forum?id=iCLJHkE5s1},
}
Figure