Zenaique

Define T5's relative position bias and what bucketing buys you

Short answer·Medium·4.0 · 0·~3 min·Asked atNykaaObserve AiRobinhood
Attempt it

T5 injects positional information via a relative position bias added to attention scores. Describe the mechanism precisely, including what 'bucketing' means and why T5 uses it. Note where parameters are shared and where they are not.

Free · 2 AI evals / day
TL;DR

T5 adds a learned scalar bias to the pre-softmax score, indexed by the offset (i minus j) through a log-spaced bucketing function so the parameter count stays small.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Think of a long auditorium with numbered seats. Instead of giving each seat its own name tag, T5 hands out a small set of distance stickers: one for 'right next to me', one for 'a few seats away', one for 'across the row', one for 'somewhere far'. Two people sitting close get the same close-distance sticker no matter where they are in the auditorium; two people far apart get the same far-distance sticker. The model learns one rule per sticker about how much to listen, and that rule applies everywhere. The auditorium can be any size and the model still works, because the stickers cover the whole range with a small fixed set.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

T5's relative position bias is the cleanest example of the 'put position on the score' design philosophy. Instead of injecting position into the input embeddings (like BERT's learned PE or the original Transformer's sinusoidal PE) or into the Q and K vectors (like RoPE), T5 adds a learned scalar to the pre-softmax attention score for each pair of positions. The scalar depends only on the offset between positions, making it a relative encoding.

The scheme is elegant for two reasons. It's translation-invariant by construction: shifting the whole sequence by k positions changes nothing because the bias depends only on offsets. And it's parameter-efficient: a small bucketing function maps arbitrary offsets to a fixed set of buckets, so total bias parameters stay in the low thousands regardless of context length.

This deep dive walks through the bias formula, the bucketing function in detail, the sharing pattern across layers and heads, the limits of length extrapolation, and the trade-offs vs RoPE and ALiBi.

The bias formula and where it sits

The attention score in T5 is the standard dot product plus a learned offset-indexed bias:

score(i,j,h)=qikjdk+b(ij,h)\text{score}(i, j, h) = \frac{q_i \cdot k_j}{\sqrt{d_k}} + b(i - j, h)

Where i is the query position, j is the key position, h is the head index, and b is the learned bias table.

What sits where

  • Input embeddings: no positional component; just token embeddings.
  • Q and K vectors: no rotation, no positional mixing. Pure projections of the hidden states.
  • Score matrix: bias added here, just before softmax.

The contrast with other schemes is sharp. BERT adds a learned PE vector to the input embeddings. The original Transformer adds sinusoidal PE to the input. RoPE rotates Q and K by position-dependent angles inside the attention. T5 does none of these; the only position signal is the score-side bias.

Translation invariance for free

Because the bias depends only on i - j, shifting all positions by k leaves every bias value unchanged. This is the defining property of relative position encoding: the model cares about distances between tokens, not their absolute indices.

The bucketing function
Sharing pattern and parameter accounting
Length extrapolation and the comparison to RoPE / ALiBi
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
python
import torch

def relative_position_bucket(relative_position, num_buckets=32, max_distance=128):
    # T5's bucketing function: half the buckets for negative offsets,
    # half for positive; close offsets get unique buckets,
    # far offsets get log-spaced buckets.
    ret = 0
    n = -relative_position
    num_buckets //= 2
    ret += (n < 0).long() * num_buckets
    n = n.abs()
    max_exact = num_buckets // 2
    is_small = n < max_exact
    val_if_large = max_exact + (
        torch.log(n.float() / max_exact)
        / torch.log(torch.tensor(max_distance / max_exact))
        * (num_buckets - max_exact)
    ).long()
    val_if_large = torch.min(val_if_large, torch.full_like(val_if_large, num_buckets - 1))
    ret += torch.where(is_small, n, val_if_large)
    return ret

# bias table: (num_heads, num_buckets), shared across all layers
bias_table = torch.nn.Parameter(torch.zeros(12, 32))

# at attention time: compute offsets, bucket them, lookup
i = torch.arange(seq_len)[:, None]
j = torch.arange(seq_len)[None, :]
buckets = relative_position_bucket(j - i)
bias = bias_table[:, buckets]  # (num_heads, seq_len, seq_len)
scores = (q @ k.transpose(-2, -1)) / math.sqrt(d_k) + bias

Real products, models, and research that use this idea.

  • T5 (Raffel et al. 2020) and its descendants (Flan-T5, mT5) all use this bias scheme as the only position signal.
  • Google's UL2 follows T5's relative bias design for its denoising-objective pretraining.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy log-spaced buckets specifically, instead of uniform or quadratic spacing?
A

Linguistic and visual structure tends to fall off in importance roughly logarithmically with distance: very close tokens carry most syntactic dependency, medium-range tokens carry phrase-level structure, and very far tokens carry coarse topical context. Log spacing matches that distribution by giving close offsets fine resolution and far offsets coarse resolution, getting more useful inductive bias per parameter.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Confusing T5's bias with a learned absolute positional embedding added to the input. The bias goes onto the score, not the embedding, and depends only on the offset between positions, not on absolute index.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Where T5 puts the position signal in the attention pipeline

  • Exact formula for the modified attention score with bias

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Explain scaled dot product attention.
Short answer·Medium