T5 Architecture › Relative Position Biases

Michael BrenndoerferAugust 14, 202512 min read
Language AI HandbookAll chapters
Part 25 of 67Encoder-Decoder Models
Chapter 177T5 Architecture › Relative Position Biases
Reading section 4 of 12 · Relative Position Biases · 12 minSection 4 of 12 · 12 min

Book contents

Section 4 of 12 · 12 min read

T5 Architecture › Relative Position BiasesOne section · Sections in this chapter4 / 12 ↓
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

Standard Transformers use absolute position embeddings. Each position in the sequence gets a fixed embedding vector added to the token embedding. T5 takes a different approach with relative position biases, which encode the distance between tokens rather than their absolute locations. This design decision shows a better understanding of what position information the model needs.

Why Relative Positions?

Absolute positions have limitations that become apparent when you consider how humans process language. When reading the phrase "the big red ball," understanding that "big" and "red" both modify "ball" doesn't depend on whether this phrase appears at the beginning or middle of a paragraph. What matters is that these words are adjacent to each other. A model trained on sequences up to 512 tokens has never seen position 513, making generalization to longer sequences difficult with absolute positions. The embedding for position 513 simply doesn't exist, forcing awkward workarounds like position interpolation.

Relative positions encode the offset between the query and key positions in attention, capturing this linguistically real notion of proximity. Whether two words appear at positions 10 and 15 or positions 100 and 105, they're "5 positions apart" in both cases. This translation invariance helps the model generalize across different sequence positions. A relationship learned between adjacent words at the beginning of training examples automatically transfers to adjacent words anywhere in any sequence.

The key insight is that position biases should modify attention scores directly. Rather than adding position information to token embeddings (which then influences attention indirectly through the learned query/key projections), T5 adds learned biases directly to the attention logits. In standard attention, the attention score between positions ii and jj becomes:

score(i,j)=qi⋅kjdk+b(j−i)\text{score}(i, j) = \frac{\mathbf{q}_i \cdot \mathbf{k}_j}{\sqrt{d_k}} + b(j - i)

where:

  • qi\mathbf{q}_i: the query vector at position ii
  • kj\mathbf{k}_j: the key vector at position jj
  • dkd_k: the dimension of the key vectors (used for scaling)
  • b(j−i)b(j - i): the learned bias for relative position j−ij - i

Let's unpack what this formula tells us. The first term, qi⋅kjdk\frac{\mathbf{q}_i \cdot \mathbf{k}_j}{\sqrt{d_k}}, is the standard scaled dot-product attention score that measures content-based similarity between positions. This captures whether the semantic content at position ii wants to attend to the semantic content at position jj. The second term, b(j−i)b(j - i), adds a position-based preference that depends only on how far apart the positions are, not where they sit in the sequence. A token might learn that it generally wants to attend strongly to the immediately preceding token (j−i=−1j - i = -1), regardless of content.

The bias term b(j−i)b(j - i) depends only on the distance between positions, not their absolute values. This allows the same position relationship to receive the same bias regardless of where it appears in the sequence. The model can learn, for example, that in English text, tokens often attend strongly to words 1-3 positions away (local syntactic patterns) while having weaker but still real attention to more distant positions (long-range dependencies).

Notice that the position bias is added directly to the pre-softmax logits, not to the post-softmax weights. This placement matters: adding to logits means the bias operates in the same space as the content-based scores, letting a large positive bias to override weak content similarity and a large negative bias to suppress attention even when content similarity is high. An absolute position embedding added to token representations, by contrast, must be routed through the learned query and key projections before influencing attention scores, giving the model less direct control over position-based attention patterns.

Relative Position Bias

A learned scalar added to attention logits based on the distance between query and key positions. Unlike position embeddings added to token representations, position biases directly modulate attention weights.

T5's Position Bias Implementation

T5 uses a bucketed relative position scheme that balances expressiveness with parameter efficiency. Instead of learning a separate bias for every possible offset (which would require unbounded parameters as sequence length grows), T5 groups offsets into logarithmically spaced buckets. For a query at position ii and a key at position jj, the relative position is computed as:

r=j−ir = j - i

where:

  • rr: the relative position offset (positive when the key comes after the query, negative when before)
  • ii: the position of the query token in the sequence
  • jj: the position of the key token in the sequence

This offset rr is then mapped to a bucket index, and the model learns a bias value for each bucket. The bucketing strategy encodes an important insight: precise position differences matter more for nearby words than for distant ones. Whether a related word is exactly 57 or 62 positions away rarely changes its relevance, but whether it's 1 or 2 positions away often does.

The bucketing works as follows:

  1. Compute the relative position: r=j−ir = j - i where ii is the query position and jj is the key position
  2. For small offsets, use exact values (each offset gets its own bucket)
  3. For larger offsets, use logarithmic bucketing (multiple offsets share a bucket)
  4. Look up the learned bias for that bucket

The logarithmic spacing means nearby positions (which often carry more grammatical signal) get fine-grained distinctions, while distant positions are grouped more coarsely. This keeps the parameter count manageable while still capturing useful position information. With 32 total buckets split between forward and backward directions, the model can stand for a rich set of position relationships without requiring thousands of parameters per attention head.

For larger offsets, the bucket index is computed using logarithmic scaling:

b=bexact+⌊log⁡(r/bexact)log⁡(dmax/bexact)⋅(Bdir−bexact)⌋b = b_{\text{exact}} + \left\lfloor \frac{\log(r / b_{\text{exact}})}{\log(d_{\text{max}} / b_{\text{exact}})} \cdot (B_{\text{dir}} - b_{\text{exact}}) \right\rfloor

