ALiBi, short for attention with linear biases, is a method for giving a transformer a sense of token order by adding a fixed penalty to each attention score that grows linearly with the distance between the query and the key. In neural networks for language modeling, it replaces learned or sinusoidal positional embeddings entirely, so no position information is ever added to the token representations themselves.
The method was introduced in 2021 with a specific promise: train on short sequences, test on longer ones. Because the bias depends only on relative distance and requires no learned parameters, a model trained with ALiBi degrades far more gracefully than embedding-based models when it reads inputs longer than those seen during training, a property known as length extrapolation.
How ALiBi biases the attention scores
ALiBi works by subtracting a distance-proportional term from every query-key dot product before the softmax. For a query at position i and a key at position j, the score becomes the usual scaled dot product plus m multiplied by (j minus i), where m is a fixed slope assigned to each attention head. In a causal decoder, j is never greater than i, so the added term is zero or negative and grows more negative the further back the key lies.
The result is a built-in recency preference. Nearby tokens keep almost their full score, while distant tokens are pushed down unless their content match is strong enough to overcome the penalty. The bias is implemented as a static matrix added to the attention logits, which fits naturally alongside the causal mask and costs almost nothing to compute.
The slopes form a geometric sequence across heads. For a model with 8 heads, the slopes are 1/2, 1/4, 1/8 and so on down to 1/256. The following table shows how the penalty for a single head changes with distance.
| Distance in tokens | Bias added to the score | Practical effect |
|---|---|---|
| 0 | 0 | Full attention weight available |
| 4 | -1 | Mild preference for closer tokens |
| 16 | -4 | Noticeably reduced weight |
| 64 | -16 | Nearly ignored unless the match is strong |
Head slopes and their role
Each head receives a different slope, which gives the model a spectrum of effective context lengths. Heads with steep slopes behave like local attention and focus on the last few tokens, while heads with shallow slopes can still reach hundreds or thousands of tokens back. This mixture lets the network combine short-range syntax with longer-range reference without any learned position parameters.
The original recipe sets the slopes as powers of 2 starting at 2 to the power of minus 8 divided by the number of heads. When the head count is not a power of two, implementations interleave slopes from the two nearest powers of two. The table below summarizes the parameters that define ALiBi.
| Parameter | Typical value | What it controls |
|---|---|---|
| Slope per head | 1/2 to 1/256 for 8 heads | How fast the penalty grows |
| Number of heads | 8 to 128 | Granularity of the slope spectrum |
| Learned parameters | None | No extra memory or training |
| Training length | Often 512 to 2,048 tokens | Baseline for extrapolation |
| Bias sign | Zero or negative | Penalizes distant keys only |
Some later work made the slopes trainable, but the fixed schedule remains the default because it is robust and reproducible across model sizes.
ALiBi compared with other position methods
ALiBi is one of several ways to inject order into self-attention, which is otherwise permutation invariant. Its closest neighbors are sinusoidal positional encoding, learned absolute embeddings and rotary position embedding (RoPE). They differ in where position enters the computation and in how they behave beyond the training length.
| Method | Where position enters | Extra parameters | Behavior beyond training length |
|---|---|---|---|
| ALiBi | Bias on attention logits | None | Degrades gracefully |
| Sinusoidal encoding | Added to input embeddings | None | Degrades quickly |
| Learned absolute embedding | Added to input embeddings | One vector per position | Undefined past the maximum |
| Rotary position embedding | Rotation of queries and keys | None | Needs scaling methods to extend |
| T5 relative bias | Bucketed bias on logits | Small learned table | Moderate, with bucket limits |
In practice, RoPE became the dominant choice in large open models after 2023, partly because context-extension techniques such as position interpolation work well with it. ALiBi remains attractive where simplicity and zero-shot extrapolation matter more than peak long-context retrieval quality, and it was used in several well-known open models, including BLOOM and MPT.
Why ALiBi extrapolates to longer inputs
Extrapolation works because ALiBi never presents an unseen position to the network. Every distance the model meets at inference time produces a bias computed by the same linear rule it saw during training, so there is no unfamiliar embedding vector to confuse later layers.
There is an important nuance. Much of the gain comes from the recency bias itself: distant tokens are heavily down-weighted, so the model effectively attends within a sliding window and ignores most of the extra context. Perplexity stays stable on longer inputs, but this does not mean the model uses the full long context effectively. Tasks that require retrieving a fact thousands of tokens back can still fail.
Implementation choices and practical use
ALiBi is implemented by building a bias tensor of shape heads by query length by key length and adding it to the logits before the softmax. Optimized kernels such as FlashAttention support ALiBi slopes directly, which avoids materializing the full matrix and keeps memory use linear in sequence length. The decision table below shows when ALiBi is a sensible choice.
| Situation | Recommended choice | Reason |
|---|---|---|
| Training a compact model on short sequences | ALiBi | Cheap training, usable longer inputs |
| Long-document retrieval is critical | RoPE with context extension | Better access to distant tokens |
| Encoder for classification | Learned or relative embeddings | Bidirectional context, fixed length |
| Strict memory budget at inference | ALiBi with a fused kernel | No parameters, no extra tensors |
In encoder models with bidirectional attention, the bias is usually applied symmetrically using the absolute distance, although this variant was less studied than the causal one.
Limitations and common misconceptions
ALiBi is not a general solution to long context. Its main limitations and misunderstandings are the following:
- Stable perplexity is not long-range reasoning: the model may simply ignore far tokens.
- Fixed slopes limit flexibility: the recency profile cannot adapt to tasks that need uniform attention.
- Extrapolation has a ceiling: quality eventually declines when inputs are many times longer than training sequences.
- Not compatible with RoPE scaling tricks: methods designed for rotary embeddings do not transfer directly.
- Kernel support matters: a naive implementation that builds the full bias matrix wastes memory on long inputs.
A non-technical view
A language model has to know which words came first. ALiBi gives it that sense in a simple way. Words that are far away count a little less. The further back a word is, the smaller its voice in the decision.
Think of listening in a crowded room. You hear the person next to you clearly. Voices across the room are fainter, but a loud and relevant one still gets through. ALiBi turns distance into quietness. Because the rule is the same at any distance, the model can handle longer texts than it practiced on without getting lost.
A concrete example
A research team trains a 1.3-billion-parameter decoder for summarizing internal technical reports. Their compute budget allows training only on sequences of 1,024 tokens, yet many reports run to about 3,000 tokens. With learned positional embeddings, the model would have no representation for positions beyond 1,024.
The team uses ALiBi with 16 heads and slopes from 1/2 down to 1/256. At inference, they feed full 3,000-token reports. Perplexity rises only slightly compared with the training length, and summaries stay fluent. However, an evaluation shows that details from the first pages are sometimes missed, which matches the recency bias of the steep heads. The team therefore adds a chunking step for the longest reports, keeping the cheaper training run while protecting summary completeness.