ALiBi and Relative Position Bias
Instead of encoding position into the embeddings at all, ALiBi penalizes attention scores directly by how far apart two tokens are, giving every model a built-in, tunable preference for nearby context that also generalizes cleanly to longer sequences.
Prerequisites: Learned Positional Embeddings, Scaled Dot-Product Attention and the √d Factor
Both sinusoidal and learned positional encoding share a weakness: they inject position information into the token embeddings at the very start, before any attention happens, and then hope the attention mechanism learns to make good use of that signal on its own. Learned embeddings additionally break down entirely past whatever maximum length they were trained on, since there's simply no table entry for a position beyond it. ALiBi takes a different approach entirely — don't touch the embeddings at all, and instead bias the attention scores themselves, directly and explicitly, by how far apart two tokens are.
The analogy: a conversation that naturally favors recent context
Think of a group discussion where, without anyone saying so explicitly, people's attention naturally leans toward whatever was said most recently — not because older comments are irrelevant, but because relevance decays gradually with how long ago something was said, and everyone applies roughly the same discount rate to older statements. ALiBi builds exactly this discount directly into attention: rather than hoping the model discovers "nearby things usually matter more" from a positional embedding pattern, it hard-wires a distance-based penalty straight into every attention score, so the preference for nearby context is guaranteed by construction, not something that has to be learned from scratch.
The mechanics: a linear penalty added straight to attention scores
ALiBi (Attention with Linear Biases) adds a penalty proportional to the distance between query position and key position directly to the raw attention score, before softmax:
In words: take the ordinary scaled dot-product score exactly as before, then subtract a penalty equal to a fixed slope times the distance between the two positions — the further apart two tokens are, the larger the subtraction, so distant tokens are steered toward lower attention weight after softmax, with no positional information added to the embeddings at all. The slope is set differently per attention head (following a fixed geometric schedule decided in advance, not learned), so different heads end up favoring different effective ranges: some heads with a steep slope focus almost entirely on nearby tokens, others with a shallow slope stay more evenly spread across long distances.
Because this penalty is defined for any distance , including distances far larger than anything seen during training, ALiBi extrapolates to much longer sequences at inference time than it was trained on — a model trained on 1,024-token sequences can be run on 4,096-token sequences with only a modest, predictable quality change, something neither sinusoidal nor learned embeddings reliably achieve.
Worked example 1: computing a penalized score by hand
Suppose the raw scaled score between query position 10 and key position 7 is , and this head has slope . Distance is , penalty , biased score . Compare key position 2: raw score also , distance , penalty , biased score . Even though both had identical raw relevance, the more distant position ends up with a lower biased score purely from being further away — the "recency discount," applied uniformly and predictably.
Worked example 2: different heads, different effective ranges
Say one head has slope (steep) and another (shallow), both looking at tokens 20 positions apart with identical raw score . Steep head: penalty , biased score — after softmax against nearer competitors, this pair contributes almost nothing. Shallow head: penalty , biased score — still highly competitive. The steep head behaves like a short-range local-attention head, the shallow head like a long-range one, purely from the fixed per-head slope, with no extra learned parameters needed.
Picture the penalty term as a straight line rising with distance (a simple linear, not curved, function) — it's subtracted directly from the score, so bias grows smoothly and predictably the further apart two positions are, with no discontinuity at any particular length.
What this means in practice
ALiBi is used specifically where a model needs to reliably handle sequences longer at inference than anything seen during training — long-document processing, extended chat histories, or any deployment where input length can't be tightly bounded in advance. It trades away the fully-learned flexibility of learned embeddings for a built-in, distance-based inductive bias that turns out to generalize far better, and it adds essentially no extra parameters or compute relative to ordinary attention, since it's just one subtraction per score.
ALiBi skips positional embeddings entirely and instead subtracts a distance-proportional penalty directly from raw attention scores, with a different fixed slope per head — giving nearby tokens a built-in advantage and letting the model generalize to sequence lengths well beyond what it was trained on, something table-based positional schemes cannot reliably do.
Practice
- Compute the ALiBi-biased score for raw score , distance , slope .
- Explain why a head with a very small slope behaves more like a "look anywhere" head than a "look nearby" head.
- Why can ALiBi be applied to sequence lengths never seen during training, unlike a learned positional embedding table?
The common confusion is thinking ALiBi's distance penalty makes the model unable to attend far away. It only biases scores, it doesn't zero anything out — a sufficiently strong raw dot-product relevance can still overcome the penalty and win high attention weight, especially in shallow-slope heads. ALiBi is a soft inductive bias toward locality, not a hard cutoff.
Related concepts
Practice in interviews
Further reading
- Press, Smith & Lewis, Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation (2022)