where:

  • bb: the final bucket index for this relative position
  • bexactb_{\text{exact}}: the number of buckets reserved for exact (small) offsets
  • rr: the absolute value of the relative position offset
  • dmaxd_{\text{max}}: the maximum distance considered (default 128 in T5)
  • BdirB_{\text{dir}}: the number of buckets per direction (half the total buckets)

This formula deserves careful examination because it reveals the design philosophy behind T5's position encoding. The numerator log⁡(r/bexact)\log(r / b_{\text{exact}}) measures how far beyond the exact-bucket threshold the offset reaches, on a logarithmic scale. Dividing by log⁡(dmax/bexact)\log(d_{\text{max}} / b_{\text{exact}}) normalizes this to a value between 0 and 1 across the range of larger offsets. Multiplying by (Bdir−bexact)(B_{\text{dir}} - b_{\text{exact}}) spreads these normalized values across the available buckets for large offsets. The floor operation ensures we get discrete bucket indices. Adding bexactb_{\text{exact}} shifts the result into the correct range, after the buckets reserved for small exact offsets.

This formula maps offsets beyond bexactb_{\text{exact}} into logarithmically-spaced buckets. This keeps the distinction between positions 1 and 2 is preserved while positions 50 and 55 share the same bucket. The logarithmic spacing means bucket boundaries grow exponentially: perhaps buckets for offsets 1, 2, 3, 4, then 5-7, 8-15, 16-31, 32-63, and so on. This mirrors human perception of distance. We notice fine distinctions between nearby objects but group distant objects more coarsely.

An important practical benefit of this design is length generalization. Because the biases are parameterized by relative distance rather than absolute position, the model can handle sequences longer than those seen during training without requiring any extrapolation of position embeddings. For offsets beyond dmax=128d_{\text{max}} = 128, T5 simply clamps them to the last bucket, assigning the same bias to all very distant positions. This is a mild form of extrapolation, and in practice it works well: the model does not become confused by long documents, it simply treats all tokens beyond 128 positions as "distant."

In[6]:
Code
import numpy as np


def compute_t5_bucket(relative_position, num_buckets=32, max_distance=128):
    """
    Compute T5's relative position bucket.
    T5 uses half the buckets for exact positions, half for log-spaced.
    """
    relative_buckets = 0

    # Handle negative (backward) positions
    # In encoder, positions can be negative (key before query)
    # Use separate buckets for forward and backward
    num_buckets_per_direction = num_buckets // 2

    if relative_position < 0:
        relative_buckets = num_buckets_per_direction
        relative_position = -relative_position

    # Exact buckets for small offsets
    max_exact = num_buckets_per_direction // 2
    if relative_position < max_exact:
        return relative_buckets + relative_position

    # Log buckets for larger offsets
    relative_position_if_large = max_exact + int(
        np.log(relative_position / max_exact)
        / np.log(max_distance / max_exact)
        * (num_buckets_per_direction - max_exact)
    )
    relative_position_if_large = min(
        relative_position_if_large, num_buckets_per_direction - 1
    )

    return relative_buckets + relative_position_if_large


# Show bucket assignments for different offsets
print("Offset -> Bucket mapping:")
offsets = [-10, -5, -1, 0, 1, 2, 5, 10, 20, 50, 100]
for offset in offsets:
    bucket = compute_t5_bucket(offset)
    print(f"  Offset {offset:4d} -> Bucket {bucket:2d}")
Out[7]:
Console
Offset -> Bucket mapping:
  Offset  -10 -> Bucket 24
  Offset   -5 -> Bucket 21
  Offset   -1 -> Bucket 17
  Offset    0 -> Bucket  0
  Offset    1 -> Bucket  1
  Offset    2 -> Bucket  2
  Offset    5 -> Bucket  5
  Offset   10 -> Bucket  8
  Offset   20 -> Bucket 10
  Offset   50 -> Bucket 13
  Offset  100 -> Bucket 15

Notice how small positive offsets (0, 1, 2) each get unique buckets, while larger offsets (20, 50, 100) start collapsing into shared buckets. Negative offsets (key before query) use a separate set of buckets, letting the model to learn different biases for forward vs. backward attention. This asymmetry makes linguistic sense: attending to a word that came before ("I saw the") versus a word that comes after ("the dog ran") often serves different purposes, and the model can learn distinct patterns for each direction.

Out[8]:
Visualization
Staircase plot showing T5 relative position bucket mapping, with fine-grained buckets for small offsets and coarse-grained buckets for larger offsets.
T5's logarithmic bucketing assigns unique buckets to small offsets (fine-grained) while grouping larger offsets together (coarse-grained). The staircase pattern shows bucket boundaries growing exponentially, which shows the intuition that precise distances matter more for nearby tokens.

Visualizing Position Biases

Let's visualize the actual learned position biases from a trained T5 model:

Out[9]:
Visualization
Heatmap showing T5 position bias matrix with stronger attention near the diagonal.
Learned relative position biases for T5-small's first encoder layer, head 0. Positive biases (lighter) encourage attention between those relative positions, while negative biases (darker) suppress it. The diagonal band of positive values shows the model's learned preference for local attention to nearby tokens.

The bias pattern shows the model has learned to encourage attention to nearby positions (near the diagonal) while letting more flexibility for distant positions. This structure emerges purely from training. The model discovers what relative position patterns help solve its pretraining objective. Different attention heads learn different position bias patterns: some heads specialize in local attention (strong diagonal bands), while others develop patterns suited for attending to specific structural positions like the beginning of the sequence or position zero.