Summary
Position can change embeddings, query/key geometry, or scores before softmax. Those are different computations, not interchangeable ways to relabel a checkpoint. Unmasked position-free self-attention is permutation-equivariant: permuting input rows permutes output rows. A fixed causal mask already restricts positions asymmetrically.
Task: query position may read keys . Hold scaled content scores at zero and use causal ALiBi slope . Compute the attention probabilities, then change the slope and permitted directions.
The lineup
The pipeline is embeddings, Q/K projections, dot product, bias/mask, then softmax. Locate each method at its actual stage.
Sinusoidal (original Transformer, 2017)
Add fixed absolute-position vectors before the projections:
PE(pos, 2i) = sin(pos / 10000^(2i/d))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d))
Defined values at unseen positions do not guarantee useful extrapolation.
Learned absolute positional embeddings
Add a learned table row for each supported position, as in BERT or GPT-2. Unseen indices need an explicit adaptation, not an undefined table lookup.
Relative positional encoding
T5-style relative bucket biases enter scores; bucket definitions are part of the model. Shaw and Transformer-XL use other relative constructions, not the same lookup formula.
RoPE (Rotary Position Embeddings)
Rotate query/key coordinate pairs before their dot product, rather than adding an embedding vector.
For positions m and n, use their specified block-diagonal rotations:
Q' = R_m * Q (Q rotated by query position)
K' = R_n * K (K rotated by key position)
The rotation derivation proves the common-shift relationship for fixed content vectors. The figure retains positions and ; their relative offsets agree.
ALiBi (Attention with Linear Biases)
Add a distance bias after the scaled content score:
score_h(i, j) = Q_i K_j^T / sqrt(d) - s_h * (i - j)
Here is head width, is a fixed head-specific slope, and allowed causal keys satisfy . Forbidden scores still require a separate mask.
Learning objective
RoPE rotates vectors; ALiBi subtracts distance from attention scores
Derivation
Does an ALiBi penalty rotate the stored query and key vectors?
Compute and change the row
| Slope | Distances | Score biases | Softmax probabilities |
|---|---|---|---|
For the first row, exponentials are , summing to . The newest key is times as likely as the oldest because their content scores match. Shifting query to and keys to preserves distances and probabilities for unchanged content.
New task: strengthen recency preference. Double the slope and recompute softmax, as the second row shows. Use when: deliberately adapting head distance preferences; changing deployed slopes changes the model.
New task: allow an encoder query at to read key . Use a bidirectional mask and the specified absolute-distance bias. Check: slope gives , not from misusing signed causal distance. Use when: available source positions lie on both sides.
Position-free / implicit position
A causal mask or recurrence can impose order without adding a positional vector. Mixture-of-experts routing alone does not supply sequence position.
What an interviewer expects you to say
Separate position representation, visibility, and context capacity. Failure: a fixed slope is not a causal mask; unmasked future keys can receive an unintended positive signed-distance bias.
Reconstruct: can RoPE replace ALiBi unchanged because both use relative positions?
Answer
No. Rotation changes content dot products; ALiBi adds score biases. The same trained weights need not preserve scores, probabilities, or useful behavior after substitution.
Common confusions
Relative scores do not guarantee long-context generalization. Context capacity also depends on memory, compute, implementation limits, and training. An unbounded position formula is not an empirical quality result.
Long-context extension techniques
- Position interpolation rescales positions toward the trained range, with its specified continued training.
- NTK-aware variants change the frequency schedule; inspect the actual formula rather than assuming one universal implementation.
- YaRN changes interpolation and attention scaling under its chosen recipe.
- Continued long-context training changes the learning exposure and compute budget, not merely a position index limit.
Evaluate short-context retention, retrieval, reasoning, location sensitivity, latency, and memory separately. One successful retrieval needle does not qualify every long-document task.
Why interviewers ask
The useful test is whether you can locate the operation, reproduce the score change, and specify evidence for adaptation.
Related: Transformer architecture, FlashAttention, KV cache.