Positional Embeddings in Transformer Models
Long-context finetuning extends a model's context length, meaning how many tokens it can handle at once.
Why a Longer Input Is Not Just More Text
Suppose a language model can only take in a certain number of tokens at once, and you want it to read much longer documents or code files. The obvious plan is to gather longer documents, feed them to the model during finetuning, and let training do the rest. This article explains why that plan is incomplete. Extending how much a model can read at once is a different kind of change from most finetuning, and it reaches into the model's architecture, specifically the positional embeddings that record where each token sits.
What do you think happens?
If you only feed longer documents during finetuning and leave the model's architecture untouched, does the model gain the ability to handle the longer context?
Reveal answer
No, the component that represents token positions must also be adjusted
The source explains that a longer sequence means more possible token positions. The positional embeddings must be able to cover those additional positions, and if the component was built only for shorter positions, feeding it longer inputs is not enough.
The Common First Guess About Longer Inputs
The first guess goes like this: feed the model longer documents during finetuning, let it practice on them, and it learns to read further. Under this picture, only the amount of text in each training example changes, and the model's internal machinery stays the same. The belief is understandable, because most finetuning works that way. You change the data, run training, and the model's behavior shifts. Long-context finetuning is different, and the difference matters for anyone who wants to extend a model's reach.
What Context Length Means
Context length is the number of tokens a model can take into account at once. Long-context finetuning is a technique for extending a model's context length, so the model can handle more tokens in a single input.
The defining feature of long-context finetuning is that it typically requires modifying the model's architecture rather than only changing the training data. The source names a specific part that is usually adjusted: the positional embeddings.
Positional Embeddings as a Record of Positions
Every token in a sequence occupies a position, and the model needs a way to represent where each token sits. Positional embeddings are the component of the architecture that handles these token positions. You can think of them as the model's record of where each token sits in the sequence. Each position in a short input has a slot in that record. When the input grows, more slots are needed.
The diagram is a simplified picture of the record. The first slots cover the positions the model was built for, and the added slots represent the wider range that extended context requires. The source does not describe a specific method for how the added positions are represented, so the diagram shows only that the record must grow to cover them.
Why Context Length Forces an Architecture Change
The three pieces are tied together. The context window size sets how many token positions a model must handle. The positional embeddings represent those positions. Token positions are what each token in an input occupies. A longer sequence means more possible token positions, so the positional embeddings must be able to cover those additional positions. If the component was built only for shorter positions, feeding it longer inputs is not enough. The component itself has to be adjusted so it can represent the wider range.
Code Llama's Extension from 4,096 to 16,384 Tokens
The source gives a concrete case. Code Llama models were created from the base model Llama 2 using different finetuning techniques. Using long-context finetuning, the developers increased the model's maximum context length from 4,096 tokens to 16,384 tokens so it could accommodate longer code files. Code files are a natural motivation, since a single source file or a set of related files can run far longer than a short question or a paragraph.
Reasoning through a context extension
A model's maximum context length is 4,096 tokens. The goal is to handle 16,384-token code files. Which parts of the model must be considered, and why is more training data alone insufficient?
- 1Identify the old and new limits
The maximum context length grows from 4,096 tokens to 16,384 tokens, so the number of token positions the model must represent grows as well.
- 2Locate the component that represents positions
The positional embeddings handle token positions. They were built for the original range, so they must be able to cover the additional positions.
- 3Check whether data changes reach that component
Longer training examples add text, but they do not by themselves change the component that represents positions. The architecture must be adjusted as well.
The extension requires adjusting the positional embeddings so they cover positions beyond the original 4,096, and longer finetuning data is only one part of the work.
Distinguishing Long-Context from Other Finetuning
The source places long-context finetuning alongside two other techniques, and the comparison clarifies what makes it distinct. Instruction finetuning is supervised finetuning on instruction data. Its responses can be open-ended, such as a book summary, or close-ended, such as a classification answer. The source notes that high-quality instruction data can be challenging and expensive to create, especially when instructions demand factual consistency, domain expertise, or political correctness.
Preference finetuning uses reinforcement learning to push the model toward responses that people prefer. It depends on comparative data, typically arranged as an instruction, a winning response, and a losing response. Long-context finetuning, by contrast, is aimed at a property of the model: how many tokens it can handle. It is not defined by the kind of data or the reward signal used during training. Neither instruction finetuning nor preference finetuning is defined by extending context length.
| Method | What it targets | Typical input | Defined by context length? |
|---|---|---|---|
| Long-context finetuning | Number of tokens the model can handle at once | Longer sequences, often with architecture changes | Yes |
| Instruction finetuning | Supervised behavior on instruction tasks | Instruction data with open or close-ended responses | No |
| Preference finetuning | Responses people prefer | Comparative data: instruction, winning response, losing response | No |
The three finetuning methods differ in what they aim to change
The Risk on Shorter Sequences
Extending context length is harder than many other finetuning techniques, and the resulting model might degrade on shorter sequences. Before extension, the model handles short inputs with the positional record it was built for. After extension, the same model is asked to represent a wider range of positions, and its behavior on short inputs is no longer guaranteed to stay the same. The source states the risk as a possibility, so the practical lesson is to check short-input behavior after a context extension rather than assume it is preserved.
Mistakes Beginners Make with Context Extension
Common mistake Treating long-context finetuning as only a data change
What it looks likeFeeding 16,384-token code files during finetuning while leaving the positional embeddings unchanged
Why it failsThe positional embeddings must be able to cover the additional positions, and longer data does not change that component on its own.
Do this insteadPlan an architecture adjustment, such as changing the positional embeddings, alongside the longer training data.
Common mistake Assuming shorter inputs are unaffected after extension
What it looks likeReleasing an extended model without rechecking how it handles short questions
Why it failsThe source warns that the resulting model might degrade on shorter sequences.
Do this insteadEvaluate short-input behavior after the extension, not only long-input behavior.
Common mistake Confusing context length with instruction or preference training
What it looks likeCalling a model with a larger context window preference-finetuned because it was trained on comparative data
Why it failsLong-context finetuning is aimed at how many tokens the model can handle. Instruction and preference finetuning are defined by data format and training goals, not by context length.
Do this insteadName the method by what it changes: context length for long-context finetuning, instruction data for instruction finetuning, and preferred responses for preference finetuning.
Checking Your Understanding of Context Extension
A team has a model that accepts up to 4,096 tokens. They want it to read 16,384-token code files. Their plan is to collect longer code files, finetune on them, and leave the model's architecture unchanged. Explain what part of the model is likely to limit this plan, and what extra step the team should take before and after training.
Hints
- Ask what a longer sequence means for the number of token positions.
- Recall which component the source says handles token positions and what must happen to it.
- Think about whether the model's short-input behavior should be rechecked after the change.
Name the finetuning method that would be used to make a model accept longer inputs, and name the two other methods it differs from. For each, state the one thing it is defined by.
Hints
- Long-context finetuning is defined by context length.
- Instruction finetuning uses supervised training on instruction data.
- Preference finetuning uses reinforcement learning with comparative data.
Key Takeaways
- Long-context finetuning extends a model's context length, meaning how many tokens it can take into account at once.
- Extending context length typically requires modifying the architecture, especially the positional embeddings, because a longer sequence means more possible token positions that must be covered.
- Data changes alone are not enough, and a context-extended model might degrade on shorter sequences, so short-input behavior should be checked.
- Long-context finetuning is defined by context length, while instruction finetuning uses supervised training on instruction data and preference finetuning uses reinforcement learning on comparative data.