ALiBi
ALiBi injects position into attention by adding a head-specific linear distance penalty to attention logits instead of adding positional embeddings to token representations.
Metadata
| Field | Value |
|---|---|
| Paper | Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation |
| Authors | Ofir Press, Noah A. Smith, Mike Lewis |
| Year | 2021 preprint; 2022 ICLR paper |
| Venue | ICLR 2022 |
| arXiv | 2108.12409 |
| Status | full note started |
One-Line Takeaway
ALiBi turns positional information into an additive bias on attention scores:
where is a fixed slope for head and is a distance-based bias. In causal language modeling, distant past tokens get a larger penalty.
Question
Plain self-attention has no order signal unless position is injected somewhere:
The usual Transformer route adds positional encodings to token embeddings:
That works, but long-context extrapolation raises a sharper question:
ALiBi asks whether position should enter the attention logit directly rather than the token embedding.
Main Claim
The paper’s architecture claim is:
Instead of learning or tabulating one position vector per index, ALiBi uses a fixed monotonic bias:
in the attention score before softmax.
For a causal query at position attending to a key at position , define distance:
Then a simplified ALiBi logit is:
The slope differs across heads, giving some heads a strong recency bias and others a weaker long-range bias.
Architecture Contract
| Item | Contract |
|---|---|
| Input | ordered token sequence |
| Target block | self-attention logits |
| Position operation | add fixed head-specific linear bias by relative distance |
| Token embedding | no added positional embedding required by ALiBi |
| Parameters | no learned position table |
| Main use | decoder-only language modeling and long-context extrapolation |
| Main claim | train on shorter sequences and test on longer sequences |
| Main risk | bias shape is fixed and may not fit every task or attention pattern |
ALiBi is not a new backbone. It is a positional mechanism for attention.
Attention Logit View
For a batch and one head, standard attention logits are:
With a causal mask :
ALiBi adds a relative-position term:
For causal attention:
Then:
The value mixing is unchanged:
ALiBi only changes the logit geometry before softmax.
Why Linear Bias Helps Extrapolation
Learned absolute embeddings usually define parameters only for positions seen during training:
Extrapolating to a longer sequence requires some strategy for:
ALiBi avoids this lookup-table issue. The distance penalty is defined for any distance:
So the same rule extends beyond the training length:
This does not guarantee perfect long-context behavior, but it removes one hard length boundary from the architecture.
Head Slopes
ALiBi uses different slopes for different heads:
The architecture intuition:
| Head Bias | Effect |
|---|---|
| large slope | strong preference for nearby tokens |
| small slope | weaker distance penalty and more long-range access |
This creates a spread of attention distance priors across heads without learning a position table.
Relation to Other Position Methods
| Method | Where Position Enters | Extrapolation Story |
|---|---|---|
| sinusoidal position | add fixed vector to token embedding | analytic but still mixed into token state |
| learned absolute position | add learned vector to token embedding | tied to trained position range |
| relative position bias | add learned or bucketed bias to logits | depends on buckets and maximum distance |
| RoPE | rotate query/key vectors | relative phase, later often scaled for longer context |
| ALiBi | add fixed linear distance bias to logits | distance rule naturally extends |
The key distinction:
Both are position mechanisms for Attention, but their implementation and extrapolation behavior differ.
Evidence to Read
| Claim | Evidence Type | What It Supports | Caveat |
|---|---|---|---|
| train-short test-long works | language modeling perplexity at longer test lengths | ALiBi can extrapolate beyond training context | result depends on task and model setup |
| memory and speed improve | comparison against training at longer context length | shorter training sequences can reduce training cost | not a free serving-time long-context method |
| recency bias helps | comparison with position methods on WikiText-103 | fixed distance prior is useful for language modeling | recency prior may not fit every modality |
| implementation is small | logit-bias addition | easy to add to existing attention code | kernel and mask code must handle bias correctly |
Implementation Reading
An implementation should make these decisions explicit:
- Is the model causal, bidirectional, or cross-attentional?
- Are distances , , or another relation?
- Are slopes fixed exactly as in the paper or changed?
- Is the ALiBi bias added before softmax and before masking is finalized?
- Does the attention kernel support additive bias without materializing a huge dense matrix?
- Are train and test context lengths reported separately?
For causal language models, the simplified logit update is:
Limitations
- ALiBi is a fixed inductive bias; it is not universally optimal for every sequence task.
- It helps remove positional-table length limits, but attention compute still scales with context length unless paired with efficient attention or sparsity.
- Extrapolation claims should report both training length and evaluation length.
- Some later long-context systems prefer RoPE variants or position interpolation/scaling, so ALiBi is one branch of the design space rather than the final answer.
Common Misreadings
| Misreading | Correction |
|---|---|
| ”ALiBi makes attention linear time.” | It changes position bias, not dense attention complexity. |
| ”ALiBi is a new Transformer architecture.” | It is a positional mechanism inside attention. |
| ”No positional embeddings means no position information.” | Position enters through the logit bias. |
| ”Length extrapolation is solved completely.” | The paper shows a useful route; evaluation still depends on task, data, model size, and context range. |
What to Remember
ALiBi is the cleanest example of position as an attention-logit prior:
For the architecture shelf, keep it near RoPE, Transformer-XL, Performer, and FlashAttention because it changes how Transformers handle sequence length without changing the whole backbone.