Bayesian Attention Mechanism: A Probabilistic Framework for Positional Encoding and Context Length Extrapolation
Arthur S. Bianchessi, Yasmin C. Aguirre, Rodrigo C. Barros, Lucas S. Kupssinskü
TL;DR
The paper tackles the lack of principled understanding for positional encoding in transformers and its impact on context length extrapolation. By recasting self-attention as a Bayesian mechanism, BAM treats PE as a prior over token positions, unifying NoPE, ALiBi, and related approaches under a single probabilistic framework. The authors introduce a Generalized Gaussian prior (GGD-BAM) that, with a small parameter count, substantially improves long-context retrieval and maintains competitive perplexity, validated on passkey retrieval and downstream benchmarks. The work provides both theoretical insights and practical methods for learning and visualizing attention priors, with scalable softmax compatibility, offering a path to more robust and interpretable long-context transformers.
Abstract
Transformer-based language models rely on positional encoding (PE) to handle token order and support context length extrapolation. However, existing PE methods lack theoretical clarity and rely on limited evaluation metrics to substantiate their extrapolation claims. We propose the Bayesian Attention Mechanism (BAM), a theoretical framework that formulates positional encoding as a prior within a probabilistic model. BAM unifies existing methods (e.g., NoPE and ALiBi) and motivates a new Generalized Gaussian positional prior that substantially improves long-context generalization. Empirically, BAM enables accurate information retrieval at $500\times$ the training context length, outperforming previous state-of-the-art context length generalization in long context retrieval accuracy while maintaining comparable perplexity and introducing minimal additional parameters.
