Stretching Positions: Context Extension

Lesson 4 of 4 in Positional Encoding: Teaching Order to a Set.

A model was trained on sequences up to 4,096 tokens. You have a 30,000-token contract to analyze. Why not just feed it in?

Because the position scheme is the Context window. With a learned table, the answer is brutal: there is no row for position 4,097, full stop. With RoPE the failure is subtler and more interesting: the formula happily computes rotations for position 30,000 — but the model has never seen them. During training, every attention pattern was learned over rotation angles and offsets inside the 4,096 range. Past it, queries and keys meet in configurations that are out of distribution, and quality does not degrade gracefully — perplexity spikes and retrieval of early-context facts collapses.

The word for what goes wrong is extrapolation. And the key insight behind Context extension is that models are terrible at extrapolating positions but surprisingly good at interpolating them: squeezed between trained positions is a far safer place to be than beyond them.

That insight became position interpolation (Chen et al., 2023): instead of letting a 30,000-token document run past the trained range, rescale every position index down — multiply by 4,096/30,000 — so the whole document maps into the range the model knows. Every token now sits at a fractional position between trained ones; resolution drops (neighboring tokens sit closer together in position-space), but nothing is out of distribution. A comparatively short fine-tune at the new scale recovers most of the lost sharpness — orders of magnitude cheaper than pre-training at the longer length from scratch.

A family of refinements followed, all turning the same dial with more finesse: instead of rescaling all rotation frequencies uniformly, NTK-aware and YaRN-style RoPE scaling stretch the slow frequencies (which encode coarse, long-range position) more than the fast ones (which distinguish immediate neighbors), preserving local resolution while extending reach (YaRN: Peng et al., 2023, arXiv:2309.00071; NTK-aware scaling originated in community posts rather than a paper). The mechanics differ; the lesson is identical — context extension means changing the position scheme and then training the model to live with the change.

Separate from all of this are serving-time tricks — sliding-window attention over a long stream, keeping a few Attention sink tokens pinned at the start — which manage memory rather than truly extending what the model can relate across. What long contexts do to the KV cache, batching, latency, and your bill is the Inference & Serving domain’s story.

Roads to a longer context window — all of them run through the position scheme
What changesWhy it helpsWhat it costs

Naive extrapolation

Nothing — just feed longer sequences

It doesn’t: RoPE angles beyond the trained range are out of distribution (learned tables simply run out)

Quality collapses past the trained length; looks fine in a demo, fails on real documents

Position interpolation

Position indices rescaled so the long sequence maps into the trained range, plus a short fine-tune

Interpolating between seen positions keeps attention in distribution

Lower position resolution; a fine-tuning run; still bounded by how far you can squeeze

Frequency-aware RoPE scaling (NTK-aware, YaRN family)

RoPE frequencies rescaled non-uniformly — slow dimensions stretched more than fast ones — usually plus fine-tuning

Preserves local ordering sharpness while extending long-range reach

More knobs that must match between training and serving; still needs long-context training data to shine

Train long from the start

Pre-training (or a late pre-training stage) on long sequences

No distribution mismatch at all — the model natively knows the range

Attention compute grows steeply with length; by far the most expensive road

In production

When a cloud catalog lists a longer-context variant of a model, what you are being offered is a different position-scheme-plus-training package — and the physics of longer positions follows you onto every platform.

AWS

Whether you consume models through Amazon Bedrock or host your own on SageMaker or GPU instances, the context window arrived baked into the checkpoint — the service exposes it, it does not create it. Self-hosting makes this concrete: RoPE scaling settings in your serving stack must match what the checkpoint was fine-tuned with, or quality silently degrades at long range. And because the KV cache grows with every position you actually use, the window you enable drives memory per request, which drives instance sizing and cost.

Azure

In the Azure AI Foundry model catalog, variants of the same family with different context lengths are different model builds, not tiers of one switch. Choosing the longer one changes your latency and cost profile mechanically — prefill compute and KV-cache memory scale with the tokens you send — independent of any quota or deployment setting. For open weights deployed on Azure GPU compute, treat the advertised window as a claim to verify with long-range retrieval evals before you architect around it.

Google Cloud

On Vertex AI, managed long-context endpoints meter by token because that is where the real cost lives: a longer window multiplies the attention work and cache memory behind each call. If you serve your own models on GKE or GPU VMs, the position-scheme parameters (RoPE base, scaling factor) belong in your deployment manifest next to the model version — a checkpoint served with mismatched position settings is a quiet correctness bug, not a crash.

Interactive checkpoint quiz (1 questions) — open this page in a browser to take it.