Abstract
Large Language Models (LLMs) rely on positional encodings like Rotary Positional Embedding (RoPE) to understand sequence order. However, RoPE exhibits an intrinsic distance bias that hinders long-context performance and requires manual tuning for extrapolation. We present Token-Aware Phase Attention (TAPA), which replaces fixed rotations with a learnable, token-dependent phase function. TAPA eliminates distance bias while maintaining high variance for distant token interactions, enabling stable extrapolation to unseen sequence lengths. Empirical results show TAPA significantly outperforms RoPE and its variants in long-context perplexity and robustness.
Introduction
In the landscape of Large Language Models (LLMs), the ability to process long contexts is a critical differentiator. The current industry standard for positional encoding, Rotary Positional Embedding (RoPE), has enabled significant progress. However, RoPE possesses a fundamental flaw: an intrinsic bias toward nearby tokens. This paper introduces Token-Aware Phase Attention (TAPA), a method developed at Meta Superintelligence Labs that mathematically addresses this bias and provides a scalable solution for long-context modeling.
The Problem: Intrinsic Distance Bias in RoPE
Most modern LLMs, including Llama 3 and Mistral, utilize RoPE to encode token positions. The authors demonstrate that RoPE attention is dominated by the distance between tokens rather than their semantic content. This "distance bias" forces the model to prioritize local information, making it difficult to retrieve relevant data buried deep within long sequences. Furthermore, RoPE struggles with extrapolation; models often fail when encountering sequences longer than those seen during training. Existing solutions like Position Interpolation (PI) or YaRN are often manual "hacks" that require extensive tuning and can degrade performance on shorter contexts.
Token-Aware Phase Attention (TAPA)
TAPA addresses these limitations by replacing fixed, position-only rotations with a learnable phase function, $\phi(q, k)$, that depends on the tokens themselves. The attention mechanism is modified as follows: $$Attn(q, k) \propto \cos(2\pi |m-n|^\alpha \phi(q, k))$$
Key Innovations
- Token-Awareness: The rotation angle (phase) is determined by the query and key vectors. This allows the model to dynamically decide which token interactions are preserved over long distances.
- Quadratic Phase: The researchers utilize a quadratic form for the phase function. This design creates a "stationary phase," reducing sensitivity to minor representation changes while maintaining high expressivity.
- Parameter Efficiency: By partitioning query and key vectors into amplitude and phase components, TAPA introduces no additional parameters or FLOPs compared to standard Transformers.
As shown in the ablation study above, the quadratic phase significantly outperforms the linear phase across all context window sizes, particularly as the sequence length increases.
Experimental Results
The researchers evaluated TAPA against RoPE, PI, and YaRN using a Llama-3 7B architecture. Models were pretrained on 420 billion tokens from The Pile with an 8k context window, then fine-tuned on the PG19 dataset at 32k lengths.
Superior Perplexity and Extrapolation
TAPA demonstrated remarkable stability and performance. When fine-tuned at 32k, TAPA achieved a perplexity of 11.74, outperforming RoPE/PI (~12.96) and YaRN (~12.16). Crucially, TAPA was the only method that remained stable when evaluated at 64k—double its fine-tuning length—maintaining a perplexity of 11.75 while competing methods saw their perplexity blow up into the thousands.
Zero-Shot Robustness
Beyond fine-tuned performance, TAPA exhibited strong zero-shot capabilities. Even without long-context fine-tuning, it performed significantly better on 32k sequences than its competitors, suggesting that the token-aware mechanism learns a more fundamental and robust representation of position.
Conclusion
TAPA represents a significant advancement in positional encoding for LLMs. By mathematically eliminating the distance bias inherent in RoPE and introducing token-aware rotations, TAPA enables models to handle long contexts more effectively and extrapolate to unseen lengths with unprecedented stability. Future work will focus on hardware-level optimizations, such as custom Triton kernels, and scaling the approach to models with 70B+ parameters.
Summary updated