How does a learned relative position bias like T5's inject position information into attention?
Last updated
T5 adds a learned per-head scalar to each attention logit, indexed by bucketed relative distance:
q_i·k_j + b_h[bucket(j - i)]. Token representations themselves are never modified.Buckets (32 by default) are exact for small offsets and logarithmic for larger ones. Everything beyond 128 shares the last bucket, so any length gets a defined bias.
It is cheap (one shared embedding per stack) and generalizes well. However, it needs an L×L bias matrix, which slows fused kernels, and far distances become indistinguishable.
- https://github.com/huggingface/transformers/blob/main/src/transformers/models/switch_transformers/modeling_switch_transformers.py
- https://github.com/huggingface/transformers/blob/main/src/transformers/models/umt5/modeling_umt5.py
- https://github.com/huggingface/transformers/blob/main/src/transformers/models/t5/modeling_t5.py