Large language models process sequences by transforming discrete tokens into continuous vector representations. Standard dot-product self-attention is permutation-invariant: without explicit positional information, the attention operation treats a sequence as an unordered bag of tokens. Early transformer architectures addressed this limitation using Absolute Positional Embeddings (APE), either through fixed sinusoidal functions or learned lookup tables added directly to token embeddings.
While absolute embeddings allow models to distinguish token ordering within their training window, they fail when exposed to longer context lengths at inference. Models trained with sinusoidal or learned embeddings experience severe perplexity degradation when sequence length exceeds the pre-training context window .
Attention with Linear Biases (ALiBi), introduced by Ofir Press, Noah A. Smith, and Mike Lewis at ICLR 2022, resolved this limitation by removing positional embeddings from token representations entirely. Instead, ALiBi injects a static, non-learned linear penalty directly into the query-key attention score matrix, enabling zero-shot context length extrapolation.
The Mathematical Mechanics of ALiBi
In standard multi-head attention, the raw attention score between query token and key token is computed as:
ALiBi modifies this operation by subtracting a distance penalty scaled by a fixed, head-specific slope :
Where:
- represents the query position index.
- represents the key position index (with in causal auto-regressive decoding).
- is the non-negative token distance.
- is a static, head-specific scalar slope that does not update during backpropagation.
After applying the causal attention mask and softmax normalization, the resulting attention weights determine how much probability mass query allocates to key .
Query i Key j (Distance: i - j)
[0] [0] -> Penalty: 0 * m
[1] [0] -> Penalty: 1 * m
[2] [0] -> Penalty: 2 * m
[N] [0] -> Penalty: N * m (Monotonically decays attention)Because the penalty grows linearly with token distance, attention scores naturally decay for tokens situated further back in the sequence.

