ALiBi › The Core Idea: Penalizing Distance

Michael BrenndoerferUpdated May 30, 20253 min read
Language AI HandbookAll chapters
Part 14 of 67Positional Encoding
Chapter 101ALiBi › The Core Idea: Penalizing Distance
Reading section 3 of 16 · The Core Idea: Penalizing Distance · 3 minSection 3 of 16 · 3 min

Book contents

Section 3 of 16 · 3 min read

ALiBi › The Core Idea: Penalizing DistanceOne section · Sections in this chapter3 / 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

ALiBi's insight is elegant in its simplicity: do not encode position in the embeddings at all. Instead, modify the attention mechanism itself to prefer nearby tokens over distant ones. Embeddings carry semantic content; let attention carry positional structure.

Think of it as a gravitational field. Every token exerts an attentional "pull" on every other token based on content compatibility. ALiBi adds a distance decay to this pull: the further two tokens are from each other, the weaker the pull, regardless of content. Nearby tokens always have a structural advantage. This is not unlike how we read: we naturally weight recent context more heavily than distant context when interpreting the meaning of a word.

Consider what attention scores represent. They measure compatibility between a query and a key. After softmax, these scores determine how much each position contributes to the output. ALiBi introduces a simple bias that subtracts from the score based on distance. To compute the modified attention score between position ii (as the query) and position jj (as the key), we start with the standard dot product and then impose the distance penalty:

scoreij=qi⋅kj−m⋅∣i−j∣\text{score}_{ij} = \mathbf{q}_i \cdot \mathbf{k}_j - m \cdot |i - j|

where:

  • scoreij\text{score}_{ij}: the modified attention score between positions ii and jj
  • qi⋅kj\mathbf{q}_i \cdot \mathbf{k}_j: the original dot product between query ii and key jj, capturing content-based compatibility
  • mm: a slope parameter that controls how aggressively to penalize distance (different for each attention head)
  • ∣i−j∣|i - j|: the absolute distance between the two positions in the sequence

The subtraction means distant tokens receive lower scores. A token 10 positions away gets penalized by 10m10m, while an adjacent token gets penalized by just mm. After softmax normalization, this translates to nearby tokens receiving higher attention weights. The key insight is that the penalty is linear in distance: every additional step away costs exactly mm more in score, regardless of whether you are stepping from position 1 to position 2 or from position 1000 to position 1001.

ALiBi Bias

ALiBi adds a linear bias to attention scores based on the distance between query and key positions. The bias is always non-positive, penalizing distant positions. The penalty grows linearly with distance, controlled by a slope parameter mm that is fixed (not learned) and specific to each attention head.

This is position encoding without position embeddings. The model learns nothing about absolute position during pretraining of its embeddings. Position information enters only through the attention bias, and only at the moment of computing attention scores. The embeddings remain clean semantic representations of token content, and the relative positional structure is layered on top at attention time.

Why does the penalty have to be linear? The authors experimented with other functional forms, including concave penalties that taper off quickly and convex penalties that grow steeply. Linear penalties produced the best combination of in-distribution performance and out-of-distribution (long-sequence) extrapolation. The linear form also has a pleasing mathematical property: it represents a constant cost per unit of distance, which creates a uniform "discount rate" on attention over distance. This discount rate is consistent regardless of absolute position, which is the property that makes extrapolation work.