Why do Transformers require explicit positional information while RNNs do not?
Last updated
An RNN processes tokens one after another, so order is baked into its computation. Self-attention treats its input as an unordered set: permute the tokens and the outputs are permuted in the same way. So Transformers must inject position explicitly.
RNNs: order is implicit: The hidden state
h_t = f(h_{t-1}, x_t)depends on everything before step t, so swapping two tokens changes the result.Self-attention is permutation-equivariant:
Attention scores depend only on content (
q_i · k_j), not on where tokens sit. The FFN is applied per token, so it adds no order either.Without positions, "dog bites man" and "man bites dog" would produce the same set of representations.
How position is added:
Absolute encodings added to the embeddings: sinusoidal (original paper) or learned (BERT, GPT-2).
Relative schemes inside attention: RoPE rotates
qandk, and ALiBi adds a distance-based bias. These tend to extrapolate better to longer sequences.
Nuance: A causal mask breaks the symmetry, so decoder-only models without positional encodings (NoPE) can partly infer position. Bidirectional encoders cannot.