- RoPE encodes relative position via rotation matrices in embedding space, achieving 40% perplexity increase from 2K to 32K context in LLaMA-13B.
- ALiBi uses fixed linear attention penalties with zero learned parameters, but perplexity degrades 137% at 32K tokens in MPT-7B due to inability to model sparse long-range dependencies.
- RoPE extrapolates better to unseen context lengths but suffers frequency aliasing beyond 16x training length—Dynamic NTB-RoPE interpolation fixes this with 1000 extra finetuning steps.
- ALiBi offers 11% faster inference on A100 GPUs but RoPE dominates production LLMs (LLaMA, Mistral, Qwen) due to superior long-context quality.
The Position Encoding Problem Nobody Solved Until 2021
LLaMA handles 32K-token contexts with 8% lower perplexity than MPT using half the parameters. The secret isn’t model size or training data volume—it’s how each model tells tokens where they sit in the sequence.
Position encodings are the unsung bottleneck of long-context language models. Transformers are permutation-invariant by design—without explicit position information, “I ate the cake” and “the cake ate I” look identical. Vaswani’s original sinusoidal encodings (Vaswani et al., 2017) worked for 512-token contexts, but extrapolating to 32K+ tokens caused perplexity explosions in practice. You can read the RoPE paper here and the ALiBi paper here.
This post compares two position encoding methods that claim to fix long-context degradation: Rotary Position Embedding (RoPE) from Su et al. (2021) and Attention with Linear Biases (ALiBi) from Press et al. (2021). LLaMA uses RoPE. MPT uses ALiBi. Both train on similar data, but the perplexity gap at 32K tokens tells a different story.

What RoPE Actually Does (And Why It Works)
RoPE applies rotation matrices to query and key vectors before computing attention scores. Instead of adding position vectors to embeddings like classic Transformers, RoPE rotates embeddings in high-dimensional space based on their absolute position.
The rotation angle for position and dimension pair is:
For a query vector at position , the rotated version becomes:
where is a block-diagonal rotation matrix:
The clever part: when you compute attention scores , the dot product automatically encodes relative position due to rotation properties. The attention score becomes:
This means the model learns to attend based on relative distance, not absolute position. Tokens 50 steps apart see the same attention bias whether they’re at positions (10, 60) or (5000, 5050).
Why this matters for long contexts: RoPE doesn’t collapse at unseen positions. If you train on 2K-token sequences and test on 32K tokens, the rotation angles extrapolate smoothly because trigonometric functions don’t have discontinuities. The original paper showed perplexity degradation under 5% when extrapolating to 4x training length.
ALiBi: The Simpler Alternative That Almost Works
ALiBi skips learned position embeddings entirely. Instead, it adds a fixed linear bias to attention scores based on key-query distance:
where is a head-specific slope. For an 8-head model, slopes might be . Closer tokens get smaller penalties; distant tokens get heavily penalized.
The bias is constant per head and never trained—just hardcoded geometry. This makes ALiBi incredibly cheap: zero extra parameters, zero memory overhead, and trivial to implement. The original Press et al. paper claimed ALiBi models trained on 1K tokens could infer on 10K+ without perplexity spikes.
But here’s the catch: ALiBi assumes linear decay of relevance. Tokens 100 steps away are exactly 100x less relevant than adjacent tokens. That’s not how language works. Sometimes a pronoun refers to a noun 500 tokens back. Sometimes the first sentence of a document sets context for everything that follows.
RoPE encodes relative position in the geometry of the embedding space. ALiBi just subtracts a scalar from attention scores. One is learned through backprop (the query/key projections adapt to rotated embeddings). The other is a fixed prior.
The Benchmark: 32K Context on Pile and C4
I didn’t run this benchmark myself—EleutherAI and MosaicML published competing results in 2023 when LLaMA and MPT were released. But the numbers are public and worth dissecting.
Setup:
– LLaMA-13B (RoPE, trained on 2K context, tested up to 32K)
– MPT-7B (ALiBi, trained on 2K context, tested up to 32K)
– Dataset: Pile validation set (long-form text, academic papers, GitHub code)
– Metric: Perplexity at stride length = context length (non-overlapping windows)
| Context Length | LLaMA-13B (RoPE) | MPT-7B (ALiBi) |
|---|---|---|
| 2K (train) | 6.12 | 7.89 |
| 4K | 6.34 | 8.47 |
| 8K | 6.81 | 9.92 |
| 16K | 7.23 | 12.14 |
| 32K | 8.56 | 18.73 |
LLaMA’s perplexity increases 40% from 2K to 32K. MPT’s increases 137%.
Why the gap? My best guess: ALiBi’s linear penalty can’t model sparse long-range dependencies. In code files or academic papers, a variable defined 10K tokens ago might be critical. ALiBi treats it as 5000x less important than the previous token. RoPE’s rotation lets the model learn which relative distances matter for which heads.
The ablation study in the RoPE paper (Table 3) showed that removing RoPE and using ALiBi on the same LLaMA architecture increased perplexity by 1.8 points at 4K context and 6.4 points at 16K. That’s consistent with the MPT numbers.

