Positional Encoding
How Transformers understand token order β sinusoidal encoding, learned embeddings, RoPE, ALiBi, and long-context extension techniques.
Self-attention is permutation-equivariant β shuffling input tokens produces shuffled outputs with the same values. Without positional information, the model cannot distinguish βthe cat sat on the matβ from βmat the on sat cat the.β Positional encoding solves this.
1. Why Position Matters
Given tokens and any permutation , self-attention satisfies:
The attention scores depend only on the content of Q and K, not their positions. We need to inject position information explicitly.
2. Absolute Positional Encoding
Sinusoidal (Original Transformer)
Add position-dependent vectors to token embeddings:
Each dimension oscillates at a different frequency. Low dimensions change slowly (capture long-range patterns), high dimensions change rapidly (capture nearby positions).
Properties: Fixed (no learnable parameters), theoretically enables extrapolation to longer sequences, but in practice extrapolation is poor.
Learned Positional Embeddings (GPT-2)
Simply learn a embedding table β one vector per position. Simple and effective, but hard-coded to a maximum sequence length. Cannot extrapolate beyond training length.
Limitations of Absolute Encoding
Both approaches encode position as a fixed offset from the start. Problems:
- Relative distances between tokens are hard to learn from absolute positions
- No generalization to sequence lengths beyond training
- Position 1000 and position 1001 have very different absolute encodings despite being adjacent
3. Rotary Position Embedding (RoPE)
RoPE is the dominant position encoding in modern LLMs (LLaMA, Qwen, Mistral, DeepSeek). It encodes relative position directly into the attention score computation.
Core Idea
Instead of adding position to embeddings, RoPE rotates query and key vectors by a position-dependent angle:
Where is a rotation matrix at position . The attention score becomes:
The score depends only on the relative distance , not the absolute positions.
Why this works mathematically: Rotation matrices have the property . So the product of the two rotations collapses into a single rotation by the difference. This means two tokens at positions (100, 105) produce the same attention score as tokens at (0, 5)
same relative distance, same score.
Implementation
RoPE operates on pairs of dimensions. For a -dimensional vector, group dimensions into pairs, each rotated by angle :
def apply_rope(x, positions):
"""Apply RoPE to query or key tensor.
x: (B, Nh, S, dk), positions: (S,)
"""
d = x.shape[-1]
freqs = 1.0 / (10000 ** (torch.arange(0, d, 2).float() / d)) # (dk/2,)
angles = positions.unsqueeze(-1) * freqs.unsqueeze(0) # (S, dk/2)
cos = angles.cos() # (S, dk/2)
sin = angles.sin() # (S, dk/2)
x1, x2 = x[..., ::2], x[..., 1::2] # split even/odd dimensions
out_even = x1 * cos - x2 * sin
out_odd = x1 * sin + x2 * cos
return torch.stack([out_even, out_odd], dim=-1).flatten(-2)
Infrastructure implication: RoPE is applied to Q and K after projection but before the attention score computation. Itβs typically fused into the attention kernel for efficiency β separate application would add an extra memory read/write pass.
4. ALiBi (Attention with Linear Biases)
ALiBi adds a position-dependent bias directly to attention scores instead of modifying embeddings:
Where is a head-specific slope. Closer tokens get higher scores; distant tokens are penalized linearly.
Advantages: No parameters, trivially extends to any sequence length. Disadvantage: The linear penalty may be too aggressive for some tasks. Used by BLOOM and some earlier models, but largely superseded by RoPE.
5. Long-Context Extension
Training with a 4K context but serving at 128K requires extending position encoding. Key techniques:
Position Interpolation
Scale positions to fit within the training range:
Requires a small amount of fine-tuning to adapt. Used by Code LLaMA (16K β 100K).
NTK-aware Scaling
Instead of uniformly scaling all frequencies, adjust the base:
This preserves high-frequency (local position) information while extending low-frequency (long-range) capacity.
YaRN
Combines NTK scaling with an attention temperature factor, achieving better quality on long contexts. Used by Qwen and other recent models.
Infrastructure Impact
Long-context extension is critical for AI infra:
- KV cache scales linearly with context: 128K context = 32Γ the KV cache of 4K
- Attention compute scales quadratically: 128K context = 1024Γ the attention FLOPs of 4K
- RoPE computation must support dynamic context lengths at serving time
Self-Check
- Explain why self-attention needs positional encoding
- Explain the difference between absolute and relative positional encoding
- Explain how RoPE makes attention scores depend on relative distance
- Implement basic RoPE in PyTorch
- Explain Position Interpolation and NTK scaling for long-context extension
- Explain the infrastructure impact of long context (KV cache, attention FLOPs)