Quant Memo
Core

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 ii and key position jj directly to the raw attention score, before softmax:

score(i,j)=qikjdmij\text{score}(i,j) = \frac{q_i \cdot k_j}{\sqrt{d}} - m \cdot |i - j|

In words: take the ordinary scaled dot-product score exactly as before, then subtract a penalty equal to a fixed slope mm 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 mm 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 ij|i-j|, 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 2.02.0, and this head has slope m=0.25m = 0.25. Distance is 107=3|10-7|=3, penalty 0.25×3=0.750.25 \times 3 = 0.75, biased score 2.00.75=1.252.0 - 0.75 = 1.25. Compare key position 2: raw score also 2.02.0, distance 102=8|10-2|=8, penalty 0.25×8=2.00.25 \times 8 = 2.0, biased score 2.02.0=02.0 - 2.0 = 0. 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.

steep head (m=0.5) shallow head (m=0.03) distance →
A steep-slope head's penalty rises fast, forcing it toward local attention; a shallow-slope head's penalty barely grows, letting it stay competitive at long range.

Worked example 2: different heads, different effective ranges

Say one head has slope m=0.5m=0.5 (steep) and another m=0.03m=0.03 (shallow), both looking at tokens 20 positions apart with identical raw score 3.03.0. Steep head: penalty =0.5×20=10= 0.5 \times 20 = 10, biased score =3.010=7= 3.0 - 10 = -7 — after softmax against nearer competitors, this pair contributes almost nothing. Shallow head: penalty =0.03×20=0.6= 0.03 \times 20 = 0.6, biased score =2.4= 2.4 — 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.

Function explorer
-2222.0
x = 1.00f(x) = 2.000

Picture the penalty term mijm \cdot |i-j| 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

  1. Compute the ALiBi-biased score for raw score 1.51.5, distance 66, slope m=0.2m=0.2.
  2. Explain why a head with a very small slope mm behaves more like a "look anywhere" head than a "look nearby" head.
  3. 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)
ShareTwitterLinkedIn