Positional Encoding
As of 2026-11-14
What is Positional Encoding?
RoPE encodes relative position directly into the attention dot product, and its base θ sets how slowly the slowest dimensions rotate — but θ_base is not the whole story: Llama 3 used θ=500,000 with an 8K context, and Llama 3.1/3.3 reach 128K only with rope_scaling plus long-context training.
Positional encoding tells a transformer where each token sits in a sequence. Self-attention on its own is order-blind: without a position signal, "the client escalated after the invoice" and "the invoice escalated after the client" produce the same set of attention scores. Any LLM-based workflow that must track that a payment term applies after a contract amendment, not before it, depends on the positional scheme working at the context lengths the deployment actually uses.
Four schemes and how they behave at length
Sinusoidal encoding, from the original Transformer, adds fixed sine and cosine waves to each token embedding; their wavelengths form a geometric progression from 2π to 10,000·2π tokens. The authors chose it over learned position embeddings — which gave nearly identical results — because it might let the model extrapolate to longer sequences (Vaswani et al., 2017). Learned absolute embeddings look up one trained vector per position index, so a position beyond the trained maximum has no representation at all.
Why it matters
- Without positional encoding, self-attention is order-blind: 'Q2 revenue decreased 15%' and 'revenue decreased Q2 15%' would give the same attention scores inside an LLM reasoning chain.
- Learned absolute embeddings have no representation beyond the trained maximum, and sinusoidal encoding did not deliver the extrapolation it was designed for — RoPE with documented long-context training is the baseline for enterprise documents.
- A config that reads rope_theta 500000 can still describe an 8K model (Llama 3): procurement must read θ, rope_scaling and the documented training length together, then test at the target length.
Key points
- RoPE rotates query and key dimension pairs by an angle proportional to position, so the query–key dot product depends only on the distance between two tokens (Su et al., 2021).
- θ=500,000 alone gave Llama 3 an 8K context; Llama 3.1/3.3 reach 128K only by adding rope_scaling (llama3 type, factor 8) and long-context training. Confirm rope_theta, rope_scaling and the original pre-training length together before accepting a vendor's stated context length — then test at the target length.
- ALiBi subtracts a distance-proportional penalty from each attention score, with one fixed slope per head from a geometric sequence; it has no learned position parameters. Consider it when document length is truly unbounded and you train your own model; it saves no memory over RoPE, which also has no parameters and the same KV cache.
- The YaRN paper extended Llama 2 from 4K to 128K tokens with a few hundred fine-tuning steps (400 steps to 64K, 200 more to 128K); Llama 3.1's 128K window itself comes from rope_scaling of a model pre-trained at 8K plus about 800B tokens of long-context pre-training.
- Sinusoidal encoding (Vaswani et al., 2017) and learned absolute embeddings are the original baselines; neither should be selected for a deployment that must handle documents longer than the training maximum.
- In current open-weight families: Llama 3.x θ=500,000; Mistral 7B v0.2 θ=1,000,000 with a 32K window; Gemma 2 θ=10,000 with 4,096-token sliding-window layers and an 8K window. BLOOM uses ALiBi.
- Llama 3.1 rope_scaling, as implemented in Hugging Face: pairs with wavelength above 8,192 tokens slowed by 8, below 2,048 untouched, in between blended — 29 / 29 / 6 of the 64 pairs at θ=500,000. Since 8 × 8,192 = 65,536, the factor alone does not explain 131,072.
- Position matters even inside a properly trained window: retrieval accuracy is often highest at the start and end of the input and lower in the middle (Liu et al., 2023). Put decisive content at the edges.
- Each RoPE dimension pair rotates at its own frequency: the fastest completes a turn every 2π ≈ 6.3 tokens, the slowest every 2π·θ^(126/128) tokens for head dimension 128 — about 54,000 tokens at θ=10,000 and about 2.56 million at θ=500,000.
- Position Interpolation linearly down-scales positions and extended LLaMA from 2K to up to 32K tokens with at most 1,000 fine-tuning steps; YaRN interpolates per frequency band and adds an attention temperature, needing 10× fewer tokens and 2.5× fewer steps than earlier methods.
Terms used on this page
- RoPE (Rotary Position Embedding)
- Su et al. 2021: rotates each pair of query and key dimensions by a position-dependent angle, so the attention dot product encodes the relative distance between tokens. Used by the Llama, Mistral and Gemma families.
- θ_base
- RoPE base (rope_theta in config.json) from which each dimension pair's rotation frequency is derived; a higher value slows the slowest pairs. Llama 3.x uses 500,000. It is one input to context length, not a measure of it.
- ALiBi (Attention with Linear Biases)
- Press et al.: subtracts from each attention score a penalty proportional to query–key distance, with a fixed slope per head from a geometric sequence. No learned position parameters; designed to extrapolate beyond the training length.
- YaRN (Yet another RoPE extensioN)
- Peng et al. 2023: fine-tuning method that interpolates RoPE per frequency band and adds an attention temperature; extended Llama 2 from 4K to 128K with 10× fewer tokens and 2.5× fewer steps than earlier methods.
- NTK-aware / NTK-by-parts interpolation
- Precursors that YaRN builds on: rather than compressing every RoPE frequency equally, they scale low-frequency dimension pairs and keep high-frequency pairs, preserving short-range resolution.
- Length generalisation
- The ability to keep coherent attention on sequences longer than those seen in training. ALiBi was designed for it; RoPE models usually reach long windows through context extension and long-context training.
- Sinusoidal encoding
- Vaswani et al. 2017: fixed sine and cosine waves added to token embeddings, wavelengths from 2π to 10,000·2π. Chosen over learned embeddings for possible extrapolation; a baseline today.
- Learned absolute embedding
- One trained vector per position index. A position beyond the trained maximum has no representation, so it cannot generalise past that length.
Sources
- Su et al. 2021 — RoFormer: Enhanced Transformer with Rotary Position Embedding
- Press et al. — Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation (ALiBi)
- Peng et al. 2023 — YaRN: Efficient Context Window Extension of Large Language Models
- Vaswani et al. 2017 — Attention Is All You Need (sinusoidal encoding baseline)
- Llama Team, Meta 2024 — The Llama 3 Herd of Models (RoPE base 500,000; long-context pre-training 8K → 128K)
- Liu et al. 2023 — Lost in the Middle: How Language Models Use Long Contexts
- Chen et al. 2023 — Extending Context Window of Large Language Models via Positional Interpolation
- Meta — Llama 3 70B model card and config (rope_theta 500000, 8,192-token context)
- Meta — Llama 3.1 70B model card and config (rope_scaling llama3, factor 8, 131,072 positions)
- Hugging Face Transformers — RoPE utilities (llama3 frequency scaling)
- SAP AI Core documentation — Grounding (generative AI hub orchestration)
- Mistral AI — Mistral 7B Instruct v0.2 model card (rope_theta 1,000,000, 32K)
- Gemma Team — Gemma 2 technical report, arXiv:2408.00118 (2024)
Full card with the Consultant plan — €19.90/month. What the full card adds: the full decision framework · the SAP vs Snowflake / Databricks / Fabric comparison · the common pitfalls and their fix · the cheat sheet · the architecture schemas · the code blocks · the facts worth quoting.