ALiBi › The Mathematical Formulation
Language AI HandbookAll chapters ALiBi › The Mathematical FormulationOne section · Sections in this chapter4 / 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
Now that we understand ALiBi's core insight, let's develop the complete mathematical framework. We will build from standard attention to ALiBi-augmented attention, showing exactly where and how the position bias enters the computation.
Standard attention and ALiBi attention are nearly identical. The only difference is one additional term added to the score matrix before the softmax. This surgical simplicity is part of what makes ALiBi attractive: it requires minimal changes to an existing attention implementation and adds almost no computational overhead.
Starting Point: Standard Scaled Dot-Product Attention
Recall that standard self-attention operates on three matrices derived from the input sequence. For a sequence of tokens, the attention mechanism computes:
where:
- : the query matrix containing query vectors for all positions
- : the key matrix containing key vectors for all positions
- : the value matrix containing value vectors for all positions
- : the dimension of queries and keys
- : the raw attention score matrix where entry is the dot product between query and key
- : scaling factor that prevents dot products from growing too large in high dimensions, which would push the softmax into regions of very small gradients
The matrix captures content-based similarity: how much each query "wants" to attend to each key based purely on their learned representations. This matrix is completely blind to position. Token 1 attending to token 2 produces the same content-based score whether they appear next to each other or 500 positions apart. This is by design for the core attention mechanism, which wants to capture global relationships. But it means we need another mechanism to introduce positional awareness. That mechanism is the ALiBi bias.
Why does ALiBi add bias at this stage rather than modifying the queries and keys? Adding to the pre-softmax scores is the most direct possible intervention. The scores are the raw votes about which tokens matter. By adjusting the scores before normalization, we influence the attention distribution in a clear and simple way. Modifying queries and keys (as RoPE does) achieves a similar effect but through a more complex indirect path involving embedding geometry.
Injecting Position: The Bias Matrix
ALiBi's modification is surgical. Rather than changing how queries, keys, or values are computed, it adds a single term to the attention scores. The complete ALiBi-augmented attention reads:
The bias matrix is added directly to the scaled scores before softmax. This is the only change to standard attention. The matrix encodes position information through a remarkably simple rule: each entry is determined entirely by the distance between the corresponding positions. Specifically,
where:
- : the bias added to the attention score between query position and key position
- : the slope parameter for this attention head, controlling the strength of the distance penalty
- : the absolute distance between positions and in the token sequence
Let's unpack why this formula works. The absolute distance measures how far apart two positions are in the sequence. Adjacent tokens have distance 1, tokens separated by 10 positions have distance 10. Multiplying by a positive slope and negating creates a penalty that grows linearly with distance. A token that is close gets a small penalty; a token that is far gets a large penalty.
Consider what happens for a query at position 5 attending to various keys:
- Attending to position 5 (itself): (no penalty, full score preserved)
- Attending to position 4 (adjacent): (small penalty)
- Attending to position 3 (two steps away): (moderate penalty)
- Attending to position 0 (distant): (large penalty)
The negative bias reduces the attention score, and larger distances produce more negative biases. After softmax normalization, this translates to lower attention weights for distant tokens. The content-based similarity in still matters completely: a very semantically relevant distant token can overcome its distance penalty if its dot product score is large enough. But now content has to compete against a structural penalty that favors proximity.
Why does this formula make sense? Notice that the formula simply implements a universal assumption that language models hold implicitly: nearby context tends to be more relevant than distant context. Rather than forcing the model to learn this bias from data (which is expensive and may lead to overfitting on long-range spurious correlations), ALiBi builds it in as an inductive bias. The model is free to override it with strong content signals, but the default behavior is local attention. This is similar to how convolutional neural networks build in a translation-invariance inductive bias, rather than forcing the network to learn that from scratch.
The Causal Case: Masking the Future
For decoder-style models that generate text left-to-right, we need causal masking: position can only attend to positions . The combined bias matrix integrates both the ALiBi distance penalties and the causal mask:
The structure reveals two distinct components working together. The lower triangle, including the diagonal, contains the ALiBi distance penalties. Entry with holds the value , which is zero on the diagonal and grows more negative as you move left along any row. The upper triangle holds values from the causal mask.
The entries ensure that after applying inside the softmax, future positions contribute nothing to the weighted sum. The linear penalties in the lower triangle then shape how attention flows among the allowed past positions. These two components operate in entirely different regimes: the causal mask is a hard prohibition (zero weight, always), while the ALiBi penalty is a soft preference (lower weight, but nonzero).
Notice that the causal mask interacts naturally with ALiBi. For a model processing position , the only tokens it can attend to are positions 0 through . These are exactly the tokens for which , so the distance is always , always positive, and always measured in the backward direction. The ALiBi penalty is therefore always a backward-looking decay, consistently favoring the most recent tokens in the context.
Building the Bias Matrix: Step-by-Step Implementation
Let's translate this mathematics into code, building up the bias matrix piece by piece. The core insight behind the implementation is that we can compute all pairwise distances simultaneously using broadcasting, rather than iterating over all pairs with nested loops.
import numpy as np
def create_alibi_bias(seq_len, slope):
"""
Create the ALiBi bias matrix for a single attention head.
Args:
seq_len: Length of the sequence
slope: The slope parameter m for this head
Returns:
Bias matrix of shape (seq_len, seq_len)
"""
# Create position indices
positions = np.arange(seq_len)
# Compute pairwise distances: |i - j|
# positions[:, None] is (seq_len, 1), positions[None, :] is (1, seq_len)
# Broadcasting gives (seq_len, seq_len)
distances = np.abs(positions[:, None] - positions[None, :])
# Apply linear penalty
bias = -slope * distances
return biasThe key implementation insight is using NumPy broadcasting to compute all pairwise distances at once. By reshaping the position array into a column vector positions[:, None] and a row vector positions[None, :], subtraction produces an matrix where entry is . Taking the absolute value gives us the distance matrix. Multiplying by the slope and negating gives the penalty matrix. This vectorized approach is both faster and more readable than explicit loops.
Let's see what this produces for a small sequence:
ALiBi bias matrix for sequence length 6, slope 0.5: [[-0. -0.5 -1. -1.5 -2. -2.5] [-0.5 -0. -0.5 -1. -1.5 -2. ] [-1. -0.5 -0. -0.5 -1. -1.5] [-1.5 -1. -0.5 -0. -0.5 -1. ] [-2. -1.5 -1. -0.5 -0. -0.5] [-2.5 -2. -1.5 -1. -0.5 -0. ]]
Reading this matrix: the diagonal is zero because a token attending to itself has distance zero and receives no penalty. Moving away from the diagonal in either direction, penalties grow linearly. Position 5 (row 5) attending to position 0 (column 0) shows , which is exactly as expected. The matrix is symmetric because distance is symmetric: . In a causal model we will only use the lower triangle, but the full symmetric matrix is the natural output of the distance computation.
For decoder models, we overlay the causal mask on top of the distance penalties:
def create_causal_alibi_bias(seq_len, slope):
"""
Create ALiBi bias with causal masking.
Args:
seq_len: Length of the sequence
slope: The slope parameter m
Returns:
Bias matrix with -inf for future positions
"""
# Start with the distance-based bias
bias = create_alibi_bias(seq_len, slope)
# Create causal mask (upper triangle should be -inf)
causal_mask = np.triu(np.ones((seq_len, seq_len)), k=1)
# Apply causal mask: future positions get -inf
bias = np.where(causal_mask == 1, -np.inf, bias)
return biasThe np.triu function creates an upper triangular matrix of ones, which we use to identify positions that should be masked. The k=1 argument excludes the diagonal, since a token should be able to attend to itself. The np.where call then replaces those positions with negative infinity, which becomes effectively zero after the exponential in softmax.
Causal ALiBi bias matrix: [[-0. -inf -inf -inf -inf -inf] [-0.5 -0. -inf -inf -inf -inf] [-1. -0.5 -0. -inf -inf -inf] [-1.5 -1. -0.5 -0. -inf -inf] [-2. -1.5 -1. -0.5 -0. -inf] [-2.5 -2. -1.5 -1. -0.5 -0. ]]
Now the upper triangle shows inf (NumPy's display for ), while the lower triangle retains the distance penalties. Row 5 can attend to all previous positions with penalties for positions 0 through 5 respectively. Row 0 can only attend to itself with penalty 0, since there are no previous positions available.
Let's visualize both the distance matrix and the resulting causal ALiBi bias matrix side by side to make the structure visually clear:


The left matrix shows raw distances: the diagonal is 0 (same position), adjacent cells are 1, and values grow as positions diverge. The right matrix shows what happens after applying ALiBi: distances become negative penalties (scaled by the slope), and the upper triangle is masked to enforce causality. This visual makes clear that the bias matrix is nothing more than a scaled, negated distance matrix with causal masking applied on top.