﻿# ALiBi: adding distance bias to attention scores

ALiBi introduces a head-dependent distance bias into attention scores. Learn how this differs from changing token vectors and what length extrapolation evidence means.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/alibi-attention-distance-bias/
Language: en

Sequence order can enter a model through the attention score rather than through an added position vector. ALiBi follows this direction by applying a distance-based bias. The mechanism becomes clear when content similarity and positional preference are written as separate score contributions.

## The bias changes the comparison before normalization

Attention first constructs content-dependent scores for eligible query-key pairs. ALiBi adds a position-related contribution before softmax. The contribution depends on distance and a slope associated with the head. It therefore changes relative preference across key positions without changing their vocabulary identities. This is a score-level operation, unlike a method that directly adds a learned position vector to the input. Both supply order information, but they do so at different locations in the attention computation.

## Head slopes provide different distance preferences

Different heads can receive different slopes, allowing their positional biases to operate at different distance scales. The content score still matters: a distance penalty is not an instruction to ignore every distant key. A useful explanation considers the sum of content evidence and positional bias before normalization. Treating a head’s slope as a hard attention window would misdescribe the mechanism. A hard mask removes eligibility, while a finite bias adjusts preference among positions that remain available to the query.

## Compare equally matched keys at different distances

As a thought experiment, give two eligible keys equal content scores but different distances from a query. Their positional contributions can differ, so the resulting normalized weights need not be equal. Next increase the distant key’s content score and examine how the balance changes. These controlled comparisons explain the bias without claiming that a particular natural-language token always wins. Real learned vectors supply varying content scores, and the rest of the network further transforms the aggregate information.

## Keep mechanism and extrapolation evidence connected

The original study investigates training on shorter sequences and evaluating longer ones. Its evidence motivates the method but does not establish unlimited context quality for arbitrary checkpoints. Sliding-window sparsity, position interpolation and score biases answer different architectural questions. When reading a model implementation, identify whether ALiBi is actually used, how slopes and distances are calculated, and what masks apply. Then evaluate the task and length range of interest. A precise mechanism description helps interpret such a test rather than replace it with a general extrapolation promise.

## Primary research and implementation references

- [Longformer: The Long-Document Transformer](https://arxiv.org/abs/2004.05150).

- [RoFormer: Enhanced Transformer with Rotary Position Embedding](https://arxiv.org/abs/2104.09864).

- [Extending Context Window via Positional Interpolation](https://arxiv.org/abs/2306.15595).

- [Train Short, Test Long: Attention with Linear Biases](https://arxiv.org/abs/2108.12409).

## Related engineering context

[Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/); [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/).
