RCStat: A Statistical Framework for using Relative Contextualization in Transformers

Debabrata Mahapatra, Shubham Agarwal, Apoorv Saxena, Subrata MitraarXiv 2025 · DOI · arXiv

Abstract

Standard Transformer attention mechanisms rely on Softmax-normalized weights, which often obscure subtle contextual signals and introduce structural biases like attention sinks. We present RCStat, a statistical framework that analyzes raw attention logits to quantify "Relative Contextualization" (RC). By treating pre-softmax scores as random variables, RCStat enables precise token attribution and efficient KV cache compression—reducing memory usage by up to 80%—without requiring model retraining. Our results across multiple benchmarks demonstrate that RCStat significantly outperforms heuristic-based methods in both generation quality and attribution accuracy.

Introduction

In a standard Transformer, the attention mechanism calculates relevance scores between tokens using a dot product, which is then passed through a Softmax function. While Softmax is necessary for training, it creates a "structural bias" during inference by aggressively sharpening the highest scores and flattening everything else. This leads to two major issues:

  1. Information Loss: Subtle but meaningful contextual signals are "squashed," making it difficult to identify which tokens truly drive specific outputs.
  2. Attention Sinks: Softmax often forces the model to allocate disproportionate attention to irrelevant tokens (like the start-of-sentence <s> token), leading to sub-optimal memory management in the Key-Value (KV) cache.

RCStat addresses these challenges by operating directly in the logit space (pre-softmax) to recover the raw signal.

Methodology: Relative Contextualization

The core of RCStat is the Relative Contextualization (RC) metric, a probabilistic framework that distinguishes between Cross-Contextualization (CC)—how the prompt influences the generation—and Self-Contextualization (SC)—how generated tokens influence each other.

Probabilistic modeling of Cross-Contextualization (CC) and Self-Contextualization (SC) in the logit space.

By modeling attention scores as random variables, RC measures how much more a prompt segment influences a generation compared to the generation's own internal influence. To maintain computational efficiency, the authors derive a practical upper bound based on the "Area Under CDFs" (Cumulative Distribution Functions), reducing the complexity from $O(n^3)$ to $O(n \log n)$. This allows for Adaptive Eviction in KV cache compression, where the framework identifies which attention heads are performing the most contextual work and preserves their memory while pruning others.

Evaluation and Key Results

The framework was evaluated using LLaMA-3 (3B and 8B) models across diverse benchmarks, including SQuAD v2.0 (QA), QMSum (summarization), and VERI-GRAN (grounded generation).

Comparison of pre-softmax and post-softmax distributions for token attribution, highlighting how raw logits preserve attribution signals.

KV Cache Compression

RCStat improves generation quality by 15–40% compared to state-of-the-art methods like SnapKV or H2O at high compression ratios. By intelligently selecting heads to prune, it achieves 2–5% higher total compression with minimal quality loss.

Attribution Accuracy

By selecting only the top 2% of attention heads ranked by RC, the framework boosted attribution accuracy by 2–3% on summarization and QA benchmarks. Conversely, using the "worst" heads caused accuracy to plummet, confirming that RC successfully identifies the most informative components of the model.

Task and Layer Insights

  • The Middle Layer: The most influential "contextualizing" heads consistently reside in the middle layers of the model.
  • Task Complexity: Complex tasks like multi-sentence summarization recruit significantly more high-RC attention heads (up to 9x more) than simpler tasks, suggesting RC can serve as a proxy for task difficulty.

Conclusion and Future Work

RCStat provides a principled, zero-shot upgrade for existing LLM deployments, requiring no retraining or fine-tuning. While it excels at chunk and sentence-level attribution, scaling to extremely fine-grained token-level attribution in overlapping regions remains a challenge. Future research will focus on deriving formal probabilistic guarantees and exploring the use of RC for real-time hallucination detection by measuring how well a model grounds its output in the provided context.

Summary updated

Abstract

Prior work on input-token importance in auto-regressive transformers has relied on Softmax-normalized attention weights, which obscure the richer structure of pre-Softmax query-key logits. We introduce RCStat, a statistical framework that harnesses raw attention logits via Relative Contextualization (RC), a random variable measuring contextual alignment between token segments, and derive an efficient upper bound for RC. We demonstrate two applications: (i) Key-Value compression, where RC-based thresholds drive adaptive key-value eviction for substantial cache reduction with minimal quality loss; and (ii) Attribution, where RC yields higher-fidelity token-, sentence-, and chunk-level explanations than post-Softmax methods. Across question answering, summarization, and attribution benchmarks, RCStat achieves significant empirical gains, delivering state-of-the-art compression and attribution performance without any model retraining.
BibTeX
@misc{mahapatra2025rcstat,
  author = {Debabrata Mahapatra and Shubham Agarwal and Apoorv Saxena and Subrata Mitra},
  title = {{RCStat: A Statistical Framework for using Relative Contextualization in Transformers}},
  year = {2025},
  eprint = {2506.19549},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2506.19549},
}
Figure