Relative Position Encoding › Clipping Relative Positions
Language AI HandbookAll chapters Relative Position Encoding › Clipping Relative PositionsOne section · Sections in this chapter5 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The formulation above assumes we have a separate embedding for every possible relative position. But this creates a practical problem: for a sequence of length , relative positions range from to , requiring embeddings. For a sequence of length 512, that's 1023 different embeddings per dimension, for both keys and values.
This approach has two flaws. First, memory grows linearly with sequence length. Second, distant positions appear infrequently during training. In a 128-token sequence, only one position pair has offset (position 0 to position 127), while thousands of pairs have offset . The embedding for offset would be severely undertrained: the gradient updates for that embedding come from only one pair per sequence, while the embedding for offset receives gradient contributions from 127 pairs. The result is that distant-position embeddings never become reliable.
Shaw et al. address both problems with a simple insight: fine-grained distance matters more locally than globally. The difference between "3 positions back" and "4 positions back" may be linguistically significant, as it could distinguish an adjective-noun relationship from a determiner-noun relationship. But the difference between "100 positions back" and "101 positions back" rarely carries meaning. Both are simply "far away." The model can usefully represent "very distant" as a single category without losing important information.
This motivates clipping: relative positions beyond a maximum distance are clamped to :
where:
- : the raw relative position (offset from position to position )
- : the maximum relative position to distinguish (a hyperparameter)
- : clamps to the range , returning if , if , and otherwise
With clipping, we need only relative position embeddings regardless of sequence length. Position pairs further than apart share the same "far away" embedding, either for "far backward" or for "far forward."
The choice of reflects a trade-off between expressiveness and learnability. Typical values range from 16 to 128, depending on the expected locality of relevant patterns in the data. For sentence-level tasks where the longest dependencies span 10-20 tokens, often suffices. For document-level tasks with paragraph-scale dependencies, or gives the model more distance resolution. In practice, the performance difference between and is often small, but the difference between and can be significant for tasks involving long-range grammatical dependencies.
def clip_relative_position(j, i, max_dist):
"""
Compute clipped relative position.
Args:
j: Key/value position
i: Query position
max_dist: Maximum relative distance to distinguish
Returns:
Clipped relative position in range [-max_dist, max_dist]
"""
rel_pos = j - i
return max(-max_dist, min(max_dist, rel_pos))
# Demonstrate clipping
max_k = 4
positions = list(range(10))
query_pos = 5Query position: 5
Maximum relative distance (k): 4
Position | Raw Offset | Clipped Offset
------------------------------------------
0 | -5 | -4
1 | -4 | -4
2 | -3 | -3
3 | -2 | -2
4 | -1 | -1
5 | +0 | +0
6 | +1 | +1
7 | +2 | +2
8 | +3 | +3
9 | +4 | +4The table reveals how clipping works in practice. Positions 0 and 1 have raw offsets of -5 and -4, but both clip to -4 because they exceed our maximum distance of 4. Position 9 has raw offset +4, which equals the maximum so no clipping occurs. All positions beyond the clipping threshold share the same "far away" embedding, which captures the intuition that distinguishing between "very far" and "extremely far" rarely matters for language understanding.

Notice the diagonal band structure: each anti-diagonal contains the same relative position. The corners, where positions are far apart, are clipped to the boundary values . This Toeplitz-like structure is key to efficient implementation because it means position biases repeat in a predictable pattern that can be computed once and reused.