Skip to main content
issue 2026-09-11AI Research36 minarXiv 2021interactive

ALiBi: Attention with Linear Biases for Length Extrapolation

Sinusoidal, rotary, and T5 relative biases degrade when L_valid exceeds the training length. ALiBi replaces bottom PE with a static linear distance penalty so the model can extrapolate.

Bias attention scores by distance — skip bottom PELeft: absolute or rotary position embeddings degrade when validation length exceeds training length L. Middle: ALiBi adds a fixed head slope times key–query distance to the logits. Right: the same model keeps usable perplexity past L without retraining at that length.ENTER · PE BREAKSL_VALID > LXTOKENPEABS/ROTPPLCLIMBSTRAIN LENGTH LOCKRUN · SCORE BIASNO BOTTOM PEQ_iQUERYKKEYSm·dLINEARSOFTMAX(QK + BIAS)LEAVE · PAST LTRAIN SHORTOKPPLSAME FROZEN SLOPESPosition lives in the logits — a fixed slope times key–query distanceALiBi biases scores by distanceMobile layout: PE failure past L, then m times distance on logits, then extrapolation.ENTERPEBREAKSRUN · m·dQmKLEAVEPAST LOK

When you train a Transformer language model, you pick a training length LL. At inference you often want more context than LL. Vaswani et al. hoped Transformers might “extrapolate to sequence lengths longer than the ones encountered during training.” In practice, models that inject position embeddings into the token stream — sinusoidal, rotary, or learned relatives like T5’s bias — often lose perplexity as soon as validation length LvalidL_{\mathrm{valid}} exceeds LL.

Attention with Linear Biases (ALiBi) throws out bottom-of-network positional embeddings. Instead it adds a fixed, head-specific linear penalty to the attention scores: farther keys get a more negative bias before Softmax. The slope mm is set once (not learned) and reused across domains. The punchline in the abstract: a 1.3B model trained on length 1024 extrapolates to 2048, matching a sinusoidal model trained on 2048 while training 11% faster and using 11% less memory (paper-reported).

What this paper explains

Press, Smith, and Lewis ask a systems-flavored modeling question with a tiny architectural answer:

  1. Define extrapolation. Keep next-token language modeling quality as LvalidL_{\mathrm{valid}} grows past the training length LL.
  2. Show the failure mode. On WikiText-103, sinusoidal / rotary / T5 relative methods degrade as LvalidL_{\mathrm{valid}} increases (their Figure 1; appendix Tables 2–4).
  3. Replace PE with score bias. For query ii, Softmax sees
softmax(qiK+m[(i1),,2,1,0]),\mathrm{softmax}\bigl(\mathbf{q}_i\mathbf{K}^{\top} + m\cdot[-(i-1),\ldots,-2,-1,0]\bigr),

with head-specific constant mm.

  1. Pick slopes once. For nn heads, use the geometric progression starting at 28/n2^{-8/n} with the same ratio — e.g. eight heads use 12,14,,1256\tfrac{1}{2},\tfrac{1}{4},\ldots,\tfrac{1}{256}.
  2. Scale the story. Beyond WikiText-103, transfer the same slopes to BooksCorpus and to a 1.3B model on CC100+RoBERTa, training shorter than the sinusoidal baseline while matching or beating its extrapolated perplexity.

This is not Ring Attention (sequence-parallel KV circulation) and not a KV-cache eviction policy. It is a position inductive bias sitting inside the attention score matrix.

Absolute PE versus ALiBi score biasLeft: content and position vectors are added before attention. Right: content alone; Softmax receives query-key scores plus a linear distance bias.Absolute / rotary PEx + p at the bottomx+pbreaks when L_valid > LALiBiSoftmax(qK + m · distance)x+m·d biasno bottom PE table
ALiBi moves position out of the embedding sum and into a closed-form attention logit bias.

Prior limits

  • Sinusoidal / absolute PE (Vaswani et al.) bind training and inference lengths in practice: when Lvalid>LL_{\mathrm{valid}} > L, WikiText curves in the paper climb in perplexity even under sliding-window evaluation.
  • Rotary (RoPE) and T5 relative bias improve relative modeling, but the authors’ WikiText extrapolation plots still degrade past LL (T5 also OOMs on their 32GB GPU at longer eval lengths in Figure 1).
  • Training longer is expensive. Appendix Figure 7 shows words/sec falling as LL grows — so “just train at the inference length” is a budget decision, not free.
  • Early RNNs routinely trained short and assumed longer eval; Transformers lost that habit once PE entered the bottom of the stack.

The mechanism

Drop bottom PE. Token embeddings enter the stack without added position vectors.

Bias the scores, not the vectors. After qiK\mathbf{q}_i\mathbf{K}^{\top} (and the usual scale), add a vector whose jj-th entry is m(ij)-m\cdot(i-j) for keys to the left of the query (causal LM). Softmax then sees a linear distance penalty. Everything else — FFN, residual path, causal mask — is unchanged.

Head-specific slopes, frozen. Different heads get different mm so the layer keeps a mix of near-sighted and longer-range heads. Values are not trained; they are the geometric recipe above. The paper argues you can reuse the same recipe across domains and sizes (WikiText → Books → 1.3B), analogous to how sinusoidal wavelengths were set once.

Recency inductive bias. Larger distance ⇒ more negative logit ⇒ Softmax mass shifts toward recent keys. That is intentional: language modeling often rewards local context, and the bias stays well-defined for any ii larger than those seen in training.

