Define T5's relative position bias and what bucketing buys you
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.
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.
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.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
6m: score-side bias formula, bucketing function with log-spaced buckets, parameter accounting and layer sharing, contrast with RoPE and ALiBi, length-extrapolation limits, and interaction with FlashAttention.
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) + biasReal 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.
- ByT5 (byte-level T5) keeps the same bucketing function despite operating on much longer character sequences.
- Most 2026 frontier decoders (Llama 4 Maverick, Claude Opus 4.7, Gemini 3.1 Pro, DeepSeek V4) have moved away from T5-style biases to RoPE plus YaRN or position interpolation for long-context training.
- Swin Transformer for vision uses a related learned bias table idea inside windows, drawing directly on the T5 design.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy log-spaced buckets specifically, instead of uniform or quadratic spacing?
QCan T5 bias extrapolate to sequences longer than what it was trained on?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
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.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.