ALiBi › Why ALiBi Extrapolates

Michael BrenndoerferUpdated May 30, 20254 min read
Language AI HandbookAll chapters
Part 14 of 67Positional Encoding
Chapter 101ALiBi › Why ALiBi Extrapolates
Reading section 9 of 16 · Why ALiBi Extrapolates · 4 minSection 9 of 16 · 4 min

Book contents

Section 9 of 16 · 4 min read

ALiBi › Why ALiBi ExtrapolatesOne section · Sections in this chapter9 / 16 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The key to ALiBi's extrapolation ability lies in what the model learns during training. With other position encoding schemes, the model learns to interpret specific position representations. A sinusoidal encoding for position 512 produces a particular vector that the model has seen and learned to use. A learned position embedding for position 512 is a specific trained vector. When you encounter position 1025, this is a novel representation the model has not seen, and its layers have no well-defined response to it.

ALiBi sidesteps this problem entirely. The model never learns position representations because there are none to learn. Instead, it learns to work with relative attention patterns shaped by the linear bias. During training on sequences of length 1024, the model sees attention patterns where nearby tokens are favored and distant tokens are penalized. This is true at position 10, position 500, and position 1000. At every position in the training range, the model observes the same structural pattern: recent tokens have higher weight, distant tokens have lower weight.

When inference extends to position 2048, the same principle applies. The local neighborhood still receives favorable bias. Tokens 10 positions away still get penalized by the same amount. The absolute positions are larger, but the relative structure is completely unchanged. The model has learned to extract information from attention patterns that favor locality, and those patterns remain consistent regardless of sequence length.

Extrapolation Mechanism

ALiBi extrapolates because it encodes relative distance, not absolute position. The linear penalty for distance 10 is −10m-10m whether you are at position 50 or position 5000. The model learns to work with distance-biased attention patterns, which remain structurally consistent across all sequence lengths. This is the fundamental distinction from methods that encode absolute position as a vector.

There is a subtle but important point here. When a model processes a long sequence at inference time, tokens near the beginning of the sequence will be far from the current query position. ALiBi will penalize them heavily, potentially so heavily that they receive near-zero attention weight. This is not a failure: it is the expected behavior. The model is effectively operating with a soft window: it pays close attention to recent context and discounts the distant past. For sequences significantly longer than the training length, the very first tokens may be practically invisible to later queries. Whether this hurts performance depends on the task: for many language modeling and generation tasks, the most relevant context is recent, so this graceful distance falloff can improve performance.

Let's visualize the consistency property that makes extrapolation work:

Out[21]:
Visualization
Line plot showing ALiBi bias as a function of distance for three different query positions, all following the same linear penalty curve.
ALiBi bias as a function of distance for three query positions (20, 100, and 500). All three curves are identical because the bias depends only on distance, not on absolute position. This structural consistency is what allows models trained on short sequences to process longer ones without degradation.

The curves overlap perfectly because ALiBi's bias depends only on distance, never on absolute position. Whether the query is at token 20 or token 5000, the bias for a key 30 positions back is the same: −30m-30m. This is the mathematical basis for length extrapolation, and it is also the mathematical reason why it could not work for sinusoidal or learned position embeddings: those methods embed absolute position, so the representation at position 5000 is fundamentally different from (and trained less on than) the representation at position 20.