Why Is Positional Information Still Necessary?
In transformer-based LLMs, token embeddings alone cannot express order information. "The red fox jumps over the gray fox" and "The gray fox jumps over the red fox" have the same set of tokens but completely different meanings. As pointed out in the original text, the causal mask provides implicit order information by restricting reference to previous tokens, but it cannot distinguish between "tokens one space apart" and "tokens ten spaces apart." Therefore, position embedding is essential to explicitly inject this distance information.
Causal masks only determine which tokens are accessible and do not distinguish between positions. Thus, explicit positional methods are still useful or essential.
Absolute Position Embedding: The GPT-2 Method
GPT-2 adds learned absolute position vectors to token embeddings and passes them to the first transformer block. It retrieves the embedding $\mathbf{e}_p$ for position $p$ from a lookup table and adds it to the token embedding $\mathbf{x}_p$.
$$ \mathbf{h}_p = \mathbf{x}_p + \mathbf{e}_p $$
This method is simple to implement, but there is an extrapolation problem where performance degrades due to the absence of position embeddings when inferring with sequence lengths not seen during training.
How Relative Position Embedding and RoPE Work
Relative position embedding adds a bias to the attention scores that depends only on the distance $i-j$ between tokens. RoPE implements this through rotation operations, rotating the query and key vectors according to their positions. The key idea is to use rotation in the complex plane.
RoPE Formula
The dimension $d$ is divided into $d/2$ pairs, and for each pair, a rotation angle $\theta_p = p \cdot \theta_k$ is applied based on position $p$.
$$ \text{RoPE}(\mathbf{q}_p, p) = \mathbf{q}_p \odot \cos(\theta_p) + \text{rotate}(\mathbf{q}_p) \odot \sin(\theta_p) $$
This operation is applied equally to queries and keys, and has the property of relying only on the relative distance $p-q$ during attention score calculation.
| Feature | Absolute Position Embedding (GPT-2) | RoPE (LLaMA, MiniMax-M3, etc.) |
|---|---|---|
| Injection Point | Immediately after embedding layer (once) | Inside all attention layers (repeated) |
| Distance Representation | Absolute position $p$ | Relative distance $p-q$ |
| Extrapolation Performance | Limited | Excellent (utilizes rotation periodicity) |
| Implementation Complexity | Low | Medium (requires rotation matrix) |
Positional Encoding Choices in Latest Architectures (2026 Preview)
The original text previews the choices of major open-weight models in 2026 in Section 6. The main trends are as follows:
- Dominance of RoPE Series: Most of the latest models, including LLaMA series, MiniMax-M3, Qwen, and DeepSeek, adopt RoPE or its variants (YaRN, LongRoPE, etc.)
- ALiBi: Some models use ALiBi (Attention with Linear Biases), an attention bias method.
- NoPE Experiments: Research is ongoing to completely remove position embeddings and learn solely with causal masks.
| RoPE | 70 |
| ALiBi | 15 |
| NoPE | 10 |
| Others | 5 |
Key Points for PyTorch Implementation
The minimal implementation provided in the original text consists of the following steps:
1. Frequency Basis Calculation: $\theta_k = 10000^{-2k/d}$ for $k=0,\dots,d/2-1$ 2. Generate Rotation Matrices by Position: Pre-calculate $\cos(\theta_p), \sin(\theta_p)$ 3. Apply Query/Key Rotation: Perform rotation operations for odd/even dimension pairs 4. Attention Calculation: Conduct standard scaled dot-product attention with rotated queries/keys
```python def precompute_freqs_cis(dim: int, end: int, theta: float = 10000.0): freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim)) t = torch.arange(end, device=freqs.device) freqs = torch.outer(t, freqs) return torch.polar(torch.ones_like(freqs), freqs) # complex64 ```
This implementation efficiently handles rotations using complex operations.
Practical Implications and Remaining Challenges
While RoPE has effectively become the standard, issues with rotation periodicity breaking in ultra-long contexts (128K+ tokens) have been reported. To address this, extension techniques such as YaRN (Yet another RoPE extensioN), LongRoPE, and PI (Position Interpolation) are actively being researched. Additionally, approaches like NoPE, which omit position embeddings and learn order through data and model scale, are gaining attention.
Developers should verify whether RoPE variant support aligns with context window requirements when selecting models, and assess the impact of freezing position embedding parameters during fine-tuning on performance.