ALiBi adds no positional embedding. It adds a bias −m(i−j) to the attention score between query position i and key position j≤i, with a head-specific slope m. For a query at position 4 whose four raw scores are all equal, what attention weights result when m=0.5 and when m=1/16? Give the slopes for 8 heads, and explain why ALiBi can help a model run on longer sequences than it was trained on?

Sequence models and transformers, Positional encoding
Level L3 Intermediate. Length Medium. Nature Th+Mat Both.

What's being asked

Whether you can read a relative-position bias as a change to the softmax, and see why it behaves well beyond the training length.

Key idea

ALiBi uses scoreij=qi⊤kj/dk−m (i−j). Since the softmax is shift-invariant, equal raw scores give weights proportional to e−m(i−j), so each head has a built-in recency preference that decays geometrically with distance.

Steps

  1. With equal raw scores the distances from position 4 to keys 1,2,3,4 are 3,2,1,0, so weights are ∝e−3m,e−2m,e−m,1.
  2. For m=0.5: e−1.5=0.2231, e−1=0.3679, e−0.5=0.6065, 1. The sum is 2.1975, so the weights are (0.102, 0.167, 0.276, 0.455). A strong recency bias.
  3. For m=1/16: e−0.1875=0.829, e−0.125=0.8825, e−0.0625=0.9394, 1. The sum is 3.651, giving (0.227, 0.242, 0.257, 0.274). Almost uniform, so this head looks far back.
  4. Slopes for n=8 heads form a geometric sequence 2−8i/n, which is 12,14,18,…,1256. Steep slopes give local heads and shallow slopes give long-range heads.
  5. Extrapolation. The bias depends only on the distance i−j, and no position index is ever fed into the network. At test time with longer sequences, distances simply grow, and the penalty keeps growing linearly, so far tokens get small weight. Learned or sinusoidal absolute embeddings meet position values never seen in training.
  6. The slopes m are fixed, not learned, and the bias is added only to the scores (not to the values), so there is no extra parameter or memory.

Common mistakes

How long your answer should be

Half a page; 4–5 minutes.

Last checked on 4 December 2026.

Read this

Previous in Sequence models and transformers: In connectionist temporal classification (CTC), an acoustic model gives, for each of T=4 frames, a distribution over the symbols {a,b,blank}. How are alignments mapped to labels, how many alignments of length 4 collapse to the label "ab", how many to "aa", and what is the probability of "ab" if every frame is uniform over the three symbols?
Next in Sequence models and transformers: A unigram language-model tokeniser has the vocabulary and probabilities h 0.10, u 0.10, g 0.10, s 0.10, hu 0.10, ug 0.10, hug 0.20, gs 0.05, ugs 0.15. Which segmentation of "hugs" does it choose, how is it found efficiently, and how does this differ from BPE?