How does a transformer know token order? Compare learned/sinusoidal embeddings, RoPE and ALiBi, and explain how a model trained at 8k context can be stretched to 128k.
Answer
Model answer, written in a candidate's voice
In short
- Attention alone is permutation-invariant, so position must be injected
- Absolute learned/sinusoidal embeddings add position vectors; sinusoidal is fixed, learned is trained but length-limited
- RoPE rotates query/key pairs by position-dependent angles so dot products depend on relative offset; ALiBi adds a distance-proportional logit bias
- Stretch 8k to 128k via position interpolation or NTK/YaRN-style frequency rescaling, then long-context fine-tuning
- Validate with needle-style retrieval and realistic long-document tasks; effective context is often shorter than advertised
- Cost: KV cache grows with context and full attention compute grows roughly quadratically
In full
A plain attention layer is permutation-invariant, so order has to be injected. Absolute schemes add a position vector to each token: sinusoidal is fixed and deterministic, while learned embeddings are trained but tied to the maximum length seen. Relative schemes bake order into attention: RoPE rotates query and key vectors by position-dependent angles so their dot product depends on the relative offset, while ALiBi keeps embeddings position-free and adds a distance-proportional bias to logits. To stretch an 8k-trained model towards 128k, you rescale positions or RoPE frequencies, such as position interpolation or NTK-aware/YaRN-style rescaling, and then fine-tune on long-context data; rescaling alone does not create capability. I would validate with needle-style retrieval plus realistic long-document tasks, because effective context is often shorter than the advertised window. The trade-off is cost: KV cache grows with context and full attention compute grows roughly quadratically, so long context is not free.
What a strong answer covers
- Attention alone is permutation-invariant, so position must be injected
- Absolute (learned/sinusoidal) vs relative schemes; RoPE rotates query/key pairs by position-dependent angles so their dot product depends on relative offset; ALiBi adds a distance-proportional bias
- Extension: position interpolation, NTK-aware / YaRN frequency rescaling, plus a phase of long-context fine-tuning
- Validation: needle-style retrieval and realistic long-document tasks; effective context is often shorter than advertised
- Cost: cache and attention compute grow with context
Common mistakes
- Treating extension as free without further training or evaluation
- using needle-in-a-haystack as proof of long-context reasoning