Check the RoPE relative property#
Long context with RoPE#
- base
- the RoPE frequency scaler. Increase it to extend context at inference.
- NTK-aware
- rescale
base to base * (scale_factor)^(d/(d-2)) when extending from 4K to 16K+. - YaRN
- per-dimension interpolation that preserves attention entropy. Llama 3.1 128K uses it.
- LongRoPE
- evolutionary search picks per-dimension scale factors. Phi-3-Long uses it.
- Position interpolation
- shrink positions by the extension factor. Fine-tune for 1–5B tokens.
- Result
- Llama 3 extended from 8K to 128K context with RoPE scaling.
Config fields to read#
| Command | What it does |
|---|
rope_theta | RoPE base frequency |
|---|
max_position_embeddings | Training context length |
|---|
model.config.rope_scaling | Scaling type and factor, for example yarn |
|---|
"position": "rope-yarn" | Qwen 2.5 72B in the calculator |
|---|
"position": "rope-sliding" | Gemma 2 9B, RoPE plus sliding layers |
|---|
"position": "learned" | GPT-2 Small, 1024 positions |
|---|
sliding_window | Last W tokens for sliding layers |
|---|
layer_types | Which layers are full or sliding |
|---|
Troubleshooting#
| Symptom | Cause | Fix |
|---|
seq_len <n> exceeds max_context_length <L> | Input longer than the table | Raise max_context_length |
|---|
d_model must be even for sinusoidal | Odd d_model | Use an even d_model |
|---|
token and positional embeddings must share d_model | Two widths | Build both with one d_model |
|---|
| Output the same for shuffled tokens | No position signal | Add a position scheme |
|---|
| Learned model fails past trained length | The row does not exist | Use RoPE with scaling |
|---|
Sinusoidal fails beyond max_len | No training at those positions | Use RoPE or ALiBi |
|---|
| Same gap, different RoPE scores | Rotation on Q or K only | Rotate both with apply_rope |
|---|
| Long context degrades with RoPE | Phase wrap at small base | Raise base, try YaRN |
|---|
Use X when#
- Use RoPE for a new decoder model. Every Llama, Mistral, Qwen, DeepSeek, and Gemma uses RoPE.
- Use RoPE + YaRN when you extend context and can pay a fine-tune stage.
- Use ALiBi when length extrapolation matters most and you want no embeddings.
- Use sinusoidal when you want a parameter-free input table. It is historical for frontier models.
- Use learned absolute only with a fixed maximum context, as in GPT-2.
- Apply RoPE and ALiBi at the attention step, not at the input.
Questions#
- Why does attention need position?
- It is order-blind. Shuffled rows of
X give shuffled output rows. - Why is RoPE relative?
- The dot product picks up a cos of the gap times theta on each pair.
- Sum or concatenate?
- Sum. It keeps
D constant through the network. - Parameter cost?
- Learned adds
max_context_length * D. Sinusoidal adds zero. - Why did RoPE win?
- No architecture change, relative position, and a
base knob for long context. - Learned vs sinusoidal at init?
- Learned neighbours look random. Sinusoidal decays smoothly.