ALiBi Adds Distance Bias to Scores

ALiBi adds a finite head-specific linear distance penalty to allowed attention scores. Calculate its softmax effect, compare slopes, distinguish bias from masking, and keep published extrapolation evidence within its measured setup.

Attention with Linear Biases, or ALiBi, does not add a position vector to each token. It adds a head-specific distance penalty to attention scores before softmax.

For causal attention using query index ii and readable key index jij\le i, one convention is

eij(r)=qi(r)kj(r)dkmr(ij),e^{(r)}_{ij} =\frac{q_i^{(r)\top}k_j^{(r)}}{\sqrt{d_k}} -m_r(i-j),

where mr>0m_r>0 is the slope for head rr. Earlier keys have larger distances and receive more negative biases.

Calculate One Biased Row

Let query index i=3i=3 read key indices j=0,1,2,3j=0,1,2,3. Suppose the content scores are all zero and the head slope is mr=0.5m_r=0.5. The distances are

[3,2,1,0],[3,2,1,0],

so the biases are

[1.5,1.0,0.5,0].[-1.5,-1.0,-0.5,0].

After softmax, the attention weights are approximately

[0.102,0.167,0.276,0.455].[0.102,0.167,0.276,0.455].

The closest key receives the largest weight only because the content scores were equal. A sufficiently strong content score for an earlier token can overcome the distance penalty.

Different Slopes Give Different Distance Preferences

A small slope changes slowly with distance. A larger slope more strongly prefers recent keys, all else equal. Assigning different slopes to different heads gives the layer several fixed distance priors without learning a position embedding table.

The slopes are part of the method's configuration. Their schedule must be reproduced when porting a model. Replacing every slope with one shared value changes the model even though all tensor shapes remain valid.

Separate content scores from ALiBi bias

Change the content scores and slopes to see when distance preference is preserved or overcome.

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

Bias Is Not a Mask

A causal mask assigns forbidden future keys an effectively negative-infinite score, making their weights zero. ALiBi applies finite penalties to allowed keys. It shapes preference but does not change which positions are legal. Implementations may combine the causal mask and ALiBi bias in one additive tensor, but their meanings remain different.

Keep the Extrapolation Claim Bounded

Press, Smith, and Lewis trained a 1.3-billion-parameter language model on length-1024 inputs and evaluated it at length 2048. In their reported setup, ALiBi matched the perplexity of a sinusoidal model trained at length 2048 while reducing training memory and time under the stated comparison.

This evidence motivates the method. It does not guarantee the same result for every architecture, dataset, task, context extension, slope schedule, or evaluation metric.

Q1. Calculate an ALiBi score bias

A causal query is at index 5, a readable key is at index 2, and the head slope is 0.250.25. What ALiBi bias is added under the convention on this page?

Compute it first, then check your number.

Hint
Use mr(ij)-m_r(i-j).
Solution
0.25(52)=0.75-0.25(5-2)=-0.75.
Not attempted
Review

Not marked done.

Reference

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.