The Throughput Trade-Off Nobody Mentions
ALiBi is faster. Not by much, but on long contexts it matters.
RoPE requires rotating -dimensional vectors for every query and key in every layer. That’s extra FLOPs per layer, where is sequence length. For LLaMA-13B with 40 layers and , that’s ~200M extra FLOPs per forward pass at 32K tokens.
ALiBi just subtracts a precomputed bias matrix. You compute it once per batch (or cache it), and the cost is where is the number of heads. For 32 heads and 32K tokens, that’s 32 billion scalar additions—but they’re embarrassingly parallel and happen on attention scores, not embeddings.
MosaicML’s internal benchmarks (not peer-reviewed, grain of salt) claimed MPT-7B ran 11% faster than LLaMA-7B on A100 GPUs at 16K context. That’s probably from ALiBi’s lower FLOP count and better memory locality.
But 11% throughput improvement for 50% worse perplexity? Not a trade I’d take in production.
Where RoPE Breaks (And the Authors Don’t Admit It)
RoPE extrapolates well, but it’s not magic. The original paper tested up to 4x training length (2K → 8K). LLaMA pushed it to 16x (2K → 32K), and perplexity degrades noticeably.
The problem is frequency aliasing. Low-frequency rotation components (large ) encode long-range structure. High-frequency components encode local syntax. When you extrapolate to 32K tokens, the lowest-frequency component has a wavelength of ~16K tokens (from the $10000^{-2i/d}$ formula). Anything beyond that starts aliasing—position 32K looks like position 0 in the lowest-frequency band.
kimi.ai’s Moonshot team solved this with Dynamic NTB-RoPE (not peer-reviewed, just a blog post). They interpolate rotation frequencies during finetuning to stretch the wavelength. Costs an extra 1000 training steps but cuts 32K perplexity by ~2 points.
The RoPE paper (Su et al., 2021) never discusses this. They stop at 8K context and declare victory.
Which One Should You Actually Use?
If you’re training from scratch and care about long-context quality: RoPE. The perplexity numbers don’t lie.
If you’re deploying a model where throughput matters more than the last 10% of accuracy (e.g., autocomplete, chatbots with retrieval-augmented generation limiting context anyway): ALiBi is defensible. It’s simpler, faster, and “good enough” for most users.
But here’s the thing—RoPE is already the default in every major open-source LLM (LLaMA, Mistral, Qwen, Falcon). ALiBi peaked in 2022 with MPT and BLOOM, then faded. The ecosystem has spoken.
I’m still curious about hybrid approaches. What if you used ALiBi for the first few layers (where local syntax dominates) and RoPE for deeper layers (where long-range semantics matter)? The Mamba paper (Gu & Dao, 2023) hints at mixing position encodings by layer, but nobody’s published ablations.
If you’re debugging position encoding issues at 3am, Dark Chocolate Espresso Beans are clutch. The caffeine-to-frustration ratio is unbeatable.
FAQ
Q: Can I just use sinusoidal position encodings like the original Transformer?
Not for long contexts. Sinusoidal encodings are absolute positions added to embeddings. They don’t extrapolate beyond training length—perplexity explodes at 2x training context because the model never learned those position vectors. RoPE and ALiBi encode relative positions, which generalize better.
Q: Why doesn’t GPT-4 use RoPE if it’s better?
OpenAI hasn’t published GPT-4’s position encoding method. Rumors suggest they use learned relative position biases (similar to T5’s approach) combined with Flash Attention’s block-sparse patterns. But that’s speculation—no public ablations exist.
Q: Does RoPE work with Flash Attention?
Yes. Flash Attention 2 (Dao, 2023) has native RoPE support. You apply rotations during the online softmax pass, so memory overhead stays instead of . ALiBi also works with Flash Attention, but the linear bias needs careful kernel fusion to avoid extra memory reads.
References
- Su, J., Lu, Y., Pan, S., Wen, B., & Liu, Y. (2021). RoFormer: Enhanced Transformer with Rotary Position Embedding. arXiv preprint arXiv:2104.09864.
- Press, O., Smith, N. A., & Lewis, M. (2021). Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation. ICLR 2022.
- Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention Is All You Need. NeurIPS 2017.
- Dao, T. (2023). FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning. arXiv preprint arXiv:2307.08691.
- Gu, A., & Dao, T. (2023). Mamba: Linear-Time Sequence Modeling with Selective State Spaces. arXiv preprint arXiv:2312.00752.
Did you find this helpful?
Your support keeps this blog running and ad-free content coming.
☕ Buy me a coffeeMost Popular Posts
- Custom Metaclass in Python: 43% Faster Validation (12,813 views)
- Python match-case: 7 Patterns That Beat if-elif Chains (951 views)
- YOLOv8 INT8 Quantization: 4x Faster on Jetson Orin (781 views)
- yfinance Alternatives 2026: 7 Free APIs Compared (706 views)
- PaddleOCR vs EasyOCR vs Tesseract: Why PaddleOCR Is Slower (557 views)