ALiBi › Head-Specific Slopes
Language AI HandbookAll chapters ALiBi › Head-Specific SlopesOne section · Sections in this chapter5 / 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
A single slope would force all attention heads to have the same locality preference. That would be limiting: different aspects of language operate at different scales, and a model benefits from heads that specialize in different scopes. Some heads might focus on very local context (steep slope, harsh penalties for distance), while others maintain broader receptive fields (gentle slope, mild penalties).
Think of it like a camera system with multiple lenses. A telephoto lens captures fine detail up close but misses the broader scene. A wide-angle lens captures the full picture but loses local detail. You want both working together. ALiBi gives each attention head a different "focal length" by assigning it a different slope. The ensemble of heads covers a wide spectrum of temporal scales, from immediate local syntax to long-range topic coherence.
The slopes are not learned but fixed according to a geometric sequence. The decision to fix them (rather than learn them) is deliberate: it eliminates hyperparameters from the position encoding, makes the encoding fully deterministic and reproducible, and avoids the risk of the model learning pathological slope values during training. For a model with attention heads, the slope for head is:
where:
- : the slope parameter for attention head
- : the total number of attention heads in the model
- : the head index, ranging from 1 to
- : a scaling factor that ensures slopes span a consistent range regardless of how many heads the model has
- : the denominator that grows exponentially with head index, making later heads have gentler slopes
Why does this formula make sense? Notice that the exponent ranges from (head 1) to (head ). The denominator is therefore always through , independent of . This ensures that the range of slopes remains consistent across model architectures with different head counts. Whether you have 4 heads or 32 heads, the steepest head always has a slope near and the gentlest head always has a slope near .
The base of 8 in the exponent was chosen empirically by the ALiBi authors. With 8 heads, the exponent simplifies to just , giving slopes of , which equals . With other head counts, the formula interpolates appropriately.
def get_alibi_slopes(num_heads):
"""
Compute ALiBi slopes for each attention head.
The slopes follow a geometric sequence, with the first head
having the steepest slope (most local attention) and the last
head having the gentlest slope (broadest attention).
Args:
num_heads: Number of attention heads
Returns:
Array of slopes, one per head
"""
# Compute the ratio for the geometric sequence
ratio = 2 ** (8 / num_heads)
# Generate slopes: 1/ratio, 1/ratio^2, ..., 1/ratio^num_heads
slopes = 1.0 / (ratio ** np.arange(1, num_heads + 1))
return slopes4 heads: slopes = [0.25 0.0625 0.015625 0.003906] 8 heads: slopes = [0.5 0.25 0.125 0.0625 0.03125 0.015625 0.007812 0.003906] 16 heads: slopes = [0.707107 0.5 0.353553 0.25 0.176777 0.125 0.088388 0.0625 0.044194 0.03125 0.022097 0.015625 0.011049 0.007812 0.005524 0.003906]
The geometric progression ensures that slopes span several orders of magnitude. With 8 heads, the steepest slope (0.5) penalizes a distance of 10 by 5 logits, effectively eliminating distant tokens from consideration after softmax. The gentlest slope (roughly 0.004) penalizes the same distance by only 0.04 logits, allowing the head to attend broadly across hundreds of positions. This spread is what gives ALiBi its multi-scale character.
Notice that the formula produces a different set of slopes for 4, 8, and 16 heads, but in each case, the slopes span roughly the same overall range from steep to gentle. With 4 heads, you get fewer granular distinctions but the extremes remain similar. With 16 heads, you get finer granularity, more heads at intermediate slopes, but still roughly the same endpoints. This consistency is the purpose of the scaling factor.