Interactive

How steep is the distance penalty?

Teaching dial: pick an 8-head ALiBi slope from the paper's geometric recipe 1/2 … 1/256, then a key–query distance. The bias is −m × distance on the logits — pedagogy, not a WikiText PPL table.

Bias −m·d-8.0000
At d=1-0.5000
At d=64-32.0000

Large negative logit — Softmax mass collapses toward recent keys for this head.

Algorithm / figure walkthrough

  1. Choose head count nn; set slopes mh=28/n(28/n)hm_h = 2^{-8/n}\cdot(2^{-8/n})^{h} for h=0,,n1h=0,\ldots,n-1 (paper’s geometric construction).
  2. Embed tokens without absolute / rotary PE at the bottom.
  3. For each attention head, compute scores qiK/d\mathbf{q}_i\mathbf{K}^{\top}/\sqrt{d} as usual.
  4. Add mh[(i1),,0]m_h\cdot[-(i-1),\ldots,0] to those scores (causal positions only).
  5. Softmax → attend → continue the Transformer block.
  6. At eval, feed sequences with Lvalid>LL_{\mathrm{valid}} > L; the bias formula does not need new PE rows.
ALiBi linear distance bias on attention scoresQuery at position i attends to earlier keys. Each key gets bias minus m times its distance from the query before Softmax.Query i · causal keys to the leftk_4m·(-4)-2.0 mk_3m·(-3)-1.5 mk_2m·(-2)-1.0 mk_1m·(-1)-0.5 mq_ibias 0farther keys → more negative logit → Softmax shifts toward recency
For a fixed slope m, the bias is exactly linear in key–query distance (paper Eq. in §3).

The opening visual is the same arc: ENTER with PE that breaks past LL; RUN with frozen slope × distance on the logits; LEAVE with usable longer context without retraining at that length.

What to notice when reading

  • Extrapolate ≠ memorize longer PE tables. ALiBi never allocates a longer embedding — the bias is a closed-form function of distance.
  • Where the non-linearity lives. Softmax still mixes keys; ALiBi only reshapes the logits. Do not confuse the linear bias with linear attention.
  • Comparison fairness. On the 1.3B runs, ALiBi trains on shorter LL than the sinusoidal baseline it is compared against when claiming speed/memory wins (Figure 5 / Table 11 narrative).
  • Non-extrapolating regime. On the large corpus, when both methods train at the same LL, ALiBi is merely similar to sinusoidal (Appendix Table 12) — the headline advantage is the train-short / eval-long setting plus WikiText wins even without extrapolation.
  • Implementation cost. Authors claim negligible memory overhead vs sinusoidal at the same LL (about 0–0.7%) and no extra parameters — you can implement by changing a few lines in the attention kernel.

Results and evidence

Numbers below are from the paper (arXiv:2108.12409) — not re-measured here.

WikiText-103 extrapolation (Figure 1; Tables 2–4). Models trained at L{512,1024,3072}L\in\{512,1024,3072\}: sinusoidal / rotary / T5 degrade as LvalidL_{\mathrm{valid}} grows; ALiBi stays flat or improves over a wide range past LL. Example at train L=512L=512: sinusoidal PPL goes from 20.05 at 512 inputs toward the low-20s+ as length increases, while ALiBi starts at 19.73 and continues improving into the high-19s in the tabulated range (see Table 2 for the full grid).

BooksCorpus transfer. Same slope recipe, BERT vocabulary, tied embeddings — ALiBi still extrapolates where sinusoidal does not (paper §4.1).

1.3B on CC100+RoBERTa (461 GB mix; 25 layers, 16 heads, d=2048d=2048). Figure 5 / abstract:

SettingTrain LLEval LvalidL_{\mathrm{valid}}Claim (paper)
ALiBi vs sin.5121024Within 0.06 PPL of sinusoidal trained at 1024; 7% faster; 1.6 GB less memory
ALiBi vs sin.102420480.09 PPL better than sinusoidal trained at 2048; 3.1 GB less memory; ~11% faster to a given PPL

Limitations

Compressed from the paper’s discussion and experimental scope:

  • Language modeling focus. Extrapolation is measured with perplexity on LM corpora; the paper does not claim universal wins on every downstream task or every architecture family.
  • Finite stretch, not unbounded. ALiBi curves stay healthy far past LL in their plots, but the authors do not assert arbitrary context without eventual degradation.
  • Recency bias is a prior. Tasks that need sharp long-range links may want different position machinery (or complementary systems tricks).
  • Parallel work. Wennberg & Henter (2021) also bias attention by distance with an RBF and trainable parameters — different functional form and cost profile.
  • Venue string. arXiv abs/OpenAlex list this as a 2021 preprint; we do not invent a conference badge beyond what those sources show.

How to read the paper

  1. Abstract + §1 — extrapolation definition and Figure 1 failure of PE methods.
  2. §3 — the Softmax bias formula, slope recipe, Figure 3.
  3. §4.1 — WikiText / Books results and ablations.
  4. §4.2 — 1.3B CC100+RoBERTa Figure 5 and memory/speed claims.
  5. §5 related work + appendix tables for exact PPL grids.

Knowledge check

What does ALiBi remove from the bottom of the Transformer, and what does it add instead?

For eight heads, which slope set does the paper use?

On the 1.3B CC100+RoBERTa runs, what is the abstract’s train/eval length pair for the headline comparison?

Keep reading / Sources