Geometric Slope Distribution Across Attention Heads
A single scalar slope cannot accommodate both short-range syntax tracking and long-range semantic retrieval. To provide multi-scale temporal resolution, ALiBi distributes slopes across the attention heads of each layer using a fixed geometric sequence.
For a transformer layer containing attention heads (where is a power of 2), the slope for head is defined as:
For an 8-head model (), the geometric ratio is . The resulting slopes evaluate to:
When the number of heads is not a power of 2, the algorithm determines the nearest power of 2 below , generates the base geometric sequence, and interpolates additional slopes from a higher-frequency sequence to maintain balance.
This mathematical configuration yields structural specialization across the model:
- Steep Slopes (e.g., ): Penalize distance heavily. The attention score drops by 0.5 logits per token separation. At a distance of 20 tokens, the logit penalty reaches , driving the post-softmax attention weight near zero. These heads function as localized syntactic collectors.
- Shallow Slopes (e.g., ): Impose minimal distance attenuation. At a distance of 1,000 tokens, the logit penalty is only , allowing content-driven semantic matching to dominate over token distance. These heads preserve long-range retrieval.
The Train-Short, Test-Long Property
Prior to ALiBi, extending context windows required training on full target sequence lengths. Training on long sequences incurs quadratic computational and memory costs: doubling sequence length quadruples attention computation and doubles KV cache footprint.
The original empirical evaluation in Press et al. (2021) demonstrated that models trained with ALiBi on sequence lengths of 1,024 tokens could extrapolate to 2,048 tokens zero-shot, matching the perplexity of baseline sinusoidal models trained natively on 2,048 tokens. This setup yielded an 11% reduction in training time and an 11% reduction in peak memory consumption while maintaining stable perplexity on validation sequences up to 16,384 tokens.
In contrast, models utilizing Sinusoidal Position Embeddings, Rotary Position Embeddings (RoPE), or learned absolute embeddings suffered catastrophic perplexity explosions when evaluated at sequence lengths beyond their training budget.
Architectural Comparison: ALiBi vs. RoPE and NoPE
Positional representations in modern autoregressive transformers divide into four distinct paradigms:
- Absolute Positional Embeddings (APE / Sinusoidal): Injected directly into token embeddings (). Employs global coordinate lookup tables or fixed sinusoidal trigonometric series. Fails zero-shot extrapolation beyond because query-key dot products drift into unseen coordinate regions.
- Rotary Position Embeddings (RoPE): Multiplied directly onto query and key projections (). Preserves relative distance via complex rotation in two-dimensional subspaces. Requires post-hoc frequency interpolation methods (such as YaRN or NTK-aware scaling) to handle extended context windows without phase drift.
- Attention with Linear Biases (ALiBi): Injected as an additive scalar bias directly into attention logits (). Requires no learnable parameters and provides native zero-shot length extrapolation because attention logits are strictly bounded by monotonic linear decay.
- No Position Embeddings (NoPE): Omits positional encoding entirely. Relies solely on causal masking asymmetry for ordering cues. While partially robust to sequence scaling, it degrades significantly on complex structural and permutation tasks.
While RoFormer's Rotary Position Embedding rotates the query and key vectors in two-dimensional slices according to position , it alters the geometric angles between representations. When evaluated at lengths , unseen rotation angles push the query-key inner products into uncalibrated regimes. RoPE therefore requires specialized context extension methods such as Position Interpolation or YaRN.
ALiBi avoids out-of-distribution logit values because adding negative distances simply pushes long-range attention weights toward zero, ensuring stable softmax output distributions.
Practical Limitations and Serving Trade-Offs
Despite its theoretical advantages for length extrapolation, ALiBi introduces specific architectural trade-offs:
1. The Fog-of-War Effect
Because the penalty is strictly monotonic, ALiBi assumes that older tokens are intrinsically less relevant than recent tokens. In tasks requiring precise needle-in-a-haystack retrieval from early context (such as referencing system instructions or initial variable declarations placed 32k tokens in the past), even the shallowest slopes introduce substantial negative biases (e.g., ). This logit suppression can effectively blind the attention mechanism to distant critical tokens.
2. Numerical Precision Collapses in Low-Precision Floats
In standard FP16 or BF16 representations, the dynamic range and mantissa resolution are constrained. As documented in SambaNova's positional encoding analysis, at context lengths exceeding 8,000 tokens, the linear subtraction can suffer from step discretization. Differences between consecutive positions and drop below machine epsilon for specific exponent ranges, causing multiple adjacent tokens to receive identical positional penalties.
3. Incompatibility with FlashAttention-1 Optimizations
Early fused attention implementations like FlashAttention-1 were designed around standard matrix multiplication kernels without arbitrary per-element additive bias tensors. Injecting dynamic bias matrices of shape created memory bandwidth overheads unless custom fused CUDA kernels (such as Triton or FlashAttention-2 with ALiBi support) were explicitly deployed.
Production Adoptions and Legacy
ALiBi has served as the positional foundation for several notable open-weight language models:
- BLOOM (176B): The BigScience consortium selected ALiBi for its flagship 176-billion-parameter multilingual model, as detailed in the BLOOM architectural technical report, prioritizing multi-scale stability across 46 natural languages and 13 programming languages.
- MPT-7B and MPT-30B: MosaicML utilized ALiBi in its MPT series, successfully extrapolating models trained on 2,048 tokens up to 65k and 84k context windows during inference without fine-tuning.
- Falcon and Baichuan: Several early multi-billion parameter foundation architectures deployed ALiBi or hybrid variations to facilitate inference scaling without retraining.
While contemporary frontier models (such as Llama 3, Mistral, and Qwen 2.5) have largely coalesced around high-base RoPE combined with YaRN-style temperature scaling due to superior associative recall across deep context, ALiBi established the empirical foundation for relative positional inductive biases in modern transformer engineering.
Sources
- Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation (arXiv:2108.12409)
- BLOOM: A 176B-Parameter Open-Access Multilingual Language Model (arXiv:2211.05100)
- RoFormer: Enhanced Transformer with Rotary Position Embedding (arXiv:2104.09864)
- Extending Context Window of Large Language Models via Positional Interpolation (arXiv:2306.15595)
- YaRN: Efficient Context Window Extension of Large Language Models (arXiv:2309.00071)
- ALiBi Deep Dive: Interpolation vs. Extrapolation (SambaNova)



