Learnixo
Back to blog
AI Systemsintermediate

ALiBi: Attention with Linear Biases

How ALiBi adds a static linear penalty to attention scores based on distance, why it extrapolates to longer sequences at inference, and how it compares to RoPE.

Asma Hafeez KhanMay 16, 20264 min read
TransformersALiBiPositional EncodingLong ContextInterview
Share:๐•

Core Idea

ALiBi (Press et al., 2021) takes a strikingly simple approach: add a static linear penalty to attention scores based on the distance between the query and key positions. No positional embeddings added to token inputs at all.

Standard attention score:
  score(i, j) = q_i ยท k_j / โˆšdโ‚–

ALiBi attention score:
  score(i, j) = q_i ยท k_j / โˆšdโ‚–  -  m ยท (i - j)

where:
  (i - j) = distance from key position j to query position i (always โ‰ฅ 0)
  m       = head-specific slope (fixed, not learned)

Closer tokens get a smaller penalty; distant tokens get a larger penalty. The model is biased toward attending to nearby context.


The Slopes

Each attention head uses a different slope m, creating a geometric sequence:

For h heads, slopes mโ‚, ..., mโ‚•:

m_k = 2^(-8k/h)  for k = 1, 2, ..., h

Example with 8 heads:
  mโ‚ = 2^(-1) = 0.5
  mโ‚‚ = 2^(-2) = 0.25
  ...
  mโ‚ˆ = 2^(-8) = 0.004

Head 1 (steepest slope): strongly penalises distance โ€” attends locally
Head 8 (flattest slope):  weakly penalises distance โ€” can attend far

Different heads specialise in different distance ranges automatically.


Code

Python
import torch
import math

def get_alibi_slopes(num_heads: int) -> torch.Tensor:
    def get_slopes_power_of_2(n):
        start = 2 ** (-(2 ** -(math.log2(n) - 3)))
        ratio = start
        return [start * (ratio ** i) for i in range(n)]

    if math.log2(num_heads).is_integer():
        slopes = get_slopes_power_of_2(num_heads)
    else:
        # Nearest power of 2 plus interpolation for non-power-of-2 heads
        n = 2 ** math.floor(math.log2(num_heads))
        slopes = get_slopes_power_of_2(n) + get_slopes_power_of_2(2 * n)[0::2][:num_heads - n]

    return torch.tensor(slopes)

def build_alibi_bias(seq_len: int, num_heads: int) -> torch.Tensor:
    slopes = get_alibi_slopes(num_heads)            # (num_heads,)
    positions = torch.arange(seq_len)
    # distance matrix: dist[i, j] = i - j (0 for j > i because causal)
    dist = positions.unsqueeze(0) - positions.unsqueeze(1)  # (seq, seq)
    dist = torch.clamp(dist, min=0)                          # only past distances

    # bias: (num_heads, seq, seq) โ€” negative so it penalises distance
    bias = -slopes.unsqueeze(-1).unsqueeze(-1) * dist.unsqueeze(0)
    return bias

# Usage in attention:
scores = q @ k.transpose(-2, -1) / math.sqrt(d_k)
scores = scores + alibi_bias[:, :seq_len, :seq_len]
attn = torch.softmax(scores, dim=-1)

Why ALiBi Extrapolates

The crucial property: ALiBi adds no positional information at the input level. The bias is applied to attention scores at each layer, and the formula works for any distance.

Training on 1024 tokens:
  Positions 0..1023 are seen
  Bias values are distances 0..1023 multiplied by slopes

Inference on 2048 tokens:
  New distances 1024..2047 appear โ€” never seen during training
  But the bias is just a linear function of distance
  โ†’ Model has already learned "larger penalty = further away"
  โ†’ Extrapolation is structurally sound

Compare to learned absolute:
  Position 1024 โ†’ unseen embedding โ†’ garbage representation

ALiBi vs RoPE

| Property | ALiBi | RoPE | |----------|-------|------| | How applied | Added to attention scores | Applied to Q/K before dot product | | Positional info in embeddings | No (no PE at input) | No (no PE at input) | | Extrapolation | Strong โ€” linear bias scales | Good โ€” frequency-based | | Head specialisation | Built-in (different slopes) | Not built-in | | Implementation complexity | Very simple | Moderate | | Modern usage | MPT, BLOOM, some EleutherAI | LLaMA, Mistral, most 2023+ |


Practical Context Length Extension

ALiBi-based models handle long context at inference without fine-tuning on long sequences:

MPT-7B: trained on 2048 tokens with ALiBi
At inference: tested up to 65K tokens
Performance degrades gracefully rather than catastrophically

RoPE models (without YaRN/LongRoPE fine-tuning):
At inference with 2ร— training length: significant quality drop

Interview Answer

"ALiBi adds a static linear penalty to attention scores: score(i,j) = qยทk/โˆšdโ‚– - mยท(i-j), where m is a fixed head-specific slope and (i-j) is the distance from key to query position. Closer tokens are penalised less; distant tokens more. Each head uses a different slope from a geometric sequence, so heads naturally specialise from local to global range. ALiBi requires no positional embeddings in the input and extrapolates gracefully to longer sequences at inference, because the linear bias formula works for any distance โ€” including distances not seen during training."

Enjoyed this article?

Explore the AI Systems learning path for more.

Found this helpful?

Share:๐•

Leave a comment

Have a question, correction, or just found this helpful? Leave a note below.