Understanding Positional Embeddings in Modern LLMs
Sebastian Raschka's technical walkthrough explains how LLMs encode token positions, comparing classic absolute embeddings and modern rotary position embeddings (RoPE) with math and PyTorch code.
read it Read it for a clear, step-by-step breakdown of how positional encodings work from absolute embeddings to RoPE, backed by concrete math and PyTorch code.
summary
Sebastian Raschka breaks down how large language models handle token position information, spanning both classic and contemporary approaches. The post walks through learned absolute embeddings in GPT-2 and fixed sinusoidal encodings from the original Transformer before diving deep into Rotary Position Embedding (RoPE). It illustrates the mechanics using visual examples, equations, and small PyTorch snippets, concluding with a look at open-weight model choices.
key points
- GPT-2 uses learned absolute position embedding tables added directly to token embeddings, which scale poorly for long context windows.
- The original Transformer used fixed sinusoidal functions designed to make relative position shifts representable as linear transformations.
- RoPE (Rotary Position Embedding) injects position information directly into attention by rotating query and key feature pairs based on their positions.
who should read it
ML engineers and students looking for a grounded, code-backed explanation of transformer positional encodings.
discussion positive
consensus
Commenters note that while the post covers implementation well, true theoretical understanding requires abstract algebra and group theory, though many point out that ML research often relies on empirical trial-and-error rather than pure theory.
pushback
A few commenters pushed back on overly abstract group-representation explanations as post-facto rationalizations for empirical hacks.
This article is only about understanding the implementation of positional embeddings, but to really understand positional embeddings you have to understand the theory. Positional embeddings are group representations. This is how you find them, and this is also how you parameterize them to make them differentiable/learnable. To give a few examples:
1. Rotary positional embeddings are representations of the translation group. The n-dimensional translation group ℝⁿ is abelian, so its irreducible representations are all one-dimensional ρ: ℝⁿ → ℂ, specified by a momentum vector m: ρₘ(x) = …
It introduces the theoretical framing of positional embeddings as group representations, specifically the translation group for RoPE.
Generated by an LLM from the article and thread. Check the sources before relying on it.