ALiBi
What Is ALiBi in Machine Learning?
ALiBi, short for Attention with Linear Biases, gives transformer models a sense of distance between tokens. It adds a distance-based penalty to attention scores, favoring nearby earlier information while still allowing distant context. It is unrelated to the legal term “alibi,” meaning being elsewhere when an event occurred.
Rather than adding a separate position value to every token, ALiBi changes the attention calculation itself. Language, code, and other sequences depend on order and distance. For example, “not” may affect a nearby phrase, while an earlier definition may matter later.
Why Transformer Models Need Position Information
Self-attention lets a transformer compare tokens and decide which information is useful. On its own, however, attention does not know whether tokens came before, after, or far from one another. Without position information, a shuffled sentence could appear too similar to the original.
Positional methods provide structure for word order, code dependencies, speech time steps, and biological sequences. In an autoregressive language model, which predicts the next token from earlier tokens, position helps distinguish recent instructions from older details.
How ALiBi Works
ALiBi adds its position signal inside each attention layer. In a causal language model, a token can attend only to earlier tokens.
- The model converts input into tokens, such as words, word pieces, punctuation, or code symbols.
- Each attention head calculates raw attention scores, or logits, between the current token and earlier tokens.
- The model measures each relative distance.
- It subtracts a linear distance penalty from every raw score, giving farther tokens a larger negative adjustment.
- Each head uses its own penalty slope, allowing different context preferences.
- Softmax converts adjusted scores into attention weights that add up to one.
- Those weights combine earlier information for the next-token prediction.
The causal mask remains essential. ALiBi does not reveal future text. It changes preferences only among tokens the model may already inspect.
Key Components of Attention with Linear Biases
Attention heads are parallel views of a sequence. One may focus on nearby grammar, while another tracks longer patterns. Attention logits are raw compatibility scores before normalization, and ALiBi adds its bias to logits rather than final weights.
Relative token distance is the gap between the current and earlier position. A linear slope turns that gap into a negative bias, with larger slopes penalizing distant positions more strongly. A causal mask blocks future positions, while head-specific slopes give heads different locality preferences. Unlike absolute positional embeddings, ALiBi does not add a learned position vector to every token embedding.
ALiBi vs. Other Positional Encoding Methods
Positional methods address the same problem differently. Results depend on model size, training data, context length, and task.
| Method | How position is represented | Long-context behavior | Main tradeoff |
|---|---|---|---|
| ALiBi | A linear distance bias added to attention logits | Can be evaluated beyond the training length, but accuracy must be tested | Simple and parameter-light, but does not remove attention cost |
| Learned absolute embeddings | A learned vector for each position | Usually tied to positions represented during training | Flexible during training, but fixed tables can limit extension |
| Sinusoidal embeddings | Fixed mathematical waves added to token embeddings | Can represent unseen positions | No learned position table, but may not fit every architecture equally well |
| Relative position embeddings | Learned or structured signals based on token-to-token distance | Often supports relative relationships effectively | Can add design and implementation complexity |
| Rotary positional embeddings | Position-dependent rotations applied to query and key vectors | Widely used for modern long-context models, often with extension techniques | Extension can require careful scaling and validation |
Where ALiBi Is Used
ALiBi is most associated with ordered-sequence transformers, especially causal, decoder-only language models. It is a design choice, not a universal upgrade.
- Decoder-only language models that generate one token at a time.
- Long-document summarization.
- Code generation and completion, where dependencies may span many lines or files.
- Chat systems with growing histories, subject to context and memory design.
- Retrieval-augmented workflows with long supplied documents.
- Other sequence tasks, including speech, time series, and scientific data, when relative distance signals help.
Teams should still assess prompts, retrieval quality, and latency across the full stack. This also applies when an AI app builder creates an interface around a model.
Benefits of ALiBi
ALiBi makes distance visible to attention compactly. Its benefits are architectural, not a guarantee of better results on every task.
- It works directly in attention scores without a separate positional embedding table.
- It adds little or no learned positional parameter overhead because slopes can be defined rather than trained.
- It favors nearby context while preserving access to distant context.
- Different heads can use complementary distance preferences.
- It can support longer-context experiments more naturally than fixed learned position tables.
- Parameter efficiency does not make long-context inference cheap. Standard attention still grows expensive as sequences grow.
Practical Limits and Challenges
ALiBi addresses only one part of the long-context problem. A model must still retrieve, interpret, and reason over distant information.
- Standard full attention grows quadratically in pairwise comparisons as context length increases.
- Accepting longer input does not guarantee accurate use of details near its beginning.
- Results depend on training length, data quality, architecture, optimization, and task.
- Fixed slopes make assumptions about how strongly heads favor local context.
- Adding ALiBi to a pretrained model using another position method can create compatibility and quality problems.
- Context-extension claims need controlled evaluation, not just proof that more tokens are accepted.
How to Evaluate an ALiBi-Based Model
Separate ordinary model quality from long-context reliability. Use workloads resembling the intended application, not one synthetic benchmark.
- Establish a baseline using the same architecture and training conditions without ALiBi, or with another positional method.
- Measure in-distribution performance at training context lengths.
- Test progressively longer inputs.
- Use retrieval and needle-in-a-haystack tests carefully, varying fact positions and wording.
- Measure accuracy, response quality, memory use, and latency together.
- Inspect missed evidence, wrong references, and overreliance on recent text.
- Validate on realistic documents, conversations, or codebases before making product claims.
For user-facing tools, broader app design and development also matters, including source display and ways users can correct errors.
Common Implementation Pitfalls
Small errors can make an ALiBi model behave differently from the intended design.
- Using distance in the wrong direction and rewarding faraway tokens.
- Adding bias after softmax instead of to logits before normalization.
- Mixing causal and bidirectional masking rules, allowing future-token access during training.
- Using different slope schedules in training and inference.
- Ignoring numerical precision issues when long distances create large negative values.
- Confusing maximum accepted context with the shorter length at which answers remain dependable.
A Simple Example of ALiBi Attention Bias
Imagine a model generating a final word and comparing a token three positions earlier with one 300 positions earlier. If their raw scores are similar, ALiBi subtracts a small penalty from the nearby token and a larger one from the distant token. The distant token can still win, but needs stronger evidence.
One head may use a steep slope for nearby grammar, while another uses a gentler slope for distant topics or instructions. This division of labor is why ALiBi uses head-specific biases instead of one shared penalty.
When ALiBi Is a Good Choice
ALiBi is a good choice when a model builder wants a simple, distance-aware method for causal attention and will test longer contexts carefully. It is worth comparing when fixed absolute position tables are restrictive or low positional parameter overhead is important. However, no positional method alone solves long-context reasoning, retrieval, or compute cost. Compare ALiBi with alternatives under matched training conditions, then choose the approach that performs reliably on the real task.
on Emergent today

