We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Position Encodings step 4 of 5
ALiBi-Style Distance Bias
A distance bias lowers the attention score between positions that are far apart. Different heads can use different penalty strengths: a larger slope subtracts more for the same distance.
ALiBi applies a distance-based penalty to attention scores rather than adding position vectors to embeddings. This exercise uses a specified symmetric distance penalty and a simplified slope schedule. Implement the formulas below, not another implementation's slope generator or causal mask.
Your task
Implement alibi_attention(S, n_heads).
- S is a floating-point array of shape
(n_heads, seq_len, seq_len). n_headsis a positive integer matching S.shape[0].seq_lenis positive. Rows index queries and columns index keys.
For each zero-based head index h, use:
slope = 2**(-8 * h / n_heads)
output[h, i, j] = S[h, i, j] - slope * abs(i - j)
Use ordinary division in the exponent. Return the same shape without modifying S. The first head has slope 1. No softmax or masking is part of this function: return biased scores, not attention weights.
Example
For two heads the slopes are 1 and 1/16. If each score matrix is all ones and the sequence length is two, the outputs are:
head 0: [[1, 0], head 1: [[1, 0.9375],
[0, 1]] [0.9375, 1]]
Diagonal scores stay unchanged because their distance is zero. The penalty is symmetric, so a key two positions ahead receives the same penalty as one two positions behind. The output need not be symmetric if S is not symmetric.
A finite penalty does not exclude future positions. A caller that needs causal attention must apply a separate causal mask before softmax. This task only adds the distance bias.
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Give attention information about token order
Self-attention based only on token content has no absolute position input. Position information can be added to token vectors, added to attention scores, or introduced by transforming queries and keys.
Learned absolute embeddings provide a vector for each position. Sinusoidal encodings use sine and cosine pairs at different frequencies. When adding either to token embeddings, the feature widths must match. In a sinusoidal encoding, position determines the angle while the feature-pair index determines the frequency.
Describe the distance between query and key
Relative-position methods represent the relationship between two positions.
A learned score table, for example, can use their signed distance as an index.
For query position 3 and key position 1, key - query is -2, while
query - key is 2. Use the task's sign convention.
Clipping distances to a fixed range makes all more-distant pairs share an entry at the corresponding end of that range.
Rotate vectors or add a score bias
RoPE rotates pairs of query and key coordinates according to position. Rotations preserve vector lengths. Applying the same frequency scheme to queries and keys makes their dot product depend on the relative rotation between their positions. RoPE transforms the vectors rather than adding an embedding to them.
Some implementations pair adjacent features; others pair corresponding features from the two halves of a vector. Use the specified pairing, because it changes the result.
ALiBi adds a linear distance bias to attention scores, with a slope for each head. It affects preferences among keys without creating a position-embedding vector. Causal masking is still needed separately: a finite negative score bias does not give a future key exactly zero attention.
Check position zero, equal positions, and a small offset. With a cache during generation, use the new token's actual sequence position; restarting at zero on every step gives the wrong positional relationship to cached keys.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.